PostgreSQL Source Code  git master
parse_relation.h File Reference
Include dependency graph for parse_relation.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ParseNamespaceItemrefnameNamespaceItem (ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
 
CommonTableExprscanNameSpaceForCTE (ParseState *pstate, const char *refname, Index *ctelevelsup)
 
bool scanNameSpaceForENR (ParseState *pstate, const char *refname)
 
void checkNameSpaceConflicts (ParseState *pstate, List *namespace1, List *namespace2)
 
ParseNamespaceItemGetNSItemByRangeTablePosn (ParseState *pstate, int varno, int sublevels_up)
 
RangeTblEntryGetRTEByRangeTablePosn (ParseState *pstate, int varno, int sublevels_up)
 
CommonTableExprGetCTEForRTE (ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
 
NodescanNSItemForColumn (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
 
NodecolNameToVar (ParseState *pstate, const char *colname, bool localonly, int location)
 
void markVarForSelectPriv (ParseState *pstate, Var *var)
 
Relation parserOpenTable (ParseState *pstate, const RangeVar *relation, int lockmode)
 
ParseNamespaceItemaddRangeTableEntry (ParseState *pstate, RangeVar *relation, Alias *alias, bool inh, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForRelation (ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForSubquery (ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForFunction (ParseState *pstate, List *funcnames, List *funcexprs, List *coldeflists, RangeFunction *rangefunc, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForValues (ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForTableFunc (ParseState *pstate, TableFunc *tf, Alias *alias, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForJoin (ParseState *pstate, List *colnames, ParseNamespaceColumn *nscolumns, JoinType jointype, int nummergedcols, List *aliasvars, List *leftcols, List *rightcols, Alias *joinalias, Alias *alias, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForCTE (ParseState *pstate, CommonTableExpr *cte, Index levelsup, RangeVar *rv, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForENR (ParseState *pstate, RangeVar *rv, bool inFromCl)
 
bool isLockedRefname (ParseState *pstate, const char *refname)
 
void addNSItemToQuery (ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
 
void errorMissingRTE (ParseState *pstate, RangeVar *relation) pg_attribute_noreturn()
 
void errorMissingColumn (ParseState *pstate, const char *relname, const char *colname, int location) pg_attribute_noreturn()
 
void expandRTE (RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
 
ListexpandNSItemVars (ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
 
ListexpandNSItemAttrs (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location)
 
int attnameAttNum (Relation rd, const char *attname, bool sysColOK)
 
const NameDataattnumAttName (Relation rd, int attid)
 
Oid attnumTypeId (Relation rd, int attid)
 
Oid attnumCollationId (Relation rd, int attid)
 
bool isQueryUsingTempRelation (Query *query)
 

Function Documentation

◆ addNSItemToQuery()

void addNSItemToQuery ( ParseState pstate,
ParseNamespaceItem nsitem,
bool  addToJoinList,
bool  addToRelNameSpace,
bool  addToVarNameSpace 
)

Definition at line 2544 of file parse_relation.c.

References lappend(), makeNode, ParseNamespaceItem::p_cols_visible, ParseState::p_joinlist, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_rtindex, and RangeTblRef::rtindex.

Referenced by AddRelationNewConstraints(), AlterPolicy(), CreatePolicy(), CreateTrigger(), DefineRelation(), DoCopy(), setTargetTable(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformIndexStmt(), transformInsertStmt(), transformOnConflictClause(), transformPartitionSpec(), transformRuleStmt(), transformSetOperationStmt(), transformStatsStmt(), and transformValuesClause().

2547 {
2548  if (addToJoinList)
2549  {
2551 
2552  rtr->rtindex = nsitem->p_rtindex;
2553  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2554  }
2555  if (addToRelNameSpace || addToVarNameSpace)
2556  {
2557  /* Set the new nsitem's visibility flags correctly */
2558  nsitem->p_rel_visible = addToRelNameSpace;
2559  nsitem->p_cols_visible = addToVarNameSpace;
2560  nsitem->p_lateral_only = false;
2561  nsitem->p_lateral_ok = true;
2562  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2563  }
2564 }
List * p_namespace
Definition: parse_node.h:186
List * lappend(List *list, void *datum)
Definition: list.c:336
#define makeNode(_type_)
Definition: nodes.h:587
List * p_joinlist
Definition: parse_node.h:184

◆ addRangeTableEntry()

ParseNamespaceItem* addRangeTableEntry ( ParseState pstate,
RangeVar relation,
Alias alias,
bool  inh,
bool  inFromCl 
)

Definition at line 1402 of file parse_relation.c.

References AccessShareLock, ACL_SELECT, RangeTblEntry::alias, Alias::aliasname, Assert, buildNSItemFromTupleDesc(), buildRelationAliases(), RangeTblEntry::checkAsUser, RangeTblEntry::eref, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, isLockedRefname(), lappend(), RangeTblEntry::lateral, list_length(), makeAlias(), makeNode, NIL, NoLock, ParseState::p_rtable, parserOpenTable(), RelationData::rd_att, RelationData::rd_rel, RelationGetRelid, RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::rellockmode, RangeVar::relname, RangeTblEntry::requiredPerms, RowShareLock, RTE_RELATION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, table_close(), and RangeTblEntry::updatedCols.

Referenced by transformTableEntry().

1407 {
1409  char *refname = alias ? alias->aliasname : relation->relname;
1410  LOCKMODE lockmode;
1411  Relation rel;
1412  ParseNamespaceItem *nsitem;
1413 
1414  Assert(pstate != NULL);
1415 
1416  rte->rtekind = RTE_RELATION;
1417  rte->alias = alias;
1418 
1419  /*
1420  * Identify the type of lock we'll need on this relation. It's not the
1421  * query's target table (that case is handled elsewhere), so we need
1422  * either RowShareLock if it's locked by FOR UPDATE/SHARE, or plain
1423  * AccessShareLock otherwise.
1424  */
1425  lockmode = isLockedRefname(pstate, refname) ? RowShareLock : AccessShareLock;
1426 
1427  /*
1428  * Get the rel's OID. This access also ensures that we have an up-to-date
1429  * relcache entry for the rel. Since this is typically the first access
1430  * to a rel in a statement, we must open the rel with the proper lockmode.
1431  */
1432  rel = parserOpenTable(pstate, relation, lockmode);
1433  rte->relid = RelationGetRelid(rel);
1434  rte->relkind = rel->rd_rel->relkind;
1435  rte->rellockmode = lockmode;
1436 
1437  /*
1438  * Build the list of effective column names using user-supplied aliases
1439  * and/or actual column names.
1440  */
1441  rte->eref = makeAlias(refname, NIL);
1442  buildRelationAliases(rel->rd_att, alias, rte->eref);
1443 
1444  /*
1445  * Set flags and access permissions.
1446  *
1447  * The initial default on access checks is always check-for-READ-access,
1448  * which is the right thing for all except target tables.
1449  */
1450  rte->lateral = false;
1451  rte->inh = inh;
1452  rte->inFromCl = inFromCl;
1453 
1454  rte->requiredPerms = ACL_SELECT;
1455  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1456  rte->selectedCols = NULL;
1457  rte->insertedCols = NULL;
1458  rte->updatedCols = NULL;
1459  rte->extraUpdatedCols = NULL;
1460 
1461  /*
1462  * Add completed RTE to pstate's range table list, so that we know its
1463  * index. But we don't add it to the join list --- caller must do that if
1464  * appropriate.
1465  */
1466  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1467 
1468  /*
1469  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1470  * list --- caller must do that if appropriate.
1471  */
1472  nsitem = buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1473  rel->rd_att);
1474 
1475  /*
1476  * Drop the rel refcount, but keep the access lock till end of transaction
1477  * so that the table can't be deleted or have its schema modified
1478  * underneath us.
1479  */
1480  table_close(rel, NoLock);
1481 
1482  return nsitem;
1483 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Alias * alias
Definition: parsenodes.h:1140
int LOCKMODE
Definition: lockdefs.h:26
#define AccessShareLock
Definition: lockdefs.h:36
Relation parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
AclMode requiredPerms
Definition: parsenodes.h:1145
Form_pg_class rd_rel
Definition: rel.h:109
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
char * relname
Definition: primnodes.h:68
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1150
Bitmapset * selectedCols
Definition: parsenodes.h:1147
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
#define NoLock
Definition: lockdefs.h:34
List * lappend(List *list, void *datum)
Definition: list.c:336
#define RowShareLock
Definition: lockdefs.h:37
#define ACL_SELECT
Definition: parsenodes.h:83
TupleDesc rd_att
Definition: rel.h:110
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1149
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:995
Bitmapset * insertedCols
Definition: parsenodes.h:1148
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
bool isLockedRefname(ParseState *pstate, const char *refname)
Alias * eref
Definition: parsenodes.h:1141
#define RelationGetRelid(relation)
Definition: rel.h:477
List * p_rtable
Definition: parse_node.h:182

◆ addRangeTableEntryForCTE()

ParseNamespaceItem* addRangeTableEntryForCTE ( ParseState pstate,
CommonTableExpr cte,
Index  levelsup,
RangeVar rv,
bool  inFromCl 
)

Definition at line 2233 of file parse_relation.c.

References RangeVar::alias, RangeTblEntry::alias, Alias::aliasname, Assert, buildNSItemFromLists(), RangeTblEntry::checkAsUser, CMD_SELECT, RangeTblEntry::colcollations, Alias::colnames, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, Query::commandType, copyObject, CommonTableExpr::ctecolcollations, CommonTableExpr::ctecolnames, CommonTableExpr::ctecoltypes, CommonTableExpr::ctecoltypmods, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, CommonTableExpr::ctename, CommonTableExpr::ctequery, CommonTableExpr::cterecursive, CommonTableExpr::cterefcount, CommonTableExpr::cycle_clause, CTECycleClause::cycle_mark_collation, CTECycleClause::cycle_mark_column, CTECycleClause::cycle_mark_type, CTECycleClause::cycle_mark_typmod, CTECycleClause::cycle_path_column, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::extraUpdatedCols, i, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, IsA, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, list_copy(), list_length(), RangeVar::location, makeAlias(), makeNode, makeString(), NIL, ParseNamespaceColumn::p_dontexpand, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseState::p_rtable, parser_errposition(), RangeTblEntry::requiredPerms, Query::returningList, RTE_CTE, RangeTblEntry::rtekind, CTESearchClause::search_breadth_first, CommonTableExpr::search_clause, CTESearchClause::search_seq_column, RangeTblEntry::selectedCols, RangeTblEntry::self_reference, and RangeTblEntry::updatedCols.

Referenced by getNSItemForSpecialRelationTypes().

2238 {
2240  Alias *alias = rv->alias;
2241  char *refname = alias ? alias->aliasname : cte->ctename;
2242  Alias *eref;
2243  int numaliases;
2244  int varattno;
2245  ListCell *lc;
2246  int n_dontexpand_columns = 0;
2247  ParseNamespaceItem *psi;
2248 
2249  Assert(pstate != NULL);
2250 
2251  rte->rtekind = RTE_CTE;
2252  rte->ctename = cte->ctename;
2253  rte->ctelevelsup = levelsup;
2254 
2255  /* Self-reference if and only if CTE's parse analysis isn't completed */
2256  rte->self_reference = !IsA(cte->ctequery, Query);
2257  Assert(cte->cterecursive || !rte->self_reference);
2258  /* Bump the CTE's refcount if this isn't a self-reference */
2259  if (!rte->self_reference)
2260  cte->cterefcount++;
2261 
2262  /*
2263  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
2264  * This won't get checked in case of a self-reference, but that's OK
2265  * because data-modifying CTEs aren't allowed to be recursive anyhow.
2266  */
2267  if (IsA(cte->ctequery, Query))
2268  {
2269  Query *ctequery = (Query *) cte->ctequery;
2270 
2271  if (ctequery->commandType != CMD_SELECT &&
2272  ctequery->returningList == NIL)
2273  ereport(ERROR,
2274  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2275  errmsg("WITH query \"%s\" does not have a RETURNING clause",
2276  cte->ctename),
2277  parser_errposition(pstate, rv->location)));
2278  }
2279 
2280  rte->coltypes = list_copy(cte->ctecoltypes);
2281  rte->coltypmods = list_copy(cte->ctecoltypmods);
2283 
2284  rte->alias = alias;
2285  if (alias)
2286  eref = copyObject(alias);
2287  else
2288  eref = makeAlias(refname, NIL);
2289  numaliases = list_length(eref->colnames);
2290 
2291  /* fill in any unspecified alias columns */
2292  varattno = 0;
2293  foreach(lc, cte->ctecolnames)
2294  {
2295  varattno++;
2296  if (varattno > numaliases)
2297  eref->colnames = lappend(eref->colnames, lfirst(lc));
2298  }
2299  if (varattno < numaliases)
2300  ereport(ERROR,
2301  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2302  errmsg("table \"%s\" has %d columns available but %d columns specified",
2303  refname, varattno, numaliases)));
2304 
2305  rte->eref = eref;
2306 
2307  if (cte->search_clause)
2308  {
2311  rte->coltypes = lappend_oid(rte->coltypes, RECORDOID);
2312  else
2313  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2314  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2316 
2317  n_dontexpand_columns += 1;
2318  }
2319 
2320  if (cte->cycle_clause)
2321  {
2326 
2328  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2329  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2331 
2332  n_dontexpand_columns += 2;
2333  }
2334 
2335  /*
2336  * Set flags and access permissions.
2337  *
2338  * Subqueries are never checked for access rights.
2339  */
2340  rte->lateral = false;
2341  rte->inh = false; /* never true for subqueries */
2342  rte->inFromCl = inFromCl;
2343 
2344  rte->requiredPerms = 0;
2345  rte->checkAsUser = InvalidOid;
2346  rte->selectedCols = NULL;
2347  rte->insertedCols = NULL;
2348  rte->updatedCols = NULL;
2349  rte->extraUpdatedCols = NULL;
2350 
2351  /*
2352  * Add completed RTE to pstate's range table list, so that we know its
2353  * index. But we don't add it to the join list --- caller must do that if
2354  * appropriate.
2355  */
2356  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2357 
2358  /*
2359  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2360  * list --- caller must do that if appropriate.
2361  */
2362  psi = buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2363  rte->coltypes, rte->coltypmods,
2364  rte->colcollations);
2365 
2366  /*
2367  * The columns added by search and cycle clauses are not included in star
2368  * expansion in queries contained in the CTE.
2369  */
2370  if (rte->ctelevelsup > 0)
2371  for (int i = 0; i < n_dontexpand_columns; i++)
2372  psi->p_nscolumns[list_length(psi->p_names->colnames) - 1 - i].p_dontexpand = true;
2373 
2374  return psi;
2375 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
bool search_breadth_first
Definition: parsenodes.h:1476
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Alias * alias
Definition: parsenodes.h:1140
List * colnames
Definition: primnodes.h:43
char * search_seq_column
Definition: parsenodes.h:1477
List * coltypmods
Definition: parsenodes.h:1128
List * list_copy(const List *oldlist)
Definition: list.c:1418
int errcode(int sqlerrcode)
Definition: elog.c:698
AclMode requiredPerms
Definition: parsenodes.h:1145
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
List * colcollations
Definition: parsenodes.h:1129
int location
Definition: primnodes.h:73
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1150
Bitmapset * selectedCols
Definition: parsenodes.h:1147
#define ERROR
Definition: elog.h:46
CTESearchClause * search_clause
Definition: parsenodes.h:1505
List * ctecoltypmods
Definition: parsenodes.h:1514
List * returningList
Definition: parsenodes.h:156
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
List * ctecolnames
Definition: parsenodes.h:1512
bool self_reference
Definition: parsenodes.h:1107
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
Oid cycle_mark_collation
Definition: parsenodes.h:1493
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
Bitmapset * updatedCols
Definition: parsenodes.h:1149
CmdType commandType
Definition: parsenodes.h:120
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Index ctelevelsup
Definition: parsenodes.h:1106
List * ctecoltypes
Definition: parsenodes.h:1513
RTEKind rtekind
Definition: parsenodes.h:995
char * cycle_path_column
Definition: parsenodes.h:1488
char * ctename
Definition: parsenodes.h:1105
CTECycleClause * cycle_clause
Definition: parsenodes.h:1506
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:271
int errmsg(const char *fmt,...)
Definition: elog.c:909
char * cycle_mark_column
Definition: parsenodes.h:1485
Bitmapset * insertedCols
Definition: parsenodes.h:1148
int i
Alias * alias
Definition: primnodes.h:72
Alias * eref
Definition: parsenodes.h:1141
List * ctecolcollations
Definition: parsenodes.h:1515
#define copyObject(obj)
Definition: nodes.h:655
List * coltypes
Definition: parsenodes.h:1127
List * p_rtable
Definition: parse_node.h:182

◆ addRangeTableEntryForENR()

ParseNamespaceItem* addRangeTableEntryForENR ( ParseState pstate,
RangeVar rv,
bool  inFromCl 
)

Definition at line 2391 of file parse_relation.c.

References RangeVar::alias, Alias::aliasname, Assert, buildNSItemFromTupleDesc(), buildRelationAliases(), RangeTblEntry::checkAsUser, RangeTblEntry::colcollations, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, elog, ENR_NAMED_TUPLESTORE, ENRMetadataGetTupDesc(), RangeTblEntry::enrname, EphemeralNamedRelationMetadataData::enrtuples, RangeTblEntry::enrtuples, EphemeralNamedRelationMetadataData::enrtype, RangeTblEntry::eref, ERROR, get_visible_ENR(), RangeTblEntry::inFromCl, RangeTblEntry::inh, InvalidOid, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, list_length(), makeAlias(), makeNode, EphemeralNamedRelationMetadataData::name, TupleDescData::natts, NIL, ParseState::p_rtable, RangeTblEntry::relid, EphemeralNamedRelationMetadataData::reliddesc, RangeVar::relname, RangeTblEntry::requiredPerms, RTE_NAMEDTUPLESTORE, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, and TupleDescAttr.

Referenced by getNSItemForSpecialRelationTypes().

2394 {
2396  Alias *alias = rv->alias;
2397  char *refname = alias ? alias->aliasname : rv->relname;
2399  TupleDesc tupdesc;
2400  int attno;
2401 
2402  Assert(pstate != NULL);
2403  enrmd = get_visible_ENR(pstate, rv->relname);
2404  Assert(enrmd != NULL);
2405 
2406  switch (enrmd->enrtype)
2407  {
2408  case ENR_NAMED_TUPLESTORE:
2410  break;
2411 
2412  default:
2413  elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2414  return NULL; /* for fussy compilers */
2415  }
2416 
2417  /*
2418  * Record dependency on a relation. This allows plans to be invalidated
2419  * if they access transition tables linked to a table that is altered.
2420  */
2421  rte->relid = enrmd->reliddesc;
2422 
2423  /*
2424  * Build the list of effective column names using user-supplied aliases
2425  * and/or actual column names.
2426  */
2427  tupdesc = ENRMetadataGetTupDesc(enrmd);
2428  rte->eref = makeAlias(refname, NIL);
2429  buildRelationAliases(tupdesc, alias, rte->eref);
2430 
2431  /* Record additional data for ENR, including column type info */
2432  rte->enrname = enrmd->name;
2433  rte->enrtuples = enrmd->enrtuples;
2434  rte->coltypes = NIL;
2435  rte->coltypmods = NIL;
2436  rte->colcollations = NIL;
2437  for (attno = 1; attno <= tupdesc->natts; ++attno)
2438  {
2439  Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2440 
2441  if (att->attisdropped)
2442  {
2443  /* Record zeroes for a dropped column */
2444  rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2445  rte->coltypmods = lappend_int(rte->coltypmods, 0);
2447  }
2448  else
2449  {
2450  /* Let's just make sure we can tell this isn't dropped */
2451  if (att->atttypid == InvalidOid)
2452  elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2453  rv->relname);
2454  rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2455  rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2457  att->attcollation);
2458  }
2459  }
2460 
2461  /*
2462  * Set flags and access permissions.
2463  *
2464  * ENRs are never checked for access rights.
2465  */
2466  rte->lateral = false;
2467  rte->inh = false; /* never true for ENRs */
2468  rte->inFromCl = inFromCl;
2469 
2470  rte->requiredPerms = 0;
2471  rte->checkAsUser = InvalidOid;
2472  rte->selectedCols = NULL;
2473 
2474  /*
2475  * Add completed RTE to pstate's range table list, so that we know its
2476  * index. But we don't add it to the join list --- caller must do that if
2477  * appropriate.
2478  */
2479  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2480 
2481  /*
2482  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2483  * list --- caller must do that if appropriate.
2484  */
2485  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
2486  tupdesc);
2487 }
#define NIL
Definition: pg_list.h:65
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
List * coltypmods
Definition: parsenodes.h:1128
AclMode requiredPerms
Definition: parsenodes.h:1145
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
List * colcollations
Definition: parsenodes.h:1129
EphemeralNamedRelationMetadata get_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:26
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
char * relname
Definition: primnodes.h:68
Bitmapset * selectedCols
Definition: parsenodes.h:1147
#define ERROR
Definition: elog.h:46
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
char * enrname
Definition: parsenodes.h:1134
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
RTEKind rtekind
Definition: parsenodes.h:995
double enrtuples
Definition: parsenodes.h:1135
#define elog(elevel,...)
Definition: elog.h:232
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * alias
Definition: primnodes.h:72
EphemeralNameRelationType enrtype
Alias * eref
Definition: parsenodes.h:1141
List * coltypes
Definition: parsenodes.h:1127
List * p_rtable
Definition: parse_node.h:182

◆ addRangeTableEntryForFunction()

ParseNamespaceItem* addRangeTableEntryForFunction ( ParseState pstate,
List funcnames,
List funcexprs,
List coldeflists,
RangeFunction rangefunc,
bool  lateral,
bool  inFromCl 
)

Definition at line 1668 of file parse_relation.c.

References RangeFunction::alias, RangeTblEntry::alias, Alias::aliasname, Assert, buildNSItemFromTupleDesc(), buildRelationAliases(), RangeTblEntry::checkAsUser, CheckAttributeNamesTypes(), CHKATYPE_ANYRECORD, chooseScalarFunctionAlias(), ColumnDef::colname, CreateTemplateTupleDesc(), RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, exprCollation(), exprLocation(), exprType(), exprTypmod(), RangeTblEntry::extraUpdatedCols, format_type_be(), forthree, RangeTblFunction::funccolcollations, RangeTblFunction::funccolcount, RangeTblFunction::funccolnames, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblFunction::funcparams, RangeTblEntry::functions, get_expr_result_type(), GetColumnDefCollation(), i, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, linitial, list_length(), ColumnDef::location, makeAlias(), makeNode, makeString(), TupleDescData::natts, NIL, RangeFunction::ordinality, ParseState::p_rtable, palloc(), parser_errposition(), pstrdup(), RangeTblEntry::relid, RangeTblEntry::requiredPerms, RTE_FUNCTION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, TypeName::setof, RangeTblEntry::subquery, TupleDescCopyEntry(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, TYPEFUNC_SCALAR, ColumnDef::typeName, typenameTypeIdAndMod(), and RangeTblEntry::updatedCols.

Referenced by transformRangeFunction().

1675 {
1677  Alias *alias = rangefunc->alias;
1678  Alias *eref;
1679  char *aliasname;
1680  int nfuncs = list_length(funcexprs);
1681  TupleDesc *functupdescs;
1682  TupleDesc tupdesc;
1683  ListCell *lc1,
1684  *lc2,
1685  *lc3;
1686  int i;
1687  int j;
1688  int funcno;
1689  int natts,
1690  totalatts;
1691 
1692  Assert(pstate != NULL);
1693 
1694  rte->rtekind = RTE_FUNCTION;
1695  rte->relid = InvalidOid;
1696  rte->subquery = NULL;
1697  rte->functions = NIL; /* we'll fill this list below */
1698  rte->funcordinality = rangefunc->ordinality;
1699  rte->alias = alias;
1700 
1701  /*
1702  * Choose the RTE alias name. We default to using the first function's
1703  * name even when there's more than one; which is maybe arguable but beats
1704  * using something constant like "table".
1705  */
1706  if (alias)
1707  aliasname = alias->aliasname;
1708  else
1709  aliasname = linitial(funcnames);
1710 
1711  eref = makeAlias(aliasname, NIL);
1712  rte->eref = eref;
1713 
1714  /* Process each function ... */
1715  functupdescs = (TupleDesc *) palloc(nfuncs * sizeof(TupleDesc));
1716 
1717  totalatts = 0;
1718  funcno = 0;
1719  forthree(lc1, funcexprs, lc2, funcnames, lc3, coldeflists)
1720  {
1721  Node *funcexpr = (Node *) lfirst(lc1);
1722  char *funcname = (char *) lfirst(lc2);
1723  List *coldeflist = (List *) lfirst(lc3);
1725  TypeFuncClass functypclass;
1726  Oid funcrettype;
1727 
1728  /* Initialize RangeTblFunction node */
1729  rtfunc->funcexpr = funcexpr;
1730  rtfunc->funccolnames = NIL;
1731  rtfunc->funccoltypes = NIL;
1732  rtfunc->funccoltypmods = NIL;
1733  rtfunc->funccolcollations = NIL;
1734  rtfunc->funcparams = NULL; /* not set until planning */
1735 
1736  /*
1737  * Now determine if the function returns a simple or composite type.
1738  */
1739  functypclass = get_expr_result_type(funcexpr,
1740  &funcrettype,
1741  &tupdesc);
1742 
1743  /*
1744  * A coldeflist is required if the function returns RECORD and hasn't
1745  * got a predetermined record type, and is prohibited otherwise. This
1746  * can be a bit confusing, so we expend some effort on delivering a
1747  * relevant error message.
1748  */
1749  if (coldeflist != NIL)
1750  {
1751  switch (functypclass)
1752  {
1753  case TYPEFUNC_RECORD:
1754  /* ok */
1755  break;
1756  case TYPEFUNC_COMPOSITE:
1758 
1759  /*
1760  * If the function's raw result type is RECORD, we must
1761  * have resolved it using its OUT parameters. Otherwise,
1762  * it must have a named composite type.
1763  */
1764  if (exprType(funcexpr) == RECORDOID)
1765  ereport(ERROR,
1766  (errcode(ERRCODE_SYNTAX_ERROR),
1767  errmsg("a column definition list is redundant for a function with OUT parameters"),
1768  parser_errposition(pstate,
1769  exprLocation((Node *) coldeflist))));
1770  else
1771  ereport(ERROR,
1772  (errcode(ERRCODE_SYNTAX_ERROR),
1773  errmsg("a column definition list is redundant for a function returning a named composite type"),
1774  parser_errposition(pstate,
1775  exprLocation((Node *) coldeflist))));
1776  break;
1777  default:
1778  ereport(ERROR,
1779  (errcode(ERRCODE_SYNTAX_ERROR),
1780  errmsg("a column definition list is only allowed for functions returning \"record\""),
1781  parser_errposition(pstate,
1782  exprLocation((Node *) coldeflist))));
1783  break;
1784  }
1785  }
1786  else
1787  {
1788  if (functypclass == TYPEFUNC_RECORD)
1789  ereport(ERROR,
1790  (errcode(ERRCODE_SYNTAX_ERROR),
1791  errmsg("a column definition list is required for functions returning \"record\""),
1792  parser_errposition(pstate, exprLocation(funcexpr))));
1793  }
1794 
1795  if (functypclass == TYPEFUNC_COMPOSITE ||
1796  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
1797  {
1798  /* Composite data type, e.g. a table's row type */
1799  Assert(tupdesc);
1800  }
1801  else if (functypclass == TYPEFUNC_SCALAR)
1802  {
1803  /* Base data type, i.e. scalar */
1804  tupdesc = CreateTemplateTupleDesc(1);
1805  TupleDescInitEntry(tupdesc,
1806  (AttrNumber) 1,
1807  chooseScalarFunctionAlias(funcexpr, funcname,
1808  alias, nfuncs),
1809  funcrettype,
1810  exprTypmod(funcexpr),
1811  0);
1813  (AttrNumber) 1,
1814  exprCollation(funcexpr));
1815  }
1816  else if (functypclass == TYPEFUNC_RECORD)
1817  {
1818  ListCell *col;
1819 
1820  /*
1821  * Use the column definition list to construct a tupdesc and fill
1822  * in the RangeTblFunction's lists.
1823  */
1824  tupdesc = CreateTemplateTupleDesc(list_length(coldeflist));
1825  i = 1;
1826  foreach(col, coldeflist)
1827  {
1828  ColumnDef *n = (ColumnDef *) lfirst(col);
1829  char *attrname;
1830  Oid attrtype;
1831  int32 attrtypmod;
1832  Oid attrcollation;
1833 
1834  attrname = n->colname;
1835  if (n->typeName->setof)
1836  ereport(ERROR,
1837  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
1838  errmsg("column \"%s\" cannot be declared SETOF",
1839  attrname),
1840  parser_errposition(pstate, n->location)));
1841  typenameTypeIdAndMod(pstate, n->typeName,
1842  &attrtype, &attrtypmod);
1843  attrcollation = GetColumnDefCollation(pstate, n, attrtype);
1844  TupleDescInitEntry(tupdesc,
1845  (AttrNumber) i,
1846  attrname,
1847  attrtype,
1848  attrtypmod,
1849  0);
1851  (AttrNumber) i,
1852  attrcollation);
1853  rtfunc->funccolnames = lappend(rtfunc->funccolnames,
1854  makeString(pstrdup(attrname)));
1855  rtfunc->funccoltypes = lappend_oid(rtfunc->funccoltypes,
1856  attrtype);
1857  rtfunc->funccoltypmods = lappend_int(rtfunc->funccoltypmods,
1858  attrtypmod);
1860  attrcollation);
1861 
1862  i++;
1863  }
1864 
1865  /*
1866  * Ensure that the coldeflist defines a legal set of names (no
1867  * duplicates, but we needn't worry about system column names) and
1868  * datatypes. Although we mostly can't allow pseudo-types, it
1869  * seems safe to allow RECORD and RECORD[], since values within
1870  * those type classes are self-identifying at runtime, and the
1871  * coldeflist doesn't represent anything that will be visible to
1872  * other sessions.
1873  */
1874  CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE,
1876  }
1877  else
1878  ereport(ERROR,
1879  (errcode(ERRCODE_DATATYPE_MISMATCH),
1880  errmsg("function \"%s\" in FROM has unsupported return type %s",
1881  funcname, format_type_be(funcrettype)),
1882  parser_errposition(pstate, exprLocation(funcexpr))));
1883 
1884  /* Finish off the RangeTblFunction and add it to the RTE's list */
1885  rtfunc->funccolcount = tupdesc->natts;
1886  rte->functions = lappend(rte->functions, rtfunc);
1887 
1888  /* Save the tupdesc for use below */
1889  functupdescs[funcno] = tupdesc;
1890  totalatts += tupdesc->natts;
1891  funcno++;
1892  }
1893 
1894  /*
1895  * If there's more than one function, or we want an ordinality column, we
1896  * have to produce a merged tupdesc.
1897  */
1898  if (nfuncs > 1 || rangefunc->ordinality)
1899  {
1900  if (rangefunc->ordinality)
1901  totalatts++;
1902 
1903  /* Merge the tuple descs of each function into a composite one */
1904  tupdesc = CreateTemplateTupleDesc(totalatts);
1905  natts = 0;
1906  for (i = 0; i < nfuncs; i++)
1907  {
1908  for (j = 1; j <= functupdescs[i]->natts; j++)
1909  TupleDescCopyEntry(tupdesc, ++natts, functupdescs[i], j);
1910  }
1911 
1912  /* Add the ordinality column if needed */
1913  if (rangefunc->ordinality)
1914  {
1915  TupleDescInitEntry(tupdesc,
1916  (AttrNumber) ++natts,
1917  "ordinality",
1918  INT8OID,
1919  -1,
1920  0);
1921  /* no need to set collation */
1922  }
1923 
1924  Assert(natts == totalatts);
1925  }
1926  else
1927  {
1928  /* We can just use the single function's tupdesc as-is */
1929  tupdesc = functupdescs[0];
1930  }
1931 
1932  /* Use the tupdesc while assigning column aliases for the RTE */
1933  buildRelationAliases(tupdesc, alias, eref);
1934 
1935  /*
1936  * Set flags and access permissions.
1937  *
1938  * Functions are never checked for access rights (at least, not by the RTE
1939  * permissions mechanism).
1940  */
1941  rte->lateral = lateral;
1942  rte->inh = false; /* never true for functions */
1943  rte->inFromCl = inFromCl;
1944 
1945  rte->requiredPerms = 0;
1946  rte->checkAsUser = InvalidOid;
1947  rte->selectedCols = NULL;
1948  rte->insertedCols = NULL;
1949  rte->updatedCols = NULL;
1950  rte->extraUpdatedCols = NULL;
1951 
1952  /*
1953  * Add completed RTE to pstate's range table list, so that we know its
1954  * index. But we don't add it to the join list --- caller must do that if
1955  * appropriate.
1956  */
1957  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1958 
1959  /*
1960  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1961  * list --- caller must do that if appropriate.
1962  */
1963  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1964  tupdesc);
1965 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:579
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1250
Alias * alias
Definition: parsenodes.h:1140
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
static char * chooseScalarFunctionAlias(Node *funcexpr, char *funcname, Alias *alias, int nfuncs)
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:491
char * pstrdup(const char *in)
Definition: mcxt.c:1299
Definition: nodes.h:539
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
AclMode requiredPerms
Definition: parsenodes.h:1145
bool funcordinality
Definition: parsenodes.h:1090
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:223
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
signed int int32
Definition: c.h:429
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1150
Bitmapset * selectedCols
Definition: parsenodes.h:1147
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:267
bool setof
Definition: parsenodes.h:223
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:754
int location
Definition: parsenodes.h:676
TypeFuncClass
Definition: funcapi.h:146
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
Oid GetColumnDefCollation(ParseState *pstate, ColumnDef *coldef, Oid typeOid)
Definition: parse_type.c:538
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:310
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1179
#define ereport(elevel,...)
Definition: elog.h:157
Bitmapset * updatedCols
Definition: parsenodes.h:1149
List * funccolcollations
Definition: parsenodes.h:1180
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1089
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
TypeName * typeName
Definition: parsenodes.h:659
Bitmapset * funcparams
Definition: parsenodes.h:1182
RTEKind rtekind
Definition: parsenodes.h:995
#define CHKATYPE_ANYRECORD
Definition: heap.h:24
Query * subquery
Definition: parsenodes.h:1030
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
Bitmapset * insertedCols
Definition: parsenodes.h:1148
int i
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
char * colname
Definition: parsenodes.h:658
Alias * eref
Definition: parsenodes.h:1141
void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, int flags)
Definition: heap.c:500
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
List * p_rtable
Definition: parse_node.h:182

◆ addRangeTableEntryForJoin()

ParseNamespaceItem* addRangeTableEntryForJoin ( ParseState pstate,
List colnames,
ParseNamespaceColumn nscolumns,
JoinType  jointype,
int  nummergedcols,
List aliasvars,
List leftcols,
List rightcols,
Alias joinalias,
Alias alias,
bool  inFromCl 
)

Definition at line 2135 of file parse_relation.c.

References RangeTblEntry::alias, Assert, RangeTblEntry::checkAsUser, Alias::colnames, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, RangeTblEntry::join_using_alias, RangeTblEntry::joinaliasvars, RangeTblEntry::joinleftcols, RangeTblEntry::joinmergedcols, RangeTblEntry::joinrightcols, RangeTblEntry::jointype, lappend(), RangeTblEntry::lateral, list_concat(), list_copy_tail(), list_length(), makeAlias(), makeNode, MaxAttrNumber, NIL, ParseNamespaceItem::p_cols_visible, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseNamespaceItem::p_rel_visible, ParseState::p_rtable, ParseNamespaceItem::p_rte, ParseNamespaceItem::p_rtindex, palloc(), RangeTblEntry::relid, RangeTblEntry::requiredPerms, RTE_JOIN, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, RangeTblEntry::subquery, and RangeTblEntry::updatedCols.

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

2146 {
2148  Alias *eref;
2149  int numaliases;
2150  ParseNamespaceItem *nsitem;
2151 
2152  Assert(pstate != NULL);
2153 
2154  /*
2155  * Fail if join has too many columns --- we must be able to reference any
2156  * of the columns with an AttrNumber.
2157  */
2158  if (list_length(aliasvars) > MaxAttrNumber)
2159  ereport(ERROR,
2160  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2161  errmsg("joins can have at most %d columns",
2162  MaxAttrNumber)));
2163 
2164  rte->rtekind = RTE_JOIN;
2165  rte->relid = InvalidOid;
2166  rte->subquery = NULL;
2167  rte->jointype = jointype;
2168  rte->joinmergedcols = nummergedcols;
2169  rte->joinaliasvars = aliasvars;
2170  rte->joinleftcols = leftcols;
2171  rte->joinrightcols = rightcols;
2172  rte->join_using_alias = join_using_alias;
2173  rte->alias = alias;
2174 
2175  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
2176  numaliases = list_length(eref->colnames);
2177 
2178  /* fill in any unspecified alias columns */
2179  if (numaliases < list_length(colnames))
2180  eref->colnames = list_concat(eref->colnames,
2181  list_copy_tail(colnames, numaliases));
2182 
2183  rte->eref = eref;
2184 
2185  /*
2186  * Set flags and access permissions.
2187  *
2188  * Joins are never checked for access rights.
2189  */
2190  rte->lateral = false;
2191  rte->inh = false; /* never true for joins */
2192  rte->inFromCl = inFromCl;
2193 
2194  rte->requiredPerms = 0;
2195  rte->checkAsUser = InvalidOid;
2196  rte->selectedCols = NULL;
2197  rte->insertedCols = NULL;
2198  rte->updatedCols = NULL;
2199  rte->extraUpdatedCols = NULL;
2200 
2201  /*
2202  * Add completed RTE to pstate's range table list, so that we know its
2203  * index. But we don't add it to the join list --- caller must do that if
2204  * appropriate.
2205  */
2206  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2207 
2208  /*
2209  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2210  * list --- caller must do that if appropriate.
2211  */
2212  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2213  nsitem->p_names = rte->eref;
2214  nsitem->p_rte = rte;
2215  nsitem->p_rtindex = list_length(pstate->p_rtable);
2216  nsitem->p_nscolumns = nscolumns;
2217  /* set default visibility flags; might get changed later */
2218  nsitem->p_rel_visible = true;
2219  nsitem->p_cols_visible = true;
2220  nsitem->p_lateral_only = false;
2221  nsitem->p_lateral_ok = true;
2222 
2223  return nsitem;
2224 }
#define NIL
Definition: pg_list.h:65
List * joinaliasvars
Definition: parsenodes.h:1070
Alias * alias
Definition: parsenodes.h:1140
List * colnames
Definition: primnodes.h:43
#define MaxAttrNumber
Definition: attnum.h:24
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
int errcode(int sqlerrcode)
Definition: elog.c:698
AclMode requiredPerms
Definition: parsenodes.h:1145
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1437
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
RangeTblEntry * p_rte
Definition: parse_node.h:268
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1150
Bitmapset * selectedCols
Definition: parsenodes.h:1147
#define ERROR
Definition: elog.h:46
JoinType jointype
Definition: parsenodes.h:1068
List * joinrightcols
Definition: parsenodes.h:1072
List * lappend(List *list, void *datum)
Definition: list.c:336
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
Bitmapset * updatedCols
Definition: parsenodes.h:1149
List * joinleftcols
Definition: parsenodes.h:1071
Alias * join_using_alias
Definition: parsenodes.h:1079
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:995
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:271
Query * subquery
Definition: parsenodes.h:1030
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
Bitmapset * insertedCols
Definition: parsenodes.h:1148
Alias * eref
Definition: parsenodes.h:1141
#define copyObject(obj)
Definition: nodes.h:655
List * p_rtable
Definition: parse_node.h:182

◆ addRangeTableEntryForRelation()

ParseNamespaceItem* addRangeTableEntryForRelation ( ParseState pstate,
Relation  rel,
int  lockmode,
Alias alias,
bool  inh,
bool  inFromCl 
)

Definition at line 1502 of file parse_relation.c.

References AccessShareLock, ACL_SELECT, RangeTblEntry::alias, Alias::aliasname, Assert, buildNSItemFromTupleDesc(), buildRelationAliases(), RangeTblEntry::checkAsUser, CheckRelationLockedByMe(), RangeTblEntry::eref, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), RangeTblEntry::lateral, list_length(), makeAlias(), makeNode, NIL, ParseState::p_rtable, RelationData::rd_att, RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::rellockmode, RangeTblEntry::requiredPerms, RowExclusiveLock, RowShareLock, RTE_RELATION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, and RangeTblEntry::updatedCols.

Referenced by AddRelationNewConstraints(), AlterPolicy(), copy_table(), CreatePolicy(), CreateTrigger(), DefineRelation(), DoCopy(), RemoveRoleFromObjectPolicy(), rewriteTargetView(), setTargetTable(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformIndexStmt(), transformOnConflictClause(), transformPartitionSpec(), transformRuleStmt(), transformStatsStmt(), and UpdateRangeTableOfViewParse().

1508 {
1510  char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1511 
1512  Assert(pstate != NULL);
1513 
1514  Assert(lockmode == AccessShareLock ||
1515  lockmode == RowShareLock ||
1516  lockmode == RowExclusiveLock);
1517  Assert(CheckRelationLockedByMe(rel, lockmode, true));
1518 
1519  rte->rtekind = RTE_RELATION;
1520  rte->alias = alias;
1521  rte->relid = RelationGetRelid(rel);
1522  rte->relkind = rel->rd_rel->relkind;
1523  rte->rellockmode = lockmode;
1524 
1525  /*
1526  * Build the list of effective column names using user-supplied aliases
1527  * and/or actual column names.
1528  */
1529  rte->eref = makeAlias(refname, NIL);
1530  buildRelationAliases(rel->rd_att, alias, rte->eref);
1531 
1532  /*
1533  * Set flags and access permissions.
1534  *
1535  * The initial default on access checks is always check-for-READ-access,
1536  * which is the right thing for all except target tables.
1537  */
1538  rte->lateral = false;
1539  rte->inh = inh;
1540  rte->inFromCl = inFromCl;
1541 
1542  rte->requiredPerms = ACL_SELECT;
1543  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1544  rte->selectedCols = NULL;
1545  rte->insertedCols = NULL;
1546  rte->updatedCols = NULL;
1547  rte->extraUpdatedCols = NULL;
1548 
1549  /*
1550  * Add completed RTE to pstate's range table list, so that we know its
1551  * index. But we don't add it to the join list --- caller must do that if
1552  * appropriate.
1553  */
1554  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1555 
1556  /*
1557  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1558  * list --- caller must do that if appropriate.
1559  */
1560  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1561  rel->rd_att);
1562 }
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1140
#define AccessShareLock
Definition: lockdefs.h:36
AclMode requiredPerms
Definition: parsenodes.h:1145
Form_pg_class rd_rel
Definition: rel.h:109
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1150
Bitmapset * selectedCols
Definition: parsenodes.h:1147
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
#define RowExclusiveLock
Definition: lockdefs.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:511
List * lappend(List *list, void *datum)
Definition: list.c:336
#define RowShareLock
Definition: lockdefs.h:37
#define ACL_SELECT
Definition: parsenodes.h:83
TupleDesc rd_att
Definition: rel.h:110
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1149
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:303
RTEKind rtekind
Definition: parsenodes.h:995
Bitmapset * insertedCols
Definition: parsenodes.h:1148
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * eref
Definition: parsenodes.h:1141
#define RelationGetRelid(relation)
Definition: rel.h:477
List * p_rtable
Definition: parse_node.h:182

◆ addRangeTableEntryForSubquery()

ParseNamespaceItem* addRangeTableEntryForSubquery ( ParseState pstate,
Query subquery,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1572 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, Assert, buildNSItemFromLists(), RangeTblEntry::checkAsUser, Alias::colnames, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, list_length(), makeNode, makeString(), NIL, ParseState::p_rtable, pstrdup(), RangeTblEntry::requiredPerms, TargetEntry::resjunk, TargetEntry::resname, TargetEntry::resno, RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, RangeTblEntry::subquery, Query::targetList, and RangeTblEntry::updatedCols.

Referenced by convert_ANY_sublink_to_join(), transformInsertStmt(), transformRangeSubselect(), and transformSetOperationTree().

1577 {
1579  char *refname = alias->aliasname;
1580  Alias *eref;
1581  int numaliases;
1582  List *coltypes,
1583  *coltypmods,
1584  *colcollations;
1585  int varattno;
1586  ListCell *tlistitem;
1587 
1588  Assert(pstate != NULL);
1589 
1590  rte->rtekind = RTE_SUBQUERY;
1591  rte->subquery = subquery;
1592  rte->alias = alias;
1593 
1594  eref = copyObject(alias);
1595  numaliases = list_length(eref->colnames);
1596 
1597  /* fill in any unspecified alias columns, and extract column type info */
1598  coltypes = coltypmods = colcollations = NIL;
1599  varattno = 0;
1600  foreach(tlistitem, subquery->targetList)
1601  {
1602  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1603 
1604  if (te->resjunk)
1605  continue;
1606  varattno++;
1607  Assert(varattno == te->resno);
1608  if (varattno > numaliases)
1609  {
1610  char *attrname;
1611 
1612  attrname = pstrdup(te->resname);
1613  eref->colnames = lappend(eref->colnames, makeString(attrname));
1614  }
1615  coltypes = lappend_oid(coltypes,
1616  exprType((Node *) te->expr));
1617  coltypmods = lappend_int(coltypmods,
1618  exprTypmod((Node *) te->expr));
1619  colcollations = lappend_oid(colcollations,
1620  exprCollation((Node *) te->expr));
1621  }
1622  if (varattno < numaliases)
1623  ereport(ERROR,
1624  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1625  errmsg("table \"%s\" has %d columns available but %d columns specified",
1626  refname, varattno, numaliases)));
1627 
1628  rte->eref = eref;
1629 
1630  /*
1631  * Set flags and access permissions.
1632  *
1633  * Subqueries are never checked for access rights.
1634  */
1635  rte->lateral = lateral;
1636  rte->inh = false; /* never true for subqueries */
1637  rte->inFromCl = inFromCl;
1638 
1639  rte->requiredPerms = 0;
1640  rte->checkAsUser = InvalidOid;
1641  rte->selectedCols = NULL;
1642  rte->insertedCols = NULL;
1643  rte->updatedCols = NULL;
1644  rte->extraUpdatedCols = NULL;
1645 
1646  /*
1647  * Add completed RTE to pstate's range table list, so that we know its
1648  * index. But we don't add it to the join list --- caller must do that if
1649  * appropriate.
1650  */
1651  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1652 
1653  /*
1654  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1655  * list --- caller must do that if appropriate.
1656  */
1657  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
1658  coltypes, coltypmods, colcollations);
1659 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1140
List * colnames
Definition: primnodes.h:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
char * pstrdup(const char *in)
Definition: mcxt.c:1299
Definition: nodes.h:539
int errcode(int sqlerrcode)
Definition: elog.c:698
AclMode requiredPerms
Definition: parsenodes.h:1145
char * resname
Definition: primnodes.h:1446
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
List * targetList
Definition: parsenodes.h:150
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1150
Bitmapset * selectedCols
Definition: parsenodes.h:1147
bool resjunk
Definition: primnodes.h:1451
#define ERROR
Definition: elog.h:46
AttrNumber resno
Definition: primnodes.h:1445
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
Bitmapset * updatedCols
Definition: parsenodes.h:1149
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
Expr * expr
Definition: primnodes.h:1444
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
RTEKind rtekind
Definition: parsenodes.h:995
Query * subquery
Definition: parsenodes.h:1030
int errmsg(const char *fmt,...)
Definition: elog.c:909
Bitmapset * insertedCols
Definition: parsenodes.h:1148
Alias * eref
Definition: parsenodes.h:1141
#define copyObject(obj)
Definition: nodes.h:655
Definition: pg_list.h:50
List * p_rtable
Definition: parse_node.h:182

◆ addRangeTableEntryForTableFunc()

ParseNamespaceItem* addRangeTableEntryForTableFunc ( ParseState pstate,
TableFunc tf,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1974 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, Assert, buildNSItemFromLists(), RangeTblEntry::checkAsUser, TableFunc::colcollations, RangeTblEntry::colcollations, Alias::colnames, TableFunc::colnames, TableFunc::coltypes, RangeTblEntry::coltypes, TableFunc::coltypmods, RangeTblEntry::coltypmods, copyObject, RangeTblEntry::eref, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), RangeTblEntry::lateral, list_concat(), list_copy_tail(), list_length(), makeAlias(), makeNode, NIL, ParseState::p_rtable, pstrdup(), RangeTblEntry::relid, RangeTblEntry::requiredPerms, RTE_TABLEFUNC, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, RangeTblEntry::subquery, RangeTblEntry::tablefunc, and RangeTblEntry::updatedCols.

Referenced by transformRangeTableFunc().

1979 {
1981  char *refname = alias ? alias->aliasname : pstrdup("xmltable");
1982  Alias *eref;
1983  int numaliases;
1984 
1985  Assert(pstate != NULL);
1986 
1987  rte->rtekind = RTE_TABLEFUNC;
1988  rte->relid = InvalidOid;
1989  rte->subquery = NULL;
1990  rte->tablefunc = tf;
1991  rte->coltypes = tf->coltypes;
1992  rte->coltypmods = tf->coltypmods;
1993  rte->colcollations = tf->colcollations;
1994  rte->alias = alias;
1995 
1996  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1997  numaliases = list_length(eref->colnames);
1998 
1999  /* fill in any unspecified alias columns */
2000  if (numaliases < list_length(tf->colnames))
2001  eref->colnames = list_concat(eref->colnames,
2002  list_copy_tail(tf->colnames, numaliases));
2003 
2004  rte->eref = eref;
2005 
2006  /*
2007  * Set flags and access permissions.
2008  *
2009  * Tablefuncs are never checked for access rights (at least, not by the
2010  * RTE permissions mechanism).
2011  */
2012  rte->lateral = lateral;
2013  rte->inh = false; /* never true for tablefunc RTEs */
2014  rte->inFromCl = inFromCl;
2015 
2016  rte->requiredPerms = 0;
2017  rte->checkAsUser = InvalidOid;
2018  rte->selectedCols = NULL;
2019  rte->insertedCols = NULL;
2020  rte->updatedCols = NULL;
2021  rte->extraUpdatedCols = NULL;
2022 
2023  /*
2024  * Add completed RTE to pstate's range table list, so that we know its
2025  * index. But we don't add it to the join list --- caller must do that if
2026  * appropriate.
2027  */
2028  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2029 
2030  /*
2031  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2032  * list --- caller must do that if appropriate.
2033  */
2034  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2035  rte->coltypes, rte->coltypmods,
2036  rte->colcollations);
2037 }
#define NIL
Definition: pg_list.h:65
List * colnames
Definition: primnodes.h:89
Alias * alias
Definition: parsenodes.h:1140
List * colnames
Definition: primnodes.h:43
List * coltypmods
Definition: primnodes.h:91
char * pstrdup(const char *in)
Definition: mcxt.c:1299
List * coltypmods
Definition: parsenodes.h:1128
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
AclMode requiredPerms
Definition: parsenodes.h:1145
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1437
List * colcollations
Definition: parsenodes.h:1129
List * colcollations
Definition: primnodes.h:92
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1150
Bitmapset * selectedCols
Definition: parsenodes.h:1147
TableFunc * tablefunc
Definition: parsenodes.h:1095
List * lappend(List *list, void *datum)
Definition: list.c:336
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
List * coltypes
Definition: primnodes.h:90
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1149
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:995
Query * subquery
Definition: parsenodes.h:1030
Bitmapset * insertedCols
Definition: parsenodes.h:1148
Alias * eref
Definition: parsenodes.h:1141
#define copyObject(obj)
Definition: nodes.h:655
List * coltypes
Definition: parsenodes.h:1127
List * p_rtable
Definition: parse_node.h:182

◆ addRangeTableEntryForValues()

ParseNamespaceItem* addRangeTableEntryForValues ( ParseState pstate,
List exprs,
List coltypes,
List coltypmods,
List colcollations,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 2046 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, Assert, buildNSItemFromLists(), RangeTblEntry::checkAsUser, RangeTblEntry::colcollations, Alias::colnames, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), RangeTblEntry::lateral, linitial, list_length(), makeAlias(), makeNode, makeString(), NIL, ParseState::p_rtable, pstrdup(), RangeTblEntry::relid, RangeTblEntry::requiredPerms, RTE_VALUES, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, snprintf, RangeTblEntry::subquery, RangeTblEntry::updatedCols, and RangeTblEntry::values_lists.

Referenced by transformInsertStmt(), and transformValuesClause().

2054 {
2056  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
2057  Alias *eref;
2058  int numaliases;
2059  int numcolumns;
2060 
2061  Assert(pstate != NULL);
2062 
2063  rte->rtekind = RTE_VALUES;
2064  rte->relid = InvalidOid;
2065  rte->subquery = NULL;
2066  rte->values_lists = exprs;
2067  rte->coltypes = coltypes;
2068  rte->coltypmods = coltypmods;
2069  rte->colcollations = colcollations;
2070  rte->alias = alias;
2071 
2072  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2073 
2074  /* fill in any unspecified alias columns */
2075  numcolumns = list_length((List *) linitial(exprs));
2076  numaliases = list_length(eref->colnames);
2077  while (numaliases < numcolumns)
2078  {
2079  char attrname[64];
2080 
2081  numaliases++;
2082  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
2083  eref->colnames = lappend(eref->colnames,
2084  makeString(pstrdup(attrname)));
2085  }
2086  if (numcolumns < numaliases)
2087  ereport(ERROR,
2088  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2089  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
2090  refname, numcolumns, numaliases)));
2091 
2092  rte->eref = eref;
2093 
2094  /*
2095  * Set flags and access permissions.
2096  *
2097  * Subqueries are never checked for access rights.
2098  */
2099  rte->lateral = lateral;
2100  rte->inh = false; /* never true for values RTEs */
2101  rte->inFromCl = inFromCl;
2102 
2103  rte->requiredPerms = 0;
2104  rte->checkAsUser = InvalidOid;
2105  rte->selectedCols = NULL;
2106  rte->insertedCols = NULL;
2107  rte->updatedCols = NULL;
2108  rte->extraUpdatedCols = NULL;
2109 
2110  /*
2111  * Add completed RTE to pstate's range table list, so that we know its
2112  * index. But we don't add it to the join list --- caller must do that if
2113  * appropriate.
2114  */
2115  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2116 
2117  /*
2118  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2119  * list --- caller must do that if appropriate.
2120  */
2121  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2122  rte->coltypes, rte->coltypmods,
2123  rte->colcollations);
2124 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1140
List * colnames
Definition: primnodes.h:43
char * pstrdup(const char *in)
Definition: mcxt.c:1299
List * coltypmods
Definition: parsenodes.h:1128
int errcode(int sqlerrcode)
Definition: elog.c:698
AclMode requiredPerms
Definition: parsenodes.h:1145
List * colcollations
Definition: parsenodes.h:1129
List * values_lists
Definition: parsenodes.h:1100
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1150
Bitmapset * selectedCols
Definition: parsenodes.h:1147
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
List * lappend(List *list, void *datum)
Definition: list.c:336
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
Bitmapset * updatedCols
Definition: parsenodes.h:1149
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:995
Query * subquery
Definition: parsenodes.h:1030
int errmsg(const char *fmt,...)
Definition: elog.c:909
Bitmapset * insertedCols
Definition: parsenodes.h:1148
Alias * eref
Definition: parsenodes.h:1141
#define copyObject(obj)
Definition: nodes.h:655
List * coltypes
Definition: parsenodes.h:1127
Definition: pg_list.h:50
#define snprintf
Definition: port.h:216
List * p_rtable
Definition: parse_node.h:182

◆ attnameAttNum()

int attnameAttNum ( Relation  rd,
const char *  attname,
bool  sysColOK 
)

Definition at line 3383 of file parse_relation.c.

References i, InvalidAttrNumber, namestrcmp(), RelationData::rd_att, RelationGetNumberOfAttributes, specialAttNum(), and TupleDescAttr.

Referenced by checkInsertTargets(), CreateTrigger(), do_analyze_rel(), and transformUpdateTargetList().

3384 {
3385  int i;
3386 
3387  for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3388  {
3389  Form_pg_attribute att = TupleDescAttr(rd->rd_att, i);
3390 
3391  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3392  return i + 1;
3393  }
3394 
3395  if (sysColOK)
3396  {
3397  if ((i = specialAttNum(attname)) != InvalidAttrNumber)
3398  return i;
3399  }
3400 
3401  /* on failure */
3402  return InvalidAttrNumber;
3403 }
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:483
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int namestrcmp(Name name, const char *str)
Definition: name.c:247
static int specialAttNum(const char *attname)
NameData attname
Definition: pg_attribute.h:41
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
TupleDesc rd_att
Definition: rel.h:110
#define InvalidAttrNumber
Definition: attnum.h:23
int i

◆ attnumAttName()

const NameData* attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3433 of file parse_relation.c.

References elog, ERROR, FormData_pg_attribute, TupleDescData::natts, RelationData::rd_att, SystemAttributeDefinition(), and TupleDescAttr.

Referenced by transformFkeyGetPrimaryKey().

3434 {
3435  if (attid <= 0)
3436  {
3437  const FormData_pg_attribute *sysatt;
3438 
3439  sysatt = SystemAttributeDefinition(attid);
3440  return &sysatt->attname;
3441  }
3442  if (attid > rd->rd_att->natts)
3443  elog(ERROR, "invalid attribute number %d", attid);
3444  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3445 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:251
#define ERROR
Definition: elog.h:46
TupleDesc rd_att
Definition: rel.h:110
FormData_pg_attribute
Definition: pg_attribute.h:191
#define elog(elevel,...)
Definition: elog.h:232

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3475 of file parse_relation.c.

References elog, ERROR, InvalidOid, TupleDescData::natts, RelationData::rd_att, and TupleDescAttr.

3476 {
3477  if (attid <= 0)
3478  {
3479  /* All system attributes are of noncollatable types. */
3480  return InvalidOid;
3481  }
3482  if (attid > rd->rd_att->natts)
3483  elog(ERROR, "invalid attribute number %d", attid);
3484  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3485 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define ERROR
Definition: elog.h:46
TupleDesc rd_att
Definition: rel.h:110
#define InvalidOid
Definition: postgres_ext.h:36
#define elog(elevel,...)
Definition: elog.h:232

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3455 of file parse_relation.c.

References elog, ERROR, FormData_pg_attribute, TupleDescData::natts, RelationData::rd_att, SystemAttributeDefinition(), and TupleDescAttr.

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

3456 {
3457  if (attid <= 0)
3458  {
3459  const FormData_pg_attribute *sysatt;
3460 
3461  sysatt = SystemAttributeDefinition(attid);
3462  return sysatt->atttypid;
3463  }
3464  if (attid > rd->rd_att->natts)
3465  elog(ERROR, "invalid attribute number %d", attid);
3466  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3467 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:251
#define ERROR
Definition: elog.h:46
TupleDesc rd_att
Definition: rel.h:110
FormData_pg_attribute
Definition: pg_attribute.h:191
#define elog(elevel,...)
Definition: elog.h:232

◆ checkNameSpaceConflicts()

void checkNameSpaceConflicts ( ParseState pstate,
List namespace1,
List namespace2 
)

Definition at line 414 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, ereport, errcode(), errmsg(), ERROR, lfirst, ParseNamespaceItem::p_names, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_rte, RangeTblEntry::relid, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by transformFromClause(), and transformFromClauseItem().

416 {
417  ListCell *l1;
418 
419  foreach(l1, namespace1)
420  {
421  ParseNamespaceItem *nsitem1 = (ParseNamespaceItem *) lfirst(l1);
422  RangeTblEntry *rte1 = nsitem1->p_rte;
423  const char *aliasname1 = nsitem1->p_names->aliasname;
424  ListCell *l2;
425 
426  if (!nsitem1->p_rel_visible)
427  continue;
428 
429  foreach(l2, namespace2)
430  {
431  ParseNamespaceItem *nsitem2 = (ParseNamespaceItem *) lfirst(l2);
432  RangeTblEntry *rte2 = nsitem2->p_rte;
433  const char *aliasname2 = nsitem2->p_names->aliasname;
434 
435  if (!nsitem2->p_rel_visible)
436  continue;
437  if (strcmp(aliasname2, aliasname1) != 0)
438  continue; /* definitely no conflict */
439  if (rte1->rtekind == RTE_RELATION && rte1->alias == NULL &&
440  rte2->rtekind == RTE_RELATION && rte2->alias == NULL &&
441  rte1->relid != rte2->relid)
442  continue; /* no conflict per SQL rule */
443  ereport(ERROR,
444  (errcode(ERRCODE_DUPLICATE_ALIAS),
445  errmsg("table name \"%s\" specified more than once",
446  aliasname1)));
447  }
448  }
449 }
Alias * alias
Definition: parsenodes.h:1140
int errcode(int sqlerrcode)
Definition: elog.c:698
RangeTblEntry * p_rte
Definition: parse_node.h:268
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
RTEKind rtekind
Definition: parsenodes.h:995
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ colNameToVar()

Node* colNameToVar ( ParseState pstate,
const char *  colname,
bool  localonly,
int  location 
)

Definition at line 857 of file parse_relation.c.

References check_lateral_ref_ok(), ereport, errcode(), errmsg(), ERROR, lfirst, ParseNamespaceItem::p_cols_visible, ParseState::p_lateral_active, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, ParseState::parentParseState, parser_errposition(), and scanNSItemForColumn().

Referenced by findTargetlistEntrySQL92(), and transformColumnRef().

859 {
860  Node *result = NULL;
861  int sublevels_up = 0;
862  ParseState *orig_pstate = pstate;
863 
864  while (pstate != NULL)
865  {
866  ListCell *l;
867 
868  foreach(l, pstate->p_namespace)
869  {
871  Node *newresult;
872 
873  /* Ignore table-only items */
874  if (!nsitem->p_cols_visible)
875  continue;
876  /* If not inside LATERAL, ignore lateral-only items */
877  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
878  continue;
879 
880  /* use orig_pstate here for consistency with other callers */
881  newresult = scanNSItemForColumn(orig_pstate, nsitem, sublevels_up,
882  colname, location);
883 
884  if (newresult)
885  {
886  if (result)
887  ereport(ERROR,
888  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
889  errmsg("column reference \"%s\" is ambiguous",
890  colname),
891  parser_errposition(pstate, location)));
892  check_lateral_ref_ok(pstate, nsitem, location);
893  result = newresult;
894  }
895  }
896 
897  if (result != NULL || localonly)
898  break; /* found, or don't want to look at parent */
899 
900  pstate = pstate->parentParseState;
901  sublevels_up++;
902  }
903 
904  return result;
905 }
ParseState * parentParseState
Definition: parse_node.h:180
Definition: nodes.h:539
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem, int location)
List * p_namespace
Definition: parse_node.h:186
#define ereport(elevel,...)
Definition: elog.h:157
bool p_lateral_active
Definition: parse_node.h:188
#define lfirst(lc)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ errorMissingColumn()

void errorMissingColumn ( ParseState pstate,
const char *  relname,
const char *  colname,
int  location 
)

Definition at line 3555 of file parse_relation.c.

References Alias::aliasname, AttributeNumberIsValid, Alias::colnames, RangeTblEntry::eref, ereport, errcode(), errhint(), errmsg(), ERROR, FuzzyAttrMatchState::first, list_nth(), parser_errposition(), FuzzyAttrMatchState::rfirst, FuzzyAttrMatchState::rsecond, searchRangeTableForCol(), FuzzyAttrMatchState::second, and strVal.

Referenced by transformColumnRef().

3557 {
3559  char *closestfirst = NULL;
3560 
3561  /*
3562  * Search the entire rtable looking for possible matches. If we find one,
3563  * emit a hint about it.
3564  *
3565  * TODO: improve this code (and also errorMissingRTE) to mention using
3566  * LATERAL if appropriate.
3567  */
3568  state = searchRangeTableForCol(pstate, relname, colname, location);
3569 
3570  /*
3571  * Extract closest col string for best match, if any.
3572  *
3573  * Infer an exact match referenced despite not being visible from the fact
3574  * that an attribute number was not present in state passed back -- this
3575  * is what is reported when !closestfirst. There might also be an exact
3576  * match that was qualified with an incorrect alias, in which case
3577  * closestfirst will be set (so hint is the same as generic fuzzy case).
3578  */
3579  if (state->rfirst && AttributeNumberIsValid(state->first))
3580  closestfirst = strVal(list_nth(state->rfirst->eref->colnames,
3581  state->first - 1));
3582 
3583  if (!state->rsecond)
3584  {
3585  /*
3586  * Handle case where there is zero or one column suggestions to hint,
3587  * including exact matches referenced but not visible.
3588  */
3589  ereport(ERROR,
3590  (errcode(ERRCODE_UNDEFINED_COLUMN),
3591  relname ?
3592  errmsg("column %s.%s does not exist", relname, colname) :
3593  errmsg("column \"%s\" does not exist", colname),
3594  state->rfirst ? closestfirst ?
3595  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3596  state->rfirst->eref->aliasname, closestfirst) :
3597  errhint("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3598  colname, state->rfirst->eref->aliasname) : 0,
3599  parser_errposition(pstate, location)));
3600  }
3601  else
3602  {
3603  /* Handle case where there are two equally useful column hints */
3604  char *closestsecond;
3605 
3606  closestsecond = strVal(list_nth(state->rsecond->eref->colnames,
3607  state->second - 1));
3608 
3609  ereport(ERROR,
3610  (errcode(ERRCODE_UNDEFINED_COLUMN),
3611  relname ?
3612  errmsg("column %s.%s does not exist", relname, colname) :
3613  errmsg("column \"%s\" does not exist", colname),
3614  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3615  state->rfirst->eref->aliasname, closestfirst,
3616  state->rsecond->eref->aliasname, closestsecond),
3617  parser_errposition(pstate, location)));
3618  }
3619 }
int errhint(const char *fmt,...)
Definition: elog.c:1156
List * colnames
Definition: primnodes.h:43
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colname, int location)
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:698
NameData relname
Definition: pg_class.h:38
#define ERROR
Definition: elog.h:46
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
RangeTblEntry * rfirst
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
#define ereport(elevel,...)
Definition: elog.h:157
char * aliasname
Definition: primnodes.h:42
Definition: regguts.h:317
RangeTblEntry * rsecond
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:909
Alias * eref
Definition: parsenodes.h:1141

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3494 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, RangeTblEntry::eref, ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errhint(), errmsg(), ERROR, RangeVar::location, ParseNamespaceItem::p_rte, parser_errposition(), refnameNamespaceItem(), RangeVar::relname, and searchRangeTableForRel().

Referenced by ExpandColumnRefStar(), and transformColumnRef().

3495 {
3496  RangeTblEntry *rte;
3497  const char *badAlias = NULL;
3498 
3499  /*
3500  * Check to see if there are any potential matches in the query's
3501  * rangetable. (Note: cases involving a bad schema name in the RangeVar
3502  * will throw error immediately here. That seems OK.)
3503  */
3504  rte = searchRangeTableForRel(pstate, relation);
3505 
3506  /*
3507  * If we found a match that has an alias and the alias is visible in the
3508  * namespace, then the problem is probably use of the relation's real name
3509  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3510  * common enough to justify a specific hint.
3511  *
3512  * If we found a match that doesn't meet those criteria, assume the
3513  * problem is illegal use of a relation outside its scope, as in the
3514  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3515  */
3516  if (rte && rte->alias &&
3517  strcmp(rte->eref->aliasname, relation->relname) != 0)
3518  {
3519  ParseNamespaceItem *nsitem;
3520  int sublevels_up;
3521 
3522  nsitem = refnameNamespaceItem(pstate, NULL, rte->eref->aliasname,
3523  relation->location,
3524  &sublevels_up);
3525  if (nsitem && nsitem->p_rte == rte)
3526  badAlias = rte->eref->aliasname;
3527  }
3528 
3529  if (rte)
3530  ereport(ERROR,
3532  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3533  relation->relname),
3534  (badAlias ?
3535  errhint("Perhaps you meant to reference the table alias \"%s\".",
3536  badAlias) :
3537  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3538  rte->eref->aliasname)),
3539  parser_errposition(pstate, relation->location)));
3540  else
3541  ereport(ERROR,
3543  errmsg("missing FROM-clause entry for table \"%s\"",
3544  relation->relname),
3545  parser_errposition(pstate, relation->location)));
3546 }
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:77
Alias * alias
Definition: parsenodes.h:1140
int errcode(int sqlerrcode)
Definition: elog.c:698
int location
Definition: primnodes.h:73
char * relname
Definition: primnodes.h:68
RangeTblEntry * p_rte
Definition: parse_node.h:268
static RangeTblEntry * searchRangeTableForRel(ParseState *pstate, RangeVar *relation)
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
char * aliasname
Definition: primnodes.h:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:909
Alias * eref
Definition: parsenodes.h:1141

◆ expandNSItemAttrs()

List* expandNSItemAttrs ( ParseState pstate,
ParseNamespaceItem nsitem,
int  sublevels_up,
int  location 
)

Definition at line 3101 of file parse_relation.c.

References ACL_SELECT, Assert, expandNSItemVars(), forboth, label, lappend(), lfirst, makeTargetEntry(), markVarForSelectPriv(), name, NIL, ParseState::p_next_resno, ParseNamespaceItem::p_rte, RangeTblEntry::requiredPerms, RTE_RELATION, RangeTblEntry::rtekind, and strVal.

Referenced by ExpandAllTables(), ExpandSingleTable(), and transformValuesClause().

3103 {
3104  RangeTblEntry *rte = nsitem->p_rte;
3105  List *names,
3106  *vars;
3107  ListCell *name,
3108  *var;
3109  List *te_list = NIL;
3110 
3111  vars = expandNSItemVars(nsitem, sublevels_up, location, &names);
3112 
3113  /*
3114  * Require read access to the table. This is normally redundant with the
3115  * markVarForSelectPriv calls below, but not if the table has zero
3116  * columns. We need not do anything if the nsitem is for a join: its
3117  * component tables will have been marked ACL_SELECT when they were added
3118  * to the rangetable. (This step changes things only for the target
3119  * relation of UPDATE/DELETE, which cannot be under a join.)
3120  */
3121  if (rte->rtekind == RTE_RELATION)
3122  rte->requiredPerms |= ACL_SELECT;
3123 
3124  forboth(name, names, var, vars)
3125  {
3126  char *label = strVal(lfirst(name));
3127  Var *varnode = (Var *) lfirst(var);
3128  TargetEntry *te;
3129 
3130  te = makeTargetEntry((Expr *) varnode,
3131  (AttrNumber) pstate->p_next_resno++,
3132  label,
3133  false);
3134  te_list = lappend(te_list, te);
3135 
3136  /* Require read access to each column */
3137  markVarForSelectPriv(pstate, varnode);
3138  }
3139 
3140  Assert(name == NULL && var == NULL); /* lists not the same length? */
3141 
3142  return te_list;
3143 }
#define NIL
Definition: pg_list.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
void markVarForSelectPriv(ParseState *pstate, Var *var)
#define strVal(v)
Definition: value.h:54
AclMode requiredPerms
Definition: parsenodes.h:1145
Definition: primnodes.h:186
RangeTblEntry * p_rte
Definition: parse_node.h:268
int p_next_resno
Definition: parse_node.h:197
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
List * lappend(List *list, void *datum)
Definition: list.c:336
#define ACL_SELECT
Definition: parsenodes.h:83
static char * label
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * expandNSItemVars(ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
const char * name
Definition: encode.c:515
RTEKind rtekind
Definition: parsenodes.h:995
Definition: regcomp.c:237
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21

◆ expandNSItemVars()

List* expandNSItemVars ( ParseNamespaceItem nsitem,
int  sublevels_up,
int  location,
List **  colnames 
)

Definition at line 3042 of file parse_relation.c.

References Assert, Alias::colnames, lappend(), lfirst, Var::location, makeVar(), NIL, ParseNamespaceColumn::p_dontexpand, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseNamespaceColumn::p_varattno, ParseNamespaceColumn::p_varattnosyn, ParseNamespaceColumn::p_varcollid, ParseNamespaceColumn::p_varno, ParseNamespaceColumn::p_varnosyn, ParseNamespaceColumn::p_vartype, ParseNamespaceColumn::p_vartypmod, strVal, Var::varattnosyn, and Var::varnosyn.

Referenced by coerce_record_to_complex(), expandNSItemAttrs(), ExpandSingleTable(), and transformInsertStmt().

3045 {
3046  List *result = NIL;
3047  int colindex;
3048  ListCell *lc;
3049 
3050  if (colnames)
3051  *colnames = NIL;
3052  colindex = 0;
3053  foreach(lc, nsitem->p_names->colnames)
3054  {
3055  Value *colnameval = (Value *) lfirst(lc);
3056  const char *colname = strVal(colnameval);
3057  ParseNamespaceColumn *nscol = nsitem->p_nscolumns + colindex;
3058 
3059  if (nscol->p_dontexpand)
3060  {
3061  /* skip */
3062  }
3063  else if (colname[0])
3064  {
3065  Var *var;
3066 
3067  Assert(nscol->p_varno > 0);
3068  var = makeVar(nscol->p_varno,
3069  nscol->p_varattno,
3070  nscol->p_vartype,
3071  nscol->p_vartypmod,
3072  nscol->p_varcollid,
3073  sublevels_up);
3074  /* makeVar doesn't offer parameters for these, so set by hand: */
3075  var->varnosyn = nscol->p_varnosyn;
3076  var->varattnosyn = nscol->p_varattnosyn;
3077  var->location = location;
3078  result = lappend(result, var);
3079  if (colnames)
3080  *colnames = lappend(*colnames, colnameval);
3081  }
3082  else
3083  {
3084  /* dropped column, ignore */
3085  Assert(nscol->p_varno == 0);
3086  }
3087  colindex++;
3088  }
3089  return result;
3090 }
#define NIL
Definition: pg_list.h:65
List * colnames
Definition: primnodes.h:43
#define strVal(v)
Definition: value.h:54
Definition: primnodes.h:186
int location
Definition: primnodes.h:201
AttrNumber p_varattno
Definition: parse_node.h:302
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
Index varnosyn
Definition: primnodes.h:199
List * lappend(List *list, void *datum)
Definition: list.c:336
AttrNumber varattnosyn
Definition: primnodes.h:200
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Definition: value.h:42
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:271
AttrNumber p_varattnosyn
Definition: parse_node.h:307
Definition: pg_list.h:50

◆ expandRTE()

void expandRTE ( RangeTblEntry rte,
int  rtindex,
int  sublevels_up,
int  location,
bool  include_dropped,
List **  colnames,
List **  colvars 
)

Definition at line 2584 of file parse_relation.c.

References Assert, attnum, RangeTblEntry::colcollations, Alias::colnames, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, copyObject, elog, RangeTblEntry::eref, ERROR, expandRelation(), expandTupleDesc(), TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), forboth, forthree, RangeTblFunction::funccolcollations, RangeTblFunction::funccolcount, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), InvalidOid, IsA, RangeTblEntry::joinaliasvars, label, lappend(), lfirst, lfirst_int, lfirst_oid, list_concat(), list_copy_tail(), list_head(), list_length(), list_nth(), list_truncate(), llast, lnext(), Var::location, makeNullConst(), makeString(), makeVar(), NIL, OidIsValid, pstrdup(), RangeTblEntry::relid, TargetEntry::resjunk, TargetEntry::resno, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, strVal, RangeTblEntry::subquery, Query::targetList, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, TYPEFUNC_SCALAR, and Var::varlevelsup.

Referenced by build_physical_tlist(), expandRecordVariable(), pullup_replace_vars_callback(), ReplaceVarsFromTargetList_callback(), and transformWholeRowRef().

2587 {
2588  int varattno;
2589 
2590  if (colnames)
2591  *colnames = NIL;
2592  if (colvars)
2593  *colvars = NIL;
2594 
2595  switch (rte->rtekind)
2596  {
2597  case RTE_RELATION:
2598  /* Ordinary relation RTE */
2599  expandRelation(rte->relid, rte->eref,
2600  rtindex, sublevels_up, location,
2601  include_dropped, colnames, colvars);
2602  break;
2603  case RTE_SUBQUERY:
2604  {
2605  /* Subquery RTE */
2606  ListCell *aliasp_item = list_head(rte->eref->colnames);
2607  ListCell *tlistitem;
2608 
2609  varattno = 0;
2610  foreach(tlistitem, rte->subquery->targetList)
2611  {
2612  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
2613 
2614  if (te->resjunk)
2615  continue;
2616  varattno++;
2617  Assert(varattno == te->resno);
2618 
2619  /*
2620  * In scenarios where columns have been added to a view
2621  * since the outer query was originally parsed, there can
2622  * be more items in the subquery tlist than the outer
2623  * query expects. We should ignore such extra column(s)
2624  * --- compare the behavior for composite-returning
2625  * functions, in the RTE_FUNCTION case below.
2626  */
2627  if (!aliasp_item)
2628  break;
2629 
2630  if (colnames)
2631  {
2632  char *label = strVal(lfirst(aliasp_item));
2633 
2634  *colnames = lappend(*colnames, makeString(pstrdup(label)));
2635  }
2636 
2637  if (colvars)
2638  {
2639  Var *varnode;
2640 
2641  varnode = makeVar(rtindex, varattno,
2642  exprType((Node *) te->expr),
2643  exprTypmod((Node *) te->expr),
2644  exprCollation((Node *) te->expr),
2645  sublevels_up);
2646  varnode->location = location;
2647 
2648  *colvars = lappend(*colvars, varnode);
2649  }
2650 
2651  aliasp_item = lnext(rte->eref->colnames, aliasp_item);
2652  }
2653  }
2654  break;
2655  case RTE_FUNCTION:
2656  {
2657  /* Function RTE */
2658  int atts_done = 0;
2659  ListCell *lc;
2660 
2661  foreach(lc, rte->functions)
2662  {
2663  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2664  TypeFuncClass functypclass;
2665  Oid funcrettype;
2666  TupleDesc tupdesc;
2667 
2668  functypclass = get_expr_result_type(rtfunc->funcexpr,
2669  &funcrettype,
2670  &tupdesc);
2671  if (functypclass == TYPEFUNC_COMPOSITE ||
2672  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
2673  {
2674  /* Composite data type, e.g. a table's row type */
2675  Assert(tupdesc);
2676  expandTupleDesc(tupdesc, rte->eref,
2677  rtfunc->funccolcount, atts_done,
2678  rtindex, sublevels_up, location,
2679  include_dropped, colnames, colvars);
2680  }
2681  else if (functypclass == TYPEFUNC_SCALAR)
2682  {
2683  /* Base data type, i.e. scalar */
2684  if (colnames)
2685  *colnames = lappend(*colnames,
2686  list_nth(rte->eref->colnames,
2687  atts_done));
2688 
2689  if (colvars)
2690  {
2691  Var *varnode;
2692 
2693  varnode = makeVar(rtindex, atts_done + 1,
2694  funcrettype,
2695  exprTypmod(rtfunc->funcexpr),
2696  exprCollation(rtfunc->funcexpr),
2697  sublevels_up);
2698  varnode->location = location;
2699 
2700  *colvars = lappend(*colvars, varnode);
2701  }
2702  }
2703  else if (functypclass == TYPEFUNC_RECORD)
2704  {
2705  if (colnames)
2706  {
2707  List *namelist;
2708 
2709  /* extract appropriate subset of column list */
2710  namelist = list_copy_tail(rte->eref->colnames,
2711  atts_done);
2712  namelist = list_truncate(namelist,
2713  rtfunc->funccolcount);
2714  *colnames = list_concat(*colnames, namelist);
2715  }
2716 
2717  if (colvars)
2718  {
2719  ListCell *l1;
2720  ListCell *l2;
2721  ListCell *l3;
2722  int attnum = atts_done;
2723 
2724  forthree(l1, rtfunc->funccoltypes,
2725  l2, rtfunc->funccoltypmods,
2726  l3, rtfunc->funccolcollations)
2727  {
2728  Oid attrtype = lfirst_oid(l1);
2729  int32 attrtypmod = lfirst_int(l2);
2730  Oid attrcollation = lfirst_oid(l3);
2731  Var *varnode;
2732 
2733  attnum++;
2734  varnode = makeVar(rtindex,
2735  attnum,
2736  attrtype,
2737  attrtypmod,
2738  attrcollation,
2739  sublevels_up);
2740  varnode->location = location;
2741  *colvars = lappend(*colvars, varnode);
2742  }
2743  }
2744  }
2745  else
2746  {
2747  /* addRangeTableEntryForFunction should've caught this */
2748  elog(ERROR, "function in FROM has unsupported return type");
2749  }
2750  atts_done += rtfunc->funccolcount;
2751  }
2752 
2753  /* Append the ordinality column if any */
2754  if (rte->funcordinality)
2755  {
2756  if (colnames)
2757  *colnames = lappend(*colnames,
2758  llast(rte->eref->colnames));
2759 
2760  if (colvars)
2761  {
2762  Var *varnode = makeVar(rtindex,
2763  atts_done + 1,
2764  INT8OID,
2765  -1,
2766  InvalidOid,
2767  sublevels_up);
2768 
2769  *colvars = lappend(*colvars, varnode);
2770  }
2771  }
2772  }
2773  break;
2774  case RTE_JOIN:
2775  {
2776  /* Join RTE */
2777  ListCell *colname;
2778  ListCell *aliasvar;
2779 
2781 
2782  varattno = 0;
2783  forboth(colname, rte->eref->colnames, aliasvar, rte->joinaliasvars)
2784  {
2785  Node *avar = (Node *) lfirst(aliasvar);
2786 
2787  varattno++;
2788 
2789  /*
2790  * During ordinary parsing, there will never be any
2791  * deleted columns in the join. While this function is
2792  * also used by the rewriter and planner, they do not
2793  * currently call it on any JOIN RTEs. Therefore, this
2794  * next bit is dead code, but it seems prudent to handle
2795  * the case correctly anyway.
2796  */
2797  if (avar == NULL)
2798  {
2799  if (include_dropped)
2800  {
2801  if (colnames)
2802  *colnames = lappend(*colnames,
2803  makeString(pstrdup("")));
2804  if (colvars)
2805  {
2806  /*
2807  * Can't use join's column type here (it might
2808  * be dropped!); but it doesn't really matter
2809  * what type the Const claims to be.
2810  */
2811  *colvars = lappend(*colvars,
2812  makeNullConst(INT4OID, -1,
2813  InvalidOid));
2814  }
2815  }
2816  continue;
2817  }
2818 
2819  if (colnames)
2820  {
2821  char *label = strVal(lfirst(colname));
2822 
2823  *colnames = lappend(*colnames,
2824  makeString(pstrdup(label)));
2825  }
2826 
2827  if (colvars)
2828  {
2829  Var *varnode;
2830 
2831  /*
2832  * If the joinaliasvars entry is a simple Var, just
2833  * copy it (with adjustment of varlevelsup and
2834  * location); otherwise it is a JOIN USING column and
2835  * we must generate a join alias Var. This matches
2836  * the results that expansion of "join.*" by
2837  * expandNSItemVars would have produced, if we had
2838  * access to the ParseNamespaceItem for the join.
2839  */
2840  if (IsA(avar, Var))
2841  {
2842  varnode = copyObject((Var *) avar);
2843  varnode->varlevelsup = sublevels_up;
2844  }
2845  else
2846  varnode = makeVar(rtindex, varattno,
2847  exprType(avar),
2848  exprTypmod(avar),
2849  exprCollation(avar),
2850  sublevels_up);
2851  varnode->location = location;
2852 
2853  *colvars = lappend(*colvars, varnode);
2854  }
2855  }
2856  }
2857  break;
2858  case RTE_TABLEFUNC:
2859  case RTE_VALUES:
2860  case RTE_CTE:
2861  case RTE_NAMEDTUPLESTORE:
2862  {
2863  /* Tablefunc, Values, CTE, or ENR RTE */
2864  ListCell *aliasp_item = list_head(rte->eref->colnames);
2865  ListCell *lct;
2866  ListCell *lcm;
2867  ListCell *lcc;
2868 
2869  varattno = 0;
2870  forthree(lct, rte->coltypes,
2871  lcm, rte->coltypmods,
2872  lcc, rte->colcollations)
2873  {
2874  Oid coltype = lfirst_oid(lct);
2875  int32 coltypmod = lfirst_int(lcm);
2876  Oid colcoll = lfirst_oid(lcc);
2877 
2878  varattno++;
2879 
2880  if (colnames)
2881  {
2882  /* Assume there is one alias per output column */
2883  if (OidIsValid(coltype))
2884  {
2885  char *label = strVal(lfirst(aliasp_item));
2886 
2887  *colnames = lappend(*colnames,
2888  makeString(pstrdup(label)));
2889  }
2890  else if (include_dropped)
2891  *colnames = lappend(*colnames,
2892  makeString(pstrdup("")));
2893 
2894  aliasp_item = lnext(rte->eref->colnames, aliasp_item);
2895  }
2896 
2897  if (colvars)
2898  {
2899  if (OidIsValid(coltype))
2900  {
2901  Var *varnode;
2902 
2903  varnode = makeVar(rtindex, varattno,
2904  coltype, coltypmod, colcoll,
2905  sublevels_up);
2906  varnode->location = location;
2907 
2908  *colvars = lappend(*colvars, varnode);
2909  }
2910  else if (include_dropped)
2911  {
2912  /*
2913  * It doesn't really matter what type the Const
2914  * claims to be.
2915  */
2916  *colvars = lappend(*colvars,
2917  makeNullConst(INT4OID, -1,
2918  InvalidOid));
2919  }
2920  }
2921  }
2922  }
2923  break;
2924  case RTE_RESULT:
2925  /* These expose no columns, so nothing to do */
2926  break;
2927  default:
2928  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2929  }
2930 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
List * joinaliasvars
Definition: parsenodes.h:1070
Index varlevelsup
Definition: primnodes.h:196
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
List * colnames
Definition: primnodes.h:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:491
char * pstrdup(const char *in)
Definition: mcxt.c:1299
List * coltypmods
Definition: parsenodes.h:1128
#define llast(l)
Definition: pg_list.h:194
List * list_truncate(List *list, int new_size)
Definition: list.c:600
Definition: nodes.h:539
#define strVal(v)
Definition: value.h:54
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1437
bool funcordinality
Definition: parsenodes.h:1090
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:186
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:223
#define OidIsValid(objectId)
Definition: c.h:710
List * colcollations
Definition: parsenodes.h:1129
signed int int32
Definition: c.h:429
List * targetList
Definition: parsenodes.h:150
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:337
bool resjunk
Definition: primnodes.h:1451
#define ERROR
Definition: elog.h:46
#define lfirst_int(lc)
Definition: pg_list.h:170
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
int location
Definition: primnodes.h:201
AttrNumber resno
Definition: primnodes.h:1445
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
TypeFuncClass
Definition: funcapi.h:146
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
static void expandRelation(Oid relid, Alias *eref, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
List * lappend(List *list, void *datum)
Definition: list.c:336
static char * label
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1179
int16 attnum
Definition: pg_attribute.h:83
List * funccolcollations
Definition: parsenodes.h:1180
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * functions
Definition: parsenodes.h:1089
Expr * expr
Definition: primnodes.h:1444
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
static void expandTupleDesc(TupleDesc tupdesc, Alias *eref, int count, int offset, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
RTEKind rtekind
Definition: parsenodes.h:995
Query * subquery
Definition: parsenodes.h:1030
#define elog(elevel,...)
Definition: elog.h:232
Alias * eref
Definition: parsenodes.h:1141
#define copyObject(obj)
Definition: nodes.h:655
List * coltypes
Definition: parsenodes.h:1127
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ GetCTEForRTE()

CommonTableExpr* GetCTEForRTE ( ParseState pstate,
RangeTblEntry rte,
int  rtelevelsup 
)

Definition at line 537 of file parse_relation.c.

References Assert, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, CommonTableExpr::ctename, elog, ERROR, lfirst, ParseState::p_ctenamespace, ParseState::parentParseState, RTE_CTE, and RangeTblEntry::rtekind.

Referenced by expandRecordVariable(), and markTargetListOrigin().

538 {
539  Index levelsup;
540  ListCell *lc;
541 
542  Assert(rte->rtekind == RTE_CTE);
543  levelsup = rte->ctelevelsup + rtelevelsup;
544  while (levelsup-- > 0)
545  {
546  pstate = pstate->parentParseState;
547  if (!pstate) /* shouldn't happen */
548  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
549  }
550  foreach(lc, pstate->p_ctenamespace)
551  {
552  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
553 
554  if (strcmp(cte->ctename, rte->ctename) == 0)
555  return cte;
556  }
557  /* shouldn't happen */
558  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
559  return NULL; /* keep compiler quiet */
560 }
ParseState * parentParseState
Definition: parse_node.h:180
#define ERROR
Definition: elog.h:46
unsigned int Index
Definition: c.h:549
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Index ctelevelsup
Definition: parsenodes.h:1106
RTEKind rtekind
Definition: parsenodes.h:995
char * ctename
Definition: parsenodes.h:1105
#define elog(elevel,...)
Definition: elog.h:232
List * p_ctenamespace
Definition: parse_node.h:189

◆ GetNSItemByRangeTablePosn()

ParseNamespaceItem* GetNSItemByRangeTablePosn ( ParseState pstate,
int  varno,
int  sublevels_up 
)

Definition at line 490 of file parse_relation.c.

References Assert, elog, ERROR, lfirst, ParseState::p_namespace, ParseNamespaceItem::p_rtindex, and ParseState::parentParseState.

Referenced by coerce_record_to_complex(), ExpandRowReference(), and ParseComplexProjection().

493 {
494  ListCell *lc;
495 
496  while (sublevels_up-- > 0)
497  {
498  pstate = pstate->parentParseState;
499  Assert(pstate != NULL);
500  }
501  foreach(lc, pstate->p_namespace)
502  {
503  ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
504 
505  if (nsitem->p_rtindex == varno)
506  return nsitem;
507  }
508  elog(ERROR, "nsitem not found (internal error)");
509  return NULL; /* keep compiler quiet */
510 }
ParseState * parentParseState
Definition: parse_node.h:180
#define ERROR
Definition: elog.h:46
List * p_namespace
Definition: parse_node.h:186
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
#define elog(elevel,...)
Definition: elog.h:232

◆ GetRTEByRangeTablePosn()

RangeTblEntry* GetRTEByRangeTablePosn ( ParseState pstate,
int  varno,
int  sublevels_up 
)

Definition at line 517 of file parse_relation.c.

References Assert, list_length(), ParseState::p_rtable, ParseState::parentParseState, and rt_fetch.

Referenced by count_rowexpr_columns(), expandRecordVariable(), markTargetListOrigin(), and unknown_attribute().

520 {
521  while (sublevels_up-- > 0)
522  {
523  pstate = pstate->parentParseState;
524  Assert(pstate != NULL);
525  }
526  Assert(varno > 0 && varno <= list_length(pstate->p_rtable));
527  return rt_fetch(varno, pstate->p_rtable);
528 }
ParseState * parentParseState
Definition: parse_node.h:180
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
List * p_rtable
Definition: parse_node.h:182

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

Definition at line 2500 of file parse_relation.c.

References lfirst, LockingClause::lockedRels, NIL, ParseState::p_locked_from_parent, ParseState::p_locking_clause, and RangeVar::relname.

Referenced by addRangeTableEntry(), and transformRangeSubselect().

2501 {
2502  ListCell *l;
2503 
2504  /*
2505  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2506  * parent level, then act as though there's a generic FOR UPDATE here.
2507  */
2508  if (pstate->p_locked_from_parent)
2509  return true;
2510 
2511  foreach(l, pstate->p_locking_clause)
2512  {
2513  LockingClause *lc = (LockingClause *) lfirst(l);
2514 
2515  if (lc->lockedRels == NIL)
2516  {
2517  /* all tables used in query */
2518  return true;
2519  }
2520  else
2521  {
2522  /* just the named tables */
2523  ListCell *l2;
2524 
2525  foreach(l2, lc->lockedRels)
2526  {
2527  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2528 
2529  if (strcmp(refname, thisrel->relname) == 0)
2530  return true;
2531  }
2532  }
2533  }
2534  return false;
2535 }
List * lockedRels
Definition: parsenodes.h:764
#define NIL
Definition: pg_list.h:65
bool p_locked_from_parent
Definition: parse_node.h:200
char * relname
Definition: primnodes.h:68
List * p_locking_clause
Definition: parse_node.h:199
#define lfirst(lc)
Definition: pg_list.h:169

◆ isQueryUsingTempRelation()

bool isQueryUsingTempRelation ( Query query)

Definition at line 3627 of file parse_relation.c.

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

3628 {
3629  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3630 }
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
Definition: nodes.h:539

◆ markVarForSelectPriv()

void markVarForSelectPriv ( ParseState pstate,
Var var 
)

Definition at line 1087 of file parse_relation.c.

References Assert, IsA, markRTEForSelectPriv(), ParseState::parentParseState, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by expandNSItemAttrs(), ExpandSingleTable(), scanNSItemForColumn(), transformJoinUsingClause(), and transformWholeRowRef().

1088 {
1089  Index lv;
1090 
1091  Assert(IsA(var, Var));
1092  /* Find the appropriate pstate if it's an uplevel Var */
1093  for (lv = 0; lv < var->varlevelsup; lv++)
1094  pstate = pstate->parentParseState;
1095  markRTEForSelectPriv(pstate, var->varno, var->varattno);
1096 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Index varlevelsup
Definition: primnodes.h:196
ParseState * parentParseState
Definition: parse_node.h:180
AttrNumber varattno
Definition: primnodes.h:191
static void markRTEForSelectPriv(ParseState *pstate, int rtindex, AttrNumber col)
Definition: primnodes.h:186
Index varno
Definition: primnodes.h:189
unsigned int Index
Definition: c.h:549
#define Assert(condition)
Definition: c.h:804

◆ parserOpenTable()

Relation parserOpenTable ( ParseState pstate,
const RangeVar relation,
int  lockmode 
)

Definition at line 1350 of file parse_relation.c.

References cancel_parser_errposition_callback(), ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errdetail(), errhint(), errmsg(), ERROR, isFutureCTE(), RangeVar::location, RangeVar::relname, RangeVar::schemaname, setup_parser_errposition_callback(), and table_openrv_extended().

Referenced by addRangeTableEntry(), and setTargetTable().

1351 {
1352  Relation rel;
1353  ParseCallbackState pcbstate;
1354 
1355  setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1356  rel = table_openrv_extended(relation, lockmode, true);
1357  if (rel == NULL)
1358  {
1359  if (relation->schemaname)
1360  ereport(ERROR,
1362  errmsg("relation \"%s.%s\" does not exist",
1363  relation->schemaname, relation->relname)));
1364  else
1365  {
1366  /*
1367  * An unqualified name might have been meant as a reference to
1368  * some not-yet-in-scope CTE. The bare "does not exist" message
1369  * has proven remarkably unhelpful for figuring out such problems,
1370  * so we take pains to offer a specific hint.
1371  */
1372  if (isFutureCTE(pstate, relation->relname))
1373  ereport(ERROR,
1375  errmsg("relation \"%s\" does not exist",
1376  relation->relname),
1377  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1378  relation->relname),
1379  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1380  else
1381  ereport(ERROR,
1383  errmsg("relation \"%s\" does not exist",
1384  relation->relname)));
1385  }
1386  }
1388  return rel;
1389 }
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:77
Relation table_openrv_extended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
Definition: table.c:132
static bool isFutureCTE(ParseState *pstate, const char *refname)
int errcode(int sqlerrcode)
Definition: elog.c:698
char * schemaname
Definition: primnodes.h:67
int location
Definition: primnodes.h:73
char * relname
Definition: primnodes.h:68
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:161
#define ERROR
Definition: elog.h:46
int errdetail(const char *fmt,...)
Definition: elog.c:1042
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:145
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ refnameNamespaceItem()

ParseNamespaceItem* refnameNamespaceItem ( ParseState pstate,
const char *  schemaname,
const char *  refname,
int  location,
int *  sublevels_up 
)

Definition at line 109 of file parse_relation.c.

References get_relname_relid(), InvalidOid, LookupNamespaceNoError(), OidIsValid, ParseState::parentParseState, scanNameSpaceForRefname(), and scanNameSpaceForRelid().

Referenced by errorMissingRTE(), ExpandColumnRefStar(), and transformColumnRef().

114 {
115  Oid relId = InvalidOid;
116 
117  if (sublevels_up)
118  *sublevels_up = 0;
119 
120  if (schemaname != NULL)
121  {
122  Oid namespaceId;
123 
124  /*
125  * We can use LookupNamespaceNoError() here because we are only
126  * interested in finding existing RTEs. Checking USAGE permission on
127  * the schema is unnecessary since it would have already been checked
128  * when the RTE was made. Furthermore, we want to report "RTE not
129  * found", not "no permissions for schema", if the name happens to
130  * match a schema name the user hasn't got access to.
131  */
132  namespaceId = LookupNamespaceNoError(schemaname);
133  if (!OidIsValid(namespaceId))
134  return NULL;
135  relId = get_relname_relid(refname, namespaceId);
136  if (!OidIsValid(relId))
137  return NULL;
138  }
139 
140  while (pstate != NULL)
141  {
142  ParseNamespaceItem *result;
143 
144  if (OidIsValid(relId))
145  result = scanNameSpaceForRelid(pstate, relId, location);
146  else
147  result = scanNameSpaceForRefname(pstate, refname, location);
148 
149  if (result)
150  return result;
151 
152  if (sublevels_up)
153  (*sublevels_up)++;
154  else
155  break;
156 
157  pstate = pstate->parentParseState;
158  }
159  return NULL;
160 }
ParseState * parentParseState
Definition: parse_node.h:180
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1856
static ParseNamespaceItem * scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location)
#define InvalidOid
Definition: postgres_ext.h:36
static ParseNamespaceItem * scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
Oid LookupNamespaceNoError(const char *nspname)
Definition: namespace.c:2908

◆ scanNameSpaceForCTE()

CommonTableExpr* scanNameSpaceForCTE ( ParseState pstate,
const char *  refname,
Index ctelevelsup 
)

Definition at line 262 of file parse_relation.c.

References CommonTableExpr::ctename, lfirst, ParseState::p_ctenamespace, and ParseState::parentParseState.

Referenced by getNSItemForSpecialRelationTypes(), and searchRangeTableForRel().

264 {
265  Index levelsup;
266 
267  for (levelsup = 0;
268  pstate != NULL;
269  pstate = pstate->parentParseState, levelsup++)
270  {
271  ListCell *lc;
272 
273  foreach(lc, pstate->p_ctenamespace)
274  {
275  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
276 
277  if (strcmp(cte->ctename, refname) == 0)
278  {
279  *ctelevelsup = levelsup;
280  return cte;
281  }
282  }
283  }
284  return NULL;
285 }
ParseState * parentParseState
Definition: parse_node.h:180
unsigned int Index
Definition: c.h:549
#define lfirst(lc)
Definition: pg_list.h:169
List * p_ctenamespace
Definition: parse_node.h:189

◆ scanNameSpaceForENR()

bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 315 of file parse_relation.c.

References name_matches_visible_ENR().

Referenced by getNSItemForSpecialRelationTypes(), searchRangeTableForRel(), and setTargetTable().

316 {
317  return name_matches_visible_ENR(pstate, refname);
318 }
bool name_matches_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:20

◆ scanNSItemForColumn()

Node* scanNSItemForColumn ( ParseState pstate,
ParseNamespaceItem nsitem,
int  sublevels_up,
const char *  colname,
int  location 
)

Definition at line 668 of file parse_relation.c.

References Alias::aliasname, attnum, ereport, errcode(), errmsg(), ERROR, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_GENERATED_COLUMN, FormData_pg_attribute, InvalidAttrNumber, Var::location, makeVar(), markVarForSelectPriv(), ParseState::p_expr_kind, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseNamespaceItem::p_rte, ParseNamespaceItem::p_rtindex, ParseNamespaceColumn::p_varattno, ParseNamespaceColumn::p_varattnosyn, ParseNamespaceColumn::p_varcollid, ParseNamespaceColumn::p_varno, ParseNamespaceColumn::p_varnosyn, ParseNamespaceColumn::p_vartype, ParseNamespaceColumn::p_vartypmod, parser_errposition(), scanRTEForColumn(), SystemAttributeDefinition(), TableOidAttributeNumber, Var::varattnosyn, and Var::varnosyn.

Referenced by colNameToVar(), ParseComplexProjection(), and transformColumnRef().

670 {
671  RangeTblEntry *rte = nsitem->p_rte;
672  int attnum;
673  Var *var;
674 
675  /*
676  * Scan the nsitem's column names (or aliases) for a match. Complain if
677  * multiple matches.
678  */
679  attnum = scanRTEForColumn(pstate, rte, nsitem->p_names,
680  colname, location,
681  0, NULL);
682 
683  if (attnum == InvalidAttrNumber)
684  return NULL; /* Return NULL if no match */
685 
686  /* In constraint check, no system column is allowed except tableOid */
687  if (pstate->p_expr_kind == EXPR_KIND_CHECK_CONSTRAINT &&
688  attnum < InvalidAttrNumber && attnum != TableOidAttributeNumber)
689  ereport(ERROR,
690  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
691  errmsg("system column \"%s\" reference in check constraint is invalid",
692  colname),
693  parser_errposition(pstate, location)));
694 
695  /* In generated column, no system column is allowed except tableOid */
696  if (pstate->p_expr_kind == EXPR_KIND_GENERATED_COLUMN &&
697  attnum < InvalidAttrNumber && attnum != TableOidAttributeNumber)
698  ereport(ERROR,
699  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
700  errmsg("cannot use system column \"%s\" in column generation expression",
701  colname),
702  parser_errposition(pstate, location)));
703 
704  /* Found a valid match, so build a Var */
705  if (attnum > InvalidAttrNumber)
706  {
707  /* Get attribute data from the ParseNamespaceColumn array */
708  ParseNamespaceColumn *nscol = &nsitem->p_nscolumns[attnum - 1];
709 
710  /* Complain if dropped column. See notes in scanRTEForColumn. */
711  if (nscol->p_varno == 0)
712  ereport(ERROR,
713  (errcode(ERRCODE_UNDEFINED_COLUMN),
714  errmsg("column \"%s\" of relation \"%s\" does not exist",
715  colname,
716  nsitem->p_names->aliasname)));
717 
718  var = makeVar(nscol->p_varno,
719  nscol->p_varattno,
720  nscol->p_vartype,
721  nscol->p_vartypmod,
722  nscol->p_varcollid,
723  sublevels_up);
724  /* makeVar doesn't offer parameters for these, so set them by hand: */
725  var->varnosyn = nscol->p_varnosyn;
726  var->varattnosyn = nscol->p_varattnosyn;
727  }
728  else
729  {
730  /* System column, so use predetermined type data */
731  const FormData_pg_attribute *sysatt;
732 
733  sysatt = SystemAttributeDefinition(attnum);
734  var = makeVar(nsitem->p_rtindex,
735  attnum,
736  sysatt->atttypid,
737  sysatt->atttypmod,
738  sysatt->attcollation,
739  sublevels_up);
740  }
741  var->location = location;
742 
743  /* Require read access to the column */
744  markVarForSelectPriv(pstate, var);
745 
746  return (Node *) var;
747 }
static int scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, Alias *eref, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
void markVarForSelectPriv(ParseState *pstate, Var *var)
Definition: nodes.h:539
int errcode(int sqlerrcode)
Definition: elog.c:698
Definition: primnodes.h:186
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:251
RangeTblEntry * p_rte
Definition: parse_node.h:268
#define ERROR
Definition: elog.h:46
int location
Definition: primnodes.h:201
AttrNumber p_varattno
Definition: parse_node.h:302
#define TableOidAttributeNumber
Definition: sysattr.h:26
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
Index varnosyn
Definition: primnodes.h:199
AttrNumber varattnosyn
Definition: primnodes.h:200
ParseExprKind p_expr_kind
Definition: parse_node.h:196
FormData_pg_attribute
Definition: pg_attribute.h:191
int16 attnum
Definition: pg_attribute.h:83
#define ereport(elevel,...)
Definition: elog.h:157
char * aliasname
Definition: primnodes.h:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
#define InvalidAttrNumber
Definition: attnum.h:23
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:271
int errmsg(const char *fmt,...)
Definition: elog.c:909
AttrNumber p_varattnosyn
Definition: parse_node.h:307