Skip to content

Commit bed6ed3

Browse files
committed
Move any code specific to log_destination=csvlog to its own file
The recent refactoring done in ac7c807 makes this move possible and simple, as this just moves some code around. This reduces the size of elog.c by 7%. Author: Michael Paquier, Sehrope Sarkuni Reviewed-by: Nathan Bossart Discussion: https://postgr.es/m/CAH7T-aqswBM6JWe4pDehi1uOiufqe06DJWaU5=X7dDLyqUExHg@mail.gmail.com simply moves the routines related to csvlog into their own file
1 parent ac7c807 commit bed6ed3

File tree

3 files changed

+265
-231
lines changed

3 files changed

+265
-231
lines changed

src/backend/utils/error/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ include $(top_builddir)/src/Makefile.global
1414

1515
OBJS = \
1616
assert.o \
17+
csvlog.o \
1718
elog.o
1819

1920
include $(top_srcdir)/src/backend/common.mk

src/backend/utils/error/csvlog.c

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* csvlog.c
4+
* CSV logging
5+
*
6+
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1994, Regents of the University of Californi
8+
*
9+
*
10+
* IDENTIFICATION
11+
* src/backend/utils/error/csvlog.c
12+
*
13+
*-------------------------------------------------------------------------
14+
*/
15+
16+
#include "postgres.h"
17+
18+
#include "access/xact.h"
19+
#include "libpq/libpq.h"
20+
#include "lib/stringinfo.h"
21+
#include "miscadmin.h"
22+
#include "postmaster/bgworker.h"
23+
#include "postmaster/syslogger.h"
24+
#include "storage/lock.h"
25+
#include "storage/proc.h"
26+
#include "tcop/tcopprot.h"
27+
#include "utils/backend_status.h"
28+
#include "utils/elog.h"
29+
#include "utils/guc.h"
30+
#include "utils/ps_status.h"
31+
32+
33+
/*
34+
* append a CSV'd version of a string to a StringInfo
35+
* We use the PostgreSQL defaults for CSV, i.e. quote = escape = '"'
36+
* If it's NULL, append nothing.
37+
*/
38+
static inline void
39+
appendCSVLiteral(StringInfo buf, const char *data)
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+
48+
appendStringInfoCharMacro(buf, '"');
49+
while ((c = *p++) != '\0')
50+
{
51+
if (c == '"')
52+
appendStringInfoCharMacro(buf, '"');
53+
appendStringInfoCharMacro(buf, c);
54+
}
55+
appendStringInfoCharMacro(buf, '"');
56+
}
57+
58+
/*
59+
* write_csvlog -- Generate and write CSV log entry
60+
*
61+
* Constructs the error message, depending on the Errordata it gets, in a CSV
62+
* format which is described in doc/src/sgml/config.sgml.
63+
*/
64+
void
65+
write_csvlog(ErrorData *edata)
66+
{
67+
StringInfoData buf;
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;
85+
reset_formatted_start_time();
86+
}
87+
log_line_number++;
88+
89+
initStringInfo(&buf);
90+
91+
/* timestamp with milliseconds */
92+
appendStringInfoString(&buf, get_formatted_log_time());
93+
appendStringInfoChar(&buf, ',');
94+
95+
/* username */
96+
if (MyProcPort)
97+
appendCSVLiteral(&buf, MyProcPort->user_name);
98+
appendStringInfoChar(&buf, ',');
99+
100+
/* database name */
101+
if (MyProcPort)
102+
appendCSVLiteral(&buf, MyProcPort->database_name);
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 */
111+
if (MyProcPort && MyProcPort->remote_host)
112+
{
113+
appendStringInfoChar(&buf, '"');
114+
appendStringInfoString(&buf, MyProcPort->remote_host);
115+
if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0')
116+
{
117+
appendStringInfoChar(&buf, ':');
118+
appendStringInfoString(&buf, MyProcPort->remote_port);
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 */
150+
appendStringInfoString(&buf, get_formatted_start_time());
151+
appendStringInfoChar(&buf, ',');
152+
153+
/* Virtual transaction id */
154+
/* keep VXID format in sync with lockfuncs.c */
155+
if (MyProc != NULL && MyProc->backendId != InvalidBackendId)
156+
appendStringInfo(&buf, "%d/%u", MyProc->backendId, MyProc->lxid);
157+
appendStringInfoChar(&buf, ',');
158+
159+
/* Transaction id */
160+
appendStringInfo(&buf, "%u", GetTopTransactionIdIfAny());
161+
appendStringInfoChar(&buf, ',');
162+
163+
/* Error severity */
164+
appendStringInfoString(&buf, _(error_severity(edata->elevel)));
165+
appendStringInfoChar(&buf, ',');
166+
167+
/* SQL state code */
168+
appendStringInfoString(&buf, unpack_sql_state(edata->sqlerrcode));
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 */
187+
appendCSVLiteral(&buf, edata->internalquery);
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)
203+
appendCSVLiteral(&buf, debug_query_string);
204+
appendStringInfoChar(&buf, ',');
205+
if (print_stmt && edata->cursorpos > 0)
206+
appendStringInfo(&buf, "%d", edata->cursorpos);
207+
appendStringInfoChar(&buf, ',');
208+
209+
/* file error location */
210+
if (Log_error_verbosity >= PGERROR_VERBOSE)
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)
230+
appendCSVLiteral(&buf, application_name);
231+
232+
appendStringInfoChar(&buf, ',');
233+
234+
/* backend type */
235+
appendCSVLiteral(&buf, get_backend_type_for_log());
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)
259+
write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_CSVLOG);
260+
else
261+
write_pipe_chunks(buf.data, buf.len, LOG_DESTINATION_CSVLOG);
262+
263+
pfree(buf.data);
264+
}

0 commit comments

Comments
 (0)