Re: Add SPLIT PARTITION/MERGE PARTITIONS commands - Mailing list pgsql-hackers

From jian he
Subject Re: Add SPLIT PARTITION/MERGE PARTITIONS commands
Date
Msg-id CACJufxHRX6xQtzn+KenK3SDR2+4kUpVnO+0axforV_pohwMnFw@mail.gmail.com
Whole thread Raw
In response to Re: Add SPLIT PARTITION/MERGE PARTITIONS commands  (Dmitry Koval <d.koval@postgrespro.ru>)
List pgsql-hackers
On Tue, Jun 10, 2025 at 6:48 AM Dmitry Koval <d.koval@postgrespro.ru> wrote:
> 3.
>  >i think, we can do the following way:
>  >if (modelRel->rd_rel->relam)
>  >  elog(ERROR, "error");
>  >relamId = modelRel->rd_rel->relam;
>
> Can you clarify what is reason to change the current AM-logic for
> creating a new partition?
>
> +       /* Look up the access method for new relation. */
> +       relamId = (modelRel->rd_rel->relam != InvalidOid) ?
> modelRel->rd_rel->relam : HEAP_TABLE_AM_OID;
>
> (If AM is set for a partitioned table, then use it, otherwise use AM for
> heap tables.)
>
I only want to allow HEAP_TABLE_AM_OID to be used
in the merge partition,
I guess that would avoid unintended consequences.

I proposed change was
+if (modelRel->rd_rel->relam != HEAP_TABLE_AM_OID)
+   elog(ERROR, "only heap table method is allowed");
+ relamId = modelRel->rd_rel->relam;


RangeVarGetAndCheckCreationNamespace
was called first on ATExecMergePartitions, then on createPartitionTable.
Maybe we can pass the first  ATExecMergePartitions call result
to createPartitionTable to avoid calling it twice.


CREATE TABLE pp (a int, b int) PARTITION BY LIST(a);
CREATE TABLE pp_p1 PARTITION OF pp FOR VALUES IN (1, 2);
CREATE TABLE pp_p2 PARTITION OF pp FOR VALUES IN (3, 4);
INSERT INTO pp(a, b) SELECT random(min=>1, max=>6),
random(min=>1::int, max=>10) FROM generate_series(0, 4) i;
alter table pp add constraint cc check(a < 0) not valid;

ALTER TABLE pp MERGE PARTITIONS (pp_p1,  pp_p2) INTO pp_p1_2;
src4=# \d+ pp_p1_2
                                         Table "public.pp_p1_2"
 Column |  Type   | Collation | Nullable | Default | Storage |
Compression | Stats target | Description
--------+---------+-----------+----------+---------+---------+-------------+--------------+-------------
 a      | integer |           |          |         | plain   |
    |              |
 b      | integer |           |          |         | plain   |
    |              |
Partition of: pp FOR VALUES IN (1, 2, 3, 4)
Partition constraint: ((a IS NOT NULL) AND (a = ANY (ARRAY[1, 2, 3, 4])))
Check constraints:
    "cc" CHECK (a < 0)
Access method: heap

constraint cc on pp_p1_2 should be NOT VALID.
also if the partitioned table has NOT ENFORCED CHECK constraint, it
will cause segfault.
attached is a possible fix, and related tests.(based on v42).


cosmetic changes:
many of the "forach" can change to "foreach_node".
for example in ATExecMergePartitions.
we can change
``foreach(listptr, cmd->partlist)``
to
``foreach_node(RangeVar, name, cmd->partlist)`

Attachment

pgsql-hackers by date:

Previous
From: Shinya Kato
Date:
Subject: Re: Extend COPY FROM with HEADER to skip multiple lines
Next
From: Mihail Nikalayeu
Date:
Subject: Re: [BUG?] check_exclusion_or_unique_constraint false negative