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 *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 2531 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(), transformOnConflictArbiter(), transformOnConflictClause(), transformPartitionSpec(), transformRuleStmt(), transformSetOperationStmt(), and transformValuesClause().

2534 {
2535  if (addToJoinList)
2536  {
2538 
2539  rtr->rtindex = nsitem->p_rtindex;
2540  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2541  }
2542  if (addToRelNameSpace || addToVarNameSpace)
2543  {
2544  /* Set the new nsitem's visibility flags correctly */
2545  nsitem->p_rel_visible = addToRelNameSpace;
2546  nsitem->p_cols_visible = addToVarNameSpace;
2547  nsitem->p_lateral_only = false;
2548  nsitem->p_lateral_ok = true;
2549  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2550  }
2551 }
List * p_namespace
Definition: parse_node.h:185
List * lappend(List *list, void *datum)
Definition: list.c:336
#define makeNode(_type_)
Definition: nodes.h:581
List * p_joinlist
Definition: parse_node.h:183

◆ addRangeTableEntry()

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

Definition at line 1392 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().

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

◆ addRangeTableEntryForCTE()

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

Definition at line 2220 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_nscolumns, ParseState::p_rtable, ParseNamespaceItem::p_rte, 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().

2225 {
2227  Alias *alias = rv->alias;
2228  char *refname = alias ? alias->aliasname : cte->ctename;
2229  Alias *eref;
2230  int numaliases;
2231  int varattno;
2232  ListCell *lc;
2233  int n_dontexpand_columns = 0;
2234  ParseNamespaceItem *psi;
2235 
2236  Assert(pstate != NULL);
2237 
2238  rte->rtekind = RTE_CTE;
2239  rte->ctename = cte->ctename;
2240  rte->ctelevelsup = levelsup;
2241 
2242  /* Self-reference if and only if CTE's parse analysis isn't completed */
2243  rte->self_reference = !IsA(cte->ctequery, Query);
2244  Assert(cte->cterecursive || !rte->self_reference);
2245  /* Bump the CTE's refcount if this isn't a self-reference */
2246  if (!rte->self_reference)
2247  cte->cterefcount++;
2248 
2249  /*
2250  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
2251  * This won't get checked in case of a self-reference, but that's OK
2252  * because data-modifying CTEs aren't allowed to be recursive anyhow.
2253  */
2254  if (IsA(cte->ctequery, Query))
2255  {
2256  Query *ctequery = (Query *) cte->ctequery;
2257 
2258  if (ctequery->commandType != CMD_SELECT &&
2259  ctequery->returningList == NIL)
2260  ereport(ERROR,
2261  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2262  errmsg("WITH query \"%s\" does not have a RETURNING clause",
2263  cte->ctename),
2264  parser_errposition(pstate, rv->location)));
2265  }
2266 
2267  rte->coltypes = list_copy(cte->ctecoltypes);
2268  rte->coltypmods = list_copy(cte->ctecoltypmods);
2270 
2271  rte->alias = alias;
2272  if (alias)
2273  eref = copyObject(alias);
2274  else
2275  eref = makeAlias(refname, NIL);
2276  numaliases = list_length(eref->colnames);
2277 
2278  /* fill in any unspecified alias columns */
2279  varattno = 0;
2280  foreach(lc, cte->ctecolnames)
2281  {
2282  varattno++;
2283  if (varattno > numaliases)
2284  eref->colnames = lappend(eref->colnames, lfirst(lc));
2285  }
2286  if (varattno < numaliases)
2287  ereport(ERROR,
2288  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2289  errmsg("table \"%s\" has %d columns available but %d columns specified",
2290  refname, varattno, numaliases)));
2291 
2292  rte->eref = eref;
2293 
2294  if (cte->search_clause)
2295  {
2298  rte->coltypes = lappend_oid(rte->coltypes, RECORDOID);
2299  else
2300  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2301  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2303 
2304  n_dontexpand_columns += 1;
2305  }
2306 
2307  if (cte->cycle_clause)
2308  {
2313 
2315  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2316  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2318 
2319  n_dontexpand_columns += 2;
2320  }
2321 
2322  /*
2323  * Set flags and access permissions.
2324  *
2325  * Subqueries are never checked for access rights.
2326  */
2327  rte->lateral = false;
2328  rte->inh = false; /* never true for subqueries */
2329  rte->inFromCl = inFromCl;
2330 
2331  rte->requiredPerms = 0;
2332  rte->checkAsUser = InvalidOid;
2333  rte->selectedCols = NULL;
2334  rte->insertedCols = NULL;
2335  rte->updatedCols = NULL;
2336  rte->extraUpdatedCols = NULL;
2337 
2338  /*
2339  * Add completed RTE to pstate's range table list, so that we know its
2340  * index. But we don't add it to the join list --- caller must do that if
2341  * appropriate.
2342  */
2343  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2344 
2345  /*
2346  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2347  * list --- caller must do that if appropriate.
2348  */
2349  psi = buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2350  rte->coltypes, rte->coltypmods,
2351  rte->colcollations);
2352 
2353  /*
2354  * The columns added by search and cycle clauses are not included in star
2355  * expansion in queries contained in the CTE.
2356  */
2357  if (rte->ctelevelsup > 0)
2358  for (int i = 0; i < n_dontexpand_columns; i++)
2359  psi->p_nscolumns[list_length(psi->p_rte->eref->colnames) - 1 - i].p_dontexpand = true;
2360 
2361  return psi;
2362 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
bool search_breadth_first
Definition: parsenodes.h:1455
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
Alias * alias
Definition: parsenodes.h:1119
List * colnames
Definition: primnodes.h:43
char * search_seq_column
Definition: parsenodes.h:1456
List * coltypmods
Definition: parsenodes.h:1107
List * list_copy(const List *oldlist)
Definition: list.c:1418
int errcode(int sqlerrcode)
Definition: elog.c:694
AclMode requiredPerms
Definition: parsenodes.h:1124
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
List * colcollations
Definition: parsenodes.h:1108
int location
Definition: primnodes.h:73
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
RangeTblEntry * p_rte
Definition: parse_node.h:258
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1129
Bitmapset * selectedCols
Definition: parsenodes.h:1126
#define ERROR
Definition: elog.h:45
CTESearchClause * search_clause
Definition: parsenodes.h:1484
List * ctecoltypmods
Definition: parsenodes.h:1493
List * returningList
Definition: parsenodes.h:146
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:1491
bool self_reference
Definition: parsenodes.h:1086
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
Oid cycle_mark_collation
Definition: parsenodes.h:1472
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
Bitmapset * updatedCols
Definition: parsenodes.h:1128
CmdType commandType
Definition: parsenodes.h:112
#define makeNode(_type_)
Definition: nodes.h:581
#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:1085
List * ctecoltypes
Definition: parsenodes.h:1492
RTEKind rtekind
Definition: parsenodes.h:981
char * cycle_path_column
Definition: parsenodes.h:1467
char * ctename
Definition: parsenodes.h:1084
CTECycleClause * cycle_clause
Definition: parsenodes.h:1485
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:261
int errmsg(const char *fmt,...)
Definition: elog.c:905
char * cycle_mark_column
Definition: parsenodes.h:1464
Bitmapset * insertedCols
Definition: parsenodes.h:1127
int i
Alias * alias
Definition: primnodes.h:72
Alias * eref
Definition: parsenodes.h:1120
List * ctecolcollations
Definition: parsenodes.h:1494
#define copyObject(obj)
Definition: nodes.h:649
List * coltypes
Definition: parsenodes.h:1106
List * p_rtable
Definition: parse_node.h:181

◆ addRangeTableEntryForENR()

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

Definition at line 2378 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().

2381 {
2383  Alias *alias = rv->alias;
2384  char *refname = alias ? alias->aliasname : rv->relname;
2386  TupleDesc tupdesc;
2387  int attno;
2388 
2389  Assert(pstate != NULL);
2390  enrmd = get_visible_ENR(pstate, rv->relname);
2391  Assert(enrmd != NULL);
2392 
2393  switch (enrmd->enrtype)
2394  {
2395  case ENR_NAMED_TUPLESTORE:
2397  break;
2398 
2399  default:
2400  elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2401  return NULL; /* for fussy compilers */
2402  }
2403 
2404  /*
2405  * Record dependency on a relation. This allows plans to be invalidated
2406  * if they access transition tables linked to a table that is altered.
2407  */
2408  rte->relid = enrmd->reliddesc;
2409 
2410  /*
2411  * Build the list of effective column names using user-supplied aliases
2412  * and/or actual column names.
2413  */
2414  tupdesc = ENRMetadataGetTupDesc(enrmd);
2415  rte->eref = makeAlias(refname, NIL);
2416  buildRelationAliases(tupdesc, alias, rte->eref);
2417 
2418  /* Record additional data for ENR, including column type info */
2419  rte->enrname = enrmd->name;
2420  rte->enrtuples = enrmd->enrtuples;
2421  rte->coltypes = NIL;
2422  rte->coltypmods = NIL;
2423  rte->colcollations = NIL;
2424  for (attno = 1; attno <= tupdesc->natts; ++attno)
2425  {
2426  Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2427 
2428  if (att->attisdropped)
2429  {
2430  /* Record zeroes for a dropped column */
2431  rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2432  rte->coltypmods = lappend_int(rte->coltypmods, 0);
2434  }
2435  else
2436  {
2437  /* Let's just make sure we can tell this isn't dropped */
2438  if (att->atttypid == InvalidOid)
2439  elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2440  rv->relname);
2441  rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2442  rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2444  att->attcollation);
2445  }
2446  }
2447 
2448  /*
2449  * Set flags and access permissions.
2450  *
2451  * ENRs are never checked for access rights.
2452  */
2453  rte->lateral = false;
2454  rte->inh = false; /* never true for ENRs */
2455  rte->inFromCl = inFromCl;
2456 
2457  rte->requiredPerms = 0;
2458  rte->checkAsUser = InvalidOid;
2459  rte->selectedCols = NULL;
2460 
2461  /*
2462  * Add completed RTE to pstate's range table list, so that we know its
2463  * index. But we don't add it to the join list --- caller must do that if
2464  * appropriate.
2465  */
2466  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2467 
2468  /*
2469  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2470  * list --- caller must do that if appropriate.
2471  */
2472  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
2473  tupdesc);
2474 }
#define NIL
Definition: pg_list.h:65
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
List * coltypmods
Definition: parsenodes.h:1107
AclMode requiredPerms
Definition: parsenodes.h:1124
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
List * colcollations
Definition: parsenodes.h:1108
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:1126
#define ERROR
Definition: elog.h:45
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
char * enrname
Definition: parsenodes.h:1113
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:581
#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:981
double enrtuples
Definition: parsenodes.h:1114
#define elog(elevel,...)
Definition: elog.h:227
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * alias
Definition: primnodes.h:72
EphemeralNameRelationType enrtype
Alias * eref
Definition: parsenodes.h:1120
List * coltypes
Definition: parsenodes.h:1106
List * p_rtable
Definition: parse_node.h:181

◆ addRangeTableEntryForFunction()

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

Definition at line 1658 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().

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

◆ addRangeTableEntryForJoin()

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

Definition at line 2125 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::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_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().

2135 {
2137  Alias *eref;
2138  int numaliases;
2139  ParseNamespaceItem *nsitem;
2140 
2141  Assert(pstate != NULL);
2142 
2143  /*
2144  * Fail if join has too many columns --- we must be able to reference any
2145  * of the columns with an AttrNumber.
2146  */
2147  if (list_length(aliasvars) > MaxAttrNumber)
2148  ereport(ERROR,
2149  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2150  errmsg("joins can have at most %d columns",
2151  MaxAttrNumber)));
2152 
2153  rte->rtekind = RTE_JOIN;
2154  rte->relid = InvalidOid;
2155  rte->subquery = NULL;
2156  rte->jointype = jointype;
2157  rte->joinmergedcols = nummergedcols;
2158  rte->joinaliasvars = aliasvars;
2159  rte->joinleftcols = leftcols;
2160  rte->joinrightcols = rightcols;
2161  rte->alias = alias;
2162 
2163  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
2164  numaliases = list_length(eref->colnames);
2165 
2166  /* fill in any unspecified alias columns */
2167  if (numaliases < list_length(colnames))
2168  eref->colnames = list_concat(eref->colnames,
2169  list_copy_tail(colnames, numaliases));
2170 
2171  rte->eref = eref;
2172 
2173  /*
2174  * Set flags and access permissions.
2175  *
2176  * Joins are never checked for access rights.
2177  */
2178  rte->lateral = false;
2179  rte->inh = false; /* never true for joins */
2180  rte->inFromCl = inFromCl;
2181 
2182  rte->requiredPerms = 0;
2183  rte->checkAsUser = InvalidOid;
2184  rte->selectedCols = NULL;
2185  rte->insertedCols = NULL;
2186  rte->updatedCols = NULL;
2187  rte->extraUpdatedCols = NULL;
2188 
2189  /*
2190  * Add completed RTE to pstate's range table list, so that we know its
2191  * index. But we don't add it to the join list --- caller must do that if
2192  * appropriate.
2193  */
2194  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2195 
2196  /*
2197  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2198  * list --- caller must do that if appropriate.
2199  */
2200  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2201  nsitem->p_rte = rte;
2202  nsitem->p_rtindex = list_length(pstate->p_rtable);
2203  nsitem->p_nscolumns = nscolumns;
2204  /* set default visibility flags; might get changed later */
2205  nsitem->p_rel_visible = true;
2206  nsitem->p_cols_visible = true;
2207  nsitem->p_lateral_only = false;
2208  nsitem->p_lateral_ok = true;
2209 
2210  return nsitem;
2211 }
#define NIL
Definition: pg_list.h:65
List * joinaliasvars
Definition: parsenodes.h:1056
Alias * alias
Definition: parsenodes.h:1119
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:694
AclMode requiredPerms
Definition: parsenodes.h:1124
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:258
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1129
Bitmapset * selectedCols
Definition: parsenodes.h:1126
#define ERROR
Definition: elog.h:45
JoinType jointype
Definition: parsenodes.h:1054
List * joinrightcols
Definition: parsenodes.h:1058
List * lappend(List *list, void *datum)
Definition: list.c:336
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
Bitmapset * updatedCols
Definition: parsenodes.h:1128
List * joinleftcols
Definition: parsenodes.h:1057
#define makeNode(_type_)
Definition: nodes.h:581
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:981
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:261
Query * subquery
Definition: parsenodes.h:1016
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:905
Bitmapset * insertedCols
Definition: parsenodes.h:1127
Alias * eref
Definition: parsenodes.h:1120
#define copyObject(obj)
Definition: nodes.h:649
List * p_rtable
Definition: parse_node.h:181

◆ addRangeTableEntryForRelation()

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

Definition at line 1492 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(), and UpdateRangeTableOfViewParse().

1498 {
1500  char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1501 
1502  Assert(pstate != NULL);
1503 
1504  Assert(lockmode == AccessShareLock ||
1505  lockmode == RowShareLock ||
1506  lockmode == RowExclusiveLock);
1507  Assert(CheckRelationLockedByMe(rel, lockmode, true));
1508 
1509  rte->rtekind = RTE_RELATION;
1510  rte->alias = alias;
1511  rte->relid = RelationGetRelid(rel);
1512  rte->relkind = rel->rd_rel->relkind;
1513  rte->rellockmode = lockmode;
1514 
1515  /*
1516  * Build the list of effective column names using user-supplied aliases
1517  * and/or actual column names.
1518  */
1519  rte->eref = makeAlias(refname, NIL);
1520  buildRelationAliases(rel->rd_att, alias, rte->eref);
1521 
1522  /*
1523  * Set flags and access permissions.
1524  *
1525  * The initial default on access checks is always check-for-READ-access,
1526  * which is the right thing for all except target tables.
1527  */
1528  rte->lateral = false;
1529  rte->inh = inh;
1530  rte->inFromCl = inFromCl;
1531 
1532  rte->requiredPerms = ACL_SELECT;
1533  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1534  rte->selectedCols = NULL;
1535  rte->insertedCols = NULL;
1536  rte->updatedCols = NULL;
1537  rte->extraUpdatedCols = NULL;
1538 
1539  /*
1540  * Add completed RTE to pstate's range table list, so that we know its
1541  * index. But we don't add it to the join list --- caller must do that if
1542  * appropriate.
1543  */
1544  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1545 
1546  /*
1547  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1548  * list --- caller must do that if appropriate.
1549  */
1550  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1551  rel->rd_att);
1552 }
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1119
#define AccessShareLock
Definition: lockdefs.h:36
AclMode requiredPerms
Definition: parsenodes.h:1124
Form_pg_class rd_rel
Definition: rel.h:110
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1129
Bitmapset * selectedCols
Definition: parsenodes.h:1126
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
#define RowExclusiveLock
Definition: lockdefs.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:491
List * lappend(List *list, void *datum)
Definition: list.c:336
#define RowShareLock
Definition: lockdefs.h:37
#define ACL_SELECT
Definition: parsenodes.h:75
TupleDesc rd_att
Definition: rel.h:111
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1128
#define makeNode(_type_)
Definition: nodes.h:581
#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:302
RTEKind rtekind
Definition: parsenodes.h:981
Bitmapset * insertedCols
Definition: parsenodes.h:1127
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * eref
Definition: parsenodes.h:1120
#define RelationGetRelid(relation)
Definition: rel.h:457
List * p_rtable
Definition: parse_node.h:181

◆ addRangeTableEntryForSubquery()

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

Definition at line 1562 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().

1567 {
1569  char *refname = alias->aliasname;
1570  Alias *eref;
1571  int numaliases;
1572  List *coltypes,
1573  *coltypmods,
1574  *colcollations;
1575  int varattno;
1576  ListCell *tlistitem;
1577 
1578  Assert(pstate != NULL);
1579 
1580  rte->rtekind = RTE_SUBQUERY;
1581  rte->subquery = subquery;
1582  rte->alias = alias;
1583 
1584  eref = copyObject(alias);
1585  numaliases = list_length(eref->colnames);
1586 
1587  /* fill in any unspecified alias columns, and extract column type info */
1588  coltypes = coltypmods = colcollations = NIL;
1589  varattno = 0;
1590  foreach(tlistitem, subquery->targetList)
1591  {
1592  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1593 
1594  if (te->resjunk)
1595  continue;
1596  varattno++;
1597  Assert(varattno == te->resno);
1598  if (varattno > numaliases)
1599  {
1600  char *attrname;
1601 
1602  attrname = pstrdup(te->resname);
1603  eref->colnames = lappend(eref->colnames, makeString(attrname));
1604  }
1605  coltypes = lappend_oid(coltypes,
1606  exprType((Node *) te->expr));
1607  coltypmods = lappend_int(coltypmods,
1608  exprTypmod((Node *) te->expr));
1609  colcollations = lappend_oid(colcollations,
1610  exprCollation((Node *) te->expr));
1611  }
1612  if (varattno < numaliases)
1613  ereport(ERROR,
1614  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1615  errmsg("table \"%s\" has %d columns available but %d columns specified",
1616  refname, varattno, numaliases)));
1617 
1618  rte->eref = eref;
1619 
1620  /*
1621  * Set flags and access permissions.
1622  *
1623  * Subqueries are never checked for access rights.
1624  */
1625  rte->lateral = lateral;
1626  rte->inh = false; /* never true for subqueries */
1627  rte->inFromCl = inFromCl;
1628 
1629  rte->requiredPerms = 0;
1630  rte->checkAsUser = InvalidOid;
1631  rte->selectedCols = NULL;
1632  rte->insertedCols = NULL;
1633  rte->updatedCols = NULL;
1634  rte->extraUpdatedCols = NULL;
1635 
1636  /*
1637  * Add completed RTE to pstate's range table list, so that we know its
1638  * index. But we don't add it to the join list --- caller must do that if
1639  * appropriate.
1640  */
1641  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1642 
1643  /*
1644  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1645  * list --- caller must do that if appropriate.
1646  */
1647  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
1648  coltypes, coltypmods, colcollations);
1649 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1119
List * colnames
Definition: primnodes.h:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
char * pstrdup(const char *in)
Definition: mcxt.c:1187
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
AclMode requiredPerms
Definition: parsenodes.h:1124
char * resname
Definition: primnodes.h:1433
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
List * targetList
Definition: parsenodes.h:140
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1129
Bitmapset * selectedCols
Definition: parsenodes.h:1126
bool resjunk
Definition: primnodes.h:1438
#define ERROR
Definition: elog.h:45
AttrNumber resno
Definition: primnodes.h:1432
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:155
Bitmapset * updatedCols
Definition: parsenodes.h:1128
#define makeNode(_type_)
Definition: nodes.h:581
#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:1431
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:981
Query * subquery
Definition: parsenodes.h:1016
int errmsg(const char *fmt,...)
Definition: elog.c:905
Bitmapset * insertedCols
Definition: parsenodes.h:1127
Alias * eref
Definition: parsenodes.h:1120
#define copyObject(obj)
Definition: nodes.h:649
Definition: pg_list.h:50
List * p_rtable
Definition: parse_node.h:181

◆ addRangeTableEntryForTableFunc()

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

Definition at line 1964 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().

1969 {
1971  char *refname = alias ? alias->aliasname : pstrdup("xmltable");
1972  Alias *eref;
1973  int numaliases;
1974 
1975  Assert(pstate != NULL);
1976 
1977  rte->rtekind = RTE_TABLEFUNC;
1978  rte->relid = InvalidOid;
1979  rte->subquery = NULL;
1980  rte->tablefunc = tf;
1981  rte->coltypes = tf->coltypes;
1982  rte->coltypmods = tf->coltypmods;
1983  rte->colcollations = tf->colcollations;
1984  rte->alias = alias;
1985 
1986  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1987  numaliases = list_length(eref->colnames);
1988 
1989  /* fill in any unspecified alias columns */
1990  if (numaliases < list_length(tf->colnames))
1991  eref->colnames = list_concat(eref->colnames,
1992  list_copy_tail(tf->colnames, numaliases));
1993 
1994  rte->eref = eref;
1995 
1996  /*
1997  * Set flags and access permissions.
1998  *
1999  * Tablefuncs are never checked for access rights (at least, not by the
2000  * RTE permissions mechanism).
2001  */
2002  rte->lateral = lateral;
2003  rte->inh = false; /* never true for tablefunc RTEs */
2004  rte->inFromCl = inFromCl;
2005 
2006  rte->requiredPerms = 0;
2007  rte->checkAsUser = InvalidOid;
2008  rte->selectedCols = NULL;
2009  rte->insertedCols = NULL;
2010  rte->updatedCols = NULL;
2011  rte->extraUpdatedCols = NULL;
2012 
2013  /*
2014  * Add completed RTE to pstate's range table list, so that we know its
2015  * index. But we don't add it to the join list --- caller must do that if
2016  * appropriate.
2017  */
2018  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2019 
2020  /*
2021  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2022  * list --- caller must do that if appropriate.
2023  */
2024  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2025  rte->coltypes, rte->coltypmods,
2026  rte->colcollations);
2027 }
#define NIL
Definition: pg_list.h:65
List * colnames
Definition: primnodes.h:89
Alias * alias
Definition: parsenodes.h:1119
List * colnames
Definition: primnodes.h:43
List * coltypmods
Definition: primnodes.h:91
char * pstrdup(const char *in)
Definition: mcxt.c:1187
List * coltypmods
Definition: parsenodes.h:1107
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
AclMode requiredPerms
Definition: parsenodes.h:1124
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1437
List * colcollations
Definition: parsenodes.h:1108
List * colcollations
Definition: primnodes.h:92
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1129
Bitmapset * selectedCols
Definition: parsenodes.h:1126
TableFunc * tablefunc
Definition: parsenodes.h:1074
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:1128
#define makeNode(_type_)
Definition: nodes.h:581
#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:981
Query * subquery
Definition: parsenodes.h:1016
Bitmapset * insertedCols
Definition: parsenodes.h:1127
Alias * eref
Definition: parsenodes.h:1120
#define copyObject(obj)
Definition: nodes.h:649
List * coltypes
Definition: parsenodes.h:1106
List * p_rtable
Definition: parse_node.h:181

◆ addRangeTableEntryForValues()

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

Definition at line 2036 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().

2044 {
2046  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
2047  Alias *eref;
2048  int numaliases;
2049  int numcolumns;
2050 
2051  Assert(pstate != NULL);
2052 
2053  rte->rtekind = RTE_VALUES;
2054  rte->relid = InvalidOid;
2055  rte->subquery = NULL;
2056  rte->values_lists = exprs;
2057  rte->coltypes = coltypes;
2058  rte->coltypmods = coltypmods;
2059  rte->colcollations = colcollations;
2060  rte->alias = alias;
2061 
2062  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2063 
2064  /* fill in any unspecified alias columns */
2065  numcolumns = list_length((List *) linitial(exprs));
2066  numaliases = list_length(eref->colnames);
2067  while (numaliases < numcolumns)
2068  {
2069  char attrname[64];
2070 
2071  numaliases++;
2072  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
2073  eref->colnames = lappend(eref->colnames,
2074  makeString(pstrdup(attrname)));
2075  }
2076  if (numcolumns < numaliases)
2077  ereport(ERROR,
2078  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2079  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
2080  refname, numcolumns, numaliases)));
2081 
2082  rte->eref = eref;
2083 
2084  /*
2085  * Set flags and access permissions.
2086  *
2087  * Subqueries are never checked for access rights.
2088  */
2089  rte->lateral = lateral;
2090  rte->inh = false; /* never true for values RTEs */
2091  rte->inFromCl = inFromCl;
2092 
2093  rte->requiredPerms = 0;
2094  rte->checkAsUser = InvalidOid;
2095  rte->selectedCols = NULL;
2096  rte->insertedCols = NULL;
2097  rte->updatedCols = NULL;
2098  rte->extraUpdatedCols = NULL;
2099 
2100  /*
2101  * Add completed RTE to pstate's range table list, so that we know its
2102  * index. But we don't add it to the join list --- caller must do that if
2103  * appropriate.
2104  */
2105  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2106 
2107  /*
2108  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2109  * list --- caller must do that if appropriate.
2110  */
2111  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2112  rte->coltypes, rte->coltypmods,
2113  rte->colcollations);
2114 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1119
List * colnames
Definition: primnodes.h:43
char * pstrdup(const char *in)
Definition: mcxt.c:1187
List * coltypmods
Definition: parsenodes.h:1107
int errcode(int sqlerrcode)
Definition: elog.c:694
AclMode requiredPerms
Definition: parsenodes.h:1124
List * colcollations
Definition: parsenodes.h:1108
List * values_lists
Definition: parsenodes.h:1079
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1129
Bitmapset * selectedCols
Definition: parsenodes.h:1126
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:45
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:155
Bitmapset * updatedCols
Definition: parsenodes.h:1128
#define makeNode(_type_)
Definition: nodes.h:581
#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:981
Query * subquery
Definition: parsenodes.h:1016
int errmsg(const char *fmt,...)
Definition: elog.c:905
Bitmapset * insertedCols
Definition: parsenodes.h:1127
Alias * eref
Definition: parsenodes.h:1120
#define copyObject(obj)
Definition: nodes.h:649
List * coltypes
Definition: parsenodes.h:1106
Definition: pg_list.h:50
#define snprintf
Definition: port.h:216
List * p_rtable
Definition: parse_node.h:181

◆ attnameAttNum()

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

Definition at line 3370 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().

3371 {
3372  int i;
3373 
3374  for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3375  {
3376  Form_pg_attribute att = TupleDescAttr(rd->rd_att, i);
3377 
3378  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3379  return i + 1;
3380  }
3381 
3382  if (sysColOK)
3383  {
3384  if ((i = specialAttNum(attname)) != InvalidAttrNumber)
3385  return i;
3386  }
3387 
3388  /* on failure */
3389  return InvalidAttrNumber;
3390 }
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:463
#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:197
TupleDesc rd_att
Definition: rel.h:111
#define InvalidAttrNumber
Definition: attnum.h:23
int i

◆ attnumAttName()

const NameData* attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3420 of file parse_relation.c.

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

Referenced by transformFkeyGetPrimaryKey().

3421 {
3422  if (attid <= 0)
3423  {
3424  const FormData_pg_attribute *sysatt;
3425 
3426  sysatt = SystemAttributeDefinition(attid);
3427  return &sysatt->attname;
3428  }
3429  if (attid > rd->rd_att->natts)
3430  elog(ERROR, "invalid attribute number %d", attid);
3431  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3432 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:249
#define ERROR
Definition: elog.h:45
TupleDesc rd_att
Definition: rel.h:111
FormData_pg_attribute
Definition: pg_attribute.h:181
#define elog(elevel,...)
Definition: elog.h:227

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3462 of file parse_relation.c.

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

3463 {
3464  if (attid <= 0)
3465  {
3466  /* All system attributes are of noncollatable types. */
3467  return InvalidOid;
3468  }
3469  if (attid > rd->rd_att->natts)
3470  elog(ERROR, "invalid attribute number %d", attid);
3471  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3472 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define ERROR
Definition: elog.h:45
TupleDesc rd_att
Definition: rel.h:111
#define InvalidOid
Definition: postgres_ext.h:36
#define elog(elevel,...)
Definition: elog.h:227

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3442 of file parse_relation.c.

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

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

3443 {
3444  if (attid <= 0)
3445  {
3446  const FormData_pg_attribute *sysatt;
3447 
3448  sysatt = SystemAttributeDefinition(attid);
3449  return sysatt->atttypid;
3450  }
3451  if (attid > rd->rd_att->natts)
3452  elog(ERROR, "invalid attribute number %d", attid);
3453  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3454 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:249
#define ERROR
Definition: elog.h:45
TupleDesc rd_att
Definition: rel.h:111
FormData_pg_attribute
Definition: pg_attribute.h:181
#define elog(elevel,...)
Definition: elog.h:227

◆ checkNameSpaceConflicts()

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

Definition at line 414 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, lfirst, 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 = rte1->eref->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 
434  if (!nsitem2->p_rel_visible)
435  continue;
436  if (strcmp(rte2->eref->aliasname, aliasname1) != 0)
437  continue; /* definitely no conflict */
438  if (rte1->rtekind == RTE_RELATION && rte1->alias == NULL &&
439  rte2->rtekind == RTE_RELATION && rte2->alias == NULL &&
440  rte1->relid != rte2->relid)
441  continue; /* no conflict per SQL rule */
442  ereport(ERROR,
443  (errcode(ERRCODE_DUPLICATE_ALIAS),
444  errmsg("table name \"%s\" specified more than once",
445  aliasname1)));
446  }
447  }
448 }
Alias * alias
Definition: parsenodes.h:1119
int errcode(int sqlerrcode)
Definition: elog.c:694
RangeTblEntry * p_rte
Definition: parse_node.h:258
#define ERROR
Definition: elog.h:45
#define ereport(elevel,...)
Definition: elog.h:155
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
RTEKind rtekind
Definition: parsenodes.h:981
int errmsg(const char *fmt,...)
Definition: elog.c:905
Alias * eref
Definition: parsenodes.h:1120

◆ colNameToVar()

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

Definition at line 849 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().

851 {
852  Node *result = NULL;
853  int sublevels_up = 0;
854  ParseState *orig_pstate = pstate;
855 
856  while (pstate != NULL)
857  {
858  ListCell *l;
859 
860  foreach(l, pstate->p_namespace)
861  {
863  Node *newresult;
864 
865  /* Ignore table-only items */
866  if (!nsitem->p_cols_visible)
867  continue;
868  /* If not inside LATERAL, ignore lateral-only items */
869  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
870  continue;
871 
872  /* use orig_pstate here for consistency with other callers */
873  newresult = scanNSItemForColumn(orig_pstate, nsitem, sublevels_up,
874  colname, location);
875 
876  if (newresult)
877  {
878  if (result)
879  ereport(ERROR,
880  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
881  errmsg("column reference \"%s\" is ambiguous",
882  colname),
883  parser_errposition(pstate, location)));
884  check_lateral_ref_ok(pstate, nsitem, location);
885  result = newresult;
886  }
887  }
888 
889  if (result != NULL || localonly)
890  break; /* found, or don't want to look at parent */
891 
892  pstate = pstate->parentParseState;
893  sublevels_up++;
894  }
895 
896  return result;
897 }
ParseState * parentParseState
Definition: parse_node.h:179
Definition: nodes.h:533
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
int errcode(int sqlerrcode)
Definition: elog.c:694
#define ERROR
Definition: elog.h:45
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem, int location)
List * p_namespace
Definition: parse_node.h:185
#define ereport(elevel,...)
Definition: elog.h:155
bool p_lateral_active
Definition: parse_node.h:187
#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:905

◆ errorMissingColumn()

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

Definition at line 3542 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().

3544 {
3546  char *closestfirst = NULL;
3547 
3548  /*
3549  * Search the entire rtable looking for possible matches. If we find one,
3550  * emit a hint about it.
3551  *
3552  * TODO: improve this code (and also errorMissingRTE) to mention using
3553  * LATERAL if appropriate.
3554  */
3555  state = searchRangeTableForCol(pstate, relname, colname, location);
3556 
3557  /*
3558  * Extract closest col string for best match, if any.
3559  *
3560  * Infer an exact match referenced despite not being visible from the fact
3561  * that an attribute number was not present in state passed back -- this
3562  * is what is reported when !closestfirst. There might also be an exact
3563  * match that was qualified with an incorrect alias, in which case
3564  * closestfirst will be set (so hint is the same as generic fuzzy case).
3565  */
3566  if (state->rfirst && AttributeNumberIsValid(state->first))
3567  closestfirst = strVal(list_nth(state->rfirst->eref->colnames,
3568  state->first - 1));
3569 
3570  if (!state->rsecond)
3571  {
3572  /*
3573  * Handle case where there is zero or one column suggestions to hint,
3574  * including exact matches referenced but not visible.
3575  */
3576  ereport(ERROR,
3577  (errcode(ERRCODE_UNDEFINED_COLUMN),
3578  relname ?
3579  errmsg("column %s.%s does not exist", relname, colname) :
3580  errmsg("column \"%s\" does not exist", colname),
3581  state->rfirst ? closestfirst ?
3582  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3583  state->rfirst->eref->aliasname, closestfirst) :
3584  errhint("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3585  colname, state->rfirst->eref->aliasname) : 0,
3586  parser_errposition(pstate, location)));
3587  }
3588  else
3589  {
3590  /* Handle case where there are two equally useful column hints */
3591  char *closestsecond;
3592 
3593  closestsecond = strVal(list_nth(state->rsecond->eref->colnames,
3594  state->second - 1));
3595 
3596  ereport(ERROR,
3597  (errcode(ERRCODE_UNDEFINED_COLUMN),
3598  relname ?
3599  errmsg("column %s.%s does not exist", relname, colname) :
3600  errmsg("column \"%s\" does not exist", colname),
3601  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3602  state->rfirst->eref->aliasname, closestfirst,
3603  state->rsecond->eref->aliasname, closestsecond),
3604  parser_errposition(pstate, location)));
3605  }
3606 }
int errhint(const char *fmt,...)
Definition: elog.c:1152
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:694
NameData relname
Definition: pg_class.h:38
#define ERROR
Definition: elog.h:45
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:155
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:905
Alias * eref
Definition: parsenodes.h:1120

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3481 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().

3482 {
3483  RangeTblEntry *rte;
3484  const char *badAlias = NULL;
3485 
3486  /*
3487  * Check to see if there are any potential matches in the query's
3488  * rangetable. (Note: cases involving a bad schema name in the RangeVar
3489  * will throw error immediately here. That seems OK.)
3490  */
3491  rte = searchRangeTableForRel(pstate, relation);
3492 
3493  /*
3494  * If we found a match that has an alias and the alias is visible in the
3495  * namespace, then the problem is probably use of the relation's real name
3496  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3497  * common enough to justify a specific hint.
3498  *
3499  * If we found a match that doesn't meet those criteria, assume the
3500  * problem is illegal use of a relation outside its scope, as in the
3501  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3502  */
3503  if (rte && rte->alias &&
3504  strcmp(rte->eref->aliasname, relation->relname) != 0)
3505  {
3506  ParseNamespaceItem *nsitem;
3507  int sublevels_up;
3508 
3509  nsitem = refnameNamespaceItem(pstate, NULL, rte->eref->aliasname,
3510  relation->location,
3511  &sublevels_up);
3512  if (nsitem && nsitem->p_rte == rte)
3513  badAlias = rte->eref->aliasname;
3514  }
3515 
3516  if (rte)
3517  ereport(ERROR,
3519  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3520  relation->relname),
3521  (badAlias ?
3522  errhint("Perhaps you meant to reference the table alias \"%s\".",
3523  badAlias) :
3524  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3525  rte->eref->aliasname)),
3526  parser_errposition(pstate, relation->location)));
3527  else
3528  ereport(ERROR,
3530  errmsg("missing FROM-clause entry for table \"%s\"",
3531  relation->relname),
3532  parser_errposition(pstate, relation->location)));
3533 }
int errhint(const char *fmt,...)
Definition: elog.c:1152
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:75
Alias * alias
Definition: parsenodes.h:1119
int errcode(int sqlerrcode)
Definition: elog.c:694
int location
Definition: primnodes.h:73
char * relname
Definition: primnodes.h:68
RangeTblEntry * p_rte
Definition: parse_node.h:258
static RangeTblEntry * searchRangeTableForRel(ParseState *pstate, RangeVar *relation)
#define ERROR
Definition: elog.h:45
#define ereport(elevel,...)
Definition: elog.h:155
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:905
Alias * eref
Definition: parsenodes.h:1120

◆ expandNSItemAttrs()

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

Definition at line 3088 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().

3090 {
3091  RangeTblEntry *rte = nsitem->p_rte;
3092  List *names,
3093  *vars;
3094  ListCell *name,
3095  *var;
3096  List *te_list = NIL;
3097 
3098  vars = expandNSItemVars(nsitem, sublevels_up, location, &names);
3099 
3100  /*
3101  * Require read access to the table. This is normally redundant with the
3102  * markVarForSelectPriv calls below, but not if the table has zero
3103  * columns. We need not do anything if the nsitem is for a join: its
3104  * component tables will have been marked ACL_SELECT when they were added
3105  * to the rangetable. (This step changes things only for the target
3106  * relation of UPDATE/DELETE, which cannot be under a join.)
3107  */
3108  if (rte->rtekind == RTE_RELATION)
3109  rte->requiredPerms |= ACL_SELECT;
3110 
3111  forboth(name, names, var, vars)
3112  {
3113  char *label = strVal(lfirst(name));
3114  Var *varnode = (Var *) lfirst(var);
3115  TargetEntry *te;
3116 
3117  te = makeTargetEntry((Expr *) varnode,
3118  (AttrNumber) pstate->p_next_resno++,
3119  label,
3120  false);
3121  te_list = lappend(te_list, te);
3122 
3123  /* Require read access to each column */
3124  markVarForSelectPriv(pstate, varnode);
3125  }
3126 
3127  Assert(name == NULL && var == NULL); /* lists not the same length? */
3128 
3129  return te_list;
3130 }
#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:1124
Definition: primnodes.h:181
RangeTblEntry * p_rte
Definition: parse_node.h:258
int p_next_resno
Definition: parse_node.h:196
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:75
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:981
Definition: regcomp.c:238
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 3029 of file parse_relation.c.

References Assert, Alias::colnames, RangeTblEntry::eref, lappend(), lfirst, Var::location, makeVar(), NIL, ParseNamespaceColumn::p_dontexpand, ParseNamespaceItem::p_nscolumns, ParseNamespaceItem::p_rte, 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().

3032 {
3033  List *result = NIL;
3034  int colindex;
3035  ListCell *lc;
3036 
3037  if (colnames)
3038  *colnames = NIL;
3039  colindex = 0;
3040  foreach(lc, nsitem->p_rte->eref->colnames)
3041  {
3042  Value *colnameval = (Value *) lfirst(lc);
3043  const char *colname = strVal(colnameval);
3044  ParseNamespaceColumn *nscol = nsitem->p_nscolumns + colindex;
3045 
3046  if (nscol->p_dontexpand)
3047  {
3048  /* skip */
3049  }
3050  else if (colname[0])
3051  {
3052  Var *var;
3053 
3054  Assert(nscol->p_varno > 0);
3055  var = makeVar(nscol->p_varno,
3056  nscol->p_varattno,
3057  nscol->p_vartype,
3058  nscol->p_vartypmod,
3059  nscol->p_varcollid,
3060  sublevels_up);
3061  /* makeVar doesn't offer parameters for these, so set by hand: */
3062  var->varnosyn = nscol->p_varnosyn;
3063  var->varattnosyn = nscol->p_varattnosyn;
3064  var->location = location;
3065  result = lappend(result, var);
3066  if (colnames)
3067  *colnames = lappend(*colnames, colnameval);
3068  }
3069  else
3070  {
3071  /* dropped column, ignore */
3072  Assert(nscol->p_varno == 0);
3073  }
3074  colindex++;
3075  }
3076  return result;
3077 }
#define NIL
Definition: pg_list.h:65
List * colnames
Definition: primnodes.h:43
#define strVal(v)
Definition: value.h:54
Definition: primnodes.h:181
RangeTblEntry * p_rte
Definition: parse_node.h:258
int location
Definition: primnodes.h:196
AttrNumber p_varattno
Definition: parse_node.h:292
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
Index varnosyn
Definition: primnodes.h:194
List * lappend(List *list, void *datum)
Definition: list.c:336
AttrNumber varattnosyn
Definition: primnodes.h:195
#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:261
AttrNumber p_varattnosyn
Definition: parse_node.h:297
Alias * eref
Definition: parsenodes.h:1120
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 2571 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(), and ReplaceVarsFromTargetList_callback().

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

537 {
538  Index levelsup;
539  ListCell *lc;
540 
541  Assert(rte->rtekind == RTE_CTE);
542  levelsup = rte->ctelevelsup + rtelevelsup;
543  while (levelsup-- > 0)
544  {
545  pstate = pstate->parentParseState;
546  if (!pstate) /* shouldn't happen */
547  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
548  }
549  foreach(lc, pstate->p_ctenamespace)
550  {
551  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
552 
553  if (strcmp(cte->ctename, rte->ctename) == 0)
554  return cte;
555  }
556  /* shouldn't happen */
557  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
558  return NULL; /* keep compiler quiet */
559 }
ParseState * parentParseState
Definition: parse_node.h:179
#define ERROR
Definition: elog.h:45
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:1085
RTEKind rtekind
Definition: parsenodes.h:981
char * ctename
Definition: parsenodes.h:1084
#define elog(elevel,...)
Definition: elog.h:227
List * p_ctenamespace
Definition: parse_node.h:188

◆ GetNSItemByRangeTablePosn()

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

Definition at line 489 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().

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

◆ GetRTEByRangeTablePosn()

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

Definition at line 516 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().

519 {
520  while (sublevels_up-- > 0)
521  {
522  pstate = pstate->parentParseState;
523  Assert(pstate != NULL);
524  }
525  Assert(varno > 0 && varno <= list_length(pstate->p_rtable));
526  return rt_fetch(varno, pstate->p_rtable);
527 }
ParseState * parentParseState
Definition: parse_node.h:179
#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:181

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

Definition at line 2487 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().

2488 {
2489  ListCell *l;
2490 
2491  /*
2492  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2493  * parent level, then act as though there's a generic FOR UPDATE here.
2494  */
2495  if (pstate->p_locked_from_parent)
2496  return true;
2497 
2498  foreach(l, pstate->p_locking_clause)
2499  {
2500  LockingClause *lc = (LockingClause *) lfirst(l);
2501 
2502  if (lc->lockedRels == NIL)
2503  {
2504  /* all tables used in query */
2505  return true;
2506  }
2507  else
2508  {
2509  /* just the named tables */
2510  ListCell *l2;
2511 
2512  foreach(l2, lc->lockedRels)
2513  {
2514  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2515 
2516  if (strcmp(refname, thisrel->relname) == 0)
2517  return true;
2518  }
2519  }
2520  }
2521  return false;
2522 }
List * lockedRels
Definition: parsenodes.h:751
#define NIL
Definition: pg_list.h:65
bool p_locked_from_parent
Definition: parse_node.h:199
char * relname
Definition: primnodes.h:68
List * p_locking_clause
Definition: parse_node.h:198
#define lfirst(lc)
Definition: pg_list.h:169

◆ isQueryUsingTempRelation()

bool isQueryUsingTempRelation ( Query query)

Definition at line 3614 of file parse_relation.c.

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

3615 {
3616  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3617 }
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
Definition: nodes.h:533

◆ markVarForSelectPriv()

void markVarForSelectPriv ( ParseState pstate,
Var var 
)

Definition at line 1079 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().

1080 {
1081  Index lv;
1082 
1083  Assert(IsA(var, Var));
1084  /* Find the appropriate pstate if it's an uplevel Var */
1085  for (lv = 0; lv < var->varlevelsup; lv++)
1086  pstate = pstate->parentParseState;
1087  markRTEForSelectPriv(pstate, var->varno, var->varattno);
1088 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
Index varlevelsup
Definition: primnodes.h:191
ParseState * parentParseState
Definition: parse_node.h:179
AttrNumber varattno
Definition: primnodes.h:186
static void markRTEForSelectPriv(ParseState *pstate, int rtindex, AttrNumber col)
Definition: primnodes.h:181
Index varno
Definition: primnodes.h:184
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 1340 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().

1341 {
1342  Relation rel;
1343  ParseCallbackState pcbstate;
1344 
1345  setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1346  rel = table_openrv_extended(relation, lockmode, true);
1347  if (rel == NULL)
1348  {
1349  if (relation->schemaname)
1350  ereport(ERROR,
1352  errmsg("relation \"%s.%s\" does not exist",
1353  relation->schemaname, relation->relname)));
1354  else
1355  {
1356  /*
1357  * An unqualified name might have been meant as a reference to
1358  * some not-yet-in-scope CTE. The bare "does not exist" message
1359  * has proven remarkably unhelpful for figuring out such problems,
1360  * so we take pains to offer a specific hint.
1361  */
1362  if (isFutureCTE(pstate, relation->relname))
1363  ereport(ERROR,
1365  errmsg("relation \"%s\" does not exist",
1366  relation->relname),
1367  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1368  relation->relname),
1369  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1370  else
1371  ereport(ERROR,
1373  errmsg("relation \"%s\" does not exist",
1374  relation->relname)));
1375  }
1376  }
1378  return rel;
1379 }
int errhint(const char *fmt,...)
Definition: elog.c:1152
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:75
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:694
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:45
int errdetail(const char *fmt,...)
Definition: elog.c:1038
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:145
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:905

◆ refnameNamespaceItem()

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

Definition at line 108 of file parse_relation.c.

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

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

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

◆ 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:179
unsigned int Index
Definition: c.h:549
#define lfirst(lc)
Definition: pg_list.h:169
List * p_ctenamespace
Definition: parse_node.h:188

◆ 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 667 of file parse_relation.c.

References Alias::aliasname, attnum, RangeTblEntry::eref, 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_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().

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