]> granicus.if.org Git - postgresql/commitdiff
Prevent failure when RowExpr or XmlExpr is parse-analyzed twice.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 23 Dec 2012 19:07:31 +0000 (14:07 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 23 Dec 2012 19:07:31 +0000 (14:07 -0500)
transformExpr() is required to cope with already-transformed expression
trees, for various ugly-but-not-quite-worth-cleaning-up reasons.  However,
some of its newer subroutines hadn't gotten the memo.  This accounts for
bug #7763 from Norbert Buchmuller: transformRowExpr() was overwriting the
previously determined type of a RowExpr during CREATE TABLE LIKE INCLUDING
INDEXES.  Additional investigation showed that transformXmlExpr had the
same kind of problem, but all the other cases seem to be safe.

Andres Freund and Tom Lane

src/backend/parser/gram.y
src/backend/parser/parse_expr.c
src/include/nodes/primnodes.h

index 44d0b3c600002e09e858c1d64a4c8e84a512f964..7a854e364d3dcbe4312cb8a90695647c1ab77c23 100644 (file)
@@ -13135,6 +13135,7 @@ makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
        x->args = args;
        /* xmloption, if relevant, must be filled in by caller */
        /* type and typmod will be filled in during parse analysis */
+       x->type = InvalidOid;                   /* marks the node as not analyzed */
        x->location = location;
        return (Node *) x;
 }
index bb1ad9af96bb5df02b9bf2a06222dd7dca8ea926..0a3a57955180356f2c6b826390b12052d1efe0e4 100644 (file)
@@ -91,6 +91,8 @@ static Expr *make_distinct_op(ParseState *pstate, List *opname,
  *     function argument to the required type (via coerce_type())
  *     can apply transformExpr to an already-transformed subexpression.
  *     An example here is "SELECT count(*) + 1.0 FROM table".
+ *     3. CREATE TABLE t1 (LIKE t2 INCLUDING INDEXES) can pass in
+ *     already-transformed index expressions.
  * While it might be possible to eliminate these cases, the path of
  * least resistance so far has been to ensure that transformExpr() does
  * no damage if applied to an already-transformed tree.  This is pretty
@@ -1685,11 +1687,17 @@ transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
 static Node *
 transformRowExpr(ParseState *pstate, RowExpr *r)
 {
-       RowExpr    *newr = makeNode(RowExpr);
+       RowExpr    *newr;
        char            fname[16];
        int                     fnum;
        ListCell   *lc;
 
+       /* If we already transformed this node, do nothing */
+       if (OidIsValid(r->row_typeid))
+               return (Node *) r;
+
+       newr = makeNode(RowExpr);
+
        /* Transform the field expressions */
        newr->args = transformExpressionList(pstate, r->args);
 
@@ -1790,16 +1798,23 @@ transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
 static Node *
 transformXmlExpr(ParseState *pstate, XmlExpr *x)
 {
-       XmlExpr    *newx = makeNode(XmlExpr);
+       XmlExpr    *newx;
        ListCell   *lc;
        int                     i;
 
+       /* If we already transformed this node, do nothing */
+       if (OidIsValid(x->type))
+               return (Node *) x;
+
+       newx = makeNode(XmlExpr);
        newx->op = x->op;
        if (x->name)
                newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
        else
                newx->name = NULL;
        newx->xmloption = x->xmloption;
+       newx->type = XMLOID;            /* this just marks the node as transformed */
+       newx->typmod = -1;
        newx->location = x->location;
 
        /*
index daabcb6cf9a137a670401d8901e290e5e01de621..b7103b1f01a3bab3b587487f1ce1b70e6bf84585 100644 (file)
@@ -959,7 +959,8 @@ typedef struct MinMaxExpr
  *
  * Note: result type/typmod/collation are not stored, but can be deduced
  * from the XmlExprOp. The type/typmod fields are just used for display
- * purposes, and are NOT the true result type of the node.
+ * purposes, and are NOT necessarily the true result type of the node.
+ * (We also use type == InvalidOid to mark a not-yet-parse-analyzed XmlExpr.)
  */
 typedef enum XmlExprOp
 {