@ -464,3 +464,78 @@ ERROR: cannot truncate only a partitioned table
HINT: Do not specify the ONLY keyword, or use TRUNCATE ONLY on the partitions directly.
TRUNCATE truncparted;
DROP TABLE truncparted;
-- foreign key on partitioned table: partition key is referencing column.
-- Make sure truncate did execute on all tables
CREATE FUNCTION tp_ins_data() RETURNS void LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO truncprim VALUES (1), (100), (150);
INSERT INTO truncpart VALUES (1), (100), (150);
END
$$;
CREATE FUNCTION tp_chk_data(OUT pktb regclass, OUT pkval int, OUT fktb regclass, OUT fkval int)
RETURNS SETOF record LANGUAGE plpgsql AS $$
BEGIN
RETURN QUERY SELECT
pk.tableoid::regclass, pk.a, fk.tableoid::regclass, fk.a
FROM truncprim pk FULL JOIN truncpart fk USING (a)
ORDER BY 2, 4;
END
$$;
CREATE TABLE truncprim (a int PRIMARY KEY);
CREATE TABLE truncpart (a int REFERENCES truncprim)
PARTITION BY RANGE (a);
CREATE TABLE truncpart_1 PARTITION OF truncpart FOR VALUES FROM (0) TO (100);
CREATE TABLE truncpart_2 PARTITION OF truncpart FOR VALUES FROM (100) TO (200)
PARTITION BY RANGE (a);
CREATE TABLE truncpart_2_1 PARTITION OF truncpart_2 FOR VALUES FROM (100) TO (150);
CREATE TABLE truncpart_2_d PARTITION OF truncpart_2 DEFAULT;
TRUNCATE TABLE truncprim; -- should fail
ERROR: cannot truncate a table referenced in a foreign key constraint
DETAIL: Table "truncpart" references "truncprim".
HINT: Truncate table "truncpart" at the same time, or use TRUNCATE ... CASCADE.
select tp_ins_data();
tp_ins_data
-------------
(1 row)
-- should truncate everything
TRUNCATE TABLE truncprim, truncpart;
select * from tp_chk_data();
pktb | pkval | fktb | fkval
------+-------+------+-------
(0 rows)
select tp_ins_data();
tp_ins_data
-------------
(1 row)
-- should truncate everything
SET client_min_messages TO WARNING; -- suppress cascading notices
TRUNCATE TABLE truncprim CASCADE;
RESET client_min_messages;
SELECT * FROM tp_chk_data();
pktb | pkval | fktb | fkval
------+-------+------+-------
(0 rows)
SELECT tp_ins_data();
tp_ins_data
-------------
(1 row)
-- should truncate all partitions
TRUNCATE TABLE truncpart;
SELECT * FROM tp_chk_data();
pktb | pkval | fktb | fkval
-----------+-------+------+-------
truncprim | 1 | |
truncprim | 100 | |
truncprim | 150 | |
(3 rows)
DROP TABLE truncprim, truncpart;
DROP FUNCTION tp_ins_data(), tp_chk_data();