]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Use palloc_object() and palloc_array() in more areas of the logical replication.
authorMasahiko Sawada <msawada@postgresql.org>
Fri, 6 Mar 2026 18:49:50 +0000 (10:49 -0800)
committerMasahiko Sawada <msawada@postgresql.org>
Fri, 6 Mar 2026 18:49:50 +0000 (10:49 -0800)
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 1b105f9472bd0c3c5c3b06a3,
31d3847a37be, and 4f7dacc5b82a. This commit extends those changes to
more locations within src/backend/replication/logical/.

Author: Peter Smith <smithpb2250@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Discussion: https://postgr.es/m/CAHut+Pv4N7Vpxo18+NAR1r9RGvR8b0BtwTkoeCE2PfFoXgmR6A@mail.gmail.com

src/backend/replication/logical/proto.c
src/backend/replication/logical/reorderbuffer.c
src/backend/replication/logical/snapbuild.c
src/backend/replication/logical/tablesync.c
src/backend/replication/logical/worker.c

index 3950dd0cf464c97e51e8df300cc378d6eee9afa9..86ad97cd937b3dcd1143edd0545f3c3c02af1626 100644 (file)
@@ -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;
 
index b7664b9000612a1be4bb0e6b5b2f143a4ce31b4e..4c230bcc8e4811e6281995c59235b8920d24b24e 100644 (file)
@@ -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;
index eddb3d9105cac3a4238aee1309742c4f70ab960f..37f0c6028bd2efd5a79c4528c5bd2824ece5e659 100644 (file)
@@ -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);
        }
 
        /*
index a0de2efded8c246365d6cc00315ad746274b7aee..f49a4852ecb3054d0cd5f966464d122c7cf89cf8 100644 (file)
@@ -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;
 
        /*
index 3f7de2efce05356ebdfcc486cd5f39e1ef482058..033858752d978e06f55000f972a1b2668e4f357c 100644 (file)
@@ -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;