Skip to content

Commit 5dcb810

Browse files
author
Antonin Houska
committed
Copy constraints from the original to the new table.
1 parent 82abd1f commit 5dcb810

3 files changed

Lines changed: 622 additions & 35 deletions

File tree

expected/pg_rewrite.out

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ CREATE TABLE tab1_new_part_1 PARTITION OF tab1_new FOR VALUES FROM (0) TO (256);
1414
CREATE TABLE tab1_new_part_2 PARTITION OF tab1_new FOR VALUES FROM (256) TO (512);
1515
CREATE TABLE tab1_new_part_3 PARTITION OF tab1_new FOR VALUES FROM (512) TO (768);
1616
CREATE TABLE tab1_new_part_4 PARTITION OF tab1_new FOR VALUES FROM (768) TO (1024);
17+
-- Also test handling of constraints that require "manual" validation.
18+
ALTER TABLE tab1 ADD CHECK (k >= 0);
19+
CREATE TABLE tab1_fk(i int REFERENCES tab1);
20+
INSERT INTO tab1_fk(i) VALUES (1);
21+
\d tab1
22+
Table "public.tab1"
23+
Column | Type | Collation | Nullable | Default
24+
--------+---------+-----------+----------+---------
25+
i | integer | | not null |
26+
j | integer | | |
27+
k | integer | | |
28+
Indexes:
29+
"tab1_pkey" PRIMARY KEY, btree (i)
30+
Check constraints:
31+
"tab1_k_check" CHECK (k >= 0)
32+
Referenced by:
33+
TABLE "tab1_fk" CONSTRAINT "tab1_fk_i_fkey" FOREIGN KEY (i) REFERENCES tab1(i)
34+
1735
-- Process the table.
1836
SELECT rewrite_table('tab1', 'tab1_new', 'tab1_orig');
1937
rewrite_table
@@ -22,6 +40,41 @@ SELECT rewrite_table('tab1', 'tab1_new', 'tab1_orig');
2240
(1 row)
2341

2442
-- tab1 should now be partitioned.
43+
\d tab1
44+
Partitioned table "public.tab1"
45+
Column | Type | Collation | Nullable | Default
46+
--------+---------+-----------+----------+---------
47+
i | integer | | not null |
48+
j | integer | | |
49+
k | integer | | |
50+
Partition key: RANGE (i)
51+
Indexes:
52+
"tab1_new_pkey" PRIMARY KEY, btree (i)
53+
Check constraints:
54+
"tab1_k_check2" CHECK (k >= 0) NOT VALID
55+
Referenced by:
56+
TABLE "tab1_fk" CONSTRAINT "tab1_fk_i_fkey2" FOREIGN KEY (i) REFERENCES tab1(i) NOT VALID
57+
Number of partitions: 4 (Use \d+ to list them.)
58+
59+
-- Validate the constraints.
60+
ALTER TABLE tab1 VALIDATE CONSTRAINT tab1_k_check2;
61+
ALTER TABLE tab1_fk VALIDATE CONSTRAINT tab1_fk_i_fkey2;
62+
\d tab1
63+
Partitioned table "public.tab1"
64+
Column | Type | Collation | Nullable | Default
65+
--------+---------+-----------+----------+---------
66+
i | integer | | not null |
67+
j | integer | | |
68+
k | integer | | |
69+
Partition key: RANGE (i)
70+
Indexes:
71+
"tab1_new_pkey" PRIMARY KEY, btree (i)
72+
Check constraints:
73+
"tab1_k_check2" CHECK (k >= 0)
74+
Referenced by:
75+
TABLE "tab1_fk" CONSTRAINT "tab1_fk_i_fkey2" FOREIGN KEY (i) REFERENCES tab1(i)
76+
Number of partitions: 4 (Use \d+ to list them.)
77+
2578
EXPLAIN SELECT * FROM tab1;
2679
QUERY PLAN
2780
---------------------------------------------------------------------------------

0 commit comments

Comments
 (0)