PostgreSQL Source Code  git master
elog.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * elog.c
4  * error logging and reporting
5  *
6  * Because of the extremely high rate at which log messages can be generated,
7  * we need to be mindful of the performance cost of obtaining any information
8  * that may be logged. Also, it's important to keep in mind that this code may
9  * get called from within an aborted transaction, in which case operations
10  * such as syscache lookups are unsafe.
11  *
12  * Some notes about recursion and errors during error processing:
13  *
14  * We need to be robust about recursive-error scenarios --- for example,
15  * if we run out of memory, it's important to be able to report that fact.
16  * There are a number of considerations that go into this.
17  *
18  * First, distinguish between re-entrant use and actual recursion. It
19  * is possible for an error or warning message to be emitted while the
20  * parameters for an error message are being computed. In this case
21  * errstart has been called for the outer message, and some field values
22  * may have already been saved, but we are not actually recursing. We handle
23  * this by providing a (small) stack of ErrorData records. The inner message
24  * can be computed and sent without disturbing the state of the outer message.
25  * (If the inner message is actually an error, this isn't very interesting
26  * because control won't come back to the outer message generator ... but
27  * if the inner message is only debug or log data, this is critical.)
28  *
29  * Second, actual recursion will occur if an error is reported by one of
30  * the elog.c routines or something they call. By far the most probable
31  * scenario of this sort is "out of memory"; and it's also the nastiest
32  * to handle because we'd likely also run out of memory while trying to
33  * report this error! Our escape hatch for this case is to reset the
34  * ErrorContext to empty before trying to process the inner error. Since
35  * ErrorContext is guaranteed to have at least 8K of space in it (see mcxt.c),
36  * we should be able to process an "out of memory" message successfully.
37  * Since we lose the prior error state due to the reset, we won't be able
38  * to return to processing the original error, but we wouldn't have anyway.
39  * (NOTE: the escape hatch is not used for recursive situations where the
40  * inner message is of less than ERROR severity; in that case we just
41  * try to process it and return normally. Usually this will work, but if
42  * it ends up in infinite recursion, we will PANIC due to error stack
43  * overflow.)
44  *
45  *
46  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
47  * Portions Copyright (c) 1994, Regents of the University of California
48  *
49  *
50  * IDENTIFICATION
51  * src/backend/utils/error/elog.c
52  *
53  *-------------------------------------------------------------------------
54  */
55 #include "postgres.h"
56 
57 #include <fcntl.h>
58 #include <time.h>
59 #include <unistd.h>
60 #include <signal.h>
61 #include <ctype.h>
62 #ifdef HAVE_SYSLOG
63 #include <syslog.h>
64 #endif
65 #ifdef HAVE_EXECINFO_H
66 #include <execinfo.h>
67 #endif
68 
69 #include "access/transam.h"
70 #include "access/xact.h"
71 #include "libpq/libpq.h"
72 #include "libpq/pqformat.h"
73 #include "mb/pg_wchar.h"
74 #include "miscadmin.h"
75 #include "postmaster/postmaster.h"
76 #include "postmaster/syslogger.h"
77 #include "storage/ipc.h"
78 #include "storage/proc.h"
79 #include "tcop/tcopprot.h"
80 #include "utils/guc.h"
81 #include "utils/memutils.h"
82 #include "utils/ps_status.h"
83 
84 
85 /* In this module, access gettext() via err_gettext() */
86 #undef _
87 #define _(x) err_gettext(x)
88 
89 
90 /* Global variables */
92 
93 sigjmp_buf *PG_exception_stack = NULL;
94 
95 extern bool redirection_done;
96 
97 /*
98  * Hook for intercepting messages before they are sent to the server log.
99  * Note that the hook will not get called for messages that are suppressed
100  * by log_min_messages. Also note that logging hooks implemented in preload
101  * libraries will miss any log messages that are generated before the
102  * library is loaded.
103  */
105 
106 /* GUC parameters */
108 char *Log_line_prefix = NULL; /* format for extra log line info */
113 
114 #ifdef HAVE_SYSLOG
115 
116 /*
117  * Max string length to send to syslog(). Note that this doesn't count the
118  * sequence-number prefix we add, and of course it doesn't count the prefix
119  * added by syslog itself. Solaris and sysklogd truncate the final message
120  * at 1024 bytes, so this value leaves 124 bytes for those prefixes. (Most
121  * other syslog implementations seem to have limits of 2KB or so.)
122  */
123 #ifndef PG_SYSLOG_LIMIT
124 #define PG_SYSLOG_LIMIT 900
125 #endif
126 
127 static bool openlog_done = false;
128 static char *syslog_ident = NULL;
129 static int syslog_facility = LOG_LOCAL0;
130 
131 static void write_syslog(int level, const char *line);
132 #endif
133 
134 #ifdef WIN32
135 extern char *event_source;
136 
137 static void write_eventlog(int level, const char *line, int len);
138 #endif
139 
140 /* We provide a small stack of ErrorData records for re-entrant cases */
141 #define ERRORDATA_STACK_SIZE 5
142 
144 
145 static int errordata_stack_depth = -1; /* index of topmost active frame */
146 
147 static int recursion_depth = 0; /* to detect actual recursion */
148 
149 /*
150  * Saved timeval and buffers for formatted timestamps that might be used by
151  * both log_line_prefix and csv logs.
152  */
153 static struct timeval saved_timeval;
154 static bool saved_timeval_set = false;
155 
156 #define FORMATTED_TS_LEN 128
159 
160 
161 /* Macro for checking errordata_stack_depth is reasonable */
162 #define CHECK_STACK_DEPTH() \
163  do { \
164  if (errordata_stack_depth < 0) \
165  { \
166  errordata_stack_depth = -1; \
167  ereport(ERROR, (errmsg_internal("errstart was not called"))); \
168  } \
169  } while (0)
170 
171 
172 static const char *err_gettext(const char *str) pg_attribute_format_arg(1);
173 static pg_noinline void set_backtrace(ErrorData *edata, int num_skip);
174 static void set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str);
175 static void write_console(const char *line, int len);
176 static void setup_formatted_log_time(void);
177 static void setup_formatted_start_time(void);
178 static const char *process_log_prefix_padding(const char *p, int *padding);
179 static void log_line_prefix(StringInfo buf, ErrorData *edata);
180 static void write_csvlog(ErrorData *edata);
181 static void send_message_to_server_log(ErrorData *edata);
182 static void write_pipe_chunks(char *data, int len, int dest);
183 static void send_message_to_frontend(ErrorData *edata);
184 static const char *error_severity(int elevel);
185 static void append_with_tabs(StringInfo buf, const char *str);
186 static bool is_log_level_output(int elevel, int log_min_level);
187 
188 
189 /*
190  * in_error_recursion_trouble --- are we at risk of infinite error recursion?
191  *
192  * This function exists to provide common control of various fallback steps
193  * that we take if we think we are facing infinite error recursion. See the
194  * callers for details.
195  */
196 bool
198 {
199  /* Pull the plug if recurse more than once */
200  return (recursion_depth > 2);
201 }
202 
203 /*
204  * One of those fallback steps is to stop trying to localize the error
205  * message, since there's a significant probability that that's exactly
206  * what's causing the recursion.
207  */
208 static inline const char *
209 err_gettext(const char *str)
210 {
211 #ifdef ENABLE_NLS
213  return str;
214  else
215  return gettext(str);
216 #else
217  return str;
218 #endif
219 }
220 
221 
222 /*
223  * errstart --- begin an error-reporting cycle
224  *
225  * Create a stack entry and store the given parameters in it. Subsequently,
226  * errmsg() and perhaps other routines will be called to further populate
227  * the stack entry. Finally, errfinish() will be called to actually process
228  * the error report.
229  *
230  * Returns true in normal case. Returns false to short-circuit the error
231  * report (if it's a warning or lower and not to be reported anywhere).
232  */
233 bool
234 errstart(int elevel, const char *filename, int lineno,
235  const char *funcname, const char *domain)
236 {
237  ErrorData *edata;
238  bool output_to_server;
239  bool output_to_client = false;
240  int i;
241 
242  /*
243  * Check some cases in which we want to promote an error into a more
244  * severe error. None of this logic applies for non-error messages.
245  */
246  if (elevel >= ERROR)
247  {
248  /*
249  * If we are inside a critical section, all errors become PANIC
250  * errors. See miscadmin.h.
251  */
252  if (CritSectionCount > 0)
253  elevel = PANIC;
254 
255  /*
256  * Check reasons for treating ERROR as FATAL:
257  *
258  * 1. we have no handler to pass the error to (implies we are in the
259  * postmaster or in backend startup).
260  *
261  * 2. ExitOnAnyError mode switch is set (initdb uses this).
262  *
263  * 3. the error occurred after proc_exit has begun to run. (It's
264  * proc_exit's responsibility to see that this doesn't turn into
265  * infinite recursion!)
266  */
267  if (elevel == ERROR)
268  {
269  if (PG_exception_stack == NULL ||
270  ExitOnAnyError ||
272  elevel = FATAL;
273  }
274 
275  /*
276  * If the error level is ERROR or more, errfinish is not going to
277  * return to caller; therefore, if there is any stacked error already
278  * in progress it will be lost. This is more or less okay, except we
279  * do not want to have a FATAL or PANIC error downgraded because the
280  * reporting process was interrupted by a lower-grade error. So check
281  * the stack and make sure we panic if panic is warranted.
282  */
283  for (i = 0; i <= errordata_stack_depth; i++)
284  elevel = Max(elevel, errordata[i].elevel);
285  }
286 
287  /*
288  * Now decide whether we need to process this report at all; if it's
289  * warning or less and not enabled for logging, just return false without
290  * starting up any error logging machinery.
291  */
292 
293  /* Determine whether message is enabled for server log output */
294  output_to_server = is_log_level_output(elevel, log_min_messages);
295 
296  /* Determine whether message is enabled for client output */
297  if (whereToSendOutput == DestRemote && elevel != LOG_SERVER_ONLY)
298  {
299  /*
300  * client_min_messages is honored only after we complete the
301  * authentication handshake. This is required both for security
302  * reasons and because many clients can't handle NOTICE messages
303  * during authentication.
304  */
306  output_to_client = (elevel >= ERROR);
307  else
308  output_to_client = (elevel >= client_min_messages ||
309  elevel == INFO);
310  }
311 
312  /* Skip processing effort if non-error message will not be output */
313  if (elevel < ERROR && !output_to_server && !output_to_client)
314  return false;
315 
316  /*
317  * We need to do some actual work. Make sure that memory context
318  * initialization has finished, else we can't do anything useful.
319  */
320  if (ErrorContext == NULL)
321  {
322  /* Oops, hard crash time; very little we can do safely here */
323  write_stderr("error occurred at %s:%d before error message processing is available\n",
324  filename ? filename : "(unknown file)", lineno);
325  exit(2);
326  }
327 
328  /*
329  * Okay, crank up a stack entry to store the info in.
330  */
331 
332  if (recursion_depth++ > 0 && elevel >= ERROR)
333  {
334  /*
335  * Oops, error during error processing. Clear ErrorContext as
336  * discussed at top of file. We will not return to the original
337  * error's reporter or handler, so we don't need it.
338  */
340 
341  /*
342  * Infinite error recursion might be due to something broken in a
343  * context traceback routine. Abandon them too. We also abandon
344  * attempting to print the error statement (which, if long, could
345  * itself be the source of the recursive failure).
346  */
348  {
349  error_context_stack = NULL;
350  debug_query_string = NULL;
351  }
352  }
354  {
355  /*
356  * Wups, stack not big enough. We treat this as a PANIC condition
357  * because it suggests an infinite loop of errors during error
358  * recovery.
359  */
360  errordata_stack_depth = -1; /* make room on stack */
361  ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded")));
362  }
363 
364  /* Initialize data for this error frame */
365  edata = &errordata[errordata_stack_depth];
366  MemSet(edata, 0, sizeof(ErrorData));
367  edata->elevel = elevel;
368  edata->output_to_server = output_to_server;
369  edata->output_to_client = output_to_client;
370  if (filename)
371  {
372  const char *slash;
373 
374  /* keep only base name, useful especially for vpath builds */
375  slash = strrchr(filename, '/');
376  if (slash)
377  filename = slash + 1;
378  }
379  edata->filename = filename;
380  edata->lineno = lineno;
381  edata->funcname = funcname;
382  /* the default text domain is the backend's */
383  edata->domain = domain ? domain : PG_TEXTDOMAIN("postgres");
384  /* initialize context_domain the same way (see set_errcontext_domain()) */
385  edata->context_domain = edata->domain;
386  /* Select default errcode based on elevel */
387  if (elevel >= ERROR)
388  edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
389  else if (elevel == WARNING)
390  edata->sqlerrcode = ERRCODE_WARNING;
391  else
392  edata->sqlerrcode = ERRCODE_SUCCESSFUL_COMPLETION;
393  /* errno is saved here so that error parameter eval can't change it */
394  edata->saved_errno = errno;
395 
396  /*
397  * Any allocations for this error state level should go into ErrorContext
398  */
399  edata->assoc_context = ErrorContext;
400 
401  recursion_depth--;
402  return true;
403 }
404 
405 /*
406  * Checks whether the given funcname matches backtrace_functions; see
407  * check_backtrace_functions.
408  */
409 static bool
410 matches_backtrace_functions(const char *funcname)
411 {
412  char *p;
413 
414  if (!backtrace_symbol_list || funcname == NULL || funcname[0] == '\0')
415  return false;
416 
418  for (;;)
419  {
420  if (*p == '\0') /* end of backtrace_symbol_list */
421  break;
422 
423  if (strcmp(funcname, p) == 0)
424  return true;
425  p += strlen(p) + 1;
426  }
427 
428  return false;
429 }
430 
431 /*
432  * errfinish --- end an error-reporting cycle
433  *
434  * Produce the appropriate error report(s) and pop the error stack.
435  *
436  * If elevel is ERROR or worse, control does not return to the caller.
437  * See elog.h for the error level definitions.
438  */
439 void
440 errfinish(int dummy,...)
441 {
442  ErrorData *edata = &errordata[errordata_stack_depth];
443  int elevel;
444  MemoryContext oldcontext;
445  ErrorContextCallback *econtext;
446 
447  recursion_depth++;
449  elevel = edata->elevel;
450 
451  /*
452  * Do processing in ErrorContext, which we hope has enough reserved space
453  * to report an error.
454  */
455  oldcontext = MemoryContextSwitchTo(ErrorContext);
456 
457  if (!edata->backtrace &&
458  edata->funcname &&
461  set_backtrace(edata, 2);
462 
463  /*
464  * Call any context callback functions. Errors occurring in callback
465  * functions will be treated as recursive errors --- this ensures we will
466  * avoid infinite recursion (see errstart).
467  */
468  for (econtext = error_context_stack;
469  econtext != NULL;
470  econtext = econtext->previous)
471  econtext->callback(econtext->arg);
472 
473  /*
474  * If ERROR (not more nor less) we pass it off to the current handler.
475  * Printing it and popping the stack is the responsibility of the handler.
476  */
477  if (elevel == ERROR)
478  {
479  /*
480  * We do some minimal cleanup before longjmp'ing so that handlers can
481  * execute in a reasonably sane state.
482  *
483  * Reset InterruptHoldoffCount in case we ereport'd from inside an
484  * interrupt holdoff section. (We assume here that no handler will
485  * itself be inside a holdoff section. If necessary, such a handler
486  * could save and restore InterruptHoldoffCount for itself, but this
487  * should make life easier for most.)
488  */
491 
492  CritSectionCount = 0; /* should be unnecessary, but... */
493 
494  /*
495  * Note that we leave CurrentMemoryContext set to ErrorContext. The
496  * handler should reset it to something else soon.
497  */
498 
499  recursion_depth--;
500  PG_RE_THROW();
501  }
502 
503  /*
504  * If we are doing FATAL or PANIC, abort any old-style COPY OUT in
505  * progress, so that we can report the message before dying. (Without
506  * this, pq_putmessage will refuse to send the message at all, which is
507  * what we want for NOTICE messages, but not for fatal exits.) This hack
508  * is necessary because of poor design of old-style copy protocol.
509  */
510  if (elevel >= FATAL && whereToSendOutput == DestRemote)
511  pq_endcopyout(true);
512 
513  /* Emit the message to the right places */
514  EmitErrorReport();
515 
516  /* Now free up subsidiary data attached to stack entry, and release it */
517  if (edata->message)
518  pfree(edata->message);
519  if (edata->detail)
520  pfree(edata->detail);
521  if (edata->detail_log)
522  pfree(edata->detail_log);
523  if (edata->hint)
524  pfree(edata->hint);
525  if (edata->context)
526  pfree(edata->context);
527  if (edata->backtrace)
528  pfree(edata->backtrace);
529  if (edata->schema_name)
530  pfree(edata->schema_name);
531  if (edata->table_name)
532  pfree(edata->table_name);
533  if (edata->column_name)
534  pfree(edata->column_name);
535  if (edata->datatype_name)
536  pfree(edata->datatype_name);
537  if (edata->constraint_name)
538  pfree(edata->constraint_name);
539  if (edata->internalquery)
540  pfree(edata->internalquery);
541 
543 
544  /* Exit error-handling context */
545  MemoryContextSwitchTo(oldcontext);
546  recursion_depth--;
547 
548  /*
549  * Perform error recovery action as specified by elevel.
550  */
551  if (elevel == FATAL)
552  {
553  /*
554  * For a FATAL error, we let proc_exit clean up and exit.
555  *
556  * If we just reported a startup failure, the client will disconnect
557  * on receiving it, so don't send any more to the client.
558  */
561 
562  /*
563  * fflush here is just to improve the odds that we get to see the
564  * error message, in case things are so hosed that proc_exit crashes.
565  * Any other code you might be tempted to add here should probably be
566  * in an on_proc_exit or on_shmem_exit callback instead.
567  */
568  fflush(stdout);
569  fflush(stderr);
570 
571  /*
572  * Do normal process-exit cleanup, then return exit code 1 to indicate
573  * FATAL termination. The postmaster may or may not consider this
574  * worthy of panic, depending on which subprocess returns it.
575  */
576  proc_exit(1);
577  }
578 
579  if (elevel >= PANIC)
580  {
581  /*
582  * Serious crash time. Postmaster will observe SIGABRT process exit
583  * status and kill the other backends too.
584  *
585  * XXX: what if we are *in* the postmaster? abort() won't kill our
586  * children...
587  */
588  fflush(stdout);
589  fflush(stderr);
590  abort();
591  }
592 
593  /*
594  * Check for cancel/die interrupt first --- this is so that the user can
595  * stop a query emitting tons of notice or warning messages, even if it's
596  * in a loop that otherwise fails to check for interrupts.
597  */
599 }
600 
601 
602 /*
603  * errcode --- add SQLSTATE error code to the current error
604  *
605  * The code is expected to be represented as per MAKE_SQLSTATE().
606  */
607 int
608 errcode(int sqlerrcode)
609 {
610  ErrorData *edata = &errordata[errordata_stack_depth];
611 
612  /* we don't bother incrementing recursion_depth */
614 
615  edata->sqlerrcode = sqlerrcode;
616 
617  return 0; /* return value does not matter */
618 }
619 
620 
621 /*
622  * errcode_for_file_access --- add SQLSTATE error code to the current error
623  *
624  * The SQLSTATE code is chosen based on the saved errno value. We assume
625  * that the failing operation was some type of disk file access.
626  *
627  * NOTE: the primary error message string should generally include %m
628  * when this is used.
629  */
630 int
632 {
633  ErrorData *edata = &errordata[errordata_stack_depth];
634 
635  /* we don't bother incrementing recursion_depth */
637 
638  switch (edata->saved_errno)
639  {
640  /* Permission-denied failures */
641  case EPERM: /* Not super-user */
642  case EACCES: /* Permission denied */
643 #ifdef EROFS
644  case EROFS: /* Read only file system */
645 #endif
646  edata->sqlerrcode = ERRCODE_INSUFFICIENT_PRIVILEGE;
647  break;
648 
649  /* File not found */
650  case ENOENT: /* No such file or directory */
651  edata->sqlerrcode = ERRCODE_UNDEFINED_FILE;
652  break;
653 
654  /* Duplicate file */
655  case EEXIST: /* File exists */
656  edata->sqlerrcode = ERRCODE_DUPLICATE_FILE;
657  break;
658 
659  /* Wrong object type or state */
660  case ENOTDIR: /* Not a directory */
661  case EISDIR: /* Is a directory */
662 #if defined(ENOTEMPTY) && (ENOTEMPTY != EEXIST) /* same code on AIX */
663  case ENOTEMPTY: /* Directory not empty */
664 #endif
665  edata->sqlerrcode = ERRCODE_WRONG_OBJECT_TYPE;
666  break;
667 
668  /* Insufficient resources */
669  case ENOSPC: /* No space left on device */
670  edata->sqlerrcode = ERRCODE_DISK_FULL;
671  break;
672 
673  case ENFILE: /* File table overflow */
674  case EMFILE: /* Too many open files */
675  edata->sqlerrcode = ERRCODE_INSUFFICIENT_RESOURCES;
676  break;
677 
678  /* Hardware failure */
679  case EIO: /* I/O error */
680  edata->sqlerrcode = ERRCODE_IO_ERROR;
681  break;
682 
683  /* All else is classified as internal errors */
684  default:
685  edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
686  break;
687  }
688 
689  return 0; /* return value does not matter */
690 }
691 
692 /*
693  * errcode_for_socket_access --- add SQLSTATE error code to the current error
694  *
695  * The SQLSTATE code is chosen based on the saved errno value. We assume
696  * that the failing operation was some type of socket access.
697  *
698  * NOTE: the primary error message string should generally include %m
699  * when this is used.
700  */
701 int
703 {
704  ErrorData *edata = &errordata[errordata_stack_depth];
705 
706  /* we don't bother incrementing recursion_depth */
708 
709  switch (edata->saved_errno)
710  {
711  /* Loss of connection */
712  case EPIPE:
713 #ifdef ECONNRESET
714  case ECONNRESET:
715 #endif
716  edata->sqlerrcode = ERRCODE_CONNECTION_FAILURE;
717  break;
718 
719  /* All else is classified as internal errors */
720  default:
721  edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
722  break;
723  }
724 
725  return 0; /* return value does not matter */
726 }
727 
728 
729 /*
730  * This macro handles expansion of a format string and associated parameters;
731  * it's common code for errmsg(), errdetail(), etc. Must be called inside
732  * a routine that is declared like "const char *fmt, ..." and has an edata
733  * pointer set up. The message is assigned to edata->targetfield, or
734  * appended to it if appendval is true. The message is subject to translation
735  * if translateit is true.
736  *
737  * Note: we pstrdup the buffer rather than just transferring its storage
738  * to the edata field because the buffer might be considerably larger than
739  * really necessary.
740  */
741 #define EVALUATE_MESSAGE(domain, targetfield, appendval, translateit) \
742  { \
743  StringInfoData buf; \
744  /* Internationalize the error format string */ \
745  if ((translateit) && !in_error_recursion_trouble()) \
746  fmt = dgettext((domain), fmt); \
747  initStringInfo(&buf); \
748  if ((appendval) && edata->targetfield) { \
749  appendStringInfoString(&buf, edata->targetfield); \
750  appendStringInfoChar(&buf, '\n'); \
751  } \
752  /* Generate actual output --- have to use appendStringInfoVA */ \
753  for (;;) \
754  { \
755  va_list args; \
756  int needed; \
757  errno = edata->saved_errno; \
758  va_start(args, fmt); \
759  needed = appendStringInfoVA(&buf, fmt, args); \
760  va_end(args); \
761  if (needed == 0) \
762  break; \
763  enlargeStringInfo(&buf, needed); \
764  } \
765  /* Save the completed message into the stack item */ \
766  if (edata->targetfield) \
767  pfree(edata->targetfield); \
768  edata->targetfield = pstrdup(buf.data); \
769  pfree(buf.data); \
770  }
771 
772 /*
773  * Same as above, except for pluralized error messages. The calling routine
774  * must be declared like "const char *fmt_singular, const char *fmt_plural,
775  * unsigned long n, ...". Translation is assumed always wanted.
776  */
777 #define EVALUATE_MESSAGE_PLURAL(domain, targetfield, appendval) \
778  { \
779  const char *fmt; \
780  StringInfoData buf; \
781  /* Internationalize the error format string */ \
782  if (!in_error_recursion_trouble()) \
783  fmt = dngettext((domain), fmt_singular, fmt_plural, n); \
784  else \
785  fmt = (n == 1 ? fmt_singular : fmt_plural); \
786  initStringInfo(&buf); \
787  if ((appendval) && edata->targetfield) { \
788  appendStringInfoString(&buf, edata->targetfield); \
789  appendStringInfoChar(&buf, '\n'); \
790  } \
791  /* Generate actual output --- have to use appendStringInfoVA */ \
792  for (;;) \
793  { \
794  va_list args; \
795  int needed; \
796  errno = edata->saved_errno; \
797  va_start(args, n); \
798  needed = appendStringInfoVA(&buf, fmt, args); \
799  va_end(args); \
800  if (needed == 0) \
801  break; \
802  enlargeStringInfo(&buf, needed); \
803  } \
804  /* Save the completed message into the stack item */ \
805  if (edata->targetfield) \
806  pfree(edata->targetfield); \
807  edata->targetfield = pstrdup(buf.data); \
808  pfree(buf.data); \
809  }
810 
811 
812 /*
813  * errmsg --- add a primary error message text to the current error
814  *
815  * In addition to the usual %-escapes recognized by printf, "%m" in
816  * fmt is replaced by the error message for the caller's value of errno.
817  *
818  * Note: no newline is needed at the end of the fmt string, since
819  * ereport will provide one for the output methods that need it.
820  */
821 int
822 errmsg(const char *fmt,...)
823 {
824  ErrorData *edata = &errordata[errordata_stack_depth];
825  MemoryContext oldcontext;
826 
827  recursion_depth++;
829  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
830 
831  edata->message_id = fmt;
832  EVALUATE_MESSAGE(edata->domain, message, false, true);
833 
834  MemoryContextSwitchTo(oldcontext);
835  recursion_depth--;
836  return 0; /* return value does not matter */
837 }
838 
839 /*
840  * Add a backtrace to the containing ereport() call. This is intended to be
841  * added temporarily during debugging.
842  */
843 int
845 {
846  ErrorData *edata = &errordata[errordata_stack_depth];
847  MemoryContext oldcontext;
848 
849  Assert(false);
850 
851  recursion_depth++;
853  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
854 
855  set_backtrace(edata, 1);
856 
857  MemoryContextSwitchTo(oldcontext);
858  recursion_depth--;
859 
860  return 0;
861 }
862 
863 /*
864  * Compute backtrace data and add it to the supplied ErrorData. num_skip
865  * specifies how many inner frames to skip. Use this to avoid showing the
866  * internal backtrace support functions in the backtrace. This requires that
867  * this and related functions are not inlined.
868  */
869 static void
870 set_backtrace(ErrorData *edata, int num_skip)
871 {
872  StringInfoData errtrace;
873 
874  initStringInfo(&errtrace);
875 
876 #ifdef HAVE_BACKTRACE_SYMBOLS
877  {
878  void *buf[100];
879  int nframes;
880  char **strfrms;
881 
882  nframes = backtrace(buf, lengthof(buf));
883  strfrms = backtrace_symbols(buf, nframes);
884  if (strfrms == NULL)
885  return;
886 
887  for (int i = num_skip; i < nframes; i++)
888  appendStringInfo(&errtrace, "\n%s", strfrms[i]);
889  free(strfrms);
890  }
891 #else
892  appendStringInfoString(&errtrace,
893  "backtrace generation is not supported by this installation");
894 #endif
895 
896  edata->backtrace = errtrace.data;
897 }
898 
899 /*
900  * errmsg_internal --- add a primary error message text to the current error
901  *
902  * This is exactly like errmsg() except that strings passed to errmsg_internal
903  * are not translated, and are customarily left out of the
904  * internationalization message dictionary. This should be used for "can't
905  * happen" cases that are probably not worth spending translation effort on.
906  * We also use this for certain cases where we *must* not try to translate
907  * the message because the translation would fail and result in infinite
908  * error recursion.
909  */
910 int
911 errmsg_internal(const char *fmt,...)
912 {
913  ErrorData *edata = &errordata[errordata_stack_depth];
914  MemoryContext oldcontext;
915 
916  recursion_depth++;
918  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
919 
920  edata->message_id = fmt;
921  EVALUATE_MESSAGE(edata->domain, message, false, false);
922 
923  MemoryContextSwitchTo(oldcontext);
924  recursion_depth--;
925  return 0; /* return value does not matter */
926 }
927 
928 
929 /*
930  * errmsg_plural --- add a primary error message text to the current error,
931  * with support for pluralization of the message text
932  */
933 int
934 errmsg_plural(const char *fmt_singular, const char *fmt_plural,
935  unsigned long n,...)
936 {
937  ErrorData *edata = &errordata[errordata_stack_depth];
938  MemoryContext oldcontext;
939 
940  recursion_depth++;
942  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
943 
944  edata->message_id = fmt_singular;
945  EVALUATE_MESSAGE_PLURAL(edata->domain, message, false);
946 
947  MemoryContextSwitchTo(oldcontext);
948  recursion_depth--;
949  return 0; /* return value does not matter */
950 }
951 
952 
953 /*
954  * errdetail --- add a detail error message text to the current error
955  */
956 int
957 errdetail(const char *fmt,...)
958 {
959  ErrorData *edata = &errordata[errordata_stack_depth];
960  MemoryContext oldcontext;
961 
962  recursion_depth++;
964  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
965 
966  EVALUATE_MESSAGE(edata->domain, detail, false, true);
967 
968  MemoryContextSwitchTo(oldcontext);
969  recursion_depth--;
970  return 0; /* return value does not matter */
971 }
972 
973 
974 /*
975  * errdetail_internal --- add a detail error message text to the current error
976  *
977  * This is exactly like errdetail() except that strings passed to
978  * errdetail_internal are not translated, and are customarily left out of the
979  * internationalization message dictionary. This should be used for detail
980  * messages that seem not worth translating for one reason or another
981  * (typically, that they don't seem to be useful to average users).
982  */
983 int
984 errdetail_internal(const char *fmt,...)
985 {
986  ErrorData *edata = &errordata[errordata_stack_depth];
987  MemoryContext oldcontext;
988 
989  recursion_depth++;
991  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
992 
993  EVALUATE_MESSAGE(edata->domain, detail, false, false);
994 
995  MemoryContextSwitchTo(oldcontext);
996  recursion_depth--;
997  return 0; /* return value does not matter */
998 }
999 
1000 
1001 /*
1002  * errdetail_log --- add a detail_log error message text to the current error
1003  */
1004 int
1005 errdetail_log(const char *fmt,...)
1006 {
1007  ErrorData *edata = &errordata[errordata_stack_depth];
1008  MemoryContext oldcontext;
1009 
1010  recursion_depth++;
1012  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
1013 
1014  EVALUATE_MESSAGE(edata->domain, detail_log, false, true);
1015 
1016  MemoryContextSwitchTo(oldcontext);
1017  recursion_depth--;
1018  return 0; /* return value does not matter */
1019 }
1020 
1021 /*
1022  * errdetail_log_plural --- add a detail_log error message text to the current error
1023  * with support for pluralization of the message text
1024  */
1025 int
1026 errdetail_log_plural(const char *fmt_singular, const char *fmt_plural,
1027  unsigned long n,...)
1028 {
1029  ErrorData *edata = &errordata[errordata_stack_depth];
1030  MemoryContext oldcontext;
1031 
1032  recursion_depth++;
1034  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
1035 
1036  EVALUATE_MESSAGE_PLURAL(edata->domain, detail_log, false);
1037 
1038  MemoryContextSwitchTo(oldcontext);
1039  recursion_depth--;
1040  return 0; /* return value does not matter */
1041 }
1042 
1043 
1044 /*
1045  * errdetail_plural --- add a detail error message text to the current error,
1046  * with support for pluralization of the message text
1047  */
1048 int
1049 errdetail_plural(const char *fmt_singular, const char *fmt_plural,
1050  unsigned long n,...)
1051 {
1052  ErrorData *edata = &errordata[errordata_stack_depth];
1053  MemoryContext oldcontext;
1054 
1055  recursion_depth++;
1057  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
1058 
1059  EVALUATE_MESSAGE_PLURAL(edata->domain, detail, false);
1060 
1061  MemoryContextSwitchTo(oldcontext);
1062  recursion_depth--;
1063  return 0; /* return value does not matter */
1064 }
1065 
1066 
1067 /*
1068  * errhint --- add a hint error message text to the current error
1069  */
1070 int
1071 errhint(const char *fmt,...)
1072 {
1073  ErrorData *edata = &errordata[errordata_stack_depth];
1074  MemoryContext oldcontext;
1075 
1076  recursion_depth++;
1078  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
1079 
1080  EVALUATE_MESSAGE(edata->domain, hint, false, true);
1081 
1082  MemoryContextSwitchTo(oldcontext);
1083  recursion_depth--;
1084  return 0; /* return value does not matter */
1085 }
1086 
1087 
1088 /*
1089  * errcontext_msg --- add a context error message text to the current error
1090  *
1091  * Unlike other cases, multiple calls are allowed to build up a stack of
1092  * context information. We assume earlier calls represent more-closely-nested
1093  * states.
1094  */
1095 int
1096 errcontext_msg(const char *fmt,...)
1097 {
1098  ErrorData *edata = &errordata[errordata_stack_depth];
1099  MemoryContext oldcontext;
1100 
1101  recursion_depth++;
1103  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
1104 
1105  EVALUATE_MESSAGE(edata->context_domain, context, true, true);
1106 
1107  MemoryContextSwitchTo(oldcontext);
1108  recursion_depth--;
1109  return 0; /* return value does not matter */
1110 }
1111 
1112 /*
1113  * set_errcontext_domain --- set message domain to be used by errcontext()
1114  *
1115  * errcontext_msg() can be called from a different module than the original
1116  * ereport(), so we cannot use the message domain passed in errstart() to
1117  * translate it. Instead, each errcontext_msg() call should be preceded by
1118  * a set_errcontext_domain() call to specify the domain. This is usually
1119  * done transparently by the errcontext() macro.
1120  *
1121  * Although errcontext is primarily meant for use at call sites distant from
1122  * the original ereport call, there are a few places that invoke errcontext
1123  * within ereport. The expansion of errcontext as a comma expression calling
1124  * set_errcontext_domain then errcontext_msg is problematic in this case,
1125  * because the intended comma expression becomes two arguments to errfinish,
1126  * which the compiler is at liberty to evaluate in either order. But in
1127  * such a case, the set_errcontext_domain calls must be selecting the same
1128  * TEXTDOMAIN value that the errstart call did, so order does not matter
1129  * so long as errstart initializes context_domain along with domain.
1130  */
1131 int
1132 set_errcontext_domain(const char *domain)
1133 {
1134  ErrorData *edata = &errordata[errordata_stack_depth];
1135 
1136  /* we don't bother incrementing recursion_depth */
1138 
1139  /* the default text domain is the backend's */
1140  edata->context_domain = domain ? domain : PG_TEXTDOMAIN("postgres");
1141 
1142  return 0; /* return value does not matter */
1143 }
1144 
1145 
1146 /*
1147  * errhidestmt --- optionally suppress STATEMENT: field of log entry
1148  *
1149  * This should be called if the message text already includes the statement.
1150  */
1151 int
1152 errhidestmt(bool hide_stmt)
1153 {
1154  ErrorData *edata = &errordata[errordata_stack_depth];
1155 
1156  /* we don't bother incrementing recursion_depth */
1158 
1159  edata->hide_stmt = hide_stmt;
1160 
1161  return 0; /* return value does not matter */
1162 }
1163 
1164 /*
1165  * errhidecontext --- optionally suppress CONTEXT: field of log entry
1166  *
1167  * This should only be used for verbose debugging messages where the repeated
1168  * inclusion of context would bloat the log volume too much.
1169  */
1170 int
1171 errhidecontext(bool hide_ctx)
1172 {
1173  ErrorData *edata = &errordata[errordata_stack_depth];
1174 
1175  /* we don't bother incrementing recursion_depth */
1177 
1178  edata->hide_ctx = hide_ctx;
1179 
1180  return 0; /* return value does not matter */
1181 }
1182 
1183 
1184 /*
1185  * errfunction --- add reporting function name to the current error
1186  *
1187  * This is used when backwards compatibility demands that the function
1188  * name appear in messages sent to old-protocol clients. Note that the
1189  * passed string is expected to be a non-freeable constant string.
1190  */
1191 int
1192 errfunction(const char *funcname)
1193 {
1194  ErrorData *edata = &errordata[errordata_stack_depth];
1195 
1196  /* we don't bother incrementing recursion_depth */
1198 
1199  edata->funcname = funcname;
1200  edata->show_funcname = true;
1201 
1202  return 0; /* return value does not matter */
1203 }
1204 
1205 /*
1206  * errposition --- add cursor position to the current error
1207  */
1208 int
1209 errposition(int cursorpos)
1210 {
1211  ErrorData *edata = &errordata[errordata_stack_depth];
1212 
1213  /* we don't bother incrementing recursion_depth */
1215 
1216  edata->cursorpos = cursorpos;
1217 
1218  return 0; /* return value does not matter */
1219 }
1220 
1221 /*
1222  * internalerrposition --- add internal cursor position to the current error
1223  */
1224 int
1225 internalerrposition(int cursorpos)
1226 {
1227  ErrorData *edata = &errordata[errordata_stack_depth];
1228 
1229  /* we don't bother incrementing recursion_depth */
1231 
1232  edata->internalpos = cursorpos;
1233 
1234  return 0; /* return value does not matter */
1235 }
1236 
1237 /*
1238  * internalerrquery --- add internal query text to the current error
1239  *
1240  * Can also pass NULL to drop the internal query text entry. This case
1241  * is intended for use in error callback subroutines that are editorializing
1242  * on the layout of the error report.
1243  */
1244 int
1245 internalerrquery(const char *query)
1246 {
1247  ErrorData *edata = &errordata[errordata_stack_depth];
1248 
1249  /* we don't bother incrementing recursion_depth */
1251 
1252  if (edata->internalquery)
1253  {
1254  pfree(edata->internalquery);
1255  edata->internalquery = NULL;
1256  }
1257 
1258  if (query)
1259  edata->internalquery = MemoryContextStrdup(edata->assoc_context, query);
1260 
1261  return 0; /* return value does not matter */
1262 }
1263 
1264 /*
1265  * err_generic_string -- used to set individual ErrorData string fields
1266  * identified by PG_DIAG_xxx codes.
1267  *
1268  * This intentionally only supports fields that don't use localized strings,
1269  * so that there are no translation considerations.
1270  *
1271  * Most potential callers should not use this directly, but instead prefer
1272  * higher-level abstractions, such as errtablecol() (see relcache.c).
1273  */
1274 int
1275 err_generic_string(int field, const char *str)
1276 {
1277  ErrorData *edata = &errordata[errordata_stack_depth];
1278 
1279  /* we don't bother incrementing recursion_depth */
1281 
1282  switch (field)
1283  {
1284  case PG_DIAG_SCHEMA_NAME:
1285  set_errdata_field(edata->assoc_context, &edata->schema_name, str);
1286  break;
1287  case PG_DIAG_TABLE_NAME:
1288  set_errdata_field(edata->assoc_context, &edata->table_name, str);
1289  break;
1290  case PG_DIAG_COLUMN_NAME:
1291  set_errdata_field(edata->assoc_context, &edata->column_name, str);
1292  break;
1293  case PG_DIAG_DATATYPE_NAME:
1294  set_errdata_field(edata->assoc_context, &edata->datatype_name, str);
1295  break;
1297  set_errdata_field(edata->assoc_context, &edata->constraint_name, str);
1298  break;
1299  default:
1300  elog(ERROR, "unsupported ErrorData field id: %d", field);
1301  break;
1302  }
1303 
1304  return 0; /* return value does not matter */
1305 }
1306 
1307 /*
1308  * set_errdata_field --- set an ErrorData string field
1309  */
1310 static void
1311 set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str)
1312 {
1313  Assert(*ptr == NULL);
1314  *ptr = MemoryContextStrdup(cxt, str);
1315 }
1316 
1317 /*
1318  * geterrcode --- return the currently set SQLSTATE error code
1319  *
1320  * This is only intended for use in error callback subroutines, since there
1321  * is no other place outside elog.c where the concept is meaningful.
1322  */
1323 int
1325 {
1326  ErrorData *edata = &errordata[errordata_stack_depth];
1327 
1328  /* we don't bother incrementing recursion_depth */
1330 
1331  return edata->sqlerrcode;
1332 }
1333 
1334 /*
1335  * geterrposition --- return the currently set error position (0 if none)
1336  *
1337  * This is only intended for use in error callback subroutines, since there
1338  * is no other place outside elog.c where the concept is meaningful.
1339  */
1340 int
1342 {
1343  ErrorData *edata = &errordata[errordata_stack_depth];
1344 
1345  /* we don't bother incrementing recursion_depth */
1347 
1348  return edata->cursorpos;
1349 }
1350 
1351 /*
1352  * getinternalerrposition --- same for internal error position
1353  *
1354  * This is only intended for use in error callback subroutines, since there
1355  * is no other place outside elog.c where the concept is meaningful.
1356  */
1357 int
1359 {
1360  ErrorData *edata = &errordata[errordata_stack_depth];
1361 
1362  /* we don't bother incrementing recursion_depth */
1364 
1365  return edata->internalpos;
1366 }
1367 
1368 
1369 /*
1370  * elog_start --- startup for old-style API
1371  *
1372  * All that we do here is stash the hidden filename/lineno/funcname
1373  * arguments into a stack entry, along with the current value of errno.
1374  *
1375  * We need this to be separate from elog_finish because there's no other
1376  * C89-compliant way to deal with inserting extra arguments into the elog
1377  * call. (When using C99's __VA_ARGS__, we could possibly merge this with
1378  * elog_finish, but there doesn't seem to be a good way to save errno before
1379  * evaluating the format arguments if we do that.)
1380  */
1381 void
1382 elog_start(const char *filename, int lineno, const char *funcname)
1383 {
1384  ErrorData *edata;
1385 
1386  /* Make sure that memory context initialization has finished */
1387  if (ErrorContext == NULL)
1388  {
1389  /* Oops, hard crash time; very little we can do safely here */
1390  write_stderr("error occurred at %s:%d before error message processing is available\n",
1391  filename ? filename : "(unknown file)", lineno);
1392  exit(2);
1393  }
1394 
1396  {
1397  /*
1398  * Wups, stack not big enough. We treat this as a PANIC condition
1399  * because it suggests an infinite loop of errors during error
1400  * recovery. Note that the message is intentionally not localized,
1401  * else failure to convert it to client encoding could cause further
1402  * recursion.
1403  */
1404  errordata_stack_depth = -1; /* make room on stack */
1405  ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded")));
1406  }
1407 
1408  edata = &errordata[errordata_stack_depth];
1409  if (filename)
1410  {
1411  const char *slash;
1412 
1413  /* keep only base name, useful especially for vpath builds */
1414  slash = strrchr(filename, '/');
1415  if (slash)
1416  filename = slash + 1;
1417  }
1418  edata->filename = filename;
1419  edata->lineno = lineno;
1420  edata->funcname = funcname;
1421  /* errno is saved now so that error parameter eval can't change it */
1422  edata->saved_errno = errno;
1423 
1424  /* Use ErrorContext for any allocations done at this level. */
1425  edata->assoc_context = ErrorContext;
1426 }
1427 
1428 /*
1429  * elog_finish --- finish up for old-style API
1430  */
1431 void
1432 elog_finish(int elevel, const char *fmt,...)
1433 {
1434  ErrorData *edata = &errordata[errordata_stack_depth];
1435  MemoryContext oldcontext;
1436 
1438 
1439  /*
1440  * Do errstart() to see if we actually want to report the message.
1441  */
1443  errno = edata->saved_errno;
1444  if (!errstart(elevel, edata->filename, edata->lineno, edata->funcname, NULL))
1445  return; /* nothing to do */
1446 
1447  /*
1448  * Format error message just like errmsg_internal().
1449  */
1450  recursion_depth++;
1451  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
1452 
1453  if (!edata->backtrace &&
1454  edata->funcname &&
1456  set_backtrace(edata, 2);
1457 
1458  edata->message_id = fmt;
1459  EVALUATE_MESSAGE(edata->domain, message, false, false);
1460 
1461  MemoryContextSwitchTo(oldcontext);
1462  recursion_depth--;
1463 
1464  /*
1465  * And let errfinish() finish up.
1466  */
1467  errfinish(0);
1468 }
1469 
1470 
1471 /*
1472  * Functions to allow construction of error message strings separately from
1473  * the ereport() call itself.
1474  *
1475  * The expected calling convention is
1476  *
1477  * pre_format_elog_string(errno, domain), var = format_elog_string(format,...)
1478  *
1479  * which can be hidden behind a macro such as GUC_check_errdetail(). We
1480  * assume that any functions called in the arguments of format_elog_string()
1481  * cannot result in re-entrant use of these functions --- otherwise the wrong
1482  * text domain might be used, or the wrong errno substituted for %m. This is
1483  * okay for the current usage with GUC check hooks, but might need further
1484  * effort someday.
1485  *
1486  * The result of format_elog_string() is stored in ErrorContext, and will
1487  * therefore survive until FlushErrorState() is called.
1488  */
1490 static const char *save_format_domain;
1491 
1492 void
1493 pre_format_elog_string(int errnumber, const char *domain)
1494 {
1495  /* Save errno before evaluation of argument functions can change it */
1496  save_format_errnumber = errnumber;
1497  /* Save caller's text domain */
1498  save_format_domain = domain;
1499 }
1500 
1501 char *
1502 format_elog_string(const char *fmt,...)
1503 {
1504  ErrorData errdata;
1505  ErrorData *edata;
1506  MemoryContext oldcontext;
1507 
1508  /* Initialize a mostly-dummy error frame */
1509  edata = &errdata;
1510  MemSet(edata, 0, sizeof(ErrorData));
1511  /* the default text domain is the backend's */
1512  edata->domain = save_format_domain ? save_format_domain : PG_TEXTDOMAIN("postgres");
1513  /* set the errno to be used to interpret %m */
1515 
1516  oldcontext = MemoryContextSwitchTo(ErrorContext);
1517 
1518  edata->message_id = fmt;
1519  EVALUATE_MESSAGE(edata->domain, message, false, true);
1520 
1521  MemoryContextSwitchTo(oldcontext);
1522 
1523  return edata->message;
1524 }
1525 
1526 
1527 /*
1528  * Actual output of the top-of-stack error message
1529  *
1530  * In the ereport(ERROR) case this is called from PostgresMain (or not at all,
1531  * if the error is caught by somebody). For all other severity levels this
1532  * is called by errfinish.
1533  */
1534 void
1536 {
1537  ErrorData *edata = &errordata[errordata_stack_depth];
1538  MemoryContext oldcontext;
1539 
1540  recursion_depth++;
1542  oldcontext = MemoryContextSwitchTo(edata->assoc_context);
1543 
1544  /*
1545  * Call hook before sending message to log. The hook function is allowed
1546  * to turn off edata->output_to_server, so we must recheck that afterward.
1547  * Making any other change in the content of edata is not considered
1548  * supported.
1549  *
1550  * Note: the reason why the hook can only turn off output_to_server, and
1551  * not turn it on, is that it'd be unreliable: we will never get here at
1552  * all if errstart() deems the message uninteresting. A hook that could
1553  * make decisions in that direction would have to hook into errstart(),
1554  * where it would have much less information available. emit_log_hook is
1555  * intended for custom log filtering and custom log message transmission
1556  * mechanisms.
1557  *
1558  * The log hook has access to both the translated and original English
1559  * error message text, which is passed through to allow it to be used as a
1560  * message identifier. Note that the original text is not available for
1561  * detail, detail_log, hint and context text elements.
1562  */
1563  if (edata->output_to_server && emit_log_hook)
1564  (*emit_log_hook) (edata);
1565 
1566  /* Send to server log, if enabled */
1567  if (edata->output_to_server)
1569 
1570  /* Send to client, if enabled */
1571  if (edata->output_to_client)
1572  send_message_to_frontend(edata);
1573 
1574  MemoryContextSwitchTo(oldcontext);
1575  recursion_depth--;
1576 }
1577 
1578 /*
1579  * CopyErrorData --- obtain a copy of the topmost error stack entry
1580  *
1581  * This is only for use in error handler code. The data is copied into the
1582  * current memory context, so callers should always switch away from
1583  * ErrorContext first; otherwise it will be lost when FlushErrorState is done.
1584  */
1585 ErrorData *
1587 {
1588  ErrorData *edata = &errordata[errordata_stack_depth];
1589  ErrorData *newedata;
1590 
1591  /*
1592  * we don't increment recursion_depth because out-of-memory here does not
1593  * indicate a problem within the error subsystem.
1594  */
1596 
1598 
1599  /* Copy the struct itself */
1600  newedata = (ErrorData *) palloc(sizeof(ErrorData));
1601  memcpy(newedata, edata, sizeof(ErrorData));
1602 
1603  /* Make copies of separately-allocated fields */
1604  if (newedata->message)
1605  newedata->message = pstrdup(newedata->message);
1606  if (newedata->detail)
1607  newedata->detail = pstrdup(newedata->detail);
1608  if (newedata->detail_log)
1609  newedata->detail_log = pstrdup(newedata->detail_log);
1610  if (newedata->hint)
1611  newedata->hint = pstrdup(newedata->hint);
1612  if (newedata->context)
1613  newedata->context = pstrdup(newedata->context);
1614  if (newedata->backtrace)
1615  newedata->backtrace = pstrdup(newedata->backtrace);
1616  if (newedata->schema_name)
1617  newedata->schema_name = pstrdup(newedata->schema_name);
1618  if (newedata->table_name)
1619  newedata->table_name = pstrdup(newedata->table_name);
1620  if (newedata->column_name)
1621  newedata->column_name = pstrdup(newedata->column_name);
1622  if (newedata->datatype_name)
1623  newedata->datatype_name = pstrdup(newedata->datatype_name);
1624  if (newedata->constraint_name)
1625  newedata->constraint_name = pstrdup(newedata->constraint_name);
1626  if (newedata->internalquery)
1627  newedata->internalquery = pstrdup(newedata->internalquery);
1628 
1629  /* Use the calling context for string allocation */
1630  newedata->assoc_context = CurrentMemoryContext;
1631 
1632  return newedata;
1633 }
1634 
1635 /*
1636  * FreeErrorData --- free the structure returned by CopyErrorData.
1637  *
1638  * Error handlers should use this in preference to assuming they know all
1639  * the separately-allocated fields.
1640  */
1641 void
1643 {
1644  if (edata->message)
1645  pfree(edata->message);
1646  if (edata->detail)
1647  pfree(edata->detail);
1648  if (edata->detail_log)
1649  pfree(edata->detail_log);
1650  if (edata->hint)
1651  pfree(edata->hint);
1652  if (edata->context)
1653  pfree(edata->context);
1654  if (edata->backtrace)
1655  pfree(edata->backtrace);
1656  if (edata->schema_name)
1657  pfree(edata->schema_name);
1658  if (edata->table_name)
1659  pfree(edata->table_name);
1660  if (edata->column_name)
1661  pfree(edata->column_name);
1662  if (edata->datatype_name)
1663  pfree(edata->datatype_name);
1664  if (edata->constraint_name)
1665  pfree(edata->constraint_name);
1666  if (edata->internalquery)
1667  pfree(edata->internalquery);
1668  pfree(edata);
1669 }
1670 
1671 /*
1672  * FlushErrorState --- flush the error state after error recovery
1673  *
1674  * This should be called by an error handler after it's done processing
1675  * the error; or as soon as it's done CopyErrorData, if it intends to
1676  * do stuff that is likely to provoke another error. You are not "out" of
1677  * the error subsystem until you have done this.
1678  */
1679 void
1681 {
1682  /*
1683  * Reset stack to empty. The only case where it would be more than one
1684  * deep is if we serviced an error that interrupted construction of
1685  * another message. We assume control escaped out of that message
1686  * construction and won't ever go back.
1687  */
1688  errordata_stack_depth = -1;
1689  recursion_depth = 0;
1690  /* Delete all data in ErrorContext */
1692 }
1693 
1694 /*
1695  * ThrowErrorData --- report an error described by an ErrorData structure
1696  *
1697  * This is somewhat like ReThrowError, but it allows elevels besides ERROR,
1698  * and the boolean flags such as output_to_server are computed via the
1699  * default rules rather than being copied from the given ErrorData.
1700  * This is primarily used to re-report errors originally reported by
1701  * background worker processes and then propagated (with or without
1702  * modification) to the backend responsible for them.
1703  */
1704 void
1706 {
1707  ErrorData *newedata;
1708  MemoryContext oldcontext;
1709 
1710  if (!errstart(edata->elevel, edata->filename, edata->lineno,
1711  edata->funcname, NULL))
1712  return; /* error is not to be reported at all */
1713 
1714  newedata = &errordata[errordata_stack_depth];
1715  recursion_depth++;
1716  oldcontext = MemoryContextSwitchTo(newedata->assoc_context);
1717 
1718  /* Copy the supplied fields to the error stack entry. */
1719  if (edata->sqlerrcode != 0)
1720  newedata->sqlerrcode = edata->sqlerrcode;
1721  if (edata->message)
1722  newedata->message = pstrdup(edata->message);
1723  if (edata->detail)
1724  newedata->detail = pstrdup(edata->detail);
1725  if (edata->detail_log)
1726  newedata->detail_log = pstrdup(edata->detail_log);
1727  if (edata->hint)
1728  newedata->hint = pstrdup(edata->hint);
1729  if (edata->context)
1730  newedata->context = pstrdup(edata->context);
1731  if (edata->backtrace)
1732  newedata->backtrace = pstrdup(edata->backtrace);
1733  /* assume message_id is not available */
1734  if (edata->schema_name)
1735  newedata->schema_name = pstrdup(edata->schema_name);
1736  if (edata->table_name)
1737  newedata->table_name = pstrdup(edata->table_name);
1738  if (edata->column_name)
1739  newedata->column_name = pstrdup(edata->column_name);
1740  if (edata->datatype_name)
1741  newedata->datatype_name = pstrdup(edata->datatype_name);
1742  if (edata->constraint_name)
1743  newedata->constraint_name = pstrdup(edata->constraint_name);
1744  newedata->cursorpos = edata->cursorpos;
1745  newedata->internalpos = edata->internalpos;
1746  if (edata->internalquery)
1747  newedata->internalquery = pstrdup(edata->internalquery);
1748 
1749  MemoryContextSwitchTo(oldcontext);
1750  recursion_depth--;
1751 
1752  /* Process the error. */
1753  errfinish(0);
1754 }
1755 
1756 /*
1757  * ReThrowError --- re-throw a previously copied error
1758  *
1759  * A handler can do CopyErrorData/FlushErrorState to get out of the error
1760  * subsystem, then do some processing, and finally ReThrowError to re-throw
1761  * the original error. This is slower than just PG_RE_THROW() but should
1762  * be used if the "some processing" is likely to incur another error.
1763  */
1764 void
1766 {
1767  ErrorData *newedata;
1768 
1769  Assert(edata->elevel == ERROR);
1770 
1771  /* Push the data back into the error context */
1772  recursion_depth++;
1774 
1776  {
1777  /*
1778  * Wups, stack not big enough. We treat this as a PANIC condition
1779  * because it suggests an infinite loop of errors during error
1780  * recovery.
1781  */
1782  errordata_stack_depth = -1; /* make room on stack */
1783  ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded")));
1784  }
1785 
1786  newedata = &errordata[errordata_stack_depth];
1787  memcpy(newedata, edata, sizeof(ErrorData));
1788 
1789  /* Make copies of separately-allocated fields */
1790  if (newedata->message)
1791  newedata->message = pstrdup(newedata->message);
1792  if (newedata->detail)
1793  newedata->detail = pstrdup(newedata->detail);
1794  if (newedata->detail_log)
1795  newedata->detail_log = pstrdup(newedata->detail_log);
1796  if (newedata->hint)
1797  newedata->hint = pstrdup(newedata->hint);
1798  if (newedata->context)
1799  newedata->context = pstrdup(newedata->context);
1800  if (newedata->backtrace)
1801  newedata->backtrace = pstrdup(newedata->backtrace);
1802  if (newedata->schema_name)
1803  newedata->schema_name = pstrdup(newedata->schema_name);
1804  if (newedata->table_name)
1805  newedata->table_name = pstrdup(newedata->table_name);
1806  if (newedata->column_name)
1807  newedata->column_name = pstrdup(newedata->column_name);
1808  if (newedata->datatype_name)
1809  newedata->datatype_name = pstrdup(newedata->datatype_name);
1810  if (newedata->constraint_name)
1811  newedata->constraint_name = pstrdup(newedata->constraint_name);
1812  if (newedata->internalquery)
1813  newedata->internalquery = pstrdup(newedata->internalquery);
1814 
1815  /* Reset the assoc_context to be ErrorContext */
1816  newedata->assoc_context = ErrorContext;
1817 
1818  recursion_depth--;
1819  PG_RE_THROW();
1820 }
1821 
1822 /*
1823  * pg_re_throw --- out-of-line implementation of PG_RE_THROW() macro
1824  */
1825 void
1827 {
1828  /* If possible, throw the error to the next outer setjmp handler */
1829  if (PG_exception_stack != NULL)
1830  siglongjmp(*PG_exception_stack, 1);
1831  else
1832  {
1833  /*
1834  * If we get here, elog(ERROR) was thrown inside a PG_TRY block, which
1835  * we have now exited only to discover that there is no outer setjmp
1836  * handler to pass the error to. Had the error been thrown outside
1837  * the block to begin with, we'd have promoted the error to FATAL, so
1838  * the correct behavior is to make it FATAL now; that is, emit it and
1839  * then call proc_exit.
1840  */
1841  ErrorData *edata = &errordata[errordata_stack_depth];
1842 
1844  Assert(edata->elevel == ERROR);
1845  edata->elevel = FATAL;
1846 
1847  /*
1848  * At least in principle, the increase in severity could have changed
1849  * where-to-output decisions, so recalculate. This should stay in
1850  * sync with errstart(), which see for comments.
1851  */
1855  else
1856  edata->output_to_server = (FATAL >= log_min_messages);
1858  edata->output_to_client = true;
1859 
1860  /*
1861  * We can use errfinish() for the rest, but we don't want it to call
1862  * any error context routines a second time. Since we know we are
1863  * about to exit, it should be OK to just clear the context stack.
1864  */
1865  error_context_stack = NULL;
1866 
1867  errfinish(0);
1868  }
1869 
1870  /* Doesn't return ... */
1871  ExceptionalCondition("pg_re_throw tried to return", "FailedAssertion",
1872  __FILE__, __LINE__);
1873 }
1874 
1875 
1876 /*
1877  * GetErrorContextStack - Return the context stack, for display/diags
1878  *
1879  * Returns a pstrdup'd string in the caller's context which includes the PG
1880  * error call stack. It is the caller's responsibility to ensure this string
1881  * is pfree'd (or its context cleaned up) when done.
1882  *
1883  * This information is collected by traversing the error contexts and calling
1884  * each context's callback function, each of which is expected to call
1885  * errcontext() to return a string which can be presented to the user.
1886  */
1887 char *
1889 {
1890  ErrorData *edata;
1891  ErrorContextCallback *econtext;
1892 
1893  /*
1894  * Okay, crank up a stack entry to store the info in.
1895  */
1896  recursion_depth++;
1897 
1899  {
1900  /*
1901  * Wups, stack not big enough. We treat this as a PANIC condition
1902  * because it suggests an infinite loop of errors during error
1903  * recovery.
1904  */
1905  errordata_stack_depth = -1; /* make room on stack */
1906  ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded")));
1907  }
1908 
1909  /*
1910  * Things look good so far, so initialize our error frame
1911  */
1912  edata = &errordata[errordata_stack_depth];
1913  MemSet(edata, 0, sizeof(ErrorData));
1914 
1915  /*
1916  * Set up assoc_context to be the caller's context, so any allocations
1917  * done (which will include edata->context) will use their context.
1918  */
1920 
1921  /*
1922  * Call any context callback functions to collect the context information
1923  * into edata->context.
1924  *
1925  * Errors occurring in callback functions should go through the regular
1926  * error handling code which should handle any recursive errors, though we
1927  * double-check above, just in case.
1928  */
1929  for (econtext = error_context_stack;
1930  econtext != NULL;
1931  econtext = econtext->previous)
1932  econtext->callback(econtext->arg);
1933 
1934  /*
1935  * Clean ourselves off the stack, any allocations done should have been
1936  * using edata->assoc_context, which we set up earlier to be the caller's
1937  * context, so we're free to just remove our entry off the stack and
1938  * decrement recursion depth and exit.
1939  */
1941  recursion_depth--;
1942 
1943  /*
1944  * Return a pointer to the string the caller asked for, which should have
1945  * been allocated in their context.
1946  */
1947  return edata->context;
1948 }
1949 
1950 
1951 /*
1952  * Initialization of error output file
1953  */
1954 void
1956 {
1957  int fd,
1958  istty;
1959 
1960  if (OutputFileName[0])
1961  {
1962  /*
1963  * A debug-output file name was given.
1964  *
1965  * Make sure we can write the file, and find out if it's a tty.
1966  */
1967  if ((fd = open(OutputFileName, O_CREAT | O_APPEND | O_WRONLY,
1968  0666)) < 0)
1969  ereport(FATAL,
1971  errmsg("could not open file \"%s\": %m", OutputFileName)));
1972  istty = isatty(fd);
1973  close(fd);
1974 
1975  /*
1976  * Redirect our stderr to the debug output file.
1977  */
1978  if (!freopen(OutputFileName, "a", stderr))
1979  ereport(FATAL,
1981  errmsg("could not reopen file \"%s\" as stderr: %m",
1982  OutputFileName)));
1983 
1984  /*
1985  * If the file is a tty and we're running under the postmaster, try to
1986  * send stdout there as well (if it isn't a tty then stderr will block
1987  * out stdout, so we may as well let stdout go wherever it was going
1988  * before).
1989  */
1990  if (istty && IsUnderPostmaster)
1991  if (!freopen(OutputFileName, "a", stdout))
1992  ereport(FATAL,
1994  errmsg("could not reopen file \"%s\" as stdout: %m",
1995  OutputFileName)));
1996  }
1997 }
1998 
1999 
2000 #ifdef HAVE_SYSLOG
2001 
2002 /*
2003  * Set or update the parameters for syslog logging
2004  */
2005 void
2006 set_syslog_parameters(const char *ident, int facility)
2007 {
2008  /*
2009  * guc.c is likely to call us repeatedly with same parameters, so don't
2010  * thrash the syslog connection unnecessarily. Also, we do not re-open
2011  * the connection until needed, since this routine will get called whether
2012  * or not Log_destination actually mentions syslog.
2013  *
2014  * Note that we make our own copy of the ident string rather than relying
2015  * on guc.c's. This may be overly paranoid, but it ensures that we cannot
2016  * accidentally free a string that syslog is still using.
2017  */
2018  if (syslog_ident == NULL || strcmp(syslog_ident, ident) != 0 ||
2019  syslog_facility != facility)
2020  {
2021  if (openlog_done)
2022  {
2023  closelog();
2024  openlog_done = false;
2025  }
2026  if (syslog_ident)
2027  free(syslog_ident);
2028  syslog_ident = strdup(ident);
2029  /* if the strdup fails, we will cope in write_syslog() */
2030  syslog_facility = facility;
2031  }
2032 }
2033 
2034 
2035 /*
2036  * Write a message line to syslog
2037  */
2038 static void
2039 write_syslog(int level, const char *line)
2040 {
2041  static unsigned long seq = 0;
2042 
2043  int len;
2044  const char *nlpos;
2045 
2046  /* Open syslog connection if not done yet */
2047  if (!openlog_done)
2048  {
2049  openlog(syslog_ident ? syslog_ident : "postgres",
2050  LOG_PID | LOG_NDELAY | LOG_NOWAIT,
2051  syslog_facility);
2052  openlog_done = true;
2053  }
2054 
2055  /*
2056  * We add a sequence number to each log message to suppress "same"
2057  * messages.
2058  */
2059  seq++;
2060 
2061  /*
2062  * Our problem here is that many syslog implementations don't handle long
2063  * messages in an acceptable manner. While this function doesn't help that
2064  * fact, it does work around by splitting up messages into smaller pieces.
2065  *
2066  * We divide into multiple syslog() calls if message is too long or if the
2067  * message contains embedded newline(s).
2068  */
2069  len = strlen(line);
2070  nlpos = strchr(line, '\n');
2071  if (syslog_split_messages && (len > PG_SYSLOG_LIMIT || nlpos != NULL))
2072  {
2073  int chunk_nr = 0;
2074 
2075  while (len > 0)
2076  {
2077  char buf[PG_SYSLOG_LIMIT + 1];
2078  int buflen;
2079  int i;
2080 
2081  /* if we start at a newline, move ahead one char */
2082  if (line[0] == '\n')
2083  {
2084  line++;
2085  len--;
2086  /* we need to recompute the next newline's position, too */
2087  nlpos = strchr(line, '\n');
2088  continue;
2089  }
2090 
2091  /* copy one line, or as much as will fit, to buf */
2092  if (nlpos != NULL)
2093  buflen = nlpos - line;
2094  else
2095  buflen = len;
2096  buflen = Min(buflen, PG_SYSLOG_LIMIT);
2097  memcpy(buf, line, buflen);
2098  buf[buflen] = '\0';
2099 
2100  /* trim to multibyte letter boundary */
2101  buflen = pg_mbcliplen(buf, buflen, buflen);
2102  if (buflen <= 0)
2103  return;
2104  buf[buflen] = '\0';
2105 
2106  /* already word boundary? */
2107  if (line[buflen] != '\0' &&
2108  !isspace((unsigned char) line[buflen]))
2109  {
2110  /* try to divide at word boundary */
2111  i = buflen - 1;
2112  while (i > 0 && !isspace((unsigned char) buf[i]))
2113  i--;
2114 
2115  if (i > 0) /* else couldn't divide word boundary */
2116  {
2117  buflen = i;
2118  buf[i] = '\0';
2119  }
2120  }
2121 
2122  chunk_nr++;
2123 
2125  syslog(level, "[%lu-%d] %s", seq, chunk_nr, buf);
2126  else
2127  syslog(level, "[%d] %s", chunk_nr, buf);
2128 
2129  line += buflen;
2130  len -= buflen;
2131  }
2132  }
2133  else
2134  {
2135  /* message short enough */
2137  syslog(level, "[%lu] %s", seq, line);
2138  else
2139  syslog(level, "%s", line);
2140  }
2141 }
2142 #endif /* HAVE_SYSLOG */
2143 
2144 #ifdef WIN32
2145 /*
2146  * Get the PostgreSQL equivalent of the Windows ANSI code page. "ANSI" system
2147  * interfaces (e.g. CreateFileA()) expect string arguments in this encoding.
2148  * Every process in a given system will find the same value at all times.
2149  */
2150 static int
2151 GetACPEncoding(void)
2152 {
2153  static int encoding = -2;
2154 
2155  if (encoding == -2)
2156  encoding = pg_codepage_to_encoding(GetACP());
2157 
2158  return encoding;
2159 }
2160 
2161 /*
2162  * Write a message line to the windows event log
2163  */
2164 static void
2165 write_eventlog(int level, const char *line, int len)
2166 {
2167  WCHAR *utf16;
2168  int eventlevel = EVENTLOG_ERROR_TYPE;
2169  static HANDLE evtHandle = INVALID_HANDLE_VALUE;
2170 
2171  if (evtHandle == INVALID_HANDLE_VALUE)
2172  {
2173  evtHandle = RegisterEventSource(NULL,
2175  if (evtHandle == NULL)
2176  {
2177  evtHandle = INVALID_HANDLE_VALUE;
2178  return;
2179  }
2180  }
2181 
2182  switch (level)
2183  {
2184  case DEBUG5:
2185  case DEBUG4:
2186  case DEBUG3:
2187  case DEBUG2:
2188  case DEBUG1:
2189  case LOG:
2190  case LOG_SERVER_ONLY:
2191  case INFO:
2192  case NOTICE:
2193  eventlevel = EVENTLOG_INFORMATION_TYPE;
2194  break;
2195  case WARNING:
2196  eventlevel = EVENTLOG_WARNING_TYPE;
2197  break;
2198  case ERROR:
2199  case FATAL:
2200  case PANIC:
2201  default:
2202  eventlevel = EVENTLOG_ERROR_TYPE;
2203  break;
2204  }
2205 
2206  /*
2207  * If message character encoding matches the encoding expected by
2208  * ReportEventA(), call it to avoid the hazards of conversion. Otherwise,
2209  * try to convert the message to UTF16 and write it with ReportEventW().
2210  * Fall back on ReportEventA() if conversion failed.
2211  *
2212  * Since we palloc the structure required for conversion, also fall
2213  * through to writing unconverted if we have not yet set up
2214  * CurrentMemoryContext.
2215  *
2216  * Also verify that we are not on our way into error recursion trouble due
2217  * to error messages thrown deep inside pgwin32_message_to_UTF16().
2218  */
2219  if (!in_error_recursion_trouble() &&
2220  CurrentMemoryContext != NULL &&
2221  GetMessageEncoding() != GetACPEncoding())
2222  {
2223  utf16 = pgwin32_message_to_UTF16(line, len, NULL);
2224  if (utf16)
2225  {
2226  ReportEventW(evtHandle,
2227  eventlevel,
2228  0,
2229  0, /* All events are Id 0 */
2230  NULL,
2231  1,
2232  0,
2233  (LPCWSTR *) &utf16,
2234  NULL);
2235  /* XXX Try ReportEventA() when ReportEventW() fails? */
2236 
2237  pfree(utf16);
2238  return;
2239  }
2240  }
2241  ReportEventA(evtHandle,
2242  eventlevel,
2243  0,
2244  0, /* All events are Id 0 */
2245  NULL,
2246  1,
2247  0,
2248  &line,
2249  NULL);
2250 }
2251 #endif /* WIN32 */
2252 
2253 static void
2254 write_console(const char *line, int len)
2255 {
2256  int rc;
2257 
2258 #ifdef WIN32
2259 
2260  /*
2261  * Try to convert the message to UTF16 and write it with WriteConsoleW().
2262  * Fall back on write() if anything fails.
2263  *
2264  * In contrast to write_eventlog(), don't skip straight to write() based
2265  * on the applicable encodings. Unlike WriteConsoleW(), write() depends
2266  * on the suitability of the console output code page. Since we put
2267  * stderr into binary mode in SubPostmasterMain(), write() skips the
2268  * necessary translation anyway.
2269  *
2270  * WriteConsoleW() will fail if stderr is redirected, so just fall through
2271  * to writing unconverted to the logfile in this case.
2272  *
2273  * Since we palloc the structure required for conversion, also fall
2274  * through to writing unconverted if we have not yet set up
2275  * CurrentMemoryContext.
2276  */
2277  if (!in_error_recursion_trouble() &&
2278  !redirection_done &&
2279  CurrentMemoryContext != NULL)
2280  {
2281  WCHAR *utf16;
2282  int utf16len;
2283 
2284  utf16 = pgwin32_message_to_UTF16(line, len, &utf16len);
2285  if (utf16 != NULL)
2286  {
2287  HANDLE stdHandle;
2288  DWORD written;
2289 
2290  stdHandle = GetStdHandle(STD_ERROR_HANDLE);
2291  if (WriteConsoleW(stdHandle, utf16, utf16len, &written, NULL))
2292  {
2293  pfree(utf16);
2294  return;
2295  }
2296 
2297  /*
2298  * In case WriteConsoleW() failed, fall back to writing the
2299  * message unconverted.
2300  */
2301  pfree(utf16);
2302  }
2303  }
2304 #else
2305 
2306  /*
2307  * Conversion on non-win32 platforms is not implemented yet. It requires
2308  * non-throw version of pg_do_encoding_conversion(), that converts
2309  * unconvertable characters to '?' without errors.
2310  */
2311 #endif
2312 
2313  /*
2314  * We ignore any error from write() here. We have no useful way to report
2315  * it ... certainly whining on stderr isn't likely to be productive.
2316  */
2317  rc = write(fileno(stderr), line, len);
2318  (void) rc;
2319 }
2320 
2321 /*
2322  * setup formatted_log_time, for consistent times between CSV and regular logs
2323  */
2324 static void
2326 {
2327  pg_time_t stamp_time;
2328  char msbuf[13];
2329 
2330  if (!saved_timeval_set)
2331  {
2332  gettimeofday(&saved_timeval, NULL);
2333  saved_timeval_set = true;
2334  }
2335 
2336  stamp_time = (pg_time_t) saved_timeval.tv_sec;
2337 
2338  /*
2339  * Note: we expect that guc.c will ensure that log_timezone is set up (at
2340  * least with a minimal GMT value) before Log_line_prefix can become
2341  * nonempty or CSV mode can be selected.
2342  */
2344  /* leave room for milliseconds... */
2345  "%Y-%m-%d %H:%M:%S %Z",
2346  pg_localtime(&stamp_time, log_timezone));
2347 
2348  /* 'paste' milliseconds into place... */
2349  sprintf(msbuf, ".%03d", (int) (saved_timeval.tv_usec / 1000));
2350  memcpy(formatted_log_time + 19, msbuf, 4);
2351 }
2352 
2353 /*
2354  * setup formatted_start_time
2355  */
2356 static void
2358 {
2359  pg_time_t stamp_time = (pg_time_t) MyStartTime;
2360 
2361  /*
2362  * Note: we expect that guc.c will ensure that log_timezone is set up (at
2363  * least with a minimal GMT value) before Log_line_prefix can become
2364  * nonempty or CSV mode can be selected.
2365  */
2367  "%Y-%m-%d %H:%M:%S %Z",
2368  pg_localtime(&stamp_time, log_timezone));
2369 }
2370 
2371 /*
2372  * process_log_prefix_padding --- helper function for processing the format
2373  * string in log_line_prefix
2374  *
2375  * Note: This function returns NULL if it finds something which
2376  * it deems invalid in the format string.
2377  */
2378 static const char *
2379 process_log_prefix_padding(const char *p, int *ppadding)
2380 {
2381  int paddingsign = 1;
2382  int padding = 0;
2383 
2384  if (*p == '-')
2385  {
2386  p++;
2387 
2388  if (*p == '\0') /* Did the buf end in %- ? */
2389  return NULL;
2390  paddingsign = -1;
2391  }
2392 
2393  /* generate an int version of the numerical string */
2394  while (*p >= '0' && *p <= '9')
2395  padding = padding * 10 + (*p++ - '0');
2396 
2397  /* format is invalid if it ends with the padding number */
2398  if (*p == '\0')
2399  return NULL;
2400 
2401  padding *= paddingsign;
2402  *ppadding = padding;
2403  return p;
2404 }
2405 
2406 /*
2407  * Format tag info for log lines; append to the provided buffer.
2408  */
2409 static void
2411 {
2412  /* static counter for line numbers */
2413  static long log_line_number = 0;
2414 
2415  /* has counter been reset in current process? */
2416  static int log_my_pid = 0;
2417  int padding;
2418  const char *p;
2419 
2420  /*
2421  * This is one of the few places where we'd rather not inherit a static
2422  * variable's value from the postmaster. But since we will, reset it when
2423  * MyProcPid changes. MyStartTime also changes when MyProcPid does, so
2424  * reset the formatted start timestamp too.
2425  */
2426  if (log_my_pid != MyProcPid)
2427  {
2428  log_line_number = 0;
2429  log_my_pid = MyProcPid;
2430  formatted_start_time[0] = '\0';
2431  }
2432  log_line_number++;
2433 
2434  if (Log_line_prefix == NULL)
2435  return; /* in case guc hasn't run yet */
2436 
2437  for (p = Log_line_prefix; *p != '\0'; p++)
2438  {
2439  if (*p != '%')
2440  {
2441  /* literal char, just copy */
2442  appendStringInfoChar(buf, *p);
2443  continue;
2444  }
2445 
2446  /* must be a '%', so skip to the next char */
2447  p++;
2448  if (*p == '\0')
2449  break; /* format error - ignore it */
2450  else if (*p == '%')
2451  {
2452  /* string contains %% */
2453  appendStringInfoChar(buf, '%');
2454  continue;
2455  }
2456 
2457 
2458  /*
2459  * Process any formatting which may exist after the '%'. Note that
2460  * process_log_prefix_padding moves p past the padding number if it
2461  * exists.
2462  *
2463  * Note: Since only '-', '0' to '9' are valid formatting characters we
2464  * can do a quick check here to pre-check for formatting. If the char
2465  * is not formatting then we can skip a useless function call.
2466  *
2467  * Further note: At least on some platforms, passing %*s rather than
2468  * %s to appendStringInfo() is substantially slower, so many of the
2469  * cases below avoid doing that unless non-zero padding is in fact
2470  * specified.
2471  */
2472  if (*p > '9')
2473  padding = 0;
2474  else if ((p = process_log_prefix_padding(p, &padding)) == NULL)
2475  break;
2476 
2477  /* process the option */
2478  switch (*p)
2479  {
2480  case 'a':
2481  if (MyProcPort)
2482  {
2483  const char *appname = application_name;
2484 
2485  if (appname == NULL || *appname == '\0')
2486  appname = _("[unknown]");
2487  if (padding != 0)
2488  appendStringInfo(buf, "%*s", padding, appname);
2489  else
2490  appendStringInfoString(buf, appname);
2491  }
2492  else if (padding != 0)
2494  padding > 0 ? padding : -padding);
2495 
2496  break;
2497  case 'u':
2498  if (MyProcPort)
2499  {
2500  const char *username = MyProcPort->user_name;
2501 
2502  if (username == NULL || *username == '\0')
2503  username = _("[unknown]");
2504  if (padding != 0)
2505  appendStringInfo(buf, "%*s", padding, username);
2506  else
2507  appendStringInfoString(buf, username);
2508  }
2509  else if (padding != 0)
2511  padding > 0 ? padding : -padding);
2512  break;
2513  case 'd':
2514  if (MyProcPort)
2515  {
2516  const char *dbname = MyProcPort->database_name;
2517 
2518  if (dbname == NULL || *dbname == '\0')
2519  dbname = _("[unknown]");
2520  if (padding != 0)
2521  appendStringInfo(buf, "%*s", padding, dbname);
2522  else
2523  appendStringInfoString(buf, dbname);
2524  }
2525  else if (padding != 0)
2527  padding > 0 ? padding : -padding);
2528  break;
2529  case 'c':
2530  if (padding != 0)
2531  {
2532  char strfbuf[128];
2533 
2534  snprintf(strfbuf, sizeof(strfbuf) - 1, "%lx.%x",
2535  (long) (MyStartTime), MyProcPid);
2536  appendStringInfo(buf, "%*s", padding, strfbuf);
2537  }
2538  else
2539  appendStringInfo(buf, "%lx.%x", (long) (MyStartTime), MyProcPid);
2540  break;
2541  case 'p':
2542  if (padding != 0)
2543  appendStringInfo(buf, "%*d", padding, MyProcPid);
2544  else
2545  appendStringInfo(buf, "%d", MyProcPid);
2546  break;
2547  case 'l':
2548  if (padding != 0)
2549  appendStringInfo(buf, "%*ld", padding, log_line_number);
2550  else
2551  appendStringInfo(buf, "%ld", log_line_number);
2552  break;
2553  case 'm':
2555  if (padding != 0)
2556  appendStringInfo(buf, "%*s", padding, formatted_log_time);
2557  else
2559  break;
2560  case 't':
2561  {
2562  pg_time_t stamp_time = (pg_time_t) time(NULL);
2563  char strfbuf[128];
2564 
2565  pg_strftime(strfbuf, sizeof(strfbuf),
2566  "%Y-%m-%d %H:%M:%S %Z",
2567  pg_localtime(&stamp_time, log_timezone));
2568  if (padding != 0)
2569  appendStringInfo(buf, "%*s", padding, strfbuf);
2570  else
2571  appendStringInfoString(buf, strfbuf);
2572  }
2573  break;
2574  case 'n':
2575  {
2576  char strfbuf[128];
2577 
2578  if (!saved_timeval_set)
2579  {
2580  gettimeofday(&saved_timeval, NULL);
2581  saved_timeval_set = true;
2582  }
2583 
2584  snprintf(strfbuf, sizeof(strfbuf), "%ld.%03d",
2585  (long) saved_timeval.tv_sec,
2586  (int) (saved_timeval.tv_usec / 1000));
2587 
2588  if (padding != 0)
2589  appendStringInfo(buf, "%*s", padding, strfbuf);
2590  else
2591  appendStringInfoString(buf, strfbuf);
2592  }
2593  break;
2594  case 's':
2595  if (formatted_start_time[0] == '\0')
2597  if (padding != 0)
2598  appendStringInfo(buf, "%*s", padding, formatted_start_time);
2599  else
2601  break;
2602  case 'i':
2603  if (MyProcPort)
2604  {
2605  const char *psdisp;
2606  int displen;
2607 
2608  psdisp = get_ps_display(&displen);
2609  if (padding != 0)
2610  appendStringInfo(buf, "%*s", padding, psdisp);
2611  else
2612  appendBinaryStringInfo(buf, psdisp, displen);
2613 
2614  }
2615  else if (padding != 0)
2617  padding > 0 ? padding : -padding);
2618  break;
2619  case 'r':
2621  {
2622  if (padding != 0)
2623  {
2624  if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0')
2625  {
2626  /*
2627  * This option is slightly special as the port
2628  * number may be appended onto the end. Here we
2629  * need to build 1 string which contains the
2630  * remote_host and optionally the remote_port (if
2631  * set) so we can properly align the string.
2632  */
2633 
2634  char *hostport;
2635 
2636  hostport = psprintf("%s(%s)", MyProcPort->remote_host, MyProcPort->remote_port);
2637  appendStringInfo(buf, "%*s", padding, hostport);
2638  pfree(hostport);
2639  }
2640  else
2641  appendStringInfo(buf, "%*s", padding, MyProcPort->remote_host);
2642  }
2643  else
2644  {
2645  /* padding is 0, so we don't need a temp buffer */
2647  if (MyProcPort->remote_port &&
2648  MyProcPort->remote_port[0] != '\0')
2649  appendStringInfo(buf, "(%s)",
2651  }
2652 
2653  }
2654  else if (padding != 0)
2656  padding > 0 ? padding : -padding);
2657  break;
2658  case 'h':
2660  {
2661  if (padding != 0)
2662  appendStringInfo(buf, "%*s", padding, MyProcPort->remote_host);
2663  else
2665  }
2666  else if (padding != 0)
2668  padding > 0 ? padding : -padding);
2669  break;
2670  case 'q':
2671  /* in postmaster and friends, stop if %q is seen */
2672  /* in a backend, just ignore */
2673  if (MyProcPort == NULL)
2674  return;
2675  break;
2676  case 'v':
2677  /* keep VXID format in sync with lockfuncs.c */
2678  if (MyProc != NULL && MyProc->backendId != InvalidBackendId)
2679  {
2680  if (padding != 0)
2681  {
2682  char strfbuf[128];
2683 
2684  snprintf(strfbuf, sizeof(strfbuf) - 1, "%d/%u",
2686  appendStringInfo(buf, "%*s", padding, strfbuf);
2687  }
2688  else
2689  appendStringInfo(buf, "%d/%u", MyProc->backendId, MyProc->lxid);
2690  }
2691  else if (padding != 0)
2693  padding > 0 ? padding : -padding);
2694  break;
2695  case 'x':
2696  if (padding != 0)
2697  appendStringInfo(buf, "%*u", padding, GetTopTransactionIdIfAny());
2698  else
2700  break;
2701  case 'e':
2702  if (padding != 0)
2703  appendStringInfo(buf, "%*s", padding, unpack_sql_state(edata->sqlerrcode));
2704  else
2706  break;
2707  default:
2708  /* format error - ignore it */
2709  break;
2710  }
2711  }
2712 }
2713 
2714 /*
2715  * append a CSV'd version of a string to a StringInfo
2716  * We use the PostgreSQL defaults for CSV, i.e. quote = escape = '"'
2717  * If it's NULL, append nothing.
2718  */
2719 static inline void
2720 appendCSVLiteral(StringInfo buf, const char *data)
2721 {
2722  const char *p = data;
2723  char c;
2724 
2725  /* avoid confusing an empty string with NULL */
2726  if (p == NULL)
2727  return;
2728 
2729  appendStringInfoCharMacro(buf, '"');
2730  while ((c = *p++) != '\0')
2731  {
2732  if (c == '"')
2733  appendStringInfoCharMacro(buf, '"');
2734  appendStringInfoCharMacro(buf, c);
2735  }
2736  appendStringInfoCharMacro(buf, '"');
2737 }
2738 
2739 /*
2740  * Constructs the error message, depending on the Errordata it gets, in a CSV
2741  * format which is described in doc/src/sgml/config.sgml.
2742  */
2743 static void
2745 {
2747  bool print_stmt = false;
2748 
2749  /* static counter for line numbers */
2750  static long log_line_number = 0;
2751 
2752  /* has counter been reset in current process? */
2753  static int log_my_pid = 0;
2754 
2755  /*
2756  * This is one of the few places where we'd rather not inherit a static
2757  * variable's value from the postmaster. But since we will, reset it when
2758  * MyProcPid changes.
2759  */
2760  if (log_my_pid != MyProcPid)
2761  {
2762  log_line_number = 0;
2763  log_my_pid = MyProcPid;
2764  formatted_start_time[0] = '\0';
2765  }
2766  log_line_number++;
2767 
2768  initStringInfo(&buf);
2769 
2770  /*
2771  * timestamp with milliseconds
2772  *
2773  * Check if the timestamp is already calculated for the syslog message,
2774  * and use it if so. Otherwise, get the current timestamp. This is done
2775  * to put same timestamp in both syslog and csvlog messages.
2776  */
2777  if (formatted_log_time[0] == '\0')
2779 
2781  appendStringInfoChar(&buf, ',');
2782 
2783  /* username */
2784  if (MyProcPort)
2786  appendStringInfoChar(&buf, ',');
2787 
2788  /* database name */
2789  if (MyProcPort)
2791  appendStringInfoChar(&buf, ',');
2792 
2793  /* Process id */
2794  if (MyProcPid != 0)
2795  appendStringInfo(&buf, "%d", MyProcPid);
2796  appendStringInfoChar(&buf, ',');
2797 
2798  /* Remote host and port */
2800  {
2801  appendStringInfoChar(&buf, '"');
2803  if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0')
2804  {
2805  appendStringInfoChar(&buf, ':');
2807  }
2808  appendStringInfoChar(&buf, '"');
2809  }
2810  appendStringInfoChar(&buf, ',');
2811 
2812  /* session id */
2813  appendStringInfo(&buf, "%lx.%x", (long) MyStartTime, MyProcPid);
2814  appendStringInfoChar(&buf, ',');
2815 
2816  /* Line number */
2817  appendStringInfo(&buf, "%ld", log_line_number);
2818  appendStringInfoChar(&buf, ',');
2819 
2820  /* PS display */
2821  if (MyProcPort)
2822  {
2823  StringInfoData msgbuf;
2824  const char *psdisp;
2825  int displen;
2826 
2827  initStringInfo(&msgbuf);
2828 
2829  psdisp = get_ps_display(&displen);
2830  appendBinaryStringInfo(&msgbuf, psdisp, displen);
2831  appendCSVLiteral(&buf, msgbuf.data);
2832 
2833  pfree(msgbuf.data);
2834  }
2835  appendStringInfoChar(&buf, ',');
2836 
2837  /* session start timestamp */
2838  if (formatted_start_time[0] == '\0')
2841  appendStringInfoChar(&buf, ',');
2842 
2843  /* Virtual transaction id */
2844  /* keep VXID format in sync with lockfuncs.c */
2845  if (MyProc != NULL && MyProc->backendId != InvalidBackendId)
2846  appendStringInfo(&buf, "%d/%u", MyProc->backendId, MyProc->lxid);
2847  appendStringInfoChar(&buf, ',');
2848 
2849  /* Transaction id */
2851  appendStringInfoChar(&buf, ',');
2852 
2853  /* Error severity */
2855  appendStringInfoChar(&buf, ',');
2856 
2857  /* SQL state code */
2859  appendStringInfoChar(&buf, ',');
2860 
2861  /* errmessage */
2862  appendCSVLiteral(&buf, edata->message);
2863  appendStringInfoChar(&buf, ',');
2864 
2865  /* errdetail or errdetail_log */
2866  if (edata->detail_log)
2867  appendCSVLiteral(&buf, edata->detail_log);
2868  else
2869  appendCSVLiteral(&buf, edata->detail);
2870  appendStringInfoChar(&buf, ',');
2871 
2872  /* errhint */
2873  appendCSVLiteral(&buf, edata->hint);
2874  appendStringInfoChar(&buf, ',');
2875 
2876  /* internal query */
2877  appendCSVLiteral(&buf, edata->internalquery);
2878  appendStringInfoChar(&buf, ',');
2879 
2880  /* if printed internal query, print internal pos too */
2881  if (edata->internalpos > 0 && edata->internalquery != NULL)
2882  appendStringInfo(&buf, "%d", edata->internalpos);
2883  appendStringInfoChar(&buf, ',');
2884 
2885  /* errcontext */
2886  if (!edata->hide_ctx)
2887  appendCSVLiteral(&buf, edata->context);
2888  appendStringInfoChar(&buf, ',');
2889 
2890  /* user query --- only reported if not disabled by the caller */
2892  debug_query_string != NULL &&
2893  !edata->hide_stmt)
2894  print_stmt = true;
2895  if (print_stmt)
2897  appendStringInfoChar(&buf, ',');
2898  if (print_stmt && edata->cursorpos > 0)
2899  appendStringInfo(&buf, "%d", edata->cursorpos);
2900  appendStringInfoChar(&buf, ',');
2901 
2902  /* file error location */
2904  {
2905  StringInfoData msgbuf;
2906 
2907  initStringInfo(&msgbuf);
2908 
2909  if (edata->funcname && edata->filename)
2910  appendStringInfo(&msgbuf, "%s, %s:%d",
2911  edata->funcname, edata->filename,
2912  edata->lineno);
2913  else if (edata->filename)
2914  appendStringInfo(&msgbuf, "%s:%d",
2915  edata->filename, edata->lineno);
2916  appendCSVLiteral(&buf, msgbuf.data);
2917  pfree(msgbuf.data);
2918  }
2919  appendStringInfoChar(&buf, ',');
2920 
2921  /* application name */
2922  if (application_name)
2924 
2925  appendStringInfoChar(&buf, '\n');
2926 
2927  /* If in the syslogger process, try to write messages direct to file */
2928  if (am_syslogger)
2930  else
2932 
2933  pfree(buf.data);
2934 }
2935 
2936 /*
2937  * Unpack MAKE_SQLSTATE code. Note that this returns a pointer to a
2938  * static buffer.
2939  */
2940 char *
2941 unpack_sql_state(int sql_state)
2942 {
2943  static char buf[12];
2944  int i;
2945 
2946  for (i = 0; i < 5; i++)
2947  {
2948  buf[i] = PGUNSIXBIT(sql_state);
2949  sql_state >>= 6;
2950  }
2951 
2952  buf[i] = '\0';
2953  return buf;
2954 }
2955 
2956 
2957 /*
2958  * Write error report to server's log
2959  */
2960 static void
2962 {
2964 
2965  initStringInfo(&buf);
2966 
2967  saved_timeval_set = false;
2968  formatted_log_time[0] = '\0';
2969 
2970  log_line_prefix(&buf, edata);
2971  appendStringInfo(&buf, "%s: ", _(error_severity(edata->elevel)));
2972 
2974  appendStringInfo(&buf, "%s: ", unpack_sql_state(edata->sqlerrcode));
2975 
2976  if (edata->message)
2977  append_with_tabs(&buf, edata->message);
2978  else
2979  append_with_tabs(&buf, _("missing error text"));
2980 
2981  if (edata->cursorpos > 0)
2982  appendStringInfo(&buf, _(" at character %d"),
2983  edata->cursorpos);
2984  else if (edata->internalpos > 0)
2985  appendStringInfo(&buf, _(" at character %d"),
2986  edata->internalpos);
2987 
2988  appendStringInfoChar(&buf, '\n');
2989 
2991  {
2992  if (edata->detail_log)
2993  {
2994  log_line_prefix(&buf, edata);
2995  appendStringInfoString(&buf, _("DETAIL: "));
2996  append_with_tabs(&buf, edata->detail_log);
2997  appendStringInfoChar(&buf, '\n');
2998  }
2999  else if (edata->detail)
3000  {
3001  log_line_prefix(&buf, edata);
3002  appendStringInfoString(&buf, _("DETAIL: "));
3003  append_with_tabs(&buf, edata->detail);
3004  appendStringInfoChar(&buf, '\n');
3005  }
3006  if (edata->hint)
3007  {
3008  log_line_prefix(&buf, edata);
3009  appendStringInfoString(&buf, _("HINT: "));
3010  append_with_tabs(&buf, edata->hint);
3011  appendStringInfoChar(&buf, '\n');
3012  }
3013  if (edata->internalquery)
3014  {
3015  log_line_prefix(&buf, edata);
3016  appendStringInfoString(&buf, _("QUERY: "));
3017  append_with_tabs(&buf, edata->internalquery);
3018  appendStringInfoChar(&buf, '\n');
3019  }
3020  if (edata->context && !edata->hide_ctx)
3021  {
3022  log_line_prefix(&buf, edata);
3023  appendStringInfoString(&buf, _("CONTEXT: "));
3024  append_with_tabs(&buf, edata->context);
3025  appendStringInfoChar(&buf, '\n');
3026  }
3027  if (edata->backtrace)
3028  {
3029  log_line_prefix(&buf, edata);
3030  appendStringInfoString(&buf, _("BACKTRACE: "));
3031  append_with_tabs(&buf, edata->backtrace);
3032  appendStringInfoChar(&buf, '\n');
3033  }
3035  {
3036  /* assume no newlines in funcname or filename... */
3037  if (edata->funcname && edata->filename)
3038  {
3039  log_line_prefix(&buf, edata);
3040  appendStringInfo(&buf, _("LOCATION: %s, %s:%d\n"),
3041  edata->funcname, edata->filename,
3042  edata->lineno);
3043  }
3044  else if (edata->filename)
3045  {
3046  log_line_prefix(&buf, edata);
3047  appendStringInfo(&buf, _("LOCATION: %s:%d\n"),
3048  edata->filename, edata->lineno);
3049  }
3050  }
3051  }
3052 
3053  /*
3054  * If the user wants the query that generated this error logged, do it.
3055  */
3057  debug_query_string != NULL &&
3058  !edata->hide_stmt)
3059  {
3060  log_line_prefix(&buf, edata);
3061  appendStringInfoString(&buf, _("STATEMENT: "));
3063  appendStringInfoChar(&buf, '\n');
3064  }
3065 
3066 #ifdef HAVE_SYSLOG
3067  /* Write to syslog, if enabled */
3069  {
3070  int syslog_level;
3071 
3072  switch (edata->elevel)
3073  {
3074  case DEBUG5:
3075  case DEBUG4:
3076  case DEBUG3:
3077  case DEBUG2:
3078  case DEBUG1:
3079  syslog_level = LOG_DEBUG;
3080  break;
3081  case LOG:
3082  case LOG_SERVER_ONLY:
3083  case INFO:
3084  syslog_level = LOG_INFO;
3085  break;
3086  case NOTICE:
3087  case WARNING:
3088  syslog_level = LOG_NOTICE;
3089  break;
3090  case ERROR:
3091  syslog_level = LOG_WARNING;
3092  break;
3093  case FATAL:
3094  syslog_level = LOG_ERR;
3095  break;
3096  case PANIC:
3097  default:
3098  syslog_level = LOG_CRIT;
3099  break;
3100  }
3101 
3102  write_syslog(syslog_level, buf.data);
3103  }
3104 #endif /* HAVE_SYSLOG */
3105 
3106 #ifdef WIN32
3107  /* Write to eventlog, if enabled */
3109  {
3110  write_eventlog(edata->elevel, buf.data, buf.len);
3111  }
3112 #endif /* WIN32 */
3113 
3114  /* Write to stderr, if enabled */
3116  {
3117  /*
3118  * Use the chunking protocol if we know the syslogger should be
3119  * catching stderr output, and we are not ourselves the syslogger.
3120  * Otherwise, just do a vanilla write to stderr.
3121  */
3123  write_pipe_chunks(buf.data, buf.len, LOG_DESTINATION_STDERR);
3124 #ifdef WIN32
3125 
3126  /*
3127  * In a win32 service environment, there is no usable stderr. Capture
3128  * anything going there and write it to the eventlog instead.
3129  *
3130  * If stderr redirection is active, it was OK to write to stderr above
3131  * because that's really a pipe to the syslogger process.
3132  */
3133  else if (pgwin32_is_service())
3134  write_eventlog(edata->elevel, buf.data, buf.len);
3135 #endif
3136  else
3137  write_console(buf.data, buf.len);
3138  }
3139 
3140  /* If in the syslogger process, try to write messages direct to file */
3141  if (am_syslogger)
3142  write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_STDERR);
3143 
3144  /* Write to CSV log if enabled */
3146  {
3148  {
3149  /*
3150  * send CSV data if it's safe to do so (syslogger doesn't need the
3151  * pipe). First get back the space in the message buffer.
3152  */
3153  pfree(buf.data);
3154  write_csvlog(edata);
3155  }
3156  else
3157  {
3158  /*
3159  * syslogger not up (yet), so just dump the message to stderr,
3160  * unless we already did so above.
3161  */
3162  if (!(Log_destination & LOG_DESTINATION_STDERR) &&
3164  write_console(buf.data, buf.len);
3165  pfree(buf.data);
3166  }
3167  }
3168  else
3169  {
3170  pfree(buf.data);
3171  }
3172 }
3173 
3174 /*
3175  * Send data to the syslogger using the chunked protocol
3176  *
3177  * Note: when there are multiple backends writing into the syslogger pipe,
3178  * it's critical that each write go into the pipe indivisibly, and not
3179  * get interleaved with data from other processes. Fortunately, the POSIX
3180  * spec requires that writes to pipes be atomic so long as they are not
3181  * more than PIPE_BUF bytes long. So we divide long messages into chunks
3182  * that are no more than that length, and send one chunk per write() call.
3183  * The collector process knows how to reassemble the chunks.
3184  *
3185  * Because of the atomic write requirement, there are only two possible
3186  * results from write() here: -1 for failure, or the requested number of
3187  * bytes. There is not really anything we can do about a failure; retry would
3188  * probably be an infinite loop, and we can't even report the error usefully.
3189  * (There is noplace else we could send it!) So we might as well just ignore
3190  * the result from write(). However, on some platforms you get a compiler
3191  * warning from ignoring write()'s result, so do a little dance with casting
3192  * rc to void to shut up the compiler.
3193  */
3194 static void
3195 write_pipe_chunks(char *data, int len, int dest)
3196 {
3197  PipeProtoChunk p;
3198  int fd = fileno(stderr);
3199  int rc;
3200 
3201  Assert(len > 0);
3202 
3203  p.proto.nuls[0] = p.proto.nuls[1] = '\0';
3204  p.proto.pid = MyProcPid;
3205 
3206  /* write all but the last chunk */
3207  while (len > PIPE_MAX_PAYLOAD)
3208  {
3209  p.proto.is_last = (dest == LOG_DESTINATION_CSVLOG ? 'F' : 'f');
3211  memcpy(p.proto.data, data, PIPE_MAX_PAYLOAD);
3212  rc = write(fd, &p, PIPE_HEADER_SIZE + PIPE_MAX_PAYLOAD);
3213  (void) rc;
3214  data += PIPE_MAX_PAYLOAD;
3215  len -= PIPE_MAX_PAYLOAD;
3216  }
3217 
3218  /* write the last chunk */
3219  p.proto.is_last = (dest == LOG_DESTINATION_CSVLOG ? 'T' : 't');
3220  p.proto.len = len;
3221  memcpy(p.proto.data, data, len);
3222  rc = write(fd, &p, PIPE_HEADER_SIZE + len);
3223  (void) rc;
3224 }
3225 
3226 
3227 /*
3228  * Append a text string to the error report being built for the client.
3229  *
3230  * This is ordinarily identical to pq_sendstring(), but if we are in
3231  * error recursion trouble we skip encoding conversion, because of the
3232  * possibility that the problem is a failure in the encoding conversion
3233  * subsystem itself. Code elsewhere should ensure that the passed-in
3234  * strings will be plain 7-bit ASCII, and thus not in need of conversion,
3235  * in such cases. (In particular, we disable localization of error messages
3236  * to help ensure that's true.)
3237  */
3238 static void
3240 {
3242  pq_send_ascii_string(buf, str);
3243  else
3244  pq_sendstring(buf, str);
3245 }
3246 
3247 /*
3248  * Write error report to client
3249  */
3250 static void
3252 {
3253  StringInfoData msgbuf;
3254 
3255  /* 'N' (Notice) is for nonfatal conditions, 'E' is for errors */
3256  pq_beginmessage(&msgbuf, (edata->elevel < ERROR) ? 'N' : 'E');
3257 
3259  {
3260  /* New style with separate fields */
3261  const char *sev;
3262  char tbuf[12];
3263  int ssval;
3264  int i;
3265 
3266  sev = error_severity(edata->elevel);
3267  pq_sendbyte(&msgbuf, PG_DIAG_SEVERITY);
3268  err_sendstring(&msgbuf, _(sev));
3270  err_sendstring(&msgbuf, sev);
3271 
3272  /* unpack MAKE_SQLSTATE code */
3273  ssval = edata->sqlerrcode;
3274  for (i = 0; i < 5; i++)
3275  {
3276  tbuf[i] = PGUNSIXBIT(ssval);
3277  ssval >>= 6;
3278  }
3279  tbuf[i] = '\0';
3280 
3281  pq_sendbyte(&msgbuf, PG_DIAG_SQLSTATE);
3282  err_sendstring(&msgbuf, tbuf);
3283 
3284  /* M field is required per protocol, so always send something */
3286  if (edata->message)
3287  err_sendstring(&msgbuf, edata->message);
3288  else
3289  err_sendstring(&msgbuf, _("missing error text"));
3290 
3291  if (edata->detail)
3292  {
3294  err_sendstring(&msgbuf, edata->detail);
3295  }
3296 
3297  /* detail_log is intentionally not used here */
3298 
3299  if (edata->hint)
3300  {
3302  err_sendstring(&msgbuf, edata->hint);
3303  }
3304 
3305  if (edata->context)
3306  {
3307  pq_sendbyte(&msgbuf, PG_DIAG_CONTEXT);
3308  err_sendstring(&msgbuf, edata->context);
3309  }
3310 
3311  if (edata->schema_name)
3312  {
3313  pq_sendbyte(&msgbuf, PG_DIAG_SCHEMA_NAME);
3314  err_sendstring(&msgbuf, edata->schema_name);
3315  }
3316 
3317  if (edata->table_name)
3318  {
3319  pq_sendbyte(&msgbuf, PG_DIAG_TABLE_NAME);
3320  err_sendstring(&msgbuf, edata->table_name);
3321  }
3322 
3323  if (edata->column_name)
3324  {
3325  pq_sendbyte(&msgbuf, PG_DIAG_COLUMN_NAME);
3326  err_sendstring(&msgbuf, edata->column_name);
3327  }
3328 
3329  if (edata->datatype_name)
3330  {
3332  err_sendstring(&msgbuf, edata->datatype_name);
3333  }
3334 
3335  if (edata->constraint_name)
3336  {
3338  err_sendstring(&msgbuf, edata->constraint_name);
3339  }
3340 
3341  if (edata->cursorpos > 0)
3342  {
3343  snprintf(tbuf, sizeof(tbuf), "%d", edata->cursorpos);
3345  err_sendstring(&msgbuf, tbuf);
3346  }
3347 
3348  if (edata->internalpos > 0)
3349  {
3350  snprintf(tbuf, sizeof(tbuf), "%d", edata->internalpos);
3352  err_sendstring(&msgbuf, tbuf);
3353  }
3354 
3355  if (edata->internalquery)
3356  {
3358  err_sendstring(&msgbuf, edata->internalquery);
3359  }
3360 
3361  if (edata->filename)
3362  {
3363  pq_sendbyte(&msgbuf, PG_DIAG_SOURCE_FILE);
3364  err_sendstring(&msgbuf, edata->filename);
3365  }
3366 
3367  if (edata->lineno > 0)
3368  {
3369  snprintf(tbuf, sizeof(tbuf), "%d", edata->lineno);
3370  pq_sendbyte(&msgbuf, PG_DIAG_SOURCE_LINE);
3371  err_sendstring(&msgbuf, tbuf);
3372  }
3373 
3374  if (edata->funcname)
3375  {
3377  err_sendstring(&msgbuf, edata->funcname);
3378  }
3379 
3380  pq_sendbyte(&msgbuf, '\0'); /* terminator */
3381  }
3382  else
3383  {
3384  /* Old style --- gin up a backwards-compatible message */
3386 
3387  initStringInfo(&buf);
3388 
3389  appendStringInfo(&buf, "%s: ", _(error_severity(edata->elevel)));
3390 
3391  if (edata->show_funcname && edata->funcname)
3392  appendStringInfo(&buf, "%s: ", edata->funcname);
3393 
3394  if (edata->message)
3395  appendStringInfoString(&buf, edata->message);
3396  else
3397  appendStringInfoString(&buf, _("missing error text"));
3398 
3399  if (edata->cursorpos > 0)
3400  appendStringInfo(&buf, _(" at character %d"),
3401  edata->cursorpos);
3402  else if (edata->internalpos > 0)
3403  appendStringInfo(&buf, _(" at character %d"),
3404  edata->internalpos);
3405 
3406  appendStringInfoChar(&buf, '\n');
3407 
3408  err_sendstring(&msgbuf, buf.data);
3409 
3410  pfree(buf.data);
3411  }
3412 
3413  pq_endmessage(&msgbuf);
3414 
3415  /*
3416  * This flush is normally not necessary, since postgres.c will flush out
3417  * waiting data when control returns to the main loop. But it seems best
3418  * to leave it here, so that the client has some clue what happened if the
3419  * backend dies before getting back to the main loop ... error/notice
3420  * messages should not be a performance-critical path anyway, so an extra
3421  * flush won't hurt much ...
3422  */
3423  pq_flush();
3424 }
3425 
3426 
3427 /*
3428  * Support routines for formatting error messages.
3429  */
3430 
3431 
3432 /*
3433  * error_severity --- get string representing elevel
3434  *
3435  * The string is not localized here, but we mark the strings for translation
3436  * so that callers can invoke _() on the result.
3437  */
3438 static const char *
3440 {
3441  const char *prefix;
3442 
3443  switch (elevel)
3444  {
3445  case DEBUG1:
3446  case DEBUG2:
3447  case DEBUG3:
3448  case DEBUG4:
3449  case DEBUG5:
3450  prefix = gettext_noop("DEBUG");
3451  break;
3452  case LOG:
3453  case LOG_SERVER_ONLY:
3454  prefix = gettext_noop("LOG");
3455  break;
3456  case INFO:
3457  prefix = gettext_noop("INFO");
3458  break;
3459  case NOTICE:
3460  prefix = gettext_noop("NOTICE");
3461  break;
3462  case WARNING:
3463  prefix = gettext_noop("WARNING");
3464  break;
3465  case ERROR:
3466  prefix = gettext_noop("ERROR");
3467  break;
3468  case FATAL:
3469  prefix = gettext_noop("FATAL");
3470  break;
3471  case PANIC:
3472  prefix = gettext_noop("PANIC");
3473  break;
3474  default:
3475  prefix = "???";
3476  break;
3477  }
3478 
3479  return prefix;
3480 }
3481 
3482 
3483 /*
3484  * append_with_tabs
3485  *
3486  * Append the string to the StringInfo buffer, inserting a tab after any
3487  * newline.
3488  */
3489 static void
3491 {
3492  char ch;
3493 
3494  while ((ch = *str++) != '\0')
3495  {
3496  appendStringInfoCharMacro(buf, ch);
3497  if (ch == '\n')
3498  appendStringInfoCharMacro(buf, '\t');
3499  }
3500 }
3501 
3502 
3503 /*
3504  * Write errors to stderr (or by equal means when stderr is
3505  * not available). Used before ereport/elog can be used
3506  * safely (memory context, GUC load etc)
3507  */
3508 void
3509 write_stderr(const char *fmt,...)
3510 {
3511  va_list ap;
3512 
3513 #ifdef WIN32
3514  char errbuf[2048]; /* Arbitrary size? */
3515 #endif
3516 
3517  fmt = _(fmt);
3518 
3519  va_start(ap, fmt);
3520 #ifndef WIN32
3521  /* On Unix, we just fprintf to stderr */
3522  vfprintf(stderr, fmt, ap);
3523  fflush(stderr);
3524 #else
3525  vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
3526 
3527  /*
3528  * On Win32, we print to stderr if running on a console, or write to
3529  * eventlog if running as a service
3530  */
3531  if (pgwin32_is_service()) /* Running as a service */
3532  {
3533  write_eventlog(ERROR, errbuf, strlen(errbuf));
3534  }
3535  else
3536  {
3537  /* Not running as service, write to stderr */
3538  write_console(errbuf, strlen(errbuf));
3539  fflush(stderr);
3540  }
3541 #endif
3542  va_end(ap);
3543 }
3544 
3545 
3546 /*
3547  * is_log_level_output -- is elevel logically >= log_min_level?
3548  *
3549  * We use this for tests that should consider LOG to sort out-of-order,
3550  * between ERROR and FATAL. Generally this is the right thing for testing
3551  * whether a message should go to the postmaster log, whereas a simple >=
3552  * test is correct for testing whether the message should go to the client.
3553  */
3554 static bool
3555 is_log_level_output(int elevel, int log_min_level)
3556 {
3557  if (elevel == LOG || elevel == LOG_SERVER_ONLY)
3558  {
3559  if (log_min_level == LOG || log_min_level <= ERROR)
3560  return true;
3561  }
3562  else if (log_min_level == LOG)
3563  {
3564  /* elevel != LOG */
3565  if (elevel >= FATAL)
3566  return true;
3567  }
3568  /* Neither is LOG */
3569  else if (elevel >= log_min_level)
3570  return true;
3571 
3572  return false;
3573 }
3574 
3575 /*
3576  * Adjust the level of a recovery-related message per trace_recovery_messages.
3577  *
3578  * The argument is the default log level of the message, eg, DEBUG2. (This
3579  * should only be applied to DEBUGn log messages, otherwise it's a no-op.)
3580  * If the level is >= trace_recovery_messages, we return LOG, causing the
3581  * message to be logged unconditionally (for most settings of
3582  * log_min_messages). Otherwise, we return the argument unchanged.
3583  * The message will then be shown based on the setting of log_min_messages.
3584  *
3585  * Intention is to keep this for at least the whole of the 9.0 production
3586  * release, so we can more easily diagnose production problems in the field.
3587  * It should go away eventually, though, because it's an ugly and
3588  * hard-to-explain kluge.
3589  */
3590 int
3591 trace_recovery(int trace_level)
3592 {
3593  if (trace_level < LOG &&
3594  trace_level >= trace_recovery_messages)
3595  return LOG;
3596 
3597  return trace_level;
3598 }
#define PIPE_HEADER_SIZE
Definition: syslogger.h:60
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:122
char * schema_name
Definition: elog.h:399
bool IsPostmasterEnvironment
Definition: globals.c:108
volatile uint32 InterruptHoldoffCount
Definition: globals.c:36
bool hide_stmt
Definition: elog.h:384
#define CHECK_STACK_DEPTH()
Definition: elog.c:162
static const char * err_gettext(const char *str) pg_attribute_format_arg(1)
Definition: elog.c:209
void write_syslogger_file(const char *buffer, int count, int destination)
Definition: syslogger.c:1080
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
int log_min_error_statement
Definition: guc.c:511
#define LOG_SERVER_ONLY
Definition: elog.h:28
char nuls[2]
Definition: syslogger.h:46
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:40
int errhint(const char *fmt,...)
Definition: elog.c:1071
int64 pg_time_t
Definition: pgtime.h:23
#define PG_DIAG_MESSAGE_PRIMARY
Definition: postgres_ext.h:58
#define PG_DIAG_SCHEMA_NAME
Definition: postgres_ext.h:65
#define pq_flush()
Definition: libpq.h:39
struct Port * MyProcPort
Definition: globals.c:43
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: syslogger.h:51
static struct timeval saved_timeval
Definition: elog.c:153
BackendId backendId
Definition: proc.h:113
MemoryContext ErrorContext
Definition: mcxt.c:45
int sqlerrcode
Definition: elog.h:391
#define DEBUG3
Definition: elog.h:23
#define PG_DIAG_MESSAGE_DETAIL
Definition: postgres_ext.h:59
static int save_format_errnumber
Definition: elog.c:1489
#define LOG_DESTINATION_EVENTLOG
Definition: elog.h:447
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:934
#define write(a, b, c)
Definition: win32.h:14
ErrorData * CopyErrorData(void)
Definition: elog.c:1586
#define FORMATTED_TS_LEN
Definition: elog.c:156
static pg_noinline void set_backtrace(ErrorData *edata, int num_skip)
Definition: elog.c:870
PGPROC * MyProc
Definition: proc.c:67
#define PG_DIAG_COLUMN_NAME
Definition: postgres_ext.h:67
const char * funcname
Definition: elog.h:388
#define PGUNSIXBIT(val)
Definition: elog.h:60
char * pstrdup(const char *in)
Definition: mcxt.c:1186
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PG_DIAG_TABLE_NAME
Definition: postgres_ext.h:66
#define PIPE_MAX_PAYLOAD
Definition: syslogger.h:61
#define ERRORDATA_STACK_SIZE
Definition: elog.c:141
char * unpack_sql_state(int sql_state)
Definition: elog.c:2941
pg_time_t MyStartTime
Definition: globals.c:41
#define Min(x, y)
Definition: c.h:905
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int lineno
Definition: elog.h:387
volatile uint32 QueryCancelHoldoffCount
Definition: globals.c:37
#define gettext_noop(x)
Definition: c.h:1142
static char formatted_log_time[FORMATTED_TS_LEN]
Definition: elog.c:158
#define PG_DIAG_SOURCE_LINE
Definition: postgres_ext.h:71
void proc_exit(int code)
Definition: ipc.c:104
int errcode(int sqlerrcode)
Definition: elog.c:608
static int recursion_depth
Definition: elog.c:147
int geterrcode(void)
Definition: elog.c:1324
int errhidestmt(bool hide_stmt)
Definition: elog.c:1152
#define MemSet(start, val, len)
Definition: c.h:956
#define INFO
Definition: elog.h:33
char * event_source
Definition: guc.c:499
#define LOG_DESTINATION_SYSLOG
Definition: elog.h:446
bool syslog_sequence_numbers
Definition: elog.c:111
char * internalquery
Definition: elog.h:406
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
char * remote_port
Definition: libpq-be.h:132
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
char * GetErrorContextStack(void)
Definition: elog.c:1888
void pq_send_ascii_string(StringInfo buf, const char *str)
Definition: pqformat.c:229
#define lengthof(array)
Definition: c.h:663
#define LOG
Definition: elog.h:26
#define PG_DIAG_INTERNAL_POSITION
Definition: postgres_ext.h:62
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:57
#define DEBUG4
Definition: elog.h:22
void(* callback)(void *arg)
Definition: elog.h:256
static void setup_formatted_start_time(void)
Definition: elog.c:2357
struct ErrorContextCallback * previous
Definition: elog.h:255
#define PANIC
Definition: elog.h:53
#define EVALUATE_MESSAGE(domain, targetfield, appendval, translateit)
Definition: elog.c:741
static int fd(const char *x, int i)
Definition: preproc-init.c:105
bool errstart(int elevel, const char *filename, int lineno, const char *funcname, const char *domain)
Definition: elog.c:234
void FlushErrorState(void)
Definition: elog.c:1680
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:87
int errdetail_internal(const char *fmt,...)
Definition: elog.c:984
int trace_recovery(int trace_level)
Definition: elog.c:3591
char * Log_line_prefix
Definition: elog.c:108
pg_tz * log_timezone
Definition: pgtz.c:31
PipeProtoHeader proto
Definition: syslogger.h:56
void ExceptionalCondition(const char *conditionName, const char *errorType, const char *fileName, int lineNumber)
Definition: assert.c:29
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
int cursorpos
Definition: elog.h:404
ErrorContextCallback * error_context_stack
Definition: elog.c:91
static void send_message_to_server_log(ErrorData *edata)
Definition: elog.c:2961
#define pg_noinline
Definition: c.h:180
#define sprintf
Definition: port.h:194
bool ClientAuthInProgress
Definition: postmaster.c:352
const char * context_domain
Definition: elog.h:390
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:128
char * format_elog_string(const char *fmt,...)
Definition: elog.c:1502
void errfinish(int dummy,...)
Definition: elog.c:440
void pfree(void *pointer)
Definition: mcxt.c:1056
#define PG_DIAG_SOURCE_FILE
Definition: postgres_ext.h:70
static int errordata_stack_depth
Definition: elog.c:145
static void appendCSVLiteral(StringInfo buf, const char *data)
Definition: elog.c:2720
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void FreeErrorData(ErrorData *edata)
Definition: elog.c:1642
char * backtrace_symbol_list
Definition: guc.c:521
#define ERROR
Definition: elog.h:43
const char * filename
Definition: elog.h:386
char * detail_log
Definition: elog.h:394
static bool is_log_level_output(int elevel, int log_min_level)
Definition: elog.c:3555
#define vsnprintf
Definition: port.h:191
#define FATAL
Definition: elog.h:52
bool am_syslogger
Definition: syslogger.c:80
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:862
static void write_csvlog(ErrorData *edata)
Definition: elog.c:2744
static void append_with_tabs(StringInfo buf, const char *str)
Definition: elog.c:3490
void pg_re_throw(void)
Definition: elog.c:1826
#define DEBUG2
Definition: elog.h:24
int errbacktrace(void)
Definition: elog.c:844
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static void write_console(const char *line, int len)
Definition: elog.c:2254
Definition: dest.h:88
const char * get_ps_display(int *displen)
Definition: ps_status.c:412
char * table_name
Definition: elog.h:400
char * c
char OutputFileName[MAXPGPATH]
Definition: globals.c:70
static char * buf
Definition: pg_test_fsync.c:67
bool output_to_server
Definition: elog.h:381
bool IsUnderPostmaster
Definition: globals.c:109
int internalpos
Definition: elog.h:405
static bool saved_timeval_set
Definition: elog.c:154
#define gettext(x)
Definition: c.h:1126
int errdetail(const char *fmt,...)
Definition: elog.c:957
Definition: dest.h:89
int errcode_for_file_access(void)
Definition: elog.c:631
int saved_errno
Definition: elog.h:407
int geterrposition(void)
Definition: elog.c:1341
#define PG_DIAG_SEVERITY
Definition: postgres_ext.h:55
static bool matches_backtrace_functions(const char *funcname)
Definition: elog.c:410
static int syslog_facility
Definition: guc.c:166
char * user_name
Definition: libpq-be.h:141
int errdetail_log(const char *fmt,...)
Definition: elog.c:1005
#define EVALUATE_MESSAGE_PLURAL(domain, targetfield, appendval)
Definition: elog.c:777
bool hide_ctx
Definition: elog.h:385
char * Log_destination_string
Definition: elog.c:110
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:409
int pgwin32_is_service(void)
volatile uint32 CritSectionCount
Definition: globals.c:38
#define PG_DIAG_STATEMENT_POSITION
Definition: postgres_ext.h:61
#define ereport(elevel, rest)
Definition: elog.h:141
void DebugFileOpen(void)
Definition: elog.c:1955
#define pg_attribute_format_arg(a)
Definition: c.h:131
int getinternalerrposition(void)
Definition: elog.c:1358
int errcode_for_socket_access(void)
Definition: elog.c:702
void ThrowErrorData(ErrorData *edata)
Definition: elog.c:1705
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define WARNING
Definition: elog.h:40
const char * debug_query_string
Definition: postgres.c:86
static ErrorData errordata[ERRORDATA_STACK_SIZE]
Definition: elog.c:143
char * backtrace
Definition: elog.h:397
static void send_message_to_frontend(ErrorData *edata)
Definition: elog.c:3251
char * datatype_name
Definition: elog.h:402
static const char * process_log_prefix_padding(const char *p, int *padding)
Definition: elog.c:2379
int elevel
Definition: elog.h:380
static int elevel
Definition: vacuumlazy.c:143
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
char * detail
Definition: elog.h:393
bool ExitOnAnyError
Definition: globals.c:113
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1160
#define InvalidBackendId
Definition: backendid.h:23
static void set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str)
Definition: elog.c:1311
const char * message_id
Definition: elog.h:398
#define PG_DIAG_DATATYPE_NAME
Definition: postgres_ext.h:68
int set_errcontext_domain(const char *domain)
Definition: elog.c:1132
void EmitErrorReport(void)
Definition: elog.c:1535
int trace_recovery_messages
Definition: guc.c:519
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static const char * save_format_domain
Definition: elog.c:1490
#define PG_DIAG_CONSTRAINT_NAME
Definition: postgres_ext.h:69
bool show_funcname
Definition: elog.h:383
int errfunction(const char *funcname)
Definition: elog.c:1192
int errdetail_log_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1026
int log_min_messages
Definition: guc.c:512
void(* emit_log_hook_type)(ErrorData *edata)
Definition: elog.h:424
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:448
#define PG_DIAG_SEVERITY_NONLOCALIZED
Definition: postgres_ext.h:56
static char * username
Definition: initdb.c:133
#define PG_DIAG_INTERNAL_QUERY
Definition: postgres_ext.h:63
#define PG_DIAG_MESSAGE_HINT
Definition: postgres_ext.h:60
static void write_pipe_chunks(char *data, int len, int dest)
Definition: elog.c:3195
#define LOG_DESTINATION_STDERR
Definition: elog.h:445
char * column_name
Definition: elog.h:401
char * backtrace_functions
Definition: guc.c:520
int Log_error_verbosity
Definition: elog.c:107
int internalerrquery(const char *query)
Definition: elog.c:1245
#define NOTICE
Definition: elog.h:37
#define free(a)
Definition: header.h:65
bool in_error_recursion_trouble(void)
Definition: elog.c:197
bool redirection_done
Definition: postmaster.c:355
int errmsg_internal(const char *fmt,...)
Definition: elog.c:911
#define Max(x, y)
Definition: c.h:899
#define DEFAULT_EVENT_SOURCE
#define Assert(condition)
Definition: c.h:733
void write_stderr(const char *fmt,...)
Definition: elog.c:3509
bool proc_exit_inprogress
Definition: ipc.c:40
char * hint
Definition: elog.h:395
#define pq_endcopyout(errorAbort)
Definition: libpq.h:47
char * dbname
Definition: streamutil.c:50
bool syslog_split_messages
Definition: elog.c:112
int errcontext_msg(const char *fmt,...)
Definition: elog.c:1096
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1049
static void err_sendstring(StringInfo buf, const char *str)
Definition: elog.c:3239
#define ECONNRESET
Definition: win32_port.h:333
#define PG_RE_THROW()
Definition: elog.h:363
char * context
Definition: elog.h:396
int32 encoding
Definition: pg_database.h:41
struct MemoryContextData * assoc_context
Definition: elog.h:410
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1375
sigjmp_buf * PG_exception_stack
Definition: elog.c:93
int GetMessageEncoding(void)
Definition: mbutils.c:1076
emit_log_hook_type emit_log_hook
Definition: elog.c:104
static char * filename
Definition: pg_dumpall.c:90
char * application_name
Definition: guc.c:535
char * remote_host
Definition: libpq-be.h:127
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:298
#define elog(elevel,...)
Definition: elog.h:228
int i
static void setup_formatted_log_time(void)
Definition: elog.c:2325
int client_min_messages
Definition: guc.c:513
static const char * error_severity(int elevel)
Definition: elog.c:3439
int Log_destination
Definition: elog.c:109
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
void elog_start(const char *filename, int lineno, const char *funcname)
Definition: elog.c:1382
#define close(a)
Definition: win32.h:12
#define vfprintf
Definition: port.h:195
#define DEBUG5
Definition: elog.h:20
CommandDest whereToSendOutput
Definition: postgres.c:89
int err_generic_string(int field, const char *str)
Definition: elog.c:1275
char * database_name
Definition: libpq-be.h:140
const char * domain
Definition: elog.h:389
void pre_format_elog_string(int errnumber, const char *domain)
Definition: elog.c:1493
char * constraint_name
Definition: elog.h:403
#define snprintf
Definition: port.h:192
#define PG_DIAG_SOURCE_FUNCTION
Definition: postgres_ext.h:72
#define _(x)
Definition: elog.c:87
ProtocolVersion FrontendProtocol
Definition: globals.c:28
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227
bool output_to_client
Definition: elog.h:382
void ReThrowError(ErrorData *edata)
Definition: elog.c:1765
char * message
Definition: elog.h:392
int errhidecontext(bool hide_ctx)
Definition: elog.c:1171
int errposition(int cursorpos)
Definition: elog.c:1209
#define PG_DIAG_CONTEXT
Definition: postgres_ext.h:64
void elog_finish(int elevel, const char *fmt,...)
Definition: elog.c:1432
static void log_line_prefix(StringInfo buf, ErrorData *edata)
Definition: elog.c:2410
static char formatted_start_time[FORMATTED_TS_LEN]
Definition: elog.c:157
LocalTransactionId lxid
Definition: proc.h:106
int internalerrposition(int cursorpos)
Definition: elog.c:1225