213

I'm bulk loading data and can re-calculate all trigger modifications much more cheaply after the fact than on a row-by-row basis.

How can I temporarily disable all triggers in PostgreSQL?

7 Answers 7

259

Alternatively, if you are wanting to disable all triggers, not just those on the USER table, you can use:

SET session_replication_role = replica;

This disables triggers for the current session.

To re-enable for the same session:

SET session_replication_role = DEFAULT;

Source: http://koo.fi/blog/2013/01/08/disable-postgresql-triggers-temporarily/

9
  • 4
    Awesome. Made my mass-deletion go from 30 minutes to <1 second :)
    – Dan Lenski
    Sep 6, 2014 at 1:30
  • 15
    It is also handy that this command doesn't disable constraint triggers Jan 30, 2015 at 11:18
  • 4
    One caveat: according to runtime config docs and the ALTER TABLE docs this will work with normal triggers but not those set with ENABLE REPLICA or ENABLE ALWAYS.
    – beldaz
    Jul 7, 2018 at 21:04
  • 3
    I'm on 10.4 and it seems to ignore this above statement.
    – Stephane
    Oct 25, 2018 at 13:07
  • 3
    Be careful it also disables checks of foreign key constraints.
    – Pifagorych
    Sep 25, 2019 at 11:30
164

PostgreSQL knows the ALTER TABLE tblname DISABLE TRIGGER USER command, which seems to do what I need. See ALTER TABLE.

6
  • And then how do you "re-calculate all trigger modifications"? Mar 20, 2014 at 11:11
  • 26
    Careful with concurrent load: ALTER TABLE ... DISABLE TRIGGER USER requires an exclusive lock on the table. May 27, 2014 at 22:14
  • 4
    @WojtekKruszewski, I think that David meant that He can re-calculate the changes manually that would have done by trigger, by using some prior knowledge (for example, if the trigger will make the same change in every row, which can be more efficiently handled by a single UPDATE). I don't think that He meant that You can do this in every situation. Aug 5, 2014 at 10:29
  • 1
    @zyzof's solution is better for disabling all triggers.
    – uthomas
    Mar 1, 2017 at 7:17
  • 1
    But this has the BIG problem that you have to use the database exclusively, because the triggers will also be disabled in other sessions!
    – Daniel
    Jun 22 at 6:18
106

For disable trigger

ALTER TABLE table_name DISABLE TRIGGER trigger_name

For enable trigger

ALTER TABLE table_name ENABLE TRIGGER trigger_name
1
  • 21
    You may also use "all" for this: ALTER TABLE table_name DISABLE TRIGGER all
    – DenisNovac
    Jun 15, 2020 at 4:03
10
SET session_replication_role = replica; 

It doesn't work with PostgreSQL 9.4 on my Linux machine if i change a table through table editor in pgAdmin and works if i change table through ordinary query. Manual changes in pg_trigger table also don't work without server restart but dynamic query like on postgresql.nabble.com ENABLE / DISABLE ALL TRIGGERS IN DATABASE works. It could be useful when you need some tuning.

For example if you have tables in a particular namespace it could be:

create or replace function disable_triggers(a boolean, nsp character varying) returns void as
$$
declare 
act character varying;
r record;
begin
    if(a is true) then
        act = 'disable';
    else
        act = 'enable';
    end if;

    for r in select c.relname from pg_namespace n
        join pg_class c on c.relnamespace = n.oid and c.relhastriggers = true
        where n.nspname = nsp
    loop
        execute format('alter table %I %s trigger all', r.relname, act); 
    end loop;
end;
$$
language plpgsql;

If you want to disable all triggers with certain trigger function it could be:

create or replace function disable_trigger_func(a boolean, f character varying) returns void as
$$
declare 
act character varying;
r record;
begin
    if(a is true) then
        act = 'disable';
    else
        act = 'enable';
    end if;

    for r in select c.relname from pg_proc p 
        join pg_trigger t on t.tgfoid = p.oid
        join pg_class c on c.oid = t.tgrelid
        where p.proname = f
    loop
        execute format('alter table %I %s trigger all', r.relname, act); 
    end loop;
end;
$$
language plpgsql;

PostgreSQL documentation for system catalogs


There are another control options of trigger firing process:

ALTER TABLE ... ENABLE REPLICA TRIGGER ... - trigger will fire in replica mode only.

ALTER TABLE ... ENABLE ALWAYS TRIGGER ... - trigger will fire always (obviously)

8

You can also disable triggers in pgAdmin (III):

  1. Find your table
  2. Expand the +
  3. Find your trigger in Triggers
  4. Right-click, uncheck "Trigger Enabled?"
8
SET session_replication_role = replica;  

also dosent work for me in Postgres 9.1. i use the two function described by bartolo-otrit with some modification. I modified the first function to make it work for me because the namespace or the schema must be present to identify the table correctly. The new code is :

CREATE OR REPLACE FUNCTION disable_triggers(a boolean, nsp character varying)
  RETURNS void AS
$BODY$
declare 
act character varying;
r record;
begin
    if(a is true) then
        act = 'disable';
    else
        act = 'enable';
    end if;

    for r in select c.relname from pg_namespace n
        join pg_class c on c.relnamespace = n.oid and c.relhastriggers = true
        where n.nspname = nsp
    loop
        execute format('alter table %I.%I %s trigger all', nsp,r.relname, act); 
    end loop;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION disable_triggers(boolean, character varying)
  OWNER TO postgres;

then i simply do a select query for every schema :

SELECT disable_triggers(true,'public');
SELECT disable_triggers(true,'Adempiere');
6

A really elegant way to handle this is to create a role that handles database population and set replication for that role:

ALTER ROLE role_name SET session_replication_role = 'replica';

That way you can use that role for populating data and not have to worry about disabling and renabling triggers etc.

1
  • Thank you! This was the only way I found to apply "globally" while doing a lot of SQL ops for seeding a dev DB. Oct 19 at 22:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.