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

Started by Raja Sai pranav3 days ago4 messagesbugs

Thread notes

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

@global #bug

#bug
Jump to latest
#1Raja Sai pranav
rajasaipranav0@gmail.com

On 26/11/2026 07:00, Alexander Lakhin wrote:

Would you like to look also at a close one?:
[...]
ALTER TABLE notnull_tbl1 ADD CONSTRAINT nn NOT NULL a NOT VALID; -- ok
CREATE TABLE notnull_tbl1_child2 (c int, b int, a int);
ALTER TABLE notnull_tbl1_child2 ADD NOT NULL a NOT VALID;
ALTER TABLE notnull_tbl1_child2 INHERIT notnull_tbl1;

Yes, thanks. I reproduced it: a master pg_dump of this schema from 18.7
fails to restore into master with the same error. It doesn't need
pg_upgrade or an older server, though: creating the schema on master
and doing a plain pg_dump / psql restore into another master database
fails the same way.

pg_dump emits both NOT VALID constraints as separate ALTER TABLE ... ADD
CONSTRAINT commands after the data, parent first. Adding "nn" to the
parent recurses and gives the child an inherited constraint named "nn",
so the child's own command then fails the name check added by 96e2af605.

I'll look into a fix, unless Álvaro would rather handle it.

Regards,
Sai Pranav

#2Alvaro Herrera
alvherre@kurilemu.de
In reply to: Raja Sai pranav (#1)

On 2026-Nov-26, Raja Sai pranav wrote:

Yes, thanks. I reproduced it: a master pg_dump of this schema from 18.7
fails to restore into master with the same error. It doesn't need
pg_upgrade or an older server, though: creating the schema on master
and doing a plain pg_dump / psql restore into another master database
fails the same way.

pg_dump emits both NOT VALID constraints as separate ALTER TABLE ... ADD
CONSTRAINT commands after the data, parent first. Adding "nn" to the
parent recurses and gives the child an inherited constraint named "nn",
so the child's own command then fails the name check added by 96e2af605.

Interesting.

I'll look into a fix, unless Álvaro would rather handle it.

Please go ahead.

--
Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/

#3Raja Sai pranav
rajasaipranav0@gmail.com
In reply to: Alvaro Herrera (#2)

On 2026-Nov-26, Álvaro Herrera wrote:

I'll look into a fix, unless Álvaro would rather handle it.

Please go ahead.

Here is a patch. pg_dump now makes each ancestor's NOT VALID not-null
constraints depend on the child's own, so the child's command runs first
and the parent's then merges into it, the same way the original schema
was built. The restored catalog then matches the source: the child keeps
its own constraint name, with conislocal = true and coninhcount = 1.

I checked plain and parallel (pg_restore -j4) restores of these cases
with and without the patch:

- the schema from Alexander's report;
- three levels, each with its own NOT VALID constraint;
- a middle table with only the inherited constraint, between a parent
and a grandchild that both have their own;
- a child with two parents, each with a NOT VALID constraint;
- a partitioned table with a NOT VALID constraint.

Without the patch, all but the partitioned case fail to restore. With
it, all of them restore and the catalog matches the source.

I added a table pair to constraints.sql and left it in place, so that
002_pg_upgrade covers this. Without the pg_dump change, that test fails
with the reported error. With it, the pg_upgrade, pg_dump and regress
suites pass on master.

REL_18_STABLE now has the same failure: the name check is there too,
after 18.7. An 18.7 dump still restores, but the child silently gets
the parent's constraint name with conislocal = false. That is the dump
mismatch Alexander mentioned for the commit before the check. The
second attachment is the same fix for REL_18_STABLE. The pg_upgrade,
pg_dump and regress suites pass there, and 002_pg_upgrade fails
without it. The master patch applies to REL_19_STABLE as is.

Regards,
Sai Pranav

On Thu, Nov 26, 2026 06:26 PM, Alvaro Herrera <alvherre@kurilemu.de> wrote:

Show quoted text

On 2026-Nov-26, Raja Sai pranav wrote:

Yes, thanks. I reproduced it: a master pg_dump of this schema from 18.7
fails to restore into master with the same error. It doesn't need
pg_upgrade or an older server, though: creating the schema on master
and doing a plain pg_dump / psql restore into another master database
fails the same way.

pg_dump emits both NOT VALID constraints as separate ALTER TABLE ... ADD
CONSTRAINT commands after the data, parent first. Adding "nn" to the
parent recurses and gives the child an inherited constraint named "nn",
so the child's own command then fails the name check added by 96e2af605.

Interesting.

I'll look into a fix, unless Álvaro would rather handle it.

Please go ahead.

--
Álvaro Herrera PostgreSQL Developer —
https://www.EnterpriseDB.com/

Attachments:

v1-0001-pg_dump-Restore-a-child-s-NOT-VALID-not-null-cons.patchtext/x-patch; charset=US-ASCII; name=v1-0001-pg_dump-Restore-a-child-s-NOT-VALID-not-null-cons.patchDownload+66-1
v1-0001-pg_dump-Restore-a-child-s-NOT-VALID-not-null-cons-REL_18.patchtext/x-patch; charset=US-ASCII; name=v1-0001-pg_dump-Restore-a-child-s-NOT-VALID-not-null-cons-REL_18.patchDownload+66-1
#4Alexander Lakhin
exclusion@gmail.com
In reply to: Raja Sai pranav (#3)

Hello Sai,

26.11.2026 23:22, Raja Sai pranav wrote:

On 2026-Nov-26, Álvaro Herrera wrote:

I'll look into a fix, unless Álvaro would rather handle it.

Please go ahead.

Here is a patch. pg_dump now makes each ancestor's NOT VALID not-null
constraints depend on the child's own, so the child's command runs first
and the parent's then merges into it, the same way the original schema
was built. The restored catalog then matches the source: the child keeps
its own constraint name, with conislocal = true and coninhcount = 1.

I checked plain and parallel (pg_restore -j4) restores of these cases
with and without the patch:

- the schema from Alexander's report;
- three levels, each with its own NOT VALID constraint;
- a middle table with only the inherited constraint, between a parent
and a grandchild that both have their own;
- a child with two parents, each with a NOT VALID constraint;
- a partitioned table with a NOT VALID constraint.

I've found another case, which probably should be fixed as well:
echo "CREATE DATABASE regression;
\connect regression

create table inh_nn1 (f1 int primary key references inh_nn1);
alter table inh_nn1 alter constraint inh_nn1_f1_not_null no inherit;

-- Tables needed for the version-adaptation step
CREATE TABLE brintest (lsncol pg_lsn);
CREATE TABLE tab_core_types (pg_lsn pg_lsn);
CREATE TABLE brintest_multi (lsncol pg_lsn);
CREATE TABLE brintest_bloom (lsncol pg_lsn);
" > /tmp/db.sql

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/20261127T100429.284/log/pg_upgrade_dump_16384.log"
for
# the probable cause of the failure.
# Failure, exiting

tail .../pg_upgrade_dump_16384.log
...
pg_restore: while PROCESSING TOC:
pg_restore: from TOC entry 3382; 2606 16390 CONSTRAINT inh_nn1 inh_nn1_pkey law
pg_restore: error: could not execute query: ERROR:  cannot create primary key on column "f1"
DETAIL:  The constraint "inh_nn1_f1_not_null" on column "f1" of table "inh_nn1", marked NO INHERIT, is incompatible with
a primary key.
HINT:  You might need to make the existing constraint inheritable using ALTER TABLE ... ALTER CONSTRAINT ... INHERIT.
Command was:
-- For binary upgrade, must preserve pg_class oids, toast chunk type oids and relfilenodes
SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('16389'::pg_catalog.oid);
SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('16389'::pg_catalog.oid);

ALTER TABLE ONLY "public"."inh_nn1"
    ADD CONSTRAINT "inh_nn1_pkey" PRIMARY KEY ("f1");

Best regards,
Alexander