3
3
-- Tests for "ALTER TABLE ... MERGE PARTITIONS ..." command
4
4
--
5
5
CREATE SCHEMA partitions_merge_schema;
6
+ CREATE SCHEMA partitions_merge_schema2;
6
7
SET search_path = partitions_merge_schema, public;
7
8
--
8
9
-- BY RANGE partitioning
@@ -36,18 +37,23 @@ ERROR: lower bound of partition "sales_mar2022" conflicts with upper bound of p
36
37
-- (space between sections sales_dec2021 and sales_jan2022)
37
38
ALTER TABLE sales_range MERGE PARTITIONS (sales_dec2021, sales_jan2022, sales_feb2022) INTO sales_dec_jan_feb2022;
38
39
ERROR: lower bound of partition "sales_jan2022" conflicts with upper bound of previous partition "sales_dec2021"
39
- -- NO ERROR: test for custom partitions order
40
- ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022, sales_mar2022, sales_jan2022) INTO sales_jan_feb_mar2022;
40
+ -- NO ERROR: test for custom partitions order, source partitions not in the search_path
41
+ SET search_path = partitions_merge_schema2, public;
42
+ ALTER TABLE partitions_merge_schema.sales_range MERGE PARTITIONS (
43
+ partitions_merge_schema.sales_feb2022,
44
+ partitions_merge_schema.sales_mar2022,
45
+ partitions_merge_schema.sales_jan2022) INTO sales_jan_feb_mar2022;
46
+ SET search_path = partitions_merge_schema, public;
41
47
SELECT c.oid::pg_catalog.regclass, c.relkind, inhdetachpending, pg_catalog.pg_get_expr(c.relpartbound, c.oid)
42
48
FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
43
49
WHERE c.oid = i.inhrelid AND i.inhparent = 'sales_range'::regclass
44
50
ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
45
- oid | relkind | inhdetachpending | pg_get_expr
46
- -----------------------+---------+------------------+--------------------------------------------------
47
- sales_apr2022 | p | f | FOR VALUES FROM ('04 -01-2022') TO ('05 -01-2022')
48
- sales_dec2021 | r | f | FOR VALUES FROM ('12 -01-2021 ') TO ('12-31-2021 ')
49
- sales_jan_feb_mar2022 | r | f | FOR VALUES FROM ('01 -01-2022 ') TO ('04-01-2022 ')
50
- sales_others | r | f | DEFAULT
51
+ oid | relkind | inhdetachpending | pg_get_expr
52
+ ------------------------------------------------ +---------+------------------+--------------------------------------------------
53
+ partitions_merge_schema2.sales_jan_feb_mar2022 | r | f | FOR VALUES FROM ('01 -01-2022') TO ('04 -01-2022')
54
+ sales_apr2022 | p | f | FOR VALUES FROM ('04 -01-2022 ') TO ('05-01-2022 ')
55
+ sales_dec2021 | r | f | FOR VALUES FROM ('12 -01-2021 ') TO ('12-31-2021 ')
56
+ sales_others | r | f | DEFAULT
51
57
(4 rows)
52
58
53
59
DROP TABLE sales_range;
@@ -95,23 +101,24 @@ SELECT c.oid::pg_catalog.regclass, c.relkind, inhdetachpending, pg_catalog.pg_ge
95
101
sales_others | r | f | DEFAULT
96
102
(5 rows)
97
103
98
- ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022, sales_mar2022, sales_apr2022) INTO sales_feb_mar_apr2022;
104
+ -- check schema-qualified name of the new partition
105
+ ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022, sales_mar2022, sales_apr2022) INTO partitions_merge_schema2.sales_feb_mar_apr2022;
99
106
-- show partitions with conditions:
100
107
SELECT c.oid::pg_catalog.regclass, c.relkind, inhdetachpending, pg_catalog.pg_get_expr(c.relpartbound, c.oid)
101
108
FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
102
109
WHERE c.oid = i.inhrelid AND i.inhparent = 'sales_range'::regclass
103
110
ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
104
- oid | relkind | inhdetachpending | pg_get_expr
105
- -----------------------+---------+------------------+--------------------------------------------------
106
- sales_feb_mar_apr2022 | r | f | FOR VALUES FROM ('02-01-2022') TO ('05-01-2022')
107
- sales_jan2022 | r | f | FOR VALUES FROM ('01-01-2022') TO ('02-01-2022')
108
- sales_others | r | f | DEFAULT
111
+ oid | relkind | inhdetachpending | pg_get_expr
112
+ ------------------------------------------------ +---------+------------------+--------------------------------------------------
113
+ partitions_merge_schema2. sales_feb_mar_apr2022 | r | f | FOR VALUES FROM ('02-01-2022') TO ('05-01-2022')
114
+ sales_jan2022 | r | f | FOR VALUES FROM ('01-01-2022') TO ('02-01-2022')
115
+ sales_others | r | f | DEFAULT
109
116
(3 rows)
110
117
111
- SELECT * FROM pg_indexes WHERE tablename = 'sales_feb_mar_apr2022' and schemaname = 'partitions_merge_schema ';
112
- schemaname | tablename | indexname | tablespace | indexdef
113
- -------------------------+-----------------------+--------------------------------------+------------+-----------------------------------------------------------------------------------------------------------------------------
114
- 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)
118
+ SELECT * FROM pg_indexes WHERE tablename = 'sales_feb_mar_apr2022' and schemaname = 'partitions_merge_schema2 ';
119
+ schemaname | tablename | indexname | tablespace | indexdef
120
+ -------------------------- +-----------------------+--------------------------------------+------------+- -----------------------------------------------------------------------------------------------------------------------------
121
+ 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)
115
122
(1 row)
116
123
117
124
SELECT * FROM sales_range;
@@ -141,7 +148,7 @@ SELECT * FROM sales_jan2022;
141
148
13 | Gandi | 377 | 01-09-2022
142
149
(3 rows)
143
150
144
- SELECT * FROM sales_feb_mar_apr2022;
151
+ SELECT * FROM partitions_merge_schema2. sales_feb_mar_apr2022;
145
152
salesman_id | salesman_name | sales_amount | sales_date
146
153
-------------+---------------+--------------+------------
147
154
2 | Smirnoff | 500 | 02-10-2022
@@ -164,7 +171,7 @@ SELECT * FROM sales_others;
164
171
165
172
-- Use indexscan for testing indexes
166
173
SET enable_seqscan = OFF;
167
- SELECT * FROM sales_feb_mar_apr2022 where sales_date > '2022-01-01';
174
+ SELECT * FROM partitions_merge_schema2. sales_feb_mar_apr2022 where sales_date > '2022-01-01';
168
175
salesman_id | salesman_name | sales_amount | sales_date
169
176
-------------+---------------+--------------+------------
170
177
2 | Smirnoff | 500 | 02-10-2022
@@ -746,6 +753,34 @@ DROP TABLE t3;
746
753
DROP TABLE t2;
747
754
DROP TABLE t1;
748
755
--
756
+ -- Try to MERGE partitions of temporary table.
757
+ --
758
+ CREATE TEMP TABLE t (i int) PARTITION BY RANGE (i);
759
+ CREATE TEMP TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
760
+ CREATE TEMP TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
761
+ SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid), c.relpersistence
762
+ FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
763
+ WHERE c.oid = i.inhrelid AND i.inhparent = 't'::regclass
764
+ ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
765
+ oid | pg_get_expr | relpersistence
766
+ --------+----------------------------+----------------
767
+ tp_0_1 | FOR VALUES FROM (0) TO (1) | t
768
+ tp_1_2 | FOR VALUES FROM (1) TO (2) | t
769
+ (2 rows)
770
+
771
+ ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
772
+ -- Partition should be temporary.
773
+ SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid), c.relpersistence
774
+ FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
775
+ WHERE c.oid = i.inhrelid AND i.inhparent = 't'::regclass
776
+ ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
777
+ oid | pg_get_expr | relpersistence
778
+ --------+----------------------------+----------------
779
+ tp_0_2 | FOR VALUES FROM (0) TO (2) | t
780
+ (1 row)
781
+
782
+ DROP TABLE t;
783
+ --
749
784
-- Check the partition index name if the partition name is the same as one
750
785
-- of the merged partitions.
751
786
--
@@ -771,4 +806,63 @@ Not-null constraints:
771
806
772
807
DROP TABLE t;
773
808
--
809
+ -- Try mixing permanent and temporary partitions.
810
+ --
811
+ SET search_path = partitions_merge_schema, pg_temp, public;
812
+ CREATE TABLE t (i int) PARTITION BY RANGE (i);
813
+ CREATE TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
814
+ CREATE TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
815
+ SELECT c.oid::pg_catalog.regclass, c.relpersistence FROM pg_catalog.pg_class c WHERE c.oid = 't'::regclass;
816
+ oid | relpersistence
817
+ -----+----------------
818
+ t | p
819
+ (1 row)
820
+
821
+ SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid), c.relpersistence
822
+ FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
823
+ WHERE c.oid = i.inhrelid AND i.inhparent = 't'::regclass
824
+ ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
825
+ oid | pg_get_expr | relpersistence
826
+ --------+----------------------------+----------------
827
+ tp_0_1 | FOR VALUES FROM (0) TO (1) | p
828
+ tp_1_2 | FOR VALUES FROM (1) TO (2) | p
829
+ (2 rows)
830
+
831
+ SET search_path = pg_temp, partitions_merge_schema, public;
832
+ -- Can't merge persistent partitions into a temporary partition
833
+ ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
834
+ ERROR: cannot create a temporary relation as partition of permanent relation "t"
835
+ SET search_path = partitions_merge_schema, public;
836
+ -- Can't merge persistent partitions into a temporary partition
837
+ ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO pg_temp.tp_0_2;
838
+ ERROR: cannot create a temporary relation as partition of permanent relation "t"
839
+ DROP TABLE t;
840
+ SET search_path = pg_temp, partitions_merge_schema, public;
841
+ BEGIN;
842
+ CREATE TABLE t (i int) PARTITION BY RANGE (i);
843
+ CREATE TABLE tp_0_1 PARTITION OF t FOR VALUES FROM (0) TO (1);
844
+ CREATE TABLE tp_1_2 PARTITION OF t FOR VALUES FROM (1) TO (2);
845
+ SELECT c.oid::pg_catalog.regclass, c.relpersistence FROM pg_catalog.pg_class c WHERE c.oid = 't'::regclass;
846
+ oid | relpersistence
847
+ -----+----------------
848
+ t | t
849
+ (1 row)
850
+
851
+ SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid), c.relpersistence
852
+ FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
853
+ WHERE c.oid = i.inhrelid AND i.inhparent = 't'::regclass
854
+ ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
855
+ oid | pg_get_expr | relpersistence
856
+ --------+----------------------------+----------------
857
+ tp_0_1 | FOR VALUES FROM (0) TO (1) | t
858
+ tp_1_2 | FOR VALUES FROM (1) TO (2) | t
859
+ (2 rows)
860
+
861
+ SET search_path = partitions_merge_schema, pg_temp, public;
862
+ -- Can't merge temporary partitions into a persistent partition
863
+ ALTER TABLE t MERGE PARTITIONS (tp_0_1, tp_1_2) INTO tp_0_2;
864
+ ROLLBACK;
865
+ RESET search_path;
866
+ --
774
867
DROP SCHEMA partitions_merge_schema;
868
+ DROP SCHEMA partitions_merge_schema2;
0 commit comments