Remove usage of `disable_referential_integrity`

🔥 Problem

disable_referential_integrity allows a developer to disable all triggers on all tables in the database. This can lead to data consistency problems as described in this PostgreSQL thread.

There are two problems with this method:

  • Triggers are disabled for all tables even though we may only need to disable them for one table
  • The disabling of triggers occurs in a separate database transaction from the query, which presents an opportunity for rollback problem.

🚒 Solution

We should remove all uses of disable_referential_integrity.

If triggers must be disabled in the place where disable_referential_integrity is being used, we should disable the triggers, complete the needed query, and enable the triggers all in one database transaction:

BEGIN;

ALTER TABLE my_table DISABLE TRIGGER ALL;

-- execute query that requires disabled triggers

ALTER TABLE my_table ENABLE TRIGGER ALL;

COMMIT;