Skip to content

Commit 9fd842c

Browse files
committed
Add GUC variable to print original query to the server logs when there
is an error, warning, etc. Gavin Sherry
1 parent 6a8babc commit 9fd842c

File tree

7 files changed

+139
-96
lines changed

7 files changed

+139
-96
lines changed

doc/src/sgml/runtime.sgml

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.132 2002/09/01 23:26:06 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.133 2002/09/02 05:42:54 momjian Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -942,7 +942,6 @@ env PGOPTIONS='-c geqo=off' psql
942942
</para>
943943
</listitem>
944944
</varlistentry>
945-
946945
<varlistentry>
947946
<term><varname>EXPLAIN_PRETTY_PRINT</varname> (<type>boolean</type>)</term>
948947
<listitem>
@@ -978,6 +977,28 @@ env PGOPTIONS='-c geqo=off' psql
978977
</listitem>
979978
</varlistentry>
980979

980+
<varlistentry>
981+
<term><varname>LOG_MIN_ERROR_STATEMENT</varname> (<type>string</type>)</term>
982+
<listitem>
983+
<para>
984+
This controls which log messages are accompanied by the original
985+
query which generated the message. All queries matching the setting
986+
or which are of a higher severity than the setting are logged. The
987+
default is <literal>ERROR</literal>. Valid values are
988+
<literal>DEBUG5</literal>, <literal>DEBUG4</literal>,
989+
<literal>DEBUG3</literal>, <literal>DEBUG2</literal>,
990+
<literal>DEBUG1</literal>, <literal>INFO</literal>,
991+
<literal>NOTICE</literal>, <literal>WARNING</literal>
992+
and <literal>ERROR</literal>.
993+
</para>
994+
<para>
995+
It is recommended you enable <literal>LOG_PID</literal> as well
996+
so you can more easily match the error statement with the error
997+
message.
998+
</para>
999+
</listitem>
1000+
</varlistentry>
1001+
9811002
<varlistentry>
9821003
<term><varname>LOG_PID</varname> (<type>boolean</type>)</term>
9831004
<listitem>
@@ -1005,8 +1026,8 @@ env PGOPTIONS='-c geqo=off' psql
10051026
<listitem>
10061027
<para>
10071028
Prints the duration of every completed query. To use this option,
1008-
enable LOG_STATEMENT and LOG_PID so you can link the original query
1009-
to the duration using the process id.
1029+
enable <literal>LOG_STATEMENT</> and <literal>LOG_PID</> so you
1030+
can link the original query to the duration using the process id.
10101031
</para>
10111032
</listitem>
10121033
</varlistentry>

src/backend/utils/error/elog.c

Lines changed: 11 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.101 2002/09/02 02:47:05 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.102 2002/09/02 05:42:54 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -33,17 +33,10 @@
3333
#include "storage/proc.h"
3434
#include "tcop/tcopprot.h"
3535
#include "utils/memutils.h"
36+
#include "utils/guc.h"
3637

3738
#include "mb/pg_wchar.h"
3839

39-
int server_min_messages;
40-
char *server_min_messages_str = NULL;
41-
const char server_min_messages_str_default[] = "notice";
42-
43-
int client_min_messages;
44-
char *client_min_messages_str = NULL;
45-
const char client_min_messages_str_default[] = "notice";
46-
4740
#ifdef HAVE_SYSLOG
4841
/*
4942
* 0 = only stdout/stderr
@@ -345,6 +338,7 @@ elog(int lev, const char *fmt,...)
345338
}
346339
}
347340

341+
348342
/*
349343
* Message prepared; send it where it should go
350344
*/
@@ -433,6 +427,14 @@ elog(int lev, const char *fmt,...)
433427
if (msg_buf != msg_fixedbuf)
434428
free(msg_buf);
435429

430+
/* If the user wants this elog() generating query logged,
431+
* do so. We only want to log if the query has been
432+
* written to debug_query_string. Also, avoid infinite loops.
433+
*/
434+
435+
if(lev != LOG && lev >= log_min_error_statement && debug_query_string)
436+
elog(LOG,"statement: %s",debug_query_string);
437+
436438
/*
437439
* Perform error recovery action as specified by lev.
438440
*/
@@ -835,71 +837,4 @@ elog_message_prefix(int lev)
835837
}
836838

837839

838-
/*
839-
* GUC support routines
840-
*/
841-
const char *
842-
assign_server_min_messages(const char *newval,
843-
bool doit, bool interactive)
844-
{
845-
if (strcasecmp(newval, "debug") == 0)
846-
{ if (doit) server_min_messages = DEBUG1; }
847-
else if (strcasecmp(newval, "debug5") == 0)
848-
{ if (doit) server_min_messages = DEBUG5; }
849-
else if (strcasecmp(newval, "debug4") == 0)
850-
{ if (doit) server_min_messages = DEBUG4; }
851-
else if (strcasecmp(newval, "debug3") == 0)
852-
{ if (doit) server_min_messages = DEBUG3; }
853-
else if (strcasecmp(newval, "debug2") == 0)
854-
{ if (doit) server_min_messages = DEBUG2; }
855-
else if (strcasecmp(newval, "debug1") == 0)
856-
{ if (doit) server_min_messages = DEBUG1; }
857-
else if (strcasecmp(newval, "info") == 0)
858-
{ if (doit) server_min_messages = INFO; }
859-
else if (strcasecmp(newval, "notice") == 0)
860-
{ if (doit) server_min_messages = NOTICE; }
861-
else if (strcasecmp(newval, "warning") == 0)
862-
{ if (doit) server_min_messages = WARNING; }
863-
else if (strcasecmp(newval, "error") == 0)
864-
{ if (doit) server_min_messages = ERROR; }
865-
else if (strcasecmp(newval, "log") == 0)
866-
{ if (doit) server_min_messages = LOG; }
867-
else if (strcasecmp(newval, "fatal") == 0)
868-
{ if (doit) server_min_messages = FATAL; }
869-
else if (strcasecmp(newval, "panic") == 0)
870-
{ if (doit) server_min_messages = PANIC; }
871-
else
872-
return NULL; /* fail */
873-
return newval; /* OK */
874-
}
875840

876-
const char *
877-
assign_client_min_messages(const char *newval,
878-
bool doit, bool interactive)
879-
{
880-
if (strcasecmp(newval, "debug") == 0)
881-
{ if (doit) client_min_messages = DEBUG1; }
882-
else if (strcasecmp(newval, "debug5") == 0)
883-
{ if (doit) client_min_messages = DEBUG5; }
884-
else if (strcasecmp(newval, "debug4") == 0)
885-
{ if (doit) client_min_messages = DEBUG4; }
886-
else if (strcasecmp(newval, "debug3") == 0)
887-
{ if (doit) client_min_messages = DEBUG3; }
888-
else if (strcasecmp(newval, "debug2") == 0)
889-
{ if (doit) client_min_messages = DEBUG2; }
890-
else if (strcasecmp(newval, "debug1") == 0)
891-
{ if (doit) client_min_messages = DEBUG1; }
892-
else if (strcasecmp(newval, "log") == 0)
893-
{ if (doit) client_min_messages = LOG; }
894-
else if (strcasecmp(newval, "info") == 0)
895-
{ if (doit) client_min_messages = INFO; }
896-
else if (strcasecmp(newval, "notice") == 0)
897-
{ if (doit) client_min_messages = NOTICE; }
898-
else if (strcasecmp(newval, "warning") == 0)
899-
{ if (doit) client_min_messages = WARNING; }
900-
else if (strcasecmp(newval, "error") == 0)
901-
{ if (doit) client_min_messages = ERROR; }
902-
else
903-
return NULL; /* fail */
904-
return newval; /* OK */
905-
}

src/backend/utils/misc/guc.c

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* command, configuration file, and command line options.
66
* See src/backend/utils/misc/README for more information.
77
*
8-
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.91 2002/09/02 01:05:06 tgl Exp $
8+
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.92 2002/09/02 05:42:54 momjian Exp $
99
*
1010
* Copyright 2000 by PostgreSQL Global Development Group
1111
* Written by Peter Eisentraut <peter_e@gmx.net>.
@@ -71,6 +71,9 @@ static const char *assign_facility(const char *facility,
7171
bool doit, bool interactive);
7272
#endif
7373

74+
static const char *assign_msglvl(int *var, const char *newval,
75+
bool doit, bool interactive);
76+
7477
/*
7578
* Debugging options
7679
*/
@@ -99,6 +102,19 @@ bool Australian_timezones = false;
99102

100103
bool Password_encryption = true;
101104

105+
int log_min_error_statement;
106+
char *log_min_error_statement_str = NULL;
107+
const char log_min_error_statement_str_default[] = "error";
108+
109+
int server_min_messages;
110+
char *server_min_messages_str = NULL;
111+
const char server_min_messages_str_default[] = "notice";
112+
113+
int client_min_messages;
114+
char *client_min_messages_str = NULL;
115+
const char client_min_messages_str_default[] = "notice";
116+
117+
102118
#ifndef PG_KRB_SRVTAB
103119
#define PG_KRB_SRVTAB ""
104120
#endif
@@ -726,6 +742,11 @@ static struct config_string
726742
client_min_messages_str_default, assign_client_min_messages, NULL
727743
},
728744

745+
{
746+
{ "log_min_error_statement", PGC_USERSET }, &log_min_error_statement_str,
747+
log_min_error_statement_str_default, assign_min_error_statement, NULL
748+
},
749+
729750
{
730751
{ "DateStyle", PGC_USERSET, GUC_LIST_INPUT }, &datestyle_string,
731752
"ISO, US", assign_datestyle, show_datestyle
@@ -2877,3 +2898,54 @@ GUCArrayDelete(ArrayType *array, const char *name)
28772898

28782899
return newarray;
28792900
}
2901+
2902+
const char *
2903+
assign_server_min_messages(const char *newval,
2904+
bool doit, bool interactive)
2905+
{
2906+
return(assign_msglvl(&server_min_messages,newval,doit,interactive));
2907+
}
2908+
2909+
const char *
2910+
assign_client_min_messages(const char *newval,
2911+
bool doit, bool interactive)
2912+
{
2913+
return(assign_msglvl(&client_min_messages,newval,doit,interactive));
2914+
}
2915+
2916+
const char *
2917+
assign_min_error_statement(const char *newval, bool doit, bool interactive)
2918+
{
2919+
return(assign_msglvl(&log_min_error_statement,newval,doit,interactive));
2920+
}
2921+
2922+
static const char *
2923+
assign_msglvl(int *var, const char *newval, bool doit, bool interactive)
2924+
{
2925+
if (strcasecmp(newval, "debug") == 0)
2926+
{ if (doit) (*var) = DEBUG1; }
2927+
else if (strcasecmp(newval, "debug5") == 0)
2928+
{ if (doit) (*var) = DEBUG5; }
2929+
else if (strcasecmp(newval, "debug4") == 0)
2930+
{ if (doit) (*var) = DEBUG4; }
2931+
else if (strcasecmp(newval, "debug3") == 0)
2932+
{ if (doit) (*var) = DEBUG3; }
2933+
else if (strcasecmp(newval, "debug2") == 0)
2934+
{ if (doit) (*var) = DEBUG2; }
2935+
else if (strcasecmp(newval, "debug1") == 0)
2936+
{ if (doit) (*var) = DEBUG1; }
2937+
else if (strcasecmp(newval, "log") == 0)
2938+
{ if (doit) (*var) = LOG; }
2939+
else if (strcasecmp(newval, "info") == 0)
2940+
{ if (doit) (*var) = INFO; }
2941+
else if (strcasecmp(newval, "notice") == 0)
2942+
{ if (doit) (*var) = NOTICE; }
2943+
else if (strcasecmp(newval, "warning") == 0)
2944+
{ if (doit) (*var) = WARNING; }
2945+
else if (strcasecmp(newval, "error") == 0)
2946+
{ if (doit) (*var) = ERROR; }
2947+
else
2948+
return NULL; /* fail */
2949+
return newval; /* OK */
2950+
}
2951+

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@
127127
#log_duration = false
128128
#log_timestamp = false
129129

130+
#log_min_error_statement = error # Values in order of increasing severity:
131+
# debug5, debug4, debug3, debug2, debug1,
132+
# info, notice, warning, error
130133
#debug_print_parse = false
131134
#debug_print_rewritten = false
132135
#debug_print_plan = false

src/bin/psql/tab-complete.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000-2002 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.61 2002/09/01 23:26:06 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.62 2002/09/02 05:42:54 momjian Exp $
77
*/
88

99
/*----------------------------------------------------------------------
@@ -271,7 +271,7 @@ psql_completion(char *text, int start, int end)
271271
"default_transaction_isolation",
272272
"search_path",
273273
"statement_timeout",
274-
274+
"log_min_error_statement",
275275
NULL
276276
};
277277

src/include/utils/elog.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: elog.h,v 1.38 2002/06/20 20:29:52 momjian Exp $
10+
* $Id: elog.h,v 1.39 2002/09/02 05:42:54 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -47,22 +47,12 @@ extern int Use_syslog;
4747
extern bool Log_timestamp;
4848
extern bool Log_pid;
4949

50-
extern char *server_min_messages_str;
51-
extern char *client_min_messages_str;
52-
extern const char server_min_messages_str_default[];
53-
extern const char client_min_messages_str_default[];
54-
5550
extern void
5651
elog(int lev, const char *fmt,...)
5752
/* This extension allows gcc to check the format string for consistency with
5853
the supplied arguments. */
5954
__attribute__((format(printf, 2, 3)));
6055

61-
extern int DebugFileOpen(void);
62-
63-
extern const char *assign_server_min_messages(const char *newval,
64-
bool doit, bool interactive);
65-
extern const char *assign_client_min_messages(const char *newval,
66-
bool doit, bool interactive);
56+
extern int DebugFileOpen(void);
6757

6858
#endif /* ELOG_H */

src/include/utils/guc.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* External declarations pertaining to backend/utils/misc/guc.c and
55
* backend/utils/misc/guc-file.l
66
*
7-
* $Id: guc.h,v 1.21 2002/09/01 23:26:06 momjian Exp $
7+
* $Id: guc.h,v 1.22 2002/09/02 05:42:54 momjian Exp $
88
*/
99
#ifndef GUC_H
1010
#define GUC_H
@@ -100,6 +100,13 @@ extern void ProcessGUCArray(ArrayType *array, GucSource source);
100100
extern ArrayType *GUCArrayAdd(ArrayType *array, const char *name, const char *value);
101101
extern ArrayType *GUCArrayDelete(ArrayType *array, const char *name);
102102

103+
extern const char *assign_min_error_statement(const char *newval, bool doit,
104+
bool interactive);
105+
106+
extern const char *assign_server_min_messages(const char *newval,
107+
bool doit, bool interactive);
108+
extern const char *assign_client_min_messages(const char *newval,
109+
bool doit, bool interactive);
103110
extern bool Log_statement;
104111
extern bool Log_duration;
105112
extern bool Debug_print_plan;
@@ -118,4 +125,19 @@ extern bool Explain_pretty_print;
118125
extern bool SQL_inheritance;
119126
extern bool Australian_timezones;
120127

128+
extern char *debug_query_string;
129+
130+
extern int log_min_error_statement;
131+
extern char *log_min_error_statement_str;
132+
extern const char log_min_error_statement_str_default[];
133+
134+
extern int server_min_messages;
135+
extern char *server_min_messages_str;
136+
extern const char server_min_messages_str_default[];
137+
138+
extern int client_min_messages;
139+
extern char *client_min_messages_str;
140+
141+
extern const char client_min_messages_str_default[];
142+
121143
#endif /* GUC_H */

0 commit comments

Comments
 (0)