|
|
|
|
|
by matthijs
1146 days ago
|
|
Using `truncate` in combination with `cascade` is another that I found unexpected: "Automatically truncate all tables that have foreign-key references to any of the named tables, or to any tables added to the group due to CASCADE." So it will simply clear out other tables that reference table to truncate, even if you have `on delete set null` and the foreign key column is null. https://www.postgresql.org/docs/current/sql-truncate.html |
|
You should be very hesitant about using TRUNCATE on a production database unless that table (and all related foreign keyed tables) are truly ephemeral. Even if the data is cleared every night at midnight, for example, is there going to be a 10 second analysis transaction running across the midnight boundary that will fail hard with suddenly missing rows?
Running a full delete on the rows and vacuum will still result in a tiny file on storage and doesn't wake me up in a cold sweat when I have a flashback. Even renaming the table is in many ways safer.