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

From: Raja Sai pranav <rajasaipranav0(at)gmail(dot)com>
To: exclusion(at)gmail(dot)com
Cc: alvherre(at)kurilemu(dot)de, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19928: pg_upgrade fails on v18 schema with NOT NULL ... NO INHERIT and inheritance
Date: 2026-11-30 16:21:13
Message-ID: CAPHduZC6aKSphH3=SqCBdOLMxrUM4uESX3tFSi71t4aaKABjOg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 29.11.2026, Alexander Lakhin wrote:
> CREATE TABLE parent(i int);
> CREATE TABLE child() INHERITS (parent);
> ALTER TABLE parent ADD NOT NULL i NOT VALID;
>
> COMMENT ON CONSTRAINT parent_i_not_null ON child IS 'comment on child';
> [...]
> pg_restore: error: could not execute query: ERROR: constraint
"parent_i_not_null" for table "child" does not exist

Here is v3. 0001 is v2 unchanged; 0002 fixes this case.

The child's constraint is not dumped itself, since restoring the parent's
creates it, but the COMMENT was made to depend on the child's constraint,
which has no TOC entry. So nothing ordered the COMMENT after the parent's
ADD CONSTRAINT. 0002 records, in flagInhAttrs(), the nearest ancestor that
defines a constraint of that name locally; the child's constraint depends
on it, which orders a plain dump, and the COMMENT depends on it directly,
which orders pg_restore. Taking the nearest local one matters when a table
in between has only the inherited constraint: its constraint has the same
name but isn't dumped either.

With both patches, your case and the same with a grandchild and a middle
table restore with plain and pg_restore -j4, along with the cases from my
earlier mails, and the regress, pg_dump and pg_upgrade suites pass on
master.

I added the case to constraints.sql, but it doesn't make a reliable test
for 0002: pg_upgrade restores serially, and with 0001 applied the serial
order already happens to put the COMMENT last. The parallel failure is a
race between two workers (pg_restore picks ready items by size, then dump
ID), and adding the case to
https://www.google.com/url?q=http://004_pg_dump_parallel.pl&source=gmail&ust=1796142070554000&sa=E
passed without 0002 in
3 runs out of 3, so I left that out. Without 0002, pg_restore -j4 failed
in 1 of 3 runs here.

Regards,
Sai Pranav

On Sun, Nov 29, 2026 09:30 PM, Alexander Lakhin <exclusion(at)gmail(dot)com> wrote:

> Hello Sai,
>
> 28.11.2026 17:09, Raja Sai pranav wrote:
> > On 27.11.2026, Alexander Lakhin wrote:
> > > I've found another case, which probably should be fixed as well:
> > > create table inh_nn1 (f1 int primary key references inh_nn1);
> > > alter table inh_nn1 alter constraint inh_nn1_f1_not_null no inherit;
> > > ...
> > > pg_restore: error: could not execute query: ERROR: cannot create
> primary key on column "f1"
> >
> > Thanks. This one isn't about inheritance or the foreign key, and it
> > isn't specific to pg_upgrade. A plain table is enough:
> >
> > create table t (f1 int primary key);
> > alter table t alter constraint t_f1_not_null no inherit;
> >
> > and then a plain pg_dump of it fails to restore with the same error,
> > on master and on 18.7.
>
> Thank you working on this!
>
> I've discovered one more issue related to NOT NULL:
> echo "CREATE DATABASE regression;
> \connect regression
>
> CREATE TABLE parent(i int);
> CREATE TABLE child() INHERITS (parent);
> ALTER TABLE parent ADD NOT NULL i NOT VALID;
>
> COMMENT ON CONSTRAINT parent_i_not_null ON child IS 'comment on child';
> " >/tmp/db.sql
>
> makes pg_upgrade fail as below:
> olddump=/tmp/db.sql oldinstall=`pwd`/tmp_install/usr/local/pgsql
> PROVE_TESTS="t/002*" make -s check -C src/bin/pg_upgrade/
>
> # Restoring database schemas in the new cluster
> # *failure*
> #
> # Consult the last few lines of
> ".../src/bin/pg_upgrade/tmp_check/t_002_pg_upgrade_new_node_
> data/pgdata/pg_upgrade_output.d/20261129T172103.889/log/pg_upgrade_dump_16384.log"
>
> for
> # the probable cause of the failure.
> # Failure, exiting
>
> cat ".../pg_upgrade_dump_16384.log"
> pg_restore: while PROCESSING TOC:
> pg_restore: from TOC entry 3535; 0 0 COMMENT CONSTRAINT
> "parent_i_not_null" ON "child" law
> pg_restore: error: could not execute query: ERROR: constraint
> "parent_i_not_null" for table "child" does not exist
> Command was: COMMENT ON CONSTRAINT "parent_i_not_null" ON "public"."child"
> IS 'comment on child';
>
>
> Best regards,
> Alexander
>

Attachment Content-Type Size
v3-0001-pg_dump-Restore-a-child-s-NOT-VALID-not-null-cons.patch text/x-patch 9.6 KB
v3-0002-pg_dump-Restore-comments-on-inherited-NOT-VALID-n.patch text/x-patch 8.4 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Nitin Motiani 2026-11-30 16:39:43 Re: BUG #19936: Planning is slower on 19.0 with many partial indexes on a SQL function
Previous Message Srinath Reddy Sadipiralla 2026-11-30 16:18:28 Re: BUG #19952: psql \d+ omits nondefault replica identity for partitioned tables