Re: TRUNCATE ONLY with foreign keys and triggers disabled - Mailing list pgsql-general

From Dimitrios Apostolou
Subject Re: TRUNCATE ONLY with foreign keys and triggers disabled
Date
Msg-id e5909bae-6782-9c61-fb86-cdf3398e3c9f@gmx.net
Whole thread Raw
In response to Re: TRUNCATE ONLY with foreign keys and triggers disabled  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: TRUNCATE ONLY with foreign keys and triggers disabled
List pgsql-general
On Mon, 14 Apr 2025, Tom Lane wrote:

> Dimitrios Apostolou <jimis@gmx.net> writes:
>> While doing TRUNCATE ONLY I get:
>>    ERROR: cannot truncate a table referenced in a foreign key constraint
>> But in my case the table to be truncated is already empty, and the
>> TRIGGERS are disabled in all tables.
>
> IIRC, it will let you do it if you truncate both the referenced and
> referencing tables in the same command.  The state of the triggers
> is not material to this, since TRUNCATE doesn't fire them anyway.

Thanks Tom, however that is not possible in my case, the referenced table
is possibly just populated and potentially very big (partitioned table
with many sub-partitions).

Context is that I've modified pg_restore to accept --data-only --clean,
and issues a TRUNCATE ONLY before each table's COPY, in the same
transaction (thus avoiding WAL too). Will send an RFC patch to
pgsql-hackers when I verify it works.


Dimitris



pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: TRUNCATE ONLY with foreign keys and triggers disabled
Next
From: Dimitrios Apostolou
Date:
Subject: Re: TRUNCATE ONLY with foreign keys and triggers disabled