@@ -2042,58 +2042,90 @@ CREATE TABLE part33_self_fk (
2042
2042
id_abc bigint
2043
2043
);
2044
2044
ALTER TABLE part3_self_fk ATTACH PARTITION part33_self_fk FOR VALUES FROM (30) TO (40);
2045
- SELECT cr.relname, co.conname, co.contype, co.convalidated,
2045
+ -- verify that this constraint works
2046
+ INSERT INTO parted_self_fk VALUES (1, NULL), (2, NULL), (3, NULL);
2047
+ INSERT INTO parted_self_fk VALUES (10, 1), (11, 2), (12, 3) RETURNING tableoid::regclass;
2048
+ tableoid
2049
+ ---------------
2050
+ part2_self_fk
2051
+ part2_self_fk
2052
+ part2_self_fk
2053
+ (3 rows)
2054
+
2055
+ INSERT INTO parted_self_fk VALUES (4, 5); -- error: referenced doesn't exist
2056
+ ERROR: insert or update on table "part1_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey"
2057
+ DETAIL: Key (id_abc)=(5) is not present in table "parted_self_fk".
2058
+ DELETE FROM parted_self_fk WHERE id = 1 RETURNING *; -- error: reference remains
2059
+ ERROR: update or delete on table "part1_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey1" on table "parted_self_fk"
2060
+ DETAIL: Key (id)=(1) is still referenced from table "parted_self_fk".
2061
+ SELECT cr.relname, co.conname, co.convalidated,
2046
2062
p.conname AS conparent, p.convalidated, cf.relname AS foreignrel
2047
2063
FROM pg_constraint co
2048
2064
JOIN pg_class cr ON cr.oid = co.conrelid
2049
2065
LEFT JOIN pg_class cf ON cf.oid = co.confrelid
2050
2066
LEFT JOIN pg_constraint p ON p.oid = co.conparentid
2051
- WHERE cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
2052
- ORDER BY co.contype, cr.relname, co.conname, p.conname;
2053
- relname | conname | contype | convalidated | conparent | convalidated | foreignrel
2054
- ----------------+----------------------------+---------+--------------+----------------------------+--------------+----------------
2055
- part1_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2056
- part2_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2057
- part32_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2058
- part33_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2059
- part3_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2060
- parted_self_fk | parted_self_fk_id_abc_fkey | f | t | | | parted_self_fk
2061
- part1_self_fk | part1_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2062
- part2_self_fk | part2_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2063
- part32_self_fk | part32_self_fk_pkey | p | t | part3_self_fk_pkey | t |
2064
- part33_self_fk | part33_self_fk_pkey | p | t | part3_self_fk_pkey | t |
2065
- part3_self_fk | part3_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2066
- parted_self_fk | parted_self_fk_pkey | p | t | | |
2067
- (12 rows)
2067
+ WHERE co.contype = 'f' AND
2068
+ cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
2069
+ ORDER BY cr. relname, co. conname, p.conname;
2070
+ relname | conname | convalidated | conparent | convalidated | foreignrel
2071
+ ----------------+-----------------------------+--------------+-----------------------------+--------------+----------------
2072
+ part1_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2073
+ part2_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2074
+ part32_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2075
+ part33_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2076
+ part3_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2077
+ parted_self_fk | parted_self_fk_id_abc_fkey | t | | | parted_self_fk
2078
+ parted_self_fk | parted_self_fk_id_abc_fkey1 | t | parted_self_fk_id_abc_fkey | t | part1_self_fk
2079
+ parted_self_fk | parted_self_fk_id_abc_fkey2 | t | parted_self_fk_id_abc_fkey | t | part2_self_fk
2080
+ parted_self_fk | parted_self_fk_id_abc_fkey3 | t | parted_self_fk_id_abc_fkey | t | part3_self_fk
2081
+ parted_self_fk | parted_self_fk_id_abc_fkey4 | t | parted_self_fk_id_abc_fkey3 | t | part32_self_fk
2082
+ parted_self_fk | parted_self_fk_id_abc_fkey5 | t | parted_self_fk_id_abc_fkey3 | t | part33_self_fk
2083
+ (11 rows)
2068
2084
2069
2085
-- detach and re-attach multiple times just to ensure everything is kosher
2070
2086
ALTER TABLE parted_self_fk DETACH PARTITION part2_self_fk;
2087
+ INSERT INTO part2_self_fk VALUES (16, 9); -- error: referenced doesn't exist
2088
+ ERROR: insert or update on table "part2_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey"
2089
+ DETAIL: Key (id_abc)=(9) is not present in table "parted_self_fk".
2090
+ DELETE FROM parted_self_fk WHERE id = 2 RETURNING *; -- error: reference remains
2091
+ ERROR: update or delete on table "part1_self_fk" violates foreign key constraint "part2_self_fk_id_abc_fkey" on table "part2_self_fk"
2092
+ DETAIL: Key (id)=(2) is still referenced from table "part2_self_fk".
2071
2093
ALTER TABLE parted_self_fk ATTACH PARTITION part2_self_fk FOR VALUES FROM (10) TO (20);
2094
+ INSERT INTO parted_self_fk VALUES (16, 9); -- error: referenced doesn't exist
2095
+ ERROR: insert or update on table "part2_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey"
2096
+ DETAIL: Key (id_abc)=(9) is not present in table "parted_self_fk".
2097
+ DELETE FROM parted_self_fk WHERE id = 3 RETURNING *; -- error: reference remains
2098
+ ERROR: update or delete on table "part1_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey1" on table "parted_self_fk"
2099
+ DETAIL: Key (id)=(3) is still referenced from table "parted_self_fk".
2072
2100
ALTER TABLE parted_self_fk DETACH PARTITION part2_self_fk;
2073
2101
ALTER TABLE parted_self_fk ATTACH PARTITION part2_self_fk FOR VALUES FROM (10) TO (20);
2074
- SELECT cr.relname, co.conname, co.contype, co.convalidated,
2102
+ ALTER TABLE parted_self_fk DETACH PARTITION part3_self_fk;
2103
+ ALTER TABLE parted_self_fk ATTACH PARTITION part3_self_fk FOR VALUES FROM (30) TO (40);
2104
+ ALTER TABLE part3_self_fk DETACH PARTITION part33_self_fk;
2105
+ ALTER TABLE part3_self_fk ATTACH PARTITION part33_self_fk FOR VALUES FROM (30) TO (40);
2106
+ SELECT cr.relname, co.conname, co.convalidated,
2075
2107
p.conname AS conparent, p.convalidated, cf.relname AS foreignrel
2076
2108
FROM pg_constraint co
2077
2109
JOIN pg_class cr ON cr.oid = co.conrelid
2078
2110
LEFT JOIN pg_class cf ON cf.oid = co.confrelid
2079
2111
LEFT JOIN pg_constraint p ON p.oid = co.conparentid
2080
- WHERE cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
2081
- ORDER BY co.contype, cr.relname, co.conname, p.conname;
2082
- relname | conname | contype | convalidated | conparent | convalidated | foreignrel
2083
- ----------------+----------------------------+---------+--------------+----------------------------+--------------+----------------
2084
- part1_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2085
- part2_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2086
- part32_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2087
- part33_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2088
- part3_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2089
- parted_self_fk | parted_self_fk_id_abc_fkey | f | t | | | parted_self_fk
2090
- part1_self_fk | part1_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2091
- part2_self_fk | part2_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2092
- part32_self_fk | part32_self_fk_pkey | p | t | part3_self_fk_pkey | t |
2093
- part33_self_fk | part33_self_fk_pkey | p | t | part3_self_fk_pkey | t |
2094
- part3_self_fk | part3_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2095
- parted_self_fk | parted_self_fk_pkey | p | t | | |
2096
- (12 rows)
2112
+ WHERE co.contype = 'f' AND
2113
+ cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
2114
+ ORDER BY cr. relname, co. conname, p.conname;
2115
+ relname | conname | convalidated | conparent | convalidated | foreignrel
2116
+ ----------------+-----------------------------+--------------+-----------------------------+--------------+----------------
2117
+ part1_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2118
+ part2_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2119
+ part32_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2120
+ part33_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2121
+ part3_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2122
+ parted_self_fk | parted_self_fk_id_abc_fkey | t | | | parted_self_fk
2123
+ parted_self_fk | parted_self_fk_id_abc_fkey1 | t | parted_self_fk_id_abc_fkey | t | part1_self_fk
2124
+ parted_self_fk | parted_self_fk_id_abc_fkey2 | t | parted_self_fk_id_abc_fkey | t | part2_self_fk
2125
+ parted_self_fk | parted_self_fk_id_abc_fkey3 | t | parted_self_fk_id_abc_fkey | t | part3_self_fk
2126
+ parted_self_fk | parted_self_fk_id_abc_fkey4 | t | parted_self_fk_id_abc_fkey3 | t | part32_self_fk
2127
+ parted_self_fk | parted_self_fk_id_abc_fkey5 | t | parted_self_fk_id_abc_fkey3 | t | part33_self_fk
2128
+ (11 rows)
2097
2129
2098
2130
-- Leave this table around, for pg_upgrade/pg_dump tests
2099
2131
-- Test creating a constraint at the parent that already exists in partitions.
0 commit comments