]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix usage of palloc() in MERGE/SPLIT PARTITION(s) code
authorAlexander Korotkov <akorotkov@postgresql.org>
Sun, 14 Dec 2025 14:10:25 +0000 (16:10 +0200)
committerAlexander Korotkov <akorotkov@postgresql.org>
Sun, 14 Dec 2025 14:10:25 +0000 (16:10 +0200)
f2e4cc427951 and 4b3d173629f4 implement ALTER TABLE ... MERGE/SPLIT
PARTITION(s) commands.  In several places, these commits use palloc(),
where we should use palloc_object() and palloc_array().  This commit
provides appropriate usage of palloc_object() and palloc_array().

Reported-by: Man Zeng <zengman@halodbtech.com>
Discussion: https://postgr.es/m/tencent_3661BB522D5466B33EA33666%40qq.com

src/backend/commands/tablecmds.c
src/backend/partitioning/partbounds.c

index 953fadb9c6b7c86c4297355e7ab59fd798d9d5ac..7550ee7c164d239aaee22a0b2b42748530f2ee86 100644 (file)
@@ -22317,7 +22317,7 @@ createTableConstraints(List **wqueue, AlteredTableInfo *tab,
                         */
                        if (attribute->attgenerated == ATTRIBUTE_GENERATED_STORED)
                        {
-                               newval = (NewColumnValue *) palloc0(sizeof(NewColumnValue));
+                               newval = palloc0_object(NewColumnValue);
                                newval->attnum = num;
                                newval->expr = expression_planner((Expr *) def);
                                newval->is_generated = (attribute->attgenerated != '\0');
@@ -22406,7 +22406,7 @@ createTableConstraints(List **wqueue, AlteredTableInfo *tab,
                        {
                                NewConstraint *newcon;
 
-                               newcon = (NewConstraint *) palloc0(sizeof(NewConstraint));
+                               newcon = palloc0_object(NewConstraint);
                                newcon->name = ccon->name;
                                newcon->contype = CONSTR_CHECK;
                                newcon->qual = qual;
@@ -22944,7 +22944,7 @@ createSplitPartitionContext(Relation partRel)
 {
        SplitPartitionContext *pc;
 
-       pc = (SplitPartitionContext *) palloc0(sizeof(SplitPartitionContext));
+       pc = palloc0_object(SplitPartitionContext);
        pc->partRel = partRel;
 
        /*
index b7f90ae109d6cd2d7fcb70fbe0a56d49fa1488d9..16b0adc172c5fd831d6917b86de2328b5fb24780 100644 (file)
@@ -5115,8 +5115,7 @@ calculate_partition_bound_for_merge(Relation parent,
                                int                     nparts = list_length(partOids);
                                List       *bounds = NIL;
 
-                               lower_bounds = (PartitionRangeBound **)
-                                       palloc0(nparts * sizeof(PartitionRangeBound *));
+                               lower_bounds = palloc0_array(PartitionRangeBound *, nparts);
 
                                /*
                                 * Create an array of lower bounds and a list of
@@ -5755,8 +5754,7 @@ check_partitions_for_split(Relation parent,
         * Make an array new_parts with new partitions except the DEFAULT
         * partition.
         */
-       new_parts = (SinglePartitionSpec **)
-               palloc0(list_length(partlist) * sizeof(SinglePartitionSpec *));
+       new_parts = palloc0_array(SinglePartitionSpec *, list_length(partlist));
 
        /* isSplitPartDefault flag: is split partition a DEFAULT partition? */
        isSplitPartDefault = (defaultPartOid == splitPartOid);
@@ -5786,8 +5784,7 @@ check_partitions_for_split(Relation parent,
                 * all partitions in ascending order of their bounds (we compare the
                 * lower bound only).
                 */
-               lower_bounds = (PartitionRangeBound **)
-                       palloc0(nparts * sizeof(PartitionRangeBound *));
+               lower_bounds = palloc0_array(PartitionRangeBound *, nparts);
 
                /* Create an array of lower bounds. */
                for (i = 0; i < nparts; i++)
@@ -5802,8 +5799,7 @@ check_partitions_for_split(Relation parent,
 
                /* Reorder the array of partitions. */
                tmp_new_parts = new_parts;
-               new_parts = (SinglePartitionSpec **)
-                       palloc0(nparts * sizeof(SinglePartitionSpec *));
+               new_parts = palloc0_array(SinglePartitionSpec *, nparts);
                for (i = 0; i < nparts; i++)
                        new_parts[i] = tmp_new_parts[lower_bounds[i]->index];