Skip to content

Instantly share code, notes, and snippets.

@NikolayS
Last active November 3, 2017 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikolayS/1deefd26fa45c86d5963db070dc07179 to your computer and use it in GitHub Desktop.
Save NikolayS/1deefd26fa45c86d5963db070dc07179 to your computer and use it in GitHub Desktop.
make all constraints DEFERRABLE
-- use this to make all constraints in "public" schema `deferrable initially immediate`
-- (edit schema name if needed)
do $$
declare
sql text;
begin
select into sql
string_agg(
'alter table ' || quote_ident(ns.nspname) || '.' || quote_ident(tb.relname)
|| ' alter constraint ' || quote_ident(conname) || ' deferrable initially immediate;'
, ' '
)
from pg_constraint c
join pg_class tb on tb.oid = c.conrelid
join pg_namespace ns on ns.oid = tb.relnamespace
where c.contype = 'f' and ns.nspname in ('public');
raise notice 'sql: %', sql;
execute sql;
end;
$$ language plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment