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 markNullableIfNeeded (ParseState *pstate, Var *var)
 
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 *join_using_alias, Alias *alias, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForCTE (ParseState *pstate, CommonTableExpr *cte, Index levelsup, RangeVar *rv, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForENR (ParseState *pstate, RangeVar *rv, bool inFromCl)
 
RTEPermissionInfoaddRTEPermissionInfo (List **rteperminfos, RangeTblEntry *rte)
 
RTEPermissionInfogetRTEPermissionInfo (List *rteperminfos, RangeTblEntry *rte)
 
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 (ParseState *pstate, 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 2624 of file parse_relation.c.

2627 {
2628  if (addToJoinList)
2629  {
2631 
2632  rtr->rtindex = nsitem->p_rtindex;
2633  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2634  }
2635  if (addToRelNameSpace || addToVarNameSpace)
2636  {
2637  /* Set the new nsitem's visibility flags correctly */
2638  nsitem->p_rel_visible = addToRelNameSpace;
2639  nsitem->p_cols_visible = addToVarNameSpace;
2640  nsitem->p_lateral_only = false;
2641  nsitem->p_lateral_ok = true;
2642  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2643  }
2644 }
List * lappend(List *list, void *datum)
Definition: list.c:338
#define makeNode(_type_)
Definition: nodes.h:176
List * p_namespace
Definition: parse_node.h:200
List * p_joinlist
Definition: parse_node.h:198

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

1474 {
1476  RTEPermissionInfo *perminfo;
1477  char *refname = alias ? alias->aliasname : relation->relname;
1478  LOCKMODE lockmode;
1479  Relation rel;
1480  ParseNamespaceItem *nsitem;
1481 
1482  Assert(pstate != NULL);
1483 
1484  rte->rtekind = RTE_RELATION;
1485  rte->alias = alias;
1486 
1487  /*
1488  * Identify the type of lock we'll need on this relation. It's not the
1489  * query's target table (that case is handled elsewhere), so we need
1490  * either RowShareLock if it's locked by FOR UPDATE/SHARE, or plain
1491  * AccessShareLock otherwise.
1492  */
1493  lockmode = isLockedRefname(pstate, refname) ? RowShareLock : AccessShareLock;
1494 
1495  /*
1496  * Get the rel's OID. This access also ensures that we have an up-to-date
1497  * relcache entry for the rel. Since this is typically the first access
1498  * to a rel in a statement, we must open the rel with the proper lockmode.
1499  */
1500  rel = parserOpenTable(pstate, relation, lockmode);
1501  rte->relid = RelationGetRelid(rel);
1502  rte->relkind = rel->rd_rel->relkind;
1503  rte->rellockmode = lockmode;
1504 
1505  /*
1506  * Build the list of effective column names using user-supplied aliases
1507  * and/or actual column names.
1508  */
1509  rte->eref = makeAlias(refname, NIL);
1510  buildRelationAliases(rel->rd_att, alias, rte->eref);
1511 
1512  /*
1513  * Set flags and initialize access permissions.
1514  *
1515  * The initial default on access checks is always check-for-READ-access,
1516  * which is the right thing for all except target tables.
1517  */
1518  rte->lateral = false;
1519  rte->inh = inh;
1520  rte->inFromCl = inFromCl;
1521 
1522  perminfo = addRTEPermissionInfo(&pstate->p_rteperminfos, rte);
1523  perminfo->requiredPerms = ACL_SELECT;
1524 
1525  /*
1526  * Add completed RTE to pstate's range table list, so that we know its
1527  * index. But we don't add it to the join list --- caller must do that if
1528  * appropriate.
1529  */
1530  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1531 
1532  /*
1533  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1534  * list --- caller must do that if appropriate.
1535  */
1536  nsitem = buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1537  perminfo, rel->rd_att);
1538 
1539  /*
1540  * Drop the rel refcount, but keep the access lock till end of transaction
1541  * so that the table can't be deleted or have its schema modified
1542  * underneath us.
1543  */
1544  table_close(rel, NoLock);
1545 
1546  return nsitem;
1547 }
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:389
RTEPermissionInfo * addRTEPermissionInfo(List **rteperminfos, RangeTblEntry *rte)
Relation parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, RTEPermissionInfo *perminfo, TupleDesc tupdesc)
bool isLockedRefname(ParseState *pstate, const char *refname)
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
@ RTE_RELATION
Definition: parsenodes.h:1013
#define ACL_SELECT
Definition: parsenodes.h:84
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define RelationGetRelid(relation)
Definition: rel.h:501
char * aliasname
Definition: primnodes.h:42
List * p_rteperminfos
Definition: parse_node.h:194
List * p_rtable
Definition: parse_node.h:193
AclMode requiredPerms
Definition: parsenodes.h:1245
Alias * eref
Definition: parsenodes.h:1199
Alias * alias
Definition: parsenodes.h:1198
RTEKind rtekind
Definition: parsenodes.h:1032
char * relname
Definition: primnodes.h:74
TupleDesc rd_att
Definition: rel.h:111
Form_pg_class rd_rel
Definition: rel.h:110
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126

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

Referenced by transformTableEntry().

◆ addRangeTableEntryForCTE()

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

Definition at line 2318 of file parse_relation.c.

2323 {
2325  Alias *alias = rv->alias;
2326  char *refname = alias ? alias->aliasname : cte->ctename;
2327  Alias *eref;
2328  int numaliases;
2329  int varattno;
2330  ListCell *lc;
2331  int n_dontexpand_columns = 0;
2332  ParseNamespaceItem *psi;
2333 
2334  Assert(pstate != NULL);
2335 
2336  rte->rtekind = RTE_CTE;
2337  rte->ctename = cte->ctename;
2338  rte->ctelevelsup = levelsup;
2339 
2340  /* Self-reference if and only if CTE's parse analysis isn't completed */
2341  rte->self_reference = !IsA(cte->ctequery, Query);
2342  Assert(cte->cterecursive || !rte->self_reference);
2343  /* Bump the CTE's refcount if this isn't a self-reference */
2344  if (!rte->self_reference)
2345  cte->cterefcount++;
2346 
2347  /*
2348  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
2349  * This won't get checked in case of a self-reference, but that's OK
2350  * because data-modifying CTEs aren't allowed to be recursive anyhow.
2351  */
2352  if (IsA(cte->ctequery, Query))
2353  {
2354  Query *ctequery = (Query *) cte->ctequery;
2355 
2356  if (ctequery->commandType != CMD_SELECT &&
2357  ctequery->returningList == NIL)
2358  ereport(ERROR,
2359  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2360  errmsg("WITH query \"%s\" does not have a RETURNING clause",
2361  cte->ctename),
2362  parser_errposition(pstate, rv->location)));
2363  }
2364 
2365  rte->coltypes = list_copy(cte->ctecoltypes);
2366  rte->coltypmods = list_copy(cte->ctecoltypmods);
2367  rte->colcollations = list_copy(cte->ctecolcollations);
2368 
2369  rte->alias = alias;
2370  if (alias)
2371  eref = copyObject(alias);
2372  else
2373  eref = makeAlias(refname, NIL);
2374  numaliases = list_length(eref->colnames);
2375 
2376  /* fill in any unspecified alias columns */
2377  varattno = 0;
2378  foreach(lc, cte->ctecolnames)
2379  {
2380  varattno++;
2381  if (varattno > numaliases)
2382  eref->colnames = lappend(eref->colnames, lfirst(lc));
2383  }
2384  if (varattno < numaliases)
2385  ereport(ERROR,
2386  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2387  errmsg("table \"%s\" has %d columns available but %d columns specified",
2388  refname, varattno, numaliases)));
2389 
2390  rte->eref = eref;
2391 
2392  if (cte->search_clause)
2393  {
2394  rte->eref->colnames = lappend(rte->eref->colnames, makeString(cte->search_clause->search_seq_column));
2395  if (cte->search_clause->search_breadth_first)
2396  rte->coltypes = lappend_oid(rte->coltypes, RECORDOID);
2397  else
2398  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2399  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2401 
2402  n_dontexpand_columns += 1;
2403  }
2404 
2405  if (cte->cycle_clause)
2406  {
2407  rte->eref->colnames = lappend(rte->eref->colnames, makeString(cte->cycle_clause->cycle_mark_column));
2408  rte->coltypes = lappend_oid(rte->coltypes, cte->cycle_clause->cycle_mark_type);
2409  rte->coltypmods = lappend_int(rte->coltypmods, cte->cycle_clause->cycle_mark_typmod);
2410  rte->colcollations = lappend_oid(rte->colcollations, cte->cycle_clause->cycle_mark_collation);
2411 
2412  rte->eref->colnames = lappend(rte->eref->colnames, makeString(cte->cycle_clause->cycle_path_column));
2413  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2414  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2416 
2417  n_dontexpand_columns += 2;
2418  }
2419 
2420  /*
2421  * Set flags and access permissions.
2422  *
2423  * Subqueries are never checked for access rights, so no need to perform
2424  * addRTEPermissionInfo().
2425  */
2426  rte->lateral = false;
2427  rte->inh = false; /* never true for subqueries */
2428  rte->inFromCl = inFromCl;
2429 
2430  /*
2431  * Add completed RTE to pstate's range table list, so that we know its
2432  * index. But we don't add it to the join list --- caller must do that if
2433  * appropriate.
2434  */
2435  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2436 
2437  /*
2438  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2439  * list --- caller must do that if appropriate.
2440  */
2441  psi = buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2442  rte->coltypes, rte->coltypmods,
2443  rte->colcollations);
2444 
2445  /*
2446  * The columns added by search and cycle clauses are not included in star
2447  * expansion in queries contained in the CTE.
2448  */
2449  if (rte->ctelevelsup > 0)
2450  for (int i = 0; i < n_dontexpand_columns; i++)
2451  psi->p_nscolumns[list_length(psi->p_names->colnames) - 1 - i].p_dontexpand = true;
2452 
2453  return psi;
2454 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
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:179
#define copyObject(obj)
Definition: nodes.h:244
@ CMD_SELECT
Definition: nodes.h:276
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
@ RTE_CTE
Definition: parsenodes.h:1019
#define lfirst(lc)
Definition: pg_list.h:172
#define InvalidOid
Definition: postgres_ext.h:36
List * colnames
Definition: primnodes.h:43
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:290
List * returningList
Definition: parsenodes.h:196
CmdType commandType
Definition: parsenodes.h:128
List * colcollations
Definition: parsenodes.h:1187
char * ctename
Definition: parsenodes.h:1163
bool self_reference
Definition: parsenodes.h:1165
Index ctelevelsup
Definition: parsenodes.h:1164
List * coltypes
Definition: parsenodes.h:1185
List * coltypmods
Definition: parsenodes.h:1186
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(), CMD_SELECT, RangeTblEntry::colcollations, Alias::colnames, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, Query::commandType, copyObject, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, CommonTableExpr::ctename, CommonTableExpr::ctequery, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, i, if(), RangeTblEntry::inFromCl, RangeTblEntry::inh, 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(), Query::returningList, RTE_CTE, RangeTblEntry::rtekind, and RangeTblEntry::self_reference.

Referenced by getNSItemForSpecialRelationTypes().

◆ addRangeTableEntryForENR()

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

Definition at line 2470 of file parse_relation.c.

2473 {
2475  Alias *alias = rv->alias;
2476  char *refname = alias ? alias->aliasname : rv->relname;
2478  TupleDesc tupdesc;
2479  int attno;
2480 
2481  Assert(pstate != NULL);
2482  enrmd = get_visible_ENR(pstate, rv->relname);
2483  Assert(enrmd != NULL);
2484 
2485  switch (enrmd->enrtype)
2486  {
2487  case ENR_NAMED_TUPLESTORE:
2489  break;
2490 
2491  default:
2492  elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2493  return NULL; /* for fussy compilers */
2494  }
2495 
2496  /*
2497  * Record dependency on a relation. This allows plans to be invalidated
2498  * if they access transition tables linked to a table that is altered.
2499  */
2500  rte->relid = enrmd->reliddesc;
2501 
2502  /*
2503  * Build the list of effective column names using user-supplied aliases
2504  * and/or actual column names.
2505  */
2506  tupdesc = ENRMetadataGetTupDesc(enrmd);
2507  rte->eref = makeAlias(refname, NIL);
2508  buildRelationAliases(tupdesc, alias, rte->eref);
2509 
2510  /* Record additional data for ENR, including column type info */
2511  rte->enrname = enrmd->name;
2512  rte->enrtuples = enrmd->enrtuples;
2513  rte->coltypes = NIL;
2514  rte->coltypmods = NIL;
2515  rte->colcollations = NIL;
2516  for (attno = 1; attno <= tupdesc->natts; ++attno)
2517  {
2518  Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2519 
2520  if (att->attisdropped)
2521  {
2522  /* Record zeroes for a dropped column */
2523  rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2524  rte->coltypmods = lappend_int(rte->coltypmods, 0);
2526  }
2527  else
2528  {
2529  /* Let's just make sure we can tell this isn't dropped */
2530  if (att->atttypid == InvalidOid)
2531  elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2532  rv->relname);
2533  rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2534  rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2536  att->attcollation);
2537  }
2538  }
2539 
2540  /*
2541  * Set flags and access permissions.
2542  *
2543  * ENRs are never checked for access rights, so no need to perform
2544  * addRTEPermissionInfo().
2545  */
2546  rte->lateral = false;
2547  rte->inh = false; /* never true for ENRs */
2548  rte->inFromCl = inFromCl;
2549 
2550  /*
2551  * Add completed RTE to pstate's range table list, so that we know its
2552  * index. But we don't add it to the join list --- caller must do that if
2553  * appropriate.
2554  */
2555  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2556 
2557  /*
2558  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2559  * list --- caller must do that if appropriate.
2560  */
2561  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable), NULL,
2562  tupdesc);
2563 }
EphemeralNamedRelationMetadata get_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:26
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1020
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:1192
Cardinality enrtuples
Definition: parsenodes.h:1193
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References RangeVar::alias, Alias::aliasname, Assert(), buildNSItemFromTupleDesc(), buildRelationAliases(), 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, RTE_NAMEDTUPLESTORE, RangeTblEntry::rtekind, 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 1734 of file parse_relation.c.

1741 {
1743  Alias *alias = rangefunc->alias;
1744  Alias *eref;
1745  char *aliasname;
1746  int nfuncs = list_length(funcexprs);
1747  TupleDesc *functupdescs;
1748  TupleDesc tupdesc;
1749  ListCell *lc1,
1750  *lc2,
1751  *lc3;
1752  int i;
1753  int j;
1754  int funcno;
1755  int natts,
1756  totalatts;
1757 
1758  Assert(pstate != NULL);
1759 
1760  rte->rtekind = RTE_FUNCTION;
1761  rte->relid = InvalidOid;
1762  rte->subquery = NULL;
1763  rte->functions = NIL; /* we'll fill this list below */
1764  rte->funcordinality = rangefunc->ordinality;
1765  rte->alias = alias;
1766 
1767  /*
1768  * Choose the RTE alias name. We default to using the first function's
1769  * name even when there's more than one; which is maybe arguable but beats
1770  * using something constant like "table".
1771  */
1772  if (alias)
1773  aliasname = alias->aliasname;
1774  else
1775  aliasname = linitial(funcnames);
1776 
1777  eref = makeAlias(aliasname, NIL);
1778  rte->eref = eref;
1779 
1780  /* Process each function ... */
1781  functupdescs = (TupleDesc *) palloc(nfuncs * sizeof(TupleDesc));
1782 
1783  totalatts = 0;
1784  funcno = 0;
1785  forthree(lc1, funcexprs, lc2, funcnames, lc3, coldeflists)
1786  {
1787  Node *funcexpr = (Node *) lfirst(lc1);
1788  char *funcname = (char *) lfirst(lc2);
1789  List *coldeflist = (List *) lfirst(lc3);
1791  TypeFuncClass functypclass;
1792  Oid funcrettype;
1793 
1794  /* Initialize RangeTblFunction node */
1795  rtfunc->funcexpr = funcexpr;
1796  rtfunc->funccolnames = NIL;
1797  rtfunc->funccoltypes = NIL;
1798  rtfunc->funccoltypmods = NIL;
1799  rtfunc->funccolcollations = NIL;
1800  rtfunc->funcparams = NULL; /* not set until planning */
1801 
1802  /*
1803  * Now determine if the function returns a simple or composite type.
1804  */
1805  functypclass = get_expr_result_type(funcexpr,
1806  &funcrettype,
1807  &tupdesc);
1808 
1809  /*
1810  * A coldeflist is required if the function returns RECORD and hasn't
1811  * got a predetermined record type, and is prohibited otherwise. This
1812  * can be a bit confusing, so we expend some effort on delivering a
1813  * relevant error message.
1814  */
1815  if (coldeflist != NIL)
1816  {
1817  switch (functypclass)
1818  {
1819  case TYPEFUNC_RECORD:
1820  /* ok */
1821  break;
1822  case TYPEFUNC_COMPOSITE:
1824 
1825  /*
1826  * If the function's raw result type is RECORD, we must
1827  * have resolved it using its OUT parameters. Otherwise,
1828  * it must have a named composite type.
1829  */
1830  if (exprType(funcexpr) == RECORDOID)
1831  ereport(ERROR,
1832  (errcode(ERRCODE_SYNTAX_ERROR),
1833  errmsg("a column definition list is redundant for a function with OUT parameters"),
1834  parser_errposition(pstate,
1835  exprLocation((Node *) coldeflist))));
1836  else
1837  ereport(ERROR,
1838  (errcode(ERRCODE_SYNTAX_ERROR),
1839  errmsg("a column definition list is redundant for a function returning a named composite type"),
1840  parser_errposition(pstate,
1841  exprLocation((Node *) coldeflist))));
1842  break;
1843  default:
1844  ereport(ERROR,
1845  (errcode(ERRCODE_SYNTAX_ERROR),
1846  errmsg("a column definition list is only allowed for functions returning \"record\""),
1847  parser_errposition(pstate,
1848  exprLocation((Node *) coldeflist))));
1849  break;
1850  }
1851  }
1852  else
1853  {
1854  if (functypclass == TYPEFUNC_RECORD)
1855  ereport(ERROR,
1856  (errcode(ERRCODE_SYNTAX_ERROR),
1857  errmsg("a column definition list is required for functions returning \"record\""),
1858  parser_errposition(pstate, exprLocation(funcexpr))));
1859  }
1860 
1861  if (functypclass == TYPEFUNC_COMPOSITE ||
1862  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
1863  {
1864  /* Composite data type, e.g. a table's row type */
1865  Assert(tupdesc);
1866  }
1867  else if (functypclass == TYPEFUNC_SCALAR)
1868  {
1869  /* Base data type, i.e. scalar */
1870  tupdesc = CreateTemplateTupleDesc(1);
1871  TupleDescInitEntry(tupdesc,
1872  (AttrNumber) 1,
1873  chooseScalarFunctionAlias(funcexpr, funcname,
1874  alias, nfuncs),
1875  funcrettype,
1876  exprTypmod(funcexpr),
1877  0);
1879  (AttrNumber) 1,
1880  exprCollation(funcexpr));
1881  }
1882  else if (functypclass == TYPEFUNC_RECORD)
1883  {
1884  ListCell *col;
1885 
1886  /*
1887  * Use the column definition list to construct a tupdesc and fill
1888  * in the RangeTblFunction's lists. Limit number of columns to
1889  * MaxHeapAttributeNumber, because CheckAttributeNamesTypes will.
1890  */
1891  if (list_length(coldeflist) > MaxHeapAttributeNumber)
1892  ereport(ERROR,
1893  (errcode(ERRCODE_TOO_MANY_COLUMNS),
1894  errmsg("column definition lists can have at most %d entries",
1896  parser_errposition(pstate,
1897  exprLocation((Node *) coldeflist))));
1898  tupdesc = CreateTemplateTupleDesc(list_length(coldeflist));
1899  i = 1;
1900  foreach(col, coldeflist)
1901  {
1902  ColumnDef *n = (ColumnDef *) lfirst(col);
1903  char *attrname;
1904  Oid attrtype;
1905  int32 attrtypmod;
1906  Oid attrcollation;
1907 
1908  attrname = n->colname;
1909  if (n->typeName->setof)
1910  ereport(ERROR,
1911  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
1912  errmsg("column \"%s\" cannot be declared SETOF",
1913  attrname),
1914  parser_errposition(pstate, n->location)));
1915  typenameTypeIdAndMod(pstate, n->typeName,
1916  &attrtype, &attrtypmod);
1917  attrcollation = GetColumnDefCollation(pstate, n, attrtype);
1918  TupleDescInitEntry(tupdesc,
1919  (AttrNumber) i,
1920  attrname,
1921  attrtype,
1922  attrtypmod,
1923  0);
1925  (AttrNumber) i,
1926  attrcollation);
1927  rtfunc->funccolnames = lappend(rtfunc->funccolnames,
1928  makeString(pstrdup(attrname)));
1929  rtfunc->funccoltypes = lappend_oid(rtfunc->funccoltypes,
1930  attrtype);
1931  rtfunc->funccoltypmods = lappend_int(rtfunc->funccoltypmods,
1932  attrtypmod);
1933  rtfunc->funccolcollations = lappend_oid(rtfunc->funccolcollations,
1934  attrcollation);
1935 
1936  i++;
1937  }
1938 
1939  /*
1940  * Ensure that the coldeflist defines a legal set of names (no
1941  * duplicates, but we needn't worry about system column names) and
1942  * datatypes. Although we mostly can't allow pseudo-types, it
1943  * seems safe to allow RECORD and RECORD[], since values within
1944  * those type classes are self-identifying at runtime, and the
1945  * coldeflist doesn't represent anything that will be visible to
1946  * other sessions.
1947  */
1948  CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE,
1950  }
1951  else
1952  ereport(ERROR,
1953  (errcode(ERRCODE_DATATYPE_MISMATCH),
1954  errmsg("function \"%s\" in FROM has unsupported return type %s",
1955  funcname, format_type_be(funcrettype)),
1956  parser_errposition(pstate, exprLocation(funcexpr))));
1957 
1958  /* Finish off the RangeTblFunction and add it to the RTE's list */
1959  rtfunc->funccolcount = tupdesc->natts;
1960  rte->functions = lappend(rte->functions, rtfunc);
1961 
1962  /* Save the tupdesc for use below */
1963  functupdescs[funcno] = tupdesc;
1964  totalatts += tupdesc->natts;
1965  funcno++;
1966  }
1967 
1968  /*
1969  * If there's more than one function, or we want an ordinality column, we
1970  * have to produce a merged tupdesc.
1971  */
1972  if (nfuncs > 1 || rangefunc->ordinality)
1973  {
1974  if (rangefunc->ordinality)
1975  totalatts++;
1976 
1977  /* Disallow more columns than will fit in a tuple */
1978  if (totalatts > MaxTupleAttributeNumber)
1979  ereport(ERROR,
1980  (errcode(ERRCODE_TOO_MANY_COLUMNS),
1981  errmsg("functions in FROM can return at most %d columns",
1983  parser_errposition(pstate,
1984  exprLocation((Node *) funcexprs))));
1985 
1986  /* Merge the tuple descs of each function into a composite one */
1987  tupdesc = CreateTemplateTupleDesc(totalatts);
1988  natts = 0;
1989  for (i = 0; i < nfuncs; i++)
1990  {
1991  for (j = 1; j <= functupdescs[i]->natts; j++)
1992  TupleDescCopyEntry(tupdesc, ++natts, functupdescs[i], j);
1993  }
1994 
1995  /* Add the ordinality column if needed */
1996  if (rangefunc->ordinality)
1997  {
1998  TupleDescInitEntry(tupdesc,
1999  (AttrNumber) ++natts,
2000  "ordinality",
2001  INT8OID,
2002  -1,
2003  0);
2004  /* no need to set collation */
2005  }
2006 
2007  Assert(natts == totalatts);
2008  }
2009  else
2010  {
2011  /* We can just use the single function's tupdesc as-is */
2012  tupdesc = functupdescs[0];
2013  }
2014 
2015  /* Use the tupdesc while assigning column aliases for the RTE */
2016  buildRelationAliases(tupdesc, alias, eref);
2017 
2018  /*
2019  * Set flags and access permissions.
2020  *
2021  * Functions are never checked for access rights (at least, not by
2022  * ExecCheckPermissions()), so no need to perform addRTEPermissionInfo().
2023  */
2024  rte->lateral = lateral;
2025  rte->inh = false; /* never true for functions */
2026  rte->inFromCl = inFromCl;
2027 
2028  /*
2029  * Add completed RTE to pstate's range table list, so that we know its
2030  * index. But we don't add it to the join list --- caller must do that if
2031  * appropriate.
2032  */
2033  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2034 
2035  /*
2036  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2037  * list --- caller must do that if appropriate.
2038  */
2039  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable), NULL,
2040  tupdesc);
2041 }
int16 AttrNumber
Definition: attnum.h:21
signed int int32
Definition: c.h:478
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
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:455
#define CHKATYPE_ANYRECORD
Definition: heap.h:24
#define MaxTupleAttributeNumber
Definition: htup_details.h:34
#define MaxHeapAttributeNumber
Definition: htup_details.h:48
int j
Definition: isn.c:74
char * pstrdup(const char *in)
Definition: mcxt.c:1624
void * palloc(Size size)
Definition: mcxt.c:1210
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:266
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:764
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1243
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:1016
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:512
#define linitial(l)
Definition: pg_list.h:178
unsigned int Oid
Definition: postgres_ext.h:31
int location
Definition: parsenodes.h:740
char * colname
Definition: parsenodes.h:721
TypeName * typeName
Definition: parsenodes.h:722
Definition: pg_list.h:54
Definition: nodes.h:129
Alias * alias
Definition: parsenodes.h:642
bool funcordinality
Definition: parsenodes.h:1148
Query * subquery
Definition: parsenodes.h:1080
List * functions
Definition: parsenodes.h:1147
bool setof
Definition: parsenodes.h:268
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(), CheckAttributeNamesTypes(), CHKATYPE_ANYRECORD, chooseScalarFunctionAlias(), ColumnDef::colname, CreateTemplateTupleDesc(), RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, exprCollation(), exprLocation(), exprType(), exprTypmod(), format_type_be(), forthree, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), GetColumnDefCollation(), i, RangeTblEntry::inFromCl, RangeTblEntry::inh, InvalidOid, j, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, linitial, list_length(), ColumnDef::location, makeAlias(), makeNode, makeString(), MaxHeapAttributeNumber, MaxTupleAttributeNumber, TupleDescData::natts, NIL, RangeFunction::ordinality, ParseState::p_rtable, palloc(), parser_errposition(), pstrdup(), RangeTblEntry::relid, RTE_FUNCTION, RangeTblEntry::rtekind, TypeName::setof, RangeTblEntry::subquery, TupleDescCopyEntry(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, TYPEFUNC_SCALAR, ColumnDef::typeName, and typenameTypeIdAndMod().

Referenced by transformRangeFunction().

◆ addRangeTableEntryForJoin()

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

Definition at line 2219 of file parse_relation.c.

2230 {
2232  Alias *eref;
2233  int numaliases;
2234  ParseNamespaceItem *nsitem;
2235 
2236  Assert(pstate != NULL);
2237 
2238  /*
2239  * Fail if join has too many columns --- we must be able to reference any
2240  * of the columns with an AttrNumber.
2241  */
2242  if (list_length(aliasvars) > MaxAttrNumber)
2243  ereport(ERROR,
2244  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2245  errmsg("joins can have at most %d columns",
2246  MaxAttrNumber)));
2247 
2248  rte->rtekind = RTE_JOIN;
2249  rte->relid = InvalidOid;
2250  rte->subquery = NULL;
2251  rte->jointype = jointype;
2252  rte->joinmergedcols = nummergedcols;
2253  rte->joinaliasvars = aliasvars;
2254  rte->joinleftcols = leftcols;
2255  rte->joinrightcols = rightcols;
2256  rte->join_using_alias = join_using_alias;
2257  rte->alias = alias;
2258 
2259  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
2260  numaliases = list_length(eref->colnames);
2261 
2262  /* fill in any unspecified alias columns */
2263  if (numaliases < list_length(colnames))
2264  eref->colnames = list_concat(eref->colnames,
2265  list_copy_tail(colnames, numaliases));
2266 
2267  if (numaliases > list_length(colnames))
2268  ereport(ERROR,
2269  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2270  errmsg("join expression \"%s\" has %d columns available but %d columns specified",
2271  eref->aliasname, list_length(colnames), numaliases)));
2272 
2273  rte->eref = eref;
2274 
2275  /*
2276  * Set flags and access permissions.
2277  *
2278  * Joins are never checked for access rights, so no need to perform
2279  * addRTEPermissionInfo().
2280  */
2281  rte->lateral = false;
2282  rte->inh = false; /* never true for joins */
2283  rte->inFromCl = inFromCl;
2284 
2285  /*
2286  * Add completed RTE to pstate's range table list, so that we know its
2287  * index. But we don't add it to the join list --- caller must do that if
2288  * appropriate.
2289  */
2290  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2291 
2292  /*
2293  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2294  * list --- caller must do that if appropriate.
2295  */
2296  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2297  nsitem->p_names = rte->eref;
2298  nsitem->p_rte = rte;
2299  nsitem->p_perminfo = NULL;
2300  nsitem->p_rtindex = list_length(pstate->p_rtable);
2301  nsitem->p_nscolumns = nscolumns;
2302  /* set default visibility flags; might get changed later */
2303  nsitem->p_rel_visible = true;
2304  nsitem->p_cols_visible = true;
2305  nsitem->p_lateral_only = false;
2306  nsitem->p_lateral_ok = true;
2307 
2308  return nsitem;
2309 }
#define MaxAttrNumber
Definition: attnum.h:24
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1612
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
@ RTE_JOIN
Definition: parsenodes.h:1015
RangeTblEntry * p_rte
Definition: parse_node.h:286
RTEPermissionInfo * p_perminfo
Definition: parse_node.h:288
Alias * join_using_alias
Definition: parsenodes.h:1137
List * joinrightcols
Definition: parsenodes.h:1130
List * joinaliasvars
Definition: parsenodes.h:1128
JoinType jointype
Definition: parsenodes.h:1126
List * joinleftcols
Definition: parsenodes.h:1129

References RangeTblEntry::alias, Alias::aliasname, Assert(), Alias::colnames, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::inFromCl, RangeTblEntry::inh, 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_perminfo, ParseNamespaceItem::p_rel_visible, ParseState::p_rtable, ParseNamespaceItem::p_rte, ParseNamespaceItem::p_rtindex, palloc(), RangeTblEntry::relid, RTE_JOIN, RangeTblEntry::rtekind, and RangeTblEntry::subquery.

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

◆ addRangeTableEntryForRelation()

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

Definition at line 1566 of file parse_relation.c.

1572 {
1574  RTEPermissionInfo *perminfo;
1575  char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1576 
1577  Assert(pstate != NULL);
1578 
1579  Assert(lockmode == AccessShareLock ||
1580  lockmode == RowShareLock ||
1581  lockmode == RowExclusiveLock);
1582  Assert(CheckRelationLockedByMe(rel, lockmode, true));
1583 
1584  rte->rtekind = RTE_RELATION;
1585  rte->alias = alias;
1586  rte->relid = RelationGetRelid(rel);
1587  rte->relkind = rel->rd_rel->relkind;
1588  rte->rellockmode = lockmode;
1589 
1590  /*
1591  * Build the list of effective column names using user-supplied aliases
1592  * and/or actual column names.
1593  */
1594  rte->eref = makeAlias(refname, NIL);
1595  buildRelationAliases(rel->rd_att, alias, rte->eref);
1596 
1597  /*
1598  * Set flags and initialize access permissions.
1599  *
1600  * The initial default on access checks is always check-for-READ-access,
1601  * which is the right thing for all except target tables.
1602  */
1603  rte->lateral = false;
1604  rte->inh = inh;
1605  rte->inFromCl = inFromCl;
1606 
1607  perminfo = addRTEPermissionInfo(&pstate->p_rteperminfos, rte);
1608  perminfo->requiredPerms = ACL_SELECT;
1609 
1610  /*
1611  * Add completed RTE to pstate's range table list, so that we know its
1612  * index. But we don't add it to the join list --- caller must do that if
1613  * appropriate.
1614  */
1615  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1616 
1617  /*
1618  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1619  * list --- caller must do that if appropriate.
1620  */
1621  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1622  perminfo, rel->rd_att);
1623 }
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:331
#define RowExclusiveLock
Definition: lockdefs.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:535

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

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

◆ addRangeTableEntryForSubquery()

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

Definition at line 1637 of file parse_relation.c.

1642 {
1644  Alias *eref;
1645  int numaliases;
1646  List *coltypes,
1647  *coltypmods,
1648  *colcollations;
1649  int varattno;
1650  ListCell *tlistitem;
1651  ParseNamespaceItem *nsitem;
1652 
1653  Assert(pstate != NULL);
1654 
1655  rte->rtekind = RTE_SUBQUERY;
1656  rte->subquery = subquery;
1657  rte->alias = alias;
1658 
1659  eref = alias ? copyObject(alias) : makeAlias("unnamed_subquery", NIL);
1660  numaliases = list_length(eref->colnames);
1661 
1662  /* fill in any unspecified alias columns, and extract column type info */
1663  coltypes = coltypmods = colcollations = NIL;
1664  varattno = 0;
1665  foreach(tlistitem, subquery->targetList)
1666  {
1667  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1668 
1669  if (te->resjunk)
1670  continue;
1671  varattno++;
1672  Assert(varattno == te->resno);
1673  if (varattno > numaliases)
1674  {
1675  char *attrname;
1676 
1677  attrname = pstrdup(te->resname);
1678  eref->colnames = lappend(eref->colnames, makeString(attrname));
1679  }
1680  coltypes = lappend_oid(coltypes,
1681  exprType((Node *) te->expr));
1682  coltypmods = lappend_int(coltypmods,
1683  exprTypmod((Node *) te->expr));
1684  colcollations = lappend_oid(colcollations,
1685  exprCollation((Node *) te->expr));
1686  }
1687  if (varattno < numaliases)
1688  ereport(ERROR,
1689  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1690  errmsg("table \"%s\" has %d columns available but %d columns specified",
1691  eref->aliasname, varattno, numaliases)));
1692 
1693  rte->eref = eref;
1694 
1695  /*
1696  * Set flags.
1697  *
1698  * Subqueries are never checked for access rights, so no need to perform
1699  * addRTEPermissionInfo().
1700  */
1701  rte->lateral = lateral;
1702  rte->inh = false; /* never true for subqueries */
1703  rte->inFromCl = inFromCl;
1704 
1705  /*
1706  * Add completed RTE to pstate's range table list, so that we know its
1707  * index. But we don't add it to the join list --- caller must do that if
1708  * appropriate.
1709  */
1710  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1711 
1712  /*
1713  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1714  * list --- caller must do that if appropriate.
1715  */
1716  nsitem = buildNSItemFromLists(rte, list_length(pstate->p_rtable),
1717  coltypes, coltypmods, colcollations);
1718 
1719  /*
1720  * Mark it visible as a relation name only if it had a user-written alias.
1721  */
1722  nsitem->p_rel_visible = (alias != NULL);
1723 
1724  return nsitem;
1725 }
@ RTE_SUBQUERY
Definition: parsenodes.h:1014
List * targetList
Definition: parsenodes.h:189
Expr * expr
Definition: primnodes.h:1722
AttrNumber resno
Definition: primnodes.h:1724

References RangeTblEntry::alias, Alias::aliasname, Assert(), buildNSItemFromLists(), Alias::colnames, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), RangeTblEntry::inFromCl, RangeTblEntry::inh, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, list_length(), makeAlias(), makeNode, makeString(), NIL, ParseNamespaceItem::p_rel_visible, ParseState::p_rtable, pstrdup(), TargetEntry::resno, RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblEntry::subquery, and Query::targetList.

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

2055 {
2057  char *refname;
2058  Alias *eref;
2059  int numaliases;
2060 
2061  Assert(pstate != NULL);
2062 
2063  /* Disallow more columns than will fit in a tuple */
2064  if (list_length(tf->colnames) > MaxTupleAttributeNumber)
2065  ereport(ERROR,
2066  (errcode(ERRCODE_TOO_MANY_COLUMNS),
2067  errmsg("functions in FROM can return at most %d columns",
2069  parser_errposition(pstate,
2070  exprLocation((Node *) tf))));
2071  Assert(list_length(tf->coltypes) == list_length(tf->colnames));
2072  Assert(list_length(tf->coltypmods) == list_length(tf->colnames));
2073  Assert(list_length(tf->colcollations) == list_length(tf->colnames));
2074 
2075  refname = alias ? alias->aliasname : pstrdup("xmltable");
2076 
2077  rte->rtekind = RTE_TABLEFUNC;
2078  rte->relid = InvalidOid;
2079  rte->subquery = NULL;
2080  rte->tablefunc = tf;
2081  rte->coltypes = tf->coltypes;
2082  rte->coltypmods = tf->coltypmods;
2083  rte->colcollations = tf->colcollations;
2084  rte->alias = alias;
2085 
2086  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2087  numaliases = list_length(eref->colnames);
2088 
2089  /* fill in any unspecified alias columns */
2090  if (numaliases < list_length(tf->colnames))
2091  eref->colnames = list_concat(eref->colnames,
2092  list_copy_tail(tf->colnames, numaliases));
2093 
2094  if (numaliases > list_length(tf->colnames))
2095  ereport(ERROR,
2096  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2097  errmsg("%s function has %d columns available but %d columns specified",
2098  "XMLTABLE",
2099  list_length(tf->colnames), numaliases)));
2100 
2101  rte->eref = eref;
2102 
2103  /*
2104  * Set flags and access permissions.
2105  *
2106  * Tablefuncs are never checked for access rights (at least, not by
2107  * ExecCheckPermissions()), so no need to perform addRTEPermissionInfo().
2108  */
2109  rte->lateral = lateral;
2110  rte->inh = false; /* never true for tablefunc RTEs */
2111  rte->inFromCl = inFromCl;
2112 
2113  /*
2114  * Add completed RTE to pstate's range table list, so that we know its
2115  * index. But we don't add it to the join list --- caller must do that if
2116  * appropriate.
2117  */
2118  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2119 
2120  /*
2121  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2122  * list --- caller must do that if appropriate.
2123  */
2124  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2125  rte->coltypes, rte->coltypmods,
2126  rte->colcollations);
2127 }
@ RTE_TABLEFUNC
Definition: parsenodes.h:1017
TableFunc * tablefunc
Definition: parsenodes.h:1153

References RangeTblEntry::alias, Alias::aliasname, Assert(), buildNSItemFromLists(), RangeTblEntry::colcollations, Alias::colnames, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, exprLocation(), RangeTblEntry::inFromCl, RangeTblEntry::inh, InvalidOid, lappend(), RangeTblEntry::lateral, list_concat(), list_copy_tail(), list_length(), makeAlias(), makeNode, MaxTupleAttributeNumber, NIL, ParseState::p_rtable, parser_errposition(), pstrdup(), RangeTblEntry::relid, RTE_TABLEFUNC, RangeTblEntry::rtekind, RangeTblEntry::subquery, and RangeTblEntry::tablefunc.

Referenced by transformRangeTableFunc().

◆ addRangeTableEntryForValues()

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

Definition at line 2136 of file parse_relation.c.

2144 {
2146  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
2147  Alias *eref;
2148  int numaliases;
2149  int numcolumns;
2150 
2151  Assert(pstate != NULL);
2152 
2153  rte->rtekind = RTE_VALUES;
2154  rte->relid = InvalidOid;
2155  rte->subquery = NULL;
2156  rte->values_lists = exprs;
2157  rte->coltypes = coltypes;
2158  rte->coltypmods = coltypmods;
2159  rte->colcollations = colcollations;
2160  rte->alias = alias;
2161 
2162  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2163 
2164  /* fill in any unspecified alias columns */
2165  numcolumns = list_length((List *) linitial(exprs));
2166  numaliases = list_length(eref->colnames);
2167  while (numaliases < numcolumns)
2168  {
2169  char attrname[64];
2170 
2171  numaliases++;
2172  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
2173  eref->colnames = lappend(eref->colnames,
2174  makeString(pstrdup(attrname)));
2175  }
2176  if (numcolumns < numaliases)
2177  ereport(ERROR,
2178  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2179  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
2180  refname, numcolumns, numaliases)));
2181 
2182  rte->eref = eref;
2183 
2184  /*
2185  * Set flags and access permissions.
2186  *
2187  * Subqueries are never checked for access rights, so no need to perform
2188  * addRTEPermissionInfo().
2189  */
2190  rte->lateral = lateral;
2191  rte->inh = false; /* never true for values RTEs */
2192  rte->inFromCl = inFromCl;
2193 
2194  /*
2195  * Add completed RTE to pstate's range table list, so that we know its
2196  * index. But we don't add it to the join list --- caller must do that if
2197  * appropriate.
2198  */
2199  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2200 
2201  /*
2202  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2203  * list --- caller must do that if appropriate.
2204  */
2205  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2206  rte->coltypes, rte->coltypmods,
2207  rte->colcollations);
2208 }
@ RTE_VALUES
Definition: parsenodes.h:1018
#define snprintf
Definition: port.h:238
List * values_lists
Definition: parsenodes.h:1158

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

Referenced by transformValuesClause().

◆ addRTEPermissionInfo()

RTEPermissionInfo* addRTEPermissionInfo ( List **  rteperminfos,
RangeTblEntry rte 
)

Definition at line 3869 of file parse_relation.c.

3870 {
3871  RTEPermissionInfo *perminfo;
3872 
3873  Assert(OidIsValid(rte->relid));
3874  Assert(rte->perminfoindex == 0);
3875 
3876  /* Nope, so make one and add to the list. */
3877  perminfo = makeNode(RTEPermissionInfo);
3878  perminfo->relid = rte->relid;
3879  perminfo->inh = rte->inh;
3880  /* Other information is set by fetching the node as and where needed. */
3881 
3882  *rteperminfos = lappend(*rteperminfos, perminfo);
3883 
3884  /* Note its index (1-based!) */
3885  rte->perminfoindex = list_length(*rteperminfos);
3886 
3887  return perminfo;
3888 }
#define OidIsValid(objectId)
Definition: c.h:759
Index perminfoindex
Definition: parsenodes.h:1075

References Assert(), RangeTblEntry::inh, RTEPermissionInfo::inh, lappend(), list_length(), makeNode, OidIsValid, RangeTblEntry::perminfoindex, RangeTblEntry::relid, and RTEPermissionInfo::relid.

Referenced by add_rte_to_flat_rtable(), addRangeTableEntry(), addRangeTableEntryForRelation(), create_edata_for_relation(), plan_cluster_use_sort(), plan_create_index_workers(), and rewriteTargetView().

◆ attnameAttNum()

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

Definition at line 3478 of file parse_relation.c.

3479 {
3480  int i;
3481 
3482  for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3483  {
3485 
3486  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3487  return i + 1;
3488  }
3489 
3490  if (sysColOK)
3491  {
3493  return i;
3494  }
3495 
3496  /* on failure */
3497  return InvalidAttrNumber;
3498 }
#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:507

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

3529 {
3530  if (attid <= 0)
3531  {
3532  const FormData_pg_attribute *sysatt;
3533 
3534  sysatt = SystemAttributeDefinition(attid);
3535  return &sysatt->attname;
3536  }
3537  if (attid > rd->rd_att->natts)
3538  elog(ERROR, "invalid attribute number %d", attid);
3539  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3540 }
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 3570 of file parse_relation.c.

3571 {
3572  if (attid <= 0)
3573  {
3574  /* All system attributes are of noncollatable types. */
3575  return InvalidOid;
3576  }
3577  if (attid > rd->rd_att->natts)
3578  elog(ERROR, "invalid attribute number %d", attid);
3579  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3580 }

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

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3550 of file parse_relation.c.

3551 {
3552  if (attid <= 0)
3553  {
3554  const FormData_pg_attribute *sysatt;
3555 
3556  sysatt = SystemAttributeDefinition(attid);
3557  return sysatt->atttypid;
3558  }
3559  if (attid > rd->rd_att->natts)
3560  elog(ERROR, "invalid attribute number %d", attid);
3561  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3562 }

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

436 {
437  ListCell *l1;
438 
439  foreach(l1, namespace1)
440  {
441  ParseNamespaceItem *nsitem1 = (ParseNamespaceItem *) lfirst(l1);
442  RangeTblEntry *rte1 = nsitem1->p_rte;
443  const char *aliasname1 = nsitem1->p_names->aliasname;
444  ListCell *l2;
445 
446  if (!nsitem1->p_rel_visible)
447  continue;
448 
449  foreach(l2, namespace2)
450  {
451  ParseNamespaceItem *nsitem2 = (ParseNamespaceItem *) lfirst(l2);
452  RangeTblEntry *rte2 = nsitem2->p_rte;
453  const char *aliasname2 = nsitem2->p_names->aliasname;
454 
455  if (!nsitem2->p_rel_visible)
456  continue;
457  if (strcmp(aliasname2, aliasname1) != 0)
458  continue; /* definitely no conflict */
459  if (rte1->rtekind == RTE_RELATION && rte1->alias == NULL &&
460  rte2->rtekind == RTE_RELATION && rte2->alias == NULL &&
461  rte1->relid != rte2->relid)
462  continue; /* no conflict per SQL rule */
463  ereport(ERROR,
464  (errcode(ERRCODE_DUPLICATE_ALIAS),
465  errmsg("table name \"%s\" specified more than once",
466  aliasname1)));
467  }
468  }
469 }

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

885 {
886  Node *result = NULL;
887  int sublevels_up = 0;
888  ParseState *orig_pstate = pstate;
889 
890  while (pstate != NULL)
891  {
892  ListCell *l;
893 
894  foreach(l, pstate->p_namespace)
895  {
897  Node *newresult;
898 
899  /* Ignore table-only items */
900  if (!nsitem->p_cols_visible)
901  continue;
902  /* If not inside LATERAL, ignore lateral-only items */
903  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
904  continue;
905 
906  /* use orig_pstate here for consistency with other callers */
907  newresult = scanNSItemForColumn(orig_pstate, nsitem, sublevels_up,
908  colname, location);
909 
910  if (newresult)
911  {
912  if (result)
913  ereport(ERROR,
914  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
915  errmsg("column reference \"%s\" is ambiguous",
916  colname),
917  parser_errposition(pstate, location)));
918  check_lateral_ref_ok(pstate, nsitem, location);
919  result = newresult;
920  }
921  }
922 
923  if (result != NULL || localonly)
924  break; /* found, or don't want to look at parent */
925 
926  pstate = pstate->parentParseState;
927  sublevels_up++;
928  }
929 
930  return result;
931 }
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:191
bool p_lateral_active
Definition: parse_node.h:202

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

3662 {
3664 
3665  /*
3666  * Search the entire rtable looking for possible matches. If we find one,
3667  * emit a hint about it.
3668  */
3669  state = searchRangeTableForCol(pstate, relname, colname, location);
3670 
3671  /*
3672  * If there are exact match(es), they must be inaccessible for some
3673  * reason.
3674  */
3675  if (state->rexact1)
3676  {
3677  /*
3678  * We don't try too hard when there's multiple inaccessible exact
3679  * matches, but at least be sure that we don't misleadingly suggest
3680  * that there's only one.
3681  */
3682  if (state->rexact2)
3683  ereport(ERROR,
3684  (errcode(ERRCODE_UNDEFINED_COLUMN),
3685  relname ?
3686  errmsg("column %s.%s does not exist", relname, colname) :
3687  errmsg("column \"%s\" does not exist", colname),
3688  errdetail("There are columns named \"%s\", but they are in tables that cannot be referenced from this part of the query.",
3689  colname),
3690  !relname ? errhint("Try using a table-qualified name.") : 0,
3691  parser_errposition(pstate, location)));
3692  /* Single exact match, so try to determine why it's inaccessible. */
3693  ereport(ERROR,
3694  (errcode(ERRCODE_UNDEFINED_COLUMN),
3695  relname ?
3696  errmsg("column %s.%s does not exist", relname, colname) :
3697  errmsg("column \"%s\" does not exist", colname),
3698  errdetail("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3699  colname, state->rexact1->eref->aliasname),
3700  rte_visible_if_lateral(pstate, state->rexact1) ?
3701  errhint("To reference that column, you must mark this subquery with LATERAL.") :
3702  (!relname && rte_visible_if_qualified(pstate, state->rexact1)) ?
3703  errhint("To reference that column, you must use a table-qualified name.") : 0,
3704  parser_errposition(pstate, location)));
3705  }
3706 
3707  if (!state->rsecond)
3708  {
3709  /* If we found no match at all, we have little to report */
3710  if (!state->rfirst)
3711  ereport(ERROR,
3712  (errcode(ERRCODE_UNDEFINED_COLUMN),
3713  relname ?
3714  errmsg("column %s.%s does not exist", relname, colname) :
3715  errmsg("column \"%s\" does not exist", colname),
3716  parser_errposition(pstate, location)));
3717  /* Handle case where we have a single alternative spelling to offer */
3718  ereport(ERROR,
3719  (errcode(ERRCODE_UNDEFINED_COLUMN),
3720  relname ?
3721  errmsg("column %s.%s does not exist", relname, colname) :
3722  errmsg("column \"%s\" does not exist", colname),
3723  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3724  state->rfirst->eref->aliasname,
3725  strVal(list_nth(state->rfirst->eref->colnames,
3726  state->first - 1))),
3727  parser_errposition(pstate, location)));
3728  }
3729  else
3730  {
3731  /* Handle case where there are two equally useful column hints */
3732  ereport(ERROR,
3733  (errcode(ERRCODE_UNDEFINED_COLUMN),
3734  relname ?
3735  errmsg("column %s.%s does not exist", relname, colname) :
3736  errmsg("column \"%s\" does not exist", colname),
3737  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3738  state->rfirst->eref->aliasname,
3739  strVal(list_nth(state->rfirst->eref->colnames,
3740  state->first - 1)),
3741  state->rsecond->eref->aliasname,
3742  strVal(list_nth(state->rsecond->eref->colnames,
3743  state->second - 1))),
3744  parser_errposition(pstate, location)));
3745  }
3746 }
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errhint(const char *fmt,...)
Definition: elog.c:1316
static bool rte_visible_if_lateral(ParseState *pstate, RangeTblEntry *rte)
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colname, int location)
static bool rte_visible_if_qualified(ParseState *pstate, RangeTblEntry *rte)
NameData relname
Definition: pg_class.h:38
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
Definition: regguts.h:318
#define strVal(v)
Definition: value.h:82

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

Referenced by transformColumnRef().

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3589 of file parse_relation.c.

3590 {
3591  RangeTblEntry *rte;
3592  const char *badAlias = NULL;
3593 
3594  /*
3595  * Check to see if there are any potential matches in the query's
3596  * rangetable. (Note: cases involving a bad schema name in the RangeVar
3597  * will throw error immediately here. That seems OK.)
3598  */
3599  rte = searchRangeTableForRel(pstate, relation);
3600 
3601  /*
3602  * If we found a match that has an alias and the alias is visible in the
3603  * namespace, then the problem is probably use of the relation's real name
3604  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3605  * common enough to justify a specific hint.
3606  *
3607  * If we found a match that doesn't meet those criteria, assume the
3608  * problem is illegal use of a relation outside its scope, as in the
3609  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3610  */
3611  if (rte && rte->alias &&
3612  strcmp(rte->eref->aliasname, relation->relname) != 0)
3613  {
3614  ParseNamespaceItem *nsitem;
3615  int sublevels_up;
3616 
3617  nsitem = refnameNamespaceItem(pstate, NULL, rte->eref->aliasname,
3618  relation->location,
3619  &sublevels_up);
3620  if (nsitem && nsitem->p_rte == rte)
3621  badAlias = rte->eref->aliasname;
3622  }
3623 
3624  /* If it looks like the user forgot to use an alias, hint about that */
3625  if (badAlias)
3626  ereport(ERROR,
3628  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3629  relation->relname),
3630  errhint("Perhaps you meant to reference the table alias \"%s\".",
3631  badAlias),
3632  parser_errposition(pstate, relation->location)));
3633  /* Hint about case where we found an (inaccessible) exact match */
3634  else if (rte)
3635  ereport(ERROR,
3637  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3638  relation->relname),
3639  errdetail("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3640  rte->eref->aliasname),
3641  rte_visible_if_lateral(pstate, rte) ?
3642  errhint("To reference that table, you must mark this subquery with LATERAL.") : 0,
3643  parser_errposition(pstate, relation->location)));
3644  /* Else, we have nothing to offer but the bald statement of error */
3645  else
3646  ereport(ERROR,
3648  errmsg("missing FROM-clause entry for table \"%s\"",
3649  relation->relname),
3650  parser_errposition(pstate, relation->location)));
3651 }
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:78

References RangeTblEntry::alias, Alias::aliasname, RangeTblEntry::eref, ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errdetail(), errhint(), errmsg(), ERROR, RangeVar::location, ParseNamespaceItem::p_rte, parser_errposition(), refnameNamespaceItem(), RangeVar::relname, rte_visible_if_lateral(), 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 3186 of file parse_relation.c.

3188 {
3189  RangeTblEntry *rte = nsitem->p_rte;
3190  RTEPermissionInfo *perminfo = nsitem->p_perminfo;
3191  List *names,
3192  *vars;
3193  ListCell *name,
3194  *var;
3195  List *te_list = NIL;
3196 
3197  vars = expandNSItemVars(pstate, nsitem, sublevels_up, location, &names);
3198 
3199  /*
3200  * Require read access to the table. This is normally redundant with the
3201  * markVarForSelectPriv calls below, but not if the table has zero
3202  * columns. We need not do anything if the nsitem is for a join: its
3203  * component tables will have been marked ACL_SELECT when they were added
3204  * to the rangetable. (This step changes things only for the target
3205  * relation of UPDATE/DELETE, which cannot be under a join.)
3206  */
3207  if (rte->rtekind == RTE_RELATION)
3208  {
3209  Assert(perminfo != NULL);
3210  perminfo->requiredPerms |= ACL_SELECT;
3211  }
3212 
3213  forboth(name, names, var, vars)
3214  {
3215  char *label = strVal(lfirst(name));
3216  Var *varnode = (Var *) lfirst(var);
3217  TargetEntry *te;
3218 
3219  te = makeTargetEntry((Expr *) varnode,
3220  (AttrNumber) pstate->p_next_resno++,
3221  label,
3222  false);
3223  te_list = lappend(te_list, te);
3224 
3225  if (require_col_privs)
3226  {
3227  /* Require read access to each column */
3228  markVarForSelectPriv(pstate, varnode);
3229  }
3230  }
3231 
3232  Assert(name == NULL && var == NULL); /* lists not the same length? */
3233 
3234  return te_list;
3235 }
const char * name
Definition: encode.c:571
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:240
void markVarForSelectPriv(ParseState *pstate, Var *var)
List * expandNSItemVars(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
static char * label
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:467
int p_next_resno
Definition: parse_node.h:211
Definition: primnodes.h:223
Definition: regcomp.c:282

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

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

◆ expandNSItemVars()

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

Definition at line 3122 of file parse_relation.c.

3125 {
3126  List *result = NIL;
3127  int colindex;
3128  ListCell *lc;
3129 
3130  if (colnames)
3131  *colnames = NIL;
3132  colindex = 0;
3133  foreach(lc, nsitem->p_names->colnames)
3134  {
3135  String *colnameval = lfirst(lc);
3136  const char *colname = strVal(colnameval);
3137  ParseNamespaceColumn *nscol = nsitem->p_nscolumns + colindex;
3138 
3139  if (nscol->p_dontexpand)
3140  {
3141  /* skip */
3142  }
3143  else if (colname[0])
3144  {
3145  Var *var;
3146 
3147  Assert(nscol->p_varno > 0);
3148  var = makeVar(nscol->p_varno,
3149  nscol->p_varattno,
3150  nscol->p_vartype,
3151  nscol->p_vartypmod,
3152  nscol->p_varcollid,
3153  sublevels_up);
3154  /* makeVar doesn't offer parameters for these, so set by hand: */
3155  var->varnosyn = nscol->p_varnosyn;
3156  var->varattnosyn = nscol->p_varattnosyn;
3157  var->location = location;
3158 
3159  /* ... and update varnullingrels */
3160  markNullableIfNeeded(pstate, var);
3161 
3162  result = lappend(result, var);
3163  if (colnames)
3164  *colnames = lappend(*colnames, colnameval);
3165  }
3166  else
3167  {
3168  /* dropped column, ignore */
3169  Assert(nscol->p_varno == 0);
3170  }
3171  colindex++;
3172  }
3173  return result;
3174 }
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
void markNullableIfNeeded(ParseState *pstate, Var *var)
AttrNumber p_varattno
Definition: parse_node.h:321
AttrNumber p_varattnosyn
Definition: parse_node.h:326
Definition: value.h:64
int location
Definition: primnodes.h:268

References Assert(), Alias::colnames, lappend(), lfirst, Var::location, makeVar(), markNullableIfNeeded(), 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, and strVal.

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

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

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::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::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(), set_relation_column_names(), and transformWholeRowRef().

◆ GetCTEForRTE()

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

Definition at line 557 of file parse_relation.c.

558 {
559  Index levelsup;
560  ListCell *lc;
561 
562  Assert(rte->rtekind == RTE_CTE);
563  levelsup = rte->ctelevelsup + rtelevelsup;
564  while (levelsup-- > 0)
565  {
566  pstate = pstate->parentParseState;
567  if (!pstate) /* shouldn't happen */
568  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
569  }
570  foreach(lc, pstate->p_ctenamespace)
571  {
572  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
573 
574  if (strcmp(cte->ctename, rte->ctename) == 0)
575  return cte;
576  }
577  /* shouldn't happen */
578  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
579  return NULL; /* keep compiler quiet */
580 }
unsigned int Index
Definition: c.h:598
List * p_ctenamespace
Definition: parse_node.h:203

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

513 {
514  ListCell *lc;
515 
516  while (sublevels_up-- > 0)
517  {
518  pstate = pstate->parentParseState;
519  Assert(pstate != NULL);
520  }
521  foreach(lc, pstate->p_namespace)
522  {
523  ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
524 
525  if (nsitem->p_rtindex == varno)
526  return nsitem;
527  }
528  elog(ERROR, "nsitem not found (internal error)");
529  return NULL; /* keep compiler quiet */
530 }

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

540 {
541  while (sublevels_up-- > 0)
542  {
543  pstate = pstate->parentParseState;
544  Assert(pstate != NULL);
545  }
546  Assert(varno > 0 && varno <= list_length(pstate->p_rtable));
547  return rt_fetch(varno, pstate->p_rtable);
548 }
#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().

◆ getRTEPermissionInfo()

RTEPermissionInfo* getRTEPermissionInfo ( List rteperminfos,
RangeTblEntry rte 
)

Definition at line 3898 of file parse_relation.c.

3899 {
3900  RTEPermissionInfo *perminfo;
3901 
3902  if (rte->perminfoindex == 0 ||
3903  rte->perminfoindex > list_length(rteperminfos))
3904  elog(ERROR, "invalid perminfoindex %d in RTE with relid %u",
3905  rte->perminfoindex, rte->relid);
3906  perminfo = list_nth_node(RTEPermissionInfo, rteperminfos,
3907  rte->perminfoindex - 1);
3908  if (perminfo->relid != rte->relid)
3909  elog(ERROR, "permission info at index %u (with relid=%u) does not match provided RTE (with relid=%u)",
3910  rte->perminfoindex, perminfo->relid, rte->relid);
3911 
3912  return perminfo;
3913 }
#define list_nth_node(type, list, n)
Definition: pg_list.h:327

References elog(), ERROR, list_length(), list_nth_node, RangeTblEntry::perminfoindex, RangeTblEntry::relid, and RTEPermissionInfo::relid.

Referenced by add_rte_to_flat_rtable(), build_simple_rel(), expand_inherited_rtentry(), get_rel_all_updated_cols(), get_row_security_policies(), GetResultRTEPermissionInfo(), markQueryForLocking(), markRTEForSelectPriv(), rewriteTargetView(), and transformLockingClause().

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

Definition at line 2580 of file parse_relation.c.

2581 {
2582  ListCell *l;
2583 
2584  /*
2585  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2586  * parent level, then act as though there's a generic FOR UPDATE here.
2587  */
2588  if (pstate->p_locked_from_parent)
2589  return true;
2590 
2591  foreach(l, pstate->p_locking_clause)
2592  {
2593  LockingClause *lc = (LockingClause *) lfirst(l);
2594 
2595  if (lc->lockedRels == NIL)
2596  {
2597  /* all tables used in query */
2598  return true;
2599  }
2600  else if (refname != NULL)
2601  {
2602  /* just the named tables */
2603  ListCell *l2;
2604 
2605  foreach(l2, lc->lockedRels)
2606  {
2607  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2608 
2609  if (strcmp(refname, thisrel->relname) == 0)
2610  return true;
2611  }
2612  }
2613  }
2614  return false;
2615 }
List * lockedRels
Definition: parsenodes.h:829
bool p_locked_from_parent
Definition: parse_node.h:214
List * p_locking_clause
Definition: parse_node.h:213

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

3820 {
3821  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3822 }
static bool isQueryUsingTempRelation_walker(Node *node, void *context)

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

◆ markNullableIfNeeded()

void markNullableIfNeeded ( ParseState pstate,
Var var 
)

Definition at line 1035 of file parse_relation.c.

1036 {
1037  int rtindex = var->varno;
1038  Bitmapset *relids;
1039 
1040  /* Find the appropriate pstate */
1041  for (int lv = 0; lv < var->varlevelsup; lv++)
1042  pstate = pstate->parentParseState;
1043 
1044  /* Find currently-relevant join relids for the Var's rel */
1045  if (rtindex > 0 && rtindex <= list_length(pstate->p_nullingrels))
1046  relids = (Bitmapset *) list_nth(pstate->p_nullingrels, rtindex - 1);
1047  else
1048  relids = NULL;
1049 
1050  /*
1051  * Merge with any already-declared nulling rels. (Typically there won't
1052  * be any, but let's get it right if there are.)
1053  */
1054  if (relids != NULL)
1055  var->varnullingrels = bms_union(var->varnullingrels, relids);
1056 }
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:226
List * p_nullingrels
Definition: parse_node.h:197
int varno
Definition: primnodes.h:230

References bms_union(), if(), list_length(), list_nth(), ParseState::p_nullingrels, ParseState::parentParseState, Var::varlevelsup, and Var::varno.

Referenced by buildVarFromNSColumn(), expandNSItemVars(), scanNSItemForColumn(), and transformWholeRowRef().

◆ markVarForSelectPriv()

void markVarForSelectPriv ( ParseState pstate,
Var var 
)

Definition at line 1150 of file parse_relation.c.

1151 {
1152  Index lv;
1153 
1154  Assert(IsA(var, Var));
1155  /* Find the appropriate pstate if it's an uplevel Var */
1156  for (lv = 0; lv < var->varlevelsup; lv++)
1157  pstate = pstate->parentParseState;
1158  markRTEForSelectPriv(pstate, var->varno, var->varattno);
1159 }
static void markRTEForSelectPriv(ParseState *pstate, int rtindex, AttrNumber col)
AttrNumber varattno
Definition: primnodes.h:235

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

1418 {
1419  Relation rel;
1420  ParseCallbackState pcbstate;
1421 
1422  setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1423  rel = table_openrv_extended(relation, lockmode, true);
1424  if (rel == NULL)
1425  {
1426  if (relation->schemaname)
1427  ereport(ERROR,
1429  errmsg("relation \"%s.%s\" does not exist",
1430  relation->schemaname, relation->relname)));
1431  else
1432  {
1433  /*
1434  * An unqualified name might have been meant as a reference to
1435  * some not-yet-in-scope CTE. The bare "does not exist" message
1436  * has proven remarkably unhelpful for figuring out such problems,
1437  * so we take pains to offer a specific hint.
1438  */
1439  if (isFutureCTE(pstate, relation->relname))
1440  ereport(ERROR,
1442  errmsg("relation \"%s\" does not exist",
1443  relation->relname),
1444  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1445  relation->relname),
1446  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1447  else
1448  ereport(ERROR,
1450  errmsg("relation \"%s\" does not exist",
1451  relation->relname)));
1452  }
1453  }
1455  return rel;
1456 }
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:161
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:145
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:103

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

134 {
135  Oid relId = InvalidOid;
136 
137  if (sublevels_up)
138  *sublevels_up = 0;
139 
140  if (schemaname != NULL)
141  {
142  Oid namespaceId;
143 
144  /*
145  * We can use LookupNamespaceNoError() here because we are only
146  * interested in finding existing RTEs. Checking USAGE permission on
147  * the schema is unnecessary since it would have already been checked
148  * when the RTE was made. Furthermore, we want to report "RTE not
149  * found", not "no permissions for schema", if the name happens to
150  * match a schema name the user hasn't got access to.
151  */
152  namespaceId = LookupNamespaceNoError(schemaname);
153  if (!OidIsValid(namespaceId))
154  return NULL;
155  relId = get_relname_relid(refname, namespaceId);
156  if (!OidIsValid(relId))
157  return NULL;
158  }
159 
160  while (pstate != NULL)
161  {
162  ParseNamespaceItem *result;
163 
164  if (OidIsValid(relId))
165  result = scanNameSpaceForRelid(pstate, relId, location);
166  else
167  result = scanNameSpaceForRefname(pstate, refname, location);
168 
169  if (result)
170  return result;
171 
172  if (sublevels_up)
173  (*sublevels_up)++;
174  else
175  break;
176 
177  pstate = pstate->parentParseState;
178  }
179  return NULL;
180 }
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1867
Oid LookupNamespaceNoError(const char *nspname)
Definition: namespace.c:2906
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 282 of file parse_relation.c.

284 {
285  Index levelsup;
286 
287  for (levelsup = 0;
288  pstate != NULL;
289  pstate = pstate->parentParseState, levelsup++)
290  {
291  ListCell *lc;
292 
293  foreach(lc, pstate->p_ctenamespace)
294  {
295  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
296 
297  if (strcmp(cte->ctename, refname) == 0)
298  {
299  *ctelevelsup = levelsup;
300  return cte;
301  }
302  }
303  }
304  return NULL;
305 }

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

336 {
337  return name_matches_visible_ENR(pstate, refname);
338 }
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 680 of file parse_relation.c.

682 {
683  RangeTblEntry *rte = nsitem->p_rte;
684  int attnum;
685  Var *var;
686 
687  /*
688  * Scan the nsitem's column names (or aliases) for a match. Complain if
689  * multiple matches.
690  */
691  attnum = scanRTEForColumn(pstate, rte, nsitem->p_names,
692  colname, location,
693  0, NULL);
694 
695  if (attnum == InvalidAttrNumber)
696  return NULL; /* Return NULL if no match */
697 
698  /* In constraint check, no system column is allowed except tableOid */
699  if (pstate->p_expr_kind == EXPR_KIND_CHECK_CONSTRAINT &&
701  ereport(ERROR,
702  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
703  errmsg("system column \"%s\" reference in check constraint is invalid",
704  colname),
705  parser_errposition(pstate, location)));
706 
707  /* In generated column, no system column is allowed except tableOid */
708  if (pstate->p_expr_kind == EXPR_KIND_GENERATED_COLUMN &&
710  ereport(ERROR,
711  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
712  errmsg("cannot use system column \"%s\" in column generation expression",
713  colname),
714  parser_errposition(pstate, location)));
715 
716  /*
717  * In a MERGE WHEN condition, no system column is allowed except tableOid
718  */
719  if (pstate->p_expr_kind == EXPR_KIND_MERGE_WHEN &&
721  ereport(ERROR,
722  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
723  errmsg("cannot use system column \"%s\" in MERGE WHEN condition",
724  colname),
725  parser_errposition(pstate, location)));
726 
727  /* Found a valid match, so build a Var */
729  {
730  /* Get attribute data from the ParseNamespaceColumn array */
731  ParseNamespaceColumn *nscol = &nsitem->p_nscolumns[attnum - 1];
732 
733  /* Complain if dropped column. See notes in scanRTEForColumn. */
734  if (nscol->p_varno == 0)
735  ereport(ERROR,
736  (errcode(ERRCODE_UNDEFINED_COLUMN),
737  errmsg("column \"%s\" of relation \"%s\" does not exist",
738  colname,
739  nsitem->p_names->aliasname)));
740 
741  var = makeVar(nscol->p_varno,
742  nscol->p_varattno,
743  nscol->p_vartype,
744  nscol->p_vartypmod,
745  nscol->p_varcollid,
746  sublevels_up);
747  /* makeVar doesn't offer parameters for these, so set them by hand: */
748  var->varnosyn = nscol->p_varnosyn;
749  var->varattnosyn = nscol->p_varattnosyn;
750  }
751  else
752  {
753  /* System column, so use predetermined type data */
754  const FormData_pg_attribute *sysatt;
755 
757  var = makeVar(nsitem->p_rtindex,
758  attnum,
759  sysatt->atttypid,
760  sysatt->atttypmod,
761  sysatt->attcollation,
762  sublevels_up);
763  }
764  var->location = location;
765 
766  /* Mark Var if it's nulled by any outer joins */
767  markNullableIfNeeded(pstate, var);
768 
769  /* Require read access to the column */
770  markVarForSelectPriv(pstate, var);
771 
772  return (Node *) var;
773 }
@ 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:210
#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(), markNullableIfNeeded(), 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(), and TableOidAttributeNumber.

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