Re: UPDATE SET (a,b,c) = (SELECT ...) versus rules - Mailing list pgsql-hackers

From Tom Lane
Subject Re: UPDATE SET (a,b,c) = (SELECT ...) versus rules
Date
Msg-id 19377.1403046552@sss.pgh.pa.us
Whole thread Raw
In response to Re: UPDATE SET (a,b,c) = (SELECT ...) versus rules  (Andres Freund <andres@2ndquadrant.com>)
List pgsql-hackers
Andres Freund <andres@2ndquadrant.com> writes:
> On 2014-06-14 19:27:03 -0500, Jim Nasby wrote:
>> I'm in favor of doing the substitution, just like we do today with
>> RULES, warts and all. We already warn people against using rules and
>> that they're very difficult to get correct, so I don't think double
>> eval of an expression should surprise anyone.

> It makes a formerly correct/safe rule unsafe. That's a showstopper from
> my POV.

Andres' objection has some merit, and it doesn't seem like very many
people are concerned about throwing errors for new constructs if it's hard
to make them work with rules.  So I've updated the patch to throw an error
for now.  We can always revisit it later if someone has a good idea about
how to implement it.

> There's *STILL* no proper warning against rules in the manual, btw.

Not sure what you want for a "proper warning", but I put in a <caution>
that I think mentions the known gotchas.

This patch is now complete as far as I'm concerned.  If there are not
objections I'll be committing it before long.

            regards, tom lane

diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index a3e8c59..7c2216a 100644
*** a/contrib/pg_stat_statements/pg_stat_statements.c
--- b/contrib/pg_stat_statements/pg_stat_statements.c
*************** JumbleExpr(pgssJumbleState *jstate, Node
*** 2396,2401 ****
--- 2396,2402 ----
                  SubLink    *sublink = (SubLink *) node;

                  APP_JUMB(sublink->subLinkType);
+                 APP_JUMB(sublink->subLinkId);
                  JumbleExpr(jstate, (Node *) sublink->testexpr);
                  JumbleQuery(jstate, (Query *) sublink->subselect);
              }
diff --git a/doc/src/sgml/ref/update.sgml b/doc/src/sgml/ref/update.sgml
index 7f565be..35b0699 100644
*** a/doc/src/sgml/ref/update.sgml
--- b/doc/src/sgml/ref/update.sgml
*************** PostgreSQL documentation
*** 24,30 ****
  [ WITH [ RECURSIVE ] <replaceable class="parameter">with_query</replaceable> [, ...] ]
  UPDATE [ ONLY ] <replaceable class="PARAMETER">table_name</replaceable> [ * ] [ [ AS ] <replaceable
class="parameter">alias</replaceable>] 
      SET { <replaceable class="PARAMETER">column_name</replaceable> = { <replaceable
class="PARAMETER">expression</replaceable>| DEFAULT } | 
!           ( <replaceable class="PARAMETER">column_name</replaceable> [, ...] ) = ( { <replaceable
class="PARAMETER">expression</replaceable>| DEFAULT } [, ...] ) } [, ...] 
      [ FROM <replaceable class="PARAMETER">from_list</replaceable> ]
      [ WHERE <replaceable class="PARAMETER">condition</replaceable> | WHERE CURRENT OF <replaceable
class="PARAMETER">cursor_name</replaceable>] 
      [ RETURNING * | <replaceable class="parameter">output_expression</replaceable> [ [ AS ] <replaceable
class="parameter">output_name</replaceable>] [, ...] ] 
--- 24,32 ----
  [ WITH [ RECURSIVE ] <replaceable class="parameter">with_query</replaceable> [, ...] ]
  UPDATE [ ONLY ] <replaceable class="PARAMETER">table_name</replaceable> [ * ] [ [ AS ] <replaceable
class="parameter">alias</replaceable>] 
      SET { <replaceable class="PARAMETER">column_name</replaceable> = { <replaceable
class="PARAMETER">expression</replaceable>| DEFAULT } | 
!           ( <replaceable class="PARAMETER">column_name</replaceable> [, ...] ) = ( { <replaceable
class="PARAMETER">expression</replaceable>| DEFAULT } [, ...] ) | 
!           ( <replaceable class="PARAMETER">column_name</replaceable> [, ...] ) = ( <replaceable
class="PARAMETER">sub-SELECT</replaceable>) 
!         } [, ...]
      [ FROM <replaceable class="PARAMETER">from_list</replaceable> ]
      [ WHERE <replaceable class="PARAMETER">condition</replaceable> | WHERE CURRENT OF <replaceable
class="PARAMETER">cursor_name</replaceable>] 
      [ RETURNING * | <replaceable class="parameter">output_expression</replaceable> [ [ AS ] <replaceable
class="parameter">output_name</replaceable>] [, ...] ] 
*************** UPDATE [ ONLY ] <replaceable class="PARA
*** 147,152 ****
--- 149,169 ----
     </varlistentry>

     <varlistentry>
+     <term><replaceable class="PARAMETER">sub-SELECT</replaceable></term>
+     <listitem>
+      <para>
+       A <literal>SELECT</> sub-query that produces as many output columns
+       as are listed in the parenthesized column list preceding it.  The
+       sub-query must yield no more than one row when executed.  If it
+       yields one row, its column values are assigned to the target columns;
+       if it yields no rows, NULL values are assigned to the target columns.
+       The sub-query can refer to old values of the current row of the table
+       being updated.
+      </para>
+     </listitem>
+    </varlistentry>
+
+    <varlistentry>
      <term><replaceable class="PARAMETER">from_list</replaceable></term>
      <listitem>
       <para>
*************** UPDATE employees SET sales_count = sales
*** 325,330 ****
--- 342,379 ----
    </para>

    <para>
+    Update contact names in an accounts table to match the currently assigned
+    salesmen:
+ <programlisting>
+ UPDATE accounts SET (contact_first_name, contact_last_name) =
+     (SELECT first_name, last_name FROM salesmen
+      WHERE salesmen.id = accounts.sales_id);
+ </programlisting>
+    A similar result could be accomplished with a join:
+ <programlisting>
+ UPDATE accounts SET contact_first_name = first_name,
+                     contact_last_name = last_name
+   FROM salesmen WHERE salesmen.id = accounts.sales_id;
+ </programlisting>
+    However, the second query may give unexpected results
+    if <structname>salesmen</>.<structfield>id</> is not a unique key, whereas
+    the first query is guaranteed to raise an error if there are multiple
+    <structfield>id</> matches.  Also, if there is no match for a particular
+    <structname>accounts</>.<structfield>sales_id</> entry, the first query
+    will set the corresponding name fields to NULL, whereas the second query
+    will not update that row at all.
+   </para>
+
+   <para>
+    Update statistics in a summary table to match the current data:
+ <programlisting>
+ UPDATE summary s SET (sum_x, sum_y, avg_x, avg_y) =
+     (SELECT sum(x), sum(y), avg(x), avg(y) FROM data d
+      WHERE d.group_id = s.group_id);
+ </programlisting>
+   </para>
+
+   <para>
     Attempt to insert a new stock item along with the quantity of stock. If
     the item already exists, instead update the stock count of the existing
     item. To do this without failing the entire transaction, use savepoints:
*************** UPDATE films SET kind = 'Dramatic' WHERE
*** 362,385 ****
    </para>

    <para>
-    According to the standard, the column-list syntax should allow a list
-    of columns to be assigned from a single row-valued expression, such
-    as a sub-select:
- <programlisting>
- UPDATE accounts SET (contact_last_name, contact_first_name) =
-     (SELECT last_name, first_name FROM salesmen
-      WHERE salesmen.id = accounts.sales_id);
- </programlisting>
-    This is not currently implemented — the source must be a list
-    of independent expressions.
-   </para>
-
-   <para>
     Some other database systems offer a <literal>FROM</> option in which
     the target table is supposed to be listed again within <literal>FROM</>.
     That is not how <productname>PostgreSQL</productname> interprets
     <literal>FROM</>.  Be careful when porting applications that use this
     extension.
    </para>
   </refsect1>
  </refentry>
--- 411,431 ----
    </para>

    <para>
     Some other database systems offer a <literal>FROM</> option in which
     the target table is supposed to be listed again within <literal>FROM</>.
     That is not how <productname>PostgreSQL</productname> interprets
     <literal>FROM</>.  Be careful when porting applications that use this
     extension.
    </para>
+
+   <para>
+    According to the standard, the source value for a parenthesized sub-list of
+    column names can be any row-valued expression yielding the correct number
+    of columns.  <productname>PostgreSQL</productname> only allows the source
+    value to be a parenthesized list of expressions (a row constructor) or a
+    sub-<literal>SELECT</>.  An individual column's updated value can be
+    specified as <literal>DEFAULT</> in the row-constructor case, but not
+    inside a sub-<literal>SELECT</>.
+   </para>
   </refsect1>
  </refentry>
diff --git a/doc/src/sgml/rules.sgml b/doc/src/sgml/rules.sgml
index 8e34fb0..66b3cc9 100644
*** a/doc/src/sgml/rules.sgml
--- b/doc/src/sgml/rules.sgml
*************** SELECT word FROM words ORDER BY word <->
*** 1164,1169 ****
--- 1164,1190 ----
      original one.
  </para>

+ <caution>
+  <para>
+   In many cases, tasks that could be performed by rules
+   on <command>INSERT</>/<command>UPDATE</>/<command>DELETE</> are better done
+   with triggers.  Triggers are notationally a bit more complicated, but their
+   semantics are much simpler to understand.  Rules tend to have surprising
+   results when the original query contains volatile functions: volatile
+   functions may get executed more times than expected in the process of
+   carrying out the rules.
+  </para>
+
+  <para>
+   Also, there are some cases that are not supported by these types of rules at
+   all, notably including <literal>WITH</> clauses in the original query and
+   multiple-assignment sub-<literal>SELECT</>s in the <literal>SET</> list
+   of <command>UPDATE</> queries.  This is because copying these constructs
+   into a rule query would result in multiple evaluations of the sub-query,
+   contrary to the express intent of the query's author.
+  </para>
+ </caution>
+
  <sect2>
  <title>How Update Rules Work</title>

diff --git a/src/backend/executor/nodeSubplan.c b/src/backend/executor/nodeSubplan.c
index 5d02d94..7aac184 100644
*** a/src/backend/executor/nodeSubplan.c
--- b/src/backend/executor/nodeSubplan.c
***************
*** 1,7 ****
  /*-------------------------------------------------------------------------
   *
   * nodeSubplan.c
!  *      routines to support subselects
   *
   * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
   * Portions Copyright (c) 1994, Regents of the University of California
--- 1,15 ----
  /*-------------------------------------------------------------------------
   *
   * nodeSubplan.c
!  *      routines to support sub-selects appearing in expressions
!  *
!  * This module is concerned with executing SubPlan expression nodes, which
!  * should not be confused with sub-SELECTs appearing in FROM.  SubPlans are
!  * divided into "initplans", which are those that need only one evaluation
!  * (among other restrictions, this requires that they don't use any direct
!  * correlation variables from the parent plan level), and regular "subplans",
!  * which are re-evaluated every time their result is required.
!  *
   *
   * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
   * Portions Copyright (c) 1994, Regents of the University of California
*************** static bool slotNoNulls(TupleTableSlot *
*** 54,59 ****
--- 62,69 ----

  /* ----------------------------------------------------------------
   *        ExecSubPlan
+  *
+  * This is the main entry point for execution of a regular SubPlan.
   * ----------------------------------------------------------------
   */
  static Datum
*************** ExecSubPlan(SubPlanState *node,
*** 72,78 ****
      /* Sanity checks */
      if (subplan->subLinkType == CTE_SUBLINK)
          elog(ERROR, "CTE subplans should not be executed via ExecSubPlan");
!     if (subplan->setParam != NIL)
          elog(ERROR, "cannot set parent params from subquery");

      /* Select appropriate evaluation strategy */
--- 82,88 ----
      /* Sanity checks */
      if (subplan->subLinkType == CTE_SUBLINK)
          elog(ERROR, "CTE subplans should not be executed via ExecSubPlan");
!     if (subplan->setParam != NIL && subplan->subLinkType != MULTIEXPR_SUBLINK)
          elog(ERROR, "cannot set parent params from subquery");

      /* Select appropriate evaluation strategy */
*************** ExecScanSubPlan(SubPlanState *node,
*** 224,229 ****
--- 234,265 ----
      ArrayBuildState *astate = NULL;

      /*
+      * MULTIEXPR subplans, when "executed", just return NULL; but first we
+      * mark the subplan's output parameters as needing recalculation.  (This
+      * is a bit of a hack: it relies on the subplan appearing later in its
+      * targetlist than any of the referencing Params, so that all the Params
+      * have been evaluated before we re-mark them for the next evaluation
+      * cycle.  But in general resjunk tlist items appear after non-resjunk
+      * ones, so this should be safe.)  Unlike ExecReScanSetParamPlan, we do
+      * *not* set bits in the parent plan node's chgParam, because we don't
+      * want to cause a rescan of the parent.
+      */
+     if (subLinkType == MULTIEXPR_SUBLINK)
+     {
+         EState       *estate = node->parent->state;
+
+         foreach(l, subplan->setParam)
+         {
+             int            paramid = lfirst_int(l);
+             ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
+
+             prm->execPlan = node;
+         }
+         *isNull = true;
+         return (Datum) 0;
+     }
+
+     /*
       * We are probably in a short-lived expression-evaluation context. Switch
       * to the per-query context for manipulating the child plan's chgParam,
       * calling ExecProcNode on it, etc.
*************** ExecInitSubPlan(SubPlan *subplan, PlanSt
*** 667,672 ****
--- 703,711 ----
      sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
                                                 subplan->plan_id - 1);

+     /* ... and to its parent's state */
+     sstate->parent = parent;
+
      /* Initialize subexpressions */
      sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent);
      sstate->args = (List *) ExecInitExpr((Expr *) subplan->args, parent);
*************** ExecInitSubPlan(SubPlan *subplan, PlanSt
*** 690,704 ****
      sstate->cur_eq_funcs = NULL;

      /*
!      * If this plan is un-correlated or undirect correlated one and want to
!      * set params for parent plan then mark parameters as needing evaluation.
       *
       * A CTE subplan's output parameter is never to be evaluated in the normal
       * way, so skip this in that case.
       *
!      * Note that in the case of un-correlated subqueries we don't care about
!      * setting parent->chgParam here: indices take care about it, for others -
!      * it doesn't matter...
       */
      if (subplan->setParam != NIL && subplan->subLinkType != CTE_SUBLINK)
      {
--- 729,744 ----
      sstate->cur_eq_funcs = NULL;

      /*
!      * If this is an initplan or MULTIEXPR subplan, it has output parameters
!      * that the parent plan will use, so mark those parameters as needing
!      * evaluation.  We don't actually run the subplan until we first need one
!      * of its outputs.
       *
       * A CTE subplan's output parameter is never to be evaluated in the normal
       * way, so skip this in that case.
       *
!      * Note that we don't set parent->chgParam here: the parent plan hasn't
!      * been run yet, so no need to force it to re-run.
       */
      if (subplan->setParam != NIL && subplan->subLinkType != CTE_SUBLINK)
      {
*************** ExecInitSubPlan(SubPlan *subplan, PlanSt
*** 890,896 ****
  /* ----------------------------------------------------------------
   *        ExecSetParamPlan
   *
!  *        Executes an InitPlan subplan and sets its output parameters.
   *
   * This is called from ExecEvalParamExec() when the value of a PARAM_EXEC
   * parameter is requested and the param's execPlan field is set (indicating
--- 930,936 ----
  /* ----------------------------------------------------------------
   *        ExecSetParamPlan
   *
!  *        Executes a subplan and sets its output parameters.
   *
   * This is called from ExecEvalParamExec() when the value of a PARAM_EXEC
   * parameter is requested and the param's execPlan field is set (indicating
*************** ExecSetParamPlan(SubPlanState *node, Exp
*** 908,913 ****
--- 948,954 ----
      SubLinkType subLinkType = subplan->subLinkType;
      MemoryContext oldcontext;
      TupleTableSlot *slot;
+     ListCell   *pvar;
      ListCell   *l;
      bool        found = false;
      ArrayBuildState *astate = NULL;
*************** ExecSetParamPlan(SubPlanState *node, Exp
*** 924,929 ****
--- 965,991 ----
      oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);

      /*
+      * Set Params of this plan from parent plan correlation values. (Any
+      * calculation we have to do is done in the parent econtext, since the
+      * Param values don't need to have per-query lifetime.)  Currently, we
+      * expect only MULTIEXPR_SUBLINK plans to have any correlation values.
+      */
+     Assert(subplan->parParam == NIL || subLinkType == MULTIEXPR_SUBLINK);
+     Assert(list_length(subplan->parParam) == list_length(node->args));
+
+     forboth(l, subplan->parParam, pvar, node->args)
+     {
+         int            paramid = lfirst_int(l);
+         ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
+
+         prm->value = ExecEvalExprSwitchContext((ExprState *) lfirst(pvar),
+                                                econtext,
+                                                &(prm->isnull),
+                                                NULL);
+         planstate->chgParam = bms_add_member(planstate->chgParam, paramid);
+     }
+
+     /*
       * Run the plan.  (If it needs to be rescanned, the first ExecProcNode
       * call will take care of that.)
       */
*************** ExecSetParamPlan(SubPlanState *node, Exp
*** 964,969 ****
--- 1026,1032 ----

          if (found &&
              (subLinkType == EXPR_SUBLINK ||
+              subLinkType == MULTIEXPR_SUBLINK ||
               subLinkType == ROWCOMPARE_SUBLINK))
              ereport(ERROR,
                      (errcode(ERRCODE_CARDINALITY_VIOLATION),
*************** ExecSetParamPlan(SubPlanState *node, Exp
*** 1035,1040 ****
--- 1098,1104 ----
          }
          else
          {
+             /* For other sublink types, set all the output params to NULL */
              foreach(l, subplan->setParam)
              {
                  int            paramid = lfirst_int(l);
diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
index 43530aa..8d3d5a7 100644
*** a/src/backend/nodes/copyfuncs.c
--- b/src/backend/nodes/copyfuncs.c
*************** _copySubLink(const SubLink *from)
*** 1327,1332 ****
--- 1327,1333 ----
      SubLink    *newnode = makeNode(SubLink);

      COPY_SCALAR_FIELD(subLinkType);
+     COPY_SCALAR_FIELD(subLinkId);
      COPY_NODE_FIELD(testexpr);
      COPY_NODE_FIELD(operName);
      COPY_NODE_FIELD(subselect);
*************** _copyResTarget(const ResTarget *from)
*** 2247,2252 ****
--- 2248,2265 ----
      return newnode;
  }

+ static MultiAssignRef *
+ _copyMultiAssignRef(const MultiAssignRef *from)
+ {
+     MultiAssignRef *newnode = makeNode(MultiAssignRef);
+
+     COPY_NODE_FIELD(source);
+     COPY_SCALAR_FIELD(colno);
+     COPY_SCALAR_FIELD(ncolumns);
+
+     return newnode;
+ }
+
  static TypeName *
  _copyTypeName(const TypeName *from)
  {
*************** copyObject(const void *from)
*** 4561,4566 ****
--- 4574,4582 ----
          case T_ResTarget:
              retval = _copyResTarget(from);
              break;
+         case T_MultiAssignRef:
+             retval = _copyMultiAssignRef(from);
+             break;
          case T_TypeCast:
              retval = _copyTypeCast(from);
              break;
diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c
index 2407cb7..e7b49f6 100644
*** a/src/backend/nodes/equalfuncs.c
--- b/src/backend/nodes/equalfuncs.c
*************** static bool
*** 382,387 ****
--- 382,388 ----
  _equalSubLink(const SubLink *a, const SubLink *b)
  {
      COMPARE_SCALAR_FIELD(subLinkType);
+     COMPARE_SCALAR_FIELD(subLinkId);
      COMPARE_NODE_FIELD(testexpr);
      COMPARE_NODE_FIELD(operName);
      COMPARE_NODE_FIELD(subselect);
*************** _equalResTarget(const ResTarget *a, cons
*** 2095,2100 ****
--- 2096,2111 ----
  }

  static bool
+ _equalMultiAssignRef(const MultiAssignRef *a, const MultiAssignRef *b)
+ {
+     COMPARE_NODE_FIELD(source);
+     COMPARE_SCALAR_FIELD(colno);
+     COMPARE_SCALAR_FIELD(ncolumns);
+
+     return true;
+ }
+
+ static bool
  _equalTypeName(const TypeName *a, const TypeName *b)
  {
      COMPARE_NODE_FIELD(names);
*************** equal(const void *a, const void *b)
*** 3029,3034 ****
--- 3040,3048 ----
          case T_ResTarget:
              retval = _equalResTarget(a, b);
              break;
+         case T_MultiAssignRef:
+             retval = _equalMultiAssignRef(a, b);
+             break;
          case T_TypeCast:
              retval = _equalTypeCast(a, b);
              break;
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index f32124b..5c09d2f 100644
*** a/src/backend/nodes/list.c
--- b/src/backend/nodes/list.c
*************** list_truncate(List *list, int new_size)
*** 385,391 ****
   * Locate the n'th cell (counting from 0) of the list.  It is an assertion
   * failure if there is no such cell.
   */
! static ListCell *
  list_nth_cell(const List *list, int n)
  {
      ListCell   *match;
--- 385,391 ----
   * Locate the n'th cell (counting from 0) of the list.  It is an assertion
   * failure if there is no such cell.
   */
! ListCell *
  list_nth_cell(const List *list, int n)
  {
      ListCell   *match;
diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c
index f4999c5..41e973b 100644
*** a/src/backend/nodes/nodeFuncs.c
--- b/src/backend/nodes/nodeFuncs.c
*************** exprType(const Node *expr)
*** 116,121 ****
--- 116,126 ----
                              format_type_be(exprType((Node *) tent->expr)))));
                      }
                  }
+                 else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
+                 {
+                     /* MULTIEXPR is always considered to return RECORD */
+                     type = RECORDOID;
+                 }
                  else
                  {
                      /* for all other sublink types, result is boolean */
*************** exprType(const Node *expr)
*** 142,147 ****
--- 147,157 ----
                                      format_type_be(subplan->firstColType))));
                      }
                  }
+                 else if (subplan->subLinkType == MULTIEXPR_SUBLINK)
+                 {
+                     /* MULTIEXPR is always considered to return RECORD */
+                     type = RECORDOID;
+                 }
                  else
                  {
                      /* for all other subplan types, result is boolean */
*************** exprTypmod(const Node *expr)
*** 299,304 ****
--- 309,315 ----
                      return exprTypmod((Node *) tent->expr);
                      /* note we don't need to care if it's an array */
                  }
+                 /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
              }
              break;
          case T_SubPlan:
*************** exprTypmod(const Node *expr)
*** 312,322 ****
                      /* note we don't need to care if it's an array */
                      return subplan->firstColTypmod;
                  }
!                 else
!                 {
!                     /* for all other subplan types, result is boolean */
!                     return -1;
!                 }
              }
              break;
          case T_AlternativeSubPlan:
--- 323,329 ----
                      /* note we don't need to care if it's an array */
                      return subplan->firstColTypmod;
                  }
!                 /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
              }
              break;
          case T_AlternativeSubPlan:
*************** exprCollation(const Node *expr)
*** 784,790 ****
                  }
                  else
                  {
!                     /* for all other sublink types, result is boolean */
                      coll = InvalidOid;
                  }
              }
--- 791,797 ----
                  }
                  else
                  {
!                     /* otherwise, result is RECORD or BOOLEAN */
                      coll = InvalidOid;
                  }
              }
*************** exprCollation(const Node *expr)
*** 802,808 ****
                  }
                  else
                  {
!                     /* for all other subplan types, result is boolean */
                      coll = InvalidOid;
                  }
              }
--- 809,815 ----
                  }
                  else
                  {
!                     /* otherwise, result is RECORD or BOOLEAN */
                      coll = InvalidOid;
                  }
              }
*************** exprSetCollation(Node *expr, Oid collati
*** 1017,1023 ****
                  }
                  else
                  {
!                     /* for all other sublink types, result is boolean */
                      Assert(!OidIsValid(collation));
                  }
              }
--- 1024,1030 ----
                  }
                  else
                  {
!                     /* otherwise, result is RECORD or BOOLEAN */
                      Assert(!OidIsValid(collation));
                  }
              }
*************** exprLocation(const Node *expr)
*** 1420,1425 ****
--- 1427,1435 ----
              /* we need not examine the contained expression (if any) */
              loc = ((const ResTarget *) expr)->location;
              break;
+         case T_MultiAssignRef:
+             loc = exprLocation(((const MultiAssignRef *) expr)->source);
+             break;
          case T_TypeCast:
              {
                  const TypeCast *tc = (const TypeCast *) expr;
*************** raw_expression_tree_walker(Node *node,
*** 3107,3112 ****
--- 3117,3124 ----
                      return true;
              }
              break;
+         case T_MultiAssignRef:
+             return walker(((MultiAssignRef *) node)->source, context);
          case T_TypeCast:
              {
                  TypeCast   *tc = (TypeCast *) node;
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index deff33f..c182212 100644
*** a/src/backend/nodes/outfuncs.c
--- b/src/backend/nodes/outfuncs.c
*************** _outSubLink(StringInfo str, const SubLin
*** 1115,1120 ****
--- 1115,1121 ----
      WRITE_NODE_TYPE("SUBLINK");

      WRITE_ENUM_FIELD(subLinkType, SubLinkType);
+     WRITE_INT_FIELD(subLinkId);
      WRITE_NODE_FIELD(testexpr);
      WRITE_NODE_FIELD(operName);
      WRITE_NODE_FIELD(subselect);
*************** _outPlannerInfo(StringInfo str, const Pl
*** 1701,1706 ****
--- 1702,1708 ----
      WRITE_INT_FIELD(join_cur_level);
      WRITE_NODE_FIELD(init_plans);
      WRITE_NODE_FIELD(cte_plan_ids);
+     WRITE_NODE_FIELD(multiexpr_params);
      WRITE_NODE_FIELD(eq_classes);
      WRITE_NODE_FIELD(canon_pathkeys);
      WRITE_NODE_FIELD(left_join_clauses);
*************** _outResTarget(StringInfo str, const ResT
*** 2581,2586 ****
--- 2583,2598 ----
  }

  static void
+ _outMultiAssignRef(StringInfo str, const MultiAssignRef *node)
+ {
+     WRITE_NODE_TYPE("MULTIASSIGNREF");
+
+     WRITE_NODE_FIELD(source);
+     WRITE_INT_FIELD(colno);
+     WRITE_INT_FIELD(ncolumns);
+ }
+
+ static void
  _outSortBy(StringInfo str, const SortBy *node)
  {
      WRITE_NODE_TYPE("SORTBY");
*************** _outNode(StringInfo str, const void *obj
*** 3191,3196 ****
--- 3203,3211 ----
              case T_ResTarget:
                  _outResTarget(str, obj);
                  break;
+             case T_MultiAssignRef:
+                 _outMultiAssignRef(str, obj);
+                 break;
              case T_SortBy:
                  _outSortBy(str, obj);
                  break;
diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c
index 1ec4f3c..69d9989 100644
*** a/src/backend/nodes/readfuncs.c
--- b/src/backend/nodes/readfuncs.c
*************** _readSubLink(void)
*** 744,749 ****
--- 744,750 ----
      READ_LOCALS(SubLink);

      READ_ENUM_FIELD(subLinkType, SubLinkType);
+     READ_INT_FIELD(subLinkId);
      READ_NODE_FIELD(testexpr);
      READ_NODE_FIELD(operName);
      READ_NODE_FIELD(subselect);
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 0f1e2e4..f2c9c99 100644
*** a/src/backend/optimizer/plan/planner.c
--- b/src/backend/optimizer/plan/planner.c
*************** subquery_planner(PlannerGlobal *glob, Qu
*** 310,315 ****
--- 310,316 ----
      root->planner_cxt = CurrentMemoryContext;
      root->init_plans = NIL;
      root->cte_plan_ids = NIL;
+     root->multiexpr_params = NIL;
      root->eq_classes = NIL;
      root->append_rel_list = NIL;
      root->rowMarks = NIL;
diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c
index 768c5c7..4d717df 100644
*** a/src/backend/optimizer/plan/setrefs.c
--- b/src/backend/optimizer/plan/setrefs.c
*************** static bool extract_query_dependencies_w
*** 157,166 ****
   * 3. We adjust Vars in upper plan nodes to refer to the outputs of their
   * subplans.
   *
!  * 4. We compute regproc OIDs for operators (ie, we look up the function
   * that implements each op).
   *
!  * 5. We create lists of specific objects that the plan depends on.
   * This will be used by plancache.c to drive invalidation of cached plans.
   * Relation dependencies are represented by OIDs, and everything else by
   * PlanInvalItems (this distinction is motivated by the shared-inval APIs).
--- 157,169 ----
   * 3. We adjust Vars in upper plan nodes to refer to the outputs of their
   * subplans.
   *
!  * 4. PARAM_MULTIEXPR Params are replaced by regular PARAM_EXEC Params,
!  * now that we have finished planning all MULTIEXPR subplans.
!  *
!  * 5. We compute regproc OIDs for operators (ie, we look up the function
   * that implements each op).
   *
!  * 6. We create lists of specific objects that the plan depends on.
   * This will be used by plancache.c to drive invalidation of cached plans.
   * Relation dependencies are represented by OIDs, and everything else by
   * PlanInvalItems (this distinction is motivated by the shared-inval APIs).
*************** fix_expr_common(PlannerInfo *root, Node
*** 1119,1128 ****
--- 1122,1160 ----
  }

  /*
+  * fix_param_node
+  *        Do set_plan_references processing on a Param
+  *
+  * If it's a PARAM_MULTIEXPR, replace it with the appropriate Param from
+  * root->multiexpr_params; otherwise no change is needed.
+  * Just for paranoia's sake, we make a copy of the node in either case.
+  */
+ static Node *
+ fix_param_node(PlannerInfo *root, Param *p)
+ {
+     if (p->paramkind == PARAM_MULTIEXPR)
+     {
+         int            subqueryid = p->paramid >> 16;
+         int            colno = p->paramid & 0xFFFF;
+         List       *params;
+
+         if (subqueryid <= 0 ||
+             subqueryid > list_length(root->multiexpr_params))
+             elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
+         params = (List *) list_nth(root->multiexpr_params, subqueryid - 1);
+         if (colno <= 0 || colno > list_length(params))
+             elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
+         return copyObject(list_nth(params, colno - 1));
+     }
+     return copyObject(p);
+ }
+
+ /*
   * fix_scan_expr
   *        Do set_plan_references processing on a scan-level expression
   *
   * This consists of incrementing all Vars' varnos by rtoffset,
+  * replacing PARAM_MULTIEXPR Params, expanding PlaceHolderVars,
   * looking up operator opcode info for OpExpr and related nodes,
   * and adding OIDs from regclass Const nodes into root->glob->relationOids.
   */
*************** fix_scan_expr(PlannerInfo *root, Node *n
*** 1134,1140 ****
      context.root = root;
      context.rtoffset = rtoffset;

!     if (rtoffset != 0 || root->glob->lastPHId != 0)
      {
          return fix_scan_expr_mutator(node, &context);
      }
--- 1166,1174 ----
      context.root = root;
      context.rtoffset = rtoffset;

!     if (rtoffset != 0 ||
!         root->multiexpr_params != NIL ||
!         root->glob->lastPHId != 0)
      {
          return fix_scan_expr_mutator(node, &context);
      }
*************** fix_scan_expr(PlannerInfo *root, Node *n
*** 1142,1152 ****
      {
          /*
           * If rtoffset == 0, we don't need to change any Vars, and if there
!          * are no placeholders anywhere we won't need to remove them.  Then
!          * it's OK to just scribble on the input node tree instead of copying
!          * (since the only change, filling in any unset opfuncid fields, is
!          * harmless).  This saves just enough cycles to be noticeable on
!          * trivial queries.
           */
          (void) fix_scan_expr_walker(node, &context);
          return node;
--- 1176,1187 ----
      {
          /*
           * If rtoffset == 0, we don't need to change any Vars, and if there
!          * are no MULTIEXPR subqueries then we don't need to replace
!          * PARAM_MULTIEXPR Params, and if there are no placeholders anywhere
!          * we won't need to remove them.  Then it's OK to just scribble on the
!          * input node tree instead of copying (since the only change, filling
!          * in any unset opfuncid fields, is harmless).  This saves just enough
!          * cycles to be noticeable on trivial queries.
           */
          (void) fix_scan_expr_walker(node, &context);
          return node;
*************** fix_scan_expr_mutator(Node *node, fix_sc
*** 1176,1181 ****
--- 1211,1218 ----
              var->varnoold += context->rtoffset;
          return (Node *) var;
      }
+     if (IsA(node, Param))
+         return fix_param_node(context->root, (Param *) node);
      if (IsA(node, CurrentOfExpr))
      {
          CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
*************** fix_join_expr_mutator(Node *node, fix_jo
*** 1745,1750 ****
--- 1782,1789 ----
          /* If not supplied by input plans, evaluate the contained expr */
          return fix_join_expr_mutator((Node *) phv->phexpr, context);
      }
+     if (IsA(node, Param))
+         return fix_param_node(context->root, (Param *) node);
      /* Try matching more complex expressions too, if tlists have any */
      if (context->outer_itlist->has_non_vars)
      {
*************** fix_upper_expr_mutator(Node *node, fix_u
*** 1847,1852 ****
--- 1886,1893 ----
          /* If not supplied by input plan, evaluate the contained expr */
          return fix_upper_expr_mutator((Node *) phv->phexpr, context);
      }
+     if (IsA(node, Param))
+         return fix_param_node(context->root, (Param *) node);
      /* Try matching more complex expressions too, if tlist has any */
      if (context->subplan_itlist->has_non_vars)
      {
diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c
index be92049..3e7dc85 100644
*** a/src/backend/optimizer/plan/subselect.c
--- b/src/backend/optimizer/plan/subselect.c
*************** typedef struct finalize_primnode_context
*** 55,62 ****

  static Node *build_subplan(PlannerInfo *root, Plan *plan, PlannerInfo *subroot,
                List *plan_params,
!               SubLinkType subLinkType, Node *testexpr,
!               bool adjust_testexpr, bool unknownEqFalse);
  static List *generate_subquery_params(PlannerInfo *root, List *tlist,
                           List **paramIds);
  static List *generate_subquery_vars(PlannerInfo *root, List *tlist,
--- 55,63 ----

  static Node *build_subplan(PlannerInfo *root, Plan *plan, PlannerInfo *subroot,
                List *plan_params,
!               SubLinkType subLinkType, int subLinkId,
!               Node *testexpr, bool adjust_testexpr,
!               bool unknownEqFalse);
  static List *generate_subquery_params(PlannerInfo *root, List *tlist,
                           List **paramIds);
  static List *generate_subquery_vars(PlannerInfo *root, List *tlist,
*************** get_first_col_type(Plan *plan, Oid *colt
*** 407,413 ****
  /*
   * Convert a SubLink (as created by the parser) into a SubPlan.
   *
!  * We are given the SubLink's contained query, type, and testexpr.  We are
   * also told if this expression appears at top level of a WHERE/HAVING qual.
   *
   * Note: we assume that the testexpr has been AND/OR flattened (actually,
--- 408,414 ----
  /*
   * Convert a SubLink (as created by the parser) into a SubPlan.
   *
!  * We are given the SubLink's contained query, type, ID, and testexpr.  We are
   * also told if this expression appears at top level of a WHERE/HAVING qual.
   *
   * Note: we assume that the testexpr has been AND/OR flattened (actually,
*************** get_first_col_type(Plan *plan, Oid *colt
*** 415,428 ****
   * implicit-AND form; and any SubLinks in it should already have been
   * converted to SubPlans.  The subquery is as yet untouched, however.
   *
!  * The result is whatever we need to substitute in place of the SubLink
!  * node in the executable expression.  This will be either the SubPlan
!  * node (if we have to do the subplan as a subplan), or a Param node
!  * representing the result of an InitPlan, or a row comparison expression
!  * tree containing InitPlan Param nodes.
   */
  static Node *
! make_subplan(PlannerInfo *root, Query *orig_subquery, SubLinkType subLinkType,
               Node *testexpr, bool isTopQual)
  {
      Query       *subquery;
--- 416,435 ----
   * implicit-AND form; and any SubLinks in it should already have been
   * converted to SubPlans.  The subquery is as yet untouched, however.
   *
!  * The result is whatever we need to substitute in place of the SubLink node
!  * in the executable expression.  If we're going to do the subplan as a
!  * regular subplan, this will be the constructed SubPlan node.  If we're going
!  * to do the subplan as an InitPlan, the SubPlan node instead goes into
!  * root->init_plans, and what we return here is an expression tree
!  * representing the InitPlan's result: usually just a Param node representing
!  * a single scalar result, but possibly a row comparison tree containing
!  * multiple Param nodes, or for a MULTIEXPR subquery a simple NULL constant
!  * (since the real output Params are elsewhere in the tree, and the MULTIEXPR
!  * subquery itself is in a resjunk tlist entry whose value is uninteresting).
   */
  static Node *
! make_subplan(PlannerInfo *root, Query *orig_subquery,
!              SubLinkType subLinkType, int subLinkId,
               Node *testexpr, bool isTopQual)
  {
      Query       *subquery;
*************** make_subplan(PlannerInfo *root, Query *o
*** 452,459 ****
       * first tuple will be retrieved.  For ALL and ANY subplans, we will be
       * able to stop evaluating if the test condition fails or matches, so very
       * often not all the tuples will be retrieved; for lack of a better idea,
!      * specify 50% retrieval.  For EXPR and ROWCOMPARE subplans, use default
!      * behavior (we're only expecting one row out, anyway).
       *
       * NOTE: if you change these numbers, also change cost_subplan() in
       * path/costsize.c.
--- 459,466 ----
       * first tuple will be retrieved.  For ALL and ANY subplans, we will be
       * able to stop evaluating if the test condition fails or matches, so very
       * often not all the tuples will be retrieved; for lack of a better idea,
!      * specify 50% retrieval.  For EXPR, MULTIEXPR, and ROWCOMPARE subplans,
!      * use default behavior (we're only expecting one row out, anyway).
       *
       * NOTE: if you change these numbers, also change cost_subplan() in
       * path/costsize.c.
*************** make_subplan(PlannerInfo *root, Query *o
*** 491,497 ****

      /* And convert to SubPlan or InitPlan format. */
      result = build_subplan(root, plan, subroot, plan_params,
!                            subLinkType, testexpr, true, isTopQual);

      /*
       * If it's a correlated EXISTS with an unimportant targetlist, we might be
--- 498,505 ----

      /* And convert to SubPlan or InitPlan format. */
      result = build_subplan(root, plan, subroot, plan_params,
!                            subLinkType, subLinkId,
!                            testexpr, true, isTopQual);

      /*
       * If it's a correlated EXISTS with an unimportant targetlist, we might be
*************** make_subplan(PlannerInfo *root, Query *o
*** 536,542 ****
                  /* OK, convert to SubPlan format. */
                  hashplan = (SubPlan *) build_subplan(root, plan, subroot,
                                                       plan_params,
!                                                      ANY_SUBLINK, newtestexpr,
                                                       false, true);
                  /* Check we got what we expected */
                  Assert(IsA(hashplan, SubPlan));
--- 544,551 ----
                  /* OK, convert to SubPlan format. */
                  hashplan = (SubPlan *) build_subplan(root, plan, subroot,
                                                       plan_params,
!                                                      ANY_SUBLINK, 0,
!                                                      newtestexpr,
                                                       false, true);
                  /* Check we got what we expected */
                  Assert(IsA(hashplan, SubPlan));
*************** make_subplan(PlannerInfo *root, Query *o
*** 559,572 ****
  /*
   * Build a SubPlan node given the raw inputs --- subroutine for make_subplan
   *
!  * Returns either the SubPlan, or an expression using initplan output Params,
!  * as explained in the comments for make_subplan.
   */
  static Node *
  build_subplan(PlannerInfo *root, Plan *plan, PlannerInfo *subroot,
                List *plan_params,
!               SubLinkType subLinkType, Node *testexpr,
!               bool adjust_testexpr, bool unknownEqFalse)
  {
      Node       *result;
      SubPlan    *splan;
--- 568,582 ----
  /*
   * Build a SubPlan node given the raw inputs --- subroutine for make_subplan
   *
!  * Returns either the SubPlan, or a replacement expression if we decide to
!  * make it an InitPlan, as explained in the comments for make_subplan.
   */
  static Node *
  build_subplan(PlannerInfo *root, Plan *plan, PlannerInfo *subroot,
                List *plan_params,
!               SubLinkType subLinkType, int subLinkId,
!               Node *testexpr, bool adjust_testexpr,
!               bool unknownEqFalse)
  {
      Node       *result;
      SubPlan    *splan;
*************** build_subplan(PlannerInfo *root, Plan *p
*** 615,626 ****
      }

      /*
!      * Un-correlated or undirect correlated plans of EXISTS, EXPR, ARRAY, or
!      * ROWCOMPARE types can be used as initPlans.  For EXISTS, EXPR, or ARRAY,
!      * we just produce a Param referring to the result of evaluating the
!      * initPlan.  For ROWCOMPARE, we must modify the testexpr tree to contain
!      * PARAM_EXEC Params instead of the PARAM_SUBLINK Params emitted by the
!      * parser.
       */
      if (splan->parParam == NIL && subLinkType == EXISTS_SUBLINK)
      {
--- 625,639 ----
      }

      /*
!      * Un-correlated or undirect correlated plans of EXISTS, EXPR, ARRAY,
!      * ROWCOMPARE, or MULTIEXPR types can be used as initPlans.  For EXISTS,
!      * EXPR, or ARRAY, we return a Param referring to the result of evaluating
!      * the initPlan.  For ROWCOMPARE, we must modify the testexpr tree to
!      * contain PARAM_EXEC Params instead of the PARAM_SUBLINK Params emitted
!      * by the parser, and then return that tree.  For MULTIEXPR, we return a
!      * null constant: the resjunk targetlist item containing the SubLink does
!      * not need to return anything useful, since the referencing Params are
!      * elsewhere.
       */
      if (splan->parParam == NIL && subLinkType == EXISTS_SUBLINK)
      {
*************** build_subplan(PlannerInfo *root, Plan *p
*** 687,692 ****
--- 700,741 ----
           * plan's expression tree; it is not kept in the initplan node.
           */
      }
+     else if (subLinkType == MULTIEXPR_SUBLINK)
+     {
+         /*
+          * Whether it's an initplan or not, it needs to set a PARAM_EXEC Param
+          * for each output column.
+          */
+         List       *params;
+
+         Assert(testexpr == NULL);
+         params = generate_subquery_params(root,
+                                           plan->targetlist,
+                                           &splan->setParam);
+
+         /*
+          * Save the list of replacement Params in the n'th cell of
+          * root->multiexpr_params; setrefs.c will use it to replace
+          * PARAM_MULTIEXPR Params.
+          */
+         while (list_length(root->multiexpr_params) < subLinkId)
+             root->multiexpr_params = lappend(root->multiexpr_params, NIL);
+         lc = list_nth_cell(root->multiexpr_params, subLinkId - 1);
+         Assert(lfirst(lc) == NIL);
+         lfirst(lc) = params;
+
+         /* It can be an initplan if there are no parParams. */
+         if (splan->parParam == NIL)
+         {
+             isInitPlan = true;
+             result = (Node *) makeNullConst(RECORDOID, -1, InvalidOid);
+         }
+         else
+         {
+             isInitPlan = false;
+             result = (Node *) splan;
+         }
+     }
      else
      {
          /*
*************** build_subplan(PlannerInfo *root, Plan *p
*** 760,784 ****
                                                     splan->plan_id);

      /* Label the subplan for EXPLAIN purposes */
!     if (isInitPlan)
      {
!         ListCell   *lc;
!         int            offset;

!         splan->plan_name = palloc(32 + 12 * list_length(splan->setParam));
!         sprintf(splan->plan_name, "InitPlan %d (returns ", splan->plan_id);
!         offset = strlen(splan->plan_name);
          foreach(lc, splan->setParam)
          {
!             sprintf(splan->plan_name + offset, "$%d%s",
!                     lfirst_int(lc),
!                     lnext(lc) ? "," : "");
!             offset += strlen(splan->plan_name + offset);
          }
-         sprintf(splan->plan_name + offset, ")");
      }
-     else
-         splan->plan_name = psprintf("SubPlan %d", splan->plan_id);

      /* Lastly, fill in the cost estimates for use later */
      cost_subplan(root, splan, plan);
--- 809,830 ----
                                                     splan->plan_id);

      /* Label the subplan for EXPLAIN purposes */
!     splan->plan_name = palloc(32 + 12 * list_length(splan->setParam));
!     sprintf(splan->plan_name, "%s %d",
!             isInitPlan ? "InitPlan" : "SubPlan",
!             splan->plan_id);
!     if (splan->setParam)
      {
!         char       *ptr = splan->plan_name + strlen(splan->plan_name);

!         ptr += sprintf(ptr, " (returns ");
          foreach(lc, splan->setParam)
          {
!             ptr += sprintf(ptr, "$%d%s",
!                            lfirst_int(lc),
!                            lnext(lc) ? "," : ")");
          }
      }

      /* Lastly, fill in the cost estimates for use later */
      cost_subplan(root, splan, plan);
*************** process_sublinks_mutator(Node *node, pro
*** 1816,1821 ****
--- 1862,1868 ----
          return make_subplan(context->root,
                              (Query *) sublink->subselect,
                              sublink->subLinkType,
+                             sublink->subLinkId,
                              testexpr,
                              context->isTopQual);
      }
diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c
index 7952194..9cb1378 100644
*** a/src/backend/optimizer/prep/prepjointree.c
--- b/src/backend/optimizer/prep/prepjointree.c
*************** pull_up_simple_subquery(PlannerInfo *roo
*** 804,809 ****
--- 804,810 ----
      subroot->planner_cxt = CurrentMemoryContext;
      subroot->init_plans = NIL;
      subroot->cte_plan_ids = NIL;
+     subroot->multiexpr_params = NIL;
      subroot->eq_classes = NIL;
      subroot->append_rel_list = NIL;
      subroot->rowMarks = NIL;
diff --git a/src/backend/optimizer/util/tlist.c b/src/backend/optimizer/util/tlist.c
index f1f1be1..b5c6a44 100644
*** a/src/backend/optimizer/util/tlist.c
--- b/src/backend/optimizer/util/tlist.c
*************** get_tlist_exprs(List *tlist, bool includ
*** 188,193 ****
--- 188,214 ----


  /*
+  * count_nonjunk_tlist_entries
+  *        What it says ...
+  */
+ int
+ count_nonjunk_tlist_entries(List *tlist)
+ {
+     int            len = 0;
+     ListCell   *l;
+
+     foreach(l, tlist)
+     {
+         TargetEntry *tle = (TargetEntry *) lfirst(l);
+
+         if (!tle->resjunk)
+             len++;
+     }
+     return len;
+ }
+
+
+ /*
   * tlist_same_exprs
   *        Check whether two target lists contain the same expressions
   *
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index dd04b1a..605c9b4 100644
*** a/src/backend/parser/gram.y
--- b/src/backend/parser/gram.y
*************** single_set_clause:
*** 9234,9239 ****
--- 9234,9247 ----
                  }
          ;

+ /*
+  * Ideally, we'd accept any row-valued a_expr as RHS of a multiple_set_clause.
+  * However, per SQL spec the row-constructor case must allow DEFAULT as a row
+  * member, and it's pretty unclear how to do that (unless perhaps we allow
+  * DEFAULT in any a_expr and let parse analysis sort it out later?).  For the
+  * moment, the planner/executor only support a subquery as a multiassignment
+  * source anyhow, so we need only accept ctext_row and subqueries here.
+  */
  multiple_set_clause:
              '(' set_target_list ')' '=' ctext_row
                  {
*************** multiple_set_clause:
*** 9242,9255 ****

                      /*
                       * Break the ctext_row apart, merge individual expressions
!                      * into the destination ResTargets.  XXX this approach
!                      * cannot work for general row expressions as sources.
                       */
                      if (list_length($2) != list_length($5))
                          ereport(ERROR,
                                  (errcode(ERRCODE_SYNTAX_ERROR),
                                   errmsg("number of columns does not match number of values"),
!                                  parser_errposition(@1)));
                      forboth(col_cell, $2, val_cell, $5)
                      {
                          ResTarget *res_col = (ResTarget *) lfirst(col_cell);
--- 9250,9264 ----

                      /*
                       * Break the ctext_row apart, merge individual expressions
!                      * into the destination ResTargets.  This is semantically
!                      * equivalent to, and much cheaper to process than, the
!                      * general case.
                       */
                      if (list_length($2) != list_length($5))
                          ereport(ERROR,
                                  (errcode(ERRCODE_SYNTAX_ERROR),
                                   errmsg("number of columns does not match number of values"),
!                                  parser_errposition(@5)));
                      forboth(col_cell, $2, val_cell, $5)
                      {
                          ResTarget *res_col = (ResTarget *) lfirst(col_cell);
*************** multiple_set_clause:
*** 9260,9265 ****
--- 9269,9304 ----

                      $$ = $2;
                  }
+             | '(' set_target_list ')' '=' select_with_parens
+                 {
+                     SubLink *sl = makeNode(SubLink);
+                     int ncolumns = list_length($2);
+                     int i = 1;
+                     ListCell *col_cell;
+
+                     /* First, convert bare SelectStmt into a SubLink */
+                     sl->subLinkType = MULTIEXPR_SUBLINK;
+                     sl->subLinkId = 0;        /* will be assigned later */
+                     sl->testexpr = NULL;
+                     sl->operName = NIL;
+                     sl->subselect = $5;
+                     sl->location = @5;
+
+                     /* Create a MultiAssignRef source for each target */
+                     foreach(col_cell, $2)
+                     {
+                         ResTarget *res_col = (ResTarget *) lfirst(col_cell);
+                         MultiAssignRef *r = makeNode(MultiAssignRef);
+
+                         r->source = (Node *) sl;
+                         r->colno = i;
+                         r->ncolumns = ncolumns;
+                         res_col->val = (Node *) r;
+                         i++;
+                     }
+
+                     $$ = $2;
+                 }
          ;

  set_target:
*************** a_expr:        c_expr                                    { $$ = $1; }
*** 11091,11096 ****
--- 11130,11136 ----
                          /* generate foo = ANY (subquery) */
                          SubLink *n = (SubLink *) $3;
                          n->subLinkType = ANY_SUBLINK;
+                         n->subLinkId = 0;
                          n->testexpr = $1;
                          n->operName = list_make1(makeString("="));
                          n->location = @2;
*************** a_expr:        c_expr                                    { $$ = $1; }
*** 11111,11116 ****
--- 11151,11157 ----
                          /* Make an = ANY node */
                          SubLink *n = (SubLink *) $4;
                          n->subLinkType = ANY_SUBLINK;
+                         n->subLinkId = 0;
                          n->testexpr = $1;
                          n->operName = list_make1(makeString("="));
                          n->location = @3;
*************** a_expr:        c_expr                                    { $$ = $1; }
*** 11127,11132 ****
--- 11168,11174 ----
                  {
                      SubLink *n = makeNode(SubLink);
                      n->subLinkType = $3;
+                     n->subLinkId = 0;
                      n->testexpr = $1;
                      n->operName = $2;
                      n->subselect = $4;
*************** c_expr:        columnref                                { $$ = $1; }
*** 11286,11291 ****
--- 11328,11334 ----
                  {
                      SubLink *n = makeNode(SubLink);
                      n->subLinkType = EXPR_SUBLINK;
+                     n->subLinkId = 0;
                      n->testexpr = NULL;
                      n->operName = NIL;
                      n->subselect = $1;
*************** c_expr:        columnref                                { $$ = $1; }
*** 11307,11312 ****
--- 11350,11356 ----
                      SubLink *n = makeNode(SubLink);
                      A_Indirection *a = makeNode(A_Indirection);
                      n->subLinkType = EXPR_SUBLINK;
+                     n->subLinkId = 0;
                      n->testexpr = NULL;
                      n->operName = NIL;
                      n->subselect = $1;
*************** c_expr:        columnref                                { $$ = $1; }
*** 11319,11324 ****
--- 11363,11369 ----
                  {
                      SubLink *n = makeNode(SubLink);
                      n->subLinkType = EXISTS_SUBLINK;
+                     n->subLinkId = 0;
                      n->testexpr = NULL;
                      n->operName = NIL;
                      n->subselect = $2;
*************** c_expr:        columnref                                { $$ = $1; }
*** 11329,11334 ****
--- 11374,11380 ----
                  {
                      SubLink *n = makeNode(SubLink);
                      n->subLinkType = ARRAY_SUBLINK;
+                     n->subLinkId = 0;
                      n->testexpr = NULL;
                      n->operName = NIL;
                      n->subselect = $2;
diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c
index 83e20db..4a8aaf6 100644
*** a/src/backend/parser/parse_expr.c
--- b/src/backend/parser/parse_expr.c
***************
*** 20,25 ****
--- 20,26 ----
  #include "miscadmin.h"
  #include "nodes/makefuncs.h"
  #include "nodes/nodeFuncs.h"
+ #include "optimizer/tlist.h"
  #include "optimizer/var.h"
  #include "parser/analyze.h"
  #include "parser/parse_clause.h"
*************** static Node *transformAExprOf(ParseState
*** 49,54 ****
--- 50,56 ----
  static Node *transformAExprIn(ParseState *pstate, A_Expr *a);
  static Node *transformBoolExpr(ParseState *pstate, BoolExpr *a);
  static Node *transformFuncCall(ParseState *pstate, FuncCall *fn);
+ static Node *transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref);
  static Node *transformCaseExpr(ParseState *pstate, CaseExpr *c);
  static Node *transformSubLink(ParseState *pstate, SubLink *sublink);
  static Node *transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
*************** transformExprRecurse(ParseState *pstate,
*** 255,260 ****
--- 257,266 ----
              result = transformFuncCall(pstate, (FuncCall *) expr);
              break;

+         case T_MultiAssignRef:
+             result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
+             break;
+
          case T_NamedArgExpr:
              {
                  NamedArgExpr *na = (NamedArgExpr *) expr;
*************** transformFuncCall(ParseState *pstate, Fu
*** 1268,1273 ****
--- 1274,1353 ----
  }

  static Node *
+ transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
+ {
+     SubLink    *sublink;
+     Query       *qtree;
+     TargetEntry *tle;
+     Param       *param;
+
+     /* We should only see this in first-stage processing of UPDATE tlists */
+     Assert(pstate->p_expr_kind == EXPR_KIND_UPDATE_SOURCE);
+
+     /* We only need to transform the source if this is the first column */
+     if (maref->colno == 1)
+     {
+         sublink = (SubLink *) transformExprRecurse(pstate, maref->source);
+         /* Currently, the grammar only allows a SubLink as source */
+         Assert(IsA(sublink, SubLink));
+         Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
+         qtree = (Query *) sublink->subselect;
+         Assert(IsA(qtree, Query));
+
+         /* Check subquery returns required number of columns */
+         if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
+             ereport(ERROR,
+                     (errcode(ERRCODE_SYNTAX_ERROR),
+                  errmsg("number of columns does not match number of values"),
+                      parser_errposition(pstate, sublink->location)));
+
+         /*
+          * Build a resjunk tlist item containing the MULTIEXPR SubLink, and
+          * add it to pstate->p_multiassign_exprs, whence it will later get
+          * appended to the completed targetlist.  We needn't worry about
+          * selecting a resno for it; transformUpdateStmt will do that.
+          */
+         tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
+         pstate->p_multiassign_exprs = lappend(pstate->p_multiassign_exprs, tle);
+
+         /*
+          * Assign a unique-within-this-targetlist ID to the MULTIEXPR SubLink.
+          * We can just use its position in the p_multiassign_exprs list.
+          */
+         sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
+     }
+     else
+     {
+         /*
+          * Second or later column in a multiassignment.  Re-fetch the
+          * transformed query, which we assume is still the last entry in
+          * p_multiassign_exprs.
+          */
+         Assert(pstate->p_multiassign_exprs != NIL);
+         tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
+         sublink = (SubLink *) tle->expr;
+         Assert(IsA(sublink, SubLink));
+         Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
+         qtree = (Query *) sublink->subselect;
+         Assert(IsA(qtree, Query));
+     }
+
+     /* Build a Param representing the appropriate subquery output column */
+     tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
+     Assert(!tle->resjunk);
+
+     param = makeNode(Param);
+     param->paramkind = PARAM_MULTIEXPR;
+     param->paramid = (sublink->subLinkId << 16) | maref->colno;
+     param->paramtype = exprType((Node *) tle->expr);
+     param->paramtypmod = exprTypmod((Node *) tle->expr);
+     param->paramcollid = exprCollation((Node *) tle->expr);
+     param->location = exprLocation((Node *) tle->expr);
+
+     return (Node *) param;
+ }
+
+ static Node *
  transformCaseExpr(ParseState *pstate, CaseExpr *c)
  {
      CaseExpr   *newc;
*************** transformSubLink(ParseState *pstate, Sub
*** 1520,1545 ****
      else if (sublink->subLinkType == EXPR_SUBLINK ||
               sublink->subLinkType == ARRAY_SUBLINK)
      {
-         ListCell   *tlist_item = list_head(qtree->targetList);
-
          /*
           * Make sure the subselect delivers a single column (ignoring resjunk
           * targets).
           */
!         if (tlist_item == NULL ||
!             ((TargetEntry *) lfirst(tlist_item))->resjunk)
              ereport(ERROR,
                      (errcode(ERRCODE_SYNTAX_ERROR),
!                      errmsg("subquery must return a column"),
                       parser_errposition(pstate, sublink->location)));
-         while ((tlist_item = lnext(tlist_item)) != NULL)
-         {
-             if (!((TargetEntry *) lfirst(tlist_item))->resjunk)
-                 ereport(ERROR,
-                         (errcode(ERRCODE_SYNTAX_ERROR),
-                          errmsg("subquery must return only one column"),
-                          parser_errposition(pstate, sublink->location)));
-         }

          /*
           * EXPR and ARRAY need no test expression or combining operator. These
--- 1600,1614 ----
      else if (sublink->subLinkType == EXPR_SUBLINK ||
               sublink->subLinkType == ARRAY_SUBLINK)
      {
          /*
           * Make sure the subselect delivers a single column (ignoring resjunk
           * targets).
           */
!         if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
              ereport(ERROR,
                      (errcode(ERRCODE_SYNTAX_ERROR),
!                      errmsg("subquery must return only one column"),
                       parser_errposition(pstate, sublink->location)));

          /*
           * EXPR and ARRAY need no test expression or combining operator. These
*************** transformSubLink(ParseState *pstate, Sub
*** 1548,1553 ****
--- 1617,1628 ----
          sublink->testexpr = NULL;
          sublink->operName = NIL;
      }
+     else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
+     {
+         /* Same as EXPR case, except no restriction on number of columns */
+         sublink->testexpr = NULL;
+         sublink->operName = NIL;
+     }
      else
      {
          /* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
diff --git a/src/backend/parser/parse_target.c b/src/backend/parser/parse_target.c
index 2ee1270..328e0c6 100644
*** a/src/backend/parser/parse_target.c
--- b/src/backend/parser/parse_target.c
*************** transformTargetEntry(ParseState *pstate,
*** 113,121 ****
   * transformTargetList()
   * Turns a list of ResTarget's into a list of TargetEntry's.
   *
!  * At this point, we don't care whether we are doing SELECT, UPDATE,
!  * or RETURNING; we just transform the given expressions (the "val" fields).
!  * However, our subroutines care, so we need the exprKind parameter.
   */
  List *
  transformTargetList(ParseState *pstate, List *targetlist,
--- 113,121 ----
   * transformTargetList()
   * Turns a list of ResTarget's into a list of TargetEntry's.
   *
!  * This code acts mostly the same for SELECT, UPDATE, or RETURNING lists;
!  * the main thing is to transform the given expressions (the "val" fields).
!  * The exprKind parameter distinguishes these cases when necesssary.
   */
  List *
  transformTargetList(ParseState *pstate, List *targetlist,
*************** transformTargetList(ParseState *pstate,
*** 124,129 ****
--- 124,132 ----
      List       *p_target = NIL;
      ListCell   *o_target;

+     /* Shouldn't have any leftover multiassign items at start */
+     Assert(pstate->p_multiassign_exprs == NIL);
+
      foreach(o_target, targetlist)
      {
          ResTarget  *res = (ResTarget *) lfirst(o_target);
*************** transformTargetList(ParseState *pstate,
*** 172,177 ****
--- 175,193 ----
                                                  false));
      }

+     /*
+      * If any multiassign resjunk items were created, attach them to the end
+      * of the targetlist.  This should only happen in an UPDATE tlist.  We
+      * don't need to worry about numbering of these items; transformUpdateStmt
+      * will set their resnos.
+      */
+     if (pstate->p_multiassign_exprs)
+     {
+         Assert(exprKind == EXPR_KIND_UPDATE_SOURCE);
+         p_target = list_concat(p_target, pstate->p_multiassign_exprs);
+         pstate->p_multiassign_exprs = NIL;
+     }
+
      return p_target;
  }

*************** transformExpressionList(ParseState *psta
*** 234,239 ****
--- 250,258 ----
                           transformExpr(pstate, e, exprKind));
      }

+     /* Shouldn't have any multiassign items here */
+     Assert(pstate->p_multiassign_exprs == NIL);
+
      return result;
  }

*************** FigureColnameInternal(Node *node, char *
*** 1691,1696 ****
--- 1710,1716 ----
                      }
                      break;
                      /* As with other operator-like nodes, these have no names */
+                 case MULTIEXPR_SUBLINK:
                  case ALL_SUBLINK:
                  case ANY_SUBLINK:
                  case ROWCOMPARE_SUBLINK:
diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewriteManip.c
index bcf3bd9..fb20314 100644
*** a/src/backend/rewrite/rewriteManip.c
--- b/src/backend/rewrite/rewriteManip.c
*************** checkExprHasSubLink_walker(Node *node, v
*** 281,286 ****
--- 281,306 ----
      return expression_tree_walker(node, checkExprHasSubLink_walker, context);
  }

+ /*
+  * Check for MULTIEXPR Param within expression tree
+  *
+  * We intentionally don't descend into SubLinks: only Params at the current
+  * query level are of interest.
+  */
+ static bool
+ contains_multiexpr_param(Node *node, void *context)
+ {
+     if (node == NULL)
+         return false;
+     if (IsA(node, Param))
+     {
+         if (((Param *) node)->paramkind == PARAM_MULTIEXPR)
+             return true;        /* abort the tree traversal and return true */
+         return false;
+     }
+     return expression_tree_walker(node, contains_multiexpr_param, context);
+ }
+

  /*
   * OffsetVarNodes - adjust Vars when appending one query's RT to another
*************** ReplaceVarsFromTargetList_callback(Var *
*** 1370,1375 ****
--- 1390,1410 ----
          if (var->varlevelsup > 0)
              IncrementVarSublevelsUp(newnode, var->varlevelsup, 0);

+         /*
+          * Check to see if the tlist item contains a PARAM_MULTIEXPR Param,
+          * and throw error if so.  This case could only happen when expanding
+          * an ON UPDATE rule's NEW variable and the referenced tlist item in
+          * the original UPDATE command is part of a multiple assignment. There
+          * seems no practical way to handle such cases without multiple
+          * evaluation of the multiple assignment's sub-select, which would
+          * create semantic oddities that users of rules would probably prefer
+          * not to cope with.  So treat it as an unimplemented feature.
+          */
+         if (contains_multiexpr_param(newnode, NULL))
+             ereport(ERROR,
+                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                      errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple
assignmentin the subject UPDATE command"))); 
+
          return newnode;
      }
  }
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index a30d8fe..0781ac8 100644
*** a/src/backend/utils/adt/ruleutils.c
--- b/src/backend/utils/adt/ruleutils.c
*************** static void
*** 5228,5235 ****
  get_update_query_def(Query *query, deparse_context *context)
  {
      StringInfo    buf = context->buf;
-     char       *sep;
      RangeTblEntry *rte;
      ListCell   *l;

      /* Insert the WITH clause if given */
--- 5228,5239 ----
  get_update_query_def(Query *query, deparse_context *context)
  {
      StringInfo    buf = context->buf;
      RangeTblEntry *rte;
+     List       *ma_sublinks;
+     ListCell   *next_ma_cell;
+     SubLink    *cur_ma_sublink;
+     int            remaining_ma_columns;
+     const char *sep;
      ListCell   *l;

      /* Insert the WITH clause if given */
*************** get_update_query_def(Query *query, depar
*** 5253,5258 ****
--- 5257,5290 ----
                           quote_identifier(rte->alias->aliasname));
      appendStringInfoString(buf, " SET ");

+     /*
+      * Prepare to deal with MULTIEXPR assignments: collect the source SubLinks
+      * into a list.  We expect them to appear, in ID order, in resjunk tlist
+      * entries.
+      */
+     ma_sublinks = NIL;
+     if (query->hasSubLinks)        /* else there can't be any */
+     {
+         foreach(l, query->targetList)
+         {
+             TargetEntry *tle = (TargetEntry *) lfirst(l);
+
+             if (tle->resjunk && IsA(tle->expr, SubLink))
+             {
+                 SubLink    *sl = (SubLink *) tle->expr;
+
+                 if (sl->subLinkType == MULTIEXPR_SUBLINK)
+                 {
+                     ma_sublinks = lappend(ma_sublinks, sl);
+                     Assert(sl->subLinkId == list_length(ma_sublinks));
+                 }
+             }
+         }
+     }
+     next_ma_cell = list_head(ma_sublinks);
+     cur_ma_sublink = NULL;
+     remaining_ma_columns = 0;
+
      /* Add the comma separated list of 'attname = value' */
      sep = "";
      foreach(l, query->targetList)
*************** get_update_query_def(Query *query, depar
*** 5263,5272 ****
--- 5295,5352 ----
          if (tle->resjunk)
              continue;            /* ignore junk entries */

+         /* Emit separator (OK whether we're in multiassignment or not) */
          appendStringInfoString(buf, sep);
          sep = ", ";

          /*
+          * Check to see if we're starting a multiassignment group: if so,
+          * output a left paren.
+          */
+         if (next_ma_cell != NULL && cur_ma_sublink == NULL)
+         {
+             /*
+              * We must dig down into the expr to see if it's a PARAM_MULTIEXPR
+              * Param.  That could be buried under FieldStores and ArrayRefs
+              * (cf processIndirection()), and underneath those there could be
+              * an implicit type coercion.
+              */
+             expr = (Node *) tle->expr;
+             while (expr)
+             {
+                 if (IsA(expr, FieldStore))
+                 {
+                     FieldStore *fstore = (FieldStore *) expr;
+
+                     expr = (Node *) linitial(fstore->newvals);
+                 }
+                 else if (IsA(expr, ArrayRef))
+                 {
+                     ArrayRef   *aref = (ArrayRef *) expr;
+
+                     if (aref->refassgnexpr == NULL)
+                         break;
+                     expr = (Node *) aref->refassgnexpr;
+                 }
+                 else
+                     break;
+             }
+             expr = strip_implicit_coercions(expr);
+
+             if (expr && IsA(expr, Param) &&
+                 ((Param *) expr)->paramkind == PARAM_MULTIEXPR)
+             {
+                 cur_ma_sublink = (SubLink *) lfirst(next_ma_cell);
+                 next_ma_cell = lnext(next_ma_cell);
+                 remaining_ma_columns = count_nonjunk_tlist_entries(
+                           ((Query *) cur_ma_sublink->subselect)->targetList);
+                 Assert(((Param *) expr)->paramid ==
+                        ((cur_ma_sublink->subLinkId << 16) | 1));
+                 appendStringInfoChar(buf, '(');
+             }
+         }
+
+         /*
           * Put out name of target column; look in the catalogs, not at
           * tle->resname, since resname will fail to track RENAME.
           */
*************** get_update_query_def(Query *query, depar
*** 5280,5285 ****
--- 5360,5379 ----
           */
          expr = processIndirection((Node *) tle->expr, context, true);

+         /*
+          * If we're in a multiassignment, skip printing anything more, unless
+          * this is the last column; in which case, what we print should be the
+          * sublink, not the Param.
+          */
+         if (cur_ma_sublink != NULL)
+         {
+             if (--remaining_ma_columns > 0)
+                 continue;        /* not the last column of multiassignment */
+             appendStringInfoChar(buf, ')');
+             expr = (Node *) cur_ma_sublink;
+             cur_ma_sublink = NULL;
+         }
+
          appendStringInfoString(buf, " = ");

          get_rule_expr(expr, context, false);
*************** get_sublink_expr(SubLink *sublink, depar
*** 8123,8128 ****
--- 8217,8223 ----
              break;

          case EXPR_SUBLINK:
+         case MULTIEXPR_SUBLINK:
          case ARRAY_SUBLINK:
              need_paren = false;
              break;
diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h
index f0926e9..dc49ace 100644
*** a/src/include/catalog/catversion.h
--- b/src/include/catalog/catversion.h
***************
*** 53,58 ****
   */

  /*                            yyyymmddN */
! #define CATALOG_VERSION_NO    201406121

  #endif
--- 53,58 ----
   */

  /*                            yyyymmddN */
! #define CATALOG_VERSION_NO    201406171

  #endif
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index 0ab2a13..1f7c6d1 100644
*** a/src/include/nodes/execnodes.h
--- b/src/include/nodes/execnodes.h
*************** typedef struct SubPlanState
*** 728,733 ****
--- 728,734 ----
  {
      ExprState    xprstate;
      struct PlanState *planstate;    /* subselect plan's state tree */
+     struct PlanState *parent;    /* parent plan node's state tree */
      ExprState  *testexpr;        /* state of combining expression */
      List       *args;            /* states of argument expression(s) */
      HeapTuple    curTuple;        /* copy of most recent tuple from subplan */
diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h
index bc58e16..7b0088f 100644
*** a/src/include/nodes/nodes.h
--- b/src/include/nodes/nodes.h
*************** typedef enum NodeTag
*** 379,384 ****
--- 379,385 ----
      T_A_Indirection,
      T_A_ArrayExpr,
      T_ResTarget,
+     T_MultiAssignRef,
      T_TypeCast,
      T_CollateClause,
      T_SortBy,
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index 9a68c87..ff126eb 100644
*** a/src/include/nodes/parsenodes.h
--- b/src/include/nodes/parsenodes.h
*************** typedef struct ResTarget
*** 385,390 ****
--- 385,407 ----
  } ResTarget;

  /*
+  * MultiAssignRef - element of a row source expression for UPDATE
+  *
+  * In an UPDATE target list, when we have SET (a,b,c) = row-valued-expression,
+  * we generate separate ResTarget items for each of a,b,c.  Their "val" trees
+  * are MultiAssignRef nodes numbered 1..n, linking to a common copy of the
+  * row-valued-expression (which parse analysis will process only once, when
+  * handling the MultiAssignRef with colno=1).
+  */
+ typedef struct MultiAssignRef
+ {
+     NodeTag        type;
+     Node       *source;            /* the row-valued expression */
+     int            colno;            /* column number for this target (1..n) */
+     int            ncolumns;        /* number of targets in the construct */
+ } MultiAssignRef;
+
+ /*
   * SortBy - for ORDER BY clause
   */
  typedef struct SortBy
diff --git a/src/include/nodes/pg_list.h b/src/include/nodes/pg_list.h
index 4167680..c545115 100644
*** a/src/include/nodes/pg_list.h
--- b/src/include/nodes/pg_list.h
*************** extern List *lcons_oid(Oid datum, List *
*** 206,211 ****
--- 206,212 ----
  extern List *list_concat(List *list1, List *list2);
  extern List *list_truncate(List *list, int new_size);

+ extern ListCell *list_nth_cell(const List *list, int n);
  extern void *list_nth(const List *list, int n);
  extern int    list_nth_int(const List *list, int n);
  extern Oid    list_nth_oid(const List *list, int n);
diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h
index db8e87f..6d9f3d9 100644
*** a/src/include/nodes/primnodes.h
--- b/src/include/nodes/primnodes.h
*************** typedef struct Const
*** 198,214 ****
   *                `paramid' field.  (This type of Param is converted to
   *                PARAM_EXEC during planning.)
   *
!  * Note: currently, paramtypmod is valid for PARAM_SUBLINK Params, and for
!  * PARAM_EXEC Params generated from them; it is always -1 for PARAM_EXTERN
!  * params, since the APIs that supply values for such parameters don't carry
!  * any typmod info.
   * ----------------
   */
  typedef enum ParamKind
  {
      PARAM_EXTERN,
      PARAM_EXEC,
!     PARAM_SUBLINK
  } ParamKind;

  typedef struct Param
--- 198,222 ----
   *                `paramid' field.  (This type of Param is converted to
   *                PARAM_EXEC during planning.)
   *
!  *        PARAM_MULTIEXPR:  Like PARAM_SUBLINK, the parameter represents an
!  *                output column of a SubLink node's sub-select, but here, the
!  *                SubLink is always a MULTIEXPR SubLink.  The high-order 16 bits
!  *                of the `paramid' field contain the SubLink's subLinkId, and
!  *                the low-order 16 bits contain the column number.  (This type
!  *                of Param is also converted to PARAM_EXEC during planning.)
!  *
!  * Note: currently, paramtypmod is always -1 for PARAM_EXTERN params, since
!  * the APIs that supply values for such parameters don't carry any typmod
!  * info.  It is valid in other types of Params, if they represent expressions
!  * with determinable typmod.
   * ----------------
   */
  typedef enum ParamKind
  {
      PARAM_EXTERN,
      PARAM_EXEC,
!     PARAM_SUBLINK,
!     PARAM_MULTIEXPR
  } ParamKind;

  typedef struct Param
*************** typedef struct BoolExpr
*** 485,498 ****
   *    ANY_SUBLINK            (lefthand) op ANY (SELECT ...)
   *    ROWCOMPARE_SUBLINK    (lefthand) op (SELECT ...)
   *    EXPR_SUBLINK        (SELECT with single targetlist item ...)
   *    ARRAY_SUBLINK        ARRAY(SELECT with single targetlist item ...)
   *    CTE_SUBLINK            WITH query (never actually part of an expression)
   * For ALL, ANY, and ROWCOMPARE, the lefthand is a list of expressions of the
   * same length as the subselect's targetlist.  ROWCOMPARE will *always* have
   * a list with more than one entry; if the subselect has just one target
   * then the parser will create an EXPR_SUBLINK instead (and any operator
!  * above the subselect will be represented separately).  Note that both
!  * ROWCOMPARE and EXPR require the subselect to deliver only one row.
   * ALL, ANY, and ROWCOMPARE require the combining operators to deliver boolean
   * results.  ALL and ANY combine the per-row results using AND and OR
   * semantics respectively.
--- 493,508 ----
   *    ANY_SUBLINK            (lefthand) op ANY (SELECT ...)
   *    ROWCOMPARE_SUBLINK    (lefthand) op (SELECT ...)
   *    EXPR_SUBLINK        (SELECT with single targetlist item ...)
+  *    MULTIEXPR_SUBLINK    (SELECT with multiple targetlist items ...)
   *    ARRAY_SUBLINK        ARRAY(SELECT with single targetlist item ...)
   *    CTE_SUBLINK            WITH query (never actually part of an expression)
   * For ALL, ANY, and ROWCOMPARE, the lefthand is a list of expressions of the
   * same length as the subselect's targetlist.  ROWCOMPARE will *always* have
   * a list with more than one entry; if the subselect has just one target
   * then the parser will create an EXPR_SUBLINK instead (and any operator
!  * above the subselect will be represented separately).
!  * ROWCOMPARE, EXPR, and MULTIEXPR require the subselect to deliver at most
!  * one row (if it returns no rows, the result is NULL).
   * ALL, ANY, and ROWCOMPARE require the combining operators to deliver boolean
   * results.  ALL and ANY combine the per-row results using AND and OR
   * semantics respectively.
*************** typedef struct BoolExpr
*** 511,518 ****
   * output columns of the subselect.  And subselect is transformed to a Query.
   * This is the representation seen in saved rules and in the rewriter.
   *
!  * In EXISTS, EXPR, and ARRAY SubLinks, testexpr and operName are unused and
!  * are always null.
   *
   * The CTE_SUBLINK case never occurs in actual SubLink nodes, but it is used
   * in SubPlans generated for WITH subqueries.
--- 521,534 ----
   * output columns of the subselect.  And subselect is transformed to a Query.
   * This is the representation seen in saved rules and in the rewriter.
   *
!  * In EXISTS, EXPR, MULTIEXPR, and ARRAY SubLinks, testexpr and operName
!  * are unused and are always null.
!  *
!  * subLinkId is currently used only for MULTIEXPR SubLinks, and is zero in
!  * other SubLinks.  This number identifies different multiple-assignment
!  * subqueries within an UPDATE statement's SET list.  It is unique only
!  * within a particular targetlist.  The output column(s) of the MULTIEXPR
!  * are referenced by PARAM_MULTIEXPR Params appearing elsewhere in the tlist.
   *
   * The CTE_SUBLINK case never occurs in actual SubLink nodes, but it is used
   * in SubPlans generated for WITH subqueries.
*************** typedef enum SubLinkType
*** 524,529 ****
--- 540,546 ----
      ANY_SUBLINK,
      ROWCOMPARE_SUBLINK,
      EXPR_SUBLINK,
+     MULTIEXPR_SUBLINK,
      ARRAY_SUBLINK,
      CTE_SUBLINK                    /* for SubPlans only */
  } SubLinkType;
*************** typedef struct SubLink
*** 533,541 ****
  {
      Expr        xpr;
      SubLinkType subLinkType;    /* see above */
      Node       *testexpr;        /* outer-query test for ALL/ANY/ROWCOMPARE */
      List       *operName;        /* originally specified operator name */
!     Node       *subselect;        /* subselect as Query* or parsetree */
      int            location;        /* token location, or -1 if unknown */
  } SubLink;

--- 550,559 ----
  {
      Expr        xpr;
      SubLinkType subLinkType;    /* see above */
+     int            subLinkId;        /* ID (1..n); 0 if not MULTIEXPR */
      Node       *testexpr;        /* outer-query test for ALL/ANY/ROWCOMPARE */
      List       *operName;        /* originally specified operator name */
!     Node       *subselect;        /* subselect as Query* or raw parsetree */
      int            location;        /* token location, or -1 if unknown */
  } SubLink;

diff --git a/src/include/nodes/relation.h b/src/include/nodes/relation.h
index 300136e..dacbe9c 100644
*** a/src/include/nodes/relation.h
--- b/src/include/nodes/relation.h
*************** typedef struct PlannerInfo
*** 190,195 ****
--- 190,198 ----

      List       *cte_plan_ids;    /* per-CTE-item list of subplan IDs */

+     List       *multiexpr_params;        /* List of Lists of Params for
+                                          * MULTIEXPR subquery outputs */
+
      List       *eq_classes;        /* list of active EquivalenceClasses */

      List       *canon_pathkeys; /* list of "canonical" PathKeys */
diff --git a/src/include/optimizer/tlist.h b/src/include/optimizer/tlist.h
index 122a179..1ebb635 100644
*** a/src/include/optimizer/tlist.h
--- b/src/include/optimizer/tlist.h
*************** extern List *add_to_flat_tlist(List *tli
*** 27,32 ****
--- 27,34 ----

  extern List *get_tlist_exprs(List *tlist, bool includeJunk);

+ extern int    count_nonjunk_tlist_entries(List *tlist);
+
  extern bool tlist_same_exprs(List *tlist1, List *tlist2);

  extern bool tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK);
diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h
index 4ce802a..b32ddf7 100644
*** a/src/include/parser/parse_node.h
--- b/src/include/parser/parse_node.h
*************** struct ParseState
*** 144,149 ****
--- 144,150 ----
      List       *p_windowdefs;    /* raw representations of window clauses */
      ParseExprKind p_expr_kind;    /* what kind of expression we're parsing */
      int            p_next_resno;    /* next targetlist resno to assign */
+     List       *p_multiassign_exprs;    /* junk tlist entries for multiassign */
      List       *p_locking_clause;        /* raw FOR UPDATE/FOR SHARE info */
      Node       *p_value_substitute;        /* what to replace VALUE with, if any */
      bool        p_hasAggs;
diff --git a/src/test/regress/expected/update.out b/src/test/regress/expected/update.out
index 71b856f..1de2a86 100644
*** a/src/test/regress/expected/update.out
--- b/src/test/regress/expected/update.out
*************** SELECT * FROM update_test;
*** 55,85 ****
  --
  -- Test multiple-set-clause syntax
  --
  UPDATE update_test SET (c,b,a) = ('bugle', b+11, DEFAULT) WHERE c = 'foo';
  SELECT * FROM update_test;
    a  | b  |   c
  -----+----+-------
   100 | 20 |
    10 | 31 | bugle
! (2 rows)

  UPDATE update_test SET (c,b) = ('car', a+b), a = a + 1 WHERE a = 10;
  SELECT * FROM update_test;
    a  | b  |  c
  -----+----+-----
   100 | 20 |
    11 | 41 | car
! (2 rows)

  -- fail, multi assignment to same column:
  UPDATE update_test SET (c,b) = ('car', a+b), b = a + 1 WHERE a = 10;
  ERROR:  multiple assignments to same column "b"
! -- XXX this should work, but doesn't yet:
! UPDATE update_test SET (a,b) = (select a,b FROM update_test where c = 'foo')
!   WHERE a = 10;
! ERROR:  syntax error at or near "select"
! LINE 1: UPDATE update_test SET (a,b) = (select a,b FROM update_test ...
!                                         ^
  -- if an alias for the target table is specified, don't allow references
  -- to the original table name
  UPDATE update_test AS t SET b = update_test.b + 10 WHERE t.a = 10;
--- 55,134 ----
  --
  -- Test multiple-set-clause syntax
  --
+ INSERT INTO update_test SELECT a,b+1,c FROM update_test;
+ SELECT * FROM update_test;
+   a  | b  |  c
+ -----+----+-----
+  100 | 20 | foo
+  100 | 20 |
+  100 | 21 | foo
+  100 | 21 |
+ (4 rows)
+
  UPDATE update_test SET (c,b,a) = ('bugle', b+11, DEFAULT) WHERE c = 'foo';
  SELECT * FROM update_test;
    a  | b  |   c
  -----+----+-------
   100 | 20 |
+  100 | 21 |
    10 | 31 | bugle
!   10 | 32 | bugle
! (4 rows)

  UPDATE update_test SET (c,b) = ('car', a+b), a = a + 1 WHERE a = 10;
  SELECT * FROM update_test;
    a  | b  |  c
  -----+----+-----
   100 | 20 |
+  100 | 21 |
    11 | 41 | car
!   11 | 42 | car
! (4 rows)

  -- fail, multi assignment to same column:
  UPDATE update_test SET (c,b) = ('car', a+b), b = a + 1 WHERE a = 10;
  ERROR:  multiple assignments to same column "b"
! -- uncorrelated sub-select:
! UPDATE update_test
!   SET (b,a) = (select a,b from update_test where b = 41 and c = 'car')
!   WHERE a = 100 AND b = 20;
! SELECT * FROM update_test;
!   a  | b  |  c
! -----+----+-----
!  100 | 21 |
!   11 | 41 | car
!   11 | 42 | car
!   41 | 11 |
! (4 rows)
!
! -- correlated sub-select:
! UPDATE update_test o
!   SET (b,a) = (select a+1,b from update_test i
!                where i.a=o.a and i.b=o.b and i.c is not distinct from o.c);
! SELECT * FROM update_test;
!  a  |  b  |  c
! ----+-----+-----
!  21 | 101 |
!  41 |  12 | car
!  42 |  12 | car
!  11 |  42 |
! (4 rows)
!
! -- fail, multiple rows supplied:
! UPDATE update_test SET (b,a) = (select a+1,b from update_test);
! ERROR:  more than one row returned by a subquery used as an expression
! -- set to null if no rows supplied:
! UPDATE update_test SET (b,a) = (select a+1,b from update_test where a = 1000)
!   WHERE a = 11;
! SELECT * FROM update_test;
!  a  |  b  |  c
! ----+-----+-----
!  21 | 101 |
!  41 |  12 | car
!  42 |  12 | car
!     |     |
! (4 rows)
!
  -- if an alias for the target table is specified, don't allow references
  -- to the original table name
  UPDATE update_test AS t SET b = update_test.b + 10 WHERE t.a = 10;
*************** HINT:  Perhaps you meant to reference th
*** 90,99 ****
  -- Make sure that we can update to a TOASTed value.
  UPDATE update_test SET c = repeat('x', 10000) WHERE c = 'car';
  SELECT a, b, char_length(c) FROM update_test;
!   a  | b  | char_length
! -----+----+-------------
!  100 | 20 |
!   11 | 41 |       10000
! (2 rows)

  DROP TABLE update_test;
--- 139,150 ----
  -- Make sure that we can update to a TOASTed value.
  UPDATE update_test SET c = repeat('x', 10000) WHERE c = 'car';
  SELECT a, b, char_length(c) FROM update_test;
!  a  |  b  | char_length
! ----+-----+-------------
!  21 | 101 |
!     |     |
!  41 |  12 |       10000
!  42 |  12 |       10000
! (4 rows)

  DROP TABLE update_test;
diff --git a/src/test/regress/sql/update.sql b/src/test/regress/sql/update.sql
index a8a028f..e71128c 100644
*** a/src/test/regress/sql/update.sql
--- b/src/test/regress/sql/update.sql
*************** SELECT * FROM update_test;
*** 39,44 ****
--- 39,47 ----
  -- Test multiple-set-clause syntax
  --

+ INSERT INTO update_test SELECT a,b+1,c FROM update_test;
+ SELECT * FROM update_test;
+
  UPDATE update_test SET (c,b,a) = ('bugle', b+11, DEFAULT) WHERE c = 'foo';
  SELECT * FROM update_test;
  UPDATE update_test SET (c,b) = ('car', a+b), a = a + 1 WHERE a = 10;
*************** SELECT * FROM update_test;
*** 46,54 ****
  -- fail, multi assignment to same column:
  UPDATE update_test SET (c,b) = ('car', a+b), b = a + 1 WHERE a = 10;

! -- XXX this should work, but doesn't yet:
! UPDATE update_test SET (a,b) = (select a,b FROM update_test where c = 'foo')
!   WHERE a = 10;

  -- if an alias for the target table is specified, don't allow references
  -- to the original table name
--- 49,70 ----
  -- fail, multi assignment to same column:
  UPDATE update_test SET (c,b) = ('car', a+b), b = a + 1 WHERE a = 10;

! -- uncorrelated sub-select:
! UPDATE update_test
!   SET (b,a) = (select a,b from update_test where b = 41 and c = 'car')
!   WHERE a = 100 AND b = 20;
! SELECT * FROM update_test;
! -- correlated sub-select:
! UPDATE update_test o
!   SET (b,a) = (select a+1,b from update_test i
!                where i.a=o.a and i.b=o.b and i.c is not distinct from o.c);
! SELECT * FROM update_test;
! -- fail, multiple rows supplied:
! UPDATE update_test SET (b,a) = (select a+1,b from update_test);
! -- set to null if no rows supplied:
! UPDATE update_test SET (b,a) = (select a+1,b from update_test where a = 1000)
!   WHERE a = 11;
! SELECT * FROM update_test;

  -- if an alias for the target table is specified, don't allow references
  -- to the original table name

pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: pg_control is missing a field for LOBLKSIZE
Next
From: Alvaro Herrera
Date:
Subject: Re: Atomics hardware support table & supported architectures