postgres.git / summary / log / commit / refs

commit    6e793582bc663fb7652834170be8902b982b7bcb
Author:   Álvaro Herrera <alvherre@alvh.no-ip.org>
Date:     Wed Nov 27 12:50:27 2024 +0000
First released in: 18.0, 17.3

    Fix pg_get_constraintdef for NOT NULL constraints on domains
    
    We added pg_constraint rows for all not-null constraints, first for
    tables and later for domains; but while the ones for tables were
    reverted, the ones for domains were not.  However, we did accidentally
    revert ruleutils.c support for the ones on domains in 6f8bb7c1e961,
    which breaks running pg_get_constraintdef() on them.  Put that back.
    
    This is only needed in branch 17, because we've reinstated this code in
    branch master with commit 14e87ffa5c54.  Add some new tests in both
    branches.
    
    I couldn't find anything else that needs de-reverting.
    
    Reported-by: Erki Eessaar <erki.eessaar@taltech.ee>
    Reviewed-by: Magnus Hagander <magnus@hagander.net>
    Discussion: https://postgr.es/m/AS8PR01MB75110350415AAB8BBABBA1ECFE222@AS8PR01MB7511.eurprd01.prod.exchangelabs.com


src/backend/utils/adt/ruleutils.c | 5 +++++ src/test/regress/expected/domain.out | 14 ++++++++++++++ src/test/regress/sql/domain.sql | 11 +++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index dc6d1d6dc52..ea5d8683364 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -2499,6 +2499,11 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, conForm->connoinherit ? " NO INHERIT" : ""); break; } + + case CONSTRAINT_NOTNULL: + appendStringInfoString(&buf, "NOT NULL"); + break; + case CONSTRAINT_TRIGGER: /* diff --git a/src/test/regress/expected/domain.out b/src/test/regress/expected/domain.out index f65b66345ab..42b6559f9c8 100644 --- a/src/test/regress/expected/domain.out +++ b/src/test/regress/expected/domain.out @@ -743,6 +743,20 @@ update domnotnull set col1 = null; -- fails ERROR: domain dnotnulltest does not allow null values alter domain dnotnulltest drop not null; update domnotnull set col1 = null; +update domnotnull set col1 = 5; +-- these constraints can also be added and removed by name +alter domain dnotnulltest add constraint dnotnulltest_notnull not null; +update domnotnull set col1 = null; -- fails +ERROR: domain dnotnulltest does not allow null values +select conname, pg_get_constraintdef(oid) from pg_constraint + where contypid = 'dnotnulltest'::regtype; + conname | pg_get_constraintdef +----------------------+---------------------- + dnotnulltest_notnull | NOT NULL +(1 row) + +alter domain dnotnulltest drop constraint dnotnulltest_notnull; +update domnotnull set col1 = null; drop domain dnotnulltest cascade; NOTICE: drop cascades to 2 other objects DETAIL: drop cascades to column col2 of table domnotnull diff --git a/src/test/regress/sql/domain.sql b/src/test/regress/sql/domain.sql index b5a70ee8be7..ee07b03174e 100644 --- a/src/test/regress/sql/domain.sql +++ b/src/test/regress/sql/domain.sql @@ -429,6 +429,17 @@ alter domain dnotnulltest drop not null; update domnotnull set col1 = null; +update domnotnull set col1 = 5; + +-- these constraints can also be added and removed by name +alter domain dnotnulltest add constraint dnotnulltest_notnull not null; +update domnotnull set col1 = null; -- fails +select conname, pg_get_constraintdef(oid) from pg_constraint + where contypid = 'dnotnulltest'::regtype; + +alter domain dnotnulltest drop constraint dnotnulltest_notnull; +update domnotnull set col1 = null; + drop domain dnotnulltest cascade; -- Test ALTER DOMAIN .. DEFAULT .. [parent: 7786717b9e5d]