Commit 09211659 authored by Tom Lane's avatar Tom Lane

Use appendStringInfoString() where appropriate in elog.c.

The nominally equivalent call appendStringInfo(buf, "%s", str) can be
significantly slower when str is large.  In particular, the former usage in
EVALUATE_MESSAGE led to O(N^2) behavior when collecting a large number of
context lines, as I found out while testing recursive functions.  The other
changes are just neatnik-ism and seem unlikely to save anything meaningful,
but a cycle shaved is a cycle earned.
parent 034967bd
...@@ -667,8 +667,10 @@ errcode_for_socket_access(void) ...@@ -667,8 +667,10 @@ errcode_for_socket_access(void)
/* Expand %m in format string */ \ /* Expand %m in format string */ \
fmtbuf = expand_fmt_string(fmt, edata); \ fmtbuf = expand_fmt_string(fmt, edata); \
initStringInfo(&buf); \ initStringInfo(&buf); \
if ((appendval) && edata->targetfield) \ if ((appendval) && edata->targetfield) { \
appendStringInfo(&buf, "%s\n", edata->targetfield); \ appendStringInfoString(&buf, edata->targetfield); \
appendStringInfoChar(&buf, '\n'); \
} \
/* Generate actual output --- have to use appendStringInfoVA */ \ /* Generate actual output --- have to use appendStringInfoVA */ \
for (;;) \ for (;;) \
{ \ { \
...@@ -708,8 +710,10 @@ errcode_for_socket_access(void) ...@@ -708,8 +710,10 @@ errcode_for_socket_access(void)
/* Expand %m in format string */ \ /* Expand %m in format string */ \
fmtbuf = expand_fmt_string(fmt, edata); \ fmtbuf = expand_fmt_string(fmt, edata); \
initStringInfo(&buf); \ initStringInfo(&buf); \
if ((appendval) && edata->targetfield) \ if ((appendval) && edata->targetfield) { \
appendStringInfo(&buf, "%s\n", edata->targetfield); \ appendStringInfoString(&buf, edata->targetfield); \
appendStringInfoChar(&buf, '\n'); \
} \
/* Generate actual output --- have to use appendStringInfoVA */ \ /* Generate actual output --- have to use appendStringInfoVA */ \
for (;;) \ for (;;) \
{ \ { \
...@@ -1809,7 +1813,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata) ...@@ -1809,7 +1813,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
if (appname == NULL || *appname == '\0') if (appname == NULL || *appname == '\0')
appname = _("[unknown]"); appname = _("[unknown]");
appendStringInfo(buf, "%s", appname); appendStringInfoString(buf, appname);
} }
break; break;
case 'u': case 'u':
...@@ -1819,7 +1823,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata) ...@@ -1819,7 +1823,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
if (username == NULL || *username == '\0') if (username == NULL || *username == '\0')
username = _("[unknown]"); username = _("[unknown]");
appendStringInfo(buf, "%s", username); appendStringInfoString(buf, username);
} }
break; break;
case 'd': case 'd':
...@@ -1829,7 +1833,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata) ...@@ -1829,7 +1833,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
if (dbname == NULL || *dbname == '\0') if (dbname == NULL || *dbname == '\0')
dbname = _("[unknown]"); dbname = _("[unknown]");
appendStringInfo(buf, "%s", dbname); appendStringInfoString(buf, dbname);
} }
break; break;
case 'c': case 'c':
...@@ -1877,7 +1881,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata) ...@@ -1877,7 +1881,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
case 'r': case 'r':
if (MyProcPort && MyProcPort->remote_host) if (MyProcPort && MyProcPort->remote_host)
{ {
appendStringInfo(buf, "%s", MyProcPort->remote_host); appendStringInfoString(buf, MyProcPort->remote_host);
if (MyProcPort->remote_port && if (MyProcPort->remote_port &&
MyProcPort->remote_port[0] != '\0') MyProcPort->remote_port[0] != '\0')
appendStringInfo(buf, "(%s)", appendStringInfo(buf, "(%s)",
...@@ -1886,7 +1890,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata) ...@@ -1886,7 +1890,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
break; break;
case 'h': case 'h':
if (MyProcPort && MyProcPort->remote_host) if (MyProcPort && MyProcPort->remote_host)
appendStringInfo(buf, "%s", MyProcPort->remote_host); appendStringInfoString(buf, MyProcPort->remote_host);
break; break;
case 'q': case 'q':
/* in postmaster and friends, stop if %q is seen */ /* in postmaster and friends, stop if %q is seen */
...@@ -2004,9 +2008,12 @@ write_csvlog(ErrorData *edata) ...@@ -2004,9 +2008,12 @@ write_csvlog(ErrorData *edata)
if (MyProcPort && MyProcPort->remote_host) if (MyProcPort && MyProcPort->remote_host)
{ {
appendStringInfoChar(&buf, '"'); appendStringInfoChar(&buf, '"');
appendStringInfo(&buf, "%s", MyProcPort->remote_host); appendStringInfoString(&buf, MyProcPort->remote_host);
if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0') if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0')
appendStringInfo(&buf, ":%s", MyProcPort->remote_port); {
appendStringInfoChar(&buf, ':');
appendStringInfoString(&buf, MyProcPort->remote_port);
}
appendStringInfoChar(&buf, '"'); appendStringInfoChar(&buf, '"');
} }
appendStringInfoChar(&buf, ','); appendStringInfoChar(&buf, ',');
...@@ -2053,40 +2060,40 @@ write_csvlog(ErrorData *edata) ...@@ -2053,40 +2060,40 @@ write_csvlog(ErrorData *edata)
appendStringInfoChar(&buf, ','); appendStringInfoChar(&buf, ',');
/* Error severity */ /* Error severity */
appendStringInfo(&buf, "%s", error_severity(edata->elevel)); appendStringInfoString(&buf, error_severity(edata->elevel));
appendStringInfoChar(&buf, ','); appendStringInfoChar(&buf, ',');
/* SQL state code */ /* SQL state code */
appendStringInfo(&buf, "%s", unpack_sql_state(edata->sqlerrcode)); appendStringInfoString(&buf, unpack_sql_state(edata->sqlerrcode));
appendStringInfoChar(&buf, ','); appendStringInfoChar(&buf, ',');
/* errmessage */ /* errmessage */
appendCSVLiteral(&buf, edata->message); appendCSVLiteral(&buf, edata->message);
appendStringInfoCharMacro(&buf, ','); appendStringInfoChar(&buf, ',');
/* errdetail or errdetail_log */ /* errdetail or errdetail_log */
if (edata->detail_log) if (edata->detail_log)
appendCSVLiteral(&buf, edata->detail_log); appendCSVLiteral(&buf, edata->detail_log);
else else
appendCSVLiteral(&buf, edata->detail); appendCSVLiteral(&buf, edata->detail);
appendStringInfoCharMacro(&buf, ','); appendStringInfoChar(&buf, ',');
/* errhint */ /* errhint */
appendCSVLiteral(&buf, edata->hint); appendCSVLiteral(&buf, edata->hint);
appendStringInfoCharMacro(&buf, ','); appendStringInfoChar(&buf, ',');
/* internal query */ /* internal query */
appendCSVLiteral(&buf, edata->internalquery); appendCSVLiteral(&buf, edata->internalquery);
appendStringInfoCharMacro(&buf, ','); appendStringInfoChar(&buf, ',');
/* if printed internal query, print internal pos too */ /* if printed internal query, print internal pos too */
if (edata->internalpos > 0 && edata->internalquery != NULL) if (edata->internalpos > 0 && edata->internalquery != NULL)
appendStringInfo(&buf, "%d", edata->internalpos); appendStringInfo(&buf, "%d", edata->internalpos);
appendStringInfoCharMacro(&buf, ','); appendStringInfoChar(&buf, ',');
/* errcontext */ /* errcontext */
appendCSVLiteral(&buf, edata->context); appendCSVLiteral(&buf, edata->context);
appendStringInfoCharMacro(&buf, ','); appendStringInfoChar(&buf, ',');
/* user query --- only reported if not disabled by the caller */ /* user query --- only reported if not disabled by the caller */
if (is_log_level_output(edata->elevel, log_min_error_statement) && if (is_log_level_output(edata->elevel, log_min_error_statement) &&
...@@ -2095,10 +2102,10 @@ write_csvlog(ErrorData *edata) ...@@ -2095,10 +2102,10 @@ write_csvlog(ErrorData *edata)
print_stmt = true; print_stmt = true;
if (print_stmt) if (print_stmt)
appendCSVLiteral(&buf, debug_query_string); appendCSVLiteral(&buf, debug_query_string);
appendStringInfoCharMacro(&buf, ','); appendStringInfoChar(&buf, ',');
if (print_stmt && edata->cursorpos > 0) if (print_stmt && edata->cursorpos > 0)
appendStringInfo(&buf, "%d", edata->cursorpos); appendStringInfo(&buf, "%d", edata->cursorpos);
appendStringInfoCharMacro(&buf, ','); appendStringInfoChar(&buf, ',');
/* file error location */ /* file error location */
if (Log_error_verbosity >= PGERROR_VERBOSE) if (Log_error_verbosity >= PGERROR_VERBOSE)
...@@ -2117,7 +2124,7 @@ write_csvlog(ErrorData *edata) ...@@ -2117,7 +2124,7 @@ write_csvlog(ErrorData *edata)
appendCSVLiteral(&buf, msgbuf.data); appendCSVLiteral(&buf, msgbuf.data);
pfree(msgbuf.data); pfree(msgbuf.data);
} }
appendStringInfoCharMacro(&buf, ','); appendStringInfoChar(&buf, ',');
/* application name */ /* application name */
if (application_name) if (application_name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment