PostgreSQL Source Code git master
Typedefs | Functions | Variables
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  analyze_requires_snapshot (RawStmt *parseTree)
 
 
const char *  LCS_asString (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

 

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 ( RawStmtparseTree )

Definition at line 502 of file analyze.c.

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

References stmt_requires_parse_analysis().

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

applyLockingClause()

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

Definition at line 3636 of file analyze.c.

3639{
3640 RowMarkClause *rc;
3641
3642 Assert(strength != LCS_NONE); /* else caller error */
3643
3644 /* If it's an explicit clause, make sure hasForUpdate gets set */
3645 if (!pushedDown)
3646 qry->hasForUpdate = true;
3647
3648 /* Check for pre-existing entry for same rtindex */
3649 if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
3650 {
3651 /*
3652 * If the same RTE is specified with more than one locking strength,
3653 * use the strongest. (Reasonable, since you can't take both a shared
3654 * and exclusive lock at the same time; it'll end up being exclusive
3655 * anyway.)
3656 *
3657 * Similarly, if the same RTE is specified with more than one lock
3658 * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
3659 * turn wins over waiting for the lock (the default). This is a bit
3660 * more debatable but raising an error doesn't seem helpful. (Consider
3661 * for instance SELECT FOR UPDATE NOWAIT from a view that internally
3662 * contains a plain FOR UPDATE spec.) Having NOWAIT win over SKIP
3663 * LOCKED is reasonable since the former throws an error in case of
3664 * coming across a locked tuple, which may be undesirable in some
3665 * cases but it seems better than silently returning inconsistent
3666 * results.
3667 *
3668 * And of course pushedDown becomes false if any clause is explicit.
3669 */
3670 rc->strength = Max(rc->strength, strength);
3671 rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
3672 rc->pushedDown &= pushedDown;
3673 return;
3674 }
3675
3676 /* Make a new RowMarkClause */
3677 rc = makeNode(RowMarkClause);
3678 rc->rti = rtindex;
3679 rc->strength = strength;
3680 rc->waitPolicy = waitPolicy;
3681 rc->pushedDown = pushedDown;
3682 qry->rowMarks = lappend(qry->rowMarks, rc);
3683}
#define Max(x, y)
Definition: c.h:997
Assert(PointerIsAligned(start, uint64))
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
bool pushedDown
Definition: parsenodes.h:1613
LockClauseStrength strength
Definition: parsenodes.h:1611
LockWaitPolicy waitPolicy
Definition: parsenodes.h:1612

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 1280 of file analyze.c.

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

Definition at line 3345 of file analyze.c.

3346{
3347 Assert(strength != LCS_NONE); /* else caller error */
3348
3349 if (qry->setOperations)
3350 ereport(ERROR,
3351 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3352 /*------
3353 translator: %s is a SQL row locking clause such as FOR UPDATE */
3354 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
3355 LCS_asString(strength))));
3356 if (qry->distinctClause != NIL)
3357 ereport(ERROR,
3358 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3359 /*------
3360 translator: %s is a SQL row locking clause such as FOR UPDATE */
3361 errmsg("%s is not allowed with DISTINCT clause",
3362 LCS_asString(strength))));
3363 if (qry->groupClause != NIL || qry->groupingSets != NIL)
3364 ereport(ERROR,
3365 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3366 /*------
3367 translator: %s is a SQL row locking clause such as FOR UPDATE */
3368 errmsg("%s is not allowed with GROUP BY clause",
3369 LCS_asString(strength))));
3370 if (qry->havingQual != NULL)
3371 ereport(ERROR,
3372 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3373 /*------
3374 translator: %s is a SQL row locking clause such as FOR UPDATE */
3375 errmsg("%s is not allowed with HAVING clause",
3376 LCS_asString(strength))));
3377 if (qry->hasAggs)
3378 ereport(ERROR,
3379 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3380 /*------
3381 translator: %s is a SQL row locking clause such as FOR UPDATE */
3382 errmsg("%s is not allowed with aggregate functions",
3383 LCS_asString(strength))));
3384 if (qry->hasWindowFuncs)
3385 ereport(ERROR,
3386 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3387 /*------
3388 translator: %s is a SQL row locking clause such as FOR UPDATE */
3389 errmsg("%s is not allowed with window functions",
3390 LCS_asString(strength))));
3391 if (qry->hasTargetSRFs)
3392 ereport(ERROR,
3393 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3394 /*------
3395 translator: %s is a SQL row locking clause such as FOR UPDATE */
3396 errmsg("%s is not allowed with set-returning functions in the target list",
3397 LCS_asString(strength))));
3398}
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
const char * LCS_asString(LockClauseStrength strength)
Definition: analyze.c:3320
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, 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 )

Definition at line 3320 of file analyze.c.

3321{
3322 switch (strength)
3323 {
3324 case LCS_NONE:
3325 Assert(false);
3326 break;
3327 case LCS_FORKEYSHARE:
3328 return "FOR KEY SHARE";
3329 case LCS_FORSHARE:
3330 return "FOR SHARE";
3331 case LCS_FORNOKEYUPDATE:
3332 return "FOR NO KEY UPDATE";
3333 case LCS_FORUPDATE:
3334 return "FOR UPDATE";
3335 }
3336 return "FOR some"; /* shouldn't happen */
3337}
@ 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 2029 of file analyze.c.

2030{
2032 Oid sortop;
2033 Oid eqop;
2034 bool hashable;
2035
2036 /* determine the eqop and optional sortop */
2037 get_sort_group_operators(rescoltype,
2038 false, true, false,
2039 &sortop, &eqop, NULL,
2040 &hashable);
2041
2042 /*
2043 * The type cache doesn't believe that record is hashable (see
2044 * cache_record_field_properties()), but if the caller really needs hash
2045 * support, we can assume it does. Worst case, if any components of the
2046 * record don't support hashing, we will fail at execution.
2047 */
2048 if (require_hash && (rescoltype == RECORDOID || rescoltype == RECORDARRAYOID))
2049 hashable = true;
2050
2051 /* we don't have a tlist yet, so can't assign sortgrouprefs */
2052 grpcl->tleSortGroupRef = 0;
2053 grpcl->eqop = eqop;
2054 grpcl->sortop = sortop;
2055 grpcl->reverse_sort = false; /* Sort-op is "less than", or InvalidOid */
2056 grpcl->nulls_first = false; /* OK with or without sortop */
2057 grpcl->hashable = hashable;
2058
2059 return grpcl;
2060}
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:181
unsigned int Oid
Definition: postgres_ext.h:32
bool nulls_first
Definition: parsenodes.h:1473
bool reverse_sort
Definition: parsenodes.h:1472
Index tleSortGroupRef
Definition: parsenodes.h:1469

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 ( RawStmtparseTree,
const char *  sourceText,
const OidparamTypes,
int  numParams,
QueryEnvironmentqueryEnv 
)

Definition at line 116 of file analyze.c.

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

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 ( RawStmtparseTree,
const char *  sourceText,
Oid **  paramTypes,
int *  numParams,
QueryEnvironmentqueryEnv 
)

Definition at line 156 of file analyze.c.

159{
160 ParseState *pstate = make_parsestate(NULL);
161 Query *query;
162 JumbleState *jstate = NULL;
163
164 Assert(sourceText != NULL); /* required as of 8.4 */
165
166 pstate->p_sourcetext = sourceText;
167
168 setup_parse_variable_parameters(pstate, paramTypes, numParams);
169
170 pstate->p_queryEnv = queryEnv;
171
172 query = transformTopLevelStmt(pstate, parseTree);
173
174 /* make sure all is well with parameter types */
175 check_variable_parameters(pstate, query);
176
177 if (IsQueryIdEnabled())
178 jstate = JumbleQuery(query);
179
181 (*post_parse_analyze_hook) (pstate, query, jstate);
182
183 free_parsestate(pstate);
184
185 pgstat_report_query_id(query->queryId, false);
186
187 return query;
188}
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 ( RawStmtparseTree,
const char *  sourceText,
ParserSetupHook  parserSetup,
void *  parserSetupArg,
QueryEnvironmentqueryEnv 
)

Definition at line 197 of file analyze.c.

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

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 ( NodeparseTree,
ParseStateparentParseState,
CommonTableExprparentCTE,
bool  locked_from_parent,
bool  resolve_unknowns 
)

Definition at line 233 of file analyze.c.

237{
238 ParseState *pstate = make_parsestate(parentParseState);
239 Query *query;
240
241 pstate->p_parent_cte = parentCTE;
242 pstate->p_locked_from_parent = locked_from_parent;
243 pstate->p_resolve_unknowns = resolve_unknowns;
244
245 query = transformStmt(pstate, parseTree);
246
247 free_parsestate(pstate);
248
249 return query;
250}
Query * transformStmt(ParseState *pstate, Node *parseTree)
Definition: analyze.c:323
bool p_locked_from_parent
Definition: parse_node.h:218
bool p_resolve_unknowns
Definition: parse_node.h:220
CommonTableExpr * p_parent_cte
Definition: parse_node.h:208

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().

query_requires_rewrite_plan()

bool query_requires_rewrite_plan ( Queryquery )

Definition at line 531 of file analyze.c.

532{
533 bool result;
534
535 if (query->commandType != CMD_UTILITY)
536 {
537 /* All optimizable statements require rewriting/planning */
538 result = true;
539 }
540 else
541 {
542 /* This list should match stmt_requires_parse_analysis() */
543 switch (nodeTag(query->utilityStmt))
544 {
545 case T_DeclareCursorStmt:
546 case T_ExplainStmt:
547 case T_CreateTableAsStmt:
548 case T_CallStmt:
549 result = true;
550 break;
551 default:
552 result = false;
553 break;
554 }
555 }
556 return result;
557}
#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, nodeTag, and Query::utilityStmt.

Referenced by BuildingPlanRequiresSnapshot(), and StmtPlanRequiresRevalidation().

stmt_requires_parse_analysis()

bool stmt_requires_parse_analysis ( RawStmtparseTree )

Definition at line 458 of file analyze.c.

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

References nodeTag, and RawStmt::stmt.

Referenced by analyze_requires_snapshot(), and StmtPlanRequiresRevalidation().

transformInsertRow()

List * transformInsertRow ( ParseStatepstate,
Listexprlist,
Liststmtcols,
Listicolumns,
Listattrnos,
bool  strip_indirection 
)

Definition at line 1063 of file analyze.c.

1066{
1067 List *result;
1068 ListCell *lc;
1069 ListCell *icols;
1070 ListCell *attnos;
1071
1072 /*
1073 * Check length of expr list. It must not have more expressions than
1074 * there are target columns. We allow fewer, but only if no explicit
1075 * columns list was given (the remaining columns are implicitly
1076 * defaulted). Note we must check this *after* transformation because
1077 * that could expand '*' into multiple items.
1078 */
1079 if (list_length(exprlist) > list_length(icolumns))
1080 ereport(ERROR,
1081 (errcode(ERRCODE_SYNTAX_ERROR),
1082 errmsg("INSERT has more expressions than target columns"),
1083 parser_errposition(pstate,
1084 exprLocation(list_nth(exprlist,
1085 list_length(icolumns))))));
1086 if (stmtcols != NIL &&
1087 list_length(exprlist) < list_length(icolumns))
1088 {
1089 /*
1090 * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
1091 * where the user accidentally created a RowExpr instead of separate
1092 * columns. Add a suitable hint if that seems to be the problem,
1093 * because the main error message is quite misleading for this case.
1094 * (If there's no stmtcols, you'll get something about data type
1095 * mismatch, which is less misleading so we don't worry about giving a
1096 * hint in that case.)
1097 */
1098 ereport(ERROR,
1099 (errcode(ERRCODE_SYNTAX_ERROR),
1100 errmsg("INSERT has more target columns than expressions"),
1101 ((list_length(exprlist) == 1 &&
1102 count_rowexpr_columns(pstate, linitial(exprlist)) ==
1103 list_length(icolumns)) ?
1104 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),
1105 parser_errposition(pstate,
1106 exprLocation(list_nth(icolumns,
1107 list_length(exprlist))))));
1108 }
1109
1110 /*
1111 * Prepare columns for assignment to target table.
1112 */
1113 result = NIL;
1114 forthree(lc, exprlist, icols, icolumns, attnos, attrnos)
1115 {
1116 Expr *expr = (Expr *) lfirst(lc);
1117 ResTarget *col = lfirst_node(ResTarget, icols);
1118 int attno = lfirst_int(attnos);
1119
1120 expr = transformAssignedExpr(pstate, expr,
1122 col->name,
1123 attno,
1124 col->indirection,
1125 col->location);
1126
1127 if (strip_indirection)
1128 {
1129 /*
1130 * We need to remove top-level FieldStores and SubscriptingRefs,
1131 * as well as any CoerceToDomain appearing above one of those ---
1132 * but not a CoerceToDomain that isn't above one of those.
1133 */
1134 while (expr)
1135 {
1136 Expr *subexpr = expr;
1137
1138 while (IsA(subexpr, CoerceToDomain))
1139 {
1140 subexpr = ((CoerceToDomain *) subexpr)->arg;
1141 }
1142 if (IsA(subexpr, FieldStore))
1143 {
1144 FieldStore *fstore = (FieldStore *) subexpr;
1145
1146 expr = (Expr *) linitial(fstore->newvals);
1147 }
1148 else if (IsA(subexpr, SubscriptingRef))
1149 {
1150 SubscriptingRef *sbsref = (SubscriptingRef *) subexpr;
1151
1152 if (sbsref->refassgnexpr == NULL)
1153 break;
1154
1155 expr = sbsref->refassgnexpr;
1156 }
1157 else
1158 break;
1159 }
1160 }
1161
1162 result = lappend(result, expr);
1163 }
1164
1165 return result;
1166}
int errhint(const char *fmt,...)
Definition: elog.c:1321
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1388
#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)
Definition: parse_target.c:454
static int count_rowexpr_columns(ParseState *pstate, Node *expr)
Definition: analyze.c:1350
#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:1192
ParseLoc location
Definition: parsenodes.h:548
List * indirection
Definition: parsenodes.h:546
char * name
Definition: parsenodes.h:545
Expr * refassgnexpr
Definition: primnodes.h:735
Definition: pg_list.h:46

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().

transformReturningClause()

void transformReturningClause ( ParseStatepstate,
Queryqry,
ReturningClausereturningClause,
ParseExprKind  exprKind 
)

Definition at line 2671 of file analyze.c.

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

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().

transformStmt()

Query * transformStmt ( ParseStatepstate,
NodeparseTree 
)

Definition at line 323 of file analyze.c.

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

References CMD_UTILITY, Query::commandType, 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 ( ParseStatepstate,
RawStmtparseTree 
)

Definition at line 260 of file analyze.c.

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

References RawStmt::stmt, RawStmt::stmt_len, Query::stmt_location, RawStmt::stmt_location, and transformOptionalSelectInto().

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

transformUpdateTargetList()

List * transformUpdateTargetList ( ParseStatepstate,
ListorigTlist 
)

Definition at line 2556 of file analyze.c.

2557{
2558 List *tlist = NIL;
2559 RTEPermissionInfo *target_perminfo;
2560 ListCell *orig_tl;
2561 ListCell *tl;
2562
2563 tlist = transformTargetList(pstate, origTlist,
2565
2566 /* Prepare to assign non-conflicting resnos to resjunk attributes */
2569
2570 /* Prepare non-junk columns for assignment to target table */
2571 target_perminfo = pstate->p_target_nsitem->p_perminfo;
2572 orig_tl = list_head(origTlist);
2573
2574 foreach(tl, tlist)
2575 {
2576 TargetEntry *tle = (TargetEntry *) lfirst(tl);
2577 ResTarget *origTarget;
2578 int attrno;
2579
2580 if (tle->resjunk)
2581 {
2582 /*
2583 * Resjunk nodes need no additional processing, but be sure they
2584 * have resnos that do not match any target columns; else rewriter
2585 * or planner might get confused. They don't need a resname
2586 * either.
2587 */
2588 tle->resno = (AttrNumber) pstate->p_next_resno++;
2589 tle->resname = NULL;
2590 continue;
2591 }
2592 if (orig_tl == NULL)
2593 elog(ERROR, "UPDATE target count mismatch --- internal error");
2594 origTarget = lfirst_node(ResTarget, orig_tl);
2595
2596 attrno = attnameAttNum(pstate->p_target_relation,
2597 origTarget->name, true);
2598 if (attrno == InvalidAttrNumber)
2599 ereport(ERROR,
2600 (errcode(ERRCODE_UNDEFINED_COLUMN),
2601 errmsg("column \"%s\" of relation \"%s\" does not exist",
2602 origTarget->name,
2604 (origTarget->indirection != NIL &&
2605 strcmp(origTarget->name, pstate->p_target_nsitem->p_names->aliasname) == 0) ?
2606 errhint("SET target columns cannot be qualified with the relation name.") : 0,
2607 parser_errposition(pstate, origTarget->location)));
2608
2609 updateTargetListEntry(pstate, tle, origTarget->name,
2610 attrno,
2611 origTarget->indirection,
2612 origTarget->location);
2613
2614 /* Mark the target column as requiring update permissions */
2615 target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols,
2617
2618 orig_tl = lnext(origTlist, orig_tl);
2619 }
2620 if (orig_tl != NULL)
2621 elog(ERROR, "UPDATE target count mismatch --- internal error");
2622
2623 return tlist;
2624}
int16 AttrNumber
Definition: attnum.h:21
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
@ 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:621
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:51
Alias * p_names
Definition: parse_node.h:294
RTEPermissionInfo * p_perminfo
Definition: parse_node.h:297
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:210
Relation p_target_relation
Definition: parse_node.h:209
Bitmapset * updatedCols
Definition: parsenodes.h:1326
AttrNumber resno
Definition: primnodes.h:2240
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

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().

Variable Documentation

post_parse_analyze_hook

PGDLLIMPORT post_parse_analyze_hook_type post_parse_analyze_hook
extern

Definition at line 67 of file analyze.c.

Referenced by _PG_init(), ExecCreateTableAs(), ExplainOneUtility(), ExplainQuery(), parse_analyze_fixedparams(), parse_analyze_varparams(), parse_analyze_withcb(), and PerformCursorOpen().

AltStyle によって変換されたページ (->オリジナル) /