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

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

Definition at line 31 of file parsetree.h.

Function Documentation

◆ get_parse_rowmark()

RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3534 of file parse_relation.c.

3535 {
3536  ListCell *l;
3537 
3538  foreach(l, qry->rowMarks)
3539  {
3540  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3541 
3542  if (rc->rti == rtindex)
3543  return rc;
3544  }
3545  return NULL;
3546 }
#define lfirst(lc)
Definition: pg_list.h:172
List * rowMarks
Definition: parsenodes.h:219

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

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

◆ get_rte_attribute_is_dropped()

bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3365 of file parse_relation.c.

3366 {
3367  bool result;
3368 
3369  switch (rte->rtekind)
3370  {
3371  case RTE_RELATION:
3372  {
3373  /*
3374  * Plain relation RTE --- get the attribute's catalog entry
3375  */
3376  HeapTuple tp;
3377  Form_pg_attribute att_tup;
3378 
3379  tp = SearchSysCache2(ATTNUM,
3380  ObjectIdGetDatum(rte->relid),
3382  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3383  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3384  attnum, rte->relid);
3385  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
3386  result = att_tup->attisdropped;
3387  ReleaseSysCache(tp);
3388  }
3389  break;
3390  case RTE_SUBQUERY:
3391  case RTE_TABLEFUNC:
3392  case RTE_VALUES:
3393  case RTE_CTE:
3394  case RTE_GROUP:
3395 
3396  /*
3397  * Subselect, Table Functions, Values, CTE, GROUP RTEs never have
3398  * dropped columns
3399  */
3400  result = false;
3401  break;
3402  case RTE_NAMEDTUPLESTORE:
3403  {
3404  /* Check dropped-ness by testing for valid coltype */
3405  if (attnum <= 0 ||
3406  attnum > list_length(rte->coltypes))
3407  elog(ERROR, "invalid varattno %d", attnum);
3408  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3409  }
3410  break;
3411  case RTE_JOIN:
3412  {
3413  /*
3414  * A join RTE would not have dropped columns when constructed,
3415  * but one in a stored rule might contain columns that were
3416  * dropped from the underlying tables, if said columns are
3417  * nowhere explicitly referenced in the rule. This will be
3418  * signaled to us by a null pointer in the joinaliasvars list.
3419  */
3420  Var *aliasvar;
3421 
3422  if (attnum <= 0 ||
3423  attnum > list_length(rte->joinaliasvars))
3424  elog(ERROR, "invalid varattno %d", attnum);
3425  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3426 
3427  result = (aliasvar == NULL);
3428  }
3429  break;
3430  case RTE_FUNCTION:
3431  {
3432  /* Function RTE */
3433  ListCell *lc;
3434  int atts_done = 0;
3435 
3436  /*
3437  * Dropped attributes are only possible with functions that
3438  * return named composite types. In such a case we have to
3439  * look up the result type to see if it currently has this
3440  * column dropped. So first, loop over the funcs until we
3441  * find the one that covers the requested column.
3442  */
3443  foreach(lc, rte->functions)
3444  {
3445  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3446 
3447  if (attnum > atts_done &&
3448  attnum <= atts_done + rtfunc->funccolcount)
3449  {
3450  TupleDesc tupdesc;
3451 
3452  /* If it has a coldeflist, it returns RECORD */
3453  if (rtfunc->funccolnames != NIL)
3454  return false; /* can't have any dropped columns */
3455 
3456  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3457  true);
3458  if (tupdesc)
3459  {
3460  /* Composite data type, e.g. a table's row type */
3461  Form_pg_attribute att_tup;
3462 
3463  Assert(tupdesc);
3464  Assert(attnum - atts_done <= tupdesc->natts);
3465  att_tup = TupleDescAttr(tupdesc,
3466  attnum - atts_done - 1);
3467  return att_tup->attisdropped;
3468  }
3469  /* Otherwise, it can't have any dropped columns */
3470  return false;
3471  }
3472  atts_done += rtfunc->funccolcount;
3473  }
3474 
3475  /* If we get here, must be looking for the ordinality column */
3476  if (rte->funcordinality && attnum == atts_done + 1)
3477  return false;
3478 
3479  /* this probably can't happen ... */
3480  ereport(ERROR,
3481  (errcode(ERRCODE_UNDEFINED_COLUMN),
3482  errmsg("column %d of relation \"%s\" does not exist",
3483  attnum,
3484  rte->eref->aliasname)));
3485  result = false; /* keep compiler quiet */
3486  }
3487  break;
3488  case RTE_RESULT:
3489  /* this probably can't happen ... */
3490  ereport(ERROR,
3491  (errcode(ERRCODE_UNDEFINED_COLUMN),
3492  errmsg("column %d of relation \"%s\" does not exist",
3493  attnum,
3494  rte->eref->aliasname)));
3495  result = false; /* keep compiler quiet */
3496  break;
3497  default:
3498  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3499  result = false; /* keep compiler quiet */
3500  }
3501 
3502  return result;
3503 }
#define Assert(condition)
Definition: c.h:858
#define OidIsValid(objectId)
Definition: c.h:775
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:551
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
@ RTE_JOIN
Definition: parsenodes.h:1019
@ RTE_CTE
Definition: parsenodes.h:1023
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1024
@ RTE_VALUES
Definition: parsenodes.h:1022
@ RTE_SUBQUERY
Definition: parsenodes.h:1018
@ RTE_RESULT
Definition: parsenodes.h:1025
@ RTE_FUNCTION
Definition: parsenodes.h:1020
@ RTE_TABLEFUNC
Definition: parsenodes.h:1021
@ RTE_GROUP
Definition: parsenodes.h:1028
@ RTE_RELATION
Definition: parsenodes.h:1017
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:321
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:172
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
bool funcordinality
Definition: parsenodes.h:1179
List * functions
Definition: parsenodes.h:1177
RTEKind rtekind
Definition: parsenodes.h:1047
Definition: primnodes.h:248
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:229
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

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

Referenced by AcquireRewriteLocks().

◆ get_rte_attribute_name()

char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3327 of file parse_relation.c.

3328 {
3329  if (attnum == InvalidAttrNumber)
3330  return "*";
3331 
3332  /*
3333  * If there is a user-written column alias, use it.
3334  */
3335  if (rte->alias &&
3336  attnum > 0 && attnum <= list_length(rte->alias->colnames))
3337  return strVal(list_nth(rte->alias->colnames, attnum - 1));
3338 
3339  /*
3340  * If the RTE is a relation, go to the system catalogs not the
3341  * eref->colnames list. This is a little slower but it will give the
3342  * right answer if the column has been renamed since the eref list was
3343  * built (which can easily happen for rules).
3344  */
3345  if (rte->rtekind == RTE_RELATION)
3346  return get_attname(rte->relid, attnum, false);
3347 
3348  /*
3349  * Otherwise use the column name from eref. There should always be one.
3350  */
3351  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3352  return strVal(list_nth(rte->eref->colnames, attnum - 1));
3353 
3354  /* else caller gave us a bogus attnum */
3355  elog(ERROR, "invalid attnum %d for rangetable entry %s",
3356  attnum, rte->eref->aliasname);
3357  return NULL; /* keep compiler quiet */
3358 }
#define InvalidAttrNumber
Definition: attnum.h:23
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:827
#define strVal(v)
Definition: value.h:82

References attnum, elog, ERROR, get_attname(), InvalidAttrNumber, list_length(), list_nth(), RangeTblEntry::relid, RTE_RELATION, RangeTblEntry::rtekind, and strVal.

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

◆ get_tle_by_resno()