Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c39c00

Browse files
committed
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 0da39aa, 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 0da39aa 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 <[email protected]> Author: jian he <[email protected]> Reviewed-by: Tom Lane <[email protected]> Discussion: https://postgr.es/m/CACJufxG59tip2+9h=rEv-ykOFjt0cbsPVchhi0RTij8bABBA0Q@mail.gmail.com Backpatch-through: 14
1 parent 1b1dc81 commit 3c39c00

File tree

4 files changed

+80
-27
lines changed

4 files changed

+80
-27
lines changed

src/backend/executor/nodeModifyTable.c

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,33 +205,53 @@ ExecCheckPlanOutput(Relation resultRel, List *targetList)
205205
attr = TupleDescAttr(resultDesc, attno);
206206
attno++;
207207

208-
if (!attr->attisdropped)
208+
/*
209+
* Special cases here should match planner's expand_insert_targetlist.
210+
*/
211+
if (attr->attisdropped)
209212
{
210-
/* Normal case: demand type match */
211-
if (exprType((Node *) tle->expr) != attr->atttypid)
213+
/*
214+
* For a dropped column, we can't check atttypid (it's likely 0).
215+
* In any case the planner has most likely inserted an INT4 null.
216+
* What we insist on is just *some* NULL constant.
217+
*/
218+
if (!IsA(tle->expr, Const) ||
219+
!((Const *) tle->expr)->constisnull)
212220
ereport(ERROR,
213221
(errcode(ERRCODE_DATATYPE_MISMATCH),
214222
errmsg("table row type and query-specified row type do not match"),
215-
errdetail("Table has type %s at ordinal position %d, but query expects %s.",
216-
format_type_be(attr->atttypid),
217-
attno,
218-
format_type_be(exprType((Node *) tle->expr)))));
223+
errdetail("Query provides a value for a dropped column at ordinal position %d.",
224+
attno)));
219225
}
220-
else
226+
else if (attr->attgenerated)
221227
{
222228
/*
223-
* For a dropped column, we can't check atttypid (it's likely 0).
224-
* In any case the planner has most likely inserted an INT4 null.
225-
* What we insist on is just *some* NULL constant.
229+
* For a generated column, the planner will have inserted a null
230+
* of the column's base type (to avoid possibly failing on domain
231+
* not-null constraints). It doesn't seem worth insisting on that
232+
* exact type though, since a null value is type-independent. As
233+
* above, just insist on *some* NULL constant.
226234
*/
227235
if (!IsA(tle->expr, Const) ||
228236
!((Const *) tle->expr)->constisnull)
229237
ereport(ERROR,
230238
(errcode(ERRCODE_DATATYPE_MISMATCH),
231239
errmsg("table row type and query-specified row type do not match"),
232-
errdetail("Query provides a value for a dropped column at ordinal position %d.",
240+
errdetail("Query provides a value for a generated column at ordinal position %d.",
233241
attno)));
234242
}
243+
else
244+
{
245+
/* Normal case: demand type match */
246+
if (exprType((Node *) tle->expr) != attr->atttypid)
247+
ereport(ERROR,
248+
(errcode(ERRCODE_DATATYPE_MISMATCH),
249+
errmsg("table row type and query-specified row type do not match"),
250+
errdetail("Table has type %s at ordinal position %d, but query expects %s.",
251+
format_type_be(attr->atttypid),
252+
attno,
253+
format_type_be(exprType((Node *) tle->expr)))));
254+
}
235255
}
236256
if (attno != resultDesc->natts)
237257
ereport(ERROR,

src/backend/optimizer/prep/preptlist.c

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "optimizer/tlist.h"
4545
#include "parser/parse_coerce.h"
4646
#include "parser/parsetree.h"
47+
#include "utils/lsyscache.h"
4748
#include "utils/rel.h"
4849

4950
static List *expand_insert_targetlist(PlannerInfo *root, List *tlist,
@@ -419,9 +420,8 @@ expand_insert_targetlist(PlannerInfo *root, List *tlist, Relation rel)
419420
*
420421
* INSERTs should insert NULL in this case. (We assume the
421422
* rewriter would have inserted any available non-NULL default
422-
* value.) Also, if the column isn't dropped, apply any domain
423-
* constraints that might exist --- this is to catch domain NOT
424-
* NULL.
423+
* value.) Also, normally we must apply any domain constraints
424+
* that might exist --- this is to catch domain NOT NULL.
425425
*
426426
* When generating a NULL constant for a dropped column, we label
427427
* it INT4 (any other guaranteed-to-exist datatype would do as
@@ -431,21 +431,17 @@ expand_insert_targetlist(PlannerInfo *root, List *tlist, Relation rel)
431431
* representation is datatype-independent. This could perhaps
432432
* confuse code comparing the finished plan to the target
433433
* relation, however.
434+
*
435+
* Another exception is that if the column is generated, the value
436+
* we produce here will be ignored, and we don't want to risk
437+
* throwing an error. So in that case we *don't* want to apply
438+
* domain constraints, so we must produce a NULL of the base type.
439+
* Again, code comparing the finished plan to the target relation
440+
* must account for this.
434441
*/
435442
Node *new_expr;
436443

437-
if (!att_tup->attisdropped)
438-
{
439-
new_expr = coerce_null_to_domain(att_tup->atttypid,
440-
att_tup->atttypmod,
441-
att_tup->attcollation,
442-
att_tup->attlen,
443-
att_tup->attbyval);
444-
/* Must run expression preprocessing on any non-const nodes */
445-
if (!IsA(new_expr, Const))
446-
new_expr = eval_const_expressions(root, new_expr);
447-
}
448-
else
444+
if (att_tup->attisdropped)
449445
{
450446
/* Insert NULL for dropped column */
451447
new_expr = (Node *) makeConst(INT4OID,
@@ -456,6 +452,33 @@ expand_insert_targetlist(PlannerInfo *root, List *tlist, Relation rel)
456452
true, /* isnull */
457453
true /* byval */ );
458454
}
455+
else if (att_tup->attgenerated)
456+
{
457+
/* Generated column, insert a NULL of the base type */
458+
Oid baseTypeId = att_tup->atttypid;
459+
int32 baseTypeMod = att_tup->atttypmod;
460+
461+
baseTypeId = getBaseTypeAndTypmod(baseTypeId, &baseTypeMod);
462+
new_expr = (Node *) makeConst(baseTypeId,
463+
baseTypeMod,
464+
att_tup->attcollation,
465+
att_tup->attlen,
466+
(Datum) 0,
467+
true, /* isnull */
468+
att_tup->attbyval);
469+
}
470+
else
471+
{
472+
/* Normal column, insert a NULL of the column datatype */
473+
new_expr = coerce_null_to_domain(att_tup->atttypid,
474+
att_tup->atttypmod,
475+
att_tup->attcollation,
476+
att_tup->attlen,
477+
att_tup->attbyval);
478+
/* Must run expression preprocessing on any non-const nodes */
479+
if (!IsA(new_expr, Const))
480+
new_expr = eval_const_expressions(root, new_expr);
481+
}
459482

460483
new_tle = makeTargetEntry((Expr *) new_expr,
461484
attrno,

src/test/regress/expected/generated.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,11 @@ CREATE TABLE gtest24 (a int PRIMARY KEY, b gtestdomain1 GENERATED ALWAYS AS (a *
759759
INSERT INTO gtest24 (a) VALUES (4); -- ok
760760
INSERT INTO gtest24 (a) VALUES (6); -- error
761761
ERROR: value for domain gtestdomain1 violates check constraint "gtestdomain1_check"
762+
CREATE DOMAIN gtestdomainnn AS int CHECK (VALUE IS NOT NULL);
763+
CREATE TABLE gtest24nn (a int, b gtestdomainnn GENERATED ALWAYS AS (a * 2) STORED);
764+
INSERT INTO gtest24nn (a) VALUES (4); -- ok
765+
INSERT INTO gtest24nn (a) VALUES (NULL); -- error
766+
ERROR: value for domain gtestdomainnn violates check constraint "gtestdomainnn_check"
762767
-- typed tables (currently not supported)
763768
CREATE TYPE gtest_type AS (f1 integer, f2 text, f3 bigint);
764769
CREATE TABLE gtest28 OF gtest_type (f1 WITH OPTIONS GENERATED ALWAYS AS (f2 *2) STORED);

src/test/regress/sql/generated.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ CREATE TABLE gtest24 (a int PRIMARY KEY, b gtestdomain1 GENERATED ALWAYS AS (a *
387387
INSERT INTO gtest24 (a) VALUES (4); -- ok
388388
INSERT INTO gtest24 (a) VALUES (6); -- error
389389

390+
CREATE DOMAIN gtestdomainnn AS int CHECK (VALUE IS NOT NULL);
391+
CREATE TABLE gtest24nn (a int, b gtestdomainnn GENERATED ALWAYS AS (a * 2) STORED);
392+
INSERT INTO gtest24nn (a) VALUES (4); -- ok
393+
INSERT INTO gtest24nn (a) VALUES (NULL); -- error
394+
390395
-- typed tables (currently not supported)
391396
CREATE TYPE gtest_type AS (f1 integer, f2 text, f3 bigint);
392397
CREATE TABLE gtest28 OF gtest_type (f1 WITH OPTIONS GENERATED ALWAYS AS (f2 *2) STORED);

0 commit comments

Comments
 (0)