From: Masahiko Sawada Date: Fri, 6 Mar 2026 18:49:50 +0000 (-0800) Subject: Use palloc_object() and palloc_array() in more areas of the logical replication. X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=50ea4e09b6c2331b4cc6434fa12b43bc62de682c;p=thirdparty%2Fpostgresql.git Use palloc_object() and palloc_array() in more areas of the logical replication. The idea is to encourage the use of newer routines across the tree, as these offer stronger type-safety guarantees than raw palloc(). Similar work has been done in commits 1b105f9472bd, 0c3c5c3b06a3, 31d3847a37be, and 4f7dacc5b82a. This commit extends those changes to more locations within src/backend/replication/logical/. Author: Peter Smith Reviewed-by: Chao Li Reviewed-by: Hayato Kuroda Reviewed-by: Masahiko Sawada Discussion: https://postgr.es/m/CAHut+Pv4N7Vpxo18+NAR1r9RGvR8b0BtwTkoeCE2PfFoXgmR6A@mail.gmail.com --- diff --git a/src/backend/replication/logical/proto.c b/src/backend/replication/logical/proto.c index 3950dd0cf46..86ad97cd937 100644 --- a/src/backend/replication/logical/proto.c +++ b/src/backend/replication/logical/proto.c @@ -870,7 +870,7 @@ logicalrep_read_tuple(StringInfo in, LogicalRepTupleData *tuple) natts = pq_getmsgint(in, 2); /* Allocate space for per-column values; zero out unused StringInfoDatas */ - tuple->colvalues = (StringInfoData *) palloc0(natts * sizeof(StringInfoData)); + tuple->colvalues = palloc0_array(StringInfoData, natts); tuple->colstatus = palloc_array(char, natts); tuple->ncols = natts; diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index b7664b90006..4c230bcc8e4 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -2492,7 +2492,7 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn, int nrelations = 0; Relation *relations; - relations = palloc0(nrelids * sizeof(Relation)); + relations = palloc0_array(Relation, nrelids); for (i = 0; i < nrelids; i++) { Oid relid = change->data.truncate.relids[i]; @@ -3518,9 +3518,9 @@ ReorderBufferAccumulateInvalidations(SharedInvalidationMessage **invals_out, else { /* Enlarge the array of inval messages */ - *invals_out = (SharedInvalidationMessage *) - repalloc(*invals_out, sizeof(SharedInvalidationMessage) * - (*ninvals_out + nmsgs_new)); + *invals_out = + repalloc_array(*invals_out, SharedInvalidationMessage, + (*ninvals_out + nmsgs_new)); memcpy(*invals_out + *ninvals_out, msgs_new, nmsgs_new * sizeof(SharedInvalidationMessage)); *ninvals_out += nmsgs_new; diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c index eddb3d9105c..37f0c6028bd 100644 --- a/src/backend/replication/logical/snapbuild.c +++ b/src/backend/replication/logical/snapbuild.c @@ -212,7 +212,7 @@ AllocateSnapshotBuilder(ReorderBuffer *reorder, builder->committed.xcnt = 0; builder->committed.xcnt_space = 128; /* arbitrary number */ builder->committed.xip = - palloc0(builder->committed.xcnt_space * sizeof(TransactionId)); + palloc0_array(TransactionId, builder->committed.xcnt_space); builder->committed.includes_all_transactions = true; builder->catchange.xcnt = 0; @@ -839,8 +839,9 @@ SnapBuildAddCommittedTxn(SnapBuild *builder, TransactionId xid) elog(DEBUG1, "increasing space for committed transactions to %u", (uint32) builder->committed.xcnt_space); - builder->committed.xip = repalloc(builder->committed.xip, - builder->committed.xcnt_space * sizeof(TransactionId)); + builder->committed.xip = repalloc_array(builder->committed.xip, + TransactionId, + builder->committed.xcnt_space); } /* diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c index a0de2efded8..f49a4852ecb 100644 --- a/src/backend/replication/logical/tablesync.c +++ b/src/backend/replication/logical/tablesync.c @@ -901,8 +901,8 @@ fetch_remote_table_info(char *nspname, char *relname, LogicalRepRelation *lrel, nspname, relname, res->err))); /* We don't know the number of rows coming, so allocate enough space. */ - lrel->attnames = palloc0(MaxTupleAttributeNumber * sizeof(char *)); - lrel->atttyps = palloc0(MaxTupleAttributeNumber * sizeof(Oid)); + lrel->attnames = palloc0_array(char *, MaxTupleAttributeNumber); + lrel->atttyps = palloc0_array(Oid, MaxTupleAttributeNumber); lrel->attkeys = NULL; /* diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index 3f7de2efce0..033858752d9 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -978,8 +978,8 @@ slot_fill_defaults(LogicalRepRelMapEntry *rel, EState *estate, if (num_phys_attrs == rel->remoterel.natts) return; - defmap = (int *) palloc(num_phys_attrs * sizeof(int)); - defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *)); + defmap = palloc_array(int, num_phys_attrs); + defexprs = palloc_array(ExprState *, num_phys_attrs); Assert(rel->attrmap->maplen == num_phys_attrs); for (attnum = 0; attnum < num_phys_attrs; attnum++) @@ -5306,8 +5306,8 @@ subxact_info_read(Oid subid, TransactionId xid) * to the subxact file and reset the logical streaming context. */ oldctx = MemoryContextSwitchTo(LogicalStreamingContext); - subxact_data.subxacts = palloc(subxact_data.nsubxacts_max * - sizeof(SubXactInfo)); + subxact_data.subxacts = palloc_array(SubXactInfo, + subxact_data.nsubxacts_max); MemoryContextSwitchTo(oldctx); if (len > 0) @@ -5373,14 +5373,14 @@ subxact_info_add(TransactionId xid) * subxact_info_read. */ oldctx = MemoryContextSwitchTo(LogicalStreamingContext); - subxacts = palloc(subxact_data.nsubxacts_max * sizeof(SubXactInfo)); + subxacts = palloc_array(SubXactInfo, subxact_data.nsubxacts_max); MemoryContextSwitchTo(oldctx); } else if (subxact_data.nsubxacts == subxact_data.nsubxacts_max) { subxact_data.nsubxacts_max *= 2; - subxacts = repalloc(subxacts, - subxact_data.nsubxacts_max * sizeof(SubXactInfo)); + subxacts = repalloc_array(subxacts, SubXactInfo, + subxact_data.nsubxacts_max); } subxacts[subxact_data.nsubxacts].xid = xid;