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

References Alias::aliasname, Assert, ATTNUM, 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, TupleDescAttr, and TYPEFUNC_COMPOSITE.

Referenced by AcquireRewriteLocks().

2854 {
2855  bool result;
2856 
2857  switch (rte->rtekind)
2858  {
2859  case RTE_RELATION:
2860  {
2861  /*
2862  * Plain relation RTE --- get the attribute's catalog entry
2863  */
2864  HeapTuple tp;
2865  Form_pg_attribute att_tup;
2866 
2867  tp = SearchSysCache2(ATTNUM,
2868  ObjectIdGetDatum(rte->relid),
2869  Int16GetDatum(attnum));
2870  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2871  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2872  attnum, rte->relid);
2873  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2874  result = att_tup->attisdropped;
2875  ReleaseSysCache(tp);
2876  }
2877  break;
2878  case RTE_SUBQUERY:
2879  case RTE_TABLEFUNC:
2880  case RTE_VALUES:
2881  case RTE_CTE:
2882 
2883  /*
2884  * Subselect, Table Functions, Values, CTE RTEs never have dropped
2885  * columns
2886  */
2887  result = false;
2888  break;
2889  case RTE_NAMEDTUPLESTORE:
2890  {
2891  Assert(rte->enrname);
2892 
2893  /*
2894  * We checked when we loaded coltypes for the tuplestore that
2895  * InvalidOid was only used for dropped columns, so it is safe
2896  * to count on that here.
2897  */
2898  result =
2899  ((list_nth_oid(rte->coltypes, attnum - 1) == InvalidOid));
2900  }
2901  break;
2902  case RTE_JOIN:
2903  {
2904  /*
2905  * A join RTE would not have dropped columns when constructed,
2906  * but one in a stored rule might contain columns that were
2907  * dropped from the underlying tables, if said columns are
2908  * nowhere explicitly referenced in the rule. This will be
2909  * signaled to us by a null pointer in the joinaliasvars list.
2910  */
2911  Var *aliasvar;
2912 
2913  if (attnum <= 0 ||
2914  attnum > list_length(rte->joinaliasvars))
2915  elog(ERROR, "invalid varattno %d", attnum);
2916  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
2917 
2918  result = (aliasvar == NULL);
2919  }
2920  break;
2921  case RTE_FUNCTION:
2922  {
2923  /* Function RTE */
2924  ListCell *lc;
2925  int atts_done = 0;
2926 
2927  /*
2928  * Dropped attributes are only possible with functions that
2929  * return named composite types. In such a case we have to
2930  * look up the result type to see if it currently has this
2931  * column dropped. So first, loop over the funcs until we
2932  * find the one that covers the requested column.
2933  */
2934  foreach(lc, rte->functions)
2935  {
2936  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2937 
2938  if (attnum > atts_done &&
2939  attnum <= atts_done + rtfunc->funccolcount)
2940  {
2941  TypeFuncClass functypclass;
2942  Oid funcrettype;
2943  TupleDesc tupdesc;
2944 
2945  functypclass = get_expr_result_type(rtfunc->funcexpr,
2946  &funcrettype,
2947  &tupdesc);
2948  if (functypclass == TYPEFUNC_COMPOSITE)
2949  {
2950  /* Composite data type, e.g. a table's row type */
2951  Form_pg_attribute att_tup;
2952 
2953  Assert(tupdesc);
2954  Assert(attnum - atts_done <= tupdesc->natts);
2955  att_tup = TupleDescAttr(tupdesc,
2956  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:988
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
#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:999
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:1036
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:676
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:998
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:944
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1043
List * coltypes
Definition: parsenodes.h:1029
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:158
char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

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

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

Definition at line 2672 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, 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, TupleDescAttr, TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by make_var().

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