From b4a71cf65d70b98aaf890b13ec600e340ff69e3f Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 14 Mar 2024 14:57:16 -0400 Subject: Make INSERT-from-multiple-VALUES-rows handle domain target columns. Commit a3c7a993d fixed some cases involving target columns that are arrays or composites by applying transformAssignedExpr to the VALUES entries, and then stripping off any assignment ArrayRefs or FieldStores that the transformation added. But I forgot about domains over arrays or composites :-(. Such cases would either fail with surprising complaints about mismatched datatypes, or insert unexpected coercions that could lead to odd results. To fix, extend the stripping logic to get rid of CoerceToDomain if it's atop an ArrayRef or FieldStore. While poking at this, I realized that there's a poorly documented and not-at-all-tested behavior nearby: we coerce each VALUES column to the domain type separately, and rely on the rewriter to merge those operations so that the domain constraints are checked only once. If that merging did not happen, it's entirely possible that we'd get unexpected domain constraint failures due to checking a partially-updated container value. There's no bug there, but while we're here let's improve the commentary about it and add some test cases that explicitly exercise that behavior. Per bug #18393 from Pablo Kharo. Back-patch to all supported branches. Discussion: https://postgr.es/m/18393-65fedb1a0de9260d@postgresql.org --- src/backend/parser/analyze.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/backend/parser/analyze.c') diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 2255314c519..7f23d18b370 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -1070,17 +1070,28 @@ transformInsertRow(ParseState *pstate, List *exprlist, if (strip_indirection) { + /* + * We need to remove top-level FieldStores and SubscriptingRefs, + * as well as any CoerceToDomain appearing above one of those --- + * but not a CoerceToDomain that isn't above one of those. + */ while (expr) { - if (IsA(expr, FieldStore)) + Expr *subexpr = expr; + + while (IsA(subexpr, CoerceToDomain)) + { + subexpr = ((CoerceToDomain *) subexpr)->arg; + } + if (IsA(subexpr, FieldStore)) { - FieldStore *fstore = (FieldStore *) expr; + FieldStore *fstore = (FieldStore *) subexpr; expr = (Expr *) linitial(fstore->newvals); } - else if (IsA(expr, SubscriptingRef)) + else if (IsA(subexpr, SubscriptingRef)) { - SubscriptingRef *sbsref = (SubscriptingRef *) expr; + SubscriptingRef *sbsref = (SubscriptingRef *) subexpr; if (sbsref->refassgnexpr == NULL) break; -- cgit v1.2.3