PostgreSQL Source Code  git master
csvlog.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "lib/stringinfo.h"
#include "libpq/libpq-be.h"
#include "miscadmin.h"
#include "postmaster/syslogger.h"
#include "storage/lock.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/backend_status.h"
#include "utils/guc.h"
#include "utils/ps_status.h"
Include dependency graph for csvlog.c:

Go to the source code of this file.

Functions

static void appendCSVLiteral (StringInfo buf, const char *data)
 
void write_csvlog (ErrorData *edata)
 

Function Documentation

◆ appendCSVLiteral()

static void appendCSVLiteral ( StringInfo  buf,
const char *  data 
)
inlinestatic

Definition at line 37 of file csvlog.c.

38 {
39  const char *p = data;
40  char c;
41 
42  /* avoid confusing an empty string with NULL */
43  if (p == NULL)
44  return;
45 
47  while ((c = *p++) != '\0')
48  {
49  if (c == '"')
52  }
54 }
const void * data
static char * buf
Definition: pg_test_fsync.c:72
char * c
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:204

References appendStringInfoCharMacro, buf, and data.

Referenced by write_csvlog().

◆ write_csvlog()

void write_csvlog ( ErrorData edata)

Definition at line 63 of file csvlog.c.

64 {
66  bool print_stmt = false;
67 
68  /* static counter for line numbers */
69  static long log_line_number = 0;
70 
71  /* has counter been reset in current process? */
72  static int log_my_pid = 0;
73 
74  /*
75  * This is one of the few places where we'd rather not inherit a static
76  * variable's value from the postmaster. But since we will, reset it when
77  * MyProcPid changes.
78  */
79  if (log_my_pid != MyProcPid)
80  {
81  log_line_number = 0;
82  log_my_pid = MyProcPid;
84  }
85  log_line_number++;
86 
88 
89  /* timestamp with milliseconds */
92 
93  /* username */
94  if (MyProcPort)
97 
98  /* database name */
99  if (MyProcPort)
101  appendStringInfoChar(&buf, ',');
102 
103  /* Process id */
104  if (MyProcPid != 0)
105  appendStringInfo(&buf, "%d", MyProcPid);
106  appendStringInfoChar(&buf, ',');
107 
108  /* Remote host and port */
110  {
111  appendStringInfoChar(&buf, '"');
113  if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0')
114  {
115  appendStringInfoChar(&buf, ':');
117  }
118  appendStringInfoChar(&buf, '"');
119  }
120  appendStringInfoChar(&buf, ',');
121 
122  /* session id */
123  appendStringInfo(&buf, "%" INT64_MODIFIER "x.%x", MyStartTime, MyProcPid);
124  appendStringInfoChar(&buf, ',');
125 
126  /* Line number */
127  appendStringInfo(&buf, "%ld", log_line_number);
128  appendStringInfoChar(&buf, ',');
129 
130  /* PS display */
131  if (MyProcPort)
132  {
133  StringInfoData msgbuf;
134  const char *psdisp;
135  int displen;
136 
137  initStringInfo(&msgbuf);
138 
139  psdisp = get_ps_display(&displen);
140  appendBinaryStringInfo(&msgbuf, psdisp, displen);
141  appendCSVLiteral(&buf, msgbuf.data);
142 
143  pfree(msgbuf.data);
144  }
145  appendStringInfoChar(&buf, ',');
146 
147  /* session start timestamp */
149  appendStringInfoChar(&buf, ',');
150 
151  /* Virtual transaction id */
152  /* keep VXID format in sync with lockfuncs.c */
153  if (MyProc != NULL && MyProc->vxid.procNumber != INVALID_PROC_NUMBER)
155  appendStringInfoChar(&buf, ',');
156 
157  /* Transaction id */
159  appendStringInfoChar(&buf, ',');
160 
161  /* Error severity */
163  appendStringInfoChar(&buf, ',');
164 
165  /* SQL state code */
167  appendStringInfoChar(&buf, ',');
168 
169  /* errmessage */
170  appendCSVLiteral(&buf, edata->message);
171  appendStringInfoChar(&buf, ',');
172 
173  /* errdetail or errdetail_log */
174  if (edata->detail_log)
175  appendCSVLiteral(&buf, edata->detail_log);
176  else
177  appendCSVLiteral(&buf, edata->detail);
178  appendStringInfoChar(&buf, ',');
179 
180  /* errhint */
181  appendCSVLiteral(&buf, edata->hint);
182  appendStringInfoChar(&buf, ',');
183 
184  /* internal query */
186  appendStringInfoChar(&buf, ',');
187 
188  /* if printed internal query, print internal pos too */
189  if (edata->internalpos > 0 && edata->internalquery != NULL)
190  appendStringInfo(&buf, "%d", edata->internalpos);
191  appendStringInfoChar(&buf, ',');
192 
193  /* errcontext */
194  if (!edata->hide_ctx)
195  appendCSVLiteral(&buf, edata->context);
196  appendStringInfoChar(&buf, ',');
197 
198  /* user query --- only reported if not disabled by the caller */
199  print_stmt = check_log_of_query(edata);
200  if (print_stmt)
202  appendStringInfoChar(&buf, ',');
203  if (print_stmt && edata->cursorpos > 0)
204  appendStringInfo(&buf, "%d", edata->cursorpos);
205  appendStringInfoChar(&buf, ',');
206 
207  /* file error location */
209  {
210  StringInfoData msgbuf;
211 
212  initStringInfo(&msgbuf);
213 
214  if (edata->funcname && edata->filename)
215  appendStringInfo(&msgbuf, "%s, %s:%d",
216  edata->funcname, edata->filename,
217  edata->lineno);
218  else if (edata->filename)
219  appendStringInfo(&msgbuf, "%s:%d",
220  edata->filename, edata->lineno);
221  appendCSVLiteral(&buf, msgbuf.data);
222  pfree(msgbuf.data);
223  }
224  appendStringInfoChar(&buf, ',');
225 
226  /* application name */
227  if (application_name)
229 
230  appendStringInfoChar(&buf, ',');
231 
232  /* backend type */
234  appendStringInfoChar(&buf, ',');
235 
236  /* leader PID */
237  if (MyProc)
238  {
239  PGPROC *leader = MyProc->lockGroupLeader;
240 
241  /*
242  * Show the leader only for active parallel workers. This leaves out
243  * the leader of a parallel group.
244  */
245  if (leader && leader->pid != MyProcPid)
246  appendStringInfo(&buf, "%d", leader->pid);
247  }
248  appendStringInfoChar(&buf, ',');
249 
250  /* query id */
251  appendStringInfo(&buf, "%lld", (long long) pgstat_get_my_query_id());
252 
253  appendStringInfoChar(&buf, '\n');
254 
255  /* If in the syslogger process, try to write messages direct to file */
256  if (MyBackendType == B_LOGGER)
258  else
260 
261  pfree(buf.data);
262 }
uint64 pgstat_get_my_query_id(void)
static void appendCSVLiteral(StringInfo buf, const char *data)
Definition: csvlog.c:37
bool check_log_of_query(ErrorData *edata)
Definition: elog.c:2731
char * get_formatted_log_time(void)
Definition: elog.c:2657
int Log_error_verbosity
Definition: elog.c:108
const char * error_severity(int elevel)
Definition: elog.c:3670
const char * get_backend_type_for_log(void)
Definition: elog.c:2754
void write_pipe_chunks(char *data, int len, int dest)
Definition: elog.c:3429
char * unpack_sql_state(int sql_state)
Definition: elog.c:3169
char * get_formatted_start_time(void)
Definition: elog.c:2707
#define _(x)
Definition: elog.c:90
void reset_formatted_start_time(void)
Definition: elog.c:2695
@ PGERROR_VERBOSE
Definition: elog.h:482
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:496
int MyProcPid
Definition: globals.c:46
struct Port * MyProcPort
Definition: globals.c:50
pg_time_t MyStartTime
Definition: globals.c:47
char * application_name
Definition: guc_tables.c:543
void pfree(void *pointer)
Definition: mcxt.c:1521
@ B_LOGGER
Definition: miscadmin.h:364
BackendType MyBackendType
Definition: miscinit.c:63
const char * debug_query_string
Definition: postgres.c:87
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
const char * get_ps_display(int *displen)
Definition: ps_status.c:530
PGPROC * MyProc
Definition: proc.c:66
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:94
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:230
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:179
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:191
void initStringInfo(StringInfo str)
Definition: stringinfo.c:56
int internalpos
Definition: elog.h:453
char * context
Definition: elog.h:444
char * internalquery
Definition: elog.h:454
int sqlerrcode
Definition: elog.h:439
const char * filename
Definition: elog.h:434
int elevel
Definition: elog.h:429
char * detail
Definition: elog.h:441
const char * funcname
Definition: elog.h:436
char * message
Definition: elog.h:440
char * detail_log
Definition: elog.h:442
int lineno
Definition: elog.h:435
char * hint
Definition: elog.h:443
bool hide_ctx
Definition: elog.h:433
int cursorpos
Definition: elog.h:452
Definition: proc.h:162
LocalTransactionId lxid
Definition: proc.h:200
struct PGPROC::@122 vxid
ProcNumber procNumber
Definition: proc.h:195
int pid
Definition: proc.h:182
PGPROC * lockGroupLeader
Definition: proc.h:304
char * user_name
Definition: libpq-be.h:152
char * remote_port
Definition: libpq-be.h:144
char * database_name
Definition: libpq-be.h:151
char * remote_host
Definition: libpq-be.h:139
void write_syslogger_file(const char *buffer, int count, int destination)
Definition: syslogger.c:1092
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:440

References _, appendBinaryStringInfo(), appendCSVLiteral(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), application_name, B_LOGGER, buf, check_log_of_query(), ErrorData::context, ErrorData::cursorpos, StringInfoData::data, Port::database_name, debug_query_string, ErrorData::detail, ErrorData::detail_log, ErrorData::elevel, error_severity(), ErrorData::filename, ErrorData::funcname, get_backend_type_for_log(), get_formatted_log_time(), get_formatted_start_time(), get_ps_display(), GetTopTransactionIdIfAny(), ErrorData::hide_ctx, ErrorData::hint, initStringInfo(), ErrorData::internalpos, ErrorData::internalquery, INVALID_PROC_NUMBER, ErrorData::lineno, PGPROC::lockGroupLeader, LOG_DESTINATION_CSVLOG, Log_error_verbosity, PGPROC::lxid, ErrorData::message, MyBackendType, MyProc, MyProcPid, MyProcPort, MyStartTime, pfree(), PGERROR_VERBOSE, pgstat_get_my_query_id(), PGPROC::pid, PGPROC::procNumber, Port::remote_host, Port::remote_port, reset_formatted_start_time(), ErrorData::sqlerrcode, unpack_sql_state(), Port::user_name, PGPROC::vxid, write_pipe_chunks(), and write_syslogger_file().

Referenced by send_message_to_server_log().