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)
 
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

Function Documentation

◆ get_parse_rowmark()

RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3273 of file parse_relation.c.

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

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

3274 {
3275  ListCell *l;
3276 
3277  foreach(l, qry->rowMarks)
3278  {
3279  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3280 
3281  if (rc->rti == rtindex)
3282  return rc;
3283  }
3284  return NULL;
3285 }
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 3109 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().

3110 {
3111  bool result;
3112 
3113  switch (rte->rtekind)
3114  {
3115  case RTE_RELATION:
3116  {
3117  /*
3118  * Plain relation RTE --- get the attribute's catalog entry
3119  */
3120  HeapTuple tp;
3121  Form_pg_attribute att_tup;
3122 
3123  tp = SearchSysCache2(ATTNUM,
3124  ObjectIdGetDatum(rte->relid),
3126  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3127  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3128  attnum, rte->relid);
3129  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
3130  result = att_tup->attisdropped;
3131  ReleaseSysCache(tp);
3132  }
3133  break;
3134  case RTE_SUBQUERY:
3135  case RTE_TABLEFUNC:
3136  case RTE_VALUES:
3137  case RTE_CTE:
3138 
3139  /*
3140  * Subselect, Table Functions, Values, CTE RTEs never have dropped
3141  * columns
3142  */
3143  result = false;
3144  break;
3145  case RTE_NAMEDTUPLESTORE:
3146  {
3147  /* Check dropped-ness by testing for valid coltype */
3148  if (attnum <= 0 ||
3149  attnum > list_length(rte->coltypes))
3150  elog(ERROR, "invalid varattno %d", attnum);
3151  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3152  }
3153  break;
3154  case RTE_JOIN:
3155  {
3156  /*
3157  * A join RTE would not have dropped columns when constructed,
3158  * but one in a stored rule might contain columns that were
3159  * dropped from the underlying tables, if said columns are
3160  * nowhere explicitly referenced in the rule. This will be
3161  * signaled to us by a null pointer in the joinaliasvars list.
3162  */
3163  Var *aliasvar;
3164 
3165  if (attnum <= 0 ||
3167  elog(ERROR, "invalid varattno %d", attnum);
3168  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3169 
3170  result = (aliasvar == NULL);
3171  }
3172  break;
3173  case RTE_FUNCTION:
3174  {
3175  /* Function RTE */
3176  ListCell *lc;
3177  int atts_done = 0;
3178 
3179  /*
3180  * Dropped attributes are only possible with functions that
3181  * return named composite types. In such a case we have to
3182  * look up the result type to see if it currently has this
3183  * column dropped. So first, loop over the funcs until we
3184  * find the one that covers the requested column.
3185  */
3186  foreach(lc, rte->functions)
3187  {
3188  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3189 
3190  if (attnum > atts_done &&
3191  attnum <= atts_done + rtfunc->funccolcount)
3192  {
3193  TupleDesc tupdesc;
3194 
3195  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3196  true);
3197  if (tupdesc)
3198  {
3199  /* Composite data type, e.g. a table's row type */
3200  Form_pg_attribute att_tup;
3201 
3202  Assert(tupdesc);
3203  Assert(attnum - atts_done <= tupdesc->natts);
3204  att_tup = TupleDescAttr(tupdesc,
3205  attnum - atts_done - 1);
3206  return att_tup->attisdropped;
3207  }
3208  /* Otherwise, it can't have any dropped columns */
3209  return false;
3210  }
3211  atts_done += rtfunc->funccolcount;
3212  }
3213 
3214  /* If we get here, must be looking for the ordinality column */
3215  if (rte->funcordinality && attnum == atts_done + 1)
3216  return false;
3217 
3218  /* this probably can't happen ... */
3219  ereport(ERROR,
3220  (errcode(ERRCODE_UNDEFINED_COLUMN),
3221  errmsg("column %d of relation \"%s\" does not exist",
3222  attnum,
3223  rte->eref->aliasname)));
3224  result = false; /* keep compiler quiet */
3225  }
3226  break;
3227  case RTE_RESULT:
3228  /* this probably can't happen ... */
3229  ereport(ERROR,
3230  (errcode(ERRCODE_UNDEFINED_COLUMN),
3231  errmsg("column %d of relation \"%s\" does not exist",
3232  attnum,
3233  rte->eref->aliasname)));
3234  result = false; /* keep compiler quiet */
3235  break;
3236  default:
3237  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3238  result = false; /* keep compiler quiet */
3239  }
3240 
3241  return result;
3242 }
List * joinaliasvars
Definition: parsenodes.h:1049
#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:1062
Definition: primnodes.h:181
#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:1061
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:1113
List * coltypes
Definition: parsenodes.h:1099

◆ get_rte_attribute_name()

char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

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

3072 {
3073  if (attnum == InvalidAttrNumber)
3074  return "*";
3075 
3076  /*
3077  * If there is a user-written column alias, use it.
3078  */
3079  if (rte->alias &&
3080  attnum > 0 && attnum <= list_length(rte->alias->colnames))
3081  return strVal(list_nth(rte->alias->colnames, attnum - 1));
3082 
3083  /*
3084  * If the RTE is a relation, go to the system catalogs not the
3085  * eref->colnames list. This is a little slower but it will give the
3086  * right answer if the column has been renamed since the eref list was
3087  * built (which can easily happen for rules).
3088  */
3089  if (rte->rtekind == RTE_RELATION)
3090  return get_attname(rte->relid, attnum, false);
3091 
3092  /*
3093  * Otherwise use the column name from eref. There should always be one.
3094  */
3095  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3096  return strVal(list_nth(rte->eref->colnames, attnum - 1));
3097 
3098  /* else caller gave us a bogus attnum */
3099  elog(ERROR, "invalid attnum %d for rangetable entry %s",
3100  attnum, rte->eref->aliasname);
3101  return NULL; /* keep compiler quiet */
3102 }
Alias * alias
Definition: parsenodes.h:1112
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:1113

◆ get_tle_by_resno()