TRUNCATE tables referenced by FKs on partitioned tables

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: TRUNCATE tables referenced by FKs on partitioned tables
Date: 2018-07-11 00:06:24
Message-ID: 20180711000624.zmeizicibxeehhsg@alvherre.pgsql
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

$subject is broken:

create table prim (a int primary key);
create table partfk (a int references prim) partition by range (a);
create table partfk1 partition of partfk for values from (0) to (100);
create table partfk2 partition of partfk for values from (100) to (200);

You can't truncate prim on its own. This is expected.
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.

However, you can't do it even if you try to include partfk in the mix:

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.

Trying to list all the partitions individually is pointless:

alvherre=# truncate table prim, partfk, partfk1, partfk2;
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.

CASCADE is also useless:

alvherre=# truncate table prim cascade;
NOTICE: truncate cascades to table "partfk"
NOTICE: truncate cascades to table "partfk1"
NOTICE: truncate cascades to table "partfk2"
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.

--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Asim R P 2018-07-11 00:15:13 Shared buffer access rule violations?
Previous Message Andres Freund 2018-07-11 00:06:05 Re: shared-memory based stats collector