LCOV - code coverage report
Current view: top level - src/backend/parser - analyze.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 937 1034 90.6 %
Date: 2021-12-04 23:09:10 Functions: 32 33 97.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * analyze.c
       4             :  *    transform the raw parse tree into a query tree
       5             :  *
       6             :  * For optimizable statements, we are careful to obtain a suitable lock on
       7             :  * each referenced table, and other modules of the backend preserve or
       8             :  * re-obtain these locks before depending on the results.  It is therefore
       9             :  * okay to do significant semantic analysis of these statements.  For
      10             :  * utility commands, no locks are obtained here (and if they were, we could
      11             :  * not be sure we'd still have them at execution).  Hence the general rule
      12             :  * for utility commands is to just dump them into a Query node untransformed.
      13             :  * DECLARE CURSOR, EXPLAIN, and CREATE TABLE AS are exceptions because they
      14             :  * contain optimizable statements, which we should transform.
      15             :  *
      16             :  *
      17             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
      18             :  * Portions Copyright (c) 1994, Regents of the University of California
      19             :  *
      20             :  *  src/backend/parser/analyze.c
      21             :  *
      22             :  *-------------------------------------------------------------------------
      23             :  */
      24             : 
      25             : #include "postgres.h"
      26             : 
      27             : #include "access/sysattr.h"
      28             : #include "catalog/pg_proc.h"
      29             : #include "catalog/pg_type.h"
      30             : #include "miscadmin.h"
      31             : #include "nodes/makefuncs.h"
      32             : #include "nodes/nodeFuncs.h"
      33             : #include "optimizer/optimizer.h"
      34             : #include "parser/analyze.h"
      35             : #include "parser/parse_agg.h"
      36             : #include "parser/parse_clause.h"
      37             : #include "parser/parse_coerce.h"
      38             : #include "parser/parse_collate.h"
      39             : #include "parser/parse_cte.h"
      40             : #include "parser/parse_expr.h"
      41             : #include "parser/parse_func.h"
      42             : #include "parser/parse_oper.h"
      43             : #include "parser/parse_param.h"
      44             : #include "parser/parse_relation.h"
      45             : #include "parser/parse_target.h"
      46             : #include "parser/parse_type.h"
      47             : #include "parser/parsetree.h"
      48             : #include "rewrite/rewriteManip.h"
      49             : #include "utils/backend_status.h"
      50             : #include "utils/builtins.h"
      51             : #include "utils/guc.h"
      52             : #include "utils/queryjumble.h"
      53             : #include "utils/rel.h"
      54             : #include "utils/syscache.h"
      55             : 
      56             : 
      57             : /* Hook for plugins to get control at end of parse analysis */
      58             : post_parse_analyze_hook_type post_parse_analyze_hook = NULL;
      59             : 
      60             : static Query *transformOptionalSelectInto(ParseState *pstate, Node *parseTree);
      61             : static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
      62             : static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
      63             : static List *transformInsertRow(ParseState *pstate, List *exprlist,
      64             :                                 List *stmtcols, List *icolumns, List *attrnos,
      65             :                                 bool strip_indirection);
      66             : static OnConflictExpr *transformOnConflictClause(ParseState *pstate,
      67             :                                                  OnConflictClause *onConflictClause);
      68             : static int  count_rowexpr_columns(ParseState *pstate, Node *expr);
      69             : static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt);
      70             : static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt);
      71             : static Query *transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt);
      72             : static Node *transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
      73             :                                        bool isTopLevel, List **targetlist);
      74             : static void determineRecursiveColTypes(ParseState *pstate,
      75             :                                        Node *larg, List *nrtargetlist);
      76             : static Query *transformReturnStmt(ParseState *pstate, ReturnStmt *stmt);
      77             : static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
      78             : static List *transformReturningList(ParseState *pstate, List *returningList);
      79             : static List *transformUpdateTargetList(ParseState *pstate,
      80             :                                        List *targetList);
      81             : static Query *transformPLAssignStmt(ParseState *pstate,
      82             :                                     PLAssignStmt *stmt);
      83             : static Query *transformDeclareCursorStmt(ParseState *pstate,
      84             :                                          DeclareCursorStmt *stmt);
      85             : static Query *transformExplainStmt(ParseState *pstate,
      86             :                                    ExplainStmt *stmt);
      87             : static Query *transformCreateTableAsStmt(ParseState *pstate,
      88             :                                          CreateTableAsStmt *stmt);
      89             : static Query *transformCallStmt(ParseState *pstate,
      90             :                                 CallStmt *stmt);
      91             : static void transformLockingClause(ParseState *pstate, Query *qry,
      92             :                                    LockingClause *lc, bool pushedDown);
      93             : #ifdef RAW_EXPRESSION_COVERAGE_TEST
      94             : static bool test_raw_expression_coverage(Node *node, void *context);
      95             : #endif
      96             : 
      97             : 
      98             : /*
      99             :  * parse_analyze
     100             :  *      Analyze a raw parse tree and transform it to Query form.
     101             :  *
     102             :  * Optionally, information about $n parameter types can be supplied.
     103             :  * References to $n indexes not defined by paramTypes[] are disallowed.
     104             :  *
     105             :  * The result is a Query node.  Optimizable statements require considerable
     106             :  * transformation, while utility-type statements are simply hung off
     107             :  * a dummy CMD_UTILITY Query node.
     108             :  */
     109             : Query *
     110      850216 : parse_analyze(RawStmt *parseTree, const char *sourceText,
     111             :               Oid *paramTypes, int numParams,
     112             :               QueryEnvironment *queryEnv)
     113             : {
     114      850216 :     ParseState *pstate = make_parsestate(NULL);
     115             :     Query      *query;
     116      850216 :     JumbleState *jstate = NULL;
     117             : 
     118             :     Assert(sourceText != NULL); /* required as of 8.4 */
     119             : 
     120      850216 :     pstate->p_sourcetext = sourceText;
     121             : 
     122      850216 :     if (numParams > 0)
     123        2600 :         parse_fixed_parameters(pstate, paramTypes, numParams);
     124             : 
     125      850216 :     pstate->p_queryEnv = queryEnv;
     126             : 
     127      850216 :     query = transformTopLevelStmt(pstate, parseTree);
     128             : 
     129      846572 :     if (IsQueryIdEnabled())
     130         572 :         jstate = JumbleQuery(query, sourceText);
     131             : 
     132      846572 :     if (post_parse_analyze_hook)
     133         564 :         (*post_parse_analyze_hook) (pstate, query, jstate);
     134             : 
     135      846572 :     free_parsestate(pstate);
     136             : 
     137      846572 :     pgstat_report_query_id(query->queryId, false);
     138             : 
     139      846572 :     return query;
     140             : }
     141             : 
     142             : /*
     143             :  * parse_analyze_varparams
     144             :  *
     145             :  * This variant is used when it's okay to deduce information about $n
     146             :  * symbol datatypes from context.  The passed-in paramTypes[] array can
     147             :  * be modified or enlarged (via repalloc).
     148             :  */
     149             : Query *
     150        9016 : parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
     151             :                         Oid **paramTypes, int *numParams)
     152             : {
     153        9016 :     ParseState *pstate = make_parsestate(NULL);
     154             :     Query      *query;
     155        9016 :     JumbleState *jstate = NULL;
     156             : 
     157             :     Assert(sourceText != NULL); /* required as of 8.4 */
     158             : 
     159        9016 :     pstate->p_sourcetext = sourceText;
     160             : 
     161        9016 :     parse_variable_parameters(pstate, paramTypes, numParams);
     162             : 
     163        9016 :     query = transformTopLevelStmt(pstate, parseTree);
     164             : 
     165             :     /* make sure all is well with parameter types */
     166        9010 :     check_variable_parameters(pstate, query);
     167             : 
     168        9010 :     if (IsQueryIdEnabled())
     169           4 :         jstate = JumbleQuery(query, sourceText);
     170             : 
     171        9010 :     if (post_parse_analyze_hook)
     172           4 :         (*post_parse_analyze_hook) (pstate, query, jstate);
     173             : 
     174        9010 :     free_parsestate(pstate);
     175             : 
     176        9010 :     pgstat_report_query_id(query->queryId, false);
     177             : 
     178        9010 :     return query;
     179             : }
     180             : 
     181             : /*
     182             :  * parse_sub_analyze
     183             :  *      Entry point for recursively analyzing a sub-statement.
     184             :  */
     185             : Query *
     186      193228 : parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
     187             :                   CommonTableExpr *parentCTE,
     188             :                   bool locked_from_parent,
     189             :                   bool resolve_unknowns)
     190             : {
     191      193228 :     ParseState *pstate = make_parsestate(parentParseState);
     192             :     Query      *query;
     193             : 
     194      193228 :     pstate->p_parent_cte = parentCTE;
     195      193228 :     pstate->p_locked_from_parent = locked_from_parent;
     196      193228 :     pstate->p_resolve_unknowns = resolve_unknowns;
     197             : 
     198      193228 :     query = transformStmt(pstate, parseTree);
     199             : 
     200      193094 :     free_parsestate(pstate);
     201             : 
     202      193094 :     return query;
     203             : }
     204             : 
     205             : /*
     206             :  * transformTopLevelStmt -
     207             :  *    transform a Parse tree into a Query tree.
     208             :  *
     209             :  * This function is just responsible for transferring statement location data
     210             :  * from the RawStmt into the finished Query.
     211             :  */
     212             : Query *
     213      901856 : transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
     214             : {
     215             :     Query      *result;
     216             : 
     217             :     /* We're at top level, so allow SELECT INTO */
     218      901856 :     result = transformOptionalSelectInto(pstate, parseTree->stmt);
     219             : 
     220      898118 :     result->stmt_location = parseTree->stmt_location;
     221      898118 :     result->stmt_len = parseTree->stmt_len;
     222             : 
     223      898118 :     return result;
     224             : }
     225             : 
     226             : /*
     227             :  * transformOptionalSelectInto -
     228             :  *    If SELECT has INTO, convert it to CREATE TABLE AS.
     229             :  *
     230             :  * The only thing we do here that we don't do in transformStmt() is to
     231             :  * convert SELECT ... INTO into CREATE TABLE AS.  Since utility statements
     232             :  * aren't allowed within larger statements, this is only allowed at the top
     233             :  * of the parse tree, and so we only try it before entering the recursive
     234             :  * transformStmt() processing.
     235             :  */
     236             : static Query *
     237      915268 : transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
     238             : {
     239      915268 :     if (IsA(parseTree, SelectStmt))
     240             :     {
     241      315544 :         SelectStmt *stmt = (SelectStmt *) parseTree;
     242             : 
     243             :         /* If it's a set-operation tree, drill down to leftmost SelectStmt */
     244      328606 :         while (stmt && stmt->op != SETOP_NONE)
     245       13062 :             stmt = stmt->larg;
     246             :         Assert(stmt && IsA(stmt, SelectStmt) && stmt->larg == NULL);
     247             : 
     248      315544 :         if (stmt->intoClause)
     249             :         {
     250          74 :             CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
     251             : 
     252          74 :             ctas->query = parseTree;
     253          74 :             ctas->into = stmt->intoClause;
     254          74 :             ctas->objtype = OBJECT_TABLE;
     255          74 :             ctas->is_select_into = true;
     256             : 
     257             :             /*
     258             :              * Remove the intoClause from the SelectStmt.  This makes it safe
     259             :              * for transformSelectStmt to complain if it finds intoClause set
     260             :              * (implying that the INTO appeared in a disallowed place).
     261             :              */
     262          74 :             stmt->intoClause = NULL;
     263             : 
     264          74 :             parseTree = (Node *) ctas;
     265             :         }
     266             :     }
     267             : 
     268      915268 :     return transformStmt(pstate, parseTree);
     269             : }
     270             : 
     271             : /*
     272             :  * transformStmt -
     273             :  *    recursively transform a Parse tree into a Query tree.
     274             :  */
     275             : Query *
     276     1149116 : transformStmt(ParseState *pstate, Node *parseTree)
     277             : {
     278             :     Query      *result;
     279             : 
     280             :     /*
     281             :      * We apply RAW_EXPRESSION_COVERAGE_TEST testing to basic DML statements;
     282             :      * we can't just run it on everything because raw_expression_tree_walker()
     283             :      * doesn't claim to handle utility statements.
     284             :      */
     285             : #ifdef RAW_EXPRESSION_COVERAGE_TEST
     286     1149116 :     switch (nodeTag(parseTree))
     287             :     {
     288      601780 :         case T_SelectStmt:
     289             :         case T_InsertStmt:
     290             :         case T_UpdateStmt:
     291             :         case T_DeleteStmt:
     292      601780 :             (void) test_raw_expression_coverage(parseTree, NULL);
     293      601780 :             break;
     294      547336 :         default:
     295      547336 :             break;
     296             :     }
     297             : #endif                          /* RAW_EXPRESSION_COVERAGE_TEST */
     298             : 
     299     1149116 :     switch (nodeTag(parseTree))
     300             :     {
     301             :             /*
     302             :              * Optimizable statements
     303             :              */
     304       63068 :         case T_InsertStmt:
     305       63068 :             result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
     306       62190 :             break;
     307             : 
     308        2746 :         case T_DeleteStmt:
     309        2746 :             result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
     310        2710 :             break;
     311             : 
     312       11060 :         case T_UpdateStmt:
     313       11060 :             result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
     314       11006 :             break;
     315             : 
     316      524906 :         case T_SelectStmt:
     317             :             {
     318      524906 :                 SelectStmt *n = (SelectStmt *) parseTree;
     319             : 
     320      524906 :                 if (n->valuesLists)
     321        3302 :                     result = transformValuesClause(pstate, n);
     322      521604 :                 else if (n->op == SETOP_NONE)
     323      507696 :                     result = transformSelectStmt(pstate, n);
     324             :                 else
     325       13908 :                     result = transformSetOperationStmt(pstate, n);
     326             :             }
     327      522036 :             break;
     328             : 
     329       23654 :         case T_ReturnStmt:
     330       23654 :             result = transformReturnStmt(pstate, (ReturnStmt *) parseTree);
     331       23650 :             break;
     332             : 
     333        3718 :         case T_PLAssignStmt:
     334        3718 :             result = transformPLAssignStmt(pstate,
     335             :                                            (PLAssignStmt *) parseTree);
     336        3692 :             break;
     337             : 
     338             :             /*
     339             :              * Special cases
     340             :              */
     341        2398 :         case T_DeclareCursorStmt:
     342        2398 :             result = transformDeclareCursorStmt(pstate,
     343             :                                                 (DeclareCursorStmt *) parseTree);
     344        2382 :             break;
     345             : 
     346       13412 :         case T_ExplainStmt:
     347       13412 :             result = transformExplainStmt(pstate,
     348             :                                           (ExplainStmt *) parseTree);
     349       13408 :             break;
     350             : 
     351        1224 :         case T_CreateTableAsStmt:
     352        1224 :             result = transformCreateTableAsStmt(pstate,
     353             :                                                 (CreateTableAsStmt *) parseTree);
     354        1224 :             break;
     355             : 
     356         326 :         case T_CallStmt:
     357         326 :             result = transformCallStmt(pstate,
     358             :                                        (CallStmt *) parseTree);
     359         306 :             break;
     360             : 
     361      502604 :         default:
     362             : 
     363             :             /*
     364             :              * other statements don't require any transformation; just return
     365             :              * the original parsetree with a Query node plastered on top.
     366             :              */
     367      502604 :             result = makeNode(Query);
     368      502604 :             result->commandType = CMD_UTILITY;
     369      502604 :             result->utilityStmt = (Node *) parseTree;
     370      502604 :             break;
     371             :     }
     372             : 
     373             :     /* Mark as original query until we learn differently */
     374     1145208 :     result->querySource = QSRC_ORIGINAL;
     375     1145208 :     result->canSetTag = true;
     376             : 
     377     1145208 :     return result;
     378             : }
     379             : 
     380             : /*
     381             :  * analyze_requires_snapshot
     382             :  *      Returns true if a snapshot must be set before doing parse analysis
     383             :  *      on the given raw parse tree.
     384             :  *
     385             :  * Classification here should match transformStmt().
     386             :  */
     387             : bool
     388      764416 : analyze_requires_snapshot(RawStmt *parseTree)
     389             : {
     390             :     bool        result;
     391             : 
     392      764416 :     switch (nodeTag(parseTree->stmt))
     393             :     {
     394             :             /*
     395             :              * Optimizable statements
     396             :              */
     397      258824 :         case T_InsertStmt:
     398             :         case T_DeleteStmt:
     399             :         case T_UpdateStmt:
     400             :         case T_SelectStmt:
     401             :         case T_PLAssignStmt:
     402      258824 :             result = true;
     403      258824 :             break;
     404             : 
     405             :             /*
     406             :              * Special cases
     407             :              */
     408       12288 :         case T_DeclareCursorStmt:
     409             :         case T_ExplainStmt:
     410             :         case T_CreateTableAsStmt:
     411             :             /* yes, because we must analyze the contained statement */
     412       12288 :             result = true;
     413       12288 :             break;
     414             : 
     415      493304 :         default:
     416             :             /* other utility statements don't have any real parse analysis */
     417      493304 :             result = false;
     418      493304 :             break;
     419             :     }
     420             : 
     421      764416 :     return result;
     422             : }
     423             : 
     424             : /*
     425             :  * transformDeleteStmt -
     426             :  *    transforms a Delete Statement
     427             :  */
     428             : static Query *
     429        2746 : transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
     430             : {
     431        2746 :     Query      *qry = makeNode(Query);
     432             :     ParseNamespaceItem *nsitem;
     433             :     Node       *qual;
     434             : 
     435        2746 :     qry->commandType = CMD_DELETE;
     436             : 
     437             :     /* process the WITH clause independently of all else */
     438        2746 :     if (stmt->withClause)
     439             :     {
     440          20 :         qry->hasRecursive = stmt->withClause->recursive;
     441          20 :         qry->cteList = transformWithClause(pstate, stmt->withClause);
     442          20 :         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
     443             :     }
     444             : 
     445             :     /* set up range table with just the result rel */
     446        5488 :     qry->resultRelation = setTargetTable(pstate, stmt->relation,
     447        2746 :                                          stmt->relation->inh,
     448             :                                          true,
     449             :                                          ACL_DELETE);
     450        2742 :     nsitem = pstate->p_target_nsitem;
     451             : 
     452             :     /* there's no DISTINCT in DELETE */
     453        2742 :     qry->distinctClause = NIL;
     454             : 
     455             :     /* subqueries in USING cannot access the result relation */
     456        2742 :     nsitem->p_lateral_only = true;
     457        2742 :     nsitem->p_lateral_ok = false;
     458             : 
     459             :     /*
     460             :      * The USING clause is non-standard SQL syntax, and is equivalent in
     461             :      * functionality to the FROM list that can be specified for UPDATE. The
     462             :      * USING keyword is used rather than FROM because FROM is already a
     463             :      * keyword in the DELETE syntax.
     464             :      */
     465        2742 :     transformFromClause(pstate, stmt->usingClause);
     466             : 
     467             :     /* remaining clauses can reference the result relation normally */
     468        2730 :     nsitem->p_lateral_only = false;
     469        2730 :     nsitem->p_lateral_ok = true;
     470             : 
     471        2730 :     qual = transformWhereClause(pstate, stmt->whereClause,
     472             :                                 EXPR_KIND_WHERE, "WHERE");
     473             : 
     474        2714 :     qry->returningList = transformReturningList(pstate, stmt->returningList);
     475             : 
     476             :     /* done building the range table and jointree */
     477        2710 :     qry->rtable = pstate->p_rtable;
     478        2710 :     qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
     479             : 
     480        2710 :     qry->hasSubLinks = pstate->p_hasSubLinks;
     481        2710 :     qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
     482        2710 :     qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
     483        2710 :     qry->hasAggs = pstate->p_hasAggs;
     484             : 
     485        2710 :     assign_query_collations(pstate, qry);
     486             : 
     487             :     /* this must be done after collations, for reliable comparison of exprs */
     488        2710 :     if (pstate->p_hasAggs)
     489           0 :         parseCheckAggregates(pstate, qry);
     490             : 
     491        2710 :     return qry;
     492             : }
     493             : 
     494             : /*
     495             :  * transformInsertStmt -
     496             :  *    transform an Insert Statement
     497             :  */
     498             : static Query *
     499       63068 : transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
     500             : {
     501       63068 :     Query      *qry = makeNode(Query);
     502       63068 :     SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
     503       63068 :     List       *exprList = NIL;
     504             :     bool        isGeneralSelect;
     505             :     List       *sub_rtable;
     506             :     List       *sub_namespace;
     507             :     List       *icolumns;
     508             :     List       *attrnos;
     509             :     ParseNamespaceItem *nsitem;
     510             :     RangeTblEntry *rte;
     511             :     ListCell   *icols;
     512             :     ListCell   *attnos;
     513             :     ListCell   *lc;
     514             :     bool        isOnConflictUpdate;
     515             :     AclMode     targetPerms;
     516             : 
     517             :     /* There can't be any outer WITH to worry about */
     518             :     Assert(pstate->p_ctenamespace == NIL);
     519             : 
     520       63068 :     qry->commandType = CMD_INSERT;
     521       63068 :     pstate->p_is_insert = true;
     522             : 
     523             :     /* process the WITH clause independently of all else */
     524       63068 :     if (stmt->withClause)
     525             :     {
     526         594 :         qry->hasRecursive = stmt->withClause->recursive;
     527         594 :         qry->cteList = transformWithClause(pstate, stmt->withClause);
     528         594 :         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
     529             :     }
     530             : 
     531       63068 :     qry->override = stmt->override;
     532             : 
     533       64092 :     isOnConflictUpdate = (stmt->onConflictClause &&
     534        1024 :                           stmt->onConflictClause->action == ONCONFLICT_UPDATE);
     535             : 
     536             :     /*
     537             :      * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
     538             :      * VALUES list, or general SELECT input.  We special-case VALUES, both for
     539             :      * efficiency and so we can handle DEFAULT specifications.
     540             :      *
     541             :      * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
     542             :      * VALUES clause.  If we have any of those, treat it as a general SELECT;
     543             :      * so it will work, but you can't use DEFAULT items together with those.
     544             :      */
     545      116764 :     isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
     546       53696 :                                       selectStmt->sortClause != NIL ||
     547       53696 :                                       selectStmt->limitOffset != NULL ||
     548       53696 :                                       selectStmt->limitCount != NULL ||
     549       53696 :                                       selectStmt->lockingClause != NIL ||
     550       53696 :                                       selectStmt->withClause != NULL));
     551             : 
     552             :     /*
     553             :      * If a non-nil rangetable/namespace was passed in, and we are doing
     554             :      * INSERT/SELECT, arrange to pass the rangetable/namespace down to the
     555             :      * SELECT.  This can only happen if we are inside a CREATE RULE, and in
     556             :      * that case we want the rule's OLD and NEW rtable entries to appear as
     557             :      * part of the SELECT's rtable, not as outer references for it.  (Kluge!)
     558             :      * The SELECT's joinlist is not affected however.  We must do this before
     559             :      * adding the target table to the INSERT's rtable.
     560             :      */
     561       63068 :     if (isGeneralSelect)
     562             :     {
     563        8910 :         sub_rtable = pstate->p_rtable;
     564        8910 :         pstate->p_rtable = NIL;
     565        8910 :         sub_namespace = pstate->p_namespace;
     566        8910 :         pstate->p_namespace = NIL;
     567             :     }
     568             :     else
     569             :     {
     570       54158 :         sub_rtable = NIL;       /* not used, but keep compiler quiet */
     571       54158 :         sub_namespace = NIL;
     572             :     }
     573             : 
     574             :     /*
     575             :      * Must get write lock on INSERT target table before scanning SELECT, else
     576             :      * we will grab the wrong kind of initial lock if the target table is also
     577             :      * mentioned in the SELECT part.  Note that the target table is not added
     578             :      * to the joinlist or namespace.
     579             :      */
     580       63068 :     targetPerms = ACL_INSERT;
     581       63068 :     if (isOnConflictUpdate)
     582         780 :         targetPerms |= ACL_UPDATE;
     583       63068 :     qry->resultRelation = setTargetTable(pstate, stmt->relation,
     584             :                                          false, false, targetPerms);
     585             : 
     586             :     /* Validate stmt->cols list, or build default list if no list given */
     587       63056 :     icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
     588             :     Assert(list_length(icolumns) == list_length(attrnos));
     589             : 
     590             :     /*
     591             :      * Determine which variant of INSERT we have.
     592             :      */
     593       63024 :     if (selectStmt == NULL)
     594             :     {
     595             :         /*
     596             :          * We have INSERT ... DEFAULT VALUES.  We can handle this case by
     597             :          * emitting an empty targetlist --- all columns will be defaulted when
     598             :          * the planner expands the targetlist.
     599             :          */
     600         462 :         exprList = NIL;
     601             :     }
     602       62562 :     else if (isGeneralSelect)
     603             :     {
     604             :         /*
     605             :          * We make the sub-pstate a child of the outer pstate so that it can
     606             :          * see any Param definitions supplied from above.  Since the outer
     607             :          * pstate's rtable and namespace are presently empty, there are no
     608             :          * side-effects of exposing names the sub-SELECT shouldn't be able to
     609             :          * see.
     610             :          */
     611        8910 :         ParseState *sub_pstate = make_parsestate(pstate);
     612             :         Query      *selectQuery;
     613             : 
     614             :         /*
     615             :          * Process the source SELECT.
     616             :          *
     617             :          * It is important that this be handled just like a standalone SELECT;
     618             :          * otherwise the behavior of SELECT within INSERT might be different
     619             :          * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
     620             :          * bugs of just that nature...)
     621             :          *
     622             :          * The sole exception is that we prevent resolving unknown-type
     623             :          * outputs as TEXT.  This does not change the semantics since if the
     624             :          * column type matters semantically, it would have been resolved to
     625             :          * something else anyway.  Doing this lets us resolve such outputs as
     626             :          * the target column's type, which we handle below.
     627             :          */
     628        8910 :         sub_pstate->p_rtable = sub_rtable;
     629        8910 :         sub_pstate->p_joinexprs = NIL;   /* sub_rtable has no joins */
     630        8910 :         sub_pstate->p_namespace = sub_namespace;
     631        8910 :         sub_pstate->p_resolve_unknowns = false;
     632             : 
     633        8910 :         selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
     634             : 
     635        8906 :         free_parsestate(sub_pstate);
     636             : 
     637             :         /* The grammar should have produced a SELECT */
     638        8906 :         if (!IsA(selectQuery, Query) ||
     639        8906 :             selectQuery->commandType != CMD_SELECT)
     640           0 :             elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
     641             : 
     642             :         /*
     643             :          * Make the source be a subquery in the INSERT's rangetable, and add
     644             :          * it to the INSERT's joinlist (but not the namespace).
     645             :          */
     646        8906 :         nsitem = addRangeTableEntryForSubquery(pstate,
     647             :                                                selectQuery,
     648             :                                                makeAlias("*SELECT*", NIL),
     649             :                                                false,
     650             :                                                false);
     651        8906 :         addNSItemToQuery(pstate, nsitem, true, false, false);
     652             : 
     653             :         /*----------
     654             :          * Generate an expression list for the INSERT that selects all the
     655             :          * non-resjunk columns from the subquery.  (INSERT's tlist must be
     656             :          * separate from the subquery's tlist because we may add columns,
     657             :          * insert datatype coercions, etc.)
     658             :          *
     659             :          * HACK: unknown-type constants and params in the SELECT's targetlist
     660             :          * are copied up as-is rather than being referenced as subquery
     661             :          * outputs.  This is to ensure that when we try to coerce them to
     662             :          * the target column's datatype, the right things happen (see
     663             :          * special cases in coerce_type).  Otherwise, this fails:
     664             :          *      INSERT INTO foo SELECT 'bar', ... FROM baz
     665             :          *----------
     666             :          */
     667        8906 :         exprList = NIL;
     668       41484 :         foreach(lc, selectQuery->targetList)
     669             :         {
     670       32578 :             TargetEntry *tle = (TargetEntry *) lfirst(lc);
     671             :             Expr       *expr;
     672             : 
     673       32578 :             if (tle->resjunk)
     674          48 :                 continue;
     675       32530 :             if (tle->expr &&
     676       43460 :                 (IsA(tle->expr, Const) || IsA(tle->expr, Param)) &&
     677       10930 :                 exprType((Node *) tle->expr) == UNKNOWNOID)
     678        4644 :                 expr = tle->expr;
     679             :             else
     680             :             {
     681       27886 :                 Var        *var = makeVarFromTargetEntry(nsitem->p_rtindex, tle);
     682             : 
     683       27886 :                 var->location = exprLocation((Node *) tle->expr);
     684       27886 :                 expr = (Expr *) var;
     685             :             }
     686       32530 :             exprList = lappend(exprList, expr);
     687             :         }
     688             : 
     689             :         /* Prepare row for assignment to target table */
     690        8906 :         exprList = transformInsertRow(pstate, exprList,
     691             :                                       stmt->cols,
     692             :                                       icolumns, attrnos,
     693             :                                       false);
     694             :     }
     695       53652 :     else if (list_length(selectStmt->valuesLists) > 1)
     696             :     {
     697             :         /*
     698             :          * Process INSERT ... VALUES with multiple VALUES sublists. We
     699             :          * generate a VALUES RTE holding the transformed expression lists, and
     700             :          * build up a targetlist containing Vars that reference the VALUES
     701             :          * RTE.
     702             :          */
     703        2728 :         List       *exprsLists = NIL;
     704        2728 :         List       *coltypes = NIL;
     705        2728 :         List       *coltypmods = NIL;
     706        2728 :         List       *colcollations = NIL;
     707        2728 :         int         sublist_length = -1;
     708        2728 :         bool        lateral = false;
     709             : 
     710             :         Assert(selectStmt->intoClause == NULL);
     711             : 
     712      151812 :         foreach(lc, selectStmt->valuesLists)
     713             :         {
     714      149084 :             List       *sublist = (List *) lfirst(lc);
     715             : 
     716             :             /*
     717             :              * Do basic expression transformation (same as a ROW() expr, but
     718             :              * allow SetToDefault at top level)
     719             :              */
     720      149084 :             sublist = transformExpressionList(pstate, sublist,
     721             :                                               EXPR_KIND_VALUES, true);
     722             : 
     723             :             /*
     724             :              * All the sublists must be the same length, *after*
     725             :              * transformation (which might expand '*' into multiple items).
     726             :              * The VALUES RTE can't handle anything different.
     727             :              */
     728      149084 :             if (sublist_length < 0)
     729             :             {
     730             :                 /* Remember post-transformation length of first sublist */
     731        2728 :                 sublist_length = list_length(sublist);
     732             :             }
     733      146356 :             else if (sublist_length != list_length(sublist))
     734             :             {
     735           0 :                 ereport(ERROR,
     736             :                         (errcode(ERRCODE_SYNTAX_ERROR),
     737             :                          errmsg("VALUES lists must all be the same length"),
     738             :                          parser_errposition(pstate,
     739             :                                             exprLocation((Node *) sublist))));
     740             :             }
     741             : 
     742             :             /*
     743             :              * Prepare row for assignment to target table.  We process any
     744             :              * indirection on the target column specs normally but then strip
     745             :              * off the resulting field/array assignment nodes, since we don't
     746             :              * want the parsed statement to contain copies of those in each
     747             :              * VALUES row.  (It's annoying to have to transform the
     748             :              * indirection specs over and over like this, but avoiding it
     749             :              * would take some really messy refactoring of
     750             :              * transformAssignmentIndirection.)
     751             :              */
     752      149084 :             sublist = transformInsertRow(pstate, sublist,
     753             :                                          stmt->cols,
     754             :                                          icolumns, attrnos,
     755             :                                          true);
     756             : 
     757             :             /*
     758             :              * We must assign collations now because assign_query_collations
     759             :              * doesn't process rangetable entries.  We just assign all the
     760             :              * collations independently in each row, and don't worry about
     761             :              * whether they are consistent vertically.  The outer INSERT query
     762             :              * isn't going to care about the collations of the VALUES columns,
     763             :              * so it's not worth the effort to identify a common collation for
     764             :              * each one here.  (But note this does have one user-visible
     765             :              * consequence: INSERT ... VALUES won't complain about conflicting
     766             :              * explicit COLLATEs in a column, whereas the same VALUES
     767             :              * construct in another context would complain.)
     768             :              */
     769      149084 :             assign_list_collations(pstate, sublist);
     770             : 
     771      149084 :             exprsLists = lappend(exprsLists, sublist);
     772             :         }
     773             : 
     774             :         /*
     775             :          * Construct column type/typmod/collation lists for the VALUES RTE.
     776             :          * Every expression in each column has been coerced to the type/typmod
     777             :          * of the corresponding target column or subfield, so it's sufficient
     778             :          * to look at the exprType/exprTypmod of the first row.  We don't care
     779             :          * about the collation labeling, so just fill in InvalidOid for that.
     780             :          */
     781        7438 :         foreach(lc, (List *) linitial(exprsLists))
     782             :         {
     783        4710 :             Node       *val = (Node *) lfirst(lc);
     784             : 
     785        4710 :             coltypes = lappend_oid(coltypes, exprType(val));
     786        4710 :             coltypmods = lappend_int(coltypmods, exprTypmod(val));
     787        4710 :             colcollations = lappend_oid(colcollations, InvalidOid);
     788             :         }
     789             : 
     790             :         /*
     791             :          * Ordinarily there can't be any current-level Vars in the expression
     792             :          * lists, because the namespace was empty ... but if we're inside
     793             :          * CREATE RULE, then NEW/OLD references might appear.  In that case we
     794             :          * have to mark the VALUES RTE as LATERAL.
     795             :          */
     796        2738 :         if (list_length(pstate->p_rtable) != 1 &&
     797          10 :             contain_vars_of_level((Node *) exprsLists, 0))
     798          10 :             lateral = true;
     799             : 
     800             :         /*
     801             :          * Generate the VALUES RTE
     802             :          */
     803        2728 :         nsitem = addRangeTableEntryForValues(pstate, exprsLists,
     804             :                                              coltypes, coltypmods, colcollations,
     805             :                                              NULL, lateral, true);
     806        2728 :         addNSItemToQuery(pstate, nsitem, true, false, false);
     807             : 
     808             :         /*
     809             :          * Generate list of Vars referencing the RTE
     810             :          */
     811        2728 :         exprList = expandNSItemVars(nsitem, 0, -1, NULL);
     812             : 
     813             :         /*
     814             :          * Re-apply any indirection on the target column specs to the Vars
     815             :          */
     816        2728 :         exprList = transformInsertRow(pstate, exprList,
     817             :                                       stmt->cols,
     818             :                                       icolumns, attrnos,
     819             :                                       false);
     820             :     }
     821             :     else
     822             :     {
     823             :         /*
     824             :          * Process INSERT ... VALUES with a single VALUES sublist.  We treat
     825             :          * this case separately for efficiency.  The sublist is just computed
     826             :          * directly as the Query's targetlist, with no VALUES RTE.  So it
     827             :          * works just like a SELECT without any FROM.
     828             :          */
     829       50924 :         List       *valuesLists = selectStmt->valuesLists;
     830             : 
     831             :         Assert(list_length(valuesLists) == 1);
     832             :         Assert(selectStmt->intoClause == NULL);
     833             : 
     834             :         /*
     835             :          * Do basic expression transformation (same as a ROW() expr, but allow
     836             :          * SetToDefault at top level)
     837             :          */
     838       50924 :         exprList = transformExpressionList(pstate,
     839       50924 :                                            (List *) linitial(valuesLists),
     840             :                                            EXPR_KIND_VALUES_SINGLE,
     841             :                                            true);
     842             : 
     843             :         /* Prepare row for assignment to target table */
     844       50908 :         exprList = transformInsertRow(pstate, exprList,
     845             :                                       stmt->cols,
     846             :                                       icolumns, attrnos,
     847             :                                       false);
     848             :     }
     849             : 
     850             :     /*
     851             :      * Generate query's target list using the computed list of expressions.
     852             :      * Also, mark all the target columns as needing insert permissions.
     853             :      */
     854       62226 :     rte = pstate->p_target_nsitem->p_rte;
     855       62226 :     qry->targetList = NIL;
     856             :     Assert(list_length(exprList) <= list_length(icolumns));
     857      255248 :     forthree(lc, exprList, icols, icolumns, attnos, attrnos)
     858             :     {
     859      193022 :         Expr       *expr = (Expr *) lfirst(lc);
     860      193022 :         ResTarget  *col = lfirst_node(ResTarget, icols);
     861      193022 :         AttrNumber  attr_num = (AttrNumber) lfirst_int(attnos);
     862             :         TargetEntry *tle;
     863             : 
     864      193022 :         tle = makeTargetEntry(expr,
     865             :                               attr_num,
     866             :                               col->name,
     867             :                               false);
     868      193022 :         qry->targetList = lappend(qry->targetList, tle);
     869             : 
     870      193022 :         rte->insertedCols = bms_add_member(rte->insertedCols,
     871             :                                            attr_num - FirstLowInvalidHeapAttributeNumber);
     872             :     }
     873             : 
     874             :     /*
     875             :      * If we have any clauses yet to process, set the query namespace to
     876             :      * contain only the target relation, removing any entries added in a
     877             :      * sub-SELECT or VALUES list.
     878             :      */
     879       62226 :     if (stmt->onConflictClause || stmt->returningList)
     880             :     {
     881        1678 :         pstate->p_namespace = NIL;
     882        1678 :         addNSItemToQuery(pstate, pstate->p_target_nsitem,
     883             :                          false, true, true);
     884             :     }
     885             : 
     886             :     /* Process ON CONFLICT, if any. */
     887       62226 :     if (stmt->onConflictClause)
     888        1024 :         qry->onConflict = transformOnConflictClause(pstate,
     889             :                                                     stmt->onConflictClause);
     890             : 
     891             :     /* Process RETURNING, if any. */
     892       62202 :     if (stmt->returningList)
     893         842 :         qry->returningList = transformReturningList(pstate,
     894             :                                                     stmt->returningList);
     895             : 
     896             :     /* done building the range table and jointree */
     897       62190 :     qry->rtable = pstate->p_rtable;
     898       62190 :     qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
     899             : 
     900       62190 :     qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
     901       62190 :     qry->hasSubLinks = pstate->p_hasSubLinks;
     902             : 
     903       62190 :     assign_query_collations(pstate, qry);
     904             : 
     905       62190 :     return qry;
     906             : }
     907             : 
     908             : /*
     909             :  * Prepare an INSERT row for assignment to the target table.
     910             :  *
     911             :  * exprlist: transformed expressions for source values; these might come from
     912             :  * a VALUES row, or be Vars referencing a sub-SELECT or VALUES RTE output.
     913             :  * stmtcols: original target-columns spec for INSERT (we just test for NIL)
     914             :  * icolumns: effective target-columns spec (list of ResTarget)
     915             :  * attrnos: integer column numbers (must be same length as icolumns)
     916             :  * strip_indirection: if true, remove any field/array assignment nodes
     917             :  */
     918             : static List *
     919      211626 : transformInsertRow(ParseState *pstate, List *exprlist,
     920             :                    List *stmtcols, List *icolumns, List *attrnos,
     921             :                    bool strip_indirection)
     922             : {
     923             :     List       *result;
     924             :     ListCell   *lc;
     925             :     ListCell   *icols;
     926             :     ListCell   *attnos;
     927             : 
     928             :     /*
     929             :      * Check length of expr list.  It must not have more expressions than
     930             :      * there are target columns.  We allow fewer, but only if no explicit
     931             :      * columns list was given (the remaining columns are implicitly
     932             :      * defaulted).  Note we must check this *after* transformation because
     933             :      * that could expand '*' into multiple items.
     934             :      */
     935      211626 :     if (list_length(exprlist) > list_length(icolumns))
     936          16 :         ereport(ERROR,
     937             :                 (errcode(ERRCODE_SYNTAX_ERROR),
     938             :                  errmsg("INSERT has more expressions than target columns"),
     939             :                  parser_errposition(pstate,
     940             :                                     exprLocation(list_nth(exprlist,
     941             :                                                           list_length(icolumns))))));
     942      365626 :     if (stmtcols != NIL &&
     943      154016 :         list_length(exprlist) < list_length(icolumns))
     944             :     {
     945             :         /*
     946             :          * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
     947             :          * where the user accidentally created a RowExpr instead of separate
     948             :          * columns.  Add a suitable hint if that seems to be the problem,
     949             :          * because the main error message is quite misleading for this case.
     950             :          * (If there's no stmtcols, you'll get something about data type
     951             :          * mismatch, which is less misleading so we don't worry about giving a
     952             :          * hint in that case.)
     953             :          */
     954           8 :         ereport(ERROR,
     955             :                 (errcode(ERRCODE_SYNTAX_ERROR),
     956             :                  errmsg("INSERT has more target columns than expressions"),
     957             :                  ((list_length(exprlist) == 1 &&
     958             :                    count_rowexpr_columns(pstate, linitial(exprlist)) ==
     959             :                    list_length(icolumns)) ?
     960             :                   errhint("The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?") : 0),
     961             :                  parser_errposition(pstate,
     962             :                                     exprLocation(list_nth(icolumns,
     963             :                                                           list_length(exprlist))))));
     964             :     }
     965             : 
     966             :     /*
     967             :      * Prepare columns for assignment to target table.
     968             :      */
     969      211602 :     result = NIL;
     970      562006 :     forthree(lc, exprlist, icols, icolumns, attnos, attrnos)
     971             :     {
     972      351158 :         Expr       *expr = (Expr *) lfirst(lc);
     973      351158 :         ResTarget  *col = lfirst_node(ResTarget, icols);
     974      351158 :         int         attno = lfirst_int(attnos);
     975             : 
     976      351158 :         expr = transformAssignedExpr(pstate, expr,
     977             :                                      EXPR_KIND_INSERT_TARGET,
     978      351158 :                                      col->name,
     979             :                                      attno,
     980             :                                      col->indirection,
     981             :                                      col->location);
     982             : 
     983      350404 :         if (strip_indirection)
     984             :         {
     985      157550 :             while (expr)
     986             :             {
     987      157550 :                 if (IsA(expr, FieldStore))
     988             :                 {
     989          64 :                     FieldStore *fstore = (FieldStore *) expr;
     990             : 
     991          64 :                     expr = (Expr *) linitial(fstore->newvals);
     992             :                 }
     993      157486 :                 else if (IsA(expr, SubscriptingRef))
     994             :                 {
     995         128 :                     SubscriptingRef *sbsref = (SubscriptingRef *) expr;
     996             : 
     997         128 :                     if (sbsref->refassgnexpr == NULL)
     998           0 :                         break;
     999             : 
    1000         128 :                     expr = sbsref->refassgnexpr;
    1001             :                 }
    1002             :                 else
    1003      157358 :                     break;
    1004             :             }
    1005             :         }
    1006             : 
    1007      350404 :         result = lappend(result, expr);
    1008             :     }
    1009             : 
    1010      210848 :     return result;
    1011             : }
    1012             : 
    1013             : /*
    1014             :  * transformOnConflictClause -
    1015             :  *    transforms an OnConflictClause in an INSERT
    1016             :  */
    1017             : static OnConflictExpr *
    1018        1024 : transformOnConflictClause(ParseState *pstate,
    1019             :                           OnConflictClause *onConflictClause)
    1020             : {
    1021        1024 :     ParseNamespaceItem *exclNSItem = NULL;
    1022             :     List       *arbiterElems;
    1023             :     Node       *arbiterWhere;
    1024             :     Oid         arbiterConstraint;
    1025        1024 :     List       *onConflictSet = NIL;
    1026        1024 :     Node       *onConflictWhere = NULL;
    1027        1024 :     int         exclRelIndex = 0;
    1028        1024 :     List       *exclRelTlist = NIL;
    1029             :     OnConflictExpr *result;
    1030             : 
    1031             :     /*
    1032             :      * If this is ON CONFLICT ... UPDATE, first create the range table entry
    1033             :      * for the EXCLUDED pseudo relation, so that that will be present while
    1034             :      * processing arbiter expressions.  (You can't actually reference it from
    1035             :      * there, but this provides a useful error message if you try.)
    1036             :      */
    1037        1024 :     if (onConflictClause->action == ONCONFLICT_UPDATE)
    1038             :     {
    1039         780 :         Relation    targetrel = pstate->p_target_relation;
    1040             :         RangeTblEntry *exclRte;
    1041             : 
    1042         780 :         exclNSItem = addRangeTableEntryForRelation(pstate,
    1043             :                                                    targetrel,
    1044             :                                                    RowExclusiveLock,
    1045             :                                                    makeAlias("excluded", NIL),
    1046             :                                                    false, false);
    1047         780 :         exclRte = exclNSItem->p_rte;
    1048         780 :         exclRelIndex = exclNSItem->p_rtindex;
    1049             : 
    1050             :         /*
    1051             :          * relkind is set to composite to signal that we're not dealing with
    1052             :          * an actual relation, and no permission checks are required on it.
    1053             :          * (We'll check the actual target relation, instead.)
    1054             :          */
    1055         780 :         exclRte->relkind = RELKIND_COMPOSITE_TYPE;
    1056         780 :         exclRte->requiredPerms = 0;
    1057             :         /* other permissions fields in exclRte are already empty */
    1058             : 
    1059             :         /* Create EXCLUDED rel's targetlist for use by EXPLAIN */
    1060         780 :         exclRelTlist = BuildOnConflictExcludedTargetlist(targetrel,
    1061             :                                                          exclRelIndex);
    1062             :     }
    1063             : 
    1064             :     /* Process the arbiter clause, ON CONFLICT ON (...) */
    1065        1024 :     transformOnConflictArbiter(pstate, onConflictClause, &arbiterElems,
    1066             :                                &arbiterWhere, &arbiterConstraint);
    1067             : 
    1068             :     /* Process DO UPDATE */
    1069        1016 :     if (onConflictClause->action == ONCONFLICT_UPDATE)
    1070             :     {
    1071             :         /*
    1072             :          * Expressions in the UPDATE targetlist need to be handled like UPDATE
    1073             :          * not INSERT.  We don't need to save/restore this because all INSERT
    1074             :          * expressions have been parsed already.
    1075             :          */
    1076         772 :         pstate->p_is_insert = false;
    1077             : 
    1078             :         /*
    1079             :          * Add the EXCLUDED pseudo relation to the query namespace, making it
    1080             :          * available in the UPDATE subexpressions.
    1081             :          */
    1082         772 :         addNSItemToQuery(pstate, exclNSItem, false, true, true);
    1083             : 
    1084             :         /*
    1085             :          * Now transform the UPDATE subexpressions.
    1086             :          */
    1087             :         onConflictSet =
    1088         772 :             transformUpdateTargetList(pstate, onConflictClause->targetList);
    1089             : 
    1090         756 :         onConflictWhere = transformWhereClause(pstate,
    1091             :                                                onConflictClause->whereClause,
    1092             :                                                EXPR_KIND_WHERE, "WHERE");
    1093             : 
    1094             :         /*
    1095             :          * Remove the EXCLUDED pseudo relation from the query namespace, since
    1096             :          * it's not supposed to be available in RETURNING.  (Maybe someday we
    1097             :          * could allow that, and drop this step.)
    1098             :          */
    1099             :         Assert((ParseNamespaceItem *) llast(pstate->p_namespace) == exclNSItem);
    1100         756 :         pstate->p_namespace = list_delete_last(pstate->p_namespace);
    1101             :     }
    1102             : 
    1103             :     /* Finally, build ON CONFLICT DO [NOTHING | UPDATE] expression */
    1104        1000 :     result = makeNode(OnConflictExpr);
    1105             : 
    1106        1000 :     result->action = onConflictClause->action;
    1107        1000 :     result->arbiterElems = arbiterElems;
    1108        1000 :     result->arbiterWhere = arbiterWhere;
    1109        1000 :     result->constraint = arbiterConstraint;
    1110        1000 :     result->onConflictSet = onConflictSet;
    1111        1000 :     result->onConflictWhere = onConflictWhere;
    1112        1000 :     result->exclRelIndex = exclRelIndex;
    1113        1000 :     result->exclRelTlist = exclRelTlist;
    1114             : 
    1115        1000 :     return result;
    1116             : }
    1117             : 
    1118             : 
    1119             : /*
    1120             :  * BuildOnConflictExcludedTargetlist
    1121             :  *      Create target list for the EXCLUDED pseudo-relation of ON CONFLICT,
    1122             :  *      representing the columns of targetrel with varno exclRelIndex.
    1123             :  *
    1124             :  * Note: Exported for use in the rewriter.
    1125             :  */
    1126             : List *
    1127         876 : BuildOnConflictExcludedTargetlist(Relation targetrel,
    1128             :                                   Index exclRelIndex)
    1129             : {
    1130         876 :     List       *result = NIL;
    1131             :     int         attno;
    1132             :     Var        *var;
    1133             :     TargetEntry *te;
    1134             : 
    1135             :     /*
    1136             :      * Note that resnos of the tlist must correspond to attnos of the
    1137             :      * underlying relation, hence we need entries for dropped columns too.
    1138             :      */
    1139        3154 :     for (attno = 0; attno < RelationGetNumberOfAttributes(targetrel); attno++)
    1140             :     {
    1141        2278 :         Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, attno);
    1142             :         char       *name;
    1143             : 
    1144        2278 :         if (attr->attisdropped)
    1145             :         {
    1146             :             /*
    1147             :              * can't use atttypid here, but it doesn't really matter what type
    1148             :              * the Const claims to be.
    1149             :              */
    1150          44 :             var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
    1151          44 :             name = NULL;
    1152             :         }
    1153             :         else
    1154             :         {
    1155        2234 :             var = makeVar(exclRelIndex, attno + 1,
    1156             :                           attr->atttypid, attr->atttypmod,
    1157             :                           attr->attcollation,
    1158             :                           0);
    1159        2234 :             name = pstrdup(NameStr(attr->attname));
    1160             :         }
    1161             : 
    1162        2278 :         te = makeTargetEntry((Expr *) var,
    1163        2278 :                              attno + 1,
    1164             :                              name,
    1165             :                              false);
    1166             : 
    1167        2278 :         result = lappend(result, te);
    1168             :     }
    1169             : 
    1170             :     /*
    1171             :      * Add a whole-row-Var entry to support references to "EXCLUDED.*".  Like
    1172             :      * the other entries in the EXCLUDED tlist, its resno must match the Var's
    1173             :      * varattno, else the wrong things happen while resolving references in
    1174             :      * setrefs.c.  This is against normal conventions for targetlists, but
    1175             :      * it's okay since we don't use this as a real tlist.
    1176             :      */
    1177         876 :     var = makeVar(exclRelIndex, InvalidAttrNumber,
    1178         876 :                   targetrel->rd_rel->reltype,
    1179             :                   -1, InvalidOid, 0);
    1180         876 :     te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
    1181         876 :     result = lappend(result, te);
    1182             : 
    1183         876 :     return result;
    1184             : }
    1185             : 
    1186             : 
    1187             : /*
    1188             :  * count_rowexpr_columns -
    1189             :  *    get number of columns contained in a ROW() expression;
    1190             :  *    return -1 if expression isn't a RowExpr or a Var referencing one.
    1191             :  *
    1192             :  * This is currently used only for hint purposes, so we aren't terribly
    1193             :  * tense about recognizing all possible cases.  The Var case is interesting
    1194             :  * because that's what we'll get in the INSERT ... SELECT (...) case.
    1195             :  */
    1196             : static int
    1197           0 : count_rowexpr_columns(ParseState *pstate, Node *expr)
    1198             : {
    1199           0 :     if (expr == NULL)
    1200           0 :         return -1;
    1201           0 :     if (IsA(expr, RowExpr))
    1202           0 :         return list_length(((RowExpr *) expr)->args);
    1203           0 :     if (IsA(expr, Var))
    1204             :     {
    1205           0 :         Var        *var = (Var *) expr;
    1206           0 :         AttrNumber  attnum = var->varattno;
    1207             : 
    1208           0 :         if (attnum > 0 && var->vartype == RECORDOID)
    1209             :         {
    1210             :             RangeTblEntry *rte;
    1211             : 
    1212           0 :             rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
    1213           0 :             if (rte->rtekind == RTE_SUBQUERY)
    1214             :             {
    1215             :                 /* Subselect-in-FROM: examine sub-select's output expr */
    1216           0 :                 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
    1217             :                                                     attnum);
    1218             : 
    1219           0 :                 if (ste == NULL || ste->resjunk)
    1220           0 :                     return -1;
    1221           0 :                 expr = (Node *) ste->expr;
    1222           0 :                 if (IsA(expr, RowExpr))
    1223           0 :                     return list_length(((RowExpr *) expr)->args);
    1224             :             }
    1225             :         }
    1226             :     }
    1227           0 :     return -1;
    1228             : }
    1229             : 
    1230             : 
    1231             : /*
    1232             :  * transformSelectStmt -
    1233             :  *    transforms a Select Statement
    1234             :  *
    1235             :  * Note: this covers only cases with no set operations and no VALUES lists;
    1236             :  * see below for the other cases.
    1237             :  */
    1238             : static Query *
    1239      507696 : transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
    1240             : {
    1241      507696 :     Query      *qry = makeNode(Query);
    1242             :     Node       *qual;
    1243             :     ListCell   *l;
    1244             : 
    1245      507696 :     qry->commandType = CMD_SELECT;
    1246             : 
    1247             :     /* process the WITH clause independently of all else */
    1248      507696 :     if (stmt->withClause)
    1249             :     {
    1250        1230 :         qry->hasRecursive = stmt->withClause->recursive;
    1251        1230 :         qry->cteList = transformWithClause(pstate, stmt->withClause);
    1252        1060 :         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
    1253             :     }
    1254             : 
    1255             :     /* Complain if we get called from someplace where INTO is not allowed */
    1256      507526 :     if (stmt->intoClause)
    1257          12 :         ereport(ERROR,
    1258             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1259             :                  errmsg("SELECT ... INTO is not allowed here"),
    1260             :                  parser_errposition(pstate,
    1261             :                                     exprLocation((Node *) stmt->intoClause))));
    1262             : 
    1263             :     /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
    1264      507514 :     pstate->p_locking_clause = stmt->lockingClause;
    1265             : 
    1266             :     /* make WINDOW info available for window functions, too */
    1267      507514 :     pstate->p_windowdefs = stmt->windowClause;
    1268             : 
    1269             :     /* process the FROM clause */
    1270      507514 :     transformFromClause(pstate, stmt->fromClause);
    1271             : 
    1272             :     /* transform targetlist */
    1273      507180 :     qry->targetList = transformTargetList(pstate, stmt->targetList,
    1274             :                                           EXPR_KIND_SELECT_TARGET);
    1275             : 
    1276             :     /* mark column origins */
    1277      505162 :     markTargetListOrigins(pstate, qry->targetList);
    1278             : 
    1279             :     /* transform WHERE */
    1280      505162 :     qual = transformWhereClause(pstate, stmt->whereClause,
    1281             :                                 EXPR_KIND_WHERE, "WHERE");
    1282             : 
    1283             :     /* initial processing of HAVING clause is much like WHERE clause */
    1284      505092 :     qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
    1285             :                                            EXPR_KIND_HAVING, "HAVING");
    1286             : 
    1287             :     /*
    1288             :      * Transform sorting/grouping stuff.  Do ORDER BY first because both
    1289             :      * transformGroupClause and transformDistinctClause need the results. Note
    1290             :      * that these functions can also change the targetList, so it's passed to
    1291             :      * them by reference.
    1292             :      */
    1293      505088 :     qry->sortClause = transformSortClause(pstate,
    1294             :                                           stmt->sortClause,
    1295             :                                           &qry->targetList,
    1296             :                                           EXPR_KIND_ORDER_BY,
    1297             :                                           false /* allow SQL92 rules */ );
    1298             : 
    1299      505068 :     qry->groupClause = transformGroupClause(pstate,
    1300             :                                             stmt->groupClause,
    1301             :                                             &qry->groupingSets,
    1302             :                                             &qry->targetList,
    1303             :                                             qry->sortClause,
    1304             :                                             EXPR_KIND_GROUP_BY,
    1305             :                                             false /* allow SQL92 rules */ );
    1306      505052 :     qry->groupDistinct = stmt->groupDistinct;
    1307             : 
    1308      505052 :     if (stmt->distinctClause == NIL)
    1309             :     {
    1310      499588 :         qry->distinctClause = NIL;
    1311      499588 :         qry->hasDistinctOn = false;
    1312             :     }
    1313        5464 :     else if (linitial(stmt->distinctClause) == NULL)
    1314             :     {
    1315             :         /* We had SELECT DISTINCT */
    1316        5346 :         qry->distinctClause = transformDistinctClause(pstate,
    1317             :                                                       &qry->targetList,
    1318             :                                                       qry->sortClause,
    1319             :                                                       false);
    1320        5346 :         qry->hasDistinctOn = false;
    1321             :     }
    1322             :     else
    1323             :     {
    1324             :         /* We had SELECT DISTINCT ON */
    1325         118 :         qry->distinctClause = transformDistinctOnClause(pstate,
    1326             :                                                         stmt->distinctClause,
    1327             :                                                         &qry->targetList,
    1328             :                                                         qry->sortClause);
    1329         110 :         qry->hasDistinctOn = true;
    1330             :     }
    1331             : 
    1332             :     /* transform LIMIT */
    1333      505044 :     qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
    1334             :                                             EXPR_KIND_OFFSET, "OFFSET",
    1335             :                                             stmt->limitOption);
    1336      505044 :     qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
    1337             :                                            EXPR_KIND_LIMIT, "LIMIT",
    1338             :                                            stmt->limitOption);
    1339      505036 :     qry->limitOption = stmt->limitOption;
    1340             : 
    1341             :     /* transform window clauses after we have seen all window functions */
    1342      505036 :     qry->windowClause = transformWindowDefinitions(pstate,
    1343             :                                                    pstate->p_windowdefs,
    1344             :                                                    &qry->targetList);
    1345             : 
    1346             :     /* resolve any still-unresolved output columns as being type text */
    1347      504992 :     if (pstate->p_resolve_unknowns)
    1348      457462 :         resolveTargetListUnknowns(pstate, qry->targetList);
    1349             : 
    1350      504992 :     qry->rtable = pstate->p_rtable;
    1351      504992 :     qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
    1352             : 
    1353      504992 :     qry->hasSubLinks = pstate->p_hasSubLinks;
    1354      504992 :     qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
    1355      504992 :     qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
    1356      504992 :     qry->hasAggs = pstate->p_hasAggs;
    1357             : 
    1358      508940 :     foreach(l, stmt->lockingClause)
    1359             :     {
    1360        3964 :         transformLockingClause(pstate, qry,
    1361        3964 :                                (LockingClause *) lfirst(l), false);
    1362             :     }
    1363             : 
    1364      504976 :     assign_query_collations(pstate, qry);
    1365             : 
    1366             :     /* this must be done after collations, for reliable comparison of exprs */
    1367      504952 :     if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
    1368       53514 :         parseCheckAggregates(pstate, qry);
    1369             : 
    1370      504880 :     return qry;
    1371             : }
    1372             : 
    1373             : /*
    1374             :  * transformValuesClause -
    1375             :  *    transforms a VALUES clause that's being used as a standalone SELECT
    1376             :  *
    1377             :  * We build a Query containing a VALUES RTE, rather as if one had written
    1378             :  *          SELECT * FROM (VALUES ...) AS "*VALUES*"
    1379             :  */
    1380             : static Query *
    1381        3302 : transformValuesClause(ParseState *pstate, SelectStmt *stmt)
    1382             : {
    1383        3302 :     Query      *qry = makeNode(Query);
    1384             :     List       *exprsLists;
    1385        3302 :     List       *coltypes = NIL;
    1386        3302 :     List       *coltypmods = NIL;
    1387        3302 :     List       *colcollations = NIL;
    1388        3302 :     List      **colexprs = NULL;
    1389        3302 :     int         sublist_length = -1;
    1390        3302 :     bool        lateral = false;
    1391             :     ParseNamespaceItem *nsitem;
    1392             :     ListCell   *lc;
    1393             :     ListCell   *lc2;
    1394             :     int         i;
    1395             : 
    1396        3302 :     qry->commandType = CMD_SELECT;
    1397             : 
    1398             :     /* Most SELECT stuff doesn't apply in a VALUES clause */
    1399             :     Assert(stmt->distinctClause == NIL);
    1400             :     Assert(stmt->intoClause == NULL);
    1401             :     Assert(stmt->targetList == NIL);
    1402             :     Assert(stmt->fromClause == NIL);
    1403             :     Assert(stmt->whereClause == NULL);
    1404             :     Assert(stmt->groupClause == NIL);
    1405             :     Assert(stmt->havingClause == NULL);
    1406             :     Assert(stmt->windowClause == NIL);
    1407             :     Assert(stmt->op == SETOP_NONE);
    1408             : 
    1409             :     /* process the WITH clause independently of all else */
    1410        3302 :     if (stmt->withClause)
    1411             :     {
    1412          32 :         qry->hasRecursive = stmt->withClause->recursive;
    1413          32 :         qry->cteList = transformWithClause(pstate, stmt->withClause);
    1414          28 :         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
    1415             :     }
    1416             : 
    1417             :     /*
    1418             :      * For each row of VALUES, transform the raw expressions.
    1419             :      *
    1420             :      * Note that the intermediate representation we build is column-organized
    1421             :      * not row-organized.  That simplifies the type and collation processing
    1422             :      * below.
    1423             :      */
    1424       16258 :     foreach(lc, stmt->valuesLists)
    1425             :     {
    1426       12966 :         List       *sublist = (List *) lfirst(lc);
    1427             : 
    1428             :         /*
    1429             :          * Do basic expression transformation (same as a ROW() expr, but here
    1430             :          * we disallow SetToDefault)
    1431             :          */
    1432       12966 :         sublist = transformExpressionList(pstate, sublist,
    1433             :                                           EXPR_KIND_VALUES, false);
    1434             : 
    1435             :         /*
    1436             :          * All the sublists must be the same length, *after* transformation
    1437             :          * (which might expand '*' into multiple items).  The VALUES RTE can't
    1438             :          * handle anything different.
    1439             :          */
    1440       12960 :         if (sublist_length < 0)
    1441             :         {
    1442             :             /* Remember post-transformation length of first sublist */
    1443        3292 :             sublist_length = list_length(sublist);
    1444             :             /* and allocate array for per-column lists */
    1445        3292 :             colexprs = (List **) palloc0(sublist_length * sizeof(List *));
    1446             :         }
    1447        9668 :         else if (sublist_length != list_length(sublist))
    1448             :         {
    1449           0 :             ereport(ERROR,
    1450             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    1451             :                      errmsg("VALUES lists must all be the same length"),
    1452             :                      parser_errposition(pstate,
    1453             :                                         exprLocation((Node *) sublist))));
    1454             :         }
    1455             : 
    1456             :         /* Build per-column expression lists */
    1457       12960 :         i = 0;
    1458       32786 :         foreach(lc2, sublist)
    1459             :         {
    1460       19826 :             Node       *col = (Node *) lfirst(lc2);
    1461             : 
    1462       19826 :             colexprs[i] = lappend(colexprs[i], col);
    1463       19826 :             i++;
    1464             :         }
    1465             : 
    1466             :         /* Release sub-list's cells to save memory */
    1467       12960 :         list_free(sublist);
    1468             :     }
    1469             : 
    1470             :     /*
    1471             :      * Now resolve the common types of the columns, and coerce everything to
    1472             :      * those types.  Then identify the common typmod and common collation, if
    1473             :      * any, of each column.
    1474             :      *
    1475             :      * We must do collation processing now because (1) assign_query_collations
    1476             :      * doesn't process rangetable entries, and (2) we need to label the VALUES
    1477             :      * RTE with column collations for use in the outer query.  We don't
    1478             :      * consider conflict of implicit collations to be an error here; instead
    1479             :      * the column will just show InvalidOid as its collation, and you'll get a
    1480             :      * failure later if that results in failure to resolve a collation.
    1481             :      *
    1482             :      * Note we modify the per-column expression lists in-place.
    1483             :      */
    1484        8464 :     for (i = 0; i < sublist_length; i++)
    1485             :     {
    1486             :         Oid         coltype;
    1487             :         int32       coltypmod;
    1488             :         Oid         colcoll;
    1489             : 
    1490        5172 :         coltype = select_common_type(pstate, colexprs[i], "VALUES", NULL);
    1491             : 
    1492       24998 :         foreach(lc, colexprs[i])
    1493             :         {
    1494       19826 :             Node       *col = (Node *) lfirst(lc);
    1495             : 
    1496       19826 :             col = coerce_to_common_type(pstate, col, coltype, "VALUES");
    1497       19826 :             lfirst(lc) = (void *) col;
    1498             :         }
    1499             : 
    1500        5172 :         coltypmod = select_common_typmod(pstate, colexprs[i], coltype);
    1501        5172 :         colcoll = select_common_collation(pstate, colexprs[i], true);
    1502             : 
    1503        5172 :         coltypes = lappend_oid(coltypes, coltype);
    1504        5172 :         coltypmods = lappend_int(coltypmods, coltypmod);
    1505        5172 :         colcollations = lappend_oid(colcollations, colcoll);
    1506             :     }
    1507             : 
    1508             :     /*
    1509             :      * Finally, rearrange the coerced expressions into row-organized lists.
    1510             :      */
    1511        3292 :     exprsLists = NIL;
    1512       16252 :     foreach(lc, colexprs[0])
    1513             :     {
    1514       12960 :         Node       *col = (Node *) lfirst(lc);
    1515             :         List       *sublist;
    1516             : 
    1517       12960 :         sublist = list_make1(col);
    1518       12960 :         exprsLists = lappend(exprsLists, sublist);
    1519             :     }
    1520        3292 :     list_free(colexprs[0]);
    1521        5172 :     for (i = 1; i < sublist_length; i++)
    1522             :     {
    1523        8746 :         forboth(lc, colexprs[i], lc2, exprsLists)
    1524             :         {
    1525        6866 :             Node       *col = (Node *) lfirst(lc);
    1526        6866 :             List       *sublist = lfirst(lc2);
    1527             : 
    1528        6866 :             sublist = lappend(sublist, col);
    1529             :         }
    1530        1880 :         list_free(colexprs[i]);
    1531             :     }
    1532             : 
    1533             :     /*
    1534             :      * Ordinarily there can't be any current-level Vars in the expression
    1535             :      * lists, because the namespace was empty ... but if we're inside CREATE
    1536             :      * RULE, then NEW/OLD references might appear.  In that case we have to
    1537             :      * mark the VALUES RTE as LATERAL.
    1538             :      */
    1539        3298 :     if (pstate->p_rtable != NIL &&
    1540           6 :         contain_vars_of_level((Node *) exprsLists, 0))
    1541           6 :         lateral = true;
    1542             : 
    1543             :     /*
    1544             :      * Generate the VALUES RTE
    1545             :      */
    1546        3292 :     nsitem = addRangeTableEntryForValues(pstate, exprsLists,
    1547             :                                          coltypes, coltypmods, colcollations,
    1548             :                                          NULL, lateral, true);
    1549        3292 :     addNSItemToQuery(pstate, nsitem, true, true, true);
    1550             : 
    1551             :     /*
    1552             :      * Generate a targetlist as though expanding "*"
    1553             :      */
    1554             :     Assert(pstate->p_next_resno == 1);
    1555        3292 :     qry->targetList = expandNSItemAttrs(pstate, nsitem, 0, -1);
    1556             : 
    1557             :     /*
    1558             :      * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
    1559             :      * VALUES, so cope.
    1560             :      */
    1561        3292 :     qry->sortClause = transformSortClause(pstate,
    1562             :                                           stmt->sortClause,
    1563             :                                           &qry->targetList,
    1564             :                                           EXPR_KIND_ORDER_BY,
    1565             :                                           false /* allow SQL92 rules */ );
    1566             : 
    1567        3292 :     qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
    1568             :                                             EXPR_KIND_OFFSET, "OFFSET",
    1569             :                                             stmt->limitOption);
    1570        3292 :     qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
    1571             :                                            EXPR_KIND_LIMIT, "LIMIT",
    1572             :                                            stmt->limitOption);
    1573        3292 :     qry->limitOption = stmt->limitOption;
    1574             : 
    1575        3292 :     if (stmt->lockingClause)
    1576           0 :         ereport(ERROR,
    1577             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1578             :         /*------
    1579             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    1580             :                  errmsg("%s cannot be applied to VALUES",
    1581             :                         LCS_asString(((LockingClause *)
    1582             :                                       linitial(stmt->lockingClause))->strength))));
    1583             : 
    1584        3292 :     qry->rtable = pstate->p_rtable;
    1585        3292 :     qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
    1586             : 
    1587        3292 :     qry->hasSubLinks = pstate->p_hasSubLinks;
    1588             : 
    1589        3292 :     assign_query_collations(pstate, qry);
    1590             : 
    1591        3292 :     return qry;
    1592             : }
    1593             : 
    1594             : /*
    1595             :  * transformSetOperationStmt -
    1596             :  *    transforms a set-operations tree
    1597             :  *
    1598             :  * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
    1599             :  * structure to it.  We must transform each leaf SELECT and build up a top-
    1600             :  * level Query that contains the leaf SELECTs as subqueries in its rangetable.
    1601             :  * The tree of set operations is converted into the setOperations field of
    1602             :  * the top-level Query.
    1603             :  */
    1604             : static Query *
    1605       13908 : transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
    1606             : {
    1607       13908 :     Query      *qry = makeNode(Query);
    1608             :     SelectStmt *leftmostSelect;
    1609             :     int         leftmostRTI;
    1610             :     Query      *leftmostQuery;
    1611             :     SetOperationStmt *sostmt;
    1612             :     List       *sortClause;
    1613             :     Node       *limitOffset;
    1614             :     Node       *limitCount;
    1615             :     List       *lockingClause;
    1616             :     WithClause *withClause;
    1617             :     Node       *node;
    1618             :     ListCell   *left_tlist,
    1619             :                *lct,
    1620             :                *lcm,
    1621             :                *lcc,
    1622             :                *l;
    1623             :     List       *targetvars,
    1624             :                *targetnames,
    1625             :                *sv_namespace;
    1626             :     int         sv_rtable_length;
    1627             :     ParseNamespaceItem *jnsitem;
    1628             :     ParseNamespaceColumn *sortnscolumns;
    1629             :     int         sortcolindex;
    1630             :     int         tllen;
    1631             : 
    1632       13908 :     qry->commandType = CMD_SELECT;
    1633             : 
    1634             :     /*
    1635             :      * Find leftmost leaf SelectStmt.  We currently only need to do this in
    1636             :      * order to deliver a suitable error message if there's an INTO clause
    1637             :      * there, implying the set-op tree is in a context that doesn't allow
    1638             :      * INTO.  (transformSetOperationTree would throw error anyway, but it
    1639             :      * seems worth the trouble to throw a different error for non-leftmost
    1640             :      * INTO, so we produce that error in transformSetOperationTree.)
    1641             :      */
    1642       13908 :     leftmostSelect = stmt->larg;
    1643       25106 :     while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
    1644       11198 :         leftmostSelect = leftmostSelect->larg;
    1645             :     Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
    1646             :            leftmostSelect->larg == NULL);
    1647       13908 :     if (leftmostSelect->intoClause)
    1648           0 :         ereport(ERROR,
    1649             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1650             :                  errmsg("SELECT ... INTO is not allowed here"),
    1651             :                  parser_errposition(pstate,
    1652             :                                     exprLocation((Node *) leftmostSelect->intoClause))));
    1653             : 
    1654             :     /*
    1655             :      * We need to extract ORDER BY and other top-level clauses here and not
    1656             :      * let transformSetOperationTree() see them --- else it'll just recurse
    1657             :      * right back here!
    1658             :      */
    1659       13908 :     sortClause = stmt->sortClause;
    1660       13908 :     limitOffset = stmt->limitOffset;
    1661       13908 :     limitCount = stmt->limitCount;
    1662       13908 :     lockingClause = stmt->lockingClause;
    1663       13908 :     withClause = stmt->withClause;
    1664             : 
    1665       13908 :     stmt->sortClause = NIL;
    1666       13908 :     stmt->limitOffset = NULL;
    1667       13908 :     stmt->limitCount = NULL;
    1668       13908 :     stmt->lockingClause = NIL;
    1669       13908 :     stmt->withClause = NULL;
    1670             : 
    1671             :     /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
    1672       13908 :     if (lockingClause)
    1673           4 :         ereport(ERROR,
    1674             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1675             :         /*------
    1676             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    1677             :                  errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
    1678             :                         LCS_asString(((LockingClause *)
    1679             :                                       linitial(lockingClause))->strength))));
    1680             : 
    1681             :     /* Process the WITH clause independently of all else */
    1682       13904 :     if (withClause)
    1683             :     {
    1684          68 :         qry->hasRecursive = withClause->recursive;
    1685          68 :         qry->cteList = transformWithClause(pstate, withClause);
    1686          68 :         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
    1687             :     }
    1688             : 
    1689             :     /*
    1690             :      * Recursively transform the components of the tree.
    1691             :      */
    1692       13904 :     sostmt = castNode(SetOperationStmt,
    1693             :                       transformSetOperationTree(pstate, stmt, true, NULL));
    1694             :     Assert(sostmt);
    1695       13868 :     qry->setOperations = (Node *) sostmt;
    1696             : 
    1697             :     /*
    1698             :      * Re-find leftmost SELECT (now it's a sub-query in rangetable)
    1699             :      */
    1700       13868 :     node = sostmt->larg;
    1701       25054 :     while (node && IsA(node, SetOperationStmt))
    1702       11186 :         node = ((SetOperationStmt *) node)->larg;
    1703             :     Assert(node && IsA(node, RangeTblRef));
    1704       13868 :     leftmostRTI = ((RangeTblRef *) node)->rtindex;
    1705       13868 :     leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
    1706             :     Assert(leftmostQuery != NULL);
    1707             : 
    1708             :     /*
    1709             :      * Generate dummy targetlist for outer query using column names of
    1710             :      * leftmost select and common datatypes/collations of topmost set
    1711             :      * operation.  Also make lists of the dummy vars and their names for use
    1712             :      * in parsing ORDER BY.
    1713             :      *
    1714             :      * Note: we use leftmostRTI as the varno of the dummy variables. It
    1715             :      * shouldn't matter too much which RT index they have, as long as they
    1716             :      * have one that corresponds to a real RT entry; else funny things may
    1717             :      * happen when the tree is mashed by rule rewriting.
    1718             :      */
    1719       13868 :     qry->targetList = NIL;
    1720       13868 :     targetvars = NIL;
    1721       13868 :     targetnames = NIL;
    1722             :     sortnscolumns = (ParseNamespaceColumn *)
    1723       13868 :         palloc0(list_length(sostmt->colTypes) * sizeof(ParseNamespaceColumn));
    1724       13868 :     sortcolindex = 0;
    1725             : 
    1726       62132 :     forfour(lct, sostmt->colTypes,
    1727             :             lcm, sostmt->colTypmods,
    1728             :             lcc, sostmt->colCollations,
    1729             :             left_tlist, leftmostQuery->targetList)
    1730             :     {
    1731       48264 :         Oid         colType = lfirst_oid(lct);
    1732       48264 :         int32       colTypmod = lfirst_int(lcm);
    1733       48264 :         Oid         colCollation = lfirst_oid(lcc);
    1734       48264 :         TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
    1735             :         char       *colName;
    1736             :         TargetEntry *tle;
    1737             :         Var        *var;
    1738             : 
    1739             :         Assert(!lefttle->resjunk);
    1740       48264 :         colName = pstrdup(lefttle->resname);
    1741       48264 :         var = makeVar(leftmostRTI,
    1742       48264 :                       lefttle->resno,
    1743             :                       colType,
    1744             :                       colTypmod,
    1745             :                       colCollation,
    1746             :                       0);
    1747       48264 :         var->location = exprLocation((Node *) lefttle->expr);
    1748       48264 :         tle = makeTargetEntry((Expr *) var,
    1749       48264 :                               (AttrNumber) pstate->p_next_resno++,
    1750             :                               colName,
    1751             :                               false);
    1752       48264 :         qry->targetList = lappend(qry->targetList, tle);
    1753       48264 :         targetvars = lappend(targetvars, var);
    1754       48264 :         targetnames = lappend(targetnames, makeString(colName));
    1755       48264 :         sortnscolumns[sortcolindex].p_varno = leftmostRTI;
    1756       48264 :         sortnscolumns[sortcolindex].p_varattno = lefttle->resno;
    1757       48264 :         sortnscolumns[sortcolindex].p_vartype = colType;
    1758       48264 :         sortnscolumns[sortcolindex].p_vartypmod = colTypmod;
    1759       48264 :         sortnscolumns[sortcolindex].p_varcollid = colCollation;
    1760       48264 :         sortnscolumns[sortcolindex].p_varnosyn = leftmostRTI;
    1761       48264 :         sortnscolumns[sortcolindex].p_varattnosyn = lefttle->resno;
    1762       48264 :         sortcolindex++;
    1763             :     }
    1764             : 
    1765             :     /*
    1766             :      * As a first step towards supporting sort clauses that are expressions
    1767             :      * using the output columns, generate a namespace entry that makes the
    1768             :      * output columns visible.  A Join RTE node is handy for this, since we
    1769             :      * can easily control the Vars generated upon matches.
    1770             :      *
    1771             :      * Note: we don't yet do anything useful with such cases, but at least
    1772             :      * "ORDER BY upper(foo)" will draw the right error message rather than
    1773             :      * "foo not found".
    1774             :      */
    1775       13868 :     sv_rtable_length = list_length(pstate->p_rtable);
    1776             : 
    1777       13868 :     jnsitem = addRangeTableEntryForJoin(pstate,
    1778             :                                         targetnames,
    1779             :                                         sortnscolumns,
    1780             :                                         JOIN_INNER,
    1781             :                                         0,
    1782             :                                         targetvars,
    1783             :                                         NIL,
    1784             :                                         NIL,
    1785             :                                         NULL,
    1786             :                                         NULL,
    1787             :                                         false);
    1788             : 
    1789       13868 :     sv_namespace = pstate->p_namespace;
    1790       13868 :     pstate->p_namespace = NIL;
    1791             : 
    1792             :     /* add jnsitem to column namespace only */
    1793       13868 :     addNSItemToQuery(pstate, jnsitem, false, false, true);
    1794             : 
    1795             :     /*
    1796             :      * For now, we don't support resjunk sort clauses on the output of a
    1797             :      * setOperation tree --- you can only use the SQL92-spec options of
    1798             :      * selecting an output column by name or number.  Enforce by checking that
    1799             :      * transformSortClause doesn't add any items to tlist.
    1800             :      */
    1801       13868 :     tllen = list_length(qry->targetList);
    1802             : 
    1803       13868 :     qry->sortClause = transformSortClause(pstate,
    1804             :                                           sortClause,
    1805             :                                           &qry->targetList,
    1806             :                                           EXPR_KIND_ORDER_BY,
    1807             :                                           false /* allow SQL92 rules */ );
    1808             : 
    1809             :     /* restore namespace, remove join RTE from rtable */
    1810       13864 :     pstate->p_namespace = sv_namespace;
    1811       13864 :     pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
    1812             : 
    1813       13864 :     if (tllen != list_length(qry->targetList))
    1814           0 :         ereport(ERROR,
    1815             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1816             :                  errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
    1817             :                  errdetail("Only result column names can be used, not expressions or functions."),
    1818             :                  errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
    1819             :                  parser_errposition(pstate,
    1820             :                                     exprLocation(list_nth(qry->targetList, tllen)))));
    1821             : 
    1822       13864 :     qry->limitOffset = transformLimitClause(pstate, limitOffset,
    1823             :                                             EXPR_KIND_OFFSET, "OFFSET",
    1824             :                                             stmt->limitOption);
    1825       13864 :     qry->limitCount = transformLimitClause(pstate, limitCount,
    1826             :                                            EXPR_KIND_LIMIT, "LIMIT",
    1827             :                                            stmt->limitOption);
    1828       13864 :     qry->limitOption = stmt->limitOption;
    1829             : 
    1830       13864 :     qry->rtable = pstate->p_rtable;
    1831       13864 :     qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
    1832             : 
    1833       13864 :     qry->hasSubLinks = pstate->p_hasSubLinks;
    1834       13864 :     qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
    1835       13864 :     qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
    1836       13864 :     qry->hasAggs = pstate->p_hasAggs;
    1837             : 
    1838       13864 :     foreach(l, lockingClause)
    1839             :     {
    1840           0 :         transformLockingClause(pstate, qry,
    1841           0 :                                (LockingClause *) lfirst(l), false);
    1842             :     }
    1843             : 
    1844       13864 :     assign_query_collations(pstate, qry);
    1845             : 
    1846             :     /* this must be done after collations, for reliable comparison of exprs */
    1847       13864 :     if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
    1848           0 :         parseCheckAggregates(pstate, qry);
    1849             : 
    1850       13864 :     return qry;
    1851             : }
    1852             : 
    1853             : /*
    1854             :  * Make a SortGroupClause node for a SetOperationStmt's groupClauses
    1855             :  *
    1856             :  * If require_hash is true, the caller is indicating that they need hash
    1857             :  * support or they will fail.  So look extra hard for hash support.
    1858             :  */
    1859             : SortGroupClause *
    1860       17928 : makeSortGroupClauseForSetOp(Oid rescoltype, bool require_hash)
    1861             : {
    1862       17928 :     SortGroupClause *grpcl = makeNode(SortGroupClause);
    1863             :     Oid         sortop;
    1864             :     Oid         eqop;
    1865             :     bool        hashable;
    1866             : 
    1867             :     /* determine the eqop and optional sortop */
    1868       17928 :     get_sort_group_operators(rescoltype,
    1869             :                              false, true, false,
    1870             :                              &sortop, &eqop, NULL,
    1871             :                              &hashable);
    1872             : 
    1873             :     /*
    1874             :      * The type cache doesn't believe that record is hashable (see
    1875             :      * cache_record_field_properties()), but if the caller really needs hash
    1876             :      * support, we can assume it does.  Worst case, if any components of the
    1877             :      * record don't support hashing, we will fail at execution.
    1878             :      */
    1879       17928 :     if (require_hash && (rescoltype == RECORDOID || rescoltype == RECORDARRAYOID))
    1880          16 :         hashable = true;
    1881             : 
    1882             :     /* we don't have a tlist yet, so can't assign sortgrouprefs */
    1883       17928 :     grpcl->tleSortGroupRef = 0;
    1884       17928 :     grpcl->eqop = eqop;
    1885       17928 :     grpcl->sortop = sortop;
    1886       17928 :     grpcl->nulls_first = false; /* OK with or without sortop */
    1887       17928 :     grpcl->hashable = hashable;
    1888             : 
    1889       17928 :     return grpcl;
    1890             : }
    1891             : 
    1892             : /*
    1893             :  * transformSetOperationTree
    1894             :  *      Recursively transform leaves and internal nodes of a set-op tree
    1895             :  *
    1896             :  * In addition to returning the transformed node, if targetlist isn't NULL
    1897             :  * then we return a list of its non-resjunk TargetEntry nodes.  For a leaf
    1898             :  * set-op node these are the actual targetlist entries; otherwise they are
    1899             :  * dummy entries created to carry the type, typmod, collation, and location
    1900             :  * (for error messages) of each output column of the set-op node.  This info
    1901             :  * is needed only during the internal recursion of this function, so outside
    1902             :  * callers pass NULL for targetlist.  Note: the reason for passing the
    1903             :  * actual targetlist entries of a leaf node is so that upper levels can
    1904             :  * replace UNKNOWN Consts with properly-coerced constants.
    1905             :  */
    1906             : static Node *
    1907       64168 : transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
    1908             :                           bool isTopLevel, List **targetlist)
    1909             : {
    1910             :     bool        isLeaf;
    1911             : 
    1912             :     Assert(stmt && IsA(stmt, SelectStmt));
    1913             : 
    1914             :     /* Guard against stack overflow due to overly complex set-expressions */
    1915       64168 :     check_stack_depth();
    1916             : 
    1917             :     /*
    1918             :      * Validity-check both leaf and internal SELECTs for disallowed ops.
    1919             :      */
    1920       64168 :     if (stmt->intoClause)
    1921           0 :         ereport(ERROR,
    1922             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1923             :                  errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
    1924             :                  parser_errposition(pstate,
    1925             :                                     exprLocation((Node *) stmt->intoClause))));
    1926             : 
    1927             :     /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
    1928       64168 :     if (stmt->lockingClause)
    1929           0 :         ereport(ERROR,
    1930             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1931             :         /*------
    1932             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    1933             :                  errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
    1934             :                         LCS_asString(((LockingClause *)
    1935             :                                       linitial(stmt->lockingClause))->strength))));
    1936             : 
    1937             :     /*
    1938             :      * If an internal node of a set-op tree has ORDER BY, LIMIT, FOR UPDATE,
    1939             :      * or WITH clauses attached, we need to treat it like a leaf node to
    1940             :      * generate an independent sub-Query tree.  Otherwise, it can be
    1941             :      * represented by a SetOperationStmt node underneath the parent Query.
    1942             :      */
    1943       64168 :     if (stmt->op == SETOP_NONE)
    1944             :     {
    1945             :         Assert(stmt->larg == NULL && stmt->rarg == NULL);
    1946       38994 :         isLeaf = true;
    1947             :     }
    1948             :     else
    1949             :     {
    1950             :         Assert(stmt->larg != NULL && stmt->rarg != NULL);
    1951       25174 :         if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
    1952       25158 :             stmt->lockingClause || stmt->withClause)
    1953          40 :             isLeaf = true;
    1954             :         else
    1955       25134 :             isLeaf = false;
    1956             :     }
    1957             : 
    1958       64168 :     if (isLeaf)
    1959             :     {
    1960             :         /* Process leaf SELECT */
    1961             :         Query      *selectQuery;
    1962             :         char        selectName[32];
    1963             :         ParseNamespaceItem *nsitem;
    1964             :         RangeTblRef *rtr;
    1965             :         ListCell   *tl;
    1966             : 
    1967             :         /*
    1968             :          * Transform SelectStmt into a Query.
    1969             :          *
    1970             :          * This works the same as SELECT transformation normally would, except
    1971             :          * that we prevent resolving unknown-type outputs as TEXT.  This does
    1972             :          * not change the subquery's semantics since if the column type
    1973             :          * matters semantically, it would have been resolved to something else
    1974             :          * anyway.  Doing this lets us resolve such outputs using
    1975             :          * select_common_type(), below.
    1976             :          *
    1977             :          * Note: previously transformed sub-queries don't affect the parsing
    1978             :          * of this sub-query, because they are not in the toplevel pstate's
    1979             :          * namespace list.
    1980             :          */
    1981       39034 :         selectQuery = parse_sub_analyze((Node *) stmt, pstate,
    1982             :                                         NULL, false, false);
    1983             : 
    1984             :         /*
    1985             :          * Check for bogus references to Vars on the current query level (but
    1986             :          * upper-level references are okay). Normally this can't happen
    1987             :          * because the namespace will be empty, but it could happen if we are
    1988             :          * inside a rule.
    1989             :          */
    1990       39014 :         if (pstate->p_namespace)
    1991             :         {
    1992           0 :             if (contain_vars_of_level((Node *) selectQuery, 1))
    1993           0 :                 ereport(ERROR,
    1994             :                         (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
    1995             :                          errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
    1996             :                          parser_errposition(pstate,
    1997             :                                             locate_var_of_level((Node *) selectQuery, 1))));
    1998             :         }
    1999             : 
    2000             :         /*
    2001             :          * Extract a list of the non-junk TLEs for upper-level processing.
    2002             :          */
    2003       39014 :         if (targetlist)
    2004             :         {
    2005       39014 :             *targetlist = NIL;
    2006      205990 :             foreach(tl, selectQuery->targetList)
    2007             :             {
    2008      166976 :                 TargetEntry *tle = (TargetEntry *) lfirst(tl);
    2009             : 
    2010      166976 :                 if (!tle->resjunk)
    2011      166964 :                     *targetlist = lappend(*targetlist, tle);
    2012             :             }
    2013             :         }
    2014             : 
    2015             :         /*
    2016             :          * Make the leaf query be a subquery in the top-level rangetable.
    2017             :          */
    2018       39014 :         snprintf(selectName, sizeof(selectName), "*SELECT* %d",
    2019       39014 :                  list_length(pstate->p_rtable) + 1);
    2020       39014 :         nsitem = addRangeTableEntryForSubquery(pstate,
    2021             :                                                selectQuery,
    2022             :                                                makeAlias(selectName, NIL),
    2023             :                                                false,
    2024             :                                                false);
    2025             : 
    2026             :         /*
    2027             :          * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
    2028             :          */
    2029       39014 :         rtr = makeNode(RangeTblRef);
    2030       39014 :         rtr->rtindex = nsitem->p_rtindex;
    2031       39014 :         return (Node *) rtr;
    2032             :     }
    2033             :     else
    2034             :     {
    2035             :         /* Process an internal node (set operation node) */
    2036       25134 :         SetOperationStmt *op = makeNode(SetOperationStmt);
    2037             :         List       *ltargetlist;
    2038             :         List       *rtargetlist;
    2039             :         ListCell   *ltl;
    2040             :         ListCell   *rtl;
    2041             :         const char *context;
    2042       25774 :         bool recursive = (pstate->p_parent_cte &&
    2043         640 :                           pstate->p_parent_cte->cterecursive);
    2044             : 
    2045       25486 :         context = (stmt->op == SETOP_UNION ? "UNION" :
    2046         352 :                    (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
    2047             :                     "EXCEPT"));
    2048             : 
    2049       25134 :         op->op = stmt->op;
    2050       25134 :         op->all = stmt->all;
    2051             : 
    2052             :         /*
    2053             :          * Recursively transform the left child node.
    2054             :          */
    2055       25134 :         op->larg = transformSetOperationTree(pstate, stmt->larg,
    2056             :                                              false,
    2057             :                                              &ltargetlist);
    2058             : 
    2059             :         /*
    2060             :          * If we are processing a recursive union query, now is the time to
    2061             :          * examine the non-recursive term's output columns and mark the
    2062             :          * containing CTE as having those result columns.  We should do this
    2063             :          * only at the topmost setop of the CTE, of course.
    2064             :          */
    2065       25130 :         if (isTopLevel && recursive)
    2066         548 :             determineRecursiveColTypes(pstate, op->larg, ltargetlist);
    2067             : 
    2068             :         /*
    2069             :          * Recursively transform the right child node.
    2070             :          */
    2071       25130 :         op->rarg = transformSetOperationTree(pstate, stmt->rarg,
    2072             :                                              false,
    2073             :                                              &rtargetlist);
    2074             : 
    2075             :         /*
    2076             :          * Verify that the two children have the same number of non-junk
    2077             :          * columns, and determine the types of the merged output columns.
    2078             :          */
    2079       25114 :         if (list_length(ltargetlist) != list_length(rtargetlist))
    2080           0 :             ereport(ERROR,
    2081             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    2082             :                      errmsg("each %s query must have the same number of columns",
    2083             :                             context),
    2084             :                      parser_errposition(pstate,
    2085             :                                         exprLocation((Node *) rtargetlist))));
    2086             : 
    2087       25114 :         if (targetlist)
    2088       11230 :             *targetlist = NIL;
    2089       25114 :         op->colTypes = NIL;
    2090       25114 :         op->colTypmods = NIL;
    2091       25114 :         op->colCollations = NIL;
    2092       25114 :         op->groupClauses = NIL;
    2093      143754 :         forboth(ltl, ltargetlist, rtl, rtargetlist)
    2094             :         {
    2095      118656 :             TargetEntry *ltle = (TargetEntry *) lfirst(ltl);
    2096      118656 :             TargetEntry *rtle = (TargetEntry *) lfirst(rtl);
    2097      118656 :             Node       *lcolnode = (Node *) ltle->expr;
    2098      118656 :             Node       *rcolnode = (Node *) rtle->expr;
    2099      118656 :             Oid         lcoltype = exprType(lcolnode);
    2100      118656 :             Oid         rcoltype = exprType(rcolnode);
    2101             :             Node       *bestexpr;
    2102             :             int         bestlocation;
    2103             :             Oid         rescoltype;
    2104             :             int32       rescoltypmod;
    2105             :             Oid         rescolcoll;
    2106             : 
    2107             :             /* select common type, same as CASE et al */
    2108      118656 :             rescoltype = select_common_type(pstate,
    2109      118656 :                                             list_make2(lcolnode, rcolnode),
    2110             :                                             context,
    2111             :                                             &bestexpr);
    2112      118656 :             bestlocation = exprLocation(bestexpr);
    2113             : 
    2114             :             /*
    2115             :              * Verify the coercions are actually possible.  If not, we'd fail
    2116             :              * later anyway, but we want to fail now while we have sufficient
    2117             :              * context to produce an error cursor position.
    2118             :              *
    2119             :              * For all non-UNKNOWN-type cases, we verify coercibility but we
    2120             :              * don't modify the child's expression, for fear of changing the
    2121             :              * child query's semantics.
    2122             :              *
    2123             :              * If a child expression is an UNKNOWN-type Const or Param, we
    2124             :              * want to replace it with the coerced expression.  This can only
    2125             :              * happen when the child is a leaf set-op node.  It's safe to
    2126             :              * replace the expression because if the child query's semantics
    2127             :              * depended on the type of this output column, it'd have already
    2128             :              * coerced the UNKNOWN to something else.  We want to do this
    2129             :              * because (a) we want to verify that a Const is valid for the
    2130             :              * target type, or resolve the actual type of an UNKNOWN Param,
    2131             :              * and (b) we want to avoid unnecessary discrepancies between the
    2132             :              * output type of the child query and the resolved target type.
    2133             :              * Such a discrepancy would disable optimization in the planner.
    2134             :              *
    2135             :              * If it's some other UNKNOWN-type node, eg a Var, we do nothing
    2136             :              * (knowing that coerce_to_common_type would fail).  The planner
    2137             :              * is sometimes able to fold an UNKNOWN Var to a constant before
    2138             :              * it has to coerce the type, so failing now would just break
    2139             :              * cases that might work.
    2140             :              */
    2141      118656 :             if (lcoltype != UNKNOWNOID)
    2142      117642 :                 lcolnode = coerce_to_common_type(pstate, lcolnode,
    2143             :                                                  rescoltype, context);
    2144        1014 :             else if (IsA(lcolnode, Const) ||
    2145           0 :                      IsA(lcolnode, Param))
    2146             :             {
    2147        1014 :                 lcolnode = coerce_to_common_type(pstate, lcolnode,
    2148             :                                                  rescoltype, context);
    2149        1014 :                 ltle->expr = (Expr *) lcolnode;
    2150             :             }
    2151             : 
    2152      118656 :             if (rcoltype != UNKNOWNOID)
    2153      114624 :                 rcolnode = coerce_to_common_type(pstate, rcolnode,
    2154             :                                                  rescoltype, context);
    2155        4032 :             else if (IsA(rcolnode, Const) ||
    2156           0 :                      IsA(rcolnode, Param))
    2157             :             {
    2158        4032 :                 rcolnode = coerce_to_common_type(pstate, rcolnode,
    2159             :                                                  rescoltype, context);
    2160        4028 :                 rtle->expr = (Expr *) rcolnode;
    2161             :             }
    2162             : 
    2163      118652 :             rescoltypmod = select_common_typmod(pstate,
    2164      118652 :                                                 list_make2(lcolnode, rcolnode),
    2165             :                                                 rescoltype);
    2166             : 
    2167             :             /*
    2168             :              * Select common collation.  A common collation is required for
    2169             :              * all set operators except UNION ALL; see SQL:2008 7.13 <query
    2170             :              * expression> Syntax Rule 15c.  (If we fail to identify a common
    2171             :              * collation for a UNION ALL column, the colCollations element
    2172             :              * will be set to InvalidOid, which may result in a runtime error
    2173             :              * if something at a higher query level wants to use the column's
    2174             :              * collation.)
    2175             :              */
    2176      118652 :             rescolcoll = select_common_collation(pstate,
    2177      118652 :                                                  list_make2(lcolnode, rcolnode),
    2178      118652 :                                                  (op->op == SETOP_UNION && op->all));
    2179             : 
    2180             :             /* emit results */
    2181      118640 :             op->colTypes = lappend_oid(op->colTypes, rescoltype);
    2182      118640 :             op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
    2183      118640 :             op->colCollations = lappend_oid(op->colCollations, rescolcoll);
    2184             : 
    2185             :             /*
    2186             :              * For all cases except UNION ALL, identify the grouping operators
    2187             :              * (and, if available, sorting operators) that will be used to
    2188             :              * eliminate duplicates.
    2189             :              */
    2190      118640 :             if (op->op != SETOP_UNION || !op->all)
    2191             :             {
    2192             :                 ParseCallbackState pcbstate;
    2193             : 
    2194       17912 :                 setup_parser_errposition_callback(&pcbstate, pstate,
    2195             :                                                   bestlocation);
    2196             : 
    2197             :                 /* If it's a recursive union, we need to require hashing support. */
    2198       17912 :                 op->groupClauses = lappend(op->groupClauses,
    2199       17912 :                                            makeSortGroupClauseForSetOp(rescoltype, recursive));
    2200             : 
    2201       17912 :                 cancel_parser_errposition_callback(&pcbstate);
    2202             :             }
    2203             : 
    2204             :             /*
    2205             :              * Construct a dummy tlist entry to return.  We use a SetToDefault
    2206             :              * node for the expression, since it carries exactly the fields
    2207             :              * needed, but any other expression node type would do as well.
    2208             :              */
    2209      118640 :             if (targetlist)
    2210             :             {
    2211       70364 :                 SetToDefault *rescolnode = makeNode(SetToDefault);
    2212             :                 TargetEntry *restle;
    2213             : 
    2214       70364 :                 rescolnode->typeId = rescoltype;
    2215       70364 :                 rescolnode->typeMod = rescoltypmod;
    2216       70364 :                 rescolnode->collation = rescolcoll;
    2217       70364 :                 rescolnode->location = bestlocation;
    2218       70364 :                 restle = makeTargetEntry((Expr *) rescolnode,
    2219             :                                          0, /* no need to set resno */
    2220             :                                          NULL,
    2221             :                                          false);
    2222       70364 :                 *targetlist = lappend(*targetlist, restle);
    2223             :             }
    2224             :         }
    2225             : 
    2226       25098 :         return (Node *) op;
    2227             :     }
    2228             : }
    2229             : 
    2230             : /*
    2231             :  * Process the outputs of the non-recursive term of a recursive union
    2232             :  * to set up the parent CTE's columns
    2233             :  */
    2234             : static void
    2235         548 : determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
    2236             : {
    2237             :     Node       *node;
    2238             :     int         leftmostRTI;
    2239             :     Query      *leftmostQuery;
    2240             :     List       *targetList;
    2241             :     ListCell   *left_tlist;
    2242             :     ListCell   *nrtl;
    2243             :     int         next_resno;
    2244             : 
    2245             :     /*
    2246             :      * Find leftmost leaf SELECT
    2247             :      */
    2248         548 :     node = larg;
    2249         552 :     while (node && IsA(node, SetOperationStmt))
    2250           4 :         node = ((SetOperationStmt *) node)->larg;
    2251             :     Assert(node && IsA(node, RangeTblRef));
    2252         548 :     leftmostRTI = ((RangeTblRef *) node)->rtindex;
    2253         548 :     leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
    2254             :     Assert(leftmostQuery != NULL);
    2255             : 
    2256             :     /*
    2257             :      * Generate dummy targetlist using column names of leftmost select and
    2258             :      * dummy result expressions of the non-recursive term.
    2259             :      */
    2260         548 :     targetList = NIL;
    2261         548 :     next_resno = 1;
    2262             : 
    2263        1836 :     forboth(nrtl, nrtargetlist, left_tlist, leftmostQuery->targetList)
    2264             :     {
    2265        1288 :         TargetEntry *nrtle = (TargetEntry *) lfirst(nrtl);
    2266        1288 :         TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
    2267             :         char       *colName;
    2268             :         TargetEntry *tle;
    2269             : 
    2270             :         Assert(!lefttle->resjunk);
    2271        1288 :         colName = pstrdup(lefttle->resname);
    2272        1288 :         tle = makeTargetEntry(nrtle->expr,
    2273        1288 :                               next_resno++,
    2274             :                               colName,
    2275             :                               false);
    2276        1288 :         targetList = lappend(targetList, tle);
    2277             :     }
    2278             : 
    2279             :     /* Now build CTE's output column info using dummy targetlist */
    2280         548 :     analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
    2281         548 : }
    2282             : 
    2283             : 
    2284             : /*
    2285             :  * transformReturnStmt -
    2286             :  *    transforms a return statement
    2287             :  */
    2288             : static Query *
    2289       23654 : transformReturnStmt(ParseState *pstate, ReturnStmt *stmt)
    2290             : {
    2291       23654 :     Query      *qry = makeNode(Query);
    2292             : 
    2293       23654 :     qry->commandType = CMD_SELECT;
    2294       23654 :     qry->isReturn = true;
    2295             : 
    2296       23654 :     qry->targetList = list_make1(makeTargetEntry((Expr *) transformExpr(pstate, stmt->returnval, EXPR_KIND_SELECT_TARGET),
    2297             :                                                  1, NULL, false));
    2298             : 
    2299       23650 :     if (pstate->p_resolve_unknowns)
    2300       23650 :         resolveTargetListUnknowns(pstate, qry->targetList);
    2301       23650 :     qry->rtable = pstate->p_rtable;
    2302       23650 :     qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
    2303       23650 :     qry->hasSubLinks = pstate->p_hasSubLinks;
    2304       23650 :     qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
    2305       23650 :     qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
    2306       23650 :     qry->hasAggs = pstate->p_hasAggs;
    2307             : 
    2308       23650 :     assign_query_collations(pstate, qry);
    2309             : 
    2310       23650 :     return qry;
    2311             : }
    2312             : 
    2313             : 
    2314             : /*
    2315             :  * transformUpdateStmt -
    2316             :  *    transforms an update statement
    2317             :  */
    2318             : static Query *
    2319       11060 : transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
    2320             : {
    2321       11060 :     Query      *qry = makeNode(Query);
    2322             :     ParseNamespaceItem *nsitem;
    2323             :     Node       *qual;
    2324             : 
    2325       11060 :     qry->commandType = CMD_UPDATE;
    2326       11060 :     pstate->p_is_insert = false;
    2327             : 
    2328             :     /* process the WITH clause independently of all else */
    2329       11060 :     if (stmt->withClause)
    2330             :     {
    2331          38 :         qry->hasRecursive = stmt->withClause->recursive;
    2332          38 :         qry->cteList = transformWithClause(pstate, stmt->withClause);
    2333          38 :         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
    2334             :     }
    2335             : 
    2336       22118 :     qry->resultRelation = setTargetTable(pstate, stmt->relation,
    2337       11060 :                                          stmt->relation->inh,
    2338             :                                          true,
    2339             :                                          ACL_UPDATE);
    2340       11058 :     nsitem = pstate->p_target_nsitem;
    2341             : 
    2342             :     /* subqueries in FROM cannot access the result relation */
    2343       11058 :     nsitem->p_lateral_only = true;
    2344       11058 :     nsitem->p_lateral_ok = false;
    2345             : 
    2346             :     /*
    2347             :      * the FROM clause is non-standard SQL syntax. We used to be able to do
    2348             :      * this with REPLACE in POSTQUEL so we keep the feature.
    2349             :      */
    2350       11058 :     transformFromClause(pstate, stmt->fromClause);
    2351             : 
    2352             :     /* remaining clauses can reference the result relation normally */
    2353       11042 :     nsitem->p_lateral_only = false;
    2354       11042 :     nsitem->p_lateral_ok = true;
    2355             : 
    2356       11042 :     qual = transformWhereClause(pstate, stmt->whereClause,
    2357             :                                 EXPR_KIND_WHERE, "WHERE");
    2358             : 
    2359       11034 :     qry->returningList = transformReturningList(pstate, stmt->returningList);
    2360             : 
    2361             :     /*
    2362             :      * Now we are done with SELECT-like processing, and can get on with
    2363             :      * transforming the target list to match the UPDATE target columns.
    2364             :      */
    2365       11034 :     qry->targetList = transformUpdateTargetList(pstate, stmt->targetList);
    2366             : 
    2367       11006 :     qry->rtable = pstate->p_rtable;
    2368       11006 :     qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
    2369             : 
    2370       11006 :     qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
    2371       11006 :     qry->hasSubLinks = pstate->p_hasSubLinks;
    2372             : 
    2373       11006 :     assign_query_collations(pstate, qry);
    2374             : 
    2375       11006 :     return qry;
    2376             : }
    2377             : 
    2378             : /*
    2379             :  * transformUpdateTargetList -
    2380             :  *  handle SET clause in UPDATE/INSERT ... ON CONFLICT UPDATE
    2381             :  */
    2382             : static List *
    2383       11806 : transformUpdateTargetList(ParseState *pstate, List *origTlist)
    2384             : {
    2385       11806 :     List       *tlist = NIL;
    2386             :     RangeTblEntry *target_rte;
    2387             :     ListCell   *orig_tl;
    2388             :     ListCell   *tl;
    2389             : 
    2390       11806 :     tlist = transformTargetList(pstate, origTlist,
    2391             :                                 EXPR_KIND_UPDATE_SOURCE);
    2392             : 
    2393             :     /* Prepare to assign non-conflicting resnos to resjunk attributes */
    2394       11778 :     if (pstate->p_next_resno <= RelationGetNumberOfAttributes(pstate->p_target_relation))
    2395       10378 :         pstate->p_next_resno = RelationGetNumberOfAttributes(pstate->p_target_relation) + 1;
    2396             : 
    2397             :     /* Prepare non-junk columns for assignment to target table */
    2398       11778 :     target_rte = pstate->p_target_nsitem->p_rte;
    2399       11778 :     orig_tl = list_head(origTlist);
    2400             : 
    2401       26858 :     foreach(tl, tlist)
    2402             :     {
    2403       15096 :         TargetEntry *tle = (TargetEntry *) lfirst(tl);
    2404             :         ResTarget  *origTarget;
    2405             :         int         attrno;
    2406             : 
    2407       15096 :         if (tle->resjunk)
    2408             :         {
    2409             :             /*
    2410             :              * Resjunk nodes need no additional processing, but be sure they
    2411             :              * have resnos that do not match any target columns; else rewriter
    2412             :              * or planner might get confused.  They don't need a resname
    2413             :              * either.
    2414             :              */
    2415          70 :             tle->resno = (AttrNumber) pstate->p_next_resno++;
    2416          70 :             tle->resname = NULL;
    2417          70 :             continue;
    2418             :         }
    2419       15026 :         if (orig_tl == NULL)
    2420           0 :             elog(ERROR, "UPDATE target count mismatch --- internal error");
    2421       15026 :         origTarget = lfirst_node(ResTarget, orig_tl);
    2422             : 
    2423       15026 :         attrno = attnameAttNum(pstate->p_target_relation,
    2424       15026 :                                origTarget->name, true);
    2425       15026 :         if (attrno == InvalidAttrNumber)
    2426           8 :             ereport(ERROR,
    2427             :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
    2428             :                      errmsg("column \"%s\" of relation \"%s\" does not exist",
    2429             :                             origTarget->name,
    2430             :                             RelationGetRelationName(pstate->p_target_relation)),
    2431             :                      parser_errposition(pstate, origTarget->location)));
    2432             : 
    2433       15018 :         updateTargetListEntry(pstate, tle, origTarget->name,
    2434             :                               attrno,
    2435             :                               origTarget->indirection,
    2436             :                               origTarget->location);
    2437             : 
    2438             :         /* Mark the target column as requiring update permissions */
    2439       15010 :         target_rte->updatedCols = bms_add_member(target_rte->updatedCols,
    2440             :                                                  attrno - FirstLowInvalidHeapAttributeNumber);
    2441             : 
    2442       15010 :         orig_tl = lnext(origTlist, orig_tl);
    2443             :     }
    2444       11762 :     if (orig_tl != NULL)
    2445           0 :         elog(ERROR, "UPDATE target count mismatch --- internal error");
    2446             : 
    2447       11762 :     return tlist;
    2448             : }
    2449             : 
    2450             : /*
    2451             :  * transformReturningList -
    2452             :  *  handle a RETURNING clause in INSERT/UPDATE/DELETE
    2453             :  */
    2454             : static List *
    2455       14590 : transformReturningList(ParseState *pstate, List *returningList)
    2456             : {
    2457             :     List       *rlist;
    2458             :     int         save_next_resno;
    2459             : 
    2460       14590 :     if (returningList == NIL)
    2461       12780 :         return NIL;             /* nothing to do */
    2462             : 
    2463             :     /*
    2464             :      * We need to assign resnos starting at one in the RETURNING list. Save
    2465             :      * and restore the main tlist's value of p_next_resno, just in case
    2466             :      * someone looks at it later (probably won't happen).
    2467             :      */
    2468        1810 :     save_next_resno = pstate->p_next_resno;
    2469        1810 :     pstate->p_next_resno = 1;
    2470             : 
    2471             :     /* transform RETURNING identically to a SELECT targetlist */
    2472        1810 :     rlist = transformTargetList(pstate, returningList, EXPR_KIND_RETURNING);
    2473             : 
    2474             :     /*
    2475             :      * Complain if the nonempty tlist expanded to nothing (which is possible
    2476             :      * if it contains only a star-expansion of a zero-column table).  If we
    2477             :      * allow this, the parsed Query will look like it didn't have RETURNING,
    2478             :      * with results that would probably surprise the user.
    2479             :      */
    2480        1794 :     if (rlist == NIL)
    2481           0 :         ereport(ERROR,
    2482             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    2483             :                  errmsg("RETURNING must have at least one column"),
    2484             :                  parser_errposition(pstate,
    2485             :                                     exprLocation(linitial(returningList)))));
    2486             : 
    2487             :     /* mark column origins */
    2488        1794 :     markTargetListOrigins(pstate, rlist);
    2489             : 
    2490             :     /* resolve any still-unresolved output columns as being type text */
    2491        1794 :     if (pstate->p_resolve_unknowns)
    2492        1794 :         resolveTargetListUnknowns(pstate, rlist);
    2493             : 
    2494             :     /* restore state */
    2495        1794 :     pstate->p_next_resno = save_next_resno;
    2496             : 
    2497        1794 :     return rlist;
    2498             : }
    2499             : 
    2500             : 
    2501             : /*
    2502             :  * transformPLAssignStmt -
    2503             :  *    transform a PL/pgSQL assignment statement
    2504             :  *
    2505             :  * If there is no opt_indirection, the transformed statement looks like
    2506             :  * "SELECT a_expr ...", except the expression has been cast to the type of
    2507             :  * the target.  With indirection, it's still a SELECT, but the expression will
    2508             :  * incorporate FieldStore and/or assignment SubscriptingRef nodes to compute a
    2509             :  * new value for a container-type variable represented by the target.  The
    2510             :  * expression references the target as the container source.
    2511             :  */
    2512             : static Query *
    2513        3718 : transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt)
    2514             : {
    2515        3718 :     Query      *qry = makeNode(Query);
    2516        3718 :     ColumnRef  *cref = makeNode(ColumnRef);
    2517        3718 :     List       *indirection = stmt->indirection;
    2518        3718 :     int         nnames = stmt->nnames;
    2519        3718 :     SelectStmt *sstmt = stmt->val;
    2520             :     Node       *target;
    2521             :     Oid         targettype;
    2522             :     int32       targettypmod;
    2523             :     Oid         targetcollation;
    2524             :     List       *tlist;
    2525             :     TargetEntry *tle;
    2526             :     Oid         type_id;
    2527             :     Node       *qual;
    2528             :     ListCell   *l;
    2529             : 
    2530             :     /*
    2531             :      * First, construct a ColumnRef for the target variable.  If the target
    2532             :      * has more than one dotted name, we have to pull the extra names out of
    2533             :      * the indirection list.
    2534             :      */
    2535        3718 :     cref->fields = list_make1(makeString(stmt->name));
    2536        3718 :     cref->location = stmt->location;
    2537        3718 :     if (nnames > 1)
    2538             :     {
    2539             :         /* avoid munging the raw parsetree */
    2540         258 :         indirection = list_copy(indirection);
    2541         530 :         while (--nnames > 0 && indirection != NIL)
    2542             :         {
    2543         272 :             Node       *ind = (Node *) linitial(indirection);
    2544             : 
    2545         272 :             if (!IsA(ind, String))
    2546           0 :                 elog(ERROR, "invalid name count in PLAssignStmt");
    2547         272 :             cref->fields = lappend(cref->fields, ind);
    2548         272 :             indirection = list_delete_first(indirection);
    2549             :         }
    2550             :     }
    2551             : 
    2552             :     /*
    2553             :      * Transform the target reference.  Typically we will get back a Param
    2554             :      * node, but there's no reason to be too picky about its type.
    2555             :      */
    2556        3718 :     target = transformExpr(pstate, (Node *) cref,
    2557             :                            EXPR_KIND_UPDATE_TARGET);
    2558        3706 :     targettype = exprType(target);
    2559        3706 :     targettypmod = exprTypmod(target);
    2560        3706 :     targetcollation = exprCollation(target);
    2561             : 
    2562             :     /*
    2563             :      * The rest mostly matches transformSelectStmt, except that we needn't
    2564             :      * consider WITH or INTO, and we build a targetlist our own way.
    2565             :      */
    2566        3706 :     qry->commandType = CMD_SELECT;
    2567        3706 :     pstate->p_is_insert = false;
    2568             : 
    2569             :     /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
    2570        3706 :     pstate->p_locking_clause = sstmt->lockingClause;
    2571             : 
    2572             :     /* make WINDOW info available for window functions, too */
    2573        3706 :     pstate->p_windowdefs = sstmt->windowClause;
    2574             : 
    2575             :     /* process the FROM clause */
    2576        3706 :     transformFromClause(pstate, sstmt->fromClause);
    2577             : 
    2578             :     /* initially transform the targetlist as if in SELECT */
    2579        3706 :     tlist = transformTargetList(pstate, sstmt->targetList,
    2580             :                                 EXPR_KIND_SELECT_TARGET);
    2581             : 
    2582             :     /* we should have exactly one targetlist item */
    2583        3706 :     if (list_length(tlist) != 1)
    2584           4 :         ereport(ERROR,
    2585             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    2586             :                  errmsg_plural("assignment source returned %d column",
    2587             :                                "assignment source returned %d columns",
    2588             :                                list_length(tlist),
    2589             :                                list_length(tlist))));
    2590             : 
    2591        3702 :     tle = linitial_node(TargetEntry, tlist);
    2592             : 
    2593             :     /*
    2594             :      * This next bit is similar to transformAssignedExpr; the key difference
    2595             :      * is we use COERCION_PLPGSQL not COERCION_ASSIGNMENT.
    2596             :      */
    2597        3702 :     type_id = exprType((Node *) tle->expr);
    2598             : 
    2599        3702 :     pstate->p_expr_kind = EXPR_KIND_UPDATE_TARGET;
    2600             : 
    2601        3702 :     if (indirection)
    2602             :     {
    2603          68 :         tle->expr = (Expr *)
    2604          78 :             transformAssignmentIndirection(pstate,
    2605             :                                            target,
    2606          78 :                                            stmt->name,
    2607             :                                            false,
    2608             :                                            targettype,
    2609             :                                            targettypmod,
    2610             :                                            targetcollation,
    2611             :                                            indirection,
    2612             :                                            list_head(indirection),
    2613          78 :                                            (Node *) tle->expr,
    2614             :                                            COERCION_PLPGSQL,
    2615             :                                            exprLocation(target));
    2616             :     }
    2617        3624 :     else if (targettype != type_id &&
    2618        1400 :              (targettype == RECORDOID || ISCOMPLEX(targettype)) &&
    2619         448 :              (type_id == RECORDOID || ISCOMPLEX(type_id)))
    2620             :     {
    2621             :         /*
    2622             :          * Hack: do not let coerce_to_target_type() deal with inconsistent
    2623             :          * composite types.  Just pass the expression result through as-is,
    2624             :          * and let the PL/pgSQL executor do the conversion its way.  This is
    2625             :          * rather bogus, but it's needed for backwards compatibility.
    2626             :          */
    2627             :     }
    2628             :     else
    2629             :     {
    2630             :         /*
    2631             :          * For normal non-qualified target column, do type checking and
    2632             :          * coercion.
    2633             :          */
    2634        3272 :         Node       *orig_expr = (Node *) tle->expr;
    2635             : 
    2636        3272 :         tle->expr = (Expr *)
    2637        3272 :             coerce_to_target_type(pstate,
    2638             :                                   orig_expr, type_id,
    2639             :                                   targettype, targettypmod,
    2640             :                                   COERCION_PLPGSQL,
    2641             :                                   COERCE_IMPLICIT_CAST,
    2642             :                                   -1);
    2643             :         /* With COERCION_PLPGSQL, this error is probably unreachable */
    2644        3272 :         if (tle->expr == NULL)
    2645           0 :             ereport(ERROR,
    2646             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
    2647             :                      errmsg("variable \"%s\" is of type %s"
    2648             :                             " but expression is of type %s",
    2649             :                             stmt->name,
    2650             :                             format_type_be(targettype),
    2651             :                             format_type_be(type_id)),
    2652             :                      errhint("You will need to rewrite or cast the expression."),
    2653             :                      parser_errposition(pstate, exprLocation(orig_expr))));
    2654             :     }
    2655             : 
    2656        3692 :     pstate->p_expr_kind = EXPR_KIND_NONE;
    2657             : 
    2658        3692 :     qry->targetList = list_make1(tle);
    2659             : 
    2660             :     /* transform WHERE */
    2661        3692 :     qual = transformWhereClause(pstate, sstmt->whereClause,
    2662             :                                 EXPR_KIND_WHERE, "WHERE");
    2663             : 
    2664             :     /* initial processing of HAVING clause is much like WHERE clause */
    2665        3692 :     qry->havingQual = transformWhereClause(pstate, sstmt->havingClause,
    2666             :                                            EXPR_KIND_HAVING, "HAVING");
    2667             : 
    2668             :     /*
    2669             :      * Transform sorting/grouping stuff.  Do ORDER BY first because both
    2670             :      * transformGroupClause and transformDistinctClause need the results. Note
    2671             :      * that these functions can also change the targetList, so it's passed to
    2672             :      * them by reference.
    2673             :      */
    2674        3692 :     qry->sortClause = transformSortClause(pstate,
    2675             :                                           sstmt->sortClause,
    2676             :                                           &qry->targetList,
    2677             :                                           EXPR_KIND_ORDER_BY,
    2678             :                                           false /* allow SQL92 rules */ );
    2679             : 
    2680        3692 :     qry->groupClause = transformGroupClause(pstate,
    2681             :                                             sstmt->groupClause,
    2682             :                                             &qry->groupingSets,
    2683             :                                             &qry->targetList,
    2684             :                                             qry->sortClause,
    2685             :                                             EXPR_KIND_GROUP_BY,
    2686             :                                             false /* allow SQL92 rules */ );
    2687             : 
    2688        3692 :     if (sstmt->distinctClause == NIL)
    2689             :     {
    2690        3692 :         qry->distinctClause = NIL;
    2691        3692 :         qry->hasDistinctOn = false;
    2692             :     }
    2693           0 :     else if (linitial(sstmt->distinctClause) == NULL)
    2694             :     {
    2695             :         /* We had SELECT DISTINCT */
    2696           0 :         qry->distinctClause = transformDistinctClause(pstate,
    2697             :                                                       &qry->targetList,
    2698             :                                                       qry->sortClause,
    2699             :                                                       false);
    2700           0 :         qry->hasDistinctOn = false;
    2701             :     }
    2702             :     else
    2703             :     {
    2704             :         /* We had SELECT DISTINCT ON */
    2705           0 :         qry->distinctClause = transformDistinctOnClause(pstate,
    2706             :                                                         sstmt->distinctClause,
    2707             :                                                         &qry->targetList,
    2708             :                                                         qry->sortClause);
    2709           0 :         qry->hasDistinctOn = true;
    2710             :     }
    2711             : 
    2712             :     /* transform LIMIT */
    2713        3692 :     qry->limitOffset = transformLimitClause(pstate, sstmt->limitOffset,
    2714             :                                             EXPR_KIND_OFFSET, "OFFSET",
    2715             :                                             sstmt->limitOption);
    2716        3692 :     qry->limitCount = transformLimitClause(pstate, sstmt->limitCount,
    2717             :                                            EXPR_KIND_LIMIT, "LIMIT",
    2718             :                                            sstmt->limitOption);
    2719        3692 :     qry->limitOption = sstmt->limitOption;
    2720             : 
    2721             :     /* transform window clauses after we have seen all window functions */
    2722        3692 :     qry->windowClause = transformWindowDefinitions(pstate,
    2723             :                                                    pstate->p_windowdefs,
    2724             :                                                    &qry->targetList);
    2725             : 
    2726        3692 :     qry->rtable = pstate->p_rtable;
    2727        3692 :     qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
    2728             : 
    2729        3692 :     qry->hasSubLinks = pstate->p_hasSubLinks;
    2730        3692 :     qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
    2731        3692 :     qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
    2732        3692 :     qry->hasAggs = pstate->p_hasAggs;
    2733             : 
    2734        3694 :     foreach(l, sstmt->lockingClause)
    2735             :     {
    2736           2 :         transformLockingClause(pstate, qry,
    2737           2 :                                (LockingClause *) lfirst(l), false);
    2738             :     }
    2739             : 
    2740        3692 :     assign_query_collations(pstate, qry);
    2741             : 
    2742             :     /* this must be done after collations, for reliable comparison of exprs */
    2743        3692 :     if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
    2744           4 :         parseCheckAggregates(pstate, qry);
    2745             : 
    2746        3692 :     return qry;
    2747             : }
    2748             : 
    2749             : 
    2750             : /*
    2751             :  * transformDeclareCursorStmt -
    2752             :  *  transform a DECLARE CURSOR Statement
    2753             :  *
    2754             :  * DECLARE CURSOR is like other utility statements in that we emit it as a
    2755             :  * CMD_UTILITY Query node; however, we must first transform the contained
    2756             :  * query.  We used to postpone that until execution, but it's really necessary
    2757             :  * to do it during the normal parse analysis phase to ensure that side effects
    2758             :  * of parser hooks happen at the expected time.
    2759             :  */
    2760             : static Query *
    2761        2398 : transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
    2762             : {
    2763             :     Query      *result;
    2764             :     Query      *query;
    2765             : 
    2766        2398 :     if ((stmt->options & CURSOR_OPT_SCROLL) &&
    2767         160 :         (stmt->options & CURSOR_OPT_NO_SCROLL))
    2768           0 :         ereport(ERROR,
    2769             :                 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
    2770             :         /* translator: %s is a SQL keyword */
    2771             :                  errmsg("cannot specify both %s and %s",
    2772             :                         "SCROLL", "NO SCROLL")));
    2773             : 
    2774        2398 :     if ((stmt->options & CURSOR_OPT_ASENSITIVE) &&
    2775           0 :         (stmt->options & CURSOR_OPT_INSENSITIVE))
    2776           0 :         ereport(ERROR,
    2777             :                 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
    2778             :         /* translator: %s is a SQL keyword */
    2779             :                  errmsg("cannot specify both %s and %s",
    2780             :                         "ASENSITIVE", "INSENSITIVE")));
    2781             : 
    2782             :     /* Transform contained query, not allowing SELECT INTO */
    2783        2398 :     query = transformStmt(pstate, stmt->query);
    2784        2382 :     stmt->query = (Node *) query;
    2785             : 
    2786             :     /* Grammar should not have allowed anything but SELECT */
    2787        2382 :     if (!IsA(query, Query) ||
    2788        2382 :         query->commandType != CMD_SELECT)
    2789           0 :         elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
    2790             : 
    2791             :     /*
    2792             :      * We also disallow data-modifying WITH in a cursor.  (This could be
    2793             :      * allowed, but the semantics of when the updates occur might be
    2794             :      * surprising.)
    2795             :      */
    2796        2382 :     if (query->hasModifyingCTE)
    2797           0 :         ereport(ERROR,
    2798             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2799             :                  errmsg("DECLARE CURSOR must not contain data-modifying statements in WITH")));
    2800             : 
    2801             :     /* FOR UPDATE and WITH HOLD are not compatible */
    2802        2382 :     if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
    2803           0 :         ereport(ERROR,
    2804             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2805             :         /*------
    2806             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    2807             :                  errmsg("DECLARE CURSOR WITH HOLD ... %s is not supported",
    2808             :                         LCS_asString(((RowMarkClause *)
    2809             :                                       linitial(query->rowMarks))->strength)),
    2810             :                  errdetail("Holdable cursors must be READ ONLY.")));
    2811             : 
    2812             :     /* FOR UPDATE and SCROLL are not compatible */
    2813        2382 :     if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
    2814           0 :         ereport(ERROR,
    2815             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2816             :         /*------
    2817             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    2818             :                  errmsg("DECLARE SCROLL CURSOR ... %s is not supported",
    2819             :                         LCS_asString(((RowMarkClause *)
    2820             :                                       linitial(query->rowMarks))->strength)),
    2821             :                  errdetail("Scrollable cursors must be READ ONLY.")));
    2822             : 
    2823             :     /* FOR UPDATE and INSENSITIVE are not compatible */
    2824        2382 :     if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
    2825           0 :         ereport(ERROR,
    2826             :                 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
    2827             :         /*------
    2828             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    2829             :                  errmsg("DECLARE INSENSITIVE CURSOR ... %s is not valid",
    2830             :                         LCS_asString(((RowMarkClause *)
    2831             :                                       linitial(query->rowMarks))->strength)),
    2832             :                  errdetail("Insensitive cursors must be READ ONLY.")));
    2833             : 
    2834             :     /* represent the command as a utility Query */
    2835        2382 :     result = makeNode(Query);
    2836        2382 :     result->commandType = CMD_UTILITY;
    2837        2382 :     result->utilityStmt = (Node *) stmt;
    2838             : 
    2839        2382 :     return result;
    2840             : }
    2841             : 
    2842             : 
    2843             : /*
    2844             :  * transformExplainStmt -
    2845             :  *  transform an EXPLAIN Statement
    2846             :  *
    2847             :  * EXPLAIN is like other utility statements in that we emit it as a
    2848             :  * CMD_UTILITY Query node; however, we must first transform the contained
    2849             :  * query.  We used to postpone that until execution, but it's really necessary
    2850             :  * to do it during the normal parse analysis phase to ensure that side effects
    2851             :  * of parser hooks happen at the expected time.
    2852             :  */
    2853             : static Query *
    2854       13412 : transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
    2855             : {
    2856             :     Query      *result;
    2857             : 
    2858             :     /* transform contained query, allowing SELECT INTO */
    2859       13412 :     stmt->query = (Node *) transformOptionalSelectInto(pstate, stmt->query);
    2860             : 
    2861             :     /* represent the command as a utility Query */
    2862       13408 :     result = makeNode(Query);
    2863       13408 :     result->commandType = CMD_UTILITY;
    2864       13408 :     result->utilityStmt = (Node *) stmt;
    2865             : 
    2866       13408 :     return result;
    2867             : }
    2868             : 
    2869             : 
    2870             : /*
    2871             :  * transformCreateTableAsStmt -
    2872             :  *  transform a CREATE TABLE AS, SELECT ... INTO, or CREATE MATERIALIZED VIEW
    2873             :  *  Statement
    2874             :  *
    2875             :  * As with DECLARE CURSOR and EXPLAIN, transform the contained statement now.
    2876             :  */
    2877             : static Query *
    2878        1224 : transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
    2879             : {
    2880             :     Query      *result;
    2881             :     Query      *query;
    2882             : 
    2883             :     /* transform contained query, not allowing SELECT INTO */
    2884        1224 :     query = transformStmt(pstate, stmt->query);
    2885        1224 :     stmt->query = (Node *) query;
    2886             : 
    2887             :     /* additional work needed for CREATE MATERIALIZED VIEW */
    2888        1224 :     if (stmt->objtype == OBJECT_MATVIEW)
    2889             :     {
    2890             :         /*
    2891             :          * Prohibit a data-modifying CTE in the query used to create a
    2892             :          * materialized view. It's not sufficiently clear what the user would
    2893             :          * want to happen if the MV is refreshed or incrementally maintained.
    2894             :          */
    2895         342 :         if (query->hasModifyingCTE)
    2896           0 :             ereport(ERROR,
    2897             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2898             :                      errmsg("materialized views must not use data-modifying statements in WITH")));
    2899             : 
    2900             :         /*
    2901             :          * Check whether any temporary database objects are used in the
    2902             :          * creation query. It would be hard to refresh data or incrementally
    2903             :          * maintain it if a source disappeared.
    2904             :          */
    2905         342 :         if (isQueryUsingTempRelation(query))
    2906           0 :             ereport(ERROR,
    2907             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2908             :                      errmsg("materialized views must not use temporary tables or views")));
    2909             : 
    2910             :         /*
    2911             :          * A materialized view would either need to save parameters for use in
    2912             :          * maintaining/loading the data or prohibit them entirely.  The latter
    2913             :          * seems safer and more sane.
    2914             :          */
    2915         342 :         if (query_contains_extern_params(query))
    2916           0 :             ereport(ERROR,
    2917             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2918             :                      errmsg("materialized views may not be defined using bound parameters")));
    2919             : 
    2920             :         /*
    2921             :          * For now, we disallow unlogged materialized views, because it seems
    2922             :          * like a bad idea for them to just go to empty after a crash. (If we
    2923             :          * could mark them as unpopulated, that would be better, but that
    2924             :          * requires catalog changes which crash recovery can't presently
    2925             :          * handle.)
    2926             :          */
    2927         342 :         if (stmt->into->rel->relpersistence == RELPERSISTENCE_UNLOGGED)
    2928           0 :             ereport(ERROR,
    2929             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2930             :                      errmsg("materialized views cannot be unlogged")));
    2931             : 
    2932             :         /*
    2933             :          * At runtime, we'll need a copy of the parsed-but-not-rewritten Query
    2934             :          * for purposes of creating the view's ON SELECT rule.  We stash that
    2935             :          * in the IntoClause because that's where intorel_startup() can
    2936             :          * conveniently get it from.
    2937             :          */
    2938         342 :         stmt->into->viewQuery = (Node *) copyObject(query);
    2939             :     }
    2940             : 
    2941             :     /* represent the command as a utility Query */
    2942        1224 :     result = makeNode(Query);
    2943        1224 :     result->commandType = CMD_UTILITY;
    2944        1224 :     result->utilityStmt = (Node *) stmt;
    2945             : 
    2946        1224 :     return result;
    2947             : }
    2948             : 
    2949             : /*
    2950             :  * transform a CallStmt
    2951             :  */
    2952             : static Query *
    2953         326 : transformCallStmt(ParseState *pstate, CallStmt *stmt)
    2954             : {
    2955             :     List       *targs;
    2956             :     ListCell   *lc;
    2957             :     Node       *node;
    2958             :     FuncExpr   *fexpr;
    2959             :     HeapTuple   proctup;
    2960             :     Datum       proargmodes;
    2961             :     bool        isNull;
    2962         326 :     List       *outargs = NIL;
    2963             :     Query      *result;
    2964             : 
    2965             :     /*
    2966             :      * First, do standard parse analysis on the procedure call and its
    2967             :      * arguments, allowing us to identify the called procedure.
    2968             :      */
    2969         326 :     targs = NIL;
    2970         806 :     foreach(lc, stmt->funccall->args)
    2971             :     {
    2972         480 :         targs = lappend(targs, transformExpr(pstate,
    2973         480 :                                              (Node *) lfirst(lc),
    2974             :                                              EXPR_KIND_CALL_ARGUMENT));
    2975             :     }
    2976             : 
    2977         326 :     node = ParseFuncOrColumn(pstate,
    2978         326 :                              stmt->funccall->funcname,
    2979             :                              targs,
    2980             :                              pstate->p_last_srf,
    2981             :                              stmt->funccall,
    2982             :                              true,
    2983         326 :                              stmt->funccall->location);
    2984             : 
    2985         306 :     assign_expr_collations(pstate, node);
    2986             : 
    2987         306 :     fexpr = castNode(FuncExpr, node);
    2988             : 
    2989         306 :     proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fexpr->funcid));
    2990         306 :     if (!HeapTupleIsValid(proctup))
    2991           0 :         elog(ERROR, "cache lookup failed for function %u", fexpr->funcid);
    2992             : 
    2993             :     /*
    2994             :      * Expand the argument list to deal with named-argument notation and
    2995             :      * default arguments.  For ordinary FuncExprs this'd be done during
    2996             :      * planning, but a CallStmt doesn't go through planning, and there seems
    2997             :      * no good reason not to do it here.
    2998             :      */
    2999         306 :     fexpr->args = expand_function_arguments(fexpr->args,
    3000             :                                             true,
    3001             :                                             fexpr->funcresulttype,
    3002             :                                             proctup);
    3003             : 
    3004             :     /* Fetch proargmodes; if it's null, there are no output args */
    3005         306 :     proargmodes = SysCacheGetAttr(PROCOID, proctup,
    3006             :                                   Anum_pg_proc_proargmodes,
    3007             :                                   &isNull);
    3008         306 :     if (!isNull)
    3009             :     {
    3010             :         /*
    3011             :          * Split the list into input arguments in fexpr->args and output
    3012             :          * arguments in stmt->outargs.  INOUT arguments appear in both lists.
    3013             :          */
    3014             :         ArrayType  *arr;
    3015             :         int         numargs;
    3016             :         char       *argmodes;
    3017             :         List       *inargs;
    3018             :         int         i;
    3019             : 
    3020         130 :         arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
    3021         130 :         numargs = list_length(fexpr->args);
    3022         130 :         if (ARR_NDIM(arr) != 1 ||
    3023         130 :             ARR_DIMS(arr)[0] != numargs ||
    3024         130 :             ARR_HASNULL(arr) ||
    3025         130 :             ARR_ELEMTYPE(arr) != CHAROID)
    3026           0 :             elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
    3027             :                  numargs);
    3028         130 :         argmodes = (char *) ARR_DATA_PTR(arr);
    3029             : 
    3030         130 :         inargs = NIL;
    3031         130 :         i = 0;
    3032         442 :         foreach(lc, fexpr->args)
    3033             :         {
    3034         312 :             Node       *n = lfirst(lc);
    3035             : 
    3036         312 :             switch (argmodes[i])
    3037             :             {
    3038         114 :                 case PROARGMODE_IN:
    3039             :                 case PROARGMODE_VARIADIC:
    3040         114 :                     inargs = lappend(inargs, n);
    3041         114 :                     break;
    3042          56 :                 case PROARGMODE_OUT:
    3043          56 :                     outargs = lappend(outargs, n);
    3044          56 :                     break;
    3045         142 :                 case PROARGMODE_INOUT:
    3046         142 :                     inargs = lappend(inargs, n);
    3047         142 :                     outargs = lappend(outargs, copyObject(n));
    3048         142 :                     break;
    3049           0 :                 default:
    3050             :                     /* note we don't support PROARGMODE_TABLE */
    3051           0 :                     elog(ERROR, "invalid argmode %c for procedure",
    3052             :                          argmodes[i]);
    3053             :                     break;
    3054             :             }
    3055         312 :             i++;
    3056             :         }
    3057         130 :         fexpr->args = inargs;
    3058             :     }
    3059             : 
    3060         306 :     stmt->funcexpr = fexpr;
    3061         306 :     stmt->outargs = outargs;
    3062             : 
    3063         306 :     ReleaseSysCache(proctup);
    3064             : 
    3065             :     /* represent the command as a utility Query */
    3066         306 :     result = makeNode(Query);
    3067         306 :     result->commandType = CMD_UTILITY;
    3068         306 :     result->utilityStmt = (Node *) stmt;
    3069             : 
    3070         306 :     return result;
    3071             : }
    3072             : 
    3073             : /*
    3074             :  * Produce a string representation of a LockClauseStrength value.
    3075             :  * This should only be applied to valid values (not LCS_NONE).
    3076             :  */
    3077             : const char *
    3078          20 : LCS_asString(LockClauseStrength strength)
    3079             : {
    3080          20 :     switch (strength)
    3081             :     {
    3082           0 :         case LCS_NONE:
    3083             :             Assert(false);
    3084           0 :             break;
    3085           0 :         case LCS_FORKEYSHARE:
    3086           0 :             return "FOR KEY SHARE";
    3087           0 :         case LCS_FORSHARE:
    3088           0 :             return "FOR SHARE";
    3089           4 :         case LCS_FORNOKEYUPDATE:
    3090           4 :             return "FOR NO KEY UPDATE";
    3091          16 :         case LCS_FORUPDATE:
    3092          16 :             return "FOR UPDATE";
    3093             :     }
    3094           0 :     return "FOR some";            /* shouldn't happen */
    3095             : }
    3096             : 
    3097             : /*
    3098             :  * Check for features that are not supported with FOR [KEY] UPDATE/SHARE.
    3099             :  *
    3100             :  * exported so planner can check again after rewriting, query pullup, etc
    3101             :  */
    3102             : void
    3103        9106 : CheckSelectLocking(Query *qry, LockClauseStrength strength)
    3104             : {
    3105             :     Assert(strength != LCS_NONE);   /* else caller error */
    3106             : 
    3107        9106 :     if (qry->setOperations)
    3108           0 :         ereport(ERROR,
    3109             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3110             :         /*------
    3111             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    3112             :                  errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
    3113             :                         LCS_asString(strength))));
    3114        9106 :     if (qry->distinctClause != NIL)
    3115           0 :         ereport(ERROR,
    3116             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3117             :         /*------
    3118             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    3119             :                  errmsg("%s is not allowed with DISTINCT clause",
    3120             :                         LCS_asString(strength))));
    3121        9106 :     if (qry->groupClause != NIL || qry->groupingSets != NIL)
    3122           8 :         ereport(ERROR,
    3123             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3124             :         /*------
    3125             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    3126             :                  errmsg("%s is not allowed with GROUP BY clause",
    3127             :                         LCS_asString(strength))));
    3128        9098 :     if (qry->havingQual != NULL)
    3129           0 :         ereport(ERROR,
    3130             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3131             :         /*------
    3132             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    3133             :                  errmsg("%s is not allowed with HAVING clause",
    3134             :                         LCS_asString(strength))));
    3135        9098 :     if (qry->hasAggs)
    3136           4 :         ereport(ERROR,
    3137             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3138             :         /*------
    3139             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    3140             :                  errmsg("%s is not allowed with aggregate functions",
    3141             :                         LCS_asString(strength))));
    3142        9094 :     if (qry->hasWindowFuncs)
    3143           0 :         ereport(ERROR,
    3144             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3145             :         /*------
    3146             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    3147             :                  errmsg("%s is not allowed with window functions",
    3148             :                         LCS_asString(strength))));
    3149        9094 :     if (qry->hasTargetSRFs)
    3150           0 :         ereport(ERROR,
    3151             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3152             :         /*------
    3153             :           translator: %s is a SQL row locking clause such as FOR UPDATE */
    3154             :                  errmsg("%s is not allowed with set-returning functions in the target list",
    3155             :                         LCS_asString(strength))));
    3156        9094 : }
    3157             : 
    3158             : /*
    3159             :  * Transform a FOR [KEY] UPDATE/SHARE clause
    3160             :  *
    3161             :  * This basically involves replacing names by integer relids.
    3162             :  *
    3163             :  * NB: if you need to change this, see also markQueryForLocking()
    3164             :  * in rewriteHandler.c, and isLockedRefname() in parse_relation.c.
    3165             :  */
    3166             : static void
    3167        3978 : transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc,
    3168             :                        bool pushedDown)
    3169             : {
    3170        3978 :     List       *lockedRels = lc->lockedRels;
    3171             :     ListCell   *l;
    3172             :     ListCell   *rt;
    3173             :     Index       i;
    3174             :     LockingClause *allrels;
    3175             : 
    3176        3978 :     CheckSelectLocking(qry, lc->strength);
    3177             : 
    3178             :     /* make a clause we can pass down to subqueries to select all rels */
    3179        3966 :     allrels = makeNode(LockingClause);
    3180        3966 :     allrels->lockedRels = NIL;   /* indicates all rels */
    3181        3966 :     allrels->strength = lc->strength;
    3182        3966 :     allrels->waitPolicy = lc->waitPolicy;
    3183             : 
    3184        3966 :     if (lockedRels == NIL)
    3185             :     {
    3186             :         /*
    3187             :          * Lock all regular tables used in query and its subqueries.  We
    3188             :          * examine inFromCl to exclude auto-added RTEs, particularly NEW/OLD
    3189             :          * in rules.  This is a bit of an abuse of a mostly-obsolete flag, but
    3190             :          * it's convenient.  We can't rely on the namespace mechanism that has
    3191             :          * largely replaced inFromCl, since for example we need to lock
    3192             :          * base-relation RTEs even if they are masked by upper joins.
    3193             :          */
    3194        1594 :         i = 0;
    3195        3256 :         foreach(rt, qry->rtable)
    3196             :         {
    3197        1662 :             RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
    3198             : 
    3199        1662 :             ++i;
    3200        1662 :             if (!rte->inFromCl)
    3201           8 :                 continue;
    3202        1654 :             switch (rte->rtekind)
    3203             :             {
    3204        1626 :                 case RTE_RELATION:
    3205        1626 :                     applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
    3206             :                                        pushedDown);
    3207        1626 :                     rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
    3208        1626 :                     break;
    3209           0 :                 case RTE_SUBQUERY:
    3210           0 :                     applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
    3211             :                                        pushedDown);
    3212             : 
    3213             :                     /*
    3214             :                      * FOR UPDATE/SHARE of subquery is propagated to all of
    3215             :                      * subquery's rels, too.  We could do this later (based on
    3216             :                      * the marking of the subquery RTE) but it is convenient
    3217             :                      * to have local knowledge in each query level about which
    3218             :                      * rels need to be opened with RowShareLock.
    3219             :                      */
    3220           0 :                     transformLockingClause(pstate, rte->subquery,
    3221             :                                            allrels, true);
    3222           0 :                     break;
    3223          28 :                 default:
    3224             :                     /* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
    3225          28 :                     break;
    3226             :             }
    3227             :         }
    3228             :     }
    3229             :     else
    3230             :     {
    3231             :         /*
    3232             :          * Lock just the named tables.  As above, we allow locking any base
    3233             :          * relation regardless of alias-visibility rules, so we need to
    3234             :          * examine inFromCl to exclude OLD/NEW.
    3235             :          */
    3236        4752 :         foreach(l, lockedRels)
    3237             :         {
    3238        2384 :             RangeVar   *thisrel = (RangeVar *) lfirst(l);
    3239             : 
    3240             :             /* For simplicity we insist on unqualified alias names here */
    3241        2384 :             if (thisrel->catalogname || thisrel->schemaname)
    3242           0 :                 ereport(ERROR,
    3243             :                         (errcode(ERRCODE_SYNTAX_ERROR),
    3244             :                 /*------
    3245             :                   translator: %s is a SQL row locking clause such as FOR UPDATE */
    3246             :                          errmsg("%s must specify unqualified relation names",
    3247             :                                 LCS_asString(lc->strength)),
    3248             :                          parser_errposition(pstate, thisrel->location)));
    3249             : 
    3250        2384 :             i = 0;
    3251        2624 :             foreach(rt, qry->rtable)
    3252             :             {
    3253        2620 :                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
    3254             : 
    3255        2620 :                 ++i;
    3256        2620 :                 if (!rte->inFromCl)
    3257          16 :                     continue;
    3258        2604 :                 if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
    3259             :                 {
    3260        2380 :                     switch (rte->rtekind)
    3261             :                     {
    3262        2368 :                         case RTE_RELATION:
    3263        2368 :                             applyLockingClause(qry, i, lc->strength,
    3264             :                                                lc->waitPolicy, pushedDown);
    3265        2368 :                             rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
    3266        2368 :                             break;
    3267          12 :                         case RTE_SUBQUERY:
    3268          12 :                             applyLockingClause(qry, i, lc->strength,
    3269             :                                                lc->waitPolicy, pushedDown);
    3270             :                             /* see comment above */
    3271          12 :                             transformLockingClause(pstate, rte->subquery,
    3272             :                                                    allrels, true);
    3273          12 :                             break;
    3274           0 :                         case RTE_JOIN:
    3275           0 :                             ereport(ERROR,
    3276             :                                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3277             :                             /*------
    3278             :                               translator: %s is a SQL row locking clause such as FOR UPDATE */
    3279             :                                      errmsg("%s cannot be applied to a join",
    3280             :                                             LCS_asString(lc->strength)),
    3281             :                                      parser_errposition(pstate, thisrel->location)));
    3282             :                             break;
    3283           0 :                         case RTE_FUNCTION:
    3284           0 :                             ereport(ERROR,
    3285             :                                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3286             :                             /*------
    3287             :                               translator: %s is a SQL row locking clause such as FOR UPDATE */
    3288             :                                      errmsg("%s cannot be applied to a function",
    3289             :                                             LCS_asString(lc->strength)),
    3290             :                                      parser_errposition(pstate, thisrel->location)));
    3291             :                             break;
    3292           0 :                         case RTE_TABLEFUNC:
    3293           0 :                             ereport(ERROR,
    3294             :                                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3295             :                             /*------
    3296             :                               translator: %s is a SQL row locking clause such as FOR UPDATE */
    3297             :                                      errmsg("%s cannot be applied to a table function",
    3298             :                                             LCS_asString(lc->strength)),
    3299             :                                      parser_errposition(pstate, thisrel->location)));
    3300             :                             break;
    3301           0 :                         case RTE_VALUES:
    3302           0 :                             ereport(ERROR,
    3303             :                                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3304             :                             /*------
    3305             :                               translator: %s is a SQL row locking clause such as FOR UPDATE */
    3306             :                                      errmsg("%s cannot be applied to VALUES",
    3307             :                                             LCS_asString(lc->strength)),
    3308             :                                      parser_errposition(pstate, thisrel->location)));
    3309             :                             break;
    3310           0 :                         case RTE_CTE:
    3311           0 :                             ereport(ERROR,
    3312             :                                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3313             :                             /*------
    3314             :                               translator: %s is a SQL row locking clause such as FOR UPDATE */
    3315             :                                      errmsg("%s cannot be applied to a WITH query",
    3316             :                                             LCS_asString(lc->strength)),
    3317             :                                      parser_errposition(pstate, thisrel->location)));
    3318             :                             break;
    3319           0 :                         case RTE_NAMEDTUPLESTORE:
    3320           0 :                             ereport(ERROR,
    3321             :                                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3322             :                             /*------
    3323             :                               translator: %s is a SQL row locking clause such as FOR UPDATE */
    3324             :                                      errmsg("%s cannot be applied to a named tuplestore",
    3325             :                                             LCS_asString(lc->strength)),
    3326             :                                      parser_errposition(pstate, thisrel->location)));
    3327             :                             break;
    3328             : 
    3329             :                             /* Shouldn't be possible to see RTE_RESULT here */
    3330             : 
    3331           0 :                         default:
    3332           0 :                             elog(ERROR, "unrecognized RTE type: %d",
    3333             :                                  (int) rte->rtekind);
    3334             :                             break;
    3335             :                     }
    3336        2380 :                     break;      /* out of foreach loop */
    3337             :                 }
    3338             :             }
    3339        2384 :             if (rt == NULL)
    3340           4 :                 ereport(ERROR,
    3341             :                         (errcode(ERRCODE_UNDEFINED_TABLE),
    3342             :                 /*------
    3343             :                   translator: %s is a SQL row locking clause such as FOR UPDATE */
    3344             :                          errmsg("relation \"%s\" in %s clause not found in FROM clause",
    3345             :                                 thisrel->relname,
    3346             :                                 LCS_asString(lc->strength)),
    3347             :                          parser_errposition(pstate, thisrel->location)));
    3348             :         }
    3349             :     }
    3350        3962 : }
    3351             : 
    3352             : /*
    3353             :  * Record locking info for a single rangetable item
    3354             :  */
    3355             : void
    3356        4070 : applyLockingClause(Query *qry, Index rtindex,
    3357             :                    LockClauseStrength strength, LockWaitPolicy waitPolicy,
    3358             :                    bool pushedDown)
    3359             : {
    3360             :     RowMarkClause *rc;
    3361             : 
    3362             :     Assert(strength != LCS_NONE);   /* else caller error */
    3363             : 
    3364             :     /* If it's an explicit clause, make sure hasForUpdate gets set */
    3365        4070 :     if (!pushedDown)
    3366        4002 :         qry->hasForUpdate = true;
    3367             : 
    3368             :     /* Check for pre-existing entry for same rtindex */
    3369        4070 :     if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
    3370             :     {
    3371             :         /*
    3372             :          * If the same RTE is specified with more than one locking strength,
    3373             :          * use the strongest.  (Reasonable, since you can't take both a shared
    3374             :          * and exclusive lock at the same time; it'll end up being exclusive
    3375             :          * anyway.)
    3376             :          *
    3377             :          * Similarly, if the same RTE is specified with more than one lock
    3378             :          * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
    3379             :          * turn wins over waiting for the lock (the default).  This is a bit
    3380             :          * more debatable but raising an error doesn't seem helpful. (Consider
    3381             :          * for instance SELECT FOR UPDATE NOWAIT from a view that internally
    3382             :          * contains a plain FOR UPDATE spec.)  Having NOWAIT win over SKIP
    3383             :          * LOCKED is reasonable since the former throws an error in case of
    3384             :          * coming across a locked tuple, which may be undesirable in some
    3385             :          * cases but it seems better than silently returning inconsistent
    3386             :          * results.
    3387             :          *
    3388             :          * And of course pushedDown becomes false if any clause is explicit.
    3389             :          */
    3390           0 :         rc->strength = Max(rc->strength, strength);
    3391           0 :         rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
    3392           0 :         rc->pushedDown &= pushedDown;
    3393           0 :         return;
    3394             :     }
    3395             : 
    3396             :     /* Make a new RowMarkClause */
    3397        4070 :     rc = makeNode(RowMarkClause);
    3398        4070 :     rc->rti = rtindex;
    3399        4070 :     rc->strength = strength;
    3400        4070 :     rc->waitPolicy = waitPolicy;
    3401        4070 :     rc->pushedDown = pushedDown;
    3402        4070 :     qry->rowMarks = lappend(qry->rowMarks, rc);
    3403             : }
    3404             : 
    3405             : /*
    3406             :  * Coverage testing for raw_expression_tree_walker().
    3407             :  *
    3408             :  * When enabled, we run raw_expression_tree_walker() over every DML statement
    3409             :  * submitted to parse analysis.  Without this provision, that function is only
    3410             :  * applied in limited cases involving CTEs, and we don't really want to have
    3411             :  * to test everything inside as well as outside a CTE.
    3412             :  */
    3413             : #ifdef RAW_EXPRESSION_COVERAGE_TEST
    3414             : 
    3415             : static bool
    3416    41665800 : test_raw_expression_coverage(Node *node, void *context)
    3417             : {
    3418    41665800 :     if (node == NULL)
    3419    20439560 :         return false;
    3420    21226240 :     return raw_expression_tree_walker(node,
    3421             :                                       test_raw_expression_coverage,
    3422             :                                       context);
    3423             : }
    3424             : 
    3425             : #endif                          /* RAW_EXPRESSION_COVERAGE_TEST */

Generated by: LCOV version 1.14