PostgreSQL Source Code  git master
analyze.h File Reference
#include "nodes/params.h"
#include "parser/parse_node.h"
#include "utils/queryjumble.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 *targetList)
 
QuerytransformTopLevelStmt (ParseState *pstate, RawStmt *parseTree)
 
QuerytransformStmt (ParseState *pstate, Node *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 430 of file analyze.c.

431 {
432  bool result;
433 
434  switch (nodeTag(parseTree->stmt))
435  {
436  /*
437  * Optimizable statements
438  */
439  case T_InsertStmt:
440  case T_DeleteStmt:
441  case T_UpdateStmt:
442  case T_MergeStmt:
443  case T_SelectStmt:
444  case T_PLAssignStmt:
445  result = true;
446  break;
447 
448  /*
449  * Special cases
450  */
451  case T_DeclareCursorStmt:
452  case T_ExplainStmt:
453  case T_CreateTableAsStmt:
454  /* yes, because we must analyze the contained statement */
455  result = true;
456  break;
457 
458  default:
459  /* other utility statements don't have any real parse analysis */
460  result = false;
461  break;
462  }
463 
464  return result;
465 }
#define nodeTag(nodeptr)
Definition: nodes.h:579
@ T_DeleteStmt
Definition: nodes.h:334
@ T_UpdateStmt
Definition: nodes.h:335
@ T_ExplainStmt
Definition: nodes.h:372
@ T_InsertStmt
Definition: nodes.h:333
@ T_CreateTableAsStmt
Definition: nodes.h:373
@ T_DeclareCursorStmt
Definition: nodes.h:401
@ T_PLAssignStmt
Definition: nodes.h:339
@ T_SelectStmt
Definition: nodes.h:337
@ T_MergeStmt
Definition: nodes.h:336
Node * stmt
Definition: parsenodes.h:1904

References nodeTag, RawStmt::stmt, T_CreateTableAsStmt, T_DeclareCursorStmt, T_DeleteStmt, T_ExplainStmt, T_InsertStmt, T_MergeStmt, T_PLAssignStmt, T_SelectStmt, and T_UpdateStmt.

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

3399 {
3400  RowMarkClause *rc;
3401 
3402  Assert(strength != LCS_NONE); /* else caller error */
3403 
3404  /* If it's an explicit clause, make sure hasForUpdate gets set */
3405  if (!pushedDown)
3406  qry->hasForUpdate = true;
3407 
3408  /* Check for pre-existing entry for same rtindex */
3409  if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
3410  {
3411  /*
3412  * If the same RTE is specified with more than one locking strength,
3413  * use the strongest. (Reasonable, since you can't take both a shared
3414  * and exclusive lock at the same time; it'll end up being exclusive
3415  * anyway.)
3416  *
3417  * Similarly, if the same RTE is specified with more than one lock
3418  * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
3419  * turn wins over waiting for the lock (the default). This is a bit
3420  * more debatable but raising an error doesn't seem helpful. (Consider
3421  * for instance SELECT FOR UPDATE NOWAIT from a view that internally
3422  * contains a plain FOR UPDATE spec.) Having NOWAIT win over SKIP
3423  * LOCKED is reasonable since the former throws an error in case of
3424  * coming across a locked tuple, which may be undesirable in some
3425  * cases but it seems better than silently returning inconsistent
3426  * results.
3427  *
3428  * And of course pushedDown becomes false if any clause is explicit.
3429  */
3430  rc->strength = Max(rc->strength, strength);
3431  rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
3432  rc->pushedDown &= pushedDown;
3433  return;
3434  }
3435 
3436  /* Make a new RowMarkClause */
3437  rc = makeNode(RowMarkClause);
3438  rc->rti = rtindex;
3439  rc->strength = strength;
3440  rc->waitPolicy = waitPolicy;
3441  rc->pushedDown = pushedDown;
3442  qry->rowMarks = lappend(qry->rowMarks, rc);
3443 }
#define Max(x, y)
Definition: c.h:991
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
@ LCS_NONE
Definition: lockoptions.h:23
#define makeNode(_type_)
Definition: nodes.h:622
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
List * rowMarks
Definition: parsenodes.h:181
bool hasForUpdate
Definition: parsenodes.h:142
LockClauseStrength strength
Definition: parsenodes.h:1433
LockWaitPolicy waitPolicy
Definition: parsenodes.h:1434

References Assert(), get_parse_rowmark(), Query::hasForUpdate, 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 1170 of file analyze.c.

1172 {
1173  List *result = NIL;
1174  int attno;
1175  Var *var;
1176  TargetEntry *te;
1177 
1178  /*
1179  * Note that resnos of the tlist must correspond to attnos of the
1180  * underlying relation, hence we need entries for dropped columns too.
1181  */
1182  for (attno = 0; attno < RelationGetNumberOfAttributes(targetrel); attno++)
1183  {
1184  Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, attno);
1185  char *name;
1186 
1187  if (attr->attisdropped)
1188  {
1189  /*
1190  * can't use atttypid here, but it doesn't really matter what type
1191  * the Const claims to be.
1192  */
1193  var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
1194  name = NULL;
1195  }
1196  else
1197  {
1198  var = makeVar(exclRelIndex, attno + 1,
1199  attr->atttypid, attr->atttypmod,
1200  attr->attcollation,
1201  0);
1202  name = pstrdup(NameStr(attr->attname));
1203  }
1204 
1205  te = makeTargetEntry((Expr *) var,
1206  attno + 1,
1207  name,
1208  false);
1209 
1210  result = lappend(result, te);
1211  }
1212 
1213  /*
1214  * Add a whole-row-Var entry to support references to "EXCLUDED.*". Like
1215  * the other entries in the EXCLUDED tlist, its resno must match the Var's
1216  * varattno, else the wrong things happen while resolving references in
1217  * setrefs.c. This is against normal conventions for targetlists, but
1218  * it's okay since we don't use this as a real tlist.
1219  */
1220  var = makeVar(exclRelIndex, InvalidAttrNumber,
1221  targetrel->rd_rel->reltype,
1222  -1, InvalidOid, 0);
1223  te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
1224  result = lappend(result, te);
1225 
1226  return result;
1227 }
#define InvalidAttrNumber
Definition: attnum.h:23
#define NameStr(name)
Definition: c.h:692
const char * name
Definition: encode.c:561
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:239
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:338
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
char * pstrdup(const char *in)
Definition: mcxt.c:1305
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
#define NIL
Definition: pg_list.h:66
#define InvalidOid
Definition: postgres_ext.h:36
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:494
Definition: pg_list.h:52
TupleDesc rd_att
Definition: rel.h:110
Form_pg_class rd_rel
Definition: rel.h:109
Definition: primnodes.h:209
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

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

3144 {
3145  Assert(strength != LCS_NONE); /* else caller error */
3146 
3147  if (qry->setOperations)
3148  ereport(ERROR,
3149  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3150  /*------
3151  translator: %s is a SQL row locking clause such as FOR UPDATE */
3152  errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
3153  LCS_asString(strength))));
3154  if (qry->distinctClause != NIL)
3155  ereport(ERROR,
3156  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3157  /*------
3158  translator: %s is a SQL row locking clause such as FOR UPDATE */
3159  errmsg("%s is not allowed with DISTINCT clause",
3160  LCS_asString(strength))));
3161  if (qry->groupClause != NIL || qry->groupingSets != NIL)
3162  ereport(ERROR,
3163  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3164  /*------
3165  translator: %s is a SQL row locking clause such as FOR UPDATE */
3166  errmsg("%s is not allowed with GROUP BY clause",
3167  LCS_asString(strength))));
3168  if (qry->havingQual != NULL)
3169  ereport(ERROR,
3170  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3171  /*------
3172  translator: %s is a SQL row locking clause such as FOR UPDATE */
3173  errmsg("%s is not allowed with HAVING clause",
3174  LCS_asString(strength))));
3175  if (qry->hasAggs)
3176  ereport(ERROR,
3177  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3178  /*------
3179  translator: %s is a SQL row locking clause such as FOR UPDATE */
3180  errmsg("%s is not allowed with aggregate functions",
3181  LCS_asString(strength))));
3182  if (qry->hasWindowFuncs)
3183  ereport(ERROR,
3184  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3185  /*------
3186  translator: %s is a SQL row locking clause such as FOR UPDATE */
3187  errmsg("%s is not allowed with window functions",
3188  LCS_asString(strength))));
3189  if (qry->hasTargetSRFs)
3190  ereport(ERROR,
3191  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3192  /*------
3193  translator: %s is a SQL row locking clause such as FOR UPDATE */
3194  errmsg("%s is not allowed with set-returning functions in the target list",
3195  LCS_asString(strength))));
3196 }
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
const char * LCS_asString(LockClauseStrength strength)
Definition: analyze.c:3118
bool hasWindowFuncs
Definition: parsenodes.h:136
Node * setOperations
Definition: parsenodes.h:183
bool hasTargetSRFs
Definition: parsenodes.h:137
List * groupClause
Definition: parsenodes.h:164
Node * havingQual
Definition: parsenodes.h:169
bool hasAggs
Definition: parsenodes.h:135
List * groupingSets
Definition: parsenodes.h:167
List * distinctClause
Definition: parsenodes.h:173

References Assert(), Query::distinctClause, ereport, errcode(), errmsg(), ERROR, Query::groupClause, Query::groupingSets, Query::hasAggs, Query::hasTargetSRFs, Query::hasWindowFuncs, 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 3118 of file analyze.c.

3119 {
3120  switch (strength)
3121  {
3122  case LCS_NONE:
3123  Assert(false);
3124  break;
3125  case LCS_FORKEYSHARE:
3126  return "FOR KEY SHARE";
3127  case LCS_FORSHARE:
3128  return "FOR SHARE";
3129  case LCS_FORNOKEYUPDATE:
3130  return "FOR NO KEY UPDATE";
3131  case LCS_FORUPDATE:
3132  return "FOR UPDATE";
3133  }
3134  return "FOR some"; /* shouldn't happen */
3135 }
@ 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 1897 of file analyze.c.

1898 {
1900  Oid sortop;
1901  Oid eqop;
1902  bool hashable;
1903 
1904  /* determine the eqop and optional sortop */
1905  get_sort_group_operators(rescoltype,
1906  false, true, false,
1907  &sortop, &eqop, NULL,
1908  &hashable);
1909 
1910  /*
1911  * The type cache doesn't believe that record is hashable (see
1912  * cache_record_field_properties()), but if the caller really needs hash
1913  * support, we can assume it does. Worst case, if any components of the
1914  * record don't support hashing, we will fail at execution.
1915  */
1916  if (require_hash && (rescoltype == RECORDOID || rescoltype == RECORDARRAYOID))
1917  hashable = true;
1918 
1919  /* we don't have a tlist yet, so can't assign sortgrouprefs */
1920  grpcl->tleSortGroupRef = 0;
1921  grpcl->eqop = eqop;
1922  grpcl->sortop = sortop;
1923  grpcl->nulls_first = false; /* OK with or without sortop */
1924  grpcl->hashable = hashable;
1925 
1926  return grpcl;
1927 }
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:192
unsigned int Oid
Definition: postgres_ext.h:31
Index tleSortGroupRef
Definition: parsenodes.h:1306

References SortGroupClause::eqop, get_sort_group_operators(), SortGroupClause::hashable, makeNode, SortGroupClause::nulls_first, 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 106 of file analyze.c.

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

References Assert(), free_parsestate(), IsQueryIdEnabled(), JumbleQuery(), make_parsestate(), ParseState::p_queryEnv, ParseState::p_sourcetext, pgstat_report_query_id(), post_parse_analyze_hook, Query::queryId, 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 146 of file analyze.c.

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

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, Query::queryId, 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 187 of file analyze.c.

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

References Assert(), free_parsestate(), IsQueryIdEnabled(), JumbleQuery(), make_parsestate(), ParseState::p_queryEnv, ParseState::p_sourcetext, pgstat_report_query_id(), post_parse_analyze_hook, Query::queryId, 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 223 of file analyze.c.

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

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

◆ transformInsertRow()

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

Definition at line 962 of file analyze.c.

965 {
966  List *result;
967  ListCell *lc;
968  ListCell *icols;
969  ListCell *attnos;
970 
971  /*
972  * Check length of expr list. It must not have more expressions than
973  * there are target columns. We allow fewer, but only if no explicit
974  * columns list was given (the remaining columns are implicitly
975  * defaulted). Note we must check this *after* transformation because
976  * that could expand '*' into multiple items.
977  */
978  if (list_length(exprlist) > list_length(icolumns))
979  ereport(ERROR,
980  (errcode(ERRCODE_SYNTAX_ERROR),
981  errmsg("INSERT has more expressions than target columns"),
982  parser_errposition(pstate,
983  exprLocation(list_nth(exprlist,
984  list_length(icolumns))))));
985  if (stmtcols != NIL &&
986  list_length(exprlist) < list_length(icolumns))
987  {
988  /*
989  * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
990  * where the user accidentally created a RowExpr instead of separate
991  * columns. Add a suitable hint if that seems to be the problem,
992  * because the main error message is quite misleading for this case.
993  * (If there's no stmtcols, you'll get something about data type
994  * mismatch, which is less misleading so we don't worry about giving a
995  * hint in that case.)
996  */
997  ereport(ERROR,
998  (errcode(ERRCODE_SYNTAX_ERROR),
999  errmsg("INSERT has more target columns than expressions"),
1000  ((list_length(exprlist) == 1 &&
1001  count_rowexpr_columns(pstate, linitial(exprlist)) ==
1002  list_length(icolumns)) ?
1003  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),
1004  parser_errposition(pstate,
1005  exprLocation(list_nth(icolumns,
1006  list_length(exprlist))))));
1007  }
1008 
1009  /*
1010  * Prepare columns for assignment to target table.
1011  */
1012  result = NIL;
1013  forthree(lc, exprlist, icols, icolumns, attnos, attrnos)
1014  {
1015  Expr *expr = (Expr *) lfirst(lc);
1016  ResTarget *col = lfirst_node(ResTarget, icols);
1017  int attno = lfirst_int(attnos);
1018 
1019  expr = transformAssignedExpr(pstate, expr,
1021  col->name,
1022  attno,
1023  col->indirection,
1024  col->location);
1025 
1026  if (strip_indirection)
1027  {
1028  while (expr)
1029  {
1030  if (IsA(expr, FieldStore))
1031  {
1032  FieldStore *fstore = (FieldStore *) expr;
1033 
1034  expr = (Expr *) linitial(fstore->newvals);
1035  }
1036  else if (IsA(expr, SubscriptingRef))
1037  {
1038  SubscriptingRef *sbsref = (SubscriptingRef *) expr;
1039 
1040  if (sbsref->refassgnexpr == NULL)
1041  break;
1042 
1043  expr = sbsref->refassgnexpr;
1044  }
1045  else
1046  break;
1047  }
1048  }
1049 
1050  result = lappend(result, expr);
1051  }
1052 
1053  return result;
1054 }
int errhint(const char *fmt,...)
Definition: elog.c:1151
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1343
#define IsA(nodeptr, _type_)
Definition: nodes.h:625
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
@ 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:1240
#define lfirst(lc)
Definition: pg_list.h:170
#define lfirst_node(type, lc)
Definition: pg_list.h:174
static int list_length(const List *l)
Definition: pg_list.h:150
#define lfirst_int(lc)
Definition: pg_list.h:171
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:510
#define linitial(l)
Definition: pg_list.h:176
static void * list_nth(const List *list, int n)
Definition: pg_list.h:297
List * newvals
Definition: primnodes.h:959
int location
Definition: parsenodes.h:475
List * indirection
Definition: parsenodes.h:473
char * name
Definition: parsenodes.h:472
Expr * refassgnexpr
Definition: primnodes.h:541

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

◆ transformStmt()

Query* transformStmt ( ParseState pstate,
Node parseTree 
)

Definition at line 313 of file analyze.c.

314 {
315  Query *result;
316 
317  /*
318  * We apply RAW_EXPRESSION_COVERAGE_TEST testing to basic DML statements;
319  * we can't just run it on everything because raw_expression_tree_walker()
320  * doesn't claim to handle utility statements.
321  */
322 #ifdef RAW_EXPRESSION_COVERAGE_TEST
323  switch (nodeTag(parseTree))
324  {
325  case T_SelectStmt:
326  case T_InsertStmt:
327  case T_UpdateStmt:
328  case T_DeleteStmt:
329  case T_MergeStmt:
330  (void) test_raw_expression_coverage(parseTree, NULL);
331  break;
332  default:
333  break;
334  }
335 #endif /* RAW_EXPRESSION_COVERAGE_TEST */
336 
337  switch (nodeTag(parseTree))
338  {
339  /*
340  * Optimizable statements
341  */
342  case T_InsertStmt:
343  result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
344  break;
345 
346  case T_DeleteStmt:
347  result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
348  break;
349 
350  case T_UpdateStmt:
351  result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
352  break;
353 
354  case T_MergeStmt:
355  result = transformMergeStmt(pstate, (MergeStmt *) parseTree);
356  break;
357 
358  case T_SelectStmt:
359  {
360  SelectStmt *n = (SelectStmt *) parseTree;
361 
362  if (n->valuesLists)
363  result = transformValuesClause(pstate, n);
364  else if (n->op == SETOP_NONE)
365  result = transformSelectStmt(pstate, n);
366  else
367  result = transformSetOperationStmt(pstate, n);
368  }
369  break;
370 
371  case T_ReturnStmt:
372  result = transformReturnStmt(pstate, (ReturnStmt *) parseTree);
373  break;
374 
375  case T_PLAssignStmt:
376  result = transformPLAssignStmt(pstate,
377  (PLAssignStmt *) parseTree);
378  break;
379 
380  /*
381  * Special cases
382  */
383  case T_DeclareCursorStmt:
384  result = transformDeclareCursorStmt(pstate,
385  (DeclareCursorStmt *) parseTree);
386  break;
387 
388  case T_ExplainStmt:
389  result = transformExplainStmt(pstate,
390  (ExplainStmt *) parseTree);
391  break;
392 
393  case T_CreateTableAsStmt:
394  result = transformCreateTableAsStmt(pstate,
395  (CreateTableAsStmt *) parseTree);
396  break;
397 
398  case T_CallStmt:
399  result = transformCallStmt(pstate,
400  (CallStmt *) parseTree);
401  break;
402 
403  default:
404 
405  /*
406  * other statements don't require any transformation; just return
407  * the original parsetree with a Query node plastered on top.
408  */
409  result = makeNode(Query);
410  result->commandType = CMD_UTILITY;
411  result->utilityStmt = (Node *) parseTree;
412  break;
413  }
414 
415  /* Mark as original query until we learn differently */
416  result->querySource = QSRC_ORIGINAL;
417  result->canSetTag = true;
418 
419  return result;
420 }
@ CMD_UTILITY
Definition: nodes.h:727
@ T_ReturnStmt
Definition: nodes.h:338
@ T_CallStmt
Definition: nodes.h:448
Query * transformMergeStmt(ParseState *pstate, MergeStmt *stmt)
Definition: parse_merge.c:96
@ SETOP_NONE
Definition: parsenodes.h:1991
@ QSRC_ORIGINAL
Definition: parsenodes.h:42
static Query * transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
Definition: analyze.c:472
static Query * transformReturnStmt(ParseState *pstate, ReturnStmt *stmt)
Definition: analyze.c:2329
static Query * transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt)
Definition: analyze.c:2553
static Query * transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
Definition: analyze.c:2918
static Query * transformCallStmt(ParseState *pstate, CallStmt *stmt)
Definition: analyze.c:2993
static Query * transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1642
static Query * transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
Definition: analyze.c:2359
static Query * transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1282
static Query * transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
Definition: analyze.c:2894
static Query * transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
Definition: analyze.c:2801
static Query * transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
Definition: analyze.c:542
static Query * transformValuesClause(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1424
Definition: nodes.h:575
bool canSetTag
Definition: parsenodes.h:128
QuerySource querySource
Definition: parsenodes.h:123
CmdType commandType
Definition: parsenodes.h:121
Node * utilityStmt
Definition: parsenodes.h:130
List * valuesLists
Definition: parsenodes.h:2023
SetOperation op
Definition: parsenodes.h:2039

References Query::canSetTag, CMD_UTILITY, Query::commandType, makeNode, nodeTag, SelectStmt::op, QSRC_ORIGINAL, Query::querySource, SETOP_NONE, T_CallStmt, T_CreateTableAsStmt, T_DeclareCursorStmt, T_DeleteStmt, T_ExplainStmt, T_InsertStmt, T_MergeStmt, T_PLAssignStmt, T_ReturnStmt, T_SelectStmt, T_UpdateStmt, 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 250 of file analyze.c.

251 {
252  Query *result;
253 
254  /* We're at top level, so allow SELECT INTO */
255  result = transformOptionalSelectInto(pstate, parseTree->stmt);
256 
257  result->stmt_location = parseTree->stmt_location;
258  result->stmt_len = parseTree->stmt_len;
259 
260  return result;
261 }
static Query * transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
Definition: analyze.c:274
int stmt_location
Definition: parsenodes.h:198
int stmt_len
Definition: parsenodes.h:199
int stmt_len
Definition: parsenodes.h:1906
int stmt_location
Definition: parsenodes.h:1905

References RawStmt::stmt, Query::stmt_len, 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 ( ParseState pstate,
List targetList 
)

Definition at line 2423 of file analyze.c.

2424 {
2425  List *tlist = NIL;
2426  RangeTblEntry *target_rte;
2427  ListCell *orig_tl;
2428  ListCell *tl;
2429 
2430  tlist = transformTargetList(pstate, origTlist,
2432 
2433  /* Prepare to assign non-conflicting resnos to resjunk attributes */
2436 
2437  /* Prepare non-junk columns for assignment to target table */
2438  target_rte = pstate->p_target_nsitem->p_rte;
2439  orig_tl = list_head(origTlist);
2440 
2441  foreach(tl, tlist)
2442  {
2443  TargetEntry *tle = (TargetEntry *) lfirst(tl);
2444  ResTarget *origTarget;
2445  int attrno;
2446 
2447  if (tle->resjunk)
2448  {
2449  /*
2450  * Resjunk nodes need no additional processing, but be sure they
2451  * have resnos that do not match any target columns; else rewriter
2452  * or planner might get confused. They don't need a resname
2453  * either.
2454  */
2455  tle->resno = (AttrNumber) pstate->p_next_resno++;
2456  tle->resname = NULL;
2457  continue;
2458  }
2459  if (orig_tl == NULL)
2460  elog(ERROR, "UPDATE target count mismatch --- internal error");
2461  origTarget = lfirst_node(ResTarget, orig_tl);
2462 
2463  attrno = attnameAttNum(pstate->p_target_relation,
2464  origTarget->name, true);
2465  if (attrno == InvalidAttrNumber)
2466  ereport(ERROR,
2467  (errcode(ERRCODE_UNDEFINED_COLUMN),
2468  errmsg("column \"%s\" of relation \"%s\" does not exist",
2469  origTarget->name,
2471  parser_errposition(pstate, origTarget->location)));
2472 
2473  updateTargetListEntry(pstate, tle, origTarget->name,
2474  attrno,
2475  origTarget->indirection,
2476  origTarget->location);
2477 
2478  /* Mark the target column as requiring update permissions */
2479  target_rte->updatedCols = bms_add_member(target_rte->updatedCols,
2481 
2482  orig_tl = lnext(origTlist, orig_tl);
2483  }
2484  if (orig_tl != NULL)
2485  elog(ERROR, "UPDATE target count mismatch --- internal error");
2486 
2487  return tlist;
2488 }
int16 AttrNumber
Definition: attnum.h:21
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:738
@ EXPR_KIND_UPDATE_SOURCE
Definition: parse_node.h:56
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
List * transformTargetList(ParseState *pstate, List *targetlist, ParseExprKind exprKind)
Definition: parse_target.c:123
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:126
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:341
#define RelationGetRelationName(relation)
Definition: rel.h:522
RangeTblEntry * p_rte
Definition: parse_node.h:269
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:194
int p_next_resno
Definition: parse_node.h:198
Relation p_target_relation
Definition: parse_node.h:193
Bitmapset * updatedCols
Definition: parsenodes.h:1170
char * resname
Definition: primnodes.h:1830
AttrNumber resno
Definition: primnodes.h:1829
bool resjunk
Definition: primnodes.h:1835
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References attnameAttNum(), bms_add_member(), elog(), ereport, errcode(), errmsg(), ERROR, EXPR_KIND_UPDATE_SOURCE, FirstLowInvalidHeapAttributeNumber, ResTarget::indirection, InvalidAttrNumber, lfirst, lfirst_node, list_head(), lnext(), ResTarget::location, ResTarget::name, NIL, ParseState::p_next_resno, ParseNamespaceItem::p_rte, ParseState::p_target_nsitem, ParseState::p_target_relation, parser_errposition(), RelationGetNumberOfAttributes, RelationGetRelationName, TargetEntry::resjunk, TargetEntry::resname, TargetEntry::resno, transformTargetList(), RangeTblEntry::updatedCols, and updateTargetListEntry().

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

Variable Documentation

◆ post_parse_analyze_hook