From 4297f1732f7bf8f45f81a5e996d8333f2863a3d3 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 23 Dec 2012 14:07:52 -0500 Subject: [PATCH] Prevent failure when RowExpr or XmlExpr is parse-analyzed twice. 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 | 3 +++ src/backend/parser/parse_expr.c | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 2b992fab4a7..eb75f37e47c 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -9817,6 +9817,9 @@ makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args) x->named_args = named_args; x->arg_names = NIL; 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 */ return (Node *) x; } diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 98b85724648..c27a1373be0 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -92,6 +92,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 @@ -1341,7 +1343,13 @@ transformArrayExpr(ParseState *pstate, ArrayExpr *a) static Node * transformRowExpr(ParseState *pstate, RowExpr *r) { - RowExpr *newr = makeNode(RowExpr); + RowExpr *newr; + + /* 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); @@ -1431,15 +1439,22 @@ 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->type = XMLOID; /* this just marks the node as transformed */ + newx->typmod = -1; /* * gram.y built the named args as a list of ResTarget. Transform each,