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

Generated by: LCOV version 2.0-1