65 #ifdef HAVE_EXECINFO_H
90 #define _(x) err_gettext(x)
127 #ifndef PG_SYSLOG_LIMIT
128 #define PG_SYSLOG_LIMIT 900
131 static bool openlog_done =
false;
132 static char *syslog_ident = NULL;
135 static void write_syslog(
int level,
const char *line);
139 static void write_eventlog(
int level,
const char *line,
int len);
143 #define ERRORDATA_STACK_SIZE 5
158 #define FORMATTED_TS_LEN 128
164 #define CHECK_STACK_DEPTH() \
166 if (errordata_stack_depth < 0) \
168 errordata_stack_depth = -1; \
169 ereport(ERROR, (errmsg_internal("errstart was not called"))); \
205 if (log_min_level ==
LOG || log_min_level <=
ERROR)
213 else if (log_min_level ==
LOG)
220 else if (elevel >= log_min_level)
255 return (elevel >=
ERROR);
277 if (elevel >=
ERROR ||
304 static inline const char *
345 bool output_to_server;
346 bool output_to_client =
false;
401 if (elevel <
ERROR && !output_to_server && !output_to_client)
411 write_stderr(
"error occurred before error message processing is available\n");
453 edata->
sqlerrcode = ERRCODE_SUCCESSFUL_COMPLETION;
891 edata->
sqlerrcode = ERRCODE_INSUFFICIENT_PRIVILEGE;
908 edata->
sqlerrcode = ERRCODE_WRONG_OBJECT_TYPE;
922 edata->
sqlerrcode = ERRCODE_INSUFFICIENT_RESOURCES;
931 edata->
sqlerrcode = ERRCODE_FILE_NAME_TOO_LONG;
964 edata->
sqlerrcode = ERRCODE_CONNECTION_FAILURE;
989 #define EVALUATE_MESSAGE(domain, targetfield, appendval, translateit) \
991 StringInfoData buf; \
993 if ((translateit) && !in_error_recursion_trouble()) \
994 fmt = dgettext((domain), fmt); \
995 initStringInfo(&buf); \
996 if ((appendval) && edata->targetfield) { \
997 appendStringInfoString(&buf, edata->targetfield); \
998 appendStringInfoChar(&buf, '\n'); \
1005 errno = edata->saved_errno; \
1006 va_start(args, fmt); \
1007 needed = appendStringInfoVA(&buf, fmt, args); \
1011 enlargeStringInfo(&buf, needed); \
1014 if (edata->targetfield) \
1015 pfree(edata->targetfield); \
1016 edata->targetfield = pstrdup(buf.data); \
1025 #define EVALUATE_MESSAGE_PLURAL(domain, targetfield, appendval) \
1028 StringInfoData buf; \
1030 if (!in_error_recursion_trouble()) \
1031 fmt = dngettext((domain), fmt_singular, fmt_plural, n); \
1033 fmt = (n == 1 ? fmt_singular : fmt_plural); \
1034 initStringInfo(&buf); \
1035 if ((appendval) && edata->targetfield) { \
1036 appendStringInfoString(&buf, edata->targetfield); \
1037 appendStringInfoChar(&buf, '\n'); \
1044 errno = edata->saved_errno; \
1045 va_start(args, n); \
1046 needed = appendStringInfoVA(&buf, fmt, args); \
1050 enlargeStringInfo(&buf, needed); \
1053 if (edata->targetfield) \
1054 pfree(edata->targetfield); \
1055 edata->targetfield = pstrdup(buf.data); \
1122 #ifdef HAVE_BACKTRACE_SYMBOLS
1129 strfrms = backtrace_symbols(
buf, nframes);
1130 if (strfrms == NULL)
1133 for (
int i = num_skip;
i < nframes;
i++)
1139 "backtrace generation is not supported by this installation");
1181 unsigned long n,...)
1273 unsigned long n,...)
1296 unsigned long n,...)
1340 unsigned long n,...)
1537 elog(
ERROR,
"unsupported ErrorData field id: %d", field);
1724 (*emit_log_hook) (edata);
1761 memcpy(newedata, edata,
sizeof(
ErrorData));
1962 memcpy(newedata, edata,
sizeof(
ErrorData));
2134 errmsg(
"could not reopen file \"%s\" as stderr: %m",
2147 errmsg(
"could not reopen file \"%s\" as stdout: %m",
2166 int newvallen = strlen(*
newval);
2176 validlen = strspn(*
newval,
2178 "abcdefghijklmnopqrstuvwxyz"
2179 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2181 if (validlen != newvallen)
2199 for (
i = 0,
j = 0;
i < newvallen;
i++)
2202 someval[
j++] =
'\0';
2203 else if ((*
newval)[
i] ==
' ' ||
2208 someval[
j++] = (*newval)[
i];
2213 someval[
j + 1] =
'\0';
2253 foreach(l, elemlist)
2255 char *tok = (
char *)
lfirst(l);
2284 *myextra = newlogdest;
2285 *extra = (
void *) myextra;
2316 if (syslog_ident == NULL || strcmp(syslog_ident,
newval) != 0)
2321 openlog_done =
false;
2324 syslog_ident = strdup(
newval);
2346 openlog_done =
false;
2360 write_syslog(
int level,
const char *line)
2362 static unsigned long seq = 0;
2370 openlog(syslog_ident ? syslog_ident :
"postgres",
2371 LOG_PID | LOG_NDELAY | LOG_NOWAIT,
2373 openlog_done =
true;
2391 nlpos = strchr(line,
'\n');
2398 char buf[PG_SYSLOG_LIMIT + 1];
2403 if (line[0] ==
'\n')
2408 nlpos = strchr(line,
'\n');
2414 buflen = nlpos - line;
2417 buflen =
Min(buflen, PG_SYSLOG_LIMIT);
2418 memcpy(
buf, line, buflen);
2428 if (line[buflen] !=
'\0' &&
2429 !isspace((
unsigned char) line[buflen]))
2433 while (
i > 0 && !isspace((
unsigned char)
buf[
i]))
2446 syslog(level,
"[%lu-%d] %s", seq, chunk_nr,
buf);
2448 syslog(level,
"[%d] %s", chunk_nr,
buf);
2458 syslog(level,
"[%lu] %s", seq, line);
2460 syslog(level,
"%s", line);
2472 GetACPEncoding(
void)
2477 encoding = pg_codepage_to_encoding(GetACP());
2486 write_eventlog(
int level,
const char *line,
int len)
2489 int eventlevel = EVENTLOG_ERROR_TYPE;
2490 static HANDLE evtHandle = INVALID_HANDLE_VALUE;
2492 if (evtHandle == INVALID_HANDLE_VALUE)
2494 evtHandle = RegisterEventSource(NULL,
2496 if (evtHandle == NULL)
2498 evtHandle = INVALID_HANDLE_VALUE;
2514 eventlevel = EVENTLOG_INFORMATION_TYPE;
2518 eventlevel = EVENTLOG_WARNING_TYPE;
2524 eventlevel = EVENTLOG_ERROR_TYPE;
2545 utf16 = pgwin32_message_to_UTF16(line,
len, NULL);
2548 ReportEventW(evtHandle,
2563 ReportEventA(evtHandle,
2606 utf16 = pgwin32_message_to_UTF16(line,
len, &utf16len);
2612 stdHandle = GetStdHandle(STD_ERROR_HANDLE);
2613 if (WriteConsoleW(stdHandle, utf16, utf16len, &written, NULL))
2641 rc =
write(fileno(stderr), line,
len);
2678 "%Y-%m-%d %H:%M:%S %Z",
2718 "%Y-%m-%d %H:%M:%S %Z",
2753 const char *backend_type_str;
2756 backend_type_str =
"postmaster";
2762 return backend_type_str;
2775 int paddingsign = 1;
2788 while (*p >=
'0' && *p <=
'9')
2789 padding = padding * 10 + (*p++ -
'0');
2795 padding *= paddingsign;
2796 *ppadding = padding;
2816 static long log_line_number = 0;
2819 static int log_my_pid = 0;
2831 log_line_number = 0;
2840 for (p =
format; *p !=
'\0'; p++)
2888 if (appname == NULL || *appname ==
'\0')
2889 appname =
_(
"[unknown]");
2895 else if (padding != 0)
2897 padding > 0 ? padding : -padding);
2922 else if (padding != 0)
2924 padding > 0 ? padding : -padding);
2938 else if (padding != 0)
2940 padding > 0 ? padding : -padding);
2947 snprintf(strfbuf,
sizeof(strfbuf) - 1,
"%lx.%x",
2972 padding > 0 ? padding : -padding);
2973 else if (padding != 0)
2978 else if (padding != 0)
2980 padding > 0 ? padding : -padding);
3005 "%Y-%m-%d %H:%M:%S %Z",
3023 snprintf(strfbuf,
sizeof(strfbuf),
"%ld.%03d",
3055 else if (padding != 0)
3057 padding > 0 ? padding : -padding);
3093 else if (padding != 0)
3095 padding > 0 ? padding : -padding);
3105 else if (padding != 0)
3107 padding > 0 ? padding : -padding);
3123 snprintf(strfbuf,
sizeof(strfbuf) - 1,
"%d/%u",
3130 else if (padding != 0)
3132 padding > 0 ? padding : -padding);
3168 static char buf[12];
3171 for (
i = 0;
i < 5;
i++)
3189 bool fallback_to_stderr =
false;
3300 syslog_level = LOG_DEBUG;
3305 syslog_level = LOG_INFO;
3310 syslog_level = LOG_NOTICE;
3313 syslog_level = LOG_WARNING;
3316 syslog_level = LOG_ERR;
3320 syslog_level = LOG_CRIT;
3324 write_syslog(syslog_level,
buf.data);
3347 fallback_to_stderr =
true;
3363 fallback_to_stderr =
true;
3429 int fd = fileno(stderr);
3723 while ((ch = *
str++) !=
'\0')
3762 write_eventlog(
ERROR, errbuf, strlen(errbuf));
void ExceptionalCondition(const char *conditionName, const char *fileName, int lineNumber)
uint64 pgstat_get_my_query_id(void)
#define pg_attribute_format_arg(a)
#define pg_attribute_cold
#define Assert(condition)
#define PG_TEXTDOMAIN(domain)
#define MemSet(start, val, len)
void write_csvlog(ErrorData *edata)
void assign_syslog_facility(int newval, void *extra)
int getinternalerrposition(void)
static bool should_output_to_client(int elevel)
void assign_syslog_ident(const char *newval, void *extra)
int errcode_for_socket_access(void)
bool errsave_start(struct Node *context, const char *domain)
static void log_line_prefix(StringInfo buf, ErrorData *edata)
static const char * process_log_prefix_padding(const char *p, int *ppadding)
int err_generic_string(int field, const char *str)
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
void errsave_finish(struct Node *context, const char *filename, int lineno, const char *funcname)
static char formatted_log_time[FORMATTED_TS_LEN]
int internalerrquery(const char *query)
static char formatted_start_time[FORMATTED_TS_LEN]
int internalerrposition(int cursorpos)
static void send_message_to_frontend(ErrorData *edata)
bool check_log_of_query(ErrorData *edata)
int errmsg_internal(const char *fmt,...)
static void append_with_tabs(StringInfo buf, const char *str)
int errhidestmt(bool hide_stmt)
char * get_formatted_log_time(void)
bool errstart(int elevel, const char *domain)
void EmitErrorReport(void)
bool syslog_split_messages
static void FreeErrorDataContents(ErrorData *edata)
static int errordata_stack_depth
static void err_sendstring(StringInfo buf, const char *str)
void FreeErrorData(ErrorData *edata)
void assign_backtrace_functions(const char *newval, void *extra)
static ErrorData * get_error_stack_entry(void)
int errdetail_internal(const char *fmt,...)
static bool saved_timeval_set
int errcode_for_file_access(void)
static char * backtrace_function_list
int errdetail(const char *fmt,...)
const char * error_severity(int elevel)
int errcontext_msg(const char *fmt,...)
static int save_format_errnumber
bool check_backtrace_functions(char **newval, void **extra, GucSource source)
void pre_format_elog_string(int errnumber, const char *domain)
static int recursion_depth
ErrorContextCallback * error_context_stack
static struct timeval saved_timeval
void ReThrowError(ErrorData *edata)
static void set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str)
const char * get_backend_type_for_log(void)
static bool matches_backtrace_functions(const char *funcname)
bool check_log_destination(char **newval, void **extra, GucSource source)
void FlushErrorState(void)
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
char * format_elog_string(const char *fmt,...)
int errhint(const char *fmt,...)
static bool is_log_level_output(int elevel, int log_min_level)
void ThrowErrorData(ErrorData *edata)
bool message_level_is_interesting(int elevel)
void write_pipe_chunks(char *data, int len, int dest)
int errhidecontext(bool hide_ctx)
emit_log_hook_type emit_log_hook
bool syslog_sequence_numbers
static void send_message_to_server_log(ErrorData *edata)
char * Log_destination_string
static void write_console(const char *line, int len)
#define EVALUATE_MESSAGE(domain, targetfield, appendval, translateit)
char * unpack_sql_state(int sql_state)
static void set_stack_entry_location(ErrorData *edata, const char *filename, int lineno, const char *funcname)
pg_attribute_cold bool errstart_cold(int elevel, const char *domain)
#define CHECK_STACK_DEPTH()
static void set_stack_entry_domain(ErrorData *edata, const char *domain)
#define EVALUATE_MESSAGE_PLURAL(domain, targetfield, appendval)
static bool should_output_to_server(int elevel)
int errcode(int sqlerrcode)
int errdetail_log_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
void write_stderr(const char *fmt,...)
int errmsg(const char *fmt,...)
void log_status_format(StringInfo buf, const char *format, ErrorData *edata)
char * GetErrorContextStack(void)
bool in_error_recursion_trouble(void)
void errfinish(const char *filename, int lineno, const char *funcname)
static const char * err_gettext(const char *str) pg_attribute_format_arg(1)
int errposition(int cursorpos)
#define ERRORDATA_STACK_SIZE
int errhint_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
int errdetail_log(const char *fmt,...)
static pg_noinline void set_backtrace(ErrorData *edata, int num_skip)
char * get_formatted_start_time(void)
int set_errcontext_domain(const char *domain)
void reset_formatted_start_time(void)
static ErrorData errordata[ERRORDATA_STACK_SIZE]
sigjmp_buf * PG_exception_stack
static const char * save_format_domain
ErrorData * CopyErrorData(void)
void assign_log_destination(const char *newval, void *extra)
void(* emit_log_hook_type)(ErrorData *edata)
#define WARNING_CLIENT_ONLY
#define LOG_DESTINATION_JSONLOG
#define LOG_DESTINATION_SYSLOG
#define LOG_DESTINATION_STDERR
#define ereport(elevel,...)
#define LOG_DESTINATION_EVENTLOG
#define LOG_DESTINATION_CSVLOG
#define palloc_object(type)
volatile uint32 QueryCancelHoldoffCount
ProtocolVersion FrontendProtocol
volatile uint32 InterruptHoldoffCount
volatile uint32 CritSectionCount
char OutputFileName[MAXPGPATH]
void * guc_malloc(int elevel, size_t size)
#define GUC_check_errdetail
int log_min_error_statement
static int syslog_facility
char * backtrace_functions
bool proc_exit_inprogress
void write_jsonlog(ErrorData *edata)
static void const char * fmt
static void const char fflush(stdout)
vfprintf(stderr, fmt, args)
void list_free(List *list)
int pg_mbcliplen(const char *mbstr, int len, int limit)
int GetMessageEncoding(void)
void MemoryContextReset(MemoryContext context)
char * pstrdup(const char *in)
void pfree(void *pointer)
MemoryContext CurrentMemoryContext
char * MemoryContextStrdup(MemoryContext context, const char *string)
MemoryContext ErrorContext
#define CHECK_FOR_INTERRUPTS()
const char * GetBackendTypeDesc(BackendType backendType)
BackendType MyBackendType
#define IsA(nodeptr, _type_)
#define DEFAULT_EVENT_SOURCE
static rewind_source * source
SessionEndType pgStatSessionEndCause
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
size_t pg_strftime(char *s, size_t maxsize, const char *format, const struct pg_tm *t)
PGDLLIMPORT pg_tz * log_timezone
#define ALL_CONNECTION_FAILURE_ERRNOS
int pg_strcasecmp(const char *s1, const char *s2)
CommandDest whereToSendOutput
const char * debug_query_string
#define PG_DIAG_INTERNAL_QUERY
#define PG_DIAG_SCHEMA_NAME
#define PG_DIAG_CONSTRAINT_NAME
#define PG_DIAG_DATATYPE_NAME
#define PG_DIAG_SOURCE_LINE
#define PG_DIAG_STATEMENT_POSITION
#define PG_DIAG_SOURCE_FILE
#define PG_DIAG_MESSAGE_HINT
#define PG_DIAG_SEVERITY_NONLOCALIZED
#define PG_DIAG_TABLE_NAME
#define PG_DIAG_MESSAGE_PRIMARY
#define PG_DIAG_COLUMN_NAME
#define PG_DIAG_MESSAGE_DETAIL
#define PG_DIAG_SOURCE_FUNCTION
#define PG_DIAG_INTERNAL_POSITION
bool ClientAuthInProgress
BackgroundWorker * MyBgworkerEntry
int pq_putmessage_v2(char msgtype, const char *s, size_t len)
#define PG_PROTOCOL_MAJOR(v)
static int fd(const char *x, int i)
#define INVALID_PROC_NUMBER
#define PqMsg_ErrorResponse
#define PqMsg_NoticeResponse
const char * get_ps_display(int *displen)
char * psprintf(const char *fmt,...)
MemoryContextSwitchTo(old_ctx)
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
void appendStringInfoSpaces(StringInfo str, int count)
void appendStringInfoString(StringInfo str, const char *s)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
#define appendStringInfoCharMacro(str, ch)
char bgw_type[BGW_MAXLEN]
struct ErrorContextCallback * previous
void(* callback)(void *arg)
struct MemoryContextData * assoc_context
const char * context_domain
void write_syslogger_file(const char *buffer, int count, int destination)
#define PIPE_PROTO_DEST_JSONLOG
#define PIPE_PROTO_IS_LAST
#define PIPE_PROTO_DEST_CSVLOG
#define PIPE_PROTO_DEST_STDERR
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
int pgwin32_is_service(void)
int gettimeofday(struct timeval *tp, void *tzp)
TransactionId GetTopTransactionIdIfAny(void)