PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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)
 
ListtransformReturningList (ParseState *pstate, List *returningList, 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)
 
const char * LCS_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)

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

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

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

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

3331 {
3332  Assert(strength != LCS_NONE); /* else caller error */
3333 
3334  if (qry->setOperations)
3335  ereport(ERROR,
3336  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3337  /*------
3338  translator: %s is a SQL row locking clause such as FOR UPDATE */
3339  errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
3340  LCS_asString(strength))));
3341  if (qry->distinctClause != NIL)
3342  ereport(ERROR,
3343  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3344  /*------
3345  translator: %s is a SQL row locking clause such as FOR UPDATE */
3346  errmsg("%s is not allowed with DISTINCT clause",
3347  LCS_asString(strength))));
3348  if (qry->groupClause != NIL || qry->groupingSets != NIL)
3349  ereport(ERROR,
3350  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3351  /*------
3352  translator: %s is a SQL row locking clause such as FOR UPDATE */
3353  errmsg("%s is not allowed with GROUP BY clause",
3354  LCS_asString(strength))));
3355  if (qry->havingQual != NULL)
3356  ereport(ERROR,
3357  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3358  /*------
3359  translator: %s is a SQL row locking clause such as FOR UPDATE */
3360  errmsg("%s is not allowed with HAVING clause",
3361  LCS_asString(strength))));
3362  if (qry->hasAggs)
3363  ereport(ERROR,
3364  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3365  /*------
3366  translator: %s is a SQL row locking clause such as FOR UPDATE */
3367  errmsg("%s is not allowed with aggregate functions",
3368  LCS_asString(strength))));
3369  if (qry->hasWindowFuncs)
3370  ereport(ERROR,
3371  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3372  /*------
3373  translator: %s is a SQL row locking clause such as FOR UPDATE */
3374  errmsg("%s is not allowed with window functions",
3375  LCS_asString(strength))));
3376  if (qry->hasTargetSRFs)
3377  ereport(ERROR,
3378  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3379  /*------
3380  translator: %s is a SQL row locking clause such as FOR UPDATE */
3381  errmsg("%s is not allowed with set-returning functions in the target list",
3382  LCS_asString(strength))));
3383 }
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:3305
Node * setOperations
Definition: parsenodes.h:221
List * groupClause
Definition: parsenodes.h:202
Node * havingQual
Definition: parsenodes.h:207
List * groupingSets
Definition: parsenodes.h:205
List * distinctClause
Definition: parsenodes.h:211

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

3306 {
3307  switch (strength)
3308  {
3309  case LCS_NONE:
3310  Assert(false);
3311  break;
3312  case LCS_FORKEYSHARE:
3313  return "FOR KEY SHARE";
3314  case LCS_FORSHARE:
3315  return "FOR SHARE";
3316  case LCS_FORNOKEYUPDATE:
3317  return "FOR NO KEY UPDATE";
3318  case LCS_FORUPDATE:
3319  return "FOR UPDATE";
3320  }
3321  return "FOR some"; /* shouldn't happen */
3322 }
@ 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 2047 of file analyze.c.

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

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
Query * transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
Definition: analyze.c:332
post_parse_analyze_hook_type post_parse_analyze_hook
Definition: analyze.c:59
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
CommonTableExpr * p_parent_cte
Definition: parse_node.h:224

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

◆ 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 }
#define nodeTag(nodeptr)
Definition: nodes.h:133
Node * stmt
Definition: parsenodes.h:2022

References nodeTag, and RawStmt::stmt.

Referenced by analyze_requires_snapshot().

◆ transformInsertRow()

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

Definition at line 1099 of file analyze.c.

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

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

◆ transformReturningList()

List* transformReturningList ( ParseState pstate,
List returningList,
ParseExprKind  exprKind 
)

Definition at line 2652 of file analyze.c.

2654 {
2655  List *rlist;
2656  int save_next_resno;
2657 
2658  if (returningList == NIL)
2659  return NIL; /* nothing to do */
2660 
2661  /*
2662  * We need to assign resnos starting at one in the RETURNING list. Save
2663  * and restore the main tlist's value of p_next_resno, just in case
2664  * someone looks at it later (probably won't happen).
2665  */
2666  save_next_resno = pstate->p_next_resno;
2667  pstate->p_next_resno = 1;
2668 
2669  /* transform RETURNING identically to a SELECT targetlist */
2670  rlist = transformTargetList(pstate, returningList, exprKind);
2671 
2672  /*
2673  * Complain if the nonempty tlist expanded to nothing (which is possible
2674  * if it contains only a star-expansion of a zero-column table). If we
2675  * allow this, the parsed Query will look like it didn't have RETURNING,
2676  * with results that would probably surprise the user.
2677  */
2678  if (rlist == NIL)
2679  ereport(ERROR,
2680  (errcode(ERRCODE_SYNTAX_ERROR),
2681  errmsg("RETURNING must have at least one column"),
2682  parser_errposition(pstate,
2683  exprLocation(linitial(returningList)))));
2684 
2685  /* mark column origins */
2686  markTargetListOrigins(pstate, rlist);
2687 
2688  /* resolve any still-unresolved output columns as being type text */
2689  if (pstate->p_resolve_unknowns)
2690  resolveTargetListUnknowns(pstate, rlist);
2691 
2692  /* restore state */
2693  pstate->p_next_resno = save_next_resno;
2694 
2695  return rlist;
2696 }
List * transformTargetList(ParseState *pstate, List *targetlist, ParseExprKind exprKind)
Definition: parse_target.c:121
void resolveTargetListUnknowns(ParseState *pstate, List *targetlist)
Definition: parse_target.c:288
void markTargetListOrigins(ParseState *pstate, List *targetlist)
Definition: parse_target.c:318
int p_next_resno
Definition: parse_node.h:231

References ereport, errcode(), errmsg(), ERROR, exprLocation(), linitial, markTargetListOrigins(), NIL, ParseState::p_next_resno, ParseState::p_resolve_unknowns, parser_errposition(), resolveTargetListUnknowns(), and transformTargetList().

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

◆ 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 }
@ CMD_UTILITY
Definition: nodes.h:270
Query * transformMergeStmt(ParseState *pstate, MergeStmt *stmt)
Definition: parse_merge.c:107
@ SETOP_NONE
Definition: parsenodes.h:2118
@ 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:2480
static Query * transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt)
Definition: analyze.c:2711
static Query * transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
Definition: analyze.c:3105
static Query * transformCallStmt(ParseState *pstate, CallStmt *stmt)
Definition: analyze.c:3180
static Query * transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1790
static Query * transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
Definition: analyze.c:2511
static Query * transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1428
static Query * transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
Definition: analyze.c:3053
static Query * transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
Definition: analyze.c:2960
static Query * transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
Definition: analyze.c:671
static Query * transformValuesClause(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1571
Definition: nodes.h:129
CmdType commandType
Definition: parsenodes.h:121
Node * utilityStmt
Definition: parsenodes.h:136
List * valuesLists
Definition: parsenodes.h:2150
SetOperation op
Definition: parsenodes.h:2166

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 }
static Query * transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
Definition: analyze.c:357
ParseLoc p_stmt_location
Definition: parse_node.h:210
ParseLoc p_stmt_len
Definition: parse_node.h:211
ParseLoc stmt_location
Definition: parsenodes.h:2023
ParseLoc stmt_len
Definition: parsenodes.h:2024

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

◆ transformUpdateTargetList()

List* transformUpdateTargetList ( ParseState pstate,
List origTlist 
)

Definition at line 2577 of file analyze.c.

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