PostgreSQL Source Code  git master
csvlog.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "libpq/libpq.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
#include "postmaster/bgworker.h"
#include "postmaster/syslogger.h"
#include "storage/lock.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/backend_status.h"
#include "utils/elog.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 39 of file csvlog.c.

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

References appendStringInfoCharMacro, buf, and data.

Referenced by write_csvlog().

◆ write_csvlog()

void write_csvlog ( ErrorData edata)

Definition at line 65 of file csvlog.c.

66 {
68  bool print_stmt = false;
69 
70  /* static counter for line numbers */
71  static long log_line_number = 0;
72 
73  /* has counter been reset in current process? */
74  static int log_my_pid = 0;
75 
76  /*
77  * This is one of the few places where we'd rather not inherit a static
78  * variable's value from the postmaster. But since we will, reset it when
79  * MyProcPid changes.
80  */
81  if (log_my_pid != MyProcPid)
82  {
83  log_line_number = 0;
84  log_my_pid = MyProcPid;
86  }
87  log_line_number++;
88 
90 
91  /* timestamp with milliseconds */
94 
95  /* username */
96  if (MyProcPort)
99 
100  /* database name */
101  if (MyProcPort)
103  appendStringInfoChar(&buf, ',');
104 
105  /* Process id */
106  if (MyProcPid != 0)
107  appendStringInfo(&buf, "%d", MyProcPid);
108  appendStringInfoChar(&buf, ',');
109 
110  /* Remote host and port */
112  {
113  appendStringInfoChar(&buf, '"');
115  if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0')
116  {
117  appendStringInfoChar(&buf, ':');
119  }
120  appendStringInfoChar(&buf, '"');
121  }
122  appendStringInfoChar(&buf, ',');
123 
124  /* session id */
125  appendStringInfo(&buf, "%lx.%x", (long) MyStartTime, MyProcPid);
126  appendStringInfoChar(&buf, ',');
127 
128  /* Line number */
129  appendStringInfo(&buf, "%ld", log_line_number);
130  appendStringInfoChar(&buf, ',');
131 
132  /* PS display */
133  if (MyProcPort)
134  {
135  StringInfoData msgbuf;
136  const char *psdisp;
137  int displen;
138 
139  initStringInfo(&msgbuf);
140 
141  psdisp = get_ps_display(&displen);
142  appendBinaryStringInfo(&msgbuf, psdisp, displen);
143  appendCSVLiteral(&buf, msgbuf.data);
144 
145  pfree(msgbuf.data);
146  }
147  appendStringInfoChar(&buf, ',');
148 
149  /* session start timestamp */
151  appendStringInfoChar(&buf, ',');
152 
153  /* Virtual transaction id */
154  /* keep VXID format in sync with lockfuncs.c */
155  if (MyProc != NULL && MyProc->backendId != InvalidBackendId)
157  appendStringInfoChar(&buf, ',');
158 
159  /* Transaction id */
161  appendStringInfoChar(&buf, ',');
162 
163  /* Error severity */
165  appendStringInfoChar(&buf, ',');
166 
167  /* SQL state code */
169  appendStringInfoChar(&buf, ',');
170 
171  /* errmessage */
172  appendCSVLiteral(&buf, edata->message);
173  appendStringInfoChar(&buf, ',');
174 
175  /* errdetail or errdetail_log */
176  if (edata->detail_log)
177  appendCSVLiteral(&buf, edata->detail_log);
178  else
179  appendCSVLiteral(&buf, edata->detail);
180  appendStringInfoChar(&buf, ',');
181 
182  /* errhint */
183  appendCSVLiteral(&buf, edata->hint);
184  appendStringInfoChar(&buf, ',');
185 
186  /* internal query */
188  appendStringInfoChar(&buf, ',');
189 
190  /* if printed internal query, print internal pos too */
191  if (edata->internalpos > 0 && edata->internalquery != NULL)
192  appendStringInfo(&buf, "%d", edata->internalpos);
193  appendStringInfoChar(&buf, ',');
194 
195  /* errcontext */
196  if (!edata->hide_ctx)
197  appendCSVLiteral(&buf, edata->context);
198  appendStringInfoChar(&buf, ',');
199 
200  /* user query --- only reported if not disabled by the caller */
201  print_stmt = check_log_of_query(edata);
202  if (print_stmt)
204  appendStringInfoChar(&buf, ',');
205  if (print_stmt && edata->cursorpos > 0)
206  appendStringInfo(&buf, "%d", edata->cursorpos);
207  appendStringInfoChar(&buf, ',');
208 
209  /* file error location */
211  {
212  StringInfoData msgbuf;
213 
214  initStringInfo(&msgbuf);
215 
216  if (edata->funcname && edata->filename)
217  appendStringInfo(&msgbuf, "%s, %s:%d",
218  edata->funcname, edata->filename,
219  edata->lineno);
220  else if (edata->filename)
221  appendStringInfo(&msgbuf, "%s:%d",
222  edata->filename, edata->lineno);
223  appendCSVLiteral(&buf, msgbuf.data);
224  pfree(msgbuf.data);
225  }
226  appendStringInfoChar(&buf, ',');
227 
228  /* application name */
229  if (application_name)
231 
232  appendStringInfoChar(&buf, ',');
233 
234  /* backend type */
236  appendStringInfoChar(&buf, ',');
237 
238  /* leader PID */
239  if (MyProc)
240  {
241  PGPROC *leader = MyProc->lockGroupLeader;
242 
243  /*
244  * Show the leader only for active parallel workers. This leaves out
245  * the leader of a parallel group.
246  */
247  if (leader && leader->pid != MyProcPid)
248  appendStringInfo(&buf, "%d", leader->pid);
249  }
250  appendStringInfoChar(&buf, ',');
251 
252  /* query id */
253  appendStringInfo(&buf, "%lld", (long long) pgstat_get_my_query_id());
254 
255  appendStringInfoChar(&buf, '\n');
256 
257  /* If in the syslogger process, try to write messages direct to file */
258  if (MyBackendType == B_LOGGER)
260  else
262 
263  pfree(buf.data);
264 }
uint64 pgstat_get_my_query_id(void)
#define InvalidBackendId
Definition: backendid.h:23
static void appendCSVLiteral(StringInfo buf, const char *data)
Definition: csvlog.c:39
bool check_log_of_query(ErrorData *edata)
Definition: elog.c:2369
char * get_formatted_log_time(void)
Definition: elog.c:2295
int Log_error_verbosity
Definition: elog.c:109
const char * error_severity(int elevel)
Definition: elog.c:3299
const char * get_backend_type_for_log(void)
Definition: elog.c:2392
void write_pipe_chunks(char *data, int len, int dest)
Definition: elog.c:3061
char * unpack_sql_state(int sql_state)
Definition: elog.c:2798
char * get_formatted_start_time(void)
Definition: elog.c:2345
#define _(x)
Definition: elog.c:89
void reset_formatted_start_time(void)
Definition: elog.c:2333
@ PGERROR_VERBOSE
Definition: elog.h:410
#define LOG_DESTINATION_CSVLOG
Definition: elog.h:424
int MyProcPid
Definition: globals.c:44
struct Port * MyProcPort
Definition: globals.c:47
pg_time_t MyStartTime
Definition: globals.c:45
char * application_name
Definition: guc.c:661
void pfree(void *pointer)
Definition: mcxt.c:1175
@ B_LOGGER
Definition: miscadmin.h:338
BackendType MyBackendType
Definition: miscinit.c:63
const char * debug_query_string
Definition: postgres.c:89
const char * get_ps_display(int *displen)
Definition: ps_status.c:430
PGPROC * MyProc
Definition: proc.c:68
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
int internalpos
Definition: elog.h:381
char * context
Definition: elog.h:372
char * internalquery
Definition: elog.h:382
int sqlerrcode
Definition: elog.h:367
const char * filename
Definition: elog.h:362
int elevel
Definition: elog.h:357
char * detail
Definition: elog.h:369
const char * funcname
Definition: elog.h:364
char * message
Definition: elog.h:368
char * detail_log
Definition: elog.h:370
int lineno
Definition: elog.h:363
char * hint
Definition: elog.h:371
bool hide_ctx
Definition: elog.h:361
int cursorpos
Definition: elog.h:380
Definition: proc.h:161
LocalTransactionId lxid
Definition: proc.h:182
BackendId backendId
Definition: proc.h:192
int pid
Definition: proc.h:185
PGPROC * lockGroupLeader
Definition: proc.h:290
char * user_name
Definition: libpq-be.h:145
char * remote_port
Definition: libpq-be.h:136
char * database_name
Definition: libpq-be.h:144
char * remote_host
Definition: libpq-be.h:131
void write_syslogger_file(const char *buffer, int count, int destination)
Definition: syslogger.c:1143
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:428

References _, appendBinaryStringInfo(), appendCSVLiteral(), appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), application_name, B_LOGGER, PGPROC::backendId, 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, InvalidBackendId, 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, Port::remote_host, Port::remote_port, reset_formatted_start_time(), ErrorData::sqlerrcode, unpack_sql_state(), Port::user_name, write_pipe_chunks(), and write_syslogger_file().

Referenced by send_message_to_server_log().