PostgreSQL Source Code  git master
parse_relation.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "access/htup_details.h"
#include "access/relation.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "catalog/heap.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_enr.h"
#include "parser/parse_relation.h"
#include "parser/parse_type.h"
#include "parser/parsetree.h"
#include "storage/lmgr.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/varlena.h"
Include dependency graph for parse_relation.c:

Go to the source code of this file.

Data Structures

struct  FuzzyAttrMatchState
 

Macros

#define MAX_FUZZY_DISTANCE   3
 

Functions

static ParseNamespaceItemscanNameSpaceForRefname (ParseState *pstate, const char *refname, int location)
 
static ParseNamespaceItemscanNameSpaceForRelid (ParseState *pstate, Oid relid, int location)
 
static void check_lateral_ref_ok (ParseState *pstate, ParseNamespaceItem *nsitem, int location)
 
static int scanRTEForColumn (ParseState *pstate, RangeTblEntry *rte, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
 
static void markRTEForSelectPriv (ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col)
 
static void expandRelation (Oid relid, Alias *eref, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
 
static void expandTupleDesc (TupleDesc tupdesc, Alias *eref, int count, int offset, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
 
static int specialAttNum (const char *attname)
 
static bool isQueryUsingTempRelation_walker (Node *node, void *context)
 
ParseNamespaceItemrefnameNamespaceItem (ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
 
CommonTableExprscanNameSpaceForCTE (ParseState *pstate, const char *refname, Index *ctelevelsup)
 
static bool isFutureCTE (ParseState *pstate, const char *refname)
 
bool scanNameSpaceForENR (ParseState *pstate, const char *refname)
 
static RangeTblEntrysearchRangeTableForRel (ParseState *pstate, RangeVar *relation)
 
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)
 
static void updateFuzzyAttrMatchState (int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate, RangeTblEntry *rte, const char *actual, const char *match, int attnum)
 
NodescanNSItemForColumn (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
 
NodecolNameToVar (ParseState *pstate, const char *colname, bool localonly, int location)
 
static FuzzyAttrMatchStatesearchRangeTableForCol (ParseState *pstate, const char *alias, const char *colname, int location)
 
void markVarForSelectPriv (ParseState *pstate, Var *var, RangeTblEntry *rte)
 
static void buildRelationAliases (TupleDesc tupdesc, Alias *alias, Alias *eref)
 
static char * chooseScalarFunctionAlias (Node *funcexpr, char *funcname, Alias *alias, int nfuncs)
 
static ParseNamespaceItembuildNSItemFromTupleDesc (RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
 
static ParseNamespaceItembuildNSItemFromLists (RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
 
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)
 
ParseNamespaceItemaddRangeTableEntryForTableFunc (ParseState *pstate, TableFunc *tf, Alias *alias, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForValues (ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, 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 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)
 
char * get_rte_attribute_name (RangeTblEntry *rte, AttrNumber attnum)
 
bool get_rte_attribute_is_dropped (RangeTblEntry *rte, AttrNumber attnum)
 
TargetEntryget_tle_by_resno (List *tlist, AttrNumber resno)
 
RowMarkClauseget_parse_rowmark (Query *qry, Index rtindex)
 
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)
 
void errorMissingRTE (ParseState *pstate, RangeVar *relation)
 
void errorMissingColumn (ParseState *pstate, const char *relname, const char *colname, int location)
 
bool isQueryUsingTempRelation (Query *query)
 

Macro Definition Documentation

◆ MAX_FUZZY_DISTANCE

#define MAX_FUZZY_DISTANCE   3

Definition at line 57 of file parse_relation.c.

Referenced by searchRangeTableForCol(), and updateFuzzyAttrMatchState().

Function Documentation

◆ addNSItemToQuery()

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

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

2499 {
2500  if (addToJoinList)
2501  {
2503 
2504  rtr->rtindex = nsitem->p_rtindex;
2505  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2506  }
2507  if (addToRelNameSpace || addToVarNameSpace)
2508  {
2509  /* Set the new nsitem's visibility flags correctly */
2510  nsitem->p_rel_visible = addToRelNameSpace;
2511  nsitem->p_cols_visible = addToVarNameSpace;
2512  nsitem->p_lateral_only = false;
2513  nsitem->p_lateral_ok = true;
2514  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2515  }
2516 }
List * p_namespace
Definition: parse_node.h:184
List * lappend(List *list, void *datum)
Definition: list.c:321
#define makeNode(_type_)
Definition: nodes.h:576
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:167
Alias * alias
Definition: parsenodes.h:1115
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:1120
Form_pg_class rd_rel
Definition: rel.h:109
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
char * relname
Definition: primnodes.h:68
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1125
Bitmapset * selectedCols
Definition: parsenodes.h:1122
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
#define NoLock
Definition: lockdefs.h:34
List * lappend(List *list, void *datum)
Definition: list.c:321
#define RowShareLock
Definition: lockdefs.h:37
#define ACL_SELECT
Definition: parsenodes.h:75
TupleDesc rd_att
Definition: rel.h:110
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1124
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:977
Bitmapset * insertedCols
Definition: parsenodes.h:1123
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
bool isLockedRefname(ParseState *pstate, const char *refname)
Alias * eref
Definition: parsenodes.h:1116
#define RelationGetRelid(relation)
Definition: rel.h:456
List * p_rtable
Definition: parse_node.h:180

◆ addRangeTableEntryForCTE()

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

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

2230 {
2232  Alias *alias = rv->alias;
2233  char *refname = alias ? alias->aliasname : cte->ctename;
2234  Alias *eref;
2235  int numaliases;
2236  int varattno;
2237  ListCell *lc;
2238 
2239  Assert(pstate != NULL);
2240 
2241  rte->rtekind = RTE_CTE;
2242  rte->ctename = cte->ctename;
2243  rte->ctelevelsup = levelsup;
2244 
2245  /* Self-reference if and only if CTE's parse analysis isn't completed */
2246  rte->self_reference = !IsA(cte->ctequery, Query);
2247  Assert(cte->cterecursive || !rte->self_reference);
2248  /* Bump the CTE's refcount if this isn't a self-reference */
2249  if (!rte->self_reference)
2250  cte->cterefcount++;
2251 
2252  /*
2253  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
2254  * This won't get checked in case of a self-reference, but that's OK
2255  * because data-modifying CTEs aren't allowed to be recursive anyhow.
2256  */
2257  if (IsA(cte->ctequery, Query))
2258  {
2259  Query *ctequery = (Query *) cte->ctequery;
2260 
2261  if (ctequery->commandType != CMD_SELECT &&
2262  ctequery->returningList == NIL)
2263  ereport(ERROR,
2264  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2265  errmsg("WITH query \"%s\" does not have a RETURNING clause",
2266  cte->ctename),
2267  parser_errposition(pstate, rv->location)));
2268  }
2269 
2270  rte->coltypes = cte->ctecoltypes;
2271  rte->coltypmods = cte->ctecoltypmods;
2272  rte->colcollations = cte->ctecolcollations;
2273 
2274  rte->alias = alias;
2275  if (alias)
2276  eref = copyObject(alias);
2277  else
2278  eref = makeAlias(refname, NIL);
2279  numaliases = list_length(eref->colnames);
2280 
2281  /* fill in any unspecified alias columns */
2282  varattno = 0;
2283  foreach(lc, cte->ctecolnames)
2284  {
2285  varattno++;
2286  if (varattno > numaliases)
2287  eref->colnames = lappend(eref->colnames, lfirst(lc));
2288  }
2289  if (varattno < numaliases)
2290  ereport(ERROR,
2291  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2292  errmsg("table \"%s\" has %d columns available but %d columns specified",
2293  refname, varattno, numaliases)));
2294 
2295  rte->eref = eref;
2296 
2297  /*
2298  * Set flags and access permissions.
2299  *
2300  * Subqueries are never checked for access rights.
2301  */
2302  rte->lateral = false;
2303  rte->inh = false; /* never true for subqueries */
2304  rte->inFromCl = inFromCl;
2305 
2306  rte->requiredPerms = 0;
2307  rte->checkAsUser = InvalidOid;
2308  rte->selectedCols = NULL;
2309  rte->insertedCols = NULL;
2310  rte->updatedCols = NULL;
2311  rte->extraUpdatedCols = NULL;
2312 
2313  /*
2314  * Add completed RTE to pstate's range table list, so that we know its
2315  * index. But we don't add it to the join list --- caller must do that if
2316  * appropriate.
2317  */
2318  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2319 
2320  /*
2321  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2322  * list --- caller must do that if appropriate.
2323  */
2324  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2325  rte->coltypes, rte->coltypmods,
2326  rte->colcollations);
2327 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
Alias * alias
Definition: parsenodes.h:1115
List * colnames
Definition: primnodes.h:43
List * coltypmods
Definition: parsenodes.h:1103
int errcode(int sqlerrcode)
Definition: elog.c:610
AclMode requiredPerms
Definition: parsenodes.h:1120
List * colcollations
Definition: parsenodes.h:1104
int location
Definition: primnodes.h:73
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1125
Bitmapset * selectedCols
Definition: parsenodes.h:1122
#define ERROR
Definition: elog.h:43
List * ctecoltypmods
Definition: parsenodes.h:1463
List * returningList
Definition: parsenodes.h:146
List * lappend(List *list, void *datum)
Definition: list.c:321
List * ctecolnames
Definition: parsenodes.h:1461
bool self_reference
Definition: parsenodes.h:1082
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
Bitmapset * updatedCols
Definition: parsenodes.h:1124
CmdType commandType
Definition: parsenodes.h:112
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Index ctelevelsup
Definition: parsenodes.h:1081
List * ctecoltypes
Definition: parsenodes.h:1462
RTEKind rtekind
Definition: parsenodes.h:977
char * ctename
Definition: parsenodes.h:1080
int errmsg(const char *fmt,...)
Definition: elog.c:821
Bitmapset * insertedCols
Definition: parsenodes.h:1123
Alias * alias
Definition: primnodes.h:72
Alias * eref
Definition: parsenodes.h:1116
List * ctecolcollations
Definition: parsenodes.h:1464
#define copyObject(obj)
Definition: nodes.h:644
List * coltypes
Definition: parsenodes.h:1102
List * p_rtable
Definition: parse_node.h:180

◆ addRangeTableEntryForENR()

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

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

2346 {
2348  Alias *alias = rv->alias;
2349  char *refname = alias ? alias->aliasname : rv->relname;
2351  TupleDesc tupdesc;
2352  int attno;
2353 
2354  Assert(pstate != NULL);
2355  enrmd = get_visible_ENR(pstate, rv->relname);
2356  Assert(enrmd != NULL);
2357 
2358  switch (enrmd->enrtype)
2359  {
2360  case ENR_NAMED_TUPLESTORE:
2362  break;
2363 
2364  default:
2365  elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2366  return NULL; /* for fussy compilers */
2367  }
2368 
2369  /*
2370  * Record dependency on a relation. This allows plans to be invalidated
2371  * if they access transition tables linked to a table that is altered.
2372  */
2373  rte->relid = enrmd->reliddesc;
2374 
2375  /*
2376  * Build the list of effective column names using user-supplied aliases
2377  * and/or actual column names.
2378  */
2379  tupdesc = ENRMetadataGetTupDesc(enrmd);
2380  rte->eref = makeAlias(refname, NIL);
2381  buildRelationAliases(tupdesc, alias, rte->eref);
2382 
2383  /* Record additional data for ENR, including column type info */
2384  rte->enrname = enrmd->name;
2385  rte->enrtuples = enrmd->enrtuples;
2386  rte->coltypes = NIL;
2387  rte->coltypmods = NIL;
2388  rte->colcollations = NIL;
2389  for (attno = 1; attno <= tupdesc->natts; ++attno)
2390  {
2391  Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2392 
2393  if (att->attisdropped)
2394  {
2395  /* Record zeroes for a dropped column */
2396  rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2397  rte->coltypmods = lappend_int(rte->coltypmods, 0);
2399  }
2400  else
2401  {
2402  /* Let's just make sure we can tell this isn't dropped */
2403  if (att->atttypid == InvalidOid)
2404  elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2405  rv->relname);
2406  rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2407  rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2409  att->attcollation);
2410  }
2411  }
2412 
2413  /*
2414  * Set flags and access permissions.
2415  *
2416  * ENRs are never checked for access rights.
2417  */
2418  rte->lateral = false;
2419  rte->inh = false; /* never true for ENRs */
2420  rte->inFromCl = inFromCl;
2421 
2422  rte->requiredPerms = 0;
2423  rte->checkAsUser = InvalidOid;
2424  rte->selectedCols = NULL;
2425 
2426  /*
2427  * Add completed RTE to pstate's range table list, so that we know its
2428  * index. But we don't add it to the join list --- caller must do that if
2429  * appropriate.
2430  */
2431  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2432 
2433  /*
2434  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2435  * list --- caller must do that if appropriate.
2436  */
2437  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
2438  tupdesc);
2439 }
#define NIL
Definition: pg_list.h:65
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
List * coltypmods
Definition: parsenodes.h:1103
AclMode requiredPerms
Definition: parsenodes.h:1120
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
List * colcollations
Definition: parsenodes.h:1104
EphemeralNamedRelationMetadata get_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:26
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
char * relname
Definition: primnodes.h:68
Bitmapset * selectedCols
Definition: parsenodes.h:1122
#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:193
char * enrname
Definition: parsenodes.h:1109
List * lappend_int(List *list, int datum)
Definition: list.c:339
List * lappend(List *list, void *datum)
Definition: list.c:321
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
RTEKind rtekind
Definition: parsenodes.h:977
double enrtuples
Definition: parsenodes.h:1110
#define elog(elevel,...)
Definition: elog.h:214
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * alias
Definition: primnodes.h:72
EphemeralNameRelationType enrtype
Alias * eref
Definition: parsenodes.h:1116
List * coltypes
Definition: parsenodes.h:1102
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(), exprType(), exprTypmod(), RangeTblEntry::extraUpdatedCols, format_type_be(), forthree, RangeTblFunction::funccolcollations, RangeTblFunction::funccolcount, RangeTblFunction::funccolnames, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblFunction::funcparams, RangeTblEntry::functions, get_expr_result_type(), GetColumnDefCollation(), i, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, linitial, list_length(), ColumnDef::location, makeAlias(), makeNode, makeString(), TupleDescData::natts, NIL, RangeFunction::ordinality, ParseState::p_rtable, palloc(), parser_errposition(), pstrdup(), RangeTblEntry::relid, RangeTblEntry::requiredPerms, RTE_FUNCTION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, TypeName::setof, RangeTblEntry::subquery, TupleDescCopyEntry(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, TYPEFUNC_SCALAR, ColumnDef::typeName, typenameTypeIdAndMod(), and RangeTblEntry::updatedCols.

Referenced by transformRangeFunction().

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. This
1741  * can be a bit confusing, so we expend some effort on delivering a
1742  * relevant error message.
1743  */
1744  if (coldeflist != NIL)
1745  {
1746  switch (functypclass)
1747  {
1748  case TYPEFUNC_RECORD:
1749  /* ok */
1750  break;
1751  case TYPEFUNC_COMPOSITE:
1753 
1754  /*
1755  * If the function's raw result type is RECORD, we must
1756  * have resolved it using its OUT parameters. Otherwise,
1757  * it must have a named composite type.
1758  */
1759  if (exprType(funcexpr) == RECORDOID)
1760  ereport(ERROR,
1761  (errcode(ERRCODE_SYNTAX_ERROR),
1762  errmsg("a column definition list is redundant for a function with OUT parameters"),
1763  parser_errposition(pstate,
1764  exprLocation((Node *) coldeflist))));
1765  else
1766  ereport(ERROR,
1767  (errcode(ERRCODE_SYNTAX_ERROR),
1768  errmsg("a column definition list is redundant for a function returning a named composite type"),
1769  parser_errposition(pstate,
1770  exprLocation((Node *) coldeflist))));
1771  break;
1772  default:
1773  ereport(ERROR,
1774  (errcode(ERRCODE_SYNTAX_ERROR),
1775  errmsg("a column definition list is only allowed for functions returning \"record\""),
1776  parser_errposition(pstate,
1777  exprLocation((Node *) coldeflist))));
1778  break;
1779  }
1780  }
1781  else
1782  {
1783  if (functypclass == TYPEFUNC_RECORD)
1784  ereport(ERROR,
1785  (errcode(ERRCODE_SYNTAX_ERROR),
1786  errmsg("a column definition list is required for functions returning \"record\""),
1787  parser_errposition(pstate, exprLocation(funcexpr))));
1788  }
1789 
1790  if (functypclass == TYPEFUNC_COMPOSITE ||
1791  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
1792  {
1793  /* Composite data type, e.g. a table's row type */
1794  Assert(tupdesc);
1795  }
1796  else if (functypclass == TYPEFUNC_SCALAR)
1797  {
1798  /* Base data type, i.e. scalar */
1799  tupdesc = CreateTemplateTupleDesc(1);
1800  TupleDescInitEntry(tupdesc,
1801  (AttrNumber) 1,
1802  chooseScalarFunctionAlias(funcexpr, funcname,
1803  alias, nfuncs),
1804  funcrettype,
1805  exprTypmod(funcexpr),
1806  0);
1808  (AttrNumber) 1,
1809  exprCollation(funcexpr));
1810  }
1811  else if (functypclass == TYPEFUNC_RECORD)
1812  {
1813  ListCell *col;
1814 
1815  /*
1816  * Use the column definition list to construct a tupdesc and fill
1817  * in the RangeTblFunction's lists.
1818  */
1819  tupdesc = CreateTemplateTupleDesc(list_length(coldeflist));
1820  i = 1;
1821  foreach(col, coldeflist)
1822  {
1823  ColumnDef *n = (ColumnDef *) lfirst(col);
1824  char *attrname;
1825  Oid attrtype;
1826  int32 attrtypmod;
1827  Oid attrcollation;
1828 
1829  attrname = n->colname;
1830  if (n->typeName->setof)
1831  ereport(ERROR,
1832  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
1833  errmsg("column \"%s\" cannot be declared SETOF",
1834  attrname),
1835  parser_errposition(pstate, n->location)));
1836  typenameTypeIdAndMod(pstate, n->typeName,
1837  &attrtype, &attrtypmod);
1838  attrcollation = GetColumnDefCollation(pstate, n, attrtype);
1839  TupleDescInitEntry(tupdesc,
1840  (AttrNumber) i,
1841  attrname,
1842  attrtype,
1843  attrtypmod,
1844  0);
1846  (AttrNumber) i,
1847  attrcollation);
1848  rtfunc->funccolnames = lappend(rtfunc->funccolnames,
1849  makeString(pstrdup(attrname)));
1850  rtfunc->funccoltypes = lappend_oid(rtfunc->funccoltypes,
1851  attrtype);
1852  rtfunc->funccoltypmods = lappend_int(rtfunc->funccoltypmods,
1853  attrtypmod);
1855  attrcollation);
1856 
1857  i++;
1858  }
1859 
1860  /*
1861  * Ensure that the coldeflist defines a legal set of names (no
1862  * duplicates, but we needn't worry about system column names) and
1863  * datatypes. Although we mostly can't allow pseudo-types, it
1864  * seems safe to allow RECORD and RECORD[], since values within
1865  * those type classes are self-identifying at runtime, and the
1866  * coldeflist doesn't represent anything that will be visible to
1867  * other sessions.
1868  */
1869  CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE,
1871  }
1872  else
1873  ereport(ERROR,
1874  (errcode(ERRCODE_DATATYPE_MISMATCH),
1875  errmsg("function \"%s\" in FROM has unsupported return type %s",
1876  funcname, format_type_be(funcrettype)),
1877  parser_errposition(pstate, exprLocation(funcexpr))));
1878 
1879  /* Finish off the RangeTblFunction and add it to the RTE's list */
1880  rtfunc->funccolcount = tupdesc->natts;
1881  rte->functions = lappend(rte->functions, rtfunc);
1882 
1883  /* Save the tupdesc for use below */
1884  functupdescs[funcno] = tupdesc;
1885  totalatts += tupdesc->natts;
1886  funcno++;
1887  }
1888 
1889  /*
1890  * If there's more than one function, or we want an ordinality column, we
1891  * have to produce a merged tupdesc.
1892  */
1893  if (nfuncs > 1 || rangefunc->ordinality)
1894  {
1895  if (rangefunc->ordinality)
1896  totalatts++;
1897 
1898  /* Merge the tuple descs of each function into a composite one */
1899  tupdesc = CreateTemplateTupleDesc(totalatts);
1900  natts = 0;
1901  for (i = 0; i < nfuncs; i++)
1902  {
1903  for (j = 1; j <= functupdescs[i]->natts; j++)
1904  TupleDescCopyEntry(tupdesc, ++natts, functupdescs[i], j);
1905  }
1906 
1907  /* Add the ordinality column if needed */
1908  if (rangefunc->ordinality)
1909  {
1910  TupleDescInitEntry(tupdesc,
1911  (AttrNumber) ++natts,
1912  "ordinality",
1913  INT8OID,
1914  -1,
1915  0);
1916  /* no need to set collation */
1917  }
1918 
1919  Assert(natts == totalatts);
1920  }
1921  else
1922  {
1923  /* We can just use the single function's tupdesc as-is */
1924  tupdesc = functupdescs[0];
1925  }
1926 
1927  /* Use the tupdesc while assigning column aliases for the RTE */
1928  buildRelationAliases(tupdesc, alias, eref);
1929 
1930  /*
1931  * Set flags and access permissions.
1932  *
1933  * Functions are never checked for access rights (at least, not by the RTE
1934  * permissions mechanism).
1935  */
1936  rte->lateral = lateral;
1937  rte->inh = false; /* never true for functions */
1938  rte->inFromCl = inFromCl;
1939 
1940  rte->requiredPerms = 0;
1941  rte->checkAsUser = InvalidOid;
1942  rte->selectedCols = NULL;
1943  rte->insertedCols = NULL;
1944  rte->updatedCols = NULL;
1945  rte->extraUpdatedCols = NULL;
1946 
1947  /*
1948  * Add completed RTE to pstate's range table list, so that we know its
1949  * index. But we don't add it to the join list --- caller must do that if
1950  * appropriate.
1951  */
1952  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1953 
1954  /*
1955  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1956  * list --- caller must do that if appropriate.
1957  */
1958  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1959  tupdesc);
1960 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:569
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1240
Alias * alias
Definition: parsenodes.h:1115
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:479
char * pstrdup(const char *in)
Definition: mcxt.c:1187
Definition: nodes.h:528
int errcode(int sqlerrcode)
Definition: elog.c:610
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
AclMode requiredPerms
Definition: parsenodes.h:1120
bool funcordinality
Definition: parsenodes.h:1065
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:221
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
signed int int32
Definition: c.h:363
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1125
Bitmapset * selectedCols
Definition: parsenodes.h:1122
#define linitial(l)
Definition: pg_list.h:174
#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:212
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:769
int location
Definition: parsenodes.h:665
TypeFuncClass
Definition: funcapi.h:146
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
List * lappend_int(List *list, int datum)
Definition: list.c:339
List * lappend(List *list, void *datum)
Definition: list.c:321
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:1154
#define ereport(elevel,...)
Definition: elog.h:144
Bitmapset * updatedCols
Definition: parsenodes.h:1124
List * funccolcollations
Definition: parsenodes.h:1155
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1064
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:768
TypeName * typeName
Definition: parsenodes.h:649
Bitmapset * funcparams
Definition: parsenodes.h:1157
RTEKind rtekind
Definition: parsenodes.h:977
#define CHKATYPE_ANYRECORD
Definition: heap.h:24
Query * subquery
Definition: parsenodes.h:1012
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:821
Bitmapset * insertedCols
Definition: parsenodes.h:1123
int i
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
char * colname
Definition: parsenodes.h:648
Alias * eref
Definition: parsenodes.h:1116
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 2130 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().

2140 {
2142  Alias *eref;
2143  int numaliases;
2144  ParseNamespaceItem *nsitem;
2145 
2146  Assert(pstate != NULL);
2147 
2148  /*
2149  * Fail if join has too many columns --- we must be able to reference any
2150  * of the columns with an AttrNumber.
2151  */
2152  if (list_length(aliasvars) > MaxAttrNumber)
2153  ereport(ERROR,
2154  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2155  errmsg("joins can have at most %d columns",
2156  MaxAttrNumber)));
2157 
2158  rte->rtekind = RTE_JOIN;
2159  rte->relid = InvalidOid;
2160  rte->subquery = NULL;
2161  rte->jointype = jointype;
2162  rte->joinmergedcols = nummergedcols;
2163  rte->joinaliasvars = aliasvars;
2164  rte->joinleftcols = leftcols;
2165  rte->joinrightcols = rightcols;
2166  rte->alias = alias;
2167 
2168  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
2169  numaliases = list_length(eref->colnames);
2170 
2171  /* fill in any unspecified alias columns */
2172  if (numaliases < list_length(colnames))
2173  eref->colnames = list_concat(eref->colnames,
2174  list_copy_tail(colnames, numaliases));
2175 
2176  rte->eref = eref;
2177 
2178  /*
2179  * Set flags and access permissions.
2180  *
2181  * Joins are never checked for access rights.
2182  */
2183  rte->lateral = false;
2184  rte->inh = false; /* never true for joins */
2185  rte->inFromCl = inFromCl;
2186 
2187  rte->requiredPerms = 0;
2188  rte->checkAsUser = InvalidOid;
2189  rte->selectedCols = NULL;
2190  rte->insertedCols = NULL;
2191  rte->updatedCols = NULL;
2192  rte->extraUpdatedCols = NULL;
2193 
2194  /*
2195  * Add completed RTE to pstate's range table list, so that we know its
2196  * index. But we don't add it to the join list --- caller must do that if
2197  * appropriate.
2198  */
2199  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2200 
2201  /*
2202  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2203  * list --- caller must do that if appropriate.
2204  */
2205  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2206  nsitem->p_rte = rte;
2207  nsitem->p_rtindex = list_length(pstate->p_rtable);
2208  nsitem->p_nscolumns = nscolumns;
2209  /* set default visibility flags; might get changed later */
2210  nsitem->p_rel_visible = true;
2211  nsitem->p_cols_visible = true;
2212  nsitem->p_lateral_only = false;
2213  nsitem->p_lateral_ok = true;
2214 
2215  return nsitem;
2216 }
#define NIL
Definition: pg_list.h:65
List * joinaliasvars
Definition: parsenodes.h:1052
Alias * alias
Definition: parsenodes.h:1115
List * colnames
Definition: primnodes.h:43
#define MaxAttrNumber
Definition: attnum.h:24
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
int errcode(int sqlerrcode)
Definition: elog.c:610
AclMode requiredPerms
Definition: parsenodes.h:1120
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1422
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
RangeTblEntry * p_rte
Definition: parse_node.h:257
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1125
Bitmapset * selectedCols
Definition: parsenodes.h:1122
#define ERROR
Definition: elog.h:43
JoinType jointype
Definition: parsenodes.h:1050
List * joinrightcols
Definition: parsenodes.h:1054
List * lappend(List *list, void *datum)
Definition: list.c:321
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
Bitmapset * updatedCols
Definition: parsenodes.h:1124
List * joinleftcols
Definition: parsenodes.h:1053
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:977
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:260
Query * subquery
Definition: parsenodes.h:1012
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:821
Bitmapset * insertedCols
Definition: parsenodes.h:1123
Alias * eref
Definition: parsenodes.h:1116
#define copyObject(obj)
Definition: nodes.h:644
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:1115
#define AccessShareLock
Definition: lockdefs.h:36
AclMode requiredPerms
Definition: parsenodes.h:1120
Form_pg_class rd_rel
Definition: rel.h:109
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1125
Bitmapset * selectedCols
Definition: parsenodes.h:1122
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
#define RowExclusiveLock
Definition: lockdefs.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:490
List * lappend(List *list, void *datum)
Definition: list.c:321
#define RowShareLock
Definition: lockdefs.h:37
#define ACL_SELECT
Definition: parsenodes.h:75
TupleDesc rd_att
Definition: rel.h:110
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1124
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:302
RTEKind rtekind
Definition: parsenodes.h:977
Bitmapset * insertedCols
Definition: parsenodes.h:1123
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * eref
Definition: parsenodes.h:1116
#define RelationGetRelid(relation)
Definition: rel.h:456
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:1115
List * colnames
Definition: primnodes.h:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
char * pstrdup(const char *in)
Definition: mcxt.c:1187
Definition: nodes.h:528
int errcode(int sqlerrcode)
Definition: elog.c:610
AclMode requiredPerms
Definition: parsenodes.h:1120
char * resname
Definition: primnodes.h:1412
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
List * targetList
Definition: parsenodes.h:140
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1125
Bitmapset * selectedCols
Definition: parsenodes.h:1122
bool resjunk
Definition: primnodes.h:1417
#define ERROR
Definition: elog.h:43
AttrNumber resno
Definition: primnodes.h:1411
List * lappend_int(List *list, int datum)
Definition: list.c:339
List * lappend(List *list, void *datum)
Definition: list.c:321
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
Bitmapset * updatedCols
Definition: parsenodes.h:1124
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
Expr * expr
Definition: primnodes.h:1410
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:768
RTEKind rtekind
Definition: parsenodes.h:977
Query * subquery
Definition: parsenodes.h:1012
int errmsg(const char *fmt,...)
Definition: elog.c:821
Bitmapset * insertedCols
Definition: parsenodes.h:1123
Alias * eref
Definition: parsenodes.h:1116
#define copyObject(obj)
Definition: nodes.h:644
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 1969 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().

1974 {
1976  char *refname = alias ? alias->aliasname : pstrdup("xmltable");
1977  Alias *eref;
1978  int numaliases;
1979 
1980  Assert(pstate != NULL);
1981 
1982  rte->rtekind = RTE_TABLEFUNC;
1983  rte->relid = InvalidOid;
1984  rte->subquery = NULL;
1985  rte->tablefunc = tf;
1986  rte->coltypes = tf->coltypes;
1987  rte->coltypmods = tf->coltypmods;
1988  rte->colcollations = tf->colcollations;
1989  rte->alias = alias;
1990 
1991  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1992  numaliases = list_length(eref->colnames);
1993 
1994  /* fill in any unspecified alias columns */
1995  if (numaliases < list_length(tf->colnames))
1996  eref->colnames = list_concat(eref->colnames,
1997  list_copy_tail(tf->colnames, numaliases));
1998 
1999  rte->eref = eref;
2000 
2001  /*
2002  * Set flags and access permissions.
2003  *
2004  * Tablefuncs are never checked for access rights (at least, not by the
2005  * RTE permissions mechanism).
2006  */
2007  rte->lateral = lateral;
2008  rte->inh = false; /* never true for tablefunc RTEs */
2009  rte->inFromCl = inFromCl;
2010 
2011  rte->requiredPerms = 0;
2012  rte->checkAsUser = InvalidOid;
2013  rte->selectedCols = NULL;
2014  rte->insertedCols = NULL;
2015  rte->updatedCols = NULL;
2016  rte->extraUpdatedCols = NULL;
2017 
2018  /*
2019  * Add completed RTE to pstate's range table list, so that we know its
2020  * index. But we don't add it to the join list --- caller must do that if
2021  * appropriate.
2022  */
2023  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2024 
2025  /*
2026  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2027  * list --- caller must do that if appropriate.
2028  */
2029  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2030  rte->coltypes, rte->coltypmods,
2031  rte->colcollations);
2032 }
#define NIL
Definition: pg_list.h:65
List * colnames
Definition: primnodes.h:89
Alias * alias
Definition: parsenodes.h:1115
List * colnames
Definition: primnodes.h:43
List * coltypmods
Definition: primnodes.h:91
char * pstrdup(const char *in)
Definition: mcxt.c:1187
List * coltypmods
Definition: parsenodes.h:1103
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
AclMode requiredPerms
Definition: parsenodes.h:1120
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1422
List * colcollations
Definition: parsenodes.h:1104
List * colcollations
Definition: primnodes.h:92
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1125
Bitmapset * selectedCols
Definition: parsenodes.h:1122
TableFunc * tablefunc
Definition: parsenodes.h:1070
List * lappend(List *list, void *datum)
Definition: list.c:321
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:1124
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:977
Query * subquery
Definition: parsenodes.h:1012
Bitmapset * insertedCols
Definition: parsenodes.h:1123
Alias * eref
Definition: parsenodes.h:1116
#define copyObject(obj)
Definition: nodes.h:644
List * coltypes
Definition: parsenodes.h:1102
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 2041 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().

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

◆ attnameAttNum()

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

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

3328 {
3329  int i;
3330 
3331  for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3332  {
3333  Form_pg_attribute att = TupleDescAttr(rd->rd_att, i);
3334 
3335  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3336  return i + 1;
3337  }
3338 
3339  if (sysColOK)
3340  {
3341  if ((i = specialAttNum(attname)) != InvalidAttrNumber)
3342  return i;
3343  }
3344 
3345  /* on failure */
3346  return InvalidAttrNumber;
3347 }
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:462
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int namestrcmp(Name name, const char *str)
Definition: name.c:247
static int specialAttNum(const char *attname)
NameData attname
Definition: pg_attribute.h:40
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
TupleDesc rd_att
Definition: rel.h:110
#define InvalidAttrNumber
Definition: attnum.h:23
int i

◆ attnumAttName()

const NameData* attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3377 of file parse_relation.c.

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

Referenced by transformFkeyGetPrimaryKey().

3378 {
3379  if (attid <= 0)
3380  {
3381  const FormData_pg_attribute *sysatt;
3382 
3383  sysatt = SystemAttributeDefinition(attid);
3384  return &sysatt->attname;
3385  }
3386  if (attid > rd->rd_att->natts)
3387  elog(ERROR, "invalid attribute number %d", attid);
3388  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3389 }
#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:110
FormData_pg_attribute
Definition: pg_attribute.h:177
#define elog(elevel,...)
Definition: elog.h:214

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3419 of file parse_relation.c.

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

3420 {
3421  if (attid <= 0)
3422  {
3423  /* All system attributes are of noncollatable types. */
3424  return InvalidOid;
3425  }
3426  if (attid > rd->rd_att->natts)
3427  elog(ERROR, "invalid attribute number %d", attid);
3428  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3429 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define ERROR
Definition: elog.h:43
TupleDesc rd_att
Definition: rel.h:110
#define InvalidOid
Definition: postgres_ext.h:36
#define elog(elevel,...)
Definition: elog.h:214

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3399 of file parse_relation.c.

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

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

3400 {
3401  if (attid <= 0)
3402  {
3403  const FormData_pg_attribute *sysatt;
3404 
3405  sysatt = SystemAttributeDefinition(attid);
3406  return sysatt->atttypid;
3407  }
3408  if (attid > rd->rd_att->natts)
3409  elog(ERROR, "invalid attribute number %d", attid);
3410  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3411 }
#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:110
FormData_pg_attribute
Definition: pg_attribute.h:177
#define elog(elevel,...)
Definition: elog.h:214

◆ buildNSItemFromLists()

static ParseNamespaceItem* buildNSItemFromLists ( RangeTblEntry rte,
Index  rtindex,
List coltypes,
List coltypmods,
List colcollations 
)
static

Definition at line 1283 of file parse_relation.c.

References Assert, Alias::colnames, RangeTblEntry::eref, forthree, lfirst_int, lfirst_oid, list_length(), ParseNamespaceItem::p_cols_visible, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseNamespaceItem::p_nscolumns, ParseNamespaceItem::p_rel_visible, 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, palloc(), and palloc0().

Referenced by addRangeTableEntryForCTE(), addRangeTableEntryForSubquery(), addRangeTableEntryForTableFunc(), and addRangeTableEntryForValues().

1285 {
1286  ParseNamespaceItem *nsitem;
1287  ParseNamespaceColumn *nscolumns;
1288  int maxattrs = list_length(coltypes);
1289  int varattno;
1290  ListCell *lct;
1291  ListCell *lcm;
1292  ListCell *lcc;
1293 
1294  /* colnames must have the same number of entries as the nsitem */
1295  Assert(maxattrs == list_length(rte->eref->colnames));
1296 
1297  Assert(maxattrs == list_length(coltypmods));
1298  Assert(maxattrs == list_length(colcollations));
1299 
1300  /* extract per-column data from the lists */
1301  nscolumns = (ParseNamespaceColumn *)
1302  palloc0(maxattrs * sizeof(ParseNamespaceColumn));
1303 
1304  varattno = 0;
1305  forthree(lct, coltypes,
1306  lcm, coltypmods,
1307  lcc, colcollations)
1308  {
1309  nscolumns[varattno].p_varno = rtindex;
1310  nscolumns[varattno].p_varattno = varattno + 1;
1311  nscolumns[varattno].p_vartype = lfirst_oid(lct);
1312  nscolumns[varattno].p_vartypmod = lfirst_int(lcm);
1313  nscolumns[varattno].p_varcollid = lfirst_oid(lcc);
1314  nscolumns[varattno].p_varnosyn = rtindex;
1315  nscolumns[varattno].p_varattnosyn = varattno + 1;
1316  varattno++;
1317  }
1318 
1319  /* ... and build the nsitem */
1320  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1321  nsitem->p_rte = rte;
1322  nsitem->p_rtindex = rtindex;
1323  nsitem->p_nscolumns = nscolumns;
1324  /* set default visibility flags; might get changed later */
1325  nsitem->p_rel_visible = true;
1326  nsitem->p_cols_visible = true;
1327  nsitem->p_lateral_only = false;
1328  nsitem->p_lateral_ok = true;
1329 
1330  return nsitem;
1331 }
List * colnames
Definition: primnodes.h:43
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:479
RangeTblEntry * p_rte
Definition: parse_node.h:257
#define lfirst_int(lc)
Definition: pg_list.h:170
AttrNumber p_varattno
Definition: parse_node.h:291
void * palloc0(Size size)
Definition: mcxt.c:981
#define Assert(condition)
Definition: c.h:746
static int list_length(const List *l)
Definition: pg_list.h:149
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:260
void * palloc(Size size)
Definition: mcxt.c:950
AttrNumber p_varattnosyn
Definition: parse_node.h:296
Alias * eref
Definition: parsenodes.h:1116
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ buildNSItemFromTupleDesc()

static ParseNamespaceItem* buildNSItemFromTupleDesc ( RangeTblEntry rte,
Index  rtindex,
TupleDesc  tupdesc 
)
static

Definition at line 1227 of file parse_relation.c.

References Assert, Alias::colnames, RangeTblEntry::eref, list_length(), TupleDescData::natts, ParseNamespaceItem::p_cols_visible, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseNamespaceItem::p_nscolumns, ParseNamespaceItem::p_rel_visible, 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, palloc(), palloc0(), and TupleDescAttr.

Referenced by addRangeTableEntry(), addRangeTableEntryForENR(), addRangeTableEntryForFunction(), and addRangeTableEntryForRelation().

1228 {
1229  ParseNamespaceItem *nsitem;
1230  ParseNamespaceColumn *nscolumns;
1231  int maxattrs = tupdesc->natts;
1232  int varattno;
1233 
1234  /* colnames must have the same number of entries as the nsitem */
1235  Assert(maxattrs == list_length(rte->eref->colnames));
1236 
1237  /* extract per-column data from the tupdesc */
1238  nscolumns = (ParseNamespaceColumn *)
1239  palloc0(maxattrs * sizeof(ParseNamespaceColumn));
1240 
1241  for (varattno = 0; varattno < maxattrs; varattno++)
1242  {
1243  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1244 
1245  /* For a dropped column, just leave the entry as zeroes */
1246  if (attr->attisdropped)
1247  continue;
1248 
1249  nscolumns[varattno].p_varno = rtindex;
1250  nscolumns[varattno].p_varattno = varattno + 1;
1251  nscolumns[varattno].p_vartype = attr->atttypid;
1252  nscolumns[varattno].p_vartypmod = attr->atttypmod;
1253  nscolumns[varattno].p_varcollid = attr->attcollation;
1254  nscolumns[varattno].p_varnosyn = rtindex;
1255  nscolumns[varattno].p_varattnosyn = varattno + 1;
1256  }
1257 
1258  /* ... and build the nsitem */
1259  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1260  nsitem->p_rte = rte;
1261  nsitem->p_rtindex = rtindex;
1262  nsitem->p_nscolumns = nscolumns;
1263  /* set default visibility flags; might get changed later */
1264  nsitem->p_rel_visible = true;
1265  nsitem->p_cols_visible = true;
1266  nsitem->p_lateral_only = false;
1267  nsitem->p_lateral_ok = true;
1268 
1269  return nsitem;
1270 }
List * colnames
Definition: primnodes.h:43
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
RangeTblEntry * p_rte
Definition: parse_node.h:257
AttrNumber p_varattno
Definition: parse_node.h:291
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void * palloc0(Size size)
Definition: mcxt.c:981
#define Assert(condition)
Definition: c.h:746
static int list_length(const List *l)
Definition: pg_list.h:149
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:260
void * palloc(Size size)
Definition: mcxt.c:950
AttrNumber p_varattnosyn
Definition: parse_node.h:296
Alias * eref
Definition: parsenodes.h:1116

◆ buildRelationAliases()

static void buildRelationAliases ( TupleDesc  tupdesc,
Alias alias,
Alias eref 
)
static

Definition at line 1111 of file parse_relation.c.

References Alias::aliasname, Assert, Alias::colnames, ereport, errcode(), errmsg(), ERROR, lappend(), lfirst, list_head(), list_length(), lnext(), makeString(), NameStr, TupleDescData::natts, NIL, pstrdup(), and TupleDescAttr.

Referenced by addRangeTableEntry(), addRangeTableEntryForENR(), addRangeTableEntryForFunction(), and addRangeTableEntryForRelation().

1112 {
1113  int maxattrs = tupdesc->natts;
1114  List *aliaslist;
1115  ListCell *aliaslc;
1116  int numaliases;
1117  int varattno;
1118  int numdropped = 0;
1119 
1120  Assert(eref->colnames == NIL);
1121 
1122  if (alias)
1123  {
1124  aliaslist = alias->colnames;
1125  aliaslc = list_head(aliaslist);
1126  numaliases = list_length(aliaslist);
1127  /* We'll rebuild the alias colname list */
1128  alias->colnames = NIL;
1129  }
1130  else
1131  {
1132  aliaslist = NIL;
1133  aliaslc = NULL;
1134  numaliases = 0;
1135  }
1136 
1137  for (varattno = 0; varattno < maxattrs; varattno++)
1138  {
1139  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1140  Value *attrname;
1141 
1142  if (attr->attisdropped)
1143  {
1144  /* Always insert an empty string for a dropped column */
1145  attrname = makeString(pstrdup(""));
1146  if (aliaslc)
1147  alias->colnames = lappend(alias->colnames, attrname);
1148  numdropped++;
1149  }
1150  else if (aliaslc)
1151  {
1152  /* Use the next user-supplied alias */
1153  attrname = (Value *) lfirst(aliaslc);
1154  aliaslc = lnext(aliaslist, aliaslc);
1155  alias->colnames = lappend(alias->colnames, attrname);
1156  }
1157  else
1158  {
1159  attrname = makeString(pstrdup(NameStr(attr->attname)));
1160  /* we're done with the alias if any */
1161  }
1162 
1163  eref->colnames = lappend(eref->colnames, attrname);
1164  }
1165 
1166  /* Too many user-supplied aliases? */
1167  if (aliaslc)
1168  ereport(ERROR,
1169  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1170  errmsg("table \"%s\" has %d columns available but %d columns specified",
1171  eref->aliasname, maxattrs - numdropped, numaliases)));
1172 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:310
List * colnames
Definition: primnodes.h:43
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1187
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
List * lappend(List *list, void *datum)
Definition: list.c:321
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
Definition: value.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define NameStr(name)
Definition: c.h:623
Definition: pg_list.h:50

◆ check_lateral_ref_ok()

static void check_lateral_ref_ok ( ParseState pstate,
ParseNamespaceItem nsitem,
int  location 
)
static

Definition at line 462 of file parse_relation.c.

References Alias::aliasname, RangeTblEntry::eref, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseNamespaceItem::p_rte, ParseState::p_target_nsitem, and parser_errposition().

Referenced by colNameToVar(), scanNameSpaceForRefname(), and scanNameSpaceForRelid().

464 {
465  if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
466  {
467  /* SQL:2008 demands this be an error, not an invisible item */
468  RangeTblEntry *rte = nsitem->p_rte;
469  char *refname = rte->eref->aliasname;
470 
471  ereport(ERROR,
472  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
473  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
474  refname),
475  (pstate->p_target_nsitem != NULL &&
476  rte == pstate->p_target_nsitem->p_rte) ?
477  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
478  refname) :
479  errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
480  parser_errposition(pstate, location)));
481  }
482 }
int errhint(const char *fmt,...)
Definition: elog.c:1068
int errcode(int sqlerrcode)
Definition: elog.c:610
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:191
RangeTblEntry * p_rte
Definition: parse_node.h:257
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:954
#define ereport(elevel,...)
Definition: elog.h:144
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:821
Alias * eref
Definition: parsenodes.h:1116

◆ 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:1115
int errcode(int sqlerrcode)
Definition: elog.c:610
RangeTblEntry * p_rte
Definition: parse_node.h:257
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
RTEKind rtekind
Definition: parsenodes.h:977
int errmsg(const char *fmt,...)
Definition: elog.c:821
Alias * eref
Definition: parsenodes.h:1116

◆ chooseScalarFunctionAlias()

static char* chooseScalarFunctionAlias ( Node funcexpr,
char *  funcname,
Alias alias,
int  nfuncs 
)
static

Definition at line 1188 of file parse_relation.c.

References Alias::aliasname, get_func_result_name(), and IsA.

Referenced by addRangeTableEntryForFunction().

1190 {
1191  char *pname;
1192 
1193  /*
1194  * If the expression is a simple function call, and the function has a
1195  * single OUT parameter that is named, use the parameter's name.
1196  */
1197  if (funcexpr && IsA(funcexpr, FuncExpr))
1198  {
1199  pname = get_func_result_name(((FuncExpr *) funcexpr)->funcid);
1200  if (pname)
1201  return pname;
1202  }
1203 
1204  /*
1205  * If there's just one function in the RTE, and the user gave an RTE alias
1206  * name, use that name. (This makes FROM func() AS foo use "foo" as the
1207  * column name as well as the table alias.)
1208  */
1209  if (nfuncs == 1 && alias)
1210  return alias->aliasname;
1211 
1212  /*
1213  * Otherwise use the function name.
1214  */
1215  return funcname;
1216 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
char * aliasname
Definition: primnodes.h:42
char * get_func_result_name(Oid functionId)
Definition: funcapi.c:1323

◆ 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:528
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
int errcode(int sqlerrcode)
Definition: elog.c:610
#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,...)
Definition: elog.h:144
bool p_lateral_active
Definition: parse_node.h:186
#define lfirst(lc)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:821

◆ errorMissingColumn()

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

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

3501 {
3503  char *closestfirst = NULL;
3504 
3505  /*
3506  * Search the entire rtable looking for possible matches. If we find one,
3507  * emit a hint about it.
3508  *
3509  * TODO: improve this code (and also errorMissingRTE) to mention using
3510  * LATERAL if appropriate.
3511  */
3512  state = searchRangeTableForCol(pstate, relname, colname, location);
3513 
3514  /*
3515  * Extract closest col string for best match, if any.
3516  *
3517  * Infer an exact match referenced despite not being visible from the fact
3518  * that an attribute number was not present in state passed back -- this
3519  * is what is reported when !closestfirst. There might also be an exact
3520  * match that was qualified with an incorrect alias, in which case
3521  * closestfirst will be set (so hint is the same as generic fuzzy case).
3522  */
3523  if (state->rfirst && AttributeNumberIsValid(state->first))
3524  closestfirst = strVal(list_nth(state->rfirst->eref->colnames,
3525  state->first - 1));
3526 
3527  if (!state->rsecond)
3528  {
3529  /*
3530  * Handle case where there is zero or one column suggestions to hint,
3531  * including exact matches referenced but not visible.
3532  */
3533  ereport(ERROR,
3534  (errcode(ERRCODE_UNDEFINED_COLUMN),
3535  relname ?
3536  errmsg("column %s.%s does not exist", relname, colname) :
3537  errmsg("column \"%s\" does not exist", colname),
3538  state->rfirst ? closestfirst ?
3539  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3540  state->rfirst->eref->aliasname, closestfirst) :
3541  errhint("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3542  colname, state->rfirst->eref->aliasname) : 0,
3543  parser_errposition(pstate, location)));
3544  }
3545  else
3546  {
3547  /* Handle case where there are two equally useful column hints */
3548  char *closestsecond;
3549 
3550  closestsecond = strVal(list_nth(state->rsecond->eref->colnames,
3551  state->second - 1));
3552 
3553  ereport(ERROR,
3554  (errcode(ERRCODE_UNDEFINED_COLUMN),
3555  relname ?
3556  errmsg("column %s.%s does not exist", relname, colname) :
3557  errmsg("column \"%s\" does not exist", colname),
3558  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3559  state->rfirst->eref->aliasname, closestfirst,
3560  state->rsecond->eref->aliasname, closestsecond),
3561  parser_errposition(pstate, location)));
3562  }
3563 }
int errhint(const char *fmt,...)
Definition: elog.c:1068
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:610
NameData relname
Definition: pg_class.h:38
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
RangeTblEntry * rfirst
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
#define ereport(elevel,...)
Definition: elog.h:144
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:821
Alias * eref
Definition: parsenodes.h:1116

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

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

3439 {
3440  RangeTblEntry *rte;
3441  const char *badAlias = NULL;
3442 
3443  /*
3444  * Check to see if there are any potential matches in the query's
3445  * rangetable. (Note: cases involving a bad schema name in the RangeVar
3446  * will throw error immediately here. That seems OK.)
3447  */
3448  rte = searchRangeTableForRel(pstate, relation);
3449 
3450  /*
3451  * If we found a match that has an alias and the alias is visible in the
3452  * namespace, then the problem is probably use of the relation's real name
3453  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3454  * common enough to justify a specific hint.
3455  *
3456  * If we found a match that doesn't meet those criteria, assume the
3457  * problem is illegal use of a relation outside its scope, as in the
3458  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3459  */
3460  if (rte && rte->alias &&
3461  strcmp(rte->eref->aliasname, relation->relname) != 0)
3462  {
3463  ParseNamespaceItem *nsitem;
3464  int sublevels_up;
3465 
3466  nsitem = refnameNamespaceItem(pstate, NULL, rte->eref->aliasname,
3467  relation->location,
3468  &sublevels_up);
3469  if (nsitem && nsitem->p_rte == rte)
3470  badAlias = rte->eref->aliasname;
3471  }
3472 
3473  if (rte)
3474  ereport(ERROR,
3476  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3477  relation->relname),
3478  (badAlias ?
3479  errhint("Perhaps you meant to reference the table alias \"%s\".",
3480  badAlias) :
3481  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3482  rte->eref->aliasname)),
3483  parser_errposition(pstate, relation->location)));
3484  else
3485  ereport(ERROR,
3487  errmsg("missing FROM-clause entry for table \"%s\"",
3488  relation->relname),
3489  parser_errposition(pstate, relation->location)));
3490 }
int errhint(const char *fmt,...)
Definition: elog.c:1068
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:74
Alias * alias
Definition: parsenodes.h:1115
int errcode(int sqlerrcode)
Definition: elog.c:610
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,...)
Definition: elog.h:144
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:821
Alias * eref
Definition: parsenodes.h:1116

◆ expandNSItemAttrs()

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

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

3051 {
3052  RangeTblEntry *rte = nsitem->p_rte;
3053  List *names,
3054  *vars;
3055  ListCell *name,
3056  *var;
3057  List *te_list = NIL;
3058 
3059  vars = expandNSItemVars(nsitem, sublevels_up, location, &names);
3060 
3061  /*
3062  * Require read access to the table. This is normally redundant with the
3063  * markVarForSelectPriv calls below, but not if the table has zero
3064  * columns.
3065  */
3066  rte->requiredPerms |= ACL_SELECT;
3067 
3068  forboth(name, names, var, vars)
3069  {
3070  char *label = strVal(lfirst(name));
3071  Var *varnode = (Var *) lfirst(var);
3072  TargetEntry *te;
3073 
3074  te = makeTargetEntry((Expr *) varnode,
3075  (AttrNumber) pstate->p_next_resno++,
3076  label,
3077  false);
3078  te_list = lappend(te_list, te);
3079 
3080  /* Require read access to each column */
3081  markVarForSelectPriv(pstate, varnode, rte);
3082  }
3083 
3084  Assert(name == NULL && var == NULL); /* lists not the same length? */
3085 
3086  return te_list;
3087 }
#define NIL
Definition: pg_list.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:434
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
#define strVal(v)
Definition: value.h:54
AclMode requiredPerms
Definition: parsenodes.h:1120
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:238
List * lappend(List *list, void *datum)
Definition: list.c:321
#define ACL_SELECT
Definition: parsenodes.h:75
static char * label
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
List * expandNSItemVars(ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
const char * name
Definition: encode.c:561
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 2994 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().

2997 {
2998  List *result = NIL;
2999  int colindex;
3000  ListCell *lc;
3001 
3002  if (colnames)
3003  *colnames = NIL;
3004  colindex = 0;
3005  foreach(lc, nsitem->p_rte->eref->colnames)
3006  {
3007  Value *colnameval = (Value *) lfirst(lc);
3008  const char *colname = strVal(colnameval);
3009  ParseNamespaceColumn *nscol = nsitem->p_nscolumns + colindex;
3010 
3011  if (colname[0])
3012  {
3013  Var *var;
3014 
3015  Assert(nscol->p_varno > 0);
3016  var = makeVar(nscol->p_varno,
3017  nscol->p_varattno,
3018  nscol->p_vartype,
3019  nscol->p_vartypmod,
3020  nscol->p_varcollid,
3021  sublevels_up);
3022  /* makeVar doesn't offer parameters for these, so set by hand: */
3023  var->varnosyn = nscol->p_varnosyn;
3024  var->varattnosyn = nscol->p_varattnosyn;
3025  var->location = location;
3026  result = lappend(result, var);
3027  if (colnames)
3028  *colnames = lappend(*colnames, colnameval);
3029  }
3030  else
3031  {
3032  /* dropped column, ignore */
3033  Assert(nscol->p_varno == 0);
3034  }
3035  colindex++;
3036  }
3037  return result;
3038 }
#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:321
AttrNumber varattnosyn
Definition: primnodes.h:195
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
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:1116
Definition: pg_list.h:50

◆ expandRelation()

static void expandRelation ( Oid  relid,
Alias eref,
int  rtindex,
int  sublevels_up,
int  location,
bool  include_dropped,
List **  colnames,
List **  colvars 
)
static

Definition at line 2888 of file parse_relation.c.

References AccessShareLock, expandTupleDesc(), TupleDescData::natts, RelationData::rd_att, relation_close(), and relation_open().

Referenced by expandRTE().

2891 {
2892  Relation rel;
2893 
2894  /* Get the tupledesc and turn it over to expandTupleDesc */
2895  rel = relation_open(relid, AccessShareLock);
2896  expandTupleDesc(rel->rd_att, eref, rel->rd_att->natts, 0,
2897  rtindex, sublevels_up,
2898  location, include_dropped,
2899  colnames, colvars);
2901 }
#define AccessShareLock
Definition: lockdefs.h:36
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
TupleDesc rd_att
Definition: rel.h:110
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
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)

◆ expandRTE()

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

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

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

◆ expandTupleDesc()

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 
)
static

Definition at line 2913 of file parse_relation.c.

References Assert, Alias::colnames, InvalidOid, label, lappend(), lfirst, list_length(), list_nth_cell(), lnext(), Var::location, makeNullConst(), makeString(), makeVar(), NameStr, pstrdup(), strVal, and TupleDescAttr.

Referenced by expandRelation(), and expandRTE().

2917 {
2918  ListCell *aliascell;
2919  int varattno;
2920 
2921  aliascell = (offset < list_length(eref->colnames)) ?
2922  list_nth_cell(eref->colnames, offset) : NULL;
2923 
2924  Assert(count <= tupdesc->natts);
2925  for (varattno = 0; varattno < count; varattno++)
2926  {
2927  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
2928 
2929  if (attr->attisdropped)
2930  {
2931  if (include_dropped)
2932  {
2933  if (colnames)
2934  *colnames = lappend(*colnames, makeString(pstrdup("")));
2935  if (colvars)
2936  {
2937  /*
2938  * can't use atttypid here, but it doesn't really matter
2939  * what type the Const claims to be.
2940  */
2941  *colvars = lappend(*colvars,
2942  makeNullConst(INT4OID, -1, InvalidOid));
2943  }
2944  }
2945  if (aliascell)
2946  aliascell = lnext(eref->colnames, aliascell);
2947  continue;
2948  }
2949 
2950  if (colnames)
2951  {
2952  char *label;
2953 
2954  if (aliascell)
2955  {
2956  label = strVal(lfirst(aliascell));
2957  aliascell = lnext(eref->colnames, aliascell);
2958  }
2959  else
2960  {
2961  /* If we run out of aliases, use the underlying name */
2962  label = NameStr(attr->attname);
2963  }
2964  *colnames = lappend(*colnames, makeString(pstrdup(label)));
2965  }
2966 
2967  if (colvars)
2968  {
2969  Var *varnode;
2970 
2971  varnode = makeVar(rtindex, varattno + offset + 1,
2972  attr->atttypid, attr->atttypmod,
2973  attr->attcollation,
2974  sublevels_up);
2975  varnode->location = location;
2976 
2977  *colvars = lappend(*colvars, varnode);
2978  }
2979  }
2980 }
Value * makeString(char *str)
Definition: value.c:53
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:310
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:244
List * colnames
Definition: primnodes.h:43
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1187
#define strVal(v)
Definition: value.h:54
Definition: primnodes.h:181
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:337
int location
Definition: primnodes.h:196
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
List * lappend(List *list, void *datum)
Definition: list.c:321
static char * label
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define NameStr(name)
Definition: c.h:623

◆ get_parse_rowmark()

RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3303 of file parse_relation.c.

References lfirst, Query::rowMarks, and RowMarkClause::rti.

Referenced by AcquireRewriteLocks(), applyLockingClause(), and ApplyRetrieveRule().

3304 {
3305  ListCell *l;
3306 
3307  foreach(l, qry->rowMarks)
3308  {
3309  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3310 
3311  if (rc->rti == rtindex)
3312  return rc;
3313  }
3314  return NULL;
3315 }
List * rowMarks
Definition: parsenodes.h:164
#define lfirst(lc)
Definition: pg_list.h:169

◆ get_rte_attribute_is_dropped()

bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3139 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, RangeTblEntry::coltypes, elog, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblFunction::funccolcount, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_tupdesc(), GETSTRUCT, HeapTupleIsValid, Int16GetDatum, RangeTblEntry::joinaliasvars, lfirst, list_length(), list_nth(), list_nth_oid(), ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), RangeTblEntry::relid, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2(), and TupleDescAttr.

Referenced by AcquireRewriteLocks().

3140 {
3141  bool result;
3142 
3143  switch (rte->rtekind)
3144  {
3145  case RTE_RELATION:
3146  {
3147  /*
3148  * Plain relation RTE --- get the attribute's catalog entry
3149  */
3150  HeapTuple tp;
3151  Form_pg_attribute att_tup;
3152 
3153  tp = SearchSysCache2(ATTNUM,
3154  ObjectIdGetDatum(rte->relid),
3156  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3157  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3158  attnum, rte->relid);
3159  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
3160  result = att_tup->attisdropped;
3161  ReleaseSysCache(tp);
3162  }
3163  break;
3164  case RTE_SUBQUERY:
3165  case RTE_TABLEFUNC:
3166  case RTE_VALUES:
3167  case RTE_CTE:
3168 
3169  /*
3170  * Subselect, Table Functions, Values, CTE RTEs never have dropped
3171  * columns
3172  */
3173  result = false;
3174  break;
3175  case RTE_NAMEDTUPLESTORE:
3176  {
3177  /* Check dropped-ness by testing for valid coltype */
3178  if (attnum <= 0 ||
3179  attnum > list_length(rte->coltypes))
3180  elog(ERROR, "invalid varattno %d", attnum);
3181  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3182  }
3183  break;
3184  case RTE_JOIN:
3185  {
3186  /*
3187  * A join RTE would not have dropped columns when constructed,
3188  * but one in a stored rule might contain columns that were
3189  * dropped from the underlying tables, if said columns are
3190  * nowhere explicitly referenced in the rule. This will be
3191  * signaled to us by a null pointer in the joinaliasvars list.
3192  */
3193  Var *aliasvar;
3194 
3195  if (attnum <= 0 ||
3197  elog(ERROR, "invalid varattno %d", attnum);
3198  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3199 
3200  result = (aliasvar == NULL);
3201  }
3202  break;
3203  case RTE_FUNCTION:
3204  {
3205  /* Function RTE */
3206  ListCell *lc;
3207  int atts_done = 0;
3208 
3209  /*
3210  * Dropped attributes are only possible with functions that
3211  * return named composite types. In such a case we have to
3212  * look up the result type to see if it currently has this
3213  * column dropped. So first, loop over the funcs until we
3214  * find the one that covers the requested column.
3215  */
3216  foreach(lc, rte->functions)
3217  {
3218  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3219 
3220  if (attnum > atts_done &&
3221  attnum <= atts_done + rtfunc->funccolcount)
3222  {
3223  TupleDesc tupdesc;
3224 
3225  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3226  true);
3227  if (tupdesc)
3228  {
3229  /* Composite data type, e.g. a table's row type */
3230  Form_pg_attribute att_tup;
3231 
3232  Assert(tupdesc);
3233  Assert(attnum - atts_done <= tupdesc->natts);
3234  att_tup = TupleDescAttr(tupdesc,
3235  attnum - atts_done - 1);
3236  return att_tup->attisdropped;
3237  }
3238  /* Otherwise, it can't have any dropped columns */
3239  return false;
3240  }
3241  atts_done += rtfunc->funccolcount;
3242  }
3243 
3244  /* If we get here, must be looking for the ordinality column */
3245  if (rte->funcordinality && attnum == atts_done + 1)
3246  return false;
3247 
3248  /* this probably can't happen ... */
3249  ereport(ERROR,
3250  (errcode(ERRCODE_UNDEFINED_COLUMN),
3251  errmsg("column %d of relation \"%s\" does not exist",
3252  attnum,
3253  rte->eref->aliasname)));
3254  result = false; /* keep compiler quiet */
3255  }
3256  break;
3257  case RTE_RESULT:
3258  /* this probably can't happen ... */
3259  ereport(ERROR,
3260  (errcode(ERRCODE_UNDEFINED_COLUMN),
3261  errmsg("column %d of relation \"%s\" does not exist",
3262  attnum,
3263  rte->eref->aliasname)));
3264  result = false; /* keep compiler quiet */
3265  break;
3266  default:
3267  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3268  result = false; /* keep compiler quiet */
3269  }
3270 
3271  return result;
3272 }
List * joinaliasvars
Definition: parsenodes.h:1052
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define Int16GetDatum(X)
Definition: postgres.h:451
int errcode(int sqlerrcode)
Definition: elog.c:610
bool funcordinality
Definition: parsenodes.h:1065
Definition: primnodes.h:181
#define OidIsValid(objectId)
Definition: c.h:652
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:288
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:438
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
int16 attnum
Definition: pg_attribute.h:79
#define ereport(elevel,...)
Definition: elog.h:144
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1064
static int list_length(const List *l)
Definition: pg_list.h:149
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127
RTEKind rtekind
Definition: parsenodes.h:977
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define elog(elevel,...)
Definition: elog.h:214
Alias * eref
Definition: parsenodes.h:1116
List * coltypes
Definition: parsenodes.h:1102

◆ get_rte_attribute_name()

char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3101 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, Alias::colnames, elog, RangeTblEntry::eref, ERROR, get_attname(), InvalidAttrNumber, list_length(), list_nth(), RangeTblEntry::relid, RTE_RELATION, RangeTblEntry::rtekind, and strVal.

Referenced by check_ungrouped_columns_walker(), get_name_for_var_field(), get_variable(), and print_expr().

3102 {
3103  if (attnum == InvalidAttrNumber)
3104  return "*";
3105 
3106  /*
3107  * If there is a user-written column alias, use it.
3108  */
3109  if (rte->alias &&
3110  attnum > 0 && attnum <= list_length(rte->alias->colnames))
3111  return strVal(list_nth(rte->alias->colnames, attnum - 1));
3112 
3113  /*
3114  * If the RTE is a relation, go to the system catalogs not the
3115  * eref->colnames list. This is a little slower but it will give the
3116  * right answer if the column has been renamed since the eref list was
3117  * built (which can easily happen for rules).
3118  */
3119  if (rte->rtekind == RTE_RELATION)
3120  return get_attname(rte->relid, attnum, false);
3121 
3122  /*
3123  * Otherwise use the column name from eref. There should always be one.
3124  */
3125  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3126  return strVal(list_nth(rte->eref->colnames, attnum - 1));
3127 
3128  /* else caller gave us a bogus attnum */
3129  elog(ERROR, "invalid attnum %d for rangetable entry %s",
3130  attnum, rte->eref->aliasname);
3131  return NULL; /* keep compiler quiet */
3132 }
Alias * alias
Definition: parsenodes.h:1115
List * colnames
Definition: primnodes.h:43
#define strVal(v)
Definition: value.h:54
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
int16 attnum
Definition: pg_attribute.h:79
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:977
#define elog(elevel,...)
Definition: elog.h:214
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:825
Alias * eref
Definition: parsenodes.h:1116

◆ get_tle_by_resno()

◆ 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:483
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
Index ctelevelsup
Definition: parsenodes.h:1081
RTEKind rtekind
Definition: parsenodes.h:977
char * ctename
Definition: parsenodes.h:1080
#define elog(elevel,...)
Definition: elog.h:214
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:746
#define lfirst(lc)
Definition: pg_list.h:169
#define elog(elevel,...)
Definition: elog.h:214

◆ 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:746
static int list_length(const List *l)
Definition: pg_list.h:149
List * p_rtable
Definition: parse_node.h:180

◆ isFutureCTE()

static bool isFutureCTE ( ParseState pstate,
const char *  refname 
)
static

Definition at line 293 of file parse_relation.c.

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

Referenced by parserOpenTable().

294 {
295  for (; pstate != NULL; pstate = pstate->parentParseState)
296  {
297  ListCell *lc;
298 
299  foreach(lc, pstate->p_future_ctes)
300  {
301  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
302 
303  if (strcmp(cte->ctename, refname) == 0)
304  return true;
305  }
306  }
307  return false;
308 }
ParseState * parentParseState
Definition: parse_node.h:178
List * p_future_ctes
Definition: parse_node.h:188
#define lfirst(lc)
Definition: pg_list.h:169

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

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

2453 {
2454  ListCell *l;
2455 
2456  /*
2457  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2458  * parent level, then act as though there's a generic FOR UPDATE here.
2459  */
2460  if (pstate->p_locked_from_parent)
2461  return true;
2462 
2463  foreach(l, pstate->p_locking_clause)
2464  {
2465  LockingClause *lc = (LockingClause *) lfirst(l);
2466 
2467  if (lc->lockedRels == NIL)
2468  {
2469  /* all tables used in query */
2470  return true;
2471  }
2472  else
2473  {
2474  /* just the named tables */
2475  ListCell *l2;
2476 
2477  foreach(l2, lc->lockedRels)
2478  {
2479  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2480 
2481  if (strcmp(refname, thisrel->relname) == 0)
2482  return true;
2483  }
2484  }
2485  }
2486  return false;
2487 }
List * lockedRels
Definition: parsenodes.h:751
#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:169

◆ isQueryUsingTempRelation()

bool isQueryUsingTempRelation ( Query query)

Definition at line 3571 of file parse_relation.c.

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

3572 {
3573  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3574 }
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
Definition: nodes.h:528

◆ isQueryUsingTempRelation_walker()

static bool isQueryUsingTempRelation_walker ( Node node,
void *  context 
)
static

Definition at line 3577 of file parse_relation.c.

References AccessShareLock, expression_tree_walker(), IsA, lfirst, QTW_IGNORE_JOINALIASES, query_tree_walker(), RelationData::rd_rel, RangeTblEntry::relid, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, table_close(), and table_open().

Referenced by isQueryUsingTempRelation().

3578 {
3579  if (node == NULL)
3580  return false;
3581 
3582  if (IsA(node, Query))
3583  {
3584  Query *query = (Query *) node;
3585  ListCell *rtable;
3586 
3587  foreach(rtable, query->rtable)
3588  {
3589  RangeTblEntry *rte = lfirst(rtable);
3590 
3591  if (rte->rtekind == RTE_RELATION)
3592  {
3594  char relpersistence = rel->rd_rel->relpersistence;
3595 
3597  if (relpersistence == RELPERSISTENCE_TEMP)
3598  return true;
3599  }
3600  }
3601 
3602  return query_tree_walker(query,
3604  context,
3606  }
3607 
3608  return expression_tree_walker(node,
3610  context);
3611 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2322
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define AccessShareLock
Definition: lockdefs.h:36
Form_pg_class rd_rel
Definition: rel.h:109
List * rtable
Definition: parsenodes.h:137
#define lfirst(lc)
Definition: pg_list.h:169
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1888
RTEKind rtekind
Definition: parsenodes.h:977
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23

◆ markRTEForSelectPriv()

static void markRTEForSelectPriv ( ParseState pstate,
RangeTblEntry rte,
int  rtindex,
AttrNumber  col 
)
static

Definition at line 1003 of file parse_relation.c.

References ACL_SELECT, bms_add_member(), elog, ERROR, FirstLowInvalidHeapAttributeNumber, InvalidAttrNumber, IsA, JoinExpr::larg, list_length(), list_nth_node, nodeTag, ParseState::p_joinexprs, ParseState::p_rtable, JoinExpr::rarg, RangeTblEntry::requiredPerms, rt_fetch, RTE_JOIN, RTE_RELATION, RangeTblEntry::rtekind, and RangeTblEntry::selectedCols.

Referenced by markVarForSelectPriv().

1005 {
1006  if (rte == NULL)
1007  rte = rt_fetch(rtindex, pstate->p_rtable);
1008 
1009  if (rte->rtekind == RTE_RELATION)
1010  {
1011  /* Make sure the rel as a whole is marked for SELECT access */
1012  rte->requiredPerms |= ACL_SELECT;
1013  /* Must offset the attnum to fit in a bitmapset */
1016  }
1017  else if (rte->rtekind == RTE_JOIN)
1018  {
1019  if (col == InvalidAttrNumber)
1020  {
1021  /*
1022  * A whole-row reference to a join has to be treated as whole-row
1023  * references to the two inputs.
1024  */
1025  JoinExpr *j;
1026 
1027  if (rtindex > 0 && rtindex <= list_length(pstate->p_joinexprs))
1028  j = list_nth_node(JoinExpr, pstate->p_joinexprs, rtindex - 1);
1029  else
1030  j = NULL;
1031  if (j == NULL)
1032  elog(ERROR, "could not find JoinExpr for whole-row reference");
1033 
1034  /* Note: we can't see FromExpr here */
1035  if (IsA(j->larg, RangeTblRef))
1036  {
1037  int varno = ((RangeTblRef *) j->larg)->rtindex;
1038 
1039  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
1040  }
1041  else if (IsA(j->larg, JoinExpr))
1042  {
1043  int varno = ((JoinExpr *) j->larg)->rtindex;
1044 
1045  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
1046  }
1047  else
1048  elog(ERROR, "unrecognized node type: %d",
1049  (int) nodeTag(j->larg));
1050  if (IsA(j->rarg, RangeTblRef))
1051  {
1052  int varno = ((RangeTblRef *) j->rarg)->rtindex;
1053 
1054  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
1055  }
1056  else if (IsA(j->rarg, JoinExpr))
1057  {
1058  int varno = ((JoinExpr *) j->rarg)->rtindex;
1059 
1060  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
1061  }
1062  else
1063  elog(ERROR, "unrecognized node type: %d",
1064  (int) nodeTag(j->rarg));
1065  }
1066  else
1067  {
1068  /*
1069  * Join alias Vars for ordinary columns must refer to merged JOIN
1070  * USING columns. We don't need to do anything here, because the
1071  * join input columns will also be referenced in the join's qual
1072  * clause, and will get marked for select privilege there.
1073  */
1074  }
1075  }
1076  /* other RTE types don't require privilege marking */
1077 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
AclMode requiredPerms
Definition: parsenodes.h:1120
Node * larg
Definition: primnodes.h:1493
Bitmapset * selectedCols
Definition: parsenodes.h:1122
#define ERROR
Definition: elog.h:43
#define list_nth_node(type, list, n)
Definition: pg_list.h:294
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
static void markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col)
#define ACL_SELECT
Definition: parsenodes.h:75
Node * rarg
Definition: primnodes.h:1494
static int list_length(const List *l)
Definition: pg_list.h:149
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define InvalidAttrNumber
Definition: attnum.h:23
#define nodeTag(nodeptr)
Definition: nodes.h:533
RTEKind rtekind
Definition: parsenodes.h:977
#define elog(elevel,...)
Definition: elog.h:214
List * p_joinexprs
Definition: parse_node.h:181
List * p_rtable
Definition: parse_node.h:180

◆ 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:579
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:483
#define Assert(condition)
Definition: c.h:746

◆ 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:1068
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:74
Relation table_openrv_extended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
Definition: table.c:132
static bool isFutureCTE(ParseState *pstate, const char *refname)
int errcode(int sqlerrcode)
Definition: elog.c:610
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:954
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:144
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:821

◆ 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:652
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1797
static ParseNamespaceItem * scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location)
#define InvalidOid
Definition: postgres_ext.h:36
static ParseNamespaceItem * scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
Oid LookupNamespaceNoError(const char *nspname)
Definition: namespace.c:2862

◆ scanNameSpaceForCTE()

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

Definition at line 262 of file parse_relation.c.

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

Referenced by getNSItemForSpecialRelationTypes(), and searchRangeTableForRel().

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

◆ scanNameSpaceForRefname()

static ParseNamespaceItem * scanNameSpaceForRefname ( ParseState pstate,
const char *  refname,
int  location 
)
static

Definition at line 179 of file parse_relation.c.

References Alias::aliasname, check_lateral_ref_ok(), RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, lfirst, ParseState::p_lateral_active, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_rte, and parser_errposition().

Referenced by refnameNamespaceItem().

180 {
181  ParseNamespaceItem *result = NULL;
182  ListCell *l;
183 
184  foreach(l, pstate->p_namespace)
185  {
187  RangeTblEntry *rte = nsitem->p_rte;
188 
189  /* Ignore columns-only items */
190  if (!nsitem->p_rel_visible)
191  continue;
192  /* If not inside LATERAL, ignore lateral-only items */
193  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
194  continue;
195 
196  if (strcmp(rte->eref->aliasname, refname) == 0)
197  {
198  if (result)
199  ereport(ERROR,
200  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
201  errmsg("table reference \"%s\" is ambiguous",
202  refname),
203  parser_errposition(pstate, location)));
204  check_lateral_ref_ok(pstate, nsitem, location);
205  result = nsitem;
206  }
207  }
208  return result;
209 }
int errcode(int sqlerrcode)
Definition: elog.c:610
RangeTblEntry * p_rte
Definition: parse_node.h:257
#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,...)
Definition: elog.h:144
bool p_lateral_active
Definition: parse_node.h:186
#define lfirst(lc)
Definition: pg_list.h:169
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:821
Alias * eref
Definition: parsenodes.h:1116

◆ scanNameSpaceForRelid()

static ParseNamespaceItem * scanNameSpaceForRelid ( ParseState pstate,
Oid  relid,
int  location 
)
static

Definition at line 220 of file parse_relation.c.

References RangeTblEntry::alias, check_lateral_ref_ok(), ereport, errcode(), errmsg(), ERROR, lfirst, ParseState::p_lateral_active, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_rte, parser_errposition(), RangeTblEntry::relid, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by refnameNamespaceItem().

221 {
222  ParseNamespaceItem *result = NULL;
223  ListCell *l;
224 
225  foreach(l, pstate->p_namespace)
226  {
228  RangeTblEntry *rte = nsitem->p_rte;
229 
230  /* Ignore columns-only items */
231  if (!nsitem->p_rel_visible)
232  continue;
233  /* If not inside LATERAL, ignore lateral-only items */
234  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
235  continue;
236 
237  /* yes, the test for alias == NULL should be there... */
238  if (rte->rtekind == RTE_RELATION &&
239  rte->relid == relid &&
240  rte->alias == NULL)
241  {
242  if (result)
243  ereport(ERROR,
244  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
245  errmsg("table reference %u is ambiguous",
246  relid),
247  parser_errposition(pstate, location)));
248  check_lateral_ref_ok(pstate, nsitem, location);
249  result = nsitem;
250  }
251  }
252  return result;
253 }
Alias * alias
Definition: parsenodes.h:1115
int errcode(int sqlerrcode)
Definition: elog.c:610
RangeTblEntry * p_rte
Definition: parse_node.h:257
#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,...)
Definition: elog.h:144
bool p_lateral_active
Definition: parse_node.h:186
#define lfirst(lc)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
RTEKind rtekind
Definition: parsenodes.h:977
int errmsg(const char *fmt,...)
Definition: elog.c:821

◆ 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:528
int errcode(int sqlerrcode)
Definition: elog.c:610
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
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:177
int16 attnum
Definition: pg_attribute.h:79
#define ereport(elevel,...)
Definition: elog.h:144
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:821
AttrNumber p_varattnosyn
Definition: parse_node.h:296
Alias * eref
Definition: parsenodes.h:1116

◆ scanRTEForColumn()

static int scanRTEForColumn ( ParseState pstate,
RangeTblEntry rte,
const char *  colname,
int  location,
int  fuzzy_rte_penalty,
FuzzyAttrMatchState fuzzystate 
)
static

Definition at line 767 of file parse_relation.c.