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

Go to the source code of this file.

Functions

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

Function Documentation

◆ addNSItemToQuery()

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

Definition at line 2466 of file parse_relation.c.

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

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

2469 {
2470  if (addToJoinList)
2471  {
2473 
2474  rtr->rtindex = nsitem->p_rtindex;
2475  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2476  }
2477  if (addToRelNameSpace || addToVarNameSpace)
2478  {
2479  /* Set the new nsitem's visibility flags correctly */
2480  nsitem->p_rel_visible = addToRelNameSpace;
2481  nsitem->p_cols_visible = addToVarNameSpace;
2482  nsitem->p_lateral_only = false;
2483  nsitem->p_lateral_ok = true;
2484  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2485  }
2486 }
List * p_namespace
Definition: parse_node.h:184
List * lappend(List *list, void *datum)
Definition: list.c:322
#define makeNode(_type_)
Definition: nodes.h:573
List * p_joinlist
Definition: parse_node.h:182

◆ addRangeTableEntry()

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

Definition at line 1397 of file parse_relation.c.

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

Referenced by transformTableEntry().

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

◆ addRangeTableEntryForCTE()

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

Definition at line 2195 of file parse_relation.c.

References RangeVar::alias, RangeTblEntry::alias, Alias::aliasname, Assert, buildNSItemFromLists(), RangeTblEntry::checkAsUser, CMD_SELECT, RangeTblEntry::colcollations, Alias::colnames, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, Query::commandType, copyObject, CommonTableExpr::ctecolcollations, CommonTableExpr::ctecolnames, CommonTableExpr::ctecoltypes, CommonTableExpr::ctecoltypmods, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, CommonTableExpr::ctename, CommonTableExpr::ctequery, CommonTableExpr::cterecursive, CommonTableExpr::cterefcount, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, IsA, lappend(), RangeTblEntry::lateral, lfirst, list_length(), RangeVar::location, makeAlias(), makeNode, NIL, ParseState::p_rtable, parser_errposition(), RangeTblEntry::requiredPerms, Query::returningList, RTE_CTE, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, RangeTblEntry::self_reference, and RangeTblEntry::updatedCols.

Referenced by getNSItemForSpecialRelationTypes().

2200 {
2202  Alias *alias = rv->alias;
2203  char *refname = alias ? alias->aliasname : cte->ctename;
2204  Alias *eref;
2205  int numaliases;
2206  int varattno;
2207  ListCell *lc;
2208 
2209  Assert(pstate != NULL);
2210 
2211  rte->rtekind = RTE_CTE;
2212  rte->ctename = cte->ctename;
2213  rte->ctelevelsup = levelsup;
2214 
2215  /* Self-reference if and only if CTE's parse analysis isn't completed */
2216  rte->self_reference = !IsA(cte->ctequery, Query);
2217  Assert(cte->cterecursive || !rte->self_reference);
2218  /* Bump the CTE's refcount if this isn't a self-reference */
2219  if (!rte->self_reference)
2220  cte->cterefcount++;
2221 
2222  /*
2223  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
2224  * This won't get checked in case of a self-reference, but that's OK
2225  * because data-modifying CTEs aren't allowed to be recursive anyhow.
2226  */
2227  if (IsA(cte->ctequery, Query))
2228  {
2229  Query *ctequery = (Query *) cte->ctequery;
2230 
2231  if (ctequery->commandType != CMD_SELECT &&
2232  ctequery->returningList == NIL)
2233  ereport(ERROR,
2234  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2235  errmsg("WITH query \"%s\" does not have a RETURNING clause",
2236  cte->ctename),
2237  parser_errposition(pstate, rv->location)));
2238  }
2239 
2240  rte->coltypes = cte->ctecoltypes;
2241  rte->coltypmods = cte->ctecoltypmods;
2242  rte->colcollations = cte->ctecolcollations;
2243 
2244  rte->alias = alias;
2245  if (alias)
2246  eref = copyObject(alias);
2247  else
2248  eref = makeAlias(refname, NIL);
2249  numaliases = list_length(eref->colnames);
2250 
2251  /* fill in any unspecified alias columns */
2252  varattno = 0;
2253  foreach(lc, cte->ctecolnames)
2254  {
2255  varattno++;
2256  if (varattno > numaliases)
2257  eref->colnames = lappend(eref->colnames, lfirst(lc));
2258  }
2259  if (varattno < numaliases)
2260  ereport(ERROR,
2261  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2262  errmsg("table \"%s\" has %d columns available but %d columns specified",
2263  refname, varattno, numaliases)));
2264 
2265  rte->eref = eref;
2266 
2267  /*
2268  * Set flags and access permissions.
2269  *
2270  * Subqueries are never checked for access rights.
2271  */
2272  rte->lateral = false;
2273  rte->inh = false; /* never true for subqueries */
2274  rte->inFromCl = inFromCl;
2275 
2276  rte->requiredPerms = 0;
2277  rte->checkAsUser = InvalidOid;
2278  rte->selectedCols = NULL;
2279  rte->insertedCols = NULL;
2280  rte->updatedCols = NULL;
2281  rte->extraUpdatedCols = NULL;
2282 
2283  /*
2284  * Add completed RTE to pstate's range table list, so that we know its
2285  * index. But we don't add it to the join list --- caller must do that if
2286  * appropriate.
2287  */
2288  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2289 
2290  /*
2291  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2292  * list --- caller must do that if appropriate.
2293  */
2294  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2295  rte->coltypes, rte->coltypmods,
2296  rte->colcollations);
2297 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Alias * alias
Definition: parsenodes.h:1112
List * colnames
Definition: primnodes.h:43
List * coltypmods
Definition: parsenodes.h:1100
int errcode(int sqlerrcode)
Definition: elog.c:608
AclMode requiredPerms
Definition: parsenodes.h:1117
List * colcollations
Definition: parsenodes.h:1101
int location
Definition: primnodes.h:73
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1122
Bitmapset * selectedCols
Definition: parsenodes.h:1119
#define ERROR
Definition: elog.h:43
List * ctecoltypmods
Definition: parsenodes.h:1460
List * returningList
Definition: parsenodes.h:146
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:322
List * ctecolnames
Definition: parsenodes.h:1458
bool self_reference
Definition: parsenodes.h:1079
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1121
CmdType commandType
Definition: parsenodes.h:112
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Index ctelevelsup
Definition: parsenodes.h:1078
List * ctecoltypes
Definition: parsenodes.h:1459
RTEKind rtekind
Definition: parsenodes.h:974
char * ctename
Definition: parsenodes.h:1077
int errmsg(const char *fmt,...)
Definition: elog.c:822
Bitmapset * insertedCols
Definition: parsenodes.h:1120
Alias * alias
Definition: primnodes.h:72
Alias * eref
Definition: parsenodes.h:1113
List * ctecolcollations
Definition: parsenodes.h:1461
#define copyObject(obj)
Definition: nodes.h:641
List * coltypes
Definition: parsenodes.h:1099
List * p_rtable
Definition: parse_node.h:180

◆ addRangeTableEntryForENR()

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

Definition at line 2313 of file parse_relation.c.

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

Referenced by getNSItemForSpecialRelationTypes().

2316 {
2318  Alias *alias = rv->alias;
2319  char *refname = alias ? alias->aliasname : rv->relname;
2321  TupleDesc tupdesc;
2322  int attno;
2323 
2324  Assert(pstate != NULL);
2325  enrmd = get_visible_ENR(pstate, rv->relname);
2326  Assert(enrmd != NULL);
2327 
2328  switch (enrmd->enrtype)
2329  {
2330  case ENR_NAMED_TUPLESTORE:
2332  break;
2333 
2334  default:
2335  elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2336  return NULL; /* for fussy compilers */
2337  }
2338 
2339  /*
2340  * Record dependency on a relation. This allows plans to be invalidated
2341  * if they access transition tables linked to a table that is altered.
2342  */
2343  rte->relid = enrmd->reliddesc;
2344 
2345  /*
2346  * Build the list of effective column names using user-supplied aliases
2347  * and/or actual column names.
2348  */
2349  tupdesc = ENRMetadataGetTupDesc(enrmd);
2350  rte->eref = makeAlias(refname, NIL);
2351  buildRelationAliases(tupdesc, alias, rte->eref);
2352 
2353  /* Record additional data for ENR, including column type info */
2354  rte->enrname = enrmd->name;
2355  rte->enrtuples = enrmd->enrtuples;
2356  rte->coltypes = NIL;
2357  rte->coltypmods = NIL;
2358  rte->colcollations = NIL;
2359  for (attno = 1; attno <= tupdesc->natts; ++attno)
2360  {
2361  Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2362 
2363  if (att->attisdropped)
2364  {
2365  /* Record zeroes for a dropped column */
2366  rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2367  rte->coltypmods = lappend_int(rte->coltypmods, 0);
2369  }
2370  else
2371  {
2372  /* Let's just make sure we can tell this isn't dropped */
2373  if (att->atttypid == InvalidOid)
2374  elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2375  rv->relname);
2376  rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2377  rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2379  att->attcollation);
2380  }
2381  }
2382 
2383  /*
2384  * Set flags and access permissions.
2385  *
2386  * ENRs are never checked for access rights.
2387  */
2388  rte->lateral = false;
2389  rte->inh = false; /* never true for ENRs */
2390  rte->inFromCl = inFromCl;
2391 
2392  rte->requiredPerms = 0;
2393  rte->checkAsUser = InvalidOid;
2394  rte->selectedCols = NULL;
2395 
2396  /*
2397  * Add completed RTE to pstate's range table list, so that we know its
2398  * index. But we don't add it to the join list --- caller must do that if
2399  * appropriate.
2400  */
2401  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2402 
2403  /*
2404  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2405  * list --- caller must do that if appropriate.
2406  */
2407  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
2408  tupdesc);
2409 }
#define NIL
Definition: pg_list.h:65
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
List * coltypmods
Definition: parsenodes.h:1100
AclMode requiredPerms
Definition: parsenodes.h:1117
List * lappend_oid(List *list, Oid datum)
Definition: list.c:358
List * colcollations
Definition: parsenodes.h:1101
EphemeralNamedRelationMetadata get_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:26
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
char * relname
Definition: primnodes.h:68
Bitmapset * selectedCols
Definition: parsenodes.h:1119
#define ERROR
Definition: elog.h:43
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
char * enrname
Definition: parsenodes.h:1106
List * lappend_int(List *list, int datum)
Definition: list.c:340
List * lappend(List *list, void *datum)
Definition: list.c:322
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
RTEKind rtekind
Definition: parsenodes.h:974
double enrtuples
Definition: parsenodes.h:1107
#define elog(elevel,...)
Definition: elog.h:228
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * alias
Definition: primnodes.h:72
EphemeralNameRelationType enrtype
Alias * eref
Definition: parsenodes.h:1113
List * coltypes
Definition: parsenodes.h:1099
List * p_rtable
Definition: parse_node.h:180

◆ addRangeTableEntryForFunction()

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

Definition at line 1663 of file parse_relation.c.

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

Referenced by transformRangeFunction().

1670 {
1672  Alias *alias = rangefunc->alias;
1673  Alias *eref;
1674  char *aliasname;
1675  int nfuncs = list_length(funcexprs);
1676  TupleDesc *functupdescs;
1677  TupleDesc tupdesc;
1678  ListCell *lc1,
1679  *lc2,
1680  *lc3;
1681  int i;
1682  int j;
1683  int funcno;
1684  int natts,
1685  totalatts;
1686 
1687  Assert(pstate != NULL);
1688 
1689  rte->rtekind = RTE_FUNCTION;
1690  rte->relid = InvalidOid;
1691  rte->subquery = NULL;
1692  rte->functions = NIL; /* we'll fill this list below */
1693  rte->funcordinality = rangefunc->ordinality;
1694  rte->alias = alias;
1695 
1696  /*
1697  * Choose the RTE alias name. We default to using the first function's
1698  * name even when there's more than one; which is maybe arguable but beats
1699  * using something constant like "table".
1700  */
1701  if (alias)
1702  aliasname = alias->aliasname;
1703  else
1704  aliasname = linitial(funcnames);
1705 
1706  eref = makeAlias(aliasname, NIL);
1707  rte->eref = eref;
1708 
1709  /* Process each function ... */
1710  functupdescs = (TupleDesc *) palloc(nfuncs * sizeof(TupleDesc));
1711 
1712  totalatts = 0;
1713  funcno = 0;
1714  forthree(lc1, funcexprs, lc2, funcnames, lc3, coldeflists)
1715  {
1716  Node *funcexpr = (Node *) lfirst(lc1);
1717  char *funcname = (char *) lfirst(lc2);
1718  List *coldeflist = (List *) lfirst(lc3);
1720  TypeFuncClass functypclass;
1721  Oid funcrettype;
1722 
1723  /* Initialize RangeTblFunction node */
1724  rtfunc->funcexpr = funcexpr;
1725  rtfunc->funccolnames = NIL;
1726  rtfunc->funccoltypes = NIL;
1727  rtfunc->funccoltypmods = NIL;
1728  rtfunc->funccolcollations = NIL;
1729  rtfunc->funcparams = NULL; /* not set until planning */
1730 
1731  /*
1732  * Now determine if the function returns a simple or composite type.
1733  */
1734  functypclass = get_expr_result_type(funcexpr,
1735  &funcrettype,
1736  &tupdesc);
1737 
1738  /*
1739  * A coldeflist is required if the function returns RECORD and hasn't
1740  * got a predetermined record type, and is prohibited otherwise.
1741  */
1742  if (coldeflist != NIL)
1743  {
1744  if (functypclass != TYPEFUNC_RECORD)
1745  ereport(ERROR,
1746  (errcode(ERRCODE_SYNTAX_ERROR),
1747  errmsg("a column definition list is only allowed for functions returning \"record\""),
1748  parser_errposition(pstate,
1749  exprLocation((Node *) coldeflist))));
1750  }
1751  else
1752  {
1753  if (functypclass == TYPEFUNC_RECORD)
1754  ereport(ERROR,
1755  (errcode(ERRCODE_SYNTAX_ERROR),
1756  errmsg("a column definition list is required for functions returning \"record\""),
1757  parser_errposition(pstate, exprLocation(funcexpr))));
1758  }
1759 
1760  if (functypclass == TYPEFUNC_COMPOSITE ||
1761  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
1762  {
1763  /* Composite data type, e.g. a table's row type */
1764  Assert(tupdesc);
1765  }
1766  else if (functypclass == TYPEFUNC_SCALAR)
1767  {
1768  /* Base data type, i.e. scalar */
1769  tupdesc = CreateTemplateTupleDesc(1);
1770  TupleDescInitEntry(tupdesc,
1771  (AttrNumber) 1,
1772  chooseScalarFunctionAlias(funcexpr, funcname,
1773  alias, nfuncs),
1774  funcrettype,
1775  exprTypmod(funcexpr),
1776  0);
1778  (AttrNumber) 1,
1779  exprCollation(funcexpr));
1780  }
1781  else if (functypclass == TYPEFUNC_RECORD)
1782  {
1783  ListCell *col;
1784 
1785  /*
1786  * Use the column definition list to construct a tupdesc and fill
1787  * in the RangeTblFunction's lists.
1788  */
1789  tupdesc = CreateTemplateTupleDesc(list_length(coldeflist));
1790  i = 1;
1791  foreach(col, coldeflist)
1792  {
1793  ColumnDef *n = (ColumnDef *) lfirst(col);
1794  char *attrname;
1795  Oid attrtype;
1796  int32 attrtypmod;
1797  Oid attrcollation;
1798 
1799  attrname = n->colname;
1800  if (n->typeName->setof)
1801  ereport(ERROR,
1802  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
1803  errmsg("column \"%s\" cannot be declared SETOF",
1804  attrname),
1805  parser_errposition(pstate, n->location)));
1806  typenameTypeIdAndMod(pstate, n->typeName,
1807  &attrtype, &attrtypmod);
1808  attrcollation = GetColumnDefCollation(pstate, n, attrtype);
1809  TupleDescInitEntry(tupdesc,
1810  (AttrNumber) i,
1811  attrname,
1812  attrtype,
1813  attrtypmod,
1814  0);
1816  (AttrNumber) i,
1817  attrcollation);
1818  rtfunc->funccolnames = lappend(rtfunc->funccolnames,
1819  makeString(pstrdup(attrname)));
1820  rtfunc->funccoltypes = lappend_oid(rtfunc->funccoltypes,
1821  attrtype);
1822  rtfunc->funccoltypmods = lappend_int(rtfunc->funccoltypmods,
1823  attrtypmod);
1825  attrcollation);
1826 
1827  i++;
1828  }
1829 
1830  /*
1831  * Ensure that the coldeflist defines a legal set of names (no
1832  * duplicates, but we needn't worry about system column names) and
1833  * datatypes. Although we mostly can't allow pseudo-types, it
1834  * seems safe to allow RECORD and RECORD[], since values within
1835  * those type classes are self-identifying at runtime, and the
1836  * coldeflist doesn't represent anything that will be visible to
1837  * other sessions.
1838  */
1839  CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE,
1841  }
1842  else
1843  ereport(ERROR,
1844  (errcode(ERRCODE_DATATYPE_MISMATCH),
1845  errmsg("function \"%s\" in FROM has unsupported return type %s",
1846  funcname, format_type_be(funcrettype)),
1847  parser_errposition(pstate, exprLocation(funcexpr))));
1848 
1849  /* Finish off the RangeTblFunction and add it to the RTE's list */
1850  rtfunc->funccolcount = tupdesc->natts;
1851  rte->functions = lappend(rte->functions, rtfunc);
1852 
1853  /* Save the tupdesc for use below */
1854  functupdescs[funcno] = tupdesc;
1855  totalatts += tupdesc->natts;
1856  funcno++;
1857  }
1858 
1859  /*
1860  * If there's more than one function, or we want an ordinality column, we
1861  * have to produce a merged tupdesc.
1862  */
1863  if (nfuncs > 1 || rangefunc->ordinality)
1864  {
1865  if (rangefunc->ordinality)
1866  totalatts++;
1867 
1868  /* Merge the tuple descs of each function into a composite one */
1869  tupdesc = CreateTemplateTupleDesc(totalatts);
1870  natts = 0;
1871  for (i = 0; i < nfuncs; i++)
1872  {
1873  for (j = 1; j <= functupdescs[i]->natts; j++)
1874  TupleDescCopyEntry(tupdesc, ++natts, functupdescs[i], j);
1875  }
1876 
1877  /* Add the ordinality column if needed */
1878  if (rangefunc->ordinality)
1879  {
1880  TupleDescInitEntry(tupdesc,
1881  (AttrNumber) ++natts,
1882  "ordinality",
1883  INT8OID,
1884  -1,
1885  0);
1886  /* no need to set collation */
1887  }
1888 
1889  Assert(natts == totalatts);
1890  }
1891  else
1892  {
1893  /* We can just use the single function's tupdesc as-is */
1894  tupdesc = functupdescs[0];
1895  }
1896 
1897  /* Use the tupdesc while assigning column aliases for the RTE */
1898  buildRelationAliases(tupdesc, alias, eref);
1899 
1900  /*
1901  * Set flags and access permissions.
1902  *
1903  * Functions are never checked for access rights (at least, not by the RTE
1904  * permissions mechanism).
1905  */
1906  rte->lateral = lateral;
1907  rte->inh = false; /* never true for functions */
1908  rte->inFromCl = inFromCl;
1909 
1910  rte->requiredPerms = 0;
1911  rte->checkAsUser = InvalidOid;
1912  rte->selectedCols = NULL;
1913  rte->insertedCols = NULL;
1914  rte->updatedCols = NULL;
1915  rte->extraUpdatedCols = NULL;
1916 
1917  /*
1918  * Add completed RTE to pstate's range table list, so that we know its
1919  * index. But we don't add it to the join list --- caller must do that if
1920  * appropriate.
1921  */
1922  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1923 
1924  /*
1925  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1926  * list --- caller must do that if appropriate.
1927  */
1928  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1929  tupdesc);
1930 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:567
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
Alias * alias
Definition: parsenodes.h:1112
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
static char * chooseScalarFunctionAlias(Node *funcexpr, char *funcname, Alias *alias, int nfuncs)
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:464
char * pstrdup(const char *in)
Definition: mcxt.c:1186
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:608
char * format_type_be(Oid type_oid)
Definition: format_type.c:326
AclMode requiredPerms
Definition: parsenodes.h:1117
bool funcordinality
Definition: parsenodes.h:1062
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:212
List * lappend_oid(List *list, Oid datum)
Definition: list.c:358
signed int int32
Definition: c.h:347
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1122
Bitmapset * selectedCols
Definition: parsenodes.h:1119
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:271
bool setof
Definition: parsenodes.h:211
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:769
int location
Definition: parsenodes.h:663
TypeFuncClass
Definition: funcapi.h:146
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend_int(List *list, int datum)
Definition: list.c:340
List * lappend(List *list, void *datum)
Definition: list.c:322
Oid GetColumnDefCollation(ParseState *pstate, ColumnDef *coldef, Oid typeOid)
Definition: parse_type.c:538
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:310
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1151
Bitmapset * updatedCols
Definition: parsenodes.h:1121
List * funccolcollations
Definition: parsenodes.h:1152
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1061
static int list_length(const List *l)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
TypeName * typeName
Definition: parsenodes.h:647
Bitmapset * funcparams
Definition: parsenodes.h:1154
RTEKind rtekind
Definition: parsenodes.h:974
#define CHKATYPE_ANYRECORD
Definition: heap.h:24
Query * subquery
Definition: parsenodes.h:1009
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
Bitmapset * insertedCols
Definition: parsenodes.h:1120
int i
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
char * colname
Definition: parsenodes.h:646
Alias * eref
Definition: parsenodes.h:1113
void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, int flags)
Definition: heap.c:489
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
List * p_rtable
Definition: parse_node.h:180

◆ addRangeTableEntryForJoin()

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

Definition at line 2100 of file parse_relation.c.

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

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

2110 {
2112  Alias *eref;
2113  int numaliases;
2114  ParseNamespaceItem *nsitem;
2115 
2116  Assert(pstate != NULL);
2117 
2118  /*
2119  * Fail if join has too many columns --- we must be able to reference any
2120  * of the columns with an AttrNumber.
2121  */
2122  if (list_length(aliasvars) > MaxAttrNumber)
2123  ereport(ERROR,
2124  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2125  errmsg("joins can have at most %d columns",
2126  MaxAttrNumber)));
2127 
2128  rte->rtekind = RTE_JOIN;
2129  rte->relid = InvalidOid;
2130  rte->subquery = NULL;
2131  rte->jointype = jointype;
2132  rte->joinmergedcols = nummergedcols;
2133  rte->joinaliasvars = aliasvars;
2134  rte->joinleftcols = leftcols;
2135  rte->joinrightcols = rightcols;
2136  rte->alias = alias;
2137 
2138  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
2139  numaliases = list_length(eref->colnames);
2140 
2141  /* fill in any unspecified alias columns */
2142  if (numaliases < list_length(colnames))
2143  eref->colnames = list_concat(eref->colnames,
2144  list_copy_tail(colnames, numaliases));
2145 
2146  rte->eref = eref;
2147 
2148  /*
2149  * Set flags and access permissions.
2150  *
2151  * Joins are never checked for access rights.
2152  */
2153  rte->lateral = false;
2154  rte->inh = false; /* never true for joins */
2155  rte->inFromCl = inFromCl;
2156 
2157  rte->requiredPerms = 0;
2158  rte->checkAsUser = InvalidOid;
2159  rte->selectedCols = NULL;
2160  rte->insertedCols = NULL;
2161  rte->updatedCols = NULL;
2162  rte->extraUpdatedCols = NULL;
2163 
2164  /*
2165  * Add completed RTE to pstate's range table list, so that we know its
2166  * index. But we don't add it to the join list --- caller must do that if
2167  * appropriate.
2168  */
2169  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2170 
2171  /*
2172  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2173  * list --- caller must do that if appropriate.
2174  */
2175  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2176  nsitem->p_rte = rte;
2177  nsitem->p_rtindex = list_length(pstate->p_rtable);
2178  nsitem->p_nscolumns = nscolumns;
2179  /* set default visibility flags; might get changed later */
2180  nsitem->p_rel_visible = true;
2181  nsitem->p_cols_visible = true;
2182  nsitem->p_lateral_only = false;
2183  nsitem->p_lateral_ok = true;
2184 
2185  return nsitem;
2186 }
#define NIL
Definition: pg_list.h:65
List * joinaliasvars
Definition: parsenodes.h:1049
Alias * alias
Definition: parsenodes.h:1112
List * colnames
Definition: primnodes.h:43
#define MaxAttrNumber
Definition: attnum.h:24
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
int errcode(int sqlerrcode)
Definition: elog.c:608
AclMode requiredPerms
Definition: parsenodes.h:1117
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1423
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
RangeTblEntry * p_rte
Definition: parse_node.h:257
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1122
Bitmapset * selectedCols
Definition: parsenodes.h:1119
#define ERROR
Definition: elog.h:43
JoinType jointype
Definition: parsenodes.h:1047
List * joinrightcols
Definition: parsenodes.h:1051
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:322
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1121
List * joinleftcols
Definition: parsenodes.h:1050
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
static int list_length(const List *l)
Definition: pg_list.h:169
RTEKind rtekind
Definition: parsenodes.h:974
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:260
Query * subquery
Definition: parsenodes.h:1009
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
Bitmapset * insertedCols
Definition: parsenodes.h:1120
Alias * eref
Definition: parsenodes.h:1113
#define copyObject(obj)
Definition: nodes.h:641
List * p_rtable
Definition: parse_node.h:180

◆ addRangeTableEntryForRelation()

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

Definition at line 1497 of file parse_relation.c.

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

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

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

◆ addRangeTableEntryForSubquery()

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

Definition at line 1567 of file parse_relation.c.

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

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

1572 {
1574  char *refname = alias->aliasname;
1575  Alias *eref;
1576  int numaliases;
1577  List *coltypes,
1578  *coltypmods,
1579  *colcollations;
1580  int varattno;
1581  ListCell *tlistitem;
1582 
1583  Assert(pstate != NULL);
1584 
1585  rte->rtekind = RTE_SUBQUERY;
1586  rte->subquery = subquery;
1587  rte->alias = alias;
1588 
1589  eref = copyObject(alias);
1590  numaliases = list_length(eref->colnames);
1591 
1592  /* fill in any unspecified alias columns, and extract column type info */
1593  coltypes = coltypmods = colcollations = NIL;
1594  varattno = 0;
1595  foreach(tlistitem, subquery->targetList)
1596  {
1597  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1598 
1599  if (te->resjunk)
1600  continue;
1601  varattno++;
1602  Assert(varattno == te->resno);
1603  if (varattno > numaliases)
1604  {
1605  char *attrname;
1606 
1607  attrname = pstrdup(te->resname);
1608  eref->colnames = lappend(eref->colnames, makeString(attrname));
1609  }
1610  coltypes = lappend_oid(coltypes,
1611  exprType((Node *) te->expr));
1612  coltypmods = lappend_int(coltypmods,
1613  exprTypmod((Node *) te->expr));
1614  colcollations = lappend_oid(colcollations,
1615  exprCollation((Node *) te->expr));
1616  }
1617  if (varattno < numaliases)
1618  ereport(ERROR,
1619  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1620  errmsg("table \"%s\" has %d columns available but %d columns specified",
1621  refname, varattno, numaliases)));
1622 
1623  rte->eref = eref;
1624 
1625  /*
1626  * Set flags and access permissions.
1627  *
1628  * Subqueries are never checked for access rights.
1629  */
1630  rte->lateral = lateral;
1631  rte->inh = false; /* never true for subqueries */
1632  rte->inFromCl = inFromCl;
1633 
1634  rte->requiredPerms = 0;
1635  rte->checkAsUser = InvalidOid;
1636  rte->selectedCols = NULL;
1637  rte->insertedCols = NULL;
1638  rte->updatedCols = NULL;
1639  rte->extraUpdatedCols = NULL;
1640 
1641  /*
1642  * Add completed RTE to pstate's range table list, so that we know its
1643  * index. But we don't add it to the join list --- caller must do that if
1644  * appropriate.
1645  */
1646  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1647 
1648  /*
1649  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1650  * list --- caller must do that if appropriate.
1651  */
1652  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
1653  coltypes, coltypmods, colcollations);
1654 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1112
List * colnames
Definition: primnodes.h:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
char * pstrdup(const char *in)
Definition: mcxt.c:1186
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:608
AclMode requiredPerms
Definition: parsenodes.h:1117
char * resname
Definition: primnodes.h:1409
List * lappend_oid(List *list, Oid datum)
Definition: list.c:358
List * targetList
Definition: parsenodes.h:140
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1122
Bitmapset * selectedCols
Definition: parsenodes.h:1119
bool resjunk
Definition: primnodes.h:1414
#define ERROR
Definition: elog.h:43
AttrNumber resno
Definition: primnodes.h:1408
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend_int(List *list, int datum)
Definition: list.c:340
List * lappend(List *list, void *datum)
Definition: list.c:322
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1121
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
Expr * expr
Definition: primnodes.h:1407
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
int errmsg(const char *fmt,...)
Definition: elog.c:822
Bitmapset * insertedCols
Definition: parsenodes.h:1120
Alias * eref
Definition: parsenodes.h:1113
#define copyObject(obj)
Definition: nodes.h:641
Definition: pg_list.h:50
List * p_rtable
Definition: parse_node.h:180

◆ addRangeTableEntryForTableFunc()

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

Definition at line 1939 of file parse_relation.c.

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

Referenced by transformRangeTableFunc().

1944 {
1946  char *refname = alias ? alias->aliasname : pstrdup("xmltable");
1947  Alias *eref;
1948  int numaliases;
1949 
1950  Assert(pstate != NULL);
1951 
1952  rte->rtekind = RTE_TABLEFUNC;
1953  rte->relid = InvalidOid;
1954  rte->subquery = NULL;
1955  rte->tablefunc = tf;
1956  rte->coltypes = tf->coltypes;
1957  rte->coltypmods = tf->coltypmods;
1958  rte->colcollations = tf->colcollations;
1959  rte->alias = alias;
1960 
1961  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1962  numaliases = list_length(eref->colnames);
1963 
1964  /* fill in any unspecified alias columns */
1965  if (numaliases < list_length(tf->colnames))
1966  eref->colnames = list_concat(eref->colnames,
1967  list_copy_tail(tf->colnames, numaliases));
1968 
1969  rte->eref = eref;
1970 
1971  /*
1972  * Set flags and access permissions.
1973  *
1974  * Tablefuncs are never checked for access rights (at least, not by the
1975  * RTE permissions mechanism).
1976  */
1977  rte->lateral = lateral;
1978  rte->inh = false; /* never true for tablefunc RTEs */
1979  rte->inFromCl = inFromCl;
1980 
1981  rte->requiredPerms = 0;
1982  rte->checkAsUser = InvalidOid;
1983  rte->selectedCols = NULL;
1984  rte->insertedCols = NULL;
1985  rte->updatedCols = NULL;
1986  rte->extraUpdatedCols = NULL;
1987 
1988  /*
1989  * Add completed RTE to pstate's range table list, so that we know its
1990  * index. But we don't add it to the join list --- caller must do that if
1991  * appropriate.
1992  */
1993  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1994 
1995  /*
1996  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1997  * list --- caller must do that if appropriate.
1998  */
1999  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2000  rte->coltypes, rte->coltypmods,
2001  rte->colcollations);
2002 }
#define NIL
Definition: pg_list.h:65
List * colnames
Definition: primnodes.h:89
Alias * alias
Definition: parsenodes.h:1112
List * colnames
Definition: primnodes.h:43
List * coltypmods
Definition: primnodes.h:91
char * pstrdup(const char *in)
Definition: mcxt.c:1186
List * coltypmods
Definition: parsenodes.h:1100
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
AclMode requiredPerms
Definition: parsenodes.h:1117
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1423
List * colcollations
Definition: parsenodes.h:1101
List * colcollations
Definition: primnodes.h:92
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1122
Bitmapset * selectedCols
Definition: parsenodes.h:1119
TableFunc * tablefunc
Definition: parsenodes.h:1067
List * lappend(List *list, void *datum)
Definition: list.c:322
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
List * coltypes
Definition: primnodes.h:90
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1121
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
Bitmapset * insertedCols
Definition: parsenodes.h:1120
Alias * eref
Definition: parsenodes.h:1113
#define copyObject(obj)
Definition: nodes.h:641
List * coltypes
Definition: parsenodes.h:1099
List * p_rtable
Definition: parse_node.h:180

◆ addRangeTableEntryForValues()

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

Definition at line 2011 of file parse_relation.c.

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

Referenced by transformInsertStmt(), and transformValuesClause().

2019 {
2021  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
2022  Alias *eref;
2023  int numaliases;
2024  int numcolumns;
2025 
2026  Assert(pstate != NULL);
2027 
2028  rte->rtekind = RTE_VALUES;
2029  rte->relid = InvalidOid;
2030  rte->subquery = NULL;
2031  rte->values_lists = exprs;
2032  rte->coltypes = coltypes;
2033  rte->coltypmods = coltypmods;
2034  rte->colcollations = colcollations;
2035  rte->alias = alias;
2036 
2037  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2038 
2039  /* fill in any unspecified alias columns */
2040  numcolumns = list_length((List *) linitial(exprs));
2041  numaliases = list_length(eref->colnames);
2042  while (numaliases < numcolumns)
2043  {
2044  char attrname[64];
2045 
2046  numaliases++;
2047  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
2048  eref->colnames = lappend(eref->colnames,
2049  makeString(pstrdup(attrname)));
2050  }
2051  if (numcolumns < numaliases)
2052  ereport(ERROR,
2053  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2054  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
2055  refname, numcolumns, numaliases)));
2056 
2057  rte->eref = eref;
2058 
2059  /*
2060  * Set flags and access permissions.
2061  *
2062  * Subqueries are never checked for access rights.
2063  */
2064  rte->lateral = lateral;
2065  rte->inh = false; /* never true for values RTEs */
2066  rte->inFromCl = inFromCl;
2067 
2068  rte->requiredPerms = 0;
2069  rte->checkAsUser = InvalidOid;
2070  rte->selectedCols = NULL;
2071  rte->insertedCols = NULL;
2072  rte->updatedCols = NULL;
2073  rte->extraUpdatedCols = NULL;
2074 
2075  /*
2076  * Add completed RTE to pstate's range table list, so that we know its
2077  * index. But we don't add it to the join list --- caller must do that if
2078  * appropriate.
2079  */
2080  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2081 
2082  /*
2083  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2084  * list --- caller must do that if appropriate.
2085  */
2086  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2087  rte->coltypes, rte->coltypmods,
2088  rte->colcollations);
2089 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1112
List * colnames
Definition: primnodes.h:43
char * pstrdup(const char *in)
Definition: mcxt.c:1186
List * coltypmods
Definition: parsenodes.h:1100
int errcode(int sqlerrcode)
Definition: elog.c:608
AclMode requiredPerms
Definition: parsenodes.h:1117
List * colcollations
Definition: parsenodes.h:1101
List * values_lists
Definition: parsenodes.h:1072
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1122
Bitmapset * selectedCols
Definition: parsenodes.h:1119
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:322
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1121
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
int errmsg(const char *fmt,...)
Definition: elog.c:822
Bitmapset * insertedCols
Definition: parsenodes.h:1120
Alias * eref
Definition: parsenodes.h:1113
#define copyObject(obj)
Definition: nodes.h:641
List * coltypes
Definition: parsenodes.h:1099
Definition: pg_list.h:50
#define snprintf
Definition: port.h:192
List * p_rtable
Definition: parse_node.h:180

◆ attnameAttNum()

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

Definition at line 3297 of file parse_relation.c.

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

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

3298 {
3299  int i;
3300 
3301  for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3302  {
3303  Form_pg_attribute att = TupleDescAttr(rd->rd_att, i);
3304 
3305  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3306  return i + 1;
3307  }
3308 
3309  if (sysColOK)
3310  {
3311  if ((i = specialAttNum(attname)) != InvalidAttrNumber)
3312  return i;
3313  }
3314 
3315  /* on failure */
3316  return InvalidAttrNumber;
3317 }
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:434
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int namestrcmp(Name name, const char *str)
Definition: name.c:287
static int specialAttNum(const char *attname)
NameData attname
Definition: pg_attribute.h:40
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
TupleDesc rd_att
Definition: rel.h:85
#define InvalidAttrNumber
Definition: attnum.h:23
int i

◆ attnumAttName()

const NameData* attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3347 of file parse_relation.c.

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

Referenced by transformFkeyGetPrimaryKey().

3348 {
3349  if (attid <= 0)
3350  {
3351  const FormData_pg_attribute *sysatt;
3352 
3353  sysatt = SystemAttributeDefinition(attid);
3354  return &sysatt->attname;
3355  }
3356  if (attid > rd->rd_att->natts)
3357  elog(ERROR, "invalid attribute number %d", attid);
3358  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3359 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:249
#define ERROR
Definition: elog.h:43
TupleDesc rd_att
Definition: rel.h:85
FormData_pg_attribute
Definition: pg_attribute.h:184
#define elog(elevel,...)
Definition: elog.h:228

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3389 of file parse_relation.c.

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

3390 {
3391  if (attid <= 0)
3392  {
3393  /* All system attributes are of noncollatable types. */
3394  return InvalidOid;
3395  }
3396  if (attid > rd->rd_att->natts)
3397  elog(ERROR, "invalid attribute number %d", attid);
3398  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3399 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define ERROR
Definition: elog.h:43
TupleDesc rd_att
Definition: rel.h:85
#define InvalidOid
Definition: postgres_ext.h:36
#define elog(elevel,...)
Definition: elog.h:228

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3369 of file parse_relation.c.

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

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

3370 {
3371  if (attid <= 0)
3372  {
3373  const FormData_pg_attribute *sysatt;
3374 
3375  sysatt = SystemAttributeDefinition(attid);
3376  return sysatt->atttypid;
3377  }
3378  if (attid > rd->rd_att->natts)
3379  elog(ERROR, "invalid attribute number %d", attid);
3380  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3381 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:249
#define ERROR
Definition: elog.h:43
TupleDesc rd_att
Definition: rel.h:85
FormData_pg_attribute
Definition: pg_attribute.h:184
#define elog(elevel,...)
Definition: elog.h:228

◆ checkNameSpaceConflicts()

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

Definition at line 414 of file parse_relation.c.

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

Referenced by transformFromClause(), and transformFromClauseItem().

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

◆ colNameToVar()

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

Definition at line 849 of file parse_relation.c.

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

Referenced by findTargetlistEntrySQL92(), and transformColumnRef().

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

◆ errorMissingColumn()

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

Definition at line 3469 of file parse_relation.c.

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

Referenced by transformColumnRef().

3471 {
3473  char *closestfirst = NULL;
3474 
3475  /*
3476  * Search the entire rtable looking for possible matches. If we find one,
3477  * emit a hint about it.
3478  *
3479  * TODO: improve this code (and also errorMissingRTE) to mention using
3480  * LATERAL if appropriate.
3481  */
3482  state = searchRangeTableForCol(pstate, relname, colname, location);
3483 
3484  /*
3485  * Extract closest col string for best match, if any.
3486  *
3487  * Infer an exact match referenced despite not being visible from the fact
3488  * that an attribute number was not present in state passed back -- this
3489  * is what is reported when !closestfirst. There might also be an exact
3490  * match that was qualified with an incorrect alias, in which case
3491  * closestfirst will be set (so hint is the same as generic fuzzy case).
3492  */
3493  if (state->rfirst && AttributeNumberIsValid(state->first))
3494  closestfirst = strVal(list_nth(state->rfirst->eref->colnames,
3495  state->first - 1));
3496 
3497  if (!state->rsecond)
3498  {
3499  /*
3500  * Handle case where there is zero or one column suggestions to hint,
3501  * including exact matches referenced but not visible.
3502  */
3503  ereport(ERROR,
3504  (errcode(ERRCODE_UNDEFINED_COLUMN),
3505  relname ?
3506  errmsg("column %s.%s does not exist", relname, colname) :
3507  errmsg("column \"%s\" does not exist", colname),
3508  state->rfirst ? closestfirst ?
3509  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3510  state->rfirst->eref->aliasname, closestfirst) :
3511  errhint("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3512  colname, state->rfirst->eref->aliasname) : 0,
3513  parser_errposition(pstate, location)));
3514  }
3515  else
3516  {
3517  /* Handle case where there are two equally useful column hints */
3518  char *closestsecond;
3519 
3520  closestsecond = strVal(list_nth(state->rsecond->eref->colnames,
3521  state->second - 1));
3522 
3523  ereport(ERROR,
3524  (errcode(ERRCODE_UNDEFINED_COLUMN),
3525  relname ?
3526  errmsg("column %s.%s does not exist", relname, colname) :
3527  errmsg("column \"%s\" does not exist", colname),
3528  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3529  state->rfirst->eref->aliasname, closestfirst,
3530  state->rsecond->eref->aliasname, closestsecond),
3531  parser_errposition(pstate, location)));
3532  }
3533 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
List * colnames
Definition: primnodes.h:43
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colname, int location)
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:608
NameData relname
Definition: pg_class.h:35
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
RangeTblEntry * rfirst
#define ereport(elevel, rest)
Definition: elog.h:141
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
char * aliasname
Definition: primnodes.h:42
Definition: regguts.h:298
RangeTblEntry * rsecond
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:822
Alias * eref
Definition: parsenodes.h:1113

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3408 of file parse_relation.c.

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

Referenced by ExpandColumnRefStar(), and transformColumnRef().

3409 {
3410  RangeTblEntry *rte;
3411  const char *badAlias = NULL;
3412 
3413  /*
3414  * Check to see if there are any potential matches in the query's
3415  * rangetable. (Note: cases involving a bad schema name in the RangeVar
3416  * will throw error immediately here. That seems OK.)
3417  */
3418  rte = searchRangeTableForRel(pstate, relation);
3419 
3420  /*
3421  * If we found a match that has an alias and the alias is visible in the
3422  * namespace, then the problem is probably use of the relation's real name
3423  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3424  * common enough to justify a specific hint.
3425  *
3426  * If we found a match that doesn't meet those criteria, assume the
3427  * problem is illegal use of a relation outside its scope, as in the
3428  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3429  */
3430  if (rte && rte->alias &&
3431  strcmp(rte->eref->aliasname, relation->relname) != 0)
3432  {
3433  ParseNamespaceItem *nsitem;
3434  int sublevels_up;
3435 
3436  nsitem = refnameNamespaceItem(pstate, NULL, rte->eref->aliasname,
3437  relation->location,
3438  &sublevels_up);
3439  if (nsitem && nsitem->p_rte == rte)
3440  badAlias = rte->eref->aliasname;
3441  }
3442 
3443  if (rte)
3444  ereport(ERROR,
3446  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3447  relation->relname),
3448  (badAlias ?
3449  errhint("Perhaps you meant to reference the table alias \"%s\".",
3450  badAlias) :
3451  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3452  rte->eref->aliasname)),
3453  parser_errposition(pstate, relation->location)));
3454  else
3455  ereport(ERROR,
3457  errmsg("missing FROM-clause entry for table \"%s\"",
3458  relation->relname),
3459  parser_errposition(pstate, relation->location)));
3460 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:73
Alias * alias
Definition: parsenodes.h:1112
int errcode(int sqlerrcode)
Definition: elog.c:608
int location
Definition: primnodes.h:73
char * relname
Definition: primnodes.h:68
RangeTblEntry * p_rte
Definition: parse_node.h:257
static RangeTblEntry * searchRangeTableForRel(ParseState *pstate, RangeVar *relation)
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
char * aliasname
Definition: primnodes.h:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:822
Alias * eref
Definition: parsenodes.h:1113

◆ expandNSItemAttrs()

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

Definition at line 3019 of file parse_relation.c.

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

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

3021 {
3022  RangeTblEntry *rte = nsitem->p_rte;
3023  List *names,
3024  *vars;
3025  ListCell *name,
3026  *var;
3027  List *te_list = NIL;
3028 
3029  vars = expandNSItemVars(nsitem, sublevels_up, location, &names);
3030 
3031  /*
3032  * Require read access to the table. This is normally redundant with the
3033  * markVarForSelectPriv calls below, but not if the table has zero
3034  * columns.
3035  */
3036  rte->requiredPerms |= ACL_SELECT;
3037 
3038  forboth(name, names, var, vars)
3039  {
3040  char *label = strVal(lfirst(name));
3041  Var *varnode = (Var *) lfirst(var);
3042  TargetEntry *te;
3043 
3044  te = makeTargetEntry((Expr *) varnode,
3045  (AttrNumber) pstate->p_next_resno++,
3046  label,
3047  false);
3048  te_list = lappend(te_list, te);
3049 
3050  /* Require read access to each column */
3051  markVarForSelectPriv(pstate, varnode, rte);
3052  }
3053 
3054  Assert(name == NULL && var == NULL); /* lists not the same length? */
3055 
3056  return te_list;
3057 }
#define NIL
Definition: pg_list.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
#define strVal(v)
Definition: value.h:54
AclMode requiredPerms
Definition: parsenodes.h:1117
Definition: primnodes.h:181
RangeTblEntry * p_rte
Definition: parse_node.h:257
int p_next_resno
Definition: parse_node.h:195
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:236
List * lappend(List *list, void *datum)
Definition: list.c:322
#define ACL_SELECT
Definition: parsenodes.h:75
static char * label
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
List * expandNSItemVars(ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
const char * name
Definition: encode.c:521
Definition: regcomp.c:224
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21

◆ expandNSItemVars()

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

Definition at line 2964 of file parse_relation.c.

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

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

2967 {
2968  List *result = NIL;
2969  int colindex;
2970  ListCell *lc;
2971 
2972  if (colnames)
2973  *colnames = NIL;
2974  colindex = 0;
2975  foreach(lc, nsitem->p_rte->eref->colnames)
2976  {
2977  Value *colnameval = (Value *) lfirst(lc);
2978  const char *colname = strVal(colnameval);
2979  ParseNamespaceColumn *nscol = nsitem->p_nscolumns + colindex;
2980 
2981  if (colname[0])
2982  {
2983  Var *var;
2984 
2985  Assert(nscol->p_varno > 0);
2986  var = makeVar(nscol->p_varno,
2987  nscol->p_varattno,
2988  nscol->p_vartype,
2989  nscol->p_vartypmod,
2990  nscol->p_varcollid,
2991  sublevels_up);
2992  /* makeVar doesn't offer parameters for these, so set by hand: */
2993  var->varnosyn = nscol->p_varnosyn;
2994  var->varattnosyn = nscol->p_varattnosyn;
2995  var->location = location;
2996  result = lappend(result, var);
2997  if (colnames)
2998  *colnames = lappend(*colnames, colnameval);
2999  }
3000  else
3001  {
3002  /* dropped column, ignore */
3003  Assert(nscol->p_varno == 0);
3004  }
3005  colindex++;
3006  }
3007  return result;
3008 }
#define NIL
Definition: pg_list.h:65
List * colnames
Definition: primnodes.h:43
#define strVal(v)
Definition: value.h:54
Definition: primnodes.h:181
RangeTblEntry * p_rte
Definition: parse_node.h:257
int location
Definition: primnodes.h:196
AttrNumber p_varattno
Definition: parse_node.h:291
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
Index varnosyn
Definition: primnodes.h:194
List * lappend(List *list, void *datum)
Definition: list.c:322
AttrNumber varattnosyn
Definition: primnodes.h:195
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
Definition: value.h:42
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:260
AttrNumber p_varattnosyn
Definition: parse_node.h:296
Alias * eref
Definition: parsenodes.h:1113
Definition: pg_list.h:50

◆ expandRTE()

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

Definition at line 2506 of file parse_relation.c.

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

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

2509 {
2510  int varattno;
2511 
2512  if (colnames)
2513  *colnames = NIL;
2514  if (colvars)
2515  *colvars = NIL;
2516 
2517  switch (rte->rtekind)
2518  {
2519  case RTE_RELATION:
2520  /* Ordinary relation RTE */
2521  expandRelation(rte->relid, rte->eref,
2522  rtindex, sublevels_up, location,
2523  include_dropped, colnames, colvars);
2524  break;
2525  case RTE_SUBQUERY:
2526  {
2527  /* Subquery RTE */
2528  ListCell *aliasp_item = list_head(rte->eref->colnames);
2529  ListCell *tlistitem;
2530 
2531  varattno = 0;
2532  foreach(tlistitem, rte->subquery->targetList)
2533  {
2534  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
2535 
2536  if (te->resjunk)
2537  continue;
2538  varattno++;
2539  Assert(varattno == te->resno);
2540 
2541  /*
2542  * In scenarios where columns have been added to a view
2543  * since the outer query was originally parsed, there can
2544  * be more items in the subquery tlist than the outer
2545  * query expects. We should ignore such extra column(s)
2546  * --- compare the behavior for composite-returning
2547  * functions, in the RTE_FUNCTION case below.
2548  */
2549  if (!aliasp_item)
2550  break;
2551 
2552  if (colnames)
2553  {
2554  char *label = strVal(lfirst(aliasp_item));
2555 
2556  *colnames = lappend(*colnames, makeString(pstrdup(label)));
2557  }
2558 
2559  if (colvars)
2560  {
2561  Var *varnode;
2562 
2563  varnode = makeVar(rtindex, varattno,
2564  exprType((Node *) te->expr),
2565  exprTypmod((Node *) te->expr),
2566  exprCollation((Node *) te->expr),
2567  sublevels_up);
2568  varnode->location = location;
2569 
2570  *colvars = lappend(*colvars, varnode);
2571  }
2572 
2573  aliasp_item = lnext(rte->eref->colnames, aliasp_item);
2574  }
2575  }
2576  break;
2577  case RTE_FUNCTION:
2578  {
2579  /* Function RTE */
2580  int atts_done = 0;
2581  ListCell *lc;
2582 
2583  foreach(lc, rte->functions)
2584  {
2585  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2586  TypeFuncClass functypclass;
2587  Oid funcrettype;
2588  TupleDesc tupdesc;
2589 
2590  functypclass = get_expr_result_type(rtfunc->funcexpr,
2591  &funcrettype,
2592  &tupdesc);
2593  if (functypclass == TYPEFUNC_COMPOSITE ||
2594  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
2595  {
2596  /* Composite data type, e.g. a table's row type */
2597  Assert(tupdesc);
2598  expandTupleDesc(tupdesc, rte->eref,
2599  rtfunc->funccolcount, atts_done,
2600  rtindex, sublevels_up, location,
2601  include_dropped, colnames, colvars);
2602  }
2603  else if (functypclass == TYPEFUNC_SCALAR)
2604  {
2605  /* Base data type, i.e. scalar */
2606  if (colnames)
2607  *colnames = lappend(*colnames,
2608  list_nth(rte->eref->colnames,
2609  atts_done));
2610 
2611  if (colvars)
2612  {
2613  Var *varnode;
2614 
2615  varnode = makeVar(rtindex, atts_done + 1,
2616  funcrettype,
2617  exprTypmod(rtfunc->funcexpr),
2618  exprCollation(rtfunc->funcexpr),
2619  sublevels_up);
2620  varnode->location = location;
2621 
2622  *colvars = lappend(*colvars, varnode);
2623  }
2624  }
2625  else if (functypclass == TYPEFUNC_RECORD)
2626  {
2627  if (colnames)
2628  {
2629  List *namelist;
2630 
2631  /* extract appropriate subset of column list */
2632  namelist = list_copy_tail(rte->eref->colnames,
2633  atts_done);
2634  namelist = list_truncate(namelist,
2635  rtfunc->funccolcount);
2636  *colnames = list_concat(*colnames, namelist);
2637  }
2638 
2639  if (colvars)
2640  {
2641  ListCell *l1;
2642  ListCell *l2;
2643  ListCell *l3;
2644  int attnum = atts_done;
2645 
2646  forthree(l1, rtfunc->funccoltypes,
2647  l2, rtfunc->funccoltypmods,
2648  l3, rtfunc->funccolcollations)
2649  {
2650  Oid attrtype = lfirst_oid(l1);
2651  int32 attrtypmod = lfirst_int(l2);
2652  Oid attrcollation = lfirst_oid(l3);
2653  Var *varnode;
2654 
2655  attnum++;
2656  varnode = makeVar(rtindex,
2657  attnum,
2658  attrtype,
2659  attrtypmod,
2660  attrcollation,
2661  sublevels_up);
2662  varnode->location = location;
2663  *colvars = lappend(*colvars, varnode);
2664  }
2665  }
2666  }
2667  else
2668  {
2669  /* addRangeTableEntryForFunction should've caught this */
2670  elog(ERROR, "function in FROM has unsupported return type");
2671  }
2672  atts_done += rtfunc->funccolcount;
2673  }
2674 
2675  /* Append the ordinality column if any */
2676  if (rte->funcordinality)
2677  {
2678  if (colnames)
2679  *colnames = lappend(*colnames,
2680  llast(rte->eref->colnames));
2681 
2682  if (colvars)
2683  {
2684  Var *varnode = makeVar(rtindex,
2685  atts_done + 1,
2686  INT8OID,
2687  -1,
2688  InvalidOid,
2689  sublevels_up);
2690 
2691  *colvars = lappend(*colvars, varnode);
2692  }
2693  }
2694  }
2695  break;
2696  case RTE_JOIN:
2697  {
2698  /* Join RTE */
2699  ListCell *colname;
2700  ListCell *aliasvar;
2701 
2703 
2704  varattno = 0;
2705  forboth(colname, rte->eref->colnames, aliasvar, rte->joinaliasvars)
2706  {
2707  Node *avar = (Node *) lfirst(aliasvar);
2708 
2709  varattno++;
2710 
2711  /*
2712  * During ordinary parsing, there will never be any
2713  * deleted columns in the join. While this function is
2714  * also used by the rewriter and planner, they do not
2715  * currently call it on any JOIN RTEs. Therefore, this
2716  * next bit is dead code, but it seems prudent to handle
2717  * the case correctly anyway.
2718  */
2719  if (avar == NULL)
2720  {
2721  if (include_dropped)
2722  {
2723  if (colnames)
2724  *colnames = lappend(*colnames,
2725  makeString(pstrdup("")));
2726  if (colvars)
2727  {
2728  /*
2729  * Can't use join's column type here (it might
2730  * be dropped!); but it doesn't really matter
2731  * what type the Const claims to be.
2732  */
2733  *colvars = lappend(*colvars,
2734  makeNullConst(INT4OID, -1,
2735  InvalidOid));
2736  }
2737  }
2738  continue;
2739  }
2740 
2741  if (colnames)
2742  {
2743  char *label = strVal(lfirst(colname));
2744 
2745  *colnames = lappend(*colnames,
2746  makeString(pstrdup(label)));
2747  }
2748 
2749  if (colvars)
2750  {
2751  Var *varnode;
2752 
2753  /*
2754  * If the joinaliasvars entry is a simple Var, just
2755  * copy it (with adjustment of varlevelsup and
2756  * location); otherwise it is a JOIN USING column and
2757  * we must generate a join alias Var. This matches
2758  * the results that expansion of "join.*" by
2759  * expandNSItemVars would have produced, if we had
2760  * access to the ParseNamespaceItem for the join.
2761  */
2762  if (IsA(avar, Var))
2763  {
2764  varnode = copyObject((Var *) avar);
2765  varnode->varlevelsup = sublevels_up;
2766  }
2767  else
2768  varnode = makeVar(rtindex, varattno,
2769  exprType(avar),
2770  exprTypmod(avar),
2771  exprCollation(avar),
2772  sublevels_up);
2773  varnode->location = location;
2774 
2775  *colvars = lappend(*colvars, varnode);
2776  }
2777  }
2778  }
2779  break;
2780  case RTE_TABLEFUNC:
2781  case RTE_VALUES:
2782  case RTE_CTE:
2783  case RTE_NAMEDTUPLESTORE:
2784  {
2785  /* Tablefunc, Values, CTE, or ENR RTE */
2786  ListCell *aliasp_item = list_head(rte->eref->colnames);
2787  ListCell *lct;
2788  ListCell *lcm;
2789  ListCell *lcc;
2790 
2791  varattno = 0;
2792  forthree(lct, rte->coltypes,
2793  lcm, rte->coltypmods,
2794  lcc, rte->colcollations)
2795  {
2796  Oid coltype = lfirst_oid(lct);
2797  int32 coltypmod = lfirst_int(lcm);
2798  Oid colcoll = lfirst_oid(lcc);
2799 
2800  varattno++;
2801 
2802  if (colnames)
2803  {
2804  /* Assume there is one alias per output column */
2805  if (OidIsValid(coltype))
2806  {
2807  char *label = strVal(lfirst(aliasp_item));
2808 
2809  *colnames = lappend(*colnames,
2810  makeString(pstrdup(label)));
2811  }
2812  else if (include_dropped)
2813  *colnames = lappend(*colnames,
2814  makeString(pstrdup("")));
2815 
2816  aliasp_item = lnext(rte->eref->colnames, aliasp_item);
2817  }
2818 
2819  if (colvars)
2820  {
2821  if (OidIsValid(coltype))
2822  {
2823  Var *varnode;
2824 
2825  varnode = makeVar(rtindex, varattno,
2826  coltype, coltypmod, colcoll,
2827  sublevels_up);
2828  varnode->location = location;
2829 
2830  *colvars = lappend(*colvars, varnode);
2831  }
2832  else if (include_dropped)
2833  {
2834  /*
2835  * It doesn't really matter what type the Const
2836  * claims to be.
2837  */
2838  *colvars = lappend(*colvars,
2839  makeNullConst(INT4OID, -1,
2840  InvalidOid));
2841  }
2842  }
2843  }
2844  }
2845  break;
2846  case RTE_RESULT:
2847  /* These expose no columns, so nothing to do */
2848  break;
2849  default:
2850  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2851  }
2852 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
List * joinaliasvars
Definition: parsenodes.h:1049
Index varlevelsup
Definition: primnodes.h:191
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
List * colnames
Definition: primnodes.h:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:464
char * pstrdup(const char *in)
Definition: mcxt.c:1186
List * coltypmods
Definition: parsenodes.h:1100
#define llast(l)
Definition: pg_list.h:215
List * list_truncate(List *list, int new_size)
Definition: list.c:586
Definition: nodes.h:525
#define strVal(v)
Definition: value.h:54
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1423
bool funcordinality
Definition: parsenodes.h:1062
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:181
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:212
#define OidIsValid(objectId)
Definition: c.h:645
List * colcollations
Definition: parsenodes.h:1101
signed int int32
Definition: c.h:347
List * targetList
Definition: parsenodes.h:140
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:335
bool resjunk
Definition: primnodes.h:1414
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:191
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
int location
Definition: primnodes.h:196
AttrNumber resno
Definition: primnodes.h:1408
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
TypeFuncClass
Definition: funcapi.h:146
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
static void expandRelation(Oid relid, Alias *eref, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
List * lappend(List *list, void *datum)
Definition: list.c:322
static char * label
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1151
int16 attnum
Definition: pg_attribute.h:79
List * funccolcollations
Definition: parsenodes.h:1152
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
List * functions
Definition: parsenodes.h:1061
Expr * expr
Definition: primnodes.h:1407
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
static void expandTupleDesc(TupleDesc tupdesc, Alias *eref, int count, int offset, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
#define elog(elevel,...)
Definition: elog.h:228
Alias * eref
Definition: parsenodes.h:1113
#define copyObject(obj)
Definition: nodes.h:641
List * coltypes
Definition: parsenodes.h:1099
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ GetCTEForRTE()

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

Definition at line 536 of file parse_relation.c.

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

Referenced by expandRecordVariable(), and markTargetListOrigin().

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

◆ GetNSItemByRangeTablePosn()

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

Definition at line 489 of file parse_relation.c.

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

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

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

◆ GetRTEByRangeTablePosn()

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

Definition at line 516 of file parse_relation.c.

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

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

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

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

Definition at line 2422 of file parse_relation.c.

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

Referenced by addRangeTableEntry(), and transformRangeSubselect().

2423 {
2424  ListCell *l;
2425 
2426  /*
2427  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2428  * parent level, then act as though there's a generic FOR UPDATE here.
2429  */
2430  if (pstate->p_locked_from_parent)
2431  return true;
2432 
2433  foreach(l, pstate->p_locking_clause)
2434  {
2435  LockingClause *lc = (LockingClause *) lfirst(l);
2436 
2437  if (lc->lockedRels == NIL)
2438  {
2439  /* all tables used in query */
2440  return true;
2441  }
2442  else
2443  {
2444  /* just the named tables */
2445  ListCell *l2;
2446 
2447  foreach(l2, lc->lockedRels)
2448  {
2449  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2450 
2451  if (strcmp(refname, thisrel->relname) == 0)
2452  return true;
2453  }
2454  }
2455  }
2456  return false;
2457 }
List * lockedRels
Definition: parsenodes.h:748
#define NIL
Definition: pg_list.h:65
bool p_locked_from_parent
Definition: parse_node.h:198
char * relname
Definition: primnodes.h:68
List * p_locking_clause
Definition: parse_node.h:197
#define lfirst(lc)
Definition: pg_list.h:190

◆ isQueryUsingTempRelation()

bool isQueryUsingTempRelation ( Query query)

Definition at line 3541 of file parse_relation.c.

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

3542 {
3543  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3544 }
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
Definition: nodes.h:525

◆ markVarForSelectPriv()

void markVarForSelectPriv ( ParseState pstate,
Var var,
RangeTblEntry rte 
)

Definition at line 1084 of file parse_relation.c.

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

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

1085 {
1086  Index lv;
1087 
1088  Assert(IsA(var, Var));
1089  /* Find the appropriate pstate if it's an uplevel Var */
1090  for (lv = 0; lv < var->varlevelsup; lv++)
1091  pstate = pstate->parentParseState;
1092  markRTEForSelectPriv(pstate, rte, var->varno, var->varattno);
1093 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Index varlevelsup
Definition: primnodes.h:191
ParseState * parentParseState
Definition: parse_node.h:178
AttrNumber varattno
Definition: primnodes.h:186
Definition: primnodes.h:181
static void markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col)
Index varno
Definition: primnodes.h:184
unsigned int Index
Definition: c.h:476
#define Assert(condition)
Definition: c.h:739

◆ parserOpenTable()

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

Definition at line 1345 of file parse_relation.c.

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

Referenced by addRangeTableEntry(), and setTargetTable().

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

◆ refnameNamespaceItem()

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

Definition at line 108 of file parse_relation.c.

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

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

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

◆ scanNameSpaceForCTE()

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

Definition at line 262 of file parse_relation.c.

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

Referenced by getNSItemForSpecialRelationTypes(), and searchRangeTableForRel().

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

◆ scanNameSpaceForENR()

bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 315 of file parse_relation.c.

References name_matches_visible_ENR().

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

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

◆ scanNSItemForColumn()

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

Definition at line 667 of file parse_relation.c.

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

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

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