1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* elog.c
|
2003-04-24 23:16:45 +02:00
|
|
|
* error logging and reporting
|
|
|
|
*
|
2011-02-17 16:29:42 +01:00
|
|
|
* Because of the extremely high rate at which log messages can be generated,
|
|
|
|
* we need to be mindful of the performance cost of obtaining any information
|
|
|
|
* that may be logged. Also, it's important to keep in mind that this code may
|
|
|
|
* get called from within an aborted transaction, in which case operations
|
|
|
|
* such as syscache lookups are unsafe.
|
|
|
|
*
|
2003-04-24 23:16:45 +02:00
|
|
|
* Some notes about recursion and errors during error processing:
|
|
|
|
*
|
|
|
|
* We need to be robust about recursive-error scenarios --- for example,
|
|
|
|
* if we run out of memory, it's important to be able to report that fact.
|
|
|
|
* There are a number of considerations that go into this.
|
|
|
|
*
|
|
|
|
* First, distinguish between re-entrant use and actual recursion. It
|
|
|
|
* is possible for an error or warning message to be emitted while the
|
|
|
|
* parameters for an error message are being computed. In this case
|
|
|
|
* errstart has been called for the outer message, and some field values
|
|
|
|
* may have already been saved, but we are not actually recursing. We handle
|
|
|
|
* this by providing a (small) stack of ErrorData records. The inner message
|
|
|
|
* can be computed and sent without disturbing the state of the outer message.
|
|
|
|
* (If the inner message is actually an error, this isn't very interesting
|
|
|
|
* because control won't come back to the outer message generator ... but
|
|
|
|
* if the inner message is only debug or log data, this is critical.)
|
|
|
|
*
|
|
|
|
* Second, actual recursion will occur if an error is reported by one of
|
|
|
|
* the elog.c routines or something they call. By far the most probable
|
|
|
|
* scenario of this sort is "out of memory"; and it's also the nastiest
|
|
|
|
* to handle because we'd likely also run out of memory while trying to
|
2004-09-05 04:01:41 +02:00
|
|
|
* report this error! Our escape hatch for this case is to reset the
|
|
|
|
* ErrorContext to empty before trying to process the inner error. Since
|
|
|
|
* ErrorContext is guaranteed to have at least 8K of space in it (see mcxt.c),
|
|
|
|
* we should be able to process an "out of memory" message successfully.
|
|
|
|
* Since we lose the prior error state due to the reset, we won't be able
|
|
|
|
* to return to processing the original error, but we wouldn't have anyway.
|
|
|
|
* (NOTE: the escape hatch is not used for recursive situations where the
|
|
|
|
* inner message is of less than ERROR severity; in that case we just
|
|
|
|
* try to process it and return normally. Usually this will work, but if
|
|
|
|
* it ends up in infinite recursion, we will PANIC due to error stack
|
|
|
|
* overflow.)
|
2003-04-24 23:16:45 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2024-01-04 02:49:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/error/elog.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1999-09-11 21:06:42 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <fcntl.h>
|
2004-06-03 04:08:07 +02:00
|
|
|
#include <time.h>
|
1996-11-06 11:32:10 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
2000-05-31 02:28:42 +02:00
|
|
|
#include <ctype.h>
|
2002-04-21 02:22:52 +02:00
|
|
|
#ifdef HAVE_SYSLOG
|
2000-06-28 05:33:33 +02:00
|
|
|
#include <syslog.h>
|
1998-08-25 23:34:10 +02:00
|
|
|
#endif
|
2019-11-08 19:44:20 +01:00
|
|
|
#ifdef HAVE_EXECINFO_H
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
1998-08-25 23:34:10 +02:00
|
|
|
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/xact.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "libpq/libpq.h"
|
1999-04-25 23:50:58 +02:00
|
|
|
#include "libpq/pqformat.h"
|
2003-04-24 23:16:45 +02:00
|
|
|
#include "mb/pg_wchar.h"
|
2000-12-06 18:25:46 +01:00
|
|
|
#include "miscadmin.h"
|
2024-03-13 15:07:00 +01:00
|
|
|
#include "nodes/miscnodes.h"
|
2021-01-17 13:34:09 +01:00
|
|
|
#include "pgstat.h"
|
2020-03-15 11:20:21 +01:00
|
|
|
#include "postmaster/bgworker.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "postmaster/postmaster.h"
|
2004-08-06 01:32:13 +02:00
|
|
|
#include "postmaster/syslogger.h"
|
2002-05-05 02:03:29 +02:00
|
|
|
#include "storage/ipc.h"
|
2007-09-05 20:10:48 +02:00
|
|
|
#include "storage/proc.h"
|
1999-04-20 04:19:59 +02:00
|
|
|
#include "tcop/tcopprot.h"
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
#include "utils/guc_hooks.h"
|
2006-07-11 18:35:33 +02:00
|
|
|
#include "utils/memutils.h"
|
2005-11-05 04:04:53 +01:00
|
|
|
#include "utils/ps_status.h"
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
#include "utils/varlena.h"
|
1998-08-25 23:34:10 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2016-08-26 20:19:03 +02:00
|
|
|
/* In this module, access gettext() via err_gettext() */
|
2009-03-02 22:18:43 +01:00
|
|
|
#undef _
|
|
|
|
#define _(x) err_gettext(x)
|
|
|
|
|
2009-06-26 01:07:15 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/* Global variables */
|
|
|
|
ErrorContextCallback *error_context_stack = NULL;
|
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
sigjmp_buf *PG_exception_stack = NULL;
|
|
|
|
|
2007-07-19 21:13:43 +02:00
|
|
|
extern bool redirection_done;
|
2007-02-11 12:59:26 +01:00
|
|
|
|
2012-03-06 21:35:41 +01:00
|
|
|
/*
|
|
|
|
* Hook for intercepting messages before they are sent to the server log.
|
|
|
|
* Note that the hook will not get called for messages that are suppressed
|
|
|
|
* by log_min_messages. Also note that logging hooks implemented in preload
|
|
|
|
* libraries will miss any log messages that are generated before the
|
|
|
|
* library is loaded.
|
|
|
|
*/
|
|
|
|
emit_log_hook_type emit_log_hook = NULL;
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/* GUC parameters */
|
2022-10-31 04:44:48 +01:00
|
|
|
int Log_error_verbosity = PGERROR_DEFAULT;
|
2004-03-19 03:23:59 +01:00
|
|
|
char *Log_line_prefix = NULL; /* format for extra log line info */
|
2004-08-06 01:32:13 +02:00
|
|
|
int Log_destination = LOG_DESTINATION_STDERR;
|
2013-08-13 21:24:52 +02:00
|
|
|
char *Log_destination_string = NULL;
|
2016-02-27 04:34:30 +01:00
|
|
|
bool syslog_sequence_numbers = true;
|
2016-03-16 03:48:53 +01:00
|
|
|
bool syslog_split_messages = true;
|
2000-10-30 07:48:36 +01:00
|
|
|
|
2023-12-28 17:25:47 +01:00
|
|
|
/* Processed form of backtrace_functions GUC */
|
|
|
|
static char *backtrace_function_list;
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
|
2002-04-21 02:22:52 +02:00
|
|
|
#ifdef HAVE_SYSLOG
|
2008-07-09 17:56:49 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Max string length to send to syslog(). Note that this doesn't count the
|
|
|
|
* sequence-number prefix we add, and of course it doesn't count the prefix
|
2011-08-06 03:02:31 +02:00
|
|
|
* added by syslog itself. Solaris and sysklogd truncate the final message
|
|
|
|
* at 1024 bytes, so this value leaves 124 bytes for those prefixes. (Most
|
|
|
|
* other syslog implementations seem to have limits of 2KB or so.)
|
2008-07-09 17:56:49 +02:00
|
|
|
*/
|
|
|
|
#ifndef PG_SYSLOG_LIMIT
|
2011-08-06 03:02:31 +02:00
|
|
|
#define PG_SYSLOG_LIMIT 900
|
2008-07-09 17:56:49 +02:00
|
|
|
#endif
|
|
|
|
|
2005-10-14 22:53:56 +02:00
|
|
|
static bool openlog_done = false;
|
|
|
|
static char *syslog_ident = NULL;
|
|
|
|
static int syslog_facility = LOG_LOCAL0;
|
2000-05-31 02:28:42 +02:00
|
|
|
|
|
|
|
static void write_syslog(int level, const char *line);
|
2004-04-05 05:02:11 +02:00
|
|
|
#endif
|
2005-10-14 18:41:02 +02:00
|
|
|
|
2004-04-05 05:02:11 +02:00
|
|
|
#ifdef WIN32
|
2011-10-25 20:02:55 +02:00
|
|
|
extern char *event_source;
|
2016-08-26 20:19:03 +02:00
|
|
|
|
2009-10-17 02:24:51 +02:00
|
|
|
static void write_eventlog(int level, const char *line, int len);
|
2004-04-05 05:02:11 +02:00
|
|
|
#endif
|
2000-05-31 02:28:42 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/* We provide a small stack of ErrorData records for re-entrant cases */
|
|
|
|
#define ERRORDATA_STACK_SIZE 5
|
|
|
|
|
|
|
|
static ErrorData errordata[ERRORDATA_STACK_SIZE];
|
|
|
|
|
|
|
|
static int errordata_stack_depth = -1; /* index of topmost active frame */
|
|
|
|
|
|
|
|
static int recursion_depth = 0; /* to detect actual recursion */
|
|
|
|
|
2015-09-08 00:40:49 +02:00
|
|
|
/*
|
|
|
|
* Saved timeval and buffers for formatted timestamps that might be used by
|
|
|
|
* both log_line_prefix and csv logs.
|
2007-08-19 03:41:25 +02:00
|
|
|
*/
|
2015-09-08 00:40:49 +02:00
|
|
|
static struct timeval saved_timeval;
|
|
|
|
static bool saved_timeval_set = false;
|
|
|
|
|
2007-08-19 03:41:25 +02:00
|
|
|
#define FORMATTED_TS_LEN 128
|
|
|
|
static char formatted_start_time[FORMATTED_TS_LEN];
|
|
|
|
static char formatted_log_time[FORMATTED_TS_LEN];
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
/* Macro for checking errordata_stack_depth is reasonable */
|
|
|
|
#define CHECK_STACK_DEPTH() \
|
|
|
|
do { \
|
|
|
|
if (errordata_stack_depth < 0) \
|
|
|
|
{ \
|
|
|
|
errordata_stack_depth = -1; \
|
|
|
|
ereport(ERROR, (errmsg_internal("errstart was not called"))); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2016-08-26 20:19:03 +02:00
|
|
|
static const char *err_gettext(const char *str) pg_attribute_format_arg(1);
|
2022-12-07 20:39:25 +01:00
|
|
|
static ErrorData *get_error_stack_entry(void);
|
|
|
|
static void set_stack_entry_domain(ErrorData *edata, const char *domain);
|
|
|
|
static void set_stack_entry_location(ErrorData *edata,
|
|
|
|
const char *filename, int lineno,
|
|
|
|
const char *funcname);
|
|
|
|
static bool matches_backtrace_functions(const char *funcname);
|
2019-11-08 19:44:20 +01:00
|
|
|
static pg_noinline void set_backtrace(ErrorData *edata, int num_skip);
|
2016-08-26 20:19:03 +02:00
|
|
|
static void set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str);
|
2022-12-07 20:39:25 +01:00
|
|
|
static void FreeErrorDataContents(ErrorData *edata);
|
2016-08-26 20:19:03 +02:00
|
|
|
static void write_console(const char *line, int len);
|
2022-09-20 22:09:30 +02:00
|
|
|
static const char *process_log_prefix_padding(const char *p, int *ppadding);
|
2009-07-03 21:14:25 +02:00
|
|
|
static void log_line_prefix(StringInfo buf, ErrorData *edata);
|
2003-04-24 23:16:45 +02:00
|
|
|
static void send_message_to_server_log(ErrorData *edata);
|
|
|
|
static void send_message_to_frontend(ErrorData *edata);
|
2003-10-17 18:49:03 +02:00
|
|
|
static void append_with_tabs(StringInfo buf, const char *str);
|
2020-11-24 01:04:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* is_log_level_output -- is elevel logically >= log_min_level?
|
|
|
|
*
|
|
|
|
* We use this for tests that should consider LOG to sort out-of-order,
|
|
|
|
* between ERROR and FATAL. Generally this is the right thing for testing
|
|
|
|
* whether a message should go to the postmaster log, whereas a simple >=
|
|
|
|
* test is correct for testing whether the message should go to the client.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
is_log_level_output(int elevel, int log_min_level)
|
|
|
|
{
|
|
|
|
if (elevel == LOG || elevel == LOG_SERVER_ONLY)
|
|
|
|
{
|
|
|
|
if (log_min_level == LOG || log_min_level <= ERROR)
|
|
|
|
return true;
|
|
|
|
}
|
2020-12-30 00:02:38 +01:00
|
|
|
else if (elevel == WARNING_CLIENT_ONLY)
|
|
|
|
{
|
|
|
|
/* never sent to log, regardless of log_min_level */
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-24 01:04:07 +01:00
|
|
|
else if (log_min_level == LOG)
|
|
|
|
{
|
|
|
|
/* elevel != LOG */
|
|
|
|
if (elevel >= FATAL)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/* Neither is LOG */
|
|
|
|
else if (elevel >= log_min_level)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Policy-setting subroutines. These are fairly simple, but it seems wise
|
|
|
|
* to have the code in just one place.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* should_output_to_server --- should message of given elevel go to the log?
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
should_output_to_server(int elevel)
|
|
|
|
{
|
|
|
|
return is_log_level_output(elevel, log_min_messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* should_output_to_client --- should message of given elevel go to the client?
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
should_output_to_client(int elevel)
|
|
|
|
{
|
|
|
|
if (whereToSendOutput == DestRemote && elevel != LOG_SERVER_ONLY)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* client_min_messages is honored only after we complete the
|
|
|
|
* authentication handshake. This is required both for security
|
|
|
|
* reasons and because many clients can't handle NOTICE messages
|
|
|
|
* during authentication.
|
|
|
|
*/
|
|
|
|
if (ClientAuthInProgress)
|
|
|
|
return (elevel >= ERROR);
|
|
|
|
else
|
|
|
|
return (elevel >= client_min_messages || elevel == INFO);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* message_level_is_interesting --- would ereport/elog do anything?
|
|
|
|
*
|
|
|
|
* Returns true if ereport/elog with this elevel will not be a no-op.
|
|
|
|
* This is useful to short-circuit any expensive preparatory work that
|
|
|
|
* might be needed for a logging message. There is no point in
|
|
|
|
* prepending this to a bare ereport/elog call, however.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
message_level_is_interesting(int elevel)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Keep this in sync with the decision-making in errstart().
|
|
|
|
*/
|
|
|
|
if (elevel >= ERROR ||
|
|
|
|
should_output_to_server(elevel) ||
|
|
|
|
should_output_to_client(elevel))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2008-10-27 20:37:22 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* in_error_recursion_trouble --- are we at risk of infinite error recursion?
|
|
|
|
*
|
|
|
|
* This function exists to provide common control of various fallback steps
|
|
|
|
* that we take if we think we are facing infinite error recursion. See the
|
|
|
|
* callers for details.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
in_error_recursion_trouble(void)
|
|
|
|
{
|
|
|
|
/* Pull the plug if recurse more than once */
|
|
|
|
return (recursion_depth > 2);
|
|
|
|
}
|
|
|
|
|
2009-03-02 22:18:43 +01:00
|
|
|
/*
|
|
|
|
* One of those fallback steps is to stop trying to localize the error
|
|
|
|
* message, since there's a significant probability that that's exactly
|
|
|
|
* what's causing the recursion.
|
|
|
|
*/
|
|
|
|
static inline const char *
|
|
|
|
err_gettext(const char *str)
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
if (in_error_recursion_trouble())
|
|
|
|
return str;
|
|
|
|
else
|
|
|
|
return gettext(str);
|
|
|
|
#else
|
|
|
|
return str;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-24 00:04:42 +01:00
|
|
|
/*
|
|
|
|
* errstart_cold
|
|
|
|
* A simple wrapper around errstart, but hinted to be "cold". Supporting
|
|
|
|
* compilers are more likely to move code for branches containing this
|
|
|
|
* function into an area away from the calling function's code. This can
|
|
|
|
* result in more commonly executed code being more compact and fitting
|
|
|
|
* on fewer cache lines.
|
|
|
|
*/
|
|
|
|
pg_attribute_cold bool
|
|
|
|
errstart_cold(int elevel, const char *domain)
|
|
|
|
{
|
|
|
|
return errstart(elevel, domain);
|
|
|
|
}
|
2009-03-02 22:18:43 +01:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
|
|
|
* errstart --- begin an error-reporting cycle
|
1999-09-11 21:06:42 +02:00
|
|
|
*
|
2020-03-24 17:08:48 +01:00
|
|
|
* Create and initialize error stack entry. Subsequently, errmsg() and
|
|
|
|
* perhaps other routines will be called to further populate the stack entry.
|
|
|
|
* Finally, errfinish() will be called to actually process the error report.
|
1999-09-11 21:06:42 +02:00
|
|
|
*
|
2017-08-16 06:22:32 +02:00
|
|
|
* Returns true in normal case. Returns false to short-circuit the error
|
2003-04-24 23:16:45 +02:00
|
|
|
* report (if it's a warning or lower and not to be reported anywhere).
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2003-04-24 23:16:45 +02:00
|
|
|
bool
|
2020-03-24 17:08:48 +01:00
|
|
|
errstart(int elevel, const char *domain)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-04-24 23:16:45 +02:00
|
|
|
ErrorData *edata;
|
2007-01-20 15:45:35 +01:00
|
|
|
bool output_to_server;
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
bool output_to_client = false;
|
2004-09-05 04:01:41 +02:00
|
|
|
int i;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
2004-09-05 04:01:41 +02:00
|
|
|
* Check some cases in which we want to promote an error into a more
|
|
|
|
* severe error. None of this logic applies for non-error messages.
|
2001-06-08 23:16:49 +02:00
|
|
|
*/
|
2003-04-24 23:16:45 +02:00
|
|
|
if (elevel >= ERROR)
|
2001-01-19 23:08:47 +01:00
|
|
|
{
|
2004-09-05 04:01:41 +02:00
|
|
|
/*
|
|
|
|
* If we are inside a critical section, all errors become PANIC
|
|
|
|
* errors. See miscadmin.h.
|
|
|
|
*/
|
2001-01-19 23:08:47 +01:00
|
|
|
if (CritSectionCount > 0)
|
2003-04-24 23:16:45 +02:00
|
|
|
elevel = PANIC;
|
2004-09-05 04:01:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check reasons for treating ERROR as FATAL:
|
|
|
|
*
|
|
|
|
* 1. we have no handler to pass the error to (implies we are in the
|
|
|
|
* postmaster or in backend startup).
|
|
|
|
*
|
|
|
|
* 2. ExitOnAnyError mode switch is set (initdb uses this).
|
|
|
|
*
|
|
|
|
* 3. the error occurred after proc_exit has begun to run. (It's
|
|
|
|
* proc_exit's responsibility to see that this doesn't turn into
|
|
|
|
* infinite recursion!)
|
|
|
|
*/
|
|
|
|
if (elevel == ERROR)
|
|
|
|
{
|
|
|
|
if (PG_exception_stack == NULL ||
|
|
|
|
ExitOnAnyError ||
|
|
|
|
proc_exit_inprogress)
|
|
|
|
elevel = FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the error level is ERROR or more, errfinish is not going to
|
|
|
|
* return to caller; therefore, if there is any stacked error already
|
|
|
|
* in progress it will be lost. This is more or less okay, except we
|
|
|
|
* do not want to have a FATAL or PANIC error downgraded because the
|
|
|
|
* reporting process was interrupted by a lower-grade error. So check
|
|
|
|
* the stack and make sure we panic if panic is warranted.
|
|
|
|
*/
|
|
|
|
for (i = 0; i <= errordata_stack_depth; i++)
|
|
|
|
elevel = Max(elevel, errordata[i].elevel);
|
2001-01-19 23:08:47 +01:00
|
|
|
}
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2004-09-05 04:01:41 +02:00
|
|
|
/*
|
|
|
|
* Now decide whether we need to process this report at all; if it's
|
2017-08-16 06:22:32 +02:00
|
|
|
* warning or less and not enabled for logging, just return false without
|
2004-09-05 04:01:41 +02:00
|
|
|
* starting up any error logging machinery.
|
|
|
|
*/
|
2020-11-24 01:04:07 +01:00
|
|
|
output_to_server = should_output_to_server(elevel);
|
|
|
|
output_to_client = should_output_to_client(elevel);
|
2003-04-24 23:16:45 +02:00
|
|
|
if (elevel < ERROR && !output_to_server && !output_to_client)
|
|
|
|
return false;
|
2000-06-04 17:06:34 +02:00
|
|
|
|
2014-01-11 22:35:26 +01:00
|
|
|
/*
|
|
|
|
* We need to do some actual work. Make sure that memory context
|
|
|
|
* initialization has finished, else we can't do anything useful.
|
|
|
|
*/
|
|
|
|
if (ErrorContext == NULL)
|
|
|
|
{
|
2017-03-14 16:38:30 +01:00
|
|
|
/* Oops, hard crash time; very little we can do safely here */
|
2020-03-24 17:08:48 +01:00
|
|
|
write_stderr("error occurred before error message processing is available\n");
|
2014-01-11 22:35:26 +01:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
1999-09-11 21:06:42 +02:00
|
|
|
/*
|
2003-04-24 23:16:45 +02:00
|
|
|
* Okay, crank up a stack entry to store the info in.
|
1999-09-11 21:06:42 +02:00
|
|
|
*/
|
2001-06-08 23:16:49 +02:00
|
|
|
|
2004-09-05 04:01:41 +02:00
|
|
|
if (recursion_depth++ > 0 && elevel >= ERROR)
|
2001-06-08 23:16:49 +02:00
|
|
|
{
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
/*
|
2017-03-14 16:38:30 +01:00
|
|
|
* Oops, error during error processing. Clear ErrorContext as
|
2004-09-05 04:01:41 +02:00
|
|
|
* discussed at top of file. We will not return to the original
|
|
|
|
* error's reporter or handler, so we don't need it.
|
2003-04-24 23:16:45 +02:00
|
|
|
*/
|
|
|
|
MemoryContextReset(ErrorContext);
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
2008-10-27 20:37:22 +01:00
|
|
|
* Infinite error recursion might be due to something broken in a
|
2007-07-22 00:12:04 +02:00
|
|
|
* context traceback routine. Abandon them too. We also abandon
|
|
|
|
* attempting to print the error statement (which, if long, could
|
|
|
|
* itself be the source of the recursive failure).
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
*/
|
2008-10-27 20:37:22 +01:00
|
|
|
if (in_error_recursion_trouble())
|
2007-07-22 00:12:04 +02:00
|
|
|
{
|
2003-04-24 23:16:45 +02:00
|
|
|
error_context_stack = NULL;
|
2007-07-22 00:12:04 +02:00
|
|
|
debug_query_string = NULL;
|
|
|
|
}
|
2001-06-08 23:16:49 +02:00
|
|
|
}
|
2000-06-04 17:06:34 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/* Initialize data for this error frame */
|
2022-12-07 20:39:25 +01:00
|
|
|
edata = get_error_stack_entry();
|
2003-04-24 23:16:45 +02:00
|
|
|
edata->elevel = elevel;
|
|
|
|
edata->output_to_server = output_to_server;
|
|
|
|
edata->output_to_client = output_to_client;
|
2022-12-07 20:39:25 +01:00
|
|
|
set_stack_entry_domain(edata, domain);
|
2003-07-19 01:20:33 +02:00
|
|
|
/* Select default errcode based on elevel */
|
|
|
|
if (elevel >= ERROR)
|
|
|
|
edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
|
2020-12-30 00:02:38 +01:00
|
|
|
else if (elevel >= WARNING)
|
2003-07-19 01:20:33 +02:00
|
|
|
edata->sqlerrcode = ERRCODE_WARNING;
|
|
|
|
else
|
|
|
|
edata->sqlerrcode = ERRCODE_SUCCESSFUL_COMPLETION;
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2013-08-01 07:07:20 +02:00
|
|
|
/*
|
|
|
|
* Any allocations for this error state level should go into ErrorContext
|
|
|
|
*/
|
|
|
|
edata->assoc_context = ErrorContext;
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
recursion_depth--;
|
|
|
|
return true;
|
|
|
|
}
|
2000-06-04 17:06:34 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
|
|
|
* errfinish --- end an error-reporting cycle
|
|
|
|
*
|
|
|
|
* Produce the appropriate error report(s) and pop the error stack.
|
|
|
|
*
|
2020-03-24 17:08:48 +01:00
|
|
|
* If elevel, as passed to errstart(), is ERROR or worse, control does not
|
|
|
|
* return to the caller. See elog.h for the error level definitions.
|
2003-04-24 23:16:45 +02:00
|
|
|
*/
|
|
|
|
void
|
2020-03-24 17:08:48 +01:00
|
|
|
errfinish(const char *filename, int lineno, const char *funcname)
|
2003-04-24 23:16:45 +02:00
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
2013-12-02 16:40:33 +01:00
|
|
|
int elevel;
|
2003-04-24 23:16:45 +02:00
|
|
|
MemoryContext oldcontext;
|
|
|
|
ErrorContextCallback *econtext;
|
2000-06-04 17:06:34 +02:00
|
|
|
|
2003-08-04 01:44:44 +02:00
|
|
|
recursion_depth++;
|
2003-04-24 23:16:45 +02:00
|
|
|
CHECK_STACK_DEPTH();
|
2020-03-24 17:08:48 +01:00
|
|
|
|
|
|
|
/* Save the last few bits of error state into the stack entry */
|
2022-12-07 20:39:25 +01:00
|
|
|
set_stack_entry_location(edata, filename, lineno, funcname);
|
2020-03-24 17:08:48 +01:00
|
|
|
|
2013-12-02 16:40:33 +01:00
|
|
|
elevel = edata->elevel;
|
2000-06-04 17:06:34 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
2003-08-04 01:44:44 +02:00
|
|
|
* Do processing in ErrorContext, which we hope has enough reserved space
|
|
|
|
* to report an error.
|
|
|
|
*/
|
|
|
|
oldcontext = MemoryContextSwitchTo(ErrorContext);
|
|
|
|
|
2022-12-07 20:39:25 +01:00
|
|
|
/* Collect backtrace, if enabled and we didn't already */
|
2019-11-08 19:44:20 +01:00
|
|
|
if (!edata->backtrace &&
|
2023-12-30 11:11:26 +01:00
|
|
|
((edata->funcname &&
|
|
|
|
backtrace_functions &&
|
|
|
|
matches_backtrace_functions(edata->funcname)) ||
|
|
|
|
(edata->sqlerrcode == ERRCODE_INTERNAL_ERROR &&
|
|
|
|
backtrace_on_internal_error)))
|
2019-11-08 19:44:20 +01:00
|
|
|
set_backtrace(edata, 2);
|
|
|
|
|
2003-08-04 01:44:44 +02:00
|
|
|
/*
|
|
|
|
* Call any context callback functions. Errors occurring in callback
|
|
|
|
* functions will be treated as recursive errors --- this ensures we will
|
|
|
|
* avoid infinite recursion (see errstart).
|
2003-04-24 23:16:45 +02:00
|
|
|
*/
|
|
|
|
for (econtext = error_context_stack;
|
|
|
|
econtext != NULL;
|
|
|
|
econtext = econtext->previous)
|
2017-09-07 18:06:23 +02:00
|
|
|
econtext->callback(econtext->arg);
|
2000-02-13 19:59:53 +01:00
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
/*
|
2004-09-05 04:01:41 +02:00
|
|
|
* If ERROR (not more nor less) we pass it off to the current handler.
|
|
|
|
* Printing it and popping the stack is the responsibility of the handler.
|
2004-07-31 02:45:57 +02:00
|
|
|
*/
|
2004-09-05 04:01:41 +02:00
|
|
|
if (elevel == ERROR)
|
2004-07-31 02:45:57 +02:00
|
|
|
{
|
2004-09-05 04:01:41 +02:00
|
|
|
/*
|
|
|
|
* We do some minimal cleanup before longjmp'ing so that handlers can
|
|
|
|
* execute in a reasonably sane state.
|
2014-03-14 01:59:42 +01:00
|
|
|
*
|
2004-09-05 04:01:41 +02:00
|
|
|
* Reset InterruptHoldoffCount in case we ereport'd from inside an
|
|
|
|
* interrupt holdoff section. (We assume here that no handler will
|
|
|
|
* itself be inside a holdoff section. If necessary, such a handler
|
|
|
|
* could save and restore InterruptHoldoffCount for itself, but this
|
|
|
|
* should make life easier for most.)
|
|
|
|
*/
|
|
|
|
InterruptHoldoffCount = 0;
|
Be more careful to not lose sync in the FE/BE protocol.
If any error occurred while we were in the middle of reading a protocol
message from the client, we could lose sync, and incorrectly try to
interpret a part of another message as a new protocol message. That will
usually lead to an "invalid frontend message" error that terminates the
connection. However, this is a security issue because an attacker might
be able to deliberately cause an error, inject a Query message in what's
supposed to be just user data, and have the server execute it.
We were quite careful to not have CHECK_FOR_INTERRUPTS() calls or other
operations that could ereport(ERROR) in the middle of processing a message,
but a query cancel interrupt or statement timeout could nevertheless cause
it to happen. Also, the V2 fastpath and COPY handling were not so careful.
It's very difficult to recover in the V2 COPY protocol, so we will just
terminate the connection on error. In practice, that's what happened
previously anyway, as we lost protocol sync.
To fix, add a new variable in pqcomm.c, PqCommReadingMsg, that is set
whenever we're in the middle of reading a message. When it's set, we cannot
safely ERROR out and continue running, because we might've read only part
of a message. PqCommReadingMsg acts somewhat similarly to critical sections
in that if an error occurs while it's set, the error handler will force the
connection to be terminated, as if the error was FATAL. It's not
implemented by promoting ERROR to FATAL in elog.c, like ERROR is promoted
to PANIC in critical sections, because we want to be able to use
PG_TRY/CATCH to recover and regain protocol sync. pq_getmessage() takes
advantage of that to prevent an OOM error from terminating the connection.
To prevent unnecessary connection terminations, add a holdoff mechanism
similar to HOLD/RESUME_INTERRUPTS() that can be used hold off query cancel
interrupts, but still allow die interrupts. The rules on which interrupts
are processed when are now a bit more complicated, so refactor
ProcessInterrupts() and the calls to it in signal handlers so that the
signal handlers always call it if ImmediateInterruptOK is set, and
ProcessInterrupts() can decide to not do anything if the other conditions
are not met.
Reported by Emil Lenngren. Patch reviewed by Noah Misch and Andres Freund.
Backpatch to all supported versions.
Security: CVE-2015-0244
2015-02-02 16:08:45 +01:00
|
|
|
QueryCancelHoldoffCount = 0;
|
2004-09-05 04:01:41 +02:00
|
|
|
|
|
|
|
CritSectionCount = 0; /* should be unnecessary, but... */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that we leave CurrentMemoryContext set to ErrorContext. The
|
|
|
|
* handler should reset it to something else soon.
|
|
|
|
*/
|
|
|
|
|
|
|
|
recursion_depth--;
|
|
|
|
PG_RE_THROW();
|
2004-07-31 02:45:57 +02:00
|
|
|
}
|
2002-09-02 07:42:54 +02:00
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
/* Emit the message to the right places */
|
|
|
|
EmitErrorReport();
|
2000-05-31 02:28:42 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/* Now free up subsidiary data attached to stack entry, and release it */
|
2022-12-07 20:39:25 +01:00
|
|
|
FreeErrorDataContents(edata);
|
2003-04-24 23:16:45 +02:00
|
|
|
errordata_stack_depth--;
|
2004-07-31 02:45:57 +02:00
|
|
|
|
|
|
|
/* Exit error-handling context */
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2003-04-24 23:16:45 +02:00
|
|
|
recursion_depth--;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
2004-07-31 02:45:57 +02:00
|
|
|
* Perform error recovery action as specified by elevel.
|
2003-04-24 23:16:45 +02:00
|
|
|
*/
|
2004-07-31 02:45:57 +02:00
|
|
|
if (elevel == FATAL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-12-01 20:52:04 +01:00
|
|
|
/*
|
2004-07-31 02:45:57 +02:00
|
|
|
* For a FATAL error, we let proc_exit clean up and exit.
|
2014-03-14 01:59:42 +01:00
|
|
|
*
|
2002-03-04 02:46:04 +01:00
|
|
|
* If we just reported a startup failure, the client will disconnect
|
|
|
|
* on receiving it, so don't send any more to the client.
|
|
|
|
*/
|
2005-11-03 18:11:40 +01:00
|
|
|
if (PG_exception_stack == NULL && whereToSendOutput == DestRemote)
|
|
|
|
whereToSendOutput = DestNone;
|
2002-03-04 02:46:04 +01:00
|
|
|
|
1999-11-22 03:06:31 +01:00
|
|
|
/*
|
2004-07-31 02:45:57 +02:00
|
|
|
* fflush here is just to improve the odds that we get to see the
|
|
|
|
* error message, in case things are so hosed that proc_exit crashes.
|
|
|
|
* Any other code you might be tempted to add here should probably be
|
2006-11-21 01:49:55 +01:00
|
|
|
* in an on_proc_exit or on_shmem_exit callback instead.
|
1999-11-22 03:06:31 +01:00
|
|
|
*/
|
2022-08-29 19:55:38 +02:00
|
|
|
fflush(NULL);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2021-01-17 13:34:09 +01:00
|
|
|
/*
|
2022-04-06 22:56:06 +02:00
|
|
|
* Let the cumulative stats system know. Only mark the session as
|
2021-01-17 13:34:09 +01:00
|
|
|
* terminated by fatal error if there is no other known cause.
|
|
|
|
*/
|
|
|
|
if (pgStatSessionEndCause == DISCONNECT_NORMAL)
|
|
|
|
pgStatSessionEndCause = DISCONNECT_FATAL;
|
|
|
|
|
1999-11-22 03:06:31 +01:00
|
|
|
/*
|
2006-11-21 01:49:55 +01:00
|
|
|
* Do normal process-exit cleanup, then return exit code 1 to indicate
|
|
|
|
* FATAL termination. The postmaster may or may not consider this
|
|
|
|
* worthy of panic, depending on which subprocess returns it.
|
1999-11-22 03:06:31 +01:00
|
|
|
*/
|
2006-11-21 01:49:55 +01:00
|
|
|
proc_exit(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
if (elevel >= PANIC)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-09-11 21:06:42 +02:00
|
|
|
/*
|
2006-11-21 01:49:55 +01:00
|
|
|
* Serious crash time. Postmaster will observe SIGABRT process exit
|
1999-09-11 21:06:42 +02:00
|
|
|
* status and kill the other backends too.
|
1999-11-16 07:13:36 +01:00
|
|
|
*
|
2002-11-01 18:55:23 +01:00
|
|
|
* XXX: what if we are *in* the postmaster? abort() won't kill our
|
1999-11-16 07:13:36 +01:00
|
|
|
* children...
|
1999-09-11 21:06:42 +02:00
|
|
|
*/
|
2022-08-29 19:55:38 +02:00
|
|
|
fflush(NULL);
|
2002-11-01 18:55:23 +01:00
|
|
|
abort();
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-09-11 21:06:42 +02:00
|
|
|
|
2006-10-02 00:08:18 +02:00
|
|
|
/*
|
2015-02-03 23:25:47 +01:00
|
|
|
* Check for cancel/die interrupt first --- this is so that the user can
|
|
|
|
* stop a query emitting tons of notice or warning messages, even if it's
|
|
|
|
* in a loop that otherwise fails to check for interrupts.
|
2006-10-02 00:08:18 +02:00
|
|
|
*/
|
|
|
|
CHECK_FOR_INTERRUPTS();
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:58:38 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* errsave_start --- begin a "soft" error-reporting cycle
|
|
|
|
*
|
|
|
|
* If "context" isn't an ErrorSaveContext node, this behaves as
|
|
|
|
* errstart(ERROR, domain), and the errsave() macro ends up acting
|
|
|
|
* exactly like ereport(ERROR, ...).
|
|
|
|
*
|
|
|
|
* If "context" is an ErrorSaveContext node, but the node creator only wants
|
|
|
|
* notification of the fact of a soft error without any details, we just set
|
|
|
|
* the error_occurred flag in the ErrorSaveContext node and return false,
|
|
|
|
* which will cause us to skip the remaining error processing steps.
|
|
|
|
*
|
|
|
|
* Otherwise, create and initialize error stack entry and return true.
|
|
|
|
* Subsequently, errmsg() and perhaps other routines will be called to further
|
|
|
|
* populate the stack entry. Finally, errsave_finish() will be called to
|
|
|
|
* tidy up.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
errsave_start(struct Node *context, const char *domain)
|
|
|
|
{
|
|
|
|
ErrorSaveContext *escontext;
|
|
|
|
ErrorData *edata;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do we have a context for soft error reporting? If not, just punt to
|
|
|
|
* errstart().
|
|
|
|
*/
|
|
|
|
if (context == NULL || !IsA(context, ErrorSaveContext))
|
|
|
|
return errstart(ERROR, domain);
|
|
|
|
|
|
|
|
/* Report that a soft error was detected */
|
|
|
|
escontext = (ErrorSaveContext *) context;
|
|
|
|
escontext->error_occurred = true;
|
|
|
|
|
|
|
|
/* Nothing else to do if caller wants no further details */
|
|
|
|
if (!escontext->details_wanted)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Okay, crank up a stack entry to store the info in.
|
|
|
|
*/
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
|
|
|
|
/* Initialize data for this error frame */
|
|
|
|
edata = get_error_stack_entry();
|
|
|
|
edata->elevel = LOG; /* signal all is well to errsave_finish */
|
|
|
|
set_stack_entry_domain(edata, domain);
|
|
|
|
/* Select default errcode based on the assumed elevel of ERROR */
|
|
|
|
edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Any allocations for this error state level should go into the caller's
|
|
|
|
* context. We don't need to pollute ErrorContext, or even require it to
|
|
|
|
* exist, in this code path.
|
|
|
|
*/
|
|
|
|
edata->assoc_context = CurrentMemoryContext;
|
|
|
|
|
|
|
|
recursion_depth--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* errsave_finish --- end a "soft" error-reporting cycle
|
|
|
|
*
|
|
|
|
* If errsave_start() decided this was a regular error, behave as
|
|
|
|
* errfinish(). Otherwise, package up the error details and save
|
|
|
|
* them in the ErrorSaveContext node.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
errsave_finish(struct Node *context, const char *filename, int lineno,
|
|
|
|
const char *funcname)
|
|
|
|
{
|
|
|
|
ErrorSaveContext *escontext = (ErrorSaveContext *) context;
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* verify stack depth before accessing *edata */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If errsave_start punted to errstart, then elevel will be ERROR or
|
|
|
|
* perhaps even PANIC. Punt likewise to errfinish.
|
|
|
|
*/
|
|
|
|
if (edata->elevel >= ERROR)
|
|
|
|
{
|
|
|
|
errfinish(filename, lineno, funcname);
|
|
|
|
pg_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Else, we should package up the stack entry contents and deliver them to
|
|
|
|
* the caller.
|
|
|
|
*/
|
|
|
|
recursion_depth++;
|
|
|
|
|
|
|
|
/* Save the last few bits of error state into the stack entry */
|
|
|
|
set_stack_entry_location(edata, filename, lineno, funcname);
|
|
|
|
|
|
|
|
/* Replace the LOG value that errsave_start inserted */
|
|
|
|
edata->elevel = ERROR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We skip calling backtrace and context functions, which are more likely
|
|
|
|
* to cause trouble than provide useful context; they might act on the
|
|
|
|
* assumption that a transaction abort is about to occur.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a copy of the error info for the caller. All the subsidiary
|
|
|
|
* strings are already in the caller's context, so it's sufficient to
|
|
|
|
* flat-copy the stack entry.
|
|
|
|
*/
|
|
|
|
escontext->error_data = palloc_object(ErrorData);
|
|
|
|
memcpy(escontext->error_data, edata, sizeof(ErrorData));
|
|
|
|
|
|
|
|
/* Exit error-handling context */
|
|
|
|
errordata_stack_depth--;
|
|
|
|
recursion_depth--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-07 20:39:25 +01:00
|
|
|
/*
|
|
|
|
* get_error_stack_entry --- allocate and initialize a new stack entry
|
|
|
|
*
|
|
|
|
* The entry should be freed, when we're done with it, by calling
|
|
|
|
* FreeErrorDataContents() and then decrementing errordata_stack_depth.
|
|
|
|
*
|
|
|
|
* Returning the entry's address is just a notational convenience,
|
|
|
|
* since it had better be errordata[errordata_stack_depth].
|
|
|
|
*
|
|
|
|
* Although the error stack is not large, we don't expect to run out of space.
|
|
|
|
* Using more than one entry implies a new error report during error recovery,
|
|
|
|
* which is possible but already suggests we're in trouble. If we exhaust the
|
|
|
|
* stack, almost certainly we are in an infinite loop of errors during error
|
|
|
|
* recovery, so we give up and PANIC.
|
|
|
|
*
|
|
|
|
* (Note that this is distinct from the recursion_depth checks, which
|
|
|
|
* guard against recursion while handling a single stack entry.)
|
|
|
|
*/
|
|
|
|
static ErrorData *
|
|
|
|
get_error_stack_entry(void)
|
|
|
|
{
|
|
|
|
ErrorData *edata;
|
|
|
|
|
|
|
|
/* Allocate error frame */
|
|
|
|
errordata_stack_depth++;
|
|
|
|
if (unlikely(errordata_stack_depth >= ERRORDATA_STACK_SIZE))
|
|
|
|
{
|
|
|
|
/* Wups, stack not big enough */
|
|
|
|
errordata_stack_depth = -1; /* make room on stack */
|
|
|
|
ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded")));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize error frame to all zeroes/NULLs */
|
|
|
|
edata = &errordata[errordata_stack_depth];
|
|
|
|
memset(edata, 0, sizeof(ErrorData));
|
|
|
|
|
|
|
|
/* Save errno immediately to ensure error parameter eval can't change it */
|
|
|
|
edata->saved_errno = errno;
|
|
|
|
|
|
|
|
return edata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set_stack_entry_domain --- fill in the internationalization domain
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
set_stack_entry_domain(ErrorData *edata, const char *domain)
|
|
|
|
{
|
|
|
|
/* the default text domain is the backend's */
|
|
|
|
edata->domain = domain ? domain : PG_TEXTDOMAIN("postgres");
|
|
|
|
/* initialize context_domain the same way (see set_errcontext_domain()) */
|
|
|
|
edata->context_domain = edata->domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set_stack_entry_location --- fill in code-location details
|
|
|
|
*
|
|
|
|
* Store the values of __FILE__, __LINE__, and __func__ from the call site.
|
|
|
|
* We make an effort to normalize __FILE__, since compilers are inconsistent
|
|
|
|
* about how much of the path they'll include, and we'd prefer that the
|
|
|
|
* behavior not depend on that (especially, that it not vary with build path).
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
set_stack_entry_location(ErrorData *edata,
|
|
|
|
const char *filename, int lineno,
|
|
|
|
const char *funcname)
|
|
|
|
{
|
|
|
|
if (filename)
|
|
|
|
{
|
|
|
|
const char *slash;
|
|
|
|
|
|
|
|
/* keep only base name, useful especially for vpath builds */
|
|
|
|
slash = strrchr(filename, '/');
|
|
|
|
if (slash)
|
|
|
|
filename = slash + 1;
|
|
|
|
/* Some Windows compilers use backslashes in __FILE__ strings */
|
|
|
|
slash = strrchr(filename, '\\');
|
|
|
|
if (slash)
|
|
|
|
filename = slash + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
edata->filename = filename;
|
|
|
|
edata->lineno = lineno;
|
|
|
|
edata->funcname = funcname;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* matches_backtrace_functions --- checks whether the given funcname matches
|
|
|
|
* backtrace_functions
|
|
|
|
*
|
|
|
|
* See check_backtrace_functions.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
matches_backtrace_functions(const char *funcname)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
2023-12-28 17:25:47 +01:00
|
|
|
if (!backtrace_function_list || funcname == NULL || funcname[0] == '\0')
|
2022-12-07 20:39:25 +01:00
|
|
|
return false;
|
|
|
|
|
2023-12-28 17:25:47 +01:00
|
|
|
p = backtrace_function_list;
|
2022-12-07 20:39:25 +01:00
|
|
|
for (;;)
|
|
|
|
{
|
2023-12-28 17:25:47 +01:00
|
|
|
if (*p == '\0') /* end of backtrace_function_list */
|
2022-12-07 20:39:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (strcmp(funcname, p) == 0)
|
|
|
|
return true;
|
|
|
|
p += strlen(p) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* errcode --- add SQLSTATE error code to the current error
|
|
|
|
*
|
|
|
|
* The code is expected to be represented as per MAKE_SQLSTATE().
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2003-04-24 23:16:45 +02:00
|
|
|
errcode(int sqlerrcode)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
edata->sqlerrcode = sqlerrcode;
|
2020-03-25 16:57:36 +01:00
|
|
|
|
|
|
|
return 0; /* return value does not matter */
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-19 01:20:33 +02:00
|
|
|
/*
|
|
|
|
* errcode_for_file_access --- add SQLSTATE error code to the current error
|
|
|
|
*
|
|
|
|
* The SQLSTATE code is chosen based on the saved errno value. We assume
|
|
|
|
* that the failing operation was some type of disk file access.
|
|
|
|
*
|
|
|
|
* NOTE: the primary error message string should generally include %m
|
|
|
|
* when this is used.
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2003-07-19 01:20:33 +02:00
|
|
|
errcode_for_file_access(void)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
switch (edata->saved_errno)
|
|
|
|
{
|
|
|
|
/* Permission-denied failures */
|
|
|
|
case EPERM: /* Not super-user */
|
|
|
|
case EACCES: /* Permission denied */
|
|
|
|
#ifdef EROFS
|
|
|
|
case EROFS: /* Read only file system */
|
|
|
|
#endif
|
|
|
|
edata->sqlerrcode = ERRCODE_INSUFFICIENT_PRIVILEGE;
|
|
|
|
break;
|
|
|
|
|
2003-08-26 23:15:27 +02:00
|
|
|
/* File not found */
|
2003-07-19 01:20:33 +02:00
|
|
|
case ENOENT: /* No such file or directory */
|
2003-08-26 23:15:27 +02:00
|
|
|
edata->sqlerrcode = ERRCODE_UNDEFINED_FILE;
|
2003-07-19 01:20:33 +02:00
|
|
|
break;
|
|
|
|
|
2003-08-26 23:15:27 +02:00
|
|
|
/* Duplicate file */
|
2003-07-19 01:20:33 +02:00
|
|
|
case EEXIST: /* File exists */
|
2003-08-26 23:15:27 +02:00
|
|
|
edata->sqlerrcode = ERRCODE_DUPLICATE_FILE;
|
2003-07-19 01:20:33 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Wrong object type or state */
|
|
|
|
case ENOTDIR: /* Not a directory */
|
|
|
|
case EISDIR: /* Is a directory */
|
2003-08-26 23:15:27 +02:00
|
|
|
case ENOTEMPTY: /* Directory not empty */
|
2003-07-19 01:20:33 +02:00
|
|
|
edata->sqlerrcode = ERRCODE_WRONG_OBJECT_TYPE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Insufficient resources */
|
|
|
|
case ENOSPC: /* No space left on device */
|
|
|
|
edata->sqlerrcode = ERRCODE_DISK_FULL;
|
|
|
|
break;
|
|
|
|
|
2024-02-02 21:34:29 +01:00
|
|
|
case ENOMEM: /* Out of memory */
|
|
|
|
edata->sqlerrcode = ERRCODE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
|
2003-07-19 01:20:33 +02:00
|
|
|
case ENFILE: /* File table overflow */
|
|
|
|
case EMFILE: /* Too many open files */
|
|
|
|
edata->sqlerrcode = ERRCODE_INSUFFICIENT_RESOURCES;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Hardware failure */
|
|
|
|
case EIO: /* I/O error */
|
|
|
|
edata->sqlerrcode = ERRCODE_IO_ERROR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* All else is classified as internal errors */
|
|
|
|
default:
|
|
|
|
edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2020-03-25 16:57:36 +01:00
|
|
|
|
|
|
|
return 0; /* return value does not matter */
|
2003-07-19 01:20:33 +02:00
|
|
|
}
|
|
|
|
|
2003-07-22 21:00:12 +02:00
|
|
|
/*
|
|
|
|
* errcode_for_socket_access --- add SQLSTATE error code to the current error
|
|
|
|
*
|
|
|
|
* The SQLSTATE code is chosen based on the saved errno value. We assume
|
|
|
|
* that the failing operation was some type of socket access.
|
|
|
|
*
|
|
|
|
* NOTE: the primary error message string should generally include %m
|
|
|
|
* when this is used.
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2003-07-22 21:00:12 +02:00
|
|
|
errcode_for_socket_access(void)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
switch (edata->saved_errno)
|
|
|
|
{
|
|
|
|
/* Loss of connection */
|
Recognize network-failure errnos as indicating hard connection loss.
Up to now, only ECONNRESET (and EPIPE, in most but not quite all places)
received special treatment in our error handling logic. This patch
changes things so that related error codes such as ECONNABORTED are
also recognized as indicating that the connection's dead and unlikely
to come back.
We continue to think, however, that only ECONNRESET and EPIPE should be
reported as probable server crashes; the other cases indicate network
connectivity problems but prove little about the server's state. Thus,
there's no change in the error message texts that are output for such
cases. The key practical effect is that errcode_for_socket_access()
will report ERRCODE_CONNECTION_FAILURE rather than
ERRCODE_INTERNAL_ERROR for a network failure. It's expected that this
will fix buildfarm member lorikeet's failures since commit 32a9c0bdf,
as that seems to be due to not treating ECONNABORTED equivalently to
ECONNRESET.
The set of errnos treated this way now includes ECONNABORTED, EHOSTDOWN,
EHOSTUNREACH, ENETDOWN, ENETRESET, and ENETUNREACH. Several of these
were second-class citizens in terms of their handling in places like
get_errno_symbol(), so upgrade the infrastructure where necessary.
As committed, this patch assumes that all these symbols are defined
everywhere. POSIX specifies all of them except EHOSTDOWN, but that
seems to exist on all platforms of interest; we'll see what the
buildfarm says about that.
Probably this should be back-patched, but let's see what the buildfarm
thinks of it first.
Fujii Masao and Tom Lane
Discussion: https://postgr.es/m/2621622.1602184554@sss.pgh.pa.us
2020-10-10 19:28:12 +02:00
|
|
|
case ALL_CONNECTION_FAILURE_ERRNOS:
|
2003-07-22 21:00:12 +02:00
|
|
|
edata->sqlerrcode = ERRCODE_CONNECTION_FAILURE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* All else is classified as internal errors */
|
|
|
|
default:
|
|
|
|
edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2020-03-25 16:57:36 +01:00
|
|
|
|
|
|
|
return 0; /* return value does not matter */
|
2003-07-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
2003-07-19 01:20:33 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
|
|
|
* This macro handles expansion of a format string and associated parameters;
|
|
|
|
* it's common code for errmsg(), errdetail(), etc. Must be called inside
|
|
|
|
* a routine that is declared like "const char *fmt, ..." and has an edata
|
|
|
|
* pointer set up. The message is assigned to edata->targetfield, or
|
2008-10-27 20:37:22 +01:00
|
|
|
* appended to it if appendval is true. The message is subject to translation
|
|
|
|
* if translateit is true.
|
2003-04-24 23:16:45 +02:00
|
|
|
*
|
|
|
|
* Note: we pstrdup the buffer rather than just transferring its storage
|
|
|
|
* to the edata field because the buffer might be considerably larger than
|
|
|
|
* really necessary.
|
|
|
|
*/
|
2012-11-12 14:10:24 +01:00
|
|
|
#define EVALUATE_MESSAGE(domain, targetfield, appendval, translateit) \
|
2003-04-24 23:16:45 +02:00
|
|
|
{ \
|
|
|
|
StringInfoData buf; \
|
|
|
|
/* Internationalize the error format string */ \
|
2015-01-12 18:40:09 +01:00
|
|
|
if ((translateit) && !in_error_recursion_trouble()) \
|
2012-11-12 14:10:24 +01:00
|
|
|
fmt = dgettext((domain), fmt); \
|
2003-04-24 23:16:45 +02:00
|
|
|
initStringInfo(&buf); \
|
2010-11-04 20:28:35 +01:00
|
|
|
if ((appendval) && edata->targetfield) { \
|
|
|
|
appendStringInfoString(&buf, edata->targetfield); \
|
|
|
|
appendStringInfoChar(&buf, '\n'); \
|
|
|
|
} \
|
2003-04-24 23:16:45 +02:00
|
|
|
/* Generate actual output --- have to use appendStringInfoVA */ \
|
|
|
|
for (;;) \
|
|
|
|
{ \
|
|
|
|
va_list args; \
|
2013-10-25 03:43:57 +02:00
|
|
|
int needed; \
|
2018-09-26 19:31:56 +02:00
|
|
|
errno = edata->saved_errno; \
|
2003-04-24 23:16:45 +02:00
|
|
|
va_start(args, fmt); \
|
2018-09-26 19:31:56 +02:00
|
|
|
needed = appendStringInfoVA(&buf, fmt, args); \
|
2003-04-24 23:16:45 +02:00
|
|
|
va_end(args); \
|
2013-10-25 03:43:57 +02:00
|
|
|
if (needed == 0) \
|
2003-04-24 23:16:45 +02:00
|
|
|
break; \
|
2013-10-25 03:43:57 +02:00
|
|
|
enlargeStringInfo(&buf, needed); \
|
2003-04-24 23:16:45 +02:00
|
|
|
} \
|
|
|
|
/* Save the completed message into the stack item */ \
|
|
|
|
if (edata->targetfield) \
|
|
|
|
pfree(edata->targetfield); \
|
|
|
|
edata->targetfield = pstrdup(buf.data); \
|
|
|
|
pfree(buf.data); \
|
|
|
|
}
|
|
|
|
|
2009-06-04 20:33:08 +02:00
|
|
|
/*
|
|
|
|
* Same as above, except for pluralized error messages. The calling routine
|
|
|
|
* must be declared like "const char *fmt_singular, const char *fmt_plural,
|
|
|
|
* unsigned long n, ...". Translation is assumed always wanted.
|
|
|
|
*/
|
2012-11-12 14:10:24 +01:00
|
|
|
#define EVALUATE_MESSAGE_PLURAL(domain, targetfield, appendval) \
|
2009-06-04 20:33:08 +02:00
|
|
|
{ \
|
|
|
|
const char *fmt; \
|
|
|
|
StringInfoData buf; \
|
|
|
|
/* Internationalize the error format string */ \
|
|
|
|
if (!in_error_recursion_trouble()) \
|
2012-11-12 14:10:24 +01:00
|
|
|
fmt = dngettext((domain), fmt_singular, fmt_plural, n); \
|
2009-06-04 20:33:08 +02:00
|
|
|
else \
|
|
|
|
fmt = (n == 1 ? fmt_singular : fmt_plural); \
|
|
|
|
initStringInfo(&buf); \
|
2010-11-04 20:28:35 +01:00
|
|
|
if ((appendval) && edata->targetfield) { \
|
|
|
|
appendStringInfoString(&buf, edata->targetfield); \
|
|
|
|
appendStringInfoChar(&buf, '\n'); \
|
|
|
|
} \
|
2009-06-04 20:33:08 +02:00
|
|
|
/* Generate actual output --- have to use appendStringInfoVA */ \
|
|
|
|
for (;;) \
|
|
|
|
{ \
|
|
|
|
va_list args; \
|
2013-10-25 03:43:57 +02:00
|
|
|
int needed; \
|
2018-09-26 19:31:56 +02:00
|
|
|
errno = edata->saved_errno; \
|
2009-06-04 20:33:08 +02:00
|
|
|
va_start(args, n); \
|
2018-09-26 19:31:56 +02:00
|
|
|
needed = appendStringInfoVA(&buf, fmt, args); \
|
2009-06-04 20:33:08 +02:00
|
|
|
va_end(args); \
|
2013-10-25 03:43:57 +02:00
|
|
|
if (needed == 0) \
|
2009-06-04 20:33:08 +02:00
|
|
|
break; \
|
2013-10-25 03:43:57 +02:00
|
|
|
enlargeStringInfo(&buf, needed); \
|
2009-06-04 20:33:08 +02:00
|
|
|
} \
|
|
|
|
/* Save the completed message into the stack item */ \
|
|
|
|
if (edata->targetfield) \
|
|
|
|
pfree(edata->targetfield); \
|
|
|
|
edata->targetfield = pstrdup(buf.data); \
|
|
|
|
pfree(buf.data); \
|
|
|
|
}
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* errmsg --- add a primary error message text to the current error
|
|
|
|
*
|
|
|
|
* In addition to the usual %-escapes recognized by printf, "%m" in
|
|
|
|
* fmt is replaced by the error message for the caller's value of errno.
|
|
|
|
*
|
|
|
|
* Note: no newline is needed at the end of the fmt string, since
|
|
|
|
* ereport will provide one for the output methods that need it.
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2003-04-24 23:16:45 +02:00
|
|
|
errmsg(const char *fmt,...)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
2013-08-01 07:07:20 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2016-03-11 10:53:06 +01:00
|
|
|
edata->message_id = fmt;
|
2012-11-12 14:10:24 +01:00
|
|
|
EVALUATE_MESSAGE(edata->domain, message, false, true);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
2020-03-25 16:57:36 +01:00
|
|
|
return 0; /* return value does not matter */
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
2019-11-08 19:44:20 +01:00
|
|
|
/*
|
|
|
|
* Add a backtrace to the containing ereport() call. This is intended to be
|
|
|
|
* added temporarily during debugging.
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2019-11-08 19:44:20 +01:00
|
|
|
errbacktrace(void)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
|
|
|
|
|
|
|
set_backtrace(edata, 1);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
2020-03-25 16:57:36 +01:00
|
|
|
|
|
|
|
return 0;
|
2019-11-08 19:44:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute backtrace data and add it to the supplied ErrorData. num_skip
|
|
|
|
* specifies how many inner frames to skip. Use this to avoid showing the
|
|
|
|
* internal backtrace support functions in the backtrace. This requires that
|
|
|
|
* this and related functions are not inlined.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
set_backtrace(ErrorData *edata, int num_skip)
|
|
|
|
{
|
|
|
|
StringInfoData errtrace;
|
|
|
|
|
|
|
|
initStringInfo(&errtrace);
|
|
|
|
|
|
|
|
#ifdef HAVE_BACKTRACE_SYMBOLS
|
|
|
|
{
|
|
|
|
void *buf[100];
|
|
|
|
int nframes;
|
|
|
|
char **strfrms;
|
|
|
|
|
|
|
|
nframes = backtrace(buf, lengthof(buf));
|
|
|
|
strfrms = backtrace_symbols(buf, nframes);
|
|
|
|
if (strfrms == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (int i = num_skip; i < nframes; i++)
|
|
|
|
appendStringInfo(&errtrace, "\n%s", strfrms[i]);
|
|
|
|
free(strfrms);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
appendStringInfoString(&errtrace,
|
|
|
|
"backtrace generation is not supported by this installation");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
edata->backtrace = errtrace.data;
|
|
|
|
}
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* errmsg_internal --- add a primary error message text to the current error
|
|
|
|
*
|
|
|
|
* This is exactly like errmsg() except that strings passed to errmsg_internal
|
2008-10-27 20:37:22 +01:00
|
|
|
* are not translated, and are customarily left out of the
|
|
|
|
* internationalization message dictionary. This should be used for "can't
|
|
|
|
* happen" cases that are probably not worth spending translation effort on.
|
|
|
|
* We also use this for certain cases where we *must* not try to translate
|
|
|
|
* the message because the translation would fail and result in infinite
|
|
|
|
* error recursion.
|
2003-04-24 23:16:45 +02:00
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2003-04-24 23:16:45 +02:00
|
|
|
errmsg_internal(const char *fmt,...)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
2013-08-01 07:07:20 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2016-03-11 10:53:06 +01:00
|
|
|
edata->message_id = fmt;
|
2012-11-12 14:10:24 +01:00
|
|
|
EVALUATE_MESSAGE(edata->domain, message, false, false);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
2020-03-25 16:57:36 +01:00
|
|
|
return 0; /* return value does not matter */
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-04 20:33:08 +02:00
|
|
|
/*
|
|
|
|
* errmsg_plural --- add a primary error message text to the current error,
|
|
|
|
* with support for pluralization of the message text
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2009-06-04 20:33:08 +02:00
|
|
|
errmsg_plural(const char *fmt_singular, const char *fmt_plural,
|
|
|
|
unsigned long n,...)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
2013-08-01 07:07:20 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
2009-06-04 20:33:08 +02:00
|
|
|
|
2016-03-11 10:53:06 +01:00
|
|
|
edata->message_id = fmt_singular;
|
2012-11-12 14:10:24 +01:00
|
|
|
EVALUATE_MESSAGE_PLURAL(edata->domain, message, false);
|
2009-06-04 20:33:08 +02:00
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
2020-03-25 16:57:36 +01:00
|
|
|
return 0; /* return value does not matter */
|
2009-06-04 20:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
|
|
|
* errdetail --- add a detail error message text to the current error
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2003-04-24 23:16:45 +02:00
|
|
|
errdetail(const char *fmt,...)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
2013-08-01 07:07:20 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2012-11-12 14:10:24 +01:00
|
|
|
EVALUATE_MESSAGE(edata->domain, detail, false, true);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
2020-03-25 16:57:36 +01:00
|
|
|
return 0; /* return value does not matter */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-06-08 23:16:49 +02:00
|
|
|
|
2011-07-16 19:41:48 +02:00
|
|
|
/*
|
|
|
|
* errdetail_internal --- add a detail error message text to the current error
|
|
|
|
*
|
|
|
|
* This is exactly like errdetail() except that strings passed to
|
|
|
|
* errdetail_internal are not translated, and are customarily left out of the
|
|
|
|
* internationalization message dictionary. This should be used for detail
|
|
|
|
* messages that seem not worth translating for one reason or another
|
|
|
|
* (typically, that they don't seem to be useful to average users).
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2011-07-16 19:41:48 +02:00
|
|
|
errdetail_internal(const char *fmt,...)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
2013-08-01 07:07:20 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
2011-07-16 19:41:48 +02:00
|
|
|
|
2012-11-12 14:10:24 +01:00
|
|
|
EVALUATE_MESSAGE(edata->domain, detail, false, false);
|
2011-07-16 19:41:48 +02:00
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
2020-03-25 16:57:36 +01:00
|
|
|
return 0; /* return value does not matter */
|
2011-07-16 19:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-24 19:08:47 +01:00
|
|
|
/*
|
|
|
|
* errdetail_log --- add a detail_log error message text to the current error
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2008-03-24 19:08:47 +01:00
|
|
|
errdetail_log(const char *fmt,...)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
2013-08-01 07:07:20 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
2008-03-24 19:08:47 +01:00
|
|
|
|
2012-11-12 14:10:24 +01:00
|
|
|
EVALUATE_MESSAGE(edata->domain, detail_log, false, true);
|
2008-03-24 19:08:47 +01:00
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
2020-03-25 16:57:36 +01:00
|
|
|
return 0; /* return value does not matter */
|
2008-03-24 19:08:47 +01:00
|
|
|
}
|
|
|
|
|
2014-03-12 19:26:47 +01:00
|
|
|
/*
|
|
|
|
* errdetail_log_plural --- add a detail_log error message text to the current error
|
|
|
|
* with support for pluralization of the message text
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2014-03-12 19:26:47 +01:00
|
|
|
errdetail_log_plural(const char *fmt_singular, const char *fmt_plural,
|
|
|
|
unsigned long n,...)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
|
|
|
|
|
|
|
EVALUATE_MESSAGE_PLURAL(edata->domain, detail_log, false);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
2020-03-25 16:57:36 +01:00
|
|
|
return 0; /* return value does not matter */
|
2014-03-12 19:26:47 +01:00
|
|
|
}
|
|
|
|
|
2008-03-24 19:08:47 +01:00
|
|
|
|
2009-06-04 20:33:08 +02:00
|
|
|
/*
|
|
|
|
* errdetail_plural --- add a detail error message text to the current error,
|
|
|
|
* with support for pluralization of the message text
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2009-06-04 20:33:08 +02:00
|
|
|
errdetail_plural(const char *fmt_singular, const char *fmt_plural,
|
|
|
|
unsigned long n,...)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
2013-08-01 07:07:20 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
2009-06-04 20:33:08 +02:00
|
|
|
|
2012-11-12 14:10:24 +01:00
|
|
|
EVALUATE_MESSAGE_PLURAL(edata->domain, detail, false);
|
2009-06-04 20:33:08 +02:00
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
2020-03-25 16:57:36 +01:00
|
|
|
return 0; /* return value does not matter */
|
2009-06-04 20:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
|
|
|
* errhint --- add a hint error message text to the current error
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2003-04-24 23:16:45 +02:00
|
|
|
errhint(const char *fmt,...)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
2013-08-01 07:07:20 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2012-11-12 14:10:24 +01:00
|
|
|
EVALUATE_MESSAGE(edata->domain, hint, false, true);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
2020-03-25 16:57:36 +01:00
|
|
|
return 0; /* return value does not matter */
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-31 09:15:51 +02:00
|
|
|
/*
|
|
|
|
* errhint_plural --- add a hint error message text to the current error,
|
|
|
|
* with support for pluralization of the message text
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
errhint_plural(const char *fmt_singular, const char *fmt_plural,
|
|
|
|
unsigned long n,...)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
|
|
|
|
|
|
|
EVALUATE_MESSAGE_PLURAL(edata->domain, hint, false);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
|
|
|
return 0; /* return value does not matter */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
2012-11-12 14:10:24 +01:00
|
|
|
* errcontext_msg --- add a context error message text to the current error
|
2003-04-24 23:16:45 +02:00
|
|
|
*
|
|
|
|
* Unlike other cases, multiple calls are allowed to build up a stack of
|
|
|
|
* context information. We assume earlier calls represent more-closely-nested
|
|
|
|
* states.
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2012-11-12 14:10:24 +01:00
|
|
|
errcontext_msg(const char *fmt,...)
|
2003-04-24 23:16:45 +02:00
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
2013-08-01 07:07:20 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2012-11-12 14:10:24 +01:00
|
|
|
EVALUATE_MESSAGE(edata->context_domain, context, true, true);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
2020-03-25 16:57:36 +01:00
|
|
|
return 0; /* return value does not matter */
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
2012-11-12 14:10:24 +01:00
|
|
|
/*
|
|
|
|
* set_errcontext_domain --- set message domain to be used by errcontext()
|
|
|
|
*
|
|
|
|
* errcontext_msg() can be called from a different module than the original
|
|
|
|
* ereport(), so we cannot use the message domain passed in errstart() to
|
|
|
|
* translate it. Instead, each errcontext_msg() call should be preceded by
|
|
|
|
* a set_errcontext_domain() call to specify the domain. This is usually
|
|
|
|
* done transparently by the errcontext() macro.
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2012-11-12 14:10:24 +01:00
|
|
|
set_errcontext_domain(const char *domain)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
2015-01-12 18:40:09 +01:00
|
|
|
/* the default text domain is the backend's */
|
|
|
|
edata->context_domain = domain ? domain : PG_TEXTDOMAIN("postgres");
|
2020-03-25 16:57:36 +01:00
|
|
|
|
|
|
|
return 0; /* return value does not matter */
|
2012-11-12 14:10:24 +01:00
|
|
|
}
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2007-03-03 00:37:23 +01:00
|
|
|
/*
|
|
|
|
* errhidestmt --- optionally suppress STATEMENT: field of log entry
|
|
|
|
*
|
|
|
|
* This should be called if the message text already includes the statement.
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2007-03-03 00:37:23 +01:00
|
|
|
errhidestmt(bool hide_stmt)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
edata->hide_stmt = hide_stmt;
|
2020-03-25 16:57:36 +01:00
|
|
|
|
|
|
|
return 0; /* return value does not matter */
|
2007-03-03 00:37:23 +01:00
|
|
|
}
|
|
|
|
|
2014-12-25 17:24:30 +01:00
|
|
|
/*
|
2015-01-05 08:35:26 +01:00
|
|
|
* errhidecontext --- optionally suppress CONTEXT: field of log entry
|
2014-12-25 17:24:30 +01:00
|
|
|
*
|
|
|
|
* This should only be used for verbose debugging messages where the repeated
|
2016-03-28 20:18:00 +02:00
|
|
|
* inclusion of context would bloat the log volume too much.
|
2014-12-25 17:24:30 +01:00
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2014-12-25 17:24:30 +01:00
|
|
|
errhidecontext(bool hide_ctx)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
edata->hide_ctx = hide_ctx;
|
2020-03-25 16:57:36 +01:00
|
|
|
|
|
|
|
return 0; /* return value does not matter */
|
2014-12-25 17:24:30 +01:00
|
|
|
}
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
|
|
|
* errposition --- add cursor position to the current error
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2003-04-24 23:16:45 +02:00
|
|
|
errposition(int cursorpos)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
edata->cursorpos = cursorpos;
|
2020-03-25 16:57:36 +01:00
|
|
|
|
|
|
|
return 0; /* return value does not matter */
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
2004-03-21 23:29:11 +01:00
|
|
|
/*
|
|
|
|
* internalerrposition --- add internal cursor position to the current error
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2004-03-21 23:29:11 +01:00
|
|
|
internalerrposition(int cursorpos)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
edata->internalpos = cursorpos;
|
2020-03-25 16:57:36 +01:00
|
|
|
|
|
|
|
return 0; /* return value does not matter */
|
2004-03-21 23:29:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* internalerrquery --- add internal query text to the current error
|
|
|
|
*
|
|
|
|
* Can also pass NULL to drop the internal query text entry. This case
|
|
|
|
* is intended for use in error callback subroutines that are editorializing
|
|
|
|
* on the layout of the error report.
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
2004-03-21 23:29:11 +01:00
|
|
|
internalerrquery(const char *query)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
if (edata->internalquery)
|
|
|
|
{
|
|
|
|
pfree(edata->internalquery);
|
|
|
|
edata->internalquery = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query)
|
2013-08-01 07:07:20 +02:00
|
|
|
edata->internalquery = MemoryContextStrdup(edata->assoc_context, query);
|
2020-03-25 16:57:36 +01:00
|
|
|
|
|
|
|
return 0; /* return value does not matter */
|
2004-03-21 23:29:11 +01:00
|
|
|
}
|
|
|
|
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
/*
|
|
|
|
* err_generic_string -- used to set individual ErrorData string fields
|
|
|
|
* identified by PG_DIAG_xxx codes.
|
|
|
|
*
|
|
|
|
* This intentionally only supports fields that don't use localized strings,
|
|
|
|
* so that there are no translation considerations.
|
|
|
|
*
|
|
|
|
* Most potential callers should not use this directly, but instead prefer
|
|
|
|
* higher-level abstractions, such as errtablecol() (see relcache.c).
|
|
|
|
*/
|
2020-03-25 16:57:36 +01:00
|
|
|
int
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
err_generic_string(int field, const char *str)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
switch (field)
|
|
|
|
{
|
|
|
|
case PG_DIAG_SCHEMA_NAME:
|
2013-08-01 07:07:20 +02:00
|
|
|
set_errdata_field(edata->assoc_context, &edata->schema_name, str);
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
break;
|
|
|
|
case PG_DIAG_TABLE_NAME:
|
2013-08-01 07:07:20 +02:00
|
|
|
set_errdata_field(edata->assoc_context, &edata->table_name, str);
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
break;
|
|
|
|
case PG_DIAG_COLUMN_NAME:
|
2013-08-01 07:07:20 +02:00
|
|
|
set_errdata_field(edata->assoc_context, &edata->column_name, str);
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
break;
|
|
|
|
case PG_DIAG_DATATYPE_NAME:
|
2013-08-01 07:07:20 +02:00
|
|
|
set_errdata_field(edata->assoc_context, &edata->datatype_name, str);
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
break;
|
|
|
|
case PG_DIAG_CONSTRAINT_NAME:
|
2013-08-01 07:07:20 +02:00
|
|
|
set_errdata_field(edata->assoc_context, &edata->constraint_name, str);
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unsupported ErrorData field id: %d", field);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-25 16:57:36 +01:00
|
|
|
|
|
|
|
return 0; /* return value does not matter */
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set_errdata_field --- set an ErrorData string field
|
|
|
|
*/
|
|
|
|
static void
|
2013-08-01 07:07:20 +02:00
|
|
|
set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str)
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
{
|
|
|
|
Assert(*ptr == NULL);
|
2013-08-01 07:07:20 +02:00
|
|
|
*ptr = MemoryContextStrdup(cxt, str);
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
}
|
|
|
|
|
2008-09-01 22:42:46 +02:00
|
|
|
/*
|
|
|
|
* geterrcode --- return the currently set SQLSTATE error code
|
|
|
|
*
|
|
|
|
* This is only intended for use in error callback subroutines, since there
|
|
|
|
* is no other place outside elog.c where the concept is meaningful.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
geterrcode(void)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
return edata->sqlerrcode;
|
|
|
|
}
|
|
|
|
|
2004-03-21 23:29:11 +01:00
|
|
|
/*
|
|
|
|
* geterrposition --- return the currently set error position (0 if none)
|
|
|
|
*
|
|
|
|
* This is only intended for use in error callback subroutines, since there
|
|
|
|
* is no other place outside elog.c where the concept is meaningful.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
geterrposition(void)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
return edata->cursorpos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getinternalerrposition --- same for internal error position
|
|
|
|
*
|
|
|
|
* This is only intended for use in error callback subroutines, since there
|
|
|
|
* is no other place outside elog.c where the concept is meaningful.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
getinternalerrposition(void)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
/* we don't bother incrementing recursion_depth */
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
return edata->internalpos;
|
|
|
|
}
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2011-04-07 06:11:01 +02:00
|
|
|
/*
|
|
|
|
* Functions to allow construction of error message strings separately from
|
|
|
|
* the ereport() call itself.
|
|
|
|
*
|
|
|
|
* The expected calling convention is
|
|
|
|
*
|
|
|
|
* pre_format_elog_string(errno, domain), var = format_elog_string(format,...)
|
|
|
|
*
|
|
|
|
* which can be hidden behind a macro such as GUC_check_errdetail(). We
|
|
|
|
* assume that any functions called in the arguments of format_elog_string()
|
|
|
|
* cannot result in re-entrant use of these functions --- otherwise the wrong
|
|
|
|
* text domain might be used, or the wrong errno substituted for %m. This is
|
|
|
|
* okay for the current usage with GUC check hooks, but might need further
|
|
|
|
* effort someday.
|
|
|
|
*
|
|
|
|
* The result of format_elog_string() is stored in ErrorContext, and will
|
|
|
|
* therefore survive until FlushErrorState() is called.
|
|
|
|
*/
|
|
|
|
static int save_format_errnumber;
|
|
|
|
static const char *save_format_domain;
|
|
|
|
|
|
|
|
void
|
|
|
|
pre_format_elog_string(int errnumber, const char *domain)
|
|
|
|
{
|
|
|
|
/* Save errno before evaluation of argument functions can change it */
|
|
|
|
save_format_errnumber = errnumber;
|
|
|
|
/* Save caller's text domain */
|
|
|
|
save_format_domain = domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
format_elog_string(const char *fmt,...)
|
|
|
|
{
|
|
|
|
ErrorData errdata;
|
|
|
|
ErrorData *edata;
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
/* Initialize a mostly-dummy error frame */
|
|
|
|
edata = &errdata;
|
|
|
|
MemSet(edata, 0, sizeof(ErrorData));
|
|
|
|
/* the default text domain is the backend's */
|
|
|
|
edata->domain = save_format_domain ? save_format_domain : PG_TEXTDOMAIN("postgres");
|
|
|
|
/* set the errno to be used to interpret %m */
|
|
|
|
edata->saved_errno = save_format_errnumber;
|
|
|
|
|
|
|
|
oldcontext = MemoryContextSwitchTo(ErrorContext);
|
|
|
|
|
2016-03-11 10:53:06 +01:00
|
|
|
edata->message_id = fmt;
|
2012-11-12 14:10:24 +01:00
|
|
|
EVALUATE_MESSAGE(edata->domain, message, false, true);
|
2011-04-07 06:11:01 +02:00
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
|
|
|
|
return edata->message;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
/*
|
|
|
|
* Actual output of the top-of-stack error message
|
|
|
|
*
|
|
|
|
* In the ereport(ERROR) case this is called from PostgresMain (or not at all,
|
|
|
|
* if the error is caught by somebody). For all other severity levels this
|
|
|
|
* is called by errfinish.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
EmitErrorReport(void)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
recursion_depth++;
|
|
|
|
CHECK_STACK_DEPTH();
|
2013-08-01 07:07:20 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
|
2004-07-31 02:45:57 +02:00
|
|
|
|
2012-03-06 21:35:41 +01:00
|
|
|
/*
|
|
|
|
* Call hook before sending message to log. The hook function is allowed
|
|
|
|
* to turn off edata->output_to_server, so we must recheck that afterward.
|
|
|
|
* Making any other change in the content of edata is not considered
|
|
|
|
* supported.
|
|
|
|
*
|
|
|
|
* Note: the reason why the hook can only turn off output_to_server, and
|
|
|
|
* not turn it on, is that it'd be unreliable: we will never get here at
|
|
|
|
* all if errstart() deems the message uninteresting. A hook that could
|
|
|
|
* make decisions in that direction would have to hook into errstart(),
|
|
|
|
* where it would have much less information available. emit_log_hook is
|
|
|
|
* intended for custom log filtering and custom log message transmission
|
|
|
|
* mechanisms.
|
2016-03-11 10:53:06 +01:00
|
|
|
*
|
|
|
|
* The log hook has access to both the translated and original English
|
|
|
|
* error message text, which is passed through to allow it to be used as a
|
|
|
|
* message identifier. Note that the original text is not available for
|
|
|
|
* detail, detail_log, hint and context text elements.
|
2012-03-06 21:35:41 +01:00
|
|
|
*/
|
|
|
|
if (edata->output_to_server && emit_log_hook)
|
|
|
|
(*emit_log_hook) (edata);
|
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
/* Send to server log, if enabled */
|
|
|
|
if (edata->output_to_server)
|
|
|
|
send_message_to_server_log(edata);
|
|
|
|
|
|
|
|
/* Send to client, if enabled */
|
|
|
|
if (edata->output_to_client)
|
|
|
|
send_message_to_frontend(edata);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
recursion_depth--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CopyErrorData --- obtain a copy of the topmost error stack entry
|
|
|
|
*
|
|
|
|
* This is only for use in error handler code. The data is copied into the
|
|
|
|
* current memory context, so callers should always switch away from
|
|
|
|
* ErrorContext first; otherwise it will be lost when FlushErrorState is done.
|
|
|
|
*/
|
|
|
|
ErrorData *
|
|
|
|
CopyErrorData(void)
|
|
|
|
{
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
ErrorData *newedata;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we don't increment recursion_depth because out-of-memory here does not
|
|
|
|
* indicate a problem within the error subsystem.
|
|
|
|
*/
|
|
|
|
CHECK_STACK_DEPTH();
|
|
|
|
|
|
|
|
Assert(CurrentMemoryContext != ErrorContext);
|
|
|
|
|
|
|
|
/* Copy the struct itself */
|
|
|
|
newedata = (ErrorData *) palloc(sizeof(ErrorData));
|
|
|
|
memcpy(newedata, edata, sizeof(ErrorData));
|
|
|
|
|
|
|
|
/* Make copies of separately-allocated fields */
|
|
|
|
if (newedata->message)
|
|
|
|
newedata->message = pstrdup(newedata->message);
|
|
|
|
if (newedata->detail)
|
|
|
|
newedata->detail = pstrdup(newedata->detail);
|
2008-03-24 19:08:47 +01:00
|
|
|
if (newedata->detail_log)
|
|
|
|
newedata->detail_log = pstrdup(newedata->detail_log);
|
2004-07-31 02:45:57 +02:00
|
|
|
if (newedata->hint)
|
|
|
|
newedata->hint = pstrdup(newedata->hint);
|
|
|
|
if (newedata->context)
|
|
|
|
newedata->context = pstrdup(newedata->context);
|
2019-11-08 19:44:20 +01:00
|
|
|
if (newedata->backtrace)
|
|
|
|
newedata->backtrace = pstrdup(newedata->backtrace);
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
if (newedata->schema_name)
|
|
|
|
newedata->schema_name = pstrdup(newedata->schema_name);
|
|
|
|
if (newedata->table_name)
|
|
|
|
newedata->table_name = pstrdup(newedata->table_name);
|
|
|
|
if (newedata->column_name)
|
|
|
|
newedata->column_name = pstrdup(newedata->column_name);
|
|
|
|
if (newedata->datatype_name)
|
|
|
|
newedata->datatype_name = pstrdup(newedata->datatype_name);
|
|
|
|
if (newedata->constraint_name)
|
|
|
|
newedata->constraint_name = pstrdup(newedata->constraint_name);
|
2004-07-31 02:45:57 +02:00
|
|
|
if (newedata->internalquery)
|
|
|
|
newedata->internalquery = pstrdup(newedata->internalquery);
|
|
|
|
|
2013-08-01 07:07:20 +02:00
|
|
|
/* Use the calling context for string allocation */
|
|
|
|
newedata->assoc_context = CurrentMemoryContext;
|
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
return newedata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FreeErrorData --- free the structure returned by CopyErrorData.
|
|
|
|
*
|
|
|
|
* Error handlers should use this in preference to assuming they know all
|
|
|
|
* the separately-allocated fields.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
FreeErrorData(ErrorData *edata)
|
2022-12-07 20:39:25 +01:00
|
|
|
{
|
|
|
|
FreeErrorDataContents(edata);
|
|
|
|
pfree(edata);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FreeErrorDataContents --- free the subsidiary data of an ErrorData.
|
|
|
|
*
|
|
|
|
* This can be used on either an error stack entry or a copied ErrorData.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
FreeErrorDataContents(ErrorData *edata)
|
2004-07-31 02:45:57 +02:00
|
|
|
{
|
|
|
|
if (edata->message)
|
|
|
|
pfree(edata->message);
|
|
|
|
if (edata->detail)
|
|
|
|
pfree(edata->detail);
|
2008-03-24 19:08:47 +01:00
|
|
|
if (edata->detail_log)
|
|
|
|
pfree(edata->detail_log);
|
2004-07-31 02:45:57 +02:00
|
|
|
if (edata->hint)
|
|
|
|
pfree(edata->hint);
|
|
|
|
if (edata->context)
|
|
|
|
pfree(edata->context);
|
2019-11-08 19:44:20 +01:00
|
|
|
if (edata->backtrace)
|
|
|
|
pfree(edata->backtrace);
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
if (edata->schema_name)
|
|
|
|
pfree(edata->schema_name);
|
|
|
|
if (edata->table_name)
|
|
|
|
pfree(edata->table_name);
|
|
|
|
if (edata->column_name)
|
|
|
|
pfree(edata->column_name);
|
|
|
|
if (edata->datatype_name)
|
|
|
|
pfree(edata->datatype_name);
|
|
|
|
if (edata->constraint_name)
|
|
|
|
pfree(edata->constraint_name);
|
2004-07-31 02:45:57 +02:00
|
|
|
if (edata->internalquery)
|
|
|
|
pfree(edata->internalquery);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FlushErrorState --- flush the error state after error recovery
|
|
|
|
*
|
|
|
|
* This should be called by an error handler after it's done processing
|
|
|
|
* the error; or as soon as it's done CopyErrorData, if it intends to
|
|
|
|
* do stuff that is likely to provoke another error. You are not "out" of
|
|
|
|
* the error subsystem until you have done this.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
FlushErrorState(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Reset stack to empty. The only case where it would be more than one
|
|
|
|
* deep is if we serviced an error that interrupted construction of
|
|
|
|
* another message. We assume control escaped out of that message
|
|
|
|
* construction and won't ever go back.
|
|
|
|
*/
|
|
|
|
errordata_stack_depth = -1;
|
|
|
|
recursion_depth = 0;
|
|
|
|
/* Delete all data in ErrorContext */
|
2023-11-15 20:42:30 +01:00
|
|
|
MemoryContextReset(ErrorContext);
|
2004-07-31 02:45:57 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 17:02:40 +01:00
|
|
|
/*
|
|
|
|
* ThrowErrorData --- report an error described by an ErrorData structure
|
|
|
|
*
|
2016-08-26 20:15:47 +02:00
|
|
|
* This is somewhat like ReThrowError, but it allows elevels besides ERROR,
|
|
|
|
* and the boolean flags such as output_to_server are computed via the
|
|
|
|
* default rules rather than being copied from the given ErrorData.
|
|
|
|
* This is primarily used to re-report errors originally reported by
|
2014-10-31 17:02:40 +01:00
|
|
|
* background worker processes and then propagated (with or without
|
|
|
|
* modification) to the backend responsible for them.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ThrowErrorData(ErrorData *edata)
|
|
|
|
{
|
|
|
|
ErrorData *newedata;
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
2020-03-24 17:08:48 +01:00
|
|
|
if (!errstart(edata->elevel, edata->domain))
|
2016-08-26 20:15:47 +02:00
|
|
|
return; /* error is not to be reported at all */
|
2014-10-31 17:02:40 +01:00
|
|
|
|
|
|
|
newedata = &errordata[errordata_stack_depth];
|
2016-08-26 20:15:47 +02:00
|
|
|
recursion_depth++;
|
|
|
|
oldcontext = MemoryContextSwitchTo(newedata->assoc_context);
|
2014-10-31 17:02:40 +01:00
|
|
|
|
2016-08-26 20:15:47 +02:00
|
|
|
/* Copy the supplied fields to the error stack entry. */
|
|
|
|
if (edata->sqlerrcode != 0)
|
2014-10-31 17:02:40 +01:00
|
|
|
newedata->sqlerrcode = edata->sqlerrcode;
|
|
|
|
if (edata->message)
|
|
|
|
newedata->message = pstrdup(edata->message);
|
|
|
|
if (edata->detail)
|
|
|
|
newedata->detail = pstrdup(edata->detail);
|
|
|
|
if (edata->detail_log)
|
|
|
|
newedata->detail_log = pstrdup(edata->detail_log);
|
|
|
|
if (edata->hint)
|
|
|
|
newedata->hint = pstrdup(edata->hint);
|
|
|
|
if (edata->context)
|
|
|
|
newedata->context = pstrdup(edata->context);
|
2019-11-08 19:44:20 +01:00
|
|
|
if (edata->backtrace)
|
|
|
|
newedata->backtrace = pstrdup(edata->backtrace);
|
2016-08-26 20:15:47 +02:00
|
|
|
/* assume message_id is not available */
|
2014-10-31 17:02:40 +01:00
|
|
|
if (edata->schema_name)
|
|
|
|
newedata->schema_name = pstrdup(edata->schema_name);
|
|
|
|
if (edata->table_name)
|
|
|
|
newedata->table_name = pstrdup(edata->table_name);
|
|
|
|
if (edata->column_name)
|
|
|
|
newedata->column_name = pstrdup(edata->column_name);
|
|
|
|
if (edata->datatype_name)
|
|
|
|
newedata->datatype_name = pstrdup(edata->datatype_name);
|
|
|
|
if (edata->constraint_name)
|
|
|
|
newedata->constraint_name = pstrdup(edata->constraint_name);
|
2016-08-26 20:15:47 +02:00
|
|
|
newedata->cursorpos = edata->cursorpos;
|
|
|
|
newedata->internalpos = edata->internalpos;
|
2014-10-31 17:02:40 +01:00
|
|
|
if (edata->internalquery)
|
|
|
|
newedata->internalquery = pstrdup(edata->internalquery);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2016-08-26 20:15:47 +02:00
|
|
|
recursion_depth--;
|
2014-10-31 17:02:40 +01:00
|
|
|
|
2016-08-26 20:15:47 +02:00
|
|
|
/* Process the error. */
|
2020-03-24 17:08:48 +01:00
|
|
|
errfinish(edata->filename, edata->lineno, edata->funcname);
|
2014-10-31 17:02:40 +01:00
|
|
|
}
|
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
/*
|
|
|
|
* ReThrowError --- re-throw a previously copied error
|
|
|
|
*
|
|
|
|
* A handler can do CopyErrorData/FlushErrorState to get out of the error
|
|
|
|
* subsystem, then do some processing, and finally ReThrowError to re-throw
|
|
|
|
* the original error. This is slower than just PG_RE_THROW() but should
|
|
|
|
* be used if the "some processing" is likely to incur another error.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ReThrowError(ErrorData *edata)
|
|
|
|
{
|
|
|
|
ErrorData *newedata;
|
|
|
|
|
|
|
|
Assert(edata->elevel == ERROR);
|
|
|
|
|
|
|
|
/* Push the data back into the error context */
|
|
|
|
recursion_depth++;
|
|
|
|
MemoryContextSwitchTo(ErrorContext);
|
|
|
|
|
2022-12-07 20:39:25 +01:00
|
|
|
newedata = get_error_stack_entry();
|
2004-07-31 02:45:57 +02:00
|
|
|
memcpy(newedata, edata, sizeof(ErrorData));
|
|
|
|
|
|
|
|
/* Make copies of separately-allocated fields */
|
|
|
|
if (newedata->message)
|
|
|
|
newedata->message = pstrdup(newedata->message);
|
|
|
|
if (newedata->detail)
|
|
|
|
newedata->detail = pstrdup(newedata->detail);
|
2008-03-24 19:08:47 +01:00
|
|
|
if (newedata->detail_log)
|
|
|
|
newedata->detail_log = pstrdup(newedata->detail_log);
|
2004-07-31 02:45:57 +02:00
|
|
|
if (newedata->hint)
|
|
|
|
newedata->hint = pstrdup(newedata->hint);
|
|
|
|
if (newedata->context)
|
|
|
|
newedata->context = pstrdup(newedata->context);
|
2019-11-08 19:44:20 +01:00
|
|
|
if (newedata->backtrace)
|
|
|
|
newedata->backtrace = pstrdup(newedata->backtrace);
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
if (newedata->schema_name)
|
|
|
|
newedata->schema_name = pstrdup(newedata->schema_name);
|
|
|
|
if (newedata->table_name)
|
|
|
|
newedata->table_name = pstrdup(newedata->table_name);
|
|
|
|
if (newedata->column_name)
|
|
|
|
newedata->column_name = pstrdup(newedata->column_name);
|
|
|
|
if (newedata->datatype_name)
|
|
|
|
newedata->datatype_name = pstrdup(newedata->datatype_name);
|
|
|
|
if (newedata->constraint_name)
|
|
|
|
newedata->constraint_name = pstrdup(newedata->constraint_name);
|
2004-07-31 02:45:57 +02:00
|
|
|
if (newedata->internalquery)
|
|
|
|
newedata->internalquery = pstrdup(newedata->internalquery);
|
|
|
|
|
2013-08-01 07:07:20 +02:00
|
|
|
/* Reset the assoc_context to be ErrorContext */
|
|
|
|
newedata->assoc_context = ErrorContext;
|
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
recursion_depth--;
|
|
|
|
PG_RE_THROW();
|
|
|
|
}
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2007-05-02 17:32:42 +02:00
|
|
|
/*
|
|
|
|
* pg_re_throw --- out-of-line implementation of PG_RE_THROW() macro
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pg_re_throw(void)
|
|
|
|
{
|
|
|
|
/* If possible, throw the error to the next outer setjmp handler */
|
|
|
|
if (PG_exception_stack != NULL)
|
|
|
|
siglongjmp(*PG_exception_stack, 1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we get here, elog(ERROR) was thrown inside a PG_TRY block, which
|
|
|
|
* we have now exited only to discover that there is no outer setjmp
|
|
|
|
* handler to pass the error to. Had the error been thrown outside
|
|
|
|
* the block to begin with, we'd have promoted the error to FATAL, so
|
|
|
|
* the correct behavior is to make it FATAL now; that is, emit it and
|
|
|
|
* then call proc_exit.
|
|
|
|
*/
|
|
|
|
ErrorData *edata = &errordata[errordata_stack_depth];
|
|
|
|
|
|
|
|
Assert(errordata_stack_depth >= 0);
|
|
|
|
Assert(edata->elevel == ERROR);
|
|
|
|
edata->elevel = FATAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At least in principle, the increase in severity could have changed
|
2020-11-24 01:04:07 +01:00
|
|
|
* where-to-output decisions, so recalculate.
|
2007-05-02 17:32:42 +02:00
|
|
|
*/
|
2020-11-24 01:04:07 +01:00
|
|
|
edata->output_to_server = should_output_to_server(FATAL);
|
|
|
|
edata->output_to_client = should_output_to_client(FATAL);
|
2007-05-02 17:32:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We can use errfinish() for the rest, but we don't want it to call
|
|
|
|
* any error context routines a second time. Since we know we are
|
|
|
|
* about to exit, it should be OK to just clear the context stack.
|
|
|
|
*/
|
|
|
|
error_context_stack = NULL;
|
|
|
|
|
2020-03-24 17:08:48 +01:00
|
|
|
errfinish(edata->filename, edata->lineno, edata->funcname);
|
2007-05-02 17:32:42 +02:00
|
|
|
}
|
2007-05-04 04:01:02 +02:00
|
|
|
|
2012-04-29 20:07:35 +02:00
|
|
|
/* Doesn't return ... */
|
2022-10-10 21:16:56 +02:00
|
|
|
ExceptionalCondition("pg_re_throw tried to return", __FILE__, __LINE__);
|
2007-05-02 17:32:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-25 00:53:27 +02:00
|
|
|
/*
|
2013-07-25 15:41:55 +02:00
|
|
|
* GetErrorContextStack - Return the context stack, for display/diags
|
2013-07-25 00:53:27 +02:00
|
|
|
*
|
2013-07-25 15:41:55 +02:00
|
|
|
* Returns a pstrdup'd string in the caller's context which includes the PG
|
2013-08-01 07:07:20 +02:00
|
|
|
* error call stack. It is the caller's responsibility to ensure this string
|
|
|
|
* is pfree'd (or its context cleaned up) when done.
|
2013-07-25 15:41:55 +02:00
|
|
|
*
|
2013-07-25 00:53:27 +02:00
|
|
|
* This information is collected by traversing the error contexts and calling
|
|
|
|
* each context's callback function, each of which is expected to call
|
|
|
|
* errcontext() to return a string which can be presented to the user.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
GetErrorContextStack(void)
|
|
|
|
{
|
|
|
|
ErrorData *edata;
|
|
|
|
ErrorContextCallback *econtext;
|
|
|
|
|
2013-07-25 15:41:55 +02:00
|
|
|
/*
|
2022-12-07 20:39:25 +01:00
|
|
|
* Crank up a stack entry to store the info in.
|
2013-07-25 15:41:55 +02:00
|
|
|
*/
|
2013-08-01 07:07:20 +02:00
|
|
|
recursion_depth++;
|
|
|
|
|
2022-12-07 20:39:25 +01:00
|
|
|
edata = get_error_stack_entry();
|
2013-07-25 00:53:27 +02:00
|
|
|
|
2013-07-25 15:41:55 +02:00
|
|
|
/*
|
2013-08-01 07:07:20 +02:00
|
|
|
* Set up assoc_context to be the caller's context, so any allocations
|
|
|
|
* done (which will include edata->context) will use their context.
|
2013-07-25 15:41:55 +02:00
|
|
|
*/
|
2013-08-01 07:07:20 +02:00
|
|
|
edata->assoc_context = CurrentMemoryContext;
|
2013-07-25 00:53:27 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Call any context callback functions to collect the context information
|
|
|
|
* into edata->context.
|
|
|
|
*
|
|
|
|
* Errors occurring in callback functions should go through the regular
|
2013-08-01 07:07:20 +02:00
|
|
|
* error handling code which should handle any recursive errors, though we
|
|
|
|
* double-check above, just in case.
|
2013-07-25 00:53:27 +02:00
|
|
|
*/
|
|
|
|
for (econtext = error_context_stack;
|
|
|
|
econtext != NULL;
|
|
|
|
econtext = econtext->previous)
|
2017-09-07 18:06:23 +02:00
|
|
|
econtext->callback(econtext->arg);
|
2013-07-25 00:53:27 +02:00
|
|
|
|
|
|
|
/*
|
2013-08-01 07:07:20 +02:00
|
|
|
* Clean ourselves off the stack, any allocations done should have been
|
|
|
|
* using edata->assoc_context, which we set up earlier to be the caller's
|
|
|
|
* context, so we're free to just remove our entry off the stack and
|
|
|
|
* decrement recursion depth and exit.
|
2013-07-25 00:53:27 +02:00
|
|
|
*/
|
2013-08-01 07:07:20 +02:00
|
|
|
errordata_stack_depth--;
|
|
|
|
recursion_depth--;
|
2013-07-25 00:53:27 +02:00
|
|
|
|
2013-07-25 15:41:55 +02:00
|
|
|
/*
|
2013-08-01 07:07:20 +02:00
|
|
|
* Return a pointer to the string the caller asked for, which should have
|
|
|
|
* been allocated in their context.
|
2013-07-25 15:41:55 +02:00
|
|
|
*/
|
2013-08-01 07:07:20 +02:00
|
|
|
return edata->context;
|
2013-07-25 00:53:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
|
|
|
* Initialization of error output file
|
|
|
|
*/
|
|
|
|
void
|
1996-11-10 04:06:38 +01:00
|
|
|
DebugFileOpen(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
int fd,
|
|
|
|
istty;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
if (OutputFileName[0])
|
|
|
|
{
|
2000-11-25 20:09:22 +01:00
|
|
|
/*
|
|
|
|
* A debug-output file name was given.
|
|
|
|
*
|
|
|
|
* Make sure we can write the file, and find out if it's a tty.
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
if ((fd = open(OutputFileName, O_CREAT | O_APPEND | O_WRONLY,
|
|
|
|
0666)) < 0)
|
2003-04-24 23:16:45 +02:00
|
|
|
ereport(FATAL,
|
2003-07-19 01:20:33 +02:00
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not open file \"%s\": %m", OutputFileName)));
|
1996-07-09 08:22:35 +02:00
|
|
|
istty = isatty(fd);
|
1997-08-13 00:55:25 +02:00
|
|
|
close(fd);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-25 20:09:22 +01:00
|
|
|
/*
|
|
|
|
* Redirect our stderr to the debug output file.
|
|
|
|
*/
|
|
|
|
if (!freopen(OutputFileName, "a", stderr))
|
2003-04-24 23:16:45 +02:00
|
|
|
ereport(FATAL,
|
2003-07-19 01:20:33 +02:00
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not reopen file \"%s\" as stderr: %m",
|
2003-04-24 23:16:45 +02:00
|
|
|
OutputFileName)));
|
2001-03-22 05:01:46 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
|
|
|
* If the file is a tty and we're running under the postmaster, try to
|
|
|
|
* send stdout there as well (if it isn't a tty then stderr will block
|
|
|
|
* out stdout, so we may as well let stdout go wherever it was going
|
|
|
|
* before).
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-11-25 20:09:22 +01:00
|
|
|
if (istty && IsUnderPostmaster)
|
|
|
|
if (!freopen(OutputFileName, "a", stdout))
|
2003-04-24 23:16:45 +02:00
|
|
|
ereport(FATAL,
|
2003-07-19 01:20:33 +02:00
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not reopen file \"%s\" as stdout: %m",
|
2003-04-24 23:16:45 +02:00
|
|
|
OutputFileName)));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-06-04 17:06:34 +02:00
|
|
|
}
|
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
/*
|
|
|
|
* GUC check_hook for backtrace_functions
|
|
|
|
*
|
|
|
|
* We split the input string, where commas separate function names
|
|
|
|
* and certain whitespace chars are ignored, into a \0-separated (and
|
|
|
|
* \0\0-terminated) list of function names. This formulation allows
|
|
|
|
* easy scanning when an error is thrown while avoiding the use of
|
|
|
|
* non-reentrant strtok(), as well as keeping the output data in a
|
|
|
|
* single palloc() chunk.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
check_backtrace_functions(char **newval, void **extra, GucSource source)
|
|
|
|
{
|
|
|
|
int newvallen = strlen(*newval);
|
|
|
|
char *someval;
|
|
|
|
int validlen;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow characters that can be C identifiers and commas as separators, as
|
|
|
|
* well as some whitespace for readability.
|
|
|
|
*/
|
|
|
|
validlen = strspn(*newval,
|
|
|
|
"0123456789_"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
", \n\t");
|
|
|
|
if (validlen != newvallen)
|
|
|
|
{
|
2024-01-18 09:35:12 +01:00
|
|
|
GUC_check_errdetail("Invalid character");
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*newval[0] == '\0')
|
|
|
|
{
|
|
|
|
*extra = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate space for the output and create the copy. We could discount
|
|
|
|
* whitespace chars to save some memory, but it doesn't seem worth the
|
|
|
|
* trouble.
|
|
|
|
*/
|
|
|
|
someval = guc_malloc(ERROR, newvallen + 1 + 1);
|
|
|
|
for (i = 0, j = 0; i < newvallen; i++)
|
|
|
|
{
|
|
|
|
if ((*newval)[i] == ',')
|
|
|
|
someval[j++] = '\0'; /* next item */
|
|
|
|
else if ((*newval)[i] == ' ' ||
|
|
|
|
(*newval)[i] == '\n' ||
|
|
|
|
(*newval)[i] == '\t')
|
|
|
|
; /* ignore these */
|
|
|
|
else
|
|
|
|
someval[j++] = (*newval)[i]; /* copy anything else */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* two \0s end the setting */
|
|
|
|
someval[j] = '\0';
|
|
|
|
someval[j + 1] = '\0';
|
|
|
|
|
|
|
|
*extra = someval;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GUC assign_hook for backtrace_functions
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
assign_backtrace_functions(const char *newval, void *extra)
|
|
|
|
{
|
2023-12-28 17:25:47 +01:00
|
|
|
backtrace_function_list = (char *) extra;
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GUC check_hook for log_destination
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
check_log_destination(char **newval, void **extra, GucSource source)
|
|
|
|
{
|
|
|
|
char *rawstring;
|
|
|
|
List *elemlist;
|
|
|
|
ListCell *l;
|
|
|
|
int newlogdest = 0;
|
|
|
|
int *myextra;
|
|
|
|
|
|
|
|
/* Need a modifiable copy of string */
|
|
|
|
rawstring = pstrdup(*newval);
|
|
|
|
|
|
|
|
/* Parse string into list of identifiers */
|
|
|
|
if (!SplitIdentifierString(rawstring, ',', &elemlist))
|
|
|
|
{
|
|
|
|
/* syntax error in list */
|
|
|
|
GUC_check_errdetail("List syntax is invalid.");
|
|
|
|
pfree(rawstring);
|
|
|
|
list_free(elemlist);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(l, elemlist)
|
|
|
|
{
|
|
|
|
char *tok = (char *) lfirst(l);
|
|
|
|
|
|
|
|
if (pg_strcasecmp(tok, "stderr") == 0)
|
|
|
|
newlogdest |= LOG_DESTINATION_STDERR;
|
|
|
|
else if (pg_strcasecmp(tok, "csvlog") == 0)
|
|
|
|
newlogdest |= LOG_DESTINATION_CSVLOG;
|
|
|
|
else if (pg_strcasecmp(tok, "jsonlog") == 0)
|
|
|
|
newlogdest |= LOG_DESTINATION_JSONLOG;
|
2002-04-21 02:22:52 +02:00
|
|
|
#ifdef HAVE_SYSLOG
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
else if (pg_strcasecmp(tok, "syslog") == 0)
|
|
|
|
newlogdest |= LOG_DESTINATION_SYSLOG;
|
|
|
|
#endif
|
|
|
|
#ifdef WIN32
|
|
|
|
else if (pg_strcasecmp(tok, "eventlog") == 0)
|
|
|
|
newlogdest |= LOG_DESTINATION_EVENTLOG;
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GUC_check_errdetail("Unrecognized key word: \"%s\".", tok);
|
|
|
|
pfree(rawstring);
|
|
|
|
list_free(elemlist);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pfree(rawstring);
|
|
|
|
list_free(elemlist);
|
|
|
|
|
|
|
|
myextra = (int *) guc_malloc(ERROR, sizeof(int));
|
|
|
|
*myextra = newlogdest;
|
|
|
|
*extra = (void *) myextra;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GUC assign_hook for log_destination
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
assign_log_destination(const char *newval, void *extra)
|
|
|
|
{
|
|
|
|
Log_destination = *((int *) extra);
|
|
|
|
}
|
2000-05-31 02:28:42 +02:00
|
|
|
|
2005-10-14 22:53:56 +02:00
|
|
|
/*
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
* GUC assign_hook for syslog_ident
|
2005-10-14 22:53:56 +02:00
|
|
|
*/
|
|
|
|
void
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
assign_syslog_ident(const char *newval, void *extra)
|
2005-10-14 22:53:56 +02:00
|
|
|
{
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
#ifdef HAVE_SYSLOG
|
2005-10-14 22:53:56 +02:00
|
|
|
/*
|
|
|
|
* guc.c is likely to call us repeatedly with same parameters, so don't
|
|
|
|
* thrash the syslog connection unnecessarily. Also, we do not re-open
|
|
|
|
* the connection until needed, since this routine will get called whether
|
|
|
|
* or not Log_destination actually mentions syslog.
|
|
|
|
*
|
|
|
|
* Note that we make our own copy of the ident string rather than relying
|
|
|
|
* on guc.c's. This may be overly paranoid, but it ensures that we cannot
|
|
|
|
* accidentally free a string that syslog is still using.
|
|
|
|
*/
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
if (syslog_ident == NULL || strcmp(syslog_ident, newval) != 0)
|
2005-10-14 22:53:56 +02:00
|
|
|
{
|
|
|
|
if (openlog_done)
|
|
|
|
{
|
|
|
|
closelog();
|
|
|
|
openlog_done = false;
|
|
|
|
}
|
2022-06-16 21:50:56 +02:00
|
|
|
free(syslog_ident);
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
syslog_ident = strdup(newval);
|
2005-10-14 22:53:56 +02:00
|
|
|
/* if the strdup fails, we will cope in write_syslog() */
|
|
|
|
}
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
#endif
|
|
|
|
/* Without syslog support, just ignore it */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GUC assign_hook for syslog_facility
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
assign_syslog_facility(int newval, void *extra)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SYSLOG
|
|
|
|
/*
|
|
|
|
* As above, don't thrash the syslog connection unnecessarily.
|
|
|
|
*/
|
|
|
|
if (syslog_facility != newval)
|
|
|
|
{
|
|
|
|
if (openlog_done)
|
|
|
|
{
|
|
|
|
closelog();
|
|
|
|
openlog_done = false;
|
|
|
|
}
|
|
|
|
syslog_facility = newval;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Without syslog support, just ignore it */
|
2005-10-14 22:53:56 +02:00
|
|
|
}
|
|
|
|
|
Split up guc.c for better build speed and ease of maintenance.
guc.c has grown to be one of our largest .c files, making it
a bottleneck for compilation. It's also acquired a bunch of
knowledge that'd be better kept elsewhere, because of our not
very good habit of putting variable-specific check hooks here.
Hence, split it up along these lines:
* guc.c itself retains just the core GUC housekeeping mechanisms.
* New file guc_funcs.c contains the SET/SHOW interfaces and some
SQL-accessible functions for GUC manipulation.
* New file guc_tables.c contains the data arrays that define the
built-in GUC variables, along with some already-exported constant
tables.
* GUC check/assign/show hook functions are moved to the variable's
home module, whenever that's clearly identifiable. A few hard-
to-classify hooks ended up in commands/variable.c, which was
already a home for miscellaneous GUC hook functions.
To avoid cluttering a lot more header files with #include "guc.h",
I also invented a new header file utils/guc_hooks.h and put all
the GUC hook functions' declarations there, regardless of their
originating module. That allowed removal of #include "guc.h"
from some existing headers. The fallout from that (hopefully
all caught here) demonstrates clearly why such inclusions are
best minimized: there are a lot of files that, for example,
were getting array.h at two or more levels of remove, despite
not having any connection at all to GUCs in themselves.
There is some very minor code beautification here, such as
renaming a couple of inconsistently-named hook functions
and improving some comments. But mostly this just moves
code from point A to point B and deals with the ensuing
needs for #include adjustments and exporting a few functions
that previously weren't exported.
Patch by me, per a suggestion from Andres Freund; thanks also
to Michael Paquier for the idea to invent guc_funcs.c.
Discussion: https://postgr.es/m/587607.1662836699@sss.pgh.pa.us
2022-09-13 17:05:07 +02:00
|
|
|
#ifdef HAVE_SYSLOG
|
2005-10-14 22:53:56 +02:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
/*
|
2005-10-14 18:41:02 +02:00
|
|
|
* Write a message line to syslog
|
2000-05-31 02:28:42 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
write_syslog(int level, const char *line)
|
|
|
|
{
|
|
|
|
static unsigned long seq = 0;
|
2000-12-01 20:52:04 +01:00
|
|
|
|
2005-10-14 18:41:02 +02:00
|
|
|
int len;
|
2008-07-09 00:17:41 +02:00
|
|
|
const char *nlpos;
|
2000-05-31 02:28:42 +02:00
|
|
|
|
2005-10-14 22:53:56 +02:00
|
|
|
/* Open syslog connection if not done yet */
|
2000-05-31 02:28:42 +02:00
|
|
|
if (!openlog_done)
|
|
|
|
{
|
2005-10-14 22:53:56 +02:00
|
|
|
openlog(syslog_ident ? syslog_ident : "postgres",
|
|
|
|
LOG_PID | LOG_NDELAY | LOG_NOWAIT,
|
|
|
|
syslog_facility);
|
2000-05-31 02:28:42 +02:00
|
|
|
openlog_done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We add a sequence number to each log message to suppress "same"
|
|
|
|
* messages.
|
|
|
|
*/
|
|
|
|
seq++;
|
|
|
|
|
2005-10-14 18:41:02 +02:00
|
|
|
/*
|
|
|
|
* Our problem here is that many syslog implementations don't handle long
|
|
|
|
* messages in an acceptable manner. While this function doesn't help that
|
|
|
|
* fact, it does work around by splitting up messages into smaller pieces.
|
|
|
|
*
|
|
|
|
* We divide into multiple syslog() calls if message is too long or if the
|
2008-07-09 00:17:41 +02:00
|
|
|
* message contains embedded newline(s).
|
2005-10-14 18:41:02 +02:00
|
|
|
*/
|
|
|
|
len = strlen(line);
|
2008-07-09 00:17:41 +02:00
|
|
|
nlpos = strchr(line, '\n');
|
2016-03-16 03:48:53 +01:00
|
|
|
if (syslog_split_messages && (len > PG_SYSLOG_LIMIT || nlpos != NULL))
|
2000-05-31 02:28:42 +02:00
|
|
|
{
|
2000-12-01 20:52:04 +01:00
|
|
|
int chunk_nr = 0;
|
2000-05-31 02:28:42 +02:00
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
2000-12-01 20:52:04 +01:00
|
|
|
char buf[PG_SYSLOG_LIMIT + 1];
|
|
|
|
int buflen;
|
|
|
|
int i;
|
|
|
|
|
2000-11-25 05:38:00 +01:00
|
|
|
/* if we start at a newline, move ahead one char */
|
|
|
|
if (line[0] == '\n')
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
len--;
|
2008-07-09 00:17:41 +02:00
|
|
|
/* we need to recompute the next newline's position, too */
|
|
|
|
nlpos = strchr(line, '\n');
|
2000-12-01 20:52:04 +01:00
|
|
|
continue;
|
2000-11-25 05:38:00 +01:00
|
|
|
}
|
2000-05-31 02:28:42 +02:00
|
|
|
|
2006-09-27 20:40:10 +02:00
|
|
|
/* copy one line, or as much as will fit, to buf */
|
|
|
|
if (nlpos != NULL)
|
|
|
|
buflen = nlpos - line;
|
|
|
|
else
|
|
|
|
buflen = len;
|
|
|
|
buflen = Min(buflen, PG_SYSLOG_LIMIT);
|
|
|
|
memcpy(buf, line, buflen);
|
|
|
|
buf[buflen] = '\0';
|
2002-08-29 09:22:30 +02:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
/* trim to multibyte letter boundary */
|
2003-04-24 23:16:45 +02:00
|
|
|
buflen = pg_mbcliplen(buf, buflen, buflen);
|
2001-02-21 07:05:23 +01:00
|
|
|
if (buflen <= 0)
|
|
|
|
return;
|
2000-05-31 02:28:42 +02:00
|
|
|
buf[buflen] = '\0';
|
2002-08-29 09:22:30 +02:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
/* already word boundary? */
|
2005-10-14 18:41:02 +02:00
|
|
|
if (line[buflen] != '\0' &&
|
|
|
|
!isspace((unsigned char) line[buflen]))
|
2000-05-31 02:28:42 +02:00
|
|
|
{
|
2000-12-01 20:52:04 +01:00
|
|
|
/* try to divide at word boundary */
|
2003-04-24 23:16:45 +02:00
|
|
|
i = buflen - 1;
|
2000-12-03 21:45:40 +01:00
|
|
|
while (i > 0 && !isspace((unsigned char) buf[i]))
|
2000-05-31 02:28:42 +02:00
|
|
|
i--;
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
if (i > 0) /* else couldn't divide word boundary */
|
2000-05-31 02:28:42 +02:00
|
|
|
{
|
|
|
|
buflen = i;
|
|
|
|
buf[i] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chunk_nr++;
|
|
|
|
|
2016-02-27 04:34:30 +01:00
|
|
|
if (syslog_sequence_numbers)
|
|
|
|
syslog(level, "[%lu-%d] %s", seq, chunk_nr, buf);
|
|
|
|
else
|
|
|
|
syslog(level, "[%d] %s", chunk_nr, buf);
|
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
line += buflen;
|
|
|
|
len -= buflen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2000-12-01 20:52:04 +01:00
|
|
|
{
|
|
|
|
/* message short enough */
|
2016-02-27 04:34:30 +01:00
|
|
|
if (syslog_sequence_numbers)
|
|
|
|
syslog(level, "[%lu] %s", seq, line);
|
|
|
|
else
|
|
|
|
syslog(level, "%s", line);
|
2000-12-01 20:52:04 +01:00
|
|
|
}
|
2000-05-31 02:28:42 +02:00
|
|
|
}
|
2003-04-24 23:16:45 +02:00
|
|
|
#endif /* HAVE_SYSLOG */
|
2005-10-14 18:41:02 +02:00
|
|
|
|
2004-04-05 05:02:11 +02:00
|
|
|
#ifdef WIN32
|
Renovate display of non-ASCII messages on Windows.
GNU gettext selects a default encoding for the messages it emits in a
platform-specific manner; it uses the Windows ANSI code page on Windows
and follows LC_CTYPE on other platforms. This is inconvenient for
PostgreSQL server processes, so realize consistent cross-platform
behavior by calling bind_textdomain_codeset() on Windows each time we
permanently change LC_CTYPE. This primarily affects SQL_ASCII databases
and processes like the postmaster that do not attach to a database,
making their behavior consistent with PostgreSQL on non-Windows
platforms. Messages from SQL_ASCII databases use the encoding implied
by the database LC_CTYPE, and messages from non-database processes use
LC_CTYPE from the postmaster system environment. PlatformEncoding
becomes unused, so remove it.
Make write_console() prefer WriteConsoleW() to write() regardless of the
encodings in use. In this situation, write() will invariably mishandle
non-ASCII characters.
elog.c has assumed that messages conform to the database encoding.
While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL.
Introduce MessageEncoding to track the actual encoding of message text.
The present consumers are Windows-specific code for converting messages
to UTF16 for use in system interfaces. This fixes the appearance in
Windows event logs and consoles of translated messages from SQL_ASCII
processes like the postmaster. Note that SQL_ASCII inherently disclaims
a strong notion of encoding, so non-ASCII byte sequences interpolated
into messages by %s may yet yield a nonsensical message. MULE_INTERNAL
has similar problems at present, albeit for a different reason: its lack
of libiconv support or a conversion to UTF8.
Consequently, one need no longer restart Windows with a different
Windows ANSI code page to broadly test backend logging under a given
language. Changing the user's locale ("Format") is enough. Several
accounts can simultaneously run postmasters under different locales, all
correctly logging localized messages to Windows event logs and consoles.
Alexander Law and Noah Misch
2013-06-26 17:17:33 +02:00
|
|
|
/*
|
|
|
|
* Get the PostgreSQL equivalent of the Windows ANSI code page. "ANSI" system
|
|
|
|
* interfaces (e.g. CreateFileA()) expect string arguments in this encoding.
|
|
|
|
* Every process in a given system will find the same value at all times.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
GetACPEncoding(void)
|
|
|
|
{
|
|
|
|
static int encoding = -2;
|
|
|
|
|
|
|
|
if (encoding == -2)
|
|
|
|
encoding = pg_codepage_to_encoding(GetACP());
|
|
|
|
|
|
|
|
return encoding;
|
|
|
|
}
|
|
|
|
|
2004-04-05 05:02:11 +02:00
|
|
|
/*
|
|
|
|
* Write a message line to the windows event log
|
|
|
|
*/
|
|
|
|
static void
|
2009-10-17 02:24:51 +02:00
|
|
|
write_eventlog(int level, const char *line, int len)
|
2004-04-05 05:02:11 +02:00
|
|
|
{
|
2009-10-17 02:24:51 +02:00
|
|
|
WCHAR *utf16;
|
|
|
|
int eventlevel = EVENTLOG_ERROR_TYPE;
|
|
|
|
static HANDLE evtHandle = INVALID_HANDLE_VALUE;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-04-05 05:02:11 +02:00
|
|
|
if (evtHandle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2014-07-17 12:42:08 +02:00
|
|
|
evtHandle = RegisterEventSource(NULL,
|
|
|
|
event_source ? event_source : DEFAULT_EVENT_SOURCE);
|
2004-04-05 05:02:11 +02:00
|
|
|
if (evtHandle == NULL)
|
|
|
|
{
|
|
|
|
evtHandle = INVALID_HANDLE_VALUE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-12 23:36:59 +02:00
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case DEBUG5:
|
|
|
|
case DEBUG4:
|
|
|
|
case DEBUG3:
|
|
|
|
case DEBUG2:
|
|
|
|
case DEBUG1:
|
|
|
|
case LOG:
|
Introduce a LOG_SERVER_ONLY ereport level, which is never sent to client.
This elevel is useful for logging audit messages and similar information
that should not be passed to the client. It's equivalent to LOG in terms
of decisions about logging priority in the postmaster log, but messages
with this elevel will never be sent to the client.
In the current implementation, it's just an alias for the longstanding
COMMERROR elevel (or more accurately, we've made COMMERROR an alias for
this). At some point it might be interesting to allow a LOG_ONLY flag to
be attached to any elevel, but that would be considerably more complicated,
and it's not clear there's enough use-cases to justify the extra work.
For now, let's just take the easy 90% solution.
David Steele, reviewed by Fabien Coelho, Petr Jelínek, and myself
2016-04-04 18:32:42 +02:00
|
|
|
case LOG_SERVER_ONLY:
|
2005-08-12 23:36:59 +02:00
|
|
|
case INFO:
|
|
|
|
case NOTICE:
|
|
|
|
eventlevel = EVENTLOG_INFORMATION_TYPE;
|
|
|
|
break;
|
|
|
|
case WARNING:
|
2020-12-30 00:02:38 +01:00
|
|
|
case WARNING_CLIENT_ONLY:
|
2005-08-12 23:36:59 +02:00
|
|
|
eventlevel = EVENTLOG_WARNING_TYPE;
|
|
|
|
break;
|
|
|
|
case ERROR:
|
|
|
|
case FATAL:
|
|
|
|
case PANIC:
|
|
|
|
default:
|
|
|
|
eventlevel = EVENTLOG_ERROR_TYPE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-10-17 02:24:51 +02:00
|
|
|
/*
|
Renovate display of non-ASCII messages on Windows.
GNU gettext selects a default encoding for the messages it emits in a
platform-specific manner; it uses the Windows ANSI code page on Windows
and follows LC_CTYPE on other platforms. This is inconvenient for
PostgreSQL server processes, so realize consistent cross-platform
behavior by calling bind_textdomain_codeset() on Windows each time we
permanently change LC_CTYPE. This primarily affects SQL_ASCII databases
and processes like the postmaster that do not attach to a database,
making their behavior consistent with PostgreSQL on non-Windows
platforms. Messages from SQL_ASCII databases use the encoding implied
by the database LC_CTYPE, and messages from non-database processes use
LC_CTYPE from the postmaster system environment. PlatformEncoding
becomes unused, so remove it.
Make write_console() prefer WriteConsoleW() to write() regardless of the
encodings in use. In this situation, write() will invariably mishandle
non-ASCII characters.
elog.c has assumed that messages conform to the database encoding.
While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL.
Introduce MessageEncoding to track the actual encoding of message text.
The present consumers are Windows-specific code for converting messages
to UTF16 for use in system interfaces. This fixes the appearance in
Windows event logs and consoles of translated messages from SQL_ASCII
processes like the postmaster. Note that SQL_ASCII inherently disclaims
a strong notion of encoding, so non-ASCII byte sequences interpolated
into messages by %s may yet yield a nonsensical message. MULE_INTERNAL
has similar problems at present, albeit for a different reason: its lack
of libiconv support or a conversion to UTF8.
Consequently, one need no longer restart Windows with a different
Windows ANSI code page to broadly test backend logging under a given
language. Changing the user's locale ("Format") is enough. Several
accounts can simultaneously run postmasters under different locales, all
correctly logging localized messages to Windows event logs and consoles.
Alexander Law and Noah Misch
2013-06-26 17:17:33 +02:00
|
|
|
* If message character encoding matches the encoding expected by
|
|
|
|
* ReportEventA(), call it to avoid the hazards of conversion. Otherwise,
|
|
|
|
* try to convert the message to UTF16 and write it with ReportEventW().
|
|
|
|
* Fall back on ReportEventA() if conversion failed.
|
2009-10-17 02:24:51 +02:00
|
|
|
*
|
2017-11-12 22:03:15 +01:00
|
|
|
* Since we palloc the structure required for conversion, also fall
|
|
|
|
* through to writing unconverted if we have not yet set up
|
|
|
|
* CurrentMemoryContext.
|
|
|
|
*
|
2009-10-17 02:24:51 +02:00
|
|
|
* Also verify that we are not on our way into error recursion trouble due
|
Renovate display of non-ASCII messages on Windows.
GNU gettext selects a default encoding for the messages it emits in a
platform-specific manner; it uses the Windows ANSI code page on Windows
and follows LC_CTYPE on other platforms. This is inconvenient for
PostgreSQL server processes, so realize consistent cross-platform
behavior by calling bind_textdomain_codeset() on Windows each time we
permanently change LC_CTYPE. This primarily affects SQL_ASCII databases
and processes like the postmaster that do not attach to a database,
making their behavior consistent with PostgreSQL on non-Windows
platforms. Messages from SQL_ASCII databases use the encoding implied
by the database LC_CTYPE, and messages from non-database processes use
LC_CTYPE from the postmaster system environment. PlatformEncoding
becomes unused, so remove it.
Make write_console() prefer WriteConsoleW() to write() regardless of the
encodings in use. In this situation, write() will invariably mishandle
non-ASCII characters.
elog.c has assumed that messages conform to the database encoding.
While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL.
Introduce MessageEncoding to track the actual encoding of message text.
The present consumers are Windows-specific code for converting messages
to UTF16 for use in system interfaces. This fixes the appearance in
Windows event logs and consoles of translated messages from SQL_ASCII
processes like the postmaster. Note that SQL_ASCII inherently disclaims
a strong notion of encoding, so non-ASCII byte sequences interpolated
into messages by %s may yet yield a nonsensical message. MULE_INTERNAL
has similar problems at present, albeit for a different reason: its lack
of libiconv support or a conversion to UTF8.
Consequently, one need no longer restart Windows with a different
Windows ANSI code page to broadly test backend logging under a given
language. Changing the user's locale ("Format") is enough. Several
accounts can simultaneously run postmasters under different locales, all
correctly logging localized messages to Windows event logs and consoles.
Alexander Law and Noah Misch
2013-06-26 17:17:33 +02:00
|
|
|
* to error messages thrown deep inside pgwin32_message_to_UTF16().
|
2009-10-17 02:24:51 +02:00
|
|
|
*/
|
Renovate display of non-ASCII messages on Windows.
GNU gettext selects a default encoding for the messages it emits in a
platform-specific manner; it uses the Windows ANSI code page on Windows
and follows LC_CTYPE on other platforms. This is inconvenient for
PostgreSQL server processes, so realize consistent cross-platform
behavior by calling bind_textdomain_codeset() on Windows each time we
permanently change LC_CTYPE. This primarily affects SQL_ASCII databases
and processes like the postmaster that do not attach to a database,
making their behavior consistent with PostgreSQL on non-Windows
platforms. Messages from SQL_ASCII databases use the encoding implied
by the database LC_CTYPE, and messages from non-database processes use
LC_CTYPE from the postmaster system environment. PlatformEncoding
becomes unused, so remove it.
Make write_console() prefer WriteConsoleW() to write() regardless of the
encodings in use. In this situation, write() will invariably mishandle
non-ASCII characters.
elog.c has assumed that messages conform to the database encoding.
While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL.
Introduce MessageEncoding to track the actual encoding of message text.
The present consumers are Windows-specific code for converting messages
to UTF16 for use in system interfaces. This fixes the appearance in
Windows event logs and consoles of translated messages from SQL_ASCII
processes like the postmaster. Note that SQL_ASCII inherently disclaims
a strong notion of encoding, so non-ASCII byte sequences interpolated
into messages by %s may yet yield a nonsensical message. MULE_INTERNAL
has similar problems at present, albeit for a different reason: its lack
of libiconv support or a conversion to UTF8.
Consequently, one need no longer restart Windows with a different
Windows ANSI code page to broadly test backend logging under a given
language. Changing the user's locale ("Format") is enough. Several
accounts can simultaneously run postmasters under different locales, all
correctly logging localized messages to Windows event logs and consoles.
Alexander Law and Noah Misch
2013-06-26 17:17:33 +02:00
|
|
|
if (!in_error_recursion_trouble() &&
|
2017-11-12 22:03:15 +01:00
|
|
|
CurrentMemoryContext != NULL &&
|
Renovate display of non-ASCII messages on Windows.
GNU gettext selects a default encoding for the messages it emits in a
platform-specific manner; it uses the Windows ANSI code page on Windows
and follows LC_CTYPE on other platforms. This is inconvenient for
PostgreSQL server processes, so realize consistent cross-platform
behavior by calling bind_textdomain_codeset() on Windows each time we
permanently change LC_CTYPE. This primarily affects SQL_ASCII databases
and processes like the postmaster that do not attach to a database,
making their behavior consistent with PostgreSQL on non-Windows
platforms. Messages from SQL_ASCII databases use the encoding implied
by the database LC_CTYPE, and messages from non-database processes use
LC_CTYPE from the postmaster system environment. PlatformEncoding
becomes unused, so remove it.
Make write_console() prefer WriteConsoleW() to write() regardless of the
encodings in use. In this situation, write() will invariably mishandle
non-ASCII characters.
elog.c has assumed that messages conform to the database encoding.
While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL.
Introduce MessageEncoding to track the actual encoding of message text.
The present consumers are Windows-specific code for converting messages
to UTF16 for use in system interfaces. This fixes the appearance in
Windows event logs and consoles of translated messages from SQL_ASCII
processes like the postmaster. Note that SQL_ASCII inherently disclaims
a strong notion of encoding, so non-ASCII byte sequences interpolated
into messages by %s may yet yield a nonsensical message. MULE_INTERNAL
has similar problems at present, albeit for a different reason: its lack
of libiconv support or a conversion to UTF8.
Consequently, one need no longer restart Windows with a different
Windows ANSI code page to broadly test backend logging under a given
language. Changing the user's locale ("Format") is enough. Several
accounts can simultaneously run postmasters under different locales, all
correctly logging localized messages to Windows event logs and consoles.
Alexander Law and Noah Misch
2013-06-26 17:17:33 +02:00
|
|
|
GetMessageEncoding() != GetACPEncoding())
|
2009-10-17 02:24:51 +02:00
|
|
|
{
|
Renovate display of non-ASCII messages on Windows.
GNU gettext selects a default encoding for the messages it emits in a
platform-specific manner; it uses the Windows ANSI code page on Windows
and follows LC_CTYPE on other platforms. This is inconvenient for
PostgreSQL server processes, so realize consistent cross-platform
behavior by calling bind_textdomain_codeset() on Windows each time we
permanently change LC_CTYPE. This primarily affects SQL_ASCII databases
and processes like the postmaster that do not attach to a database,
making their behavior consistent with PostgreSQL on non-Windows
platforms. Messages from SQL_ASCII databases use the encoding implied
by the database LC_CTYPE, and messages from non-database processes use
LC_CTYPE from the postmaster system environment. PlatformEncoding
becomes unused, so remove it.
Make write_console() prefer WriteConsoleW() to write() regardless of the
encodings in use. In this situation, write() will invariably mishandle
non-ASCII characters.
elog.c has assumed that messages conform to the database encoding.
While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL.
Introduce MessageEncoding to track the actual encoding of message text.
The present consumers are Windows-specific code for converting messages
to UTF16 for use in system interfaces. This fixes the appearance in
Windows event logs and consoles of translated messages from SQL_ASCII
processes like the postmaster. Note that SQL_ASCII inherently disclaims
a strong notion of encoding, so non-ASCII byte sequences interpolated
into messages by %s may yet yield a nonsensical message. MULE_INTERNAL
has similar problems at present, albeit for a different reason: its lack
of libiconv support or a conversion to UTF8.
Consequently, one need no longer restart Windows with a different
Windows ANSI code page to broadly test backend logging under a given
language. Changing the user's locale ("Format") is enough. Several
accounts can simultaneously run postmasters under different locales, all
correctly logging localized messages to Windows event logs and consoles.
Alexander Law and Noah Misch
2013-06-26 17:17:33 +02:00
|
|
|
utf16 = pgwin32_message_to_UTF16(line, len, NULL);
|
2009-10-17 02:24:51 +02:00
|
|
|
if (utf16)
|
|
|
|
{
|
|
|
|
ReportEventW(evtHandle,
|
|
|
|
eventlevel,
|
|
|
|
0,
|
|
|
|
0, /* All events are Id 0 */
|
|
|
|
NULL,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
(LPCWSTR *) &utf16,
|
|
|
|
NULL);
|
Renovate display of non-ASCII messages on Windows.
GNU gettext selects a default encoding for the messages it emits in a
platform-specific manner; it uses the Windows ANSI code page on Windows
and follows LC_CTYPE on other platforms. This is inconvenient for
PostgreSQL server processes, so realize consistent cross-platform
behavior by calling bind_textdomain_codeset() on Windows each time we
permanently change LC_CTYPE. This primarily affects SQL_ASCII databases
and processes like the postmaster that do not attach to a database,
making their behavior consistent with PostgreSQL on non-Windows
platforms. Messages from SQL_ASCII databases use the encoding implied
by the database LC_CTYPE, and messages from non-database processes use
LC_CTYPE from the postmaster system environment. PlatformEncoding
becomes unused, so remove it.
Make write_console() prefer WriteConsoleW() to write() regardless of the
encodings in use. In this situation, write() will invariably mishandle
non-ASCII characters.
elog.c has assumed that messages conform to the database encoding.
While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL.
Introduce MessageEncoding to track the actual encoding of message text.
The present consumers are Windows-specific code for converting messages
to UTF16 for use in system interfaces. This fixes the appearance in
Windows event logs and consoles of translated messages from SQL_ASCII
processes like the postmaster. Note that SQL_ASCII inherently disclaims
a strong notion of encoding, so non-ASCII byte sequences interpolated
into messages by %s may yet yield a nonsensical message. MULE_INTERNAL
has similar problems at present, albeit for a different reason: its lack
of libiconv support or a conversion to UTF8.
Consequently, one need no longer restart Windows with a different
Windows ANSI code page to broadly test backend logging under a given
language. Changing the user's locale ("Format") is enough. Several
accounts can simultaneously run postmasters under different locales, all
correctly logging localized messages to Windows event logs and consoles.
Alexander Law and Noah Misch
2013-06-26 17:17:33 +02:00
|
|
|
/* XXX Try ReportEventA() when ReportEventW() fails? */
|
2009-10-17 02:24:51 +02:00
|
|
|
|
|
|
|
pfree(utf16);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ReportEventA(evtHandle,
|
2005-08-12 23:36:59 +02:00
|
|
|
eventlevel,
|
2004-04-05 05:02:11 +02:00
|
|
|
0,
|
|
|
|
0, /* All events are Id 0 */
|
|
|
|
NULL,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
&line,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
#endif /* WIN32 */
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2009-10-17 02:24:51 +02:00
|
|
|
static void
|
|
|
|
write_console(const char *line, int len)
|
|
|
|
{
|
2011-10-19 03:37:51 +02:00
|
|
|
int rc;
|
|
|
|
|
2009-10-17 02:24:51 +02:00
|
|
|
#ifdef WIN32
|
2014-05-06 18:12:18 +02:00
|
|
|
|
2009-10-17 02:24:51 +02:00
|
|
|
/*
|
Renovate display of non-ASCII messages on Windows.
GNU gettext selects a default encoding for the messages it emits in a
platform-specific manner; it uses the Windows ANSI code page on Windows
and follows LC_CTYPE on other platforms. This is inconvenient for
PostgreSQL server processes, so realize consistent cross-platform
behavior by calling bind_textdomain_codeset() on Windows each time we
permanently change LC_CTYPE. This primarily affects SQL_ASCII databases
and processes like the postmaster that do not attach to a database,
making their behavior consistent with PostgreSQL on non-Windows
platforms. Messages from SQL_ASCII databases use the encoding implied
by the database LC_CTYPE, and messages from non-database processes use
LC_CTYPE from the postmaster system environment. PlatformEncoding
becomes unused, so remove it.
Make write_console() prefer WriteConsoleW() to write() regardless of the
encodings in use. In this situation, write() will invariably mishandle
non-ASCII characters.
elog.c has assumed that messages conform to the database encoding.
While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL.
Introduce MessageEncoding to track the actual encoding of message text.
The present consumers are Windows-specific code for converting messages
to UTF16 for use in system interfaces. This fixes the appearance in
Windows event logs and consoles of translated messages from SQL_ASCII
processes like the postmaster. Note that SQL_ASCII inherently disclaims
a strong notion of encoding, so non-ASCII byte sequences interpolated
into messages by %s may yet yield a nonsensical message. MULE_INTERNAL
has similar problems at present, albeit for a different reason: its lack
of libiconv support or a conversion to UTF8.
Consequently, one need no longer restart Windows with a different
Windows ANSI code page to broadly test backend logging under a given
language. Changing the user's locale ("Format") is enough. Several
accounts can simultaneously run postmasters under different locales, all
correctly logging localized messages to Windows event logs and consoles.
Alexander Law and Noah Misch
2013-06-26 17:17:33 +02:00
|
|
|
* Try to convert the message to UTF16 and write it with WriteConsoleW().
|
|
|
|
* Fall back on write() if anything fails.
|
|
|
|
*
|
|
|
|
* In contrast to write_eventlog(), don't skip straight to write() based
|
|
|
|
* on the applicable encodings. Unlike WriteConsoleW(), write() depends
|
|
|
|
* on the suitability of the console output code page. Since we put
|
|
|
|
* stderr into binary mode in SubPostmasterMain(), write() skips the
|
|
|
|
* necessary translation anyway.
|
|
|
|
*
|
|
|
|
* WriteConsoleW() will fail if stderr is redirected, so just fall through
|
2009-10-17 02:24:51 +02:00
|
|
|
* to writing unconverted to the logfile in this case.
|
2011-04-01 19:58:36 +02:00
|
|
|
*
|
|
|
|
* Since we palloc the structure required for conversion, also fall
|
|
|
|
* through to writing unconverted if we have not yet set up
|
|
|
|
* CurrentMemoryContext.
|
2009-10-17 02:24:51 +02:00
|
|
|
*/
|
Renovate display of non-ASCII messages on Windows.
GNU gettext selects a default encoding for the messages it emits in a
platform-specific manner; it uses the Windows ANSI code page on Windows
and follows LC_CTYPE on other platforms. This is inconvenient for
PostgreSQL server processes, so realize consistent cross-platform
behavior by calling bind_textdomain_codeset() on Windows each time we
permanently change LC_CTYPE. This primarily affects SQL_ASCII databases
and processes like the postmaster that do not attach to a database,
making their behavior consistent with PostgreSQL on non-Windows
platforms. Messages from SQL_ASCII databases use the encoding implied
by the database LC_CTYPE, and messages from non-database processes use
LC_CTYPE from the postmaster system environment. PlatformEncoding
becomes unused, so remove it.
Make write_console() prefer WriteConsoleW() to write() regardless of the
encodings in use. In this situation, write() will invariably mishandle
non-ASCII characters.
elog.c has assumed that messages conform to the database encoding.
While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL.
Introduce MessageEncoding to track the actual encoding of message text.
The present consumers are Windows-specific code for converting messages
to UTF16 for use in system interfaces. This fixes the appearance in
Windows event logs and consoles of translated messages from SQL_ASCII
processes like the postmaster. Note that SQL_ASCII inherently disclaims
a strong notion of encoding, so non-ASCII byte sequences interpolated
into messages by %s may yet yield a nonsensical message. MULE_INTERNAL
has similar problems at present, albeit for a different reason: its lack
of libiconv support or a conversion to UTF8.
Consequently, one need no longer restart Windows with a different
Windows ANSI code page to broadly test backend logging under a given
language. Changing the user's locale ("Format") is enough. Several
accounts can simultaneously run postmasters under different locales, all
correctly logging localized messages to Windows event logs and consoles.
Alexander Law and Noah Misch
2013-06-26 17:17:33 +02:00
|
|
|
if (!in_error_recursion_trouble() &&
|
2011-04-01 19:58:36 +02:00
|
|
|
!redirection_done &&
|
|
|
|
CurrentMemoryContext != NULL)
|
2009-10-17 02:24:51 +02:00
|
|
|
{
|
|
|
|
WCHAR *utf16;
|
|
|
|
int utf16len;
|
|
|
|
|
Renovate display of non-ASCII messages on Windows.
GNU gettext selects a default encoding for the messages it emits in a
platform-specific manner; it uses the Windows ANSI code page on Windows
and follows LC_CTYPE on other platforms. This is inconvenient for
PostgreSQL server processes, so realize consistent cross-platform
behavior by calling bind_textdomain_codeset() on Windows each time we
permanently change LC_CTYPE. This primarily affects SQL_ASCII databases
and processes like the postmaster that do not attach to a database,
making their behavior consistent with PostgreSQL on non-Windows
platforms. Messages from SQL_ASCII databases use the encoding implied
by the database LC_CTYPE, and messages from non-database processes use
LC_CTYPE from the postmaster system environment. PlatformEncoding
becomes unused, so remove it.
Make write_console() prefer WriteConsoleW() to write() regardless of the
encodings in use. In this situation, write() will invariably mishandle
non-ASCII characters.
elog.c has assumed that messages conform to the database encoding.
While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL.
Introduce MessageEncoding to track the actual encoding of message text.
The present consumers are Windows-specific code for converting messages
to UTF16 for use in system interfaces. This fixes the appearance in
Windows event logs and consoles of translated messages from SQL_ASCII
processes like the postmaster. Note that SQL_ASCII inherently disclaims
a strong notion of encoding, so non-ASCII byte sequences interpolated
into messages by %s may yet yield a nonsensical message. MULE_INTERNAL
has similar problems at present, albeit for a different reason: its lack
of libiconv support or a conversion to UTF8.
Consequently, one need no longer restart Windows with a different
Windows ANSI code page to broadly test backend logging under a given
language. Changing the user's locale ("Format") is enough. Several
accounts can simultaneously run postmasters under different locales, all
correctly logging localized messages to Windows event logs and consoles.
Alexander Law and Noah Misch
2013-06-26 17:17:33 +02:00
|
|
|
utf16 = pgwin32_message_to_UTF16(line, len, &utf16len);
|
2009-10-17 02:24:51 +02:00
|
|
|
if (utf16 != NULL)
|
|
|
|
{
|
|
|
|
HANDLE stdHandle;
|
|
|
|
DWORD written;
|
|
|
|
|
|
|
|
stdHandle = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
if (WriteConsoleW(stdHandle, utf16, utf16len, &written, NULL))
|
|
|
|
{
|
|
|
|
pfree(utf16);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In case WriteConsoleW() failed, fall back to writing the
|
|
|
|
* message unconverted.
|
|
|
|
*/
|
|
|
|
pfree(utf16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-10-17 02:24:51 +02:00
|
|
|
/*
|
2011-10-19 03:37:51 +02:00
|
|
|
* Conversion on non-win32 platforms is not implemented yet. It requires
|
2009-10-17 02:24:51 +02:00
|
|
|
* non-throw version of pg_do_encoding_conversion(), that converts
|
2022-04-11 10:49:41 +02:00
|
|
|
* unconvertible characters to '?' without errors.
|
2021-04-01 10:45:22 +02:00
|
|
|
*
|
|
|
|
* XXX: We have a no-throw version now. It doesn't convert to '?' though.
|
2009-10-17 02:24:51 +02:00
|
|
|
*/
|
|
|
|
#endif
|
|
|
|
|
2011-10-19 03:37:51 +02:00
|
|
|
/*
|
|
|
|
* We ignore any error from write() here. We have no useful way to report
|
|
|
|
* it ... certainly whining on stderr isn't likely to be productive.
|
|
|
|
*/
|
|
|
|
rc = write(fileno(stderr), line, len);
|
|
|
|
(void) rc;
|
2009-10-17 02:24:51 +02:00
|
|
|
}
|
|
|
|
|
2008-10-18 00:56:16 +02:00
|
|
|
/*
|
2022-01-12 06:16:59 +01:00
|
|
|
* get_formatted_log_time -- compute and get the log timestamp.
|
|
|
|
*
|
|
|
|
* The timestamp is computed if not set yet, so as it is kept consistent
|
|
|
|
* among all the log destinations that require it to be consistent. Note
|
|
|
|
* that the computed timestamp is returned in a static buffer, not
|
|
|
|
* palloc()'d.
|
2008-10-18 00:56:16 +02:00
|
|
|
*/
|
2022-01-12 06:16:59 +01:00
|
|
|
char *
|
|
|
|
get_formatted_log_time(void)
|
2008-10-18 00:56:16 +02:00
|
|
|
{
|
|
|
|
pg_time_t stamp_time;
|
2017-04-11 20:13:31 +02:00
|
|
|
char msbuf[13];
|
2008-10-18 00:56:16 +02:00
|
|
|
|
2022-01-12 06:16:59 +01:00
|
|
|
/* leave if already computed */
|
|
|
|
if (formatted_log_time[0] != '\0')
|
|
|
|
return formatted_log_time;
|
|
|
|
|
2015-09-08 00:40:49 +02:00
|
|
|
if (!saved_timeval_set)
|
|
|
|
{
|
|
|
|
gettimeofday(&saved_timeval, NULL);
|
|
|
|
saved_timeval_set = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
stamp_time = (pg_time_t) saved_timeval.tv_sec;
|
2008-10-18 00:56:16 +02:00
|
|
|
|
|
|
|
/*
|
2011-09-09 23:59:11 +02:00
|
|
|
* Note: we expect that guc.c will ensure that log_timezone is set up (at
|
|
|
|
* least with a minimal GMT value) before Log_line_prefix can become
|
|
|
|
* nonempty or CSV mode can be selected.
|
2008-10-18 00:56:16 +02:00
|
|
|
*/
|
|
|
|
pg_strftime(formatted_log_time, FORMATTED_TS_LEN,
|
|
|
|
/* leave room for milliseconds... */
|
|
|
|
"%Y-%m-%d %H:%M:%S %Z",
|
2011-09-09 23:59:11 +02:00
|
|
|
pg_localtime(&stamp_time, log_timezone));
|
2008-10-18 00:56:16 +02:00
|
|
|
|
|
|
|
/* 'paste' milliseconds into place... */
|
2015-09-08 00:40:49 +02:00
|
|
|
sprintf(msbuf, ".%03d", (int) (saved_timeval.tv_usec / 1000));
|
Replace a bunch more uses of strncpy() with safer coding.
strncpy() has a well-deserved reputation for being unsafe, so make an
effort to get rid of nearly all occurrences in HEAD.
A large fraction of the remaining uses were passing length less than or
equal to the known strlen() of the source, in which case no null-padding
can occur and the behavior is equivalent to memcpy(), though doubtless
slower and certainly harder to reason about. So just use memcpy() in
these cases.
In other cases, use either StrNCpy() or strlcpy() as appropriate (depending
on whether padding to the full length of the destination buffer seems
useful).
I left a few strncpy() calls alone in the src/timezone/ code, to keep it
in sync with upstream (the IANA tzcode distribution). There are also a
few such calls in ecpg that could possibly do with more analysis.
AFAICT, none of these changes are more than cosmetic, except for the four
occurrences in fe-secure-openssl.c, which are in fact buggy: an overlength
source leads to a non-null-terminated destination buffer and ensuing
misbehavior. These don't seem like security issues, first because no stack
clobber is possible and second because if your values of sslcert etc are
coming from untrusted sources then you've got problems way worse than this.
Still, it's undesirable to have unpredictable behavior for overlength
inputs, so back-patch those four changes to all active branches.
2015-01-24 19:05:42 +01:00
|
|
|
memcpy(formatted_log_time + 19, msbuf, 4);
|
2022-01-12 06:16:59 +01:00
|
|
|
|
|
|
|
return formatted_log_time;
|
2008-10-18 00:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-01-12 06:16:59 +01:00
|
|
|
* reset_formatted_start_time -- reset the start timestamp
|
2008-10-18 00:56:16 +02:00
|
|
|
*/
|
2022-01-12 06:16:59 +01:00
|
|
|
void
|
|
|
|
reset_formatted_start_time(void)
|
|
|
|
{
|
|
|
|
formatted_start_time[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_formatted_start_time -- compute and get the start timestamp.
|
|
|
|
*
|
|
|
|
* The timestamp is computed if not set yet. Note that the computed
|
|
|
|
* timestamp is returned in a static buffer, not palloc()'d.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
get_formatted_start_time(void)
|
2008-10-18 00:56:16 +02:00
|
|
|
{
|
|
|
|
pg_time_t stamp_time = (pg_time_t) MyStartTime;
|
|
|
|
|
2022-01-12 06:16:59 +01:00
|
|
|
/* leave if already computed */
|
|
|
|
if (formatted_start_time[0] != '\0')
|
|
|
|
return formatted_start_time;
|
|
|
|
|
2008-10-18 00:56:16 +02:00
|
|
|
/*
|
2011-09-09 23:59:11 +02:00
|
|
|
* Note: we expect that guc.c will ensure that log_timezone is set up (at
|
|
|
|
* least with a minimal GMT value) before Log_line_prefix can become
|
|
|
|
* nonempty or CSV mode can be selected.
|
2008-10-18 00:56:16 +02:00
|
|
|
*/
|
|
|
|
pg_strftime(formatted_start_time, FORMATTED_TS_LEN,
|
|
|
|
"%Y-%m-%d %H:%M:%S %Z",
|
2011-09-09 23:59:11 +02:00
|
|
|
pg_localtime(&stamp_time, log_timezone));
|
2022-01-12 06:16:59 +01:00
|
|
|
|
|
|
|
return formatted_start_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check_log_of_query -- check if a query can be logged
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
check_log_of_query(ErrorData *edata)
|
|
|
|
{
|
|
|
|
/* log required? */
|
|
|
|
if (!is_log_level_output(edata->elevel, log_min_error_statement))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* query log wanted? */
|
|
|
|
if (edata->hide_stmt)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* query string available? */
|
|
|
|
if (debug_query_string == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_backend_type_for_log -- backend type for log entries
|
|
|
|
*
|
|
|
|
* Returns a pointer to a static buffer, not palloc()'d.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
get_backend_type_for_log(void)
|
|
|
|
{
|
|
|
|
const char *backend_type_str;
|
|
|
|
|
|
|
|
if (MyProcPid == PostmasterPid)
|
|
|
|
backend_type_str = "postmaster";
|
|
|
|
else if (MyBackendType == B_BG_WORKER)
|
|
|
|
backend_type_str = MyBgworkerEntry->bgw_type;
|
|
|
|
else
|
|
|
|
backend_type_str = GetBackendTypeDesc(MyBackendType);
|
|
|
|
|
|
|
|
return backend_type_str;
|
2008-10-18 00:56:16 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:54:20 +02:00
|
|
|
/*
|
|
|
|
* process_log_prefix_padding --- helper function for processing the format
|
|
|
|
* string in log_line_prefix
|
|
|
|
*
|
|
|
|
* Note: This function returns NULL if it finds something which
|
|
|
|
* it deems invalid in the format string.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
process_log_prefix_padding(const char *p, int *ppadding)
|
|
|
|
{
|
|
|
|
int paddingsign = 1;
|
|
|
|
int padding = 0;
|
|
|
|
|
|
|
|
if (*p == '-')
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p == '\0') /* Did the buf end in %- ? */
|
|
|
|
return NULL;
|
|
|
|
paddingsign = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate an int version of the numerical string */
|
|
|
|
while (*p >= '0' && *p <= '9')
|
|
|
|
padding = padding * 10 + (*p++ - '0');
|
|
|
|
|
|
|
|
/* format is invalid if it ends with the padding number */
|
|
|
|
if (*p == '\0')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
padding *= paddingsign;
|
|
|
|
*ppadding = padding;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2004-03-09 05:43:07 +01:00
|
|
|
/*
|
2022-07-11 21:29:33 +02:00
|
|
|
* Format log status information using Log_line_prefix.
|
2004-03-09 05:43:07 +01:00
|
|
|
*/
|
2004-03-19 03:23:59 +01:00
|
|
|
static void
|
2009-07-03 21:14:25 +02:00
|
|
|
log_line_prefix(StringInfo buf, ErrorData *edata)
|
2022-07-11 21:29:33 +02:00
|
|
|
{
|
|
|
|
log_status_format(buf, Log_line_prefix, edata);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format log status info; append to the provided buffer.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
log_status_format(StringInfo buf, const char *format, ErrorData *edata)
|
2004-03-09 05:43:07 +01:00
|
|
|
{
|
2004-03-19 03:23:59 +01:00
|
|
|
/* static counter for line numbers */
|
|
|
|
static long log_line_number = 0;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-03-19 03:23:59 +01:00
|
|
|
/* has counter been reset in current process? */
|
|
|
|
static int log_my_pid = 0;
|
2013-09-26 23:54:20 +02:00
|
|
|
int padding;
|
2013-11-10 15:20:52 +01:00
|
|
|
const char *p;
|
2004-03-19 03:23:59 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is one of the few places where we'd rather not inherit a static
|
|
|
|
* variable's value from the postmaster. But since we will, reset it when
|
2007-08-19 03:41:25 +02:00
|
|
|
* MyProcPid changes. MyStartTime also changes when MyProcPid does, so
|
|
|
|
* reset the formatted start timestamp too.
|
2004-03-19 03:23:59 +01:00
|
|
|
*/
|
|
|
|
if (log_my_pid != MyProcPid)
|
|
|
|
{
|
|
|
|
log_line_number = 0;
|
|
|
|
log_my_pid = MyProcPid;
|
2022-01-12 06:16:59 +01:00
|
|
|
reset_formatted_start_time();
|
2004-03-19 03:23:59 +01:00
|
|
|
}
|
|
|
|
log_line_number++;
|
2004-03-09 05:43:07 +01:00
|
|
|
|
2022-07-11 21:29:33 +02:00
|
|
|
if (format == NULL)
|
2004-03-19 03:23:59 +01:00
|
|
|
return; /* in case guc hasn't run yet */
|
2004-03-09 05:43:07 +01:00
|
|
|
|
2022-07-11 21:29:33 +02:00
|
|
|
for (p = format; *p != '\0'; p++)
|
2004-03-09 05:43:07 +01:00
|
|
|
{
|
2013-09-26 23:54:20 +02:00
|
|
|
if (*p != '%')
|
2004-03-09 05:43:07 +01:00
|
|
|
{
|
2004-03-19 03:23:59 +01:00
|
|
|
/* literal char, just copy */
|
2013-09-26 23:54:20 +02:00
|
|
|
appendStringInfoChar(buf, *p);
|
2004-03-19 03:23:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
2013-09-26 23:54:20 +02:00
|
|
|
|
|
|
|
/* must be a '%', so skip to the next char */
|
|
|
|
p++;
|
|
|
|
if (*p == '\0')
|
2005-06-10 22:48:54 +02:00
|
|
|
break; /* format error - ignore it */
|
2013-09-26 23:54:20 +02:00
|
|
|
else if (*p == '%')
|
|
|
|
{
|
|
|
|
/* string contains %% */
|
|
|
|
appendStringInfoChar(buf, '%');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process any formatting which may exist after the '%'. Note that
|
|
|
|
* process_log_prefix_padding moves p past the padding number if it
|
|
|
|
* exists.
|
|
|
|
*
|
|
|
|
* Note: Since only '-', '0' to '9' are valid formatting characters we
|
|
|
|
* can do a quick check here to pre-check for formatting. If the char
|
|
|
|
* is not formatting then we can skip a useless function call.
|
|
|
|
*
|
|
|
|
* Further note: At least on some platforms, passing %*s rather than
|
|
|
|
* %s to appendStringInfo() is substantially slower, so many of the
|
|
|
|
* cases below avoid doing that unless non-zero padding is in fact
|
|
|
|
* specified.
|
|
|
|
*/
|
|
|
|
if (*p > '9')
|
|
|
|
padding = 0;
|
|
|
|
else if ((p = process_log_prefix_padding(p, &padding)) == NULL)
|
|
|
|
break;
|
2004-03-09 05:43:07 +01:00
|
|
|
|
2004-03-19 03:23:59 +01:00
|
|
|
/* process the option */
|
2013-09-26 23:54:20 +02:00
|
|
|
switch (*p)
|
2004-03-09 05:43:07 +01:00
|
|
|
{
|
2009-11-29 00:38:08 +01:00
|
|
|
case 'a':
|
|
|
|
if (MyProcPort)
|
|
|
|
{
|
|
|
|
const char *appname = application_name;
|
|
|
|
|
|
|
|
if (appname == NULL || *appname == '\0')
|
|
|
|
appname = _("[unknown]");
|
2013-09-26 23:54:20 +02:00
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*s", padding, appname);
|
|
|
|
else
|
|
|
|
appendStringInfoString(buf, appname);
|
2009-11-29 00:38:08 +01:00
|
|
|
}
|
2013-09-26 23:54:20 +02:00
|
|
|
else if (padding != 0)
|
|
|
|
appendStringInfoSpaces(buf,
|
|
|
|
padding > 0 ? padding : -padding);
|
|
|
|
|
2009-11-29 00:38:08 +01:00
|
|
|
break;
|
2020-03-15 11:20:21 +01:00
|
|
|
case 'b':
|
|
|
|
{
|
2022-01-12 06:16:59 +01:00
|
|
|
const char *backend_type_str = get_backend_type_for_log();
|
2020-03-15 11:20:21 +01:00
|
|
|
|
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*s", padding, backend_type_str);
|
|
|
|
else
|
|
|
|
appendStringInfoString(buf, backend_type_str);
|
|
|
|
break;
|
|
|
|
}
|
2004-03-19 03:23:59 +01:00
|
|
|
case 'u':
|
|
|
|
if (MyProcPort)
|
|
|
|
{
|
|
|
|
const char *username = MyProcPort->user_name;
|
2004-03-09 05:43:07 +01:00
|
|
|
|
2004-03-19 03:23:59 +01:00
|
|
|
if (username == NULL || *username == '\0')
|
2005-02-22 05:43:23 +01:00
|
|
|
username = _("[unknown]");
|
2013-09-26 23:54:20 +02:00
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*s", padding, username);
|
|
|
|
else
|
|
|
|
appendStringInfoString(buf, username);
|
2004-03-19 03:23:59 +01:00
|
|
|
}
|
2013-09-26 23:54:20 +02:00
|
|
|
else if (padding != 0)
|
|
|
|
appendStringInfoSpaces(buf,
|
|
|
|
padding > 0 ? padding : -padding);
|
2004-03-19 03:23:59 +01:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (MyProcPort)
|
|
|
|
{
|
|
|
|
const char *dbname = MyProcPort->database_name;
|
2004-03-09 05:43:07 +01:00
|
|
|
|
2004-03-19 03:23:59 +01:00
|
|
|
if (dbname == NULL || *dbname == '\0')
|
2005-02-22 05:43:23 +01:00
|
|
|
dbname = _("[unknown]");
|
2013-09-26 23:54:20 +02:00
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*s", padding, dbname);
|
|
|
|
else
|
|
|
|
appendStringInfoString(buf, dbname);
|
2004-03-19 03:23:59 +01:00
|
|
|
}
|
2013-09-26 23:54:20 +02:00
|
|
|
else if (padding != 0)
|
|
|
|
appendStringInfoSpaces(buf,
|
|
|
|
padding > 0 ? padding : -padding);
|
2004-03-19 03:23:59 +01:00
|
|
|
break;
|
|
|
|
case 'c':
|
2013-09-26 23:54:20 +02:00
|
|
|
if (padding != 0)
|
|
|
|
{
|
|
|
|
char strfbuf[128];
|
2014-05-06 18:12:18 +02:00
|
|
|
|
2013-11-10 15:20:52 +01:00
|
|
|
snprintf(strfbuf, sizeof(strfbuf) - 1, "%lx.%x",
|
2013-09-26 23:54:20 +02:00
|
|
|
(long) (MyStartTime), MyProcPid);
|
|
|
|
appendStringInfo(buf, "%*s", padding, strfbuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
appendStringInfo(buf, "%lx.%x", (long) (MyStartTime), MyProcPid);
|
2004-03-19 03:23:59 +01:00
|
|
|
break;
|
|
|
|
case 'p':
|
2013-09-26 23:54:20 +02:00
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*d", padding, MyProcPid);
|
|
|
|
else
|
|
|
|
appendStringInfo(buf, "%d", MyProcPid);
|
2004-03-19 03:23:59 +01:00
|
|
|
break;
|
2020-08-03 06:38:48 +02:00
|
|
|
|
|
|
|
case 'P':
|
|
|
|
if (MyProc)
|
|
|
|
{
|
|
|
|
PGPROC *leader = MyProc->lockGroupLeader;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Show the leader only for active parallel workers. This
|
|
|
|
* leaves out the leader of a parallel group.
|
|
|
|
*/
|
|
|
|
if (leader == NULL || leader->pid == MyProcPid)
|
|
|
|
appendStringInfoSpaces(buf,
|
|
|
|
padding > 0 ? padding : -padding);
|
|
|
|
else if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*d", padding, leader->pid);
|
|
|
|
else
|
|
|
|
appendStringInfo(buf, "%d", leader->pid);
|
|
|
|
}
|
|
|
|
else if (padding != 0)
|
|
|
|
appendStringInfoSpaces(buf,
|
|
|
|
padding > 0 ? padding : -padding);
|
|
|
|
break;
|
|
|
|
|
2004-03-19 03:23:59 +01:00
|
|
|
case 'l':
|
2013-09-26 23:54:20 +02:00
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*ld", padding, log_line_number);
|
|
|
|
else
|
|
|
|
appendStringInfo(buf, "%ld", log_line_number);
|
2004-03-19 03:23:59 +01:00
|
|
|
break;
|
2005-06-10 00:29:52 +02:00
|
|
|
case 'm':
|
2022-01-12 06:16:59 +01:00
|
|
|
/* force a log timestamp reset */
|
|
|
|
formatted_log_time[0] = '\0';
|
|
|
|
(void) get_formatted_log_time();
|
|
|
|
|
2013-09-26 23:54:20 +02:00
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*s", padding, formatted_log_time);
|
|
|
|
else
|
|
|
|
appendStringInfoString(buf, formatted_log_time);
|
2005-06-10 00:29:52 +02:00
|
|
|
break;
|
2004-03-19 03:23:59 +01:00
|
|
|
case 't':
|
|
|
|
{
|
2007-08-04 03:26:54 +02:00
|
|
|
pg_time_t stamp_time = (pg_time_t) time(NULL);
|
2004-03-22 16:34:22 +01:00
|
|
|
char strfbuf[128];
|
2004-03-09 05:43:07 +01:00
|
|
|
|
2007-08-04 03:26:54 +02:00
|
|
|
pg_strftime(strfbuf, sizeof(strfbuf),
|
|
|
|
"%Y-%m-%d %H:%M:%S %Z",
|
2011-09-09 23:59:11 +02:00
|
|
|
pg_localtime(&stamp_time, log_timezone));
|
2013-09-26 23:54:20 +02:00
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*s", padding, strfbuf);
|
|
|
|
else
|
|
|
|
appendStringInfoString(buf, strfbuf);
|
2004-03-19 03:23:59 +01:00
|
|
|
}
|
|
|
|
break;
|
2015-09-07 22:46:31 +02:00
|
|
|
case 'n':
|
|
|
|
{
|
|
|
|
char strfbuf[128];
|
|
|
|
|
2015-09-08 00:40:49 +02:00
|
|
|
if (!saved_timeval_set)
|
|
|
|
{
|
|
|
|
gettimeofday(&saved_timeval, NULL);
|
|
|
|
saved_timeval_set = true;
|
|
|
|
}
|
|
|
|
|
2016-12-07 01:52:34 +01:00
|
|
|
snprintf(strfbuf, sizeof(strfbuf), "%ld.%03d",
|
|
|
|
(long) saved_timeval.tv_sec,
|
|
|
|
(int) (saved_timeval.tv_usec / 1000));
|
2015-09-07 22:46:31 +02:00
|
|
|
|
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*s", padding, strfbuf);
|
|
|
|
else
|
|
|
|
appendStringInfoString(buf, strfbuf);
|
|
|
|
}
|
|
|
|
break;
|
2004-03-19 03:23:59 +01:00
|
|
|
case 's':
|
2022-01-12 06:16:59 +01:00
|
|
|
{
|
|
|
|
char *start_time = get_formatted_start_time();
|
|
|
|
|
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*s", padding, start_time);
|
|
|
|
else
|
|
|
|
appendStringInfoString(buf, start_time);
|
|
|
|
}
|
2004-03-19 03:23:59 +01:00
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
if (MyProcPort)
|
2005-11-05 04:04:53 +01:00
|
|
|
{
|
|
|
|
const char *psdisp;
|
|
|
|
int displen;
|
|
|
|
|
|
|
|
psdisp = get_ps_display(&displen);
|
2013-09-26 23:54:20 +02:00
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*s", padding, psdisp);
|
|
|
|
else
|
|
|
|
appendBinaryStringInfo(buf, psdisp, displen);
|
2005-11-05 04:04:53 +01:00
|
|
|
}
|
2013-09-26 23:54:20 +02:00
|
|
|
else if (padding != 0)
|
|
|
|
appendStringInfoSpaces(buf,
|
|
|
|
padding > 0 ? padding : -padding);
|
2004-03-19 03:23:59 +01:00
|
|
|
break;
|
|
|
|
case 'r':
|
2005-11-05 04:04:53 +01:00
|
|
|
if (MyProcPort && MyProcPort->remote_host)
|
2004-03-19 03:23:59 +01:00
|
|
|
{
|
2013-11-10 15:20:52 +01:00
|
|
|
if (padding != 0)
|
2013-09-26 23:54:20 +02:00
|
|
|
{
|
|
|
|
if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0')
|
|
|
|
{
|
2013-11-10 15:20:52 +01:00
|
|
|
/*
|
2013-09-26 23:54:20 +02:00
|
|
|
* This option is slightly special as the port
|
2013-11-10 15:20:52 +01:00
|
|
|
* number may be appended onto the end. Here we
|
2013-09-26 23:54:20 +02:00
|
|
|
* need to build 1 string which contains the
|
|
|
|
* remote_host and optionally the remote_port (if
|
|
|
|
* set) so we can properly align the string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *hostport;
|
2014-05-06 18:12:18 +02:00
|
|
|
|
2013-10-22 13:04:41 +02:00
|
|
|
hostport = psprintf("%s(%s)", MyProcPort->remote_host, MyProcPort->remote_port);
|
2013-09-26 23:54:20 +02:00
|
|
|
appendStringInfo(buf, "%*s", padding, hostport);
|
|
|
|
pfree(hostport);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
appendStringInfo(buf, "%*s", padding, MyProcPort->remote_host);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* padding is 0, so we don't need a temp buffer */
|
|
|
|
appendStringInfoString(buf, MyProcPort->remote_host);
|
|
|
|
if (MyProcPort->remote_port &&
|
|
|
|
MyProcPort->remote_port[0] != '\0')
|
2013-11-10 15:20:52 +01:00
|
|
|
appendStringInfo(buf, "(%s)",
|
2013-09-26 23:54:20 +02:00
|
|
|
MyProcPort->remote_port);
|
|
|
|
}
|
2004-03-19 03:23:59 +01:00
|
|
|
}
|
2013-09-26 23:54:20 +02:00
|
|
|
else if (padding != 0)
|
|
|
|
appendStringInfoSpaces(buf,
|
|
|
|
padding > 0 ? padding : -padding);
|
2004-03-19 03:23:59 +01:00
|
|
|
break;
|
2005-06-10 00:29:52 +02:00
|
|
|
case 'h':
|
2013-11-10 15:20:52 +01:00
|
|
|
if (MyProcPort && MyProcPort->remote_host)
|
2013-09-26 23:54:20 +02:00
|
|
|
{
|
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*s", padding, MyProcPort->remote_host);
|
|
|
|
else
|
|
|
|
appendStringInfoString(buf, MyProcPort->remote_host);
|
|
|
|
}
|
|
|
|
else if (padding != 0)
|
|
|
|
appendStringInfoSpaces(buf,
|
|
|
|
padding > 0 ? padding : -padding);
|
2005-06-10 00:29:52 +02:00
|
|
|
break;
|
2004-09-22 05:55:27 +02:00
|
|
|
case 'q':
|
|
|
|
/* in postmaster and friends, stop if %q is seen */
|
2004-03-19 03:23:59 +01:00
|
|
|
/* in a backend, just ignore */
|
|
|
|
if (MyProcPort == NULL)
|
2013-09-26 23:54:20 +02:00
|
|
|
return;
|
2004-03-19 03:23:59 +01:00
|
|
|
break;
|
2007-09-05 20:10:48 +02:00
|
|
|
case 'v':
|
|
|
|
/* keep VXID format in sync with lockfuncs.c */
|
2024-03-03 18:38:22 +01:00
|
|
|
if (MyProc != NULL && MyProc->vxid.procNumber != INVALID_PROC_NUMBER)
|
2013-09-26 23:54:20 +02:00
|
|
|
{
|
|
|
|
if (padding != 0)
|
|
|
|
{
|
|
|
|
char strfbuf[128];
|
2014-05-06 18:12:18 +02:00
|
|
|
|
2013-09-26 23:54:20 +02:00
|
|
|
snprintf(strfbuf, sizeof(strfbuf) - 1, "%d/%u",
|
2024-03-03 18:38:22 +01:00
|
|
|
MyProc->vxid.procNumber, MyProc->vxid.lxid);
|
2013-09-26 23:54:20 +02:00
|
|
|
appendStringInfo(buf, "%*s", padding, strfbuf);
|
|
|
|
}
|
|
|
|
else
|
2024-03-03 18:38:22 +01:00
|
|
|
appendStringInfo(buf, "%d/%u", MyProc->vxid.procNumber, MyProc->vxid.lxid);
|
2013-09-26 23:54:20 +02:00
|
|
|
}
|
|
|
|
else if (padding != 0)
|
|
|
|
appendStringInfoSpaces(buf,
|
|
|
|
padding > 0 ? padding : -padding);
|
2007-09-05 20:10:48 +02:00
|
|
|
break;
|
2004-09-22 05:55:27 +02:00
|
|
|
case 'x':
|
2013-09-26 23:54:20 +02:00
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*u", padding, GetTopTransactionIdIfAny());
|
|
|
|
else
|
|
|
|
appendStringInfo(buf, "%u", GetTopTransactionIdIfAny());
|
2004-09-22 05:55:27 +02:00
|
|
|
break;
|
2009-07-03 21:14:25 +02:00
|
|
|
case 'e':
|
2013-09-26 23:54:20 +02:00
|
|
|
if (padding != 0)
|
|
|
|
appendStringInfo(buf, "%*s", padding, unpack_sql_state(edata->sqlerrcode));
|
|
|
|
else
|
|
|
|
appendStringInfoString(buf, unpack_sql_state(edata->sqlerrcode));
|
2004-03-19 03:23:59 +01:00
|
|
|
break;
|
Make use of in-core query id added by commit 5fd9dfa5f5
Use the in-core query id computation for pg_stat_activity,
log_line_prefix, and EXPLAIN VERBOSE.
Similar to other fields in pg_stat_activity, only the queryid from the
top level statements are exposed, and if the backends status isn't
active then the queryid from the last executed statements is displayed.
Add a %Q placeholder to include the queryid in log_line_prefix, which
will also only expose top level statements.
For EXPLAIN VERBOSE, if a query identifier has been computed, either by
enabling compute_query_id or using a third-party module, display it.
Bump catalog version.
Discussion: https://postgr.es/m/20210407125726.tkvjdbw76hxnpwfi@nol
Author: Julien Rouhaud
Reviewed-by: Alvaro Herrera, Nitin Jadhav, Zhihong Yu
2021-04-07 20:03:56 +02:00
|
|
|
case 'Q':
|
|
|
|
if (padding != 0)
|
2021-04-08 04:30:30 +02:00
|
|
|
appendStringInfo(buf, "%*lld", padding,
|
2021-04-20 18:22:26 +02:00
|
|
|
(long long) pgstat_get_my_query_id());
|
Make use of in-core query id added by commit 5fd9dfa5f5
Use the in-core query id computation for pg_stat_activity,
log_line_prefix, and EXPLAIN VERBOSE.
Similar to other fields in pg_stat_activity, only the queryid from the
top level statements are exposed, and if the backends status isn't
active then the queryid from the last executed statements is displayed.
Add a %Q placeholder to include the queryid in log_line_prefix, which
will also only expose top level statements.
For EXPLAIN VERBOSE, if a query identifier has been computed, either by
enabling compute_query_id or using a third-party module, display it.
Bump catalog version.
Discussion: https://postgr.es/m/20210407125726.tkvjdbw76hxnpwfi@nol
Author: Julien Rouhaud
Reviewed-by: Alvaro Herrera, Nitin Jadhav, Zhihong Yu
2021-04-07 20:03:56 +02:00
|
|
|
else
|
2021-04-08 04:30:30 +02:00
|
|
|
appendStringInfo(buf, "%lld",
|
2021-04-20 18:22:26 +02:00
|
|
|
(long long) pgstat_get_my_query_id());
|
Make use of in-core query id added by commit 5fd9dfa5f5
Use the in-core query id computation for pg_stat_activity,
log_line_prefix, and EXPLAIN VERBOSE.
Similar to other fields in pg_stat_activity, only the queryid from the
top level statements are exposed, and if the backends status isn't
active then the queryid from the last executed statements is displayed.
Add a %Q placeholder to include the queryid in log_line_prefix, which
will also only expose top level statements.
For EXPLAIN VERBOSE, if a query identifier has been computed, either by
enabling compute_query_id or using a third-party module, display it.
Bump catalog version.
Discussion: https://postgr.es/m/20210407125726.tkvjdbw76hxnpwfi@nol
Author: Julien Rouhaud
Reviewed-by: Alvaro Herrera, Nitin Jadhav, Zhihong Yu
2021-04-07 20:03:56 +02:00
|
|
|
break;
|
2004-03-19 03:23:59 +01:00
|
|
|
default:
|
|
|
|
/* format error - ignore it */
|
|
|
|
break;
|
2004-03-09 05:43:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-10 18:23:11 +02:00
|
|
|
/*
|
|
|
|
* Unpack MAKE_SQLSTATE code. Note that this returns a pointer to a
|
|
|
|
* static buffer.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
unpack_sql_state(int sql_state)
|
|
|
|
{
|
|
|
|
static char buf[12];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
{
|
|
|
|
buf[i] = PGUNSIXBIT(sql_state);
|
|
|
|
sql_state >>= 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[i] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write error report to server's log
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
send_message_to_server_log(ErrorData *edata)
|
|
|
|
{
|
|
|
|
StringInfoData buf;
|
2021-10-08 04:08:35 +02:00
|
|
|
bool fallback_to_stderr = false;
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
initStringInfo(&buf);
|
|
|
|
|
2015-09-08 00:40:49 +02:00
|
|
|
saved_timeval_set = false;
|
2007-08-19 03:41:25 +02:00
|
|
|
formatted_log_time[0] = '\0';
|
|
|
|
|
2009-07-03 21:14:25 +02:00
|
|
|
log_line_prefix(&buf, edata);
|
2016-08-26 22:20:17 +02:00
|
|
|
appendStringInfo(&buf, "%s: ", _(error_severity(edata->elevel)));
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2003-06-30 18:47:02 +02:00
|
|
|
if (Log_error_verbosity >= PGERROR_VERBOSE)
|
2005-06-10 18:23:11 +02:00
|
|
|
appendStringInfo(&buf, "%s: ", unpack_sql_state(edata->sqlerrcode));
|
2003-06-30 18:47:02 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
if (edata->message)
|
2003-10-17 18:49:03 +02:00
|
|
|
append_with_tabs(&buf, edata->message);
|
2003-04-24 23:16:45 +02:00
|
|
|
else
|
2005-02-22 05:43:23 +01:00
|
|
|
append_with_tabs(&buf, _("missing error text"));
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2003-06-30 18:47:02 +02:00
|
|
|
if (edata->cursorpos > 0)
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buf, _(" at character %d"),
|
2004-03-21 23:29:11 +01:00
|
|
|
edata->cursorpos);
|
|
|
|
else if (edata->internalpos > 0)
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buf, _(" at character %d"),
|
2004-03-21 23:29:11 +01:00
|
|
|
edata->internalpos);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2003-06-30 18:47:02 +02:00
|
|
|
appendStringInfoChar(&buf, '\n');
|
|
|
|
|
|
|
|
if (Log_error_verbosity >= PGERROR_DEFAULT)
|
|
|
|
{
|
2008-03-24 19:08:47 +01:00
|
|
|
if (edata->detail_log)
|
|
|
|
{
|
2009-07-03 21:14:25 +02:00
|
|
|
log_line_prefix(&buf, edata);
|
2008-03-24 19:08:47 +01:00
|
|
|
appendStringInfoString(&buf, _("DETAIL: "));
|
|
|
|
append_with_tabs(&buf, edata->detail_log);
|
|
|
|
appendStringInfoChar(&buf, '\n');
|
|
|
|
}
|
|
|
|
else if (edata->detail)
|
2003-10-17 18:49:03 +02:00
|
|
|
{
|
2009-07-03 21:14:25 +02:00
|
|
|
log_line_prefix(&buf, edata);
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfoString(&buf, _("DETAIL: "));
|
2003-10-17 18:49:03 +02:00
|
|
|
append_with_tabs(&buf, edata->detail);
|
|
|
|
appendStringInfoChar(&buf, '\n');
|
|
|
|
}
|
2003-06-30 18:47:02 +02:00
|
|
|
if (edata->hint)
|
2003-10-17 18:49:03 +02:00
|
|
|
{
|
2009-07-03 21:14:25 +02:00
|
|
|
log_line_prefix(&buf, edata);
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfoString(&buf, _("HINT: "));
|
2003-10-17 18:49:03 +02:00
|
|
|
append_with_tabs(&buf, edata->hint);
|
|
|
|
appendStringInfoChar(&buf, '\n');
|
|
|
|
}
|
2004-03-21 23:29:11 +01:00
|
|
|
if (edata->internalquery)
|
|
|
|
{
|
2009-07-03 21:14:25 +02:00
|
|
|
log_line_prefix(&buf, edata);
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfoString(&buf, _("QUERY: "));
|
2004-03-21 23:29:11 +01:00
|
|
|
append_with_tabs(&buf, edata->internalquery);
|
|
|
|
appendStringInfoChar(&buf, '\n');
|
|
|
|
}
|
2014-12-25 17:24:30 +01:00
|
|
|
if (edata->context && !edata->hide_ctx)
|
2003-10-17 18:49:03 +02:00
|
|
|
{
|
2009-07-03 21:14:25 +02:00
|
|
|
log_line_prefix(&buf, edata);
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfoString(&buf, _("CONTEXT: "));
|
2003-10-17 18:49:03 +02:00
|
|
|
append_with_tabs(&buf, edata->context);
|
|
|
|
appendStringInfoChar(&buf, '\n');
|
|
|
|
}
|
2003-06-30 18:47:02 +02:00
|
|
|
if (Log_error_verbosity >= PGERROR_VERBOSE)
|
|
|
|
{
|
2003-10-17 18:49:03 +02:00
|
|
|
/* assume no newlines in funcname or filename... */
|
2003-06-30 18:47:02 +02:00
|
|
|
if (edata->funcname && edata->filename)
|
2004-03-19 03:23:59 +01:00
|
|
|
{
|
2009-07-03 21:14:25 +02:00
|
|
|
log_line_prefix(&buf, edata);
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buf, _("LOCATION: %s, %s:%d\n"),
|
2003-06-30 18:47:02 +02:00
|
|
|
edata->funcname, edata->filename,
|
|
|
|
edata->lineno);
|
2004-03-19 03:23:59 +01:00
|
|
|
}
|
2003-06-30 18:47:02 +02:00
|
|
|
else if (edata->filename)
|
2004-03-19 03:23:59 +01:00
|
|
|
{
|
2009-07-03 21:14:25 +02:00
|
|
|
log_line_prefix(&buf, edata);
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buf, _("LOCATION: %s:%d\n"),
|
2003-06-30 18:47:02 +02:00
|
|
|
edata->filename, edata->lineno);
|
2004-03-19 03:23:59 +01:00
|
|
|
}
|
2003-06-30 18:47:02 +02:00
|
|
|
}
|
2020-07-10 08:27:00 +02:00
|
|
|
if (edata->backtrace)
|
|
|
|
{
|
|
|
|
log_line_prefix(&buf, edata);
|
|
|
|
appendStringInfoString(&buf, _("BACKTRACE: "));
|
|
|
|
append_with_tabs(&buf, edata->backtrace);
|
|
|
|
appendStringInfoChar(&buf, '\n');
|
|
|
|
}
|
2003-06-30 18:47:02 +02:00
|
|
|
}
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
/*
|
2003-10-17 18:49:03 +02:00
|
|
|
* If the user wants the query that generated this error logged, do it.
|
2003-04-24 23:16:45 +02:00
|
|
|
*/
|
2022-01-12 06:16:59 +01:00
|
|
|
if (check_log_of_query(edata))
|
2003-10-17 18:49:03 +02:00
|
|
|
{
|
2009-07-03 21:14:25 +02:00
|
|
|
log_line_prefix(&buf, edata);
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfoString(&buf, _("STATEMENT: "));
|
2003-10-17 18:49:03 +02:00
|
|
|
append_with_tabs(&buf, debug_query_string);
|
|
|
|
appendStringInfoChar(&buf, '\n');
|
|
|
|
}
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_SYSLOG
|
|
|
|
/* Write to syslog, if enabled */
|
2004-04-05 05:02:11 +02:00
|
|
|
if (Log_destination & LOG_DESTINATION_SYSLOG)
|
2003-04-24 23:16:45 +02:00
|
|
|
{
|
|
|
|
int syslog_level;
|
|
|
|
|
|
|
|
switch (edata->elevel)
|
|
|
|
{
|
|
|
|
case DEBUG5:
|
|
|
|
case DEBUG4:
|
|
|
|
case DEBUG3:
|
|
|
|
case DEBUG2:
|
|
|
|
case DEBUG1:
|
|
|
|
syslog_level = LOG_DEBUG;
|
|
|
|
break;
|
|
|
|
case LOG:
|
Introduce a LOG_SERVER_ONLY ereport level, which is never sent to client.
This elevel is useful for logging audit messages and similar information
that should not be passed to the client. It's equivalent to LOG in terms
of decisions about logging priority in the postmaster log, but messages
with this elevel will never be sent to the client.
In the current implementation, it's just an alias for the longstanding
COMMERROR elevel (or more accurately, we've made COMMERROR an alias for
this). At some point it might be interesting to allow a LOG_ONLY flag to
be attached to any elevel, but that would be considerably more complicated,
and it's not clear there's enough use-cases to justify the extra work.
For now, let's just take the easy 90% solution.
David Steele, reviewed by Fabien Coelho, Petr Jelínek, and myself
2016-04-04 18:32:42 +02:00
|
|
|
case LOG_SERVER_ONLY:
|
2003-04-24 23:16:45 +02:00
|
|
|
case INFO:
|
|
|
|
syslog_level = LOG_INFO;
|
|
|
|
break;
|
|
|
|
case NOTICE:
|
|
|
|
case WARNING:
|
2020-12-30 00:02:38 +01:00
|
|
|
case WARNING_CLIENT_ONLY:
|
2003-04-24 23:16:45 +02:00
|
|
|
syslog_level = LOG_NOTICE;
|
|
|
|
break;
|
|
|
|
case ERROR:
|
|
|
|
syslog_level = LOG_WARNING;
|
|
|
|
break;
|
|
|
|
case FATAL:
|
|
|
|
syslog_level = LOG_ERR;
|
|
|
|
break;
|
|
|
|
case PANIC:
|
|
|
|
default:
|
|
|
|
syslog_level = LOG_CRIT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_syslog(syslog_level, buf.data);
|
|
|
|
}
|
2002-04-21 02:22:52 +02:00
|
|
|
#endif /* HAVE_SYSLOG */
|
2005-03-12 02:54:44 +01:00
|
|
|
|
2004-04-05 05:02:11 +02:00
|
|
|
#ifdef WIN32
|
2005-03-12 02:54:44 +01:00
|
|
|
/* Write to eventlog, if enabled */
|
2004-04-05 05:02:11 +02:00
|
|
|
if (Log_destination & LOG_DESTINATION_EVENTLOG)
|
|
|
|
{
|
2009-10-17 02:24:51 +02:00
|
|
|
write_eventlog(edata->elevel, buf.data, buf.len);
|
2004-04-05 05:02:11 +02:00
|
|
|
}
|
|
|
|
#endif /* WIN32 */
|
2005-03-12 02:54:44 +01:00
|
|
|
|
2021-10-08 04:08:35 +02:00
|
|
|
/* Write to csvlog, if enabled */
|
|
|
|
if (Log_destination & LOG_DESTINATION_CSVLOG)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Send CSV data if it's safe to do so (syslogger doesn't need the
|
|
|
|
* pipe). If this is not possible, fallback to an entry written to
|
|
|
|
* stderr.
|
|
|
|
*/
|
|
|
|
if (redirection_done || MyBackendType == B_LOGGER)
|
|
|
|
write_csvlog(edata);
|
|
|
|
else
|
|
|
|
fallback_to_stderr = true;
|
|
|
|
}
|
|
|
|
|
Introduce log_destination=jsonlog
"jsonlog" is a new value that can be added to log_destination to provide
logs in the JSON format, with its output written to a file, making it
the third type of destination of this kind, after "stderr" and
"csvlog". The format is convenient to feed logs to other applications.
There is also a plugin external to core that provided this feature using
the hook in elog.c, but this had to overwrite the output of "stderr" to
work, so being able to do both at the same time was not possible. The
files generated by this log format are suffixed with ".json", and use
the same rotation policies as the other two formats depending on the
backend configuration.
This takes advantage of the refactoring work done previously in ac7c807,
bed6ed3, 8b76f89 and 2d77d83 for the backend parts, and 72b76f7 for the
TAP tests, making the addition of any new file-based format rather
straight-forward.
The documentation is updated to list all the keys and the values that
can exist in this new format. pg_current_logfile() also required a
refresh for the new option.
Author: Sehrope Sarkuni, Michael Paquier
Reviewed-by: Nathan Bossart, Justin Pryzby
Discussion: https://postgr.es/m/CAH7T-aqswBM6JWe4pDehi1uOiufqe06DJWaU5=X7dDLyqUExHg@mail.gmail.com
2022-01-17 02:16:53 +01:00
|
|
|
/* Write to JSON log, if enabled */
|
|
|
|
if (Log_destination & LOG_DESTINATION_JSONLOG)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Send JSON data if it's safe to do so (syslogger doesn't need the
|
|
|
|
* pipe). If this is not possible, fallback to an entry written to
|
|
|
|
* stderr.
|
|
|
|
*/
|
|
|
|
if (redirection_done || MyBackendType == B_LOGGER)
|
|
|
|
{
|
|
|
|
write_jsonlog(edata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fallback_to_stderr = true;
|
|
|
|
}
|
|
|
|
|
2021-10-08 04:08:35 +02:00
|
|
|
/*
|
|
|
|
* Write to stderr, if enabled or if required because of a previous
|
|
|
|
* limitation.
|
|
|
|
*/
|
|
|
|
if ((Log_destination & LOG_DESTINATION_STDERR) ||
|
|
|
|
whereToSendOutput == DestDebug ||
|
|
|
|
fallback_to_stderr)
|
2005-02-27 02:02:57 +01:00
|
|
|
{
|
2007-07-19 23:58:12 +02:00
|
|
|
/*
|
|
|
|
* Use the chunking protocol if we know the syslogger should be
|
|
|
|
* catching stderr output, and we are not ourselves the syslogger.
|
|
|
|
* Otherwise, just do a vanilla write to stderr.
|
|
|
|
*/
|
2020-03-11 16:36:40 +01:00
|
|
|
if (redirection_done && MyBackendType != B_LOGGER)
|
2007-08-19 03:41:25 +02:00
|
|
|
write_pipe_chunks(buf.data, buf.len, LOG_DESTINATION_STDERR);
|
2005-02-27 02:02:57 +01:00
|
|
|
#ifdef WIN32
|
2014-05-06 18:12:18 +02:00
|
|
|
|
2005-03-12 02:54:44 +01:00
|
|
|
/*
|
|
|
|
* In a win32 service environment, there is no usable stderr. Capture
|
|
|
|
* anything going there and write it to the eventlog instead.
|
|
|
|
*
|
2007-07-19 23:58:12 +02:00
|
|
|
* If stderr redirection is active, it was OK to write to stderr above
|
|
|
|
* because that's really a pipe to the syslogger process.
|
2005-03-12 02:54:44 +01:00
|
|
|
*/
|
2007-07-19 23:58:12 +02:00
|
|
|
else if (pgwin32_is_service())
|
2009-10-17 02:24:51 +02:00
|
|
|
write_eventlog(edata->elevel, buf.data, buf.len);
|
2005-02-27 02:02:57 +01:00
|
|
|
#endif
|
2007-07-19 23:58:12 +02:00
|
|
|
else
|
2009-10-17 02:24:51 +02:00
|
|
|
write_console(buf.data, buf.len);
|
2005-02-27 02:02:57 +01:00
|
|
|
}
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2007-08-23 03:24:43 +02:00
|
|
|
/* If in the syslogger process, try to write messages direct to file */
|
2020-03-11 16:36:40 +01:00
|
|
|
if (MyBackendType == B_LOGGER)
|
2007-08-23 03:24:43 +02:00
|
|
|
write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_STDERR);
|
|
|
|
|
2021-10-08 04:08:35 +02:00
|
|
|
/* No more need of the message formatted for stderr */
|
|
|
|
pfree(buf.data);
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
2001-06-08 23:16:49 +02:00
|
|
|
|
2007-06-14 03:48:51 +02:00
|
|
|
/*
|
|
|
|
* Send data to the syslogger using the chunked protocol
|
2011-10-19 03:37:51 +02:00
|
|
|
*
|
|
|
|
* Note: when there are multiple backends writing into the syslogger pipe,
|
|
|
|
* it's critical that each write go into the pipe indivisibly, and not
|
|
|
|
* get interleaved with data from other processes. Fortunately, the POSIX
|
|
|
|
* spec requires that writes to pipes be atomic so long as they are not
|
|
|
|
* more than PIPE_BUF bytes long. So we divide long messages into chunks
|
|
|
|
* that are no more than that length, and send one chunk per write() call.
|
|
|
|
* The collector process knows how to reassemble the chunks.
|
|
|
|
*
|
|
|
|
* Because of the atomic write requirement, there are only two possible
|
|
|
|
* results from write() here: -1 for failure, or the requested number of
|
|
|
|
* bytes. There is not really anything we can do about a failure; retry would
|
|
|
|
* probably be an infinite loop, and we can't even report the error usefully.
|
|
|
|
* (There is noplace else we could send it!) So we might as well just ignore
|
|
|
|
* the result from write(). However, on some platforms you get a compiler
|
|
|
|
* warning from ignoring write()'s result, so do a little dance with casting
|
|
|
|
* rc to void to shut up the compiler.
|
2007-06-14 03:48:51 +02:00
|
|
|
*/
|
2022-01-12 06:16:59 +01:00
|
|
|
void
|
2007-08-19 03:41:25 +02:00
|
|
|
write_pipe_chunks(char *data, int len, int dest)
|
2007-06-14 03:48:51 +02:00
|
|
|
{
|
|
|
|
PipeProtoChunk p;
|
2007-08-19 03:41:25 +02:00
|
|
|
int fd = fileno(stderr);
|
2011-10-19 03:37:51 +02:00
|
|
|
int rc;
|
2007-08-19 03:41:25 +02:00
|
|
|
|
2007-06-14 03:48:51 +02:00
|
|
|
Assert(len > 0);
|
|
|
|
|
|
|
|
p.proto.nuls[0] = p.proto.nuls[1] = '\0';
|
|
|
|
p.proto.pid = MyProcPid;
|
2021-09-13 02:03:45 +02:00
|
|
|
p.proto.flags = 0;
|
|
|
|
if (dest == LOG_DESTINATION_STDERR)
|
|
|
|
p.proto.flags |= PIPE_PROTO_DEST_STDERR;
|
|
|
|
else if (dest == LOG_DESTINATION_CSVLOG)
|
|
|
|
p.proto.flags |= PIPE_PROTO_DEST_CSVLOG;
|
Introduce log_destination=jsonlog
"jsonlog" is a new value that can be added to log_destination to provide
logs in the JSON format, with its output written to a file, making it
the third type of destination of this kind, after "stderr" and
"csvlog". The format is convenient to feed logs to other applications.
There is also a plugin external to core that provided this feature using
the hook in elog.c, but this had to overwrite the output of "stderr" to
work, so being able to do both at the same time was not possible. The
files generated by this log format are suffixed with ".json", and use
the same rotation policies as the other two formats depending on the
backend configuration.
This takes advantage of the refactoring work done previously in ac7c807,
bed6ed3, 8b76f89 and 2d77d83 for the backend parts, and 72b76f7 for the
TAP tests, making the addition of any new file-based format rather
straight-forward.
The documentation is updated to list all the keys and the values that
can exist in this new format. pg_current_logfile() also required a
refresh for the new option.
Author: Sehrope Sarkuni, Michael Paquier
Reviewed-by: Nathan Bossart, Justin Pryzby
Discussion: https://postgr.es/m/CAH7T-aqswBM6JWe4pDehi1uOiufqe06DJWaU5=X7dDLyqUExHg@mail.gmail.com
2022-01-17 02:16:53 +01:00
|
|
|
else if (dest == LOG_DESTINATION_JSONLOG)
|
|
|
|
p.proto.flags |= PIPE_PROTO_DEST_JSONLOG;
|
2007-06-14 03:48:51 +02:00
|
|
|
|
|
|
|
/* write all but the last chunk */
|
|
|
|
while (len > PIPE_MAX_PAYLOAD)
|
|
|
|
{
|
2021-09-13 02:03:45 +02:00
|
|
|
/* no need to set PIPE_PROTO_IS_LAST yet */
|
2007-06-14 03:48:51 +02:00
|
|
|
p.proto.len = PIPE_MAX_PAYLOAD;
|
|
|
|
memcpy(p.proto.data, data, PIPE_MAX_PAYLOAD);
|
2011-10-19 03:37:51 +02:00
|
|
|
rc = write(fd, &p, PIPE_HEADER_SIZE + PIPE_MAX_PAYLOAD);
|
|
|
|
(void) rc;
|
2007-06-14 03:48:51 +02:00
|
|
|
data += PIPE_MAX_PAYLOAD;
|
|
|
|
len -= PIPE_MAX_PAYLOAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the last chunk */
|
2021-09-13 02:03:45 +02:00
|
|
|
p.proto.flags |= PIPE_PROTO_IS_LAST;
|
2007-06-14 03:48:51 +02:00
|
|
|
p.proto.len = len;
|
|
|
|
memcpy(p.proto.data, data, len);
|
2011-10-19 03:37:51 +02:00
|
|
|
rc = write(fd, &p, PIPE_HEADER_SIZE + len);
|
|
|
|
(void) rc;
|
2007-06-14 03:48:51 +02:00
|
|
|
}
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2009-03-02 22:18:43 +01:00
|
|
|
/*
|
|
|
|
* Append a text string to the error report being built for the client.
|
|
|
|
*
|
|
|
|
* This is ordinarily identical to pq_sendstring(), but if we are in
|
|
|
|
* error recursion trouble we skip encoding conversion, because of the
|
|
|
|
* possibility that the problem is a failure in the encoding conversion
|
|
|
|
* subsystem itself. Code elsewhere should ensure that the passed-in
|
|
|
|
* strings will be plain 7-bit ASCII, and thus not in need of conversion,
|
|
|
|
* in such cases. (In particular, we disable localization of error messages
|
|
|
|
* to help ensure that's true.)
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
err_sendstring(StringInfo buf, const char *str)
|
|
|
|
{
|
|
|
|
if (in_error_recursion_trouble())
|
|
|
|
pq_send_ascii_string(buf, str);
|
|
|
|
else
|
|
|
|
pq_sendstring(buf, str);
|
|
|
|
}
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
|
|
|
* Write error report to client
|
|
|
|
*/
|
2001-06-08 23:16:49 +02:00
|
|
|
static void
|
2003-04-24 23:16:45 +02:00
|
|
|
send_message_to_frontend(ErrorData *edata)
|
2001-06-08 23:16:49 +02:00
|
|
|
{
|
2003-04-24 23:16:45 +02:00
|
|
|
StringInfoData msgbuf;
|
2001-06-08 23:16:49 +02:00
|
|
|
|
2021-03-04 09:45:55 +01:00
|
|
|
/*
|
|
|
|
* We no longer support pre-3.0 FE/BE protocol, except here. If a client
|
|
|
|
* tries to connect using an older protocol version, it's nice to send the
|
|
|
|
* "protocol version not supported" error in a format the client
|
|
|
|
* understands. If protocol hasn't been set yet, early in backend
|
|
|
|
* startup, assume modern protocol.
|
|
|
|
*/
|
|
|
|
if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3 || FrontendProtocol == 0)
|
2003-04-24 23:16:45 +02:00
|
|
|
{
|
|
|
|
/* New style with separate fields */
|
2016-08-26 22:20:17 +02:00
|
|
|
const char *sev;
|
2003-04-24 23:16:45 +02:00
|
|
|
char tbuf[12];
|
|
|
|
|
2021-03-04 09:45:55 +01:00
|
|
|
/* 'N' (Notice) is for nonfatal conditions, 'E' is for errors */
|
2023-08-23 04:16:12 +02:00
|
|
|
if (edata->elevel < ERROR)
|
|
|
|
pq_beginmessage(&msgbuf, PqMsg_NoticeResponse);
|
|
|
|
else
|
|
|
|
pq_beginmessage(&msgbuf, PqMsg_ErrorResponse);
|
2021-03-04 09:45:55 +01:00
|
|
|
|
2016-08-26 22:20:17 +02:00
|
|
|
sev = error_severity(edata->elevel);
|
2003-08-27 02:33:34 +02:00
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_SEVERITY);
|
2016-08-26 22:20:17 +02:00
|
|
|
err_sendstring(&msgbuf, _(sev));
|
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_SEVERITY_NONLOCALIZED);
|
|
|
|
err_sendstring(&msgbuf, sev);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
2003-08-27 02:33:34 +02:00
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_SQLSTATE);
|
2021-09-01 04:48:08 +02:00
|
|
|
err_sendstring(&msgbuf, unpack_sql_state(edata->sqlerrcode));
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
/* M field is required per protocol, so always send something */
|
2003-08-27 02:33:34 +02:00
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_MESSAGE_PRIMARY);
|
2003-04-24 23:16:45 +02:00
|
|
|
if (edata->message)
|
2009-03-02 22:18:43 +01:00
|
|
|
err_sendstring(&msgbuf, edata->message);
|
2003-04-24 23:16:45 +02:00
|
|
|
else
|
2009-03-02 22:18:43 +01:00
|
|
|
err_sendstring(&msgbuf, _("missing error text"));
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
if (edata->detail)
|
|
|
|
{
|
2003-08-27 02:33:34 +02:00
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_MESSAGE_DETAIL);
|
2009-03-02 22:18:43 +01:00
|
|
|
err_sendstring(&msgbuf, edata->detail);
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
2008-03-24 19:08:47 +01:00
|
|
|
/* detail_log is intentionally not used here */
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
if (edata->hint)
|
|
|
|
{
|
2003-08-27 02:33:34 +02:00
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_MESSAGE_HINT);
|
2009-03-02 22:18:43 +01:00
|
|
|
err_sendstring(&msgbuf, edata->hint);
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (edata->context)
|
|
|
|
{
|
2003-08-27 02:33:34 +02:00
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_CONTEXT);
|
2009-03-02 22:18:43 +01:00
|
|
|
err_sendstring(&msgbuf, edata->context);
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
Provide database object names as separate fields in error messages.
This patch addresses the problem that applications currently have to
extract object names from possibly-localized textual error messages,
if they want to know for example which index caused a UNIQUE_VIOLATION
failure. It adds new error message fields to the wire protocol, which
can carry the name of a table, table column, data type, or constraint
associated with the error. (Since the protocol spec has always instructed
clients to ignore unrecognized field types, this should not create any
compatibility problem.)
Support for providing these new fields has been added to just a limited set
of error reports (mainly, those in the "integrity constraint violation"
SQLSTATE class), but we will doubtless add them to more calls in future.
Pavel Stehule, reviewed and extensively revised by Peter Geoghegan, with
additional hacking by Tom Lane.
2013-01-29 23:06:26 +01:00
|
|
|
if (edata->schema_name)
|
|
|
|
{
|
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_SCHEMA_NAME);
|
|
|
|
err_sendstring(&msgbuf, edata->schema_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (edata->table_name)
|
|
|
|
{
|
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_TABLE_NAME);
|
|
|
|
err_sendstring(&msgbuf, edata->table_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (edata->column_name)
|
|
|
|
{
|
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_COLUMN_NAME);
|
|
|
|
err_sendstring(&msgbuf, edata->column_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (edata->datatype_name)
|
|
|
|
{
|
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_DATATYPE_NAME);
|
|
|
|
err_sendstring(&msgbuf, edata->datatype_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (edata->constraint_name)
|
|
|
|
{
|
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_CONSTRAINT_NAME);
|
|
|
|
err_sendstring(&msgbuf, edata->constraint_name);
|
|
|
|
}
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
if (edata->cursorpos > 0)
|
|
|
|
{
|
|
|
|
snprintf(tbuf, sizeof(tbuf), "%d", edata->cursorpos);
|
2003-08-27 02:33:34 +02:00
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_STATEMENT_POSITION);
|
2009-03-02 22:18:43 +01:00
|
|
|
err_sendstring(&msgbuf, tbuf);
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
2004-03-21 23:29:11 +01:00
|
|
|
if (edata->internalpos > 0)
|
|
|
|
{
|
|
|
|
snprintf(tbuf, sizeof(tbuf), "%d", edata->internalpos);
|
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_INTERNAL_POSITION);
|
2009-03-02 22:18:43 +01:00
|
|
|
err_sendstring(&msgbuf, tbuf);
|
2004-03-21 23:29:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (edata->internalquery)
|
|
|
|
{
|
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_INTERNAL_QUERY);
|
2009-03-02 22:18:43 +01:00
|
|
|
err_sendstring(&msgbuf, edata->internalquery);
|
2004-03-21 23:29:11 +01:00
|
|
|
}
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
if (edata->filename)
|
|
|
|
{
|
2003-08-27 02:33:34 +02:00
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_SOURCE_FILE);
|
2009-03-02 22:18:43 +01:00
|
|
|
err_sendstring(&msgbuf, edata->filename);
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (edata->lineno > 0)
|
|
|
|
{
|
|
|
|
snprintf(tbuf, sizeof(tbuf), "%d", edata->lineno);
|
2003-08-27 02:33:34 +02:00
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_SOURCE_LINE);
|
2009-03-02 22:18:43 +01:00
|
|
|
err_sendstring(&msgbuf, tbuf);
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (edata->funcname)
|
|
|
|
{
|
2003-08-27 02:33:34 +02:00
|
|
|
pq_sendbyte(&msgbuf, PG_DIAG_SOURCE_FUNCTION);
|
2009-03-02 22:18:43 +01:00
|
|
|
err_sendstring(&msgbuf, edata->funcname);
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pq_sendbyte(&msgbuf, '\0'); /* terminator */
|
2021-03-04 09:45:55 +01:00
|
|
|
|
|
|
|
pq_endmessage(&msgbuf);
|
2003-04-24 23:16:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Old style --- gin up a backwards-compatible message */
|
|
|
|
StringInfoData buf;
|
|
|
|
|
|
|
|
initStringInfo(&buf);
|
|
|
|
|
2016-08-26 22:20:17 +02:00
|
|
|
appendStringInfo(&buf, "%s: ", _(error_severity(edata->elevel)));
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
if (edata->message)
|
2003-10-17 18:49:03 +02:00
|
|
|
appendStringInfoString(&buf, edata->message);
|
2003-04-24 23:16:45 +02:00
|
|
|
else
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfoString(&buf, _("missing error text"));
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
appendStringInfoChar(&buf, '\n');
|
|
|
|
|
2021-03-04 09:45:55 +01:00
|
|
|
/* 'N' (Notice) is for nonfatal conditions, 'E' is for errors */
|
|
|
|
pq_putmessage_v2((edata->elevel < ERROR) ? 'N' : 'E', buf.data, buf.len + 1);
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
pfree(buf.data);
|
|
|
|
}
|
|
|
|
|
2001-06-08 23:16:49 +02:00
|
|
|
/*
|
|
|
|
* This flush is normally not necessary, since postgres.c will flush out
|
|
|
|
* waiting data when control returns to the main loop. But it seems best
|
|
|
|
* to leave it here, so that the client has some clue what happened if the
|
|
|
|
* backend dies before getting back to the main loop ... error/notice
|
|
|
|
* messages should not be a performance-critical path anyway, so an extra
|
|
|
|
* flush won't hurt much ...
|
|
|
|
*/
|
|
|
|
pq_flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
|
|
|
* Support routines for formatting error messages.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2016-08-26 22:20:17 +02:00
|
|
|
* error_severity --- get string representing elevel
|
|
|
|
*
|
|
|
|
* The string is not localized here, but we mark the strings for translation
|
|
|
|
* so that callers can invoke _() on the result.
|
2003-04-24 23:16:45 +02:00
|
|
|
*/
|
2022-01-12 06:16:59 +01:00
|
|
|
const char *
|
2003-04-24 23:16:45 +02:00
|
|
|
error_severity(int elevel)
|
2001-06-08 23:16:49 +02:00
|
|
|
{
|
2003-04-24 23:16:45 +02:00
|
|
|
const char *prefix;
|
2001-06-08 23:16:49 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
switch (elevel)
|
2001-06-08 23:16:49 +02:00
|
|
|
{
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
case DEBUG1:
|
|
|
|
case DEBUG2:
|
|
|
|
case DEBUG3:
|
|
|
|
case DEBUG4:
|
|
|
|
case DEBUG5:
|
2016-08-26 22:20:17 +02:00
|
|
|
prefix = gettext_noop("DEBUG");
|
2001-06-08 23:16:49 +02:00
|
|
|
break;
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
case LOG:
|
Introduce a LOG_SERVER_ONLY ereport level, which is never sent to client.
This elevel is useful for logging audit messages and similar information
that should not be passed to the client. It's equivalent to LOG in terms
of decisions about logging priority in the postmaster log, but messages
with this elevel will never be sent to the client.
In the current implementation, it's just an alias for the longstanding
COMMERROR elevel (or more accurately, we've made COMMERROR an alias for
this). At some point it might be interesting to allow a LOG_ONLY flag to
be attached to any elevel, but that would be considerably more complicated,
and it's not clear there's enough use-cases to justify the extra work.
For now, let's just take the easy 90% solution.
David Steele, reviewed by Fabien Coelho, Petr Jelínek, and myself
2016-04-04 18:32:42 +02:00
|
|
|
case LOG_SERVER_ONLY:
|
2016-08-26 22:20:17 +02:00
|
|
|
prefix = gettext_noop("LOG");
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
break;
|
|
|
|
case INFO:
|
2016-08-26 22:20:17 +02:00
|
|
|
prefix = gettext_noop("INFO");
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
break;
|
2001-06-08 23:16:49 +02:00
|
|
|
case NOTICE:
|
2016-08-26 22:20:17 +02:00
|
|
|
prefix = gettext_noop("NOTICE");
|
2001-06-08 23:16:49 +02:00
|
|
|
break;
|
2002-03-06 07:10:59 +01:00
|
|
|
case WARNING:
|
2020-12-30 00:02:38 +01:00
|
|
|
case WARNING_CLIENT_ONLY:
|
2016-08-26 22:20:17 +02:00
|
|
|
prefix = gettext_noop("WARNING");
|
2002-03-06 07:10:59 +01:00
|
|
|
break;
|
2001-06-08 23:16:49 +02:00
|
|
|
case ERROR:
|
2016-08-26 22:20:17 +02:00
|
|
|
prefix = gettext_noop("ERROR");
|
2001-06-08 23:16:49 +02:00
|
|
|
break;
|
|
|
|
case FATAL:
|
2016-08-26 22:20:17 +02:00
|
|
|
prefix = gettext_noop("FATAL");
|
2001-06-08 23:16:49 +02:00
|
|
|
break;
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
case PANIC:
|
2016-08-26 22:20:17 +02:00
|
|
|
prefix = gettext_noop("PANIC");
|
2003-04-24 23:16:45 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
prefix = "???";
|
2001-06-08 23:16:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefix;
|
|
|
|
}
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
|
2003-10-08 05:49:38 +02:00
|
|
|
/*
|
2003-10-17 18:49:03 +02:00
|
|
|
* append_with_tabs
|
2003-10-08 05:49:38 +02:00
|
|
|
*
|
2003-10-17 18:49:03 +02:00
|
|
|
* Append the string to the StringInfo buffer, inserting a tab after any
|
|
|
|
* newline.
|
2003-10-08 05:49:38 +02:00
|
|
|
*/
|
2003-10-17 18:49:03 +02:00
|
|
|
static void
|
|
|
|
append_with_tabs(StringInfo buf, const char *str)
|
2003-10-08 05:49:38 +02:00
|
|
|
{
|
2003-10-17 18:49:03 +02:00
|
|
|
char ch;
|
2003-10-08 05:49:38 +02:00
|
|
|
|
2003-10-17 18:49:03 +02:00
|
|
|
while ((ch = *str++) != '\0')
|
2003-10-08 05:49:38 +02:00
|
|
|
{
|
2003-10-17 18:49:03 +02:00
|
|
|
appendStringInfoCharMacro(buf, ch);
|
|
|
|
if (ch == '\n')
|
|
|
|
appendStringInfoCharMacro(buf, '\t');
|
2003-10-08 05:49:38 +02:00
|
|
|
}
|
|
|
|
}
|
2004-06-24 23:03:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write errors to stderr (or by equal means when stderr is
|
|
|
|
* not available). Used before ereport/elog can be used
|
|
|
|
* safely (memory context, GUC load etc)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
write_stderr(const char *fmt,...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-10-17 02:24:51 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
char errbuf[2048]; /* Arbitrary size? */
|
|
|
|
#endif
|
2004-06-24 23:03:42 +02:00
|
|
|
|
2005-02-22 05:43:23 +01:00
|
|
|
fmt = _(fmt);
|
2004-06-24 23:03:42 +02:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#ifndef WIN32
|
|
|
|
/* On Unix, we just fprintf to stderr */
|
|
|
|
vfprintf(stderr, fmt, ap);
|
2007-06-14 03:48:51 +02:00
|
|
|
fflush(stderr);
|
2004-06-24 23:03:42 +02:00
|
|
|
#else
|
2009-10-17 02:24:51 +02:00
|
|
|
vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-06-24 23:03:42 +02:00
|
|
|
/*
|
|
|
|
* On Win32, we print to stderr if running on a console, or write to
|
|
|
|
* eventlog if running as a service
|
|
|
|
*/
|
|
|
|
if (pgwin32_is_service()) /* Running as a service */
|
|
|
|
{
|
2009-10-17 02:24:51 +02:00
|
|
|
write_eventlog(ERROR, errbuf, strlen(errbuf));
|
2004-06-24 23:03:42 +02:00
|
|
|
}
|
|
|
|
else
|
2007-06-14 03:48:51 +02:00
|
|
|
{
|
2004-10-07 17:21:58 +02:00
|
|
|
/* Not running as service, write to stderr */
|
2009-10-17 02:24:51 +02:00
|
|
|
write_console(errbuf, strlen(errbuf));
|
2007-06-14 03:48:51 +02:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
2004-06-24 23:03:42 +02:00
|
|
|
#endif
|
|
|
|
va_end(ap);
|
|
|
|
}
|