BUG #19847: pg_dump loses ENFORCED on inherited CHECK under NOT ENFORCED parent

Started by PG Bug reporting form21 days ago1 messagesbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19847
Logged by: Atish Prakash
Email address: 125atishprak@gmail.com
PostgreSQL version: 19rc1
Operating system: Linux x86_64
Description:

If a child's inherited CHECK constraint is made ENFORCED while the parent's
stays
NOT ENFORCED, a dump and restore silently makes the child's constraint NOT
ENFORCED. The restored table then accepts rows the original rejects, with no
warning from pg_dump or from the restore.

CREATE TABLE ckp(a int, CONSTRAINT c CHECK (a > 0) NOT ENFORCED);
CREATE TABLE ckc() INHERITS (ckp);
ALTER TABLE ckc ALTER CONSTRAINT c ENFORCED;

INSERT INTO ckc VALUES (-1);
-- ERROR: new row for relation "ckc" violates check constraint "c"

Then run pg_dump and restore into an empty database:

INSERT INTO ckc VALUES (-1);
-- INSERT 0 1

pg_constraint for ckc.c:

original: conenforced = t, convalidated = t, conislocal = f,
coninhcount = 1
restored: conenforced = f, convalidated = f, conislocal = f,
coninhcount = 1

Expected: the restored child constraint stays ENFORCED.

The dump contains only the parent's constraint, which the child inherits
when it
is created, so nothing records the child's different state. A child that has
its
own local constraint is not affected: an ENFORCED partition attached under a
NOT
ENFORCED parent restored correctly in my test.

pg_upgrade from 19rc1 to master kept ENFORCED in my test, so this affects
plain
dumps.

Probably the same mechanism: a child's constraint that was validated while
the
parent's stays NOT VALID comes back NOT VALID after a restore:

CREATE TABLE c8p(a int);
CREATE TABLE c8c(a int) INHERITS (c8p);
ALTER TABLE c8p ADD CONSTRAINT k CHECK (a > 0) NOT VALID;
ALTER TABLE c8c VALIDATE CONSTRAINT k;

I have not written a patch.

Found with dump and restore differential tests. I reproduced and checked the
result myself.