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

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

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

3016 {
3017  ListCell *l;
3018 
3019  foreach(l, qry->rowMarks)
3020  {
3021  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3022 
3023  if (rc->rti == rtindex)
3024  return rc;
3025  }
3026  return NULL;
3027 }
List * rowMarks
Definition: parsenodes.h:161
#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 2854 of file parse_relation.c.

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

Referenced by AcquireRewriteLocks().

2855 {
2856  bool result;
2857 
2858  switch (rte->rtekind)
2859  {
2860  case RTE_RELATION:
2861  {
2862  /*
2863  * Plain relation RTE --- get the attribute's catalog entry
2864  */
2865  HeapTuple tp;
2866  Form_pg_attribute att_tup;
2867 
2868  tp = SearchSysCache2(ATTNUM,
2869  ObjectIdGetDatum(rte->relid),
2870  Int16GetDatum(attnum));
2871  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2872  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2873  attnum, rte->relid);
2874  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2875  result = att_tup->attisdropped;
2876  ReleaseSysCache(tp);
2877  }
2878  break;
2879  case RTE_SUBQUERY:
2880  case RTE_TABLEFUNC:
2881  case RTE_VALUES:
2882  case RTE_CTE:
2883 
2884  /*
2885  * Subselect, Table Functions, Values, CTE RTEs never have dropped
2886  * columns
2887  */
2888  result = false;
2889  break;
2890  case RTE_NAMEDTUPLESTORE:
2891  {
2892  Assert(rte->enrname);
2893 
2894  /*
2895  * We checked when we loaded coltypes for the tuplestore that
2896  * InvalidOid was only used for dropped columns, so it is safe
2897  * to count on that here.
2898  */
2899  result =
2900  ((list_nth_oid(rte->coltypes, attnum - 1) == InvalidOid));
2901  }
2902  break;
2903  case RTE_JOIN:
2904  {
2905  /*
2906  * A join RTE would not have dropped columns when constructed,
2907  * but one in a stored rule might contain columns that were
2908  * dropped from the underlying tables, if said columns are
2909  * nowhere explicitly referenced in the rule. This will be
2910  * signaled to us by a null pointer in the joinaliasvars list.
2911  */
2912  Var *aliasvar;
2913 
2914  if (attnum <= 0 ||
2915  attnum > list_length(rte->joinaliasvars))
2916  elog(ERROR, "invalid varattno %d", attnum);
2917  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
2918 
2919  result = (aliasvar == NULL);
2920  }
2921  break;
2922  case RTE_FUNCTION:
2923  {
2924  /* Function RTE */
2925  ListCell *lc;
2926  int atts_done = 0;
2927 
2928  /*
2929  * Dropped attributes are only possible with functions that
2930  * return named composite types. In such a case we have to
2931  * look up the result type to see if it currently has this
2932  * column dropped. So first, loop over the funcs until we
2933  * find the one that covers the requested column.
2934  */
2935  foreach(lc, rte->functions)
2936  {
2937  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2938 
2939  if (attnum > atts_done &&
2940  attnum <= atts_done + rtfunc->funccolcount)
2941  {
2942  TypeFuncClass functypclass;
2943  Oid funcrettype;
2944  TupleDesc tupdesc;
2945 
2946  functypclass = get_expr_result_type(rtfunc->funcexpr,
2947  &funcrettype,
2948  &tupdesc);
2949  if (functypclass == TYPEFUNC_COMPOSITE)
2950  {
2951  /* Composite data type, e.g. a table's row type */
2952  Form_pg_attribute att_tup;
2953 
2954  Assert(tupdesc);
2955  Assert(attnum - atts_done <= tupdesc->natts);
2956  att_tup = tupdesc->attrs[attnum - atts_done - 1];
2957  return att_tup->attisdropped;
2958  }
2959  /* Otherwise, it can't have any dropped columns */
2960  return false;
2961  }
2962  atts_done += rtfunc->funccolcount;
2963  }
2964 
2965  /* If we get here, must be looking for the ordinality column */
2966  if (rte->funcordinality && attnum == atts_done + 1)
2967  return false;
2968 
2969  /* this probably can't happen ... */
2970  ereport(ERROR,
2971  (errcode(ERRCODE_UNDEFINED_COLUMN),
2972  errmsg("column %d of relation \"%s\" does not exist",
2973  attnum,
2974  rte->eref->aliasname)));
2975  result = false; /* keep compiler quiet */
2976  }
2977  break;
2978  default:
2979  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2980  result = false; /* keep compiler quiet */
2981  }
2982 
2983  return result;
2984 }
Oid list_nth_oid(const List *list, int n)
Definition: list.c:432
List * joinaliasvars
Definition: parsenodes.h:980
#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:1633
bool funcordinality
Definition: parsenodes.h:991
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:187
TypeFuncClass
Definition: funcapi.h:150
#define ereport(elevel, rest)
Definition: elog.h:122
char * enrname
Definition: parsenodes.h:1028
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define InvalidOid
Definition: postgres_ext.h:36
#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:990
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:936
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1035
List * coltypes
Definition: parsenodes.h:1021
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:158
char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

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

2636 {
2637  if (attnum == InvalidAttrNumber)
2638  return "*";
2639 
2640  /*
2641  * If there is a user-written column alias, use it.
2642  */
2643  if (rte->alias &&
2644  attnum > 0 && attnum <= list_length(rte->alias->colnames))
2645  return strVal(list_nth(rte->alias->colnames, attnum - 1));
2646 
2647  /*
2648  * If the RTE is a relation, go to the system catalogs not the
2649  * eref->colnames list. This is a little slower but it will give the
2650  * right answer if the column has been renamed since the eref list was
2651  * built (which can easily happen for rules).
2652  */
2653  if (rte->rtekind == RTE_RELATION)
2654  return get_relid_attribute_name(rte->relid, attnum);
2655 
2656  /*
2657  * Otherwise use the column name from eref. There should always be one.
2658  */
2659  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
2660  return strVal(list_nth(rte->eref->colnames, attnum - 1));
2661 
2662  /* else caller gave us a bogus attnum */
2663  elog(ERROR, "invalid attnum %d for rangetable entry %s",
2664  attnum, rte->eref->aliasname);
2665  return NULL; /* keep compiler quiet */
2666 }
Alias * alias
Definition: parsenodes.h:1034
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:936
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1035
void get_rte_attribute_type ( RangeTblEntry rte,
AttrNumber  attnum,
Oid vartype,
int32 vartypmod,
Oid varcollid 
)

Definition at line 2673 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_NAMEDTUPLESTORE, 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().

2675 {
2676  switch (rte->rtekind)
2677  {
2678  case RTE_RELATION:
2679  {
2680  /* Plain relation RTE --- get the attribute's type info */
2681  HeapTuple tp;
2682  Form_pg_attribute att_tup;
2683 
2684  tp = SearchSysCache2(ATTNUM,
2685  ObjectIdGetDatum(rte->relid),
2686  Int16GetDatum(attnum));
2687  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2688  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2689  attnum, rte->relid);
2690  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2691 
2692  /*
2693  * If dropped column, pretend it ain't there. See notes in
2694  * scanRTEForColumn.
2695  */
2696  if (att_tup->attisdropped)
2697  ereport(ERROR,
2698  (errcode(ERRCODE_UNDEFINED_COLUMN),
2699  errmsg("column \"%s\" of relation \"%s\" does not exist",
2700  NameStr(att_tup->attname),
2701  get_rel_name(rte->relid))));
2702  *vartype = att_tup->atttypid;
2703  *vartypmod = att_tup->atttypmod;
2704  *varcollid = att_tup->attcollation;
2705  ReleaseSysCache(tp);
2706  }
2707  break;
2708  case RTE_SUBQUERY:
2709  {
2710  /* Subselect RTE --- get type info from subselect's tlist */
2712  attnum);
2713 
2714  if (te == NULL || te->resjunk)
2715  elog(ERROR, "subquery %s does not have attribute %d",
2716  rte->eref->aliasname, attnum);
2717  *vartype = exprType((Node *) te->expr);
2718  *vartypmod = exprTypmod((Node *) te->expr);
2719  *varcollid = exprCollation((Node *) te->expr);
2720  }
2721  break;
2722  case RTE_FUNCTION:
2723  {
2724  /* Function RTE */
2725  ListCell *lc;
2726  int atts_done = 0;
2727 
2728  /* Identify which function covers the requested column */
2729  foreach(lc, rte->functions)
2730  {
2731  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2732 
2733  if (attnum > atts_done &&
2734  attnum <= atts_done + rtfunc->funccolcount)
2735  {
2736  TypeFuncClass functypclass;
2737  Oid funcrettype;
2738  TupleDesc tupdesc;
2739 
2740  attnum -= atts_done; /* now relative to this func */
2741  functypclass = get_expr_result_type(rtfunc->funcexpr,
2742  &funcrettype,
2743  &tupdesc);
2744 
2745  if (functypclass == TYPEFUNC_COMPOSITE)
2746  {
2747  /* Composite data type, e.g. a table's row type */
2748  Form_pg_attribute att_tup;
2749 
2750  Assert(tupdesc);
2751  Assert(attnum <= tupdesc->natts);
2752  att_tup = tupdesc->attrs[attnum - 1];
2753 
2754  /*
2755  * If dropped column, pretend it ain't there. See
2756  * notes in scanRTEForColumn.
2757  */
2758  if (att_tup->attisdropped)
2759  ereport(ERROR,
2760  (errcode(ERRCODE_UNDEFINED_COLUMN),
2761  errmsg("column \"%s\" of relation \"%s\" does not exist",
2762  NameStr(att_tup->attname),
2763  rte->eref->aliasname)));
2764  *vartype = att_tup->atttypid;
2765  *vartypmod = att_tup->atttypmod;
2766  *varcollid = att_tup->attcollation;
2767  }
2768  else if (functypclass == TYPEFUNC_SCALAR)
2769  {
2770  /* Base data type, i.e. scalar */
2771  *vartype = funcrettype;
2772  *vartypmod = -1;
2773  *varcollid = exprCollation(rtfunc->funcexpr);
2774  }
2775  else if (functypclass == TYPEFUNC_RECORD)
2776  {
2777  *vartype = list_nth_oid(rtfunc->funccoltypes,
2778  attnum - 1);
2779  *vartypmod = list_nth_int(rtfunc->funccoltypmods,
2780  attnum - 1);
2781  *varcollid = list_nth_oid(rtfunc->funccolcollations,
2782  attnum - 1);
2783  }
2784  else
2785  {
2786  /*
2787  * addRangeTableEntryForFunction should've caught
2788  * this
2789  */
2790  elog(ERROR, "function in FROM has unsupported return type");
2791  }
2792  return;
2793  }
2794  atts_done += rtfunc->funccolcount;
2795  }
2796 
2797  /* If we get here, must be looking for the ordinality column */
2798  if (rte->funcordinality && attnum == atts_done + 1)
2799  {
2800  *vartype = INT8OID;
2801  *vartypmod = -1;
2802  *varcollid = InvalidOid;
2803  return;
2804  }
2805 
2806  /* this probably can't happen ... */
2807  ereport(ERROR,
2808  (errcode(ERRCODE_UNDEFINED_COLUMN),
2809  errmsg("column %d of relation \"%s\" does not exist",
2810  attnum,
2811  rte->eref->aliasname)));
2812  }
2813  break;
2814  case RTE_JOIN:
2815  {
2816  /*
2817  * Join RTE --- get type info from join RTE's alias variable
2818  */
2819  Node *aliasvar;
2820 
2821  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
2822  aliasvar = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
2823  Assert(aliasvar != NULL);
2824  *vartype = exprType(aliasvar);
2825  *vartypmod = exprTypmod(aliasvar);
2826  *varcollid = exprCollation(aliasvar);
2827  }
2828  break;
2829  case RTE_TABLEFUNC:
2830  case RTE_VALUES:
2831  case RTE_CTE:
2832  case RTE_NAMEDTUPLESTORE:
2833  {
2834  /*
2835  * tablefunc, VALUES or CTE RTE --- get type info from lists
2836  * in the RTE
2837  */
2838  Assert(attnum > 0 && attnum <= list_length(rte->coltypes));
2839  *vartype = list_nth_oid(rte->coltypes, attnum - 1);
2840  *vartypmod = list_nth_int(rte->coltypmods, attnum - 1);
2841  *varcollid = list_nth_oid(rte->colcollations, attnum - 1);
2842  }
2843  break;
2844  default:
2845  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2846  }
2847 }
Oid list_nth_oid(const List *list, int n)
Definition: list.c:432
List * joinaliasvars
Definition: parsenodes.h:980
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
List * coltypmods
Definition: parsenodes.h:1022
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define Int16GetDatum(X)
Definition: postgres.h:457
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
bool funcordinality
Definition: parsenodes.h:991
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:1023
List * targetList
Definition: parsenodes.h:138
bool resjunk
Definition: primnodes.h:1375
#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:187
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:1117
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1072
#define INT8OID
Definition: pg_type.h:304
List * funccolcollations
Definition: parsenodes.h:1073
#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:990
Expr * expr
Definition: primnodes.h:1368
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:720
RTEKind rtekind
Definition: parsenodes.h:936
Query * subquery
Definition: parsenodes.h:959
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:1035
List * coltypes
Definition: parsenodes.h:1021
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1726
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:158