Skip to content

Commit ae7b1dd

Browse files
committed
Fix assignment to array of domain over composite.
An update such as "UPDATE ... SET fld[n].subfld = whatever" failed if the array elements were domains rather than plain composites. That's because isAssignmentIndirectionExpr() failed to cope with the CoerceToDomain node that would appear in the expression tree in this case. The result would typically be a crash, and even if we accidentally didn't crash, we'd not correctly preserve other fields of the same array element. Per report from Onder Kalaci. Back-patch to v11 where arrays of domains came in. Discussion: https://postgr.es/m/PH0PR21MB132823A46AA36F0685B7A29AD8BD9@PH0PR21MB1328.namprd21.prod.outlook.com
1 parent 079ac01 commit ae7b1dd

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

src/backend/executor/execExpr.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,11 +2768,14 @@ ExecInitSubscriptingRef(ExprEvalStep *scratch, SubscriptingRef *sbsref,
27682768
* (We could use this in FieldStore too, but in that case passing the old
27692769
* value is so cheap there's no need.)
27702770
*
2771-
* Note: it might seem that this needs to recurse, but it does not; the
2772-
* CaseTestExpr, if any, will be directly the arg or refexpr of the top-level
2773-
* node. Nested-assignment situations give rise to expression trees in which
2774-
* each level of assignment has its own CaseTestExpr, and the recursive
2775-
* structure appears within the newvals or refassgnexpr field.
2771+
* Note: it might seem that this needs to recurse, but in most cases it does
2772+
* not; the CaseTestExpr, if any, will be directly the arg or refexpr of the
2773+
* top-level node. Nested-assignment situations give rise to expression
2774+
* trees in which each level of assignment has its own CaseTestExpr, and the
2775+
* recursive structure appears within the newvals or refassgnexpr field.
2776+
* There is an exception, though: if the array is an array-of-domain, we will
2777+
* have a CoerceToDomain as the refassgnexpr, and we need to be able to look
2778+
* through that.
27762779
*/
27772780
static bool
27782781
isAssignmentIndirectionExpr(Expr *expr)
@@ -2793,6 +2796,12 @@ isAssignmentIndirectionExpr(Expr *expr)
27932796
if (sbsRef->refexpr && IsA(sbsRef->refexpr, CaseTestExpr))
27942797
return true;
27952798
}
2799+
else if (IsA(expr, CoerceToDomain))
2800+
{
2801+
CoerceToDomain *cd = (CoerceToDomain *) expr;
2802+
2803+
return isAssignmentIndirectionExpr(cd->arg);
2804+
}
27962805
return false;
27972806
}
27982807

src/test/regress/expected/domain.out

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,30 @@ LINE 1: update dposintatable set (f1[2])[1] = array[98];
518518
drop table dposintatable;
519519
drop domain posint cascade;
520520
NOTICE: drop cascades to type dposinta
521+
-- Test arrays over domains of composite
522+
create type comptype as (cf1 int, cf2 int);
523+
create domain dcomptype as comptype check ((value).cf1 > 0);
524+
create table dcomptable (f1 dcomptype[]);
525+
insert into dcomptable values (null);
526+
update dcomptable set f1[1].cf2 = 5;
527+
table dcomptable;
528+
f1
529+
----------
530+
{"(,5)"}
531+
(1 row)
532+
533+
update dcomptable set f1[1].cf1 = -1; -- fail
534+
ERROR: value for domain dcomptype violates check constraint "dcomptype_check"
535+
update dcomptable set f1[1].cf1 = 1;
536+
table dcomptable;
537+
f1
538+
-----------
539+
{"(1,5)"}
540+
(1 row)
541+
542+
drop table dcomptable;
543+
drop type comptype cascade;
544+
NOTICE: drop cascades to type dcomptype
521545
-- Test not-null restrictions
522546
create domain dnotnull varchar(15) NOT NULL;
523547
create domain dnull varchar(15);

src/test/regress/sql/domain.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,23 @@ drop table dposintatable;
268268
drop domain posint cascade;
269269

270270

271+
-- Test arrays over domains of composite
272+
273+
create type comptype as (cf1 int, cf2 int);
274+
create domain dcomptype as comptype check ((value).cf1 > 0);
275+
276+
create table dcomptable (f1 dcomptype[]);
277+
insert into dcomptable values (null);
278+
update dcomptable set f1[1].cf2 = 5;
279+
table dcomptable;
280+
update dcomptable set f1[1].cf1 = -1; -- fail
281+
update dcomptable set f1[1].cf1 = 1;
282+
table dcomptable;
283+
284+
drop table dcomptable;
285+
drop type comptype cascade;
286+
287+
271288
-- Test not-null restrictions
272289

273290
create domain dnotnull varchar(15) NOT NULL;

0 commit comments

Comments
 (0)