Skip to content

Instantly share code, notes, and snippets.

@ianpgall
Created March 11, 2014 19:07
Show Gist options
  • Save ianpgall/9492760 to your computer and use it in GitHub Desktop.
Save ianpgall/9492760 to your computer and use it in GitHub Desktop.
PostgreSQL function that deletes all triggers for all tables
CREATE OR REPLACE FUNCTION strip_all_triggers() RETURNS text AS $$ DECLARE
triggNameRecord RECORD;
triggTableRecord RECORD;
BEGIN
FOR triggNameRecord IN select distinct(trigger_name) from information_schema.triggers where trigger_schema = 'public' LOOP
FOR triggTableRecord IN SELECT distinct(event_object_table) from information_schema.triggers where trigger_name = triggNameRecord.trigger_name LOOP
RAISE NOTICE 'Dropping trigger: % on table: %', triggNameRecord.trigger_name, triggTableRecord.event_object_table;
EXECUTE 'DROP TRIGGER ' || triggNameRecord.trigger_name || ' ON ' || triggTableRecord.event_object_table || ';';
END LOOP;
END LOOP;
RETURN 'done';
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment