// dialect comparison

Disabling and enabling triggers in every SQL dialect

Triggers run for every row or statement of a bulk load. SQL Server, PostgreSQL and Oracle can switch them off; MySQL and SQLite cannot.

Filed under Sessions & admin

The verdict

Three engines have real statements, two do not. The MySQL session-variable guard is the only pattern that survives code review there; in SQLite, dropping and recreating from version-controlled schema is normal.

Where they differ

SQL Server, PostgreSQL and Oracle have first-class disable statements. MySQL and SQLite have none: you either drop and recreate the trigger, or build a kill-switch into the trigger body.

How each engine writes it

SQL Server

DISABLE TRIGGER trg_audit ON orders;
-- re-enable:
ENABLE TRIGGER trg_audit ON orders;

DISABLE TRIGGER ... ON DATABASE or ON ALL SERVER covers DDL triggers, and sys.triggers shows is_disabled. BULK INSERT and bcp skip triggers anyway unless you pass FIRE_TRIGGERS.

PostgreSQL

ALTER TABLE orders DISABLE TRIGGER trg_audit;
-- all triggers on the table: ALTER TABLE orders DISABLE TRIGGER ALL;

DISABLE TRIGGER ALL also disables FK-checking triggers, which is the documented bulk-load path. ENABLE TRIGGER ALL brings them back; superuser needed for system triggers. SET session_replication_role = replica skips ordinary triggers for one session instead.

MySQL

-- no disable: guard the trigger body with a session flag
SET @disable_triggers = 1;
-- inside the trigger body: IF @disable_triggers = 1 THEN LEAVE trig; END IF;
SET @disable_triggers = NULL;

The convention: every trigger checks a session variable first and exits. DROP TRIGGER and re-CREATE also works, but rows other sessions write in between skip the trigger.

SQLite

-- no disable; drop and recreate, or add a WHEN guard

No enable/disable. DROP TRIGGER then re-create from your schema source, or design triggers with WHEN conditions you can satisfy/skip.

Oracle

ALTER TRIGGER trg_audit DISABLE;
-- all triggers on a table:
ALTER TABLE orders DISABLE ALL TRIGGERS;

Mirrored by ENABLE / ENABLE ALL TRIGGERS. user_triggers shows status (ENABLED/DISABLED); disabled triggers still compile but never fire.

Questions that come up

Do disabled triggers still fire for FK enforcement?

PostgreSQL's DISABLE TRIGGER ALL covers internal FK triggers too, which is why it needs superuser. SQL Server and Oracle foreign keys are constraints, not triggers, and stay enforced.

What happens to data written while disabled?

Nothing retroactive: the trigger simply does not run. After re-enabling, backfill anything the disabled period skipped.

Why is my bulk load slow even with triggers off?

Usually the indexes. Drop or disable nonessential indexes too, load, then rebuild them; index maintenance is often the bigger cost.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.