Reduce memory requirements for writing CSVlogs, so it will work with about

the same amount of memory in ErrorContext as standard logs.
This commit is contained in:
Andrew Dunstan 2007-08-23 01:24:43 +00:00
parent 1535f26c70
commit 44b5efbae6
1 changed files with 32 additions and 18 deletions

View File

@ -42,7 +42,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.194 2007/08/19 01:41:25 adunstan Exp $ * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.195 2007/08/23 01:24:43 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -133,7 +133,8 @@ static const char *error_severity(int elevel);
static void append_with_tabs(StringInfo buf, const char *str); static void append_with_tabs(StringInfo buf, const char *str);
static bool is_log_level_output(int elevel, int log_min_level); static bool is_log_level_output(int elevel, int log_min_level);
static void write_pipe_chunks(char *data, int len, int dest); static void write_pipe_chunks(char *data, int len, int dest);
static void get_error_message(StringInfo buf, ErrorData *edata); static void get_csv_error_message(StringInfo buf, ErrorData *edata);
static void write_csvlog(ErrorData *edata);
/* /*
* errstart --- begin an error-reporting cycle * errstart --- begin an error-reporting cycle
@ -1809,9 +1810,7 @@ write_csvlog(ErrorData *edata)
appendStringInfoChar(&buf, ','); appendStringInfoChar(&buf, ',');
/* Error message and cursor position if any */ /* Error message and cursor position if any */
get_error_message(&msgbuf, edata); get_csv_error_message(&buf, edata);
appendCSVLiteral(&buf, msgbuf.data);
appendStringInfoChar(&buf, '\n'); appendStringInfoChar(&buf, '\n');
@ -1826,15 +1825,23 @@ write_csvlog(ErrorData *edata)
} }
/* /*
* Appends the buffer with the error message and the cursor position. * Appends the buffer with the error message and the cursor position, all
* CSV escaped.
*/ */
static void static void
get_error_message(StringInfo buf, ErrorData *edata) get_csv_error_message(StringInfo buf, ErrorData *edata)
{ {
if (edata->message) char *msg = edata->message ? edata-> message : _("missing error text");
appendStringInfo(buf, "%s", edata->message); char c;
else
appendStringInfo(buf, "%s", _("missing error text")); appendStringInfoCharMacro(buf, '"');
while ( (c = *msg++) != '\0' )
{
if (c == '"')
appendStringInfoCharMacro(buf, '"');
appendStringInfoCharMacro(buf, c);
}
if (edata->cursorpos > 0) if (edata->cursorpos > 0)
appendStringInfo(buf, _(" at character %d"), appendStringInfo(buf, _(" at character %d"),
@ -1842,6 +1849,8 @@ get_error_message(StringInfo buf, ErrorData *edata)
else if (edata->internalpos > 0) else if (edata->internalpos > 0)
appendStringInfo(buf, _(" at character %d"), appendStringInfo(buf, _(" at character %d"),
edata->internalpos); edata->internalpos);
appendStringInfoCharMacro(buf, '"');
} }
/* /*
@ -2032,13 +2041,19 @@ send_message_to_server_log(ErrorData *edata)
write(fileno(stderr), buf.data, buf.len); write(fileno(stderr), buf.data, buf.len);
} }
/* If in the syslogger process, try to write messages direct to file */
if (am_syslogger)
write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_STDERR);
/* Write to CSV log if enabled */
if (Log_destination & LOG_DESTINATION_CSVLOG) if (Log_destination & LOG_DESTINATION_CSVLOG)
{ {
if (redirection_done || am_syslogger) if (redirection_done || am_syslogger)
{ {
/* send CSV data if it's safe to do so (syslogger doesn't need /* send CSV data if it's safe to do so (syslogger doesn't need
* the pipe) * the pipe). First get back the space in the message buffer.
*/ */
pfree(buf.data);
write_csvlog(edata); write_csvlog(edata);
} }
else else
@ -2051,14 +2066,13 @@ send_message_to_server_log(ErrorData *edata)
/* write message to stderr unless we just sent it above */ /* write message to stderr unless we just sent it above */
write(fileno(stderr), buf.data, buf.len); write(fileno(stderr), buf.data, buf.len);
} }
pfree(buf.data);
} }
} }
else
/* If in the syslogger process, try to write messages direct to file */ {
if (am_syslogger) pfree(buf.data);
write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_STDERR); }
pfree(buf.data);
} }
/* /*