Skip to content

Commit 05fc744

Browse files
committed
Add a new ereport auxiliary function errdetail_log(), which works the same as
errdetail except the string goes only to the server log, replacing the normal errdetail there. This provides a reasonably clean way of dealing with error details that are too security-sensitive or too bulky to send to the client. This commit just adds the infrastructure --- actual uses to follow.
1 parent 7feabcb commit 05fc744

File tree

5 files changed

+75
-9
lines changed

5 files changed

+75
-9
lines changed

doc/src/sgml/sources.sgml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/sources.sgml,v 2.29 2007/11/07 13:12:21 petere Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/sources.sgml,v 2.30 2008/03/24 18:08:47 tgl Exp $ -->
22

33
<chapter id="source">
44
<title>PostgreSQL Coding Conventions</title>
@@ -158,6 +158,17 @@ ereport(ERROR,
158158
<function>errmsg</>.
159159
</para>
160160
</listitem>
161+
<listitem>
162+
<para>
163+
<function>errdetail_log(const char *msg, ...)</function> is the same as
164+
<function>errdetail</> except that this string goes only to the server
165+
log, never to the client. If both <function>errdetail</> and
166+
<function>errdetail_log</> are used then one string goes to the client
167+
and the other to the log. This is useful for error details that are
168+
too security-sensitive or too bulky to include in the report
169+
sent to the client.
170+
</para>
171+
</listitem>
161172
<listitem>
162173
<para>
163174
<function>errhint(const char *msg, ...)</function> supplies an optional

src/backend/nls.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# $PostgreSQL: pgsql/src/backend/nls.mk,v 1.21 2008/01/30 11:05:37 petere Exp $
1+
# $PostgreSQL: pgsql/src/backend/nls.mk,v 1.22 2008/03/24 18:08:47 tgl Exp $
22
CATALOG_NAME := postgres
33
AVAIL_LANGUAGES := af cs de es fr hr hu it ko nb nl pt_BR ro ru sk sl sv tr zh_CN zh_TW
44
GETTEXT_FILES := + gettext-files
55
# you can add "elog:2" and "errmsg_internal" to this list if you want to
66
# include internal messages in the translation list.
7-
GETTEXT_TRIGGERS:= _ errmsg errdetail errhint errcontext write_stderr yyerror
7+
GETTEXT_TRIGGERS:= _ errmsg errdetail errdetail_log errhint errcontext write_stderr yyerror
88

99
gettext-files: distprep
1010
find $(srcdir)/ $(srcdir)/../port/ -name '*.c' -print >$@

src/backend/port/ipc_test.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
*
2323
* IDENTIFICATION
24-
* $PostgreSQL: pgsql/src/backend/port/ipc_test.c,v 1.23 2008/01/01 19:45:51 momjian Exp $
24+
* $PostgreSQL: pgsql/src/backend/port/ipc_test.c,v 1.24 2008/03/24 18:08:47 tgl Exp $
2525
*
2626
*-------------------------------------------------------------------------
2727
*/
@@ -185,6 +185,13 @@ errdetail(const char *fmt,...)
185185
return 0; /* return value does not matter */
186186
}
187187

188+
int
189+
errdetail_log(const char *fmt,...)
190+
{
191+
fprintf(stderr, "DETAIL: %s\n", fmt);
192+
return 0; /* return value does not matter */
193+
}
194+
188195
int
189196
errhint(const char *fmt,...)
190197
{

src/backend/utils/error/elog.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*
4343
*
4444
* IDENTIFICATION
45-
* $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.202 2008/03/10 12:55:13 mha Exp $
45+
* $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.203 2008/03/24 18:08:47 tgl Exp $
4646
*
4747
*-------------------------------------------------------------------------
4848
*/
@@ -382,6 +382,8 @@ errfinish(int dummy,...)
382382
pfree(edata->message);
383383
if (edata->detail)
384384
pfree(edata->detail);
385+
if (edata->detail_log)
386+
pfree(edata->detail_log);
385387
if (edata->hint)
386388
pfree(edata->hint);
387389
if (edata->context)
@@ -700,6 +702,27 @@ errdetail(const char *fmt,...)
700702
}
701703

702704

705+
/*
706+
* errdetail_log --- add a detail_log error message text to the current error
707+
*/
708+
int
709+
errdetail_log(const char *fmt,...)
710+
{
711+
ErrorData *edata = &errordata[errordata_stack_depth];
712+
MemoryContext oldcontext;
713+
714+
recursion_depth++;
715+
CHECK_STACK_DEPTH();
716+
oldcontext = MemoryContextSwitchTo(ErrorContext);
717+
718+
EVALUATE_MESSAGE(detail_log, false);
719+
720+
MemoryContextSwitchTo(oldcontext);
721+
recursion_depth--;
722+
return 0; /* return value does not matter */
723+
}
724+
725+
703726
/*
704727
* errhint --- add a hint error message text to the current error
705728
*/
@@ -1010,6 +1033,8 @@ CopyErrorData(void)
10101033
newedata->message = pstrdup(newedata->message);
10111034
if (newedata->detail)
10121035
newedata->detail = pstrdup(newedata->detail);
1036+
if (newedata->detail_log)
1037+
newedata->detail_log = pstrdup(newedata->detail_log);
10131038
if (newedata->hint)
10141039
newedata->hint = pstrdup(newedata->hint);
10151040
if (newedata->context)
@@ -1033,6 +1058,8 @@ FreeErrorData(ErrorData *edata)
10331058
pfree(edata->message);
10341059
if (edata->detail)
10351060
pfree(edata->detail);
1061+
if (edata->detail_log)
1062+
pfree(edata->detail_log);
10361063
if (edata->hint)
10371064
pfree(edata->hint);
10381065
if (edata->context)
@@ -1103,6 +1130,8 @@ ReThrowError(ErrorData *edata)
11031130
newedata->message = pstrdup(newedata->message);
11041131
if (newedata->detail)
11051132
newedata->detail = pstrdup(newedata->detail);
1133+
if (newedata->detail_log)
1134+
newedata->detail_log = pstrdup(newedata->detail_log);
11061135
if (newedata->hint)
11071136
newedata->hint = pstrdup(newedata->hint);
11081137
if (newedata->context)
@@ -1790,8 +1819,11 @@ write_csvlog(ErrorData *edata)
17901819
appendCSVLiteral(&buf, edata->message);
17911820
appendStringInfoCharMacro(&buf, ',');
17921821

1793-
/* errdetail */
1794-
appendCSVLiteral(&buf, edata->detail);
1822+
/* errdetail or errdetail_log */
1823+
if (edata->detail_log)
1824+
appendCSVLiteral(&buf, edata->detail_log);
1825+
else
1826+
appendCSVLiteral(&buf, edata->detail);
17951827
appendStringInfoCharMacro(&buf, ',');
17961828

17971829
/* errhint */
@@ -1907,7 +1939,14 @@ send_message_to_server_log(ErrorData *edata)
19071939

19081940
if (Log_error_verbosity >= PGERROR_DEFAULT)
19091941
{
1910-
if (edata->detail)
1942+
if (edata->detail_log)
1943+
{
1944+
log_line_prefix(&buf);
1945+
appendStringInfoString(&buf, _("DETAIL: "));
1946+
append_with_tabs(&buf, edata->detail_log);
1947+
appendStringInfoChar(&buf, '\n');
1948+
}
1949+
else if (edata->detail)
19111950
{
19121951
log_line_prefix(&buf);
19131952
appendStringInfoString(&buf, _("DETAIL: "));
@@ -2157,6 +2196,8 @@ send_message_to_frontend(ErrorData *edata)
21572196
pq_sendstring(&msgbuf, edata->detail);
21582197
}
21592198

2199+
/* detail_log is intentionally not used here */
2200+
21602201
if (edata->hint)
21612202
{
21622203
pq_sendbyte(&msgbuf, PG_DIAG_MESSAGE_HINT);

src/include/utils/elog.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/elog.h,v 1.91 2008/03/10 12:55:13 mha Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/elog.h,v 1.92 2008/03/24 18:08:47 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -125,6 +125,12 @@ errdetail(const char *fmt,...)
125125
the supplied arguments. */
126126
__attribute__((format(printf, 1, 2)));
127127

128+
extern int
129+
errdetail_log(const char *fmt,...)
130+
/* This extension allows gcc to check the format string for consistency with
131+
the supplied arguments. */
132+
__attribute__((format(printf, 1, 2)));
133+
128134
extern int
129135
errhint(const char *fmt,...)
130136
/* This extension allows gcc to check the format string for consistency with
@@ -258,6 +264,7 @@ typedef struct ErrorData
258264
int sqlerrcode; /* encoded ERRSTATE */
259265
char *message; /* primary error message */
260266
char *detail; /* detail error message */
267+
char *detail_log; /* detail error message for server log only */
261268
char *hint; /* hint message */
262269
char *context; /* context message */
263270
int cursorpos; /* cursor index into query string */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy