BUG #19928: pg_upgrade fails on v18 schema with NOT NULL ... NO INHERIT and inheritance
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
The following bug has been logged on the website:
Bug reference: 19928
Logged by: Alexander Lakhin
Email address: exclusion@gmail.com
PostgreSQL version: 19.0
Operating system: Ubuntu 24.04
Description:
A database with the following contents:
echo "CREATE DATABASE regression;
\connect regression
CREATE TABLE regress_notnull3 (a int, NOT NULL a NO INHERIT);
CREATE TABLE regress_notnull4 () INHERITS (regress_notnull3);
INSERT INTO regress_notnull4 VALUES (NULL);
-- 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/db18.sql
can't be upgraded from v18 to v19, e.g. with:
olddump=/tmp/db18.sql oldinstall=/tmp/pg18 PROVE_TESTS="t/002*" make -s
check -C src/bin/pg_upgrade/
due to this error during the consistency check:
# Checking for not-null constraint inconsistencies fatal
#
# Your installation contains inconsistent NOT NULL constraints.
# If the parent column(s) are NOT NULL, then the child column must
# also be marked NOT NULL, or the upgrade will fail.
# You can fix this by running
# ALTER TABLE tablename ALTER column SET NOT NULL;
# on each column listed in the file:
#
.../src/bin/pg_upgrade/tmp_check/t_002_pg_upgrade_new_node_data/pgdata/pg_upgrade_output.d/20261121T024932.279/not_null_inconsistent_columns.txt
# Failure, exiting
cat
.../src/bin/pg_upgrade/tmp_check/t_002_pg_upgrade_new_node_data/pgdata/pg_upgrade_output.d/20261121T024932.279/not_null_inconsistent_columns.txt
In database: regression
public.regress_notnull4.a
Reproduced starting from f295494d3.
Hi,
I reproduced this with pg_upgrade from master and an 18.7 old cluster,
using just the two tables from your script.
check_for_not_null_inheritance() reports a child column that is nullable
while the parent's column is NOT NULL, but it doesn't look at whether the
parent's not-null constraint is NO INHERIT. Such a constraint doesn't apply
to the children, so this is a valid v18 schema.
The attached patch skips parent columns whose not-null constraint (contype
'n') is NO INHERIT. Versions before 18 have no not-null constraints in
pg_constraint, so the check is unchanged for them.
With the patch, pg_upgrade --check passes and the full upgrade works: the
NULL row is kept, the child column stays nullable, and the parent's
constraint is still NO INHERIT. A real inconsistency is still reported: I
made one on 18 by clearing attnotnull on a child column in pg_attribute,
and the check still lists it.
I didn't add a test, because the check only runs when the old cluster is 18
or older, so the same-version pg_upgrade tests never reach it. The patch is
for master and applies to REL_19_STABLE; REL_18_STABLE doesn't have this
check.
Regards,
Sai Pranav
Hello Sai,
22.11.2026 15:45, Raja Sai pranav wrote:
I reproduced this with pg_upgrade from master and an 18.7 old cluster, using just the two tables from your script.
check_for_not_null_inheritance() reports a child column that is nullable while the parent's column is NOT NULL, but it
doesn't look at whether the parent's not-null constraint is NO INHERIT. Such a constraint doesn't apply to the
children, so this is a valid v18 schema.
Thank you for paying attention to that defect!
Would you like to look also at a close one?:
echo "CREATE DATABASE regression;
\connect regression
CREATE TABLE notnull_tbl1 (a int, b int);
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;
-- 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/db18.sql
makes
olddump=/tmp/db18.sql oldinstall=/tmp/pg18 PROVE_TESTS="t/002*" make -s check -C src/bin/pg_upgrade/
fail as below:
# 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/20261126T063528.875/log/pg_upgrade_dump_16384.log"
for
# the probable cause of the failure.
cat .../pg_upgrade_dump_16384.log
...
pg_restore: from TOC entry 3359; 2606 16392 CONSTRAINT notnull_tbl1_child2 notnull_tbl1_child2_a_not_null law
pg_restore: error: could not execute query: ERROR: cannot create not-null constraint "notnull_tbl1_child2_a_not_null"
on column "a" of table "notnull_tbl1_child2"
DETAIL: A not-null constraint named "nn" already exists for this column.
Command was: ALTER TABLE "public"."notnull_tbl1_child2"
ADD CONSTRAINT "notnull_tbl1_child2_a_not_null" NOT NULL "a" NOT VALID;
This can also be reproduced with:
sed -i '0,/^ALTER TABLE notnull_tbl1_child2 INHERIT notnull_tbl1;$/!d' src/test/regress/sql/constraints.sql
src/test/regress/expected/constraints.out
PROVE_TESTS="t/002*" make -s check -C src/bin/pg_upgrade/
Reproduced starting from 96e2af605 (at the previous commit, pg_upgrade
succeeds, but a follow-up dump compare fails).
Best regards,
Alexander
On 22/11/2026 15:45, Raja Sai pranav wrote:
Hi,
I reproduced this with pg_upgrade from master and an 18.7 old cluster,
using just the two tables from your script.check_for_not_null_inheritance() reports a child column that is nullable
while the parent's column is NOT NULL, but it doesn't look at whether
the parent's not-null constraint is NO INHERIT. Such a constraint
doesn't apply to the children, so this is a valid v18 schema.The attached patch skips parent columns whose not-null constraint
(contype 'n') is NO INHERIT. Versions before 18 have no not-null
constraints in pg_constraint, so the check is unchanged for them.With the patch, pg_upgrade --check passes and the full upgrade works:
the NULL row is kept, the child column stays nullable, and the parent's
constraint is still NO INHERIT. A real inconsistency is still reported:
I made one on 18 by clearing attnotnull on a child column in
pg_attribute, and the check still lists it.I didn't add a test, because the check only runs when the old cluster is
18 or older, so the same-version pg_upgrade tests never reach it. The
patch is for master and applies to REL_19_STABLE; REL_18_STABLE doesn't
have this check.
REL_18_STABLE does have this check too, it runs on a same-version
upgrade. But _why_ do we run that check on a 18->18 upgrade? The
scenario it's checking for should not be possible on a v18 cluster.
There may have been a point in running that check in the v18 development
cycle, for upgrades from older v18devel version to newer v18devel
version. But commit 14e87ffa5c54 landed pretty early in the development
cycle, way before the beta period, so there's no need to support that
anymore.
So I think this needs to be backpatched to REL_18_STABLE too, and the
version guard should be fixed to:
diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c
index 0c150ad4467..ea4b88c35bc 100644
--- a/src/bin/pg_upgrade/check.c
+++ b/src/bin/pg_upgrade/check.c
@@ -675,8 +675,8 @@ check_and_dump_old_cluster(void)
/*
* Pre-PG 18 allowed child tables to omit not-null constraints that their
* parents columns have, but schema restore fails for them. Verify there
* are none, iff applicable.
*/
- if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1800)
+ if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1700)
check_for_not_null_inheritance(&old_cluster);
And once you do that, you don't need to change the query in
check_for_not_null_inheritance() anymore, the query is correct for
pre-18 clusters. So all we need to do is fix that version guard.
Am I missing something?
- Heikki
On 26/11/2026 09:32, Heikki Linnakangas wrote:
REL_18_STABLE does have this check too, it runs on a same-version
upgrade. But _why_ do we run that check on a 18->18 upgrade? The
scenario it's checking for should not be possible on a v18 cluster.
You're right, and I was wrong about REL_18_STABLE: the check is there, with
the same <= 1800 guard.
I tested the scenario on 18.7: dropping the not-null constraint of a child
column whose parent has an inheritable one fails with "cannot drop
inherited constraint", and a NOT VALID constraint added on the parent gives
the child a matching one. So I found no way to reach that state on v18.
So I think this needs to be backpatched to REL_18_STABLE too, and the
version guard should be fixed to:
[...]
And once you do that, you don't need to change the query in
check_for_not_null_inheritance() anymore, the query is correct for
pre-18 clusters. So all we need to do is fix that version guard.
With only that change, pg_upgrade --check from 18.7 to master passes on the
NO INHERIT schema from the report, and the full upgrade works. A PG 14
cluster with a real inconsistency (a child column whose NOT NULL was
dropped) is still reported, and a clean PG 14 cluster passes.
v2 attached: just your version guard change, with a commit message. It
applies to master and to REL_18_STABLE. There is still no test, since the
same-version pg_upgrade tests no longer reach the check.
Regards,
Sai Pranav
On 26/11/2026 11:53, Raja Sai pranav wrote:
On 26/11/2026 09:32, Heikki Linnakangas wrote:
REL_18_STABLE does have this check too, it runs on a same-version
upgrade. But _why_ do we run that check on a 18->18 upgrade? The
scenario it's checking for should not be possible on a v18 cluster.You're right, and I was wrong about REL_18_STABLE: the check is there,
with the same <= 1800 guard.I tested the scenario on 18.7: dropping the not-null constraint of a
child column whose parent has an inheritable one fails with "cannot drop
inherited constraint", and a NOT VALID constraint added on the parent
gives the child a matching one. So I found no way to reach that state on
v18.So I think this needs to be backpatched to REL_18_STABLE too, and the
version guard should be fixed to:
[...]
And once you do that, you don't need to change the query in
check_for_not_null_inheritance() anymore, the query is correct for
pre-18 clusters. So all we need to do is fix that version guard.With only that change, pg_upgrade --check from 18.7 to master passes on
the NO INHERIT schema from the report, and the full upgrade works. A PG
14 cluster with a real inconsistency (a child column whose NOT NULL was
dropped) is still reported, and a clean PG 14 cluster passes.v2 attached: just your version guard change, with a commit message. It
applies to master and to REL_18_STABLE. There is still no test, since
the same-version pg_upgrade tests no longer reach the check.
Pushed, thanks.
- Heikki