Skip to content

Commit 0921165

Browse files
committed
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.
1 parent 034967b commit 0921165

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

src/backend/utils/error/elog.c

+29-22
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,10 @@ errcode_for_socket_access(void)
667667
/* Expand %m in format string */ \
668668
fmtbuf = expand_fmt_string(fmt, edata); \
669669
initStringInfo(&buf); \
670-
if ((appendval) && edata->targetfield) \
671-
appendStringInfo(&buf, "%s\n", edata->targetfield); \
670+
if ((appendval) && edata->targetfield) { \
671+
appendStringInfoString(&buf, edata->targetfield); \
672+
appendStringInfoChar(&buf, '\n'); \
673+
} \
672674
/* Generate actual output --- have to use appendStringInfoVA */ \
673675
for (;;) \
674676
{ \
@@ -708,8 +710,10 @@ errcode_for_socket_access(void)
708710
/* Expand %m in format string */ \
709711
fmtbuf = expand_fmt_string(fmt, edata); \
710712
initStringInfo(&buf); \
711-
if ((appendval) && edata->targetfield) \
712-
appendStringInfo(&buf, "%s\n", edata->targetfield); \
713+
if ((appendval) && edata->targetfield) { \
714+
appendStringInfoString(&buf, edata->targetfield); \
715+
appendStringInfoChar(&buf, '\n'); \
716+
} \
713717
/* Generate actual output --- have to use appendStringInfoVA */ \
714718
for (;;) \
715719
{ \
@@ -1809,7 +1813,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
18091813

18101814
if (appname == NULL || *appname == '\0')
18111815
appname = _("[unknown]");
1812-
appendStringInfo(buf, "%s", appname);
1816+
appendStringInfoString(buf, appname);
18131817
}
18141818
break;
18151819
case 'u':
@@ -1819,7 +1823,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
18191823

18201824
if (username == NULL || *username == '\0')
18211825
username = _("[unknown]");
1822-
appendStringInfo(buf, "%s", username);
1826+
appendStringInfoString(buf, username);
18231827
}
18241828
break;
18251829
case 'd':
@@ -1829,7 +1833,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
18291833

18301834
if (dbname == NULL || *dbname == '\0')
18311835
dbname = _("[unknown]");
1832-
appendStringInfo(buf, "%s", dbname);
1836+
appendStringInfoString(buf, dbname);
18331837
}
18341838
break;
18351839
case 'c':
@@ -1877,7 +1881,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
18771881
case 'r':
18781882
if (MyProcPort && MyProcPort->remote_host)
18791883
{
1880-
appendStringInfo(buf, "%s", MyProcPort->remote_host);
1884+
appendStringInfoString(buf, MyProcPort->remote_host);
18811885
if (MyProcPort->remote_port &&
18821886
MyProcPort->remote_port[0] != '\0')
18831887
appendStringInfo(buf, "(%s)",
@@ -1886,7 +1890,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
18861890
break;
18871891
case 'h':
18881892
if (MyProcPort && MyProcPort->remote_host)
1889-
appendStringInfo(buf, "%s", MyProcPort->remote_host);
1893+
appendStringInfoString(buf, MyProcPort->remote_host);
18901894
break;
18911895
case 'q':
18921896
/* in postmaster and friends, stop if %q is seen */
@@ -2004,9 +2008,12 @@ write_csvlog(ErrorData *edata)
20042008
if (MyProcPort && MyProcPort->remote_host)
20052009
{
20062010
appendStringInfoChar(&buf, '"');
2007-
appendStringInfo(&buf, "%s", MyProcPort->remote_host);
2011+
appendStringInfoString(&buf, MyProcPort->remote_host);
20082012
if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0')
2009-
appendStringInfo(&buf, ":%s", MyProcPort->remote_port);
2013+
{
2014+
appendStringInfoChar(&buf, ':');
2015+
appendStringInfoString(&buf, MyProcPort->remote_port);
2016+
}
20102017
appendStringInfoChar(&buf, '"');
20112018
}
20122019
appendStringInfoChar(&buf, ',');
@@ -2053,40 +2060,40 @@ write_csvlog(ErrorData *edata)
20532060
appendStringInfoChar(&buf, ',');
20542061

20552062
/* Error severity */
2056-
appendStringInfo(&buf, "%s", error_severity(edata->elevel));
2063+
appendStringInfoString(&buf, error_severity(edata->elevel));
20572064
appendStringInfoChar(&buf, ',');
20582065

20592066
/* SQL state code */
2060-
appendStringInfo(&buf, "%s", unpack_sql_state(edata->sqlerrcode));
2067+
appendStringInfoString(&buf, unpack_sql_state(edata->sqlerrcode));
20612068
appendStringInfoChar(&buf, ',');
20622069

20632070
/* errmessage */
20642071
appendCSVLiteral(&buf, edata->message);
2065-
appendStringInfoCharMacro(&buf, ',');
2072+
appendStringInfoChar(&buf, ',');
20662073

20672074
/* errdetail or errdetail_log */
20682075
if (edata->detail_log)
20692076
appendCSVLiteral(&buf, edata->detail_log);
20702077
else
20712078
appendCSVLiteral(&buf, edata->detail);
2072-
appendStringInfoCharMacro(&buf, ',');
2079+
appendStringInfoChar(&buf, ',');
20732080

20742081
/* errhint */
20752082
appendCSVLiteral(&buf, edata->hint);
2076-
appendStringInfoCharMacro(&buf, ',');
2083+
appendStringInfoChar(&buf, ',');
20772084

20782085
/* internal query */
20792086
appendCSVLiteral(&buf, edata->internalquery);
2080-
appendStringInfoCharMacro(&buf, ',');
2087+
appendStringInfoChar(&buf, ',');
20812088

20822089
/* if printed internal query, print internal pos too */
20832090
if (edata->internalpos > 0 && edata->internalquery != NULL)
20842091
appendStringInfo(&buf, "%d", edata->internalpos);
2085-
appendStringInfoCharMacro(&buf, ',');
2092+
appendStringInfoChar(&buf, ',');
20862093

20872094
/* errcontext */
20882095
appendCSVLiteral(&buf, edata->context);
2089-
appendStringInfoCharMacro(&buf, ',');
2096+
appendStringInfoChar(&buf, ',');
20902097

20912098
/* user query --- only reported if not disabled by the caller */
20922099
if (is_log_level_output(edata->elevel, log_min_error_statement) &&
@@ -2095,10 +2102,10 @@ write_csvlog(ErrorData *edata)
20952102
print_stmt = true;
20962103
if (print_stmt)
20972104
appendCSVLiteral(&buf, debug_query_string);
2098-
appendStringInfoCharMacro(&buf, ',');
2105+
appendStringInfoChar(&buf, ',');
20992106
if (print_stmt && edata->cursorpos > 0)
21002107
appendStringInfo(&buf, "%d", edata->cursorpos);
2101-
appendStringInfoCharMacro(&buf, ',');
2108+
appendStringInfoChar(&buf, ',');
21022109

21032110
/* file error location */
21042111
if (Log_error_verbosity >= PGERROR_VERBOSE)
@@ -2117,7 +2124,7 @@ write_csvlog(ErrorData *edata)
21172124
appendCSVLiteral(&buf, msgbuf.data);
21182125
pfree(msgbuf.data);
21192126
}
2120-
appendStringInfoCharMacro(&buf, ',');
2127+
appendStringInfoChar(&buf, ',');
21212128

21222129
/* application name */
21232130
if (application_name)

0 commit comments

Comments
 (0)