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

Generated by: LCOV version 2.0-1