0

I want to write a query to list the check constraints in a database along with its constraint definition. I am not able to find a column to find the check constraint definition just like the search condition column in oracle all_constraints table. Is there a PG alternate for this?

Mano
  • 601
  • 10
  • 32

1 Answers1

1

The catalog pg_constraint stores check, primary key, unique, foreign key, and exclusion constraints on tables and contype column saves data about the type of the constraint i,e

c = check constraint
f = foreign key constraint
p = primary key constraint
u = unique constraint
t = constraint trigger
x = exclusion constraint

select pgc.conname as constraint_name,
       ccu.table_schema as table_schema,
       ccu.table_name,
       ccu.column_name,
       pg_get_constraintdef(pgc.oid) 
from pg_constraint pgc
join pg_namespace nsp on nsp.oid = pgc.connamespace
join pg_class  cls on pgc.conrelid = cls.oid
left join information_schema.constraint_column_usage ccu
          on pgc.conname = ccu.constraint_name
          and nsp.nspname = ccu.constraint_schema
where contype ='c'
order by pgc.conname;
Nikhil B
  • 353
  • 2
  • 7
  • Thanks, i'm able to get the required info from pg_get_constraintdef(pgc.oid) from your query. – Mano Jun 16 '21 at 18:09