1/*-------------------------------------------------------------------------
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/error/jsonlog.c
13 *-------------------------------------------------------------------------
38 * Append to a StringInfo a comma followed by a JSON key and a value.
39 * The key is always escaped. The value can be escaped optionally, that
40 * is dependent on the data type of the key.
63 * appendJSONKeyValueFmt
65 * Evaluate the fmt string and then invoke appendJSONKeyValue() as the
66 * value of the JSON property. Both the key and value will be escaped by
67 * appendJSONKeyValue().
71 bool escape_key,
const char *fmt,...)
73 int save_errno = errno;
74 size_t len = 128;
/* initial assumption about buffer size */
82 /* Allocate result buffer */
85 /* Try to format the data. */
94 /* Release buffer and loop around to try again with larger len. */
106 * Write logs in json format.
115 /* static counter for line numbers */
116 static long log_line_number = 0;
118 /* Has the counter been reset in the current process? */
119 static int log_my_pid = 0;
122 * This is one of the few places where we'd rather not inherit a static
123 * variable's value from the postmaster. But since we will, reset it when
136 /* Initialize string */
139 /* timestamp with milliseconds */
143 * First property does not use appendJSONKeyValue as it does not have
162 /* Remote host and port */
192 /* session start timestamp */
196 /* Virtual transaction id */
197 /* keep VXID format in sync with lockfuncs.c */
219 /* errdetail or error_detail log */
234 /* if printed internal query, print internal pos too */
243 /* user query --- only reported if not disabled by the caller */
252 /* file error location */
265 /* Application name */
278 * Show the leader only for active parallel workers. This leaves out
279 * the leader of a parallel group.
294 /* If in the syslogger process, try to write messages direct to file */
int64 pgstat_get_my_query_id(void)
#define pg_attribute_printf(f, a)
bool check_log_of_query(ErrorData *edata)
const char * error_severity(int elevel)
char * get_formatted_start_time(void)
void write_pipe_chunks(char *data, int len, int dest)
const char * get_backend_type_for_log(void)
char * get_formatted_log_time(void)
char * unpack_sql_state(int sql_state)
void reset_formatted_start_time(void)
#define LOG_DESTINATION_JSONLOG
Assert(PointerIsAligned(start, uint64))
void escape_json(StringInfo buf, const char *str)
void write_jsonlog(ErrorData *edata)
static void static void appendJSONKeyValue(StringInfo buf, const char *key, const char *value, bool escape_value)
static void appendJSONKeyValueFmt(StringInfo buf, const char *key, bool escape_key, const char *fmt,...) pg_attribute_printf(4
void pfree(void *pointer)
BackendType MyBackendType
const char * debug_query_string
#define INVALID_PROC_NUMBER
const char * get_ps_display(int *displen)
size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args)
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
void appendStringInfoString(StringInfo str, const char *s)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
void write_syslogger_file(const char *buffer, int count, int destination)
TransactionId GetTopTransactionIdIfAny(void)