aboutsummaryrefslogtreecommitdiff
path: root/src/backend/rewrite/rewriteHandler.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2017-07-11 16:48:59 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2017-07-11 16:48:59 -0400
commitb1cb32fb62c9951c9ba35cb774fb8beec9090cb7 (patch)
tree8ba609733c79f9e32d5d12458c16255a23641f1a /src/backend/rewrite/rewriteHandler.c
parent42171e2cd23c8307bbe0ec64e901f58e297db1c3 (diff)
downloadpostgresql-b1cb32fb62c9951c9ba35cb774fb8beec9090cb7.tar.gz
postgresql-b1cb32fb62c9951c9ba35cb774fb8beec9090cb7.zip
Fix multiple assignments to a column of a domain type.
We allow INSERT and UPDATE commands to assign to the same column more than once, as long as the assignments are to subfields or elements rather than the whole column. However, this failed when the target column was a domain over array rather than plain array. Fix by teaching process_matched_tle() to look through CoerceToDomain nodes, and add relevant test cases. Also add a group of test cases exercising domains over array of composite. It's doubtless accidental that CREATE DOMAIN allows this case while not allowing straight domain over composite; but it does, so we'd better make sure we don't break it. (I could not find any documentation mentioning either side of that, so no doc changes.) It's been like this for a long time, so back-patch to all supported branches. Discussion: https://postgr.es/m/4206.1499798337@sss.pgh.pa.us
Diffstat (limited to 'src/backend/rewrite/rewriteHandler.c')
-rw-r--r--src/backend/rewrite/rewriteHandler.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c
index f3c75261951..6b79c697955 100644
--- a/src/backend/rewrite/rewriteHandler.c
+++ b/src/backend/rewrite/rewriteHandler.c
@@ -934,6 +934,7 @@ process_matched_tle(TargetEntry *src_tle,
const char *attrName)
{
TargetEntry *result;
+ CoerceToDomain *coerce_expr = NULL;
Node *src_expr;
Node *prior_expr;
Node *src_input;
@@ -970,10 +971,30 @@ process_matched_tle(TargetEntry *src_tle,
* For FieldStore, instead of nesting we can generate a single
* FieldStore with multiple target fields. We must nest when
* ArrayRefs are involved though.
+ *
+ * As a further complication, the destination column might be a domain,
+ * resulting in each assignment containing a CoerceToDomain node over a
+ * FieldStore or ArrayRef. These should have matching target domains,
+ * so we strip them and reconstitute a single CoerceToDomain over the
+ * combined FieldStore/ArrayRef nodes. (Notice that this has the result
+ * that the domain's checks are applied only after we do all the field or
+ * element updates, not after each one. This is arguably desirable.)
*----------
*/
src_expr = (Node *) src_tle->expr;
prior_expr = (Node *) prior_tle->expr;
+
+ if (src_expr && IsA(src_expr, CoerceToDomain) &&
+ prior_expr && IsA(prior_expr, CoerceToDomain) &&
+ ((CoerceToDomain *) src_expr)->resulttype ==
+ ((CoerceToDomain *) prior_expr)->resulttype)
+ {
+ /* we assume without checking that resulttypmod/resultcollid match */
+ coerce_expr = (CoerceToDomain *) src_expr;
+ src_expr = (Node *) ((CoerceToDomain *) src_expr)->arg;
+ prior_expr = (Node *) ((CoerceToDomain *) prior_expr)->arg;
+ }
+
src_input = get_assignment_input(src_expr);
prior_input = get_assignment_input(prior_expr);
if (src_input == NULL ||
@@ -1042,6 +1063,16 @@ process_matched_tle(TargetEntry *src_tle,
newexpr = NULL;
}
+ if (coerce_expr)
+ {
+ /* put back the CoerceToDomain */
+ CoerceToDomain *newcoerce = makeNode(CoerceToDomain);
+
+ memcpy(newcoerce, coerce_expr, sizeof(CoerceToDomain));
+ newcoerce->arg = (Expr *) newexpr;
+ newexpr = (Node *) newcoerce;
+ }
+
result = flatCopyTargetEntry(src_tle);
result->expr = (Expr *) newexpr;
return result;