/src/backend/utils/error/elog.c
C | 3512 lines | 2055 code | 457 blank | 1000 comment | 461 complexity | 9381f471cab1b9ce2347102416768978 MD5 | raw file
Possible License(s): AGPL-3.0
Large files files are truncated, but you can click here to view the full file
- /*-------------------------------------------------------------------------
- *
- * elog.c
- * error logging and reporting
- *
- * 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.
- *
- * 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
- * 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.)
- *
- *
- * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- *
- * IDENTIFICATION
- * src/backend/utils/error/elog.c
- *
- *-------------------------------------------------------------------------
- */
- #include "postgres.h"
- #include <fcntl.h>
- #include <time.h>
- #include <unistd.h>
- #include <signal.h>
- #include <ctype.h>
- #ifdef HAVE_SYSLOG
- #include <syslog.h>
- #endif
- #ifdef HAVE_EXECINFO_H
- #include <execinfo.h>
- #endif
- #include "access/transam.h"
- #include "access/xact.h"
- #include "libpq/libpq.h"
- #include "libpq/pqformat.h"
- #include "mb/pg_wchar.h"
- #include "miscadmin.h"
- #include "postmaster/bgworker.h"
- #include "postmaster/postmaster.h"
- #include "postmaster/syslogger.h"
- #include "storage/ipc.h"
- #include "storage/proc.h"
- #include "tcop/tcopprot.h"
- #include "utils/guc.h"
- #include "utils/memutils.h"
- #include "utils/ps_status.h"
- /* In this module, access gettext() via err_gettext() */
- #undef _
- #define _(x) err_gettext(x)
- /* Global variables */
- ErrorContextCallback *error_context_stack = NULL;
- sigjmp_buf *PG_exception_stack = NULL;
- extern bool redirection_done;
- /*
- * 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;
- /* GUC parameters */
- int Log_error_verbosity = PGERROR_VERBOSE;
- char *Log_line_prefix = NULL; /* format for extra log line info */
- int Log_destination = LOG_DESTINATION_STDERR;
- char *Log_destination_string = NULL;
- bool syslog_sequence_numbers = true;
- bool syslog_split_messages = true;
- #ifdef HAVE_SYSLOG
- /*
- * 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
- * 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.)
- */
- #ifndef PG_SYSLOG_LIMIT
- #define PG_SYSLOG_LIMIT 900
- #endif
- static bool openlog_done = false;
- static char *syslog_ident = NULL;
- static int syslog_facility = LOG_LOCAL0;
- static void write_syslog(int level, const char *line);
- #endif
- #ifdef WIN32
- extern char *event_source;
- static void write_eventlog(int level, const char *line, int len);
- #endif
- /* 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 */
- /*
- * Saved timeval and buffers for formatted timestamps that might be used by
- * both log_line_prefix and csv logs.
- */
- static struct timeval saved_timeval;
- static bool saved_timeval_set = false;
- #define FORMATTED_TS_LEN 128
- static char formatted_start_time[FORMATTED_TS_LEN];
- static char formatted_log_time[FORMATTED_TS_LEN];
- /* 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)
- static const char *err_gettext(const char *str) pg_attribute_format_arg(1);
- static pg_noinline void set_backtrace(ErrorData *edata, int num_skip);
- static void set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str);
- static void write_console(const char *line, int len);
- static void setup_formatted_log_time(void);
- static void setup_formatted_start_time(void);
- static const char *process_log_prefix_padding(const char *p, int *padding);
- static void log_line_prefix(StringInfo buf, ErrorData *edata);
- static void write_csvlog(ErrorData *edata);
- static void send_message_to_server_log(ErrorData *edata);
- static void write_pipe_chunks(char *data, int len, int dest);
- static void send_message_to_frontend(ErrorData *edata);
- static const char *error_severity(int elevel);
- static void append_with_tabs(StringInfo buf, const char *str);
- static bool is_log_level_output(int elevel, int log_min_level);
- /*
- * 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);
- }
- /*
- * 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
- }
- /*
- * errstart --- begin an error-reporting cycle
- *
- * 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.
- *
- * Returns true in normal case. Returns false to short-circuit the error
- * report (if it's a warning or lower and not to be reported anywhere).
- */
- bool
- errstart(int elevel, const char *domain)
- {
- ErrorData *edata;
- bool output_to_server;
- bool output_to_client = false;
- int i;
- /*
- * 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.
- */
- if (elevel >= ERROR)
- {
- /*
- * If we are inside a critical section, all errors become PANIC
- * errors. See miscadmin.h.
- */
- if (CritSectionCount > 0)
- elevel = PANIC;
- /*
- * 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);
- }
- /*
- * Now decide whether we need to process this report at all; if it's
- * warning or less and not enabled for logging, just return false without
- * starting up any error logging machinery.
- */
- /* Determine whether message is enabled for server log output */
- output_to_server = is_log_level_output(elevel, log_min_messages);
- /* Determine whether message is enabled for client output */
- 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)
- output_to_client = (elevel >= ERROR);
- else
- output_to_client = (elevel >= client_min_messages ||
- elevel == INFO);
- }
- /* Skip processing effort if non-error message will not be output */
- if (elevel < ERROR && !output_to_server && !output_to_client)
- return false;
- /*
- * 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)
- {
- /* Oops, hard crash time; very little we can do safely here */
- write_stderr("error occurred before error message processing is available\n");
- exit(2);
- }
- /*
- * Okay, crank up a stack entry to store the info in.
- */
- if (recursion_depth++ > 0 && elevel >= ERROR)
- {
- /*
- * Oops, error during error processing. Clear ErrorContext as
- * discussed at top of file. We will not return to the original
- * error's reporter or handler, so we don't need it.
- */
- MemoryContextReset(ErrorContext);
- /*
- * Infinite error recursion might be due to something broken in a
- * 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).
- */
- if (in_error_recursion_trouble())
- {
- error_context_stack = NULL;
- debug_query_string = NULL;
- }
- }
- if (++errordata_stack_depth >= ERRORDATA_STACK_SIZE)
- {
- /*
- * Wups, stack not big enough. We treat this as a PANIC condition
- * because it suggests an infinite loop of errors during error
- * recovery.
- */
- errordata_stack_depth = -1; /* make room on stack */
- ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded")));
- }
- /* Initialize data for this error frame */
- edata = &errordata[errordata_stack_depth];
- MemSet(edata, 0, sizeof(ErrorData));
- edata->elevel = elevel;
- edata->output_to_server = output_to_server;
- edata->output_to_client = output_to_client;
- /* 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;
- /* Select default errcode based on elevel */
- if (elevel >= ERROR)
- edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
- else if (elevel == WARNING)
- edata->sqlerrcode = ERRCODE_WARNING;
- else
- edata->sqlerrcode = ERRCODE_SUCCESSFUL_COMPLETION;
- /* errno is saved here so that error parameter eval can't change it */
- edata->saved_errno = errno;
- /*
- * Any allocations for this error state level should go into ErrorContext
- */
- edata->assoc_context = ErrorContext;
- recursion_depth--;
- return true;
- }
- /*
- * Checks whether the given funcname matches backtrace_functions; see
- * check_backtrace_functions.
- */
- static bool
- matches_backtrace_functions(const char *funcname)
- {
- char *p;
- if (!backtrace_symbol_list || funcname == NULL || funcname[0] == '\0')
- return false;
- p = backtrace_symbol_list;
- for (;;)
- {
- if (*p == '\0') /* end of backtrace_symbol_list */
- break;
- if (strcmp(funcname, p) == 0)
- return true;
- p += strlen(p) + 1;
- }
- return false;
- }
- /*
- * errfinish --- end an error-reporting cycle
- *
- * Produce the appropriate error report(s) and pop the error stack.
- *
- * 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.
- */
- void
- errfinish(const char *filename, int lineno, const char *funcname)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- int elevel;
- MemoryContext oldcontext;
- ErrorContextCallback *econtext;
- recursion_depth++;
- CHECK_STACK_DEPTH();
- /* Save the last few bits of error state into the stack entry */
- if (filename)
- {
- const char *slash;
- /* keep only base name, useful especially for vpath builds */
- slash = strrchr(filename, '/');
- if (slash)
- filename = slash + 1;
- }
- edata->filename = filename;
- edata->lineno = lineno;
- edata->funcname = funcname;
- elevel = edata->elevel;
- /*
- * Do processing in ErrorContext, which we hope has enough reserved space
- * to report an error.
- */
- oldcontext = MemoryContextSwitchTo(ErrorContext);
- if (!edata->backtrace &&
- edata->funcname &&
- backtrace_functions &&
- matches_backtrace_functions(edata->funcname))
- set_backtrace(edata, 2);
- /*
- * 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).
- */
- for (econtext = error_context_stack;
- econtext != NULL;
- econtext = econtext->previous)
- econtext->callback(econtext->arg);
- /*
- * 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.
- */
- if (elevel == ERROR)
- {
- /*
- * We do some minimal cleanup before longjmp'ing so that handlers can
- * execute in a reasonably sane state.
- *
- * 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;
- QueryCancelHoldoffCount = 0;
- 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();
- }
- /*
- * If we are doing FATAL or PANIC, abort any old-style COPY OUT in
- * progress, so that we can report the message before dying. (Without
- * this, pq_putmessage will refuse to send the message at all, which is
- * what we want for NOTICE messages, but not for fatal exits.) This hack
- * is necessary because of poor design of old-style copy protocol.
- */
- if (elevel >= FATAL && whereToSendOutput == DestRemote)
- pq_endcopyout(true);
- /* Emit the message to the right places */
- EmitErrorReport();
- /* Now free up subsidiary data attached to stack entry, and release it */
- if (edata->message)
- pfree(edata->message);
- if (edata->detail)
- pfree(edata->detail);
- if (edata->detail_log)
- pfree(edata->detail_log);
- if (edata->hint)
- pfree(edata->hint);
- if (edata->context)
- pfree(edata->context);
- if (edata->backtrace)
- pfree(edata->backtrace);
- 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);
- if (edata->internalquery)
- pfree(edata->internalquery);
- errordata_stack_depth--;
- /* Exit error-handling context */
- MemoryContextSwitchTo(oldcontext);
- recursion_depth--;
- /*
- * Perform error recovery action as specified by elevel.
- */
- if (elevel == FATAL)
- {
- /*
- * For a FATAL error, we let proc_exit clean up and exit.
- *
- * If we just reported a startup failure, the client will disconnect
- * on receiving it, so don't send any more to the client.
- */
- if (PG_exception_stack == NULL && whereToSendOutput == DestRemote)
- whereToSendOutput = DestNone;
- /*
- * 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
- * in an on_proc_exit or on_shmem_exit callback instead.
- */
- fflush(stdout);
- fflush(stderr);
- /*
- * 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.
- */
- proc_exit(1);
- }
- if (elevel >= PANIC)
- {
- /*
- * Serious crash time. Postmaster will observe SIGABRT process exit
- * status and kill the other backends too.
- *
- * XXX: what if we are *in* the postmaster? abort() won't kill our
- * children...
- */
- fflush(stdout);
- fflush(stderr);
- abort();
- }
- /*
- * 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.
- */
- CHECK_FOR_INTERRUPTS();
- }
- /*
- * errcode --- add SQLSTATE error code to the current error
- *
- * The code is expected to be represented as per MAKE_SQLSTATE().
- */
- int
- errcode(int sqlerrcode)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- /* we don't bother incrementing recursion_depth */
- CHECK_STACK_DEPTH();
- edata->sqlerrcode = sqlerrcode;
- return 0; /* return value does not matter */
- }
- /*
- * 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.
- */
- int
- 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;
- /* File not found */
- case ENOENT: /* No such file or directory */
- edata->sqlerrcode = ERRCODE_UNDEFINED_FILE;
- break;
- /* Duplicate file */
- case EEXIST: /* File exists */
- edata->sqlerrcode = ERRCODE_DUPLICATE_FILE;
- break;
- /* Wrong object type or state */
- case ENOTDIR: /* Not a directory */
- case EISDIR: /* Is a directory */
- #if defined(ENOTEMPTY) && (ENOTEMPTY != EEXIST) /* same code on AIX */
- case ENOTEMPTY: /* Directory not empty */
- #endif
- edata->sqlerrcode = ERRCODE_WRONG_OBJECT_TYPE;
- break;
- /* Insufficient resources */
- case ENOSPC: /* No space left on device */
- edata->sqlerrcode = ERRCODE_DISK_FULL;
- break;
- 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;
- }
- return 0; /* return value does not matter */
- }
- /*
- * 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.
- */
- int
- 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 */
- case EPIPE:
- #ifdef ECONNRESET
- case ECONNRESET:
- #endif
- edata->sqlerrcode = ERRCODE_CONNECTION_FAILURE;
- break;
- /* All else is classified as internal errors */
- default:
- edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
- break;
- }
- return 0; /* return value does not matter */
- }
- /*
- * 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
- * appended to it if appendval is true. The message is subject to translation
- * if translateit is true.
- *
- * 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.
- */
- #define EVALUATE_MESSAGE(domain, targetfield, appendval, translateit) \
- { \
- StringInfoData buf; \
- /* Internationalize the error format string */ \
- if ((translateit) && !in_error_recursion_trouble()) \
- fmt = dgettext((domain), fmt); \
- initStringInfo(&buf); \
- if ((appendval) && edata->targetfield) { \
- appendStringInfoString(&buf, edata->targetfield); \
- appendStringInfoChar(&buf, '\n'); \
- } \
- /* Generate actual output --- have to use appendStringInfoVA */ \
- for (;;) \
- { \
- va_list args; \
- int needed; \
- errno = edata->saved_errno; \
- va_start(args, fmt); \
- needed = appendStringInfoVA(&buf, fmt, args); \
- va_end(args); \
- if (needed == 0) \
- break; \
- enlargeStringInfo(&buf, needed); \
- } \
- /* Save the completed message into the stack item */ \
- if (edata->targetfield) \
- pfree(edata->targetfield); \
- edata->targetfield = pstrdup(buf.data); \
- pfree(buf.data); \
- }
- /*
- * 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.
- */
- #define EVALUATE_MESSAGE_PLURAL(domain, targetfield, appendval) \
- { \
- const char *fmt; \
- StringInfoData buf; \
- /* Internationalize the error format string */ \
- if (!in_error_recursion_trouble()) \
- fmt = dngettext((domain), fmt_singular, fmt_plural, n); \
- else \
- fmt = (n == 1 ? fmt_singular : fmt_plural); \
- initStringInfo(&buf); \
- if ((appendval) && edata->targetfield) { \
- appendStringInfoString(&buf, edata->targetfield); \
- appendStringInfoChar(&buf, '\n'); \
- } \
- /* Generate actual output --- have to use appendStringInfoVA */ \
- for (;;) \
- { \
- va_list args; \
- int needed; \
- errno = edata->saved_errno; \
- va_start(args, n); \
- needed = appendStringInfoVA(&buf, fmt, args); \
- va_end(args); \
- if (needed == 0) \
- break; \
- enlargeStringInfo(&buf, needed); \
- } \
- /* Save the completed message into the stack item */ \
- if (edata->targetfield) \
- pfree(edata->targetfield); \
- edata->targetfield = pstrdup(buf.data); \
- pfree(buf.data); \
- }
- /*
- * 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.
- */
- int
- errmsg(const char *fmt,...)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- MemoryContext oldcontext;
- recursion_depth++;
- CHECK_STACK_DEPTH();
- oldcontext = MemoryContextSwitchTo(edata->assoc_context);
- edata->message_id = fmt;
- EVALUATE_MESSAGE(edata->domain, message, false, true);
- MemoryContextSwitchTo(oldcontext);
- recursion_depth--;
- return 0; /* return value does not matter */
- }
- /*
- * Add a backtrace to the containing ereport() call. This is intended to be
- * added temporarily during debugging.
- */
- int
- 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--;
- return 0;
- }
- /*
- * 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;
- }
- /*
- * errmsg_internal --- add a primary error message text to the current error
- *
- * This is exactly like errmsg() except that strings passed to errmsg_internal
- * 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.
- */
- int
- errmsg_internal(const char *fmt,...)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- MemoryContext oldcontext;
- recursion_depth++;
- CHECK_STACK_DEPTH();
- oldcontext = MemoryContextSwitchTo(edata->assoc_context);
- edata->message_id = fmt;
- EVALUATE_MESSAGE(edata->domain, message, false, false);
- MemoryContextSwitchTo(oldcontext);
- recursion_depth--;
- return 0; /* return value does not matter */
- }
- /*
- * errmsg_plural --- add a primary error message text to the current error,
- * with support for pluralization of the message text
- */
- int
- 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();
- oldcontext = MemoryContextSwitchTo(edata->assoc_context);
- edata->message_id = fmt_singular;
- EVALUATE_MESSAGE_PLURAL(edata->domain, message, false);
- MemoryContextSwitchTo(oldcontext);
- recursion_depth--;
- return 0; /* return value does not matter */
- }
- /*
- * errdetail --- add a detail error message text to the current error
- */
- int
- errdetail(const char *fmt,...)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- MemoryContext oldcontext;
- recursion_depth++;
- CHECK_STACK_DEPTH();
- oldcontext = MemoryContextSwitchTo(edata->assoc_context);
- EVALUATE_MESSAGE(edata->domain, detail, false, true);
- MemoryContextSwitchTo(oldcontext);
- recursion_depth--;
- return 0; /* return value does not matter */
- }
- /*
- * 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).
- */
- int
- errdetail_internal(const char *fmt,...)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- MemoryContext oldcontext;
- recursion_depth++;
- CHECK_STACK_DEPTH();
- oldcontext = MemoryContextSwitchTo(edata->assoc_context);
- EVALUATE_MESSAGE(edata->domain, detail, false, false);
- MemoryContextSwitchTo(oldcontext);
- recursion_depth--;
- return 0; /* return value does not matter */
- }
- /*
- * errdetail_log --- add a detail_log error message text to the current error
- */
- int
- errdetail_log(const char *fmt,...)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- MemoryContext oldcontext;
- recursion_depth++;
- CHECK_STACK_DEPTH();
- oldcontext = MemoryContextSwitchTo(edata->assoc_context);
- EVALUATE_MESSAGE(edata->domain, detail_log, false, true);
- MemoryContextSwitchTo(oldcontext);
- recursion_depth--;
- return 0; /* return value does not matter */
- }
- /*
- * errdetail_log_plural --- add a detail_log error message text to the current error
- * with support for pluralization of the message text
- */
- int
- 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--;
- return 0; /* return value does not matter */
- }
- /*
- * errdetail_plural --- add a detail error message text to the current error,
- * with support for pluralization of the message text
- */
- int
- 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();
- oldcontext = MemoryContextSwitchTo(edata->assoc_context);
- EVALUATE_MESSAGE_PLURAL(edata->domain, detail, false);
- MemoryContextSwitchTo(oldcontext);
- recursion_depth--;
- return 0; /* return value does not matter */
- }
- /*
- * errhint --- add a hint error message text to the current error
- */
- int
- errhint(const char *fmt,...)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- MemoryContext oldcontext;
- recursion_depth++;
- CHECK_STACK_DEPTH();
- oldcontext = MemoryContextSwitchTo(edata->assoc_context);
- EVALUATE_MESSAGE(edata->domain, hint, false, true);
- MemoryContextSwitchTo(oldcontext);
- recursion_depth--;
- return 0; /* return value does not matter */
- }
- /*
- * errcontext_msg --- add a context error message text to the current error
- *
- * Unlike other cases, multiple calls are allowed to build up a stack of
- * context information. We assume earlier calls represent more-closely-nested
- * states.
- */
- int
- errcontext_msg(const char *fmt,...)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- MemoryContext oldcontext;
- recursion_depth++;
- CHECK_STACK_DEPTH();
- oldcontext = MemoryContextSwitchTo(edata->assoc_context);
- EVALUATE_MESSAGE(edata->context_domain, context, true, true);
- MemoryContextSwitchTo(oldcontext);
- recursion_depth--;
- return 0; /* return value does not matter */
- }
- /*
- * 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.
- */
- int
- set_errcontext_domain(const char *domain)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- /* we don't bother incrementing recursion_depth */
- CHECK_STACK_DEPTH();
- /* the default text domain is the backend's */
- edata->context_domain = domain ? domain : PG_TEXTDOMAIN("postgres");
- return 0; /* return value does not matter */
- }
- /*
- * errhidestmt --- optionally suppress STATEMENT: field of log entry
- *
- * This should be called if the message text already includes the statement.
- */
- int
- 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;
- return 0; /* return value does not matter */
- }
- /*
- * errhidecontext --- optionally suppress CONTEXT: field of log entry
- *
- * This should only be used for verbose debugging messages where the repeated
- * inclusion of context would bloat the log volume too much.
- */
- int
- 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;
- return 0; /* return value does not matter */
- }
- /*
- * errfunction --- add reporting function name to the current error
- *
- * This is used when backwards compatibility demands that the function
- * name appear in messages sent to old-protocol clients. Note that the
- * passed string is expected to be a non-freeable constant string.
- */
- int
- errfunction(const char *funcname)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- /* we don't bother incrementing recursion_depth */
- CHECK_STACK_DEPTH();
- edata->funcname = funcname;
- edata->show_funcname = true;
- return 0; /* return value does not matter */
- }
- /*
- * errposition --- add cursor position to the current error
- */
- int
- errposition(int cursorpos)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- /* we don't bother incrementing recursion_depth */
- CHECK_STACK_DEPTH();
- edata->cursorpos = cursorpos;
- return 0; /* return value does not matter */
- }
- /*
- * internalerrposition --- add internal cursor position to the current error
- */
- int
- internalerrposition(int cursorpos)
- {
- ErrorData *edata = &errordata[errordata_stack_depth];
- /* we don't bother incrementing recursion_depth */
- CHECK_STACK_DEPTH();
- edata->internalpos = cursorpos;
- return 0; /* return value does not matter */
- }
- /*
- * 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.
- */
- int
- 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)
- edata->internalquery = MemoryContextStrdup(edata->assoc_context, query);
- return 0; /* return value does not matter */
- }
- /*
- * 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).
- */
- int
- 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:
- set_errdata_field(edata->assoc_context, &edata->schema_name, str);
- break;
- case PG_DIAG_TABLE_NAME:
- set_errdata_field(edata->assoc_context, &edata->table_name, str);
- break;
- case PG_DIAG_COLUMN_NAME:
- set_errdata_field(edata->assoc_context, &edata->column_name, str);
- break;
- case PG_DIAG_DATATYPE_NAME:
- set_errdata_field(edata->assoc_context, &edata->datatype_name, str);
- break;
- case PG_DIAG_CONSTRAINT_NAME:
- set_errdata_field(edata->assoc_context, &edata->constraint_name, str);
- break;
- default:
- elog(ERROR, "unsupported ErrorData field id: %d", field);
- break;
- }
- return 0; /* return value does not matter */
- }
- /*
- * set_errdata_field --- set an ErrorData string field
- */
- static void
- set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str)
- {
- Assert(*ptr == NULL);
- *ptr = MemoryContextStrdup(cxt, str);
- }
- /*
- * 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;
- }
- /*
- * 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;
- }
- /*
- * 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);
- edata->message_id = fmt;
- EVALUATE_MESSAGE(edata->domain, message, false, true);
- MemoryContextSwitchTo(oldcontext);
- return edata->message;
- }
- /*
- * 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();
- oldcontext = MemoryContextSwitchTo(edata->assoc_context);
- /*
- * 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.
- *
- * 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.
- */
- if (edata->output_to_server && emit_log_hook)
- (*emit_log_hook) (edata);
- /* 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);
- if (newedata->detail_log)
- newedata->detail_log = pstrdup(newedata->detail_log);
- if (newedata->hint)
- newedata->hint = pstrdup(newedata->hint);
- if (newedata->context)
- newedata->context = pstrdup(newedata->context);
- if (newedata->backtrace)
- newedata->backtrace = pstrdup(newedata->backtrace);
- 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);
- if (newedata->internalquery)
- newedata->internalquery = pstrdup(newedata->internalquery);
- /* Use the calling context for string allocation */
- newedata->assoc_context = CurrentMemoryContext;
- 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)
- {
- if (edata->message)
- pfree(edata->message);
- if (edata->detail)
- pfree(edata->detail);
- if (edata->detail_log)
- pfree(edata->detail_log);
- if (edata->hint)
- pfree(edata->hint);
- if (edata->context)
- pfree(edata->context);
- if (edata->backtrace)
- pfree(edata->backtrace);
- 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);
- if (edata->internalquery)
- pfree(edata->internalquery);
- pfree(edata);
- }
- /*
- * 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 */
- MemoryContextResetAndDeleteChildren(ErrorContext);
- }
- /*
- * ThrowErrorData --- report an error described by an ErrorData structure
- *
- * 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
- * background worker processes and then propagated (with or without
- * modification) to the backend responsible for them.
- */
- void
- ThrowErrorData(ErrorData *edata)
- {
- ErrorData *newedata;
- MemoryContext oldcontext;
- if (!errstart(edata->elevel, edata->domain))
- return; /* error is not to be reported at all */
- newedata = &errordata[errordata_stack_depth];
- recursion_depth++;
- oldcontext = MemoryContextSwitchTo(newedata->assoc_context);
- /* Copy the supplied fields to the error stack entry. */
- if (edata->sqlerrcode != 0)
- 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);
- if (edata->backtrace)
- newedata->backtrace = pstrdup(edata->backtrace);
- /* assume message_id is not available */
- 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);
- newedata->cursorpos = edata->cursorpos;
- newedata->internalpos = edata->internalpos;
- if (edata->internalquery)
- newedata->internalquery = pstrdup(edata->internalquery);
- MemoryContextSwitchTo(oldcontext);
- recursion_depth--;
- /* Process the error. */
- errfinish(edata->filename, edata->lineno, edata->funcname);
- }
- /*
- * 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);
- if (++errordata_stack_depth >= ERRORDATA_STACK_SIZE)
- {
- /*
- * Wups, stack not big enough. We treat this as a PANIC condition
- * because it suggests an infinite loop of errors during error
- * recovery.
- */
- errordata_stack_depth = -1; /* make room on stack */
- ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded")));
- }
- newedata = &errordata[errordata_stack_depth];
- 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);
- if (newedata->detail_log)
- newedata->detail_log = pstrdup(newedata->detail_log);
- if (newedata->hint)
- newedata->hint = pstrdup(newedata->hint);
- if (newedata->context)
- newedata->context = pstrdup(newedata->context);
- if (newedata->backtrace)
- newedata->backtrace = pstrdup(newedata->backtrace);
- 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);
- if (newedata->internalquery)
- newedata->internalquery = pstrdup(newedata->internalquery);
- /* Reset the assoc_context…
Large files files are truncated, but you can click here to view the full file