Re: Add SPLIT PARTITION/MERGE PARTITIONS commands

jian he <jian.universality@gmail.com>

From: jian he <jian.universality@gmail.com>
To: Dmitry Koval <d.koval@postgrespro.ru>
Cc: pgsql-hackers@lists.postgresql.org, Alexander Korotkov <aekorotkov@gmail.com>
Date: 2025-06-05T04:22:40Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Adjust errcode in checkPartition()

  2. Fix usage of palloc() in MERGE/SPLIT PARTITION(s) code

  3. Implement ALTER TABLE ... SPLIT PARTITION ... command

  4. Implement ALTER TABLE ... MERGE PARTITIONS ... command

  5. Calculate agglevelsup correctly when Aggref contains a CTE.

  6. Use PqMsg_* macros in applyparallelworker.c.

  7. Restrict psql meta-commands in plain-text dumps.

  8. Ensure we have a snapshot when updating various system catalogs.

  9. Use specific collation where needed in new test

  10. Expand virtual generated columns in the planner

  11. Virtual generated columns

  12. Define PG_LOGICAL_DIR for path pg_logical/ in data folder

  13. Revert support for ALTER TABLE ... MERGE/SPLIT PARTITION(S) commands

  14. Avoid repeated table name lookups in createPartitionTable()

  15. Provide deterministic order for catalog queries in partition_split.sql

  16. Don't copy extended statistics during MERGE/SPLIT partition operations

  17. Fix the name collision detection in MERGE/SPLIT partition operations

  18. Fix regression tests conflict in 3ca43dbbb6

  19. Add permission check for MERGE/SPLIT partition operations

  20. Fix one more portability shortcoming in new test_pg_dump test.

  21. Inherit parent's AM for partition MERGE/SPLIT operations

  22. Add tab completion for partition MERGE/SPLIT operations

  23. Rename tables in tests of partition MERGE/SPLIT operations

  24. Make new partitions with parent's persistence during MERGE/SPLIT

  25. Document the way partition MERGE/SPLIT operations create new partitions

  26. Change the way ATExecMergePartitions() handles the name collision

  27. Grammar fixes for split/merge partitions code

  28. Checks for ALTER TABLE ... SPLIT/MERGE PARTITIONS ... commands

  29. Fix some grammer errors from error messages and codes comments

  30. Support TZ and OF format codes in to_timestamp().

  31. Support identity columns in partitioned tables

  32. Fix indentation in twophase.c

  33. Fix corner-case planner failure for MERGE.

  34. Doc: fix documentation example for bytea hex output format.

  35. Avoid repeated name lookups during table and index DDL.

Attachments

hi.

the following are review of
v40-0001-Implement-ALTER-TABLE-.-MERGE-PARTITIONS-.-comma.patch

ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022,
sales_mar2022) INTO sales_feb_mar_apr2022;
There are no tests when sales_feb2022 or sales_mar2022 have any constraints.
a partition can have its own constraint.

What should we do when any to be merged partition has constraints?
----------------------------------------------------------------
DROP TABLE IF EXISTS sales_range cascade;
CREATE TABLE sales_range (salesperson_id INT, salesperson_name text,
sales_amount INT generated always as (1) stored, sales_date DATE)
PARTITION BY RANGE (sales_date);
CREATE TABLE sales_feb2022 PARTITION OF sales_range FOR VALUES FROM
('2022-02-01') TO ('2022-03-01');
CREATE TABLE sales_mar2022 PARTITION OF sales_range FOR VALUES FROM
('2022-03-01') TO ('2022-04-01');
ALTER TABLE sales_feb2022 ALTER COLUMN sales_amount SET EXPRESSION AS (10);
ALTER TABLE sales_mar2022 ALTER COLUMN sales_amount SET EXPRESSION AS (20);

ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022,
sales_mar2022) INTO sales_feb_mar2022;
with v40, sales_feb_mar2022 column sales_int generated expression is
(generated always as (1) stored)

maybe this is what we expected.
but we should have some tests on it.
----------------------------------------------------------------
DROP TABLE IF EXISTS sales_range cascade;
CREATE TABLE sales_range (salesperson_id INT, salesperson_name text,
sales_amount INT, sales_date DATE) PARTITION BY RANGE (sales_date);
CREATE TABLE sales_feb2022 PARTITION OF sales_range FOR VALUES FROM
('2022-02-01') TO ('2022-03-01');
CREATE TABLE sales_mar2022 PARTITION OF sales_range FOR VALUES FROM
('2022-03-01') TO ('2022-04-01');

CREATE VIEW x AS SELECT * FROM sales_mar2022;
ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022,
sales_mar2022) INTO sales_feb_mar2022;
ERROR:  cannot drop table public.sales_mar2022 because other objects
depend on it
DETAIL:  view public.x depends on table public.sales_mar2022
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

Maybe this is expected, but we need to mention it somewhere and have
some tests on it.
saying that MERGE PARTITIONS will effectively drop the partitions, so
if any object depends on that partition
then MERGE PARTITIONS can not be done.
----------------------------------------------------------------
+ */
+static Relation
+createPartitionTable(RangeVar *newPartName, Relation modelRel, Oid ownerId)
+{
+ Relation newRel;
+ Oid newRelId;
+ TupleDesc descriptor;
+ List   *colList = NIL;
+ Oid relamId;
+ Oid namespaceId;
+
+ /* If existing rel is temp, it must belong to this session */
+ if (modelRel->rd_rel->relpersistence == RELPERSISTENCE_TEMP &&
+ !modelRel->rd_islocaltemp)
+ ereport(ERROR,
+ errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("cannot create as partition of temporary relation of another
session"));

Looking at it, modelRel is the partitioned table we called ALTER TABLE.
for example:
ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022,
sales_mar2022) INTO sales_feb_mar2022;
modelRel is sales_range.

so this error check can be performed as early as the
transformPartitionCmdForMerge stage?
----------------------------------------------------------------
+ /* Look up the access method for new relation. */
+ relamId = (modelRel->rd_rel->relam != InvalidOid) ?
modelRel->rd_rel->relam : HEAP_TABLE_AM_OID;
looking at the output of "select * from pg_am;".

i think, we can do the following way:
if (modelRel->rd_rel->relam)
elog(ERROR, "error");
relamId = modelRel->rd_rel->relam;
----------------------------------------------------------------
Attached is some refactoring in moveMergedTablesRows, hope it's straightforward.

for example:
+ /* Extract data from old tuple. */
+ slot_getallattrs(srcslot);
+
+ if (tuple_map)
+ {
+ /* Need to use map to copy attributes. */
+ insertslot = execute_attr_map_slot(tuple_map->attrMap, srcslot, dstslot);
+ }
execute_attr_map_slot will call "slot_getallattrs(srcslot);" so the
first one is unncessary.

+ srcslot = MakeSingleTupleTableSlot(RelationGetDescr(mergingPartition),
+   table_slot_callbacks(mergingPartition));
can change to
srcslot = table_slot_create(mergingPartition, NULL);