PostgreSQL Source Code  git master
rewriteHandler.c File Reference
Include dependency graph for rewriteHandler.c:

Go to the source code of this file.

Data Structures

struct  rewrite_event
 
struct  acquireLocksOnSubLinks_context
 

Macros

#define ALL_EVENTS   ((1 << CMD_INSERT) | (1 << CMD_UPDATE) | (1 << CMD_DELETE))
 

Typedefs

typedef struct rewrite_event rewrite_event
 
typedef struct acquireLocksOnSubLinks_context acquireLocksOnSubLinks_context
 

Functions

static bool acquireLocksOnSubLinks (Node *node, acquireLocksOnSubLinks_context *context)
 
static QueryrewriteRuleAction (Query *parsetree, Query *rule_action, Node *rule_qual, int rt_index, CmdType event, bool *returning_flag)
 
static ListadjustJoinTreeList (Query *parsetree, bool removert, int rt_index)
 
static ListrewriteTargetListIU (List *targetList, CmdType commandType, OverridingKind override, Relation target_relation, RangeTblEntry *values_rte, int values_rte_index, Bitmapset **unused_values_attrnos)
 
static TargetEntryprocess_matched_tle (TargetEntry *src_tle, TargetEntry *prior_tle, const char *attrName)
 
static Nodeget_assignment_input (Node *node)
 
static BitmapsetfindDefaultOnlyColumns (RangeTblEntry *rte)
 
static bool rewriteValuesRTE (Query *parsetree, RangeTblEntry *rte, int rti, Relation target_relation, Bitmapset *unused_cols)
 
static void rewriteValuesRTEToNulls (Query *parsetree, RangeTblEntry *rte)
 
static void markQueryForLocking (Query *qry, Node *jtnode, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
 
static ListmatchLocks (CmdType event, Relation relation, int varno, Query *parsetree, bool *hasUpdate)
 
static QueryfireRIRrules (Query *parsetree, List *activeRIRs)
 
static Bitmapsetadjust_view_column_set (Bitmapset *cols, List *targetlist)
 
void AcquireRewriteLocks (Query *parsetree, bool forExecute, bool forUpdatePushedDown)
 
Nodebuild_column_default (Relation rel, int attrno)
 
static bool searchForDefault (RangeTblEntry *rte)
 
static QueryApplyRetrieveRule (Query *parsetree, RewriteRule *rule, int rt_index, Relation relation, List *activeRIRs)
 
static bool fireRIRonSubLink (Node *node, List *activeRIRs)
 
static QueryCopyAndAddInvertedQual (Query *parsetree, Node *rule_qual, int rt_index, CmdType event)
 
static ListfireRules (Query *parsetree, int rt_index, CmdType event, List *locks, bool *instead_flag, bool *returning_flag, Query **qual_product)
 
Queryget_view_query (Relation view)
 
bool view_has_instead_trigger (Relation view, CmdType event, List *mergeActionList)
 
static const char * view_col_is_auto_updatable (RangeTblRef *rtr, TargetEntry *tle)
 
const char * view_query_is_auto_updatable (Query *viewquery, bool check_cols)
 
static const char * view_cols_are_auto_updatable (Query *viewquery, Bitmapset *required_cols, Bitmapset **updatable_cols, char **non_updatable_col)
 
int relation_is_updatable (Oid reloid, List *outer_reloids, bool include_triggers, Bitmapset *include_cols)
 
void error_view_not_updatable (Relation view, CmdType command, List *mergeActionList, const char *detail)
 
static QueryrewriteTargetView (Query *parsetree, Relation view)
 
static ListRewriteQuery (Query *parsetree, List *rewrite_events, int orig_rt_length)
 
ListQueryRewrite (Query *parsetree)
 

Macro Definition Documentation

◆ ALL_EVENTS

#define ALL_EVENTS   ((1 << CMD_INSERT) | (1 << CMD_UPDATE) | (1 << CMD_DELETE))

Typedef Documentation

◆ acquireLocksOnSubLinks_context

◆ rewrite_event

typedef struct rewrite_event rewrite_event

Function Documentation

◆ acquireLocksOnSubLinks()

static bool acquireLocksOnSubLinks ( Node node,
acquireLocksOnSubLinks_context context 
)
static

Definition at line 301 of file rewriteHandler.c.

302 {
303  if (node == NULL)
304  return false;
305  if (IsA(node, SubLink))
306  {
307  SubLink *sub = (SubLink *) node;
308 
309  /* Do what we came for */
311  context->for_execute,
312  false);
313  /* Fall through to process lefthand args of SubLink */
314  }
315 
316  /*
317  * Do NOT recurse into Query nodes, because AcquireRewriteLocks already
318  * processed subselects of subselects for us.
319  */
321 }
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
tree context
Definition: radixtree.h:1835
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
static bool acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context)

References AcquireRewriteLocks(), context, expression_tree_walker, IsA, and SubLink::subselect.

Referenced by AcquireRewriteLocks(), CopyAndAddInvertedQual(), fireRIRrules(), rewriteRuleAction(), and rewriteTargetView().

◆ AcquireRewriteLocks()

void AcquireRewriteLocks ( Query parsetree,
bool  forExecute,
bool  forUpdatePushedDown 
)

Definition at line 139 of file rewriteHandler.c.

142 {
143  ListCell *l;
144  int rt_index;
146 
147  context.for_execute = forExecute;
148 
149  /*
150  * First, process RTEs of the current query level.
151  */
152  rt_index = 0;
153  foreach(l, parsetree->rtable)
154  {
155  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
156  Relation rel;
157  LOCKMODE lockmode;
158  List *newaliasvars;
159  Index curinputvarno;
160  RangeTblEntry *curinputrte;
161  ListCell *ll;
162 
163  ++rt_index;
164  switch (rte->rtekind)
165  {
166  case RTE_RELATION:
167 
168  /*
169  * Grab the appropriate lock type for the relation, and do not
170  * release it until end of transaction. This protects the
171  * rewriter, planner, and executor against schema changes
172  * mid-query.
173  *
174  * If forExecute is false, ignore rellockmode and just use
175  * AccessShareLock.
176  */
177  if (!forExecute)
178  lockmode = AccessShareLock;
179  else if (forUpdatePushedDown)
180  {
181  /* Upgrade RTE's lock mode to reflect pushed-down lock */
182  if (rte->rellockmode == AccessShareLock)
183  rte->rellockmode = RowShareLock;
184  lockmode = rte->rellockmode;
185  }
186  else
187  lockmode = rte->rellockmode;
188 
189  rel = table_open(rte->relid, lockmode);
190 
191  /*
192  * While we have the relation open, update the RTE's relkind,
193  * just in case it changed since this rule was made.
194  */
195  rte->relkind = rel->rd_rel->relkind;
196 
197  table_close(rel, NoLock);
198  break;
199 
200  case RTE_JOIN:
201 
202  /*
203  * Scan the join's alias var list to see if any columns have
204  * been dropped, and if so replace those Vars with null
205  * pointers.
206  *
207  * Since a join has only two inputs, we can expect to see
208  * multiple references to the same input RTE; optimize away
209  * multiple fetches.
210  */
211  newaliasvars = NIL;
212  curinputvarno = 0;
213  curinputrte = NULL;
214  foreach(ll, rte->joinaliasvars)
215  {
216  Var *aliasitem = (Var *) lfirst(ll);
217  Var *aliasvar = aliasitem;
218 
219  /* Look through any implicit coercion */
220  aliasvar = (Var *) strip_implicit_coercions((Node *) aliasvar);
221 
222  /*
223  * If the list item isn't a simple Var, then it must
224  * represent a merged column, ie a USING column, and so it
225  * couldn't possibly be dropped, since it's referenced in
226  * the join clause. (Conceivably it could also be a null
227  * pointer already? But that's OK too.)
228  */
229  if (aliasvar && IsA(aliasvar, Var))
230  {
231  /*
232  * The elements of an alias list have to refer to
233  * earlier RTEs of the same rtable, because that's the
234  * order the planner builds things in. So we already
235  * processed the referenced RTE, and so it's safe to
236  * use get_rte_attribute_is_dropped on it. (This might
237  * not hold after rewriting or planning, but it's OK
238  * to assume here.)
239  */
240  Assert(aliasvar->varlevelsup == 0);
241  if (aliasvar->varno != curinputvarno)
242  {
243  curinputvarno = aliasvar->varno;
244  if (curinputvarno >= rt_index)
245  elog(ERROR, "unexpected varno %d in JOIN RTE %d",
246  curinputvarno, rt_index);
247  curinputrte = rt_fetch(curinputvarno,
248  parsetree->rtable);
249  }
250  if (get_rte_attribute_is_dropped(curinputrte,
251  aliasvar->varattno))
252  {
253  /* Replace the join alias item with a NULL */
254  aliasitem = NULL;
255  }
256  }
257  newaliasvars = lappend(newaliasvars, aliasitem);
258  }
259  rte->joinaliasvars = newaliasvars;
260  break;
261 
262  case RTE_SUBQUERY:
263 
264  /*
265  * The subquery RTE itself is all right, but we have to
266  * recurse to process the represented subquery.
267  */
269  forExecute,
270  (forUpdatePushedDown ||
271  get_parse_rowmark(parsetree, rt_index) != NULL));
272  break;
273 
274  default:
275  /* ignore other types of RTEs */
276  break;
277  }
278  }
279 
280  /* Recurse into subqueries in WITH */
281  foreach(l, parsetree->cteList)
282  {
283  CommonTableExpr *cte = (CommonTableExpr *) lfirst(l);
284 
285  AcquireRewriteLocks((Query *) cte->ctequery, forExecute, false);
286  }
287 
288  /*
289  * Recurse into sublink subqueries, too. But we already did the ones in
290  * the rtable and cteList.
291  */
292  if (parsetree->hasSubLinks)
295 }
#define Assert(condition)
Definition: c.h:858
unsigned int Index
Definition: c.h:614
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
List * lappend(List *list, void *datum)
Definition: list.c:339
int LOCKMODE
Definition: lockdefs.h:26
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
#define RowShareLock
Definition: lockdefs.h:37
Node * strip_implicit_coercions(Node *node)
Definition: nodeFuncs.c:700
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:156
#define QTW_IGNORE_RC_SUBQUERIES
Definition: nodeFuncs.h:24
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
bool get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
@ RTE_JOIN
Definition: parsenodes.h:1030
@ RTE_SUBQUERY
Definition: parsenodes.h:1029
@ RTE_RELATION
Definition: parsenodes.h:1028
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
Definition: pg_list.h:54
Definition: nodes.h:129
List * cteList
Definition: parsenodes.h:166
List * rtable
Definition: parsenodes.h:168
Query * subquery
Definition: parsenodes.h:1114
RTEKind rtekind
Definition: parsenodes.h:1057
Form_pg_class rd_rel
Definition: rel.h:111
Definition: primnodes.h:248
AttrNumber varattno
Definition: primnodes.h:260
int varno
Definition: primnodes.h:255
Index varlevelsup
Definition: primnodes.h:280
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References AccessShareLock, acquireLocksOnSubLinks(), Assert, context, Query::cteList, CommonTableExpr::ctequery, elog, ERROR, get_parse_rowmark(), get_rte_attribute_is_dropped(), IsA, lappend(), lfirst, NIL, NoLock, QTW_IGNORE_RC_SUBQUERIES, query_tree_walker, RelationData::rd_rel, RangeTblEntry::relid, RowShareLock, rt_fetch, Query::rtable, RTE_JOIN, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, strip_implicit_coercions(), RangeTblEntry::subquery, table_close(), table_open(), Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by acquireLocksOnSubLinks(), ApplyRetrieveRule(), fmgr_sql_validator(), get_query_def(), init_sql_fcache(), inline_set_returning_function(), make_ruledef(), print_function_sqlbody(), refresh_matview_datafill(), and rewriteRuleAction().

◆ adjust_view_column_set()

static Bitmapset * adjust_view_column_set ( Bitmapset cols,
List targetlist 
)
static

Definition at line 2993 of file rewriteHandler.c.

2994 {
2995  Bitmapset *result = NULL;
2996  int col;
2997 
2998  col = -1;
2999  while ((col = bms_next_member(cols, col)) >= 0)
3000  {
3001  /* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
3003 
3004  if (attno == InvalidAttrNumber)
3005  {
3006  /*
3007  * There's a whole-row reference to the view. For permissions
3008  * purposes, treat it as a reference to each column available from
3009  * the view. (We should *not* convert this to a whole-row
3010  * reference to the base relation, since the view may not touch
3011  * all columns of the base relation.)
3012  */
3013  ListCell *lc;
3014 
3015  foreach(lc, targetlist)
3016  {
3017  TargetEntry *tle = lfirst_node(TargetEntry, lc);
3018  Var *var;
3019 
3020  if (tle->resjunk)
3021  continue;
3022  var = castNode(Var, tle->expr);
3023  result = bms_add_member(result,
3025  }
3026  }
3027  else
3028  {
3029  /*
3030  * Views do not have system columns, so we do not expect to see
3031  * any other system attnos here. If we do find one, the error
3032  * case will apply.
3033  */
3034  TargetEntry *tle = get_tle_by_resno(targetlist, attno);
3035 
3036  if (tle != NULL && !tle->resjunk && IsA(tle->expr, Var))
3037  {
3038  Var *var = (Var *) tle->expr;
3039 
3040  result = bms_add_member(result,
3042  }
3043  else
3044  elog(ERROR, "attribute number %d not found in view targetlist",
3045  attno);
3046  }
3047  }
3048 
3049  return result;
3050 }
int16 AttrNumber
Definition: attnum.h:21
#define InvalidAttrNumber
Definition: attnum.h:23
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define lfirst_node(type, lc)
Definition: pg_list.h:176
Expr * expr
Definition: primnodes.h:2186
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References bms_add_member(), bms_next_member(), castNode, elog, ERROR, TargetEntry::expr, FirstLowInvalidHeapAttributeNumber, get_tle_by_resno(), InvalidAttrNumber, IsA, lfirst_node, and Var::varattno.

Referenced by relation_is_updatable(), and rewriteTargetView().

◆ adjustJoinTreeList()

static List * adjustJoinTreeList ( Query parsetree,
bool  removert,
int  rt_index 
)
static

Definition at line 695 of file rewriteHandler.c.

696 {
697  List *newjointree = copyObject(parsetree->jointree->fromlist);
698  ListCell *l;
699 
700  if (removert)
701  {
702  foreach(l, newjointree)
703  {
704  RangeTblRef *rtr = lfirst(l);
705 
706  if (IsA(rtr, RangeTblRef) &&
707  rtr->rtindex == rt_index)
708  {
709  newjointree = foreach_delete_current(newjointree, l);
710  break;
711  }
712  }
713  }
714  return newjointree;
715 }
#define copyObject(obj)
Definition: nodes.h:224
#define foreach_delete_current(lst, var_or_cell)
Definition: pg_list.h:391
List * fromlist
Definition: primnodes.h:2304
FromExpr * jointree
Definition: parsenodes.h:175

References copyObject, foreach_delete_current, FromExpr::fromlist, IsA, Query::jointree, lfirst, and RangeTblRef::rtindex.

Referenced by rewriteRuleAction().

◆ ApplyRetrieveRule()

static Query* ApplyRetrieveRule ( Query parsetree,
RewriteRule rule,
int  rt_index,
Relation  relation,
List activeRIRs 
)
static

Definition at line 1716 of file rewriteHandler.c.

1721 {
1722  Query *rule_action;
1723  RangeTblEntry *rte;
1724  RowMarkClause *rc;
1725  int numCols;
1726 
1727  if (list_length(rule->actions) != 1)
1728  elog(ERROR, "expected just one rule action");
1729  if (rule->qual != NULL)
1730  elog(ERROR, "cannot handle qualified ON SELECT rule");
1731 
1732  if (rt_index == parsetree->resultRelation)
1733  {
1734  /*
1735  * We have a view as the result relation of the query, and it wasn't
1736  * rewritten by any rule. This case is supported if there is an
1737  * INSTEAD OF trigger that will trap attempts to insert/update/delete
1738  * view rows. The executor will check that; for the moment just plow
1739  * ahead. We have two cases:
1740  *
1741  * For INSERT, we needn't do anything. The unmodified RTE will serve
1742  * fine as the result relation.
1743  *
1744  * For UPDATE/DELETE/MERGE, we need to expand the view so as to have
1745  * source data for the operation. But we also need an unmodified RTE
1746  * to serve as the target. So, copy the RTE and add the copy to the
1747  * rangetable. Note that the copy does not get added to the jointree.
1748  * Also note that there's a hack in fireRIRrules to avoid calling this
1749  * function again when it arrives at the copied RTE.
1750  */
1751  if (parsetree->commandType == CMD_INSERT)
1752  return parsetree;
1753  else if (parsetree->commandType == CMD_UPDATE ||
1754  parsetree->commandType == CMD_DELETE ||
1755  parsetree->commandType == CMD_MERGE)
1756  {
1757  RangeTblEntry *newrte;
1758  Var *var;
1759  TargetEntry *tle;
1760 
1761  rte = rt_fetch(rt_index, parsetree->rtable);
1762  newrte = copyObject(rte);
1763  parsetree->rtable = lappend(parsetree->rtable, newrte);
1764  parsetree->resultRelation = list_length(parsetree->rtable);
1765  /* parsetree->mergeTargetRelation unchanged (use expanded view) */
1766 
1767  /*
1768  * For the most part, Vars referencing the view should remain as
1769  * they are, meaning that they implicitly represent OLD values.
1770  * But in the RETURNING list if any, we want such Vars to
1771  * represent NEW values, so change them to reference the new RTE.
1772  *
1773  * Since ChangeVarNodes scribbles on the tree in-place, copy the
1774  * RETURNING list first for safety.
1775  */
1776  parsetree->returningList = copyObject(parsetree->returningList);
1777  ChangeVarNodes((Node *) parsetree->returningList, rt_index,
1778  parsetree->resultRelation, 0);
1779 
1780  /*
1781  * To allow the executor to compute the original view row to pass
1782  * to the INSTEAD OF trigger, we add a resjunk whole-row Var
1783  * referencing the original RTE. This will later get expanded
1784  * into a RowExpr computing all the OLD values of the view row.
1785  */
1786  var = makeWholeRowVar(rte, rt_index, 0, false);
1787  tle = makeTargetEntry((Expr *) var,
1788  list_length(parsetree->targetList) + 1,
1789  pstrdup("wholerow"),
1790  true);
1791 
1792  parsetree->targetList = lappend(parsetree->targetList, tle);
1793 
1794  /* Now, continue with expanding the original view RTE */
1795  }
1796  else
1797  elog(ERROR, "unrecognized commandType: %d",
1798  (int) parsetree->commandType);
1799  }
1800 
1801  /*
1802  * Check if there's a FOR [KEY] UPDATE/SHARE clause applying to this view.
1803  *
1804  * Note: we needn't explicitly consider any such clauses appearing in
1805  * ancestor query levels; their effects have already been pushed down to
1806  * here by markQueryForLocking, and will be reflected in "rc".
1807  */
1808  rc = get_parse_rowmark(parsetree, rt_index);
1809 
1810  /*
1811  * Make a modifiable copy of the view query, and acquire needed locks on
1812  * the relations it mentions. Force at least RowShareLock for all such
1813  * rels if there's a FOR [KEY] UPDATE/SHARE clause affecting this view.
1814  */
1815  rule_action = copyObject(linitial(rule->actions));
1816 
1817  AcquireRewriteLocks(rule_action, true, (rc != NULL));
1818 
1819  /*
1820  * If FOR [KEY] UPDATE/SHARE of view, mark all the contained tables as
1821  * implicit FOR [KEY] UPDATE/SHARE, the same as the parser would have done
1822  * if the view's subquery had been written out explicitly.
1823  */
1824  if (rc != NULL)
1825  markQueryForLocking(rule_action, (Node *) rule_action->jointree,
1826  rc->strength, rc->waitPolicy, true);
1827 
1828  /*
1829  * Recursively expand any view references inside the view.
1830  */
1831  rule_action = fireRIRrules(rule_action, activeRIRs);
1832 
1833  /*
1834  * Now, plug the view query in as a subselect, converting the relation's
1835  * original RTE to a subquery RTE.
1836  */
1837  rte = rt_fetch(rt_index, parsetree->rtable);
1838 
1839  rte->rtekind = RTE_SUBQUERY;
1840  rte->subquery = rule_action;
1841  rte->security_barrier = RelationIsSecurityView(relation);
1842 
1843  /*
1844  * Clear fields that should not be set in a subquery RTE. Note that we
1845  * leave the relid, relkind, rellockmode, and perminfoindex fields set, so
1846  * that the view relation can be appropriately locked before execution and
1847  * its permissions checked.
1848  */
1849  rte->tablesample = NULL;
1850  rte->inh = false; /* must not be set for a subquery */
1851 
1852  /*
1853  * Since we allow CREATE OR REPLACE VIEW to add columns to a view, the
1854  * rule_action might emit more columns than we expected when the current
1855  * query was parsed. Various places expect rte->eref->colnames to be
1856  * consistent with the non-junk output columns of the subquery, so patch
1857  * things up if necessary by adding some dummy column names.
1858  */
1859  numCols = ExecCleanTargetListLength(rule_action->targetList);
1860  while (list_length(rte->eref->colnames) < numCols)
1861  {
1862  rte->eref->colnames = lappend(rte->eref->colnames,
1863  makeString(pstrdup("?column?")));
1864  }
1865 
1866  return parsetree;
1867 }
int ExecCleanTargetListLength(List *targetlist)
Definition: execUtils.c:1119
Var * makeWholeRowVar(RangeTblEntry *rte, int varno, Index varlevelsup, bool allowScalar)
Definition: makefuncs.c:135
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:240
char * pstrdup(const char *in)
Definition: mcxt.c:1696
@ CMD_MERGE
Definition: nodes.h:269
@ CMD_INSERT
Definition: nodes.h:267
@ CMD_DELETE
Definition: nodes.h:268
@ CMD_UPDATE
Definition: nodes.h:266
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
#define RelationIsSecurityView(relation)
Definition: rel.h:426
static void markQueryForLocking(Query *qry, Node *jtnode, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
static Query * fireRIRrules(Query *parsetree, List *activeRIRs)
void ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
Definition: rewriteManip.c:674
List * returningList
Definition: parsenodes.h:198
CmdType commandType
Definition: parsenodes.h:121
List * targetList
Definition: parsenodes.h:191
struct TableSampleClause * tablesample
Definition: parsenodes.h:1108
LockClauseStrength strength
Definition: parsenodes.h:1583
LockWaitPolicy waitPolicy
Definition: parsenodes.h:1584
Definition: localtime.c:73
String * makeString(char *str)
Definition: value.c:63

References AcquireRewriteLocks(), ChangeVarNodes(), CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_UPDATE, Query::commandType, copyObject, elog, ERROR, ExecCleanTargetListLength(), fireRIRrules(), get_parse_rowmark(), RangeTblEntry::inh, Query::jointree, lappend(), linitial, list_length(), makeString(), makeTargetEntry(), makeWholeRowVar(), markQueryForLocking(), pstrdup(), RelationIsSecurityView, Query::returningList, rt_fetch, Query::rtable, RTE_SUBQUERY, RangeTblEntry::rtekind, RowMarkClause::strength, RangeTblEntry::subquery, RangeTblEntry::tablesample, Query::targetList, and RowMarkClause::waitPolicy.

Referenced by fireRIRrules().

◆ build_column_default()

Node* build_column_default ( Relation  rel,
int  attrno 
)

Definition at line 1223 of file rewriteHandler.c.

1224 {
1225  TupleDesc rd_att = rel->rd_att;
1226  Form_pg_attribute att_tup = TupleDescAttr(rd_att, attrno - 1);
1227  Oid atttype = att_tup->atttypid;
1228  int32 atttypmod = att_tup->atttypmod;
1229  Node *expr = NULL;
1230  Oid exprtype;
1231 
1232  if (att_tup->attidentity)
1233  {
1235 
1236  nve->seqid = getIdentitySequence(rel, attrno, false);
1237  nve->typeId = att_tup->atttypid;
1238 
1239  return (Node *) nve;
1240  }
1241 
1242  /*
1243  * If relation has a default for this column, fetch that expression.
1244  */
1245  if (att_tup->atthasdef)
1246  {
1247  expr = TupleDescGetDefault(rd_att, attrno);
1248  if (expr == NULL)
1249  elog(ERROR, "default expression not found for attribute %d of relation \"%s\"",
1250  attrno, RelationGetRelationName(rel));
1251  }
1252 
1253  /*
1254  * No per-column default, so look for a default for the type itself. But
1255  * not for generated columns.
1256  */
1257  if (expr == NULL && !att_tup->attgenerated)
1258  expr = get_typdefault(atttype);
1259 
1260  if (expr == NULL)
1261  return NULL; /* No default anywhere */
1262 
1263  /*
1264  * Make sure the value is coerced to the target column type; this will
1265  * generally be true already, but there seem to be some corner cases
1266  * involving domain defaults where it might not be true. This should match
1267  * the parser's processing of non-defaulted expressions --- see
1268  * transformAssignedExpr().
1269  */
1270  exprtype = exprType(expr);
1271 
1272  expr = coerce_to_target_type(NULL, /* no UNKNOWN params here */
1273  expr, exprtype,
1274  atttype, atttypmod,
1277  -1);
1278  if (expr == NULL)
1279  ereport(ERROR,
1280  (errcode(ERRCODE_DATATYPE_MISMATCH),
1281  errmsg("column \"%s\" is of type %s"
1282  " but default expression is of type %s",
1283  NameStr(att_tup->attname),
1284  format_type_be(atttype),
1285  format_type_be(exprtype)),
1286  errhint("You will need to rewrite or cast the expression.")));
1287 
1288  return expr;
1289 }
#define NameStr(name)
Definition: c.h:746
signed int int32
Definition: c.h:494
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
Node * get_typdefault(Oid typid)
Definition: lsyscache.c:2448
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define makeNode(_type_)
Definition: nodes.h:155
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:78
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
Oid getIdentitySequence(Relation rel, AttrNumber attnum, bool missing_ok)
Definition: pg_depend.c:946
unsigned int Oid
Definition: postgres_ext.h:31
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:736
@ COERCION_ASSIGNMENT
Definition: primnodes.h:715
#define RelationGetRelationName(relation)
Definition: rel.h:539
TupleDesc rd_att
Definition: rel.h:112
Node * TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
Definition: tupdesc.c:899
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, elog, ereport, errcode(), errhint(), errmsg(), ERROR, exprType(), format_type_be(), get_typdefault(), getIdentitySequence(), makeNode, NameStr, RelationData::rd_att, RelationGetRelationName, NextValueExpr::seqid, TupleDescAttr, TupleDescGetDefault(), and NextValueExpr::typeId.

Referenced by ATExecAddColumn(), ATExecAlterColumnType(), ATExecSetExpression(), BeginCopyFrom(), ExecInitStoredGenerated(), rewriteTargetListIU(), rewriteValuesRTE(), and slot_fill_defaults().

◆ CopyAndAddInvertedQual()

static Query* CopyAndAddInvertedQual ( Query parsetree,
Node rule_qual,
int  rt_index,
CmdType  event 
)
static

Definition at line 2269 of file rewriteHandler.c.

2273 {
2274  /* Don't scribble on the passed qual (it's in the relcache!) */
2275  Node *new_qual = copyObject(rule_qual);
2277 
2278  context.for_execute = true;
2279 
2280  /*
2281  * In case there are subqueries in the qual, acquire necessary locks and
2282  * fix any deleted JOIN RTE entries. (This is somewhat redundant with
2283  * rewriteRuleAction, but not entirely ... consider restructuring so that
2284  * we only need to process the qual this way once.)
2285  */
2286  (void) acquireLocksOnSubLinks(new_qual, &context);
2287 
2288  /* Fix references to OLD */
2289  ChangeVarNodes(new_qual, PRS2_OLD_VARNO, rt_index, 0);
2290  /* Fix references to NEW */
2291  if (event == CMD_INSERT || event == CMD_UPDATE)
2292  new_qual = ReplaceVarsFromTargetList(new_qual,
2294  0,
2295  rt_fetch(rt_index,
2296  parsetree->rtable),
2297  parsetree->targetList,
2298  (event == CMD_UPDATE) ?
2301  rt_index,
2302  &parsetree->hasSubLinks);
2303  /* And attach the fixed qual */
2304  AddInvertedQual(parsetree, new_qual);
2305 
2306  return parsetree;
2307 }
#define PRS2_OLD_VARNO
Definition: primnodes.h:244
#define PRS2_NEW_VARNO
Definition: primnodes.h:245
Node * ReplaceVarsFromTargetList(Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
void AddInvertedQual(Query *parsetree, Node *qual)
@ REPLACEVARS_SUBSTITUTE_NULL
Definition: rewriteManip.h:40
@ REPLACEVARS_CHANGE_VARNO
Definition: rewriteManip.h:39

References acquireLocksOnSubLinks(), AddInvertedQual(), ChangeVarNodes(), CMD_INSERT, CMD_UPDATE, context, copyObject, PRS2_NEW_VARNO, PRS2_OLD_VARNO, REPLACEVARS_CHANGE_VARNO, REPLACEVARS_SUBSTITUTE_NULL, ReplaceVarsFromTargetList(), rt_fetch, Query::rtable, and Query::targetList.

Referenced by fireRules().

◆ error_view_not_updatable()

void error_view_not_updatable ( Relation  view,
CmdType  command,
List mergeActionList,
const char *  detail 
)

Definition at line 3067 of file rewriteHandler.c.

3071 {
3072  TriggerDesc *trigDesc = view->trigdesc;
3073 
3074  switch (command)
3075  {
3076  case CMD_INSERT:
3077  ereport(ERROR,
3078  errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3079  errmsg("cannot insert into view \"%s\"",
3080  RelationGetRelationName(view)),
3081  detail ? errdetail_internal("%s", _(detail)) : 0,
3082  errhint("To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule."));
3083  break;
3084  case CMD_UPDATE:
3085  ereport(ERROR,
3086  errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3087  errmsg("cannot update view \"%s\"",
3088  RelationGetRelationName(view)),
3089  detail ? errdetail_internal("%s", _(detail)) : 0,
3090  errhint("To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule."));
3091  break;
3092  case CMD_DELETE:
3093  ereport(ERROR,
3094  errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3095  errmsg("cannot delete from view \"%s\"",
3096  RelationGetRelationName(view)),
3097  detail ? errdetail_internal("%s", _(detail)) : 0,
3098  errhint("To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule."));
3099  break;
3100  case CMD_MERGE:
3101 
3102  /*
3103  * Note that the error hints here differ from above, since MERGE
3104  * doesn't support rules.
3105  */
3106  foreach_node(MergeAction, action, mergeActionList)
3107  {
3108  switch (action->commandType)
3109  {
3110  case CMD_INSERT:
3111  if (!trigDesc || !trigDesc->trig_insert_instead_row)
3112  ereport(ERROR,
3113  errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3114  errmsg("cannot insert into view \"%s\"",
3115  RelationGetRelationName(view)),
3116  detail ? errdetail_internal("%s", _(detail)) : 0,
3117  errhint("To enable inserting into the view using MERGE, provide an INSTEAD OF INSERT trigger."));
3118  break;
3119  case CMD_UPDATE:
3120  if (!trigDesc || !trigDesc->trig_update_instead_row)
3121  ereport(ERROR,
3122  errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3123  errmsg("cannot update view \"%s\"",
3124  RelationGetRelationName(view)),
3125  detail ? errdetail_internal("%s", _(detail)) : 0,
3126  errhint("To enable updating the view using MERGE, provide an INSTEAD OF UPDATE trigger."));
3127  break;
3128  case CMD_DELETE:
3129  if (!trigDesc || !trigDesc->trig_delete_instead_row)
3130  ereport(ERROR,
3131  errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3132  errmsg("cannot delete from view \"%s\"",
3133  RelationGetRelationName(view)),
3134  detail ? errdetail_internal("%s", _(detail)) : 0,
3135  errhint("To enable deleting from the view using MERGE, provide an INSTEAD OF DELETE trigger."));
3136  break;
3137  case CMD_NOTHING:
3138  break;
3139  default:
3140  elog(ERROR, "unrecognized commandType: %d", action->commandType);
3141  break;
3142  }
3143  }
3144  break;
3145  default:
3146  elog(ERROR, "unrecognized CmdType: %d", (int) command);
3147  break;
3148  }
3149 }
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1230
#define _(x)
Definition: elog.c:90
@ CMD_NOTHING
Definition: nodes.h:272
#define foreach_node(type, var, lst)
Definition: pg_list.h:496
TriggerDesc * trigdesc
Definition: rel.h:117
bool trig_update_instead_row
Definition: reltrigger.h:63
bool trig_delete_instead_row
Definition: reltrigger.h:68
bool trig_insert_instead_row
Definition: reltrigger.h:58

References _, generate_unaccent_rules::action, CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_NOTHING, CMD_UPDATE, elog, ereport, errcode(), errdetail_internal(), errhint(), errmsg(), ERROR, foreach_node, RelationGetRelationName, TriggerDesc::trig_delete_instead_row, TriggerDesc::trig_insert_instead_row, TriggerDesc::trig_update_instead_row, and RelationData::trigdesc.

Referenced by CheckValidResultRel(), RewriteQuery(), and rewriteTargetView().

◆ findDefaultOnlyColumns()

static Bitmapset * findDefaultOnlyColumns ( RangeTblEntry rte)
static

Definition at line 1320 of file rewriteHandler.c.

1321 {
1322  Bitmapset *default_only_cols = NULL;
1323  ListCell *lc;
1324 
1325  foreach(lc, rte->values_lists)
1326  {
1327  List *sublist = (List *) lfirst(lc);
1328  ListCell *lc2;
1329  int i;
1330 
1331  if (default_only_cols == NULL)
1332  {
1333  /* Populate the initial result bitmap from the first row */
1334  i = 0;
1335  foreach(lc2, sublist)
1336  {
1337  Node *col = (Node *) lfirst(lc2);
1338 
1339  i++;
1340  if (IsA(col, SetToDefault))
1341  default_only_cols = bms_add_member(default_only_cols, i);
1342  }
1343  }
1344  else
1345  {
1346  /* Update the result bitmap from this next row */
1347  i = 0;
1348  foreach(lc2, sublist)
1349  {
1350  Node *col = (Node *) lfirst(lc2);
1351 
1352  i++;
1353  if (!IsA(col, SetToDefault))
1354  default_only_cols = bms_del_member(default_only_cols, i);
1355  }
1356  }
1357 
1358  /*
1359  * If no column in the rows read so far contains only DEFAULT items,
1360  * we are done.
1361  */
1362  if (bms_is_empty(default_only_cols))
1363  break;
1364  }
1365 
1366  return default_only_cols;
1367 }
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
#define bms_is_empty(a)
Definition: bitmapset.h:118
int i
Definition: isn.c:73
List * values_lists
Definition: parsenodes.h:1200

References bms_add_member(), bms_del_member(), bms_is_empty, i, IsA, lfirst, and RangeTblEntry::values_lists.

Referenced by rewriteTargetListIU().

◆ fireRIRonSubLink()

static bool fireRIRonSubLink ( Node node,
List activeRIRs 
)
static

Definition at line 1946 of file rewriteHandler.c.

1947 {
1948  if (node == NULL)
1949  return false;
1950  if (IsA(node, SubLink))
1951  {
1952  SubLink *sub = (SubLink *) node;
1953 
1954  /* Do what we came for */
1955  sub->subselect = (Node *) fireRIRrules((Query *) sub->subselect,
1956  activeRIRs);
1957  /* Fall through to process lefthand args of SubLink */
1958  }
1959 
1960  /*
1961  * Do NOT recurse into Query nodes, because fireRIRrules already processed
1962  * subselects of subselects for us.
1963  */
1965  (void *) activeRIRs);
1966 }
static bool fireRIRonSubLink(Node *node, List *activeRIRs)

References expression_tree_walker, fireRIRrules(), IsA, and SubLink::subselect.

Referenced by fireRIRrules().

◆ fireRIRrules()

static Query * fireRIRrules ( Query parsetree,
List activeRIRs 
)
static

Definition at line 1977 of file rewriteHandler.c.

1978 {
1979  int origResultRelation = parsetree->resultRelation;
1980  int rt_index;
1981  ListCell *lc;
1982 
1983  /*
1984  * Expand SEARCH and CYCLE clauses in CTEs.
1985  *
1986  * This is just a convenient place to do this, since we are already
1987  * looking at each Query.
1988  */
1989  foreach(lc, parsetree->cteList)
1990  {
1992 
1993  if (cte->search_clause || cte->cycle_clause)
1994  {
1995  cte = rewriteSearchAndCycle(cte);
1996  lfirst(lc) = cte;
1997  }
1998  }
1999 
2000  /*
2001  * don't try to convert this into a foreach loop, because rtable list can
2002  * get changed each time through...
2003  */
2004  rt_index = 0;
2005  while (rt_index < list_length(parsetree->rtable))
2006  {
2007  RangeTblEntry *rte;
2008  Relation rel;
2009  List *locks;
2010  RuleLock *rules;
2011  RewriteRule *rule;
2012  int i;
2013 
2014  ++rt_index;
2015 
2016  rte = rt_fetch(rt_index, parsetree->rtable);
2017 
2018  /*
2019  * A subquery RTE can't have associated rules, so there's nothing to
2020  * do to this level of the query, but we must recurse into the
2021  * subquery to expand any rule references in it.
2022  */
2023  if (rte->rtekind == RTE_SUBQUERY)
2024  {
2025  rte->subquery = fireRIRrules(rte->subquery, activeRIRs);
2026  continue;
2027  }
2028 
2029  /*
2030  * Joins and other non-relation RTEs can be ignored completely.
2031  */
2032  if (rte->rtekind != RTE_RELATION)
2033  continue;
2034 
2035  /*
2036  * Always ignore RIR rules for materialized views referenced in
2037  * queries. (This does not prevent refreshing MVs, since they aren't
2038  * referenced in their own query definitions.)
2039  *
2040  * Note: in the future we might want to allow MVs to be conditionally
2041  * expanded as if they were regular views, if they are not scannable.
2042  * In that case this test would need to be postponed till after we've
2043  * opened the rel, so that we could check its state.
2044  */
2045  if (rte->relkind == RELKIND_MATVIEW)
2046  continue;
2047 
2048  /*
2049  * In INSERT ... ON CONFLICT, ignore the EXCLUDED pseudo-relation;
2050  * even if it points to a view, we needn't expand it, and should not
2051  * because we want the RTE to remain of RTE_RELATION type. Otherwise,
2052  * it would get changed to RTE_SUBQUERY type, which is an
2053  * untested/unsupported situation.
2054  */
2055  if (parsetree->onConflict &&
2056  rt_index == parsetree->onConflict->exclRelIndex)
2057  continue;
2058 
2059  /*
2060  * If the table is not referenced in the query, then we ignore it.
2061  * This prevents infinite expansion loop due to new rtable entries
2062  * inserted by expansion of a rule. A table is referenced if it is
2063  * part of the join set (a source table), or is referenced by any Var
2064  * nodes, or is the result table.
2065  */
2066  if (rt_index != parsetree->resultRelation &&
2067  !rangeTableEntry_used((Node *) parsetree, rt_index, 0))
2068  continue;
2069 
2070  /*
2071  * Also, if this is a new result relation introduced by
2072  * ApplyRetrieveRule, we don't want to do anything more with it.
2073  */
2074  if (rt_index == parsetree->resultRelation &&
2075  rt_index != origResultRelation)
2076  continue;
2077 
2078  /*
2079  * We can use NoLock here since either the parser or
2080  * AcquireRewriteLocks should have locked the rel already.
2081  */
2082  rel = table_open(rte->relid, NoLock);
2083 
2084  /*
2085  * Collect the RIR rules that we must apply
2086  */
2087  rules = rel->rd_rules;
2088  if (rules != NULL)
2089  {
2090  locks = NIL;
2091  for (i = 0; i < rules->numLocks; i++)
2092  {
2093  rule = rules->rules[i];
2094  if (rule->event != CMD_SELECT)
2095  continue;
2096 
2097  locks = lappend(locks, rule);
2098  }
2099 
2100  /*
2101  * If we found any, apply them --- but first check for recursion!
2102  */
2103  if (locks != NIL)
2104  {
2105  ListCell *l;
2106 
2107  if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
2108  ereport(ERROR,
2109  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2110  errmsg("infinite recursion detected in rules for relation \"%s\"",
2111  RelationGetRelationName(rel))));
2112  activeRIRs = lappend_oid(activeRIRs, RelationGetRelid(rel));
2113 
2114  foreach(l, locks)
2115  {
2116  rule = lfirst(l);
2117 
2118  parsetree = ApplyRetrieveRule(parsetree,
2119  rule,
2120  rt_index,
2121  rel,
2122  activeRIRs);
2123  }
2124 
2125  activeRIRs = list_delete_last(activeRIRs);
2126  }
2127  }
2128 
2129  table_close(rel, NoLock);
2130  }
2131 
2132  /* Recurse into subqueries in WITH */
2133  foreach(lc, parsetree->cteList)
2134  {
2135  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
2136 
2137  cte->ctequery = (Node *)
2138  fireRIRrules((Query *) cte->ctequery, activeRIRs);
2139  }
2140 
2141  /*
2142  * Recurse into sublink subqueries, too. But we already did the ones in
2143  * the rtable and cteList.
2144  */
2145  if (parsetree->hasSubLinks)
2146  query_tree_walker(parsetree, fireRIRonSubLink, (void *) activeRIRs,
2148 
2149  /*
2150  * Apply any row-level security policies. We do this last because it
2151  * requires special recursion detection if the new quals have sublink
2152  * subqueries, and if we did it in the loop above query_tree_walker would
2153  * then recurse into those quals a second time.
2154  */
2155  rt_index = 0;
2156  foreach(lc, parsetree->rtable)
2157  {
2158  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
2159  Relation rel;
2160  List *securityQuals;
2161  List *withCheckOptions;
2162  bool hasRowSecurity;
2163  bool hasSubLinks;
2164 
2165  ++rt_index;
2166 
2167  /* Only normal relations can have RLS policies */
2168  if (rte->rtekind != RTE_RELATION ||
2169  (rte->relkind != RELKIND_RELATION &&
2170  rte->relkind != RELKIND_PARTITIONED_TABLE))
2171  continue;
2172 
2173  rel = table_open(rte->relid, NoLock);
2174 
2175  /*
2176  * Fetch any new security quals that must be applied to this RTE.
2177  */
2178  get_row_security_policies(parsetree, rte, rt_index,
2179  &securityQuals, &withCheckOptions,
2180  &hasRowSecurity, &hasSubLinks);
2181 
2182  if (securityQuals != NIL || withCheckOptions != NIL)
2183  {
2184  if (hasSubLinks)
2185  {
2187 
2188  /*
2189  * Recursively process the new quals, checking for infinite
2190  * recursion.
2191  */
2192  if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
2193  ereport(ERROR,
2194  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2195  errmsg("infinite recursion detected in policy for relation \"%s\"",
2196  RelationGetRelationName(rel))));
2197 
2198  activeRIRs = lappend_oid(activeRIRs, RelationGetRelid(rel));
2199 
2200  /*
2201  * get_row_security_policies just passed back securityQuals
2202  * and/or withCheckOptions, and there were SubLinks, make sure
2203  * we lock any relations which are referenced.
2204  *
2205  * These locks would normally be acquired by the parser, but
2206  * securityQuals and withCheckOptions are added post-parsing.
2207  */
2208  context.for_execute = true;
2209  (void) acquireLocksOnSubLinks((Node *) securityQuals, &context);
2210  (void) acquireLocksOnSubLinks((Node *) withCheckOptions,
2211  &context);
2212 
2213  /*
2214  * Now that we have the locks on anything added by
2215  * get_row_security_policies, fire any RIR rules for them.
2216  */
2217  expression_tree_walker((Node *) securityQuals,
2218  fireRIRonSubLink, (void *) activeRIRs);
2219 
2220  expression_tree_walker((Node *) withCheckOptions,
2221  fireRIRonSubLink, (void *) activeRIRs);
2222 
2223  activeRIRs = list_delete_last(activeRIRs);
2224  }
2225 
2226  /*
2227  * Add the new security barrier quals to the start of the RTE's
2228  * list so that they get applied before any existing barrier quals
2229  * (which would have come from a security-barrier view, and should
2230  * get lower priority than RLS conditions on the table itself).
2231  */
2232  rte->securityQuals = list_concat(securityQuals,
2233  rte->securityQuals);
2234 
2235  parsetree->withCheckOptions = list_concat(withCheckOptions,
2236  parsetree->withCheckOptions);
2237  }
2238 
2239  /*
2240  * Make sure the query is marked correctly if row-level security
2241  * applies, or if the new quals had sublinks.
2242  */
2243  if (hasRowSecurity)
2244  parsetree->hasRowSecurity = true;
2245  if (hasSubLinks)
2246  parsetree->hasSubLinks = true;
2247 
2248  table_close(rel, NoLock);
2249  }
2250 
2251  return parsetree;
2252 }
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:722
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
List * list_delete_last(List *list)
Definition: list.c:957
@ CMD_SELECT
Definition: nodes.h:265
#define RelationGetRelid(relation)
Definition: rel.h:505
static Query * ApplyRetrieveRule(Query *parsetree, RewriteRule *rule, int rt_index, Relation relation, List *activeRIRs)
bool rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
Definition: rewriteManip.c:966
CommonTableExpr * rewriteSearchAndCycle(CommonTableExpr *cte)
void get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index, List **securityQuals, List **withCheckOptions, bool *hasRowSecurity, bool *hasSubLinks)
Definition: rowsecurity.c:98
OnConflictExpr * onConflict
Definition: parsenodes.h:196
RuleLock * rd_rules
Definition: rel.h:115
static struct rule * rules
Definition: zic.c:283

References acquireLocksOnSubLinks(), ApplyRetrieveRule(), CMD_SELECT, context, Query::cteList, CommonTableExpr::ctequery, ereport, errcode(), errmsg(), ERROR, OnConflictExpr::exclRelIndex, expression_tree_walker, fireRIRonSubLink(), get_row_security_policies(), i, lappend(), lappend_oid(), lfirst, lfirst_node, list_concat(), list_delete_last(), list_length(), list_member_oid(), NIL, NoLock, Query::onConflict, QTW_IGNORE_RC_SUBQUERIES, query_tree_walker, rangeTableEntry_used(), RelationData::rd_rules, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, rewriteSearchAndCycle(), rt_fetch, Query::rtable, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, rules, RangeTblEntry::subquery, table_close(), and table_open().

Referenced by ApplyRetrieveRule(), fireRIRonSubLink(), and QueryRewrite().

◆ fireRules()

static List* fireRules ( Query parsetree,
int  rt_index,
CmdType  event,
List locks,
bool instead_flag,
bool returning_flag,
Query **  qual_product 
)
static

Definition at line 2339 of file rewriteHandler.c.

2346 {
2347  List *results = NIL;
2348  ListCell *l;
2349 
2350  foreach(l, locks)
2351  {
2352  RewriteRule *rule_lock = (RewriteRule *) lfirst(l);
2353  Node *event_qual = rule_lock->qual;
2354  List *actions = rule_lock->actions;
2355  QuerySource qsrc;
2356  ListCell *r;
2357 
2358  /* Determine correct QuerySource value for actions */
2359  if (rule_lock->isInstead)
2360  {
2361  if (event_qual != NULL)
2362  qsrc = QSRC_QUAL_INSTEAD_RULE;
2363  else
2364  {
2365  qsrc = QSRC_INSTEAD_RULE;
2366  *instead_flag = true; /* report unqualified INSTEAD */
2367  }
2368  }
2369  else
2370  qsrc = QSRC_NON_INSTEAD_RULE;
2371 
2372  if (qsrc == QSRC_QUAL_INSTEAD_RULE)
2373  {
2374  /*
2375  * If there are INSTEAD rules with qualifications, the original
2376  * query is still performed. But all the negated rule
2377  * qualifications of the INSTEAD rules are added so it does its
2378  * actions only in cases where the rule quals of all INSTEAD rules
2379  * are false. Think of it as the default action in a case. We save
2380  * this in *qual_product so RewriteQuery() can add it to the query
2381  * list after we mangled it up enough.
2382  *
2383  * If we have already found an unqualified INSTEAD rule, then
2384  * *qual_product won't be used, so don't bother building it.
2385  */
2386  if (!*instead_flag)
2387  {
2388  if (*qual_product == NULL)
2389  *qual_product = copyObject(parsetree);
2390  *qual_product = CopyAndAddInvertedQual(*qual_product,
2391  event_qual,
2392  rt_index,
2393  event);
2394  }
2395  }
2396 
2397  /* Now process the rule's actions and add them to the result list */
2398  foreach(r, actions)
2399  {
2400  Query *rule_action = lfirst(r);
2401 
2402  if (rule_action->commandType == CMD_NOTHING)
2403  continue;
2404 
2405  rule_action = rewriteRuleAction(parsetree, rule_action,
2406  event_qual, rt_index, event,
2407  returning_flag);
2408 
2409  rule_action->querySource = qsrc;
2410  rule_action->canSetTag = false; /* might change later */
2411 
2412  results = lappend(results, rule_action);
2413  }
2414  }
2415 
2416  return results;
2417 }
QuerySource
Definition: parsenodes.h:35
@ QSRC_NON_INSTEAD_RULE
Definition: parsenodes.h:40
@ QSRC_QUAL_INSTEAD_RULE
Definition: parsenodes.h:39
@ QSRC_INSTEAD_RULE
Definition: parsenodes.h:38
static Query * rewriteRuleAction(Query *parsetree, Query *rule_action, Node *rule_qual, int rt_index, CmdType event, bool *returning_flag)
static Query * CopyAndAddInvertedQual(Query *parsetree, Node *rule_qual, int rt_index, CmdType event)
List * actions
Definition: prs2lock.h:29
bool isInstead
Definition: prs2lock.h:31
Node * qual
Definition: prs2lock.h:28

References RewriteRule::actions, CMD_NOTHING, Query::commandType, CopyAndAddInvertedQual(), copyObject, RewriteRule::isInstead, lappend(), lfirst, NIL, QSRC_INSTEAD_RULE, QSRC_NON_INSTEAD_RULE, QSRC_QUAL_INSTEAD_RULE, RewriteRule::qual, and rewriteRuleAction().

Referenced by RewriteQuery().

◆ get_assignment_input()

static Node * get_assignment_input ( Node node)
static

Definition at line 1194 of file rewriteHandler.c.

1195 {
1196  if (node == NULL)
1197  return NULL;
1198  if (IsA(node, FieldStore))
1199  {
1200  FieldStore *fstore = (FieldStore *) node;
1201 
1202  return (Node *) fstore->arg;
1203  }
1204  else if (IsA(node, SubscriptingRef))
1205  {
1206  SubscriptingRef *sbsref = (SubscriptingRef *) node;
1207 
1208  if (sbsref->refassgnexpr == NULL)
1209  return NULL;
1210 
1211  return (Node *) sbsref->refexpr;
1212  }
1213 
1214  return NULL;
1215 }
Expr * arg
Definition: primnodes.h:1159
Expr * refassgnexpr
Definition: primnodes.h:703
Expr * refexpr
Definition: primnodes.h:701

References FieldStore::arg, IsA, SubscriptingRef::refassgnexpr, and SubscriptingRef::refexpr.

Referenced by process_matched_tle().

◆ get_view_query()

Query* get_view_query ( Relation  view)

Definition at line 2430 of file rewriteHandler.c.

2431 {
2432  int i;
2433 
2434  Assert(view->rd_rel->relkind == RELKIND_VIEW);
2435 
2436  for (i = 0; i < view->rd_rules->numLocks; i++)
2437  {
2438  RewriteRule *rule = view->rd_rules->rules[i];
2439 
2440  if (rule->event == CMD_SELECT)
2441  {
2442  /* A _RETURN rule should have only one action */
2443  if (list_length(rule->actions) != 1)
2444  elog(ERROR, "invalid _RETURN rule action specification");
2445 
2446  return (Query *) linitial(rule->actions);
2447  }
2448  }
2449 
2450  elog(ERROR, "failed to find _RETURN rule for view");
2451  return NULL; /* keep compiler quiet */
2452 }
RewriteRule ** rules
Definition: prs2lock.h:43
int numLocks
Definition: prs2lock.h:42

References Assert, CMD_SELECT, elog, ERROR, i, linitial, list_length(), RuleLock::numLocks, RelationData::rd_rel, RelationData::rd_rules, and RuleLock::rules.

Referenced by ATExecSetRelOptions(), LockViewRecurse(), relation_is_updatable(), and rewriteTargetView().

◆ markQueryForLocking()

static void markQueryForLocking ( Query qry,
Node jtnode,
LockClauseStrength  strength,
LockWaitPolicy  waitPolicy,
bool  pushedDown 
)
static

Definition at line 1882 of file rewriteHandler.c.

1885 {
1886  if (jtnode == NULL)
1887  return;
1888  if (IsA(jtnode, RangeTblRef))
1889  {
1890  int rti = ((RangeTblRef *) jtnode)->rtindex;
1891  RangeTblEntry *rte = rt_fetch(rti, qry->rtable);
1892 
1893  if (rte->rtekind == RTE_RELATION)
1894  {
1895  RTEPermissionInfo *perminfo;
1896 
1897  applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
1898 
1899  perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
1900  perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
1901  }
1902  else if (rte->rtekind == RTE_SUBQUERY)
1903  {
1904  applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
1905  /* FOR UPDATE/SHARE of subquery is propagated to subquery's rels */
1907  strength, waitPolicy, true);
1908  }
1909  /* other RTE types are unaffected by FOR UPDATE */
1910  }
1911  else if (IsA(jtnode, FromExpr))
1912  {
1913  FromExpr *f = (FromExpr *) jtnode;
1914  ListCell *l;
1915 
1916  foreach(l, f->fromlist)
1917  markQueryForLocking(qry, lfirst(l), strength, waitPolicy, pushedDown);
1918  }
1919  else if (IsA(jtnode, JoinExpr))
1920  {
1921  JoinExpr *j = (JoinExpr *) jtnode;
1922 
1923  markQueryForLocking(qry, j->larg, strength, waitPolicy, pushedDown);
1924  markQueryForLocking(qry, j->rarg, strength, waitPolicy, pushedDown);
1925  }
1926  else
1927  elog(ERROR, "unrecognized node type: %d",
1928  (int) nodeTag(jtnode));
1929 }
int j
Definition: isn.c:74
#define nodeTag(nodeptr)
Definition: nodes.h:133
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
#define ACL_SELECT_FOR_UPDATE
Definition: parsenodes.h:94
void applyLockingClause(Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
Definition: analyze.c:3529
AclMode requiredPerms
Definition: parsenodes.h:1295

References ACL_SELECT_FOR_UPDATE, applyLockingClause(), elog, ERROR, FromExpr::fromlist, getRTEPermissionInfo(), IsA, j, Query::jointree, lfirst, nodeTag, RTEPermissionInfo::requiredPerms, rt_fetch, Query::rtable, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, and RangeTblEntry::subquery.

Referenced by ApplyRetrieveRule().

◆ matchLocks()

static List * matchLocks ( CmdType  event,
Relation  relation,
int  varno,
Query parsetree,
bool hasUpdate 
)
static

Definition at line 1641 of file rewriteHandler.c.

1646 {
1647  RuleLock *rulelocks = relation->rd_rules;
1648  List *matching_locks = NIL;
1649  int nlocks;
1650  int i;
1651 
1652  if (rulelocks == NULL)
1653  return NIL;
1654 
1655  if (parsetree->commandType != CMD_SELECT)
1656  {
1657  if (parsetree->resultRelation != varno)
1658  return NIL;
1659  }
1660 
1661  nlocks = rulelocks->numLocks;
1662 
1663  for (i = 0; i < nlocks; i++)
1664  {
1665  RewriteRule *oneLock = rulelocks->rules[i];
1666 
1667  if (oneLock->event == CMD_UPDATE)
1668  *hasUpdate = true;
1669 
1670  /*
1671  * Suppress ON INSERT/UPDATE/DELETE rules that are disabled or
1672  * configured to not fire during the current session's replication
1673  * role. ON SELECT rules will always be applied in order to keep views
1674  * working even in LOCAL or REPLICA role.
1675  */
1676  if (oneLock->event != CMD_SELECT)
1677  {
1679  {
1680  if (oneLock->enabled == RULE_FIRES_ON_ORIGIN ||
1681  oneLock->enabled == RULE_DISABLED)
1682  continue;
1683  }
1684  else /* ORIGIN or LOCAL ROLE */
1685  {
1686  if (oneLock->enabled == RULE_FIRES_ON_REPLICA ||
1687  oneLock->enabled == RULE_DISABLED)
1688  continue;
1689  }
1690 
1691  /* Non-SELECT rules are not supported for MERGE */
1692  if (parsetree->commandType == CMD_MERGE)
1693  ereport(ERROR,
1694  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1695  errmsg("cannot execute MERGE on relation \"%s\"",
1696  RelationGetRelationName(relation)),
1697  errdetail("MERGE is not supported for relations with rules."));
1698  }
1699 
1700  if (oneLock->event == event)
1701  {
1702  if (parsetree->commandType != CMD_SELECT ||
1703  rangeTableEntry_used((Node *) parsetree, varno, 0))
1704  matching_locks = lappend(matching_locks, oneLock);
1705  }
1706  }
1707 
1708  return matching_locks;
1709 }
int errdetail(const char *fmt,...)
Definition: elog.c:1203
#define RULE_FIRES_ON_ORIGIN
Definition: rewriteDefine.h:21
#define RULE_FIRES_ON_REPLICA
Definition: rewriteDefine.h:23
#define RULE_DISABLED
Definition: rewriteDefine.h:24
CmdType event
Definition: prs2lock.h:27
char enabled
Definition: prs2lock.h:30
int SessionReplicationRole
Definition: trigger.c:63
#define SESSION_REPLICATION_ROLE_REPLICA
Definition: trigger.h:141

References CMD_MERGE, CMD_SELECT, CMD_UPDATE, Query::commandType, RewriteRule::enabled, ereport, errcode(), errdetail(), errmsg(), ERROR, RewriteRule::event, i, lappend(), NIL, RuleLock::numLocks, rangeTableEntry_used(), RelationData::rd_rules, RelationGetRelationName, RULE_DISABLED, RULE_FIRES_ON_ORIGIN, RULE_FIRES_ON_REPLICA, RuleLock::rules, SESSION_REPLICATION_ROLE_REPLICA, and SessionReplicationRole.

Referenced by RewriteQuery(), and rewriteValuesRTE().

◆ process_matched_tle()

static TargetEntry * process_matched_tle ( TargetEntry src_tle,
TargetEntry prior_tle,
const char *  attrName 
)
static

Definition at line 1041 of file rewriteHandler.c.

1044 {
1045  TargetEntry *result;
1046  CoerceToDomain *coerce_expr = NULL;
1047  Node *src_expr;
1048  Node *prior_expr;
1049  Node *src_input;
1050  Node *prior_input;
1051  Node *priorbottom;
1052  Node *newexpr;
1053 
1054  if (prior_tle == NULL)
1055  {
1056  /*
1057  * Normal case where this is the first assignment to the attribute.
1058  */
1059  return src_tle;
1060  }
1061 
1062  /*----------
1063  * Multiple assignments to same attribute. Allow only if all are
1064  * FieldStore or SubscriptingRef assignment operations. This is a bit
1065  * tricky because what we may actually be looking at is a nest of
1066  * such nodes; consider
1067  * UPDATE tab SET col.fld1.subfld1 = x, col.fld2.subfld2 = y
1068  * The two expressions produced by the parser will look like
1069  * FieldStore(col, fld1, FieldStore(placeholder, subfld1, x))
1070  * FieldStore(col, fld2, FieldStore(placeholder, subfld2, y))
1071  * However, we can ignore the substructure and just consider the top
1072  * FieldStore or SubscriptingRef from each assignment, because it works to
1073  * combine these as
1074  * FieldStore(FieldStore(col, fld1,
1075  * FieldStore(placeholder, subfld1, x)),
1076  * fld2, FieldStore(placeholder, subfld2, y))
1077  * Note the leftmost expression goes on the inside so that the
1078  * assignments appear to occur left-to-right.
1079  *
1080  * For FieldStore, instead of nesting we can generate a single
1081  * FieldStore with multiple target fields. We must nest when
1082  * SubscriptingRefs are involved though.
1083  *
1084  * As a further complication, the destination column might be a domain,
1085  * resulting in each assignment containing a CoerceToDomain node over a
1086  * FieldStore or SubscriptingRef. These should have matching target
1087  * domains, so we strip them and reconstitute a single CoerceToDomain over
1088  * the combined FieldStore/SubscriptingRef nodes. (Notice that this has
1089  * the result that the domain's checks are applied only after we do all
1090  * the field or element updates, not after each one. This is desirable.)
1091  *----------
1092  */
1093  src_expr = (Node *) src_tle->expr;
1094  prior_expr = (Node *) prior_tle->expr;
1095 
1096  if (src_expr && IsA(src_expr, CoerceToDomain) &&
1097  prior_expr && IsA(prior_expr, CoerceToDomain) &&
1098  ((CoerceToDomain *) src_expr)->resulttype ==
1099  ((CoerceToDomain *) prior_expr)->resulttype)
1100  {
1101  /* we assume without checking that resulttypmod/resultcollid match */
1102  coerce_expr = (CoerceToDomain *) src_expr;
1103  src_expr = (Node *) ((CoerceToDomain *) src_expr)->arg;
1104  prior_expr = (Node *) ((CoerceToDomain *) prior_expr)->arg;
1105  }
1106 
1107  src_input = get_assignment_input(src_expr);
1108  prior_input = get_assignment_input(prior_expr);
1109  if (src_input == NULL ||
1110  prior_input == NULL ||
1111  exprType(src_expr) != exprType(prior_expr))
1112  ereport(ERROR,
1113  (errcode(ERRCODE_SYNTAX_ERROR),
1114  errmsg("multiple assignments to same column \"%s\"",
1115  attrName)));
1116 
1117  /*
1118  * Prior TLE could be a nest of assignments if we do this more than once.
1119  */
1120  priorbottom = prior_input;
1121  for (;;)
1122  {
1123  Node *newbottom = get_assignment_input(priorbottom);
1124 
1125  if (newbottom == NULL)
1126  break; /* found the original Var reference */
1127  priorbottom = newbottom;
1128  }
1129  if (!equal(priorbottom, src_input))
1130  ereport(ERROR,
1131  (errcode(ERRCODE_SYNTAX_ERROR),
1132  errmsg("multiple assignments to same column \"%s\"",
1133  attrName)));
1134 
1135  /*
1136  * Looks OK to nest 'em.
1137  */
1138  if (IsA(src_expr, FieldStore))
1139  {
1140  FieldStore *fstore = makeNode(FieldStore);
1141 
1142  if (IsA(prior_expr, FieldStore))
1143  {
1144  /* combine the two */
1145  memcpy(fstore, prior_expr, sizeof(FieldStore));
1146  fstore->newvals =
1147  list_concat_copy(((FieldStore *) prior_expr)->newvals,
1148  ((FieldStore *) src_expr)->newvals);
1149  fstore->fieldnums =
1150  list_concat_copy(((FieldStore *) prior_expr)->fieldnums,
1151  ((FieldStore *) src_expr)->fieldnums);
1152  }
1153  else
1154  {
1155  /* general case, just nest 'em */
1156  memcpy(fstore, src_expr, sizeof(FieldStore));
1157  fstore->arg = (Expr *) prior_expr;
1158  }
1159  newexpr = (Node *) fstore;
1160  }
1161  else if (IsA(src_expr, SubscriptingRef))
1162  {
1164 
1165  memcpy(sbsref, src_expr, sizeof(SubscriptingRef));
1166  sbsref->refexpr = (Expr *) prior_expr;
1167  newexpr = (Node *) sbsref;
1168  }
1169  else
1170  {
1171  elog(ERROR, "cannot happen");
1172  newexpr = NULL;
1173  }
1174 
1175  if (coerce_expr)
1176  {
1177  /* put back the CoerceToDomain */
1178  CoerceToDomain *newcoerce = makeNode(CoerceToDomain);
1179 
1180  memcpy(newcoerce, coerce_expr, sizeof(CoerceToDomain));
1181  newcoerce->arg = (Expr *) newexpr;
1182  newexpr = (Node *) newcoerce;
1183  }
1184 
1185  result = flatCopyTargetEntry(src_tle);
1186  result->expr = (Expr *) newexpr;
1187  return result;
1188 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:598
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition: makefuncs.c:273
void * arg
static Node * get_assignment_input(Node *node)
List * newvals
Definition: primnodes.h:1160

References arg, FieldStore::arg, CoerceToDomain::arg, elog, equal(), ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprType(), flatCopyTargetEntry(), get_assignment_input(), IsA, list_concat_copy(), makeNode, FieldStore::newvals, and SubscriptingRef::refexpr.

Referenced by rewriteTargetListIU().

◆ QueryRewrite()

List* QueryRewrite ( Query parsetree)

Definition at line 4361 of file rewriteHandler.c.

4362 {
4363  uint64 input_query_id = parsetree->queryId;
4364  List *querylist;
4365  List *results;
4366  ListCell *l;
4367  CmdType origCmdType;
4368  bool foundOriginalQuery;
4369  Query *lastInstead;
4370 
4371  /*
4372  * This function is only applied to top-level original queries
4373  */
4374  Assert(parsetree->querySource == QSRC_ORIGINAL);
4375  Assert(parsetree->canSetTag);
4376 
4377  /*
4378  * Step 1
4379  *
4380  * Apply all non-SELECT rules possibly getting 0 or many queries
4381  */
4382  querylist = RewriteQuery(parsetree, NIL, 0);
4383 
4384  /*
4385  * Step 2
4386  *
4387  * Apply all the RIR rules on each query
4388  *
4389  * This is also a handy place to mark each query with the original queryId
4390  */
4391  results = NIL;
4392  foreach(l, querylist)
4393  {
4394  Query *query = (Query *) lfirst(l);
4395 
4396  query = fireRIRrules(query, NIL);
4397 
4398  query->queryId = input_query_id;
4399 
4400  results = lappend(results, query);
4401  }
4402 
4403  /*
4404  * Step 3
4405  *
4406  * Determine which, if any, of the resulting queries is supposed to set
4407  * the command-result tag; and update the canSetTag fields accordingly.
4408  *
4409  * If the original query is still in the list, it sets the command tag.
4410  * Otherwise, the last INSTEAD query of the same kind as the original is
4411  * allowed to set the tag. (Note these rules can leave us with no query
4412  * setting the tag. The tcop code has to cope with this by setting up a
4413  * default tag based on the original un-rewritten query.)
4414  *
4415  * The Asserts verify that at most one query in the result list is marked
4416  * canSetTag. If we aren't checking asserts, we can fall out of the loop
4417  * as soon as we find the original query.
4418  */
4419  origCmdType = parsetree->commandType;
4420  foundOriginalQuery = false;
4421  lastInstead = NULL;
4422 
4423  foreach(l, results)
4424  {
4425  Query *query = (Query *) lfirst(l);
4426 
4427  if (query->querySource == QSRC_ORIGINAL)
4428  {
4429  Assert(query->canSetTag);
4430  Assert(!foundOriginalQuery);
4431  foundOriginalQuery = true;
4432 #ifndef USE_ASSERT_CHECKING
4433  break;
4434 #endif
4435  }
4436  else
4437  {
4438  Assert(!query->canSetTag);
4439  if (query->commandType == origCmdType &&
4440  (query->querySource == QSRC_INSTEAD_RULE ||
4441  query->querySource == QSRC_QUAL_INSTEAD_RULE))
4442  lastInstead = query;
4443  }
4444  }
4445 
4446  if (!foundOriginalQuery && lastInstead != NULL)
4447  lastInstead->canSetTag = true;
4448 
4449  return results;
4450 }
CmdType
Definition: nodes.h:263
@ QSRC_ORIGINAL
Definition: parsenodes.h:36
static List * RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length)

References Assert, Query::commandType, fireRIRrules(), lappend(), lfirst, NIL, QSRC_INSTEAD_RULE, QSRC_ORIGINAL, QSRC_QUAL_INSTEAD_RULE, and RewriteQuery().

Referenced by ExecCreateTableAs(), ExplainOneUtility(), ExplainQuery(), PerformCursorOpen(), pg_rewrite_query(), and refresh_matview_datafill().

◆ relation_is_updatable()

int relation_is_updatable ( Oid  reloid,
List outer_reloids,
bool  include_triggers,
Bitmapset include_cols 
)

Definition at line 2812 of file rewriteHandler.c.

2816 {
2817  int events = 0;
2818  Relation rel;
2819  RuleLock *rulelocks;
2820 
2821 #define ALL_EVENTS ((1 << CMD_INSERT) | (1 << CMD_UPDATE) | (1 << CMD_DELETE))
2822 
2823  /* Since this function recurses, it could be driven to stack overflow */
2825 
2826  rel = try_relation_open(reloid, AccessShareLock);
2827 
2828  /*
2829  * If the relation doesn't exist, return zero rather than throwing an
2830  * error. This is helpful since scanning an information_schema view under
2831  * MVCC rules can result in referencing rels that have actually been
2832  * deleted already.
2833  */
2834  if (rel == NULL)
2835  return 0;
2836 
2837  /* If we detect a recursive view, report that it is not updatable */
2838  if (list_member_oid(outer_reloids, RelationGetRelid(rel)))
2839  {
2841  return 0;
2842  }
2843 
2844  /* If the relation is a table, it is always updatable */
2845  if (rel->rd_rel->relkind == RELKIND_RELATION ||
2846  rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2847  {
2849  return ALL_EVENTS;
2850  }
2851 
2852  /* Look for unconditional DO INSTEAD rules, and note supported events */
2853  rulelocks = rel->rd_rules;
2854  if (rulelocks != NULL)
2855  {
2856  int i;
2857 
2858  for (i = 0; i < rulelocks->numLocks; i++)
2859  {
2860  if (rulelocks->rules[i]->isInstead &&
2861  rulelocks->rules[i]->qual == NULL)
2862  {
2863  events |= ((1 << rulelocks->rules[i]->event) & ALL_EVENTS);
2864  }
2865  }
2866 
2867  /* If we have rules for all events, we're done */
2868  if (events == ALL_EVENTS)
2869  {
2871  return events;
2872  }
2873  }
2874 
2875  /* Similarly look for INSTEAD OF triggers, if they are to be included */
2876  if (include_triggers)
2877  {
2878  TriggerDesc *trigDesc = rel->trigdesc;
2879 
2880  if (trigDesc)
2881  {
2882  if (trigDesc->trig_insert_instead_row)
2883  events |= (1 << CMD_INSERT);
2884  if (trigDesc->trig_update_instead_row)
2885  events |= (1 << CMD_UPDATE);
2886  if (trigDesc->trig_delete_instead_row)
2887  events |= (1 << CMD_DELETE);
2888 
2889  /* If we have triggers for all events, we're done */
2890  if (events == ALL_EVENTS)
2891  {
2893  return events;
2894  }
2895  }
2896  }
2897 
2898  /* If this is a foreign table, check which update events it supports */
2899  if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2900  {
2901  FdwRoutine *fdwroutine = GetFdwRoutineForRelation(rel, false);
2902 
2903  if (fdwroutine->IsForeignRelUpdatable != NULL)
2904  events |= fdwroutine->IsForeignRelUpdatable(rel);
2905  else
2906  {
2907  /* Assume presence of executor functions is sufficient */
2908  if (fdwroutine->ExecForeignInsert != NULL)
2909  events |= (1 << CMD_INSERT);
2910  if (fdwroutine->ExecForeignUpdate != NULL)
2911  events |= (1 << CMD_UPDATE);
2912  if (fdwroutine->ExecForeignDelete != NULL)
2913  events |= (1 << CMD_DELETE);
2914  }
2915 
2917  return events;
2918  }
2919 
2920  /* Check if this is an automatically updatable view */
2921  if (rel->rd_rel->relkind == RELKIND_VIEW)
2922  {
2923  Query *viewquery = get_view_query(rel);
2924 
2925  if (view_query_is_auto_updatable(viewquery, false) == NULL)
2926  {
2927  Bitmapset *updatable_cols;
2928  int auto_events;
2929  RangeTblRef *rtr;
2930  RangeTblEntry *base_rte;
2931  Oid baseoid;
2932 
2933  /*
2934  * Determine which of the view's columns are updatable. If there
2935  * are none within the set of columns we are looking at, then the
2936  * view doesn't support INSERT/UPDATE, but it may still support
2937  * DELETE.
2938  */
2939  view_cols_are_auto_updatable(viewquery, NULL,
2940  &updatable_cols, NULL);
2941 
2942  if (include_cols != NULL)
2943  updatable_cols = bms_int_members(updatable_cols, include_cols);
2944 
2945  if (bms_is_empty(updatable_cols))
2946  auto_events = (1 << CMD_DELETE); /* May support DELETE */
2947  else
2948  auto_events = ALL_EVENTS; /* May support all events */
2949 
2950  /*
2951  * The base relation must also support these update commands.
2952  * Tables are always updatable, but for any other kind of base
2953  * relation we must do a recursive check limited to the columns
2954  * referenced by the locally updatable columns in this view.
2955  */
2956  rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
2957  base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
2958  Assert(base_rte->rtekind == RTE_RELATION);
2959 
2960  if (base_rte->relkind != RELKIND_RELATION &&
2961  base_rte->relkind != RELKIND_PARTITIONED_TABLE)
2962  {
2963  baseoid = base_rte->relid;
2964  outer_reloids = lappend_oid(outer_reloids,
2965  RelationGetRelid(rel));
2966  include_cols = adjust_view_column_set(updatable_cols,
2967  viewquery->targetList);
2968  auto_events &= relation_is_updatable(baseoid,
2969  outer_reloids,
2970  include_triggers,
2971  include_cols);
2972  outer_reloids = list_delete_last(outer_reloids);
2973  }
2974  events |= auto_events;
2975  }
2976  }
2977 
2978  /* If we reach here, the relation may support some update commands */
2980  return events;
2981 }
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1109
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:432
void check_stack_depth(void)
Definition: postgres.c:3530
const char * view_query_is_auto_updatable(Query *viewquery, bool check_cols)
int relation_is_updatable(Oid reloid, List *outer_reloids, bool include_triggers, Bitmapset *include_cols)
static Bitmapset * adjust_view_column_set(Bitmapset *cols, List *targetlist)
static const char * view_cols_are_auto_updatable(Query *viewquery, Bitmapset *required_cols, Bitmapset **updatable_cols, char **non_updatable_col)
Query * get_view_query(Relation view)
#define ALL_EVENTS
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:88
ExecForeignInsert_function ExecForeignInsert
Definition: fdwapi.h:232
ExecForeignUpdate_function ExecForeignUpdate
Definition: fdwapi.h:235
ExecForeignDelete_function ExecForeignDelete
Definition: fdwapi.h:236
IsForeignRelUpdatable_function IsForeignRelUpdatable
Definition: fdwapi.h:240

References AccessShareLock, adjust_view_column_set(), ALL_EVENTS, Assert, bms_int_members(), bms_is_empty, check_stack_depth(), CMD_DELETE, CMD_INSERT, CMD_UPDATE, RewriteRule::event, FdwRoutine::ExecForeignDelete, FdwRoutine::ExecForeignInsert, FdwRoutine::ExecForeignUpdate, FromExpr::fromlist, get_view_query(), GetFdwRoutineForRelation(), i, FdwRoutine::IsForeignRelUpdatable, RewriteRule::isInstead, Query::jointree, lappend_oid(), linitial, list_delete_last(), list_member_oid(), RuleLock::numLocks, RewriteRule::qual, RelationData::rd_rel, RelationData::rd_rules, relation_close(), RelationGetRelid, RangeTblEntry::relid, rt_fetch, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, RangeTblRef::rtindex, RuleLock::rules, Query::targetList, TriggerDesc::trig_delete_instead_row, TriggerDesc::trig_insert_instead_row, TriggerDesc::trig_update_instead_row, RelationData::trigdesc, try_relation_open(), view_cols_are_auto_updatable(), and view_query_is_auto_updatable().

Referenced by pg_column_is_updatable(), and pg_relation_is_updatable().

◆ RewriteQuery()

static List* RewriteQuery ( Query parsetree,
List rewrite_events,
int  orig_rt_length 
)
static

Definition at line 3815 of file rewriteHandler.c.

3816 {
3817  CmdType event = parsetree->commandType;
3818  bool instead = false;
3819  bool returning = false;
3820  bool updatableview = false;
3821  Query *qual_product = NULL;
3822  List *rewritten = NIL;
3823  ListCell *lc1;
3824 
3825  /*
3826  * First, recursively process any insert/update/delete/merge statements in
3827  * WITH clauses. (We have to do this first because the WITH clauses may
3828  * get copied into rule actions below.)
3829  */
3830  foreach(lc1, parsetree->cteList)
3831  {
3833  Query *ctequery = castNode(Query, cte->ctequery);
3834  List *newstuff;
3835 
3836  if (ctequery->commandType == CMD_SELECT)
3837  continue;
3838 
3839  newstuff = RewriteQuery(ctequery, rewrite_events, 0);
3840 
3841  /*
3842  * Currently we can only handle unconditional, single-statement DO
3843  * INSTEAD rules correctly; we have to get exactly one non-utility
3844  * Query out of the rewrite operation to stuff back into the CTE node.
3845  */
3846  if (list_length(newstuff) == 1)
3847  {
3848  /* Must check it's not a utility command */
3849  ctequery = linitial_node(Query, newstuff);
3850  if (!(ctequery->commandType == CMD_SELECT ||
3851  ctequery->commandType == CMD_UPDATE ||
3852  ctequery->commandType == CMD_INSERT ||
3853  ctequery->commandType == CMD_DELETE ||
3854  ctequery->commandType == CMD_MERGE))
3855  {
3856  /*
3857  * Currently it could only be NOTIFY; this error message will
3858  * need work if we ever allow other utility commands in rules.
3859  */
3860  ereport(ERROR,
3861  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3862  errmsg("DO INSTEAD NOTIFY rules are not supported for data-modifying statements in WITH")));
3863  }
3864  /* WITH queries should never be canSetTag */
3865  Assert(!ctequery->canSetTag);
3866  /* Push the single Query back into the CTE node */
3867  cte->ctequery = (Node *) ctequery;
3868  }
3869  else if (newstuff == NIL)
3870  {
3871  ereport(ERROR,
3872  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3873  errmsg("DO INSTEAD NOTHING rules are not supported for data-modifying statements in WITH")));
3874  }
3875  else
3876  {
3877  ListCell *lc2;
3878 
3879  /* examine queries to determine which error message to issue */
3880  foreach(lc2, newstuff)
3881  {
3882  Query *q = (Query *) lfirst(lc2);
3883 
3884  if (q->querySource == QSRC_QUAL_INSTEAD_RULE)
3885  ereport(ERROR,
3886  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3887  errmsg("conditional DO INSTEAD rules are not supported for data-modifying statements in WITH")));
3888  if (q->querySource == QSRC_NON_INSTEAD_RULE)
3889  ereport(ERROR,
3890  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3891  errmsg("DO ALSO rules are not supported for data-modifying statements in WITH")));
3892  }
3893 
3894  ereport(ERROR,
3895  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3896  errmsg("multi-statement DO INSTEAD rules are not supported for data-modifying statements in WITH")));
3897  }
3898  }
3899 
3900  /*
3901  * If the statement is an insert, update, delete, or merge, adjust its
3902  * targetlist as needed, and then fire INSERT/UPDATE/DELETE rules on it.
3903  *
3904  * SELECT rules are handled later when we have all the queries that should
3905  * get executed. Also, utilities aren't rewritten at all (do we still
3906  * need that check?)
3907  */
3908  if (event != CMD_SELECT && event != CMD_UTILITY)
3909  {
3910  int result_relation;
3911  RangeTblEntry *rt_entry;
3912  Relation rt_entry_relation;
3913  List *locks;
3914  int product_orig_rt_length;
3915  List *product_queries;
3916  bool hasUpdate = false;
3917  int values_rte_index = 0;
3918  bool defaults_remaining = false;
3919 
3920  result_relation = parsetree->resultRelation;
3921  Assert(result_relation != 0);
3922  rt_entry = rt_fetch(result_relation, parsetree->rtable);
3923  Assert(rt_entry->rtekind == RTE_RELATION);
3924 
3925  /*
3926  * We can use NoLock here since either the parser or
3927  * AcquireRewriteLocks should have locked the rel already.
3928  */
3929  rt_entry_relation = table_open(rt_entry->relid, NoLock);
3930 
3931  /*
3932  * Rewrite the targetlist as needed for the command type.
3933  */
3934  if (event == CMD_INSERT)
3935  {
3936  ListCell *lc2;
3937  RangeTblEntry *values_rte = NULL;
3938 
3939  /*
3940  * Test if it's a multi-row INSERT ... VALUES (...), (...), ... by
3941  * looking for a VALUES RTE in the fromlist. For product queries,
3942  * we must ignore any already-processed VALUES RTEs from the
3943  * original query. These appear at the start of the rangetable.
3944  */
3945  foreach(lc2, parsetree->jointree->fromlist)
3946  {
3947  RangeTblRef *rtr = (RangeTblRef *) lfirst(lc2);
3948 
3949  if (IsA(rtr, RangeTblRef) && rtr->rtindex > orig_rt_length)
3950  {
3951  RangeTblEntry *rte = rt_fetch(rtr->rtindex,
3952  parsetree->rtable);
3953 
3954  if (rte->rtekind == RTE_VALUES)
3955  {
3956  /* should not find more than one VALUES RTE */
3957  if (values_rte != NULL)
3958  elog(ERROR, "more than one VALUES RTE found");
3959 
3960  values_rte = rte;
3961  values_rte_index = rtr->rtindex;
3962  }
3963  }
3964  }
3965 
3966  if (values_rte)
3967  {
3968  Bitmapset *unused_values_attrnos = NULL;
3969 
3970  /* Process the main targetlist ... */
3971  parsetree->targetList = rewriteTargetListIU(parsetree->targetList,
3972  parsetree->commandType,
3973  parsetree->override,
3974  rt_entry_relation,
3975  values_rte,
3976  values_rte_index,
3977  &unused_values_attrnos);
3978  /* ... and the VALUES expression lists */
3979  if (!rewriteValuesRTE(parsetree, values_rte, values_rte_index,
3980  rt_entry_relation,
3981  unused_values_attrnos))
3982  defaults_remaining = true;
3983  }
3984  else
3985  {
3986  /* Process just the main targetlist */
3987  parsetree->targetList =
3988  rewriteTargetListIU(parsetree->targetList,
3989  parsetree->commandType,
3990  parsetree->override,
3991  rt_entry_relation,
3992  NULL, 0, NULL);
3993  }
3994 
3995  if (parsetree->onConflict &&
3996  parsetree->onConflict->action == ONCONFLICT_UPDATE)
3997  {
3998  parsetree->onConflict->onConflictSet =
4000  CMD_UPDATE,
4001  parsetree->override,
4002  rt_entry_relation,
4003  NULL, 0, NULL);
4004  }
4005  }
4006  else if (event == CMD_UPDATE)
4007  {
4008  Assert(parsetree->override == OVERRIDING_NOT_SET);
4009  parsetree->targetList =
4010  rewriteTargetListIU(parsetree->targetList,
4011  parsetree->commandType,
4012  parsetree->override,
4013  rt_entry_relation,
4014  NULL, 0, NULL);
4015  }
4016  else if (event == CMD_MERGE)
4017  {
4018  Assert(parsetree->override == OVERRIDING_NOT_SET);
4019 
4020  /*
4021  * Rewrite each action targetlist separately
4022  */
4023  foreach(lc1, parsetree->mergeActionList)
4024  {
4025  MergeAction *action = (MergeAction *) lfirst(lc1);
4026 
4027  switch (action->commandType)
4028  {
4029  case CMD_NOTHING:
4030  case CMD_DELETE: /* Nothing to do here */
4031  break;
4032  case CMD_UPDATE:
4033  case CMD_INSERT:
4034 
4035  /*
4036  * MERGE actions do not permit multi-row INSERTs, so
4037  * there is no VALUES RTE to deal with here.
4038  */
4039  action->targetList =
4040  rewriteTargetListIU(action->targetList,
4041  action->commandType,
4042  action->override,
4043  rt_entry_relation,
4044  NULL, 0, NULL);
4045  break;
4046  default:
4047  elog(ERROR, "unrecognized commandType: %d", action->commandType);
4048  break;
4049  }
4050  }
4051  }
4052  else if (event == CMD_DELETE)
4053  {
4054  /* Nothing to do here */
4055  }
4056  else
4057  elog(ERROR, "unrecognized commandType: %d", (int) event);
4058 
4059  /*
4060  * Collect and apply the appropriate rules.
4061  */
4062  locks = matchLocks(event, rt_entry_relation,
4063  result_relation, parsetree, &hasUpdate);
4064 
4065  product_orig_rt_length = list_length(parsetree->rtable);
4066  product_queries = fireRules(parsetree,
4067  result_relation,
4068  event,
4069  locks,
4070  &instead,
4071  &returning,
4072  &qual_product);
4073 
4074  /*
4075  * If we have a VALUES RTE with any remaining untouched DEFAULT items,
4076  * and we got any product queries, finalize the VALUES RTE for each
4077  * product query (replacing the remaining DEFAULT items with NULLs).
4078  * We don't do this for the original query, because we know that it
4079  * must be an auto-insert on a view, and so should use the base
4080  * relation's defaults for any remaining DEFAULT items.
4081  */
4082  if (defaults_remaining && product_queries != NIL)
4083  {
4084  ListCell *n;
4085 
4086  /*
4087  * Each product query has its own copy of the VALUES RTE at the
4088  * same index in the rangetable, so we must finalize each one.
4089  *
4090  * Note that if the product query is an INSERT ... SELECT, then
4091  * the VALUES RTE will be at the same index in the SELECT part of
4092  * the product query rather than the top-level product query
4093  * itself.
4094  */
4095  foreach(n, product_queries)
4096  {
4097  Query *pt = (Query *) lfirst(n);
4098  RangeTblEntry *values_rte;
4099 
4100  if (pt->commandType == CMD_INSERT &&
4101  pt->jointree && IsA(pt->jointree, FromExpr) &&
4102  list_length(pt->jointree->fromlist) == 1)
4103  {
4104  Node *jtnode = (Node *) linitial(pt->jointree->fromlist);
4105 
4106  if (IsA(jtnode, RangeTblRef))
4107  {
4108  int rtindex = ((RangeTblRef *) jtnode)->rtindex;
4109  RangeTblEntry *src_rte = rt_fetch(rtindex, pt->rtable);
4110 
4111  if (src_rte->rtekind == RTE_SUBQUERY &&
4112  src_rte->subquery &&
4113  IsA(src_rte->subquery, Query) &&
4114  src_rte->subquery->commandType == CMD_SELECT)
4115  pt = src_rte->subquery;
4116  }
4117  }
4118 
4119  values_rte = rt_fetch(values_rte_index, pt->rtable);
4120  if (values_rte->rtekind != RTE_VALUES)
4121  elog(ERROR, "failed to find VALUES RTE in product query");
4122 
4123  rewriteValuesRTEToNulls(pt, values_rte);
4124  }
4125  }
4126 
4127  /*
4128  * If there was no unqualified INSTEAD rule, and the target relation
4129  * is a view without any INSTEAD OF triggers, see if the view can be
4130  * automatically updated. If so, we perform the necessary query
4131  * transformation here and add the resulting query to the
4132  * product_queries list, so that it gets recursively rewritten if
4133  * necessary. For MERGE, the view must be automatically updatable if
4134  * any of the merge actions lack a corresponding INSTEAD OF trigger.
4135  *
4136  * If the view cannot be automatically updated, we throw an error here
4137  * which is OK since the query would fail at runtime anyway. Throwing
4138  * the error here is preferable to the executor check since we have
4139  * more detailed information available about why the view isn't
4140  * updatable.
4141  */
4142  if (!instead &&
4143  rt_entry_relation->rd_rel->relkind == RELKIND_VIEW &&
4144  !view_has_instead_trigger(rt_entry_relation, event,
4145  parsetree->mergeActionList))
4146  {
4147  /*
4148  * If there were any qualified INSTEAD rules, don't allow the view
4149  * to be automatically updated (an unqualified INSTEAD rule or
4150  * INSTEAD OF trigger is required).
4151  */
4152  if (qual_product != NULL)
4153  error_view_not_updatable(rt_entry_relation,
4154  parsetree->commandType,
4155  parsetree->mergeActionList,
4156  gettext_noop("Views with conditional DO INSTEAD rules are not automatically updatable."));
4157 
4158  /*
4159  * Attempt to rewrite the query to automatically update the view.
4160  * This throws an error if the view can't be automatically
4161  * updated.
4162  */
4163  parsetree = rewriteTargetView(parsetree, rt_entry_relation);
4164 
4165  /*
4166  * At this point product_queries contains any DO ALSO rule
4167  * actions. Add the rewritten query before or after those. This
4168  * must match the handling the original query would have gotten
4169  * below, if we allowed it to be included again.
4170  */
4171  if (parsetree->commandType == CMD_INSERT)
4172  product_queries = lcons(parsetree, product_queries);
4173  else
4174  product_queries = lappend(product_queries, parsetree);
4175 
4176  /*
4177  * Set the "instead" flag, as if there had been an unqualified
4178  * INSTEAD, to prevent the original query from being included a
4179  * second time below. The transformation will have rewritten any
4180  * RETURNING list, so we can also set "returning" to forestall
4181  * throwing an error below.
4182  */
4183  instead = true;
4184  returning = true;
4185  updatableview = true;
4186  }
4187 
4188  /*
4189  * If we got any product queries, recursively rewrite them --- but
4190  * first check for recursion!
4191  */
4192  if (product_queries != NIL)
4193  {
4194  ListCell *n;
4195  rewrite_event *rev;
4196 
4197  foreach(n, rewrite_events)
4198  {
4199  rev = (rewrite_event *) lfirst(n);
4200  if (rev->relation == RelationGetRelid(rt_entry_relation) &&
4201  rev->event == event)
4202  ereport(ERROR,
4203  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
4204  errmsg("infinite recursion detected in rules for relation \"%s\"",
4205  RelationGetRelationName(rt_entry_relation))));
4206  }
4207 
4208  rev = (rewrite_event *) palloc(sizeof(rewrite_event));
4209  rev->relation = RelationGetRelid(rt_entry_relation);
4210  rev->event = event;
4211  rewrite_events = lappend(rewrite_events, rev);
4212 
4213  foreach(n, product_queries)
4214  {
4215  Query *pt = (Query *) lfirst(n);
4216  List *newstuff;
4217 
4218  /*
4219  * For an updatable view, pt might be the rewritten version of
4220  * the original query, in which case we pass on orig_rt_length
4221  * to finish processing any VALUES RTE it contained.
4222  *
4223  * Otherwise, we have a product query created by fireRules().
4224  * Any VALUES RTEs from the original query have been fully
4225  * processed, and must be skipped when we recurse.
4226  */
4227  newstuff = RewriteQuery(pt, rewrite_events,
4228  pt == parsetree ?
4229  orig_rt_length :
4230  product_orig_rt_length);
4231  rewritten = list_concat(rewritten, newstuff);
4232  }
4233 
4234  rewrite_events = list_delete_last(rewrite_events);
4235  }
4236 
4237  /*
4238  * If there is an INSTEAD, and the original query has a RETURNING, we
4239  * have to have found a RETURNING in the rule(s), else fail. (Because
4240  * DefineQueryRewrite only allows RETURNING in unconditional INSTEAD
4241  * rules, there's no need to worry whether the substituted RETURNING
4242  * will actually be executed --- it must be.)
4243  */
4244  if ((instead || qual_product != NULL) &&
4245  parsetree->returningList &&
4246  !returning)
4247  {
4248  switch (event)
4249  {
4250  case CMD_INSERT:
4251  ereport(ERROR,
4252  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4253  errmsg("cannot perform INSERT RETURNING on relation \"%s\"",
4254  RelationGetRelationName(rt_entry_relation)),
4255  errhint("You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.")));
4256  break;
4257  case CMD_UPDATE:
4258  ereport(ERROR,
4259  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4260  errmsg("cannot perform UPDATE RETURNING on relation \"%s\"",
4261  RelationGetRelationName(rt_entry_relation)),
4262  errhint("You need an unconditional ON UPDATE DO INSTEAD rule with a RETURNING clause.")));
4263  break;
4264  case CMD_DELETE:
4265  ereport(ERROR,
4266  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4267  errmsg("cannot perform DELETE RETURNING on relation \"%s\"",
4268  RelationGetRelationName(rt_entry_relation)),
4269  errhint("You need an unconditional ON DELETE DO INSTEAD rule with a RETURNING clause.")));
4270  break;
4271  default:
4272  elog(ERROR, "unrecognized commandType: %d",
4273  (int) event);
4274  break;
4275  }
4276  }
4277 
4278  /*
4279  * Updatable views are supported by ON CONFLICT, so don't prevent that
4280  * case from proceeding
4281  */
4282  if (parsetree->onConflict &&
4283  (product_queries != NIL || hasUpdate) &&
4284  !updatableview)
4285  ereport(ERROR,
4286  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4287  errmsg("INSERT with ON CONFLICT clause cannot be used with table that has INSERT or UPDATE rules")));
4288 
4289  table_close(rt_entry_relation, NoLock);
4290  }
4291 
4292  /*
4293  * For INSERTs, the original query is done first; for UPDATE/DELETE, it is
4294  * done last. This is needed because update and delete rule actions might
4295  * not do anything if they are invoked after the update or delete is
4296  * performed. The command counter increment between the query executions
4297  * makes the deleted (and maybe the updated) tuples disappear so the scans
4298  * for them in the rule actions cannot find them.
4299  *
4300  * If we found any unqualified INSTEAD, the original query is not done at
4301  * all, in any form. Otherwise, we add the modified form if qualified
4302  * INSTEADs were found, else the unmodified form.
4303  */
4304  if (!instead)
4305  {
4306  if (parsetree->commandType == CMD_INSERT)
4307  {
4308  if (qual_product != NULL)
4309  rewritten = lcons(qual_product, rewritten);
4310  else
4311  rewritten = lcons(parsetree, rewritten);
4312  }
4313  else
4314  {
4315  if (qual_product != NULL)
4316  rewritten = lappend(rewritten, qual_product);
4317  else
4318  rewritten = lappend(rewritten, parsetree);
4319  }
4320  }
4321 
4322  /*
4323  * If the original query has a CTE list, and we generated more than one
4324  * non-utility result query, we have to fail because we'll have copied the
4325  * CTE list into each result query. That would break the expectation of
4326  * single evaluation of CTEs. This could possibly be fixed by
4327  * restructuring so that a CTE list can be shared across multiple Query
4328  * and PlannableStatement nodes.
4329  */
4330  if (parsetree->cteList != NIL)
4331  {
4332  int qcount = 0;
4333 
4334  foreach(lc1, rewritten)
4335  {
4336  Query *q = (Query *) lfirst(lc1);
4337 
4338  if (q->commandType != CMD_UTILITY)
4339  qcount++;
4340  }
4341  if (qcount > 1)
4342  ereport(ERROR,
4343  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4344  errmsg("WITH cannot be used in a query that is rewritten by rules into multiple queries")));
4345  }
4346 
4347  return rewritten;
4348 }
#define gettext_noop(x)
Definition: c.h:1196
List * lcons(void *datum, List *list)
Definition: list.c:495
void * palloc(Size size)
Definition: mcxt.c:1317
@ ONCONFLICT_UPDATE
Definition: nodes.h:420
@ CMD_UTILITY
Definition: nodes.h:270
@ RTE_VALUES
Definition: parsenodes.h:1033
#define linitial_node(type, l)
Definition: pg_list.h:181
@ OVERRIDING_NOT_SET
Definition: primnodes.h:27
static List * fireRules(Query *parsetree, int rt_index, CmdType event, List *locks, bool *instead_flag, bool *returning_flag, Query **qual_product)
bool view_has_instead_trigger(Relation view, CmdType event, List *mergeActionList)
static bool rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti, Relation target_relation, Bitmapset *unused_cols)
static void rewriteValuesRTEToNulls(Query *parsetree, RangeTblEntry *rte)
static List * matchLocks(CmdType event, Relation relation, int varno, Query *parsetree, bool *hasUpdate)
void error_view_not_updatable(Relation view, CmdType command, List *mergeActionList, const char *detail)
static List * rewriteTargetListIU(List *targetList, CmdType commandType, OverridingKind override, Relation target_relation, RangeTblEntry *values_rte, int values_rte_index, Bitmapset **unused_values_attrnos)
static Query * rewriteTargetView(Query *parsetree, Relation view)
OnConflictAction action
Definition: primnodes.h:2320
List * onConflictSet
Definition: primnodes.h:2329
List * mergeActionList
Definition: parsenodes.h:178

References generate_unaccent_rules::action, OnConflictExpr::action, Assert, castNode, CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_NOTHING, CMD_SELECT, CMD_UPDATE, CMD_UTILITY, Query::commandType, Query::cteList, CommonTableExpr::ctequery, elog, ereport, errcode(), errhint(), errmsg(), ERROR, error_view_not_updatable(), rewrite_event::event, fireRules(), FromExpr::fromlist, gettext_noop, IsA, Query::jointree, lappend(), lcons(), lfirst, lfirst_node, linitial, linitial_node, list_concat(), list_delete_last(), list_length(), matchLocks(), Query::mergeActionList, NIL, NoLock, Query::onConflict, ONCONFLICT_UPDATE, OnConflictExpr::onConflictSet, OVERRIDING_NOT_SET, palloc(), QSRC_NON_INSTEAD_RULE, QSRC_QUAL_INSTEAD_RULE, RelationData::rd_rel, rewrite_event::relation, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, Query::returningList, rewriteTargetListIU(), rewriteTargetView(), rewriteValuesRTE(), rewriteValuesRTEToNulls(), rt_fetch, Query::rtable, RTE_RELATION, RTE_SUBQUERY, RTE_VALUES, RangeTblEntry::rtekind, RangeTblRef::rtindex, RangeTblEntry::subquery, table_close(), table_open(), Query::targetList, and view_has_instead_trigger().

Referenced by QueryRewrite().

◆ rewriteRuleAction()

static Query * rewriteRuleAction ( Query parsetree,
Query rule_action,
Node rule_qual,
int  rt_index,
CmdType  event,
bool returning_flag 
)
static

Definition at line 342 of file rewriteHandler.c.

348 {
349  int current_varno,
350  new_varno;
351  int rt_length;
352  Query *sub_action;
353  Query **sub_action_ptr;
355  ListCell *lc;
356 
357  context.for_execute = true;
358 
359  /*
360  * Make modifiable copies of rule action and qual (what we're passed are
361  * the stored versions in the relcache; don't touch 'em!).
362  */
363  rule_action = copyObject(rule_action);
364  rule_qual = copyObject(rule_qual);
365 
366  /*
367  * Acquire necessary locks and fix any deleted JOIN RTE entries.
368  */
369  AcquireRewriteLocks(rule_action, true, false);
370  (void) acquireLocksOnSubLinks(rule_qual, &context);
371 
372  current_varno = rt_index;
373  rt_length = list_length(parsetree->rtable);
374  new_varno = PRS2_NEW_VARNO + rt_length;
375 
376  /*
377  * Adjust rule action and qual to offset its varnos, so that we can merge
378  * its rtable with the main parsetree's rtable.
379  *
380  * If the rule action is an INSERT...SELECT, the OLD/NEW rtable entries
381  * will be in the SELECT part, and we have to modify that rather than the
382  * top-level INSERT (kluge!).
383  */
384  sub_action = getInsertSelectQuery(rule_action, &sub_action_ptr);
385 
386  OffsetVarNodes((Node *) sub_action, rt_length, 0);
387  OffsetVarNodes(rule_qual, rt_length, 0);
388  /* but references to OLD should point at original rt_index */
389  ChangeVarNodes((Node *) sub_action,
390  PRS2_OLD_VARNO + rt_length, rt_index, 0);
391  ChangeVarNodes(rule_qual,
392  PRS2_OLD_VARNO + rt_length, rt_index, 0);
393 
394  /*
395  * Mark any subquery RTEs in the rule action as LATERAL if they contain
396  * Vars referring to the current query level (references to NEW/OLD).
397  * Those really are lateral references, but we've historically not
398  * required users to mark such subqueries with LATERAL explicitly. But
399  * the planner will complain if such Vars exist in a non-LATERAL subquery,
400  * so we have to fix things up here.
401  */
402  foreach(lc, sub_action->rtable)
403  {
404  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
405 
406  if (rte->rtekind == RTE_SUBQUERY && !rte->lateral &&
407  contain_vars_of_level((Node *) rte->subquery, 1))
408  rte->lateral = true;
409  }
410 
411  /*
412  * Generate expanded rtable consisting of main parsetree's rtable plus
413  * rule action's rtable; this becomes the complete rtable for the rule
414  * action. Some of the entries may be unused after we finish rewriting,
415  * but we leave them all in place to avoid having to adjust the query's
416  * varnos. RT entries that are not referenced in the completed jointree
417  * will be ignored by the planner, so they do not affect query semantics.
418  *
419  * Also merge RTEPermissionInfo lists to ensure that all permissions are
420  * checked correctly.
421  *
422  * If the rule is INSTEAD, then the original query won't be executed at
423  * all, and so its rteperminfos must be preserved so that the executor
424  * will do the correct permissions checks on the relations referenced in
425  * it. This allows us to check that the caller has, say, insert-permission
426  * on a view, when the view is not semantically referenced at all in the
427  * resulting query.
428  *
429  * When a rule is not INSTEAD, the permissions checks done using the
430  * copied entries will be redundant with those done during execution of
431  * the original query, but we don't bother to treat that case differently.
432  *
433  * NOTE: because planner will destructively alter rtable and rteperminfos,
434  * we must ensure that rule action's lists are separate and shares no
435  * substructure with the main query's lists. Hence do a deep copy here
436  * for both.
437  */
438  {
439  List *rtable_tail = sub_action->rtable;
440  List *perminfos_tail = sub_action->rteperminfos;
441 
442  /*
443  * RewriteQuery relies on the fact that RT entries from the original
444  * query appear at the start of the expanded rtable, so we put the
445  * action's original table at the end of the list.
446  */
447  sub_action->rtable = copyObject(parsetree->rtable);
448  sub_action->rteperminfos = copyObject(parsetree->rteperminfos);
449  CombineRangeTables(&sub_action->rtable, &sub_action->rteperminfos,
450  rtable_tail, perminfos_tail);
451  }
452 
453  /*
454  * There could have been some SubLinks in parsetree's rtable, in which
455  * case we'd better mark the sub_action correctly.
456  */
457  if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
458  {
459  foreach(lc, parsetree->rtable)
460  {
461  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
462 
463  switch (rte->rtekind)
464  {
465  case RTE_RELATION:
466  sub_action->hasSubLinks =
468  break;
469  case RTE_FUNCTION:
470  sub_action->hasSubLinks =
472  break;
473  case RTE_TABLEFUNC:
474  sub_action->hasSubLinks =
476  break;
477  case RTE_VALUES:
478  sub_action->hasSubLinks =
480  break;
481  default:
482  /* other RTE types don't contain bare expressions */
483  break;
484  }
485  sub_action->hasSubLinks |=
486  checkExprHasSubLink((Node *) rte->securityQuals);
487  if (sub_action->hasSubLinks)
488  break; /* no need to keep scanning rtable */
489  }
490  }
491 
492  /*
493  * Also, we might have absorbed some RTEs with RLS conditions into the
494  * sub_action. If so, mark it as hasRowSecurity, whether or not those
495  * RTEs will be referenced after we finish rewriting. (Note: currently
496  * this is a no-op because RLS conditions aren't added till later, but it
497  * seems like good future-proofing to do this anyway.)
498  */
499  sub_action->hasRowSecurity |= parsetree->hasRowSecurity;
500 
501  /*
502  * Each rule action's jointree should be the main parsetree's jointree
503  * plus that rule's jointree, but usually *without* the original rtindex
504  * that we're replacing (if present, which it won't be for INSERT). Note
505  * that if the rule action refers to OLD, its jointree will add a
506  * reference to rt_index. If the rule action doesn't refer to OLD, but
507  * either the rule_qual or the user query quals do, then we need to keep
508  * the original rtindex in the jointree to provide data for the quals. We
509  * don't want the original rtindex to be joined twice, however, so avoid
510  * keeping it if the rule action mentions it.
511  *
512  * As above, the action's jointree must not share substructure with the
513  * main parsetree's.
514  */
515  if (sub_action->commandType != CMD_UTILITY)
516  {
517  bool keeporig;
518  List *newjointree;
519 
520  Assert(sub_action->jointree != NULL);
521  keeporig = (!rangeTableEntry_used((Node *) sub_action->jointree,
522  rt_index, 0)) &&
523  (rangeTableEntry_used(rule_qual, rt_index, 0) ||
524  rangeTableEntry_used(parsetree->jointree->quals, rt_index, 0));
525  newjointree = adjustJoinTreeList(parsetree, !keeporig, rt_index);
526  if (newjointree != NIL)
527  {
528  /*
529  * If sub_action is a setop, manipulating its jointree will do no
530  * good at all, because the jointree is dummy. (Perhaps someday
531  * we could push the joining and quals down to the member
532  * statements of the setop?)
533  */
534  if (sub_action->setOperations != NULL)
535  ereport(ERROR,
536  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
537  errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
538 
539  sub_action->jointree->fromlist =
540  list_concat(newjointree, sub_action->jointree->fromlist);
541 
542  /*
543  * There could have been some SubLinks in newjointree, in which
544  * case we'd better mark the sub_action correctly.
545  */
546  if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
547  sub_action->hasSubLinks =
548  checkExprHasSubLink((Node *) newjointree);
549  }
550  }
551 
552  /*
553  * If the original query has any CTEs, copy them into the rule action. But
554  * we don't need them for a utility action.
555  */
556  if (parsetree->cteList != NIL && sub_action->commandType != CMD_UTILITY)
557  {
558  /*
559  * Annoying implementation restriction: because CTEs are identified by
560  * name within a cteList, we can't merge a CTE from the original query
561  * if it has the same name as any CTE in the rule action.
562  *
563  * This could possibly be fixed by using some sort of internally
564  * generated ID, instead of names, to link CTE RTEs to their CTEs.
565  * However, decompiling the results would be quite confusing; note the
566  * merge of hasRecursive flags below, which could change the apparent
567  * semantics of such redundantly-named CTEs.
568  */
569  foreach(lc, parsetree->cteList)
570  {
571  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
572  ListCell *lc2;
573 
574  foreach(lc2, sub_action->cteList)
575  {
576  CommonTableExpr *cte2 = (CommonTableExpr *) lfirst(lc2);
577 
578  if (strcmp(cte->ctename, cte2->ctename) == 0)
579  ereport(ERROR,
580  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
581  errmsg("WITH query name \"%s\" appears in both a rule action and the query being rewritten",
582  cte->ctename)));
583  }
584  }
585 
586  /* OK, it's safe to combine the CTE lists */
587  sub_action->cteList = list_concat(sub_action->cteList,
588  copyObject(parsetree->cteList));
589  /* ... and don't forget about the associated flags */
590  sub_action->hasRecursive |= parsetree->hasRecursive;
591  sub_action->hasModifyingCTE |= parsetree->hasModifyingCTE;
592 
593  /*
594  * If rule_action is different from sub_action (i.e., the rule action
595  * is an INSERT...SELECT), then we might have just added some
596  * data-modifying CTEs that are not at the top query level. This is
597  * disallowed by the parser and we mustn't generate such trees here
598  * either, so throw an error.
599  *
600  * Conceivably such cases could be supported by attaching the original
601  * query's CTEs to rule_action not sub_action. But to do that, we'd
602  * have to increment ctelevelsup in RTEs and SubLinks copied from the
603  * original query. For now, it doesn't seem worth the trouble.
604  */
605  if (sub_action->hasModifyingCTE && rule_action != sub_action)
606  ereport(ERROR,
607  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
608  errmsg("INSERT ... SELECT rule actions are not supported for queries having data-modifying statements in WITH")));
609  }
610 
611  /*
612  * Event Qualification forces copying of parsetree and splitting into two
613  * queries one w/rule_qual, one w/NOT rule_qual. Also add user query qual
614  * onto rule action
615  */
616  AddQual(sub_action, rule_qual);
617 
618  AddQual(sub_action, parsetree->jointree->quals);
619 
620  /*
621  * Rewrite new.attribute with right hand side of target-list entry for
622  * appropriate field name in insert/update.
623  *
624  * KLUGE ALERT: since ReplaceVarsFromTargetList returns a mutated copy, we
625  * can't just apply it to sub_action; we have to remember to update the
626  * sublink inside rule_action, too.
627  */
628  if ((event == CMD_INSERT || event == CMD_UPDATE) &&
629  sub_action->commandType != CMD_UTILITY)
630  {
631  sub_action = (Query *)
632  ReplaceVarsFromTargetList((Node *) sub_action,
633  new_varno,
634  0,
635  rt_fetch(new_varno, sub_action->rtable),
636  parsetree->targetList,
637  (event == CMD_UPDATE) ?
640  current_varno,
641  NULL);
642  if (sub_action_ptr)
643  *sub_action_ptr = sub_action;
644  else
645  rule_action = sub_action;
646  }
647 
648  /*
649  * If rule_action has a RETURNING clause, then either throw it away if the
650  * triggering query has no RETURNING clause, or rewrite it to emit what
651  * the triggering query's RETURNING clause asks for. Throw an error if
652  * more than one rule has a RETURNING clause.
653  */
654  if (!parsetree->returningList)
655  rule_action->returningList = NIL;
656  else if (rule_action->returningList)
657  {
658  if (*returning_flag)
659  ereport(ERROR,
660  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
661  errmsg("cannot have RETURNING lists in multiple rules")));
662  *returning_flag = true;
663  rule_action->returningList = (List *)
665  parsetree->resultRelation,
666  0,
667  rt_fetch(parsetree->resultRelation,
668  parsetree->rtable),
669  rule_action->returningList,
671  0,
672  &rule_action->hasSubLinks);
673 
674  /*
675  * There could have been some SubLinks in parsetree's returningList,
676  * in which case we'd better mark the rule_action correctly.
677  */
678  if (parsetree->hasSubLinks && !rule_action->hasSubLinks)
679  rule_action->hasSubLinks =
680  checkExprHasSubLink((Node *) rule_action->returningList);
681  }
682 
683  return rule_action;
684 }
@ RTE_FUNCTION
Definition: parsenodes.h:1031
@ RTE_TABLEFUNC
Definition: parsenodes.h:1032
static List * adjustJoinTreeList(Query *parsetree, bool removert, int rt_index)
void OffsetVarNodes(Node *node, int offset, int sublevels_up)
Definition: rewriteManip.c:480
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:295
Query * getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
Definition: rewriteManip.c:998
void CombineRangeTables(List **dst_rtable, List **dst_perminfos, List *src_rtable, List *src_perminfos)
Definition: rewriteManip.c:350
void AddQual(Query *parsetree, Node *qual)
@ REPLACEVARS_REPORT_ERROR
Definition: rewriteManip.h:38
Node * quals
Definition: primnodes.h:2305
Node * setOperations
Definition: parsenodes.h:219
TableFunc * tablefunc
Definition: parsenodes.h:1194
List * functions
Definition: parsenodes.h:1187
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:441

References acquireLocksOnSubLinks(), AcquireRewriteLocks(), AddQual(), adjustJoinTreeList(), Assert, ChangeVarNodes(), checkExprHasSubLink(), CMD_INSERT, CMD_UPDATE, CMD_UTILITY, CombineRangeTables(), Query::commandType, contain_vars_of_level(), context, copyObject, Query::cteList, CommonTableExpr::ctename, ereport, errcode(), errmsg(), ERROR, FromExpr::fromlist, RangeTblEntry::functions, getInsertSelectQuery(), Query::jointree, lfirst, list_concat(), list_length(), NIL, OffsetVarNodes(), PRS2_NEW_VARNO, PRS2_OLD_VARNO, FromExpr::quals, rangeTableEntry_used(), REPLACEVARS_CHANGE_VARNO, REPLACEVARS_REPORT_ERROR, REPLACEVARS_SUBSTITUTE_NULL, ReplaceVarsFromTargetList(), Query::returningList, rt_fetch, Query::rtable, RTE_FUNCTION, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, Query::setOperations, RangeTblEntry::subquery, RangeTblEntry::tablefunc, RangeTblEntry::tablesample, Query::targetList, and RangeTblEntry::values_lists.

Referenced by fireRules().

◆ rewriteTargetListIU()

static List * rewriteTargetListIU ( List targetList,
CmdType  commandType,
OverridingKind  override,
Relation  target_relation,
RangeTblEntry values_rte,
int  values_rte_index,
Bitmapset **  unused_values_attrnos 
)
static

Definition at line 757 of file rewriteHandler.c.

764 {
765  TargetEntry **new_tles;
766  List *new_tlist = NIL;
767  List *junk_tlist = NIL;
768  Form_pg_attribute att_tup;
769  int attrno,
770  next_junk_attrno,
771  numattrs;
772  ListCell *temp;
773  Bitmapset *default_only_cols = NULL;
774 
775  /*
776  * We process the normal (non-junk) attributes by scanning the input tlist
777  * once and transferring TLEs into an array, then scanning the array to
778  * build an output tlist. This avoids O(N^2) behavior for large numbers
779  * of attributes.
780  *
781  * Junk attributes are tossed into a separate list during the same tlist
782  * scan, then appended to the reconstructed tlist.
783  */
784  numattrs = RelationGetNumberOfAttributes(target_relation);
785  new_tles = (TargetEntry **) palloc0(numattrs * sizeof(TargetEntry *));
786  next_junk_attrno = numattrs + 1;
787 
788  foreach(temp, targetList)
789  {
790  TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
791 
792  if (!old_tle->resjunk)
793  {
794  /* Normal attr: stash it into new_tles[] */
795  attrno = old_tle->resno;
796  if (attrno < 1 || attrno > numattrs)
797  elog(ERROR, "bogus resno %d in targetlist", attrno);
798  att_tup = TupleDescAttr(target_relation->rd_att, attrno - 1);
799 
800  /* We can (and must) ignore deleted attributes */
801  if (att_tup->attisdropped)
802  continue;
803 
804  /* Merge with any prior assignment to same attribute */
805  new_tles[attrno - 1] =
806  process_matched_tle(old_tle,
807  new_tles[attrno - 1],
808  NameStr(att_tup->attname));
809  }
810  else
811  {
812  /*
813  * Copy all resjunk tlist entries to junk_tlist, and assign them
814  * resnos above the last real resno.
815  *
816  * Typical junk entries include ORDER BY or GROUP BY expressions
817  * (are these actually possible in an INSERT or UPDATE?), system
818  * attribute references, etc.
819  */
820 
821  /* Get the resno right, but don't copy unnecessarily */
822  if (old_tle->resno != next_junk_attrno)
823  {
824  old_tle = flatCopyTargetEntry(old_tle);
825  old_tle->resno = next_junk_attrno;
826  }
827  junk_tlist = lappend(junk_tlist, old_tle);
828  next_junk_attrno++;
829  }
830  }
831 
832  for (attrno = 1; attrno <= numattrs; attrno++)
833  {
834  TargetEntry *new_tle = new_tles[attrno - 1];
835  bool apply_default;
836 
837  att_tup = TupleDescAttr(target_relation->rd_att, attrno - 1);
838 
839  /* We can (and must) ignore deleted attributes */
840  if (att_tup->attisdropped)
841  continue;
842 
843  /*
844  * Handle the two cases where we need to insert a default expression:
845  * it's an INSERT and there's no tlist entry for the column, or the
846  * tlist entry is a DEFAULT placeholder node.
847  */
848  apply_default = ((new_tle == NULL && commandType == CMD_INSERT) ||
849  (new_tle && new_tle->expr && IsA(new_tle->expr, SetToDefault)));
850 
851  if (commandType == CMD_INSERT)
852  {
853  int values_attrno = 0;
854 
855  /* Source attribute number for values that come from a VALUES RTE */
856  if (values_rte && new_tle && IsA(new_tle->expr, Var))
857  {
858  Var *var = (Var *) new_tle->expr;
859 
860  if (var->varno == values_rte_index)
861  values_attrno = var->varattno;
862  }
863 
864  /*
865  * Can only insert DEFAULT into GENERATED ALWAYS identity columns,
866  * unless either OVERRIDING USER VALUE or OVERRIDING SYSTEM VALUE
867  * is specified.
868  */
869  if (att_tup->attidentity == ATTRIBUTE_IDENTITY_ALWAYS && !apply_default)
870  {
871  if (override == OVERRIDING_USER_VALUE)
872  apply_default = true;
873  else if (override != OVERRIDING_SYSTEM_VALUE)
874  {
875  /*
876  * If this column's values come from a VALUES RTE, test
877  * whether it contains only SetToDefault items. Since the
878  * VALUES list might be quite large, we arrange to only
879  * scan it once.
880  */
881  if (values_attrno != 0)
882  {
883  if (default_only_cols == NULL)
884  default_only_cols = findDefaultOnlyColumns(values_rte);
885 
886  if (bms_is_member(values_attrno, default_only_cols))
887  apply_default = true;
888  }
889 
890  if (!apply_default)
891  ereport(ERROR,
892  (errcode(ERRCODE_GENERATED_ALWAYS),
893  errmsg("cannot insert a non-DEFAULT value into column \"%s\"",
894  NameStr(att_tup->attname)),
895  errdetail("Column \"%s\" is an identity column defined as GENERATED ALWAYS.",
896  NameStr(att_tup->attname)),
897  errhint("Use OVERRIDING SYSTEM VALUE to override.")));
898  }
899  }
900 
901  /*
902  * Although inserting into a GENERATED BY DEFAULT identity column
903  * is allowed, apply the default if OVERRIDING USER VALUE is
904  * specified.
905  */
906  if (att_tup->attidentity == ATTRIBUTE_IDENTITY_BY_DEFAULT &&
907  override == OVERRIDING_USER_VALUE)
908  apply_default = true;
909 
910  /*
911  * Can only insert DEFAULT into generated columns, regardless of
912  * any OVERRIDING clauses.
913  */
914  if (att_tup->attgenerated && !apply_default)
915  {
916  /*
917  * If this column's values come from a VALUES RTE, test
918  * whether it contains only SetToDefault items, as above.
919  */
920  if (values_attrno != 0)
921  {
922  if (default_only_cols == NULL)
923  default_only_cols = findDefaultOnlyColumns(values_rte);
924 
925  if (bms_is_member(values_attrno, default_only_cols))
926  apply_default = true;
927  }
928 
929  if (!apply_default)
930  ereport(ERROR,
931  (errcode(ERRCODE_GENERATED_ALWAYS),
932  errmsg("cannot insert a non-DEFAULT value into column \"%s\"",
933  NameStr(att_tup->attname)),
934  errdetail("Column \"%s\" is a generated column.",
935  NameStr(att_tup->attname))));
936  }
937 
938  /*
939  * For an INSERT from a VALUES RTE, return the attribute numbers
940  * of any VALUES columns that will no longer be used (due to the
941  * targetlist entry being replaced by a default expression).
942  */
943  if (values_attrno != 0 && apply_default && unused_values_attrnos)
944  *unused_values_attrnos = bms_add_member(*unused_values_attrnos,
945  values_attrno);
946  }
947 
948  /*
949  * Updates to identity and generated columns follow the same rules as
950  * above, except that UPDATE doesn't admit OVERRIDING clauses. Also,
951  * the source can't be a VALUES RTE, so we needn't consider that.
952  */
953  if (commandType == CMD_UPDATE)
954  {
955  if (att_tup->attidentity == ATTRIBUTE_IDENTITY_ALWAYS &&
956  new_tle && !apply_default)
957  ereport(ERROR,
958  (errcode(ERRCODE_GENERATED_ALWAYS),
959  errmsg("column \"%s\" can only be updated to DEFAULT",
960  NameStr(att_tup->attname)),
961  errdetail("Column \"%s\" is an identity column defined as GENERATED ALWAYS.",
962  NameStr(att_tup->attname))));
963 
964  if (att_tup->attgenerated && new_tle && !apply_default)
965  ereport(ERROR,
966  (errcode(ERRCODE_GENERATED_ALWAYS),
967  errmsg("column \"%s\" can only be updated to DEFAULT",
968  NameStr(att_tup->attname)),
969  errdetail("Column \"%s\" is a generated column.",
970  NameStr(att_tup->attname))));
971  }
972 
973  if (att_tup->attgenerated)
974  {
975  /*
976  * stored generated column will be fixed in executor
977  */
978  new_tle = NULL;
979  }
980  else if (apply_default)
981  {
982  Node *new_expr;
983 
984  new_expr = build_column_default(target_relation, attrno);
985 
986  /*
987  * If there is no default (ie, default is effectively NULL), we
988  * can omit the tlist entry in the INSERT case, since the planner
989  * can insert a NULL for itself, and there's no point in spending
990  * any more rewriter cycles on the entry. But in the UPDATE case
991  * we've got to explicitly set the column to NULL.
992  */
993  if (!new_expr)
994  {
995  if (commandType == CMD_INSERT)
996  new_tle = NULL;
997  else
998  {
999  new_expr = (Node *) makeConst(att_tup->atttypid,
1000  -1,
1001  att_tup->attcollation,
1002  att_tup->attlen,
1003  (Datum) 0,
1004  true, /* isnull */
1005  att_tup->attbyval);
1006  /* this is to catch a NOT NULL domain constraint */
1007  new_expr = coerce_to_domain(new_expr,
1008  InvalidOid, -1,
1009  att_tup->atttypid,
1012  -1,
1013  false);
1014  }
1015  }
1016 
1017  if (new_expr)
1018  new_tle = makeTargetEntry((Expr *) new_expr,
1019  attrno,
1020  pstrdup(NameStr(att_tup->attname)),
1021  false);
1022  }
1023 
1024  if (new_tle)
1025  new_tlist = lappend(new_tlist, new_tle);
1026  }
1027 
1028  pfree(new_tles);
1029 
1030  return list_concat(new_tlist, junk_tlist);
1031 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:301
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc0(Size size)
Definition: mcxt.c:1347
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
Definition: parse_coerce.c:676
uintptr_t Datum
Definition: postgres.h:64
#define InvalidOid
Definition: postgres_ext.h:36
@ OVERRIDING_SYSTEM_VALUE
Definition: primnodes.h:29
@ OVERRIDING_USER_VALUE
Definition: primnodes.h:28
@ COERCION_IMPLICIT
Definition: primnodes.h:714
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:511
static TargetEntry * process_matched_tle(TargetEntry *src_tle, TargetEntry *prior_tle, const char *attrName)
static Bitmapset * findDefaultOnlyColumns(RangeTblEntry *rte)
Node * build_column_default(Relation rel, int attrno)
AttrNumber resno
Definition: primnodes.h:2188

References bms_add_member(), bms_is_member(), build_column_default(), CMD_INSERT, CMD_UPDATE, COERCE_IMPLICIT_CAST, coerce_to_domain(), COERCION_IMPLICIT, elog, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, TargetEntry::expr, findDefaultOnlyColumns(), flatCopyTargetEntry(), if(), InvalidOid, IsA, lappend(), lfirst, list_concat(), makeConst(), makeTargetEntry(), NameStr, NIL, OVERRIDING_SYSTEM_VALUE, OVERRIDING_USER_VALUE, palloc0(), pfree(), process_matched_tle(), pstrdup(), RelationData::rd_att, RelationGetNumberOfAttributes, TargetEntry::resno, TupleDescAttr, Var::varattno, and Var::varno.

Referenced by RewriteQuery().

◆ rewriteTargetView()

static Query* rewriteTargetView ( Query parsetree,
Relation  view 
)
static

Definition at line 3162 of file rewriteHandler.c.

3163 {
3164  Query *viewquery;
3165  bool insert_or_update;
3166  const char *auto_update_detail;
3167  RangeTblRef *rtr;
3168  int base_rt_index;
3169  int new_rt_index;
3170  RangeTblEntry *base_rte;
3171  RangeTblEntry *view_rte;
3172  RangeTblEntry *new_rte;
3173  RTEPermissionInfo *base_perminfo;
3174  RTEPermissionInfo *view_perminfo;
3175  RTEPermissionInfo *new_perminfo;
3176  Relation base_rel;
3177  List *view_targetlist;
3178  ListCell *lc;
3179 
3180  /*
3181  * Get the Query from the view's ON SELECT rule. We're going to munge the
3182  * Query to change the view's base relation into the target relation,
3183  * along with various other changes along the way, so we need to make a
3184  * copy of it (get_view_query() returns a pointer into the relcache, so we
3185  * have to treat it as read-only).
3186  */
3187  viewquery = copyObject(get_view_query(view));
3188 
3189  /* Locate RTE and perminfo describing the view in the outer query */
3190  view_rte = rt_fetch(parsetree->resultRelation, parsetree->rtable);
3191  view_perminfo = getRTEPermissionInfo(parsetree->rteperminfos, view_rte);
3192 
3193  /*
3194  * Are we doing INSERT/UPDATE, or MERGE containing INSERT/UPDATE? If so,
3195  * various additional checks on the view columns need to be applied, and
3196  * any view CHECK OPTIONs need to be enforced.
3197  */
3198  insert_or_update =
3199  (parsetree->commandType == CMD_INSERT ||
3200  parsetree->commandType == CMD_UPDATE);
3201 
3202  if (parsetree->commandType == CMD_MERGE)
3203  {
3205  {
3206  if (action->commandType == CMD_INSERT ||
3207  action->commandType == CMD_UPDATE)
3208  {
3209  insert_or_update = true;
3210  break;
3211  }
3212  }
3213  }
3214 
3215  /*
3216  * The view must be updatable, else fail.
3217  *
3218  * If we are doing INSERT/UPDATE (or MERGE containing INSERT/UPDATE), we
3219  * also check that there is at least one updatable column.
3220  */
3221  auto_update_detail =
3222  view_query_is_auto_updatable(viewquery, insert_or_update);
3223 
3224  if (auto_update_detail)
3226  parsetree->commandType,
3227  parsetree->mergeActionList,
3228  auto_update_detail);
3229 
3230  /*
3231  * For INSERT/UPDATE (or MERGE containing INSERT/UPDATE) the modified
3232  * columns must all be updatable.
3233  */
3234  if (insert_or_update)
3235  {
3236  Bitmapset *modified_cols;
3237  char *non_updatable_col;
3238 
3239  /*
3240  * Compute the set of modified columns as those listed in the result
3241  * RTE's insertedCols and/or updatedCols sets plus those that are
3242  * targets of the query's targetlist(s). We must consider the query's
3243  * targetlist because rewriteTargetListIU may have added additional
3244  * targetlist entries for view defaults, and these must also be
3245  * updatable. But rewriteTargetListIU can also remove entries if they
3246  * are DEFAULT markers and the column's default is NULL, so
3247  * considering only the targetlist would also be wrong.
3248  */
3249  modified_cols = bms_union(view_perminfo->insertedCols,
3250  view_perminfo->updatedCols);
3251 
3252  foreach(lc, parsetree->targetList)
3253  {
3254  TargetEntry *tle = (TargetEntry *) lfirst(lc);
3255 
3256  if (!tle->resjunk)
3257  modified_cols = bms_add_member(modified_cols,
3259  }
3260 
3261  if (parsetree->onConflict)
3262  {
3263  foreach(lc, parsetree->onConflict->onConflictSet)
3264  {
3265  TargetEntry *tle = (TargetEntry *) lfirst(lc);
3266 
3267  if (!tle->resjunk)
3268  modified_cols = bms_add_member(modified_cols,
3270  }
3271  }
3272 
3274  {
3275  if (action->commandType == CMD_INSERT ||
3276  action->commandType == CMD_UPDATE)
3277  {
3278  foreach_node(TargetEntry, tle, action->targetList)
3279  {
3280  if (!tle->resjunk)
3281  modified_cols = bms_add_member(modified_cols,
3283  }
3284  }
3285  }
3286 
3287  auto_update_detail = view_cols_are_auto_updatable(viewquery,
3288  modified_cols,
3289  NULL,
3290  &non_updatable_col);
3291  if (auto_update_detail)
3292  {
3293  /*
3294  * This is a different error, caused by an attempt to update a
3295  * non-updatable column in an otherwise updatable view.
3296  */
3297  switch (parsetree->commandType)
3298  {
3299  case CMD_INSERT:
3300  ereport(ERROR,
3301  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3302  errmsg("cannot insert into column \"%s\" of view \"%s\"",
3303  non_updatable_col,
3304  RelationGetRelationName(view)),
3305  errdetail_internal("%s", _(auto_update_detail))));
3306  break;
3307  case CMD_UPDATE:
3308  ereport(ERROR,
3309  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3310  errmsg("cannot update column \"%s\" of view \"%s\"",
3311  non_updatable_col,
3312  RelationGetRelationName(view)),
3313  errdetail_internal("%s", _(auto_update_detail))));
3314  break;
3315  case CMD_MERGE:
3316  ereport(ERROR,
3317  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3318  errmsg("cannot merge into column \"%s\" of view \"%s\"",
3319  non_updatable_col,
3320  RelationGetRelationName(view)),
3321  errdetail_internal("%s", _(auto_update_detail))));
3322  break;
3323  default:
3324  elog(ERROR, "unrecognized CmdType: %d",
3325  (int) parsetree->commandType);
3326  break;
3327  }
3328  }
3329  }
3330 
3331  /*
3332  * For MERGE, there must not be any INSTEAD OF triggers on an otherwise
3333  * updatable view. The caller already checked that there isn't a full set
3334  * of INSTEAD OF triggers, so this is to guard against having a partial
3335  * set (mixing auto-update and trigger-update actions in a single command
3336  * isn't supported).
3337  */
3338  if (parsetree->commandType == CMD_MERGE)
3339  {
3341  {
3342  if (action->commandType != CMD_NOTHING &&
3343  view_has_instead_trigger(view, action->commandType, NIL))
3344  ereport(ERROR,
3345  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3346  errmsg("cannot merge into view \"%s\"",
3347  RelationGetRelationName(view)),
3348  errdetail("MERGE is not supported for views with INSTEAD OF triggers for some actions, but not others."),
3349  errhint("To enable merging into the view, either provide a full set of INSTEAD OF triggers or drop the existing INSTEAD OF triggers."));
3350  }
3351  }
3352 
3353  /*
3354  * If we get here, view_query_is_auto_updatable() has verified that the
3355  * view contains a single base relation.
3356  */
3357  Assert(list_length(viewquery->jointree->fromlist) == 1);
3358  rtr = linitial_node(RangeTblRef, viewquery->jointree->fromlist);
3359 
3360  base_rt_index = rtr->rtindex;
3361  base_rte = rt_fetch(base_rt_index, viewquery->rtable);
3362  Assert(base_rte->rtekind == RTE_RELATION);
3363  base_perminfo = getRTEPermissionInfo(viewquery->rteperminfos, base_rte);
3364 
3365  /*
3366  * Up to now, the base relation hasn't been touched at all in our query.
3367  * We need to acquire lock on it before we try to do anything with it.
3368  * (The subsequent recursive call of RewriteQuery will suppose that we
3369  * already have the right lock!) Since it will become the query target
3370  * relation, RowExclusiveLock is always the right thing.
3371  */
3372  base_rel = table_open(base_rte->relid, RowExclusiveLock);
3373 
3374  /*
3375  * While we have the relation open, update the RTE's relkind, just in case
3376  * it changed since this view was made (cf. AcquireRewriteLocks).
3377  */
3378  base_rte->relkind = base_rel->rd_rel->relkind;
3379 
3380  /*
3381  * If the view query contains any sublink subqueries then we need to also
3382  * acquire locks on any relations they refer to. We know that there won't
3383  * be any subqueries in the range table or CTEs, so we can skip those, as
3384  * in AcquireRewriteLocks.
3385  */
3386  if (viewquery->hasSubLinks)
3387  {
3389 
3390  context.for_execute = true;
3393  }
3394 
3395  /*
3396  * Create a new target RTE describing the base relation, and add it to the
3397  * outer query's rangetable. (What's happening in the next few steps is
3398  * very much like what the planner would do to "pull up" the view into the
3399  * outer query. Perhaps someday we should refactor things enough so that
3400  * we can share code with the planner.)
3401  *
3402  * Be sure to set rellockmode to the correct thing for the target table.
3403  * Since we copied the whole viewquery above, we can just scribble on
3404  * base_rte instead of copying it.
3405  */
3406  new_rte = base_rte;
3407  new_rte->rellockmode = RowExclusiveLock;
3408 
3409  parsetree->rtable = lappend(parsetree->rtable, new_rte);
3410  new_rt_index = list_length(parsetree->rtable);
3411 
3412  /*
3413  * INSERTs never inherit. For UPDATE/DELETE/MERGE, we use the view
3414  * query's inheritance flag for the base relation.
3415  */
3416  if (parsetree->commandType == CMD_INSERT)
3417  new_rte->inh = false;
3418 
3419  /*
3420  * Adjust the view's targetlist Vars to reference the new target RTE, ie
3421  * make their varnos be new_rt_index instead of base_rt_index. There can
3422  * be no Vars for other rels in the tlist, so this is sufficient to pull
3423  * up the tlist expressions for use in the outer query. The tlist will
3424  * provide the replacement expressions used by ReplaceVarsFromTargetList
3425  * below.
3426  */
3427  view_targetlist = viewquery->targetList;
3428 
3429  ChangeVarNodes((Node *) view_targetlist,
3430  base_rt_index,
3431  new_rt_index,
3432  0);
3433 
3434  /*
3435  * If the view has "security_invoker" set, mark the new target relation
3436  * for the permissions checks that we want to enforce against the query
3437  * caller. Otherwise we want to enforce them against the view owner.
3438  *
3439  * At the relation level, require the same INSERT/UPDATE/DELETE
3440  * permissions that the query caller needs against the view. We drop the
3441  * ACL_SELECT bit that is presumably in new_perminfo->requiredPerms
3442  * initially.
3443  *
3444  * Note: the original view's RTEPermissionInfo remains in the query's
3445  * rteperminfos so that the executor still performs appropriate
3446  * permissions checks for the query caller's use of the view.
3447  *
3448  * Disregard the perminfo in viewquery->rteperminfos that the base_rte
3449  * would currently be pointing at, because we'd like it to point now to a
3450  * new one that will be filled below. Must set perminfoindex to 0 to not
3451  * trip over the Assert in addRTEPermissionInfo().
3452  */
3453  new_rte->perminfoindex = 0;
3454  new_perminfo = addRTEPermissionInfo(&parsetree->rteperminfos, new_rte);
3455  if (RelationHasSecurityInvoker(view))
3456  new_perminfo->checkAsUser = InvalidOid;
3457  else
3458  new_perminfo->checkAsUser = view->rd_rel->relowner;
3459  new_perminfo->requiredPerms = view_perminfo->requiredPerms;
3460 
3461  /*
3462  * Now for the per-column permissions bits.
3463  *
3464  * Initially, new_perminfo (base_perminfo) contains selectedCols
3465  * permission check bits for all base-rel columns referenced by the view,
3466  * but since the view is a SELECT query its insertedCols/updatedCols is
3467  * empty. We set insertedCols and updatedCols to include all the columns
3468  * the outer query is trying to modify, adjusting the column numbers as
3469  * needed. But we leave selectedCols as-is, so the view owner must have
3470  * read permission for all columns used in the view definition, even if
3471  * some of them are not read by the outer query. We could try to limit
3472  * selectedCols to only columns used in the transformed query, but that
3473  * does not correspond to what happens in ordinary SELECT usage of a view:
3474  * all referenced columns must have read permission, even if optimization
3475  * finds that some of them can be discarded during query transformation.
3476  * The flattening we're doing here is an optional optimization, too. (If
3477  * you are unpersuaded and want to change this, note that applying
3478  * adjust_view_column_set to view_perminfo->selectedCols is clearly *not*
3479  * the right answer, since that neglects base-rel columns used in the
3480  * view's WHERE quals.)
3481  *
3482  * This step needs the modified view targetlist, so we have to do things
3483  * in this order.
3484  */
3485  Assert(bms_is_empty(new_perminfo->insertedCols) &&
3486  bms_is_empty(new_perminfo->updatedCols));
3487 
3488  new_perminfo->selectedCols = base_perminfo->selectedCols;
3489 
3490  new_perminfo->insertedCols =
3491  adjust_view_column_set(view_perminfo->insertedCols, view_targetlist);
3492 
3493  new_perminfo->updatedCols =
3494  adjust_view_column_set(view_perminfo->updatedCols, view_targetlist);
3495 
3496  /*
3497  * Move any security barrier quals from the view RTE onto the new target
3498  * RTE. Any such quals should now apply to the new target RTE and will
3499  * not reference the original view RTE in the rewritten query.
3500  */
3501  new_rte->securityQuals = view_rte->securityQuals;
3502  view_rte->securityQuals = NIL;
3503 
3504  /*
3505  * Now update all Vars in the outer query that reference the view to
3506  * reference the appropriate column of the base relation instead.
3507  */
3508  parsetree = (Query *)
3509  ReplaceVarsFromTargetList((Node *) parsetree,
3510  parsetree->resultRelation,
3511  0,
3512  view_rte,
3513  view_targetlist,
3515  0,
3516  NULL);
3517 
3518  /*
3519  * Update all other RTI references in the query that point to the view
3520  * (for example, parsetree->resultRelation itself) to point to the new
3521  * base relation instead. Vars will not be affected since none of them
3522  * reference parsetree->resultRelation any longer.
3523  */
3524  ChangeVarNodes((Node *) parsetree,
3525  parsetree->resultRelation,
3526  new_rt_index,
3527  0);
3528  Assert(parsetree->resultRelation == new_rt_index);
3529 
3530  /*
3531  * For INSERT/UPDATE we must also update resnos in the targetlist to refer
3532  * to columns of the base relation, since those indicate the target
3533  * columns to be affected. Similarly, for MERGE we must update the resnos
3534  * in the merge action targetlists of any INSERT/UPDATE actions.
3535  *
3536  * Note that this destroys the resno ordering of the targetlists, but that
3537  * will be fixed when we recurse through RewriteQuery, which will invoke
3538  * rewriteTargetListIU again on the updated targetlists.
3539  */
3540  if (parsetree->commandType != CMD_DELETE)
3541  {
3542  foreach(lc, parsetree->targetList)
3543  {
3544  TargetEntry *tle = (TargetEntry *) lfirst(lc);
3545  TargetEntry *view_tle;
3546 
3547  if (tle->resjunk)
3548  continue;
3549 
3550  view_tle = get_tle_by_resno(view_targetlist, tle->resno);
3551  if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3552  tle->resno = ((Var *) view_tle->expr)->varattno;
3553  else
3554  elog(ERROR, "attribute number %d not found in view targetlist",
3555  tle->resno);
3556  }
3557 
3559  {
3560  if (action->commandType == CMD_INSERT ||
3561  action->commandType == CMD_UPDATE)
3562  {
3563  foreach_node(TargetEntry, tle, action->targetList)
3564  {
3565  TargetEntry *view_tle;
3566 
3567  if (tle->resjunk)
3568  continue;
3569 
3570  view_tle = get_tle_by_resno(view_targetlist, tle->resno);
3571  if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3572  tle->resno = ((Var *) view_tle->expr)->varattno;
3573  else
3574  elog(ERROR, "attribute number %d not found in view targetlist",
3575  tle->resno);
3576  }
3577  }
3578  }
3579  }
3580 
3581  /*
3582  * For INSERT .. ON CONFLICT .. DO UPDATE, we must also update assorted
3583  * stuff in the onConflict data structure.
3584  */
3585  if (parsetree->onConflict &&
3586  parsetree->onConflict->action == ONCONFLICT_UPDATE)
3587  {
3588  Index old_exclRelIndex,
3589  new_exclRelIndex;
3590  ParseNamespaceItem *new_exclNSItem;
3591  RangeTblEntry *new_exclRte;
3592  List *tmp_tlist;
3593 
3594  /*
3595  * Like the INSERT/UPDATE code above, update the resnos in the
3596  * auxiliary UPDATE targetlist to refer to columns of the base
3597  * relation.
3598  */
3599  foreach(lc, parsetree->onConflict->onConflictSet)
3600  {
3601  TargetEntry *tle = (TargetEntry *) lfirst(lc);
3602  TargetEntry *view_tle;
3603 
3604  if (tle->resjunk)
3605  continue;
3606 
3607  view_tle = get_tle_by_resno(view_targetlist, tle->resno);
3608  if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3609  tle->resno = ((Var *) view_tle->expr)->varattno;
3610  else
3611  elog(ERROR, "attribute number %d not found in view targetlist",
3612  tle->resno);
3613  }
3614 
3615  /*
3616  * Also, create a new RTE for the EXCLUDED pseudo-relation, using the
3617  * query's new base rel (which may well have a different column list
3618  * from the view, hence we need a new column alias list). This should
3619  * match transformOnConflictClause. In particular, note that the
3620  * relkind is set to composite to signal that we're not dealing with
3621  * an actual relation.
3622  */
3623  old_exclRelIndex = parsetree->onConflict->exclRelIndex;
3624 
3625  new_exclNSItem = addRangeTableEntryForRelation(make_parsestate(NULL),
3626  base_rel,
3628  makeAlias("excluded", NIL),
3629  false, false);
3630  new_exclRte = new_exclNSItem->p_rte;
3631  new_exclRte->relkind = RELKIND_COMPOSITE_TYPE;
3632  /* Ignore the RTEPermissionInfo that would've been added. */
3633  new_exclRte->perminfoindex = 0;
3634 
3635  parsetree->rtable = lappend(parsetree->rtable, new_exclRte);
3636  new_exclRelIndex = parsetree->onConflict->exclRelIndex =
3637  list_length(parsetree->rtable);
3638 
3639  /*
3640  * Replace the targetlist for the EXCLUDED pseudo-relation with a new
3641  * one, representing the columns from the new base relation.
3642  */
3643  parsetree->onConflict->exclRelTlist =
3644  BuildOnConflictExcludedTargetlist(base_rel, new_exclRelIndex);
3645 
3646  /*
3647  * Update all Vars in the ON CONFLICT clause that refer to the old
3648  * EXCLUDED pseudo-relation. We want to use the column mappings
3649  * defined in the view targetlist, but we need the outputs to refer to
3650  * the new EXCLUDED pseudo-relation rather than the new target RTE.
3651  * Also notice that "EXCLUDED.*" will be expanded using the view's
3652  * rowtype, which seems correct.
3653  */
3654  tmp_tlist = copyObject(view_targetlist);
3655 
3656  ChangeVarNodes((Node *) tmp_tlist, new_rt_index,
3657  new_exclRelIndex, 0);
3658 
3659  parsetree->onConflict = (OnConflictExpr *)
3660  ReplaceVarsFromTargetList((Node *) parsetree->onConflict,
3661  old_exclRelIndex,
3662  0,
3663  view_rte,
3664  tmp_tlist,
3666  0,
3667  &parsetree->hasSubLinks);
3668  }
3669 
3670  /*
3671  * For UPDATE/DELETE/MERGE, pull up any WHERE quals from the view. We
3672  * know that any Vars in the quals must reference the one base relation,
3673  * so we need only adjust their varnos to reference the new target (just
3674  * the same as we did with the view targetlist).
3675  *
3676  * If it's a security-barrier view, its WHERE quals must be applied before
3677  * quals from the outer query, so we attach them to the RTE as security
3678  * barrier quals rather than adding them to the main WHERE clause.
3679  *
3680  * For INSERT, the view's quals can be ignored in the main query.
3681  */
3682  if (parsetree->commandType != CMD_INSERT &&
3683  viewquery->jointree->quals != NULL)
3684  {
3685  Node *viewqual = (Node *) viewquery->jointree->quals;
3686 
3687  /*
3688  * Even though we copied viewquery already at the top of this
3689  * function, we must duplicate the viewqual again here, because we may
3690  * need to use the quals again below for a WithCheckOption clause.
3691  */
3692  viewqual = copyObject(viewqual);
3693 
3694  ChangeVarNodes(viewqual, base_rt_index, new_rt_index, 0);
3695 
3696  if (RelationIsSecurityView(view))
3697  {
3698  /*
3699  * The view's quals go in front of existing barrier quals: those
3700  * would have come from an outer level of security-barrier view,
3701  * and so must get evaluated later.
3702  *
3703  * Note: the parsetree has been mutated, so the new_rte pointer is
3704  * stale and needs to be re-computed.
3705  */
3706  new_rte = rt_fetch(new_rt_index, parsetree->rtable);
3707  new_rte->securityQuals = lcons(viewqual, new_rte->securityQuals);
3708 
3709  /*
3710  * Do not set parsetree->hasRowSecurity, because these aren't RLS
3711  * conditions (they aren't affected by enabling/disabling RLS).
3712  */
3713 
3714  /*
3715  * Make sure that the query is marked correctly if the added qual
3716  * has sublinks.
3717  */
3718  if (!parsetree->hasSubLinks)
3719  parsetree->hasSubLinks = checkExprHasSubLink(viewqual);
3720  }
3721  else
3722  AddQual(parsetree, (Node *) viewqual);
3723  }
3724 
3725  /*
3726  * For INSERT/UPDATE (or MERGE containing INSERT/UPDATE), if the view has
3727  * the WITH CHECK OPTION, or any parent view specified WITH CASCADED CHECK
3728  * OPTION, add the quals from the view to the query's withCheckOptions
3729  * list.
3730  */
3731  if (insert_or_update)
3732  {
3733  bool has_wco = RelationHasCheckOption(view);
3734  bool cascaded = RelationHasCascadedCheckOption(view);
3735 
3736  /*
3737  * If the parent view has a cascaded check option, treat this view as
3738  * if it also had a cascaded check option.
3739  *
3740  * New WithCheckOptions are added to the start of the list, so if
3741  * there is a cascaded check option, it will be the first item in the
3742  * list.
3743  */
3744  if (parsetree->withCheckOptions != NIL)
3745  {
3746  WithCheckOption *parent_wco =
3747  (WithCheckOption *) linitial(parsetree->withCheckOptions);
3748 
3749  if (parent_wco->cascaded)
3750  {
3751  has_wco = true;
3752  cascaded = true;
3753  }
3754  }
3755 
3756  /*
3757  * Add the new WithCheckOption to the start of the list, so that
3758  * checks on inner views are run before checks on outer views, as
3759  * required by the SQL standard.
3760  *
3761  * If the new check is CASCADED, we need to add it even if this view
3762  * has no quals, since there may be quals on child views. A LOCAL
3763  * check can be omitted if this view has no quals.
3764  */
3765  if (has_wco && (cascaded || viewquery->jointree->quals != NULL))
3766  {
3767  WithCheckOption *wco;
3768 
3769  wco = makeNode(WithCheckOption);
3770  wco->kind = WCO_VIEW_CHECK;
3771  wco->relname = pstrdup(RelationGetRelationName(view));
3772  wco->polname = NULL;
3773  wco->qual = NULL;
3774  wco->cascaded = cascaded;
3775 
3776  parsetree->withCheckOptions = lcons(wco,
3777  parsetree->withCheckOptions);
3778 
3779  if (viewquery->jointree->quals != NULL)
3780  {
3781  wco->qual = (Node *) viewquery->jointree->quals;
3782  ChangeVarNodes(wco->qual, base_rt_index, new_rt_index, 0);
3783 
3784  /*
3785  * For INSERT, make sure that the query is marked correctly if
3786  * the added qual has sublinks. This can be skipped for
3787  * UPDATE/MERGE, since the same qual will have already been
3788  * added above, and the check will already have been done.
3789  */
3790  if (!parsetree->hasSubLinks &&
3791  parsetree->commandType == CMD_INSERT)
3792  parsetree->hasSubLinks = checkExprHasSubLink(wco->qual);
3793  }
3794  }
3795  }
3796 
3797  table_close(base_rel, NoLock);
3798 
3799  return parsetree;
3800 }
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
#define RowExclusiveLock
Definition: lockdefs.h:38
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:389
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:39
RTEPermissionInfo * addRTEPermissionInfo(List **rteperminfos, RangeTblEntry *rte)
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
@ WCO_VIEW_CHECK
Definition: parsenodes.h:1363
List * BuildOnConflictExcludedTargetlist(Relation targetrel, Index exclRelIndex)
Definition: analyze.c:1225
#define RelationHasCheckOption(relation)
Definition: rel.h:446
#define RelationHasSecurityInvoker(relation)
Definition: rel.h:436
#define RelationHasCascadedCheckOption(relation)
Definition: rel.h:468
List * exclRelTlist
Definition: primnodes.h:2332
RangeTblEntry * p_rte
Definition: parse_node.h:287
Bitmapset * selectedCols
Definition: parsenodes.h:1297
Bitmapset * insertedCols
Definition: parsenodes.h:1298
Bitmapset * updatedCols
Definition: parsenodes.h:1299

References _, acquireLocksOnSubLinks(), generate_unaccent_rules::action, OnConflictExpr::action, AddQual(), addRangeTableEntryForRelation(), addRTEPermissionInfo(), adjust_view_column_set(), Assert, bms_add_member(), bms_is_empty, bms_union(), BuildOnConflictExcludedTargetlist(), WithCheckOption::cascaded, ChangeVarNodes(), RTEPermissionInfo::checkAsUser, checkExprHasSubLink(), CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_NOTHING, CMD_UPDATE, Query::commandType, context, copyObject, elog, ereport, errcode(), errdetail(), errdetail_internal(), errhint(), errmsg(), ERROR, error_view_not_updatable(), OnConflictExpr::exclRelIndex, OnConflictExpr::exclRelTlist, TargetEntry::expr, FirstLowInvalidHeapAttributeNumber, foreach_node, FromExpr::fromlist, get_tle_by_resno(), get_view_query(), getRTEPermissionInfo(), RangeTblEntry::inh, RTEPermissionInfo::insertedCols, InvalidOid, IsA, Query::jointree, WithCheckOption::kind, lappend(), lcons(), lfirst, linitial, linitial_node, list_length(), make_parsestate(), makeAlias(), makeNode, Query::mergeActionList, NIL, NoLock, Query::onConflict, ONCONFLICT_UPDATE, OnConflictExpr::onConflictSet, ParseNamespaceItem::p_rte, WithCheckOption::polname, pstrdup(), QTW_IGNORE_RC_SUBQUERIES, WithCheckOption::qual, FromExpr::quals, query_tree_walker, RelationData::rd_rel, RelationGetRelationName, RelationHasCascadedCheckOption, RelationHasCheckOption, RelationHasSecurityInvoker, RelationIsSecurityView, RangeTblEntry::relid, WithCheckOption::relname, REPLACEVARS_REPORT_ERROR, ReplaceVarsFromTargetList(), RTEPermissionInfo::requiredPerms, TargetEntry::resno, RowExclusiveLock, rt_fetch, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, RangeTblRef::rtindex, RTEPermissionInfo::selectedCols, table_close(), table_open(), Query::targetList, RTEPermissionInfo::updatedCols, view_cols_are_auto_updatable(), view_has_instead_trigger(), view_query_is_auto_updatable(), and WCO_VIEW_CHECK.

Referenced by RewriteQuery().

◆ rewriteValuesRTE()

static bool rewriteValuesRTE ( Query parsetree,
RangeTblEntry rte,
int  rti,
Relation  target_relation,
Bitmapset unused_cols 
)
static

Definition at line 1408 of file rewriteHandler.c.

1411 {
1412  List *newValues;
1413  ListCell *lc;
1414  bool isAutoUpdatableView;
1415  bool allReplaced;
1416  int numattrs;
1417  int *attrnos;
1418 
1419  /* Steps below are not sensible for non-INSERT queries */
1420  Assert(parsetree->commandType == CMD_INSERT);
1421  Assert(rte->rtekind == RTE_VALUES);
1422 
1423  /*
1424  * Rebuilding all the lists is a pretty expensive proposition in a big
1425  * VALUES list, and it's a waste of time if there aren't any DEFAULT
1426  * placeholders. So first scan to see if there are any.
1427  */
1428  if (!searchForDefault(rte))
1429  return true; /* nothing to do */
1430 
1431  /*
1432  * Scan the targetlist for entries referring to the VALUES RTE, and note
1433  * the target attributes. As noted above, we should only need to do this
1434  * for targetlist entries containing simple Vars --- nothing else in the
1435  * VALUES RTE should contain DEFAULT items (except possibly for unused
1436  * columns), and we complain if such a thing does occur.
1437  */
1438  numattrs = list_length(linitial(rte->values_lists));
1439  attrnos = (int *) palloc0(numattrs * sizeof(int));
1440 
1441  foreach(lc, parsetree->targetList)
1442  {
1443  TargetEntry *tle = (TargetEntry *) lfirst(lc);
1444 
1445  if (IsA(tle->expr, Var))
1446  {
1447  Var *var = (Var *) tle->expr;
1448 
1449  if (var->varno == rti)
1450  {
1451  int attrno = var->varattno;
1452 
1453  Assert(attrno >= 1 && attrno <= numattrs);
1454  attrnos[attrno - 1] = tle->resno;
1455  }
1456  }
1457  }
1458 
1459  /*
1460  * Check if the target relation is an auto-updatable view, in which case
1461  * unresolved defaults will be left untouched rather than being set to
1462  * NULL.
1463  */
1464  isAutoUpdatableView = false;
1465  if (target_relation->rd_rel->relkind == RELKIND_VIEW &&
1466  !view_has_instead_trigger(target_relation, CMD_INSERT, NIL))
1467  {
1468  List *locks;
1469  bool hasUpdate;
1470  bool found;
1471  ListCell *l;
1472 
1473  /* Look for an unconditional DO INSTEAD rule */
1474  locks = matchLocks(CMD_INSERT, target_relation,
1475  parsetree->resultRelation, parsetree, &hasUpdate);
1476 
1477  found = false;
1478  foreach(l, locks)
1479  {
1480  RewriteRule *rule_lock = (RewriteRule *) lfirst(l);
1481 
1482  if (rule_lock->isInstead &&
1483  rule_lock->qual == NULL)
1484  {
1485  found = true;
1486  break;
1487  }
1488  }
1489 
1490  /*
1491  * If we didn't find an unconditional DO INSTEAD rule, assume that the
1492  * view is auto-updatable. If it isn't, rewriteTargetView() will
1493  * throw an error.
1494  */
1495  if (!found)
1496  isAutoUpdatableView = true;
1497  }
1498 
1499  newValues = NIL;
1500  allReplaced = true;
1501  foreach(lc, rte->values_lists)
1502  {
1503  List *sublist = (List *) lfirst(lc);
1504  List *newList = NIL;
1505  ListCell *lc2;
1506  int i;
1507 
1508  Assert(list_length(sublist) == numattrs);
1509 
1510  i = 0;
1511  foreach(lc2, sublist)
1512  {
1513  Node *col = (Node *) lfirst(lc2);
1514  int attrno = attrnos[i++];
1515 
1516  if (IsA(col, SetToDefault))
1517  {
1518  Form_pg_attribute att_tup;
1519  Node *new_expr;
1520 
1521  /*
1522  * If this column isn't used, just replace the DEFAULT with
1523  * NULL (attrno will be 0 in this case because the targetlist
1524  * entry will have been replaced by the default expression).
1525  */
1526  if (bms_is_member(i, unused_cols))
1527  {
1528  SetToDefault *def = (SetToDefault *) col;
1529 
1530  newList = lappend(newList,
1531  makeNullConst(def->typeId,
1532  def->typeMod,
1533  def->collation));
1534  continue;
1535  }
1536 
1537  if (attrno == 0)
1538  elog(ERROR, "cannot set value in column %d to DEFAULT", i);
1539  Assert(attrno > 0 && attrno <= target_relation->rd_att->natts);
1540  att_tup = TupleDescAttr(target_relation->rd_att, attrno - 1);
1541 
1542  if (!att_tup->attisdropped)
1543  new_expr = build_column_default(target_relation, attrno);
1544  else
1545  new_expr = NULL; /* force a NULL if dropped */
1546 
1547  /*
1548  * If there is no default (ie, default is effectively NULL),
1549  * we've got to explicitly set the column to NULL, unless the
1550  * target relation is an auto-updatable view.
1551  */
1552  if (!new_expr)
1553  {
1554  if (isAutoUpdatableView)
1555  {
1556  /* Leave the value untouched */
1557  newList = lappend(newList, col);
1558  allReplaced = false;
1559  continue;
1560  }
1561 
1562  new_expr = (Node *) makeConst(att_tup->atttypid,
1563  -1,
1564  att_tup->attcollation,
1565  att_tup->attlen,
1566  (Datum) 0,
1567  true, /* isnull */
1568  att_tup->attbyval);
1569  /* this is to catch a NOT NULL domain constraint */
1570  new_expr = coerce_to_domain(new_expr,
1571  InvalidOid, -1,
1572  att_tup->atttypid,
1575  -1,
1576  false);
1577  }
1578  newList = lappend(newList, new_expr);
1579  }
1580  else
1581  newList = lappend(newList, col);
1582  }
1583  newValues = lappend(newValues, newList);
1584  }
1585  rte->values_lists = newValues;
1586 
1587  pfree(attrnos);
1588 
1589  return allReplaced;
1590 }
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:339
static bool searchForDefault(RangeTblEntry *rte)

References Assert, bms_is_member(), build_column_default(), CMD_INSERT, COERCE_IMPLICIT_CAST, coerce_to_domain(), COERCION_IMPLICIT, Query::commandType, elog, ERROR, TargetEntry::expr, i, if(), InvalidOid, IsA, RewriteRule::isInstead, lappend(), lfirst, linitial, list_length(), makeConst(), makeNullConst(), matchLocks(), NIL, palloc0(), pfree(), RewriteRule::qual, RelationData::rd_att, RelationData::rd_rel, TargetEntry::resno, RTE_VALUES, RangeTblEntry::rtekind, searchForDefault(), Query::targetList, TupleDescAttr, SetToDefault::typeId, RangeTblEntry::values_lists, Var::varattno, Var::varno, and view_has_instead_trigger().

Referenced by RewriteQuery().

◆ rewriteValuesRTEToNulls()

static void rewriteValuesRTEToNulls ( Query parsetree,
RangeTblEntry rte 
)
static

Definition at line 1603 of file rewriteHandler.c.

1604 {
1605  List *newValues;
1606  ListCell *lc;
1607 
1608  newValues = NIL;
1609  foreach(lc, rte->values_lists)
1610  {
1611  List *sublist = (List *) lfirst(lc);
1612  List *newList = NIL;
1613  ListCell *lc2;
1614 
1615  foreach(lc2, sublist)
1616  {
1617  Node *col = (Node *) lfirst(lc2);
1618 
1619  if (IsA(col, SetToDefault))
1620  {
1621  SetToDefault *def = (SetToDefault *) col;
1622 
1623  newList = lappend(newList, makeNullConst(def->typeId,
1624  def->typeMod,
1625  def->collation));
1626  }
1627  else
1628  newList = lappend(newList, col);
1629  }
1630  newValues = lappend(newValues, newList);
1631  }
1632  rte->values_lists = newValues;
1633 }

References IsA, lappend(), lfirst, makeNullConst(), NIL, SetToDefault::typeId, and RangeTblEntry::values_lists.

Referenced by RewriteQuery().

◆ searchForDefault()

static bool searchForDefault ( RangeTblEntry rte)
static

Definition at line 1294 of file rewriteHandler.c.

1295 {
1296  ListCell *lc;
1297 
1298  foreach(lc, rte->values_lists)
1299  {
1300  List *sublist = (List *) lfirst(lc);
1301  ListCell *lc2;
1302 
1303  foreach(lc2, sublist)
1304  {
1305  Node *col = (Node *) lfirst(lc2);
1306 
1307  if (IsA(col, SetToDefault))
1308  return true;
1309  }
1310  }
1311  return false;
1312 }

References IsA, lfirst, and RangeTblEntry::values_lists.

Referenced by rewriteValuesRTE().

◆ view_col_is_auto_updatable()

static const char* view_col_is_auto_updatable ( RangeTblRef rtr,
TargetEntry tle 
)
static

Definition at line 2533 of file rewriteHandler.c.

2534 {
2535  Var *var = (Var *) tle->expr;
2536 
2537  /*
2538  * For now, the only updatable columns we support are those that are Vars
2539  * referring to user columns of the underlying base relation.
2540  *
2541  * The view targetlist may contain resjunk columns (e.g., a view defined
2542  * like "SELECT * FROM t ORDER BY a+b" is auto-updatable) but such columns
2543  * are not auto-updatable, and in fact should never appear in the outer
2544  * query's targetlist.
2545  */
2546  if (tle->resjunk)
2547  return gettext_noop("Junk view columns are not updatable.");
2548 
2549  if (!IsA(var, Var) ||
2550  var->varno != rtr->rtindex ||
2551  var->varlevelsup != 0)
2552  return gettext_noop("View columns that are not columns of their base relation are not updatable.");
2553 
2554  if (var->varattno < 0)
2555  return gettext_noop("View columns that refer to system columns are not updatable.");
2556 
2557  if (var->varattno == 0)
2558  return gettext_noop("View columns that return whole-row references are not updatable.");
2559 
2560  return NULL; /* the view column is updatable */
2561 }

References TargetEntry::expr, gettext_noop, if(), IsA, RangeTblRef::rtindex, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by view_cols_are_auto_updatable(), and view_query_is_auto_updatable().

◆ view_cols_are_auto_updatable()

static const char* view_cols_are_auto_updatable ( Query viewquery,
Bitmapset required_cols,
Bitmapset **  updatable_cols,
char **  non_updatable_col 
)
static

Definition at line 2729 of file rewriteHandler.c.

2733 {
2734  RangeTblRef *rtr;
2735  AttrNumber col;
2736  ListCell *cell;
2737 
2738  /*
2739  * The caller should have verified that this view is auto-updatable and so
2740  * there should be a single base relation.
2741  */
2742  Assert(list_length(viewquery->jointree->fromlist) == 1);
2743  rtr = linitial_node(RangeTblRef, viewquery->jointree->fromlist);
2744 
2745  /* Initialize the optional return values */
2746  if (updatable_cols != NULL)
2747  *updatable_cols = NULL;
2748  if (non_updatable_col != NULL)
2749  *non_updatable_col = NULL;
2750 
2751  /* Test each view column for updatability */
2753  foreach(cell, viewquery->targetList)
2754  {
2755  TargetEntry *tle = (TargetEntry *) lfirst(cell);
2756  const char *col_update_detail;
2757 
2758  col++;
2759  col_update_detail = view_col_is_auto_updatable(rtr, tle);
2760 
2761  if (col_update_detail == NULL)
2762  {
2763  /* The column is updatable */
2764  if (updatable_cols != NULL)
2765  *updatable_cols = bms_add_member(*updatable_cols, col);
2766  }
2767  else if (bms_is_member(col, required_cols))
2768  {
2769  /* The required column is not updatable */
2770  if (non_updatable_col != NULL)
2771  *non_updatable_col = tle->resname;
2772  return col_update_detail;
2773  }
2774  }
2775 
2776  return NULL; /* all the required view columns are updatable */
2777 }
static const char * view_col_is_auto_updatable(RangeTblRef *rtr, TargetEntry *tle)

References Assert, bms_add_member(), bms_is_member(), FirstLowInvalidHeapAttributeNumber, FromExpr::fromlist, Query::jointree, lfirst, linitial_node, list_length(), Query::targetList, and view_col_is_auto_updatable().

Referenced by relation_is_updatable(), and rewriteTargetView().

◆ view_has_instead_trigger()

bool view_has_instead_trigger ( Relation  view,
CmdType  event,
List mergeActionList 
)

Definition at line 2469 of file rewriteHandler.c.

2470 {
2471  TriggerDesc *trigDesc = view->trigdesc;
2472 
2473  switch (event)
2474  {
2475  case CMD_INSERT:
2476  if (trigDesc && trigDesc->trig_insert_instead_row)
2477  return true;
2478  break;
2479  case CMD_UPDATE:
2480  if (trigDesc && trigDesc->trig_update_instead_row)
2481  return true;
2482  break;
2483  case CMD_DELETE:
2484  if (trigDesc && trigDesc->trig_delete_instead_row)
2485  return true;
2486  break;
2487  case CMD_MERGE:
2488  foreach_node(MergeAction, action, mergeActionList)
2489  {
2490  switch (action->commandType)
2491  {
2492  case CMD_INSERT:
2493  if (!trigDesc || !trigDesc->trig_insert_instead_row)
2494  return false;
2495  break;
2496  case CMD_UPDATE:
2497  if (!trigDesc || !trigDesc->trig_update_instead_row)
2498  return false;
2499  break;
2500  case CMD_DELETE:
2501  if (!trigDesc || !trigDesc->trig_delete_instead_row)
2502  return false;
2503  break;
2504  case CMD_NOTHING:
2505  /* No trigger required */
2506  break;
2507  default:
2508  elog(ERROR, "unrecognized commandType: %d", action->commandType);
2509  break;
2510  }
2511  }
2512  return true; /* no actions without an INSTEAD OF trigger */
2513  default:
2514  elog(ERROR, "unrecognized CmdType: %d", (int) event);
2515  break;
2516  }
2517  return false;
2518 }

References generate_unaccent_rules::action, CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_NOTHING, CMD_UPDATE, elog, ERROR, foreach_node, TriggerDesc::trig_delete_instead_row, TriggerDesc::trig_insert_instead_row, TriggerDesc::trig_update_instead_row, and RelationData::trigdesc.

Referenced by CheckValidResultRel(), RewriteQuery(), rewriteTargetView(), and rewriteValuesRTE().

◆ view_query_is_auto_updatable()

const char* view_query_is_auto_updatable ( Query viewquery,
bool  check_cols 
)

Definition at line 2581 of file rewriteHandler.c.

2582 {
2583  RangeTblRef *rtr;
2584  RangeTblEntry *base_rte;
2585 
2586  /*----------
2587  * Check if the view is simply updatable. According to SQL-92 this means:
2588  * - No DISTINCT clause.
2589  * - Each TLE is a column reference, and each column appears at most once.
2590  * - FROM contains exactly one base relation.
2591  * - No GROUP BY or HAVING clauses.
2592  * - No set operations (UNION, INTERSECT or EXCEPT).
2593  * - No sub-queries in the WHERE clause that reference the target table.
2594  *
2595  * We ignore that last restriction since it would be complex to enforce
2596  * and there isn't any actual benefit to disallowing sub-queries. (The
2597  * semantic issues that the standard is presumably concerned about don't
2598  * arise in Postgres, since any such sub-query will not see any updates
2599  * executed by the outer query anyway, thanks to MVCC snapshotting.)
2600  *
2601  * We also relax the second restriction by supporting part of SQL:1999
2602  * feature T111, which allows for a mix of updatable and non-updatable
2603  * columns, provided that an INSERT or UPDATE doesn't attempt to assign to
2604  * a non-updatable column.
2605  *
2606  * In addition we impose these constraints, involving features that are
2607  * not part of SQL-92:
2608  * - No CTEs (WITH clauses).
2609  * - No OFFSET or LIMIT clauses (this matches a SQL:2008 restriction).
2610  * - No system columns (including whole-row references) in the tlist.
2611  * - No window functions in the tlist.
2612  * - No set-returning functions in the tlist.
2613  *
2614  * Note that we do these checks without recursively expanding the view.
2615  * If the base relation is a view, we'll recursively deal with it later.
2616  *----------
2617  */
2618  if (viewquery->distinctClause != NIL)
2619  return gettext_noop("Views containing DISTINCT are not automatically updatable.");
2620 
2621  if (viewquery->groupClause != NIL || viewquery->groupingSets)
2622  return gettext_noop("Views containing GROUP BY are not automatically updatable.");
2623 
2624  if (viewquery->havingQual != NULL)
2625  return gettext_noop("Views containing HAVING are not automatically updatable.");
2626 
2627  if (viewquery->setOperations != NULL)
2628  return gettext_noop("Views containing UNION, INTERSECT, or EXCEPT are not automatically updatable.");
2629 
2630  if (viewquery->cteList != NIL)
2631  return gettext_noop("Views containing WITH are not automatically updatable.");
2632 
2633  if (viewquery->limitOffset != NULL || viewquery->limitCount != NULL)
2634  return gettext_noop("Views containing LIMIT or OFFSET are not automatically updatable.");
2635 
2636  /*
2637  * We must not allow window functions or set returning functions in the
2638  * targetlist. Otherwise we might end up inserting them into the quals of
2639  * the main query. We must also check for aggregates in the targetlist in
2640  * case they appear without a GROUP BY.
2641  *
2642  * These restrictions ensure that each row of the view corresponds to a
2643  * unique row in the underlying base relation.
2644  */
2645  if (viewquery->hasAggs)
2646  return gettext_noop("Views that return aggregate functions are not automatically updatable.");
2647 
2648  if (viewquery->hasWindowFuncs)
2649  return gettext_noop("Views that return window functions are not automatically updatable.");
2650 
2651  if (viewquery->hasTargetSRFs)
2652  return gettext_noop("Views that return set-returning functions are not automatically updatable.");
2653 
2654  /*
2655  * The view query should select from a single base relation, which must be
2656  * a table or another view.
2657  */
2658  if (list_length(viewquery->jointree->fromlist) != 1)
2659  return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2660 
2661  rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
2662  if (!IsA(rtr, RangeTblRef))
2663  return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2664 
2665  base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
2666  if (base_rte->rtekind != RTE_RELATION ||
2667  (base_rte->relkind != RELKIND_RELATION &&
2668  base_rte->relkind != RELKIND_FOREIGN_TABLE &&
2669  base_rte->relkind != RELKIND_VIEW &&
2670  base_rte->relkind != RELKIND_PARTITIONED_TABLE))
2671  return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2672 
2673  if (base_rte->tablesample)
2674  return gettext_noop("Views containing TABLESAMPLE are not automatically updatable.");
2675 
2676  /*
2677  * Check that the view has at least one updatable column. This is required
2678  * for INSERT/UPDATE but not for DELETE.
2679  */
2680  if (check_cols)
2681  {
2682  ListCell *cell;
2683  bool found;
2684 
2685  found = false;
2686  foreach(cell, viewquery->targetList)
2687  {
2688  TargetEntry *tle = (TargetEntry *) lfirst(cell);
2689 
2690  if (view_col_is_auto_updatable(rtr, tle) == NULL)
2691  {
2692  found = true;
2693  break;
2694  }
2695  }
2696 
2697  if (!found)
2698  return gettext_noop("Views that have no updatable columns are not automatically updatable.");
2699  }
2700 
2701  return NULL; /* the view is updatable */
2702 }
Node * limitCount
Definition: parsenodes.h:214
List * groupClause
Definition: parsenodes.h:200
Node * havingQual
Definition: parsenodes.h:205
Node * limitOffset
Definition: parsenodes.h:213
List * groupingSets
Definition: parsenodes.h:203
List * distinctClause
Definition: parsenodes.h:209

References Query::cteList, Query::distinctClause, FromExpr::fromlist, gettext_noop, Query::groupClause, Query::groupingSets, Query::havingQual, IsA, Query::jointree, lfirst, Query::limitCount, Query::limitOffset, linitial, list_length(), NIL, rt_fetch, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, RangeTblRef::rtindex, Query::setOperations, RangeTblEntry::tablesample, Query::targetList, and view_col_is_auto_updatable().

Referenced by ATExecSetRelOptions(), DefineView(), relation_is_updatable(), and rewriteTargetView().