Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fdO51-0008Lb-K1 for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Jul 2018 22:59:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fdO4z-00044t-A4 for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Jul 2018 22:59:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fdO4y-00044k-VU for pgsql-hackers@lists.postgresql.org; Wed, 11 Jul 2018 22:59:25 +0000 Received: from mail-qk0-x242.google.com ([2607:f8b0:400d:c09::242]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fdO4u-0002ID-Fv for pgsql-hackers@postgresql.org; Wed, 11 Jul 2018 22:59:24 +0000 Received: by mail-qk0-x242.google.com with SMTP id o2-v6so14539275qkc.13 for ; Wed, 11 Jul 2018 15:59:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=jnM5FbX5Gcft1bdr0Ex990jkbYbXAc7OF1VPJennrdc=; b=lVwDDHhWc1g/vIxLslkJlonHeE798VRvpRFRWFGJ4Zy4yXTYfm9sKV6th+NtgrPGVU DdZCR7hcBEdgfEADSON+i46Hs7g8uyqFGQvgPX933slhUHd8JgsBDSPa+oB4H2hoU4NX Rk0vF/3PxCQgkKb3tXu4oKRnAAr170zXKhGkYUAWdHFlAySzZWo78S6CiEBIEUjAhFzz an39LkzetncmQffMvS8PWvGM0kdwm3M/KhuypUjR9c7hIf1aFePpjtUjWkJjv8SwD9sp bpEVGuytVJSfSscKQvlTpI5fRKe9lpAKD9v5u7TIdbZDDxcOcl1jHyQM963lHgOaRFFS +OfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=jnM5FbX5Gcft1bdr0Ex990jkbYbXAc7OF1VPJennrdc=; b=QMk15aWYQXS2WQgZgfteImLKSRI9t1ndmmbTEpOqu62VoEjmMiIIUSPj3+TBMjSAr4 0v9a6yJqFlsstxsweYVuKRU0PHoIulRaLqPV8M2xvebtxtEL34Nn79uTV32+PywtdD2H haFUYswaP3uoO1ep0VuU6aD/yVr78M4Sa0sOXuXDP4QBzOgOkpKVWqFsC5IbPQ0htKMR NDqMwJkJaQ4U0Q9Agb4XYWyAJhCnfVJXxR0F7uGYStFQDs/m3fNQDXlKw44AQCfWGcdx iF91+YfnbCYvlWmdrgZmUs4lrWXJKhUK8Q354PSso1W5ZFWKw7w+aAKMgTSEhhNKaA/6 Ycqw== X-Gm-Message-State: AOUpUlG+vChKoh4j/HGNjLstu6j5EEtsuvWKew4oEOfpoKwwtnt23JVO mgPxuhmrZ3ReefaEqzhUZIINWLO4 X-Google-Smtp-Source: AAOMgpclkoW4NBWTL/VTkTpFNZvkJk0Mj13j2bJgi3dBTNqRd6TkYgjJXY5pmTgSw0M3F0WfJTMGiw== X-Received: by 2002:a37:8547:: with SMTP id h68-v6mr486707qkd.340.1531349958787; Wed, 11 Jul 2018 15:59:18 -0700 (PDT) Received: from alvin.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id r23-v6sm6217504qtn.40.2018.07.11.15.59.17 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 11 Jul 2018 15:59:17 -0700 (PDT) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 3A1409F7; Wed, 11 Jul 2018 18:59:16 -0400 (-04) Date: Wed, 11 Jul 2018 18:59:16 -0400 From: Alvaro Herrera To: Michael Paquier Cc: Pg Hackers Subject: Re: TRUNCATE tables referenced by FKs on partitioned tables Message-ID: <20180711225916.domrlbkmukhrg6jg@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="3gafq63hwg2hoiqw" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20180711071647.GC14301@paquier.xyz> User-Agent: NeoMutt/20170306-137-4415bd-dirty (1.8.0) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --3gafq63hwg2hoiqw Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2018-Jul-11, Michael Paquier wrote: > > alvherre=# truncate table prim, partfk; > > ERROR: cannot truncate a table referenced in a foreign key constraint > > DETALLE: Table "partfk" references "prim". > > SUGERENCIA: Truncate table "partfk" at the same time, or use TRUNCATE ... CASCADE. > > Your first and second queries are the same :) Yeah, C&P failure :-( Anyway, this patch seems to fix it, and adds what I think is appropriate test coverage. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --3gafq63hwg2hoiqw Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0001-fix-truncate.patch" From 07500a1d7d7e6e2a79514672a7b6441781baa1da Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 11 Jul 2018 18:54:00 -0400 Subject: [PATCH] fix truncate --- src/backend/catalog/heap.c | 7 +++- src/backend/commands/tablecmds.c | 2 +- src/test/regress/expected/truncate.out | 75 ++++++++++++++++++++++++++++++++++ src/test/regress/sql/truncate.sql | 47 +++++++++++++++++++++ 4 files changed, 128 insertions(+), 3 deletions(-) diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index d223ba8537..4cfc0c8911 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -3181,13 +3181,16 @@ heap_truncate_check_FKs(List *relations, bool tempTables) * Build a list of OIDs of the interesting relations. * * If a relation has no triggers, then it can neither have FKs nor be - * referenced by a FK from another table, so we can ignore it. + * referenced by a FK from another table, so we can ignore it. For + * partitioned tables, FKs have no triggers, so we must include them + * anyway. */ foreach(cell, relations) { Relation rel = lfirst(cell); - if (rel->rd_rel->relhastriggers) + if (rel->rd_rel->relhastriggers || + rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) oids = lappend_oid(oids, RelationGetRelid(rel)); } diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 7c0cf0d7ee..22e81e712d 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -1421,7 +1421,7 @@ ExecuteTruncateGuts(List *explicit_rels, List *relids, List *relids_logged, Oid *logrelids; /* - * Open, exclusive-lock, and check all the explicitly-specified relations + * Check the explicitly-specified relations. * * In CASCADE mode, suck in all referencing relations as well. This * requires multiple iterations to find indirectly-dependent relations. At diff --git a/src/test/regress/expected/truncate.out b/src/test/regress/expected/truncate.out index 735d0e862d..0c2ba173db 100644 --- a/src/test/regress/expected/truncate.out +++ b/src/test/regress/expected/truncate.out @@ -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, 1), (100, 100), (150, 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, b int, filler text) + 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(); diff --git a/src/test/regress/sql/truncate.sql b/src/test/regress/sql/truncate.sql index fbd1d1a8a5..bfa73f043b 100644 --- a/src/test/regress/sql/truncate.sql +++ b/src/test/regress/sql/truncate.sql @@ -244,3 +244,50 @@ INSERT INTO truncparted VALUES (1, 'a'); TRUNCATE ONLY truncparted; 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, 1), (100, 100), (150, 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, b int, filler text) + 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 + +select tp_ins_data(); +-- should truncate everything +TRUNCATE TABLE truncprim, truncpart; +select * from tp_chk_data(); + +select tp_ins_data(); +-- 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(); + +SELECT tp_ins_data(); +-- should truncate all partitions +TRUNCATE TABLE truncpart; +SELECT * FROM tp_chk_data(); +DROP TABLE truncprim, truncpart; +DROP FUNCTION tp_ins_data(), tp_chk_data(); -- 2.11.0 --3gafq63hwg2hoiqw--