@ -3,6 +3,7 @@
-- Tests for "ALTER TABLE ... MERGE PARTITIONS ..." command
--
CREATE SCHEMA partitions_merge_schema;
CREATE SCHEMA partitions_merge_schema2;
SET search_path = partitions_merge_schema, public;
--
-- BY RANGE partitioning
@ -36,18 +37,23 @@ ERROR: lower bound of partition "sales_mar2022" conflicts with upper bound of p
-- (space between sections sales_dec2021 and sales_jan2022)
ALTER TABLE sales_range MERGE PARTITIONS (sales_dec2021, sales_jan2022, sales_feb2022) INTO sales_dec_jan_feb2022;
ERROR: lower bound of partition "sales_jan2022" conflicts with upper bound of previous partition "sales_dec2021"
-- NO ERROR: test for custom partitions order
ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022, sales_mar2022, sales_jan2022) INTO sales_jan_feb_mar2022;
-- NO ERROR: test for custom partitions order, source partitions not in the search_path
SET search_path = partitions_merge_schema2, public;
ALTER TABLE partitions_merge_schema.sales_range MERGE PARTITIONS (
partitions_merge_schema.sales_feb2022,
partitions_merge_schema.sales_mar2022,
partitions_merge_schema.sales_jan2022) INTO sales_jan_feb_mar2022;
SET search_path = partitions_merge_schema, public;
SELECT c.oid::pg_catalog.regclass, c.relkind, inhdetachpending, pg_catalog.pg_get_expr(c.relpartbound, c.oid)
FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
WHERE c.oid = i.inhrelid AND i.inhparent = 'sales_range'::regclass
ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
oid | relkind | inhdetachpending | pg_get_expr
-----------------------+---------+------------------+--------------------------------------------------
sales_apr2022 | p | f | FOR VALUES FROM ('04-01-2022') TO ('05 -01-2022')
sales_dec2021 | r | f | FOR VALUES FROM ('12-01-2021') TO ('12-31-2021 ')
sales_jan_feb_mar2022 | r | f | FOR VALUES FROM ('01-01-2022') TO ('04-01-2022 ')
sales_others | r | f | DEFAULT
oid | relkind | inhdetachpending | pg_get_expr
------------------------------------------------ +---------+------------------+--------------------------------------------------
partitions_merge_schema2.sales_jan_feb_mar2022 | r | f | FOR VALUES FROM ('01-01-2022') TO ('04 -01-2022')
sales_apr2022 | p | f | FOR VALUES FROM ('04-01-2022') TO ('05-01-2022 ')
sales_dec2021 | r | f | FOR VALUES FROM ('12-01-2021') TO ('12-31-2021 ')
sales_others | r | f | DEFAULT
(4 rows)
DROP TABLE sales_range;
@ -95,23 +101,24 @@ SELECT c.oid::pg_catalog.regclass, c.relkind, inhdetachpending, pg_catalog.pg_ge
sales_others | r | f | DEFAULT
(5 rows)
ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022, sales_mar2022, sales_apr2022) INTO sales_feb_mar_apr2022;
-- check schema-qualified name of the new partition
ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022, sales_mar2022, sales_apr2022) INTO partitions_merge_schema2.sales_feb_mar_apr2022;
-- show partitions with conditions:
SELECT c.oid::pg_catalog.regclass, c.relkind, inhdetachpending, pg_catalog.pg_get_expr(c.relpartbound, c.oid)
FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
WHERE c.oid = i.inhrelid AND i.inhparent = 'sales_range'::regclass
ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
oid | relkind | inhdetachpending | pg_get_expr
-----------------------+---------+------------------+--------------------------------------------------
sales_feb_mar_apr2022 | r | f | FOR VALUES FROM ('02-01-2022') TO ('05-01-2022')
sales_jan2022 | r | f | FOR VALUES FROM ('01-01-2022') TO ('02-01-2022')
sales_others | r | f | DEFAULT
oid | relkind | inhdetachpending | pg_get_expr
------------------------------------------------ +---------+------------------+--------------------------------------------------
partitions_merge_schema2. sales_feb_mar_apr2022 | r | f | FOR VALUES FROM ('02-01-2022') TO ('05-01-2022')
sales_jan2022 | r | f | FOR VALUES FROM ('01-01-2022') TO ('02-01-2022')
sales_others | r | f | DEFAULT
(3 rows)
SELECT * FROM pg_indexes WHERE tablename = 'sales_feb_mar_apr2022' and schemaname = 'partitions_merge_schema';
schemaname | tablename | indexname | tablespace | indexdef
-------------------------+-----------------------+--------------------------------------+------------+-----------------------------------------------------------------------------------------------------------------------------
partitions_merge_schema | sales_feb_mar_apr2022 | sales_feb_mar_apr2022_sales_date_idx | | CREATE INDEX sales_feb_mar_apr2022_sales_date_idx ON partitions_merge_schema.sales_feb_mar_apr2022 USING btree (sales_date)
SELECT * FROM pg_indexes WHERE tablename = 'sales_feb_mar_apr2022' and schemaname = 'partitions_merge_schema2 ';
schemaname | tablename | indexname | tablespace | indexdef
-------------------------- +-----------------------+--------------------------------------+------------+- -----------------------------------------------------------------------------------------------------------------------------
partitions_merge_schema2 | sales_feb_mar_apr2022 | sales_feb_mar_apr2022_sales_date_idx | | CREATE INDEX sales_feb_mar_apr2022_sales_date_idx ON partitions_merge_schema2 .sales_feb_mar_apr2022 USING btree (sales_date)
(1 row)
SELECT * FROM sales_range;
@ -141,7 +148,7 @@ SELECT * FROM sales_jan2022;
13 | Gandi | 377 | 01-09-2022
(3 rows)
SELECT * FROM sales_feb_mar_apr2022;
SELECT * FROM partitions_merge_schema2. sales_feb_mar_apr2022;
salesman_id | salesman_name | sales_amount | sales_date
-------------+---------------+--------------+------------
2 | Smirnoff | 500 | 02-10-2022
@ -164,7 +171,7 @@ SELECT * FROM sales_others;
-- Use indexscan for testing indexes
SET enable_seqscan = OFF;
SELECT * FROM sales_feb_mar_apr2022 where sales_date > '2022-01-01';
SELECT * FROM partitions_merge_schema2. sales_feb_mar_apr2022 where sales_date > '2022-01-01';
salesman_id | salesman_name | sales_amount | sales_date
-------------+---------------+--------------+------------
2 | Smirnoff | 500 | 02-10-2022
@ -746,6 +753,34 @@ DROP TABLE t3;
DROP TABLE t2;
DROP TABLE t1;
--
-- Try to MERGE partitions of temporary table.
--
CREATE TEMP TABLE t (i int) PARTITION BY RANGE (i);
CREATE TEMP TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
CREATE TEMP TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid), c.relpersistence
FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
WHERE c.oid = i.inhrelid AND i.inhparent = 't'::regclass
ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
oid | pg_get_expr | relpersistence
--------+----------------------------+----------------
tp_0_1 | FOR VALUES FROM (0) TO (1) | t
tp_1_2 | FOR VALUES FROM (1) TO (2) | t
(2 rows)
ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
-- Partition should be temporary.
SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid), c.relpersistence
FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
WHERE c.oid = i.inhrelid AND i.inhparent = 't'::regclass
ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
oid | pg_get_expr | relpersistence
--------+----------------------------+----------------
tp_0_2 | FOR VALUES FROM (0) TO (2) | t
(1 row)
DROP TABLE t;
--
-- Check the partition index name if the partition name is the same as one
-- of the merged partitions.
--
@ -771,4 +806,63 @@ Not-null constraints:
DROP TABLE t;
--
-- Try mixing permanent and temporary partitions.
--
SET search_path = partitions_merge_schema, pg_temp, public;
CREATE TABLE t (i int) PARTITION BY RANGE (i);
CREATE TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
CREATE TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
SELECT c.oid::pg_catalog.regclass, c.relpersistence FROM pg_catalog.pg_class c WHERE c.oid = 't'::regclass;
oid | relpersistence
-----+----------------
t | p
(1 row)
SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid), c.relpersistence
FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
WHERE c.oid = i.inhrelid AND i.inhparent = 't'::regclass
ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
oid | pg_get_expr | relpersistence
--------+----------------------------+----------------
tp_0_1 | FOR VALUES FROM (0) TO (1) | p
tp_1_2 | FOR VALUES FROM (1) TO (2) | p
(2 rows)
SET search_path = pg_temp, partitions_merge_schema, public;
-- Can't merge persistent partitions into a temporary partition
ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
ERROR: cannot create a temporary relation as partition of permanent relation "t"
SET search_path = partitions_merge_schema, public;
-- Can't merge persistent partitions into a temporary partition
ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO pg_temp.tp_0_2;
ERROR: cannot create a temporary relation as partition of permanent relation "t"
DROP TABLE t;
SET search_path = pg_temp, partitions_merge_schema, public;
BEGIN;
CREATE TABLE t (i int) PARTITION BY RANGE (i);
CREATE TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
CREATE TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
SELECT c.oid::pg_catalog.regclass, c.relpersistence FROM pg_catalog.pg_class c WHERE c.oid = 't'::regclass;
oid | relpersistence
-----+----------------
t | t
(1 row)
SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid), c.relpersistence
FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
WHERE c.oid = i.inhrelid AND i.inhparent = 't'::regclass
ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
oid | pg_get_expr | relpersistence
--------+----------------------------+----------------
tp_0_1 | FOR VALUES FROM (0) TO (1) | t
tp_1_2 | FOR VALUES FROM (1) TO (2) | t
(2 rows)
SET search_path = partitions_merge_schema, pg_temp, public;
-- Can't merge temporary partitions into a persistent partition
ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
ROLLBACK;
RESET search_path;
--
DROP SCHEMA partitions_merge_schema;
DROP SCHEMA partitions_merge_schema2;