public inbox for [email protected]  
help / color / mirror / Atom feed
TRUNCATE ONLY with foreign keys and triggers disabled
2+ messages / 2 participants
[nested] [flat]

* TRUNCATE ONLY with foreign keys and triggers disabled
@ 2025-04-14 10:57  Dimitrios Apostolou <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Dimitrios Apostolou @ 2025-04-14 10:57 UTC (permalink / raw)
  To: [email protected]

Hello list,

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.

I wonder, is there a deeper reason for this error, or is it just an 
implementation shortcoming? In other words, how easy would it be to write 
a patch that allows TRUNCATE even with foreign keys when triggers are 
disabled or the table is empty?

Thanks in advance,
Dimitris







^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: TRUNCATE ONLY with foreign keys and triggers disabled
@ 2025-04-14 14:45  Tom Lane <[email protected]>
  parent: Dimitrios Apostolou <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2025-04-14 14:45 UTC (permalink / raw)
  To: Dimitrios Apostolou <[email protected]>; +Cc: [email protected]

Dimitrios Apostolou <[email protected]> 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.

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2025-04-14 14:45 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-04-14 10:57 TRUNCATE ONLY with foreign keys and triggers disabled Dimitrios Apostolou <[email protected]>
2025-04-14 14:45 ` Tom Lane <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox