PostgreSQL Source Code  git master
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))
 

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

◆ rt_fetch

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

Function Documentation

◆ get_parse_rowmark()

RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3118 of file parse_relation.c.

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

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

3119 {
3120  ListCell *l;
3121 
3122  foreach(l, qry->rowMarks)
3123  {
3124  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3125 
3126  if (rc->rti == rtindex)
3127  return rc;
3128  }
3129  return NULL;
3130 }
List * rowMarks
Definition: parsenodes.h:163
#define lfirst(lc)
Definition: pg_list.h:190

◆ get_rte_attribute_is_dropped()

bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2954 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, RangeTblEntry::coltypes, elog, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblFunction::funccolcount, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_tupdesc(), GETSTRUCT, HeapTupleIsValid, Int16GetDatum, RangeTblEntry::joinaliasvars, lfirst, list_length(), list_nth(), list_nth_oid(), ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), RangeTblEntry::relid, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2(), and TupleDescAttr.

Referenced by AcquireRewriteLocks().

2955 {
2956  bool result;
2957 
2958  switch (rte->rtekind)
2959  {
2960  case RTE_RELATION:
2961  {
2962  /*
2963  * Plain relation RTE --- get the attribute's catalog entry
2964  */
2965  HeapTuple tp;
2966  Form_pg_attribute att_tup;
2967 
2968  tp = SearchSysCache2(ATTNUM,
2969  ObjectIdGetDatum(rte->relid),
2971  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2972  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2973  attnum, rte->relid);
2974  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2975  result = att_tup->attisdropped;
2976  ReleaseSysCache(tp);
2977  }
2978  break;
2979  case RTE_SUBQUERY:
2980  case RTE_TABLEFUNC:
2981  case RTE_VALUES:
2982  case RTE_CTE:
2983 
2984  /*
2985  * Subselect, Table Functions, Values, CTE RTEs never have dropped
2986  * columns
2987  */
2988  result = false;
2989  break;
2990  case RTE_NAMEDTUPLESTORE:
2991  {
2992  /* Check dropped-ness by testing for valid coltype */
2993  if (attnum <= 0 ||
2994  attnum > list_length(rte->coltypes))
2995  elog(ERROR, "invalid varattno %d", attnum);
2996  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
2997  }
2998  break;
2999  case RTE_JOIN:
3000  {
3001  /*
3002  * A join RTE would not have dropped columns when constructed,
3003  * but one in a stored rule might contain columns that were
3004  * dropped from the underlying tables, if said columns are
3005  * nowhere explicitly referenced in the rule. This will be
3006  * signaled to us by a null pointer in the joinaliasvars list.
3007  */
3008  Var *aliasvar;
3009 
3010  if (attnum <= 0 ||
3012  elog(ERROR, "invalid varattno %d", attnum);
3013  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3014 
3015  result = (aliasvar == NULL);
3016  }
3017  break;
3018  case RTE_FUNCTION:
3019  {
3020  /* Function RTE */
3021  ListCell *lc;
3022  int atts_done = 0;
3023 
3024  /*
3025  * Dropped attributes are only possible with functions that
3026  * return named composite types. In such a case we have to
3027  * look up the result type to see if it currently has this
3028  * column dropped. So first, loop over the funcs until we
3029  * find the one that covers the requested column.
3030  */
3031  foreach(lc, rte->functions)
3032  {
3033  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3034 
3035  if (attnum > atts_done &&
3036  attnum <= atts_done + rtfunc->funccolcount)
3037  {
3038  TupleDesc tupdesc;
3039 
3040  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3041  true);
3042  if (tupdesc)
3043  {
3044  /* Composite data type, e.g. a table's row type */
3045  Form_pg_attribute att_tup;
3046 
3047  Assert(tupdesc);
3048  Assert(attnum - atts_done <= tupdesc->natts);
3049  att_tup = TupleDescAttr(tupdesc,
3050  attnum - atts_done - 1);
3051  return att_tup->attisdropped;
3052  }
3053  /* Otherwise, it can't have any dropped columns */
3054  return false;
3055  }
3056  atts_done += rtfunc->funccolcount;
3057  }
3058 
3059  /* If we get here, must be looking for the ordinality column */
3060  if (rte->funcordinality && attnum == atts_done + 1)
3061  return false;
3062 
3063  /* this probably can't happen ... */
3064  ereport(ERROR,
3065  (errcode(ERRCODE_UNDEFINED_COLUMN),
3066  errmsg("column %d of relation \"%s\" does not exist",
3067  attnum,
3068  rte->eref->aliasname)));
3069  result = false; /* keep compiler quiet */
3070  }
3071  break;
3072  case RTE_RESULT:
3073  /* this probably can't happen ... */
3074  ereport(ERROR,
3075  (errcode(ERRCODE_UNDEFINED_COLUMN),
3076  errmsg("column %d of relation \"%s\" does not exist",
3077  attnum,
3078  rte->eref->aliasname)));
3079  result = false; /* keep compiler quiet */
3080  break;
3081  default:
3082  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3083  result = false; /* keep compiler quiet */
3084  }
3085 
3086  return result;
3087 }
List * joinaliasvars
Definition: parsenodes.h:1030
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define Int16GetDatum(X)
Definition: postgres.h:451
int errcode(int sqlerrcode)
Definition: elog.c:608
bool funcordinality
Definition: parsenodes.h:1041
Definition: primnodes.h:167
#define OidIsValid(objectId)
Definition: c.h:645
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:299
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:429
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
int16 attnum
Definition: pg_attribute.h:79
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1040
static int list_length(const List *l)
Definition: pg_list.h:169
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127
RTEKind rtekind
Definition: parsenodes.h:974
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
Alias * eref
Definition: parsenodes.h:1092
List * coltypes
Definition: parsenodes.h:1078

◆ get_rte_attribute_name()

char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2718 of file parse_relation.c.

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

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

2719 {
2720  if (attnum == InvalidAttrNumber)
2721  return "*";
2722 
2723  /*
2724  * If there is a user-written column alias, use it.
2725  */
2726  if (rte->alias &&
2727  attnum > 0 && attnum <= list_length(rte->alias->colnames))
2728  return strVal(list_nth(rte->alias->colnames, attnum - 1));
2729 
2730  /*
2731  * If the RTE is a relation, go to the system catalogs not the
2732  * eref->colnames list. This is a little slower but it will give the
2733  * right answer if the column has been renamed since the eref list was
2734  * built (which can easily happen for rules).
2735  */
2736  if (rte->rtekind == RTE_RELATION)
2737  return get_attname(rte->relid, attnum, false);
2738 
2739  /*
2740  * Otherwise use the column name from eref. There should always be one.
2741  */
2742  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
2743  return strVal(list_nth(rte->eref->colnames, attnum - 1));
2744 
2745  /* else caller gave us a bogus attnum */
2746  elog(ERROR, "invalid attnum %d for rangetable entry %s",
2747  attnum, rte->eref->aliasname);
2748  return NULL; /* keep compiler quiet */
2749 }
Alias * alias
Definition: parsenodes.h:1091
List * colnames
Definition: primnodes.h:43
#define strVal(v)
Definition: value.h:54
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
int16 attnum
Definition: pg_attribute.h:79
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:974
#define elog(elevel,...)
Definition: elog.h:228
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:775
Alias * eref
Definition: parsenodes.h:1092

◆ get_rte_attribute_type()

void get_rte_attribute_type ( RangeTblEntry rte,
AttrNumber  attnum,
Oid vartype,
int32 vartypmod,
Oid varcollid 
)

Definition at line 2756 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, InvalidOid, RangeTblEntry::joinaliasvars, lfirst, list_length(), list_nth(), list_nth_int(), list_nth_oid(), NameStr, ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), RangeTblEntry::relid, TargetEntry::resjunk, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2(), RangeTblEntry::subquery, Query::targetList, TupleDescAttr, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by make_var().

2758 {
2759  switch (rte->rtekind)
2760  {
2761  case RTE_RELATION:
2762  {
2763  /* Plain relation RTE --- get the attribute's type info */
2764  HeapTuple tp;
2765  Form_pg_attribute att_tup;
2766 
2767  tp = SearchSysCache2(ATTNUM,
2768  ObjectIdGetDatum(rte->relid),
2770  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2771  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2772  attnum, rte->relid);
2773  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2774 
2775  /*
2776  * If dropped column, pretend it ain't there. See notes in
2777  * scanRTEForColumn.
2778  */
2779  if (att_tup->attisdropped)
2780  ereport(ERROR,
2781  (errcode(ERRCODE_UNDEFINED_COLUMN),
2782  errmsg("column \"%s\" of relation \"%s\" does not exist",
2783  NameStr(att_tup->attname),
2784  get_rel_name(rte->relid))));
2785  *vartype = att_tup->atttypid;
2786  *vartypmod = att_tup->atttypmod;
2787  *varcollid = att_tup->attcollation;
2788  ReleaseSysCache(tp);
2789  }
2790  break;
2791  case RTE_SUBQUERY:
2792  {
2793  /* Subselect RTE --- get type info from subselect's tlist */
2795  attnum);
2796 
2797  if (te == NULL || te->resjunk)
2798  elog(ERROR, "subquery %s does not have attribute %d",
2799  rte->eref->aliasname, attnum);
2800  *vartype = exprType((Node *) te->expr);
2801  *vartypmod = exprTypmod((Node *) te->expr);
2802  *varcollid = exprCollation((Node *) te->expr);
2803  }
2804  break;
2805  case RTE_FUNCTION:
2806  {
2807  /* Function RTE */
2808  ListCell *lc;
2809  int atts_done = 0;
2810 
2811  /* Identify which function covers the requested column */
2812  foreach(lc, rte->functions)
2813  {
2814  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2815 
2816  if (attnum > atts_done &&
2817  attnum <= atts_done + rtfunc->funccolcount)
2818  {
2819  TypeFuncClass functypclass;
2820  Oid funcrettype;
2821  TupleDesc tupdesc;
2822 
2823  attnum -= atts_done; /* now relative to this func */
2824  functypclass = get_expr_result_type(rtfunc->funcexpr,
2825  &funcrettype,
2826  &tupdesc);
2827 
2828  if (functypclass == TYPEFUNC_COMPOSITE ||
2829  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
2830  {
2831  /* Composite data type, e.g. a table's row type */
2832  Form_pg_attribute att_tup;
2833 
2834  Assert(tupdesc);
2835  Assert(attnum <= tupdesc->natts);
2836  att_tup = TupleDescAttr(tupdesc, attnum - 1);
2837 
2838  /*
2839  * If dropped column, pretend it ain't there. See
2840  * notes in scanRTEForColumn.
2841  */
2842  if (att_tup->attisdropped)
2843  ereport(ERROR,
2844  (errcode(ERRCODE_UNDEFINED_COLUMN),
2845  errmsg("column \"%s\" of relation \"%s\" does not exist",
2846  NameStr(att_tup->attname),
2847  rte->eref->aliasname)));
2848  *vartype = att_tup->atttypid;
2849  *vartypmod = att_tup->atttypmod;
2850  *varcollid = att_tup->attcollation;
2851  }
2852  else if (functypclass == TYPEFUNC_SCALAR)
2853  {
2854  /* Base data type, i.e. scalar */
2855  *vartype = funcrettype;
2856  *vartypmod = -1;
2857  *varcollid = exprCollation(rtfunc->funcexpr);
2858  }
2859  else if (functypclass == TYPEFUNC_RECORD)
2860  {
2861  *vartype = list_nth_oid(rtfunc->funccoltypes,
2862  attnum - 1);
2863  *vartypmod = list_nth_int(rtfunc->funccoltypmods,
2864  attnum - 1);
2865  *varcollid = list_nth_oid(rtfunc->funccolcollations,
2866  attnum - 1);
2867  }
2868  else
2869  {
2870  /*
2871  * addRangeTableEntryForFunction should've caught
2872  * this
2873  */
2874  elog(ERROR, "function in FROM has unsupported return type");
2875  }
2876  return;
2877  }
2878  atts_done += rtfunc->funccolcount;
2879  }
2880 
2881  /* If we get here, must be looking for the ordinality column */
2882  if (rte->funcordinality && attnum == atts_done + 1)
2883  {
2884  *vartype = INT8OID;
2885  *vartypmod = -1;
2886  *varcollid = InvalidOid;
2887  return;
2888  }
2889 
2890  /* this probably can't happen ... */
2891  ereport(ERROR,
2892  (errcode(ERRCODE_UNDEFINED_COLUMN),
2893  errmsg("column %d of relation \"%s\" does not exist",
2894  attnum,
2895  rte->eref->aliasname)));
2896  }
2897  break;
2898  case RTE_JOIN:
2899  {
2900  /*
2901  * Join RTE --- get type info from join RTE's alias variable
2902  */
2903  Node *aliasvar;
2904 
2905  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
2906  aliasvar = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
2907  Assert(aliasvar != NULL);
2908  *vartype = exprType(aliasvar);
2909  *vartypmod = exprTypmod(aliasvar);
2910  *varcollid = exprCollation(aliasvar);
2911  }
2912  break;
2913  case RTE_TABLEFUNC:
2914  case RTE_VALUES:
2915  case RTE_CTE:
2916  case RTE_NAMEDTUPLESTORE:
2917  {
2918  /*
2919  * tablefunc, VALUES, CTE, or ENR RTE --- get type info from
2920  * lists in the RTE
2921  */
2922  Assert(attnum > 0 && attnum <= list_length(rte->coltypes));
2923  *vartype = list_nth_oid(rte->coltypes, attnum - 1);
2924  *vartypmod = list_nth_int(rte->coltypmods, attnum - 1);
2925  *varcollid = list_nth_oid(rte->colcollations, attnum - 1);
2926 
2927  /* For ENR, better check for dropped column */
2928  if (!OidIsValid(*vartype))
2929  ereport(ERROR,
2930  (errcode(ERRCODE_UNDEFINED_COLUMN),
2931  errmsg("column %d of relation \"%s\" does not exist",
2932  attnum,
2933  rte->eref->aliasname)));
2934  }
2935  break;
2936  case RTE_RESULT:
2937  /* this probably can't happen ... */
2938  ereport(ERROR,
2939  (errcode(ERRCODE_UNDEFINED_COLUMN),
2940  errmsg("column %d of relation \"%s\" does not exist",
2941  attnum,
2942  rte->eref->aliasname)));
2943  break;
2944  default:
2945  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2946  }
2947 }
List * joinaliasvars
Definition: parsenodes.h:1030
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
List * coltypmods
Definition: parsenodes.h:1079
#define Int16GetDatum(X)
Definition: postgres.h:451
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:608
bool funcordinality
Definition: parsenodes.h:1041
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:212
#define OidIsValid(objectId)
Definition: c.h:645
static int list_nth_int(const List *list, int n)
Definition: pg_list.h:288
List * colcollations
Definition: parsenodes.h:1080
List * targetList
Definition: parsenodes.h:140
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:299
bool resjunk
Definition: primnodes.h:1400
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
TypeFuncClass
Definition: funcapi.h:146
#define ereport(elevel, rest)
Definition: elog.h:141
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1130
int16 attnum
Definition: pg_attribute.h:79
List * funccolcollations
Definition: parsenodes.h:1131
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1040
Expr * expr
Definition: primnodes.h:1393
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define NameStr(name)
Definition: c.h:616
Alias * eref
Definition: parsenodes.h:1092
List * coltypes
Definition: parsenodes.h:1078
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730

◆ get_tle_by_resno()