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, Alias *eref, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
 
static void markRTEForSelectPriv (ParseState *pstate, 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)
 
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 *join_using_alias, Alias *alias, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForCTE (ParseState *pstate, CommonTableExpr *cte, Index levelsup, RangeVar *rv, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForENR (ParseState *pstate, RangeVar *rv, bool inFromCl)
 
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, bool require_col_privs, 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.

Function Documentation

◆ addNSItemToQuery()

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

Definition at line 2571 of file parse_relation.c.

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

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

Referenced by AddRelationNewConstraints(), AlterPolicy(), CreatePolicy(), CreateTriggerFiringOn(), DefineRelation(), DoCopy(), setTargetTable(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformIndexStmt(), transformInsertStmt(), transformMergeStmt(), transformOnConflictClause(), transformPartitionSpec(), TransformPubWhereClauses(), transformRuleStmt(), transformSetOperationStmt(), transformStatsStmt(), and transformValuesClause().

◆ addRangeTableEntry()

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

Definition at line 1413 of file parse_relation.c.

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

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

Referenced by transformTableEntry().

◆ addRangeTableEntryForCTE()

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

Definition at line 2260 of file parse_relation.c.

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

References RangeTblEntry::alias, RangeVar::alias, Alias::aliasname, Assert(), buildNSItemFromLists(), RangeTblEntry::checkAsUser, CMD_SELECT, RangeTblEntry::colcollations, Alias::colnames, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, Query::commandType, copyObject, CommonTableExpr::ctecolcollations, CommonTableExpr::ctecolnames, CommonTableExpr::ctecoltypes, CommonTableExpr::ctecoltypmods, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, CommonTableExpr::ctename, CommonTableExpr::ctequery, CommonTableExpr::cterecursive, CommonTableExpr::cterefcount, CommonTableExpr::cycle_clause, CTECycleClause::cycle_mark_collation, CTECycleClause::cycle_mark_column, CTECycleClause::cycle_mark_type, CTECycleClause::cycle_mark_typmod, CTECycleClause::cycle_path_column, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::extraUpdatedCols, i, if(), RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, IsA, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, list_copy(), list_length(), RangeVar::location, makeAlias(), makeNode, makeString(), NIL, ParseNamespaceColumn::p_dontexpand, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseState::p_rtable, parser_errposition(), RangeTblEntry::requiredPerms, Query::returningList, RTE_CTE, RangeTblEntry::rtekind, CTESearchClause::search_breadth_first, CommonTableExpr::search_clause, CTESearchClause::search_seq_column, RangeTblEntry::selectedCols, RangeTblEntry::self_reference, and RangeTblEntry::updatedCols.

Referenced by getNSItemForSpecialRelationTypes().

◆ addRangeTableEntryForENR()

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

Definition at line 2418 of file parse_relation.c.

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

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

Referenced by getNSItemForSpecialRelationTypes().

◆ addRangeTableEntryForFunction()

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

Definition at line 1679 of file parse_relation.c.

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

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

Referenced by transformRangeFunction().

◆ addRangeTableEntryForJoin()

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

Definition at line 2156 of file parse_relation.c.

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

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

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

◆ addRangeTableEntryForRelation()

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

Definition at line 1513 of file parse_relation.c.

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

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

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

◆ addRangeTableEntryForSubquery()

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

Definition at line 1583 of file parse_relation.c.

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

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

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

◆ addRangeTableEntryForTableFunc()

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

Definition at line 1985 of file parse_relation.c.

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

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

Referenced by transformJsonTable(), and transformRangeTableFunc().

◆ addRangeTableEntryForValues()

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

Definition at line 2067 of file parse_relation.c.

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

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

Referenced by transformValuesClause().

◆ attnameAttNum()

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

Definition at line 3414 of file parse_relation.c.

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

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

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

◆ attnumAttName()

const NameData* attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3464 of file parse_relation.c.

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

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

Referenced by transformFkeyGetPrimaryKey().

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3506 of file parse_relation.c.

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

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

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3486 of file parse_relation.c.

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

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

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

◆ buildNSItemFromLists()

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

Definition at line 1298 of file parse_relation.c.

1300 {
1301  ParseNamespaceItem *nsitem;
1302  ParseNamespaceColumn *nscolumns;
1303  int maxattrs = list_length(coltypes);
1304  int varattno;
1305  ListCell *lct;
1306  ListCell *lcm;
1307  ListCell *lcc;
1308 
1309  /* colnames must have the same number of entries as the nsitem */
1310  Assert(maxattrs == list_length(rte->eref->colnames));
1311 
1312  Assert(maxattrs == list_length(coltypmods));
1313  Assert(maxattrs == list_length(colcollations));
1314 
1315  /* extract per-column data from the lists */
1316  nscolumns = (ParseNamespaceColumn *)
1317  palloc0(maxattrs * sizeof(ParseNamespaceColumn));
1318 
1319  varattno = 0;
1320  forthree(lct, coltypes,
1321  lcm, coltypmods,
1322  lcc, colcollations)
1323  {
1324  nscolumns[varattno].p_varno = rtindex;
1325  nscolumns[varattno].p_varattno = varattno + 1;
1326  nscolumns[varattno].p_vartype = lfirst_oid(lct);
1327  nscolumns[varattno].p_vartypmod = lfirst_int(lcm);
1328  nscolumns[varattno].p_varcollid = lfirst_oid(lcc);
1329  nscolumns[varattno].p_varnosyn = rtindex;
1330  nscolumns[varattno].p_varattnosyn = varattno + 1;
1331  varattno++;
1332  }
1333 
1334  /* ... and build the nsitem */
1335  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1336  nsitem->p_names = rte->eref;
1337  nsitem->p_rte = rte;
1338  nsitem->p_rtindex = rtindex;
1339  nsitem->p_nscolumns = nscolumns;
1340  /* set default visibility flags; might get changed later */
1341  nsitem->p_rel_visible = true;
1342  nsitem->p_cols_visible = true;
1343  nsitem->p_lateral_only = false;
1344  nsitem->p_lateral_ok = true;
1345 
1346  return nsitem;
1347 }
void * palloc0(Size size)
Definition: mcxt.c:1099
#define lfirst_int(lc)
Definition: pg_list.h:170
#define lfirst_oid(lc)
Definition: pg_list.h:171
AttrNumber p_varattno
Definition: parse_node.h:303
AttrNumber p_varattnosyn
Definition: parse_node.h:308

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

◆ buildNSItemFromTupleDesc()

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

Definition at line 1241 of file parse_relation.c.

1242 {
1243  ParseNamespaceItem *nsitem;
1244  ParseNamespaceColumn *nscolumns;
1245  int maxattrs = tupdesc->natts;
1246  int varattno;
1247 
1248  /* colnames must have the same number of entries as the nsitem */
1249  Assert(maxattrs == list_length(rte->eref->colnames));
1250 
1251  /* extract per-column data from the tupdesc */
1252  nscolumns = (ParseNamespaceColumn *)
1253  palloc0(maxattrs * sizeof(ParseNamespaceColumn));
1254 
1255  for (varattno = 0; varattno < maxattrs; varattno++)
1256  {
1257  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1258 
1259  /* For a dropped column, just leave the entry as zeroes */
1260  if (attr->attisdropped)
1261  continue;
1262 
1263  nscolumns[varattno].p_varno = rtindex;
1264  nscolumns[varattno].p_varattno = varattno + 1;
1265  nscolumns[varattno].p_vartype = attr->atttypid;
1266  nscolumns[varattno].p_vartypmod = attr->atttypmod;
1267  nscolumns[varattno].p_varcollid = attr->attcollation;
1268  nscolumns[varattno].p_varnosyn = rtindex;
1269  nscolumns[varattno].p_varattnosyn = varattno + 1;
1270  }
1271 
1272  /* ... and build the nsitem */
1273  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1274  nsitem->p_names = rte->eref;
1275  nsitem->p_rte = rte;
1276  nsitem->p_rtindex = rtindex;
1277  nsitem->p_nscolumns = nscolumns;
1278  /* set default visibility flags; might get changed later */
1279  nsitem->p_rel_visible = true;
1280  nsitem->p_cols_visible = true;
1281  nsitem->p_lateral_only = false;
1282  nsitem->p_lateral_ok = true;
1283 
1284  return nsitem;
1285 }

References Assert(), Alias::colnames, RangeTblEntry::eref, list_length(), TupleDescData::natts, ParseNamespaceItem::p_cols_visible, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseNamespaceItem::p_names, 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().

◆ buildRelationAliases()

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

Definition at line 1125 of file parse_relation.c.

1126 {
1127  int maxattrs = tupdesc->natts;
1128  List *aliaslist;
1129  ListCell *aliaslc;
1130  int numaliases;
1131  int varattno;
1132  int numdropped = 0;
1133 
1134  Assert(eref->colnames == NIL);
1135 
1136  if (alias)
1137  {
1138  aliaslist = alias->colnames;
1139  aliaslc = list_head(aliaslist);
1140  numaliases = list_length(aliaslist);
1141  /* We'll rebuild the alias colname list */
1142  alias->colnames = NIL;
1143  }
1144  else
1145  {
1146  aliaslist = NIL;
1147  aliaslc = NULL;
1148  numaliases = 0;
1149  }
1150 
1151  for (varattno = 0; varattno < maxattrs; varattno++)
1152  {
1153  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1154  String *attrname;
1155 
1156  if (attr->attisdropped)
1157  {
1158  /* Always insert an empty string for a dropped column */
1159  attrname = makeString(pstrdup(""));
1160  if (aliaslc)
1161  alias->colnames = lappend(alias->colnames, attrname);
1162  numdropped++;
1163  }
1164  else if (aliaslc)
1165  {
1166  /* Use the next user-supplied alias */
1167  attrname = lfirst_node(String, aliaslc);
1168  aliaslc = lnext(aliaslist, aliaslc);
1169  alias->colnames = lappend(alias->colnames, attrname);
1170  }
1171  else
1172  {
1173  attrname = makeString(pstrdup(NameStr(attr->attname)));
1174  /* we're done with the alias if any */
1175  }
1176 
1177  eref->colnames = lappend(eref->colnames, attrname);
1178  }
1179 
1180  /* Too many user-supplied aliases? */
1181  if (aliaslc)
1182  ereport(ERROR,
1183  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1184  errmsg("table \"%s\" has %d columns available but %d columns specified",
1185  eref->aliasname, maxattrs - numdropped, numaliases)));
1186 }
#define NameStr(name)
Definition: c.h:681
#define lfirst_node(type, lc)
Definition: pg_list.h:172
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
Definition: value.h:58

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

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

◆ check_lateral_ref_ok()

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

Definition at line 463 of file parse_relation.c.

465 {
466  if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
467  {
468  /* SQL:2008 demands this be an error, not an invisible item */
469  RangeTblEntry *rte = nsitem->p_rte;
470  char *refname = nsitem->p_names->aliasname;
471 
472  ereport(ERROR,
473  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
474  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
475  refname),
476  (pstate->p_target_nsitem != NULL &&
477  rte == pstate->p_target_nsitem->p_rte) ?
478  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
479  refname) :
480  errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
481  parser_errposition(pstate, location)));
482  }
483 }
int errdetail(const char *fmt,...)
Definition: elog.c:1037
int errhint(const char *fmt,...)
Definition: elog.c:1151
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:194

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

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

◆ checkNameSpaceConflicts()

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

Definition at line 414 of file parse_relation.c.

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

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

Referenced by transformFromClause(), and transformFromClauseItem().

◆ chooseScalarFunctionAlias()

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

Definition at line 1202 of file parse_relation.c.

1204 {
1205  char *pname;
1206 
1207  /*
1208  * If the expression is a simple function call, and the function has a
1209  * single OUT parameter that is named, use the parameter's name.
1210  */
1211  if (funcexpr && IsA(funcexpr, FuncExpr))
1212  {
1213  pname = get_func_result_name(((FuncExpr *) funcexpr)->funcid);
1214  if (pname)
1215  return pname;
1216  }
1217 
1218  /*
1219  * If there's just one function in the RTE, and the user gave an RTE alias
1220  * name, use that name. (This makes FROM func() AS foo use "foo" as the
1221  * column name as well as the table alias.)
1222  */
1223  if (nfuncs == 1 && alias)
1224  return alias->aliasname;
1225 
1226  /*
1227  * Otherwise use the function name.
1228  */
1229  return funcname;
1230 }
char * get_func_result_name(Oid functionId)
Definition: funcapi.c:1567

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

Referenced by addRangeTableEntryForFunction().

◆ colNameToVar()

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

Definition at line 868 of file parse_relation.c.

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

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

Referenced by findTargetlistEntrySQL92(), and transformColumnRef().

◆ errorMissingColumn()

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

Definition at line 3586 of file parse_relation.c.

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

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

Referenced by transformColumnRef().

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3525 of file parse_relation.c.

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

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

Referenced by ExpandColumnRefStar(), and transformColumnRef().

◆ expandNSItemAttrs()

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

Definition at line 3129 of file parse_relation.c.

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

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

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

◆ expandNSItemVars()

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

Definition at line 3069 of file parse_relation.c.

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

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

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

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

2966 {
2967  Relation rel;
2968 
2969  /* Get the tupledesc and turn it over to expandTupleDesc */
2970  rel = relation_open(relid, AccessShareLock);
2971  expandTupleDesc(rel->rd_att, eref, rel->rd_att->natts, 0,
2972  rtindex, sublevels_up,
2973  location, include_dropped,
2974  colnames, colvars);
2976 }
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)
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48

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

Referenced by expandRTE().

◆ expandRTE()

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

Definition at line 2611 of file parse_relation.c.

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

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

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

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

2992 {
2993  ListCell *aliascell;
2994  int varattno;
2995 
2996  aliascell = (offset < list_length(eref->colnames)) ?
2997  list_nth_cell(eref->colnames, offset) : NULL;
2998 
2999  Assert(count <= tupdesc->natts);
3000  for (varattno = 0; varattno < count; varattno++)
3001  {
3002  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
3003 
3004  if (attr->attisdropped)
3005  {
3006  if (include_dropped)
3007  {
3008  if (colnames)
3009  *colnames = lappend(*colnames, makeString(pstrdup("")));
3010  if (colvars)
3011  {
3012  /*
3013  * can't use atttypid here, but it doesn't really matter
3014  * what type the Const claims to be.
3015  */
3016  *colvars = lappend(*colvars,
3017  makeNullConst(INT4OID, -1, InvalidOid));
3018  }
3019  }
3020  if (aliascell)
3021  aliascell = lnext(eref->colnames, aliascell);
3022  continue;
3023  }
3024 
3025  if (colnames)
3026  {
3027  char *label;
3028 
3029  if (aliascell)
3030  {
3031  label = strVal(lfirst(aliascell));
3032  aliascell = lnext(eref->colnames, aliascell);
3033  }
3034  else
3035  {
3036  /* If we run out of aliases, use the underlying name */
3037  label = NameStr(attr->attname);
3038  }
3039  *colnames = lappend(*colnames, makeString(pstrdup(label)));
3040  }
3041 
3042  if (colvars)
3043  {
3044  Var *varnode;
3045 
3046  varnode = makeVar(rtindex, varattno + offset + 1,
3047  attr->atttypid, attr->atttypmod,
3048  attr->attcollation,
3049  sublevels_up);
3050  varnode->location = location;
3051 
3052  *colvars = lappend(*colvars, varnode);
3053  }
3054  }
3055 }
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:256

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

◆ get_parse_rowmark()

RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3390 of file parse_relation.c.

3391 {
3392  ListCell *l;
3393 
3394  foreach(l, qry->rowMarks)
3395  {
3396  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3397 
3398  if (rc->rti == rtindex)
3399  return rc;
3400  }
3401  return NULL;
3402 }
List * rowMarks
Definition: parsenodes.h:180

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

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

◆ get_rte_attribute_is_dropped()

bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3226 of file parse_relation.c.

3227 {
3228  bool result;
3229 
3230  switch (rte->rtekind)
3231  {
3232  case RTE_RELATION:
3233  {
3234  /*
3235  * Plain relation RTE --- get the attribute's catalog entry
3236  */
3237  HeapTuple tp;
3238  Form_pg_attribute att_tup;
3239 
3240  tp = SearchSysCache2(ATTNUM,
3241  ObjectIdGetDatum(rte->relid),
3243  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3244  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3245  attnum, rte->relid);
3246  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
3247  result = att_tup->attisdropped;
3248  ReleaseSysCache(tp);
3249  }
3250  break;
3251  case RTE_SUBQUERY:
3252  case RTE_TABLEFUNC:
3253  case RTE_VALUES:
3254  case RTE_CTE:
3255 
3256  /*
3257  * Subselect, Table Functions, Values, CTE RTEs never have dropped
3258  * columns
3259  */
3260  result = false;
3261  break;
3262  case RTE_NAMEDTUPLESTORE:
3263  {
3264  /* Check dropped-ness by testing for valid coltype */
3265  if (attnum <= 0 ||
3266  attnum > list_length(rte->coltypes))
3267  elog(ERROR, "invalid varattno %d", attnum);
3268  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3269  }
3270  break;
3271  case RTE_JOIN:
3272  {
3273  /*
3274  * A join RTE would not have dropped columns when constructed,
3275  * but one in a stored rule might contain columns that were
3276  * dropped from the underlying tables, if said columns are
3277  * nowhere explicitly referenced in the rule. This will be
3278  * signaled to us by a null pointer in the joinaliasvars list.
3279  */
3280  Var *aliasvar;
3281 
3282  if (attnum <= 0 ||
3284  elog(ERROR, "invalid varattno %d", attnum);
3285  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3286 
3287  result = (aliasvar == NULL);
3288  }
3289  break;
3290  case RTE_FUNCTION:
3291  {
3292  /* Function RTE */
3293  ListCell *lc;
3294  int atts_done = 0;
3295 
3296  /*
3297  * Dropped attributes are only possible with functions that
3298  * return named composite types. In such a case we have to
3299  * look up the result type to see if it currently has this
3300  * column dropped. So first, loop over the funcs until we
3301  * find the one that covers the requested column.
3302  */
3303  foreach(lc, rte->functions)
3304  {
3305  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3306 
3307  if (attnum > atts_done &&
3308  attnum <= atts_done + rtfunc->funccolcount)
3309  {
3310  TupleDesc tupdesc;
3311 
3312  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3313  true);
3314  if (tupdesc)
3315  {
3316  /* Composite data type, e.g. a table's row type */
3317  Form_pg_attribute att_tup;
3318 
3319  Assert(tupdesc);
3320  Assert(attnum - atts_done <= tupdesc->natts);
3321  att_tup = TupleDescAttr(tupdesc,
3322  attnum - atts_done - 1);
3323  return att_tup->attisdropped;
3324  }
3325  /* Otherwise, it can't have any dropped columns */
3326  return false;
3327  }
3328  atts_done += rtfunc->funccolcount;
3329  }
3330 
3331  /* If we get here, must be looking for the ordinality column */
3332  if (rte->funcordinality && attnum == atts_done + 1)
3333  return false;
3334 
3335  /* this probably can't happen ... */
3336  ereport(ERROR,
3337  (errcode(ERRCODE_UNDEFINED_COLUMN),
3338  errmsg("column %d of relation \"%s\" does not exist",
3339  attnum,
3340  rte->eref->aliasname)));
3341  result = false; /* keep compiler quiet */
3342  }
3343  break;
3344  case RTE_RESULT:
3345  /* this probably can't happen ... */
3346  ereport(ERROR,
3347  (errcode(ERRCODE_UNDEFINED_COLUMN),
3348  errmsg("column %d of relation \"%s\" does not exist",
3349  attnum,
3350  rte->eref->aliasname)));
3351  result = false; /* keep compiler quiet */
3352  break;
3353  default:
3354  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3355  result = false; /* keep compiler quiet */
3356  }
3357 
3358  return result;
3359 }
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:509
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:300
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define Int16GetDatum(X)
Definition: postgres.h:495
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1184
@ ATTNUM
Definition: syscache.h:41

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

◆ get_rte_attribute_name()

char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3188 of file parse_relation.c.

3189 {
3190  if (attnum == InvalidAttrNumber)
3191  return "*";
3192 
3193  /*
3194  * If there is a user-written column alias, use it.
3195  */
3196  if (rte->alias &&
3197  attnum > 0 && attnum <= list_length(rte->alias->colnames))
3198  return strVal(list_nth(rte->alias->colnames, attnum - 1));
3199 
3200  /*
3201  * If the RTE is a relation, go to the system catalogs not the
3202  * eref->colnames list. This is a little slower but it will give the
3203  * right answer if the column has been renamed since the eref list was
3204  * built (which can easily happen for rules).
3205  */
3206  if (rte->rtekind == RTE_RELATION)
3207  return get_attname(rte->relid, attnum, false);
3208 
3209  /*
3210  * Otherwise use the column name from eref. There should always be one.
3211  */
3212  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3213  return strVal(list_nth(rte->eref->colnames, attnum - 1));
3214 
3215  /* else caller gave us a bogus attnum */
3216  elog(ERROR, "invalid attnum %d for rangetable entry %s",
3217  attnum, rte->eref->aliasname);
3218  return NULL; /* keep compiler quiet */
3219 }
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:825

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

◆ get_tle_by_resno()

◆ GetCTEForRTE()

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

Definition at line 537 of file parse_relation.c.

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

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

Referenced by expandRecordVariable(), and markTargetListOrigin().

◆ GetNSItemByRangeTablePosn()

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

Definition at line 490 of file parse_relation.c.

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

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

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

◆ GetRTEByRangeTablePosn()

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

Definition at line 517 of file parse_relation.c.

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

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

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

◆ isFutureCTE()

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

Definition at line 293 of file parse_relation.c.

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 }
List * p_future_ctes
Definition: parse_node.h:191

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

Referenced by parserOpenTable().

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

Definition at line 2527 of file parse_relation.c.

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

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

Referenced by addRangeTableEntry(), and transformRangeSubselect().

◆ isQueryUsingTempRelation()

bool isQueryUsingTempRelation ( Query query)

Definition at line 3658 of file parse_relation.c.

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

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

◆ isQueryUsingTempRelation_walker()

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

Definition at line 3664 of file parse_relation.c.

3665 {
3666  if (node == NULL)
3667  return false;
3668 
3669  if (IsA(node, Query))
3670  {
3671  Query *query = (Query *) node;
3672  ListCell *rtable;
3673 
3674  foreach(rtable, query->rtable)
3675  {
3676  RangeTblEntry *rte = lfirst(rtable);
3677 
3678  if (rte->rtekind == RTE_RELATION)
3679  {
3681  char relpersistence = rel->rd_rel->relpersistence;
3682 
3684  if (relpersistence == RELPERSISTENCE_TEMP)
3685  return true;
3686  }
3687  }
3688 
3689  return query_tree_walker(query,
3691  context,
3693  }
3694 
3695  return expression_tree_walker(node,
3697  context);
3698 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2570
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:2015
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23
List * rtable
Definition: parsenodes.h:148
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

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

◆ markRTEForSelectPriv()

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

Definition at line 1018 of file parse_relation.c.

1019 {
1020  RangeTblEntry *rte = rt_fetch(rtindex, pstate->p_rtable);
1021 
1022  if (rte->rtekind == RTE_RELATION)
1023  {
1024  /* Make sure the rel as a whole is marked for SELECT access */
1025  rte->requiredPerms |= ACL_SELECT;
1026  /* Must offset the attnum to fit in a bitmapset */
1029  }
1030  else if (rte->rtekind == RTE_JOIN)
1031  {
1032  if (col == InvalidAttrNumber)
1033  {
1034  /*
1035  * A whole-row reference to a join has to be treated as whole-row
1036  * references to the two inputs.
1037  */
1038  JoinExpr *j;
1039 
1040  if (rtindex > 0 && rtindex <= list_length(pstate->p_joinexprs))
1041  j = list_nth_node(JoinExpr, pstate->p_joinexprs, rtindex - 1);
1042  else
1043  j = NULL;
1044  if (j == NULL)
1045  elog(ERROR, "could not find JoinExpr for whole-row reference");
1046 
1047  /* Note: we can't see FromExpr here */
1048  if (IsA(j->larg, RangeTblRef))
1049  {
1050  int varno = ((RangeTblRef *) j->larg)->rtindex;
1051 
1052  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1053  }
1054  else if (IsA(j->larg, JoinExpr))
1055  {
1056  int varno = ((JoinExpr *) j->larg)->rtindex;
1057 
1058  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1059  }
1060  else
1061  elog(ERROR, "unrecognized node type: %d",
1062  (int) nodeTag(j->larg));
1063  if (IsA(j->rarg, RangeTblRef))
1064  {
1065  int varno = ((RangeTblRef *) j->rarg)->rtindex;
1066 
1067  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1068  }
1069  else if (IsA(j->rarg, JoinExpr))
1070  {
1071  int varno = ((JoinExpr *) j->rarg)->rtindex;
1072 
1073  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1074  }
1075  else
1076  elog(ERROR, "unrecognized node type: %d",
1077  (int) nodeTag(j->rarg));
1078  }
1079  else
1080  {
1081  /*
1082  * Join alias Vars for ordinary columns must refer to merged JOIN
1083  * USING columns. We don't need to do anything here, because the
1084  * join input columns will also be referenced in the join's qual
1085  * clause, and will get marked for select privilege there.
1086  */
1087  }
1088  }
1089  /* other RTE types don't require privilege marking */
1090 }
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:738
#define nodeTag(nodeptr)
Definition: nodes.h:578
static void markRTEForSelectPriv(ParseState *pstate, int rtindex, AttrNumber col)
#define list_nth_node(type, list, n)
Definition: pg_list.h:306
List * p_joinexprs
Definition: parse_node.h:184
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

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

Referenced by markVarForSelectPriv().

◆ markVarForSelectPriv()

void markVarForSelectPriv ( ParseState pstate,
Var var 
)

Definition at line 1098 of file parse_relation.c.

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

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

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

◆ parserOpenTable()

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

Definition at line 1361 of file parse_relation.c.

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

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

Referenced by addRangeTableEntry(), and setTargetTable().

◆ refnameNamespaceItem()

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

Definition at line 109 of file parse_relation.c.

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

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

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

◆ scanNameSpaceForCTE()

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

Definition at line 262 of file parse_relation.c.

264 {
265  Index levelsup;
266 
267  for (levelsup = 0;
268  pstate != NULL;
269  pstate = pstate->parentParseState, levelsup++)
270  {
271  ListCell *lc;
272 
273  foreach(lc, pstate->p_ctenamespace)
274  {
275  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
276 
277  if (strcmp(cte->ctename, refname) == 0)
278  {
279  *ctelevelsup = levelsup;
280  return cte;
281  }
282  }
283  }
284  return NULL;
285 }

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

Referenced by getNSItemForSpecialRelationTypes(), and searchRangeTableForRel().

◆ scanNameSpaceForENR()

bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 315 of file parse_relation.c.

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

References name_matches_visible_ENR().

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

◆ scanNameSpaceForRefname()

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

Definition at line 180 of file parse_relation.c.

181 {
182  ParseNamespaceItem *result = NULL;
183  ListCell *l;
184 
185  foreach(l, pstate->p_namespace)
186  {
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(nsitem->p_names->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 }

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

Referenced by refnameNamespaceItem().

◆ scanNameSpaceForRelid()

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

Definition at line 220 of file parse_relation.c.

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 }

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

◆ scanNSItemForColumn()

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

Definition at line 668 of file parse_relation.c.

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

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

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

◆ scanRTEForColumn()

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

Definition at line 785 of file parse_relation.c.

790 {
791  int result = InvalidAttrNumber;
792  int attnum = 0;
793  ListCell *c;
794 
795  /*
796  * Scan the user column names (or aliases) for a match. Complain if
797  * multiple matches.
798  *
799  * Note: eref->colnames may include entries for dropped columns, but those
800  * will be empty strings that cannot match any legal SQL identifier, so we
801  * don't bother to test for that case here.
802  *
803  * Should this somehow go wrong and we try to access a dropped column,
804  * we'll still catch it by virtue of the check in scanNSItemForColumn().
805  * Callers interested in finding match with shortest distance need to
806  * defend against this directly, though.
807  */
808  foreach(c, eref->colnames)
809  {
810  const char *attcolname = strVal(lfirst(c));
811 
812  attnum++;
813  if (strcmp(attcolname, colname) == 0)
814  {
815  if (result)
816  ereport(ERROR,
817  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
818  errmsg("column reference \"%s\" is ambiguous",
819  colname),
820  parser_errposition(pstate, location)));
821  result = attnum;
822  }
823 
824  /* Update fuzzy match state, if provided. */
825  if (fuzzystate != NULL)
826  updateFuzzyAttrMatchState(fuzzy_rte_penalty, fuzzystate,
827  rte, attcolname, colname, attnum);
828  }
829 
830  /*
831  * If we have a unique match, return it. Note that this allows a user
832  * alias to override a system column name (such as OID) without error.
833  */
834  if (result)
835  return result;
836 
837  /*
838  * If the RTE represents a real relation, consider system column names.
839  * Composites are only used for pseudo-relations like ON CONFLICT's
840  * excluded.
841  */
842  if (rte->rtekind == RTE_RELATION &&
843  rte->relkind != RELKIND_COMPOSITE_TYPE)
844  {
845  /* quick check to see if name could be a system column */
846  attnum = specialAttNum(colname);
847  if (attnum != InvalidAttrNumber)
848  {
849  /* now check to see if column actually is defined */
851  ObjectIdGetDatum(rte->relid),
853  result = attnum;
854  }
855  }
856 
857  return result;
858 }
static void updateFuzzyAttrMatchState(int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate, RangeTblEntry *rte, const char *actual, const char *match, int attnum)
char * c
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:190

References attnum, ATTNUM, Alias::colnames, ereport, errcode(), errmsg(), ERROR, Int16GetDatum, InvalidAttrNumber, lfirst, ObjectIdGetDatum, parser_errposition(), RangeTblEntry::relid, RangeTblEntry::relkind, RTE_RELATION, RangeTblEntry::rtekind, SearchSysCacheExists2, specialAttNum(), strVal, and updateFuzzyAttrMatchState().

Referenced by scanNSItemForColumn(), and searchRangeTableForCol().

◆ searchRangeTableForCol()

static FuzzyAttrMatchState* searchRangeTableForCol ( ParseState pstate,
const char *  alias,
const char *  colname,
int  location 
)
static

Definition at line 943 of file parse_relation.c.

945 {
946  ParseState *orig_pstate = pstate;
947  FuzzyAttrMatchState *fuzzystate = palloc(sizeof(FuzzyAttrMatchState));
948 
949  fuzzystate->distance = MAX_FUZZY_DISTANCE + 1;
950  fuzzystate->rfirst = NULL;
951  fuzzystate->rsecond = NULL;
952  fuzzystate->first = InvalidAttrNumber;
953  fuzzystate->second = InvalidAttrNumber;
954 
955  while (pstate != NULL)
956  {
957  ListCell *l;
958 
959  foreach(l, pstate->p_rtable)
960  {
961  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
962  int fuzzy_rte_penalty = 0;
963 
964  /*
965  * Typically, it is not useful to look for matches within join
966  * RTEs; they effectively duplicate other RTEs for our purposes,
967  * and if a match is chosen from a join RTE, an unhelpful alias is
968  * displayed in the final diagnostic message.
969  */
970  if (rte->rtekind == RTE_JOIN)
971  continue;
972 
973  /*
974  * If the user didn't specify an alias, then matches against one
975  * RTE are as good as another. But if the user did specify an
976  * alias, then we want at least a fuzzy - and preferably an exact
977  * - match for the range table entry.
978  */
979  if (alias != NULL)
980  fuzzy_rte_penalty =
981  varstr_levenshtein_less_equal(alias, strlen(alias),
982  rte->eref->aliasname,
983  strlen(rte->eref->aliasname),
984  1, 1, 1,
985  MAX_FUZZY_DISTANCE + 1,
986  true);
987 
988  /*
989  * Scan for a matching column; if we find an exact match, we're
990  * done. Otherwise, update fuzzystate.
991  */
992  if (scanRTEForColumn(orig_pstate, rte, rte->eref, colname, location,
993  fuzzy_rte_penalty, fuzzystate)
994  && fuzzy_rte_penalty == 0)
995  {
996  fuzzystate->rfirst = rte;
997  fuzzystate->first = InvalidAttrNumber;
998  fuzzystate->rsecond = NULL;
999  fuzzystate->second = InvalidAttrNumber;
1000  return fuzzystate;
1001  }
1002  }
1003 
1004  pstate = pstate->parentParseState;
1005  }
1006 
1007  return fuzzystate;
1008 }
#define MAX_FUZZY_DISTANCE
RangeTblEntry * rfirst
RangeTblEntry * rsecond
int varstr_levenshtein_less_equal(const char *source, int slen, const char *target, int tlen, int ins_c, int del_c, int sub_c, int max_d, bool trusted)

References Alias::aliasname, FuzzyAttrMatchState::distance, RangeTblEntry::eref, FuzzyAttrMatchState::first, InvalidAttrNumber, lfirst, MAX_FUZZY_DISTANCE, ParseState::p_rtable, palloc(), ParseState::parentParseState, FuzzyAttrMatchState::rfirst, FuzzyAttrMatchState::rsecond, RTE_JOIN, RangeTblEntry::rtekind, scanRTEForColumn(), FuzzyAttrMatchState::second, and varstr_levenshtein_less_equal().

Referenced by errorMissingColumn().

◆ searchRangeTableForRel()

static RangeTblEntry* searchRangeTableForRel ( ParseState pstate,
RangeVar relation 
)
static

Definition at line 336 of file parse_relation.c.

337 {
338  const char *refname = relation->relname;
339  Oid relId = InvalidOid;
340  CommonTableExpr *cte = NULL;
341  bool isenr = false;
342  Index ctelevelsup = 0;
343  Index levelsup;
344 
345  /*
346  * If it's an unqualified name, check for possible CTE matches. A CTE
347  * hides any real relation matches. If no CTE, look for a matching
348  * relation.
349  *
350  * NB: It's not critical that RangeVarGetRelid return the correct answer
351  * here in the face of concurrent DDL. If it doesn't, the worst case
352  * scenario is a less-clear error message. Also, the tables involved in
353  * the query are already locked, which reduces the number of cases in
354  * which surprising behavior can occur. So we do the name lookup
355  * unlocked.
356  */
357  if (!relation->schemaname)
358  {
359  cte = scanNameSpaceForCTE(pstate, refname, &ctelevelsup);
360  if (!cte)
361  isenr = scanNameSpaceForENR(pstate, refname);
362  }
363 
364  if (!cte && !isenr)
365  relId = RangeVarGetRelid(relation, NoLock, true);
366 
367  /* Now look for RTEs matching either the relation/CTE/ENR or the alias */
368  for (levelsup = 0;
369  pstate != NULL;
370  pstate = pstate->parentParseState, levelsup++)
371  {
372  ListCell *l;
373 
374  foreach(l, pstate->p_rtable)
375  {
376  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
377 
378  if (rte->rtekind == RTE_RELATION &&
379  OidIsValid(relId) &&
380  rte->relid == relId)
381  return rte;
382  if (rte->rtekind == RTE_CTE &&
383  cte != NULL &&
384  rte->ctelevelsup + levelsup == ctelevelsup &&
385  strcmp(rte->ctename, refname) == 0)
386  return rte;
387  if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
388  isenr &&
389  strcmp(rte->enrname, refname) == 0)
390  return rte;
391  if (strcmp(rte->eref->aliasname, refname) == 0)
392  return rte;
393  }
394  }
395  return NULL;
396 }
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:79
CommonTableExpr * scanNameSpaceForCTE(ParseState *pstate, const char *refname, Index *ctelevelsup)
bool scanNameSpaceForENR(ParseState *pstate, const char *refname)

References Alias::aliasname, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, RangeTblEntry::enrname, RangeTblEntry::eref, InvalidOid, lfirst, NoLock, OidIsValid, ParseState::p_rtable, ParseState::parentParseState, RangeVarGetRelid, RangeTblEntry::relid, RangeVar::relname, RTE_CTE, RTE_NAMEDTUPLESTORE, RTE_RELATION, RangeTblEntry::rtekind, scanNameSpaceForCTE(), scanNameSpaceForENR(), and RangeVar::schemaname.

Referenced by errorMissingRTE().

◆ specialAttNum()

static int specialAttNum ( const char *  attname)
static

Definition at line 3445 of file parse_relation.c.

3446 {
3447  const FormData_pg_attribute *sysatt;
3448 
3449  sysatt = SystemAttributeByName(attname);
3450  if (sysatt != NULL)
3451  return sysatt->attnum;
3452  return InvalidAttrNumber;
3453 }
const FormData_pg_attribute * SystemAttributeByName(const char *attname)
Definition: heap.c:251

References attname, FormData_pg_attribute, InvalidAttrNumber, and SystemAttributeByName().

Referenced by attnameAttNum(), and scanRTEForColumn().

◆ updateFuzzyAttrMatchState()

static void updateFuzzyAttrMatchState ( int  fuzzy_rte_penalty,
FuzzyAttrMatchState fuzzystate,
RangeTblEntry rte,
const char *  actual,
const char *  match,
int  attnum 
)
static

Definition at line 567 of file parse_relation.c.

570 {
571  int columndistance;
572  int matchlen;
573 
574  /* Bail before computing the Levenshtein distance if there's no hope. */
575  if (fuzzy_rte_penalty > fuzzystate->distance)
576  return;
577 
578  /*
579  * Outright reject dropped columns, which can appear here with apparent
580  * empty actual names, per remarks within scanRTEForColumn().
581  */
582  if (actual[0] == '\0')
583  return;
584 
585  /* Use Levenshtein to compute match distance. */
586  matchlen = strlen(match);
587  columndistance =
588  varstr_levenshtein_less_equal(actual, strlen(actual), match, matchlen,
589  1, 1, 1,
590  fuzzystate->distance + 1
591  - fuzzy_rte_penalty,
592  true);
593 
594  /*
595  * If more than half the characters are different, don't treat it as a
596  * match, to avoid making ridiculous suggestions.
597  */
598  if (columndistance > matchlen / 2)
599  return;
600 
601  /*
602  * From this point on, we can ignore the distinction between the RTE-name
603  * distance and the column-name distance.
604  */
605  columndistance += fuzzy_rte_penalty;
606 
607  /*
608  * If the new distance is less than or equal to that of the best match
609  * found so far, update fuzzystate.
610  */
611  if (columndistance < fuzzystate->distance)
612  {
613  /* Store new lowest observed distance for RTE */
614  fuzzystate->distance = columndistance;
615  fuzzystate->rfirst = rte;
616  fuzzystate->first = attnum;
617  fuzzystate->rsecond = NULL;
618  fuzzystate->second = InvalidAttrNumber;
619  }
620  else if (columndistance == fuzzystate->distance)
621  {
622  /*
623  * This match distance may equal a prior match within this same range
624  * table. When that happens, the prior match may also be given, but
625  * only if there is no more than two equally distant matches from the
626  * RTE (in turn, our caller will only accept two equally distant
627  * matches overall).
628  */
629  if (AttributeNumberIsValid(fuzzystate->second))
630  {
631  /* Too many RTE-level matches */
632  fuzzystate->rfirst = NULL;
633  fuzzystate->first = InvalidAttrNumber;
634  fuzzystate->rsecond = NULL;
635  fuzzystate->second = InvalidAttrNumber;
636  /* Clearly, distance is too low a bar (for *any* RTE) */
637  fuzzystate->distance = columndistance - 1;
638  }
639  else if (AttributeNumberIsValid(fuzzystate->first))
640  {
641  /* Record as provisional second match for RTE */
642  fuzzystate->rsecond = rte;
643  fuzzystate->second = attnum;
644  }
645  else if (fuzzystate->distance <= MAX_FUZZY_DISTANCE)
646  {
647  /*
648  * Record as provisional first match (this can occasionally occur
649  * because previous lowest distance was "too low a bar", rather
650  * than being associated with a real match)
651  */
652  fuzzystate->rfirst = rte;
653  fuzzystate->first = attnum;
654  }
655  }
656 }

References attnum, AttributeNumberIsValid, FuzzyAttrMatchState::distance, FuzzyAttrMatchState::first, InvalidAttrNumber, MAX_FUZZY_DISTANCE, FuzzyAttrMatchState::rfirst, FuzzyAttrMatchState::rsecond, FuzzyAttrMatchState::second, and varstr_levenshtein_less_equal().

Referenced by scanRTEForColumn().