PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parsetree.h File Reference
#include "nodes/parsenodes.h"
Include dependency graph for parsetree.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define rt_fetch(rangetable_index, rangetable)   ((RangeTblEntry *) list_nth(rangetable, (rangetable_index)-1))
 
#define getrelid(rangeindex, rangetable)   (rt_fetch(rangeindex, rangetable)->relid)
 

Functions

char * get_rte_attribute_name (RangeTblEntry *rte, AttrNumber attnum)
 
void get_rte_attribute_type (RangeTblEntry *rte, AttrNumber attnum, Oid *vartype, int32 *vartypmod, Oid *varcollid)
 
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)
 

Macro Definition Documentation

#define getrelid (   rangeindex,
  rangetable 
)    (rt_fetch(rangeindex, rangetable)->relid)

Definition at line 41 of file parsetree.h.

Referenced by ExecOpenScanRelation(), expand_targetlist(), and InitPlan().

#define rt_fetch (   rangetable_index,
  rangetable 
)    ((RangeTblEntry *) list_nth(rangetable, (rangetable_index)-1))

Function Documentation

RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 2804 of file parse_relation.c.

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

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

2805 {
2806  ListCell *l;
2807 
2808  foreach(l, qry->rowMarks)
2809  {
2810  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
2811 
2812  if (rc->rti == rtindex)
2813  return rc;
2814  }
2815  return NULL;
2816 }
List * rowMarks
Definition: parsenodes.h:152
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2661 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, tupleDesc::attrs, elog, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblFunction::funccolcount, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), GETSTRUCT, HeapTupleIsValid, Int16GetDatum, RangeTblEntry::joinaliasvars, lfirst, list_length(), list_nth(), NULL, ObjectIdGetDatum, ReleaseSysCache(), RangeTblEntry::relid, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_RELATION, RTE_SUBQUERY, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2, and TYPEFUNC_COMPOSITE.

Referenced by AcquireRewriteLocks().

2662 {
2663  bool result;
2664 
2665  switch (rte->rtekind)
2666  {
2667  case RTE_RELATION:
2668  {
2669  /*
2670  * Plain relation RTE --- get the attribute's catalog entry
2671  */
2672  HeapTuple tp;
2673  Form_pg_attribute att_tup;
2674 
2675  tp = SearchSysCache2(ATTNUM,
2676  ObjectIdGetDatum(rte->relid),
2677  Int16GetDatum(attnum));
2678  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2679  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2680  attnum, rte->relid);
2681  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2682  result = att_tup->attisdropped;
2683  ReleaseSysCache(tp);
2684  }
2685  break;
2686  case RTE_SUBQUERY:
2687  case RTE_VALUES:
2688  case RTE_CTE:
2689  /* Subselect, Values, CTE RTEs never have dropped columns */
2690  result = false;
2691  break;
2692  case RTE_JOIN:
2693  {
2694  /*
2695  * A join RTE would not have dropped columns when constructed,
2696  * but one in a stored rule might contain columns that were
2697  * dropped from the underlying tables, if said columns are
2698  * nowhere explicitly referenced in the rule. This will be
2699  * signaled to us by a null pointer in the joinaliasvars list.
2700  */
2701  Var *aliasvar;
2702 
2703  if (attnum <= 0 ||
2704  attnum > list_length(rte->joinaliasvars))
2705  elog(ERROR, "invalid varattno %d", attnum);
2706  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
2707 
2708  result = (aliasvar == NULL);
2709  }
2710  break;
2711  case RTE_FUNCTION:
2712  {
2713  /* Function RTE */
2714  ListCell *lc;
2715  int atts_done = 0;
2716 
2717  /*
2718  * Dropped attributes are only possible with functions that
2719  * return named composite types. In such a case we have to
2720  * look up the result type to see if it currently has this
2721  * column dropped. So first, loop over the funcs until we
2722  * find the one that covers the requested column.
2723  */
2724  foreach(lc, rte->functions)
2725  {
2726  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2727 
2728  if (attnum > atts_done &&
2729  attnum <= atts_done + rtfunc->funccolcount)
2730  {
2731  TypeFuncClass functypclass;
2732  Oid funcrettype;
2733  TupleDesc tupdesc;
2734 
2735  functypclass = get_expr_result_type(rtfunc->funcexpr,
2736  &funcrettype,
2737  &tupdesc);
2738  if (functypclass == TYPEFUNC_COMPOSITE)
2739  {
2740  /* Composite data type, e.g. a table's row type */
2741  Form_pg_attribute att_tup;
2742 
2743  Assert(tupdesc);
2744  Assert(attnum - atts_done <= tupdesc->natts);
2745  att_tup = tupdesc->attrs[attnum - atts_done - 1];
2746  return att_tup->attisdropped;
2747  }
2748  /* Otherwise, it can't have any dropped columns */
2749  return false;
2750  }
2751  atts_done += rtfunc->funccolcount;
2752  }
2753 
2754  /* If we get here, must be looking for the ordinality column */
2755  if (rte->funcordinality && attnum == atts_done + 1)
2756  return false;
2757 
2758  /* this probably can't happen ... */
2759  ereport(ERROR,
2760  (errcode(ERRCODE_UNDEFINED_COLUMN),
2761  errmsg("column %d of relation \"%s\" does not exist",
2762  attnum,
2763  rte->eref->aliasname)));
2764  result = false; /* keep compiler quiet */
2765  }
2766  break;
2767  default:
2768  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2769  result = false; /* keep compiler quiet */
2770  }
2771 
2772  return result;
2773 }
List * joinaliasvars
Definition: parsenodes.h:921
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define Int16GetDatum(X)
Definition: postgres.h:459
int errcode(int sqlerrcode)
Definition: elog.c:575
bool funcordinality
Definition: parsenodes.h:932
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:141
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
TypeFuncClass
Definition: funcapi.h:150
#define ereport(elevel, rest)
Definition: elog.h:122
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
List * functions
Definition: parsenodes.h:931
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:882
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:961
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:151
char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2447 of file parse_relation.c.

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

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

2448 {
2449  if (attnum == InvalidAttrNumber)
2450  return "*";
2451 
2452  /*
2453  * If there is a user-written column alias, use it.
2454  */
2455  if (rte->alias &&
2456  attnum > 0 && attnum <= list_length(rte->alias->colnames))
2457  return strVal(list_nth(rte->alias->colnames, attnum - 1));
2458 
2459  /*
2460  * If the RTE is a relation, go to the system catalogs not the
2461  * eref->colnames list. This is a little slower but it will give the
2462  * right answer if the column has been renamed since the eref list was
2463  * built (which can easily happen for rules).
2464  */
2465  if (rte->rtekind == RTE_RELATION)
2466  return get_relid_attribute_name(rte->relid, attnum);
2467 
2468  /*
2469  * Otherwise use the column name from eref. There should always be one.
2470  */
2471  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
2472  return strVal(list_nth(rte->eref->colnames, attnum - 1));
2473 
2474  /* else caller gave us a bogus attnum */
2475  elog(ERROR, "invalid attnum %d for rangetable entry %s",
2476  attnum, rte->eref->aliasname);
2477  return NULL; /* keep compiler quiet */
2478 }
Alias * alias
Definition: parsenodes.h:960
List * colnames
Definition: primnodes.h:42
#define strVal(v)
Definition: value.h:54
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
char * get_relid_attribute_name(Oid relid, AttrNumber attnum)
Definition: lsyscache.c:801
#define NULL
Definition: c.h:226
char * aliasname
Definition: primnodes.h:41
static int list_length(const List *l)
Definition: pg_list.h:89
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:882
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:961
void get_rte_attribute_type ( RangeTblEntry rte,
AttrNumber  attnum,
Oid vartype,
int32 vartypmod,
Oid varcollid 
)

Definition at line 2485 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, tupleDesc::attrs, RangeTblEntry::colcollations, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, elog, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), RangeTblFunction::funccolcollations, RangeTblFunction::funccolcount, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), get_rel_name(), get_tle_by_resno(), GETSTRUCT, HeapTupleIsValid, Int16GetDatum, INT8OID, InvalidOid, RangeTblEntry::joinaliasvars, lfirst, list_length(), list_nth(), list_nth_int(), list_nth_oid(), NameStr, NULL, ObjectIdGetDatum, ReleaseSysCache(), RangeTblEntry::relid, TargetEntry::resjunk, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_RELATION, RTE_SUBQUERY, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2, RangeTblEntry::subquery, Query::targetList, TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by make_var().

2487 {
2488  switch (rte->rtekind)
2489  {
2490  case RTE_RELATION:
2491  {
2492  /* Plain relation RTE --- get the attribute's type info */
2493  HeapTuple tp;
2494  Form_pg_attribute att_tup;
2495 
2496  tp = SearchSysCache2(ATTNUM,
2497  ObjectIdGetDatum(rte->relid),
2498  Int16GetDatum(attnum));
2499  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2500  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2501  attnum, rte->relid);
2502  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2503 
2504  /*
2505  * If dropped column, pretend it ain't there. See notes in
2506  * scanRTEForColumn.
2507  */
2508  if (att_tup->attisdropped)
2509  ereport(ERROR,
2510  (errcode(ERRCODE_UNDEFINED_COLUMN),
2511  errmsg("column \"%s\" of relation \"%s\" does not exist",
2512  NameStr(att_tup->attname),
2513  get_rel_name(rte->relid))));
2514  *vartype = att_tup->atttypid;
2515  *vartypmod = att_tup->atttypmod;
2516  *varcollid = att_tup->attcollation;
2517  ReleaseSysCache(tp);
2518  }
2519  break;
2520  case RTE_SUBQUERY:
2521  {
2522  /* Subselect RTE --- get type info from subselect's tlist */
2524  attnum);
2525 
2526  if (te == NULL || te->resjunk)
2527  elog(ERROR, "subquery %s does not have attribute %d",
2528  rte->eref->aliasname, attnum);
2529  *vartype = exprType((Node *) te->expr);
2530  *vartypmod = exprTypmod((Node *) te->expr);
2531  *varcollid = exprCollation((Node *) te->expr);
2532  }
2533  break;
2534  case RTE_FUNCTION:
2535  {
2536  /* Function RTE */
2537  ListCell *lc;
2538  int atts_done = 0;
2539 
2540  /* Identify which function covers the requested column */
2541  foreach(lc, rte->functions)
2542  {
2543  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2544 
2545  if (attnum > atts_done &&
2546  attnum <= atts_done + rtfunc->funccolcount)
2547  {
2548  TypeFuncClass functypclass;
2549  Oid funcrettype;
2550  TupleDesc tupdesc;
2551 
2552  attnum -= atts_done; /* now relative to this func */
2553  functypclass = get_expr_result_type(rtfunc->funcexpr,
2554  &funcrettype,
2555  &tupdesc);
2556 
2557  if (functypclass == TYPEFUNC_COMPOSITE)
2558  {
2559  /* Composite data type, e.g. a table's row type */
2560  Form_pg_attribute att_tup;
2561 
2562  Assert(tupdesc);
2563  Assert(attnum <= tupdesc->natts);
2564  att_tup = tupdesc->attrs[attnum - 1];
2565 
2566  /*
2567  * If dropped column, pretend it ain't there. See
2568  * notes in scanRTEForColumn.
2569  */
2570  if (att_tup->attisdropped)
2571  ereport(ERROR,
2572  (errcode(ERRCODE_UNDEFINED_COLUMN),
2573  errmsg("column \"%s\" of relation \"%s\" does not exist",
2574  NameStr(att_tup->attname),
2575  rte->eref->aliasname)));
2576  *vartype = att_tup->atttypid;
2577  *vartypmod = att_tup->atttypmod;
2578  *varcollid = att_tup->attcollation;
2579  }
2580  else if (functypclass == TYPEFUNC_SCALAR)
2581  {
2582  /* Base data type, i.e. scalar */
2583  *vartype = funcrettype;
2584  *vartypmod = -1;
2585  *varcollid = exprCollation(rtfunc->funcexpr);
2586  }
2587  else if (functypclass == TYPEFUNC_RECORD)
2588  {
2589  *vartype = list_nth_oid(rtfunc->funccoltypes,
2590  attnum - 1);
2591  *vartypmod = list_nth_int(rtfunc->funccoltypmods,
2592  attnum - 1);
2593  *varcollid = list_nth_oid(rtfunc->funccolcollations,
2594  attnum - 1);
2595  }
2596  else
2597  {
2598  /*
2599  * addRangeTableEntryForFunction should've caught
2600  * this
2601  */
2602  elog(ERROR, "function in FROM has unsupported return type");
2603  }
2604  return;
2605  }
2606  atts_done += rtfunc->funccolcount;
2607  }
2608 
2609  /* If we get here, must be looking for the ordinality column */
2610  if (rte->funcordinality && attnum == atts_done + 1)
2611  {
2612  *vartype = INT8OID;
2613  *vartypmod = -1;
2614  *varcollid = InvalidOid;
2615  return;
2616  }
2617 
2618  /* this probably can't happen ... */
2619  ereport(ERROR,
2620  (errcode(ERRCODE_UNDEFINED_COLUMN),
2621  errmsg("column %d of relation \"%s\" does not exist",
2622  attnum,
2623  rte->eref->aliasname)));
2624  }
2625  break;
2626  case RTE_JOIN:
2627  {
2628  /*
2629  * Join RTE --- get type info from join RTE's alias variable
2630  */
2631  Node *aliasvar;
2632 
2633  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
2634  aliasvar = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
2635  Assert(aliasvar != NULL);
2636  *vartype = exprType(aliasvar);
2637  *vartypmod = exprTypmod(aliasvar);
2638  *varcollid = exprCollation(aliasvar);
2639  }
2640  break;
2641  case RTE_VALUES:
2642  case RTE_CTE:
2643  {
2644  /* VALUES or CTE RTE --- get type info from lists in the RTE */
2645  Assert(attnum > 0 && attnum <= list_length(rte->coltypes));
2646  *vartype = list_nth_oid(rte->coltypes, attnum - 1);
2647  *vartypmod = list_nth_int(rte->coltypmods, attnum - 1);
2648  *varcollid = list_nth_oid(rte->colcollations, attnum - 1);
2649  }
2650  break;
2651  default:
2652  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2653  }
2654 }
Oid list_nth_oid(const List *list, int n)
Definition: list.c:432
List * joinaliasvars
Definition: parsenodes.h:921
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
List * coltypmods
Definition: parsenodes.h:954
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define Int16GetDatum(X)
Definition: postgres.h:459
Definition: nodes.h:508
int errcode(int sqlerrcode)
Definition: elog.c:575
bool funcordinality
Definition: parsenodes.h:932
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
List * colcollations
Definition: parsenodes.h:955
List * targetList
Definition: parsenodes.h:131
bool resjunk
Definition: primnodes.h:1337
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
TypeFuncClass
Definition: funcapi.h:150
#define ereport(elevel, rest)
Definition: elog.h:122
int list_nth_int(const List *list, int n)
Definition: list.c:421
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:998
#define INT8OID
Definition: pg_type.h:304
List * funccolcollations
Definition: parsenodes.h:999
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
List * functions
Definition: parsenodes.h:931
Expr * expr
Definition: primnodes.h:1330
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
RTEKind rtekind
Definition: parsenodes.h:882
Query * subquery
Definition: parsenodes.h:900
int errmsg(const char *fmt,...)
Definition: elog.c:797
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
List * funccoltypes
Definition: parsenodes.h:997
#define NameStr(name)
Definition: c.h:495
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:961
List * coltypes
Definition: parsenodes.h:953
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1694
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:151