I have an SQL script that needs to remove several constraints and restore them at the end, but the constraint names are automatically generated and will be different each time the script is run.
I know how to get the name of a constraint from table names, but it is not possible to use this information in a drop statement.
select conname from pg_constraint where conrelid = (select oid from pg_class where relname='table name') and confrelid = (select oid from pg_class where relname='reference table');
alter table something drop constraint (some subquery) is a syntax error.
Ideally, I would like to get the name of the constraint and store it in a variable, but Postgres doesn't seem to support this, and I can't get it to work with psql \set .
Is it possible?
plpgsql postgresql psql dynamic-sql
takteek
source share