pgsql: Fix failure for generated column with a not-null domain constrai

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix failure for generated column with a not-null domain constrai
Date: 2025-04-15 16:08:51
Message-ID: E1u4iqA-000CHz-2w@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix failure for generated column with a not-null domain constraint.

If a GENERATED column is declared to have a domain data type where
the domain's constraints disallow null values, INSERT commands failed
because we built a targetlist that included coercing a null constant
to the domain's type. The failure occurred even when the generated
value would have been perfectly OK. This is adjacent to the issues
fixed in 0da39aa76, but we didn't notice for lack of testing a domain
with such a constraint.

We aren't going to use the result of the targetlist entry for the
generated column --- ExecComputeStoredGenerated will overwrite it.
So it's not really necessary that it have the exact datatype of
the generated column. This patch fixes the problem by changing
the targetlist entry to be a null Const of the domain's base type,
which should be sufficiently legal. (We do have to tweak
ExecCheckPlanOutput to accept the situation, though.)

This has been broken since we implemented generated columns.
However, this patch only applies easily as far back as v14, partly
because I (tgl) only carried 0da39aa76 back that far, but mostly
because v14 significantly refactored the handling of INSERT/UPDATE
targetlists. Given the lack of field complaints and the short
remaining support lifetime of v13, I judge the cost-benefit ratio
not good for devising a version that would work in v13.

Reported-by: jian he <jian(dot)universality(at)gmail(dot)com>
Author: jian he <jian(dot)universality(at)gmail(dot)com>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/CACJufxG59tip2+9h=rEv-ykOFjt0cbsPVchhi0RTij8bABBA0Q@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/f04e0faa3702edcc3bebefcb0a21be53ee442e59

Modified Files
--------------
src/backend/executor/nodeModifyTable.c | 44 +++++++++++++++++++--------
src/backend/optimizer/prep/preptlist.c | 53 +++++++++++++++++++++++----------
src/test/regress/expected/generated.out | 5 ++++
src/test/regress/sql/generated.sql | 5 ++++
4 files changed, 80 insertions(+), 27 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Daniel Gustafsson 2025-04-15 19:36:04 pgsql: doc: Fix typos in documentation
Previous Message Fujii Masao 2025-04-15 14:17:19 pgsql: doc: Fix missing whitespace in pg_restore documentation.