pgsql: Implement ALTER TABLE ... MERGE PARTITIONS ... command

From: Alexander Korotkov <akorotkov(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Implement ALTER TABLE ... MERGE PARTITIONS ... command
Date: 2025-12-14 11:30:02
Message-ID: E1vUkIb-000hmq-0B@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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

This new DDL command merges several partitions into a single partition of the
target table. The target partition is created using the new
createPartitionTable() function with the parent partition as the template.

This commit comprises a quite naive implementation which works in a single
process and holds the ACCESS EXCLUSIVE LOCK on the parent table during all
the operations, including the tuple routing. This is why this new DDL
command can't be recommended for large partitioned tables under a high load.
However, this implementation comes in handy in certain cases, even as it is.
Also, it could serve as a foundation for future implementations with less
locking and possibly parallelism.

Discussion: https://postgr.es/m/c73a1746-0cd0-6bdd-6b23-3ae0b7c0c582%40postgrespro.ru
Author: Dmitry Koval <d(dot)koval(at)postgrespro(dot)ru>
Co-authored-by: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Co-authored-by: Tender Wang <tndrwang(at)gmail(dot)com>
Co-authored-by: Richard Guo <guofenglinux(at)gmail(dot)com>
Co-authored-by: Dagfinn Ilmari Mannsaker <ilmari(at)ilmari(dot)org>
Co-authored-by: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
Co-authored-by: Jian He <jian(dot)universality(at)gmail(dot)com>
Reviewed-by: Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>
Reviewed-by: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Reviewed-by: Zhihong Yu <zyu(at)yugabyte(dot)com>
Reviewed-by: Justin Pryzby <pryzby(at)telsasoft(dot)com>
Reviewed-by: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Reviewed-by: Robert Haas <rhaas(at)postgresql(dot)org>
Reviewed-by: Stephane Tachoires <stephane(dot)tachoires(at)gmail(dot)com>
Reviewed-by: Jian He <jian(dot)universality(at)gmail(dot)com>
Reviewed-by: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Reviewed-by: Pavel Borisov <pashkin(dot)elfe(at)gmail(dot)com>
Reviewed-by: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Reviewed-by: Alexander Lakhin <exclusion(at)gmail(dot)com>
Reviewed-by: Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>
Reviewed-by: Daniel Gustafsson <dgustafsson(at)postgresql(dot)org>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Reviewed-by: Noah Misch <noah(at)leadboat(dot)com>

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/f2e4cc427951b7c46629fb7625a22f7898586f3a

Modified Files
--------------
doc/src/sgml/ddl.sgml | 19 +
doc/src/sgml/ref/alter_table.sgml | 127 ++-
src/backend/catalog/dependency.c | 54 +-
src/backend/catalog/pg_constraint.c | 2 +-
src/backend/commands/tablecmds.c | 894 +++++++++++++++-
src/backend/parser/gram.y | 22 +-
src/backend/parser/parse_utilcmd.c | 152 ++-
src/backend/partitioning/partbounds.c | 196 ++++
src/bin/psql/tab-complete.in.c | 10 +
src/include/catalog/dependency.h | 2 +
src/include/nodes/parsenodes.h | 8 +-
src/include/parser/kwlist.h | 1 +
src/include/partitioning/partbounds.h | 6 +
src/test/isolation/expected/partition-merge.out | 243 +++++
src/test/isolation/isolation_schedule | 1 +
src/test/isolation/specs/partition-merge.spec | 62 ++
.../test_ddl_deparse/expected/alter_table.out | 5 +
.../modules/test_ddl_deparse/sql/alter_table.sql | 3 +
.../modules/test_ddl_deparse/test_ddl_deparse.c | 3 +
src/test/regress/expected/partition_merge.out | 1097 ++++++++++++++++++++
src/test/regress/parallel_schedule | 2 +-
src/test/regress/sql/partition_merge.sql | 791 ++++++++++++++
22 files changed, 3668 insertions(+), 32 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Alexander Korotkov 2025-12-14 14:10:53 pgsql: Fix usage of palloc() in MERGE/SPLIT PARTITION(s) code
Previous Message Michael Paquier 2025-12-14 02:24:24 pgsql: doc: Fix incorrect documentation for test_custom_stats