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 rte_visible_if_lateral (ParseState *pstate, RangeTblEntry *rte)
 
static bool rte_visible_if_qualified (ParseState *pstate, RangeTblEntry *rte)
 
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, RTEPermissionInfo *perminfo, 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)
 
static ParseNamespaceItemfindNSItemForRTE (ParseState *pstate, RangeTblEntry *rte)
 
bool isQueryUsingTempRelation (Query *query)
 
RTEPermissionInfoaddRTEPermissionInfo (List **rteperminfos, RangeTblEntry *rte)
 
RTEPermissionInfogetRTEPermissionInfo (List *rteperminfos, RangeTblEntry *rte)
 

Macro Definition Documentation

◆ MAX_FUZZY_DISTANCE

#define MAX_FUZZY_DISTANCE   3

Definition at line 75 of file parse_relation.c.

Function Documentation

◆ addNSItemToQuery()

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

Definition at line 2592 of file parse_relation.c.

2595 {
2596  if (addToJoinList)
2597  {
2599 
2600  rtr->rtindex = nsitem->p_rtindex;
2601  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2602  }
2603  if (addToRelNameSpace || addToVarNameSpace)
2604  {
2605  /* Set the new nsitem's visibility flags correctly */
2606  nsitem->p_rel_visible = addToRelNameSpace;
2607  nsitem->p_cols_visible = addToVarNameSpace;
2608  nsitem->p_lateral_only = false;
2609  nsitem->p_lateral_ok = true;
2610  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2611  }
2612 }
List * lappend(List *list, void *datum)
Definition: list.c:338
#define makeNode(_type_)
Definition: nodes.h:165
List * p_namespace
Definition: parse_node.h:192
List * p_joinlist
Definition: parse_node.h:190

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

1442 {
1444  RTEPermissionInfo *perminfo;
1445  char *refname = alias ? alias->aliasname : relation->relname;
1446  LOCKMODE lockmode;
1447  Relation rel;
1448  ParseNamespaceItem *nsitem;
1449 
1450  Assert(pstate != NULL);
1451 
1452  rte->rtekind = RTE_RELATION;
1453  rte->alias = alias;
1454 
1455  /*
1456  * Identify the type of lock we'll need on this relation. It's not the
1457  * query's target table (that case is handled elsewhere), so we need
1458  * either RowShareLock if it's locked by FOR UPDATE/SHARE, or plain
1459  * AccessShareLock otherwise.
1460  */
1461  lockmode = isLockedRefname(pstate, refname) ? RowShareLock : AccessShareLock;
1462 
1463  /*
1464  * Get the rel's OID. This access also ensures that we have an up-to-date
1465  * relcache entry for the rel. Since this is typically the first access
1466  * to a rel in a statement, we must open the rel with the proper lockmode.
1467  */
1468  rel = parserOpenTable(pstate, relation, lockmode);
1469  rte->relid = RelationGetRelid(rel);
1470  rte->relkind = rel->rd_rel->relkind;
1471  rte->rellockmode = lockmode;
1472 
1473  /*
1474  * Build the list of effective column names using user-supplied aliases
1475  * and/or actual column names.
1476  */
1477  rte->eref = makeAlias(refname, NIL);
1478  buildRelationAliases(rel->rd_att, alias, rte->eref);
1479 
1480  /*
1481  * Set flags and initialize access permissions.
1482  *
1483  * The initial default on access checks is always check-for-READ-access,
1484  * which is the right thing for all except target tables.
1485  */
1486  rte->lateral = false;
1487  rte->inh = inh;
1488  rte->inFromCl = inFromCl;
1489 
1490  perminfo = addRTEPermissionInfo(&pstate->p_rteperminfos, rte);
1491  perminfo->requiredPerms = ACL_SELECT;
1492 
1493  /*
1494  * Add completed RTE to pstate's range table list, so that we know its
1495  * index. But we don't add it to the join list --- caller must do that if
1496  * appropriate.
1497  */
1498  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1499 
1500  /*
1501  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1502  * list --- caller must do that if appropriate.
1503  */
1504  nsitem = buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1505  perminfo, rel->rd_att);
1506 
1507  /*
1508  * Drop the rel refcount, but keep the access lock till end of transaction
1509  * so that the table can't be deleted or have its schema modified
1510  * underneath us.
1511  */
1512  table_close(rel, NoLock);
1513 
1514  return nsitem;
1515 }
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:387
RTEPermissionInfo * addRTEPermissionInfo(List **rteperminfos, RangeTblEntry *rte)
Relation parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, RTEPermissionInfo *perminfo, TupleDesc tupdesc)
bool isLockedRefname(ParseState *pstate, const char *refname)
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
@ RTE_RELATION
Definition: parsenodes.h:982
#define ACL_SELECT
Definition: parsenodes.h:84
static int list_length(const List *l)
Definition: pg_list.h:150
#define NIL
Definition: pg_list.h:66
#define RelationGetRelid(relation)
Definition: rel.h:501
char * aliasname
Definition: primnodes.h:42
List * p_rteperminfos
Definition: parse_node.h:187
List * p_rtable
Definition: parse_node.h:186
AclMode requiredPerms
Definition: parsenodes.h:1208
Alias * eref
Definition: parsenodes.h:1152
Alias * alias
Definition: parsenodes.h:1151
RTEKind rtekind
Definition: parsenodes.h:1001
char * relname
Definition: primnodes.h:77
TupleDesc rd_att
Definition: rel.h:111
Form_pg_class rd_rel
Definition: rel.h:110
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126

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

Referenced by transformTableEntry().

◆ addRangeTableEntryForCTE()

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

Definition at line 2286 of file parse_relation.c.

2291 {
2293  Alias *alias = rv->alias;
2294  char *refname = alias ? alias->aliasname : cte->ctename;
2295  Alias *eref;
2296  int numaliases;
2297  int varattno;
2298  ListCell *lc;
2299  int n_dontexpand_columns = 0;
2300  ParseNamespaceItem *psi;
2301 
2302  Assert(pstate != NULL);
2303 
2304  rte->rtekind = RTE_CTE;
2305  rte->ctename = cte->ctename;
2306  rte->ctelevelsup = levelsup;
2307 
2308  /* Self-reference if and only if CTE's parse analysis isn't completed */
2309  rte->self_reference = !IsA(cte->ctequery, Query);
2310  Assert(cte->cterecursive || !rte->self_reference);
2311  /* Bump the CTE's refcount if this isn't a self-reference */
2312  if (!rte->self_reference)
2313  cte->cterefcount++;
2314 
2315  /*
2316  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
2317  * This won't get checked in case of a self-reference, but that's OK
2318  * because data-modifying CTEs aren't allowed to be recursive anyhow.
2319  */
2320  if (IsA(cte->ctequery, Query))
2321  {
2322  Query *ctequery = (Query *) cte->ctequery;
2323 
2324  if (ctequery->commandType != CMD_SELECT &&
2325  ctequery->returningList == NIL)
2326  ereport(ERROR,
2327  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2328  errmsg("WITH query \"%s\" does not have a RETURNING clause",
2329  cte->ctename),
2330  parser_errposition(pstate, rv->location)));
2331  }
2332 
2333  rte->coltypes = list_copy(cte->ctecoltypes);
2334  rte->coltypmods = list_copy(cte->ctecoltypmods);
2336 
2337  rte->alias = alias;
2338  if (alias)
2339  eref = copyObject(alias);
2340  else
2341  eref = makeAlias(refname, NIL);
2342  numaliases = list_length(eref->colnames);
2343 
2344  /* fill in any unspecified alias columns */
2345  varattno = 0;
2346  foreach(lc, cte->ctecolnames)
2347  {
2348  varattno++;
2349  if (varattno > numaliases)
2350  eref->colnames = lappend(eref->colnames, lfirst(lc));
2351  }
2352  if (varattno < numaliases)
2353  ereport(ERROR,
2354  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2355  errmsg("table \"%s\" has %d columns available but %d columns specified",
2356  refname, varattno, numaliases)));
2357 
2358  rte->eref = eref;
2359 
2360  if (cte->search_clause)
2361  {
2364  rte->coltypes = lappend_oid(rte->coltypes, RECORDOID);
2365  else
2366  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2367  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2369 
2370  n_dontexpand_columns += 1;
2371  }
2372 
2373  if (cte->cycle_clause)
2374  {
2379 
2381  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2382  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2384 
2385  n_dontexpand_columns += 2;
2386  }
2387 
2388  /*
2389  * Set flags and access permissions.
2390  *
2391  * Subqueries are never checked for access rights, so no need to perform
2392  * addRTEPermissionInfo().
2393  */
2394  rte->lateral = false;
2395  rte->inh = false; /* never true for subqueries */
2396  rte->inFromCl = inFromCl;
2397 
2398  /*
2399  * Add completed RTE to pstate's range table list, so that we know its
2400  * index. But we don't add it to the join list --- caller must do that if
2401  * appropriate.
2402  */
2403  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2404 
2405  /*
2406  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2407  * list --- caller must do that if appropriate.
2408  */
2409  psi = buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2410  rte->coltypes, rte->coltypmods,
2411  rte->colcollations);
2412 
2413  /*
2414  * The columns added by search and cycle clauses are not included in star
2415  * expansion in queries contained in the CTE.
2416  */
2417  if (rte->ctelevelsup > 0)
2418  for (int i = 0; i < n_dontexpand_columns; i++)
2419  psi->p_nscolumns[list_length(psi->p_names->colnames) - 1 - i].p_dontexpand = true;
2420 
2421  return psi;
2422 }
int errcode(int sqlerrcode)
Definition: elog.c:735
int errmsg(const char *fmt,...)
Definition: elog.c:946
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
List * lappend_int(List *list, int datum)
Definition: list.c:356
List * lappend_oid(List *list, Oid datum)
Definition: list.c:374
List * list_copy(const List *oldlist)
Definition: list.c:1572
#define IsA(nodeptr, _type_)
Definition: nodes.h:168
#define copyObject(obj)
Definition: nodes.h:233
@ CMD_SELECT
Definition: nodes.h:265
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:988
#define lfirst(lc)
Definition: pg_list.h:170
#define InvalidOid
Definition: postgres_ext.h:36
List * colnames
Definition: primnodes.h:43
char * cycle_path_column
Definition: parsenodes.h:1552
Oid cycle_mark_collation
Definition: parsenodes.h:1557
char * cycle_mark_column
Definition: parsenodes.h:1549
char * search_seq_column
Definition: parsenodes.h:1541
bool search_breadth_first
Definition: parsenodes.h:1540
List * ctecoltypes
Definition: parsenodes.h:1577
CTECycleClause * cycle_clause
Definition: parsenodes.h:1570
CTESearchClause * search_clause
Definition: parsenodes.h:1569
List * ctecolcollations
Definition: parsenodes.h:1579
List * ctecolnames
Definition: parsenodes.h:1576
List * ctecoltypmods
Definition: parsenodes.h:1578
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:282
List * returningList
Definition: parsenodes.h:170
CmdType commandType
Definition: parsenodes.h:124
List * colcollations
Definition: parsenodes.h:1140
char * ctename
Definition: parsenodes.h:1116
bool self_reference
Definition: parsenodes.h:1118
Index ctelevelsup
Definition: parsenodes.h:1117
List * coltypes
Definition: parsenodes.h:1138
List * coltypmods
Definition: parsenodes.h:1139
int location
Definition: primnodes.h:89
Alias * alias
Definition: primnodes.h:86
String * makeString(char *str)
Definition: value.c:63

References RangeTblEntry::alias, RangeVar::alias, Alias::aliasname, Assert(), buildNSItemFromLists(), CMD_SELECT, RangeTblEntry::colcollations, Alias::colnames, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, Query::commandType, copyObject, 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, i, if(), RangeTblEntry::inFromCl, RangeTblEntry::inh, InvalidOid, IsA, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, list_copy(), list_length(), RangeVar::location, makeAlias(), makeNode, makeString(), NIL, ParseNamespaceColumn::p_dontexpand, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseState::p_rtable, parser_errposition(), Query::returningList, RTE_CTE, RangeTblEntry::rtekind, CTESearchClause::search_breadth_first, CommonTableExpr::search_clause, CTESearchClause::search_seq_column, and RangeTblEntry::self_reference.

Referenced by getNSItemForSpecialRelationTypes().

◆ addRangeTableEntryForENR()

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

Definition at line 2438 of file parse_relation.c.

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

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

Referenced by getNSItemForSpecialRelationTypes().

◆ addRangeTableEntryForFunction()

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

Definition at line 1702 of file parse_relation.c.

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

References RangeFunction::alias, RangeTblEntry::alias, Alias::aliasname, Assert(), buildNSItemFromTupleDesc(), buildRelationAliases(), CheckAttributeNamesTypes(), CHKATYPE_ANYRECORD, chooseScalarFunctionAlias(), ColumnDef::colname, CreateTemplateTupleDesc(), RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, exprCollation(), exprLocation(), exprType(), exprTypmod(), format_type_be(), forthree, RangeTblFunction::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, InvalidOid, j, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, linitial, list_length(), ColumnDef::location, makeAlias(), makeNode, makeString(), MaxHeapAttributeNumber, MaxTupleAttributeNumber, TupleDescData::natts, NIL, RangeFunction::ordinality, ParseState::p_rtable, palloc(), parser_errposition(), pstrdup(), RangeTblEntry::relid, RTE_FUNCTION, RangeTblEntry::rtekind, TypeName::setof, RangeTblEntry::subquery, TupleDescCopyEntry(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, TYPEFUNC_SCALAR, ColumnDef::typeName, and typenameTypeIdAndMod().

Referenced by transformRangeFunction().

◆ addRangeTableEntryForJoin()

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

Definition at line 2187 of file parse_relation.c.

2198 {
2200  Alias *eref;
2201  int numaliases;
2202  ParseNamespaceItem *nsitem;
2203 
2204  Assert(pstate != NULL);
2205 
2206  /*
2207  * Fail if join has too many columns --- we must be able to reference any
2208  * of the columns with an AttrNumber.
2209  */
2210  if (list_length(aliasvars) > MaxAttrNumber)
2211  ereport(ERROR,
2212  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2213  errmsg("joins can have at most %d columns",
2214  MaxAttrNumber)));
2215 
2216  rte->rtekind = RTE_JOIN;
2217  rte->relid = InvalidOid;
2218  rte->subquery = NULL;
2219  rte->jointype = jointype;
2220  rte->joinmergedcols = nummergedcols;
2221  rte->joinaliasvars = aliasvars;
2222  rte->joinleftcols = leftcols;
2223  rte->joinrightcols = rightcols;
2224  rte->join_using_alias = join_using_alias;
2225  rte->alias = alias;
2226 
2227  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
2228  numaliases = list_length(eref->colnames);
2229 
2230  /* fill in any unspecified alias columns */
2231  if (numaliases < list_length(colnames))
2232  eref->colnames = list_concat(eref->colnames,
2233  list_copy_tail(colnames, numaliases));
2234 
2235  if (numaliases > list_length(colnames))
2236  ereport(ERROR,
2237  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2238  errmsg("join expression \"%s\" has %d columns available but %d columns specified",
2239  eref->aliasname, list_length(colnames), numaliases)));
2240 
2241  rte->eref = eref;
2242 
2243  /*
2244  * Set flags and access permissions.
2245  *
2246  * Joins are never checked for access rights, so no need to perform
2247  * addRTEPermissionInfo().
2248  */
2249  rte->lateral = false;
2250  rte->inh = false; /* never true for joins */
2251  rte->inFromCl = inFromCl;
2252 
2253  /*
2254  * Add completed RTE to pstate's range table list, so that we know its
2255  * index. But we don't add it to the join list --- caller must do that if
2256  * appropriate.
2257  */
2258  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2259 
2260  /*
2261  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2262  * list --- caller must do that if appropriate.
2263  */
2264  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2265  nsitem->p_names = rte->eref;
2266  nsitem->p_rte = rte;
2267  nsitem->p_perminfo = NULL;
2268  nsitem->p_rtindex = list_length(pstate->p_rtable);
2269  nsitem->p_nscolumns = nscolumns;
2270  /* set default visibility flags; might get changed later */
2271  nsitem->p_rel_visible = true;
2272  nsitem->p_cols_visible = true;
2273  nsitem->p_lateral_only = false;
2274  nsitem->p_lateral_ok = true;
2275 
2276  return nsitem;
2277 }
#define MaxAttrNumber
Definition: attnum.h:24
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1612
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
@ RTE_JOIN
Definition: parsenodes.h:984
RangeTblEntry * p_rte
Definition: parse_node.h:278
RTEPermissionInfo * p_perminfo
Definition: parse_node.h:280
Alias * join_using_alias
Definition: parsenodes.h:1090
List * joinrightcols
Definition: parsenodes.h:1083
List * joinaliasvars
Definition: parsenodes.h:1081
JoinType jointype
Definition: parsenodes.h:1079
List * joinleftcols
Definition: parsenodes.h:1082

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

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

◆ addRangeTableEntryForRelation()

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

Definition at line 1534 of file parse_relation.c.

1540 {
1542  RTEPermissionInfo *perminfo;
1543  char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1544 
1545  Assert(pstate != NULL);
1546 
1547  Assert(lockmode == AccessShareLock ||
1548  lockmode == RowShareLock ||
1549  lockmode == RowExclusiveLock);
1550  Assert(CheckRelationLockedByMe(rel, lockmode, true));
1551 
1552  rte->rtekind = RTE_RELATION;
1553  rte->alias = alias;
1554  rte->relid = RelationGetRelid(rel);
1555  rte->relkind = rel->rd_rel->relkind;
1556  rte->rellockmode = lockmode;
1557 
1558  /*
1559  * Build the list of effective column names using user-supplied aliases
1560  * and/or actual column names.
1561  */
1562  rte->eref = makeAlias(refname, NIL);
1563  buildRelationAliases(rel->rd_att, alias, rte->eref);
1564 
1565  /*
1566  * Set flags and initialize access permissions.
1567  *
1568  * The initial default on access checks is always check-for-READ-access,
1569  * which is the right thing for all except target tables.
1570  */
1571  rte->lateral = false;
1572  rte->inh = inh;
1573  rte->inFromCl = inFromCl;
1574 
1575  perminfo = addRTEPermissionInfo(&pstate->p_rteperminfos, rte);
1576  perminfo->requiredPerms = ACL_SELECT;
1577 
1578  /*
1579  * Add completed RTE to pstate's range table list, so that we know its
1580  * index. But we don't add it to the join list --- caller must do that if
1581  * appropriate.
1582  */
1583  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1584 
1585  /*
1586  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1587  * list --- caller must do that if appropriate.
1588  */
1589  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1590  perminfo, rel->rd_att);
1591 }
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:331
#define RowExclusiveLock
Definition: lockdefs.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:535

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

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

◆ addRangeTableEntryForSubquery()

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

Definition at line 1605 of file parse_relation.c.

1610 {
1612  Alias *eref;
1613  int numaliases;
1614  List *coltypes,
1615  *coltypmods,
1616  *colcollations;
1617  int varattno;
1618  ListCell *tlistitem;
1619  ParseNamespaceItem *nsitem;
1620 
1621  Assert(pstate != NULL);
1622 
1623  rte->rtekind = RTE_SUBQUERY;
1624  rte->subquery = subquery;
1625  rte->alias = alias;
1626 
1627  eref = alias ? copyObject(alias) : makeAlias("unnamed_subquery", NIL);
1628  numaliases = list_length(eref->colnames);
1629 
1630  /* fill in any unspecified alias columns, and extract column type info */
1631  coltypes = coltypmods = colcollations = NIL;
1632  varattno = 0;
1633  foreach(tlistitem, subquery->targetList)
1634  {
1635  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1636 
1637  if (te->resjunk)
1638  continue;
1639  varattno++;
1640  Assert(varattno == te->resno);
1641  if (varattno > numaliases)
1642  {
1643  char *attrname;
1644 
1645  attrname = pstrdup(te->resname);
1646  eref->colnames = lappend(eref->colnames, makeString(attrname));
1647  }
1648  coltypes = lappend_oid(coltypes,
1649  exprType((Node *) te->expr));
1650  coltypmods = lappend_int(coltypmods,
1651  exprTypmod((Node *) te->expr));
1652  colcollations = lappend_oid(colcollations,
1653  exprCollation((Node *) te->expr));
1654  }
1655  if (varattno < numaliases)
1656  ereport(ERROR,
1657  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1658  errmsg("table \"%s\" has %d columns available but %d columns specified",
1659  eref->aliasname, varattno, numaliases)));
1660 
1661  rte->eref = eref;
1662 
1663  /*
1664  * Set flags.
1665  *
1666  * Subqueries are never checked for access rights, so no need to perform
1667  * addRTEPermissionInfo().
1668  */
1669  rte->lateral = lateral;
1670  rte->inh = false; /* never true for subqueries */
1671  rte->inFromCl = inFromCl;
1672 
1673  /*
1674  * Add completed RTE to pstate's range table list, so that we know its
1675  * index. But we don't add it to the join list --- caller must do that if
1676  * appropriate.
1677  */
1678  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1679 
1680  /*
1681  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1682  * list --- caller must do that if appropriate.
1683  */
1684  nsitem = buildNSItemFromLists(rte, list_length(pstate->p_rtable),
1685  coltypes, coltypmods, colcollations);
1686 
1687  /*
1688  * Mark it visible as a relation name only if it had a user-written alias.
1689  */
1690  nsitem->p_rel_visible = (alias != NULL);
1691 
1692  return nsitem;
1693 }
@ RTE_SUBQUERY
Definition: parsenodes.h:983
List * targetList
Definition: parsenodes.h:164
Expr * expr
Definition: primnodes.h:1555
char * resname
Definition: primnodes.h:1557
AttrNumber resno
Definition: primnodes.h:1556
bool resjunk
Definition: primnodes.h:1562

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

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

◆ addRangeTableEntryForTableFunc()

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

Definition at line 2018 of file parse_relation.c.

2023 {
2025  char *refname;
2026  Alias *eref;
2027  int numaliases;
2028 
2029  Assert(pstate != NULL);
2030 
2031  /* Disallow more columns than will fit in a tuple */
2033  ereport(ERROR,
2034  (errcode(ERRCODE_TOO_MANY_COLUMNS),
2035  errmsg("functions in FROM can return at most %d columns",
2037  parser_errposition(pstate,
2038  exprLocation((Node *) tf))));
2042 
2043  refname = alias ? alias->aliasname : pstrdup("xmltable");
2044 
2045  rte->rtekind = RTE_TABLEFUNC;
2046  rte->relid = InvalidOid;
2047  rte->subquery = NULL;
2048  rte->tablefunc = tf;
2049  rte->coltypes = tf->coltypes;
2050  rte->coltypmods = tf->coltypmods;
2051  rte->colcollations = tf->colcollations;
2052  rte->alias = alias;
2053 
2054  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2055  numaliases = list_length(eref->colnames);
2056 
2057  /* fill in any unspecified alias columns */
2058  if (numaliases < list_length(tf->colnames))
2059  eref->colnames = list_concat(eref->colnames,
2060  list_copy_tail(tf->colnames, numaliases));
2061 
2062  if (numaliases > list_length(tf->colnames))
2063  ereport(ERROR,
2064  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2065  errmsg("%s function has %d columns available but %d columns specified",
2066  "XMLTABLE",
2067  list_length(tf->colnames), numaliases)));
2068 
2069  rte->eref = eref;
2070 
2071  /*
2072  * Set flags and access permissions.
2073  *
2074  * Tablefuncs are never checked for access rights (at least, not by
2075  * ExecCheckPermissions()), so no need to perform addRTEPermissionInfo().
2076  */
2077  rte->lateral = lateral;
2078  rte->inh = false; /* never true for tablefunc RTEs */
2079  rte->inFromCl = inFromCl;
2080 
2081  /*
2082  * Add completed RTE to pstate's range table list, so that we know its
2083  * index. But we don't add it to the join list --- caller must do that if
2084  * appropriate.
2085  */
2086  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2087 
2088  /*
2089  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2090  * list --- caller must do that if appropriate.
2091  */
2092  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2093  rte->coltypes, rte->coltypmods,
2094  rte->colcollations);
2095 }
@ RTE_TABLEFUNC
Definition: parsenodes.h:986
TableFunc * tablefunc
Definition: parsenodes.h:1106
List * coltypmods
Definition: primnodes.h:107
List * coltypes
Definition: primnodes.h:106
List * colnames
Definition: primnodes.h:105
List * colcollations
Definition: primnodes.h:108

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

Referenced by transformRangeTableFunc().

◆ addRangeTableEntryForValues()

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

Definition at line 2104 of file parse_relation.c.

2112 {
2114  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
2115  Alias *eref;
2116  int numaliases;
2117  int numcolumns;
2118 
2119  Assert(pstate != NULL);
2120 
2121  rte->rtekind = RTE_VALUES;
2122  rte->relid = InvalidOid;
2123  rte->subquery = NULL;
2124  rte->values_lists = exprs;
2125  rte->coltypes = coltypes;
2126  rte->coltypmods = coltypmods;
2127  rte->colcollations = colcollations;
2128  rte->alias = alias;
2129 
2130  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2131 
2132  /* fill in any unspecified alias columns */
2133  numcolumns = list_length((List *) linitial(exprs));
2134  numaliases = list_length(eref->colnames);
2135  while (numaliases < numcolumns)
2136  {
2137  char attrname[64];
2138 
2139  numaliases++;
2140  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
2141  eref->colnames = lappend(eref->colnames,
2142  makeString(pstrdup(attrname)));
2143  }
2144  if (numcolumns < numaliases)
2145  ereport(ERROR,
2146  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2147  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
2148  refname, numcolumns, numaliases)));
2149 
2150  rte->eref = eref;
2151 
2152  /*
2153  * Set flags and access permissions.
2154  *
2155  * Subqueries are never checked for access rights, so no need to perform
2156  * addRTEPermissionInfo().
2157  */
2158  rte->lateral = lateral;
2159  rte->inh = false; /* never true for values RTEs */
2160  rte->inFromCl = inFromCl;
2161 
2162  /*
2163  * Add completed RTE to pstate's range table list, so that we know its
2164  * index. But we don't add it to the join list --- caller must do that if
2165  * appropriate.
2166  */
2167  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2168 
2169  /*
2170  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2171  * list --- caller must do that if appropriate.
2172  */
2173  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2174  rte->coltypes, rte->coltypmods,
2175  rte->colcollations);
2176 }
@ RTE_VALUES
Definition: parsenodes.h:987
#define snprintf
Definition: port.h:238
List * values_lists
Definition: parsenodes.h:1111

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

Referenced by transformValuesClause().

◆ addRTEPermissionInfo()

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

Definition at line 3833 of file parse_relation.c.

3834 {
3835  RTEPermissionInfo *perminfo;
3836 
3837  Assert(rte->rtekind == RTE_RELATION);
3838  Assert(rte->perminfoindex == 0);
3839 
3840  /* Nope, so make one and add to the list. */
3841  perminfo = makeNode(RTEPermissionInfo);
3842  perminfo->relid = rte->relid;
3843  perminfo->inh = rte->inh;
3844  /* Other information is set by fetching the node as and where needed. */
3845 
3846  *rteperminfos = lappend(*rteperminfos, perminfo);
3847 
3848  /* Note its index (1-based!) */
3849  rte->perminfoindex = list_length(*rteperminfos);
3850 
3851  return perminfo;
3852 }
Index perminfoindex
Definition: parsenodes.h:1036

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

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

◆ attnameAttNum()

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

Definition at line 3442 of file parse_relation.c.

3443 {
3444  int i;
3445 
3446  for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3447  {
3449 
3450  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3451  return i + 1;
3452  }
3453 
3454  if (sysColOK)
3455  {
3457  return i;
3458  }
3459 
3460  /* on failure */
3461  return InvalidAttrNumber;
3462 }
#define InvalidAttrNumber
Definition: attnum.h:23
int namestrcmp(Name name, const char *str)
Definition: name.c:247
static int specialAttNum(const char *attname)
NameData attname
Definition: pg_attribute.h:41
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:507

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

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

◆ attnumAttName()

const NameData* attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3492 of file parse_relation.c.

3493 {
3494  if (attid <= 0)
3495  {
3496  const FormData_pg_attribute *sysatt;
3497 
3498  sysatt = SystemAttributeDefinition(attid);
3499  return &sysatt->attname;
3500  }
3501  if (attid > rd->rd_att->natts)
3502  elog(ERROR, "invalid attribute number %d", attid);
3503  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3504 }
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 3534 of file parse_relation.c.

3535 {
3536  if (attid <= 0)
3537  {
3538  /* All system attributes are of noncollatable types. */
3539  return InvalidOid;
3540  }
3541  if (attid > rd->rd_att->natts)
3542  elog(ERROR, "invalid attribute number %d", attid);
3543  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3544 }

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

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3514 of file parse_relation.c.

3515 {
3516  if (attid <= 0)
3517  {
3518  const FormData_pg_attribute *sysatt;
3519 
3520  sysatt = SystemAttributeDefinition(attid);
3521  return sysatt->atttypid;
3522  }
3523  if (attid > rd->rd_att->natts)
3524  elog(ERROR, "invalid attribute number %d", attid);
3525  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3526 }

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

1324 {
1325  ParseNamespaceItem *nsitem;
1326  ParseNamespaceColumn *nscolumns;
1327  int maxattrs = list_length(coltypes);
1328  int varattno;
1329  ListCell *lct;
1330  ListCell *lcm;
1331  ListCell *lcc;
1332 
1333  /* colnames must have the same number of entries as the nsitem */
1334  Assert(maxattrs == list_length(rte->eref->colnames));
1335 
1336  Assert(maxattrs == list_length(coltypmods));
1337  Assert(maxattrs == list_length(colcollations));
1338 
1339  /* extract per-column data from the lists */
1340  nscolumns = (ParseNamespaceColumn *)
1341  palloc0(maxattrs * sizeof(ParseNamespaceColumn));
1342 
1343  varattno = 0;
1344  forthree(lct, coltypes,
1345  lcm, coltypmods,
1346  lcc, colcollations)
1347  {
1348  nscolumns[varattno].p_varno = rtindex;
1349  nscolumns[varattno].p_varattno = varattno + 1;
1350  nscolumns[varattno].p_vartype = lfirst_oid(lct);
1351  nscolumns[varattno].p_vartypmod = lfirst_int(lcm);
1352  nscolumns[varattno].p_varcollid = lfirst_oid(lcc);
1353  nscolumns[varattno].p_varnosyn = rtindex;
1354  nscolumns[varattno].p_varattnosyn = varattno + 1;
1355  varattno++;
1356  }
1357 
1358  /* ... and build the nsitem */
1359  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1360  nsitem->p_names = rte->eref;
1361  nsitem->p_rte = rte;
1362  nsitem->p_rtindex = rtindex;
1363  nsitem->p_nscolumns = nscolumns;
1364  /* set default visibility flags; might get changed later */
1365  nsitem->p_rel_visible = true;
1366  nsitem->p_cols_visible = true;
1367  nsitem->p_lateral_only = false;
1368  nsitem->p_lateral_ok = true;
1369 
1370  return nsitem;
1371 }
void * palloc0(Size size)
Definition: mcxt.c:1230
#define lfirst_int(lc)
Definition: pg_list.h:171
#define lfirst_oid(lc)
Definition: pg_list.h:172
AttrNumber p_varattno
Definition: parse_node.h:313
AttrNumber p_varattnosyn
Definition: parse_node.h:318

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,
RTEPermissionInfo perminfo,
TupleDesc  tupdesc 
)
static

Definition at line 1262 of file parse_relation.c.

1265 {
1266  ParseNamespaceItem *nsitem;
1267  ParseNamespaceColumn *nscolumns;
1268  int maxattrs = tupdesc->natts;
1269  int varattno;
1270 
1271  /* colnames must have the same number of entries as the nsitem */
1272  Assert(maxattrs == list_length(rte->eref->colnames));
1273 
1274  /* extract per-column data from the tupdesc */
1275  nscolumns = (ParseNamespaceColumn *)
1276  palloc0(maxattrs * sizeof(ParseNamespaceColumn));
1277 
1278  for (varattno = 0; varattno < maxattrs; varattno++)
1279  {
1280  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1281 
1282  /* For a dropped column, just leave the entry as zeroes */
1283  if (attr->attisdropped)
1284  continue;
1285 
1286  nscolumns[varattno].p_varno = rtindex;
1287  nscolumns[varattno].p_varattno = varattno + 1;
1288  nscolumns[varattno].p_vartype = attr->atttypid;
1289  nscolumns[varattno].p_vartypmod = attr->atttypmod;
1290  nscolumns[varattno].p_varcollid = attr->attcollation;
1291  nscolumns[varattno].p_varnosyn = rtindex;
1292  nscolumns[varattno].p_varattnosyn = varattno + 1;
1293  }
1294 
1295  /* ... and build the nsitem */
1296  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1297  nsitem->p_names = rte->eref;
1298  nsitem->p_rte = rte;
1299  nsitem->p_rtindex = rtindex;
1300  nsitem->p_perminfo = perminfo;
1301  nsitem->p_nscolumns = nscolumns;
1302  /* set default visibility flags; might get changed later */
1303  nsitem->p_rel_visible = true;
1304  nsitem->p_cols_visible = true;
1305  nsitem->p_lateral_only = false;
1306  nsitem->p_lateral_ok = true;
1307 
1308  return nsitem;
1309 }

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_perminfo, 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 1145 of file parse_relation.c.

1146 {
1147  int maxattrs = tupdesc->natts;
1148  List *aliaslist;
1149  ListCell *aliaslc;
1150  int numaliases;
1151  int varattno;
1152  int numdropped = 0;
1153 
1154  Assert(eref->colnames == NIL);
1155 
1156  if (alias)
1157  {
1158  aliaslist = alias->colnames;
1159  aliaslc = list_head(aliaslist);
1160  numaliases = list_length(aliaslist);
1161  /* We'll rebuild the alias colname list */
1162  alias->colnames = NIL;
1163  }
1164  else
1165  {
1166  aliaslist = NIL;
1167  aliaslc = NULL;
1168  numaliases = 0;
1169  }
1170 
1171  for (varattno = 0; varattno < maxattrs; varattno++)
1172  {
1173  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1174  String *attrname;
1175 
1176  if (attr->attisdropped)
1177  {
1178  /* Always insert an empty string for a dropped column */
1179  attrname = makeString(pstrdup(""));
1180  if (aliaslc)
1181  alias->colnames = lappend(alias->colnames, attrname);
1182  numdropped++;
1183  }
1184  else if (aliaslc)
1185  {
1186  /* Use the next user-supplied alias */
1187  attrname = lfirst_node(String, aliaslc);
1188  aliaslc = lnext(aliaslist, aliaslc);
1189  alias->colnames = lappend(alias->colnames, attrname);
1190  }
1191  else
1192  {
1193  attrname = makeString(pstrdup(NameStr(attr->attname)));
1194  /* we're done with the alias if any */
1195  }
1196 
1197  eref->colnames = lappend(eref->colnames, attrname);
1198  }
1199 
1200  /* Too many user-supplied aliases? */
1201  if (aliaslc)
1202  ereport(ERROR,
1203  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1204  errmsg("table \"%s\" has %d columns available but %d columns specified",
1205  eref->aliasname, maxattrs - numdropped, numaliases)));
1206 }
#define NameStr(name)
Definition: c.h:682
#define lfirst_node(type, lc)
Definition: pg_list.h:174
static ListCell * list_head(const List *l)
Definition: pg_list.h:126
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:341
Definition: value.h:64

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

485 {
486  if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
487  {
488  /* SQL:2008 demands this be an error, not an invisible item */
489  RangeTblEntry *rte = nsitem->p_rte;
490  char *refname = nsitem->p_names->aliasname;
491 
492  ereport(ERROR,
493  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
494  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
495  refname),
496  (pstate->p_target_nsitem != NULL &&
497  rte == pstate->p_target_nsitem->p_rte) ?
498  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
499  refname) :
500  errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
501  parser_errposition(pstate, location)));
502  }
503 }
int errdetail(const char *fmt,...)
Definition: elog.c:1079
int errhint(const char *fmt,...)
Definition: elog.c:1193
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:199

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

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

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

Referenced by transformFromClause(), and transformFromClauseItem().

◆ chooseScalarFunctionAlias()

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

Definition at line 1222 of file parse_relation.c.

1224 {
1225  char *pname;
1226 
1227  /*
1228  * If the expression is a simple function call, and the function has a
1229  * single OUT parameter that is named, use the parameter's name.
1230  */
1231  if (funcexpr && IsA(funcexpr, FuncExpr))
1232  {
1233  pname = get_func_result_name(((FuncExpr *) funcexpr)->funcid);
1234  if (pname)
1235  return pname;
1236  }
1237 
1238  /*
1239  * If there's just one function in the RTE, and the user gave an RTE alias
1240  * name, use that name. (This makes FROM func() AS foo use "foo" as the
1241  * column name as well as the table alias.)
1242  */
1243  if (nfuncs == 1 && alias)
1244  return alias->aliasname;
1245 
1246  /*
1247  * Otherwise use the function name.
1248  */
1249  return funcname;
1250 }
char * get_func_result_name(Oid functionId)
Definition: funcapi.c:1599

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

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

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

3626 {
3628 
3629  /*
3630  * Search the entire rtable looking for possible matches. If we find one,
3631  * emit a hint about it.
3632  */
3633  state = searchRangeTableForCol(pstate, relname, colname, location);
3634 
3635  /*
3636  * If there are exact match(es), they must be inaccessible for some
3637  * reason.
3638  */
3639  if (state->rexact1)
3640  {
3641  /*
3642  * We don't try too hard when there's multiple inaccessible exact
3643  * matches, but at least be sure that we don't misleadingly suggest
3644  * that there's only one.
3645  */
3646  if (state->rexact2)
3647  ereport(ERROR,
3648  (errcode(ERRCODE_UNDEFINED_COLUMN),
3649  relname ?
3650  errmsg("column %s.%s does not exist", relname, colname) :
3651  errmsg("column \"%s\" does not exist", colname),
3652  errdetail("There are columns named \"%s\", but they are in tables that cannot be referenced from this part of the query.",
3653  colname),
3654  !relname ? errhint("Try using a table-qualified name.") : 0,
3655  parser_errposition(pstate, location)));
3656  /* Single exact match, so try to determine why it's inaccessible. */
3657  ereport(ERROR,
3658  (errcode(ERRCODE_UNDEFINED_COLUMN),
3659  relname ?
3660  errmsg("column %s.%s does not exist", relname, colname) :
3661  errmsg("column \"%s\" does not exist", colname),
3662  errdetail("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3663  colname, state->rexact1->eref->aliasname),
3664  rte_visible_if_lateral(pstate, state->rexact1) ?
3665  errhint("To reference that column, you must mark this subquery with LATERAL.") :
3666  (!relname && rte_visible_if_qualified(pstate, state->rexact1)) ?
3667  errhint("To reference that column, you must use a table-qualified name.") : 0,
3668  parser_errposition(pstate, location)));
3669  }
3670 
3671  if (!state->rsecond)
3672  {
3673  /* If we found no match at all, we have little to report */
3674  if (!state->rfirst)
3675  ereport(ERROR,
3676  (errcode(ERRCODE_UNDEFINED_COLUMN),
3677  relname ?
3678  errmsg("column %s.%s does not exist", relname, colname) :
3679  errmsg("column \"%s\" does not exist", colname),
3680  parser_errposition(pstate, location)));
3681  /* Handle case where we have a single alternative spelling to offer */
3682  ereport(ERROR,
3683  (errcode(ERRCODE_UNDEFINED_COLUMN),
3684  relname ?
3685  errmsg("column %s.%s does not exist", relname, colname) :
3686  errmsg("column \"%s\" does not exist", colname),
3687  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3688  state->rfirst->eref->aliasname,
3689  strVal(list_nth(state->rfirst->eref->colnames,
3690  state->first - 1))),
3691  parser_errposition(pstate, location)));
3692  }
3693  else
3694  {
3695  /* Handle case where there are two equally useful column hints */
3696  ereport(ERROR,
3697  (errcode(ERRCODE_UNDEFINED_COLUMN),
3698  relname ?
3699  errmsg("column %s.%s does not exist", relname, colname) :
3700  errmsg("column \"%s\" does not exist", colname),
3701  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3702  state->rfirst->eref->aliasname,
3703  strVal(list_nth(state->rfirst->eref->colnames,
3704  state->first - 1)),
3705  state->rsecond->eref->aliasname,
3706  strVal(list_nth(state->rsecond->eref->colnames,
3707  state->second - 1))),
3708  parser_errposition(pstate, location)));
3709  }
3710 }
static bool rte_visible_if_lateral(ParseState *pstate, RangeTblEntry *rte)
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colname, int location)
static bool rte_visible_if_qualified(ParseState *pstate, RangeTblEntry *rte)
NameData relname
Definition: pg_class.h:38
static void * list_nth(const List *list, int n)
Definition: pg_list.h:297
Definition: regguts.h:318
#define strVal(v)
Definition: value.h:82

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

Referenced by transformColumnRef().

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3553 of file parse_relation.c.

3554 {
3555  RangeTblEntry *rte;
3556  const char *badAlias = NULL;
3557 
3558  /*
3559  * Check to see if there are any potential matches in the query's
3560  * rangetable. (Note: cases involving a bad schema name in the RangeVar
3561  * will throw error immediately here. That seems OK.)
3562  */
3563  rte = searchRangeTableForRel(pstate, relation);
3564 
3565  /*
3566  * If we found a match that has an alias and the alias is visible in the
3567  * namespace, then the problem is probably use of the relation's real name
3568  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3569  * common enough to justify a specific hint.
3570  *
3571  * If we found a match that doesn't meet those criteria, assume the
3572  * problem is illegal use of a relation outside its scope, as in the
3573  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3574  */
3575  if (rte && rte->alias &&
3576  strcmp(rte->eref->aliasname, relation->relname) != 0)
3577  {
3578  ParseNamespaceItem *nsitem;
3579  int sublevels_up;
3580 
3581  nsitem = refnameNamespaceItem(pstate, NULL, rte->eref->aliasname,
3582  relation->location,
3583  &sublevels_up);
3584  if (nsitem && nsitem->p_rte == rte)
3585  badAlias = rte->eref->aliasname;
3586  }
3587 
3588  /* If it looks like the user forgot to use an alias, hint about that */
3589  if (badAlias)
3590  ereport(ERROR,
3592  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3593  relation->relname),
3594  errhint("Perhaps you meant to reference the table alias \"%s\".",
3595  badAlias),
3596  parser_errposition(pstate, relation->location)));
3597  /* Hint about case where we found an (inaccessible) exact match */
3598  else if (rte)
3599  ereport(ERROR,
3601  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3602  relation->relname),
3603  errdetail("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3604  rte->eref->aliasname),
3605  rte_visible_if_lateral(pstate, rte) ?
3606  errhint("To reference that table, you must mark this subquery with LATERAL.") : 0,
3607  parser_errposition(pstate, relation->location)));
3608  /* Else, we have nothing to offer but the bald statement of error */
3609  else
3610  ereport(ERROR,
3612  errmsg("missing FROM-clause entry for table \"%s\"",
3613  relation->relname),
3614  parser_errposition(pstate, relation->location)));
3615 }
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:77

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

Referenced by ExpandColumnRefStar(), and transformColumnRef().

◆ expandNSItemAttrs()

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

Definition at line 3150 of file parse_relation.c.

3152 {
3153  RangeTblEntry *rte = nsitem->p_rte;
3154  RTEPermissionInfo *perminfo = nsitem->p_perminfo;
3155  List *names,
3156  *vars;
3157  ListCell *name,
3158  *var;
3159  List *te_list = NIL;
3160 
3161  vars = expandNSItemVars(nsitem, sublevels_up, location, &names);
3162 
3163  /*
3164  * Require read access to the table. This is normally redundant with the
3165  * markVarForSelectPriv calls below, but not if the table has zero
3166  * columns. We need not do anything if the nsitem is for a join: its
3167  * component tables will have been marked ACL_SELECT when they were added
3168  * to the rangetable. (This step changes things only for the target
3169  * relation of UPDATE/DELETE, which cannot be under a join.)
3170  */
3171  if (rte->rtekind == RTE_RELATION)
3172  {
3173  Assert(perminfo != NULL);
3174  perminfo->requiredPerms |= ACL_SELECT;
3175  }
3176 
3177  forboth(name, names, var, vars)
3178  {
3179  char *label = strVal(lfirst(name));
3180  Var *varnode = (Var *) lfirst(var);
3181  TargetEntry *te;
3182 
3183  te = makeTargetEntry((Expr *) varnode,
3184  (AttrNumber) pstate->p_next_resno++,
3185  label,
3186  false);
3187  te_list = lappend(te_list, te);
3188 
3189  if (require_col_privs)
3190  {
3191  /* Require read access to each column */
3192  markVarForSelectPriv(pstate, varnode);
3193  }
3194  }
3195 
3196  Assert(name == NULL && var == NULL); /* lists not the same length? */
3197 
3198  return te_list;
3199 }
const char * name
Definition: encode.c:561
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
void markVarForSelectPriv(ParseState *pstate, Var *var)
List * expandNSItemVars(ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
static char * label
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:465
int p_next_resno
Definition: parse_node.h:203
Definition: primnodes.h:205
Definition: regcomp.c:282

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

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

◆ expandNSItemVars()

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

Definition at line 3090 of file parse_relation.c.

3093 {
3094  List *result = NIL;
3095  int colindex;
3096  ListCell *lc;
3097 
3098  if (colnames)
3099  *colnames = NIL;
3100  colindex = 0;
3101  foreach(lc, nsitem->p_names->colnames)
3102  {
3103  String *colnameval = lfirst(lc);
3104  const char *colname = strVal(colnameval);
3105  ParseNamespaceColumn *nscol = nsitem->p_nscolumns + colindex;
3106 
3107  if (nscol->p_dontexpand)
3108  {
3109  /* skip */
3110  }
3111  else if (colname[0])
3112  {
3113  Var *var;
3114 
3115  Assert(nscol->p_varno > 0);
3116  var = makeVar(nscol->p_varno,
3117  nscol->p_varattno,
3118  nscol->p_vartype,
3119  nscol->p_vartypmod,
3120  nscol->p_varcollid,
3121  sublevels_up);
3122  /* makeVar doesn't offer parameters for these, so set by hand: */
3123  var->varnosyn = nscol->p_varnosyn;
3124  var->varattnosyn = nscol->p_varattnosyn;
3125  var->location = location;
3126  result = lappend(result, var);
3127  if (colnames)
3128  *colnames = lappend(*colnames, colnameval);
3129  }
3130  else
3131  {
3132  /* dropped column, ignore */
3133  Assert(nscol->p_varno == 0);
3134  }
3135  colindex++;
3136  }
3137  return result;
3138 }
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
int location
Definition: primnodes.h:243

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, and strVal.

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

2987 {
2988  Relation rel;
2989 
2990  /* Get the tupledesc and turn it over to expandTupleDesc */
2991  rel = relation_open(relid, AccessShareLock);
2992  expandTupleDesc(rel->rd_att, eref, rel->rd_att->natts, 0,
2993  rtindex, sublevels_up,
2994  location, include_dropped,
2995  colnames, colvars);
2997 }
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 2632 of file parse_relation.c.

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

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(), set_relation_column_names(), 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 3009 of file parse_relation.c.

3013 {
3014  ListCell *aliascell;
3015  int varattno;
3016 
3017  aliascell = (offset < list_length(eref->colnames)) ?
3018  list_nth_cell(eref->colnames, offset) : NULL;
3019 
3020  Assert(count <= tupdesc->natts);
3021  for (varattno = 0; varattno < count; varattno++)
3022  {
3023  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
3024 
3025  if (attr->attisdropped)
3026  {
3027  if (include_dropped)
3028  {
3029  if (colnames)
3030  *colnames = lappend(*colnames, makeString(pstrdup("")));
3031  if (colvars)
3032  {
3033  /*
3034  * can't use atttypid here, but it doesn't really matter
3035  * what type the Const claims to be.
3036  */
3037  *colvars = lappend(*colvars,
3038  makeNullConst(INT4OID, -1, InvalidOid));
3039  }
3040  }
3041  if (aliascell)
3042  aliascell = lnext(eref->colnames, aliascell);
3043  continue;
3044  }
3045 
3046  if (colnames)
3047  {
3048  char *label;
3049 
3050  if (aliascell)
3051  {
3052  label = strVal(lfirst(aliascell));
3053  aliascell = lnext(eref->colnames, aliascell);
3054  }
3055  else
3056  {
3057  /* If we run out of aliases, use the underlying name */
3058  label = NameStr(attr->attname);
3059  }
3060  *colnames = lappend(*colnames, makeString(pstrdup(label)));
3061  }
3062 
3063  if (colvars)
3064  {
3065  Var *varnode;
3066 
3067  varnode = makeVar(rtindex, varattno + offset + 1,
3068  attr->atttypid, attr->atttypmod,
3069  attr->attcollation,
3070  sublevels_up);
3071  varnode->location = location;
3072 
3073  *colvars = lappend(*colvars, varnode);
3074  }
3075  }
3076 }
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:275

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

◆ findNSItemForRTE()

static ParseNamespaceItem* findNSItemForRTE ( ParseState pstate,
RangeTblEntry rte 
)
static

Definition at line 3717 of file parse_relation.c.

3718 {
3719  while (pstate != NULL)
3720  {
3721  ListCell *l;
3722 
3723  foreach(l, pstate->p_namespace)
3724  {
3725  ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(l);
3726 
3727  if (nsitem->p_rte == rte)
3728  return nsitem;
3729  }
3730  pstate = pstate->parentParseState;
3731  }
3732  return NULL;
3733 }

References lfirst, ParseState::p_namespace, ParseNamespaceItem::p_rte, and ParseState::parentParseState.

Referenced by rte_visible_if_lateral(), and rte_visible_if_qualified().

◆ get_parse_rowmark()

RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3418 of file parse_relation.c.

3419 {
3420  ListCell *l;
3421 
3422  foreach(l, qry->rowMarks)
3423  {
3424  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3425 
3426  if (rc->rti == rtindex)
3427  return rc;
3428  }
3429  return NULL;
3430 }
List * rowMarks
Definition: parsenodes.h:189

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

3255 {
3256  bool result;
3257 
3258  switch (rte->rtekind)
3259  {
3260  case RTE_RELATION:
3261  {
3262  /*
3263  * Plain relation RTE --- get the attribute's catalog entry
3264  */
3265  HeapTuple tp;
3266  Form_pg_attribute att_tup;
3267 
3268  tp = SearchSysCache2(ATTNUM,
3269  ObjectIdGetDatum(rte->relid),
3271  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3272  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3273  attnum, rte->relid);
3274  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
3275  result = att_tup->attisdropped;
3276  ReleaseSysCache(tp);
3277  }
3278  break;
3279  case RTE_SUBQUERY:
3280  case RTE_TABLEFUNC:
3281  case RTE_VALUES:
3282  case RTE_CTE:
3283 
3284  /*
3285  * Subselect, Table Functions, Values, CTE RTEs never have dropped
3286  * columns
3287  */
3288  result = false;
3289  break;
3290  case RTE_NAMEDTUPLESTORE:
3291  {
3292  /* Check dropped-ness by testing for valid coltype */
3293  if (attnum <= 0 ||
3294  attnum > list_length(rte->coltypes))
3295  elog(ERROR, "invalid varattno %d", attnum);
3296  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3297  }
3298  break;
3299  case RTE_JOIN:
3300  {
3301  /*
3302  * A join RTE would not have dropped columns when constructed,
3303  * but one in a stored rule might contain columns that were
3304  * dropped from the underlying tables, if said columns are
3305  * nowhere explicitly referenced in the rule. This will be
3306  * signaled to us by a null pointer in the joinaliasvars list.
3307  */
3308  Var *aliasvar;
3309 
3310  if (attnum <= 0 ||
3312  elog(ERROR, "invalid varattno %d", attnum);
3313  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3314 
3315  result = (aliasvar == NULL);
3316  }
3317  break;
3318  case RTE_FUNCTION:
3319  {
3320  /* Function RTE */
3321  ListCell *lc;
3322  int atts_done = 0;
3323 
3324  /*
3325  * Dropped attributes are only possible with functions that
3326  * return named composite types. In such a case we have to
3327  * look up the result type to see if it currently has this
3328  * column dropped. So first, loop over the funcs until we
3329  * find the one that covers the requested column.
3330  */
3331  foreach(lc, rte->functions)
3332  {
3333  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3334 
3335  if (attnum > atts_done &&
3336  attnum <= atts_done + rtfunc->funccolcount)
3337  {
3338  TupleDesc tupdesc;
3339 
3340  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3341  true);
3342  if (tupdesc)
3343  {
3344  /* Composite data type, e.g. a table's row type */
3345  Form_pg_attribute att_tup;
3346 
3347  Assert(tupdesc);
3348  Assert(attnum - atts_done <= tupdesc->natts);
3349  att_tup = TupleDescAttr(tupdesc,
3350  attnum - atts_done - 1);
3351  return att_tup->attisdropped;
3352  }
3353  /* Otherwise, it can't have any dropped columns */
3354  return false;
3355  }
3356  atts_done += rtfunc->funccolcount;
3357  }
3358 
3359  /* If we get here, must be looking for the ordinality column */
3360  if (rte->funcordinality && attnum == atts_done + 1)
3361  return false;
3362 
3363  /* this probably can't happen ... */
3364  ereport(ERROR,
3365  (errcode(ERRCODE_UNDEFINED_COLUMN),
3366  errmsg("column %d of relation \"%s\" does not exist",
3367  attnum,
3368  rte->eref->aliasname)));
3369  result = false; /* keep compiler quiet */
3370  }
3371  break;
3372  case RTE_RESULT:
3373  /* this probably can't happen ... */
3374  ereport(ERROR,
3375  (errcode(ERRCODE_UNDEFINED_COLUMN),
3376  errmsg("column %d of relation \"%s\" does not exist",
3377  attnum,
3378  rte->eref->aliasname)));
3379  result = false; /* keep compiler quiet */
3380  break;
3381  default:
3382  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3383  result = false; /* keep compiler quiet */
3384  }
3385 
3386  return result;
3387 }
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:543
#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:319
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:520
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
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 3216 of file parse_relation.c.

3217 {
3218  if (attnum == InvalidAttrNumber)
3219  return "*";
3220 
3221  /*
3222  * If there is a user-written column alias, use it.
3223  */
3224  if (rte->alias &&
3225  attnum > 0 && attnum <= list_length(rte->alias->colnames))
3226  return strVal(list_nth(rte->alias->colnames, attnum - 1));
3227 
3228  /*
3229  * If the RTE is a relation, go to the system catalogs not the
3230  * eref->colnames list. This is a little slower but it will give the
3231  * right answer if the column has been renamed since the eref list was
3232  * built (which can easily happen for rules).
3233  */
3234  if (rte->rtekind == RTE_RELATION)
3235  return get_attname(rte->relid, attnum, false);
3236 
3237  /*
3238  * Otherwise use the column name from eref. There should always be one.
3239  */
3240  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3241  return strVal(list_nth(rte->eref->colnames, attnum - 1));
3242 
3243  /* else caller gave us a bogus attnum */
3244  elog(ERROR, "invalid attnum %d for rangetable entry %s",
3245  attnum, rte->eref->aliasname);
3246  return NULL; /* keep compiler quiet */
3247 }
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:826

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

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

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

Referenced by expandRecordVariable(), and markTargetListOrigin().

◆ GetNSItemByRangeTablePosn()

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

Definition at line 510 of file parse_relation.c.

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

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

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

◆ GetRTEByRangeTablePosn()

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

Definition at line 537 of file parse_relation.c.

540 {
541  while (sublevels_up-- > 0)
542  {
543  pstate = pstate->parentParseState;
544  Assert(pstate != NULL);
545  }
546  Assert(varno > 0 && varno <= list_length(pstate->p_rtable));
547  return rt_fetch(varno, pstate->p_rtable);
548 }
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31

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

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

◆ getRTEPermissionInfo()

RTEPermissionInfo* getRTEPermissionInfo ( List rteperminfos,
RangeTblEntry rte 
)

Definition at line 3862 of file parse_relation.c.

3863 {
3864  RTEPermissionInfo *perminfo;
3865 
3866  if (rte->perminfoindex == 0 ||
3867  rte->perminfoindex > list_length(rteperminfos))
3868  elog(ERROR, "invalid perminfoindex %d in RTE with relid %u",
3869  rte->perminfoindex, rte->relid);
3870  perminfo = list_nth_node(RTEPermissionInfo, rteperminfos,
3871  rte->perminfoindex - 1);
3872  if (perminfo->relid != rte->relid)
3873  elog(ERROR, "permission info at index %u (with relid=%u) does not match provided RTE (with relid=%u)",
3874  rte->perminfoindex, perminfo->relid, rte->relid);
3875 
3876  return perminfo;
3877 }
#define list_nth_node(type, list, n)
Definition: pg_list.h:325

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

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

◆ isFutureCTE()

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

Definition at line 313 of file parse_relation.c.

314 {
315  for (; pstate != NULL; pstate = pstate->parentParseState)
316  {
317  ListCell *lc;
318 
319  foreach(lc, pstate->p_future_ctes)
320  {
321  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
322 
323  if (strcmp(cte->ctename, refname) == 0)
324  return true;
325  }
326  }
327  return false;
328 }
List * p_future_ctes
Definition: parse_node.h:196

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

2549 {
2550  ListCell *l;
2551 
2552  /*
2553  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2554  * parent level, then act as though there's a generic FOR UPDATE here.
2555  */
2556  if (pstate->p_locked_from_parent)
2557  return true;
2558 
2559  foreach(l, pstate->p_locking_clause)
2560  {
2561  LockingClause *lc = (LockingClause *) lfirst(l);
2562 
2563  if (lc->lockedRels == NIL)
2564  {
2565  /* all tables used in query */
2566  return true;
2567  }
2568  else if (refname != NULL)
2569  {
2570  /* just the named tables */
2571  ListCell *l2;
2572 
2573  foreach(l2, lc->lockedRels)
2574  {
2575  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2576 
2577  if (strcmp(refname, thisrel->relname) == 0)
2578  return true;
2579  }
2580  }
2581  }
2582  return false;
2583 }
List * lockedRels
Definition: parsenodes.h:798
bool p_locked_from_parent
Definition: parse_node.h:206
List * p_locking_clause
Definition: parse_node.h:205

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

3784 {
3785  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3786 }
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 3789 of file parse_relation.c.

3790 {
3791  if (node == NULL)
3792  return false;
3793 
3794  if (IsA(node, Query))
3795  {
3796  Query *query = (Query *) node;
3797  ListCell *rtable;
3798 
3799  foreach(rtable, query->rtable)
3800  {
3801  RangeTblEntry *rte = lfirst(rtable);
3802 
3803  if (rte->rtekind == RTE_RELATION)
3804  {
3806  char relpersistence = rel->rd_rel->relpersistence;
3807 
3809  if (relpersistence == RELPERSISTENCE_TEMP)
3810  return true;
3811  }
3812  }
3813 
3814  return query_tree_walker(query,
3816  context,
3818  }
3819 
3820  return expression_tree_walker(node,
3822  context);
3823 }
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:156
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:25
List * rtable
Definition: parsenodes.h:155
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

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

1035 {
1036  RangeTblEntry *rte = rt_fetch(rtindex, pstate->p_rtable);
1037 
1038  if (rte->rtekind == RTE_RELATION)
1039  {
1040  RTEPermissionInfo *perminfo;
1041 
1042  /* Make sure the rel as a whole is marked for SELECT access */
1043  perminfo = getRTEPermissionInfo(pstate->p_rteperminfos, rte);
1044  perminfo->requiredPerms |= ACL_SELECT;
1045  /* Must offset the attnum to fit in a bitmapset */
1046  perminfo->selectedCols =
1047  bms_add_member(perminfo->selectedCols,
1049  }
1050  else if (rte->rtekind == RTE_JOIN)
1051  {
1052  if (col == InvalidAttrNumber)
1053  {
1054  /*
1055  * A whole-row reference to a join has to be treated as whole-row
1056  * references to the two inputs.
1057  */
1058  JoinExpr *j;
1059 
1060  if (rtindex > 0 && rtindex <= list_length(pstate->p_joinexprs))
1061  j = list_nth_node(JoinExpr, pstate->p_joinexprs, rtindex - 1);
1062  else
1063  j = NULL;
1064  if (j == NULL)
1065  elog(ERROR, "could not find JoinExpr for whole-row reference");
1066 
1067  /* Note: we can't see FromExpr here */
1068  if (IsA(j->larg, RangeTblRef))
1069  {
1070  int varno = ((RangeTblRef *) j->larg)->rtindex;
1071 
1072  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1073  }
1074  else if (IsA(j->larg, JoinExpr))
1075  {
1076  int varno = ((JoinExpr *) j->larg)->rtindex;
1077 
1078  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1079  }
1080  else
1081  elog(ERROR, "unrecognized node type: %d",
1082  (int) nodeTag(j->larg));
1083  if (IsA(j->rarg, RangeTblRef))
1084  {
1085  int varno = ((RangeTblRef *) j->rarg)->rtindex;
1086 
1087  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1088  }
1089  else if (IsA(j->rarg, JoinExpr))
1090  {
1091  int varno = ((JoinExpr *) j->rarg)->rtindex;
1092 
1093  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1094  }
1095  else
1096  elog(ERROR, "unrecognized node type: %d",
1097  (int) nodeTag(j->rarg));
1098  }
1099  else
1100  {
1101  /*
1102  * Join alias Vars for ordinary columns must refer to merged JOIN
1103  * USING columns. We don't need to do anything here, because the
1104  * join input columns will also be referenced in the join's qual
1105  * clause, and will get marked for select privilege there.
1106  */
1107  }
1108  }
1109  /* other RTE types don't require privilege marking */
1110 }
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:739
#define nodeTag(nodeptr)
Definition: nodes.h:122
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
static void markRTEForSelectPriv(ParseState *pstate, int rtindex, AttrNumber col)
List * p_joinexprs
Definition: parse_node.h:189
Bitmapset * selectedCols
Definition: parsenodes.h:1210
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

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

Referenced by markVarForSelectPriv().

◆ markVarForSelectPriv()

void markVarForSelectPriv ( ParseState pstate,
Var var 
)

Definition at line 1118 of file parse_relation.c.

1119 {
1120  Index lv;
1121 
1122  Assert(IsA(var, Var));
1123  /* Find the appropriate pstate if it's an uplevel Var */
1124  for (lv = 0; lv < var->varlevelsup; lv++)
1125  pstate = pstate->parentParseState;
1126  markRTEForSelectPriv(pstate, var->varno, var->varattno);
1127 }
AttrNumber varattno
Definition: primnodes.h:217
int varno
Definition: primnodes.h:212

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

1386 {
1387  Relation rel;
1388  ParseCallbackState pcbstate;
1389 
1390  setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1391  rel = table_openrv_extended(relation, lockmode, true);
1392  if (rel == NULL)
1393  {
1394  if (relation->schemaname)
1395  ereport(ERROR,
1397  errmsg("relation \"%s.%s\" does not exist",
1398  relation->schemaname, relation->relname)));
1399  else
1400  {
1401  /*
1402  * An unqualified name might have been meant as a reference to
1403  * some not-yet-in-scope CTE. The bare "does not exist" message
1404  * has proven remarkably unhelpful for figuring out such problems,
1405  * so we take pains to offer a specific hint.
1406  */
1407  if (isFutureCTE(pstate, relation->relname))
1408  ereport(ERROR,
1410  errmsg("relation \"%s\" does not exist",
1411  relation->relname),
1412  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1413  relation->relname),
1414  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1415  else
1416  ereport(ERROR,
1418  errmsg("relation \"%s\" does not exist",
1419  relation->relname)));
1420  }
1421  }
1423  return rel;
1424 }
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:74
Relation table_openrv_extended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
Definition: table.c:103

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

Referenced by addRangeTableEntry(), and setTargetTable().

◆ refnameNamespaceItem()

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

Definition at line 129 of file parse_relation.c.

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

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

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

◆ rte_visible_if_lateral()

static bool rte_visible_if_lateral ( ParseState pstate,
RangeTblEntry rte 
)
static

Definition at line 3745 of file parse_relation.c.

3746 {
3747  ParseNamespaceItem *nsitem;
3748 
3749  /* If LATERAL *is* active, we're clearly barking up the wrong tree */
3750  if (pstate->p_lateral_active)
3751  return false;
3752  nsitem = findNSItemForRTE(pstate, rte);
3753  if (nsitem)
3754  {
3755  /* Found it, report whether it's LATERAL-only */
3756  return nsitem->p_lateral_only && nsitem->p_lateral_ok;
3757  }
3758  return false;
3759 }
static ParseNamespaceItem * findNSItemForRTE(ParseState *pstate, RangeTblEntry *rte)

References findNSItemForRTE(), ParseState::p_lateral_active, ParseNamespaceItem::p_lateral_ok, and ParseNamespaceItem::p_lateral_only.

Referenced by errorMissingColumn(), and errorMissingRTE().

◆ rte_visible_if_qualified()

static bool rte_visible_if_qualified ( ParseState pstate,
RangeTblEntry rte 
)
static

Definition at line 3765 of file parse_relation.c.

3766 {
3767  ParseNamespaceItem *nsitem = findNSItemForRTE(pstate, rte);
3768 
3769  if (nsitem)
3770  {
3771  /* Found it, report whether it's relation-only */
3772  return nsitem->p_rel_visible && !nsitem->p_cols_visible;
3773  }
3774  return false;
3775 }

References findNSItemForRTE(), ParseNamespaceItem::p_cols_visible, and ParseNamespaceItem::p_rel_visible.

Referenced by errorMissingColumn().

◆ scanNameSpaceForCTE()

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

Definition at line 282 of file parse_relation.c.

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

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

Referenced by getNSItemForSpecialRelationTypes(), and searchRangeTableForRel().

◆ scanNameSpaceForENR()

bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 335 of file parse_relation.c.

336 {
337  return name_matches_visible_ENR(pstate, refname);
338 }
bool name_matches_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:20

References name_matches_visible_ENR().

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

◆ scanNameSpaceForRefname()

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

Definition at line 200 of file parse_relation.c.

201 {
202  ParseNamespaceItem *result = NULL;
203  ListCell *l;
204 
205  foreach(l, pstate->p_namespace)
206  {
208 
209  /* Ignore columns-only items */
210  if (!nsitem->p_rel_visible)
211  continue;
212  /* If not inside LATERAL, ignore lateral-only items */
213  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
214  continue;
215 
216  if (strcmp(nsitem->p_names->aliasname, refname) == 0)
217  {
218  if (result)
219  ereport(ERROR,
220  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
221  errmsg("table reference \"%s\" is ambiguous",
222  refname),
223  parser_errposition(pstate, location)));
224  check_lateral_ref_ok(pstate, nsitem, location);
225  result = nsitem;
226  }
227  }
228  return result;
229 }

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

241 {
242  ParseNamespaceItem *result = NULL;
243  ListCell *l;
244 
245  foreach(l, pstate->p_namespace)
246  {
248  RangeTblEntry *rte = nsitem->p_rte;
249 
250  /* Ignore columns-only items */
251  if (!nsitem->p_rel_visible)
252  continue;
253  /* If not inside LATERAL, ignore lateral-only items */
254  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
255  continue;
256 
257  /* yes, the test for alias == NULL should be there... */
258  if (rte->rtekind == RTE_RELATION &&
259  rte->relid == relid &&
260  rte->alias == NULL)
261  {
262  if (result)
263  ereport(ERROR,
264  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
265  errmsg("table reference %u is ambiguous",
266  relid),
267  parser_errposition(pstate, location)));
268  check_lateral_ref_ok(pstate, nsitem, location);
269  result = nsitem;
270  }
271  }
272  return result;
273 }

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

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

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

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

951 {
952  ParseState *orig_pstate = pstate;
953  FuzzyAttrMatchState *fuzzystate = palloc(sizeof(FuzzyAttrMatchState));
954 
955  fuzzystate->distance = MAX_FUZZY_DISTANCE + 1;
956  fuzzystate->rfirst = NULL;
957  fuzzystate->rsecond = NULL;
958  fuzzystate->rexact1 = NULL;
959  fuzzystate->rexact2 = NULL;
960 
961  while (pstate != NULL)
962  {
963  ListCell *l;
964 
965  foreach(l, pstate->p_rtable)
966  {
967  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
968  int fuzzy_rte_penalty = 0;
969  int attnum;
970 
971  /*
972  * Typically, it is not useful to look for matches within join
973  * RTEs; they effectively duplicate other RTEs for our purposes,
974  * and if a match is chosen from a join RTE, an unhelpful alias is
975  * displayed in the final diagnostic message.
976  */
977  if (rte->rtekind == RTE_JOIN)
978  continue;
979 
980  /*
981  * If the user didn't specify an alias, then matches against one
982  * RTE are as good as another. But if the user did specify an
983  * alias, then we want at least a fuzzy - and preferably an exact
984  * - match for the range table entry.
985  */
986  if (alias != NULL)
987  fuzzy_rte_penalty =
988  varstr_levenshtein_less_equal(alias, strlen(alias),
989  rte->eref->aliasname,
990  strlen(rte->eref->aliasname),
991  1, 1, 1,
992  MAX_FUZZY_DISTANCE + 1,
993  true);
994 
995  /*
996  * Scan for a matching column, and update fuzzystate. Non-exact
997  * matches are dealt with inside scanRTEForColumn, but exact
998  * matches are handled here. (There won't be more than one exact
999  * match in the same RTE, else we'd have thrown error earlier.)
1000  */
1001  attnum = scanRTEForColumn(orig_pstate, rte, rte->eref,
1002  colname, location,
1003  fuzzy_rte_penalty, fuzzystate);
1004  if (attnum != InvalidAttrNumber && fuzzy_rte_penalty == 0)
1005  {
1006  if (fuzzystate->rexact1 == NULL)
1007  {
1008  fuzzystate->rexact1 = rte;
1009  fuzzystate->exact1 = attnum;
1010  }
1011  else
1012  {
1013  /* Needn't worry about overwriting previous rexact2 */
1014  fuzzystate->rexact2 = rte;
1015  fuzzystate->exact2 = attnum;
1016  }
1017  }
1018  }
1019 
1020  pstate = pstate->parentParseState;
1021  }
1022 
1023  return fuzzystate;
1024 }
#define MAX_FUZZY_DISTANCE
RangeTblEntry * rfirst
RangeTblEntry * rexact1
RangeTblEntry * rexact2
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, attnum, FuzzyAttrMatchState::distance, RangeTblEntry::eref, FuzzyAttrMatchState::exact1, FuzzyAttrMatchState::exact2, InvalidAttrNumber, lfirst, MAX_FUZZY_DISTANCE, ParseState::p_rtable, palloc(), ParseState::parentParseState, FuzzyAttrMatchState::rexact1, FuzzyAttrMatchState::rexact2, FuzzyAttrMatchState::rfirst, FuzzyAttrMatchState::rsecond, RTE_JOIN, RangeTblEntry::rtekind, scanRTEForColumn(), and varstr_levenshtein_less_equal().

Referenced by errorMissingColumn().

◆ searchRangeTableForRel()

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

Definition at line 356 of file parse_relation.c.

357 {
358  const char *refname = relation->relname;
359  Oid relId = InvalidOid;
360  CommonTableExpr *cte = NULL;
361  bool isenr = false;
362  Index ctelevelsup = 0;
363  Index levelsup;
364 
365  /*
366  * If it's an unqualified name, check for possible CTE matches. A CTE
367  * hides any real relation matches. If no CTE, look for a matching
368  * relation.
369  *
370  * NB: It's not critical that RangeVarGetRelid return the correct answer
371  * here in the face of concurrent DDL. If it doesn't, the worst case
372  * scenario is a less-clear error message. Also, the tables involved in
373  * the query are already locked, which reduces the number of cases in
374  * which surprising behavior can occur. So we do the name lookup
375  * unlocked.
376  */
377  if (!relation->schemaname)
378  {
379  cte = scanNameSpaceForCTE(pstate, refname, &ctelevelsup);
380  if (!cte)
381  isenr = scanNameSpaceForENR(pstate, refname);
382  }
383 
384  if (!cte && !isenr)
385  relId = RangeVarGetRelid(relation, NoLock, true);
386 
387  /* Now look for RTEs matching either the relation/CTE/ENR or the alias */
388  for (levelsup = 0;
389  pstate != NULL;
390  pstate = pstate->parentParseState, levelsup++)
391  {
392  ListCell *l;
393 
394  foreach(l, pstate->p_rtable)
395  {
396  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
397 
398  if (rte->rtekind == RTE_RELATION &&
399  OidIsValid(relId) &&
400  rte->relid == relId)
401  return rte;
402  if (rte->rtekind == RTE_CTE &&
403  cte != NULL &&
404  rte->ctelevelsup + levelsup == ctelevelsup &&
405  strcmp(rte->ctename, refname) == 0)
406  return rte;
407  if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
408  isenr &&
409  strcmp(rte->enrname, refname) == 0)
410  return rte;
411  if (strcmp(rte->eref->aliasname, refname) == 0)
412  return rte;
413  }
414  }
415  return NULL;
416 }
#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 3473 of file parse_relation.c.

3474 {
3475  const FormData_pg_attribute *sysatt;
3476 
3477  sysatt = SystemAttributeByName(attname);
3478  if (sysatt != NULL)
3479  return sysatt->attnum;
3480  return InvalidAttrNumber;
3481 }
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 587 of file parse_relation.c.

590 {
591  int columndistance;
592  int matchlen;
593 
594  /* Bail before computing the Levenshtein distance if there's no hope. */
595  if (fuzzy_rte_penalty > fuzzystate->distance)
596  return;
597 
598  /*
599  * Outright reject dropped columns, which can appear here with apparent
600  * empty actual names, per remarks within scanRTEForColumn().
601  */
602  if (actual[0] == '\0')
603  return;
604 
605  /* Use Levenshtein to compute match distance. */
606  matchlen = strlen(match);
607  columndistance =
608  varstr_levenshtein_less_equal(actual, strlen(actual), match, matchlen,
609  1, 1, 1,
610  fuzzystate->distance + 1
611  - fuzzy_rte_penalty,
612  true);
613 
614  /*
615  * If more than half the characters are different, don't treat it as a
616  * match, to avoid making ridiculous suggestions.
617  */
618  if (columndistance > matchlen / 2)
619  return;
620 
621  /*
622  * From this point on, we can ignore the distinction between the RTE-name
623  * distance and the column-name distance.
624  */
625  columndistance += fuzzy_rte_penalty;
626 
627  /*
628  * If the new distance is less than or equal to that of the best match
629  * found so far, update fuzzystate.
630  */
631  if (columndistance < fuzzystate->distance)
632  {
633  /* Store new lowest observed distance as first/only match */
634  fuzzystate->distance = columndistance;
635  fuzzystate->rfirst = rte;
636  fuzzystate->first = attnum;
637  fuzzystate->rsecond = NULL;
638  }
639  else if (columndistance == fuzzystate->distance)
640  {
641  /* If we already have a match of this distance, update state */
642  if (fuzzystate->rsecond != NULL)
643  {
644  /*
645  * Too many matches at same distance. Clearly, this value of
646  * distance is too low a bar, so drop these entries while keeping
647  * the current distance value, so that only smaller distances will
648  * be considered interesting. Only if we find something of lower
649  * distance will we re-populate rfirst (via the stanza above).
650  */
651  fuzzystate->rfirst = NULL;
652  fuzzystate->rsecond = NULL;
653  }
654  else if (fuzzystate->rfirst != NULL)
655  {
656  /* Record as provisional second match */
657  fuzzystate->rsecond = rte;
658  fuzzystate->second = attnum;
659  }
660  else
661  {
662  /*
663  * Do nothing. When rfirst is NULL, distance is more than what we
664  * want to consider acceptable, so we should ignore this match.
665  */
666  }
667  }
668 }

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

Referenced by scanRTEForColumn().