postgresql/src/test/modules/test_ddl_deparse/sql/alter_table.sql
Alexander Korotkov 4b3d173629 Implement ALTER TABLE ... SPLIT PARTITION ... command
This new DDL command splits a single partition into several partitions.  Just
like the ALTER TABLE ... MERGE PARTITIONS ... command, new partitions are
created using the 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 the new DDL command
can't be recommended for large, partitioned tables under 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.koval@postgrespro.ru>
Co-authored-by: Alexander Korotkov <aekorotkov@gmail.com>
Co-authored-by: Tender Wang <tndrwang@gmail.com>
Co-authored-by: Richard Guo <guofenglinux@gmail.com>
Co-authored-by: Dagfinn Ilmari Mannsaker <ilmari@ilmari.org>
Co-authored-by: Fujii Masao <masao.fujii@gmail.com>
Co-authored-by: Jian He <jian.universality@gmail.com>
Reviewed-by: Matthias van de Meent <boekewurm+postgres@gmail.com>
Reviewed-by: Laurenz Albe <laurenz.albe@cybertec.at>
Reviewed-by: Zhihong Yu <zyu@yugabyte.com>
Reviewed-by: Justin Pryzby <pryzby@telsasoft.com>
Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org>
Reviewed-by: Robert Haas <rhaas@postgresql.org>
Reviewed-by: Stephane Tachoires <stephane.tachoires@gmail.com>
Reviewed-by: Jian He <jian.universality@gmail.com>
Reviewed-by: Alexander Korotkov <aekorotkov@gmail.com>
Reviewed-by: Pavel Borisov <pashkin.elfe@gmail.com>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Reviewed-by: Alexander Lakhin <exclusion@gmail.com>
Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Reviewed-by: Daniel Gustafsson <dgustafsson@postgresql.org>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Noah Misch <noah@leadboat.com>
2025-12-14 13:29:38 +02:00

85 lines
2.4 KiB
SQL

CREATE TABLE parent (
a int
);
ALTER TABLE parent SET (fillfactor = 50);
ALTER TABLE parent RESET (fillfactor);
ALTER TABLE parent SET UNLOGGED;
ALTER TABLE parent SET LOGGED;
CREATE INDEX parent_index ON parent(a);
ALTER TABLE parent CLUSTER ON parent_index;
DROP INDEX parent_index;
CREATE TABLE child () INHERITS (parent);
CREATE TABLE grandchild () INHERITS (child);
ALTER TABLE parent ADD COLUMN b serial;
ALTER TABLE parent RENAME COLUMN b TO c;
-- Constraint, no recursion
ALTER TABLE ONLY grandchild ADD CONSTRAINT a_pos CHECK (a > 0);
-- Constraint, with recursion
ALTER TABLE parent ADD CONSTRAINT a_pos CHECK (a > 0);
ALTER TABLE parent ALTER COLUMN a SET NOT NULL;
ALTER TABLE parent ALTER COLUMN a DROP NOT NULL;
ALTER TABLE parent ALTER COLUMN a SET NOT NULL;
ALTER TABLE parent ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;
ALTER TABLE parent ALTER COLUMN a SET GENERATED BY DEFAULT;
ALTER TABLE parent ALTER COLUMN a DROP IDENTITY;
ALTER TABLE parent ALTER COLUMN a SET STATISTICS 100;
ALTER TABLE parent ALTER COLUMN a SET STORAGE PLAIN;
ALTER TABLE parent ENABLE ROW LEVEL SECURITY;
ALTER TABLE parent NO FORCE ROW LEVEL SECURITY;
ALTER TABLE parent FORCE ROW LEVEL SECURITY;
ALTER TABLE parent DISABLE ROW LEVEL SECURITY;
CREATE STATISTICS parent_stat (dependencies) ON a, c FROM parent;
ALTER TABLE parent ALTER COLUMN c TYPE numeric;
ALTER TABLE parent ALTER COLUMN c SET DEFAULT 0;
CREATE TABLE part (
a int
) PARTITION BY RANGE (a);
CREATE TABLE part1 PARTITION OF part FOR VALUES FROM (1) to (100);
CREATE TABLE part2 (a int);
ALTER TABLE part ATTACH PARTITION part2 FOR VALUES FROM (101) to (200);
ALTER TABLE part DETACH PARTITION part2;
DROP TABLE part2;
CREATE TABLE part2 PARTITION OF part FOR VALUES FROM (100) to (200);
ALTER TABLE part MERGE PARTITIONS (part1, part2) INTO part1;
ALTER TABLE part SPLIT PARTITION part1 INTO
(PARTITION part1 FOR VALUES FROM (1) to (100),
PARTITION part2 FOR VALUES FROM (100) to (200));
ALTER TABLE part ADD PRIMARY KEY (a);
CREATE TABLE tbl (
a int generated always as (b::int * 2) stored,
b text
);
ALTER TABLE tbl ALTER COLUMN a SET EXPRESSION AS (b::int * 3);
ALTER TABLE tbl ALTER COLUMN a DROP EXPRESSION;
ALTER TABLE tbl ALTER COLUMN b SET COMPRESSION pglz;
CREATE TYPE comptype AS (r float8);
CREATE DOMAIN dcomptype AS comptype;
ALTER DOMAIN dcomptype ADD CONSTRAINT c1 check ((value).r > 0);
ALTER TYPE comptype ALTER ATTRIBUTE r TYPE bigint;