PostgreSQL Source Code git master
Loading...
Searching...
No Matches
analyze.h File Reference
#include "nodes/params.h"
#include "nodes/queryjumble.h"
#include "parser/parse_node.h"
Include dependency graph for analyze.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef void(* post_parse_analyze_hook_type) (ParseState *pstate, Query *query, JumbleState *jstate)
 

Functions

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)
 
ListtransformInsertRow (ParseState *pstate, List *exprlist, List *stmtcols, List *icolumns, List *attrnos, bool strip_indirection)
 
ListtransformUpdateTargetList (ParseState *pstate, List *origTlist)
 
void transformReturningClause (ParseState *pstate, Query *qry, ReturningClause *returningClause, ParseExprKind exprKind)
 
QuerytransformTopLevelStmt (ParseState *pstate, RawStmt *parseTree)
 
QuerytransformStmt (ParseState *pstate, Node *parseTree)
 
bool stmt_requires_parse_analysis (RawStmt *parseTree)
 
bool analyze_requires_snapshot (RawStmt *parseTree)
 
bool query_requires_rewrite_plan (Query *query)
 
const charLCS_asString (LockClauseStrength strength)
 
void CheckSelectLocking (Query *qry, LockClauseStrength strength)
 
void applyLockingClause (Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
 
ListBuildOnConflictExcludedTargetlist (Relation targetrel, Index exclRelIndex)
 
SortGroupClausemakeSortGroupClauseForSetOp (Oid rescoltype, bool require_hash)
 

Variables

PGDLLIMPORT post_parse_analyze_hook_type post_parse_analyze_hook
 

Typedef Documentation

◆ post_parse_analyze_hook_type

typedef void(* post_parse_analyze_hook_type) (ParseState *pstate, Query *query, JumbleState *jstate)

Definition at line 22 of file analyze.h.

Function Documentation

◆ analyze_requires_snapshot()

bool analyze_requires_snapshot ( RawStmt parseTree)
extern

Definition at line 503 of file analyze.c.

504{
505 /*
506 * Currently, this should return true in exactly the same cases that
507 * stmt_requires_parse_analysis() does, so we just invoke that function
508 * rather than duplicating it. We keep the two entry points separate for
509 * clarity of callers, since from the callers' standpoint these are
510 * different conditions.
511 *
512 * While there may someday be a statement type for which transformStmt()
513 * does something nontrivial and yet no snapshot is needed for that
514 * processing, it seems likely that making such a choice would be fragile.
515 * If you want to install an exception, document the reasoning for it in a
516 * comment.
517 */
519}
bool stmt_requires_parse_analysis(RawStmt *parseTree)
Definition analyze.c:459
static int fb(int x)

References fb(), and stmt_requires_parse_analysis().

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

◆ applyLockingClause()

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

Definition at line 3649 of file analyze.c.

3652{
3653 RowMarkClause *rc;
3654
3655 Assert(strength != LCS_NONE); /* else caller error */
3656
3657 /* If it's an explicit clause, make sure hasForUpdate gets set */
3658 if (!pushedDown)
3659 qry->hasForUpdate = true;
3660
3661 /* Check for pre-existing entry for same rtindex */
3662 if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
3663 {
3664 /*
3665 * If the same RTE is specified with more than one locking strength,
3666 * use the strongest. (Reasonable, since you can't take both a shared
3667 * and exclusive lock at the same time; it'll end up being exclusive
3668 * anyway.)
3669 *
3670 * Similarly, if the same RTE is specified with more than one lock
3671 * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
3672 * turn wins over waiting for the lock (the default). This is a bit
3673 * more debatable but raising an error doesn't seem helpful. (Consider
3674 * for instance SELECT FOR UPDATE NOWAIT from a view that internally
3675 * contains a plain FOR UPDATE spec.) Having NOWAIT win over SKIP
3676 * LOCKED is reasonable since the former throws an error in case of
3677 * coming across a locked tuple, which may be undesirable in some
3678 * cases but it seems better than silently returning inconsistent
3679 * results.
3680 *
3681 * And of course pushedDown becomes false if any clause is explicit.
3682 */
3683 rc->strength = Max(rc->strength, strength);
3684 rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
3685 rc->pushedDown &= pushedDown;
3686 return;
3687 }
3688
3689 /* Make a new RowMarkClause */
3690 rc = makeNode(RowMarkClause);
3691 rc->rti = rtindex;
3692 rc->strength = strength;
3693 rc->waitPolicy = waitPolicy;
3694 rc->pushedDown = pushedDown;
3695 qry->rowMarks = lappend(qry->rowMarks, rc);
3696}
#define Max(x, y)
Definition c.h:1001
#define Assert(condition)
Definition c.h:873
List * lappend(List *list, void *datum)
Definition list.c:339
@ LCS_NONE
Definition lockoptions.h:23
#define makeNode(_type_)
Definition nodes.h:161
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
List * rowMarks
Definition parsenodes.h:234
LockClauseStrength strength
LockWaitPolicy waitPolicy

References Assert, fb(), 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 
)
extern

Definition at line 1291 of file analyze.c.

1293{
1294 List *result = NIL;
1295 int attno;
1296 Var *var;
1297 TargetEntry *te;
1298
1299 /*
1300 * Note that resnos of the tlist must correspond to attnos of the
1301 * underlying relation, hence we need entries for dropped columns too.
1302 */
1303 for (attno = 0; attno < RelationGetNumberOfAttributes(targetrel); attno++)
1304 {
1305 Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, attno);
1306 char *name;
1307
1308 if (attr->attisdropped)
1309 {
1310 /*
1311 * can't use atttypid here, but it doesn't really matter what type
1312 * the Const claims to be.
1313 */
1314 var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
1315 name = NULL;
1316 }
1317 else
1318 {
1319 var = makeVar(exclRelIndex, attno + 1,
1320 attr->atttypid, attr->atttypmod,
1321 attr->attcollation,
1322 0);
1323 name = pstrdup(NameStr(attr->attname));
1324 }
1325
1326 te = makeTargetEntry((Expr *) var,
1327 attno + 1,
1328 name,
1329 false);
1330
1331 result = lappend(result, te);
1332 }
1333
1334 /*
1335 * Add a whole-row-Var entry to support references to "EXCLUDED.*". Like
1336 * the other entries in the EXCLUDED tlist, its resno must match the Var's
1337 * varattno, else the wrong things happen while resolving references in
1338 * setrefs.c. This is against normal conventions for targetlists, but
1339 * it's okay since we don't use this as a real tlist.
1340 */
1341 var = makeVar(exclRelIndex, InvalidAttrNumber,
1342 targetrel->rd_rel->reltype,
1343 -1, InvalidOid, 0);
1344 te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
1345 result = lappend(result, te);
1346
1347 return result;
1348}
#define InvalidAttrNumber
Definition attnum.h:23
#define NameStr(name)
Definition c.h:765
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:388
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition makefuncs.c:289
char * pstrdup(const char *in)
Definition mcxt.c:1781
FormData_pg_attribute * Form_pg_attribute
#define NIL
Definition pg_list.h:68
#define InvalidOid
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:520
Definition pg_list.h:54
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:160
const char * name

References fb(), InvalidAttrNumber, InvalidOid, lappend(), makeNullConst(), makeTargetEntry(), makeVar(), name, NameStr, NIL, pstrdup(), RelationGetNumberOfAttributes, and TupleDescAttr().

Referenced by rewriteTargetView(), and transformOnConflictClause().

◆ CheckSelectLocking()

void CheckSelectLocking ( Query qry,
LockClauseStrength  strength 
)
extern

Definition at line 3358 of file analyze.c.

3359{
3360 Assert(strength != LCS_NONE); /* else caller error */
3361
3362 if (qry->setOperations)
3363 ereport(ERROR,
3365 /*------
3366 translator: %s is a SQL row locking clause such as FOR UPDATE */
3367 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
3368 LCS_asString(strength))));
3369 if (qry->distinctClause != NIL)
3370 ereport(ERROR,
3372 /*------
3373 translator: %s is a SQL row locking clause such as FOR UPDATE */
3374 errmsg("%s is not allowed with DISTINCT clause",
3375 LCS_asString(strength))));
3376 if (qry->groupClause != NIL || qry->groupingSets != NIL)
3377 ereport(ERROR,
3379 /*------
3380 translator: %s is a SQL row locking clause such as FOR UPDATE */
3381 errmsg("%s is not allowed with GROUP BY clause",
3382 LCS_asString(strength))));
3383 if (qry->havingQual != NULL)
3384 ereport(ERROR,
3386 /*------
3387 translator: %s is a SQL row locking clause such as FOR UPDATE */
3388 errmsg("%s is not allowed with HAVING clause",
3389 LCS_asString(strength))));
3390 if (qry->hasAggs)
3391 ereport(ERROR,
3393 /*------
3394 translator: %s is a SQL row locking clause such as FOR UPDATE */
3395 errmsg("%s is not allowed with aggregate functions",
3396 LCS_asString(strength))));
3397 if (qry->hasWindowFuncs)
3398 ereport(ERROR,
3400 /*------
3401 translator: %s is a SQL row locking clause such as FOR UPDATE */
3402 errmsg("%s is not allowed with window functions",
3403 LCS_asString(strength))));
3404 if (qry->hasTargetSRFs)
3405 ereport(ERROR,
3407 /*------
3408 translator: %s is a SQL row locking clause such as FOR UPDATE */
3409 errmsg("%s is not allowed with set-returning functions in the target list",
3410 LCS_asString(strength))));
3411}
int errcode(int sqlerrcode)
Definition elog.c:864
int errmsg(const char *fmt,...)
Definition elog.c:1081
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
const char * LCS_asString(LockClauseStrength strength)
Definition analyze.c:3333
Node * setOperations
Definition parsenodes.h:236
List * groupClause
Definition parsenodes.h:216
Node * havingQual
Definition parsenodes.h:222
List * groupingSets
Definition parsenodes.h:220
List * distinctClause
Definition parsenodes.h:226

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

Referenced by preprocess_rowmarks(), and transformLockingClause().

◆ LCS_asString()

const char * LCS_asString ( LockClauseStrength  strength)
extern

Definition at line 3333 of file analyze.c.

3334{
3335 switch (strength)
3336 {
3337 case LCS_NONE:
3338 Assert(false);
3339 break;
3340 case LCS_FORKEYSHARE:
3341 return "FOR KEY SHARE";
3342 case LCS_FORSHARE:
3343 return "FOR SHARE";
3344 case LCS_FORNOKEYUPDATE:
3345 return "FOR NO KEY UPDATE";
3346 case LCS_FORUPDATE:
3347 return "FOR UPDATE";
3348 }
3349 return "FOR some"; /* shouldn't happen */
3350}
@ LCS_FORUPDATE
Definition lockoptions.h:28
@ LCS_FORSHARE
Definition lockoptions.h:26
@ LCS_FORKEYSHARE
Definition lockoptions.h:25
@ LCS_FORNOKEYUPDATE
Definition lockoptions.h:27

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 
)
extern

Definition at line 2040 of file analyze.c.

2041{
2043 Oid sortop;
2044 Oid eqop;
2045 bool hashable;
2046
2047 /* determine the eqop and optional sortop */
2049 false, true, false,
2050 &sortop, &eqop, NULL,
2051 &hashable);
2052
2053 /*
2054 * The type cache doesn't believe that record is hashable (see
2055 * cache_record_field_properties()), but if the caller really needs hash
2056 * support, we can assume it does. Worst case, if any components of the
2057 * record don't support hashing, we will fail at execution.
2058 */
2060 hashable = true;
2061
2062 /* we don't have a tlist yet, so can't assign sortgrouprefs */
2063 grpcl->tleSortGroupRef = 0;
2064 grpcl->eqop = eqop;
2065 grpcl->sortop = sortop;
2066 grpcl->reverse_sort = false; /* Sort-op is "less than", or InvalidOid */
2067 grpcl->nulls_first = false; /* OK with or without sortop */
2068 grpcl->hashable = hashable;
2069
2070 return grpcl;
2071}
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:182
unsigned int Oid

References fb(), get_sort_group_operators(), and makeNode.

Referenced by rewriteSearchAndCycle(), and transformSetOperationTree().

◆ parse_analyze_fixedparams()

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

Definition at line 117 of file analyze.c.

120{
122 Query *query;
124
125 Assert(sourceText != NULL); /* required as of 8.4 */
126
127 pstate->p_sourcetext = sourceText;
128
129 if (numParams > 0)
130 setup_parse_fixed_parameters(pstate, paramTypes, numParams);
131
132 pstate->p_queryEnv = queryEnv;
133
134 query = transformTopLevelStmt(pstate, parseTree);
135
136 if (IsQueryIdEnabled())
137 jstate = JumbleQuery(query);
138
140 (*post_parse_analyze_hook) (pstate, query, jstate);
141
142 free_parsestate(pstate);
143
144 pgstat_report_query_id(query->queryId, false);
145
146 return query;
147}
void pgstat_report_query_id(int64 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:68
Query * transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
Definition analyze.c:261
static bool IsQueryIdEnabled(void)
JumbleState * JumbleQuery(Query *query)
QueryEnvironment * p_queryEnv
Definition parse_node.h:218
const char * p_sourcetext
Definition parse_node.h:191

References Assert, fb(), 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 
)
extern

Definition at line 157 of file analyze.c.

160{
162 Query *query;
164
165 Assert(sourceText != NULL); /* required as of 8.4 */
166
167 pstate->p_sourcetext = sourceText;
168
169 setup_parse_variable_parameters(pstate, paramTypes, numParams);
170
171 pstate->p_queryEnv = queryEnv;
172
173 query = transformTopLevelStmt(pstate, parseTree);
174
175 /* make sure all is well with parameter types */
176 check_variable_parameters(pstate, query);
177
178 if (IsQueryIdEnabled())
179 jstate = JumbleQuery(query);
180
182 (*post_parse_analyze_hook) (pstate, query, jstate);
183
184 free_parsestate(pstate);
185
186 pgstat_report_query_id(query->queryId, false);
187
188 return query;
189}
void check_variable_parameters(ParseState *pstate, Query *query)
void setup_parse_variable_parameters(ParseState *pstate, Oid **paramTypes, int *numParams)
Definition parse_param.c:84

References Assert, check_variable_parameters(), fb(), 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 
)
extern

Definition at line 198 of file analyze.c.

202{
204 Query *query;
206
207 Assert(sourceText != NULL); /* required as of 8.4 */
208
209 pstate->p_sourcetext = sourceText;
210 pstate->p_queryEnv = queryEnv;
211 (*parserSetup) (pstate, parserSetupArg);
212
213 query = transformTopLevelStmt(pstate, parseTree);
214
215 if (IsQueryIdEnabled())
216 jstate = JumbleQuery(query);
217
219 (*post_parse_analyze_hook) (pstate, query, jstate);
220
221 free_parsestate(pstate);
222
223 pgstat_report_query_id(query->queryId, false);
224
225 return query;
226}

References Assert, fb(), 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 
)
extern

Definition at line 234 of file analyze.c.

238{
239 ParseState *pstate = make_parsestate(parentParseState);
240 Query *query;
241
242 pstate->p_parent_cte = parentCTE;
245
246 query = transformStmt(pstate, parseTree);
247
248 free_parsestate(pstate);
249
250 return query;
251}
Query * transformStmt(ParseState *pstate, Node *parseTree)
Definition analyze.c:324
bool p_locked_from_parent
Definition parse_node.h:213
bool p_resolve_unknowns
Definition parse_node.h:215
CommonTableExpr * p_parent_cte
Definition parse_node.h:204

References fb(), 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().

◆ query_requires_rewrite_plan()

bool query_requires_rewrite_plan ( Query query)
extern

Definition at line 532 of file analyze.c.

533{
534 bool result;
535
536 if (query->commandType != CMD_UTILITY)
537 {
538 /* All optimizable statements require rewriting/planning */
539 result = true;
540 }
541 else
542 {
543 /* This list should match stmt_requires_parse_analysis() */
544 switch (nodeTag(query->utilityStmt))
545 {
547 case T_ExplainStmt:
549 case T_CallStmt:
550 result = true;
551 break;
552 default:
553 result = false;
554 break;
555 }
556 }
557 return result;
558}
#define nodeTag(nodeptr)
Definition nodes.h:139
@ CMD_UTILITY
Definition nodes.h:280
CmdType commandType
Definition parsenodes.h:121
Node * utilityStmt
Definition parsenodes.h:141

References CMD_UTILITY, Query::commandType, fb(), nodeTag, and Query::utilityStmt.

Referenced by BuildingPlanRequiresSnapshot(), and StmtPlanRequiresRevalidation().

◆ stmt_requires_parse_analysis()

bool stmt_requires_parse_analysis ( RawStmt parseTree)
extern

Definition at line 459 of file analyze.c.

460{
461 bool result;
462
463 switch (nodeTag(parseTree->stmt))
464 {
465 /*
466 * Optimizable statements
467 */
468 case T_InsertStmt:
469 case T_DeleteStmt:
470 case T_UpdateStmt:
471 case T_MergeStmt:
472 case T_SelectStmt:
473 case T_ReturnStmt:
474 case T_PLAssignStmt:
475 result = true;
476 break;
477
478 /*
479 * Special cases
480 */
482 case T_ExplainStmt:
484 case T_CallStmt:
485 result = true;
486 break;
487
488 default:
489 /* all other statements just get wrapped in a CMD_UTILITY Query */
490 result = false;
491 break;
492 }
493
494 return result;
495}

References fb(), and nodeTag.

Referenced by analyze_requires_snapshot(), and StmtPlanRequiresRevalidation().

◆ transformInsertRow()

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

Definition at line 1078 of file analyze.c.

1081{
1082 List *result;
1083 ListCell *lc;
1084 ListCell *icols;
1086
1087 /*
1088 * Check length of expr list. It must not have more expressions than
1089 * there are target columns. We allow fewer, but only if no explicit
1090 * columns list was given (the remaining columns are implicitly
1091 * defaulted). Note we must check this *after* transformation because
1092 * that could expand '*' into multiple items.
1093 */
1095 ereport(ERROR,
1097 errmsg("INSERT has more expressions than target columns"),
1098 parser_errposition(pstate,
1100 list_length(icolumns))))));
1101 if (stmtcols != NIL &&
1103 {
1104 /*
1105 * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
1106 * where the user accidentally created a RowExpr instead of separate
1107 * columns. Add a suitable hint if that seems to be the problem,
1108 * because the main error message is quite misleading for this case.
1109 * (If there's no stmtcols, you'll get something about data type
1110 * mismatch, which is less misleading so we don't worry about giving a
1111 * hint in that case.)
1112 */
1113 ereport(ERROR,
1115 errmsg("INSERT has more target columns than expressions"),
1116 ((list_length(exprlist) == 1 &&
1119 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),
1120 parser_errposition(pstate,
1122 list_length(exprlist))))));
1123 }
1124
1125 /*
1126 * Prepare columns for assignment to target table.
1127 */
1128 result = NIL;
1130 {
1131 Expr *expr = (Expr *) lfirst(lc);
1133 int attno = lfirst_int(attnos);
1134
1135 expr = transformAssignedExpr(pstate, expr,
1137 col->name,
1138 attno,
1139 col->indirection,
1140 col->location);
1141
1143 {
1144 /*
1145 * We need to remove top-level FieldStores and SubscriptingRefs,
1146 * as well as any CoerceToDomain appearing above one of those ---
1147 * but not a CoerceToDomain that isn't above one of those.
1148 */
1149 while (expr)
1150 {
1151 Expr *subexpr = expr;
1152
1153 while (IsA(subexpr, CoerceToDomain))
1154 {
1155 subexpr = ((CoerceToDomain *) subexpr)->arg;
1156 }
1157 if (IsA(subexpr, FieldStore))
1158 {
1159 FieldStore *fstore = (FieldStore *) subexpr;
1160
1161 expr = (Expr *) linitial(fstore->newvals);
1162 }
1163 else if (IsA(subexpr, SubscriptingRef))
1164 {
1166
1167 if (sbsref->refassgnexpr == NULL)
1168 break;
1169
1170 expr = sbsref->refassgnexpr;
1171 }
1172 else
1173 break;
1174 }
1175 }
1176
1177 result = lappend(result, expr);
1178 }
1179
1180 return result;
1181}
int errhint(const char *fmt,...)
Definition elog.c:1331
int exprLocation(const Node *expr)
Definition nodeFuncs.c:1384
#define IsA(nodeptr, _type_)
Definition nodes.h:164
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)
static int count_rowexpr_columns(ParseState *pstate, Node *expr)
Definition analyze.c:1361
#define lfirst(lc)
Definition pg_list.h:172
#define lfirst_node(type, lc)
Definition pg_list.h:176
static int list_length(const List *l)
Definition pg_list.h:152
#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
#define linitial(l)
Definition pg_list.h:178
List * newvals
Definition primnodes.h:1194
Expr * refassgnexpr
Definition primnodes.h:736

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

Referenced by transformInsertStmt(), and transformMergeStmt().

◆ transformReturningClause()

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

Definition at line 2680 of file analyze.c.

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

References addNSItemForReturning(), elog, ereport, errcode(), errmsg(), ERROR, exprLocation(), ReturningClause::exprs, fb(), 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(), transformInsertStmt(), transformMergeStmt(), and transformUpdateStmt().

◆ transformStmt()

Query * transformStmt ( ParseState pstate,
Node parseTree 
)
extern

Definition at line 324 of file analyze.c.

325{
326 Query *result;
327
328#ifdef DEBUG_NODE_TESTS_ENABLED
329
330 /*
331 * We apply debug_raw_expression_coverage_test testing to basic DML
332 * statements; we can't just run it on everything because
333 * raw_expression_tree_walker() doesn't claim to handle utility
334 * statements.
335 */
337 {
338 switch (nodeTag(parseTree))
339 {
340 case T_SelectStmt:
341 case T_InsertStmt:
342 case T_UpdateStmt:
343 case T_DeleteStmt:
344 case T_MergeStmt:
346 break;
347 default:
348 break;
349 }
350 }
351#endif /* DEBUG_NODE_TESTS_ENABLED */
352
353 /*
354 * Caution: when changing the set of statement types that have non-default
355 * processing here, see also stmt_requires_parse_analysis() and
356 * analyze_requires_snapshot().
357 */
358 switch (nodeTag(parseTree))
359 {
360 /*
361 * Optimizable statements
362 */
363 case T_InsertStmt:
364 result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
365 break;
366
367 case T_DeleteStmt:
368 result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
369 break;
370
371 case T_UpdateStmt:
372 result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
373 break;
374
375 case T_MergeStmt:
376 result = transformMergeStmt(pstate, (MergeStmt *) parseTree);
377 break;
378
379 case T_SelectStmt:
380 {
382
383 if (n->valuesLists)
384 result = transformValuesClause(pstate, n);
385 else if (n->op == SETOP_NONE)
386 result = transformSelectStmt(pstate, n, NULL);
387 else
388 result = transformSetOperationStmt(pstate, n);
389 }
390 break;
391
392 case T_ReturnStmt:
393 result = transformReturnStmt(pstate, (ReturnStmt *) parseTree);
394 break;
395
396 case T_PLAssignStmt:
397 result = transformPLAssignStmt(pstate,
399 break;
400
401 /*
402 * Special cases
403 */
405 result = transformDeclareCursorStmt(pstate,
407 break;
408
409 case T_ExplainStmt:
410 result = transformExplainStmt(pstate,
412 break;
413
415 result = transformCreateTableAsStmt(pstate,
417 break;
418
419 case T_CallStmt:
420 result = transformCallStmt(pstate,
421 (CallStmt *) parseTree);
422 break;
423
424 default:
425
426 /*
427 * other statements don't require any transformation; just return
428 * the original parsetree with a Query node plastered on top.
429 */
430 result = makeNode(Query);
431 result->commandType = CMD_UTILITY;
432 result->utilityStmt = parseTree;
433 break;
434 }
435
436 /* Mark as original query until we learn differently */
437 result->querySource = QSRC_ORIGINAL;
438 result->canSetTag = true;
439
440 return result;
441}
Query * transformMergeStmt(ParseState *pstate, MergeStmt *stmt)
@ SETOP_NONE
@ QSRC_ORIGINAL
Definition parsenodes.h:36
static Query * transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
Definition analyze.c:565
static Query * transformReturnStmt(ParseState *pstate, ReturnStmt *stmt)
Definition analyze.c:2470
static Query * transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt)
Definition analyze.c:2802
static Query * transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
Definition analyze.c:3129
static Query * transformCallStmt(ParseState *pstate, CallStmt *stmt)
Definition analyze.c:3208
static Query * transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
Definition analyze.c:1783
static Query * transformSelectStmt(ParseState *pstate, SelectStmt *stmt, SelectStmtPassthrough *passthru)
Definition analyze.c:1410
static Query * transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
Definition analyze.c:2501
static Query * transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
Definition analyze.c:3077
static Query * transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
Definition analyze.c:2984
static Query * transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
Definition analyze.c:637
static Query * transformValuesClause(ParseState *pstate, SelectStmt *stmt)
Definition analyze.c:1564
List * valuesLists
SetOperation op

References CMD_UTILITY, Query::commandType, fb(), makeNode, nodeTag, SelectStmt::op, QSRC_ORIGINAL, SETOP_NONE, 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 
)
extern

Definition at line 261 of file analyze.c.

262{
263 Query *result;
264
265 /* We're at top level, so allow SELECT INTO */
266 result = transformOptionalSelectInto(pstate, parseTree->stmt);
267
268 result->stmt_location = parseTree->stmt_location;
269 result->stmt_len = parseTree->stmt_len;
270
271 return result;
272}
static Query * transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
Definition analyze.c:285
ParseLoc stmt_location
Definition parsenodes.h:255

References fb(), Query::stmt_location, and transformOptionalSelectInto().

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

◆ transformUpdateTargetList()

List * transformUpdateTargetList ( ParseState pstate,
List origTlist 
)
extern

Definition at line 2566 of file analyze.c.

2567{
2568 List *tlist = NIL;
2571 ListCell *tl;
2572
2573 tlist = transformTargetList(pstate, origTlist,
2575
2576 /* Prepare to assign non-conflicting resnos to resjunk attributes */
2579
2580 /* Prepare non-junk columns for assignment to target table */
2583
2584 foreach(tl, tlist)
2585 {
2588 int attrno;
2589
2590 if (tle->resjunk)
2591 {
2592 /*
2593 * Resjunk nodes need no additional processing, but be sure they
2594 * have resnos that do not match any target columns; else rewriter
2595 * or planner might get confused. They don't need a resname
2596 * either.
2597 */
2598 tle->resno = (AttrNumber) pstate->p_next_resno++;
2599 tle->resname = NULL;
2600 continue;
2601 }
2602 if (orig_tl == NULL)
2603 elog(ERROR, "UPDATE target count mismatch --- internal error");
2605
2607 origTarget->name, true);
2609 ereport(ERROR,
2611 errmsg("column \"%s\" of relation \"%s\" does not exist",
2612 origTarget->name,
2614 (origTarget->indirection != NIL &&
2615 strcmp(origTarget->name, pstate->p_target_nsitem->p_names->aliasname) == 0) ?
2616 errhint("SET target columns cannot be qualified with the relation name.") : 0,
2617 parser_errposition(pstate, origTarget->location)));
2618
2619 updateTargetListEntry(pstate, tle, origTarget->name,
2620 attrno,
2621 origTarget->indirection,
2622 origTarget->location);
2623
2624 /* Mark the target column as requiring update permissions */
2625 target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols,
2627
2629 }
2630 if (orig_tl != NULL)
2631 elog(ERROR, "UPDATE target count mismatch --- internal error");
2632
2633 return tlist;
2634}
int16 AttrNumber
Definition attnum.h:21
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:814
@ 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)
static ListCell * list_head(const List *l)
Definition pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:343
#define RelationGetRelationName(relation)
Definition rel.h:548
char * aliasname
Definition primnodes.h:52
RTEPermissionInfo * p_perminfo
Definition parse_node.h:292
ParseNamespaceItem * p_target_nsitem
Definition parse_node.h:206
Relation p_target_relation
Definition parse_node.h:205
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27

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

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

Variable Documentation

◆ post_parse_analyze_hook