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

Go to the source code of this file.

Functions

ParseNamespaceItemrefnameNamespaceItem (ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
 
CommonTableExprscanNameSpaceForCTE (ParseState *pstate, const char *refname, Index *ctelevelsup)
 
bool scanNameSpaceForENR (ParseState *pstate, const char *refname)
 
void checkNameSpaceConflicts (ParseState *pstate, List *namespace1, List *namespace2)
 
ParseNamespaceItemGetNSItemByRangeTablePosn (ParseState *pstate, int varno, int sublevels_up)
 
RangeTblEntryGetRTEByRangeTablePosn (ParseState *pstate, int varno, int sublevels_up)
 
CommonTableExprGetCTEForRTE (ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
 
NodescanNSItemForColumn (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
 
NodecolNameToVar (ParseState *pstate, const char *colname, bool localonly, int location)
 
void markVarForSelectPriv (ParseState *pstate, Var *var)
 
Relation parserOpenTable (ParseState *pstate, const RangeVar *relation, int lockmode)
 
ParseNamespaceItemaddRangeTableEntry (ParseState *pstate, RangeVar *relation, Alias *alias, bool inh, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForRelation (ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForSubquery (ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForFunction (ParseState *pstate, List *funcnames, List *funcexprs, List *coldeflists, RangeFunction *rangefunc, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForValues (ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForTableFunc (ParseState *pstate, TableFunc *tf, Alias *alias, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForJoin (ParseState *pstate, List *colnames, ParseNamespaceColumn *nscolumns, JoinType jointype, int nummergedcols, List *aliasvars, List *leftcols, List *rightcols, Alias *joinalias, Alias *alias, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForCTE (ParseState *pstate, CommonTableExpr *cte, Index levelsup, RangeVar *rv, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForENR (ParseState *pstate, RangeVar *rv, bool inFromCl)
 
bool isLockedRefname (ParseState *pstate, const char *refname)
 
void addNSItemToQuery (ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
 
void errorMissingRTE (ParseState *pstate, RangeVar *relation) pg_attribute_noreturn()
 
void errorMissingColumn (ParseState *pstate, const char *relname, const char *colname, int location) pg_attribute_noreturn()
 
void expandRTE (RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
 
ListexpandNSItemVars (ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
 
ListexpandNSItemAttrs (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, bool require_col_privs, 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 2571 of file parse_relation.c.

2574 {
2575  if (addToJoinList)
2576  {
2578 
2579  rtr->rtindex = nsitem->p_rtindex;
2580  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2581  }
2582  if (addToRelNameSpace || addToVarNameSpace)
2583  {
2584  /* Set the new nsitem's visibility flags correctly */
2585  nsitem->p_rel_visible = addToRelNameSpace;
2586  nsitem->p_cols_visible = addToVarNameSpace;
2587  nsitem->p_lateral_only = false;
2588  nsitem->p_lateral_ok = true;
2589  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2590  }
2591 }
List * lappend(List *list, void *datum)
Definition: list.c:338
#define makeNode(_type_)
Definition: nodes.h:622
List * p_namespace
Definition: parse_node.h:187
List * p_joinlist
Definition: parse_node.h:185

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(), CreateTriggerFiringOn(), DefineRelation(), DoCopy(), setTargetTable(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformIndexStmt(), transformInsertStmt(), transformMergeStmt(), transformOnConflictClause(), transformPartitionSpec(), TransformPubWhereClauses(), transformRuleStmt(), transformSetOperationStmt(), transformStatsStmt(), and transformValuesClause().

◆ addRangeTableEntry()

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

Definition at line 1413 of file parse_relation.c.

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

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

◆ addRangeTableEntryForCTE()

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

Definition at line 2260 of file parse_relation.c.

2265 {
2267  Alias *alias = rv->alias;
2268  char *refname = alias ? alias->aliasname : cte->ctename;
2269  Alias *eref;
2270  int numaliases;
2271  int varattno;
2272  ListCell *lc;
2273  int n_dontexpand_columns = 0;
2274  ParseNamespaceItem *psi;
2275 
2276  Assert(pstate != NULL);
2277 
2278  rte->rtekind = RTE_CTE;
2279  rte->ctename = cte->ctename;
2280  rte->ctelevelsup = levelsup;
2281 
2282  /* Self-reference if and only if CTE's parse analysis isn't completed */
2283  rte->self_reference = !IsA(cte->ctequery, Query);
2284  Assert(cte->cterecursive || !rte->self_reference);
2285  /* Bump the CTE's refcount if this isn't a self-reference */
2286  if (!rte->self_reference)
2287  cte->cterefcount++;
2288 
2289  /*
2290  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
2291  * This won't get checked in case of a self-reference, but that's OK
2292  * because data-modifying CTEs aren't allowed to be recursive anyhow.
2293  */
2294  if (IsA(cte->ctequery, Query))
2295  {
2296  Query *ctequery = (Query *) cte->ctequery;
2297 
2298  if (ctequery->commandType != CMD_SELECT &&
2299  ctequery->returningList == NIL)
2300  ereport(ERROR,
2301  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2302  errmsg("WITH query \"%s\" does not have a RETURNING clause",
2303  cte->ctename),
2304  parser_errposition(pstate, rv->location)));
2305  }
2306 
2307  rte->coltypes = list_copy(cte->ctecoltypes);
2308  rte->coltypmods = list_copy(cte->ctecoltypmods);
2310 
2311  rte->alias = alias;
2312  if (alias)
2313  eref = copyObject(alias);
2314  else
2315  eref = makeAlias(refname, NIL);
2316  numaliases = list_length(eref->colnames);
2317 
2318  /* fill in any unspecified alias columns */
2319  varattno = 0;
2320  foreach(lc, cte->ctecolnames)
2321  {
2322  varattno++;
2323  if (varattno > numaliases)
2324  eref->colnames = lappend(eref->colnames, lfirst(lc));
2325  }
2326  if (varattno < numaliases)
2327  ereport(ERROR,
2328  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2329  errmsg("table \"%s\" has %d columns available but %d columns specified",
2330  refname, varattno, numaliases)));
2331 
2332  rte->eref = eref;
2333 
2334  if (cte->search_clause)
2335  {
2338  rte->coltypes = lappend_oid(rte->coltypes, RECORDOID);
2339  else
2340  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2341  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2343 
2344  n_dontexpand_columns += 1;
2345  }
2346 
2347  if (cte->cycle_clause)
2348  {
2353 
2355  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2356  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2358 
2359  n_dontexpand_columns += 2;
2360  }
2361 
2362  /*
2363  * Set flags and access permissions.
2364  *
2365  * Subqueries are never checked for access rights.
2366  */
2367  rte->lateral = false;
2368  rte->inh = false; /* never true for subqueries */
2369  rte->inFromCl = inFromCl;
2370 
2371  rte->requiredPerms = 0;
2372  rte->checkAsUser = InvalidOid;
2373  rte->selectedCols = NULL;
2374  rte->insertedCols = NULL;
2375  rte->updatedCols = NULL;
2376  rte->extraUpdatedCols = NULL;
2377 
2378  /*
2379  * Add completed RTE to pstate's range table list, so that we know its
2380  * index. But we don't add it to the join list --- caller must do that if
2381  * appropriate.
2382  */
2383  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2384 
2385  /*
2386  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2387  * list --- caller must do that if appropriate.
2388  */
2389  psi = buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2390  rte->coltypes, rte->coltypmods,
2391  rte->colcollations);
2392 
2393  /*
2394  * The columns added by search and cycle clauses are not included in star
2395  * expansion in queries contained in the CTE.
2396  */
2397  if (rte->ctelevelsup > 0)
2398  for (int i = 0; i < n_dontexpand_columns; i++)
2399  psi->p_nscolumns[list_length(psi->p_names->colnames) - 1 - i].p_dontexpand = true;
2400 
2401  return psi;
2402 }
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
List * lappend_int(List *list, int datum)
Definition: list.c:356
List * lappend_oid(List *list, Oid datum)
Definition: list.c:374
List * list_copy(const List *oldlist)
Definition: list.c:1572
#define IsA(nodeptr, _type_)
Definition: nodes.h:625
#define copyObject(obj)
Definition: nodes.h:690
@ CMD_SELECT
Definition: nodes.h:722
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
@ RTE_CTE
Definition: parsenodes.h:1005
#define lfirst(lc)
Definition: pg_list.h:170
List * colnames
Definition: primnodes.h:43
char * cycle_path_column
Definition: parsenodes.h:1512
Oid cycle_mark_collation
Definition: parsenodes.h:1517
char * cycle_mark_column
Definition: parsenodes.h:1509
char * search_seq_column
Definition: parsenodes.h:1501
bool search_breadth_first
Definition: parsenodes.h:1500
List * ctecoltypes
Definition: parsenodes.h:1537
CTECycleClause * cycle_clause
Definition: parsenodes.h:1530
CTESearchClause * search_clause
Definition: parsenodes.h:1529
List * ctecolcollations
Definition: parsenodes.h:1539
List * ctecolnames
Definition: parsenodes.h:1536
List * ctecoltypmods
Definition: parsenodes.h:1538
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:272
List * returningList
Definition: parsenodes.h:162
CmdType commandType
Definition: parsenodes.h:121
List * colcollations
Definition: parsenodes.h:1150
char * ctename
Definition: parsenodes.h:1126
bool self_reference
Definition: parsenodes.h:1128
Index ctelevelsup
Definition: parsenodes.h:1127
List * coltypes
Definition: parsenodes.h:1148
List * coltypmods
Definition: parsenodes.h:1149
int location
Definition: primnodes.h:86
Alias * alias
Definition: primnodes.h:83
String * makeString(char *str)
Definition: value.c:63

References RangeTblEntry::alias, RangeVar::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, if(), RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, IsA, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, list_copy(), list_length(), RangeVar::location, makeAlias(), makeNode, makeString(), NIL, ParseNamespaceColumn::p_dontexpand, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseState::p_rtable, parser_errposition(), RangeTblEntry::requiredPerms, Query::returningList, RTE_CTE, RangeTblEntry::rtekind, CTESearchClause::search_breadth_first, CommonTableExpr::search_clause, CTESearchClause::search_seq_column, RangeTblEntry::selectedCols, RangeTblEntry::self_reference, and RangeTblEntry::updatedCols.

Referenced by getNSItemForSpecialRelationTypes().

◆ addRangeTableEntryForENR()

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

Definition at line 2418 of file parse_relation.c.

2421 {
2423  Alias *alias = rv->alias;
2424  char *refname = alias ? alias->aliasname : rv->relname;
2426  TupleDesc tupdesc;
2427  int attno;
2428 
2429  Assert(pstate != NULL);
2430  enrmd = get_visible_ENR(pstate, rv->relname);
2431  Assert(enrmd != NULL);
2432 
2433  switch (enrmd->enrtype)
2434  {
2435  case ENR_NAMED_TUPLESTORE:
2437  break;
2438 
2439  default:
2440  elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2441  return NULL; /* for fussy compilers */
2442  }
2443 
2444  /*
2445  * Record dependency on a relation. This allows plans to be invalidated
2446  * if they access transition tables linked to a table that is altered.
2447  */
2448  rte->relid = enrmd->reliddesc;
2449 
2450  /*
2451  * Build the list of effective column names using user-supplied aliases
2452  * and/or actual column names.
2453  */
2454  tupdesc = ENRMetadataGetTupDesc(enrmd);
2455  rte->eref = makeAlias(refname, NIL);
2456  buildRelationAliases(tupdesc, alias, rte->eref);
2457 
2458  /* Record additional data for ENR, including column type info */
2459  rte->enrname = enrmd->name;
2460  rte->enrtuples = enrmd->enrtuples;
2461  rte->coltypes = NIL;
2462  rte->coltypmods = NIL;
2463  rte->colcollations = NIL;
2464  for (attno = 1; attno <= tupdesc->natts; ++attno)
2465  {
2466  Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2467 
2468  if (att->attisdropped)
2469  {
2470  /* Record zeroes for a dropped column */
2471  rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2472  rte->coltypmods = lappend_int(rte->coltypmods, 0);
2474  }
2475  else
2476  {
2477  /* Let's just make sure we can tell this isn't dropped */
2478  if (att->atttypid == InvalidOid)
2479  elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2480  rv->relname);
2481  rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2482  rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2484  att->attcollation);
2485  }
2486  }
2487 
2488  /*
2489  * Set flags and access permissions.
2490  *
2491  * ENRs are never checked for access rights.
2492  */
2493  rte->lateral = false;
2494  rte->inh = false; /* never true for ENRs */
2495  rte->inFromCl = inFromCl;
2496 
2497  rte->requiredPerms = 0;
2498  rte->checkAsUser = InvalidOid;
2499  rte->selectedCols = NULL;
2500 
2501  /*
2502  * Add completed RTE to pstate's range table list, so that we know its
2503  * index. But we don't add it to the join list --- caller must do that if
2504  * appropriate.
2505  */
2506  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2507 
2508  /*
2509  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2510  * list --- caller must do that if appropriate.
2511  */
2512  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
2513  tupdesc);
2514 }
EphemeralNamedRelationMetadata get_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:26
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1006
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
@ ENR_NAMED_TUPLESTORE
EphemeralNameRelationType enrtype
char * enrname
Definition: parsenodes.h:1155
Cardinality enrtuples
Definition: parsenodes.h:1156
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References RangeVar::alias, Alias::aliasname, Assert(), buildNSItemFromTupleDesc(), buildRelationAliases(), RangeTblEntry::checkAsUser, RangeTblEntry::colcollations, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, elog(), ENR_NAMED_TUPLESTORE, ENRMetadataGetTupDesc(), RangeTblEntry::enrname, RangeTblEntry::enrtuples, EphemeralNamedRelationMetadataData::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().

◆ addRangeTableEntryForFunction()

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

Definition at line 1679 of file parse_relation.c.

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

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

◆ addRangeTableEntryForJoin()

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

Definition at line 2156 of file parse_relation.c.

2167 {
2169  Alias *eref;
2170  int numaliases;
2171  ParseNamespaceItem *nsitem;
2172 
2173  Assert(pstate != NULL);
2174 
2175  /*
2176  * Fail if join has too many columns --- we must be able to reference any
2177  * of the columns with an AttrNumber.
2178  */
2179  if (list_length(aliasvars) > MaxAttrNumber)
2180  ereport(ERROR,
2181  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2182  errmsg("joins can have at most %d columns",
2183  MaxAttrNumber)));
2184 
2185  rte->rtekind = RTE_JOIN;
2186  rte->relid = InvalidOid;
2187  rte->subquery = NULL;
2188  rte->jointype = jointype;
2189  rte->joinmergedcols = nummergedcols;
2190  rte->joinaliasvars = aliasvars;
2191  rte->joinleftcols = leftcols;
2192  rte->joinrightcols = rightcols;
2193  rte->join_using_alias = join_using_alias;
2194  rte->alias = alias;
2195 
2196  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
2197  numaliases = list_length(eref->colnames);
2198 
2199  /* fill in any unspecified alias columns */
2200  if (numaliases < list_length(colnames))
2201  eref->colnames = list_concat(eref->colnames,
2202  list_copy_tail(colnames, numaliases));
2203 
2204  if (numaliases > list_length(colnames))
2205  ereport(ERROR,
2206  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2207  errmsg("join expression \"%s\" has %d columns available but %d columns specified",
2208  eref->aliasname, list_length(colnames), numaliases)));
2209 
2210  rte->eref = eref;
2211 
2212  /*
2213  * Set flags and access permissions.
2214  *
2215  * Joins are never checked for access rights.
2216  */
2217  rte->lateral = false;
2218  rte->inh = false; /* never true for joins */
2219  rte->inFromCl = inFromCl;
2220 
2221  rte->requiredPerms = 0;
2222  rte->checkAsUser = InvalidOid;
2223  rte->selectedCols = NULL;
2224  rte->insertedCols = NULL;
2225  rte->updatedCols = NULL;
2226  rte->extraUpdatedCols = NULL;
2227 
2228  /*
2229  * Add completed RTE to pstate's range table list, so that we know its
2230  * index. But we don't add it to the join list --- caller must do that if
2231  * appropriate.
2232  */
2233  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2234 
2235  /*
2236  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2237  * list --- caller must do that if appropriate.
2238  */
2239  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2240  nsitem->p_names = rte->eref;
2241  nsitem->p_rte = rte;
2242  nsitem->p_rtindex = list_length(pstate->p_rtable);
2243  nsitem->p_nscolumns = nscolumns;
2244  /* set default visibility flags; might get changed later */
2245  nsitem->p_rel_visible = true;
2246  nsitem->p_cols_visible = true;
2247  nsitem->p_lateral_only = false;
2248  nsitem->p_lateral_ok = true;
2249 
2250  return nsitem;
2251 }
#define MaxAttrNumber
Definition: attnum.h:24
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1591
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
@ RTE_JOIN
Definition: parsenodes.h:1001
RangeTblEntry * p_rte
Definition: parse_node.h:269
Alias * join_using_alias
Definition: parsenodes.h:1100
List * joinrightcols
Definition: parsenodes.h:1093
List * joinaliasvars
Definition: parsenodes.h:1091
JoinType jointype
Definition: parsenodes.h:1089
List * joinleftcols
Definition: parsenodes.h:1092

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

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

◆ addRangeTableEntryForRelation()

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

Definition at line 1513 of file parse_relation.c.

1519 {
1521  char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1522 
1523  Assert(pstate != NULL);
1524 
1525  Assert(lockmode == AccessShareLock ||
1526  lockmode == RowShareLock ||
1527  lockmode == RowExclusiveLock);
1528  Assert(CheckRelationLockedByMe(rel, lockmode, true));
1529 
1530  rte->rtekind = RTE_RELATION;
1531  rte->alias = alias;
1532  rte->relid = RelationGetRelid(rel);
1533  rte->relkind = rel->rd_rel->relkind;
1534  rte->rellockmode = lockmode;
1535 
1536  /*
1537  * Build the list of effective column names using user-supplied aliases
1538  * and/or actual column names.
1539  */
1540  rte->eref = makeAlias(refname, NIL);
1541  buildRelationAliases(rel->rd_att, alias, rte->eref);
1542 
1543  /*
1544  * Set flags and access permissions.
1545  *
1546  * The initial default on access checks is always check-for-READ-access,
1547  * which is the right thing for all except target tables.
1548  */
1549  rte->lateral = false;
1550  rte->inh = inh;
1551  rte->inFromCl = inFromCl;
1552 
1553  rte->requiredPerms = ACL_SELECT;
1554  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1555  rte->selectedCols = NULL;
1556  rte->insertedCols = NULL;
1557  rte->updatedCols = NULL;
1558  rte->extraUpdatedCols = NULL;
1559 
1560  /*
1561  * Add completed RTE to pstate's range table list, so that we know its
1562  * index. But we don't add it to the join list --- caller must do that if
1563  * appropriate.
1564  */
1565  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1566 
1567  /*
1568  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1569  * list --- caller must do that if appropriate.
1570  */
1571  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1572  rel->rd_att);
1573 }
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:331
#define RowExclusiveLock
Definition: lockdefs.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:522

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(), CreateTriggerFiringOn(), DefineRelation(), DoCopy(), rewriteTargetView(), setTargetTable(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformIndexStmt(), transformOnConflictClause(), transformPartitionSpec(), TransformPubWhereClauses(), transformRuleStmt(), transformStatsStmt(), and UpdateRangeTableOfViewParse().

◆ addRangeTableEntryForSubquery()

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

Definition at line 1583 of file parse_relation.c.

1588 {
1590  char *refname = alias->aliasname;
1591  Alias *eref;
1592  int numaliases;
1593  List *coltypes,
1594  *coltypmods,
1595  *colcollations;
1596  int varattno;
1597  ListCell *tlistitem;
1598 
1599  Assert(pstate != NULL);
1600 
1601  rte->rtekind = RTE_SUBQUERY;
1602  rte->subquery = subquery;
1603  rte->alias = alias;
1604 
1605  eref = copyObject(alias);
1606  numaliases = list_length(eref->colnames);
1607 
1608  /* fill in any unspecified alias columns, and extract column type info */
1609  coltypes = coltypmods = colcollations = NIL;
1610  varattno = 0;
1611  foreach(tlistitem, subquery->targetList)
1612  {
1613  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1614 
1615  if (te->resjunk)
1616  continue;
1617  varattno++;
1618  Assert(varattno == te->resno);
1619  if (varattno > numaliases)
1620  {
1621  char *attrname;
1622 
1623  attrname = pstrdup(te->resname);
1624  eref->colnames = lappend(eref->colnames, makeString(attrname));
1625  }
1626  coltypes = lappend_oid(coltypes,
1627  exprType((Node *) te->expr));
1628  coltypmods = lappend_int(coltypmods,
1629  exprTypmod((Node *) te->expr));
1630  colcollations = lappend_oid(colcollations,
1631  exprCollation((Node *) te->expr));
1632  }
1633  if (varattno < numaliases)
1634  ereport(ERROR,
1635  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1636  errmsg("table \"%s\" has %d columns available but %d columns specified",
1637  refname, varattno, numaliases)));
1638 
1639  rte->eref = eref;
1640 
1641  /*
1642  * Set flags and access permissions.
1643  *
1644  * Subqueries are never checked for access rights.
1645  */
1646  rte->lateral = lateral;
1647  rte->inh = false; /* never true for subqueries */
1648  rte->inFromCl = inFromCl;
1649 
1650  rte->requiredPerms = 0;
1651  rte->checkAsUser = InvalidOid;
1652  rte->selectedCols = NULL;
1653  rte->insertedCols = NULL;
1654  rte->updatedCols = NULL;
1655  rte->extraUpdatedCols = NULL;
1656 
1657  /*
1658  * Add completed RTE to pstate's range table list, so that we know its
1659  * index. But we don't add it to the join list --- caller must do that if
1660  * appropriate.
1661  */
1662  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1663 
1664  /*
1665  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1666  * list --- caller must do that if appropriate.
1667  */
1668  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
1669  coltypes, coltypmods, colcollations);
1670 }
@ RTE_SUBQUERY
Definition: parsenodes.h:1000
List * targetList
Definition: parsenodes.h:156
Expr * expr
Definition: primnodes.h:1828
char * resname
Definition: primnodes.h:1830
AttrNumber resno
Definition: primnodes.h:1829
bool resjunk
Definition: primnodes.h:1835

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

◆ addRangeTableEntryForTableFunc()

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

Definition at line 1985 of file parse_relation.c.

1990 {
1992  char *refname;
1993  Alias *eref;
1994  int numaliases;
1995 
1996  refname = alias ? alias->aliasname :
1997  pstrdup(tf->functype == TFT_XMLTABLE ? "xmltable" : "json_table");
1998 
1999  Assert(pstate != NULL);
2000 
2001  rte->rtekind = RTE_TABLEFUNC;
2002  rte->relid = InvalidOid;
2003  rte->subquery = NULL;
2004  rte->tablefunc = tf;
2005  rte->coltypes = tf->coltypes;
2006  rte->coltypmods = tf->coltypmods;
2007  rte->colcollations = tf->colcollations;
2008  rte->alias = alias;
2009 
2010  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2011  numaliases = list_length(eref->colnames);
2012 
2013  /* fill in any unspecified alias columns */
2014  if (numaliases < list_length(tf->colnames))
2015  eref->colnames = list_concat(eref->colnames,
2016  list_copy_tail(tf->colnames, numaliases));
2017 
2018  if (numaliases > list_length(tf->colnames))
2019  ereport(ERROR,
2020  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2021  errmsg("%s function has %d columns available but %d columns specified",
2022  tf->functype == TFT_XMLTABLE ? "XMLTABLE" : "JSON_TABLE",
2023  list_length(tf->colnames), numaliases)));
2024 
2025  rte->eref = eref;
2026 
2027  /*
2028  * Set flags and access permissions.
2029  *
2030  * Tablefuncs are never checked for access rights (at least, not by the
2031  * RTE permissions mechanism).
2032  */
2033  rte->lateral = lateral;
2034  rte->inh = false; /* never true for tablefunc RTEs */
2035  rte->inFromCl = inFromCl;
2036 
2037  rte->requiredPerms = 0;
2038  rte->checkAsUser = InvalidOid;
2039  rte->selectedCols = NULL;
2040  rte->insertedCols = NULL;
2041  rte->updatedCols = NULL;
2042  rte->extraUpdatedCols = NULL;
2043 
2044  /*
2045  * Add completed RTE to pstate's range table list, so that we know its
2046  * index. But we don't add it to the join list --- caller must do that if
2047  * appropriate.
2048  */
2049  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2050 
2051  /*
2052  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2053  * list --- caller must do that if appropriate.
2054  */
2055  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2056  rte->coltypes, rte->coltypmods,
2057  rte->colcollations);
2058 }
@ RTE_TABLEFUNC
Definition: parsenodes.h:1003
@ TFT_XMLTABLE
Definition: primnodes.h:91
TableFunc * tablefunc
Definition: parsenodes.h:1116
List * coltypmods
Definition: primnodes.h:111
List * coltypes
Definition: primnodes.h:110
List * colnames
Definition: primnodes.h:109
List * colcollations
Definition: primnodes.h:112
TableFuncType functype
Definition: primnodes.h:104

References RangeTblEntry::alias, Alias::aliasname, Assert(), buildNSItemFromLists(), RangeTblEntry::checkAsUser, RangeTblEntry::colcollations, TableFunc::colcollations, Alias::colnames, TableFunc::colnames, RangeTblEntry::coltypes, TableFunc::coltypes, RangeTblEntry::coltypmods, TableFunc::coltypmods, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::extraUpdatedCols, TableFunc::functype, 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, TFT_XMLTABLE, and RangeTblEntry::updatedCols.

Referenced by transformJsonTable(), and transformRangeTableFunc().

◆ addRangeTableEntryForValues()

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

Definition at line 2067 of file parse_relation.c.

2075 {
2077  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
2078  Alias *eref;
2079  int numaliases;
2080  int numcolumns;
2081 
2082  Assert(pstate != NULL);
2083 
2084  rte->rtekind = RTE_VALUES;
2085  rte->relid = InvalidOid;
2086  rte->subquery = NULL;
2087  rte->values_lists = exprs;
2088  rte->coltypes = coltypes;
2089  rte->coltypmods = coltypmods;
2090  rte->colcollations = colcollations;
2091  rte->alias = alias;
2092 
2093  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2094 
2095  /* fill in any unspecified alias columns */
2096  numcolumns = list_length((List *) linitial(exprs));
2097  numaliases = list_length(eref->colnames);
2098  while (numaliases < numcolumns)
2099  {
2100  char attrname[64];
2101 
2102  numaliases++;
2103  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
2104  eref->colnames = lappend(eref->colnames,
2105  makeString(pstrdup(attrname)));
2106  }
2107  if (numcolumns < numaliases)
2108  ereport(ERROR,
2109  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2110  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
2111  refname, numcolumns, numaliases)));
2112 
2113  rte->eref = eref;
2114 
2115  /*
2116  * Set flags and access permissions.
2117  *
2118  * Subqueries are never checked for access rights.
2119  */
2120  rte->lateral = lateral;
2121  rte->inh = false; /* never true for values RTEs */
2122  rte->inFromCl = inFromCl;
2123 
2124  rte->requiredPerms = 0;
2125  rte->checkAsUser = InvalidOid;
2126  rte->selectedCols = NULL;
2127  rte->insertedCols = NULL;
2128  rte->updatedCols = NULL;
2129  rte->extraUpdatedCols = NULL;
2130 
2131  /*
2132  * Add completed RTE to pstate's range table list, so that we know its
2133  * index. But we don't add it to the join list --- caller must do that if
2134  * appropriate.
2135  */
2136  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2137 
2138  /*
2139  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2140  * list --- caller must do that if appropriate.
2141  */
2142  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2143  rte->coltypes, rte->coltypmods,
2144  rte->colcollations);
2145 }
@ RTE_VALUES
Definition: parsenodes.h:1004
#define snprintf
Definition: port.h:225
List * values_lists
Definition: parsenodes.h:1121

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

◆ attnameAttNum()

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

Definition at line 3414 of file parse_relation.c.

3415 {
3416  int i;
3417 
3418  for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3419  {
3421 
3422  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3423  return i + 1;
3424  }
3425 
3426  if (sysColOK)
3427  {
3429  return i;
3430  }
3431 
3432  /* on failure */
3433  return InvalidAttrNumber;
3434 }
#define InvalidAttrNumber
Definition: attnum.h:23
int namestrcmp(Name name, const char *str)
Definition: name.c:247
static int specialAttNum(const char *attname)
NameData attname
Definition: pg_attribute.h:41
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:494

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

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

◆ attnumAttName()

const NameData* attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3464 of file parse_relation.c.

3465 {
3466  if (attid <= 0)
3467  {
3468  const FormData_pg_attribute *sysatt;
3469 
3470  sysatt = SystemAttributeDefinition(attid);
3471  return &sysatt->attname;
3472  }
3473  if (attid > rd->rd_att->natts)
3474  elog(ERROR, "invalid attribute number %d", attid);
3475  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3476 }
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:239
FormData_pg_attribute
Definition: pg_attribute.h:191

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

Referenced by transformFkeyGetPrimaryKey().

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3506 of file parse_relation.c.

3507 {
3508  if (attid <= 0)
3509  {
3510  /* All system attributes are of noncollatable types. */
3511  return InvalidOid;
3512  }
3513  if (attid > rd->rd_att->natts)
3514  elog(ERROR, "invalid attribute number %d", attid);
3515  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3516 }

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

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3486 of file parse_relation.c.

3487 {
3488  if (attid <= 0)
3489  {
3490  const FormData_pg_attribute *sysatt;
3491 
3492  sysatt = SystemAttributeDefinition(attid);
3493  return sysatt->atttypid;
3494  }
3495  if (attid > rd->rd_att->natts)
3496  elog(ERROR, "invalid attribute number %d", attid);
3497  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3498 }

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

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

◆ checkNameSpaceConflicts()

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

Definition at line 414 of file parse_relation.c.

416 {
417  ListCell *l1;
418 
419  foreach(l1, namespace1)
420  {
421  ParseNamespaceItem *nsitem1 = (ParseNamespaceItem *) lfirst(l1);
422  RangeTblEntry *rte1 = nsitem1->p_rte;
423  const char *aliasname1 = nsitem1->p_names->aliasname;
424  ListCell *l2;
425 
426  if (!nsitem1->p_rel_visible)
427  continue;
428 
429  foreach(l2, namespace2)
430  {
431  ParseNamespaceItem *nsitem2 = (ParseNamespaceItem *) lfirst(l2);
432  RangeTblEntry *rte2 = nsitem2->p_rte;
433  const char *aliasname2 = nsitem2->p_names->aliasname;
434 
435  if (!nsitem2->p_rel_visible)
436  continue;
437  if (strcmp(aliasname2, aliasname1) != 0)
438  continue; /* definitely no conflict */
439  if (rte1->rtekind == RTE_RELATION && rte1->alias == NULL &&
440  rte2->rtekind == RTE_RELATION && rte2->alias == NULL &&
441  rte1->relid != rte2->relid)
442  continue; /* no conflict per SQL rule */
443  ereport(ERROR,
444  (errcode(ERRCODE_DUPLICATE_ALIAS),
445  errmsg("table name \"%s\" specified more than once",
446  aliasname1)));
447  }
448  }
449 }

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

Referenced by transformFromClause(), and transformFromClauseItem().

◆ colNameToVar()

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

Definition at line 868 of file parse_relation.c.

870 {
871  Node *result = NULL;
872  int sublevels_up = 0;
873  ParseState *orig_pstate = pstate;
874 
875  while (pstate != NULL)
876  {
877  ListCell *l;
878 
879  foreach(l, pstate->p_namespace)
880  {
882  Node *newresult;
883 
884  /* Ignore table-only items */
885  if (!nsitem->p_cols_visible)
886  continue;
887  /* If not inside LATERAL, ignore lateral-only items */
888  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
889  continue;
890 
891  /* use orig_pstate here for consistency with other callers */
892  newresult = scanNSItemForColumn(orig_pstate, nsitem, sublevels_up,
893  colname, location);
894 
895  if (newresult)
896  {
897  if (result)
898  ereport(ERROR,
899  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
900  errmsg("column reference \"%s\" is ambiguous",
901  colname),
902  parser_errposition(pstate, location)));
903  check_lateral_ref_ok(pstate, nsitem, location);
904  result = newresult;
905  }
906  }
907 
908  if (result != NULL || localonly)
909  break; /* found, or don't want to look at parent */
910 
911  pstate = pstate->parentParseState;
912  sublevels_up++;
913  }
914 
915  return result;
916 }
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem, int location)
ParseState * parentParseState
Definition: parse_node.h:181
bool p_lateral_active
Definition: parse_node.h:189

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

◆ errorMissingColumn()

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

Definition at line 3586 of file parse_relation.c.

3588 {
3590  char *closestfirst = NULL;
3591 
3592  /*
3593  * Search the entire rtable looking for possible matches. If we find one,
3594  * emit a hint about it.
3595  *
3596  * TODO: improve this code (and also errorMissingRTE) to mention using
3597  * LATERAL if appropriate.
3598  */
3599  state = searchRangeTableForCol(pstate, relname, colname, location);
3600 
3601  /*
3602  * Extract closest col string for best match, if any.
3603  *
3604  * Infer an exact match referenced despite not being visible from the fact
3605  * that an attribute number was not present in state passed back -- this
3606  * is what is reported when !closestfirst. There might also be an exact
3607  * match that was qualified with an incorrect alias, in which case
3608  * closestfirst will be set (so hint is the same as generic fuzzy case).
3609  */
3610  if (state->rfirst && AttributeNumberIsValid(state->first))
3611  closestfirst = strVal(list_nth(state->rfirst->eref->colnames,
3612  state->first - 1));
3613 
3614  if (!state->rsecond)
3615  {
3616  /*
3617  * Handle case where there is zero or one column suggestions to hint,
3618  * including exact matches referenced but not visible.
3619  */
3620  ereport(ERROR,
3621  (errcode(ERRCODE_UNDEFINED_COLUMN),
3622  relname ?
3623  errmsg("column %s.%s does not exist", relname, colname) :
3624  errmsg("column \"%s\" does not exist", colname),
3625  state->rfirst ? closestfirst ?
3626  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3627  state->rfirst->eref->aliasname, closestfirst) :
3628  errhint("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3629  colname, state->rfirst->eref->aliasname) : 0,
3630  parser_errposition(pstate, location)));
3631  }
3632  else
3633  {
3634  /* Handle case where there are two equally useful column hints */
3635  char *closestsecond;
3636 
3637  closestsecond = strVal(list_nth(state->rsecond->eref->colnames,
3638  state->second - 1));
3639 
3640  ereport(ERROR,
3641  (errcode(ERRCODE_UNDEFINED_COLUMN),
3642  relname ?
3643  errmsg("column %s.%s does not exist", relname, colname) :
3644  errmsg("column \"%s\" does not exist", colname),
3645  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3646  state->rfirst->eref->aliasname, closestfirst,
3647  state->rsecond->eref->aliasname, closestsecond),
3648  parser_errposition(pstate, location)));
3649  }
3650 }
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
int errhint(const char *fmt,...)
Definition: elog.c:1151
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colname, int location)
NameData relname
Definition: pg_class.h:38
static void * list_nth(const List *list, int n)
Definition: pg_list.h:297
Definition: regguts.h:318
#define strVal(v)
Definition: value.h:72

References AttributeNumberIsValid, ereport, errcode(), errhint(), errmsg(), ERROR, list_nth(), parser_errposition(), relname, searchRangeTableForCol(), and strVal.

Referenced by transformColumnRef().

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3525 of file parse_relation.c.

3526 {
3527  RangeTblEntry *rte;
3528  const char *badAlias = NULL;
3529 
3530  /*
3531  * Check to see if there are any potential matches in the query's
3532  * rangetable. (Note: cases involving a bad schema name in the RangeVar
3533  * will throw error immediately here. That seems OK.)
3534  */
3535  rte = searchRangeTableForRel(pstate, relation);
3536 
3537  /*
3538  * If we found a match that has an alias and the alias is visible in the
3539  * namespace, then the problem is probably use of the relation's real name
3540  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3541  * common enough to justify a specific hint.
3542  *
3543  * If we found a match that doesn't meet those criteria, assume the
3544  * problem is illegal use of a relation outside its scope, as in the
3545  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3546  */
3547  if (rte && rte->alias &&
3548  strcmp(rte->eref->aliasname, relation->relname) != 0)
3549  {
3550  ParseNamespaceItem *nsitem;
3551  int sublevels_up;
3552 
3553  nsitem = refnameNamespaceItem(pstate, NULL, rte->eref->aliasname,
3554  relation->location,
3555  &sublevels_up);
3556  if (nsitem && nsitem->p_rte == rte)
3557  badAlias = rte->eref->aliasname;
3558  }
3559 
3560  if (rte)
3561  ereport(ERROR,
3563  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3564  relation->relname),
3565  (badAlias ?
3566  errhint("Perhaps you meant to reference the table alias \"%s\".",
3567  badAlias) :
3568  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3569  rte->eref->aliasname)),
3570  parser_errposition(pstate, relation->location)));
3571  else
3572  ereport(ERROR,
3574  errmsg("missing FROM-clause entry for table \"%s\"",
3575  relation->relname),
3576  parser_errposition(pstate, relation->location)));
3577 }
static RangeTblEntry * searchRangeTableForRel(ParseState *pstate, RangeVar *relation)
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:81

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

◆ expandNSItemAttrs()

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

Definition at line 3129 of file parse_relation.c.

3131 {
3132  RangeTblEntry *rte = nsitem->p_rte;
3133  List *names,
3134  *vars;
3135  ListCell *name,
3136  *var;
3137  List *te_list = NIL;
3138 
3139  vars = expandNSItemVars(nsitem, sublevels_up, location, &names);
3140 
3141  /*
3142  * Require read access to the table. This is normally redundant with the
3143  * markVarForSelectPriv calls below, but not if the table has zero
3144  * columns. We need not do anything if the nsitem is for a join: its
3145  * component tables will have been marked ACL_SELECT when they were added
3146  * to the rangetable. (This step changes things only for the target
3147  * relation of UPDATE/DELETE, which cannot be under a join.)
3148  */
3149  if (rte->rtekind == RTE_RELATION)
3150  rte->requiredPerms |= ACL_SELECT;
3151 
3152  forboth(name, names, var, vars)
3153  {
3154  char *label = strVal(lfirst(name));
3155  Var *varnode = (Var *) lfirst(var);
3156  TargetEntry *te;
3157 
3158  te = makeTargetEntry((Expr *) varnode,
3159  (AttrNumber) pstate->p_next_resno++,
3160  label,
3161  false);
3162  te_list = lappend(te_list, te);
3163 
3164  if (require_col_privs)
3165  {
3166  /* Require read access to each column */
3167  markVarForSelectPriv(pstate, varnode);
3168  }
3169  }
3170 
3171  Assert(name == NULL && var == NULL); /* lists not the same length? */
3172 
3173  return te_list;
3174 }
const char * name
Definition: encode.c:561
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:239
void markVarForSelectPriv(ParseState *pstate, Var *var)
List * expandNSItemVars(ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
static char * label
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:465
int p_next_resno
Definition: parse_node.h:198
Definition: primnodes.h:209
Definition: regcomp.c:238

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

◆ expandNSItemVars()

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

Definition at line 3069 of file parse_relation.c.

3072 {
3073  List *result = NIL;
3074  int colindex;
3075  ListCell *lc;
3076 
3077  if (colnames)
3078  *colnames = NIL;
3079  colindex = 0;
3080  foreach(lc, nsitem->p_names->colnames)
3081  {
3082  String *colnameval = lfirst(lc);
3083  const char *colname = strVal(colnameval);
3084  ParseNamespaceColumn *nscol = nsitem->p_nscolumns + colindex;
3085 
3086  if (nscol->p_dontexpand)
3087  {
3088  /* skip */
3089  }
3090  else if (colname[0])
3091  {
3092  Var *var;
3093 
3094  Assert(nscol->p_varno > 0);
3095  var = makeVar(nscol->p_varno,
3096  nscol->p_varattno,
3097  nscol->p_vartype,
3098  nscol->p_vartypmod,
3099  nscol->p_varcollid,
3100  sublevels_up);
3101  /* makeVar doesn't offer parameters for these, so set by hand: */
3102  var->varnosyn = nscol->p_varnosyn;
3103  var->varattnosyn = nscol->p_varattnosyn;
3104  var->location = location;
3105  result = lappend(result, var);
3106  if (colnames)
3107  *colnames = lappend(*colnames, colnameval);
3108  }
3109  else
3110  {
3111  /* dropped column, ignore */
3112  Assert(nscol->p_varno == 0);
3113  }
3114  colindex++;
3115  }
3116  return result;
3117 }
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
AttrNumber p_varattno
Definition: parse_node.h:303
AttrNumber p_varattnosyn
Definition: parse_node.h:308
Definition: value.h:58
AttrNumber varattnosyn
Definition: primnodes.h:239
int location
Definition: primnodes.h:242
Index varnosyn
Definition: primnodes.h:237

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

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

◆ expandRTE()

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

Definition at line 2611 of file parse_relation.c.

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

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

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

◆ GetCTEForRTE()

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

Definition at line 537 of file parse_relation.c.

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

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

◆ GetNSItemByRangeTablePosn()

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

Definition at line 490 of file parse_relation.c.

493 {
494  ListCell *lc;
495 
496  while (sublevels_up-- > 0)
497  {
498  pstate = pstate->parentParseState;
499  Assert(pstate != NULL);
500  }
501  foreach(lc, pstate->p_namespace)
502  {
503  ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
504 
505  if (nsitem->p_rtindex == varno)
506  return nsitem;
507  }
508  elog(ERROR, "nsitem not found (internal error)");
509  return NULL; /* keep compiler quiet */
510 }

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

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

◆ GetRTEByRangeTablePosn()

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

Definition at line 517 of file parse_relation.c.

520 {
521  while (sublevels_up-- > 0)
522  {
523  pstate = pstate->parentParseState;
524  Assert(pstate != NULL);
525  }
526  Assert(varno > 0 && varno <= list_length(pstate->p_rtable));
527  return rt_fetch(varno, pstate->p_rtable);
528 }
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31

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

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

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

Definition at line 2527 of file parse_relation.c.

2528 {
2529  ListCell *l;
2530 
2531  /*
2532  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2533  * parent level, then act as though there's a generic FOR UPDATE here.
2534  */
2535  if (pstate->p_locked_from_parent)
2536  return true;
2537 
2538  foreach(l, pstate->p_locking_clause)
2539  {
2540  LockingClause *lc = (LockingClause *) lfirst(l);
2541 
2542  if (lc->lockedRels == NIL)
2543  {
2544  /* all tables used in query */
2545  return true;
2546  }
2547  else
2548  {
2549  /* just the named tables */
2550  ListCell *l2;
2551 
2552  foreach(l2, lc->lockedRels)
2553  {
2554  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2555 
2556  if (strcmp(refname, thisrel->relname) == 0)
2557  return true;
2558  }
2559  }
2560  }
2561  return false;
2562 }
List * lockedRels
Definition: parsenodes.h:785
bool p_locked_from_parent
Definition: parse_node.h:201
List * p_locking_clause
Definition: parse_node.h:200

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

Referenced by addRangeTableEntry(), and transformRangeSubselect().

◆ isQueryUsingTempRelation()

bool isQueryUsingTempRelation ( Query query)

Definition at line 3658 of file parse_relation.c.

3659 {
3660  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3661 }
static bool isQueryUsingTempRelation_walker(Node *node, void *context)

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

◆ markVarForSelectPriv()

void markVarForSelectPriv ( ParseState pstate,
Var var 
)

Definition at line 1098 of file parse_relation.c.

1099 {
1100  Index lv;
1101 
1102  Assert(IsA(var, Var));
1103  /* Find the appropriate pstate if it's an uplevel Var */
1104  for (lv = 0; lv < var->varlevelsup; lv++)
1105  pstate = pstate->parentParseState;
1106  markRTEForSelectPriv(pstate, var->varno, var->varattno);
1107 }
static void markRTEForSelectPriv(ParseState *pstate, int rtindex, AttrNumber col)
AttrNumber varattno
Definition: primnodes.h:221
int varno
Definition: primnodes.h:216

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

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

◆ parserOpenTable()

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

Definition at line 1361 of file parse_relation.c.

1362 {
1363  Relation rel;
1364  ParseCallbackState pcbstate;
1365 
1366  setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1367  rel = table_openrv_extended(relation, lockmode, true);
1368  if (rel == NULL)
1369  {
1370  if (relation->schemaname)
1371  ereport(ERROR,
1373  errmsg("relation \"%s.%s\" does not exist",
1374  relation->schemaname, relation->relname)));
1375  else
1376  {
1377  /*
1378  * An unqualified name might have been meant as a reference to
1379  * some not-yet-in-scope CTE. The bare "does not exist" message
1380  * has proven remarkably unhelpful for figuring out such problems,
1381  * so we take pains to offer a specific hint.
1382  */
1383  if (isFutureCTE(pstate, relation->relname))
1384  ereport(ERROR,
1386  errmsg("relation \"%s\" does not exist",
1387  relation->relname),
1388  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1389  relation->relname),
1390  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1391  else
1392  ereport(ERROR,
1394  errmsg("relation \"%s\" does not exist",
1395  relation->relname)));
1396  }
1397  }
1399  return rel;
1400 }
int errdetail(const char *fmt,...)
Definition: elog.c:1037
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:160
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:144
static bool isFutureCTE(ParseState *pstate, const char *refname)
char * schemaname
Definition: primnodes.h:71
Relation table_openrv_extended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
Definition: table.c:132

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

◆ refnameNamespaceItem()

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

Definition at line 109 of file parse_relation.c.

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

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

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

◆ scanNameSpaceForCTE()

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

Definition at line 262 of file parse_relation.c.

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 }

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

Referenced by getNSItemForSpecialRelationTypes(), and searchRangeTableForRel().

◆ scanNameSpaceForENR()

bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 315 of file parse_relation.c.

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

References name_matches_visible_ENR().

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

◆ scanNSItemForColumn()

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

Definition at line 668 of file parse_relation.c.

670 {
671  RangeTblEntry *rte = nsitem->p_rte;
672  int attnum;
673  Var *var;
674 
675  /*
676  * Scan the nsitem's column names (or aliases) for a match. Complain if
677  * multiple matches.
678  */
679  attnum = scanRTEForColumn(pstate, rte, nsitem->p_names,
680  colname, location,
681  0, NULL);
682 
683  if (attnum == InvalidAttrNumber)
684  return NULL; /* Return NULL if no match */
685 
686  /* In constraint check, no system column is allowed except tableOid */
687  if (pstate->p_expr_kind == EXPR_KIND_CHECK_CONSTRAINT &&
689  ereport(ERROR,
690  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
691  errmsg("system column \"%s\" reference in check constraint is invalid",
692  colname),
693  parser_errposition(pstate, location)));
694 
695  /* In generated column, no system column is allowed except tableOid */
696  if (pstate->p_expr_kind == EXPR_KIND_GENERATED_COLUMN &&
698  ereport(ERROR,
699  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
700  errmsg("cannot use system column \"%s\" in column generation expression",
701  colname),
702  parser_errposition(pstate, location)));
703 
704  /*
705  * In a MERGE WHEN condition, no system column is allowed except tableOid
706  */
707  if (pstate->p_expr_kind == EXPR_KIND_MERGE_WHEN &&
709  ereport(ERROR,
710  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
711  errmsg("cannot use system column \"%s\" in MERGE WHEN condition",
712  colname),
713  parser_errposition(pstate, location)));
714 
715  /* Found a valid match, so build a Var */
717  {
718  /* Get attribute data from the ParseNamespaceColumn array */
719  ParseNamespaceColumn *nscol = &nsitem->p_nscolumns[attnum - 1];
720 
721  /* Complain if dropped column. See notes in scanRTEForColumn. */
722  if (nscol->p_varno == 0)
723  ereport(ERROR,
724  (errcode(ERRCODE_UNDEFINED_COLUMN),
725  errmsg("column \"%s\" of relation \"%s\" does not exist",
726  colname,
727  nsitem->p_names->aliasname)));
728 
729  var = makeVar(nscol->p_varno,
730  nscol->p_varattno,
731  nscol->p_vartype,
732  nscol->p_vartypmod,
733  nscol->p_varcollid,
734  sublevels_up);
735  /* makeVar doesn't offer parameters for these, so set them by hand: */
736  var->varnosyn = nscol->p_varnosyn;
737  var->varattnosyn = nscol->p_varattnosyn;
738  }
739  else
740  {
741  /* System column, so use predetermined type data */
742  const FormData_pg_attribute *sysatt;
743 
745  var = makeVar(nsitem->p_rtindex,
746  attnum,
747  sysatt->atttypid,
748  sysatt->atttypmod,
749  sysatt->attcollation,
750  sublevels_up);
751  }
752  var->location = location;
753 
754  /* Require read access to the column */
755  markVarForSelectPriv(pstate, var);
756 
757  return (Node *) var;
758 }
@ EXPR_KIND_MERGE_WHEN
Definition: parse_node.h:58
@ EXPR_KIND_GENERATED_COLUMN
Definition: parse_node.h:82
@ EXPR_KIND_CHECK_CONSTRAINT
Definition: parse_node.h:67
static int scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, Alias *eref, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
ParseExprKind p_expr_kind
Definition: parse_node.h:197
#define TableOidAttributeNumber
Definition: sysattr.h:26

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

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