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)
#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 2877 of file parse_relation.c.

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

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

2878 {
2879  ListCell *l;
2880 
2881  foreach(l, qry->rowMarks)
2882  {
2883  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
2884 
2885  if (rc->rti == rtindex)
2886  return rc;
2887  }
2888  return NULL;
2889 }
List * rowMarks
Definition: parsenodes.h:152
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2729 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, result, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2, and TYPEFUNC_COMPOSITE.

Referenced by AcquireRewriteLocks().

2730 {
2731  bool result;
2732 
2733  switch (rte->rtekind)
2734  {
2735  case RTE_RELATION:
2736  {
2737  /*
2738  * Plain relation RTE --- get the attribute's catalog entry
2739  */
2740  HeapTuple tp;
2741  Form_pg_attribute att_tup;
2742 
2743  tp = SearchSysCache2(ATTNUM,
2744  ObjectIdGetDatum(rte->relid),
2745  Int16GetDatum(attnum));
2746  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2747  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2748  attnum, rte->relid);
2749  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2750  result = att_tup->attisdropped;
2751  ReleaseSysCache(tp);
2752  }
2753  break;
2754  case RTE_SUBQUERY:
2755  case RTE_TABLEFUNC:
2756  case RTE_VALUES:
2757  case RTE_CTE:
2758 
2759  /*
2760  * Subselect, Table Functions, Values, CTE RTEs never have dropped
2761  * columns
2762  */
2763  result = false;
2764  break;
2765  case RTE_JOIN:
2766  {
2767  /*
2768  * A join RTE would not have dropped columns when constructed,
2769  * but one in a stored rule might contain columns that were
2770  * dropped from the underlying tables, if said columns are
2771  * nowhere explicitly referenced in the rule. This will be
2772  * signaled to us by a null pointer in the joinaliasvars list.
2773  */
2774  Var *aliasvar;
2775 
2776  if (attnum <= 0 ||
2777  attnum > list_length(rte->joinaliasvars))
2778  elog(ERROR, "invalid varattno %d", attnum);
2779  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
2780 
2781  result = (aliasvar == NULL);
2782  }
2783  break;
2784  case RTE_FUNCTION:
2785  {
2786  /* Function RTE */
2787  ListCell *lc;
2788  int atts_done = 0;
2789 
2790  /*
2791  * Dropped attributes are only possible with functions that
2792  * return named composite types. In such a case we have to
2793  * look up the result type to see if it currently has this
2794  * column dropped. So first, loop over the funcs until we
2795  * find the one that covers the requested column.
2796  */
2797  foreach(lc, rte->functions)
2798  {
2799  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2800 
2801  if (attnum > atts_done &&
2802  attnum <= atts_done + rtfunc->funccolcount)
2803  {
2804  TypeFuncClass functypclass;
2805  Oid funcrettype;
2806  TupleDesc tupdesc;
2807 
2808  functypclass = get_expr_result_type(rtfunc->funcexpr,
2809  &funcrettype,
2810  &tupdesc);
2811  if (functypclass == TYPEFUNC_COMPOSITE)
2812  {
2813  /* Composite data type, e.g. a table's row type */
2814  Form_pg_attribute att_tup;
2815 
2816  Assert(tupdesc);
2817  Assert(attnum - atts_done <= tupdesc->natts);
2818  att_tup = tupdesc->attrs[attnum - atts_done - 1];
2819  return att_tup->attisdropped;
2820  }
2821  /* Otherwise, it can't have any dropped columns */
2822  return false;
2823  }
2824  atts_done += rtfunc->funccolcount;
2825  }
2826 
2827  /* If we get here, must be looking for the ordinality column */
2828  if (rte->funcordinality && attnum == atts_done + 1)
2829  return false;
2830 
2831  /* this probably can't happen ... */
2832  ereport(ERROR,
2833  (errcode(ERRCODE_UNDEFINED_COLUMN),
2834  errmsg("column %d of relation \"%s\" does not exist",
2835  attnum,
2836  rte->eref->aliasname)));
2837  result = false; /* keep compiler quiet */
2838  }
2839  break;
2840  default:
2841  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2842  result = false; /* keep compiler quiet */
2843  }
2844 
2845  return result;
2846 }
List * joinaliasvars
Definition: parsenodes.h:955
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define Int16GetDatum(X)
Definition: postgres.h:457
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
bool funcordinality
Definition: parsenodes.h:966
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#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:1116
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:965
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:916
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1000
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:154
char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

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

2512 {
2513  if (attnum == InvalidAttrNumber)
2514  return "*";
2515 
2516  /*
2517  * If there is a user-written column alias, use it.
2518  */
2519  if (rte->alias &&
2520  attnum > 0 && attnum <= list_length(rte->alias->colnames))
2521  return strVal(list_nth(rte->alias->colnames, attnum - 1));
2522 
2523  /*
2524  * If the RTE is a relation, go to the system catalogs not the
2525  * eref->colnames list. This is a little slower but it will give the
2526  * right answer if the column has been renamed since the eref list was
2527  * built (which can easily happen for rules).
2528  */
2529  if (rte->rtekind == RTE_RELATION)
2530  return get_relid_attribute_name(rte->relid, attnum);
2531 
2532  /*
2533  * Otherwise use the column name from eref. There should always be one.
2534  */
2535  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
2536  return strVal(list_nth(rte->eref->colnames, attnum - 1));
2537 
2538  /* else caller gave us a bogus attnum */
2539  elog(ERROR, "invalid attnum %d for rangetable entry %s",
2540  attnum, rte->eref->aliasname);
2541  return NULL; /* keep compiler quiet */
2542 }
Alias * alias
Definition: parsenodes.h:999
List * colnames
Definition: primnodes.h:43
#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:229
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:916
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1000
void get_rte_attribute_type ( RangeTblEntry rte,
AttrNumber  attnum,
Oid vartype,
int32 vartypmod,
Oid varcollid 
)

Definition at line 2549 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_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2, RangeTblEntry::subquery, Query::targetList, TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by make_var().

2551 {
2552  switch (rte->rtekind)
2553  {
2554  case RTE_RELATION:
2555  {
2556  /* Plain relation RTE --- get the attribute's type info */
2557  HeapTuple tp;
2558  Form_pg_attribute att_tup;
2559 
2560  tp = SearchSysCache2(ATTNUM,
2561  ObjectIdGetDatum(rte->relid),
2562  Int16GetDatum(attnum));
2563  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2564  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2565  attnum, rte->relid);
2566  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2567 
2568  /*
2569  * If dropped column, pretend it ain't there. See notes in
2570  * scanRTEForColumn.
2571  */
2572  if (att_tup->attisdropped)
2573  ereport(ERROR,
2574  (errcode(ERRCODE_UNDEFINED_COLUMN),
2575  errmsg("column \"%s\" of relation \"%s\" does not exist",
2576  NameStr(att_tup->attname),
2577  get_rel_name(rte->relid))));
2578  *vartype = att_tup->atttypid;
2579  *vartypmod = att_tup->atttypmod;
2580  *varcollid = att_tup->attcollation;
2581  ReleaseSysCache(tp);
2582  }
2583  break;
2584  case RTE_SUBQUERY:
2585  {
2586  /* Subselect RTE --- get type info from subselect's tlist */
2588  attnum);
2589 
2590  if (te == NULL || te->resjunk)
2591  elog(ERROR, "subquery %s does not have attribute %d",
2592  rte->eref->aliasname, attnum);
2593  *vartype = exprType((Node *) te->expr);
2594  *vartypmod = exprTypmod((Node *) te->expr);
2595  *varcollid = exprCollation((Node *) te->expr);
2596  }
2597  break;
2598  case RTE_FUNCTION:
2599  {
2600  /* Function RTE */
2601  ListCell *lc;
2602  int atts_done = 0;
2603 
2604  /* Identify which function covers the requested column */
2605  foreach(lc, rte->functions)
2606  {
2607  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2608 
2609  if (attnum > atts_done &&
2610  attnum <= atts_done + rtfunc->funccolcount)
2611  {
2612  TypeFuncClass functypclass;
2613  Oid funcrettype;
2614  TupleDesc tupdesc;
2615 
2616  attnum -= atts_done; /* now relative to this func */
2617  functypclass = get_expr_result_type(rtfunc->funcexpr,
2618  &funcrettype,
2619  &tupdesc);
2620 
2621  if (functypclass == TYPEFUNC_COMPOSITE)
2622  {
2623  /* Composite data type, e.g. a table's row type */
2624  Form_pg_attribute att_tup;
2625 
2626  Assert(tupdesc);
2627  Assert(attnum <= tupdesc->natts);
2628  att_tup = tupdesc->attrs[attnum - 1];
2629 
2630  /*
2631  * If dropped column, pretend it ain't there. See
2632  * notes in scanRTEForColumn.
2633  */
2634  if (att_tup->attisdropped)
2635  ereport(ERROR,
2636  (errcode(ERRCODE_UNDEFINED_COLUMN),
2637  errmsg("column \"%s\" of relation \"%s\" does not exist",
2638  NameStr(att_tup->attname),
2639  rte->eref->aliasname)));
2640  *vartype = att_tup->atttypid;
2641  *vartypmod = att_tup->atttypmod;
2642  *varcollid = att_tup->attcollation;
2643  }
2644  else if (functypclass == TYPEFUNC_SCALAR)
2645  {
2646  /* Base data type, i.e. scalar */
2647  *vartype = funcrettype;
2648  *vartypmod = -1;
2649  *varcollid = exprCollation(rtfunc->funcexpr);
2650  }
2651  else if (functypclass == TYPEFUNC_RECORD)
2652  {
2653  *vartype = list_nth_oid(rtfunc->funccoltypes,
2654  attnum - 1);
2655  *vartypmod = list_nth_int(rtfunc->funccoltypmods,
2656  attnum - 1);
2657  *varcollid = list_nth_oid(rtfunc->funccolcollations,
2658  attnum - 1);
2659  }
2660  else
2661  {
2662  /*
2663  * addRangeTableEntryForFunction should've caught
2664  * this
2665  */
2666  elog(ERROR, "function in FROM has unsupported return type");
2667  }
2668  return;
2669  }
2670  atts_done += rtfunc->funccolcount;
2671  }
2672 
2673  /* If we get here, must be looking for the ordinality column */
2674  if (rte->funcordinality && attnum == atts_done + 1)
2675  {
2676  *vartype = INT8OID;
2677  *vartypmod = -1;
2678  *varcollid = InvalidOid;
2679  return;
2680  }
2681 
2682  /* this probably can't happen ... */
2683  ereport(ERROR,
2684  (errcode(ERRCODE_UNDEFINED_COLUMN),
2685  errmsg("column %d of relation \"%s\" does not exist",
2686  attnum,
2687  rte->eref->aliasname)));
2688  }
2689  break;
2690  case RTE_JOIN:
2691  {
2692  /*
2693  * Join RTE --- get type info from join RTE's alias variable
2694  */
2695  Node *aliasvar;
2696 
2697  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
2698  aliasvar = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
2699  Assert(aliasvar != NULL);
2700  *vartype = exprType(aliasvar);
2701  *vartypmod = exprTypmod(aliasvar);
2702  *varcollid = exprCollation(aliasvar);
2703  }
2704  break;
2705  case RTE_TABLEFUNC:
2706  case RTE_VALUES:
2707  case RTE_CTE:
2708  {
2709  /*
2710  * tablefunc, VALUES or CTE RTE --- get type info from lists
2711  * in the RTE
2712  */
2713  Assert(attnum > 0 && attnum <= list_length(rte->coltypes));
2714  *vartype = list_nth_oid(rte->coltypes, attnum - 1);
2715  *vartypmod = list_nth_int(rte->coltypmods, attnum - 1);
2716  *varcollid = list_nth_oid(rte->colcollations, attnum - 1);
2717  }
2718  break;
2719  default:
2720  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2721  }
2722 }
Oid list_nth_oid(const List *list, int n)
Definition: list.c:432
List * joinaliasvars
Definition: parsenodes.h:955
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
List * coltypmods
Definition: parsenodes.h:993
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define Int16GetDatum(X)
Definition: postgres.h:457
Definition: nodes.h:522
int errcode(int sqlerrcode)
Definition: elog.c:575
bool funcordinality
Definition: parsenodes.h:966
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:994
List * targetList
Definition: parsenodes.h:131
bool resjunk
Definition: primnodes.h:1359
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#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:1116
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1037
#define INT8OID
Definition: pg_type.h:304
List * funccolcollations
Definition: parsenodes.h:1038
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:965
Expr * expr
Definition: primnodes.h:1352
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:916
Query * subquery
Definition: parsenodes.h:934
int errmsg(const char *fmt,...)
Definition: elog.c:797
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1000
List * coltypes
Definition: parsenodes.h:992
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1694
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:154