diff options
| author | David Rowley <drowley@postgresql.org> | 2025-11-06 14:59:48 +1300 |
|---|---|---|
| committer | David Rowley <drowley@postgresql.org> | 2025-11-06 14:59:48 +1300 |
| commit | 6d0eba66275b125bf634bbdffda90c70856e3f93 (patch) | |
| tree | e8457e698dcb49a6f9591e79373d8dfddd617f9e /src/backend/access/transam/xlogbackup.c | |
| parent | cf638b46aff2ccb8d4811e3b5d8a2c2410638190 (diff) | |
Use stack allocated StringInfoDatas, where possible
Various places that were using StringInfo but didn't need that
StringInfo to exist beyond the scope of the function were using
makeStringInfo(), which allocates both a StringInfoData and the buffer it
uses as two separate allocations. It's more efficient for these cases to
use a StringInfoData on the stack and initialize it with initStringInfo(),
which only allocates the string buffer. This also simplifies the cleanup,
in a few cases.
Author: Mats Kindahl <mats.kindahl@gmail.com>
Reviewed-by: David Rowley <dgrowleyml@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/4379aac8-26f1-42f2-a356-ff0e886228d3@gmail.com
Diffstat (limited to 'src/backend/access/transam/xlogbackup.c')
| -rw-r--r-- | src/backend/access/transam/xlogbackup.c | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/src/backend/access/transam/xlogbackup.c b/src/backend/access/transam/xlogbackup.c index cda4b38b7d6..8a8a2a7b326 100644 --- a/src/backend/access/transam/xlogbackup.c +++ b/src/backend/access/transam/xlogbackup.c @@ -31,18 +31,19 @@ build_backup_content(BackupState *state, bool ishistoryfile) char startstrbuf[128]; char startxlogfile[MAXFNAMELEN]; /* backup start WAL file */ XLogSegNo startsegno; - StringInfo result = makeStringInfo(); - char *data; + StringInfoData result; Assert(state != NULL); + initStringInfo(&result); + /* Use the log timezone here, not the session timezone */ pg_strftime(startstrbuf, sizeof(startstrbuf), "%Y-%m-%d %H:%M:%S %Z", pg_localtime(&state->starttime, log_timezone)); XLByteToSeg(state->startpoint, startsegno, wal_segment_size); XLogFileName(startxlogfile, state->starttli, startsegno, wal_segment_size); - appendStringInfo(result, "START WAL LOCATION: %X/%08X (file %s)\n", + appendStringInfo(&result, "START WAL LOCATION: %X/%08X (file %s)\n", LSN_FORMAT_ARGS(state->startpoint), startxlogfile); if (ishistoryfile) @@ -52,18 +53,18 @@ build_backup_content(BackupState *state, bool ishistoryfile) XLByteToSeg(state->stoppoint, stopsegno, wal_segment_size); XLogFileName(stopxlogfile, state->stoptli, stopsegno, wal_segment_size); - appendStringInfo(result, "STOP WAL LOCATION: %X/%08X (file %s)\n", + appendStringInfo(&result, "STOP WAL LOCATION: %X/%08X (file %s)\n", LSN_FORMAT_ARGS(state->stoppoint), stopxlogfile); } - appendStringInfo(result, "CHECKPOINT LOCATION: %X/%08X\n", + appendStringInfo(&result, "CHECKPOINT LOCATION: %X/%08X\n", LSN_FORMAT_ARGS(state->checkpointloc)); - appendStringInfoString(result, "BACKUP METHOD: streamed\n"); - appendStringInfo(result, "BACKUP FROM: %s\n", + appendStringInfoString(&result, "BACKUP METHOD: streamed\n"); + appendStringInfo(&result, "BACKUP FROM: %s\n", state->started_in_recovery ? "standby" : "primary"); - appendStringInfo(result, "START TIME: %s\n", startstrbuf); - appendStringInfo(result, "LABEL: %s\n", state->name); - appendStringInfo(result, "START TIMELINE: %u\n", state->starttli); + appendStringInfo(&result, "START TIME: %s\n", startstrbuf); + appendStringInfo(&result, "LABEL: %s\n", state->name); + appendStringInfo(&result, "START TIMELINE: %u\n", state->starttli); if (ishistoryfile) { @@ -73,22 +74,19 @@ build_backup_content(BackupState *state, bool ishistoryfile) pg_strftime(stopstrfbuf, sizeof(stopstrfbuf), "%Y-%m-%d %H:%M:%S %Z", pg_localtime(&state->stoptime, log_timezone)); - appendStringInfo(result, "STOP TIME: %s\n", stopstrfbuf); - appendStringInfo(result, "STOP TIMELINE: %u\n", state->stoptli); + appendStringInfo(&result, "STOP TIME: %s\n", stopstrfbuf); + appendStringInfo(&result, "STOP TIMELINE: %u\n", state->stoptli); } /* either both istartpoint and istarttli should be set, or neither */ Assert(XLogRecPtrIsInvalid(state->istartpoint) == (state->istarttli == 0)); if (!XLogRecPtrIsInvalid(state->istartpoint)) { - appendStringInfo(result, "INCREMENTAL FROM LSN: %X/%08X\n", + appendStringInfo(&result, "INCREMENTAL FROM LSN: %X/%08X\n", LSN_FORMAT_ARGS(state->istartpoint)); - appendStringInfo(result, "INCREMENTAL FROM TLI: %u\n", + appendStringInfo(&result, "INCREMENTAL FROM TLI: %u\n", state->istarttli); } - data = result->data; - pfree(result); - - return data; + return result.data; } |
