Re: BUG #19928: pg_upgrade fails on v18 schema with NOT NULL ... NO INHERIT and inheritance

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: Raja Sai pranav <rajasaipranav0(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org, exclusion(at)gmail(dot)com, alvherre(at)kurilemu(dot)de
Subject: Re: BUG #19928: pg_upgrade fails on v18 schema with NOT NULL ... NO INHERIT and inheritance
Date: 2026-11-30 22:27:45
Message-ID: CAN4CZFMsnGqU+gwMK=yPF6vXXdB5_RNgY=7gHCr4-7LhCjOP6A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hello!

I started testing/reviewing this patch, here are some initial findings:

1.

+ * addInvalidNotNullDependencies
+ * Make the NOT VALID not-null constraints on column attname of all of
+ * tbinfo's ancestors depend on childcon, a NOT VALID not-null constraint
+ * that a descendant of theirs defines locally on that column.

We can create inheritance cycles like the following:

CREATE TABLE r (x int); CREATE TABLE a () INHERITS (r); CREATE TABLE b
() INHERITS (a);
CREATE TABLE d (x int); CREATE TABLE c () INHERITS (d);
ALTER TABLE c ADD CONSTRAINT c_nn NOT NULL x NOT VALID;
ALTER TABLE d ADD CONSTRAINT d_nn NOT NULL x NOT VALID;
ALTER TABLE r ADD CONSTRAINT r_nn NOT NULL x NOT VALID;
-- s1: BEGIN; ALTER TABLE d INHERIT b;
-- s2: BEGIN; ALTER TABLE a INHERIT c;
-- s1: COMMIT; s2: COMMIT;

while this isn't entirely a supported schema, it can happen, even if
by accident, and currently crashes the patched pg_dump:

pg_dump -Fc / -s / --binary-upgrade: Segmentation fault (core dumped), rc=139

Which definitely shouldn't happen.

2.

+ inhAttrInd = strInArray(attname, parent->attnames, parent->numatts);
+ if (inhAttrInd < 0)
+ continue;

Only direct parents of dumped tables get their attributes loaded, so
the walk stops at the first ancestor that is neither dumped nor a
direct parent. With g <- p2 <- p1 <- c (a_nn on g, own z_nn on c), the
pre-data section restored, then:

create table g (a int);
create table p2 () inherits (g);
create table p1 () inherits (p2);
create table c (a int);
alter table c add constraint z_nn not null a not valid;
alter table c inherit p1;
alter table g add constraint a_nn not null a not valid;

pg_dump --section=post-data -t g -t c -> ADD a_nn ... ; ADD z_nn ...
ERROR: cannot create not-null constraint "z_nn" on column "a" of table "c"
DETAIL: A not-null constraint named "a_nn" already exists for this column.
pg_dump --section=post-data -t g -t p2 -t c -> OK

3.

NO INHERIT ancestors change a restored name:

CREATE TABLE root (a int);
CREATE TABLE branch () INHERITS (root);
CREATE TABLE other (a int);
CREATE TABLE leaf () INHERITS (branch, other);
ALTER TABLE root ADD CONSTRAINT aa NOT NULL a NO INHERIT NOT VALID;
ALTER TABLE other ADD CONSTRAINT mm NOT NULL a NOT VALID;
ALTER TABLE branch ADD CONSTRAINT zz NOT NULL a NOT VALID;

master pg_dump: leaf has mm
patched pg_dump: leaf has zz

+1, similar to the above but most likely a separate independent issue
that shouldn't be part of this patch:

CREATE TABLE pt (a int) PARTITION BY LIST (a);
ALTER TABLE pt ADD CONSTRAINT pt_nn NOT NULL a NOT VALID;
CREATE TABLE pp (a int); ALTER TABLE pp ADD CONSTRAINT pp_nn NOT NULL
a NOT VALID;
ALTER TABLE pt ATTACH PARTITION pp FOR VALUES IN (1);

But after dump/restore pp's contraint will be also called pt_nn.

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Zsolt Parragi 2026-11-30 23:17:42 Re: BUG #19957: jsonpath `last` returns the wrong index after a filter containing `exists($x[0])`
Previous Message Robert Haas 2026-11-30 21:59:04 Re: BUG #19519: REPACK can fail due to missing chunk for toast value