TRUNCATE tables referenced by FKs on partitioned tables - Mailing list pgsql-hackers

From Alvaro Herrera
Subject TRUNCATE tables referenced by FKs on partitioned tables
Date
Msg-id 20180711000624.zmeizicibxeehhsg@alvherre.pgsql
Whole thread Raw
Responses Re: TRUNCATE tables referenced by FKs on partitioned tables  (Michael Paquier <michael@paquier.xyz>)
List 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


pgsql-hackers by date:

Previous
From: Andres Freund
Date:
Subject: Re: shared-memory based stats collector
Next
From: Asim R P
Date:
Subject: Shared buffer access rule violations?