@@ -1970,58 +1970,98 @@ CREATE TABLE part33_self_fk (
1970
1970
id_abc bigint
1971
1971
);
1972
1972
ALTER TABLE part3_self_fk ATTACH PARTITION part33_self_fk FOR VALUES FROM (30) TO (40);
1973
- SELECT cr.relname, co.conname, co.contype, co.convalidated,
1973
+ -- verify that this constraint works
1974
+ INSERT INTO parted_self_fk VALUES (1, NULL), (2, NULL), (3, NULL);
1975
+ INSERT INTO parted_self_fk VALUES (10, 1), (11, 2), (12, 3) RETURNING tableoid::regclass;
1976
+ tableoid
1977
+ ---------------
1978
+ part2_self_fk
1979
+ part2_self_fk
1980
+ part2_self_fk
1981
+ (3 rows)
1982
+
1983
+ INSERT INTO parted_self_fk VALUES (4, 5); -- error: referenced doesn't exist
1984
+ ERROR: insert or update on table "part1_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey"
1985
+ DETAIL: Key (id_abc)=(5) is not present in table "parted_self_fk".
1986
+ DELETE FROM parted_self_fk WHERE id = 1 RETURNING *; -- error: reference remains
1987
+ ERROR: update or delete on table "part1_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey1" on table "parted_self_fk"
1988
+ DETAIL: Key (id)=(1) is still referenced from table "parted_self_fk".
1989
+ SELECT cr.relname, co.conname, co.convalidated,
1974
1990
p.conname AS conparent, p.convalidated, cf.relname AS foreignrel
1975
1991
FROM pg_constraint co
1976
1992
JOIN pg_class cr ON cr.oid = co.conrelid
1977
1993
LEFT JOIN pg_class cf ON cf.oid = co.confrelid
1978
1994
LEFT JOIN pg_constraint p ON p.oid = co.conparentid
1979
- WHERE cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
1980
- ORDER BY co.contype, cr.relname, co.conname, p.conname;
1981
- relname | conname | contype | convalidated | conparent | convalidated | foreignrel
1982
- ----------------+----------------------------+---------+--------------+----------------------------+--------------+----------------
1983
- part1_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
1984
- part2_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
1985
- part32_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
1986
- part33_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
1987
- part3_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
1988
- parted_self_fk | parted_self_fk_id_abc_fkey | f | t | | | parted_self_fk
1989
- part1_self_fk | part1_self_fk_pkey | p | t | parted_self_fk_pkey | t |
1990
- part2_self_fk | part2_self_fk_pkey | p | t | parted_self_fk_pkey | t |
1991
- part32_self_fk | part32_self_fk_pkey | p | t | part3_self_fk_pkey | t |
1992
- part33_self_fk | part33_self_fk_pkey | p | t | part3_self_fk_pkey | t |
1993
- part3_self_fk | part3_self_fk_pkey | p | t | parted_self_fk_pkey | t |
1994
- parted_self_fk | parted_self_fk_pkey | p | t | | |
1995
- (12 rows)
1995
+ WHERE co.contype = 'f' AND
1996
+ cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
1997
+ ORDER BY cr. relname, co. conname, p.conname;
1998
+ relname | conname | convalidated | conparent | convalidated | foreignrel
1999
+ ----------------+-----------------------------+--------------+-----------------------------+--------------+----------------
2000
+ part1_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2001
+ part2_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2002
+ part32_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2003
+ part33_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2004
+ part3_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2005
+ parted_self_fk | parted_self_fk_id_abc_fkey | t | | | parted_self_fk
2006
+ parted_self_fk | parted_self_fk_id_abc_fkey1 | t | parted_self_fk_id_abc_fkey | t | part1_self_fk
2007
+ parted_self_fk | parted_self_fk_id_abc_fkey2 | t | parted_self_fk_id_abc_fkey | t | part2_self_fk
2008
+ parted_self_fk | parted_self_fk_id_abc_fkey3 | t | parted_self_fk_id_abc_fkey | t | part3_self_fk
2009
+ parted_self_fk | parted_self_fk_id_abc_fkey4 | t | parted_self_fk_id_abc_fkey3 | t | part32_self_fk
2010
+ parted_self_fk | parted_self_fk_id_abc_fkey5 | t | parted_self_fk_id_abc_fkey3 | t | part33_self_fk
2011
+ (11 rows)
1996
2012
1997
2013
-- detach and re-attach multiple times just to ensure everything is kosher
1998
2014
ALTER TABLE parted_self_fk DETACH PARTITION part2_self_fk;
2015
+ \d+ part2_self_fk
2016
+ Table "public.part2_self_fk"
2017
+ Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
2018
+ --------+--------+-----------+----------+---------+---------+--------------+-------------
2019
+ id | bigint | | not null | | plain | |
2020
+ id_abc | bigint | | | | plain | |
2021
+ Indexes:
2022
+ "part2_self_fk_pkey" PRIMARY KEY, btree (id)
2023
+
2024
+ INSERT INTO part2_self_fk VALUES (16, 9); -- good, but it'll prevent the attach below
2025
+ ALTER TABLE parted_self_fk ATTACH PARTITION part2_self_fk FOR VALUES FROM (10) TO (20);
2026
+ ERROR: insert or update on table "part2_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey"
2027
+ DETAIL: Key (id_abc)=(9) is not present in table "parted_self_fk".
2028
+ DELETE FROM part2_self_fk WHERE id = 16;
1999
2029
ALTER TABLE parted_self_fk ATTACH PARTITION part2_self_fk FOR VALUES FROM (10) TO (20);
2030
+ INSERT INTO parted_self_fk VALUES (16, 9); -- error: referenced doesn't exist
2031
+ ERROR: insert or update on table "part2_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey"
2032
+ DETAIL: Key (id_abc)=(9) is not present in table "parted_self_fk".
2033
+ DELETE FROM parted_self_fk WHERE id = 3 RETURNING *; -- error: reference remains
2034
+ ERROR: update or delete on table "part1_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey1" on table "parted_self_fk"
2035
+ DETAIL: Key (id)=(3) is still referenced from table "parted_self_fk".
2000
2036
ALTER TABLE parted_self_fk DETACH PARTITION part2_self_fk;
2001
2037
ALTER TABLE parted_self_fk ATTACH PARTITION part2_self_fk FOR VALUES FROM (10) TO (20);
2002
- SELECT cr.relname, co.conname, co.contype, co.convalidated,
2038
+ ALTER TABLE parted_self_fk DETACH PARTITION part3_self_fk;
2039
+ ALTER TABLE parted_self_fk ATTACH PARTITION part3_self_fk FOR VALUES FROM (30) TO (40);
2040
+ ALTER TABLE part3_self_fk DETACH PARTITION part33_self_fk;
2041
+ ALTER TABLE part3_self_fk ATTACH PARTITION part33_self_fk FOR VALUES FROM (30) TO (40);
2042
+ SELECT cr.relname, co.conname, co.convalidated,
2003
2043
p.conname AS conparent, p.convalidated, cf.relname AS foreignrel
2004
2044
FROM pg_constraint co
2005
2045
JOIN pg_class cr ON cr.oid = co.conrelid
2006
2046
LEFT JOIN pg_class cf ON cf.oid = co.confrelid
2007
2047
LEFT JOIN pg_constraint p ON p.oid = co.conparentid
2008
- WHERE cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
2009
- ORDER BY co.contype, cr.relname, co.conname, p.conname;
2010
- relname | conname | contype | convalidated | conparent | convalidated | foreignrel
2011
- ----------------+----------------------------+---------+--------------+----------------------------+--------------+----------------
2012
- part1_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2013
- part2_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2014
- part32_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2015
- part33_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2016
- part3_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2017
- parted_self_fk | parted_self_fk_id_abc_fkey | f | t | | | parted_self_fk
2018
- part1_self_fk | part1_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2019
- part2_self_fk | part2_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2020
- part32_self_fk | part32_self_fk_pkey | p | t | part3_self_fk_pkey | t |
2021
- part33_self_fk | part33_self_fk_pkey | p | t | part3_self_fk_pkey | t |
2022
- part3_self_fk | part3_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2023
- parted_self_fk | parted_self_fk_pkey | p | t | | |
2024
- (12 rows)
2048
+ WHERE co.contype = 'f' AND
2049
+ cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
2050
+ ORDER BY cr. relname, co. conname, p.conname;
2051
+ relname | conname | convalidated | conparent | convalidated | foreignrel
2052
+ ----------------+-----------------------------+--------------+-----------------------------+--------------+----------------
2053
+ part1_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2054
+ part2_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2055
+ part32_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2056
+ part33_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2057
+ part3_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2058
+ parted_self_fk | parted_self_fk_id_abc_fkey | t | | | parted_self_fk
2059
+ parted_self_fk | parted_self_fk_id_abc_fkey1 | t | parted_self_fk_id_abc_fkey | t | part1_self_fk
2060
+ parted_self_fk | parted_self_fk_id_abc_fkey2 | t | parted_self_fk_id_abc_fkey | t | part2_self_fk
2061
+ parted_self_fk | parted_self_fk_id_abc_fkey3 | t | parted_self_fk_id_abc_fkey | t | part3_self_fk
2062
+ parted_self_fk | parted_self_fk_id_abc_fkey4 | t | parted_self_fk_id_abc_fkey3 | t | part32_self_fk
2063
+ parted_self_fk | parted_self_fk_id_abc_fkey5 | t | parted_self_fk_id_abc_fkey3 | t | part33_self_fk
2064
+ (11 rows)
2025
2065
2026
2066
-- Leave this table around, for pg_upgrade/pg_dump tests
2027
2067
-- Test creating a constraint at the parent that already exists in partitions.
0 commit comments