pg_get_constraintdef()

A function returning the definition of a constraint

pg_get_constraintdef() is a system function for obtaining the definition of a constraint.

pg_get_constraintdef() was added in PostgreSQL 7.3.

Usage

pg_get_constraintdef ( constraint oid [, pretty boolean ] ) → text

The returned definition is reconstructed from metadata and is not the text of the definition originally provided.

Change history

Examples

Simple example of pg_get_constraintdef() usage, using a table with a single constraint:

postgres=# CREATE TABLE foo (id INT NOT NULL CHECK (id BETWEEN 1 AND 2));
CREATE TABLE

postgres=# \d foo
                Table "public.foo"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 id     | integer |           | not null | 
Check constraints:
    "foo_id_check" CHECK (id >= 1 AND id <= 2)

This query returns the definition of all constraints on the table using pg_get_constraintdef():

postgres=# SELECT pg_get_constraintdef(oid) 
             FROM pg_constraint
            WHERE conrelid='foo'::regclass;
       pg_get_constraintdef        
-----------------------------------
 CHECK (((id >= 1) AND (id <= 2)))
(1 row)

Using the pretty option:

postgres=# SELECT pg_get_constraintdef(oid, TRUE)
             FROM pg_constraint
            WHERE conrelid='foo'::regclass;
    pg_get_constraintdef     
-----------------------------
 CHECK (id >= 1 AND id <= 2)
(1 row)

Categories

System function

See also

pg_get_functiondef(), pg_get_indexdef(), pg_get_ruledef(), pg_get_statisticsobjdef(), pg_get_triggerdef(), pg_get_viewdef()