From 915b4ed0566542453f06754d328445c458e21a3a Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 8 Jul 2008 22:17:47 +0000 Subject: [PATCH] Fix performance bug in write_syslog(): the code to preferentially break the log message at newlines cost O(N^2) for very long messages with few or no newlines. For messages in the megabyte range this became the dominant cost. Per gripe from Achilleas Mantzios. Patch all the way back, since this is a safe change with no portability risks. I am also thinking of increasing PG_SYSLOG_LIMIT, but that should be done separately. --- src/backend/utils/error/elog.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index cdb40bb13eb..09eb1e86f6f 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -42,7 +42,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.201 2008/01/01 19:45:53 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.201.2.1 2008/07/08 22:17:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1277,6 +1277,7 @@ write_syslog(int level, const char *line) static unsigned long seq = 0; int len; + const char *nlpos; /* Open syslog connection if not done yet */ if (!openlog_done) @@ -1299,17 +1300,17 @@ write_syslog(int level, const char *line) * fact, it does work around by splitting up messages into smaller pieces. * * We divide into multiple syslog() calls if message is too long or if the - * message contains embedded NewLine(s) '\n'. + * message contains embedded newline(s). */ len = strlen(line); - if (len > PG_SYSLOG_LIMIT || strchr(line, '\n') != NULL) + nlpos = strchr(line, '\n'); + if (len > PG_SYSLOG_LIMIT || nlpos != NULL) { int chunk_nr = 0; while (len > 0) { char buf[PG_SYSLOG_LIMIT + 1]; - const char *nlpos; int buflen; int i; @@ -1318,11 +1319,12 @@ write_syslog(int level, const char *line) { line++; len--; + /* we need to recompute the next newline's position, too */ + nlpos = strchr(line, '\n'); continue; } /* copy one line, or as much as will fit, to buf */ - nlpos = strchr(line, '\n'); if (nlpos != NULL) buflen = nlpos - line; else -- 2.39.5