PostgreSQL Source Code git master
Loading...
Searching...
No Matches
parse_relation.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "access/htup_details.h"
#include "access/relation.h"
#include "access/table.h"
#include "catalog/heap.h"
#include "catalog/namespace.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/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, VarReturningType returning_type, 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, VarReturningType returning_type, 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)
 
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 markNullableIfNeeded (ParseState *pstate, Var *var)
 
void markVarForSelectPriv (ParseState *pstate, Var *var)
 
static void buildRelationAliases (TupleDesc tupdesc, Alias *alias, Alias *eref)
 
static charchooseScalarFunctionAlias (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, LOCKMODE lockmode)
 
ParseNamespaceItemaddRangeTableEntry (ParseState *pstate, RangeVar *relation, Alias *alias, bool inh, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForRelation (ParseState *pstate, Relation rel, LOCKMODE 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)
 
ParseNamespaceItemaddRangeTableEntryForGraphTable (ParseState *pstate, Oid graphid, GraphPattern *graph_pattern, List *columns, List *colnames, 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)
 
ParseNamespaceItemaddRangeTableEntryForGroup (ParseState *pstate, List *groupClauses)
 
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, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
 
ListexpandNSItemVars (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
 
ListexpandNSItemAttrs (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, bool require_col_privs, int location)
 
charget_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)
 
RTEPermissionInfoaddRTEPermissionInfo (List **rteperminfos, RangeTblEntry *rte)
 
RTEPermissionInfogetRTEPermissionInfo (List *rteperminfos, RangeTblEntry *rte)
 

Macro Definition Documentation

◆ MAX_FUZZY_DISTANCE

#define MAX_FUZZY_DISTANCE   3

Definition at line 72 of file parse_relation.c.

Function Documentation

◆ addNSItemToQuery()

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

Definition at line 2793 of file parse_relation.c.

2796{
2797 if (addToJoinList)
2798 {
2800
2801 rtr->rtindex = nsitem->p_rtindex;
2802 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2803 }
2805 {
2806 /* Set the new nsitem's visibility flags correctly */
2807 nsitem->p_rel_visible = addToRelNameSpace;
2808 nsitem->p_cols_visible = addToVarNameSpace;
2809 nsitem->p_lateral_only = false;
2810 nsitem->p_lateral_ok = true;
2811 pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2812 }
2813}
List * lappend(List *list, void *datum)
Definition list.c:339
#define makeNode(_type_)
Definition nodes.h:161
static int fb(int x)
List * p_namespace
Definition parse_node.h:218
List * p_joinlist
Definition parse_node.h:216

References fb(), lappend(), makeNode, ParseState::p_joinlist, and ParseState::p_namespace.

Referenced by addNSItemForReturning(), AddRelationNewConstraints(), AlterPolicy(), CreatePolicy(), CreateTriggerFiringOn(), DefineRelation(), DoCopy(), insert_property_records(), 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 1479 of file parse_relation.c.

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

References AccessShareLock, ACL_SELECT, addRTEPermissionInfo(), Alias::aliasname, Assert, buildNSItemFromTupleDesc(), buildRelationAliases(), fb(), isLockedRefname(), lappend(), list_length(), makeAlias(), makeNode, NIL, NoLock, ParseState::p_rtable, ParseState::p_rteperminfos, parserOpenTable(), RelationData::rd_att, RelationData::rd_rel, RelationGetRelid, RangeVar::relname, RowShareLock, RTE_RELATION, and table_close().

Referenced by transformTableEntry().

◆ addRangeTableEntryForCTE()

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

Definition at line 2415 of file parse_relation.c.

2420{
2422 Alias *alias = rv->alias;
2423 char *refname = alias ? alias->aliasname : cte->ctename;
2424 Alias *eref;
2425 int numaliases;
2426 int varattno;
2427 ListCell *lc;
2428 int n_dontexpand_columns = 0;
2430
2431 Assert(pstate != NULL);
2432
2433 rte->rtekind = RTE_CTE;
2434 rte->ctename = cte->ctename;
2435 rte->ctelevelsup = levelsup;
2436
2437 /* Self-reference if and only if CTE's parse analysis isn't completed */
2438 rte->self_reference = !IsA(cte->ctequery, Query);
2439 Assert(cte->cterecursive || !rte->self_reference);
2440 /* Bump the CTE's refcount if this isn't a self-reference */
2441 if (!rte->self_reference)
2442 cte->cterefcount++;
2443
2444 /*
2445 * We throw error if the CTE is INSERT/UPDATE/DELETE/MERGE without
2446 * RETURNING. This won't get checked in case of a self-reference, but
2447 * that's OK because data-modifying CTEs aren't allowed to be recursive
2448 * anyhow.
2449 */
2450 if (IsA(cte->ctequery, Query))
2451 {
2452 Query *ctequery = (Query *) cte->ctequery;
2453
2454 if (ctequery->commandType != CMD_SELECT &&
2455 ctequery->returningList == NIL)
2456 ereport(ERROR,
2458 errmsg("WITH query \"%s\" does not have a RETURNING clause",
2459 cte->ctename),
2460 parser_errposition(pstate, rv->location)));
2461 }
2462
2463 rte->coltypes = list_copy(cte->ctecoltypes);
2464 rte->coltypmods = list_copy(cte->ctecoltypmods);
2465 rte->colcollations = list_copy(cte->ctecolcollations);
2466
2467 rte->alias = alias;
2468 if (alias)
2469 eref = copyObject(alias);
2470 else
2471 eref = makeAlias(refname, NIL);
2472 numaliases = list_length(eref->colnames);
2473
2474 /* fill in any unspecified alias columns */
2475 varattno = 0;
2476 foreach(lc, cte->ctecolnames)
2477 {
2478 varattno++;
2479 if (varattno > numaliases)
2480 eref->colnames = lappend(eref->colnames, lfirst(lc));
2481 }
2482 if (varattno < numaliases)
2483 ereport(ERROR,
2485 errmsg("table \"%s\" has %d columns available but %d columns specified",
2486 refname, varattno, numaliases)));
2487
2488 rte->eref = eref;
2489
2490 if (cte->search_clause)
2491 {
2492 rte->eref->colnames = lappend(rte->eref->colnames, makeString(cte->search_clause->search_seq_column));
2493 if (cte->search_clause->search_breadth_first)
2494 rte->coltypes = lappend_oid(rte->coltypes, RECORDOID);
2495 else
2496 rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2497 rte->coltypmods = lappend_int(rte->coltypmods, -1);
2498 rte->colcollations = lappend_oid(rte->colcollations, InvalidOid);
2499
2501 }
2502
2503 if (cte->cycle_clause)
2504 {
2505 rte->eref->colnames = lappend(rte->eref->colnames, makeString(cte->cycle_clause->cycle_mark_column));
2506 rte->coltypes = lappend_oid(rte->coltypes, cte->cycle_clause->cycle_mark_type);
2507 rte->coltypmods = lappend_int(rte->coltypmods, cte->cycle_clause->cycle_mark_typmod);
2508 rte->colcollations = lappend_oid(rte->colcollations, cte->cycle_clause->cycle_mark_collation);
2509
2510 rte->eref->colnames = lappend(rte->eref->colnames, makeString(cte->cycle_clause->cycle_path_column));
2511 rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2512 rte->coltypmods = lappend_int(rte->coltypmods, -1);
2513 rte->colcollations = lappend_oid(rte->colcollations, InvalidOid);
2514
2516 }
2517
2518 /*
2519 * Set flags and access permissions.
2520 *
2521 * Subqueries are never checked for access rights, so no need to perform
2522 * addRTEPermissionInfo().
2523 */
2524 rte->lateral = false;
2525 rte->inFromCl = inFromCl;
2526
2527 /*
2528 * Add completed RTE to pstate's range table list, so that we know its
2529 * index. But we don't add it to the join list --- caller must do that if
2530 * appropriate.
2531 */
2532 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2533
2534 /*
2535 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2536 * list --- caller must do that if appropriate.
2537 */
2539 rte->coltypes, rte->coltypmods,
2540 rte->colcollations);
2541
2542 /*
2543 * The columns added by search and cycle clauses are not included in star
2544 * expansion in queries contained in the CTE.
2545 */
2546 if (rte->ctelevelsup > 0)
2547 for (int i = 0; i < n_dontexpand_columns; i++)
2548 psi->p_nscolumns[list_length(psi->p_names->colnames) - 1 - i].p_dontexpand = true;
2549
2550 return psi;
2551}
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
int i
Definition isn.c:77
List * list_copy(const List *oldlist)
Definition list.c:1573
List * lappend_int(List *list, int datum)
Definition list.c:357
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
@ CMD_SELECT
Definition nodes.h:275
static char * errmsg
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
@ RTE_CTE
#define lfirst(lc)
Definition pg_list.h:172
#define InvalidOid
Alias * alias
Definition primnodes.h:93
ParseLoc location
Definition primnodes.h:96
String * makeString(char *str)
Definition value.c:63

References RangeVar::alias, Alias::aliasname, Assert, buildNSItemFromLists(), CMD_SELECT, Query::commandType, copyObject, CommonTableExpr::ctename, CommonTableExpr::ctequery, ereport, errcode(), errmsg, ERROR, fb(), i, InvalidOid, IsA, lappend(), lappend_int(), lappend_oid(), lfirst, list_copy(), list_length(), RangeVar::location, makeAlias(), makeNode, makeString(), NIL, ParseState::p_rtable, parser_errposition(), Query::returningList, and RTE_CTE.

Referenced by getNSItemForSpecialRelationTypes().

◆ addRangeTableEntryForENR()

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

Definition at line 2567 of file parse_relation.c.

2570{
2572 Alias *alias = rv->alias;
2573 char *refname = alias ? alias->aliasname : rv->relname;
2575 TupleDesc tupdesc;
2576 int attno;
2577
2578 Assert(pstate != NULL);
2579 enrmd = get_visible_ENR(pstate, rv->relname);
2580 Assert(enrmd != NULL);
2581
2582 switch (enrmd->enrtype)
2583 {
2585 rte->rtekind = RTE_NAMEDTUPLESTORE;
2586 break;
2587
2588 default:
2589 elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2590 return NULL; /* for fussy compilers */
2591 }
2592
2593 /*
2594 * Record dependency on a relation. This allows plans to be invalidated
2595 * if they access transition tables linked to a table that is altered.
2596 */
2597 rte->relid = enrmd->reliddesc;
2598
2599 /*
2600 * Build the list of effective column names using user-supplied aliases
2601 * and/or actual column names.
2602 */
2603 tupdesc = ENRMetadataGetTupDesc(enrmd);
2604 rte->eref = makeAlias(refname, NIL);
2605 buildRelationAliases(tupdesc, alias, rte->eref);
2606
2607 /* Record additional data for ENR, including column type info */
2608 rte->enrname = enrmd->name;
2609 rte->enrtuples = enrmd->enrtuples;
2610 rte->coltypes = NIL;
2611 rte->coltypmods = NIL;
2612 rte->colcollations = NIL;
2613 for (attno = 1; attno <= tupdesc->natts; ++attno)
2614 {
2615 Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2616
2617 if (att->attisdropped)
2618 {
2619 /* Record zeroes for a dropped column */
2620 rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2621 rte->coltypmods = lappend_int(rte->coltypmods, 0);
2622 rte->colcollations = lappend_oid(rte->colcollations, InvalidOid);
2623 }
2624 else
2625 {
2626 /* Let's just make sure we can tell this isn't dropped */
2627 if (att->atttypid == InvalidOid)
2628 elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2629 rv->relname);
2630 rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2631 rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2632 rte->colcollations = lappend_oid(rte->colcollations,
2633 att->attcollation);
2634 }
2635 }
2636
2637 /*
2638 * Set flags and access permissions.
2639 *
2640 * ENRs are never checked for access rights, so no need to perform
2641 * addRTEPermissionInfo().
2642 */
2643 rte->lateral = false;
2644 rte->inFromCl = inFromCl;
2645
2646 /*
2647 * Add completed RTE to pstate's range table list, so that we know its
2648 * index. But we don't add it to the join list --- caller must do that if
2649 * appropriate.
2650 */
2651 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2652
2653 /*
2654 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2655 * list --- caller must do that if appropriate.
2656 */
2658 tupdesc);
2659}
#define elog(elevel,...)
Definition elog.h:226
EphemeralNamedRelationMetadata get_visible_ENR(ParseState *pstate, const char *refname)
Definition parse_enr.c:26
@ RTE_NAMEDTUPLESTORE
FormData_pg_attribute * Form_pg_attribute
TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
@ ENR_NAMED_TUPLESTORE
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178

References RangeVar::alias, Alias::aliasname, Assert, buildNSItemFromTupleDesc(), buildRelationAliases(), elog, ENR_NAMED_TUPLESTORE, ENRMetadataGetTupDesc(), ERROR, fb(), get_visible_ENR(), InvalidOid, lappend(), lappend_int(), lappend_oid(), list_length(), makeAlias(), makeNode, TupleDescData::natts, NIL, ParseState::p_rtable, RangeVar::relname, RTE_NAMEDTUPLESTORE, 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 1739 of file parse_relation.c.

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

References Alias::aliasname, Assert, buildNSItemFromTupleDesc(), buildRelationAliases(), CheckAttributeNamesTypes(), CHKATYPE_ANYRECORD, chooseScalarFunctionAlias(), ColumnDef::colname, CreateTemplateTupleDesc(), ereport, errcode(), errmsg, ERROR, exprCollation(), exprLocation(), exprType(), exprTypmod(), fb(), format_type_be(), forthree, RangeTblFunction::funcexpr, funcname, get_expr_result_type(), GetColumnDefCollation(), i, InvalidOid, j, lappend(), lappend_int(), lappend_oid(), lfirst, linitial, list_length(), ColumnDef::location, makeAlias(), makeNode, makeString(), MaxHeapAttributeNumber, MaxTupleAttributeNumber, TupleDescData::natts, NIL, ParseState::p_rtable, palloc_array, parser_errposition(), pstrdup(), RTE_FUNCTION, TypeName::setof, TupleDescCopyEntry(), TupleDescFinalize(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, TYPEFUNC_SCALAR, ColumnDef::typeName, and typenameTypeIdAndMod().

Referenced by transformRangeFunction().

◆ addRangeTableEntryForGraphTable()

ParseNamespaceItem * addRangeTableEntryForGraphTable ( ParseState pstate,
Oid  graphid,
GraphPattern graph_pattern,
List columns,
List colnames,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 2135 of file parse_relation.c.

2143{
2145 char *refname = alias ? alias->aliasname : pstrdup("graph_table");
2146 Alias *eref;
2147 int numaliases;
2148 int varattno;
2149 ListCell *lc;
2150 List *coltypes = NIL;
2151 List *coltypmods = NIL;
2155
2156 Assert(pstate != NULL);
2157
2158 rte->rtekind = RTE_GRAPH_TABLE;
2159 rte->relid = graphid;
2160 rte->relkind = RELKIND_PROPGRAPH;
2161 rte->graph_pattern = graph_pattern;
2162 rte->graph_table_columns = columns;
2163 rte->alias = alias;
2164 rte->rellockmode = AccessShareLock;
2165
2166 eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2167
2168 if (!eref->colnames)
2169 eref->colnames = colnames;
2170
2171 numaliases = list_length(eref->colnames);
2172
2173 /* fill in any unspecified alias columns */
2174 varattno = 0;
2175 foreach(lc, colnames)
2176 {
2177 varattno++;
2178 if (varattno > numaliases)
2179 eref->colnames = lappend(eref->colnames, lfirst(lc));
2180 }
2181 if (varattno < numaliases)
2182 ereport(ERROR,
2184 errmsg("GRAPH_TABLE \"%s\" has %d columns available but %d columns specified",
2185 refname, varattno, numaliases)));
2186
2187 rte->eref = eref;
2188
2189 foreach(lc, columns)
2190 {
2192 Node *colexpr = (Node *) te->expr;
2193
2197 }
2198
2199 /*
2200 * Set flags and access permissions.
2201 */
2202 rte->lateral = lateral;
2203 rte->inFromCl = inFromCl;
2204
2206 perminfo->requiredPerms = ACL_SELECT;
2207
2208 /*
2209 * Add completed RTE to pstate's range table list, so that we know its
2210 * index. But we don't add it to the join list --- caller must do that if
2211 * appropriate.
2212 */
2213 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2214
2215 /*
2216 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2217 * list --- caller must do that if appropriate.
2218 */
2221
2222 nsitem->p_perminfo = perminfo;
2223
2224 return nsitem;
2225}
@ RTE_GRAPH_TABLE
#define lfirst_node(type, lc)
Definition pg_list.h:176
List * colnames
Definition primnodes.h:53
Expr * expr
Definition primnodes.h:2265

References AccessShareLock, ACL_SELECT, addRTEPermissionInfo(), Alias::aliasname, Assert, buildNSItemFromLists(), copyObject, ereport, errcode(), errmsg, ERROR, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), fb(), lappend(), lappend_int(), lappend_oid(), lfirst, lfirst_node, list_length(), makeAlias(), makeNode, NIL, ParseState::p_rtable, ParseState::p_rteperminfos, pstrdup(), and RTE_GRAPH_TABLE.

Referenced by transformRangeGraphTable().

◆ addRangeTableEntryForGroup()

ParseNamespaceItem * addRangeTableEntryForGroup ( ParseState pstate,
List groupClauses 
)

Definition at line 2666 of file parse_relation.c.

2668{
2670 Alias *eref;
2671 List *groupexprs;
2672 List *coltypes,
2673 *coltypmods,
2675 ListCell *lc;
2677
2678 Assert(pstate != NULL);
2679
2680 rte->rtekind = RTE_GROUP;
2681 rte->alias = NULL;
2682
2683 eref = makeAlias("*GROUP*", NIL);
2684
2685 /* fill in any unspecified alias columns, and extract column type info */
2686 groupexprs = NIL;
2688 foreach(lc, groupClauses)
2689 {
2690 TargetEntry *te = (TargetEntry *) lfirst(lc);
2691 char *colname = te->resname ? pstrdup(te->resname) : "?column?";
2692
2693 eref->colnames = lappend(eref->colnames, makeString(colname));
2694
2695 groupexprs = lappend(groupexprs, copyObject(te->expr));
2696
2698 exprType((Node *) te->expr));
2700 exprTypmod((Node *) te->expr));
2702 exprCollation((Node *) te->expr));
2703 }
2704
2705 rte->eref = eref;
2706 rte->groupexprs = groupexprs;
2707
2708 /*
2709 * Set flags.
2710 *
2711 * The grouping step is never checked for access rights, so no need to
2712 * perform addRTEPermissionInfo().
2713 */
2714 rte->lateral = false;
2715 rte->inFromCl = false;
2716
2717 /*
2718 * Add completed RTE to pstate's range table list, so that we know its
2719 * index. But we don't add it to the join list --- caller must do that if
2720 * appropriate.
2721 */
2722 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2723
2724 /*
2725 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2726 * list --- caller must do that if appropriate.
2727 */
2730
2731 return nsitem;
2732}
@ RTE_GROUP

References Assert, buildNSItemFromLists(), copyObject, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), fb(), lappend(), lappend_int(), lappend_oid(), lfirst, list_length(), makeAlias(), makeNode, makeString(), NIL, ParseState::p_rtable, pstrdup(), and RTE_GROUP.

Referenced by parseCheckAggregates().

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

2327{
2329 Alias *eref;
2330 int numaliases;
2332
2333 Assert(pstate != NULL);
2334
2335 /*
2336 * Fail if join has too many columns --- we must be able to reference any
2337 * of the columns with an AttrNumber.
2338 */
2340 ereport(ERROR,
2342 errmsg("joins can have at most %d columns",
2343 MaxAttrNumber)));
2344
2345 rte->rtekind = RTE_JOIN;
2346 rte->relid = InvalidOid;
2347 rte->subquery = NULL;
2348 rte->jointype = jointype;
2349 rte->joinmergedcols = nummergedcols;
2350 rte->joinaliasvars = aliasvars;
2351 rte->joinleftcols = leftcols;
2352 rte->joinrightcols = rightcols;
2353 rte->join_using_alias = join_using_alias;
2354 rte->alias = alias;
2355
2356 eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
2357 numaliases = list_length(eref->colnames);
2358
2359 /* fill in any unspecified alias columns */
2360 if (numaliases < list_length(colnames))
2361 eref->colnames = list_concat(eref->colnames,
2362 list_copy_tail(colnames, numaliases));
2363
2364 if (numaliases > list_length(colnames))
2365 ereport(ERROR,
2367 errmsg("join expression \"%s\" has %d columns available but %d columns specified",
2368 eref->aliasname, list_length(colnames), numaliases)));
2369
2370 rte->eref = eref;
2371
2372 /*
2373 * Set flags and access permissions.
2374 *
2375 * Joins are never checked for access rights, so no need to perform
2376 * addRTEPermissionInfo().
2377 */
2378 rte->lateral = false;
2379 rte->inFromCl = inFromCl;
2380
2381 /*
2382 * Add completed RTE to pstate's range table list, so that we know its
2383 * index. But we don't add it to the join list --- caller must do that if
2384 * appropriate.
2385 */
2386 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2387
2388 /*
2389 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2390 * list --- caller must do that if appropriate.
2391 */
2393 nsitem->p_names = rte->eref;
2394 nsitem->p_rte = rte;
2395 nsitem->p_perminfo = NULL;
2396 nsitem->p_rtindex = list_length(pstate->p_rtable);
2397 nsitem->p_nscolumns = nscolumns;
2398 /* set default visibility flags; might get changed later */
2399 nsitem->p_rel_visible = true;
2400 nsitem->p_cols_visible = true;
2401 nsitem->p_lateral_only = false;
2402 nsitem->p_lateral_ok = true;
2403 nsitem->p_returning_type = VAR_RETURNING_DEFAULT;
2404
2405 return nsitem;
2406}
#define MaxAttrNumber
Definition attnum.h:24
#define palloc_object(type)
Definition fe_memutils.h:74
List * list_copy_tail(const List *oldlist, int nskip)
Definition list.c:1613
List * list_concat(List *list1, const List *list2)
Definition list.c:561
@ RTE_JOIN
@ VAR_RETURNING_DEFAULT
Definition primnodes.h:257

References Assert, copyObject, ereport, errcode(), errmsg, ERROR, fb(), InvalidOid, lappend(), list_concat(), list_copy_tail(), list_length(), makeAlias(), makeNode, MaxAttrNumber, NIL, ParseState::p_rtable, palloc_object, RTE_JOIN, and VAR_RETURNING_DEFAULT.

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

◆ addRangeTableEntryForRelation()

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

Definition at line 1572 of file parse_relation.c.

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

References AccessShareLock, ACL_SELECT, addRTEPermissionInfo(), Alias::aliasname, Assert, buildNSItemFromTupleDesc(), buildRelationAliases(), CheckRelationLockedByMe(), fb(), lappend(), list_length(), makeAlias(), makeNode, NIL, ParseState::p_rtable, ParseState::p_rteperminfos, RelationData::rd_att, RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, RowExclusiveLock, RowShareLock, and RTE_RELATION.

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

◆ addRangeTableEntryForSubquery()

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

Definition at line 1643 of file parse_relation.c.

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

References Assert, buildNSItemFromLists(), copyObject, ereport, errcode(), errmsg, ERROR, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), fb(), lappend(), lappend_int(), lappend_oid(), lfirst, list_length(), makeAlias(), makeNode, makeString(), NIL, ParseState::p_rtable, pstrdup(), TargetEntry::resno, RTE_SUBQUERY, and Query::targetList.

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

◆ addRangeTableEntryForTableFunc()

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

Definition at line 2056 of file parse_relation.c.

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

References Alias::aliasname, Assert, buildNSItemFromLists(), copyObject, ereport, errcode(), errmsg, ERROR, exprLocation(), fb(), TableFunc::functype, InvalidOid, lappend(), list_concat(), list_copy_tail(), list_length(), makeAlias(), makeNode, MaxTupleAttributeNumber, NIL, ParseState::p_rtable, parser_errposition(), pstrdup(), RTE_TABLEFUNC, and TFT_XMLTABLE.

Referenced by transformJsonTable(), and transformRangeTableFunc().

◆ addRangeTableEntryForValues()

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

Definition at line 2234 of file parse_relation.c.

2242{
2244 char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
2245 Alias *eref;
2246 int numaliases;
2247 int numcolumns;
2248
2249 Assert(pstate != NULL);
2250
2251 rte->rtekind = RTE_VALUES;
2252 rte->relid = InvalidOid;
2253 rte->subquery = NULL;
2254 rte->values_lists = exprs;
2255 rte->coltypes = coltypes;
2256 rte->coltypmods = coltypmods;
2257 rte->colcollations = colcollations;
2258 rte->alias = alias;
2259
2260 eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2261
2262 /* fill in any unspecified alias columns */
2263 numcolumns = list_length((List *) linitial(exprs));
2264 numaliases = list_length(eref->colnames);
2265 while (numaliases < numcolumns)
2266 {
2267 char attrname[64];
2268
2269 numaliases++;
2270 snprintf(attrname, sizeof(attrname), "column%d", numaliases);
2271 eref->colnames = lappend(eref->colnames,
2273 }
2274 if (numcolumns < numaliases)
2275 ereport(ERROR,
2277 errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
2278 refname, numcolumns, numaliases)));
2279
2280 rte->eref = eref;
2281
2282 /*
2283 * Set flags and access permissions.
2284 *
2285 * Subqueries are never checked for access rights, so no need to perform
2286 * addRTEPermissionInfo().
2287 */
2288 rte->lateral = lateral;
2289 rte->inFromCl = inFromCl;
2290
2291 /*
2292 * Add completed RTE to pstate's range table list, so that we know its
2293 * index. But we don't add it to the join list --- caller must do that if
2294 * appropriate.
2295 */
2296 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2297
2298 /*
2299 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2300 * list --- caller must do that if appropriate.
2301 */
2303 rte->coltypes, rte->coltypmods,
2304 rte->colcollations);
2305}
@ RTE_VALUES
#define snprintf
Definition port.h:260

References Alias::aliasname, Assert, buildNSItemFromLists(), copyObject, ereport, errcode(), errmsg, ERROR, fb(), InvalidOid, lappend(), linitial, list_length(), makeAlias(), makeNode, makeString(), NIL, ParseState::p_rtable, pstrdup(), RTE_VALUES, and snprintf.

Referenced by transformInsertStmt(), and transformValuesClause().

◆ addRTEPermissionInfo()

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

Definition at line 4018 of file parse_relation.c.

4019{
4021
4022 Assert(OidIsValid(rte->relid));
4023 Assert(rte->perminfoindex == 0);
4024
4025 /* Nope, so make one and add to the list. */
4027 perminfo->relid = rte->relid;
4028 perminfo->inh = rte->inh;
4029 /* Other information is set by fetching the node as and where needed. */
4030
4031 *rteperminfos = lappend(*rteperminfos, perminfo);
4032
4033 /* Note its index (1-based!) */
4034 rte->perminfoindex = list_length(*rteperminfos);
4035
4036 return perminfo;
4037}
#define OidIsValid(objectId)
Definition c.h:860

References Assert, fb(), lappend(), list_length(), makeNode, and OidIsValid.

Referenced by add_rte_to_flat_rtable(), addRangeTableEntry(), addRangeTableEntryForGraphTable(), addRangeTableEntryForRelation(), create_edata_for_relation(), create_estate_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 3674 of file parse_relation.c.

3675{
3676 int i;
3677
3678 for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3679 {
3681
3682 if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3683 return i + 1;
3684 }
3685
3686 if (sysColOK)
3687 {
3689 return i;
3690 }
3691
3692 /* on failure */
3693 return InvalidAttrNumber;
3694}
#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
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:520

References attname, fb(), i, InvalidAttrNumber, namestrcmp(), RelationGetNumberOfAttributes, specialAttNum(), and TupleDescAttr().

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

◆ attnumAttName()

const NameData * attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3724 of file parse_relation.c.

3725{
3726 if (attid <= 0)
3727 {
3729
3731 return &sysatt->attname;
3732 }
3733 if (attid > rd->rd_att->natts)
3734 elog(ERROR, "invalid attribute number %d", attid);
3735 return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3736}
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition heap.c:236
FormData_pg_attribute

References elog, ERROR, fb(), FormData_pg_attribute, SystemAttributeDefinition(), and TupleDescAttr().

Referenced by transformFkeyGetPrimaryKey().

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3766 of file parse_relation.c.

3767{
3768 if (attid <= 0)
3769 {
3770 /* All system attributes are of noncollatable types. */
3771 return InvalidOid;
3772 }
3773 if (attid > rd->rd_att->natts)
3774 elog(ERROR, "invalid attribute number %d", attid);
3775 return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3776}

References elog, ERROR, fb(), InvalidOid, and TupleDescAttr().

Referenced by transformFkeyGetPrimaryKey().

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3746 of file parse_relation.c.

3747{
3748 if (attid <= 0)
3749 {
3751
3753 return sysatt->atttypid;
3754 }
3755 if (attid > rd->rd_att->natts)
3756 elog(ERROR, "invalid attribute number %d", attid);
3757 return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3758}

References elog, ERROR, fb(), FormData_pg_attribute, 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 1366 of file parse_relation.c.

1368{
1372 int varattno;
1373 ListCell *lct;
1374 ListCell *lcm;
1375 ListCell *lcc;
1376
1377 /* colnames must have the same number of entries as the nsitem */
1378 Assert(maxattrs == list_length(rte->eref->colnames));
1379
1382
1383 /* extract per-column data from the lists */
1386
1387 varattno = 0;
1389 lcm, coltypmods,
1391 {
1392 nscolumns[varattno].p_varno = rtindex;
1393 nscolumns[varattno].p_varattno = varattno + 1;
1394 nscolumns[varattno].p_vartype = lfirst_oid(lct);
1395 nscolumns[varattno].p_vartypmod = lfirst_int(lcm);
1396 nscolumns[varattno].p_varcollid = lfirst_oid(lcc);
1397 nscolumns[varattno].p_varnosyn = rtindex;
1398 nscolumns[varattno].p_varattnosyn = varattno + 1;
1399 varattno++;
1400 }
1401
1402 /* ... and build the nsitem */
1404 nsitem->p_names = rte->eref;
1405 nsitem->p_rte = rte;
1406 nsitem->p_rtindex = rtindex;
1407 nsitem->p_perminfo = NULL;
1408 nsitem->p_nscolumns = nscolumns;
1409 /* set default visibility flags; might get changed later */
1410 nsitem->p_rel_visible = true;
1411 nsitem->p_cols_visible = true;
1412 nsitem->p_lateral_only = false;
1413 nsitem->p_lateral_ok = true;
1414 nsitem->p_returning_type = VAR_RETURNING_DEFAULT;
1415
1416 return nsitem;
1417}
void * palloc0(Size size)
Definition mcxt.c:1417
#define lfirst_int(lc)
Definition pg_list.h:173
#define lfirst_oid(lc)
Definition pg_list.h:174

References Assert, fb(), forthree, lfirst_int, lfirst_oid, list_length(), palloc0(), palloc_object, and VAR_RETURNING_DEFAULT.

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

◆ buildNSItemFromTupleDesc()

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

Definition at line 1305 of file parse_relation.c.

1308{
1311 int maxattrs = tupdesc->natts;
1312 int varattno;
1313
1314 /* colnames must have the same number of entries as the nsitem */
1315 Assert(maxattrs == list_length(rte->eref->colnames));
1316
1317 /* extract per-column data from the tupdesc */
1320
1321 for (varattno = 0; varattno < maxattrs; varattno++)
1322 {
1323 Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1324
1325 /* For a dropped column, just leave the entry as zeroes */
1326 if (attr->attisdropped)
1327 continue;
1328
1329 nscolumns[varattno].p_varno = rtindex;
1330 nscolumns[varattno].p_varattno = varattno + 1;
1331 nscolumns[varattno].p_vartype = attr->atttypid;
1332 nscolumns[varattno].p_vartypmod = attr->atttypmod;
1333 nscolumns[varattno].p_varcollid = attr->attcollation;
1334 nscolumns[varattno].p_varnosyn = rtindex;
1335 nscolumns[varattno].p_varattnosyn = varattno + 1;
1336 }
1337
1338 /* ... and build the nsitem */
1340 nsitem->p_names = rte->eref;
1341 nsitem->p_rte = rte;
1342 nsitem->p_rtindex = rtindex;
1343 nsitem->p_perminfo = perminfo;
1344 nsitem->p_nscolumns = nscolumns;
1345 /* set default visibility flags; might get changed later */
1346 nsitem->p_rel_visible = true;
1347 nsitem->p_cols_visible = true;
1348 nsitem->p_lateral_only = false;
1349 nsitem->p_lateral_ok = true;
1350 nsitem->p_returning_type = VAR_RETURNING_DEFAULT;
1351
1352 return nsitem;
1353}

References Assert, fb(), list_length(), TupleDescData::natts, palloc0(), palloc_object, TupleDescAttr(), and VAR_RETURNING_DEFAULT.

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

◆ buildRelationAliases()

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

Definition at line 1188 of file parse_relation.c.

1189{
1190 int maxattrs = tupdesc->natts;
1191 List *aliaslist;
1193 int numaliases;
1194 int varattno;
1195 int numdropped = 0;
1196
1197 Assert(eref->colnames == NIL);
1198
1199 if (alias)
1200 {
1201 aliaslist = alias->colnames;
1204 /* We'll rebuild the alias colname list */
1205 alias->colnames = NIL;
1206 }
1207 else
1208 {
1209 aliaslist = NIL;
1210 aliaslc = NULL;
1211 numaliases = 0;
1212 }
1213
1214 for (varattno = 0; varattno < maxattrs; varattno++)
1215 {
1216 Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1218
1219 if (attr->attisdropped)
1220 {
1221 /* Always insert an empty string for a dropped column */
1223 if (aliaslc)
1224 alias->colnames = lappend(alias->colnames, attrname);
1225 numdropped++;
1226 }
1227 else if (aliaslc)
1228 {
1229 /* Use the next user-supplied alias */
1232 alias->colnames = lappend(alias->colnames, attrname);
1233 }
1234 else
1235 {
1236 attrname = makeString(pstrdup(NameStr(attr->attname)));
1237 /* we're done with the alias if any */
1238 }
1239
1240 eref->colnames = lappend(eref->colnames, attrname);
1241 }
1242
1243 /* Too many user-supplied aliases? */
1244 if (aliaslc)
1245 ereport(ERROR,
1247 errmsg("table \"%s\" has %d columns available but %d columns specified",
1248 eref->aliasname, maxattrs - numdropped, numaliases)));
1249}
#define NameStr(name)
Definition c.h:837
static ListCell * list_head(const List *l)
Definition pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:343
Definition value.h:64

References Assert, Alias::colnames, ereport, errcode(), errmsg, ERROR, fb(), 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 487 of file parse_relation.c.

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

References ereport, errcode(), errdetail(), errhint(), errmsg, ERROR, fb(), 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 438 of file parse_relation.c.

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

References ereport, errcode(), errmsg, ERROR, fb(), lfirst, and RTE_RELATION.

Referenced by transformFromClause(), and transformFromClauseItem().

◆ chooseScalarFunctionAlias()

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

Definition at line 1265 of file parse_relation.c.

1267{
1268 char *pname;
1269
1270 /*
1271 * If the expression is a simple function call, and the function has a
1272 * single OUT parameter that is named, use the parameter's name.
1273 */
1274 if (funcexpr && IsA(funcexpr, FuncExpr))
1275 {
1276 pname = get_func_result_name(((FuncExpr *) funcexpr)->funcid);
1277 if (pname)
1278 return pname;
1279 }
1280
1281 /*
1282 * If there's just one function in the RTE, and the user gave an RTE alias
1283 * name, use that name. (This makes FROM func() AS foo use "foo" as the
1284 * column name as well as the table alias.)
1285 */
1286 if (nfuncs == 1 && alias)
1287 return alias->aliasname;
1288
1289 /*
1290 * Otherwise use the function name.
1291 */
1292 return funcname;
1293}
char * get_func_result_name(Oid functionId)
Definition funcapi.c:1610

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

Referenced by addRangeTableEntryForFunction().

◆ colNameToVar()

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

Definition at line 894 of file parse_relation.c.

896{
897 Node *result = NULL;
898 int sublevels_up = 0;
899 ParseState *orig_pstate = pstate;
900
901 while (pstate != NULL)
902 {
903 ListCell *l;
904
905 foreach(l, pstate->p_namespace)
906 {
909
910 /* Ignore table-only items */
911 if (!nsitem->p_cols_visible)
912 continue;
913 /* If not inside LATERAL, ignore lateral-only items */
914 if (nsitem->p_lateral_only && !pstate->p_lateral_active)
915 continue;
916
917 /* use orig_pstate here for consistency with other callers */
919 colname, location);
920
921 if (newresult)
922 {
923 if (result)
926 errmsg("column reference \"%s\" is ambiguous",
927 colname),
928 parser_errposition(pstate, location)));
929 check_lateral_ref_ok(pstate, nsitem, location);
930 result = newresult;
931 }
932 }
933
934 if (result != NULL || localonly)
935 break; /* found, or don't want to look at parent */
936
937 pstate = pstate->parentParseState;
938 sublevels_up++;
939 }
940
941 return result;
942}
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:209
bool p_lateral_active
Definition parse_node.h:220

References check_lateral_ref_ok(), ereport, errcode(), errmsg, ERROR, fb(), lfirst, ParseState::p_lateral_active, 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 3856 of file parse_relation.c.

3858{
3860
3861 /*
3862 * Search the entire rtable looking for possible matches. If we find one,
3863 * emit a hint about it.
3864 */
3865 state = searchRangeTableForCol(pstate, relname, colname, location);
3866
3867 /*
3868 * If there are exact match(es), they must be inaccessible for some
3869 * reason.
3870 */
3871 if (state->rexact1)
3872 {
3873 /*
3874 * We don't try too hard when there's multiple inaccessible exact
3875 * matches, but at least be sure that we don't misleadingly suggest
3876 * that there's only one.
3877 */
3878 if (state->rexact2)
3879 ereport(ERROR,
3881 relname ?
3882 errmsg("column %s.%s does not exist", relname, colname) :
3883 errmsg("column \"%s\" does not exist", colname),
3884 errdetail("There are columns named \"%s\", but they are in tables that cannot be referenced from this part of the query.",
3885 colname),
3886 !relname ? errhint("Try using a table-qualified name.") : 0,
3887 parser_errposition(pstate, location)));
3888 /* Single exact match, so try to determine why it's inaccessible. */
3889 ereport(ERROR,
3891 relname ?
3892 errmsg("column %s.%s does not exist", relname, colname) :
3893 errmsg("column \"%s\" does not exist", colname),
3894 errdetail("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3895 colname, state->rexact1->eref->aliasname),
3896 rte_visible_if_lateral(pstate, state->rexact1) ?
3897 errhint("To reference that column, you must mark this subquery with LATERAL.") :
3898 (!relname && rte_visible_if_qualified(pstate, state->rexact1)) ?
3899 errhint("To reference that column, you must use a table-qualified name.") : 0,
3900 parser_errposition(pstate, location)));
3901 }
3902
3903 if (!state->rsecond)
3904 {
3905 /* If we found no match at all, we have little to report */
3906 if (!state->rfirst)
3907 ereport(ERROR,
3909 relname ?
3910 errmsg("column %s.%s does not exist", relname, colname) :
3911 errmsg("column \"%s\" does not exist", colname),
3912 parser_errposition(pstate, location)));
3913 /* Handle case where we have a single alternative spelling to offer */
3914 ereport(ERROR,
3916 relname ?
3917 errmsg("column %s.%s does not exist", relname, colname) :
3918 errmsg("column \"%s\" does not exist", colname),
3919 errhint("Perhaps you meant to reference the column \"%s.%s\".",
3920 state->rfirst->eref->aliasname,
3921 strVal(list_nth(state->rfirst->eref->colnames,
3922 state->first - 1))),
3923 parser_errposition(pstate, location)));
3924 }
3925 else
3926 {
3927 /* Handle case where there are two equally useful column hints */
3928 ereport(ERROR,
3930 relname ?
3931 errmsg("column %s.%s does not exist", relname, colname) :
3932 errmsg("column \"%s\" does not exist", colname),
3933 errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3934 state->rfirst->eref->aliasname,
3935 strVal(list_nth(state->rfirst->eref->colnames,
3936 state->first - 1)),
3937 state->rsecond->eref->aliasname,
3938 strVal(list_nth(state->rsecond->eref->colnames,
3939 state->second - 1))),
3940 parser_errposition(pstate, location)));
3941 }
3942}
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:40
static void * list_nth(const List *list, int n)
Definition pg_list.h:299
#define strVal(v)
Definition value.h:82

References ereport, errcode(), errdetail(), errhint(), errmsg, ERROR, fb(), 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 3785 of file parse_relation.c.

3786{
3788 const char *badAlias = NULL;
3789
3790 /*
3791 * Check to see if there are any potential matches in the query's
3792 * rangetable. (Note: cases involving a bad schema name in the RangeVar
3793 * will throw error immediately here. That seems OK.)
3794 */
3795 rte = searchRangeTableForRel(pstate, relation);
3796
3797 /*
3798 * If we found a match that has an alias and the alias is visible in the
3799 * namespace, then the problem is probably use of the relation's real name
3800 * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3801 * common enough to justify a specific hint.
3802 *
3803 * If we found a match that doesn't meet those criteria, assume the
3804 * problem is illegal use of a relation outside its scope, as in the
3805 * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3806 */
3807 if (rte && rte->alias &&
3808 strcmp(rte->eref->aliasname, relation->relname) != 0)
3809 {
3811 int sublevels_up;
3812
3813 nsitem = refnameNamespaceItem(pstate, NULL, rte->eref->aliasname,
3814 relation->location,
3815 &sublevels_up);
3816 if (nsitem && nsitem->p_rte == rte)
3817 badAlias = rte->eref->aliasname;
3818 }
3819
3820 /* If it looks like the user forgot to use an alias, hint about that */
3821 if (badAlias)
3822 ereport(ERROR,
3824 errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3825 relation->relname),
3826 errhint("Perhaps you meant to reference the table alias \"%s\".",
3827 badAlias),
3828 parser_errposition(pstate, relation->location)));
3829 /* Hint about case where we found an (inaccessible) exact match */
3830 else if (rte)
3831 ereport(ERROR,
3833 errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3834 relation->relname),
3835 errdetail("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3836 rte->eref->aliasname),
3837 rte_visible_if_lateral(pstate, rte) ?
3838 errhint("To reference that table, you must mark this subquery with LATERAL.") : 0,
3839 parser_errposition(pstate, relation->location)));
3840 /* Else, we have nothing to offer but the bald statement of error */
3841 else
3842 ereport(ERROR,
3844 errmsg("missing FROM-clause entry for table \"%s\"",
3845 relation->relname),
3846 parser_errposition(pstate, relation->location)));
3847}
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:79

References ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errdetail(), errhint(), errmsg, ERROR, fb(), RangeVar::location, 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 3376 of file parse_relation.c.

3378{
3379 RangeTblEntry *rte = nsitem->p_rte;
3380 RTEPermissionInfo *perminfo = nsitem->p_perminfo;
3381 List *names,
3382 *vars;
3383 ListCell *name,
3384 *var;
3385 List *te_list = NIL;
3386
3387 vars = expandNSItemVars(pstate, nsitem, sublevels_up, location, &names);
3388
3389 /*
3390 * Require read access to the table. This is normally redundant with the
3391 * markVarForSelectPriv calls below, but not if the table has zero
3392 * columns. We need not do anything if the nsitem is for a join: its
3393 * component tables will have been marked ACL_SELECT when they were added
3394 * to the rangetable. (This step changes things only for the target
3395 * relation of UPDATE/DELETE, which cannot be under a join.)
3396 */
3397 if (rte->rtekind == RTE_RELATION)
3398 {
3399 Assert(perminfo != NULL);
3400 perminfo->requiredPerms |= ACL_SELECT;
3401 }
3402
3403 forboth(name, names, var, vars)
3404 {
3405 char *label = strVal(lfirst(name));
3406 Var *varnode = (Var *) lfirst(var);
3407 TargetEntry *te;
3408
3409 te = makeTargetEntry((Expr *) varnode,
3410 (AttrNumber) pstate->p_next_resno++,
3411 label,
3412 false);
3413 te_list = lappend(te_list, te);
3414
3416 {
3417 /* Require read access to each column */
3419 }
3420 }
3421
3422 Assert(name == NULL && var == NULL); /* lists not the same length? */
3423
3424 return te_list;
3425}
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition makefuncs.c:289
void markVarForSelectPriv(ParseState *pstate, Var *var)
List * expandNSItemVars(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
static char * label
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:518
int p_next_resno
Definition parse_node.h:229
const char * name

References ACL_SELECT, Assert, expandNSItemVars(), fb(), forboth, label, lappend(), lfirst, makeTargetEntry(), markVarForSelectPriv(), name, NIL, ParseState::p_next_resno, RTE_RELATION, and strVal.

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

◆ expandNSItemVars()

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

Definition at line 3311 of file parse_relation.c.

3314{
3315 List *result = NIL;
3316 int colindex;
3317 ListCell *lc;
3318
3319 if (colnames)
3320 *colnames = NIL;
3321 colindex = 0;
3322 foreach(lc, nsitem->p_names->colnames)
3323 {
3325 const char *colname = strVal(colnameval);
3326 ParseNamespaceColumn *nscol = nsitem->p_nscolumns + colindex;
3327
3328 if (nscol->p_dontexpand)
3329 {
3330 /* skip */
3331 }
3332 else if (colname[0])
3333 {
3334 Var *var;
3335
3336 Assert(nscol->p_varno > 0);
3337 var = makeVar(nscol->p_varno,
3338 nscol->p_varattno,
3339 nscol->p_vartype,
3340 nscol->p_vartypmod,
3341 nscol->p_varcollid,
3342 sublevels_up);
3343 /* makeVar doesn't offer parameters for these, so set by hand: */
3344 var->varreturningtype = nscol->p_varreturningtype;
3345 var->varnosyn = nscol->p_varnosyn;
3346 var->varattnosyn = nscol->p_varattnosyn;
3347 var->location = location;
3348
3349 /* ... and update varnullingrels */
3350 markNullableIfNeeded(pstate, var);
3351
3352 result = lappend(result, var);
3353 if (colnames)
3354 *colnames = lappend(*colnames, colnameval);
3355 }
3356 else
3357 {
3358 /* dropped column, ignore */
3359 Assert(nscol->p_varno == 0);
3360 }
3361 colindex++;
3362 }
3363 return result;
3364}
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition makefuncs.c:66
void markNullableIfNeeded(ParseState *pstate, Var *var)
ParseLoc location
Definition primnodes.h:311
VarReturningType varreturningtype
Definition primnodes.h:298

References Assert, fb(), lappend(), lfirst, Var::location, makeVar(), markNullableIfNeeded(), NIL, strVal, and Var::varreturningtype.

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

◆ expandRelation()

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

Definition at line 3202 of file parse_relation.c.

3206{
3207 Relation rel;
3208
3209 /* Get the tupledesc and turn it over to expandTupleDesc */
3210 rel = relation_open(relid, AccessShareLock);
3211 expandTupleDesc(rel->rd_att, eref, rel->rd_att->natts, 0,
3212 rtindex, sublevels_up, returning_type,
3213 location, include_dropped,
3214 colnames, colvars);
3216}
static void expandTupleDesc(TupleDesc tupdesc, Alias *eref, int count, int offset, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:47

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

Referenced by expandRTE().

◆ expandRTE()

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

Definition at line 2834 of file parse_relation.c.

2838{
2839 int varattno;
2840
2841 if (colnames)
2842 *colnames = NIL;
2843 if (colvars)
2844 *colvars = NIL;
2845
2846 switch (rte->rtekind)
2847 {
2848 case RTE_RELATION:
2849 /* Ordinary relation RTE */
2850 expandRelation(rte->relid, rte->eref,
2851 rtindex, sublevels_up, returning_type, location,
2852 include_dropped, colnames, colvars);
2853 break;
2854 case RTE_SUBQUERY:
2855 {
2856 /* Subquery RTE */
2857 ListCell *aliasp_item = list_head(rte->eref->colnames);
2859
2860 varattno = 0;
2861 foreach(tlistitem, rte->subquery->targetList)
2862 {
2864
2865 if (te->resjunk)
2866 continue;
2867 varattno++;
2868 Assert(varattno == te->resno);
2869
2870 /*
2871 * Formerly it was possible for the subquery tlist to have
2872 * more non-junk entries than the colnames list does (if
2873 * this RTE has been expanded from a view that has more
2874 * columns than it did when the current query was parsed).
2875 * Now that ApplyRetrieveRule cleans up such cases, we
2876 * shouldn't see that anymore, but let's just check.
2877 */
2878 if (!aliasp_item)
2879 elog(ERROR, "too few column names for subquery %s",
2880 rte->eref->aliasname);
2881
2882 if (colnames)
2883 {
2884 char *label = strVal(lfirst(aliasp_item));
2885
2886 *colnames = lappend(*colnames, makeString(pstrdup(label)));
2887 }
2888
2889 if (colvars)
2890 {
2891 Var *varnode;
2892
2893 varnode = makeVar(rtindex, varattno,
2894 exprType((Node *) te->expr),
2895 exprTypmod((Node *) te->expr),
2896 exprCollation((Node *) te->expr),
2897 sublevels_up);
2898 varnode->varreturningtype = returning_type;
2899 varnode->location = location;
2900
2902 }
2903
2904 aliasp_item = lnext(rte->eref->colnames, aliasp_item);
2905 }
2906 }
2907 break;
2908 case RTE_FUNCTION:
2909 {
2910 /* Function RTE */
2911 int atts_done = 0;
2912 ListCell *lc;
2913
2914 foreach(lc, rte->functions)
2915 {
2919 TupleDesc tupdesc = NULL;
2920
2921 /* If it has a coldeflist, it returns RECORD */
2922 if (rtfunc->funccolnames != NIL)
2924 else
2926 &funcrettype,
2927 &tupdesc);
2928
2931 {
2932 /* Composite data type, e.g. a table's row type */
2933 Assert(tupdesc);
2934 expandTupleDesc(tupdesc, rte->eref,
2935 rtfunc->funccolcount, atts_done,
2936 rtindex, sublevels_up,
2937 returning_type, location,
2938 include_dropped, colnames, colvars);
2939 }
2940 else if (functypclass == TYPEFUNC_SCALAR)
2941 {
2942 /* Base data type, i.e. scalar */
2943 if (colnames)
2944 *colnames = lappend(*colnames,
2945 list_nth(rte->eref->colnames,
2946 atts_done));
2947
2948 if (colvars)
2949 {
2950 Var *varnode;
2951
2952 varnode = makeVar(rtindex, atts_done + 1,
2954 exprTypmod(rtfunc->funcexpr),
2955 exprCollation(rtfunc->funcexpr),
2956 sublevels_up);
2957 varnode->varreturningtype = returning_type;
2958 varnode->location = location;
2959
2961 }
2962 }
2963 else if (functypclass == TYPEFUNC_RECORD)
2964 {
2965 if (colnames)
2966 {
2967 List *namelist;
2968
2969 /* extract appropriate subset of column list */
2970 namelist = list_copy_tail(rte->eref->colnames,
2971 atts_done);
2973 rtfunc->funccolcount);
2974 *colnames = list_concat(*colnames, namelist);
2975 }
2976
2977 if (colvars)
2978 {
2979 ListCell *l1;
2980 ListCell *l2;
2981 ListCell *l3;
2982 int attnum = atts_done;
2983
2984 forthree(l1, rtfunc->funccoltypes,
2985 l2, rtfunc->funccoltypmods,
2986 l3, rtfunc->funccolcollations)
2987 {
2988 Oid attrtype = lfirst_oid(l1);
2989 int32 attrtypmod = lfirst_int(l2);
2991 Var *varnode;
2992
2993 attnum++;
2994 varnode = makeVar(rtindex,
2995 attnum,
2996 attrtype,
2997 attrtypmod,
2999 sublevels_up);
3000 varnode->varreturningtype = returning_type;
3001 varnode->location = location;
3003 }
3004 }
3005 }
3006 else
3007 {
3008 /* addRangeTableEntryForFunction should've caught this */
3009 elog(ERROR, "function in FROM has unsupported return type");
3010 }
3011 atts_done += rtfunc->funccolcount;
3012 }
3013
3014 /* Append the ordinality column if any */
3015 if (rte->funcordinality)
3016 {
3017 if (colnames)
3018 *colnames = lappend(*colnames,
3019 llast(rte->eref->colnames));
3020
3021 if (colvars)
3022 {
3023 Var *varnode = makeVar(rtindex,
3024 atts_done + 1,
3025 INT8OID,
3026 -1,
3027 InvalidOid,
3028 sublevels_up);
3029
3030 varnode->varreturningtype = returning_type;
3032 }
3033 }
3034 }
3035 break;
3036 case RTE_JOIN:
3037 {
3038 /* Join RTE */
3039 ListCell *colname;
3041
3042 Assert(list_length(rte->eref->colnames) == list_length(rte->joinaliasvars));
3043
3044 varattno = 0;
3045 forboth(colname, rte->eref->colnames, aliasvar, rte->joinaliasvars)
3046 {
3047 Node *avar = (Node *) lfirst(aliasvar);
3048
3049 varattno++;
3050
3051 /*
3052 * During ordinary parsing, there will never be any
3053 * deleted columns in the join. While this function is
3054 * also used by the rewriter and planner, they do not
3055 * currently call it on any JOIN RTEs. Therefore, this
3056 * next bit is dead code, but it seems prudent to handle
3057 * the case correctly anyway.
3058 */
3059 if (avar == NULL)
3060 {
3061 if (include_dropped)
3062 {
3063 if (colnames)
3064 *colnames = lappend(*colnames,
3065 makeString(pstrdup("")));
3066 if (colvars)
3067 {
3068 /*
3069 * Can't use join's column type here (it might
3070 * be dropped!); but it doesn't really matter
3071 * what type the Const claims to be.
3072 */
3075 InvalidOid));
3076 }
3077 }
3078 continue;
3079 }
3080
3081 if (colnames)
3082 {
3083 char *label = strVal(lfirst(colname));
3084
3085 *colnames = lappend(*colnames,
3087 }
3088
3089 if (colvars)
3090 {
3091 Var *varnode;
3092
3093 /*
3094 * If the joinaliasvars entry is a simple Var, just
3095 * copy it (with adjustment of varlevelsup and
3096 * location); otherwise it is a JOIN USING column and
3097 * we must generate a join alias Var. This matches
3098 * the results that expansion of "join.*" by
3099 * expandNSItemVars would have produced, if we had
3100 * access to the ParseNamespaceItem for the join.
3101 */
3102 if (IsA(avar, Var))
3103 {
3104 varnode = copyObject((Var *) avar);
3105 varnode->varlevelsup = sublevels_up;
3106 }
3107 else
3108 varnode = makeVar(rtindex, varattno,
3109 exprType(avar),
3112 sublevels_up);
3113 varnode->varreturningtype = returning_type;
3114 varnode->location = location;
3115
3117 }
3118 }
3119 }
3120 break;
3121 case RTE_TABLEFUNC:
3122 case RTE_VALUES:
3123 case RTE_CTE:
3125 case RTE_GRAPH_TABLE:
3126 {
3127 /* Tablefunc, Values, CTE, or ENR RTE */
3128 ListCell *aliasp_item = list_head(rte->eref->colnames);
3129 ListCell *lct;
3130 ListCell *lcm;
3131 ListCell *lcc;
3132
3133 varattno = 0;
3134 forthree(lct, rte->coltypes,
3135 lcm, rte->coltypmods,
3136 lcc, rte->colcollations)
3137 {
3138 Oid coltype = lfirst_oid(lct);
3141
3142 varattno++;
3143
3144 if (colnames)
3145 {
3146 /* Assume there is one alias per output column */
3147 if (OidIsValid(coltype))
3148 {
3149 char *label = strVal(lfirst(aliasp_item));
3150
3151 *colnames = lappend(*colnames,
3153 }
3154 else if (include_dropped)
3155 *colnames = lappend(*colnames,
3156 makeString(pstrdup("")));
3157
3158 aliasp_item = lnext(rte->eref->colnames, aliasp_item);
3159 }
3160
3161 if (colvars)
3162 {
3163 if (OidIsValid(coltype))
3164 {
3165 Var *varnode;
3166
3167 varnode = makeVar(rtindex, varattno,
3168 coltype, coltypmod, colcoll,
3169 sublevels_up);
3170 varnode->varreturningtype = returning_type;
3171 varnode->location = location;
3172
3174 }
3175 else if (include_dropped)
3176 {
3177 /*
3178 * It doesn't really matter what type the Const
3179 * claims to be.
3180 */
3183 InvalidOid));
3184 }
3185 }
3186 }
3187 }
3188 break;
3189 case RTE_RESULT:
3190 case RTE_GROUP:
3191 /* These expose no columns, so nothing to do */
3192 break;
3193 default:
3194 elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3195 }
3196}
List * list_truncate(List *list, int new_size)
Definition list.c:631
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition makefuncs.c:388
static void expandRelation(Oid relid, Alias *eref, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
@ RTE_RESULT
int16 attnum
#define llast(l)
Definition pg_list.h:198

References Assert, attnum, copyObject, elog, ERROR, expandRelation(), expandTupleDesc(), TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), fb(), forboth, forthree, RangeTblFunction::funcexpr, get_expr_result_type(), InvalidOid, IsA, label, lappend(), lfirst, lfirst_int, lfirst_oid, list_concat(), list_copy_tail(), list_head(), list_length(), list_nth(), list_truncate(), llast, lnext(), makeNullConst(), makeString(), makeVar(), NIL, OidIsValid, pstrdup(), TargetEntry::resno, RTE_CTE, RTE_FUNCTION, RTE_GRAPH_TABLE, RTE_GROUP, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, strVal, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by build_physical_tlist(), expandRecordVariable(), ReplaceVarFromTargetList(), set_relation_column_names(), and transformWholeRowRef().

◆ expandTupleDesc()

static void expandTupleDesc ( TupleDesc  tupdesc,
Alias eref,
int  count,
int  offset,
int  rtindex,
int  sublevels_up,
VarReturningType  returning_type,
int  location,
bool  include_dropped,
List **  colnames,
List **  colvars 
)
static

Definition at line 3228 of file parse_relation.c.

3233{
3235 int varattno;
3236
3237 aliascell = (offset < list_length(eref->colnames)) ?
3238 list_nth_cell(eref->colnames, offset) : NULL;
3239
3241 for (varattno = 0; varattno < count; varattno++)
3242 {
3243 Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
3244
3245 if (attr->attisdropped)
3246 {
3247 if (include_dropped)
3248 {
3249 if (colnames)
3250 *colnames = lappend(*colnames, makeString(pstrdup("")));
3251 if (colvars)
3252 {
3253 /*
3254 * can't use atttypid here, but it doesn't really matter
3255 * what type the Const claims to be.
3256 */
3259 }
3260 }
3261 if (aliascell)
3262 aliascell = lnext(eref->colnames, aliascell);
3263 continue;
3264 }
3265
3266 if (colnames)
3267 {
3268 char *label;
3269
3270 if (aliascell)
3271 {
3273 aliascell = lnext(eref->colnames, aliascell);
3274 }
3275 else
3276 {
3277 /* If we run out of aliases, use the underlying name */
3278 label = NameStr(attr->attname);
3279 }
3280 *colnames = lappend(*colnames, makeString(pstrdup(label)));
3281 }
3282
3283 if (colvars)
3284 {
3285 Var *varnode;
3286
3287 varnode = makeVar(rtindex, varattno + offset + 1,
3288 attr->atttypid, attr->atttypmod,
3289 attr->attcollation,
3290 sublevels_up);
3291 varnode->varreturningtype = returning_type;
3292 varnode->location = location;
3293
3295 }
3296 }
3297}
static ListCell * list_nth_cell(const List *list, int n)
Definition pg_list.h:277

References Assert, fb(), InvalidOid, label, lappend(), lfirst, list_length(), list_nth_cell(), lnext(), 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 3949 of file parse_relation.c.

3950{
3951 while (pstate != NULL)
3952 {
3953 ListCell *l;
3954
3955 foreach(l, pstate->p_namespace)
3956 {
3958
3959 if (nsitem->p_rte == rte)
3960 return nsitem;
3961 }
3962 pstate = pstate->parentParseState;
3963 }
3964 return NULL;
3965}

References fb(), lfirst, ParseState::p_namespace, 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 3650 of file parse_relation.c.

3651{
3652 ListCell *l;
3653
3654 foreach(l, qry->rowMarks)
3655 {
3656 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3657
3658 if (rc->rti == rtindex)
3659 return rc;
3660 }
3661 return NULL;
3662}
List * rowMarks
Definition parsenodes.h:234

References fb(), 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 3480 of file parse_relation.c.

3481{
3482 bool result;
3483
3484 switch (rte->rtekind)
3485 {
3486 case RTE_RELATION:
3487 {
3488 /*
3489 * Plain relation RTE --- get the attribute's catalog entry
3490 */
3491 HeapTuple tp;
3493
3495 ObjectIdGetDatum(rte->relid),
3497 if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3498 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3499 attnum, rte->relid);
3501 result = att_tup->attisdropped;
3502 ReleaseSysCache(tp);
3503 }
3504 break;
3505 case RTE_SUBQUERY:
3506 case RTE_TABLEFUNC:
3507 case RTE_VALUES:
3508 case RTE_CTE:
3509 case RTE_GROUP:
3510 case RTE_GRAPH_TABLE:
3511
3512 /*
3513 * Subselect, Table Functions, Values, CTE, GROUP RTEs, Property
3514 * graph references never have dropped columns
3515 */
3516 result = false;
3517 break;
3519 {
3520 /* Check dropped-ness by testing for valid coltype */
3521 if (attnum <= 0 ||
3522 attnum > list_length(rte->coltypes))
3523 elog(ERROR, "invalid varattno %d", attnum);
3524 result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3525 }
3526 break;
3527 case RTE_JOIN:
3528 {
3529 /*
3530 * A join RTE would not have dropped columns when constructed,
3531 * but one in a stored rule might contain columns that were
3532 * dropped from the underlying tables, if said columns are
3533 * nowhere explicitly referenced in the rule. This will be
3534 * signaled to us by a null pointer in the joinaliasvars list.
3535 */
3536 Var *aliasvar;
3537
3538 if (attnum <= 0 ||
3539 attnum > list_length(rte->joinaliasvars))
3540 elog(ERROR, "invalid varattno %d", attnum);
3541 aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3542
3543 result = (aliasvar == NULL);
3544 }
3545 break;
3546 case RTE_FUNCTION:
3547 {
3548 /* Function RTE */
3549 ListCell *lc;
3550 int atts_done = 0;
3551
3552 /*
3553 * Dropped attributes are only possible with functions that
3554 * return named composite types. In such a case we have to
3555 * look up the result type to see if it currently has this
3556 * column dropped. So first, loop over the funcs until we
3557 * find the one that covers the requested column.
3558 */
3559 foreach(lc, rte->functions)
3560 {
3562
3563 if (attnum > atts_done &&
3565 {
3566 TupleDesc tupdesc;
3567
3568 /* If it has a coldeflist, it returns RECORD */
3569 if (rtfunc->funccolnames != NIL)
3570 return false; /* can't have any dropped columns */
3571
3572 tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3573 true);
3574 if (tupdesc)
3575 {
3576 /* Composite data type, e.g. a table's row type */
3578
3579 Assert(tupdesc);
3581 att = TupleDescCompactAttr(tupdesc,
3582 attnum - atts_done - 1);
3583 return att->attisdropped;
3584 }
3585 /* Otherwise, it can't have any dropped columns */
3586 return false;
3587 }
3588 atts_done += rtfunc->funccolcount;
3589 }
3590
3591 /* If we get here, must be looking for the ordinality column */
3592 if (rte->funcordinality && attnum == atts_done + 1)
3593 return false;
3594
3595 /* this probably can't happen ... */
3596 ereport(ERROR,
3598 errmsg("column %d of relation \"%s\" does not exist",
3599 attnum,
3600 rte->eref->aliasname)));
3601 result = false; /* keep compiler quiet */
3602 }
3603 break;
3604 case RTE_RESULT:
3605 /* this probably can't happen ... */
3606 ereport(ERROR,
3608 errmsg("column %d of relation \"%s\" does not exist",
3609 attnum,
3610 rte->eref->aliasname)));
3611 result = false; /* keep compiler quiet */
3612 break;
3613 default:
3614 elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3615 result = false; /* keep compiler quiet */
3616 }
3617
3618 return result;
3619}
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition funcapi.c:553
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
static Oid list_nth_oid(const List *list, int n)
Definition pg_list.h:321
static Datum Int16GetDatum(int16 X)
Definition postgres.h:172
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache2(SysCacheIdentifier cacheId, Datum key1, Datum key2)
Definition syscache.c:230
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:193

References Assert, attnum, elog, ereport, errcode(), errmsg, ERROR, fb(), RangeTblFunction::funcexpr, get_expr_result_tupdesc(), GETSTRUCT(), HeapTupleIsValid, Int16GetDatum(), lfirst, list_length(), list_nth(), list_nth_oid(), NIL, ObjectIdGetDatum(), OidIsValid, ReleaseSysCache(), RTE_CTE, RTE_FUNCTION, RTE_GRAPH_TABLE, RTE_GROUP, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, SearchSysCache2(), and TupleDescCompactAttr().

Referenced by AcquireRewriteLocks().

◆ get_rte_attribute_name()

char * get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3442 of file parse_relation.c.

3443{
3445 return "*";
3446
3447 /*
3448 * If there is a user-written column alias, use it.
3449 */
3450 if (rte->alias &&
3451 attnum > 0 && attnum <= list_length(rte->alias->colnames))
3452 return strVal(list_nth(rte->alias->colnames, attnum - 1));
3453
3454 /*
3455 * If the RTE is a relation, go to the system catalogs not the
3456 * eref->colnames list. This is a little slower but it will give the
3457 * right answer if the column has been renamed since the eref list was
3458 * built (which can easily happen for rules).
3459 */
3460 if (rte->rtekind == RTE_RELATION)
3461 return get_attname(rte->relid, attnum, false);
3462
3463 /*
3464 * Otherwise use the column name from eref. There should always be one.
3465 */
3466 if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3467 return strVal(list_nth(rte->eref->colnames, attnum - 1));
3468
3469 /* else caller gave us a bogus attnum */
3470 elog(ERROR, "invalid attnum %d for rangetable entry %s",
3471 attnum, rte->eref->aliasname);
3472 return NULL; /* keep compiler quiet */
3473}
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition lsyscache.c:946

References attnum, elog, ERROR, fb(), get_attname(), InvalidAttrNumber, list_length(), list_nth(), RTE_RELATION, and strVal.

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

◆ get_tle_by_resno()

◆ GetCTEForRTE()

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

Definition at line 561 of file parse_relation.c.

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

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

Referenced by expandRecordVariable(), and markTargetListOrigin().

◆ GetNSItemByRangeTablePosn()

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

Definition at line 514 of file parse_relation.c.

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

References Assert, elog, ERROR, fb(), lfirst, ParseState::p_namespace, 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 541 of file parse_relation.c.

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

References Assert, fb(), 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 4047 of file parse_relation.c.

4048{
4050
4051 if (rte->perminfoindex == 0 ||
4052 rte->perminfoindex > list_length(rteperminfos))
4053 elog(ERROR, "invalid perminfoindex %u in RTE with relid %u",
4054 rte->perminfoindex, rte->relid);
4056 rte->perminfoindex - 1);
4057 if (perminfo->relid != rte->relid)
4058 elog(ERROR, "permission info at index %u (with relid=%u) does not match provided RTE (with relid=%u)",
4059 rte->perminfoindex, perminfo->relid, rte->relid);
4060
4061 return perminfo;
4062}
#define list_nth_node(type, list, n)
Definition pg_list.h:327

References elog, ERROR, fb(), list_length(), and list_nth_node.

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

◆ isFutureCTE()

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

Definition at line 317 of file parse_relation.c.

318{
319 for (; pstate != NULL; pstate = pstate->parentParseState)
320 {
321 ListCell *lc;
322
323 foreach(lc, pstate->p_future_ctes)
324 {
326
327 if (strcmp(cte->ctename, refname) == 0)
328 return true;
329 }
330 }
331 return false;
332}
List * p_future_ctes
Definition parse_node.h:222

References CommonTableExpr::ctename, fb(), lfirst, ParseState::p_future_ctes, and ParseState::parentParseState.

Referenced by parserOpenTable().

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char refname 
)

Definition at line 2749 of file parse_relation.c.

2750{
2751 ListCell *l;
2752
2753 /*
2754 * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2755 * parent level, then act as though there's a generic FOR UPDATE here.
2756 */
2757 if (pstate->p_locked_from_parent)
2758 return true;
2759
2760 foreach(l, pstate->p_locking_clause)
2761 {
2763
2764 if (lc->lockedRels == NIL)
2765 {
2766 /* all tables used in query */
2767 return true;
2768 }
2769 else if (refname != NULL)
2770 {
2771 /* just the named tables */
2772 ListCell *l2;
2773
2774 foreach(l2, lc->lockedRels)
2775 {
2776 RangeVar *thisrel = (RangeVar *) lfirst(l2);
2777
2778 if (strcmp(refname, thisrel->relname) == 0)
2779 return true;
2780 }
2781 }
2782 }
2783 return false;
2784}
bool p_locked_from_parent
Definition parse_node.h:232
List * p_locking_clause
Definition parse_node.h:231

References fb(), lfirst, NIL, ParseState::p_locked_from_parent, and ParseState::p_locking_clause.

Referenced by addRangeTableEntry(), and transformRangeSubselect().

◆ markNullableIfNeeded()

void markNullableIfNeeded ( ParseState pstate,
Var var 
)

Definition at line 1046 of file parse_relation.c.

1047{
1048 int rtindex = var->varno;
1049 Bitmapset *relids;
1050
1051 /* Find the appropriate pstate */
1052 for (int lv = 0; lv < var->varlevelsup; lv++)
1053 pstate = pstate->parentParseState;
1054
1055 /* Find currently-relevant join relids for the Var's rel */
1056 if (rtindex > 0 && rtindex <= list_length(pstate->p_nullingrels))
1057 relids = (Bitmapset *) list_nth(pstate->p_nullingrels, rtindex - 1);
1058 else
1059 relids = NULL;
1060
1061 /*
1062 * Merge with any already-declared nulling rels. (Typically there won't
1063 * be any, but let's get it right if there are.)
1064 */
1065 if (relids != NULL)
1066 var->varnullingrels = bms_union(var->varnullingrels, relids);
1067}
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:251
List * p_nullingrels
Definition parse_node.h:215
int varno
Definition primnodes.h:270
Index varlevelsup
Definition primnodes.h:295

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

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

◆ markRTEForSelectPriv()

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

Definition at line 1077 of file parse_relation.c.

1078{
1079 RangeTblEntry *rte = rt_fetch(rtindex, pstate->p_rtable);
1080
1081 if (rte->rtekind == RTE_RELATION)
1082 {
1084
1085 /* Make sure the rel as a whole is marked for SELECT access */
1087 perminfo->requiredPerms |= ACL_SELECT;
1088 /* Must offset the attnum to fit in a bitmapset */
1089 perminfo->selectedCols =
1090 bms_add_member(perminfo->selectedCols,
1092 }
1093 else if (rte->rtekind == RTE_JOIN)
1094 {
1095 if (col == InvalidAttrNumber)
1096 {
1097 /*
1098 * A whole-row reference to a join has to be treated as whole-row
1099 * references to the two inputs.
1100 */
1101 JoinExpr *j;
1102
1103 if (rtindex > 0 && rtindex <= list_length(pstate->p_joinexprs))
1104 j = list_nth_node(JoinExpr, pstate->p_joinexprs, rtindex - 1);
1105 else
1106 j = NULL;
1107 if (j == NULL)
1108 elog(ERROR, "could not find JoinExpr for whole-row reference");
1109
1110 /* Note: we can't see FromExpr here */
1111 if (IsA(j->larg, RangeTblRef))
1112 {
1113 int varno = ((RangeTblRef *) j->larg)->rtindex;
1114
1116 }
1117 else if (IsA(j->larg, JoinExpr))
1118 {
1119 int varno = ((JoinExpr *) j->larg)->rtindex;
1120
1122 }
1123 else
1124 elog(ERROR, "unrecognized node type: %d",
1125 (int) nodeTag(j->larg));
1126 if (IsA(j->rarg, RangeTblRef))
1127 {
1128 int varno = ((RangeTblRef *) j->rarg)->rtindex;
1129
1131 }
1132 else if (IsA(j->rarg, JoinExpr))
1133 {
1134 int varno = ((JoinExpr *) j->rarg)->rtindex;
1135
1137 }
1138 else
1139 elog(ERROR, "unrecognized node type: %d",
1140 (int) nodeTag(j->rarg));
1141 }
1142 else
1143 {
1144 /*
1145 * Join alias Vars for ordinary columns must refer to merged JOIN
1146 * USING columns. We don't need to do anything here, because the
1147 * join input columns will also be referenced in the join's qual
1148 * clause, and will get marked for select privilege there.
1149 */
1150 }
1151 }
1152 /* other RTE types don't require privilege marking */
1153}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
#define nodeTag(nodeptr)
Definition nodes.h:139
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
static void markRTEForSelectPriv(ParseState *pstate, int rtindex, AttrNumber col)
List * p_joinexprs
Definition parse_node.h:214
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27

References ACL_SELECT, bms_add_member(), elog, ERROR, fb(), FirstLowInvalidHeapAttributeNumber, getRTEPermissionInfo(), InvalidAttrNumber, IsA, j, list_length(), list_nth_node, markRTEForSelectPriv(), nodeTag, ParseState::p_joinexprs, ParseState::p_rtable, ParseState::p_rteperminfos, rt_fetch, RTE_JOIN, and RTE_RELATION.

Referenced by markRTEForSelectPriv(), and markVarForSelectPriv().

◆ markVarForSelectPriv()

void markVarForSelectPriv ( ParseState pstate,
Var var 
)

Definition at line 1161 of file parse_relation.c.

1162{
1163 Index lv;
1164
1165 Assert(IsA(var, Var));
1166 /* Find the appropriate pstate if it's an uplevel Var */
1167 for (lv = 0; lv < var->varlevelsup; lv++)
1168 pstate = pstate->parentParseState;
1169 markRTEForSelectPriv(pstate, var->varno, var->varattno);
1170}
AttrNumber varattno
Definition primnodes.h:275

References Assert, fb(), 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,
LOCKMODE  lockmode 
)

Definition at line 1427 of file parse_relation.c.

1428{
1429 Relation rel;
1431
1433 rel = table_openrv_extended(relation, lockmode, true);
1434 if (rel == NULL)
1435 {
1436 if (relation->schemaname)
1437 ereport(ERROR,
1439 errmsg("relation \"%s.%s\" does not exist",
1440 relation->schemaname, relation->relname)));
1441 else
1442 {
1443 /*
1444 * An unqualified name might have been meant as a reference to
1445 * some not-yet-in-scope CTE. The bare "does not exist" message
1446 * has proven remarkably unhelpful for figuring out such problems,
1447 * so we take pains to offer a specific hint.
1448 */
1449 if (isFutureCTE(pstate, relation->relname))
1450 ereport(ERROR,
1452 errmsg("relation \"%s\" does not exist",
1453 relation->relname),
1454 errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1455 relation->relname),
1456 errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1457 else
1458 ereport(ERROR,
1460 errmsg("relation \"%s\" does not exist",
1461 relation->relname)));
1462 }
1463 }
1465 return rel;
1466}
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition parse_node.c:156
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition parse_node.c:140
static bool isFutureCTE(ParseState *pstate, const char *refname)
char * schemaname
Definition primnodes.h:81
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, fb(), 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 130 of file parse_relation.c.

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

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

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

◆ rte_visible_if_lateral()

static bool rte_visible_if_lateral ( ParseState pstate,
RangeTblEntry rte 
)
static

Definition at line 3977 of file parse_relation.c.

3978{
3980
3981 /* If LATERAL *is* active, we're clearly barking up the wrong tree */
3982 if (pstate->p_lateral_active)
3983 return false;
3984 nsitem = findNSItemForRTE(pstate, rte);
3985 if (nsitem)
3986 {
3987 /* Found it, report whether it's LATERAL-only */
3988 return nsitem->p_lateral_only && nsitem->p_lateral_ok;
3989 }
3990 return false;
3991}
static ParseNamespaceItem * findNSItemForRTE(ParseState *pstate, RangeTblEntry *rte)

References fb(), findNSItemForRTE(), and ParseState::p_lateral_active.

Referenced by errorMissingColumn(), and errorMissingRTE().

◆ rte_visible_if_qualified()

static bool rte_visible_if_qualified ( ParseState pstate,
RangeTblEntry rte 
)
static

Definition at line 3997 of file parse_relation.c.

3998{
4000
4001 if (nsitem)
4002 {
4003 /* Found it, report whether it's relation-only */
4004 return nsitem->p_rel_visible && !nsitem->p_cols_visible;
4005 }
4006 return false;
4007}

References fb(), and findNSItemForRTE().

Referenced by errorMissingColumn().

◆ scanNameSpaceForCTE()

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

Definition at line 286 of file parse_relation.c.

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

References CommonTableExpr::ctename, fb(), lfirst, ParseState::p_ctenamespace, and ParseState::parentParseState.

Referenced by getNSItemForSpecialRelationTypes(), and searchRangeTableForRel().

◆ scanNameSpaceForENR()

bool scanNameSpaceForENR ( ParseState pstate,
const char refname 
)

Definition at line 339 of file parse_relation.c.

340{
341 return name_matches_visible_ENR(pstate, refname);
342}
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 201 of file parse_relation.c.

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

References check_lateral_ref_ok(), ereport, errcode(), errmsg, ERROR, fb(), lfirst, ParseState::p_lateral_active, ParseState::p_namespace, and parser_errposition().

Referenced by refnameNamespaceItem().

◆ scanNameSpaceForRelid()

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

Definition at line 241 of file parse_relation.c.

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

References check_lateral_ref_ok(), ereport, errcode(), errmsg, ERROR, fb(), lfirst, ParseState::p_lateral_active, ParseState::p_namespace, parser_errposition(), RTE_RELATION, and VAR_RETURNING_DEFAULT.

Referenced by refnameNamespaceItem().

◆ scanNSItemForColumn()

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

Definition at line 684 of file parse_relation.c.

686{
687 RangeTblEntry *rte = nsitem->p_rte;
688 int attnum;
689 Var *var;
690
691 /*
692 * Scan the nsitem's column names (or aliases) for a match. Complain if
693 * multiple matches.
694 */
695 attnum = scanRTEForColumn(pstate, rte, nsitem->p_names,
696 colname, location,
697 0, NULL);
698
700 return NULL; /* Return NULL if no match */
701
702 /* In constraint check, no system column is allowed except tableOid */
707 errmsg("system column \"%s\" reference in check constraint is invalid",
708 colname),
709 parser_errposition(pstate, location)));
710
711 /*
712 * In generated column, no system column is allowed except tableOid.
713 * (Required for stored generated, but we also do it for virtual generated
714 * for now for consistency.)
715 */
720 errmsg("cannot use system column \"%s\" in column generation expression",
721 colname),
722 parser_errposition(pstate, location)));
723
724 /*
725 * In a MERGE WHEN condition, no system column is allowed except tableOid
726 */
727 if (pstate->p_expr_kind == EXPR_KIND_MERGE_WHEN &&
731 errmsg("cannot use system column \"%s\" in MERGE WHEN condition",
732 colname),
733 parser_errposition(pstate, location)));
734
735 /* Found a valid match, so build a Var */
737 {
738 /* Get attribute data from the ParseNamespaceColumn array */
739 ParseNamespaceColumn *nscol = &nsitem->p_nscolumns[attnum - 1];
740
741 /* Complain if dropped column. See notes in scanRTEForColumn. */
742 if (nscol->p_varno == 0)
745 errmsg("column \"%s\" of relation \"%s\" does not exist",
746 colname,
747 nsitem->p_names->aliasname)));
748
749 var = makeVar(nscol->p_varno,
750 nscol->p_varattno,
751 nscol->p_vartype,
752 nscol->p_vartypmod,
753 nscol->p_varcollid,
754 sublevels_up);
755 /* makeVar doesn't offer parameters for these, so set them by hand: */
756 var->varnosyn = nscol->p_varnosyn;
757 var->varattnosyn = nscol->p_varattnosyn;
758 }
759 else
760 {
761 /* System column, so use predetermined type data */
763
765 var = makeVar(nsitem->p_rtindex,
766 attnum,
767 sysatt->atttypid,
768 sysatt->atttypmod,
769 sysatt->attcollation,
770 sublevels_up);
771 }
772 var->location = location;
773
774 /* Mark Var for RETURNING OLD/NEW, as necessary */
775 var->varreturningtype = nsitem->p_returning_type;
776
777 /* Mark Var if it's nulled by any outer joins */
778 markNullableIfNeeded(pstate, var);
779
780 /* Require read access to the column */
781 markVarForSelectPriv(pstate, var);
782
783 return (Node *) var;
784}
@ EXPR_KIND_MERGE_WHEN
Definition parse_node.h:58
@ EXPR_KIND_GENERATED_COLUMN
Definition parse_node.h:83
@ EXPR_KIND_CHECK_CONSTRAINT
Definition parse_node.h:68
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:228
#define TableOidAttributeNumber
Definition sysattr.h:26

References attnum, ereport, errcode(), errmsg, ERROR, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_GENERATED_COLUMN, EXPR_KIND_MERGE_WHEN, fb(), FormData_pg_attribute, InvalidAttrNumber, Var::location, makeVar(), markNullableIfNeeded(), markVarForSelectPriv(), ParseState::p_expr_kind, parser_errposition(), scanRTEForColumn(), SystemAttributeDefinition(), TableOidAttributeNumber, and Var::varreturningtype.

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

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

References attnum, ereport, errcode(), errmsg, ERROR, fb(), Int16GetDatum(), InvalidAttrNumber, lfirst, ObjectIdGetDatum(), parser_errposition(), RTE_RELATION, 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 963 of file parse_relation.c.

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

Referenced by errorMissingColumn().

◆ searchRangeTableForRel()

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

Definition at line 360 of file parse_relation.c.

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

References fb(), InvalidOid, lfirst, NoLock, OidIsValid, ParseState::p_rtable, ParseState::parentParseState, RangeVarGetRelid, RangeVar::relname, RTE_CTE, RTE_NAMEDTUPLESTORE, RTE_RELATION, scanNameSpaceForCTE(), scanNameSpaceForENR(), and RangeVar::schemaname.

Referenced by errorMissingRTE().

◆ specialAttNum()

static int specialAttNum ( const char attname)
static

Definition at line 3705 of file parse_relation.c.

3706{
3708
3710 if (sysatt != NULL)
3711 return sysatt->attnum;
3712 return InvalidAttrNumber;
3713}
const FormData_pg_attribute * SystemAttributeByName(const char *attname)
Definition heap.c:248

References attname, fb(), 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 591 of file parse_relation.c.

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

References attnum, fb(), and varstr_levenshtein_less_equal().

Referenced by scanRTEForColumn().