Skip to content

Commit 6d0eba6

Browse files
committed
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 <[email protected]> Reviewed-by: David Rowley <[email protected]> Reviewed-by: Chao Li <[email protected]> Discussion: https://postgr.es/m/[email protected]
1 parent cf638b4 commit 6d0eba6

File tree

12 files changed

+144
-130
lines changed

12 files changed

+144
-130
lines changed

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,7 +2841,7 @@ postgresExplainForeignScan(ForeignScanState *node, ExplainState *es)
28412841
*/
28422842
if (list_length(fdw_private) > FdwScanPrivateRelations)
28432843
{
2844-
StringInfo relations;
2844+
StringInfoData relations;
28452845
char *rawrelations;
28462846
char *ptr;
28472847
int minrti,
@@ -2875,7 +2875,7 @@ postgresExplainForeignScan(ForeignScanState *node, ExplainState *es)
28752875
rtoffset = bms_next_member(plan->fs_base_relids, -1) - minrti;
28762876

28772877
/* Now we can translate the string */
2878-
relations = makeStringInfo();
2878+
initStringInfo(&relations);
28792879
ptr = rawrelations;
28802880
while (*ptr)
28812881
{
@@ -2897,24 +2897,24 @@ postgresExplainForeignScan(ForeignScanState *node, ExplainState *es)
28972897
char *namespace;
28982898

28992899
namespace = get_namespace_name_or_temp(get_rel_namespace(rte->relid));
2900-
appendStringInfo(relations, "%s.%s",
2900+
appendStringInfo(&relations, "%s.%s",
29012901
quote_identifier(namespace),
29022902
quote_identifier(relname));
29032903
}
29042904
else
2905-
appendStringInfoString(relations,
2905+
appendStringInfoString(&relations,
29062906
quote_identifier(relname));
29072907
refname = (char *) list_nth(es->rtable_names, rti - 1);
29082908
if (refname == NULL)
29092909
refname = rte->eref->aliasname;
29102910
if (strcmp(refname, relname) != 0)
2911-
appendStringInfo(relations, " %s",
2911+
appendStringInfo(&relations, " %s",
29122912
quote_identifier(refname));
29132913
}
29142914
else
2915-
appendStringInfoChar(relations, *ptr++);
2915+
appendStringInfoChar(&relations, *ptr++);
29162916
}
2917-
ExplainPropertyText("Relations", relations->data, es);
2917+
ExplainPropertyText("Relations", relations.data, es);
29182918
}
29192919

29202920
/*

contrib/tcn/tcn.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ triggered_change_notification(PG_FUNCTION_ARGS)
6666
TupleDesc tupdesc;
6767
char *channel;
6868
char operation;
69-
StringInfo payload = makeStringInfo();
69+
StringInfoData payload;
7070
bool foundPK;
7171

7272
List *indexoidlist;
7373
ListCell *indexoidscan;
7474

75+
initStringInfo(&payload);
7576
/* make sure it's called as a trigger */
7677
if (!CALLED_AS_TRIGGER(fcinfo))
7778
ereport(ERROR,
@@ -149,22 +150,22 @@ triggered_change_notification(PG_FUNCTION_ARGS)
149150

150151
foundPK = true;
151152

152-
strcpy_quoted(payload, RelationGetRelationName(rel), '"');
153-
appendStringInfoCharMacro(payload, ',');
154-
appendStringInfoCharMacro(payload, operation);
153+
strcpy_quoted(&payload, RelationGetRelationName(rel), '"');
154+
appendStringInfoCharMacro(&payload, ',');
155+
appendStringInfoCharMacro(&payload, operation);
155156

156157
for (i = 0; i < indnkeyatts; i++)
157158
{
158159
int colno = index->indkey.values[i];
159160
Form_pg_attribute attr = TupleDescAttr(tupdesc, colno - 1);
160161

161-
appendStringInfoCharMacro(payload, ',');
162-
strcpy_quoted(payload, NameStr(attr->attname), '"');
163-
appendStringInfoCharMacro(payload, '=');
164-
strcpy_quoted(payload, SPI_getvalue(trigtuple, tupdesc, colno), '\'');
162+
appendStringInfoCharMacro(&payload, ',');
163+
strcpy_quoted(&payload, NameStr(attr->attname), '"');
164+
appendStringInfoCharMacro(&payload, '=');
165+
strcpy_quoted(&payload, SPI_getvalue(trigtuple, tupdesc, colno), '\'');
165166
}
166167

167-
Async_Notify(channel, payload->data);
168+
Async_Notify(channel, payload.data);
168169
}
169170
ReleaseSysCache(indexTuple);
170171
break;

src/backend/access/transam/xlogbackup.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@ build_backup_content(BackupState *state, bool ishistoryfile)
3131
char startstrbuf[128];
3232
char startxlogfile[MAXFNAMELEN]; /* backup start WAL file */
3333
XLogSegNo startsegno;
34-
StringInfo result = makeStringInfo();
35-
char *data;
34+
StringInfoData result;
3635

3736
Assert(state != NULL);
3837

38+
initStringInfo(&result);
39+
3940
/* Use the log timezone here, not the session timezone */
4041
pg_strftime(startstrbuf, sizeof(startstrbuf), "%Y-%m-%d %H:%M:%S %Z",
4142
pg_localtime(&state->starttime, log_timezone));
4243

4344
XLByteToSeg(state->startpoint, startsegno, wal_segment_size);
4445
XLogFileName(startxlogfile, state->starttli, startsegno, wal_segment_size);
45-
appendStringInfo(result, "START WAL LOCATION: %X/%08X (file %s)\n",
46+
appendStringInfo(&result, "START WAL LOCATION: %X/%08X (file %s)\n",
4647
LSN_FORMAT_ARGS(state->startpoint), startxlogfile);
4748

4849
if (ishistoryfile)
@@ -52,18 +53,18 @@ build_backup_content(BackupState *state, bool ishistoryfile)
5253

5354
XLByteToSeg(state->stoppoint, stopsegno, wal_segment_size);
5455
XLogFileName(stopxlogfile, state->stoptli, stopsegno, wal_segment_size);
55-
appendStringInfo(result, "STOP WAL LOCATION: %X/%08X (file %s)\n",
56+
appendStringInfo(&result, "STOP WAL LOCATION: %X/%08X (file %s)\n",
5657
LSN_FORMAT_ARGS(state->stoppoint), stopxlogfile);
5758
}
5859

59-
appendStringInfo(result, "CHECKPOINT LOCATION: %X/%08X\n",
60+
appendStringInfo(&result, "CHECKPOINT LOCATION: %X/%08X\n",
6061
LSN_FORMAT_ARGS(state->checkpointloc));
61-
appendStringInfoString(result, "BACKUP METHOD: streamed\n");
62-
appendStringInfo(result, "BACKUP FROM: %s\n",
62+
appendStringInfoString(&result, "BACKUP METHOD: streamed\n");
63+
appendStringInfo(&result, "BACKUP FROM: %s\n",
6364
state->started_in_recovery ? "standby" : "primary");
64-
appendStringInfo(result, "START TIME: %s\n", startstrbuf);
65-
appendStringInfo(result, "LABEL: %s\n", state->name);
66-
appendStringInfo(result, "START TIMELINE: %u\n", state->starttli);
65+
appendStringInfo(&result, "START TIME: %s\n", startstrbuf);
66+
appendStringInfo(&result, "LABEL: %s\n", state->name);
67+
appendStringInfo(&result, "START TIMELINE: %u\n", state->starttli);
6768

6869
if (ishistoryfile)
6970
{
@@ -73,22 +74,19 @@ build_backup_content(BackupState *state, bool ishistoryfile)
7374
pg_strftime(stopstrfbuf, sizeof(stopstrfbuf), "%Y-%m-%d %H:%M:%S %Z",
7475
pg_localtime(&state->stoptime, log_timezone));
7576

76-
appendStringInfo(result, "STOP TIME: %s\n", stopstrfbuf);
77-
appendStringInfo(result, "STOP TIMELINE: %u\n", state->stoptli);
77+
appendStringInfo(&result, "STOP TIME: %s\n", stopstrfbuf);
78+
appendStringInfo(&result, "STOP TIMELINE: %u\n", state->stoptli);
7879
}
7980

8081
/* either both istartpoint and istarttli should be set, or neither */
8182
Assert(XLogRecPtrIsInvalid(state->istartpoint) == (state->istarttli == 0));
8283
if (!XLogRecPtrIsInvalid(state->istartpoint))
8384
{
84-
appendStringInfo(result, "INCREMENTAL FROM LSN: %X/%08X\n",
85+
appendStringInfo(&result, "INCREMENTAL FROM LSN: %X/%08X\n",
8586
LSN_FORMAT_ARGS(state->istartpoint));
86-
appendStringInfo(result, "INCREMENTAL FROM TLI: %u\n",
87+
appendStringInfo(&result, "INCREMENTAL FROM TLI: %u\n",
8788
state->istarttli);
8889
}
8990

90-
data = result->data;
91-
pfree(result);
92-
93-
return data;
91+
return result.data;
9492
}

src/backend/backup/basebackup.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink,
239239
TimeLineID endtli;
240240
backup_manifest_info manifest;
241241
BackupState *backup_state;
242-
StringInfo tablespace_map;
242+
StringInfoData tablespace_map;
243243

244244
/* Initial backup state, insofar as we know it now. */
245245
state.tablespaces = NIL;
@@ -263,11 +263,11 @@ perform_base_backup(basebackup_options *opt, bbsink *sink,
263263

264264
/* Allocate backup related variables. */
265265
backup_state = (BackupState *) palloc0(sizeof(BackupState));
266-
tablespace_map = makeStringInfo();
266+
initStringInfo(&tablespace_map);
267267

268268
basebackup_progress_wait_checkpoint();
269269
do_pg_backup_start(opt->label, opt->fastcheckpoint, &state.tablespaces,
270-
backup_state, tablespace_map);
270+
backup_state, &tablespace_map);
271271

272272
state.startptr = backup_state->startpoint;
273273
state.starttli = backup_state->starttli;
@@ -342,7 +342,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink,
342342
if (opt->sendtblspcmapfile)
343343
{
344344
sendFileWithContent(sink, TABLESPACE_MAP,
345-
tablespace_map->data, -1, &manifest);
345+
tablespace_map.data, -1, &manifest);
346346
sendtblspclinks = false;
347347
}
348348

@@ -399,7 +399,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink,
399399
endtli = backup_state->stoptli;
400400

401401
/* Deallocate backup-related variables. */
402-
destroyStringInfo(tablespace_map);
402+
pfree(tablespace_map.data);
403403
pfree(backup_state);
404404
}
405405
PG_END_ENSURE_ERROR_CLEANUP(do_pg_abort_backup, BoolGetDatum(false));

src/backend/commands/subscriptioncmds.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -491,20 +491,20 @@ static void
491491
check_publications(WalReceiverConn *wrconn, List *publications)
492492
{
493493
WalRcvExecResult *res;
494-
StringInfo cmd;
494+
StringInfoData cmd;
495495
TupleTableSlot *slot;
496496
List *publicationsCopy = NIL;
497497
Oid tableRow[1] = {TEXTOID};
498498

499-
cmd = makeStringInfo();
500-
appendStringInfoString(cmd, "SELECT t.pubname FROM\n"
499+
initStringInfo(&cmd);
500+
appendStringInfoString(&cmd, "SELECT t.pubname FROM\n"
501501
" pg_catalog.pg_publication t WHERE\n"
502502
" t.pubname IN (");
503-
GetPublicationsStr(publications, cmd, true);
504-
appendStringInfoChar(cmd, ')');
503+
GetPublicationsStr(publications, &cmd, true);
504+
appendStringInfoChar(&cmd, ')');
505505

506-
res = walrcv_exec(wrconn, cmd->data, 1, tableRow);
507-
destroyStringInfo(cmd);
506+
res = walrcv_exec(wrconn, cmd.data, 1, tableRow);
507+
pfree(cmd.data);
508508

509509
if (res->status != WALRCV_OK_TUPLES)
510510
ereport(ERROR,
@@ -535,15 +535,17 @@ check_publications(WalReceiverConn *wrconn, List *publications)
535535
if (list_length(publicationsCopy))
536536
{
537537
/* Prepare the list of non-existent publication(s) for error message. */
538-
StringInfo pubnames = makeStringInfo();
538+
StringInfoData pubnames;
539+
540+
initStringInfo(&pubnames);
539541

540-
GetPublicationsStr(publicationsCopy, pubnames, false);
542+
GetPublicationsStr(publicationsCopy, &pubnames, false);
541543
ereport(WARNING,
542544
errcode(ERRCODE_UNDEFINED_OBJECT),
543545
errmsg_plural("publication %s does not exist on the publisher",
544546
"publications %s do not exist on the publisher",
545547
list_length(publicationsCopy),
546-
pubnames->data));
548+
pubnames.data));
547549
}
548550
}
549551

@@ -2885,12 +2887,13 @@ fetch_relation_list(WalReceiverConn *wrconn, List *publications)
28852887
int server_version = walrcv_server_version(wrconn);
28862888
bool check_columnlist = (server_version >= 150000);
28872889
int column_count = check_columnlist ? 4 : 3;
2888-
StringInfo pub_names = makeStringInfo();
2890+
StringInfoData pub_names;
28892891

28902892
initStringInfo(&cmd);
2893+
initStringInfo(&pub_names);
28912894

28922895
/* Build the pub_names comma-separated string. */
2893-
GetPublicationsStr(publications, pub_names, true);
2896+
GetPublicationsStr(publications, &pub_names, true);
28942897

28952898
/* Get the list of relations from the publisher */
28962899
if (server_version >= 160000)
@@ -2917,7 +2920,7 @@ fetch_relation_list(WalReceiverConn *wrconn, List *publications)
29172920
" FROM pg_publication\n"
29182921
" WHERE pubname IN ( %s )) AS gpt\n"
29192922
" ON gpt.relid = c.oid\n",
2920-
pub_names->data);
2923+
pub_names.data);
29212924

29222925
/* From version 19, inclusion of sequences in the target is supported */
29232926
if (server_version >= 190000)
@@ -2926,7 +2929,7 @@ fetch_relation_list(WalReceiverConn *wrconn, List *publications)
29262929
" SELECT DISTINCT s.schemaname, s.sequencename, " CppAsString2(RELKIND_SEQUENCE) "::\"char\" AS relkind, NULL::int2vector AS attrs\n"
29272930
" FROM pg_catalog.pg_publication_sequences s\n"
29282931
" WHERE s.pubname IN ( %s )",
2929-
pub_names->data);
2932+
pub_names.data);
29302933
}
29312934
else
29322935
{
@@ -2939,10 +2942,10 @@ fetch_relation_list(WalReceiverConn *wrconn, List *publications)
29392942

29402943
appendStringInfo(&cmd, "FROM pg_catalog.pg_publication_tables t\n"
29412944
" WHERE t.pubname IN ( %s )",
2942-
pub_names->data);
2945+
pub_names.data);
29432946
}
29442947

2945-
destroyStringInfo(pub_names);
2948+
pfree(pub_names.data);
29462949

29472950
res = walrcv_exec(wrconn, cmd.data, column_count, tableRow);
29482951
pfree(cmd.data);

0 commit comments

Comments
 (0)