PostgreSQL Source Code git master
analyze.c File Reference
#include "postgres.h"
#include "access/sysattr.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/queryjumble.h"
#include "optimizer/optimizer.h"
#include "parser/analyze.h"
#include "parser/parse_agg.h"
#include "parser/parse_clause.h"
#include "parser/parse_coerce.h"
#include "parser/parse_collate.h"
#include "parser/parse_cte.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h"
#include "parser/parse_merge.h"
#include "parser/parse_oper.h"
#include "parser/parse_param.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
#include "parser/parse_type.h"
#include "parser/parsetree.h"
#include "utils/backend_status.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for analyze.c:

Go to the source code of this file.

Functions

static QuerytransformOptionalSelectInto (ParseState *pstate, Node *parseTree)
 
static QuerytransformDeleteStmt (ParseState *pstate, DeleteStmt *stmt)
 
static QuerytransformInsertStmt (ParseState *pstate, InsertStmt *stmt)
 
static OnConflictExprtransformOnConflictClause (ParseState *pstate, OnConflictClause *onConflictClause)
 
static int count_rowexpr_columns (ParseState *pstate, Node *expr)
 
static QuerytransformSelectStmt (ParseState *pstate, SelectStmt *stmt)
 
static QuerytransformValuesClause (ParseState *pstate, SelectStmt *stmt)
 
static QuerytransformSetOperationStmt (ParseState *pstate, SelectStmt *stmt)
 
static NodetransformSetOperationTree (ParseState *pstate, SelectStmt *stmt, bool isTopLevel, List **targetlist)
 
static void determineRecursiveColTypes (ParseState *pstate, Node *larg, List *nrtargetlist)
 
static QuerytransformReturnStmt (ParseState *pstate, ReturnStmt *stmt)
 
static QuerytransformUpdateStmt (ParseState *pstate, UpdateStmt *stmt)
 
static QuerytransformPLAssignStmt (ParseState *pstate, PLAssignStmt *stmt)
 
static QuerytransformDeclareCursorStmt (ParseState *pstate, DeclareCursorStmt *stmt)
 
static QuerytransformExplainStmt (ParseState *pstate, ExplainStmt *stmt)
 
static QuerytransformCreateTableAsStmt (ParseState *pstate, CreateTableAsStmt *stmt)
 
static QuerytransformCallStmt (ParseState *pstate, CallStmt *stmt)
 
static void transformLockingClause (ParseState *pstate, Query *qry, LockingClause *lc, bool pushedDown)
 
Queryparse_analyze_fixedparams (RawStmt *parseTree, const char *sourceText, const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
 
Queryparse_analyze_varparams (RawStmt *parseTree, const char *sourceText, Oid **paramTypes, int *numParams, QueryEnvironment *queryEnv)
 
Queryparse_analyze_withcb (RawStmt *parseTree, const char *sourceText, ParserSetupHook parserSetup, void *parserSetupArg, QueryEnvironment *queryEnv)
 
Queryparse_sub_analyze (Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
 
static void setQueryLocationAndLength (ParseState *pstate, Query *qry, Node *parseTree)
 
QuerytransformTopLevelStmt (ParseState *pstate, RawStmt *parseTree)
 
QuerytransformStmt (ParseState *pstate, Node *parseTree)
 
bool stmt_requires_parse_analysis (RawStmt *parseTree)
 
bool analyze_requires_snapshot (RawStmt *parseTree)
 
ListtransformInsertRow (ParseState *pstate, List *exprlist, List *stmtcols, List *icolumns, List *attrnos, bool strip_indirection)
 
ListBuildOnConflictExcludedTargetlist (Relation targetrel, Index exclRelIndex)
 
SortGroupClausemakeSortGroupClauseForSetOp (Oid rescoltype, bool require_hash)
 
ListtransformUpdateTargetList (ParseState *pstate, List *origTlist)
 
static void addNSItemForReturning (ParseState *pstate, const char *aliasname, VarReturningType returning_type)
 
void transformReturningClause (ParseState *pstate, Query *qry, ReturningClause *returningClause, ParseExprKind exprKind)
 
const char * LCS_asString (LockClauseStrength strength)
 
void CheckSelectLocking (Query *qry, LockClauseStrength strength)
 
void applyLockingClause (Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
 

Variables

post_parse_analyze_hook_type post_parse_analyze_hook = NULL
 

Function Documentation

◆ addNSItemForReturning()

static void addNSItemForReturning ( ParseState pstate,
const char *  aliasname,
VarReturningType  returning_type 
)
static

Definition at line 2651 of file analyze.c.

2653{
2654 List *colnames;
2655 int numattrs;
2656 ParseNamespaceColumn *nscolumns;
2657 ParseNamespaceItem *nsitem;
2658
2659 /* copy per-column data from the target relation */
2660 colnames = pstate->p_target_nsitem->p_rte->eref->colnames;
2661 numattrs = list_length(colnames);
2662
2663 nscolumns = (ParseNamespaceColumn *)
2664 palloc(numattrs * sizeof(ParseNamespaceColumn));
2665
2666 memcpy(nscolumns, pstate->p_target_nsitem->p_nscolumns,
2667 numattrs * sizeof(ParseNamespaceColumn));
2668
2669 /* mark all columns as returning OLD/NEW */
2670 for (int i = 0; i < numattrs; i++)
2671 nscolumns[i].p_varreturningtype = returning_type;
2672
2673 /* build the nsitem, copying most fields from the target relation */
2674 nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2675 nsitem->p_names = makeAlias(aliasname, colnames);
2676 nsitem->p_rte = pstate->p_target_nsitem->p_rte;
2677 nsitem->p_rtindex = pstate->p_target_nsitem->p_rtindex;
2678 nsitem->p_perminfo = pstate->p_target_nsitem->p_perminfo;
2679 nsitem->p_nscolumns = nscolumns;
2680 nsitem->p_returning_type = returning_type;
2681
2682 /* add it to the query namespace as a table-only item */
2683 addNSItemToQuery(pstate, nsitem, false, true, false);
2684}
int i
Definition: isn.c:72
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:391
void * palloc(Size size)
Definition: mcxt.c:1317
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
static int list_length(const List *l)
Definition: pg_list.h:152
Definition: pg_list.h:54
RangeTblEntry * p_rte
Definition: parse_node.h:311
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:315
RTEPermissionInfo * p_perminfo
Definition: parse_node.h:313
VarReturningType p_returning_type
Definition: parse_node.h:320
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:226

References addNSItemToQuery(), i, list_length(), makeAlias(), ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseNamespaceItem::p_perminfo, ParseNamespaceItem::p_returning_type, ParseNamespaceItem::p_rte, ParseNamespaceItem::p_rtindex, ParseState::p_target_nsitem, and palloc().

Referenced by transformReturningClause().

◆ analyze_requires_snapshot()

bool analyze_requires_snapshot ( RawStmt parseTree)

Definition at line 576 of file analyze.c.

577{
578 /*
579 * Currently, this should return true in exactly the same cases that
580 * stmt_requires_parse_analysis() does, so we just invoke that function
581 * rather than duplicating it. We keep the two entry points separate for
582 * clarity of callers, since from the callers' standpoint these are
583 * different conditions.
584 *
585 * While there may someday be a statement type for which transformStmt()
586 * does something nontrivial and yet no snapshot is needed for that
587 * processing, it seems likely that making such a choice would be fragile.
588 * If you want to install an exception, document the reasoning for it in a
589 * comment.
590 */
591 return stmt_requires_parse_analysis(parseTree);
592}
bool stmt_requires_parse_analysis(RawStmt *parseTree)
Definition: analyze.c:532

References stmt_requires_parse_analysis().

Referenced by BuildCachedPlan(), exec_bind_message(), exec_parse_message(), and exec_simple_query().

◆ applyLockingClause()

void applyLockingClause ( Query qry,
Index  rtindex,
LockClauseStrength  strength,
LockWaitPolicy  waitPolicy,
bool  pushedDown 
)

Definition at line 3723 of file analyze.c.

3726{
3727 RowMarkClause *rc;
3728
3729 Assert(strength != LCS_NONE); /* else caller error */
3730
3731 /* If it's an explicit clause, make sure hasForUpdate gets set */
3732 if (!pushedDown)
3733 qry->hasForUpdate = true;
3734
3735 /* Check for pre-existing entry for same rtindex */
3736 if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
3737 {
3738 /*
3739 * If the same RTE is specified with more than one locking strength,
3740 * use the strongest. (Reasonable, since you can't take both a shared
3741 * and exclusive lock at the same time; it'll end up being exclusive
3742 * anyway.)
3743 *
3744 * Similarly, if the same RTE is specified with more than one lock
3745 * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
3746 * turn wins over waiting for the lock (the default). This is a bit
3747 * more debatable but raising an error doesn't seem helpful. (Consider
3748 * for instance SELECT FOR UPDATE NOWAIT from a view that internally
3749 * contains a plain FOR UPDATE spec.) Having NOWAIT win over SKIP
3750 * LOCKED is reasonable since the former throws an error in case of
3751 * coming across a locked tuple, which may be undesirable in some
3752 * cases but it seems better than silently returning inconsistent
3753 * results.
3754 *
3755 * And of course pushedDown becomes false if any clause is explicit.
3756 */
3757 rc->strength = Max(rc->strength, strength);
3758 rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
3759 rc->pushedDown &= pushedDown;
3760 return;
3761 }
3762
3763 /* Make a new RowMarkClause */
3764 rc = makeNode(RowMarkClause);
3765 rc->rti = rtindex;
3766 rc->strength = strength;
3767 rc->waitPolicy = waitPolicy;
3768 rc->pushedDown = pushedDown;
3769 qry->rowMarks = lappend(qry->rowMarks, rc);
3770}
#define Max(x, y)
Definition: c.h:955
#define Assert(condition)
Definition: c.h:815
List * lappend(List *list, void *datum)
Definition: list.c:339
@ LCS_NONE
Definition: lockoptions.h:23
#define makeNode(_type_)
Definition: nodes.h:155
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
List * rowMarks
Definition: parsenodes.h:228
LockClauseStrength strength
Definition: parsenodes.h:1589
LockWaitPolicy waitPolicy
Definition: parsenodes.h:1590

References Assert, get_parse_rowmark(), lappend(), LCS_NONE, makeNode, Max, RowMarkClause::pushedDown, Query::rowMarks, RowMarkClause::rti, RowMarkClause::strength, and RowMarkClause::waitPolicy.

Referenced by markQueryForLocking(), and transformLockingClause().

◆ BuildOnConflictExcludedTargetlist()

List * BuildOnConflictExcludedTargetlist ( Relation  targetrel,
Index  exclRelIndex 
)

Definition at line 1315 of file analyze.c.

1317{
1318 List *result = NIL;
1319 int attno;
1320 Var *var;
1321 TargetEntry *te;
1322
1323 /*
1324 * Note that resnos of the tlist must correspond to attnos of the
1325 * underlying relation, hence we need entries for dropped columns too.
1326 */
1327 for (attno = 0; attno < RelationGetNumberOfAttributes(targetrel); attno++)
1328 {
1329 Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, attno);
1330 char *name;
1331
1332 if (attr->attisdropped)
1333 {
1334 /*
1335 * can't use atttypid here, but it doesn't really matter what type
1336 * the Const claims to be.
1337 */
1338 var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
1339 name = NULL;
1340 }
1341 else
1342 {
1343 var = makeVar(exclRelIndex, attno + 1,
1344 attr->atttypid, attr->atttypmod,
1345 attr->attcollation,
1346 0);
1347 name = pstrdup(NameStr(attr->attname));
1348 }
1349
1350 te = makeTargetEntry((Expr *) var,
1351 attno + 1,
1352 name,
1353 false);
1354
1355 result = lappend(result, te);
1356 }
1357
1358 /*
1359 * Add a whole-row-Var entry to support references to "EXCLUDED.*". Like
1360 * the other entries in the EXCLUDED tlist, its resno must match the Var's
1361 * varattno, else the wrong things happen while resolving references in
1362 * setrefs.c. This is against normal conventions for targetlists, but
1363 * it's okay since we don't use this as a real tlist.
1364 */
1365 var = makeVar(exclRelIndex, InvalidAttrNumber,
1366 targetrel->rd_rel->reltype,
1367 -1, InvalidOid, 0);
1368 te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
1369 result = lappend(result, te);
1370
1371 return result;
1372}
#define InvalidAttrNumber
Definition: attnum.h:23
#define NameStr(name)
Definition: c.h:703
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:341
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:242
char * pstrdup(const char *in)
Definition: mcxt.c:1696
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define NIL
Definition: pg_list.h:68
#define InvalidOid
Definition: postgres_ext.h:37
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:511
TupleDesc rd_att
Definition: rel.h:112
Form_pg_class rd_rel
Definition: rel.h:111
Definition: primnodes.h:261
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:153
const char * name

References InvalidAttrNumber, InvalidOid, lappend(), makeNullConst(), makeTargetEntry(), makeVar(), name, NameStr, NIL, pstrdup(), RelationData::rd_att, RelationData::rd_rel, RelationGetNumberOfAttributes, and TupleDescAttr().

Referenced by rewriteTargetView(), and transformOnConflictClause().

◆ CheckSelectLocking()

void CheckSelectLocking ( Query qry,
LockClauseStrength  strength 
)

Definition at line 3432 of file analyze.c.

3433{
3434 Assert(strength != LCS_NONE); /* else caller error */
3435
3436 if (qry->setOperations)
3437 ereport(ERROR,
3438 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3439 /*------
3440 translator: %s is a SQL row locking clause such as FOR UPDATE */
3441 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
3442 LCS_asString(strength))));
3443 if (qry->distinctClause != NIL)
3444 ereport(ERROR,
3445 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3446 /*------
3447 translator: %s is a SQL row locking clause such as FOR UPDATE */
3448 errmsg("%s is not allowed with DISTINCT clause",
3449 LCS_asString(strength))));
3450 if (qry->groupClause != NIL || qry->groupingSets != NIL)
3451 ereport(ERROR,
3452 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3453 /*------
3454 translator: %s is a SQL row locking clause such as FOR UPDATE */
3455 errmsg("%s is not allowed with GROUP BY clause",
3456 LCS_asString(strength))));
3457 if (qry->havingQual != NULL)
3458 ereport(ERROR,
3459 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3460 /*------
3461 translator: %s is a SQL row locking clause such as FOR UPDATE */
3462 errmsg("%s is not allowed with HAVING clause",
3463 LCS_asString(strength))));
3464 if (qry->hasAggs)
3465 ereport(ERROR,
3466 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3467 /*------
3468 translator: %s is a SQL row locking clause such as FOR UPDATE */
3469 errmsg("%s is not allowed with aggregate functions",
3470 LCS_asString(strength))));
3471 if (qry->hasWindowFuncs)
3472 ereport(ERROR,
3473 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3474 /*------
3475 translator: %s is a SQL row locking clause such as FOR UPDATE */
3476 errmsg("%s is not allowed with window functions",
3477 LCS_asString(strength))));
3478 if (qry->hasTargetSRFs)
3479 ereport(ERROR,
3480 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3481 /*------
3482 translator: %s is a SQL row locking clause such as FOR UPDATE */
3483 errmsg("%s is not allowed with set-returning functions in the target list",
3484 LCS_asString(strength))));
3485}
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
const char * LCS_asString(LockClauseStrength strength)
Definition: analyze.c:3407
Node * setOperations
Definition: parsenodes.h:230
List * groupClause
Definition: parsenodes.h:211
Node * havingQual
Definition: parsenodes.h:216
List * groupingSets
Definition: parsenodes.h:214
List * distinctClause
Definition: parsenodes.h:220

References Assert, Query::distinctClause, ereport, errcode(), errmsg(), ERROR, Query::groupClause, Query::groupingSets, Query::havingQual, LCS_asString(), LCS_NONE, NIL, and Query::setOperations.

Referenced by preprocess_rowmarks(), and transformLockingClause().

◆ count_rowexpr_columns()

static int count_rowexpr_columns ( ParseState pstate,
Node expr 
)
static

Definition at line 1385 of file analyze.c.

1386{
1387 if (expr == NULL)
1388 return -1;
1389 if (IsA(expr, RowExpr))
1390 return list_length(((RowExpr *) expr)->args);
1391 if (IsA(expr, Var))
1392 {
1393 Var *var = (Var *) expr;
1394 AttrNumber attnum = var->varattno;
1395
1396 if (attnum > 0 && var->vartype == RECORDOID)
1397 {
1398 RangeTblEntry *rte;
1399
1400 rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1401 if (rte->rtekind == RTE_SUBQUERY)
1402 {
1403 /* Subselect-in-FROM: examine sub-select's output expr */
1405 attnum);
1406
1407 if (ste == NULL || ste->resjunk)
1408 return -1;
1409 expr = (Node *) ste->expr;
1410 if (IsA(expr, RowExpr))
1411 return list_length(((RowExpr *) expr)->args);
1412 }
1413 }
1414 }
1415 return -1;
1416}
int16 AttrNumber
Definition: attnum.h:21
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
@ RTE_SUBQUERY
Definition: parsenodes.h:1027
int16 attnum
Definition: pg_attribute.h:74
Definition: nodes.h:129
List * targetList
Definition: parsenodes.h:193
Query * subquery
Definition: parsenodes.h:1113
RTEKind rtekind
Definition: parsenodes.h:1056
Expr * expr
Definition: primnodes.h:2245
AttrNumber varattno
Definition: primnodes.h:273
int varno
Definition: primnodes.h:268
Index varlevelsup
Definition: primnodes.h:293

References generate_unaccent_rules::args, attnum, TargetEntry::expr, get_tle_by_resno(), GetRTEByRangeTablePosn(), if(), IsA, list_length(), RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblEntry::subquery, Query::targetList, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by transformInsertRow().

◆ determineRecursiveColTypes()

static void determineRecursiveColTypes ( ParseState pstate,
Node larg,
List nrtargetlist 
)
static

Definition at line 2425 of file analyze.c.

2426{
2427 Node *node;
2428 int leftmostRTI;
2429 Query *leftmostQuery;
2430 List *targetList;
2431 ListCell *left_tlist;
2432 ListCell *nrtl;
2433 int next_resno;
2434
2435 /*
2436 * Find leftmost leaf SELECT
2437 */
2438 node = larg;
2439 while (node && IsA(node, SetOperationStmt))
2440 node = ((SetOperationStmt *) node)->larg;
2441 Assert(node && IsA(node, RangeTblRef));
2442 leftmostRTI = ((RangeTblRef *) node)->rtindex;
2443 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
2444 Assert(leftmostQuery != NULL);
2445
2446 /*
2447 * Generate dummy targetlist using column names of leftmost select and
2448 * dummy result expressions of the non-recursive term.
2449 */
2450 targetList = NIL;
2451 next_resno = 1;
2452
2453 forboth(nrtl, nrtargetlist, left_tlist, leftmostQuery->targetList)
2454 {
2455 TargetEntry *nrtle = (TargetEntry *) lfirst(nrtl);
2456 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
2457 char *colName;
2458 TargetEntry *tle;
2459
2460 Assert(!lefttle->resjunk);
2461 colName = pstrdup(lefttle->resname);
2462 tle = makeTargetEntry(nrtle->expr,
2463 next_resno++,
2464 colName,
2465 false);
2466 targetList = lappend(targetList, tle);
2467 }
2468
2469 /* Now build CTE's output column info using dummy targetlist */
2470 analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
2471}
void analyzeCTETargetList(ParseState *pstate, CommonTableExpr *cte, List *tlist)
Definition: parse_cte.c:570
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define lfirst(lc)
Definition: pg_list.h:172
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
CommonTableExpr * p_parent_cte
Definition: parse_node.h:224
List * p_rtable
Definition: parse_node.h:212

References analyzeCTETargetList(), Assert, TargetEntry::expr, forboth, IsA, lappend(), lfirst, makeTargetEntry(), NIL, ParseState::p_parent_cte, ParseState::p_rtable, pstrdup(), rt_fetch, and Query::targetList.

Referenced by transformSetOperationTree().

◆ LCS_asString()

const char * LCS_asString ( LockClauseStrength  strength)

Definition at line 3407 of file analyze.c.

3408{
3409 switch (strength)
3410 {
3411 case LCS_NONE:
3412 Assert(false);
3413 break;
3414 case LCS_FORKEYSHARE:
3415 return "FOR KEY SHARE";
3416 case LCS_FORSHARE:
3417 return "FOR SHARE";
3418 case LCS_FORNOKEYUPDATE:
3419 return "FOR NO KEY UPDATE";
3420 case LCS_FORUPDATE:
3421 return "FOR UPDATE";
3422 }
3423 return "FOR some"; /* shouldn't happen */
3424}
@ LCS_FORUPDATE
Definition: lockoptions.h:27
@ LCS_FORSHARE
Definition: lockoptions.h:25
@ LCS_FORKEYSHARE
Definition: lockoptions.h:24
@ LCS_FORNOKEYUPDATE
Definition: lockoptions.h:26

References Assert, LCS_FORKEYSHARE, LCS_FORNOKEYUPDATE, LCS_FORSHARE, LCS_FORUPDATE, and LCS_NONE.

Referenced by CheckSelectLocking(), grouping_planner(), make_outerjoininfo(), transformDeclareCursorStmt(), transformLockingClause(), transformSetOperationStmt(), transformSetOperationTree(), and transformValuesClause().

◆ makeSortGroupClauseForSetOp()

SortGroupClause * makeSortGroupClauseForSetOp ( Oid  rescoltype,
bool  require_hash 
)

Definition at line 2046 of file analyze.c.

2047{
2049 Oid sortop;
2050 Oid eqop;
2051 bool hashable;
2052
2053 /* determine the eqop and optional sortop */
2054 get_sort_group_operators(rescoltype,
2055 false, true, false,
2056 &sortop, &eqop, NULL,
2057 &hashable);
2058
2059 /*
2060 * The type cache doesn't believe that record is hashable (see
2061 * cache_record_field_properties()), but if the caller really needs hash
2062 * support, we can assume it does. Worst case, if any components of the
2063 * record don't support hashing, we will fail at execution.
2064 */
2065 if (require_hash && (rescoltype == RECORDOID || rescoltype == RECORDARRAYOID))
2066 hashable = true;
2067
2068 /* we don't have a tlist yet, so can't assign sortgrouprefs */
2069 grpcl->tleSortGroupRef = 0;
2070 grpcl->eqop = eqop;
2071 grpcl->sortop = sortop;
2072 grpcl->reverse_sort = false; /* Sort-op is "less than", or InvalidOid */
2073 grpcl->nulls_first = false; /* OK with or without sortop */
2074 grpcl->hashable = hashable;
2075
2076 return grpcl;
2077}
void get_sort_group_operators(Oid argtype, bool needLT, bool needEQ, bool needGT, Oid *ltOpr, Oid *eqOpr, Oid *gtOpr, bool *isHashable)
Definition: parse_oper.c:180
unsigned int Oid
Definition: postgres_ext.h:32
Index tleSortGroupRef
Definition: parsenodes.h:1447

References SortGroupClause::eqop, get_sort_group_operators(), makeNode, SortGroupClause::nulls_first, SortGroupClause::reverse_sort, SortGroupClause::sortop, and SortGroupClause::tleSortGroupRef.

Referenced by rewriteSearchAndCycle(), and transformSetOperationTree().

◆ parse_analyze_fixedparams()

Query * parse_analyze_fixedparams ( RawStmt parseTree,
const char *  sourceText,
const Oid paramTypes,
int  numParams,
QueryEnvironment queryEnv 
)

Definition at line 105 of file analyze.c.

108{
109 ParseState *pstate = make_parsestate(NULL);
110 Query *query;
111 JumbleState *jstate = NULL;
112
113 Assert(sourceText != NULL); /* required as of 8.4 */
114
115 pstate->p_sourcetext = sourceText;
116
117 if (numParams > 0)
118 setup_parse_fixed_parameters(pstate, paramTypes, numParams);
119
120 pstate->p_queryEnv = queryEnv;
121
122 query = transformTopLevelStmt(pstate, parseTree);
123
124 if (IsQueryIdEnabled())
125 jstate = JumbleQuery(query);
126
128 (*post_parse_analyze_hook) (pstate, query, jstate);
129
130 free_parsestate(pstate);
131
132 pgstat_report_query_id(query->queryId, false);
133
134 return query;
135}
void pgstat_report_query_id(uint64 query_id, bool force)
void free_parsestate(ParseState *pstate)
Definition: parse_node.c:72
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:39
void setup_parse_fixed_parameters(ParseState *pstate, const Oid *paramTypes, int numParams)
Definition: parse_param.c:68
post_parse_analyze_hook_type post_parse_analyze_hook
Definition: analyze.c:59
Query * transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
Definition: analyze.c:332
static bool IsQueryIdEnabled(void)
Definition: queryjumble.h:77
JumbleState * JumbleQuery(Query *query)
QueryEnvironment * p_queryEnv
Definition: parse_node.h:239
const char * p_sourcetext
Definition: parse_node.h:209

References Assert, free_parsestate(), IsQueryIdEnabled(), JumbleQuery(), make_parsestate(), ParseState::p_queryEnv, ParseState::p_sourcetext, pgstat_report_query_id(), post_parse_analyze_hook, setup_parse_fixed_parameters(), and transformTopLevelStmt().

Referenced by DefineView(), and pg_analyze_and_rewrite_fixedparams().

◆ parse_analyze_varparams()

Query * parse_analyze_varparams ( RawStmt parseTree,
const char *  sourceText,
Oid **  paramTypes,
int *  numParams,
QueryEnvironment queryEnv 
)

Definition at line 145 of file analyze.c.

148{
149 ParseState *pstate = make_parsestate(NULL);
150 Query *query;
151 JumbleState *jstate = NULL;
152
153 Assert(sourceText != NULL); /* required as of 8.4 */
154
155 pstate->p_sourcetext = sourceText;
156
157 setup_parse_variable_parameters(pstate, paramTypes, numParams);
158
159 pstate->p_queryEnv = queryEnv;
160
161 query = transformTopLevelStmt(pstate, parseTree);
162
163 /* make sure all is well with parameter types */
164 check_variable_parameters(pstate, query);
165
166 if (IsQueryIdEnabled())
167 jstate = JumbleQuery(query);
168
170 (*post_parse_analyze_hook) (pstate, query, jstate);
171
172 free_parsestate(pstate);
173
174 pgstat_report_query_id(query->queryId, false);
175
176 return query;
177}
void check_variable_parameters(ParseState *pstate, Query *query)
Definition: parse_param.c:269
void setup_parse_variable_parameters(ParseState *pstate, Oid **paramTypes, int *numParams)
Definition: parse_param.c:84

References Assert, check_variable_parameters(), free_parsestate(), IsQueryIdEnabled(), JumbleQuery(), make_parsestate(), ParseState::p_queryEnv, ParseState::p_sourcetext, pgstat_report_query_id(), post_parse_analyze_hook, setup_parse_variable_parameters(), and transformTopLevelStmt().

Referenced by pg_analyze_and_rewrite_varparams().

◆ parse_analyze_withcb()

Query * parse_analyze_withcb ( RawStmt parseTree,
const char *  sourceText,
ParserSetupHook  parserSetup,
void *  parserSetupArg,
QueryEnvironment queryEnv 
)

Definition at line 186 of file analyze.c.

190{
191 ParseState *pstate = make_parsestate(NULL);
192 Query *query;
193 JumbleState *jstate = NULL;
194
195 Assert(sourceText != NULL); /* required as of 8.4 */
196
197 pstate->p_sourcetext = sourceText;
198 pstate->p_queryEnv = queryEnv;
199 (*parserSetup) (pstate, parserSetupArg);
200
201 query = transformTopLevelStmt(pstate, parseTree);
202
203 if (IsQueryIdEnabled())
204 jstate = JumbleQuery(query);
205
207 (*post_parse_analyze_hook) (pstate, query, jstate);
208
209 free_parsestate(pstate);
210
211 pgstat_report_query_id(query->queryId, false);
212
213 return query;
214}

References Assert, free_parsestate(), IsQueryIdEnabled(), JumbleQuery(), make_parsestate(), ParseState::p_queryEnv, ParseState::p_sourcetext, pgstat_report_query_id(), post_parse_analyze_hook, and transformTopLevelStmt().

Referenced by pg_analyze_and_rewrite_withcb().

◆ parse_sub_analyze()

Query * parse_sub_analyze ( Node parseTree,
ParseState parentParseState,
CommonTableExpr parentCTE,
bool  locked_from_parent,
bool  resolve_unknowns 
)

Definition at line 222 of file analyze.c.

226{
227 ParseState *pstate = make_parsestate(parentParseState);
228 Query *query;
229
230 pstate->p_parent_cte = parentCTE;
231 pstate->p_locked_from_parent = locked_from_parent;
232 pstate->p_resolve_unknowns = resolve_unknowns;
233
234 query = transformStmt(pstate, parseTree);
235
236 free_parsestate(pstate);
237
238 return query;
239}
Query * transformStmt(ParseState *pstate, Node *parseTree)
Definition: analyze.c:396
bool p_locked_from_parent
Definition: parse_node.h:234
bool p_resolve_unknowns
Definition: parse_node.h:236

References free_parsestate(), make_parsestate(), ParseState::p_locked_from_parent, ParseState::p_parent_cte, ParseState::p_resolve_unknowns, and transformStmt().

Referenced by analyzeCTE(), transformRangeSubselect(), transformSetOperationTree(), and transformSubLink().

◆ setQueryLocationAndLength()

static void setQueryLocationAndLength ( ParseState pstate,
Query qry,
Node parseTree 
)
static

Definition at line 259 of file analyze.c.

260{
261 ParseLoc stmt_len = 0;
262
263 /*
264 * If there is no information about the top RawStmt's length, leave it at
265 * 0 to use the whole string.
266 */
267 if (pstate->p_stmt_len == 0)
268 return;
269
270 switch (nodeTag(parseTree))
271 {
272 case T_InsertStmt:
273 qry->stmt_location = ((InsertStmt *) parseTree)->stmt_location;
274 stmt_len = ((InsertStmt *) parseTree)->stmt_len;
275 break;
276
277 case T_DeleteStmt:
278 qry->stmt_location = ((DeleteStmt *) parseTree)->stmt_location;
279 stmt_len = ((DeleteStmt *) parseTree)->stmt_len;
280 break;
281
282 case T_UpdateStmt:
283 qry->stmt_location = ((UpdateStmt *) parseTree)->stmt_location;
284 stmt_len = ((UpdateStmt *) parseTree)->stmt_len;
285 break;
286
287 case T_MergeStmt:
288 qry->stmt_location = ((MergeStmt *) parseTree)->stmt_location;
289 stmt_len = ((MergeStmt *) parseTree)->stmt_len;
290 break;
291
292 case T_SelectStmt:
293 qry->stmt_location = ((SelectStmt *) parseTree)->stmt_location;
294 stmt_len = ((SelectStmt *) parseTree)->stmt_len;
295 break;
296
297 case T_PLAssignStmt:
298 qry->stmt_location = ((PLAssignStmt *) parseTree)->location;
299 break;
300
301 default:
302 qry->stmt_location = pstate->p_stmt_location;
303 break;
304 }
305
306 if (stmt_len > 0)
307 {
308 /* Statement's length is known, use it */
309 qry->stmt_len = stmt_len;
310 }
311 else
312 {
313 /*
314 * Compute the statement's length from the statement's location and
315 * the RawStmt's length and location.
316 */
317 qry->stmt_len = pstate->p_stmt_len - (qry->stmt_location - pstate->p_stmt_location);
318 }
319
320 /* The calculated statement length should be calculated as positive. */
321 Assert(qry->stmt_len >= 0);
322}
#define nodeTag(nodeptr)
Definition: nodes.h:133
int ParseLoc
Definition: nodes.h:240
ParseLoc p_stmt_location
Definition: parse_node.h:210
ParseLoc p_stmt_len
Definition: parse_node.h:211
ParseLoc stmt_location
Definition: parsenodes.h:249

References Assert, nodeTag, ParseState::p_stmt_len, ParseState::p_stmt_location, and Query::stmt_location.

Referenced by transformStmt().

◆ stmt_requires_parse_analysis()

bool stmt_requires_parse_analysis ( RawStmt parseTree)

Definition at line 532 of file analyze.c.

533{
534 bool result;
535
536 switch (nodeTag(parseTree->stmt))
537 {
538 /*
539 * Optimizable statements
540 */
541 case T_InsertStmt:
542 case T_DeleteStmt:
543 case T_UpdateStmt:
544 case T_MergeStmt:
545 case T_SelectStmt:
546 case T_ReturnStmt:
547 case T_PLAssignStmt:
548 result = true;
549 break;
550
551 /*
552 * Special cases
553 */
554 case T_DeclareCursorStmt:
555 case T_ExplainStmt:
556 case T_CreateTableAsStmt:
557 case T_CallStmt:
558 result = true;
559 break;
560
561 default:
562 /* all other statements just get wrapped in a CMD_UTILITY Query */
563 result = false;
564 break;
565 }
566
567 return result;
568}
Node * stmt
Definition: parsenodes.h:2066

References nodeTag, and RawStmt::stmt.

Referenced by analyze_requires_snapshot().

◆ transformCallStmt()

static Query * transformCallStmt ( ParseState pstate,
CallStmt stmt 
)
static

Definition at line 3282 of file analyze.c.

3283{
3284 List *targs;
3285 ListCell *lc;
3286 Node *node;
3287 FuncExpr *fexpr;
3288 HeapTuple proctup;
3289 Datum proargmodes;
3290 bool isNull;
3291 List *outargs = NIL;
3292 Query *result;
3293
3294 /*
3295 * First, do standard parse analysis on the procedure call and its
3296 * arguments, allowing us to identify the called procedure.
3297 */
3298 targs = NIL;
3299 foreach(lc, stmt->funccall->args)
3300 {
3301 targs = lappend(targs, transformExpr(pstate,
3302 (Node *) lfirst(lc),
3304 }
3305
3306 node = ParseFuncOrColumn(pstate,
3307 stmt->funccall->funcname,
3308 targs,
3309 pstate->p_last_srf,
3310 stmt->funccall,
3311 true,
3312 stmt->funccall->location);
3313
3314 assign_expr_collations(pstate, node);
3315
3316 fexpr = castNode(FuncExpr, node);
3317
3318 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fexpr->funcid));
3319 if (!HeapTupleIsValid(proctup))
3320 elog(ERROR, "cache lookup failed for function %u", fexpr->funcid);
3321
3322 /*
3323 * Expand the argument list to deal with named-argument notation and
3324 * default arguments. For ordinary FuncExprs this'd be done during
3325 * planning, but a CallStmt doesn't go through planning, and there seems
3326 * no good reason not to do it here.
3327 */
3328 fexpr->args = expand_function_arguments(fexpr->args,
3329 true,
3330 fexpr->funcresulttype,
3331 proctup);
3332
3333 /* Fetch proargmodes; if it's null, there are no output args */
3334 proargmodes = SysCacheGetAttr(PROCOID, proctup,
3335 Anum_pg_proc_proargmodes,
3336 &isNull);
3337 if (!isNull)
3338 {
3339 /*
3340 * Split the list into input arguments in fexpr->args and output
3341 * arguments in stmt->outargs. INOUT arguments appear in both lists.
3342 */
3343 ArrayType *arr;
3344 int numargs;
3345 char *argmodes;
3346 List *inargs;
3347 int i;
3348
3349 arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
3350 numargs = list_length(fexpr->args);
3351 if (ARR_NDIM(arr) != 1 ||
3352 ARR_DIMS(arr)[0] != numargs ||
3353 ARR_HASNULL(arr) ||
3354 ARR_ELEMTYPE(arr) != CHAROID)
3355 elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
3356 numargs);
3357 argmodes = (char *) ARR_DATA_PTR(arr);
3358
3359 inargs = NIL;
3360 i = 0;
3361 foreach(lc, fexpr->args)
3362 {
3363 Node *n = lfirst(lc);
3364
3365 switch (argmodes[i])
3366 {
3367 case PROARGMODE_IN:
3368 case PROARGMODE_VARIADIC:
3369 inargs = lappend(inargs, n);
3370 break;
3371 case PROARGMODE_OUT:
3372 outargs = lappend(outargs, n);
3373 break;
3374 case PROARGMODE_INOUT:
3375 inargs = lappend(inargs, n);
3376 outargs = lappend(outargs, copyObject(n));
3377 break;
3378 default:
3379 /* note we don't support PROARGMODE_TABLE */
3380 elog(ERROR, "invalid argmode %c for procedure",
3381 argmodes[i]);
3382 break;
3383 }
3384 i++;
3385 }
3386 fexpr->args = inargs;
3387 }
3388
3389 stmt->funcexpr = fexpr;
3390 stmt->outargs = outargs;
3391
3392 ReleaseSysCache(proctup);
3393
3394 /* represent the command as a utility Query */
3395 result = makeNode(Query);
3396 result->commandType = CMD_UTILITY;
3397 result->utilityStmt = (Node *) stmt;
3398
3399 return result;
3400}
#define ARR_NDIM(a)
Definition: array.h:290
#define ARR_DATA_PTR(a)
Definition: array.h:322
#define DatumGetArrayTypeP(X)
Definition: array.h:261
#define ARR_ELEMTYPE(a)
Definition: array.h:292
#define ARR_DIMS(a)
Definition: array.h:294
#define ARR_HASNULL(a)
Definition: array.h:291
List * expand_function_arguments(List *args, bool include_out_arguments, Oid result_type, HeapTuple func_tuple)
Definition: clauses.c:4177
#define elog(elevel,...)
Definition: elog.h:225
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define stmt
Definition: indent_codes.h:59
#define copyObject(obj)
Definition: nodes.h:224
@ CMD_UTILITY
Definition: nodes.h:270
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
void assign_expr_collations(ParseState *pstate, Node *expr)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:118
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:90
@ EXPR_KIND_CALL_ARGUMENT
Definition: parse_node.h:81
uintptr_t Datum
Definition: postgres.h:69
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
Oid funcid
Definition: primnodes.h:766
List * args
Definition: primnodes.h:784
Node * p_last_srf
Definition: parse_node.h:248
CmdType commandType
Definition: parsenodes.h:121
Node * utilityStmt
Definition: parsenodes.h:136
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:600

References FuncExpr::args, ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, assign_expr_collations(), castNode, CMD_UTILITY, Query::commandType, copyObject, DatumGetArrayTypeP, elog, ERROR, expand_function_arguments(), EXPR_KIND_CALL_ARGUMENT, FuncExpr::funcid, HeapTupleIsValid, i, lappend(), lfirst, list_length(), makeNode, NIL, ObjectIdGetDatum(), ParseState::p_last_srf, ParseFuncOrColumn(), ReleaseSysCache(), SearchSysCache1(), stmt, SysCacheGetAttr(), transformExpr(), and Query::utilityStmt.

Referenced by transformStmt().

◆ transformCreateTableAsStmt()

static Query * transformCreateTableAsStmt ( ParseState pstate,
CreateTableAsStmt stmt 
)
static

Definition at line 3207 of file analyze.c.

3208{
3209 Query *result;
3210 Query *query;
3211
3212 /* transform contained query, not allowing SELECT INTO */
3213 query = transformStmt(pstate, stmt->query);
3214 stmt->query = (Node *) query;
3215
3216 /* additional work needed for CREATE MATERIALIZED VIEW */
3217 if (stmt->objtype == OBJECT_MATVIEW)
3218 {
3219 /*
3220 * Prohibit a data-modifying CTE in the query used to create a
3221 * materialized view. It's not sufficiently clear what the user would
3222 * want to happen if the MV is refreshed or incrementally maintained.
3223 */
3224 if (query->hasModifyingCTE)
3225 ereport(ERROR,
3226 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3227 errmsg("materialized views must not use data-modifying statements in WITH")));
3228
3229 /*
3230 * Check whether any temporary database objects are used in the
3231 * creation query. It would be hard to refresh data or incrementally
3232 * maintain it if a source disappeared.
3233 */
3234 if (isQueryUsingTempRelation(query))
3235 ereport(ERROR,
3236 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3237 errmsg("materialized views must not use temporary tables or views")));
3238
3239 /*
3240 * A materialized view would either need to save parameters for use in
3241 * maintaining/loading the data or prohibit them entirely. The latter
3242 * seems safer and more sane.
3243 */
3245 ereport(ERROR,
3246 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3247 errmsg("materialized views may not be defined using bound parameters")));
3248
3249 /*
3250 * For now, we disallow unlogged materialized views, because it seems
3251 * like a bad idea for them to just go to empty after a crash. (If we
3252 * could mark them as unpopulated, that would be better, but that
3253 * requires catalog changes which crash recovery can't presently
3254 * handle.)
3255 */
3256 if (stmt->into->rel->relpersistence == RELPERSISTENCE_UNLOGGED)
3257 ereport(ERROR,
3258 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3259 errmsg("materialized views cannot be unlogged")));
3260
3261 /*
3262 * At runtime, we'll need a copy of the parsed-but-not-rewritten Query
3263 * for purposes of creating the view's ON SELECT rule. We stash that
3264 * in the IntoClause because that's where intorel_startup() can
3265 * conveniently get it from.
3266 */
3267 stmt->into->viewQuery = copyObject(query);
3268 }
3269
3270 /* represent the command as a utility Query */
3271 result = makeNode(Query);
3272 result->commandType = CMD_UTILITY;
3273 result->utilityStmt = (Node *) stmt;
3274
3275 return result;
3276}
bool query_contains_extern_params(Query *query)
Definition: parse_param.c:331
bool isQueryUsingTempRelation(Query *query)
@ OBJECT_MATVIEW
Definition: parsenodes.h:2335

References CMD_UTILITY, Query::commandType, copyObject, ereport, errcode(), errmsg(), ERROR, isQueryUsingTempRelation(), makeNode, OBJECT_MATVIEW, query_contains_extern_params(), stmt, transformStmt(), and Query::utilityStmt.

Referenced by transformStmt().

◆ transformDeclareCursorStmt()

static Query * transformDeclareCursorStmt ( ParseState pstate,
DeclareCursorStmt stmt 
)
static

Definition at line 3062 of file analyze.c.

3063{
3064 Query *result;
3065 Query *query;
3066
3067 if ((stmt->options & CURSOR_OPT_SCROLL) &&
3068 (stmt->options & CURSOR_OPT_NO_SCROLL))
3069 ereport(ERROR,
3070 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
3071 /* translator: %s is a SQL keyword */
3072 errmsg("cannot specify both %s and %s",
3073 "SCROLL", "NO SCROLL")));
3074
3075 if ((stmt->options & CURSOR_OPT_ASENSITIVE) &&
3076 (stmt->options & CURSOR_OPT_INSENSITIVE))
3077 ereport(ERROR,
3078 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
3079 /* translator: %s is a SQL keyword */
3080 errmsg("cannot specify both %s and %s",
3081 "ASENSITIVE", "INSENSITIVE")));
3082
3083 /* Transform contained query, not allowing SELECT INTO */
3084 query = transformStmt(pstate, stmt->query);
3085 stmt->query = (Node *) query;
3086
3087 /* Grammar should not have allowed anything but SELECT */
3088 if (!IsA(query, Query) ||
3089 query->commandType != CMD_SELECT)
3090 elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
3091
3092 /*
3093 * We also disallow data-modifying WITH in a cursor. (This could be
3094 * allowed, but the semantics of when the updates occur might be
3095 * surprising.)
3096 */
3097 if (query->hasModifyingCTE)
3098 ereport(ERROR,
3099 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3100 errmsg("DECLARE CURSOR must not contain data-modifying statements in WITH")));
3101
3102 /* FOR UPDATE and WITH HOLD are not compatible */
3103 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
3104 ereport(ERROR,
3105 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3106 /*------
3107 translator: %s is a SQL row locking clause such as FOR UPDATE */
3108 errmsg("DECLARE CURSOR WITH HOLD ... %s is not supported",
3110 linitial(query->rowMarks))->strength)),
3111 errdetail("Holdable cursors must be READ ONLY.")));
3112
3113 /* FOR UPDATE and SCROLL are not compatible */
3114 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
3115 ereport(ERROR,
3116 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3117 /*------
3118 translator: %s is a SQL row locking clause such as FOR UPDATE */
3119 errmsg("DECLARE SCROLL CURSOR ... %s is not supported",
3121 linitial(query->rowMarks))->strength)),
3122 errdetail("Scrollable cursors must be READ ONLY.")));
3123
3124 /* FOR UPDATE and INSENSITIVE are not compatible */
3125 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
3126 ereport(ERROR,
3127 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
3128 /*------
3129 translator: %s is a SQL row locking clause such as FOR UPDATE */
3130 errmsg("DECLARE INSENSITIVE CURSOR ... %s is not valid",
3132 linitial(query->rowMarks))->strength)),
3133 errdetail("Insensitive cursors must be READ ONLY.")));
3134
3135 /* represent the command as a utility Query */
3136 result = makeNode(Query);
3137 result->commandType = CMD_UTILITY;
3138 result->utilityStmt = (Node *) stmt;
3139
3140 return result;
3141}
int errdetail(const char *fmt,...)
Definition: elog.c:1203
@ CMD_SELECT
Definition: nodes.h:265
#define CURSOR_OPT_INSENSITIVE
Definition: parsenodes.h:3357
#define CURSOR_OPT_SCROLL
Definition: parsenodes.h:3355
#define CURSOR_OPT_HOLD
Definition: parsenodes.h:3359
#define CURSOR_OPT_ASENSITIVE
Definition: parsenodes.h:3358
#define CURSOR_OPT_NO_SCROLL
Definition: parsenodes.h:3356
#define linitial(l)
Definition: pg_list.h:178

References CMD_SELECT, CMD_UTILITY, Query::commandType, CURSOR_OPT_ASENSITIVE, CURSOR_OPT_HOLD, CURSOR_OPT_INSENSITIVE, CURSOR_OPT_NO_SCROLL, CURSOR_OPT_SCROLL, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, IsA, LCS_asString(), linitial, makeNode, NIL, Query::rowMarks, stmt, transformStmt(), and Query::utilityStmt.

Referenced by transformStmt().

◆ transformDeleteStmt()

static Query * transformDeleteStmt ( ParseState pstate,
DeleteStmt stmt 
)
static

Definition at line 599 of file analyze.c.

600{
601 Query *qry = makeNode(Query);
602 ParseNamespaceItem *nsitem;
603 Node *qual;
604
605 qry->commandType = CMD_DELETE;
606
607 /* process the WITH clause independently of all else */
608 if (stmt->withClause)
609 {
610 qry->hasRecursive = stmt->withClause->recursive;
611 qry->cteList = transformWithClause(pstate, stmt->withClause);
612 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
613 }
614
615 /* set up range table with just the result rel */
616 qry->resultRelation = setTargetTable(pstate, stmt->relation,
617 stmt->relation->inh,
618 true,
619 ACL_DELETE);
620 nsitem = pstate->p_target_nsitem;
621
622 /* there's no DISTINCT in DELETE */
623 qry->distinctClause = NIL;
624
625 /* subqueries in USING cannot access the result relation */
626 nsitem->p_lateral_only = true;
627 nsitem->p_lateral_ok = false;
628
629 /*
630 * The USING clause is non-standard SQL syntax, and is equivalent in
631 * functionality to the FROM list that can be specified for UPDATE. The
632 * USING keyword is used rather than FROM because FROM is already a
633 * keyword in the DELETE syntax.
634 */
635 transformFromClause(pstate, stmt->usingClause);
636
637 /* remaining clauses can reference the result relation normally */
638 nsitem->p_lateral_only = false;
639 nsitem->p_lateral_ok = true;
640
641 qual = transformWhereClause(pstate, stmt->whereClause,
642 EXPR_KIND_WHERE, "WHERE");
643
644 transformReturningClause(pstate, qry, stmt->returningClause,
646
647 /* done building the range table and jointree */
648 qry->rtable = pstate->p_rtable;
649 qry->rteperminfos = pstate->p_rteperminfos;
650 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
651
652 qry->hasSubLinks = pstate->p_hasSubLinks;
653 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
654 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
655 qry->hasAggs = pstate->p_hasAggs;
656
657 assign_query_collations(pstate, qry);
658
659 /* this must be done after collations, for reliable comparison of exprs */
660 if (pstate->p_hasAggs)
661 parseCheckAggregates(pstate, qry);
662
663 return qry;
664}
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Definition: makefuncs.c:289
@ CMD_DELETE
Definition: nodes.h:268
void parseCheckAggregates(ParseState *pstate, Query *qry)
Definition: parse_agg.c:1085
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
void transformFromClause(ParseState *pstate, List *frmList)
Definition: parse_clause.c:112
int setTargetTable(ParseState *pstate, RangeVar *relation, bool inh, bool alsoSource, AclMode requiredPerms)
Definition: parse_clause.c:178
void assign_query_collations(ParseState *pstate, Query *query)
List * transformWithClause(ParseState *pstate, WithClause *withClause)
Definition: parse_cte.c:109
@ EXPR_KIND_WHERE
Definition: parse_node.h:46
@ EXPR_KIND_RETURNING
Definition: parse_node.h:64
#define ACL_DELETE
Definition: parsenodes.h:79
void transformReturningClause(ParseState *pstate, Query *qry, ReturningClause *returningClause, ParseExprKind exprKind)
Definition: analyze.c:2691
bool p_hasTargetSRFs
Definition: parse_node.h:244
bool p_hasWindowFuncs
Definition: parse_node.h:243
bool p_hasModifyingCTE
Definition: parse_node.h:246
List * p_rteperminfos
Definition: parse_node.h:213
bool p_hasSubLinks
Definition: parse_node.h:245
List * p_joinlist
Definition: parse_node.h:217
bool p_hasAggs
Definition: parse_node.h:242
FromExpr * jointree
Definition: parsenodes.h:177
List * cteList
Definition: parsenodes.h:168
List * rtable
Definition: parsenodes.h:170

References ACL_DELETE, assign_query_collations(), CMD_DELETE, Query::commandType, Query::cteList, Query::distinctClause, EXPR_KIND_RETURNING, EXPR_KIND_WHERE, Query::jointree, makeFromExpr(), makeNode, NIL, ParseState::p_hasAggs, ParseState::p_hasModifyingCTE, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_hasWindowFuncs, ParseState::p_joinlist, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseState::p_rtable, ParseState::p_rteperminfos, ParseState::p_target_nsitem, parseCheckAggregates(), Query::rtable, setTargetTable(), stmt, transformFromClause(), transformReturningClause(), transformWhereClause(), and transformWithClause().

Referenced by transformStmt().

◆ transformExplainStmt()

static Query * transformExplainStmt ( ParseState pstate,
ExplainStmt stmt 
)
static

Definition at line 3155 of file analyze.c.

3156{
3157 Query *result;
3158 bool generic_plan = false;
3159 Oid *paramTypes = NULL;
3160 int numParams = 0;
3161
3162 /*
3163 * If we have no external source of parameter definitions, and the
3164 * GENERIC_PLAN option is specified, then accept variable parameter
3165 * definitions (similarly to PREPARE, for example).
3166 */
3167 if (pstate->p_paramref_hook == NULL)
3168 {
3169 ListCell *lc;
3170
3171 foreach(lc, stmt->options)
3172 {
3173 DefElem *opt = (DefElem *) lfirst(lc);
3174
3175 if (strcmp(opt->defname, "generic_plan") == 0)
3176 generic_plan = defGetBoolean(opt);
3177 /* don't "break", as we want the last value */
3178 }
3179 if (generic_plan)
3180 setup_parse_variable_parameters(pstate, &paramTypes, &numParams);
3181 }
3182
3183 /* transform contained query, allowing SELECT INTO */
3184 stmt->query = (Node *) transformOptionalSelectInto(pstate, stmt->query);
3185
3186 /* make sure all is well with parameter types */
3187 if (generic_plan)
3188 check_variable_parameters(pstate, (Query *) stmt->query);
3189
3190 /* represent the command as a utility Query */
3191 result = makeNode(Query);
3192 result->commandType = CMD_UTILITY;
3193 result->utilityStmt = (Node *) stmt;
3194
3195 return result;
3196}
bool defGetBoolean(DefElem *def)
Definition: define.c:94
static Query * transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
Definition: analyze.c:357
char * defname
Definition: parsenodes.h:826
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:256

References check_variable_parameters(), CMD_UTILITY, Query::commandType, defGetBoolean(), DefElem::defname, lfirst, makeNode, ParseState::p_paramref_hook, setup_parse_variable_parameters(), stmt, transformOptionalSelectInto(), and Query::utilityStmt.

Referenced by transformStmt().

◆ transformInsertRow()

List * transformInsertRow ( ParseState pstate,
List exprlist,
List stmtcols,
List icolumns,
List attrnos,
bool  strip_indirection 
)

Definition at line 1098 of file analyze.c.

1101{
1102 List *result;
1103 ListCell *lc;
1104 ListCell *icols;
1105 ListCell *attnos;
1106
1107 /*
1108 * Check length of expr list. It must not have more expressions than
1109 * there are target columns. We allow fewer, but only if no explicit
1110 * columns list was given (the remaining columns are implicitly
1111 * defaulted). Note we must check this *after* transformation because
1112 * that could expand '*' into multiple items.
1113 */
1114 if (list_length(exprlist) > list_length(icolumns))
1115 ereport(ERROR,
1116 (errcode(ERRCODE_SYNTAX_ERROR),
1117 errmsg("INSERT has more expressions than target columns"),
1118 parser_errposition(pstate,
1119 exprLocation(list_nth(exprlist,
1120 list_length(icolumns))))));
1121 if (stmtcols != NIL &&
1122 list_length(exprlist) < list_length(icolumns))
1123 {
1124 /*
1125 * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
1126 * where the user accidentally created a RowExpr instead of separate
1127 * columns. Add a suitable hint if that seems to be the problem,
1128 * because the main error message is quite misleading for this case.
1129 * (If there's no stmtcols, you'll get something about data type
1130 * mismatch, which is less misleading so we don't worry about giving a
1131 * hint in that case.)
1132 */
1133 ereport(ERROR,
1134 (errcode(ERRCODE_SYNTAX_ERROR),
1135 errmsg("INSERT has more target columns than expressions"),
1136 ((list_length(exprlist) == 1 &&
1137 count_rowexpr_columns(pstate, linitial(exprlist)) ==
1138 list_length(icolumns)) ?
1139 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),
1140 parser_errposition(pstate,
1141 exprLocation(list_nth(icolumns,
1142 list_length(exprlist))))));
1143 }
1144
1145 /*
1146 * Prepare columns for assignment to target table.
1147 */
1148 result = NIL;
1149 forthree(lc, exprlist, icols, icolumns, attnos, attrnos)
1150 {
1151 Expr *expr = (Expr *) lfirst(lc);
1152 ResTarget *col = lfirst_node(ResTarget, icols);
1153 int attno = lfirst_int(attnos);
1154
1155 expr = transformAssignedExpr(pstate, expr,
1157 col->name,
1158 attno,
1159 col->indirection,
1160 col->location);
1161
1162 if (strip_indirection)
1163 {
1164 /*
1165 * We need to remove top-level FieldStores and SubscriptingRefs,
1166 * as well as any CoerceToDomain appearing above one of those ---
1167 * but not a CoerceToDomain that isn't above one of those.
1168 */
1169 while (expr)
1170 {
1171 Expr *subexpr = expr;
1172
1173 while (IsA(subexpr, CoerceToDomain))
1174 {
1175 subexpr = ((CoerceToDomain *) subexpr)->arg;
1176 }
1177 if (IsA(subexpr, FieldStore))
1178 {
1179 FieldStore *fstore = (FieldStore *) subexpr;
1180
1181 expr = (Expr *) linitial(fstore->newvals);
1182 }
1183 else if (IsA(subexpr, SubscriptingRef))
1184 {
1185 SubscriptingRef *sbsref = (SubscriptingRef *) subexpr;
1186
1187 if (sbsref->refassgnexpr == NULL)
1188 break;
1189
1190 expr = sbsref->refassgnexpr;
1191 }
1192 else
1193 break;
1194 }
1195 }
1196
1197 result = lappend(result, expr);
1198 }
1199
1200 return result;
1201}
int errhint(const char *fmt,...)
Definition: elog.c:1317
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1388
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
@ EXPR_KIND_INSERT_TARGET
Definition: parse_node.h:55
Expr * transformAssignedExpr(ParseState *pstate, Expr *expr, ParseExprKind exprKind, const char *colname, int attrno, List *indirection, int location)
Definition: parse_target.c:455
static int count_rowexpr_columns(ParseState *pstate, Node *expr)
Definition: analyze.c:1385
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define lfirst_int(lc)
Definition: pg_list.h:173
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:563
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
List * newvals
Definition: primnodes.h:1176
ParseLoc location
Definition: parsenodes.h:531
List * indirection
Definition: parsenodes.h:529
char * name
Definition: parsenodes.h:528
Expr * refassgnexpr
Definition: primnodes.h:719

References count_rowexpr_columns(), ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_INSERT_TARGET, exprLocation(), forthree, ResTarget::indirection, IsA, lappend(), lfirst, lfirst_int, lfirst_node, linitial, list_length(), list_nth(), ResTarget::location, ResTarget::name, FieldStore::newvals, NIL, parser_errposition(), SubscriptingRef::refassgnexpr, and transformAssignedExpr().

Referenced by transformInsertStmt(), and transformMergeStmt().

◆ transformInsertStmt()

static Query * transformInsertStmt ( ParseState pstate,
InsertStmt stmt 
)
static

Definition at line 671 of file analyze.c.

672{
673 Query *qry = makeNode(Query);
674 SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
675 List *exprList = NIL;
676 bool isGeneralSelect;
677 List *sub_rtable;
678 List *sub_rteperminfos;
679 List *sub_namespace;
680 List *icolumns;
681 List *attrnos;
682 ParseNamespaceItem *nsitem;
683 RTEPermissionInfo *perminfo;
684 ListCell *icols;
685 ListCell *attnos;
686 ListCell *lc;
687 bool isOnConflictUpdate;
688 AclMode targetPerms;
689
690 /* There can't be any outer WITH to worry about */
691 Assert(pstate->p_ctenamespace == NIL);
692
693 qry->commandType = CMD_INSERT;
694 pstate->p_is_insert = true;
695
696 /* process the WITH clause independently of all else */
697 if (stmt->withClause)
698 {
699 qry->hasRecursive = stmt->withClause->recursive;
700 qry->cteList = transformWithClause(pstate, stmt->withClause);
701 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
702 }
703
704 qry->override = stmt->override;
705
706 isOnConflictUpdate = (stmt->onConflictClause &&
707 stmt->onConflictClause->action == ONCONFLICT_UPDATE);
708
709 /*
710 * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
711 * VALUES list, or general SELECT input. We special-case VALUES, both for
712 * efficiency and so we can handle DEFAULT specifications.
713 *
714 * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
715 * VALUES clause. If we have any of those, treat it as a general SELECT;
716 * so it will work, but you can't use DEFAULT items together with those.
717 */
718 isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
719 selectStmt->sortClause != NIL ||
720 selectStmt->limitOffset != NULL ||
721 selectStmt->limitCount != NULL ||
722 selectStmt->lockingClause != NIL ||
723 selectStmt->withClause != NULL));
724
725 /*
726 * If a non-nil rangetable/namespace was passed in, and we are doing
727 * INSERT/SELECT, arrange to pass the rangetable/rteperminfos/namespace
728 * down to the SELECT. This can only happen if we are inside a CREATE
729 * RULE, and in that case we want the rule's OLD and NEW rtable entries to
730 * appear as part of the SELECT's rtable, not as outer references for it.
731 * (Kluge!) The SELECT's joinlist is not affected however. We must do
732 * this before adding the target table to the INSERT's rtable.
733 */
734 if (isGeneralSelect)
735 {
736 sub_rtable = pstate->p_rtable;
737 pstate->p_rtable = NIL;
738 sub_rteperminfos = pstate->p_rteperminfos;
739 pstate->p_rteperminfos = NIL;
740 sub_namespace = pstate->p_namespace;
741 pstate->p_namespace = NIL;
742 }
743 else
744 {
745 sub_rtable = NIL; /* not used, but keep compiler quiet */
746 sub_rteperminfos = NIL;
747 sub_namespace = NIL;
748 }
749
750 /*
751 * Must get write lock on INSERT target table before scanning SELECT, else
752 * we will grab the wrong kind of initial lock if the target table is also
753 * mentioned in the SELECT part. Note that the target table is not added
754 * to the joinlist or namespace.
755 */
756 targetPerms = ACL_INSERT;
757 if (isOnConflictUpdate)
758 targetPerms |= ACL_UPDATE;
759 qry->resultRelation = setTargetTable(pstate, stmt->relation,
760 false, false, targetPerms);
761
762 /* Validate stmt->cols list, or build default list if no list given */
763 icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
764 Assert(list_length(icolumns) == list_length(attrnos));
765
766 /*
767 * Determine which variant of INSERT we have.
768 */
769 if (selectStmt == NULL)
770 {
771 /*
772 * We have INSERT ... DEFAULT VALUES. We can handle this case by
773 * emitting an empty targetlist --- all columns will be defaulted when
774 * the planner expands the targetlist.
775 */
776 exprList = NIL;
777 }
778 else if (isGeneralSelect)
779 {
780 /*
781 * We make the sub-pstate a child of the outer pstate so that it can
782 * see any Param definitions supplied from above. Since the outer
783 * pstate's rtable and namespace are presently empty, there are no
784 * side-effects of exposing names the sub-SELECT shouldn't be able to
785 * see.
786 */
787 ParseState *sub_pstate = make_parsestate(pstate);
788 Query *selectQuery;
789
790 /*
791 * Process the source SELECT.
792 *
793 * It is important that this be handled just like a standalone SELECT;
794 * otherwise the behavior of SELECT within INSERT might be different
795 * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
796 * bugs of just that nature...)
797 *
798 * The sole exception is that we prevent resolving unknown-type
799 * outputs as TEXT. This does not change the semantics since if the
800 * column type matters semantically, it would have been resolved to
801 * something else anyway. Doing this lets us resolve such outputs as
802 * the target column's type, which we handle below.
803 */
804 sub_pstate->p_rtable = sub_rtable;
805 sub_pstate->p_rteperminfos = sub_rteperminfos;
806 sub_pstate->p_joinexprs = NIL; /* sub_rtable has no joins */
807 sub_pstate->p_nullingrels = NIL;
808 sub_pstate->p_namespace = sub_namespace;
809 sub_pstate->p_resolve_unknowns = false;
810
811 selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
812
813 free_parsestate(sub_pstate);
814
815 /* The grammar should have produced a SELECT */
816 if (!IsA(selectQuery, Query) ||
817 selectQuery->commandType != CMD_SELECT)
818 elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
819
820 /*
821 * Make the source be a subquery in the INSERT's rangetable, and add
822 * it to the INSERT's joinlist (but not the namespace).
823 */
824 nsitem = addRangeTableEntryForSubquery(pstate,
825 selectQuery,
826 makeAlias("*SELECT*", NIL),
827 false,
828 false);
829 addNSItemToQuery(pstate, nsitem, true, false, false);
830
831 /*----------
832 * Generate an expression list for the INSERT that selects all the
833 * non-resjunk columns from the subquery. (INSERT's tlist must be
834 * separate from the subquery's tlist because we may add columns,
835 * insert datatype coercions, etc.)
836 *
837 * HACK: unknown-type constants and params in the SELECT's targetlist
838 * are copied up as-is rather than being referenced as subquery
839 * outputs. This is to ensure that when we try to coerce them to
840 * the target column's datatype, the right things happen (see
841 * special cases in coerce_type). Otherwise, this fails:
842 * INSERT INTO foo SELECT 'bar', ... FROM baz
843 *----------
844 */
845 exprList = NIL;
846 foreach(lc, selectQuery->targetList)
847 {
848 TargetEntry *tle = (TargetEntry *) lfirst(lc);
849 Expr *expr;
850
851 if (tle->resjunk)
852 continue;
853 if (tle->expr &&
854 (IsA(tle->expr, Const) || IsA(tle->expr, Param)) &&
855 exprType((Node *) tle->expr) == UNKNOWNOID)
856 expr = tle->expr;
857 else
858 {
859 Var *var = makeVarFromTargetEntry(nsitem->p_rtindex, tle);
860
861 var->location = exprLocation((Node *) tle->expr);
862 expr = (Expr *) var;
863 }
864 exprList = lappend(exprList, expr);
865 }
866
867 /* Prepare row for assignment to target table */
868 exprList = transformInsertRow(pstate, exprList,
869 stmt->cols,
870 icolumns, attrnos,
871 false);
872 }
873 else if (list_length(selectStmt->valuesLists) > 1)
874 {
875 /*
876 * Process INSERT ... VALUES with multiple VALUES sublists. We
877 * generate a VALUES RTE holding the transformed expression lists, and
878 * build up a targetlist containing Vars that reference the VALUES
879 * RTE.
880 */
881 List *exprsLists = NIL;
882 List *coltypes = NIL;
883 List *coltypmods = NIL;
884 List *colcollations = NIL;
885 int sublist_length = -1;
886 bool lateral = false;
887
888 Assert(selectStmt->intoClause == NULL);
889
890 foreach(lc, selectStmt->valuesLists)
891 {
892 List *sublist = (List *) lfirst(lc);
893
894 /*
895 * Do basic expression transformation (same as a ROW() expr, but
896 * allow SetToDefault at top level)
897 */
898 sublist = transformExpressionList(pstate, sublist,
899 EXPR_KIND_VALUES, true);
900
901 /*
902 * All the sublists must be the same length, *after*
903 * transformation (which might expand '*' into multiple items).
904 * The VALUES RTE can't handle anything different.
905 */
906 if (sublist_length < 0)
907 {
908 /* Remember post-transformation length of first sublist */
909 sublist_length = list_length(sublist);
910 }
911 else if (sublist_length != list_length(sublist))
912 {
914 (errcode(ERRCODE_SYNTAX_ERROR),
915 errmsg("VALUES lists must all be the same length"),
916 parser_errposition(pstate,
917 exprLocation((Node *) sublist))));
918 }
919
920 /*
921 * Prepare row for assignment to target table. We process any
922 * indirection on the target column specs normally but then strip
923 * off the resulting field/array assignment nodes, since we don't
924 * want the parsed statement to contain copies of those in each
925 * VALUES row. (It's annoying to have to transform the
926 * indirection specs over and over like this, but avoiding it
927 * would take some really messy refactoring of
928 * transformAssignmentIndirection.)
929 */
930 sublist = transformInsertRow(pstate, sublist,
931 stmt->cols,
932 icolumns, attrnos,
933 true);
934
935 /*
936 * We must assign collations now because assign_query_collations
937 * doesn't process rangetable entries. We just assign all the
938 * collations independently in each row, and don't worry about
939 * whether they are consistent vertically. The outer INSERT query
940 * isn't going to care about the collations of the VALUES columns,
941 * so it's not worth the effort to identify a common collation for
942 * each one here. (But note this does have one user-visible
943 * consequence: INSERT ... VALUES won't complain about conflicting
944 * explicit COLLATEs in a column, whereas the same VALUES
945 * construct in another context would complain.)
946 */
947 assign_list_collations(pstate, sublist);
948
949 exprsLists = lappend(exprsLists, sublist);
950 }
951
952 /*
953 * Construct column type/typmod/collation lists for the VALUES RTE.
954 * Every expression in each column has been coerced to the type/typmod
955 * of the corresponding target column or subfield, so it's sufficient
956 * to look at the exprType/exprTypmod of the first row. We don't care
957 * about the collation labeling, so just fill in InvalidOid for that.
958 */
959 foreach(lc, (List *) linitial(exprsLists))
960 {
961 Node *val = (Node *) lfirst(lc);
962
963 coltypes = lappend_oid(coltypes, exprType(val));
964 coltypmods = lappend_int(coltypmods, exprTypmod(val));
965 colcollations = lappend_oid(colcollations, InvalidOid);
966 }
967
968 /*
969 * Ordinarily there can't be any current-level Vars in the expression
970 * lists, because the namespace was empty ... but if we're inside
971 * CREATE RULE, then NEW/OLD references might appear. In that case we
972 * have to mark the VALUES RTE as LATERAL.
973 */
974 if (list_length(pstate->p_rtable) != 1 &&
975 contain_vars_of_level((Node *) exprsLists, 0))
976 lateral = true;
977
978 /*
979 * Generate the VALUES RTE
980 */
981 nsitem = addRangeTableEntryForValues(pstate, exprsLists,
982 coltypes, coltypmods, colcollations,
983 NULL, lateral, true);
984 addNSItemToQuery(pstate, nsitem, true, false, false);
985
986 /*
987 * Generate list of Vars referencing the RTE
988 */
989 exprList = expandNSItemVars(pstate, nsitem, 0, -1, NULL);
990
991 /*
992 * Re-apply any indirection on the target column specs to the Vars
993 */
994 exprList = transformInsertRow(pstate, exprList,
995 stmt->cols,
996 icolumns, attrnos,
997 false);
998 }
999 else
1000 {
1001 /*
1002 * Process INSERT ... VALUES with a single VALUES sublist. We treat
1003 * this case separately for efficiency. The sublist is just computed
1004 * directly as the Query's targetlist, with no VALUES RTE. So it
1005 * works just like a SELECT without any FROM.
1006 */
1007 List *valuesLists = selectStmt->valuesLists;
1008
1009 Assert(list_length(valuesLists) == 1);
1010 Assert(selectStmt->intoClause == NULL);
1011
1012 /*
1013 * Do basic expression transformation (same as a ROW() expr, but allow
1014 * SetToDefault at top level)
1015 */
1016 exprList = transformExpressionList(pstate,
1017 (List *) linitial(valuesLists),
1019 true);
1020
1021 /* Prepare row for assignment to target table */
1022 exprList = transformInsertRow(pstate, exprList,
1023 stmt->cols,
1024 icolumns, attrnos,
1025 false);
1026 }
1027
1028 /*
1029 * Generate query's target list using the computed list of expressions.
1030 * Also, mark all the target columns as needing insert permissions.
1031 */
1032 perminfo = pstate->p_target_nsitem->p_perminfo;
1033 qry->targetList = NIL;
1034 Assert(list_length(exprList) <= list_length(icolumns));
1035 forthree(lc, exprList, icols, icolumns, attnos, attrnos)
1036 {
1037 Expr *expr = (Expr *) lfirst(lc);
1038 ResTarget *col = lfirst_node(ResTarget, icols);
1039 AttrNumber attr_num = (AttrNumber) lfirst_int(attnos);
1040 TargetEntry *tle;
1041
1042 tle = makeTargetEntry(expr,
1043 attr_num,
1044 col->name,
1045 false);
1046 qry->targetList = lappend(qry->targetList, tle);
1047
1048 perminfo->insertedCols = bms_add_member(perminfo->insertedCols,
1050 }
1051
1052 /*
1053 * If we have any clauses yet to process, set the query namespace to
1054 * contain only the target relation, removing any entries added in a
1055 * sub-SELECT or VALUES list.
1056 */
1057 if (stmt->onConflictClause || stmt->returningClause)
1058 {
1059 pstate->p_namespace = NIL;
1060 addNSItemToQuery(pstate, pstate->p_target_nsitem,
1061 false, true, true);
1062 }
1063
1064 /* Process ON CONFLICT, if any. */
1065 if (stmt->onConflictClause)
1067 stmt->onConflictClause);
1068
1069 /* Process RETURNING, if any. */
1070 if (stmt->returningClause)
1071 transformReturningClause(pstate, qry, stmt->returningClause,
1073
1074 /* done building the range table and jointree */
1075 qry->rtable = pstate->p_rtable;
1076 qry->rteperminfos = pstate->p_rteperminfos;
1077 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1078
1079 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1080 qry->hasSubLinks = pstate->p_hasSubLinks;
1081
1082 assign_query_collations(pstate, qry);
1083
1084 return qry;
1085}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
long val
Definition: informix.c:689
List * lappend_int(List *list, int datum)
Definition: list.c:357
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
Var * makeVarFromTargetEntry(int varno, TargetEntry *tle)
Definition: makefuncs.c:107
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:301
@ ONCONFLICT_UPDATE
Definition: nodes.h:420
@ CMD_INSERT
Definition: nodes.h:267
void assign_list_collations(ParseState *pstate, List *exprs)
@ EXPR_KIND_VALUES
Definition: parse_node.h:66
@ EXPR_KIND_VALUES_SINGLE
Definition: parse_node.h:67
List * expandNSItemVars(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
ParseNamespaceItem * addRangeTableEntryForSubquery(ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
ParseNamespaceItem * addRangeTableEntryForValues(ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
Definition: parse_target.c:220
List * checkInsertTargets(ParseState *pstate, List *cols, List **attrnos)
uint64 AclMode
Definition: parsenodes.h:74
#define ACL_INSERT
Definition: parsenodes.h:76
#define ACL_UPDATE
Definition: parsenodes.h:78
static OnConflictExpr * transformOnConflictClause(ParseState *pstate, OnConflictClause *onConflictClause)
Definition: analyze.c:1208
List * transformInsertRow(ParseState *pstate, List *exprlist, List *stmtcols, List *icolumns, List *attrnos, bool strip_indirection)
Definition: analyze.c:1098
List * p_ctenamespace
Definition: parse_node.h:222
List * p_nullingrels
Definition: parse_node.h:216
List * p_namespace
Definition: parse_node.h:219
bool p_is_insert
Definition: parse_node.h:228
List * p_joinexprs
Definition: parse_node.h:215
OnConflictExpr * onConflict
Definition: parsenodes.h:198
List * sortClause
Definition: parsenodes.h:2200
IntoClause * intoClause
Definition: parsenodes.h:2177
Node * limitOffset
Definition: parsenodes.h:2201
List * lockingClause
Definition: parsenodes.h:2204
Node * limitCount
Definition: parsenodes.h:2202
List * valuesLists
Definition: parsenodes.h:2194
WithClause * withClause
Definition: parsenodes.h:2205
ParseLoc location
Definition: primnodes.h:309
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:444

References ACL_INSERT, ACL_UPDATE, addNSItemToQuery(), addRangeTableEntryForSubquery(), Assert, checkInsertTargets(), CMD_INSERT, CMD_SELECT, Query::commandType, Query::cteList, elog, ERROR, TargetEntry::expr, exprLocation(), exprType(), free_parsestate(), IsA, lappend(), lfirst, SelectStmt::limitCount, SelectStmt::limitOffset, list_length(), Var::location, SelectStmt::lockingClause, make_parsestate(), makeAlias(), makeNode, makeVarFromTargetEntry(), NIL, ONCONFLICT_UPDATE, ParseState::p_ctenamespace, ParseState::p_hasModifyingCTE, ParseState::p_is_insert, ParseState::p_joinexprs, ParseState::p_namespace, ParseState::p_nullingrels, ParseState::p_resolve_unknowns, ParseState::p_rtable, ParseState::p_rteperminfos, setTargetTable(), SelectStmt::sortClause, stmt, Query::targetList, transformInsertRow(), transformStmt(), transformWithClause(), SelectStmt::valuesLists, and SelectStmt::withClause.

Referenced by transformStmt().

◆ transformLockingClause()

static void transformLockingClause ( ParseState pstate,
Query qry,
LockingClause lc,
bool  pushedDown 
)
static

Definition at line 3496 of file analyze.c.

3498{
3499 List *lockedRels = lc->lockedRels;
3500 ListCell *l;
3501 ListCell *rt;
3502 Index i;
3503 LockingClause *allrels;
3504
3505 CheckSelectLocking(qry, lc->strength);
3506
3507 /* make a clause we can pass down to subqueries to select all rels */
3508 allrels = makeNode(LockingClause);
3509 allrels->lockedRels = NIL; /* indicates all rels */
3510 allrels->strength = lc->strength;
3511 allrels->waitPolicy = lc->waitPolicy;
3512
3513 if (lockedRels == NIL)
3514 {
3515 /*
3516 * Lock all regular tables used in query and its subqueries. We
3517 * examine inFromCl to exclude auto-added RTEs, particularly NEW/OLD
3518 * in rules. This is a bit of an abuse of a mostly-obsolete flag, but
3519 * it's convenient. We can't rely on the namespace mechanism that has
3520 * largely replaced inFromCl, since for example we need to lock
3521 * base-relation RTEs even if they are masked by upper joins.
3522 */
3523 i = 0;
3524 foreach(rt, qry->rtable)
3525 {
3526 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3527
3528 ++i;
3529 if (!rte->inFromCl)
3530 continue;
3531 switch (rte->rtekind)
3532 {
3533 case RTE_RELATION:
3534 {
3535 RTEPermissionInfo *perminfo;
3536
3537 applyLockingClause(qry, i,
3538 lc->strength,
3539 lc->waitPolicy,
3540 pushedDown);
3541 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3543 }
3544 break;
3545 case RTE_SUBQUERY:
3546 applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
3547 pushedDown);
3548
3549 /*
3550 * FOR UPDATE/SHARE of subquery is propagated to all of
3551 * subquery's rels, too. We could do this later (based on
3552 * the marking of the subquery RTE) but it is convenient
3553 * to have local knowledge in each query level about which
3554 * rels need to be opened with RowShareLock.
3555 */
3556 transformLockingClause(pstate, rte->subquery,
3557 allrels, true);
3558 break;
3559 default:
3560 /* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
3561 break;
3562 }
3563 }
3564 }
3565 else
3566 {
3567 /*
3568 * Lock just the named tables. As above, we allow locking any base
3569 * relation regardless of alias-visibility rules, so we need to
3570 * examine inFromCl to exclude OLD/NEW.
3571 */
3572 foreach(l, lockedRels)
3573 {
3574 RangeVar *thisrel = (RangeVar *) lfirst(l);
3575
3576 /* For simplicity we insist on unqualified alias names here */
3577 if (thisrel->catalogname || thisrel->schemaname)
3578 ereport(ERROR,
3579 (errcode(ERRCODE_SYNTAX_ERROR),
3580 /*------
3581 translator: %s is a SQL row locking clause such as FOR UPDATE */
3582 errmsg("%s must specify unqualified relation names",
3583 LCS_asString(lc->strength)),
3584 parser_errposition(pstate, thisrel->location)));
3585
3586 i = 0;
3587 foreach(rt, qry->rtable)
3588 {
3589 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3590 char *rtename = rte->eref->aliasname;
3591
3592 ++i;
3593 if (!rte->inFromCl)
3594 continue;
3595
3596 /*
3597 * A join RTE without an alias is not visible as a relation
3598 * name and needs to be skipped (otherwise it might hide a
3599 * base relation with the same name), except if it has a USING
3600 * alias, which *is* visible.
3601 *
3602 * Subquery and values RTEs without aliases are never visible
3603 * as relation names and must always be skipped.
3604 */
3605 if (rte->alias == NULL)
3606 {
3607 if (rte->rtekind == RTE_JOIN)
3608 {
3609 if (rte->join_using_alias == NULL)
3610 continue;
3611 rtename = rte->join_using_alias->aliasname;
3612 }
3613 else if (rte->rtekind == RTE_SUBQUERY ||
3614 rte->rtekind == RTE_VALUES)
3615 continue;
3616 }
3617
3618 if (strcmp(rtename, thisrel->relname) == 0)
3619 {
3620 switch (rte->rtekind)
3621 {
3622 case RTE_RELATION:
3623 {
3624 RTEPermissionInfo *perminfo;
3625
3626 applyLockingClause(qry, i,
3627 lc->strength,
3628 lc->waitPolicy,
3629 pushedDown);
3630 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3632 }
3633 break;
3634 case RTE_SUBQUERY:
3635 applyLockingClause(qry, i, lc->strength,
3636 lc->waitPolicy, pushedDown);
3637 /* see comment above */
3638 transformLockingClause(pstate, rte->subquery,
3639 allrels, true);
3640 break;
3641 case RTE_JOIN:
3642 ereport(ERROR,
3643 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3644 /*------
3645 translator: %s is a SQL row locking clause such as FOR UPDATE */
3646 errmsg("%s cannot be applied to a join",
3647 LCS_asString(lc->strength)),
3648 parser_errposition(pstate, thisrel->location)));
3649 break;
3650 case RTE_FUNCTION:
3651 ereport(ERROR,
3652 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3653 /*------
3654 translator: %s is a SQL row locking clause such as FOR UPDATE */
3655 errmsg("%s cannot be applied to a function",
3656 LCS_asString(lc->strength)),
3657 parser_errposition(pstate, thisrel->location)));
3658 break;
3659 case RTE_TABLEFUNC:
3660 ereport(ERROR,
3661 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3662 /*------
3663 translator: %s is a SQL row locking clause such as FOR UPDATE */
3664 errmsg("%s cannot be applied to a table function",
3665 LCS_asString(lc->strength)),
3666 parser_errposition(pstate, thisrel->location)));
3667 break;
3668 case RTE_VALUES:
3669 ereport(ERROR,
3670 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3671 /*------
3672 translator: %s is a SQL row locking clause such as FOR UPDATE */
3673 errmsg("%s cannot be applied to VALUES",
3674 LCS_asString(lc->strength)),
3675 parser_errposition(pstate, thisrel->location)));
3676 break;
3677 case RTE_CTE:
3678 ereport(ERROR,
3679 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3680 /*------
3681 translator: %s is a SQL row locking clause such as FOR UPDATE */
3682 errmsg("%s cannot be applied to a WITH query",
3683 LCS_asString(lc->strength)),
3684 parser_errposition(pstate, thisrel->location)));
3685 break;
3687 ereport(ERROR,
3688 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3689 /*------
3690 translator: %s is a SQL row locking clause such as FOR UPDATE */
3691 errmsg("%s cannot be applied to a named tuplestore",
3692 LCS_asString(lc->strength)),
3693 parser_errposition(pstate, thisrel->location)));
3694 break;
3695
3696 /* Shouldn't be possible to see RTE_RESULT here */
3697
3698 default:
3699 elog(ERROR, "unrecognized RTE type: %d",
3700 (int) rte->rtekind);
3701 break;
3702 }
3703 break; /* out of foreach loop */
3704 }
3705 }
3706 if (rt == NULL)
3707 ereport(ERROR,
3709 /*------
3710 translator: %s is a SQL row locking clause such as FOR UPDATE */
3711 errmsg("relation \"%s\" in %s clause not found in FROM clause",
3712 thisrel->relname,
3713 LCS_asString(lc->strength)),
3714 parser_errposition(pstate, thisrel->location)));
3715 }
3716 }
3717}
unsigned int Index
Definition: c.h:571
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
@ RTE_JOIN
Definition: parsenodes.h:1028
@ RTE_CTE
Definition: parsenodes.h:1032
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1033
@ RTE_VALUES
Definition: parsenodes.h:1031
@ RTE_FUNCTION
Definition: parsenodes.h:1029
@ RTE_TABLEFUNC
Definition: parsenodes.h:1030
@ RTE_RELATION
Definition: parsenodes.h:1026
#define ACL_SELECT_FOR_UPDATE
Definition: parsenodes.h:94
static void transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc, bool pushedDown)
Definition: analyze.c:3496
void CheckSelectLocking(Query *qry, LockClauseStrength strength)
Definition: analyze.c:3432
void applyLockingClause(Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
Definition: analyze.c:3723
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:78
List * lockedRels
Definition: parsenodes.h:845
LockClauseStrength strength
Definition: parsenodes.h:846
LockWaitPolicy waitPolicy
Definition: parsenodes.h:847
AclMode requiredPerms
Definition: parsenodes.h:1300
char * relname
Definition: primnodes.h:82
char * catalogname
Definition: primnodes.h:76
ParseLoc location
Definition: primnodes.h:94
char * schemaname
Definition: primnodes.h:79

References ACL_SELECT_FOR_UPDATE, applyLockingClause(), RangeVar::catalogname, CheckSelectLocking(), elog, ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg(), ERROR, getRTEPermissionInfo(), i, LCS_asString(), lfirst, RangeVar::location, LockingClause::lockedRels, makeNode, NIL, parser_errposition(), RangeVar::relname, RTEPermissionInfo::requiredPerms, Query::rtable, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, RangeVar::schemaname, LockingClause::strength, RangeTblEntry::subquery, transformLockingClause(), and LockingClause::waitPolicy.

Referenced by transformLockingClause(), transformPLAssignStmt(), transformSelectStmt(), and transformSetOperationStmt().

◆ transformOnConflictClause()

static OnConflictExpr * transformOnConflictClause ( ParseState pstate,
OnConflictClause onConflictClause 
)
static

Definition at line 1208 of file analyze.c.

1210{
1211 ParseNamespaceItem *exclNSItem = NULL;
1212 List *arbiterElems;
1213 Node *arbiterWhere;
1214 Oid arbiterConstraint;
1215 List *onConflictSet = NIL;
1216 Node *onConflictWhere = NULL;
1217 int exclRelIndex = 0;
1218 List *exclRelTlist = NIL;
1219 OnConflictExpr *result;
1220
1221 /*
1222 * If this is ON CONFLICT ... UPDATE, first create the range table entry
1223 * for the EXCLUDED pseudo relation, so that that will be present while
1224 * processing arbiter expressions. (You can't actually reference it from
1225 * there, but this provides a useful error message if you try.)
1226 */
1227 if (onConflictClause->action == ONCONFLICT_UPDATE)
1228 {
1229 Relation targetrel = pstate->p_target_relation;
1230 RangeTblEntry *exclRte;
1231
1232 exclNSItem = addRangeTableEntryForRelation(pstate,
1233 targetrel,
1235 makeAlias("excluded", NIL),
1236 false, false);
1237 exclRte = exclNSItem->p_rte;
1238 exclRelIndex = exclNSItem->p_rtindex;
1239
1240 /*
1241 * relkind is set to composite to signal that we're not dealing with
1242 * an actual relation, and no permission checks are required on it.
1243 * (We'll check the actual target relation, instead.)
1244 */
1245 exclRte->relkind = RELKIND_COMPOSITE_TYPE;
1246
1247 /* Create EXCLUDED rel's targetlist for use by EXPLAIN */
1248 exclRelTlist = BuildOnConflictExcludedTargetlist(targetrel,
1249 exclRelIndex);
1250 }
1251
1252 /* Process the arbiter clause, ON CONFLICT ON (...) */
1253 transformOnConflictArbiter(pstate, onConflictClause, &arbiterElems,
1254 &arbiterWhere, &arbiterConstraint);
1255
1256 /* Process DO UPDATE */
1257 if (onConflictClause->action == ONCONFLICT_UPDATE)
1258 {
1259 /*
1260 * Expressions in the UPDATE targetlist need to be handled like UPDATE
1261 * not INSERT. We don't need to save/restore this because all INSERT
1262 * expressions have been parsed already.
1263 */
1264 pstate->p_is_insert = false;
1265
1266 /*
1267 * Add the EXCLUDED pseudo relation to the query namespace, making it
1268 * available in the UPDATE subexpressions.
1269 */
1270 addNSItemToQuery(pstate, exclNSItem, false, true, true);
1271
1272 /*
1273 * Now transform the UPDATE subexpressions.
1274 */
1275 onConflictSet =
1276 transformUpdateTargetList(pstate, onConflictClause->targetList);
1277
1278 onConflictWhere = transformWhereClause(pstate,
1279 onConflictClause->whereClause,
1280 EXPR_KIND_WHERE, "WHERE");
1281
1282 /*
1283 * Remove the EXCLUDED pseudo relation from the query namespace, since
1284 * it's not supposed to be available in RETURNING. (Maybe someday we
1285 * could allow that, and drop this step.)
1286 */
1287 Assert((ParseNamespaceItem *) llast(pstate->p_namespace) == exclNSItem);
1288 pstate->p_namespace = list_delete_last(pstate->p_namespace);
1289 }
1290
1291 /* Finally, build ON CONFLICT DO [NOTHING | UPDATE] expression */
1292 result = makeNode(OnConflictExpr);
1293
1294 result->action = onConflictClause->action;
1295 result->arbiterElems = arbiterElems;
1296 result->arbiterWhere = arbiterWhere;
1297 result->constraint = arbiterConstraint;
1298 result->onConflictSet = onConflictSet;
1299 result->onConflictWhere = onConflictWhere;
1300 result->exclRelIndex = exclRelIndex;
1301 result->exclRelTlist = exclRelTlist;
1302
1303 return result;
1304}
List * list_delete_last(List *list)
Definition: list.c:957
#define RowExclusiveLock
Definition: lockdefs.h:38
void transformOnConflictArbiter(ParseState *pstate, OnConflictClause *onConflictClause, List **arbiterExpr, Node **arbiterWhere, Oid *constraint)
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
List * transformUpdateTargetList(ParseState *pstate, List *origTlist)
Definition: analyze.c:2576
List * BuildOnConflictExcludedTargetlist(Relation targetrel, Index exclRelIndex)
Definition: analyze.c:1315
#define llast(l)
Definition: pg_list.h:198
OnConflictAction action
Definition: parsenodes.h:1633
List * arbiterElems
Definition: primnodes.h:2382
OnConflictAction action
Definition: primnodes.h:2379
List * onConflictSet
Definition: primnodes.h:2388
List * exclRelTlist
Definition: primnodes.h:2391
Node * onConflictWhere
Definition: primnodes.h:2389
Node * arbiterWhere
Definition: primnodes.h:2384
Relation p_target_relation
Definition: parse_node.h:225

References OnConflictClause::action, OnConflictExpr::action, addNSItemToQuery(), addRangeTableEntryForRelation(), OnConflictExpr::arbiterElems, OnConflictExpr::arbiterWhere, Assert, BuildOnConflictExcludedTargetlist(), OnConflictExpr::constraint, OnConflictExpr::exclRelIndex, OnConflictExpr::exclRelTlist, EXPR_KIND_WHERE, list_delete_last(), llast, makeAlias(), makeNode, NIL, ONCONFLICT_UPDATE, OnConflictExpr::onConflictSet, OnConflictExpr::onConflictWhere, ParseState::p_is_insert, ParseState::p_namespace, ParseNamespaceItem::p_rte, ParseNamespaceItem::p_rtindex, ParseState::p_target_relation, RowExclusiveLock, OnConflictClause::targetList, transformOnConflictArbiter(), transformUpdateTargetList(), transformWhereClause(), and OnConflictClause::whereClause.

◆ transformOptionalSelectInto()

static Query * transformOptionalSelectInto ( ParseState pstate,
Node parseTree 
)
static

Definition at line 357 of file analyze.c.

358{
359 if (IsA(parseTree, SelectStmt))
360 {
361 SelectStmt *stmt = (SelectStmt *) parseTree;
362
363 /* If it's a set-operation tree, drill down to leftmost SelectStmt */
364 while (stmt && stmt->op != SETOP_NONE)
365 stmt = stmt->larg;
366 Assert(stmt && IsA(stmt, SelectStmt) && stmt->larg == NULL);
367
368 if (stmt->intoClause)
369 {
371
372 ctas->query = parseTree;
373 ctas->into = stmt->intoClause;
374 ctas->objtype = OBJECT_TABLE;
375 ctas->is_select_into = true;
376
377 /*
378 * Remove the intoClause from the SelectStmt. This makes it safe
379 * for transformSelectStmt to complain if it finds intoClause set
380 * (implying that the INTO appeared in a disallowed place).
381 */
382 stmt->intoClause = NULL;
383
384 parseTree = (Node *) ctas;
385 }
386 }
387
388 return transformStmt(pstate, parseTree);
389}
@ SETOP_NONE
Definition: parsenodes.h:2162
@ OBJECT_TABLE
Definition: parsenodes.h:2353
IntoClause * into
Definition: parsenodes.h:3967
ObjectType objtype
Definition: parsenodes.h:3968

References Assert, CreateTableAsStmt::into, CreateTableAsStmt::is_select_into, IsA, makeNode, OBJECT_TABLE, CreateTableAsStmt::objtype, CreateTableAsStmt::query, SETOP_NONE, stmt, and transformStmt().

Referenced by transformExplainStmt(), and transformTopLevelStmt().

◆ transformPLAssignStmt()

static Query * transformPLAssignStmt ( ParseState pstate,
PLAssignStmt stmt 
)
static

Definition at line 2813 of file analyze.c.

2814{
2815 Query *qry = makeNode(Query);
2816 ColumnRef *cref = makeNode(ColumnRef);
2817 List *indirection = stmt->indirection;
2818 int nnames = stmt->nnames;
2819 SelectStmt *sstmt = stmt->val;
2820 Node *target;
2821 Oid targettype;
2822 int32 targettypmod;
2823 Oid targetcollation;
2824 List *tlist;
2825 TargetEntry *tle;
2826 Oid type_id;
2827 Node *qual;
2828 ListCell *l;
2829
2830 /*
2831 * First, construct a ColumnRef for the target variable. If the target
2832 * has more than one dotted name, we have to pull the extra names out of
2833 * the indirection list.
2834 */
2835 cref->fields = list_make1(makeString(stmt->name));
2836 cref->location = stmt->location;
2837 if (nnames > 1)
2838 {
2839 /* avoid munging the raw parsetree */
2840 indirection = list_copy(indirection);
2841 while (--nnames > 0 && indirection != NIL)
2842 {
2843 Node *ind = (Node *) linitial(indirection);
2844
2845 if (!IsA(ind, String))
2846 elog(ERROR, "invalid name count in PLAssignStmt");
2847 cref->fields = lappend(cref->fields, ind);
2848 indirection = list_delete_first(indirection);
2849 }
2850 }
2851
2852 /*
2853 * Transform the target reference. Typically we will get back a Param
2854 * node, but there's no reason to be too picky about its type.
2855 */
2856 target = transformExpr(pstate, (Node *) cref,
2858 targettype = exprType(target);
2859 targettypmod = exprTypmod(target);
2860 targetcollation = exprCollation(target);
2861
2862 /*
2863 * The rest mostly matches transformSelectStmt, except that we needn't
2864 * consider WITH or INTO, and we build a targetlist our own way.
2865 */
2866 qry->commandType = CMD_SELECT;
2867 pstate->p_is_insert = false;
2868
2869 /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
2870 pstate->p_locking_clause = sstmt->lockingClause;
2871
2872 /* make WINDOW info available for window functions, too */
2873 pstate->p_windowdefs = sstmt->windowClause;
2874
2875 /* process the FROM clause */
2876 transformFromClause(pstate, sstmt->fromClause);
2877
2878 /* initially transform the targetlist as if in SELECT */
2879 tlist = transformTargetList(pstate, sstmt->targetList,
2881
2882 /* we should have exactly one targetlist item */
2883 if (list_length(tlist) != 1)
2884 ereport(ERROR,
2885 (errcode(ERRCODE_SYNTAX_ERROR),
2886 errmsg_plural("assignment source returned %d column",
2887 "assignment source returned %d columns",
2888 list_length(tlist),
2889 list_length(tlist))));
2890
2891 tle = linitial_node(TargetEntry, tlist);
2892
2893 /*
2894 * This next bit is similar to transformAssignedExpr; the key difference
2895 * is we use COERCION_PLPGSQL not COERCION_ASSIGNMENT.
2896 */
2897 type_id = exprType((Node *) tle->expr);
2898
2900
2901 if (indirection)
2902 {
2903 tle->expr = (Expr *)
2905 target,
2906 stmt->name,
2907 false,
2908 targettype,
2909 targettypmod,
2910 targetcollation,
2911 indirection,
2912 list_head(indirection),
2913 (Node *) tle->expr,
2915 exprLocation(target));
2916 }
2917 else if (targettype != type_id &&
2918 (targettype == RECORDOID || ISCOMPLEX(targettype)) &&
2919 (type_id == RECORDOID || ISCOMPLEX(type_id)))
2920 {
2921 /*
2922 * Hack: do not let coerce_to_target_type() deal with inconsistent
2923 * composite types. Just pass the expression result through as-is,
2924 * and let the PL/pgSQL executor do the conversion its way. This is
2925 * rather bogus, but it's needed for backwards compatibility.
2926 */
2927 }
2928 else
2929 {
2930 /*
2931 * For normal non-qualified target column, do type checking and
2932 * coercion.
2933 */
2934 Node *orig_expr = (Node *) tle->expr;
2935
2936 tle->expr = (Expr *)
2937 coerce_to_target_type(pstate,
2938 orig_expr, type_id,
2939 targettype, targettypmod,
2942 -1);
2943 /* With COERCION_PLPGSQL, this error is probably unreachable */
2944 if (tle->expr == NULL)
2945 ereport(ERROR,
2946 (errcode(ERRCODE_DATATYPE_MISMATCH),
2947 errmsg("variable \"%s\" is of type %s"
2948 " but expression is of type %s",
2949 stmt->name,
2950 format_type_be(targettype),
2951 format_type_be(type_id)),
2952 errhint("You will need to rewrite or cast the expression."),
2953 parser_errposition(pstate, exprLocation(orig_expr))));
2954 }
2955
2956 pstate->p_expr_kind = EXPR_KIND_NONE;
2957
2958 qry->targetList = list_make1(tle);
2959
2960 /* transform WHERE */
2961 qual = transformWhereClause(pstate, sstmt->whereClause,
2962 EXPR_KIND_WHERE, "WHERE");
2963
2964 /* initial processing of HAVING clause is much like WHERE clause */
2965 qry->havingQual = transformWhereClause(pstate, sstmt->havingClause,
2966 EXPR_KIND_HAVING, "HAVING");
2967
2968 /*
2969 * Transform sorting/grouping stuff. Do ORDER BY first because both
2970 * transformGroupClause and transformDistinctClause need the results. Note
2971 * that these functions can also change the targetList, so it's passed to
2972 * them by reference.
2973 */
2974 qry->sortClause = transformSortClause(pstate,
2975 sstmt->sortClause,
2976 &qry->targetList,
2978 false /* allow SQL92 rules */ );
2979
2980 qry->groupClause = transformGroupClause(pstate,
2981 sstmt->groupClause,
2982 &qry->groupingSets,
2983 &qry->targetList,
2984 qry->sortClause,
2986 false /* allow SQL92 rules */ );
2987
2988 if (sstmt->distinctClause == NIL)
2989 {
2990 qry->distinctClause = NIL;
2991 qry->hasDistinctOn = false;
2992 }
2993 else if (linitial(sstmt->distinctClause) == NULL)
2994 {
2995 /* We had SELECT DISTINCT */
2997 &qry->targetList,
2998 qry->sortClause,
2999 false);
3000 qry->hasDistinctOn = false;
3001 }
3002 else
3003 {
3004 /* We had SELECT DISTINCT ON */
3006 sstmt->distinctClause,
3007 &qry->targetList,
3008 qry->sortClause);
3009 qry->hasDistinctOn = true;
3010 }
3011
3012 /* transform LIMIT */
3013 qry->limitOffset = transformLimitClause(pstate, sstmt->limitOffset,
3014 EXPR_KIND_OFFSET, "OFFSET",
3015 sstmt->limitOption);
3016 qry->limitCount = transformLimitClause(pstate, sstmt->limitCount,
3017 EXPR_KIND_LIMIT, "LIMIT",
3018 sstmt->limitOption);
3019 qry->limitOption = sstmt->limitOption;
3020
3021 /* transform window clauses after we have seen all window functions */
3023 pstate->p_windowdefs,
3024 &qry->targetList);
3025
3026 qry->rtable = pstate->p_rtable;
3027 qry->rteperminfos = pstate->p_rteperminfos;
3028 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
3029
3030 qry->hasSubLinks = pstate->p_hasSubLinks;
3031 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
3032 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
3033 qry->hasAggs = pstate->p_hasAggs;
3034
3035 foreach(l, sstmt->lockingClause)
3036 {
3037 transformLockingClause(pstate, qry,
3038 (LockingClause *) lfirst(l), false);
3039 }
3040
3041 assign_query_collations(pstate, qry);
3042
3043 /* this must be done after collations, for reliable comparison of exprs */
3044 if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
3045 parseCheckAggregates(pstate, qry);
3046
3047 return qry;
3048}
int32_t int32
Definition: c.h:484
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1180
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
List * list_delete_first(List *list)
Definition: list.c:943
List * list_copy(const List *oldlist)
Definition: list.c:1573
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
List * transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99)
List * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
List * transformDistinctOnClause(ParseState *pstate, List *distinctlist, List **targetlist, List *sortClause)
List * transformWindowDefinitions(ParseState *pstate, List *windowdefs, List **targetlist)
List * transformDistinctClause(ParseState *pstate, List **targetlist, List *sortClause, bool is_agg)
Node * transformLimitClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName, LimitOption limitOption)
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:78
@ EXPR_KIND_ORDER_BY
Definition: parse_node.h:60
@ EXPR_KIND_OFFSET
Definition: parse_node.h:63
@ EXPR_KIND_HAVING
Definition: parse_node.h:47
@ EXPR_KIND_LIMIT
Definition: parse_node.h:62
@ EXPR_KIND_UPDATE_TARGET
Definition: parse_node.h:57
@ EXPR_KIND_SELECT_TARGET
Definition: parse_node.h:54
@ EXPR_KIND_NONE
Definition: parse_node.h:40
@ EXPR_KIND_GROUP_BY
Definition: parse_node.h:59
Node * transformAssignmentIndirection(ParseState *pstate, Node *basenode, const char *targetName, bool targetIsSubscripting, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, List *indirection, ListCell *indirection_cell, Node *rhs, CoercionContext ccontext, int location)
Definition: parse_target.c:686
List * transformTargetList(ParseState *pstate, List *targetlist, ParseExprKind exprKind)
Definition: parse_target.c:121
#define ISCOMPLEX(typeid)
Definition: parse_type.h:59
#define linitial_node(type, l)
Definition: pg_list.h:181
#define list_make1(x1)
Definition: pg_list.h:212
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:752
@ COERCION_PLPGSQL
Definition: primnodes.h:732
ParseLoc location
Definition: parsenodes.h:306
List * fields
Definition: parsenodes.h:305
ParseExprKind p_expr_kind
Definition: parse_node.h:230
List * p_windowdefs
Definition: parse_node.h:229
List * p_locking_clause
Definition: parse_node.h:233
Node * limitCount
Definition: parsenodes.h:225
Node * limitOffset
Definition: parsenodes.h:224
LimitOption limitOption
Definition: parsenodes.h:226
List * windowClause
Definition: parsenodes.h:218
List * sortClause
Definition: parsenodes.h:222
LimitOption limitOption
Definition: parsenodes.h:2203
List * targetList
Definition: parsenodes.h:2178
List * fromClause
Definition: parsenodes.h:2179
List * groupClause
Definition: parsenodes.h:2181
Node * havingClause
Definition: parsenodes.h:2183
List * windowClause
Definition: parsenodes.h:2184
List * distinctClause
Definition: parsenodes.h:2175
Node * whereClause
Definition: parsenodes.h:2180
Definition: value.h:64
String * makeString(char *str)
Definition: value.c:63

References assign_query_collations(), CMD_SELECT, COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_PLPGSQL, Query::commandType, Query::distinctClause, SelectStmt::distinctClause, elog, ereport, errcode(), errhint(), errmsg(), errmsg_plural(), ERROR, TargetEntry::expr, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_LIMIT, EXPR_KIND_NONE, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_SELECT_TARGET, EXPR_KIND_UPDATE_TARGET, EXPR_KIND_WHERE, exprCollation(), exprLocation(), exprType(), exprTypmod(), ColumnRef::fields, format_type_be(), SelectStmt::fromClause, Query::groupClause, SelectStmt::groupClause, Query::groupingSets, SelectStmt::havingClause, Query::havingQual, IsA, ISCOMPLEX, Query::jointree, lappend(), lfirst, Query::limitCount, SelectStmt::limitCount, Query::limitOffset, SelectStmt::limitOffset, Query::limitOption, SelectStmt::limitOption, linitial, linitial_node, list_copy(), list_delete_first(), list_head(), list_length(), list_make1, ColumnRef::location, SelectStmt::lockingClause, makeFromExpr(), makeNode, makeString(), NIL, ParseState::p_expr_kind, ParseState::p_hasAggs, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_hasWindowFuncs, ParseState::p_is_insert, ParseState::p_joinlist, ParseState::p_locking_clause, ParseState::p_rtable, ParseState::p_rteperminfos, ParseState::p_windowdefs, parseCheckAggregates(), parser_errposition(), Query::rtable, Query::sortClause, SelectStmt::sortClause, stmt, Query::targetList, SelectStmt::targetList, transformAssignmentIndirection(), transformDistinctClause(), transformDistinctOnClause(), transformExpr(), transformFromClause(), transformGroupClause(), transformLimitClause(), transformLockingClause(), transformSortClause(), transformTargetList(), transformWhereClause(), transformWindowDefinitions(), SelectStmt::whereClause, Query::windowClause, and SelectStmt::windowClause.

Referenced by transformStmt().

◆ transformReturningClause()

void transformReturningClause ( ParseState pstate,
Query qry,
ReturningClause returningClause,
ParseExprKind  exprKind 
)

Definition at line 2691 of file analyze.c.

2694{
2695 int save_nslen = list_length(pstate->p_namespace);
2696 int save_next_resno;
2697
2698 if (returningClause == NULL)
2699 return; /* nothing to do */
2700
2701 /*
2702 * Scan RETURNING WITH(...) options for OLD/NEW alias names. Complain if
2703 * there is any conflict with existing relations.
2704 */
2705 foreach_node(ReturningOption, option, returningClause->options)
2706 {
2707 switch (option->option)
2708 {
2710 if (qry->returningOldAlias != NULL)
2711 ereport(ERROR,
2712 errcode(ERRCODE_SYNTAX_ERROR),
2713 /* translator: %s is OLD or NEW */
2714 errmsg("%s cannot be specified multiple times", "OLD"),
2715 parser_errposition(pstate, option->location));
2716 qry->returningOldAlias = option->value;
2717 break;
2718
2720 if (qry->returningNewAlias != NULL)
2721 ereport(ERROR,
2722 errcode(ERRCODE_SYNTAX_ERROR),
2723 /* translator: %s is OLD or NEW */
2724 errmsg("%s cannot be specified multiple times", "NEW"),
2725 parser_errposition(pstate, option->location));
2726 qry->returningNewAlias = option->value;
2727 break;
2728
2729 default:
2730 elog(ERROR, "unrecognized returning option: %d", option->option);
2731 }
2732
2733 if (refnameNamespaceItem(pstate, NULL, option->value, -1, NULL) != NULL)
2734 ereport(ERROR,
2735 errcode(ERRCODE_DUPLICATE_ALIAS),
2736 errmsg("table name \"%s\" specified more than once",
2737 option->value),
2738 parser_errposition(pstate, option->location));
2739
2740 addNSItemForReturning(pstate, option->value,
2741 option->option == RETURNING_OPTION_OLD ?
2743 }
2744
2745 /*
2746 * If OLD/NEW alias names weren't explicitly specified, use "old"/"new"
2747 * unless masked by existing relations.
2748 */
2749 if (qry->returningOldAlias == NULL &&
2750 refnameNamespaceItem(pstate, NULL, "old", -1, NULL) == NULL)
2751 {
2752 qry->returningOldAlias = "old";
2754 }
2755 if (qry->returningNewAlias == NULL &&
2756 refnameNamespaceItem(pstate, NULL, "new", -1, NULL) == NULL)
2757 {
2758 qry->returningNewAlias = "new";
2760 }
2761
2762 /*
2763 * We need to assign resnos starting at one in the RETURNING list. Save
2764 * and restore the main tlist's value of p_next_resno, just in case
2765 * someone looks at it later (probably won't happen).
2766 */
2767 save_next_resno = pstate->p_next_resno;
2768 pstate->p_next_resno = 1;
2769
2770 /* transform RETURNING expressions identically to a SELECT targetlist */
2771 qry->returningList = transformTargetList(pstate,
2772 returningClause->exprs,
2773 exprKind);
2774
2775 /*
2776 * Complain if the nonempty tlist expanded to nothing (which is possible
2777 * if it contains only a star-expansion of a zero-column table). If we
2778 * allow this, the parsed Query will look like it didn't have RETURNING,
2779 * with results that would probably surprise the user.
2780 */
2781 if (qry->returningList == NIL)
2782 ereport(ERROR,
2783 (errcode(ERRCODE_SYNTAX_ERROR),
2784 errmsg("RETURNING must have at least one column"),
2785 parser_errposition(pstate,
2786 exprLocation(linitial(returningClause->exprs)))));
2787
2788 /* mark column origins */
2790
2791 /* resolve any still-unresolved output columns as being type text */
2792 if (pstate->p_resolve_unknowns)
2794
2795 /* restore state */
2796 pstate->p_namespace = list_truncate(pstate->p_namespace, save_nslen);
2797 pstate->p_next_resno = save_next_resno;
2798}
List * list_truncate(List *list, int new_size)
Definition: list.c:631
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
void resolveTargetListUnknowns(ParseState *pstate, List *targetlist)
Definition: parse_target.c:288
void markTargetListOrigins(ParseState *pstate, List *targetlist)
Definition: parse_target.c:318
@ RETURNING_OPTION_NEW
Definition: parsenodes.h:1747
@ RETURNING_OPTION_OLD
Definition: parsenodes.h:1746
static void addNSItemForReturning(ParseState *pstate, const char *aliasname, VarReturningType returning_type)
Definition: analyze.c:2651
#define foreach_node(type, var, lst)
Definition: pg_list.h:496
@ VAR_RETURNING_OLD
Definition: primnodes.h:256
@ VAR_RETURNING_NEW
Definition: primnodes.h:257
int p_next_resno
Definition: parse_node.h:231
List * returningList
Definition: parsenodes.h:209

References addNSItemForReturning(), elog, ereport, errcode(), errmsg(), ERROR, exprLocation(), ReturningClause::exprs, foreach_node, linitial, list_length(), list_truncate(), markTargetListOrigins(), NIL, ReturningClause::options, ParseState::p_namespace, ParseState::p_next_resno, ParseState::p_resolve_unknowns, parser_errposition(), refnameNamespaceItem(), resolveTargetListUnknowns(), RETURNING_OPTION_NEW, RETURNING_OPTION_OLD, Query::returningList, transformTargetList(), VAR_RETURNING_NEW, and VAR_RETURNING_OLD.

Referenced by transformDeleteStmt(), transformMergeStmt(), and transformUpdateStmt().

◆ transformReturnStmt()

static Query * transformReturnStmt ( ParseState pstate,
ReturnStmt stmt 
)
static

Definition at line 2479 of file analyze.c.

2480{
2481 Query *qry = makeNode(Query);
2482
2483 qry->commandType = CMD_SELECT;
2484 qry->isReturn = true;
2485
2487 1, NULL, false));
2488
2489 if (pstate->p_resolve_unknowns)
2491 qry->rtable = pstate->p_rtable;
2492 qry->rteperminfos = pstate->p_rteperminfos;
2493 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2494 qry->hasSubLinks = pstate->p_hasSubLinks;
2495 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
2496 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2497 qry->hasAggs = pstate->p_hasAggs;
2498
2499 assign_query_collations(pstate, qry);
2500
2501 return qry;
2502}

References assign_query_collations(), CMD_SELECT, Query::commandType, EXPR_KIND_SELECT_TARGET, Query::jointree, list_make1, makeFromExpr(), makeNode, makeTargetEntry(), ParseState::p_hasAggs, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_hasWindowFuncs, ParseState::p_joinlist, ParseState::p_resolve_unknowns, ParseState::p_rtable, ParseState::p_rteperminfos, resolveTargetListUnknowns(), Query::rtable, stmt, Query::targetList, and transformExpr().

Referenced by transformStmt().

◆ transformSelectStmt()

static Query * transformSelectStmt ( ParseState pstate,
SelectStmt stmt 
)
static

Definition at line 1427 of file analyze.c.

1428{
1429 Query *qry = makeNode(Query);
1430 Node *qual;
1431 ListCell *l;
1432
1433 qry->commandType = CMD_SELECT;
1434
1435 /* process the WITH clause independently of all else */
1436 if (stmt->withClause)
1437 {
1438 qry->hasRecursive = stmt->withClause->recursive;
1439 qry->cteList = transformWithClause(pstate, stmt->withClause);
1440 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1441 }
1442
1443 /* Complain if we get called from someplace where INTO is not allowed */
1444 if (stmt->intoClause)
1445 ereport(ERROR,
1446 (errcode(ERRCODE_SYNTAX_ERROR),
1447 errmsg("SELECT ... INTO is not allowed here"),
1448 parser_errposition(pstate,
1449 exprLocation((Node *) stmt->intoClause))));
1450
1451 /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
1452 pstate->p_locking_clause = stmt->lockingClause;
1453
1454 /* make WINDOW info available for window functions, too */
1455 pstate->p_windowdefs = stmt->windowClause;
1456
1457 /* process the FROM clause */
1458 transformFromClause(pstate, stmt->fromClause);
1459
1460 /* transform targetlist */
1461 qry->targetList = transformTargetList(pstate, stmt->targetList,
1463
1464 /* mark column origins */
1465 markTargetListOrigins(pstate, qry->targetList);
1466
1467 /* transform WHERE */
1468 qual = transformWhereClause(pstate, stmt->whereClause,
1469 EXPR_KIND_WHERE, "WHERE");
1470
1471 /* initial processing of HAVING clause is much like WHERE clause */
1472 qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
1473 EXPR_KIND_HAVING, "HAVING");
1474
1475 /*
1476 * Transform sorting/grouping stuff. Do ORDER BY first because both
1477 * transformGroupClause and transformDistinctClause need the results. Note
1478 * that these functions can also change the targetList, so it's passed to
1479 * them by reference.
1480 */
1481 qry->sortClause = transformSortClause(pstate,
1482 stmt->sortClause,
1483 &qry->targetList,
1485 false /* allow SQL92 rules */ );
1486
1487 qry->groupClause = transformGroupClause(pstate,
1488 stmt->groupClause,
1489 &qry->groupingSets,
1490 &qry->targetList,
1491 qry->sortClause,
1493 false /* allow SQL92 rules */ );
1494 qry->groupDistinct = stmt->groupDistinct;
1495
1496 if (stmt->distinctClause == NIL)
1497 {
1498 qry->distinctClause = NIL;
1499 qry->hasDistinctOn = false;
1500 }
1501 else if (linitial(stmt->distinctClause) == NULL)
1502 {
1503 /* We had SELECT DISTINCT */
1505 &qry->targetList,
1506 qry->sortClause,
1507 false);
1508 qry->hasDistinctOn = false;
1509 }
1510 else
1511 {
1512 /* We had SELECT DISTINCT ON */
1514 stmt->distinctClause,
1515 &qry->targetList,
1516 qry->sortClause);
1517 qry->hasDistinctOn = true;
1518 }
1519
1520 /* transform LIMIT */
1521 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1522 EXPR_KIND_OFFSET, "OFFSET",
1523 stmt->limitOption);
1524 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1525 EXPR_KIND_LIMIT, "LIMIT",
1526 stmt->limitOption);
1527 qry->limitOption = stmt->limitOption;
1528
1529 /* transform window clauses after we have seen all window functions */
1531 pstate->p_windowdefs,
1532 &qry->targetList);
1533
1534 /* resolve any still-unresolved output columns as being type text */
1535 if (pstate->p_resolve_unknowns)
1537
1538 qry->rtable = pstate->p_rtable;
1539 qry->rteperminfos = pstate->p_rteperminfos;
1540 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1541
1542 qry->hasSubLinks = pstate->p_hasSubLinks;
1543 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1544 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1545 qry->hasAggs = pstate->p_hasAggs;
1546
1547 foreach(l, stmt->lockingClause)
1548 {
1549 transformLockingClause(pstate, qry,
1550 (LockingClause *) lfirst(l), false);
1551 }
1552
1553 assign_query_collations(pstate, qry);
1554
1555 /* this must be done after collations, for reliable comparison of exprs */
1556 if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
1557 parseCheckAggregates(pstate, qry);
1558
1559 return qry;
1560}
bool groupDistinct
Definition: parsenodes.h:212

References assign_query_collations(), CMD_SELECT, Query::commandType, Query::cteList, Query::distinctClause, ereport, errcode(), errmsg(), ERROR, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_LIMIT, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_SELECT_TARGET, EXPR_KIND_WHERE, exprLocation(), Query::groupClause, Query::groupDistinct, Query::groupingSets, Query::havingQual, Query::jointree, lfirst, Query::limitCount, Query::limitOffset, Query::limitOption, linitial, makeFromExpr(), makeNode, markTargetListOrigins(), NIL, ParseState::p_hasAggs, ParseState::p_hasModifyingCTE, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_hasWindowFuncs, ParseState::p_joinlist, ParseState::p_locking_clause, ParseState::p_resolve_unknowns, ParseState::p_rtable, ParseState::p_rteperminfos, ParseState::p_windowdefs, parseCheckAggregates(), parser_errposition(), resolveTargetListUnknowns(), Query::rtable, Query::sortClause, stmt, Query::targetList, transformDistinctClause(), transformDistinctOnClause(), transformFromClause(), transformGroupClause(), transformLimitClause(), transformLockingClause(), transformSortClause(), transformTargetList(), transformWhereClause(), transformWindowDefinitions(), transformWithClause(), and Query::windowClause.

Referenced by transformStmt().

◆ transformSetOperationStmt()

static Query * transformSetOperationStmt ( ParseState pstate,
SelectStmt stmt 
)
static

Definition at line 1789 of file analyze.c.

1790{
1791 Query *qry = makeNode(Query);
1792 SelectStmt *leftmostSelect;
1793 int leftmostRTI;
1794 Query *leftmostQuery;
1795 SetOperationStmt *sostmt;
1796 List *sortClause;
1797 Node *limitOffset;
1798 Node *limitCount;
1799 List *lockingClause;
1800 WithClause *withClause;
1801 Node *node;
1802 ListCell *left_tlist,
1803 *lct,
1804 *lcm,
1805 *lcc,
1806 *l;
1807 List *targetvars,
1808 *targetnames,
1809 *sv_namespace;
1810 int sv_rtable_length;
1811 ParseNamespaceItem *jnsitem;
1812 ParseNamespaceColumn *sortnscolumns;
1813 int sortcolindex;
1814 int tllen;
1815
1816 qry->commandType = CMD_SELECT;
1817
1818 /*
1819 * Find leftmost leaf SelectStmt. We currently only need to do this in
1820 * order to deliver a suitable error message if there's an INTO clause
1821 * there, implying the set-op tree is in a context that doesn't allow
1822 * INTO. (transformSetOperationTree would throw error anyway, but it
1823 * seems worth the trouble to throw a different error for non-leftmost
1824 * INTO, so we produce that error in transformSetOperationTree.)
1825 */
1826 leftmostSelect = stmt->larg;
1827 while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
1828 leftmostSelect = leftmostSelect->larg;
1829 Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
1830 leftmostSelect->larg == NULL);
1831 if (leftmostSelect->intoClause)
1832 ereport(ERROR,
1833 (errcode(ERRCODE_SYNTAX_ERROR),
1834 errmsg("SELECT ... INTO is not allowed here"),
1835 parser_errposition(pstate,
1836 exprLocation((Node *) leftmostSelect->intoClause))));
1837
1838 /*
1839 * We need to extract ORDER BY and other top-level clauses here and not
1840 * let transformSetOperationTree() see them --- else it'll just recurse
1841 * right back here!
1842 */
1843 sortClause = stmt->sortClause;
1844 limitOffset = stmt->limitOffset;
1845 limitCount = stmt->limitCount;
1846 lockingClause = stmt->lockingClause;
1847 withClause = stmt->withClause;
1848
1849 stmt->sortClause = NIL;
1850 stmt->limitOffset = NULL;
1851 stmt->limitCount = NULL;
1852 stmt->lockingClause = NIL;
1853 stmt->withClause = NULL;
1854
1855 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1856 if (lockingClause)
1857 ereport(ERROR,
1858 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1859 /*------
1860 translator: %s is a SQL row locking clause such as FOR UPDATE */
1861 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
1863 linitial(lockingClause))->strength))));
1864
1865 /* Process the WITH clause independently of all else */
1866 if (withClause)
1867 {
1868 qry->hasRecursive = withClause->recursive;
1869 qry->cteList = transformWithClause(pstate, withClause);
1870 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1871 }
1872
1873 /*
1874 * Recursively transform the components of the tree.
1875 */
1876 sostmt = castNode(SetOperationStmt,
1877 transformSetOperationTree(pstate, stmt, true, NULL));
1878 Assert(sostmt);
1879 qry->setOperations = (Node *) sostmt;
1880
1881 /*
1882 * Re-find leftmost SELECT (now it's a sub-query in rangetable)
1883 */
1884 node = sostmt->larg;
1885 while (node && IsA(node, SetOperationStmt))
1886 node = ((SetOperationStmt *) node)->larg;
1887 Assert(node && IsA(node, RangeTblRef));
1888 leftmostRTI = ((RangeTblRef *) node)->rtindex;
1889 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1890 Assert(leftmostQuery != NULL);
1891
1892 /*
1893 * Generate dummy targetlist for outer query using column names of
1894 * leftmost select and common datatypes/collations of topmost set
1895 * operation. Also make lists of the dummy vars and their names for use
1896 * in parsing ORDER BY.
1897 *
1898 * Note: we use leftmostRTI as the varno of the dummy variables. It
1899 * shouldn't matter too much which RT index they have, as long as they
1900 * have one that corresponds to a real RT entry; else funny things may
1901 * happen when the tree is mashed by rule rewriting.
1902 */
1903 qry->targetList = NIL;
1904 targetvars = NIL;
1905 targetnames = NIL;
1906 sortnscolumns = (ParseNamespaceColumn *)
1907 palloc0(list_length(sostmt->colTypes) * sizeof(ParseNamespaceColumn));
1908 sortcolindex = 0;
1909
1910 forfour(lct, sostmt->colTypes,
1911 lcm, sostmt->colTypmods,
1912 lcc, sostmt->colCollations,
1913 left_tlist, leftmostQuery->targetList)
1914 {
1915 Oid colType = lfirst_oid(lct);
1916 int32 colTypmod = lfirst_int(lcm);
1917 Oid colCollation = lfirst_oid(lcc);
1918 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1919 char *colName;
1920 TargetEntry *tle;
1921 Var *var;
1922
1923 Assert(!lefttle->resjunk);
1924 colName = pstrdup(lefttle->resname);
1925 var = makeVar(leftmostRTI,
1926 lefttle->resno,
1927 colType,
1928 colTypmod,
1929 colCollation,
1930 0);
1931 var->location = exprLocation((Node *) lefttle->expr);
1932 tle = makeTargetEntry((Expr *) var,
1933 (AttrNumber) pstate->p_next_resno++,
1934 colName,
1935 false);
1936 qry->targetList = lappend(qry->targetList, tle);
1937 targetvars = lappend(targetvars, var);
1938 targetnames = lappend(targetnames, makeString(colName));
1939 sortnscolumns[sortcolindex].p_varno = leftmostRTI;
1940 sortnscolumns[sortcolindex].p_varattno = lefttle->resno;
1941 sortnscolumns[sortcolindex].p_vartype = colType;
1942 sortnscolumns[sortcolindex].p_vartypmod = colTypmod;
1943 sortnscolumns[sortcolindex].p_varcollid = colCollation;
1944 sortnscolumns[sortcolindex].p_varnosyn = leftmostRTI;
1945 sortnscolumns[sortcolindex].p_varattnosyn = lefttle->resno;
1946 sortcolindex++;
1947 }
1948
1949 /*
1950 * As a first step towards supporting sort clauses that are expressions
1951 * using the output columns, generate a namespace entry that makes the
1952 * output columns visible. A Join RTE node is handy for this, since we
1953 * can easily control the Vars generated upon matches.
1954 *
1955 * Note: we don't yet do anything useful with such cases, but at least
1956 * "ORDER BY upper(foo)" will draw the right error message rather than
1957 * "foo not found".
1958 */
1959 sv_rtable_length = list_length(pstate->p_rtable);
1960
1961 jnsitem = addRangeTableEntryForJoin(pstate,
1962 targetnames,
1963 sortnscolumns,
1964 JOIN_INNER,
1965 0,
1966 targetvars,
1967 NIL,
1968 NIL,
1969 NULL,
1970 NULL,
1971 false);
1972
1973 sv_namespace = pstate->p_namespace;
1974 pstate->p_namespace = NIL;
1975
1976 /* add jnsitem to column namespace only */
1977 addNSItemToQuery(pstate, jnsitem, false, false, true);
1978
1979 /*
1980 * For now, we don't support resjunk sort clauses on the output of a
1981 * setOperation tree --- you can only use the SQL92-spec options of
1982 * selecting an output column by name or number. Enforce by checking that
1983 * transformSortClause doesn't add any items to tlist. Note, if changing
1984 * this, add_setop_child_rel_equivalences() will need to be updated.
1985 */
1986 tllen = list_length(qry->targetList);
1987
1988 qry->sortClause = transformSortClause(pstate,
1989 sortClause,
1990 &qry->targetList,
1992 false /* allow SQL92 rules */ );
1993
1994 /* restore namespace, remove join RTE from rtable */
1995 pstate->p_namespace = sv_namespace;
1996 pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
1997
1998 if (tllen != list_length(qry->targetList))
1999 ereport(ERROR,
2000 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2001 errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
2002 errdetail("Only result column names can be used, not expressions or functions."),
2003 errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
2004 parser_errposition(pstate,
2005 exprLocation(list_nth(qry->targetList, tllen)))));
2006
2007 qry->limitOffset = transformLimitClause(pstate, limitOffset,
2008 EXPR_KIND_OFFSET, "OFFSET",
2009 stmt->limitOption);
2010 qry->limitCount = transformLimitClause(pstate, limitCount,
2011 EXPR_KIND_LIMIT, "LIMIT",
2012 stmt->limitOption);
2013 qry->limitOption = stmt->limitOption;
2014
2015 qry->rtable = pstate->p_rtable;
2016 qry->rteperminfos = pstate->p_rteperminfos;
2017 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2018
2019 qry->hasSubLinks = pstate->p_hasSubLinks;
2020 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
2021 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2022 qry->hasAggs = pstate->p_hasAggs;
2023
2024 foreach(l, lockingClause)
2025 {
2026 transformLockingClause(pstate, qry,
2027 (LockingClause *) lfirst(l), false);
2028 }
2029
2030 assign_query_collations(pstate, qry);
2031
2032 /* this must be done after collations, for reliable comparison of exprs */
2033 if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
2034 parseCheckAggregates(pstate, qry);
2035
2036 return qry;
2037}
void * palloc0(Size size)
Definition: mcxt.c:1347
@ JOIN_INNER
Definition: nodes.h:293
struct ParseNamespaceColumn ParseNamespaceColumn
Definition: parse_node.h:25
ParseNamespaceItem * addRangeTableEntryForJoin(ParseState *pstate, List *colnames, ParseNamespaceColumn *nscolumns, JoinType jointype, int nummergedcols, List *aliasvars, List *leftcols, List *rightcols, Alias *join_using_alias, Alias *alias, bool inFromCl)
static Node * transformSetOperationTree(ParseState *pstate, SelectStmt *stmt, bool isTopLevel, List **targetlist)
Definition: analyze.c:2094
#define forfour(cell1, list1, cell2, list2, cell3, list3, cell4, list4)
Definition: pg_list.h:575
#define lfirst_oid(lc)
Definition: pg_list.h:174
AttrNumber p_varattno
Definition: parse_node.h:347
AttrNumber p_varattnosyn
Definition: parse_node.h:353
struct SelectStmt * larg
Definition: parsenodes.h:2212
SetOperation op
Definition: parsenodes.h:2210
AttrNumber resno
Definition: primnodes.h:2247
bool recursive
Definition: parsenodes.h:1605

References addNSItemToQuery(), addRangeTableEntryForJoin(), Assert, assign_query_collations(), castNode, CMD_SELECT, Query::commandType, Query::cteList, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, TargetEntry::expr, EXPR_KIND_LIMIT, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, exprLocation(), forfour, Query::groupClause, Query::groupingSets, Query::havingQual, SelectStmt::intoClause, IsA, JOIN_INNER, Query::jointree, lappend(), SelectStmt::larg, SetOperationStmt::larg, LCS_asString(), lfirst, lfirst_int, lfirst_oid, Query::limitCount, Query::limitOffset, Query::limitOption, linitial, list_length(), list_nth(), list_truncate(), Var::location, makeFromExpr(), makeNode, makeString(), makeTargetEntry(), makeVar(), NIL, SelectStmt::op, ParseState::p_hasAggs, ParseState::p_hasModifyingCTE, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_hasWindowFuncs, ParseState::p_joinlist, ParseState::p_namespace, ParseState::p_next_resno, ParseState::p_rtable, ParseState::p_rteperminfos, ParseNamespaceColumn::p_varattno, ParseNamespaceColumn::p_varattnosyn, ParseNamespaceColumn::p_varcollid, ParseNamespaceColumn::p_varno, ParseNamespaceColumn::p_varnosyn, ParseNamespaceColumn::p_vartype, ParseNamespaceColumn::p_vartypmod, palloc0(), parseCheckAggregates(), parser_errposition(), pstrdup(), WithClause::recursive, TargetEntry::resno, rt_fetch, Query::rtable, SETOP_NONE, Query::setOperations, Query::sortClause, stmt, Query::targetList, transformLimitClause(), transformLockingClause(), transformSetOperationTree(), transformSortClause(), and transformWithClause().

Referenced by transformStmt().

◆ transformSetOperationTree()

static Node * transformSetOperationTree ( ParseState pstate,
SelectStmt stmt,
bool  isTopLevel,
List **  targetlist 
)
static

Definition at line 2094 of file analyze.c.

2096{
2097 bool isLeaf;
2098
2100
2101 /* Guard against stack overflow due to overly complex set-expressions */
2103
2104 /*
2105 * Validity-check both leaf and internal SELECTs for disallowed ops.
2106 */
2107 if (stmt->intoClause)
2108 ereport(ERROR,
2109 (errcode(ERRCODE_SYNTAX_ERROR),
2110 errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
2111 parser_errposition(pstate,
2112 exprLocation((Node *) stmt->intoClause))));
2113
2114 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
2115 if (stmt->lockingClause)
2116 ereport(ERROR,
2117 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2118 /*------
2119 translator: %s is a SQL row locking clause such as FOR UPDATE */
2120 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
2122 linitial(stmt->lockingClause))->strength))));
2123
2124 /*
2125 * If an internal node of a set-op tree has ORDER BY, LIMIT, FOR UPDATE,
2126 * or WITH clauses attached, we need to treat it like a leaf node to
2127 * generate an independent sub-Query tree. Otherwise, it can be
2128 * represented by a SetOperationStmt node underneath the parent Query.
2129 */
2130 if (stmt->op == SETOP_NONE)
2131 {
2132 Assert(stmt->larg == NULL && stmt->rarg == NULL);
2133 isLeaf = true;
2134 }
2135 else
2136 {
2137 Assert(stmt->larg != NULL && stmt->rarg != NULL);
2138 if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
2139 stmt->lockingClause || stmt->withClause)
2140 isLeaf = true;
2141 else
2142 isLeaf = false;
2143 }
2144
2145 if (isLeaf)
2146 {
2147 /* Process leaf SELECT */
2148 Query *selectQuery;
2149 char selectName[32];
2150 ParseNamespaceItem *nsitem;
2151 RangeTblRef *rtr;
2152 ListCell *tl;
2153
2154 /*
2155 * Transform SelectStmt into a Query.
2156 *
2157 * This works the same as SELECT transformation normally would, except
2158 * that we prevent resolving unknown-type outputs as TEXT. This does
2159 * not change the subquery's semantics since if the column type
2160 * matters semantically, it would have been resolved to something else
2161 * anyway. Doing this lets us resolve such outputs using
2162 * select_common_type(), below.
2163 *
2164 * Note: previously transformed sub-queries don't affect the parsing
2165 * of this sub-query, because they are not in the toplevel pstate's
2166 * namespace list.
2167 */
2168 selectQuery = parse_sub_analyze((Node *) stmt, pstate,
2169 NULL, false, false);
2170
2171 /*
2172 * Check for bogus references to Vars on the current query level (but
2173 * upper-level references are okay). Normally this can't happen
2174 * because the namespace will be empty, but it could happen if we are
2175 * inside a rule.
2176 */
2177 if (pstate->p_namespace)
2178 {
2179 if (contain_vars_of_level((Node *) selectQuery, 1))
2180 ereport(ERROR,
2181 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2182 errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
2183 parser_errposition(pstate,
2184 locate_var_of_level((Node *) selectQuery, 1))));
2185 }
2186
2187 /*
2188 * Extract a list of the non-junk TLEs for upper-level processing.
2189 */
2190 if (targetlist)
2191 {
2192 *targetlist = NIL;
2193 foreach(tl, selectQuery->targetList)
2194 {
2195 TargetEntry *tle = (TargetEntry *) lfirst(tl);
2196
2197 if (!tle->resjunk)
2198 *targetlist = lappend(*targetlist, tle);
2199 }
2200 }
2201
2202 /*
2203 * Make the leaf query be a subquery in the top-level rangetable.
2204 */
2205 snprintf(selectName, sizeof(selectName), "*SELECT* %d",
2206 list_length(pstate->p_rtable) + 1);
2207 nsitem = addRangeTableEntryForSubquery(pstate,
2208 selectQuery,
2209 makeAlias(selectName, NIL),
2210 false,
2211 false);
2212
2213 /*
2214 * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
2215 */
2216 rtr = makeNode(RangeTblRef);
2217 rtr->rtindex = nsitem->p_rtindex;
2218 return (Node *) rtr;
2219 }
2220 else
2221 {
2222 /* Process an internal node (set operation node) */
2224 List *ltargetlist;
2225 List *rtargetlist;
2226 ListCell *ltl;
2227 ListCell *rtl;
2228 const char *context;
2229 bool recursive = (pstate->p_parent_cte &&
2230 pstate->p_parent_cte->cterecursive);
2231
2232 context = (stmt->op == SETOP_UNION ? "UNION" :
2233 (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
2234 "EXCEPT"));
2235
2236 op->op = stmt->op;
2237 op->all = stmt->all;
2238
2239 /*
2240 * Recursively transform the left child node.
2241 */
2242 op->larg = transformSetOperationTree(pstate, stmt->larg,
2243 false,
2244 &ltargetlist);
2245
2246 /*
2247 * If we are processing a recursive union query, now is the time to
2248 * examine the non-recursive term's output columns and mark the
2249 * containing CTE as having those result columns. We should do this
2250 * only at the topmost setop of the CTE, of course.
2251 */
2252 if (isTopLevel && recursive)
2253 determineRecursiveColTypes(pstate, op->larg, ltargetlist);
2254
2255 /*
2256 * Recursively transform the right child node.
2257 */
2258 op->rarg = transformSetOperationTree(pstate, stmt->rarg,
2259 false,
2260 &rtargetlist);
2261
2262 /*
2263 * Verify that the two children have the same number of non-junk
2264 * columns, and determine the types of the merged output columns.
2265 */
2266 if (list_length(ltargetlist) != list_length(rtargetlist))
2267 ereport(ERROR,
2268 (errcode(ERRCODE_SYNTAX_ERROR),
2269 errmsg("each %s query must have the same number of columns",
2270 context),
2271 parser_errposition(pstate,
2272 exprLocation((Node *) rtargetlist))));
2273
2274 if (targetlist)
2275 *targetlist = NIL;
2276 op->colTypes = NIL;
2277 op->colTypmods = NIL;
2278 op->colCollations = NIL;
2279 op->groupClauses = NIL;
2280 forboth(ltl, ltargetlist, rtl, rtargetlist)
2281 {
2282 TargetEntry *ltle = (TargetEntry *) lfirst(ltl);
2283 TargetEntry *rtle = (TargetEntry *) lfirst(rtl);
2284 Node *lcolnode = (Node *) ltle->expr;
2285 Node *rcolnode = (Node *) rtle->expr;
2286 Oid lcoltype = exprType(lcolnode);
2287 Oid rcoltype = exprType(rcolnode);
2288 Node *bestexpr;
2289 int bestlocation;
2290 Oid rescoltype;
2291 int32 rescoltypmod;
2292 Oid rescolcoll;
2293
2294 /* select common type, same as CASE et al */
2295 rescoltype = select_common_type(pstate,
2296 list_make2(lcolnode, rcolnode),
2297 context,
2298 &bestexpr);
2299 bestlocation = exprLocation(bestexpr);
2300
2301 /*
2302 * Verify the coercions are actually possible. If not, we'd fail
2303 * later anyway, but we want to fail now while we have sufficient
2304 * context to produce an error cursor position.
2305 *
2306 * For all non-UNKNOWN-type cases, we verify coercibility but we
2307 * don't modify the child's expression, for fear of changing the
2308 * child query's semantics.
2309 *
2310 * If a child expression is an UNKNOWN-type Const or Param, we
2311 * want to replace it with the coerced expression. This can only
2312 * happen when the child is a leaf set-op node. It's safe to
2313 * replace the expression because if the child query's semantics
2314 * depended on the type of this output column, it'd have already
2315 * coerced the UNKNOWN to something else. We want to do this
2316 * because (a) we want to verify that a Const is valid for the
2317 * target type, or resolve the actual type of an UNKNOWN Param,
2318 * and (b) we want to avoid unnecessary discrepancies between the
2319 * output type of the child query and the resolved target type.
2320 * Such a discrepancy would disable optimization in the planner.
2321 *
2322 * If it's some other UNKNOWN-type node, eg a Var, we do nothing
2323 * (knowing that coerce_to_common_type would fail). The planner
2324 * is sometimes able to fold an UNKNOWN Var to a constant before
2325 * it has to coerce the type, so failing now would just break
2326 * cases that might work.
2327 */
2328 if (lcoltype != UNKNOWNOID)
2329 lcolnode = coerce_to_common_type(pstate, lcolnode,
2330 rescoltype, context);
2331 else if (IsA(lcolnode, Const) ||
2332 IsA(lcolnode, Param))
2333 {
2334 lcolnode = coerce_to_common_type(pstate, lcolnode,
2335 rescoltype, context);
2336 ltle->expr = (Expr *) lcolnode;
2337 }
2338
2339 if (rcoltype != UNKNOWNOID)
2340 rcolnode = coerce_to_common_type(pstate, rcolnode,
2341 rescoltype, context);
2342 else if (IsA(rcolnode, Const) ||
2343 IsA(rcolnode, Param))
2344 {
2345 rcolnode = coerce_to_common_type(pstate, rcolnode,
2346 rescoltype, context);
2347 rtle->expr = (Expr *) rcolnode;
2348 }
2349
2350 rescoltypmod = select_common_typmod(pstate,
2351 list_make2(lcolnode, rcolnode),
2352 rescoltype);
2353
2354 /*
2355 * Select common collation. A common collation is required for
2356 * all set operators except UNION ALL; see SQL:2008 7.13 <query
2357 * expression> Syntax Rule 15c. (If we fail to identify a common
2358 * collation for a UNION ALL column, the colCollations element
2359 * will be set to InvalidOid, which may result in a runtime error
2360 * if something at a higher query level wants to use the column's
2361 * collation.)
2362 */
2363 rescolcoll = select_common_collation(pstate,
2364 list_make2(lcolnode, rcolnode),
2365 (op->op == SETOP_UNION && op->all));
2366
2367 /* emit results */
2368 op->colTypes = lappend_oid(op->colTypes, rescoltype);
2369 op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
2370 op->colCollations = lappend_oid(op->colCollations, rescolcoll);
2371
2372 /*
2373 * For all cases except UNION ALL, identify the grouping operators
2374 * (and, if available, sorting operators) that will be used to
2375 * eliminate duplicates.
2376 */
2377 if (op->op != SETOP_UNION || !op->all)
2378 {
2379 ParseCallbackState pcbstate;
2380
2381 setup_parser_errposition_callback(&pcbstate, pstate,
2382 bestlocation);
2383
2384 /*
2385 * If it's a recursive union, we need to require hashing
2386 * support.
2387 */
2388 op->groupClauses = lappend(op->groupClauses,
2389 makeSortGroupClauseForSetOp(rescoltype, recursive));
2390
2392 }
2393
2394 /*
2395 * Construct a dummy tlist entry to return. We use a SetToDefault
2396 * node for the expression, since it carries exactly the fields
2397 * needed, but any other expression node type would do as well.
2398 */
2399 if (targetlist)
2400 {
2401 SetToDefault *rescolnode = makeNode(SetToDefault);
2402 TargetEntry *restle;
2403
2404 rescolnode->typeId = rescoltype;
2405 rescolnode->typeMod = rescoltypmod;
2406 rescolnode->collation = rescolcoll;
2407 rescolnode->location = bestlocation;
2408 restle = makeTargetEntry((Expr *) rescolnode,
2409 0, /* no need to set resno */
2410 NULL,
2411 false);
2412 *targetlist = lappend(*targetlist, restle);
2413 }
2414 }
2415
2416 return (Node *) op;
2417 }
2418}
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
int32 select_common_typmod(ParseState *pstate, List *exprs, Oid common_type)
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Oid select_common_collation(ParseState *pstate, List *exprs, bool none_ok)
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:156
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:140
@ SETOP_INTERSECT
Definition: parsenodes.h:2164
@ SETOP_UNION
Definition: parsenodes.h:2163
SortGroupClause * makeSortGroupClauseForSetOp(Oid rescoltype, bool require_hash)
Definition: analyze.c:2046
static void determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
Definition: analyze.c:2425
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition: analyze.c:222
#define list_make2(x1, x2)
Definition: pg_list.h:214
#define snprintf
Definition: port.h:238
void check_stack_depth(void)
Definition: stack_depth.c:95
SetOperation op
Definition: parsenodes.h:2242
ParseLoc location
Definition: primnodes.h:2109
int locate_var_of_level(Node *node, int levelsup)
Definition: var.c:555

References addRangeTableEntryForSubquery(), SetOperationStmt::all, Assert, cancel_parser_errposition_callback(), check_stack_depth(), coerce_to_common_type(), contain_vars_of_level(), determineRecursiveColTypes(), ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprLocation(), exprType(), forboth, IsA, lappend(), lappend_int(), lappend_oid(), SetOperationStmt::larg, LCS_asString(), lfirst, linitial, list_length(), list_make2, locate_var_of_level(), SetToDefault::location, makeAlias(), makeNode, makeSortGroupClauseForSetOp(), makeTargetEntry(), NIL, SetOperationStmt::op, ParseState::p_namespace, ParseState::p_parent_cte, ParseState::p_rtable, ParseNamespaceItem::p_rtindex, parse_sub_analyze(), parser_errposition(), SetOperationStmt::rarg, RangeTblRef::rtindex, select_common_collation(), select_common_type(), select_common_typmod(), SETOP_INTERSECT, SETOP_NONE, SETOP_UNION, setup_parser_errposition_callback(), snprintf, stmt, Query::targetList, transformSetOperationTree(), and SetToDefault::typeId.

Referenced by transformSetOperationStmt(), and transformSetOperationTree().

◆ transformStmt()

Query * transformStmt ( ParseState pstate,
Node parseTree 
)

Definition at line 396 of file analyze.c.

397{
398 Query *result;
399
400#ifdef DEBUG_NODE_TESTS_ENABLED
401
402 /*
403 * We apply debug_raw_expression_coverage_test testing to basic DML
404 * statements; we can't just run it on everything because
405 * raw_expression_tree_walker() doesn't claim to handle utility
406 * statements.
407 */
408 if (Debug_raw_expression_coverage_test)
409 {
410 switch (nodeTag(parseTree))
411 {
412 case T_SelectStmt:
413 case T_InsertStmt:
414 case T_UpdateStmt:
415 case T_DeleteStmt:
416 case T_MergeStmt:
417 (void) test_raw_expression_coverage(parseTree, NULL);
418 break;
419 default:
420 break;
421 }
422 }
423#endif /* DEBUG_NODE_TESTS_ENABLED */
424
425 /*
426 * Caution: when changing the set of statement types that have non-default
427 * processing here, see also stmt_requires_parse_analysis() and
428 * analyze_requires_snapshot().
429 */
430 switch (nodeTag(parseTree))
431 {
432 /*
433 * Optimizable statements
434 */
435 case T_InsertStmt:
436 result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
437 break;
438
439 case T_DeleteStmt:
440 result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
441 break;
442
443 case T_UpdateStmt:
444 result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
445 break;
446
447 case T_MergeStmt:
448 result = transformMergeStmt(pstate, (MergeStmt *) parseTree);
449 break;
450
451 case T_SelectStmt:
452 {
453 SelectStmt *n = (SelectStmt *) parseTree;
454
455 if (n->valuesLists)
456 result = transformValuesClause(pstate, n);
457 else if (n->op == SETOP_NONE)
458 result = transformSelectStmt(pstate, n);
459 else
460 result = transformSetOperationStmt(pstate, n);
461 }
462 break;
463
464 case T_ReturnStmt:
465 result = transformReturnStmt(pstate, (ReturnStmt *) parseTree);
466 break;
467
468 case T_PLAssignStmt:
469 result = transformPLAssignStmt(pstate,
470 (PLAssignStmt *) parseTree);
471 break;
472
473 /*
474 * Special cases
475 */
476 case T_DeclareCursorStmt:
477 result = transformDeclareCursorStmt(pstate,
478 (DeclareCursorStmt *) parseTree);
479 break;
480
481 case T_ExplainStmt:
482 result = transformExplainStmt(pstate,
483 (ExplainStmt *) parseTree);
484 break;
485
486 case T_CreateTableAsStmt:
487 result = transformCreateTableAsStmt(pstate,
488 (CreateTableAsStmt *) parseTree);
489 break;
490
491 case T_CallStmt:
492 result = transformCallStmt(pstate,
493 (CallStmt *) parseTree);
494 break;
495
496 default:
497
498 /*
499 * other statements don't require any transformation; just return
500 * the original parsetree with a Query node plastered on top.
501 */
502 result = makeNode(Query);
503 result->commandType = CMD_UTILITY;
504 result->utilityStmt = (Node *) parseTree;
505 break;
506 }
507
508 /* Mark as original query until we learn differently */
509 result->querySource = QSRC_ORIGINAL;
510 result->canSetTag = true;
511 setQueryLocationAndLength(pstate, result, parseTree);
512
513 return result;
514}
Query * transformMergeStmt(ParseState *pstate, MergeStmt *stmt)
Definition: parse_merge.c:107
@ QSRC_ORIGINAL
Definition: parsenodes.h:36
static void setQueryLocationAndLength(ParseState *pstate, Query *qry, Node *parseTree)
Definition: analyze.c:259
static Query * transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
Definition: analyze.c:599
static Query * transformReturnStmt(ParseState *pstate, ReturnStmt *stmt)
Definition: analyze.c:2479
static Query * transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt)
Definition: analyze.c:2813
static Query * transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
Definition: analyze.c:3207
static Query * transformCallStmt(ParseState *pstate, CallStmt *stmt)
Definition: analyze.c:3282
static Query * transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1789
static Query * transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
Definition: analyze.c:2510
static Query * transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1427
static Query * transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
Definition: analyze.c:3155
static Query * transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
Definition: analyze.c:3062
static Query * transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
Definition: analyze.c:671
static Query * transformValuesClause(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1570

References CMD_UTILITY, Query::commandType, makeNode, nodeTag, SelectStmt::op, QSRC_ORIGINAL, SETOP_NONE, setQueryLocationAndLength(), transformCallStmt(), transformCreateTableAsStmt(), transformDeclareCursorStmt(), transformDeleteStmt(), transformExplainStmt(), transformInsertStmt(), transformMergeStmt(), transformPLAssignStmt(), transformReturnStmt(), transformSelectStmt(), transformSetOperationStmt(), transformUpdateStmt(), transformValuesClause(), Query::utilityStmt, and SelectStmt::valuesLists.

Referenced by interpret_AS_clause(), parse_sub_analyze(), transformCreateTableAsStmt(), transformDeclareCursorStmt(), transformInsertStmt(), transformJsonArrayQueryConstructor(), transformOptionalSelectInto(), and transformRuleStmt().

◆ transformTopLevelStmt()

Query * transformTopLevelStmt ( ParseState pstate,
RawStmt parseTree 
)

Definition at line 332 of file analyze.c.

333{
334 Query *result;
335
336 /* Store RawStmt's length and location in pstate */
337 pstate->p_stmt_len = parseTree->stmt_len;
338 pstate->p_stmt_location = parseTree->stmt_location;
339
340 /* We're at top level, so allow SELECT INTO */
341 result = transformOptionalSelectInto(pstate, parseTree->stmt);
342
343 return result;
344}
ParseLoc stmt_location
Definition: parsenodes.h:2067
ParseLoc stmt_len
Definition: parsenodes.h:2068

References ParseState::p_stmt_len, ParseState::p_stmt_location, RawStmt::stmt, RawStmt::stmt_len, RawStmt::stmt_location, and transformOptionalSelectInto().

Referenced by inline_function(), parse_analyze_fixedparams(), parse_analyze_varparams(), and parse_analyze_withcb().

◆ transformUpdateStmt()

static Query * transformUpdateStmt ( ParseState pstate,
UpdateStmt stmt 
)
static

Definition at line 2510 of file analyze.c.

2511{
2512 Query *qry = makeNode(Query);
2513 ParseNamespaceItem *nsitem;
2514 Node *qual;
2515
2516 qry->commandType = CMD_UPDATE;
2517 pstate->p_is_insert = false;
2518
2519 /* process the WITH clause independently of all else */
2520 if (stmt->withClause)
2521 {
2522 qry->hasRecursive = stmt->withClause->recursive;
2523 qry->cteList = transformWithClause(pstate, stmt->withClause);
2524 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
2525 }
2526
2527 qry->resultRelation = setTargetTable(pstate, stmt->relation,
2528 stmt->relation->inh,
2529 true,
2530 ACL_UPDATE);
2531 nsitem = pstate->p_target_nsitem;
2532
2533 /* subqueries in FROM cannot access the result relation */
2534 nsitem->p_lateral_only = true;
2535 nsitem->p_lateral_ok = false;
2536
2537 /*
2538 * the FROM clause is non-standard SQL syntax. We used to be able to do
2539 * this with REPLACE in POSTQUEL so we keep the feature.
2540 */
2541 transformFromClause(pstate, stmt->fromClause);
2542
2543 /* remaining clauses can reference the result relation normally */
2544 nsitem->p_lateral_only = false;
2545 nsitem->p_lateral_ok = true;
2546
2547 qual = transformWhereClause(pstate, stmt->whereClause,
2548 EXPR_KIND_WHERE, "WHERE");
2549
2550 transformReturningClause(pstate, qry, stmt->returningClause,
2552
2553 /*
2554 * Now we are done with SELECT-like processing, and can get on with
2555 * transforming the target list to match the UPDATE target columns.
2556 */
2557 qry->targetList = transformUpdateTargetList(pstate, stmt->targetList);
2558
2559 qry->rtable = pstate->p_rtable;
2560 qry->rteperminfos = pstate->p_rteperminfos;
2561 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
2562
2563 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2564 qry->hasSubLinks = pstate->p_hasSubLinks;
2565
2566 assign_query_collations(pstate, qry);
2567
2568 return qry;
2569}
@ CMD_UPDATE
Definition: nodes.h:266

References ACL_UPDATE, assign_query_collations(), CMD_UPDATE, Query::commandType, Query::cteList, EXPR_KIND_RETURNING, EXPR_KIND_WHERE, Query::jointree, makeFromExpr(), makeNode, ParseState::p_hasModifyingCTE, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_is_insert, ParseState::p_joinlist, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseState::p_rtable, ParseState::p_rteperminfos, ParseState::p_target_nsitem, Query::rtable, setTargetTable(), stmt, Query::targetList, transformFromClause(), transformReturningClause(), transformUpdateTargetList(), transformWhereClause(), and transformWithClause().

Referenced by transformStmt().

◆ transformUpdateTargetList()

List * transformUpdateTargetList ( ParseState pstate,
List origTlist 
)

Definition at line 2576 of file analyze.c.

2577{
2578 List *tlist = NIL;
2579 RTEPermissionInfo *target_perminfo;
2580 ListCell *orig_tl;
2581 ListCell *tl;
2582
2583 tlist = transformTargetList(pstate, origTlist,
2585
2586 /* Prepare to assign non-conflicting resnos to resjunk attributes */
2589
2590 /* Prepare non-junk columns for assignment to target table */
2591 target_perminfo = pstate->p_target_nsitem->p_perminfo;
2592 orig_tl = list_head(origTlist);
2593
2594 foreach(tl, tlist)
2595 {
2596 TargetEntry *tle = (TargetEntry *) lfirst(tl);
2597 ResTarget *origTarget;
2598 int attrno;
2599
2600 if (tle->resjunk)
2601 {
2602 /*
2603 * Resjunk nodes need no additional processing, but be sure they
2604 * have resnos that do not match any target columns; else rewriter
2605 * or planner might get confused. They don't need a resname
2606 * either.
2607 */
2608 tle->resno = (AttrNumber) pstate->p_next_resno++;
2609 tle->resname = NULL;
2610 continue;
2611 }
2612 if (orig_tl == NULL)
2613 elog(ERROR, "UPDATE target count mismatch --- internal error");
2614 origTarget = lfirst_node(ResTarget, orig_tl);
2615
2616 attrno = attnameAttNum(pstate->p_target_relation,
2617 origTarget->name, true);
2618 if (attrno == InvalidAttrNumber)
2619 ereport(ERROR,
2620 (errcode(ERRCODE_UNDEFINED_COLUMN),
2621 errmsg("column \"%s\" of relation \"%s\" does not exist",
2622 origTarget->name,
2624 (origTarget->indirection != NIL &&
2625 strcmp(origTarget->name, pstate->p_target_nsitem->p_names->aliasname) == 0) ?
2626 errhint("SET target columns cannot be qualified with the relation name.") : 0,
2627 parser_errposition(pstate, origTarget->location)));
2628
2629 updateTargetListEntry(pstate, tle, origTarget->name,
2630 attrno,
2631 origTarget->indirection,
2632 origTarget->location);
2633
2634 /* Mark the target column as requiring update permissions */
2635 target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols,
2637
2638 orig_tl = lnext(origTlist, orig_tl);
2639 }
2640 if (orig_tl != NULL)
2641 elog(ERROR, "UPDATE target count mismatch --- internal error");
2642
2643 return tlist;
2644}
@ EXPR_KIND_UPDATE_SOURCE
Definition: parse_node.h:56
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
void updateTargetListEntry(ParseState *pstate, TargetEntry *tle, char *colname, int attrno, List *indirection, int location)
Definition: parse_target.c:622
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define RelationGetRelationName(relation)
Definition: rel.h:539
char * aliasname
Definition: primnodes.h:50
Bitmapset * updatedCols
Definition: parsenodes.h:1304

References Alias::aliasname, attnameAttNum(), bms_add_member(), elog, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_UPDATE_SOURCE, FirstLowInvalidHeapAttributeNumber, ResTarget::indirection, InvalidAttrNumber, lfirst, lfirst_node, list_head(), lnext(), ResTarget::location, ResTarget::name, NIL, ParseNamespaceItem::p_names, ParseState::p_next_resno, ParseNamespaceItem::p_perminfo, ParseState::p_target_nsitem, ParseState::p_target_relation, parser_errposition(), RelationGetNumberOfAttributes, RelationGetRelationName, TargetEntry::resno, transformTargetList(), RTEPermissionInfo::updatedCols, and updateTargetListEntry().

Referenced by transformMergeStmt(), transformOnConflictClause(), and transformUpdateStmt().

◆ transformValuesClause()

static Query * transformValuesClause ( ParseState pstate,
SelectStmt stmt 
)
static

Definition at line 1570 of file analyze.c.

1571{
1572 Query *qry = makeNode(Query);
1573 List *exprsLists = NIL;
1574 List *coltypes = NIL;
1575 List *coltypmods = NIL;
1576 List *colcollations = NIL;
1577 List **colexprs = NULL;
1578 int sublist_length = -1;
1579 bool lateral = false;
1580 ParseNamespaceItem *nsitem;
1581 ListCell *lc;
1582 ListCell *lc2;
1583 int i;
1584
1585 qry->commandType = CMD_SELECT;
1586
1587 /* Most SELECT stuff doesn't apply in a VALUES clause */
1588 Assert(stmt->distinctClause == NIL);
1589 Assert(stmt->intoClause == NULL);
1590 Assert(stmt->targetList == NIL);
1591 Assert(stmt->fromClause == NIL);
1592 Assert(stmt->whereClause == NULL);
1593 Assert(stmt->groupClause == NIL);
1594 Assert(stmt->havingClause == NULL);
1595 Assert(stmt->windowClause == NIL);
1596 Assert(stmt->op == SETOP_NONE);
1597
1598 /* process the WITH clause independently of all else */
1599 if (stmt->withClause)
1600 {
1601 qry->hasRecursive = stmt->withClause->recursive;
1602 qry->cteList = transformWithClause(pstate, stmt->withClause);
1603 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1604 }
1605
1606 /*
1607 * For each row of VALUES, transform the raw expressions.
1608 *
1609 * Note that the intermediate representation we build is column-organized
1610 * not row-organized. That simplifies the type and collation processing
1611 * below.
1612 */
1613 foreach(lc, stmt->valuesLists)
1614 {
1615 List *sublist = (List *) lfirst(lc);
1616
1617 /*
1618 * Do basic expression transformation (same as a ROW() expr, but here
1619 * we disallow SetToDefault)
1620 */
1621 sublist = transformExpressionList(pstate, sublist,
1622 EXPR_KIND_VALUES, false);
1623
1624 /*
1625 * All the sublists must be the same length, *after* transformation
1626 * (which might expand '*' into multiple items). The VALUES RTE can't
1627 * handle anything different.
1628 */
1629 if (sublist_length < 0)
1630 {
1631 /* Remember post-transformation length of first sublist */
1632 sublist_length = list_length(sublist);
1633 /* and allocate array for per-column lists */
1634 colexprs = (List **) palloc0(sublist_length * sizeof(List *));
1635 }
1636 else if (sublist_length != list_length(sublist))
1637 {
1638 ereport(ERROR,
1639 (errcode(ERRCODE_SYNTAX_ERROR),
1640 errmsg("VALUES lists must all be the same length"),
1641 parser_errposition(pstate,
1642 exprLocation((Node *) sublist))));
1643 }
1644
1645 /* Build per-column expression lists */
1646 i = 0;
1647 foreach(lc2, sublist)
1648 {
1649 Node *col = (Node *) lfirst(lc2);
1650
1651 colexprs[i] = lappend(colexprs[i], col);
1652 i++;
1653 }
1654
1655 /* Release sub-list's cells to save memory */
1656 list_free(sublist);
1657
1658 /* Prepare an exprsLists element for this row */
1659 exprsLists = lappend(exprsLists, NIL);
1660 }
1661
1662 /*
1663 * Now resolve the common types of the columns, and coerce everything to
1664 * those types. Then identify the common typmod and common collation, if
1665 * any, of each column.
1666 *
1667 * We must do collation processing now because (1) assign_query_collations
1668 * doesn't process rangetable entries, and (2) we need to label the VALUES
1669 * RTE with column collations for use in the outer query. We don't
1670 * consider conflict of implicit collations to be an error here; instead
1671 * the column will just show InvalidOid as its collation, and you'll get a
1672 * failure later if that results in failure to resolve a collation.
1673 *
1674 * Note we modify the per-column expression lists in-place.
1675 */
1676 for (i = 0; i < sublist_length; i++)
1677 {
1678 Oid coltype;
1679 int32 coltypmod;
1680 Oid colcoll;
1681
1682 coltype = select_common_type(pstate, colexprs[i], "VALUES", NULL);
1683
1684 foreach(lc, colexprs[i])
1685 {
1686 Node *col = (Node *) lfirst(lc);
1687
1688 col = coerce_to_common_type(pstate, col, coltype, "VALUES");
1689 lfirst(lc) = col;
1690 }
1691
1692 coltypmod = select_common_typmod(pstate, colexprs[i], coltype);
1693 colcoll = select_common_collation(pstate, colexprs[i], true);
1694
1695 coltypes = lappend_oid(coltypes, coltype);
1696 coltypmods = lappend_int(coltypmods, coltypmod);
1697 colcollations = lappend_oid(colcollations, colcoll);
1698 }
1699
1700 /*
1701 * Finally, rearrange the coerced expressions into row-organized lists.
1702 */
1703 for (i = 0; i < sublist_length; i++)
1704 {
1705 forboth(lc, colexprs[i], lc2, exprsLists)
1706 {
1707 Node *col = (Node *) lfirst(lc);
1708 List *sublist = lfirst(lc2);
1709
1710 sublist = lappend(sublist, col);
1711 lfirst(lc2) = sublist;
1712 }
1713 list_free(colexprs[i]);
1714 }
1715
1716 /*
1717 * Ordinarily there can't be any current-level Vars in the expression
1718 * lists, because the namespace was empty ... but if we're inside CREATE
1719 * RULE, then NEW/OLD references might appear. In that case we have to
1720 * mark the VALUES RTE as LATERAL.
1721 */
1722 if (pstate->p_rtable != NIL &&
1723 contain_vars_of_level((Node *) exprsLists, 0))
1724 lateral = true;
1725
1726 /*
1727 * Generate the VALUES RTE
1728 */
1729 nsitem = addRangeTableEntryForValues(pstate, exprsLists,
1730 coltypes, coltypmods, colcollations,
1731 NULL, lateral, true);
1732 addNSItemToQuery(pstate, nsitem, true, true, true);
1733
1734 /*
1735 * Generate a targetlist as though expanding "*"
1736 */
1737 Assert(pstate->p_next_resno == 1);
1738 qry->targetList = expandNSItemAttrs(pstate, nsitem, 0, true, -1);
1739
1740 /*
1741 * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
1742 * VALUES, so cope.
1743 */
1744 qry->sortClause = transformSortClause(pstate,
1745 stmt->sortClause,
1746 &qry->targetList,
1748 false /* allow SQL92 rules */ );
1749
1750 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1751 EXPR_KIND_OFFSET, "OFFSET",
1752 stmt->limitOption);
1753 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1754 EXPR_KIND_LIMIT, "LIMIT",
1755 stmt->limitOption);
1756 qry->limitOption = stmt->limitOption;
1757
1758 if (stmt->lockingClause)
1759 ereport(ERROR,
1760 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1761 /*------
1762 translator: %s is a SQL row locking clause such as FOR UPDATE */
1763 errmsg("%s cannot be applied to VALUES",
1765 linitial(stmt->lockingClause))->strength))));
1766
1767 qry->rtable = pstate->p_rtable;
1768 qry->rteperminfos = pstate->p_rteperminfos;
1769 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1770
1771 qry->hasSubLinks = pstate->p_hasSubLinks;
1772
1773 assign_query_collations(pstate, qry);
1774
1775 return qry;
1776}
void list_free(List *list)
Definition: list.c:1546
List * expandNSItemAttrs(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, bool require_col_privs, int location)

References addNSItemToQuery(), addRangeTableEntryForValues(), Assert, assign_query_collations(), CMD_SELECT, coerce_to_common_type(), Query::commandType, contain_vars_of_level(), Query::cteList, ereport, errcode(), errmsg(), ERROR, expandNSItemAttrs(), EXPR_KIND_LIMIT, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_VALUES, exprLocation(), forboth, i, Query::jointree, lappend(), lappend_int(), lappend_oid(), LCS_asString(), lfirst, Query::limitCount, Query::limitOffset, Query::limitOption, linitial, list_free(), list_length(), makeFromExpr(), makeNode, NIL, ParseState::p_hasModifyingCTE, ParseState::p_hasSubLinks, ParseState::p_joinlist, ParseState::p_next_resno, ParseState::p_rtable, ParseState::p_rteperminfos, palloc0(), parser_errposition(), Query::rtable, select_common_collation(), select_common_type(), select_common_typmod(), SETOP_NONE, Query::sortClause, stmt, Query::targetList, transformExpressionList(), transformLimitClause(), transformSortClause(), and transformWithClause().

Referenced by transformStmt().

Variable Documentation

◆ post_parse_analyze_hook