Patch
Fix loss of inherited constraints in ALTER COLUMN SET EXPRESSION
- Branch
- master
- Risk
- 60
- Base commit
- dc168a83874fbf5cee4afb76a623e1349a539724
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a234a5-8425-7393-9a63-41a61487f4dc | jq -r .body | git am
From c342382bd781794a8cd6bd67efda8a7bbe50924b Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Tue, 1 Dec 2026 18:44:31 -0800
Subject: [PATCH] Fix loss of inherited constraints in ALTER COLUMN SET
EXPRESSION
ALTER TABLE ... ALTER COLUMN ... SET EXPRESSION reuses the ALTER
COLUMN TYPE machinery for the objects that depend on the column: it
drops each dependent constraint and re-creates it from its definition.
ATPostAlterTypeCleanup() does not re-create a constraint that is only
inherited, expecting the recursion from re-adding the parent's
constraint to do that. This works for ALTER COLUMN TYPE, which always
alters the parent's column as well, but SET EXPRESSION can be applied
to an inheritance child or a partition on its own, or with ONLY to a
parent. In those cases:
- The inherited CHECK and not-null constraints of the altered child
were dropped and never re-created. The child silently stopped
enforcing the parent's CHECK constraints, so rows violating them
could be inserted, and a later ALTER TABLE ... DROP NOT NULL on the
parent failed with "cache lookup failed for not-null constraint".
- A constraint that was both local and inherited came back as local
only, after which dropping the parent's constraint failed.
- With ONLY on a parent, re-adding the parent's constraint recursed
to the children and merged into their copies, which had not been
dropped, so their coninhcount went up.
There is no need to re-create these constraints at all: changing the
generation expression changes neither the column's type nor the
definition of any CHECK or not-null constraint of the table, only the
column's values. So keep the table's own CHECK and not-null
constraints as they are and just verify them in Phase 3: queue each
enforced and validated CHECK constraint for the Phase 3 scan or
rewrite, and rely on the existing verification of not-null
constraints, which is the table rewrite for a stored column and
verify_new_notnull for a virtual one. NOT VALID and NOT ENFORCED
constraints are not checked, as before. Other kinds of constraints,
and constraints of other relations or of domains that depend on the
column through the table's rowtype, are still rebuilt as before.
As a side effect, constraints now keep their OIDs, and SET EXPRESSION
on a parent no longer fails with "incompatible NOT VALID constraint"
when a child has its own valid not-null constraint on the column
while the parent's is NOT VALID.
This takes the same approach as the patch that Chao Li posted, which
follows jian he's observation that the constraints only need to be
rechecked.
The stored-column case has existed since SET EXPRESSION was added in
v17 (commit 5d06e99a3cf); the virtual-column case since commit
f80bedd52b1 in v19, which made SET EXPRESSION on a virtual column
rebuild the dependent constraints.
Discussion: https://postgr.es/m/CACJufxEomSz3BwUGfk8A6MeAKj=Cki5B+gYTbJO=ACCJyytWZg@mail.gmail.com
Discussion: https://postgr.es/m/1A7CBF99-628F-4A75-A897-135BC410C6F0@gmail.com
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
src/backend/commands/tablecmds.c | 103 +++++++++++++++++-
.../regress/expected/generated_stored.out | 77 +++++++++++++
.../regress/expected/generated_virtual.out | 77 +++++++++++++
src/test/regress/sql/generated_stored.sql | 43 ++++++++
src/test/regress/sql/generated_virtual.sql | 43 ++++++++
5 files changed, 340 insertions(+), 3 deletions(-)
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 9755943f110..939550fa22f 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -679,6 +679,7 @@ static ObjectAddress ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
static void RememberAllDependentForRebuilding(AlteredTableInfo *tab, AlterTableType subtype,
Relation rel, AttrNumber attnum, const char *colName);
static void RememberWholeRowDependentForRebuilding(AlteredTableInfo *tab, AlterTableType subtype, Relation rel);
+static bool RememberConstraintForRecheck(Oid conoid, AlteredTableInfo *tab);
static void RememberConstraintForRebuilding(Oid conoid, AlteredTableInfo *tab);
static void RememberIndexForRebuilding(Oid indoid, AlteredTableInfo *tab);
static void RememberStatisticsForRebuilding(Oid stxoid, AlteredTableInfo *tab);
@@ -9303,7 +9304,9 @@ ATExecSetExpression(AlteredTableInfo *tab, Relation rel, const char *colName,
/*
* Find everything that depends on the column (constraints, indexes, etc),
- * and record enough information to let us recreate the objects.
+ * and record enough information to let us recreate the objects. The
+ * table's CHECK and not-null constraints are not recreated, just
+ * rechecked; see RememberConstraintForRecheck.
*/
RememberAllDependentForRebuilding(tab, AT_SetExpression, rel, attnum, colName);
@@ -16250,6 +16253,9 @@ RememberAllDependentForRebuilding(AlteredTableInfo *tab, AlterTableType subtype,
case ConstraintRelationId:
Assert(foundObject.objectSubId == 0);
+ if (subtype == AT_SetExpression &&
+ RememberConstraintForRecheck(foundObject.objectId, tab))
+ break;
RememberConstraintForRebuilding(foundObject.objectId, tab);
break;
@@ -16480,7 +16486,8 @@ RememberWholeRowDependentForRebuilding(AlteredTableInfo *tab, AlterTableType sub
*/
if (bms_is_member(InvalidAttrNumber - FirstLowInvalidHeapAttributeNumber, expr_attrs))
{
- RememberConstraintForRebuilding(conform->oid, tab);
+ if (!RememberConstraintForRecheck(conform->oid, tab))
+ RememberConstraintForRebuilding(conform->oid, tab);
}
}
}
@@ -16602,6 +16609,93 @@ RememberClusterOnForRebuilding(Oid indoid, AlteredTableInfo *tab)
tab->clusterOnIndex = get_rel_name(indoid);
}
+/*
+ * Subroutine for ATExecSetExpression: deal with a constraint that depends on
+ * a column whose generation expression is being changed, if it doesn't need
+ * to be rebuilt. Returns false if it does.
+ *
+ * Changing the generation expression changes neither the type of the column
+ * nor the definition of any CHECK or not-null constraint of the table that
+ * uses it; only the column's values change. So we keep those constraints as
+ * they are, and just have Phase 3 verify that the existing rows still satisfy
+ * them. Dropping and re-creating them instead would not work if the table is
+ * an inheritance child or partition and the command doesn't recurse from its
+ * parent: the constraints the table inherited would not be re-created, and
+ * the ones that are both local and inherited would come back as only local.
+ *
+ * Not-null constraints need nothing from us here, because Phase 3 verifies
+ * all valid ones anyway when it rewrites the table, as it does for a stored
+ * column, and ATExecSetExpression asks it to for a virtual column. CHECK
+ * constraints that are not enforced or not valid need no verification.
+ *
+ * Other kinds of constraints, and constraints of other relations or of
+ * domains (which can depend on the column through the table's rowtype), are
+ * still rebuilt as for ALTER COLUMN TYPE.
+ */
+static bool
+RememberConstraintForRecheck(Oid conoid, AlteredTableInfo *tab)
+{
+ HeapTuple tup;
+ Form_pg_constraint con;
+
+ tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(conoid));
+ if (!HeapTupleIsValid(tup)) /* should not happen */
+ elog(ERROR, "cache lookup failed for constraint %u", conoid);
+ con = (Form_pg_constraint) GETSTRUCT(tup);
+
+ if (con->conrelid != tab->relid ||
+ (con->contype != CONSTRAINT_CHECK &&
+ con->contype != CONSTRAINT_NOTNULL))
+ {
+ ReleaseSysCache(tup);
+ return false;
+ }
+
+ if (con->contype == CONSTRAINT_CHECK &&
+ con->conenforced && con->convalidated)
+ {
+ bool found = false;
+
+ /*
+ * A constraint can depend on more than one of the altered columns,
+ * and on the column both directly and through a whole-row reference;
+ * verify it only once.
+ */
+ foreach_ptr(NewConstraint, oldcon, tab->constraints)
+ {
+ if (oldcon->contype == CONSTR_CHECK && oldcon->conid == conoid)
+ {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ {
+ NewConstraint *newcon;
+ Datum conbin;
+
+ newcon = palloc0_object(NewConstraint);
+ newcon->name = pstrdup(NameStr(con->conname));
+ newcon->contype = CONSTR_CHECK;
+ newcon->conid = conoid;
+
+ /*
+ * Don't expand virtual generated columns here, since the column's
+ * new expression is not in place yet; ATRewriteTable does that.
+ */
+ conbin = SysCacheGetAttrNotNull(CONSTROID, tup,
+ Anum_pg_constraint_conbin);
+ newcon->qual = stringToNode(TextDatumGetCString(conbin));
+
+ tab->constraints = lappend(tab->constraints, newcon);
+ }
+ }
+
+ ReleaseSysCache(tup);
+ return true;
+}
+
/*
* Subroutine for ATExecAlterColumnType: remember that a constraint needs
* to be rebuilt (which we might already know).
@@ -16823,7 +16917,10 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
* new definition here; it'll get recreated when
* ATAddCheckNNConstraint recurses from adding the parent table's
* constraint. But we had to carry the info this far so that we can
- * drop the constraint below.
+ * drop the constraint below. (That relies on the parent's column
+ * being altered too, which ALTER COLUMN TYPE ensures. SET EXPRESSION
+ * doesn't, but it keeps the table's own CHECK and not-null
+ * constraints in place instead; see RememberConstraintForRecheck.)
*/
if (!conislocal)
continue;
diff --git a/src/test/regress/expected/generated_stored.out b/src/test/regress/expected/generated_stored.out
index d7a18e478f8..b554851b1c2 100644
--- a/src/test/regress/expected/generated_stored.out
+++ b/src/test/regress/expected/generated_stored.out
@@ -697,6 +697,83 @@ CREATE INDEX gtest20d_idx1 ON gtest20d (a) WHERE gtest20d = ROW (1, 2);
ALTER TABLE gtest20d ALTER COLUMN b SET EXPRESSION AS (a * 2::bigint); -- index rebuild
CREATE INDEX gtest20d_idx2 ON gtest20d ((gtest20d = ROW (1, 2)));
ALTER TABLE gtest20d ALTER COLUMN b SET EXPRESSION AS (a * 3); -- index rebuild
+-- SET EXPRESSION on a partition or inheritance child, or on ONLY the parent,
+-- keeps the constraints as they are and rechecks them
+CREATE TABLE gtest20e (a int, b int GENERATED ALWAYS AS (a * 2) STORED NOT NULL,
+ CONSTRAINT gtest20e_chk CHECK (b < 50)) PARTITION BY RANGE (a);
+CREATE TABLE gtest20e_1 PARTITION OF gtest20e FOR VALUES FROM (0) TO (100);
+CREATE TABLE gtest20f (a int, b int GENERATED ALWAYS AS (a * 2) STORED NOT NULL,
+ CONSTRAINT gtest20f_chk CHECK (b < 50));
+CREATE TABLE gtest20f_1 () INHERITS (gtest20f);
+CREATE TABLE gtest20f_2 (a int, b int GENERATED ALWAYS AS (a * 2) STORED NOT NULL,
+ CONSTRAINT gtest20f_chk CHECK (b < 50));
+ALTER TABLE gtest20f_2 INHERIT gtest20f;
+INSERT INTO gtest20e VALUES (20);
+INSERT INTO gtest20f_1 VALUES (20);
+INSERT INTO gtest20f_2 VALUES (20);
+ALTER TABLE gtest20f ADD CONSTRAINT gtest20f_nv CHECK (b < 0) NOT VALID;
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (a * 3); -- violates constraint
+ERROR: check constraint "gtest20e_chk" of relation "gtest20e_1" is violated by some row
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (nullif(a, 20)); -- violates constraint
+ERROR: column "b" of relation "gtest20e_1" contains null values
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (a + 1);
+ALTER TABLE ONLY gtest20f_1 ALTER COLUMN b SET EXPRESSION AS (a * 3); -- violates constraint
+ERROR: check constraint "gtest20f_chk" of relation "gtest20f_1" is violated by some row
+ALTER TABLE ONLY gtest20f_1 ALTER COLUMN b SET EXPRESSION AS (a + 1);
+ALTER TABLE gtest20f_2 ALTER COLUMN b SET EXPRESSION AS (a + 2);
+ALTER TABLE ONLY gtest20f ALTER COLUMN b SET EXPRESSION AS (a + 3);
+SELECT conrelid::regclass, conname, contype, conislocal, coninhcount,
+ convalidated
+ FROM pg_constraint
+ WHERE conrelid IN ('gtest20e'::regclass, 'gtest20e_1'::regclass,
+ 'gtest20f'::regclass, 'gtest20f_1'::regclass,
+ 'gtest20f_2'::regclass)
+ ORDER BY conrelid::regclass::text, conname;
+ conrelid | conname | contype | conislocal | coninhcount | convalidated
+------------+-----------------------+---------+------------+-------------+--------------
+ gtest20e | gtest20e_b_not_null | n | t | 0 | t
+ gtest20e | gtest20e_chk | c | t | 0 | t
+ gtest20e_1 | gtest20e_b_not_null | n | f | 1 | t
+ gtest20e_1 | gtest20e_chk | c | f | 1 | t
+ gtest20f | gtest20f_b_not_null | n | t | 0 | t
+ gtest20f | gtest20f_chk | c | t | 0 | t
+ gtest20f | gtest20f_nv | c | t | 0 | f
+ gtest20f_1 | gtest20f_b_not_null | n | f | 1 | t
+ gtest20f_1 | gtest20f_chk | c | f | 1 | t
+ gtest20f_1 | gtest20f_nv | c | f | 1 | f
+ gtest20f_2 | gtest20f_2_b_not_null | n | t | 1 | t
+ gtest20f_2 | gtest20f_chk | c | t | 1 | t
+ gtest20f_2 | gtest20f_nv | c | f | 1 | f
+(13 rows)
+
+INSERT INTO gtest20e VALUES (60); -- violates constraint
+ERROR: new row for relation "gtest20e_1" violates check constraint "gtest20e_chk"
+DETAIL: Failing row contains (60, 61).
+INSERT INTO gtest20f_1 VALUES (60); -- violates constraint
+ERROR: new row for relation "gtest20f_1" violates check constraint "gtest20f_chk"
+DETAIL: Failing row contains (60, 61).
+ALTER TABLE gtest20e ALTER COLUMN b DROP NOT NULL;
+ALTER TABLE gtest20e DROP CONSTRAINT gtest20e_chk;
+ALTER TABLE gtest20f DROP CONSTRAINT gtest20f_chk;
+SELECT conrelid::regclass, conname, contype, conislocal, coninhcount,
+ convalidated
+ FROM pg_constraint
+ WHERE conrelid IN ('gtest20e'::regclass, 'gtest20e_1'::regclass,
+ 'gtest20f'::regclass, 'gtest20f_1'::regclass,
+ 'gtest20f_2'::regclass)
+ ORDER BY conrelid::regclass::text, conname;
+ conrelid | conname | contype | conislocal | coninhcount | convalidated
+------------+-----------------------+---------+------------+-------------+--------------
+ gtest20f | gtest20f_b_not_null | n | t | 0 | t
+ gtest20f | gtest20f_nv | c | t | 0 | f
+ gtest20f_1 | gtest20f_b_not_null | n | f | 1 | t
+ gtest20f_1 | gtest20f_nv | c | f | 1 | f
+ gtest20f_2 | gtest20f_2_b_not_null | n | t | 1 | t
+ gtest20f_2 | gtest20f_chk | c | t | 0 | t
+ gtest20f_2 | gtest20f_nv | c | f | 1 | f
+(7 rows)
+
+DROP TABLE gtest20e, gtest20f, gtest20f_1, gtest20f_2;
-- not-null constraints
CREATE TABLE gtest21a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED NOT NULL);
INSERT INTO gtest21a (a) VALUES (1); -- ok
diff --git a/src/test/regress/expected/generated_virtual.out b/src/test/regress/expected/generated_virtual.out
index b14e10fb5a0..1b80f852c74 100644
--- a/src/test/regress/expected/generated_virtual.out
+++ b/src/test/regress/expected/generated_virtual.out
@@ -703,6 +703,83 @@ CREATE INDEX gtest20d_idx1 ON gtest20d (a) WHERE gtest20d = ROW (1, 2);
ALTER TABLE gtest20d ALTER COLUMN b SET EXPRESSION AS (a * 2::bigint); -- index rebuild
CREATE INDEX gtest20d_idx2 ON gtest20d ((gtest20d = ROW (1, 2)));
ALTER TABLE gtest20d ALTER COLUMN b SET EXPRESSION AS (a * 3); -- index rebuild
+-- SET EXPRESSION on a partition or inheritance child, or on ONLY the parent,
+-- keeps the constraints as they are and rechecks them
+CREATE TABLE gtest20e (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL NOT NULL,
+ CONSTRAINT gtest20e_chk CHECK (b < 50)) PARTITION BY RANGE (a);
+CREATE TABLE gtest20e_1 PARTITION OF gtest20e FOR VALUES FROM (0) TO (100);
+CREATE TABLE gtest20f (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL NOT NULL,
+ CONSTRAINT gtest20f_chk CHECK (b < 50));
+CREATE TABLE gtest20f_1 () INHERITS (gtest20f);
+CREATE TABLE gtest20f_2 (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL NOT NULL,
+ CONSTRAINT gtest20f_chk CHECK (b < 50));
+ALTER TABLE gtest20f_2 INHERIT gtest20f;
+INSERT INTO gtest20e VALUES (20);
+INSERT INTO gtest20f_1 VALUES (20);
+INSERT INTO gtest20f_2 VALUES (20);
+ALTER TABLE gtest20f ADD CONSTRAINT gtest20f_nv CHECK (b < 0) NOT VALID;
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (a * 3); -- violates constraint
+ERROR: check constraint "gtest20e_chk" of relation "gtest20e_1" is violated by some row
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (nullif(a, 20)); -- violates constraint
+ERROR: column "b" of relation "gtest20e_1" contains null values
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (a + 1);
+ALTER TABLE ONLY gtest20f_1 ALTER COLUMN b SET EXPRESSION AS (a * 3); -- violates constraint
+ERROR: check constraint "gtest20f_chk" of relation "gtest20f_1" is violated by some row
+ALTER TABLE ONLY gtest20f_1 ALTER COLUMN b SET EXPRESSION AS (a + 1);
+ALTER TABLE gtest20f_2 ALTER COLUMN b SET EXPRESSION AS (a + 2);
+ALTER TABLE ONLY gtest20f ALTER COLUMN b SET EXPRESSION AS (a + 3);
+SELECT conrelid::regclass, conname, contype, conislocal, coninhcount,
+ convalidated
+ FROM pg_constraint
+ WHERE conrelid IN ('gtest20e'::regclass, 'gtest20e_1'::regclass,
+ 'gtest20f'::regclass, 'gtest20f_1'::regclass,
+ 'gtest20f_2'::regclass)
+ ORDER BY conrelid::regclass::text, conname;
+ conrelid | conname | contype | conislocal | coninhcount | convalidated
+------------+-----------------------+---------+------------+-------------+--------------
+ gtest20e | gtest20e_b_not_null | n | t | 0 | t
+ gtest20e | gtest20e_chk | c | t | 0 | t
+ gtest20e_1 | gtest20e_b_not_null | n | f | 1 | t
+ gtest20e_1 | gtest20e_chk | c | f | 1 | t
+ gtest20f | gtest20f_b_not_null | n | t | 0 | t
+ gtest20f | gtest20f_chk | c | t | 0 | t
+ gtest20f | gtest20f_nv | c | t | 0 | f
+ gtest20f_1 | gtest20f_b_not_null | n | f | 1 | t
+ gtest20f_1 | gtest20f_chk | c | f | 1 | t
+ gtest20f_1 | gtest20f_nv | c | f | 1 | f
+ gtest20f_2 | gtest20f_2_b_not_null | n | t | 1 | t
+ gtest20f_2 | gtest20f_chk | c | t | 1 | t
+ gtest20f_2 | gtest20f_nv | c | f | 1 | f
+(13 rows)
+
+INSERT INTO gtest20e VALUES (60); -- violates constraint
+ERROR: new row for relation "gtest20e_1" violates check constraint "gtest20e_chk"
+DETAIL: Failing row contains (60, virtual).
+INSERT INTO gtest20f_1 VALUES (60); -- violates constraint
+ERROR: new row for relation "gtest20f_1" violates check constraint "gtest20f_chk"
+DETAIL: Failing row contains (60, virtual).
+ALTER TABLE gtest20e ALTER COLUMN b DROP NOT NULL;
+ALTER TABLE gtest20e DROP CONSTRAINT gtest20e_chk;
+ALTER TABLE gtest20f DROP CONSTRAINT gtest20f_chk;
+SELECT conrelid::regclass, conname, contype, conislocal, coninhcount,
+ convalidated
+ FROM pg_constraint
+ WHERE conrelid IN ('gtest20e'::regclass, 'gtest20e_1'::regclass,
+ 'gtest20f'::regclass, 'gtest20f_1'::regclass,
+ 'gtest20f_2'::regclass)
+ ORDER BY conrelid::regclass::text, conname;
+ conrelid | conname | contype | conislocal | coninhcount | convalidated
+------------+-----------------------+---------+------------+-------------+--------------
+ gtest20f | gtest20f_b_not_null | n | t | 0 | t
+ gtest20f | gtest20f_nv | c | t | 0 | f
+ gtest20f_1 | gtest20f_b_not_null | n | f | 1 | t
+ gtest20f_1 | gtest20f_nv | c | f | 1 | f
+ gtest20f_2 | gtest20f_2_b_not_null | n | t | 1 | t
+ gtest20f_2 | gtest20f_chk | c | t | 0 | t
+ gtest20f_2 | gtest20f_nv | c | f | 1 | f
+(7 rows)
+
+DROP TABLE gtest20e, gtest20f, gtest20f_1, gtest20f_2;
-- not-null constraints
CREATE TABLE gtest21a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) VIRTUAL NOT NULL);
INSERT INTO gtest21a (a) VALUES (1); -- ok
diff --git a/src/test/regress/sql/generated_stored.sql b/src/test/regress/sql/generated_stored.sql
index ef639ec6a18..fd5d9354c0b 100644
--- a/src/test/regress/sql/generated_stored.sql
+++ b/src/test/regress/sql/generated_stored.sql
@@ -356,6 +356,49 @@ ALTER TABLE gtest20d ALTER COLUMN b SET EXPRESSION AS (a * 2::bigint); -- index
CREATE INDEX gtest20d_idx2 ON gtest20d ((gtest20d = ROW (1, 2)));
ALTER TABLE gtest20d ALTER COLUMN b SET EXPRESSION AS (a * 3); -- index rebuild
+-- SET EXPRESSION on a partition or inheritance child, or on ONLY the parent,
+-- keeps the constraints as they are and rechecks them
+CREATE TABLE gtest20e (a int, b int GENERATED ALWAYS AS (a * 2) STORED NOT NULL,
+ CONSTRAINT gtest20e_chk CHECK (b < 50)) PARTITION BY RANGE (a);
+CREATE TABLE gtest20e_1 PARTITION OF gtest20e FOR VALUES FROM (0) TO (100);
+CREATE TABLE gtest20f (a int, b int GENERATED ALWAYS AS (a * 2) STORED NOT NULL,
+ CONSTRAINT gtest20f_chk CHECK (b < 50));
+CREATE TABLE gtest20f_1 () INHERITS (gtest20f);
+CREATE TABLE gtest20f_2 (a int, b int GENERATED ALWAYS AS (a * 2) STORED NOT NULL,
+ CONSTRAINT gtest20f_chk CHECK (b < 50));
+ALTER TABLE gtest20f_2 INHERIT gtest20f;
+INSERT INTO gtest20e VALUES (20);
+INSERT INTO gtest20f_1 VALUES (20);
+INSERT INTO gtest20f_2 VALUES (20);
+ALTER TABLE gtest20f ADD CONSTRAINT gtest20f_nv CHECK (b < 0) NOT VALID;
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (a * 3); -- violates constraint
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (nullif(a, 20)); -- violates constraint
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (a + 1);
+ALTER TABLE ONLY gtest20f_1 ALTER COLUMN b SET EXPRESSION AS (a * 3); -- violates constraint
+ALTER TABLE ONLY gtest20f_1 ALTER COLUMN b SET EXPRESSION AS (a + 1);
+ALTER TABLE gtest20f_2 ALTER COLUMN b SET EXPRESSION AS (a + 2);
+ALTER TABLE ONLY gtest20f ALTER COLUMN b SET EXPRESSION AS (a + 3);
+SELECT conrelid::regclass, conname, contype, conislocal, coninhcount,
+ convalidated
+ FROM pg_constraint
+ WHERE conrelid IN ('gtest20e'::regclass, 'gtest20e_1'::regclass,
+ 'gtest20f'::regclass, 'gtest20f_1'::regclass,
+ 'gtest20f_2'::regclass)
+ ORDER BY conrelid::regclass::text, conname;
+INSERT INTO gtest20e VALUES (60); -- violates constraint
+INSERT INTO gtest20f_1 VALUES (60); -- violates constraint
+ALTER TABLE gtest20e ALTER COLUMN b DROP NOT NULL;
+ALTER TABLE gtest20e DROP CONSTRAINT gtest20e_chk;
+ALTER TABLE gtest20f DROP CONSTRAINT gtest20f_chk;
+SELECT conrelid::regclass, conname, contype, conislocal, coninhcount,
+ convalidated
+ FROM pg_constraint
+ WHERE conrelid IN ('gtest20e'::regclass, 'gtest20e_1'::regclass,
+ 'gtest20f'::regclass, 'gtest20f_1'::regclass,
+ 'gtest20f_2'::regclass)
+ ORDER BY conrelid::regclass::text, conname;
+DROP TABLE gtest20e, gtest20f, gtest20f_1, gtest20f_2;
+
-- not-null constraints
CREATE TABLE gtest21a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED NOT NULL);
INSERT INTO gtest21a (a) VALUES (1); -- ok
diff --git a/src/test/regress/sql/generated_virtual.sql b/src/test/regress/sql/generated_virtual.sql
index e62880f941a..9e900e33a75 100644
--- a/src/test/regress/sql/generated_virtual.sql
+++ b/src/test/regress/sql/generated_virtual.sql
@@ -362,6 +362,49 @@ ALTER TABLE gtest20d ALTER COLUMN b SET EXPRESSION AS (a * 2::bigint); -- index
CREATE INDEX gtest20d_idx2 ON gtest20d ((gtest20d = ROW (1, 2)));
ALTER TABLE gtest20d ALTER COLUMN b SET EXPRESSION AS (a * 3); -- index rebuild
+-- SET EXPRESSION on a partition or inheritance child, or on ONLY the parent,
+-- keeps the constraints as they are and rechecks them
+CREATE TABLE gtest20e (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL NOT NULL,
+ CONSTRAINT gtest20e_chk CHECK (b < 50)) PARTITION BY RANGE (a);
+CREATE TABLE gtest20e_1 PARTITION OF gtest20e FOR VALUES FROM (0) TO (100);
+CREATE TABLE gtest20f (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL NOT NULL,
+ CONSTRAINT gtest20f_chk CHECK (b < 50));
+CREATE TABLE gtest20f_1 () INHERITS (gtest20f);
+CREATE TABLE gtest20f_2 (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL NOT NULL,
+ CONSTRAINT gtest20f_chk CHECK (b < 50));
+ALTER TABLE gtest20f_2 INHERIT gtest20f;
+INSERT INTO gtest20e VALUES (20);
+INSERT INTO gtest20f_1 VALUES (20);
+INSERT INTO gtest20f_2 VALUES (20);
+ALTER TABLE gtest20f ADD CONSTRAINT gtest20f_nv CHECK (b < 0) NOT VALID;
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (a * 3); -- violates constraint
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (nullif(a, 20)); -- violates constraint
+ALTER TABLE gtest20e_1 ALTER COLUMN b SET EXPRESSION AS (a + 1);
+ALTER TABLE ONLY gtest20f_1 ALTER COLUMN b SET EXPRESSION AS (a * 3); -- violates constraint
+ALTER TABLE ONLY gtest20f_1 ALTER COLUMN b SET EXPRESSION AS (a + 1);
+ALTER TABLE gtest20f_2 ALTER COLUMN b SET EXPRESSION AS (a + 2);
+ALTER TABLE ONLY gtest20f ALTER COLUMN b SET EXPRESSION AS (a + 3);
+SELECT conrelid::regclass, conname, contype, conislocal, coninhcount,
+ convalidated
+ FROM pg_constraint
+ WHERE conrelid IN ('gtest20e'::regclass, 'gtest20e_1'::regclass,
+ 'gtest20f'::regclass, 'gtest20f_1'::regclass,
+ 'gtest20f_2'::regclass)
+ ORDER BY conrelid::regclass::text, conname;
+INSERT INTO gtest20e VALUES (60); -- violates constraint
+INSERT INTO gtest20f_1 VALUES (60); -- violates constraint
+ALTER TABLE gtest20e ALTER COLUMN b DROP NOT NULL;
+ALTER TABLE gtest20e DROP CONSTRAINT gtest20e_chk;
+ALTER TABLE gtest20f DROP CONSTRAINT gtest20f_chk;
+SELECT conrelid::regclass, conname, contype, conislocal, coninhcount,
+ convalidated
+ FROM pg_constraint
+ WHERE conrelid IN ('gtest20e'::regclass, 'gtest20e_1'::regclass,
+ 'gtest20f'::regclass, 'gtest20f_1'::regclass,
+ 'gtest20f_2'::regclass)
+ ORDER BY conrelid::regclass::text, conname;
+DROP TABLE gtest20e, gtest20f, gtest20f_1, gtest20f_2;
+
-- not-null constraints
CREATE TABLE gtest21a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) VIRTUAL NOT NULL);
INSERT INTO gtest21a (a) VALUES (1); -- ok
base-commit: dc168a83874fbf5cee4afb76a623e1349a539724
--
2.53.0