PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
rewriteHandler.c File Reference
#include "postgres.h"
#include "access/sysattr.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "foreign/fdwapi.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/analyze.h"
#include "parser/parse_coerce.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteDefine.h"
#include "rewrite/rewriteHandler.h"
#include "rewrite/rewriteManip.h"
#include "rewrite/rowsecurity.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
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, Relation target_relation, int result_rti, List **attrno_list)
 
static TargetEntryprocess_matched_tle (TargetEntry *src_tle, TargetEntry *prior_tle, const char *attrName)
 
static Nodeget_assignment_input (Node *node)
 
static void rewriteValuesRTE (RangeTblEntry *rte, Relation target_relation, List *attrnos)
 
static void rewriteTargetListUD (Query *parsetree, RangeTblEntry *target_rte, Relation target_relation)
 
static void markQueryForLocking (Query *qry, Node *jtnode, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
 
static ListmatchLocks (CmdType event, RuleLock *rulelocks, int varno, Query *parsetree, bool *hasUpdate)
 
static QueryfireRIRrules (Query *parsetree, List *activeRIRs, bool forUpdatePushedDown)
 
static bool view_has_instead_trigger (Relation view, CmdType event)
 
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, bool forUpdatePushedDown)
 
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)
 
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, bool include_triggers, Bitmapset *include_cols)
 
static QueryrewriteTargetView (Query *parsetree, Relation view)
 
static ListRewriteQuery (Query *parsetree, List *rewrite_events)
 
ListQueryRewrite (Query *parsetree)
 

Macro Definition Documentation

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

Referenced by relation_is_updatable().

Typedef Documentation

Function Documentation

static bool acquireLocksOnSubLinks ( Node node,
acquireLocksOnSubLinks_context context 
)
static

Definition at line 291 of file rewriteHandler.c.

References AcquireRewriteLocks(), expression_tree_walker(), acquireLocksOnSubLinks_context::for_execute, IsA, NULL, and SubLink::subselect.

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

292 {
293  if (node == NULL)
294  return false;
295  if (IsA(node, SubLink))
296  {
297  SubLink *sub = (SubLink *) node;
298 
299  /* Do what we came for */
301  context->for_execute,
302  false);
303  /* Fall through to process lefthand args of SubLink */
304  }
305 
306  /*
307  * Do NOT recurse into Query nodes, because AcquireRewriteLocks already
308  * processed subselects of subselects for us.
309  */
310  return expression_tree_walker(node, acquireLocksOnSubLinks, context);
311 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
static bool acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context)
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
#define NULL
Definition: c.h:229
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1855
void AcquireRewriteLocks ( Query parsetree,
bool  forExecute,
bool  forUpdatePushedDown 
)

Definition at line 132 of file rewriteHandler.c.

References AccessShareLock, acquireLocksOnSubLinks(), AcquireRewriteLocks(), Assert, Query::cteList, CommonTableExpr::ctequery, elog, ERROR, acquireLocksOnSubLinks_context::for_execute, get_parse_rowmark(), get_rte_attribute_is_dropped(), Query::hasSubLinks, heap_close, heap_open(), IsA, RangeTblEntry::joinaliasvars, lappend(), lfirst, NIL, NoLock, NULL, QTW_IGNORE_RC_SUBQUERIES, query_tree_walker(), RelationData::rd_rel, RangeTblEntry::relid, RangeTblEntry::relkind, Query::resultRelation, RowExclusiveLock, RowShareLock, rt_fetch, Query::rtable, RTE_JOIN, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, strip_implicit_coercions(), RangeTblEntry::subquery, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by acquireLocksOnSubLinks(), AcquireRewriteLocks(), ApplyRetrieveRule(), get_query_def(), make_ruledef(), refresh_matview_datafill(), and rewriteRuleAction().

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

Definition at line 2555 of file rewriteHandler.c.

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

Referenced by relation_is_updatable(), and rewriteTargetView().

2556 {
2557  Bitmapset *result = NULL;
2558  int col;
2559 
2560  col = -1;
2561  while ((col = bms_next_member(cols, col)) >= 0)
2562  {
2563  /* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
2565 
2566  if (attno == InvalidAttrNumber)
2567  {
2568  /*
2569  * There's a whole-row reference to the view. For permissions
2570  * purposes, treat it as a reference to each column available from
2571  * the view. (We should *not* convert this to a whole-row
2572  * reference to the base relation, since the view may not touch
2573  * all columns of the base relation.)
2574  */
2575  ListCell *lc;
2576 
2577  foreach(lc, targetlist)
2578  {
2579  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2580  Var *var;
2581 
2582  if (tle->resjunk)
2583  continue;
2584  var = castNode(Var, tle->expr);
2585  result = bms_add_member(result,
2586  var->varattno - FirstLowInvalidHeapAttributeNumber);
2587  }
2588  }
2589  else
2590  {
2591  /*
2592  * Views do not have system columns, so we do not expect to see
2593  * any other system attnos here. If we do find one, the error
2594  * case will apply.
2595  */
2596  TargetEntry *tle = get_tle_by_resno(targetlist, attno);
2597 
2598  if (tle != NULL && !tle->resjunk && IsA(tle->expr, Var))
2599  {
2600  Var *var = (Var *) tle->expr;
2601 
2602  result = bms_add_member(result,
2603  var->varattno - FirstLowInvalidHeapAttributeNumber);
2604  }
2605  else
2606  elog(ERROR, "attribute number %d not found in view targetlist",
2607  attno);
2608  }
2609  }
2610 
2611  return result;
2612 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
#define castNode(_type_, nodeptr)
Definition: nodes.h:587
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:907
AttrNumber varattno
Definition: primnodes.h:168
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
Definition: primnodes.h:163
bool resjunk
Definition: primnodes.h:1359
#define ERROR
Definition: elog.h:43
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1352
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
#define InvalidAttrNumber
Definition: attnum.h:23
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define elog
Definition: elog.h:219
int16 AttrNumber
Definition: attnum.h:21
static List * adjustJoinTreeList ( Query parsetree,
bool  removert,
int  rt_index 
)
static

Definition at line 635 of file rewriteHandler.c.

References copyObject(), FromExpr::fromlist, IsA, Query::jointree, lfirst, list_delete_ptr(), and RangeTblRef::rtindex.

Referenced by rewriteRuleAction().

636 {
637  List *newjointree = copyObject(parsetree->jointree->fromlist);
638  ListCell *l;
639 
640  if (removert)
641  {
642  foreach(l, newjointree)
643  {
644  RangeTblRef *rtr = lfirst(l);
645 
646  if (IsA(rtr, RangeTblRef) &&
647  rtr->rtindex == rt_index)
648  {
649  newjointree = list_delete_ptr(newjointree, rtr);
650 
651  /*
652  * foreach is safe because we exit loop after list_delete...
653  */
654  break;
655  }
656  }
657  }
658  return newjointree;
659 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
FromExpr * jointree
Definition: parsenodes.h:129
List * fromlist
Definition: primnodes.h:1455
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
void * copyObject(const void *from)
Definition: copyfuncs.c:4592
#define lfirst(lc)
Definition: pg_list.h:106
Definition: pg_list.h:45
static Query* ApplyRetrieveRule ( Query parsetree,
RewriteRule rule,
int  rt_index,
Relation  relation,
List activeRIRs,
bool  forUpdatePushedDown 
)
static

Definition at line 1384 of file rewriteHandler.c.

References AcquireRewriteLocks(), RewriteRule::actions, Assert, ChangeVarNodes(), RangeTblEntry::checkAsUser, CMD_DELETE, CMD_INSERT, CMD_UPDATE, Query::commandType, copyObject(), elog, ERROR, fireRIRrules(), get_parse_rowmark(), RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, Query::jointree, lappend(), linitial, list_length(), markQueryForLocking(), NULL, PRS2_OLD_VARNO, RewriteRule::qual, RelationData::rd_id, RelationIsSecurityView, RangeTblEntry::relid, RangeTblEntry::requiredPerms, Query::resultRelation, Query::returningList, rt_fetch, Query::rtable, RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblEntry::security_barrier, RangeTblEntry::selectedCols, RowMarkClause::strength, RangeTblEntry::subquery, RangeTblEntry::updatedCols, and RowMarkClause::waitPolicy.

Referenced by fireRIRrules().

1390 {
1391  Query *rule_action;
1392  RangeTblEntry *rte,
1393  *subrte;
1394  RowMarkClause *rc;
1395 
1396  if (list_length(rule->actions) != 1)
1397  elog(ERROR, "expected just one rule action");
1398  if (rule->qual != NULL)
1399  elog(ERROR, "cannot handle qualified ON SELECT rule");
1400 
1401  if (rt_index == parsetree->resultRelation)
1402  {
1403  /*
1404  * We have a view as the result relation of the query, and it wasn't
1405  * rewritten by any rule. This case is supported if there is an
1406  * INSTEAD OF trigger that will trap attempts to insert/update/delete
1407  * view rows. The executor will check that; for the moment just plow
1408  * ahead. We have two cases:
1409  *
1410  * For INSERT, we needn't do anything. The unmodified RTE will serve
1411  * fine as the result relation.
1412  *
1413  * For UPDATE/DELETE, we need to expand the view so as to have source
1414  * data for the operation. But we also need an unmodified RTE to
1415  * serve as the target. So, copy the RTE and add the copy to the
1416  * rangetable. Note that the copy does not get added to the jointree.
1417  * Also note that there's a hack in fireRIRrules to avoid calling this
1418  * function again when it arrives at the copied RTE.
1419  */
1420  if (parsetree->commandType == CMD_INSERT)
1421  return parsetree;
1422  else if (parsetree->commandType == CMD_UPDATE ||
1423  parsetree->commandType == CMD_DELETE)
1424  {
1425  RangeTblEntry *newrte;
1426 
1427  rte = rt_fetch(rt_index, parsetree->rtable);
1428  newrte = copyObject(rte);
1429  parsetree->rtable = lappend(parsetree->rtable, newrte);
1430  parsetree->resultRelation = list_length(parsetree->rtable);
1431 
1432  /*
1433  * There's no need to do permissions checks twice, so wipe out the
1434  * permissions info for the original RTE (we prefer to keep the
1435  * bits set on the result RTE).
1436  */
1437  rte->requiredPerms = 0;
1438  rte->checkAsUser = InvalidOid;
1439  rte->selectedCols = NULL;
1440  rte->insertedCols = NULL;
1441  rte->updatedCols = NULL;
1442 
1443  /*
1444  * For the most part, Vars referencing the view should remain as
1445  * they are, meaning that they implicitly represent OLD values.
1446  * But in the RETURNING list if any, we want such Vars to
1447  * represent NEW values, so change them to reference the new RTE.
1448  *
1449  * Since ChangeVarNodes scribbles on the tree in-place, copy the
1450  * RETURNING list first for safety.
1451  */
1452  parsetree->returningList = copyObject(parsetree->returningList);
1453  ChangeVarNodes((Node *) parsetree->returningList, rt_index,
1454  parsetree->resultRelation, 0);
1455 
1456  /* Now, continue with expanding the original view RTE */
1457  }
1458  else
1459  elog(ERROR, "unrecognized commandType: %d",
1460  (int) parsetree->commandType);
1461  }
1462 
1463  /*
1464  * If FOR [KEY] UPDATE/SHARE of view, be sure we get right initial lock on
1465  * the relations it references.
1466  */
1467  rc = get_parse_rowmark(parsetree, rt_index);
1468  forUpdatePushedDown |= (rc != NULL);
1469 
1470  /*
1471  * Make a modifiable copy of the view query, and acquire needed locks on
1472  * the relations it mentions.
1473  */
1474  rule_action = copyObject(linitial(rule->actions));
1475 
1476  AcquireRewriteLocks(rule_action, true, forUpdatePushedDown);
1477 
1478  /*
1479  * Recursively expand any view references inside the view.
1480  */
1481  rule_action = fireRIRrules(rule_action, activeRIRs, forUpdatePushedDown);
1482 
1483  /*
1484  * Now, plug the view query in as a subselect, replacing the relation's
1485  * original RTE.
1486  */
1487  rte = rt_fetch(rt_index, parsetree->rtable);
1488 
1489  rte->rtekind = RTE_SUBQUERY;
1490  rte->relid = InvalidOid;
1491  rte->security_barrier = RelationIsSecurityView(relation);
1492  rte->subquery = rule_action;
1493  rte->inh = false; /* must not be set for a subquery */
1494 
1495  /*
1496  * We move the view's permission check data down to its rangetable. The
1497  * checks will actually be done against the OLD entry therein.
1498  */
1499  subrte = rt_fetch(PRS2_OLD_VARNO, rule_action->rtable);
1500  Assert(subrte->relid == relation->rd_id);
1501  subrte->requiredPerms = rte->requiredPerms;
1502  subrte->checkAsUser = rte->checkAsUser;
1503  subrte->selectedCols = rte->selectedCols;
1504  subrte->insertedCols = rte->insertedCols;
1505  subrte->updatedCols = rte->updatedCols;
1506 
1507  rte->requiredPerms = 0; /* no permission check on subquery itself */
1508  rte->checkAsUser = InvalidOid;
1509  rte->selectedCols = NULL;
1510  rte->insertedCols = NULL;
1511  rte->updatedCols = NULL;
1512 
1513  /*
1514  * If FOR [KEY] UPDATE/SHARE of view, mark all the contained tables as
1515  * implicit FOR [KEY] UPDATE/SHARE, the same as the parser would have done
1516  * if the view's subquery had been written out explicitly.
1517  *
1518  * Note: we don't consider forUpdatePushedDown here; such marks will be
1519  * made by recursing from the upper level in markQueryForLocking.
1520  */
1521  if (rc != NULL)
1522  markQueryForLocking(rule_action, (Node *) rule_action->jointree,
1523  rc->strength, rc->waitPolicy, true);
1524 
1525  return parsetree;
1526 }
static void markQueryForLocking(Query *qry, Node *jtnode, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
Node * qual
Definition: prs2lock.h:28
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
FromExpr * jointree
Definition: parsenodes.h:129
int resultRelation
Definition: parsenodes.h:113
Definition: nodes.h:518
AclMode requiredPerms
Definition: parsenodes.h:1004
LockClauseStrength strength
Definition: parsenodes.h:1259
void * copyObject(const void *from)
Definition: copyfuncs.c:4592
Bitmapset * selectedCols
Definition: parsenodes.h:1006
#define linitial(l)
Definition: pg_list.h:110
List * rtable
Definition: parsenodes.h:128
#define ERROR
Definition: elog.h:43
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
List * actions
Definition: prs2lock.h:29
List * returningList
Definition: parsenodes.h:135
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Oid rd_id
Definition: rel.h:115
List * lappend(List *list, void *datum)
Definition: list.c:128
#define PRS2_OLD_VARNO
Definition: primnodes.h:160
bool security_barrier
Definition: parsenodes.h:935
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1008
CmdType commandType
Definition: parsenodes.h:103
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static Query * fireRIRrules(Query *parsetree, List *activeRIRs, bool forUpdatePushedDown)
static int list_length(const List *l)
Definition: pg_list.h:89
LockWaitPolicy waitPolicy
Definition: parsenodes.h:1260
RTEKind rtekind
Definition: parsenodes.h:916
Query * subquery
Definition: parsenodes.h:934
Bitmapset * insertedCols
Definition: parsenodes.h:1007
#define RelationIsSecurityView(relation)
Definition: rel.h:344
void ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
Definition: rewriteManip.c:607
#define elog
Definition: elog.h:219
Node* build_column_default ( Relation  rel,
int  attrno 
)

Definition at line 1038 of file rewriteHandler.c.

References tupleDesc::attrs, COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, tupleDesc::constr, tupleConstr::defval, ereport, errcode(), errhint(), errmsg(), ERROR, exprType(), format_type_be(), get_typdefault(), NameStr, NULL, tupleConstr::num_defval, RelationData::rd_att, and stringToNode().

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

1039 {
1040  TupleDesc rd_att = rel->rd_att;
1041  Form_pg_attribute att_tup = rd_att->attrs[attrno - 1];
1042  Oid atttype = att_tup->atttypid;
1043  int32 atttypmod = att_tup->atttypmod;
1044  Node *expr = NULL;
1045  Oid exprtype;
1046 
1047  /*
1048  * Scan to see if relation has a default for this column.
1049  */
1050  if (rd_att->constr && rd_att->constr->num_defval > 0)
1051  {
1052  AttrDefault *defval = rd_att->constr->defval;
1053  int ndef = rd_att->constr->num_defval;
1054 
1055  while (--ndef >= 0)
1056  {
1057  if (attrno == defval[ndef].adnum)
1058  {
1059  /*
1060  * Found it, convert string representation to node tree.
1061  */
1062  expr = stringToNode(defval[ndef].adbin);
1063  break;
1064  }
1065  }
1066  }
1067 
1068  if (expr == NULL)
1069  {
1070  /*
1071  * No per-column default, so look for a default for the type itself.
1072  */
1073  expr = get_typdefault(atttype);
1074  }
1075 
1076  if (expr == NULL)
1077  return NULL; /* No default anywhere */
1078 
1079  /*
1080  * Make sure the value is coerced to the target column type; this will
1081  * generally be true already, but there seem to be some corner cases
1082  * involving domain defaults where it might not be true. This should match
1083  * the parser's processing of non-defaulted expressions --- see
1084  * transformAssignedExpr().
1085  */
1086  exprtype = exprType(expr);
1087 
1088  expr = coerce_to_target_type(NULL, /* no UNKNOWN params here */
1089  expr, exprtype,
1090  atttype, atttypmod,
1093  -1);
1094  if (expr == NULL)
1095  ereport(ERROR,
1096  (errcode(ERRCODE_DATATYPE_MISMATCH),
1097  errmsg("column \"%s\" is of type %s"
1098  " but default expression is of type %s",
1099  NameStr(att_tup->attname),
1100  format_type_be(atttype),
1101  format_type_be(exprtype)),
1102  errhint("You will need to rewrite or cast the expression.")));
1103 
1104  return expr;
1105 }
void * stringToNode(char *str)
Definition: read.c:38
int errhint(const char *fmt,...)
Definition: elog.c:987
Form_pg_attribute * attrs
Definition: tupdesc.h:74
Definition: nodes.h:518
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
AttrDefault * defval
Definition: tupdesc.h:39
#define ERROR
Definition: elog.h:43
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:77
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
#define ereport(elevel, rest)
Definition: elog.h:122
TupleDesc rd_att
Definition: rel.h:114
uint16 num_defval
Definition: tupdesc.h:41
#define NULL
Definition: c.h:229
Node * get_typdefault(Oid typid)
Definition: lsyscache.c:2166
TupleConstr * constr
Definition: tupdesc.h:76
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
static Query* CopyAndAddInvertedQual ( Query parsetree,
Node rule_qual,
int  rt_index,
CmdType  event 
)
static

Definition at line 1894 of file rewriteHandler.c.

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

Referenced by fireRules().

1898 {
1899  /* Don't scribble on the passed qual (it's in the relcache!) */
1900  Node *new_qual = (Node *) copyObject(rule_qual);
1902 
1903  context.for_execute = true;
1904 
1905  /*
1906  * In case there are subqueries in the qual, acquire necessary locks and
1907  * fix any deleted JOIN RTE entries. (This is somewhat redundant with
1908  * rewriteRuleAction, but not entirely ... consider restructuring so that
1909  * we only need to process the qual this way once.)
1910  */
1911  (void) acquireLocksOnSubLinks(new_qual, &context);
1912 
1913  /* Fix references to OLD */
1914  ChangeVarNodes(new_qual, PRS2_OLD_VARNO, rt_index, 0);
1915  /* Fix references to NEW */
1916  if (event == CMD_INSERT || event == CMD_UPDATE)
1917  new_qual = ReplaceVarsFromTargetList(new_qual,
1919  0,
1920  rt_fetch(rt_index,
1921  parsetree->rtable),
1922  parsetree->targetList,
1923  (event == CMD_UPDATE) ?
1926  rt_index,
1927  &parsetree->hasSubLinks);
1928  /* And attach the fixed qual */
1929  AddInvertedQual(parsetree, new_qual);
1930 
1931  return parsetree;
1932 }
static bool acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context)
void AddInvertedQual(Query *parsetree, Node *qual)
Definition: nodes.h:518
List * targetList
Definition: parsenodes.h:131
void * copyObject(const void *from)
Definition: copyfuncs.c:4592
List * rtable
Definition: parsenodes.h:128
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define PRS2_OLD_VARNO
Definition: primnodes.h:160
Node * ReplaceVarsFromTargetList(Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
bool hasSubLinks
Definition: parsenodes.h:119
void ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
Definition: rewriteManip.c:607
#define PRS2_NEW_VARNO
Definition: primnodes.h:161
static bool fireRIRonSubLink ( Node node,
List activeRIRs 
)
static

Definition at line 1600 of file rewriteHandler.c.

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

Referenced by fireRIRrules().

1601 {
1602  if (node == NULL)
1603  return false;
1604  if (IsA(node, SubLink))
1605  {
1606  SubLink *sub = (SubLink *) node;
1607 
1608  /* Do what we came for */
1609  sub->subselect = (Node *) fireRIRrules((Query *) sub->subselect,
1610  activeRIRs, false);
1611  /* Fall through to process lefthand args of SubLink */
1612  }
1613 
1614  /*
1615  * Do NOT recurse into Query nodes, because fireRIRrules already processed
1616  * subselects of subselects for us.
1617  */
1619  (void *) activeRIRs);
1620 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
Definition: nodes.h:518
static bool fireRIRonSubLink(Node *node, List *activeRIRs)
#define NULL
Definition: c.h:229
static Query * fireRIRrules(Query *parsetree, List *activeRIRs, bool forUpdatePushedDown)
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1855
static Query * fireRIRrules ( Query parsetree,
List activeRIRs,
bool  forUpdatePushedDown 
)
static

Definition at line 1628 of file rewriteHandler.c.

References acquireLocksOnSubLinks(), ApplyRetrieveRule(), CMD_SELECT, Query::cteList, CommonTableExpr::ctequery, ereport, errcode(), errmsg(), ERROR, RewriteRule::event, expression_tree_walker(), fireRIRonSubLink(), acquireLocksOnSubLinks_context::for_execute, get_parse_rowmark(), get_row_security_policies(), Query::hasRowSecurity, Query::hasSubLinks, heap_close, heap_open(), i, lappend(), lcons_oid(), lfirst, list_concat(), list_delete_first(), list_length(), list_member_oid(), NIL, NoLock, NULL, RuleLock::numLocks, QTW_IGNORE_RC_SUBQUERIES, query_tree_walker(), rangeTableEntry_used(), RelationData::rd_rules, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, RangeTblEntry::relkind, RELKIND_MATVIEW, RELKIND_RELATION, Query::resultRelation, rt_fetch, Query::rtable, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, RuleLock::rules, rules, RangeTblEntry::securityQuals, RangeTblEntry::subquery, and Query::withCheckOptions.

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

1629 {
1630  int origResultRelation = parsetree->resultRelation;
1631  int rt_index;
1632  ListCell *lc;
1633 
1634  /*
1635  * don't try to convert this into a foreach loop, because rtable list can
1636  * get changed each time through...
1637  */
1638  rt_index = 0;
1639  while (rt_index < list_length(parsetree->rtable))
1640  {
1641  RangeTblEntry *rte;
1642  Relation rel;
1643  List *locks;
1644  RuleLock *rules;
1645  RewriteRule *rule;
1646  int i;
1647 
1648  ++rt_index;
1649 
1650  rte = rt_fetch(rt_index, parsetree->rtable);
1651 
1652  /*
1653  * A subquery RTE can't have associated rules, so there's nothing to
1654  * do to this level of the query, but we must recurse into the
1655  * subquery to expand any rule references in it.
1656  */
1657  if (rte->rtekind == RTE_SUBQUERY)
1658  {
1659  rte->subquery = fireRIRrules(rte->subquery, activeRIRs,
1660  (forUpdatePushedDown ||
1661  get_parse_rowmark(parsetree, rt_index) != NULL));
1662  continue;
1663  }
1664 
1665  /*
1666  * Joins and other non-relation RTEs can be ignored completely.
1667  */
1668  if (rte->rtekind != RTE_RELATION)
1669  continue;
1670 
1671  /*
1672  * Always ignore RIR rules for materialized views referenced in
1673  * queries. (This does not prevent refreshing MVs, since they aren't
1674  * referenced in their own query definitions.)
1675  *
1676  * Note: in the future we might want to allow MVs to be conditionally
1677  * expanded as if they were regular views, if they are not scannable.
1678  * In that case this test would need to be postponed till after we've
1679  * opened the rel, so that we could check its state.
1680  */
1681  if (rte->relkind == RELKIND_MATVIEW)
1682  continue;
1683 
1684  /*
1685  * If the table is not referenced in the query, then we ignore it.
1686  * This prevents infinite expansion loop due to new rtable entries
1687  * inserted by expansion of a rule. A table is referenced if it is
1688  * part of the join set (a source table), or is referenced by any Var
1689  * nodes, or is the result table.
1690  */
1691  if (rt_index != parsetree->resultRelation &&
1692  !rangeTableEntry_used((Node *) parsetree, rt_index, 0))
1693  continue;
1694 
1695  /*
1696  * Also, if this is a new result relation introduced by
1697  * ApplyRetrieveRule, we don't want to do anything more with it.
1698  */
1699  if (rt_index == parsetree->resultRelation &&
1700  rt_index != origResultRelation)
1701  continue;
1702 
1703  /*
1704  * We can use NoLock here since either the parser or
1705  * AcquireRewriteLocks should have locked the rel already.
1706  */
1707  rel = heap_open(rte->relid, NoLock);
1708 
1709  /*
1710  * Collect the RIR rules that we must apply
1711  */
1712  rules = rel->rd_rules;
1713  if (rules != NULL)
1714  {
1715  locks = NIL;
1716  for (i = 0; i < rules->numLocks; i++)
1717  {
1718  rule = rules->rules[i];
1719  if (rule->event != CMD_SELECT)
1720  continue;
1721 
1722  locks = lappend(locks, rule);
1723  }
1724 
1725  /*
1726  * If we found any, apply them --- but first check for recursion!
1727  */
1728  if (locks != NIL)
1729  {
1730  ListCell *l;
1731 
1732  if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
1733  ereport(ERROR,
1734  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1735  errmsg("infinite recursion detected in rules for relation \"%s\"",
1736  RelationGetRelationName(rel))));
1737  activeRIRs = lcons_oid(RelationGetRelid(rel), activeRIRs);
1738 
1739  foreach(l, locks)
1740  {
1741  rule = lfirst(l);
1742 
1743  parsetree = ApplyRetrieveRule(parsetree,
1744  rule,
1745  rt_index,
1746  rel,
1747  activeRIRs,
1748  forUpdatePushedDown);
1749  }
1750 
1751  activeRIRs = list_delete_first(activeRIRs);
1752  }
1753  }
1754 
1755  heap_close(rel, NoLock);
1756  }
1757 
1758  /* Recurse into subqueries in WITH */
1759  foreach(lc, parsetree->cteList)
1760  {
1761  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1762 
1763  cte->ctequery = (Node *)
1764  fireRIRrules((Query *) cte->ctequery, activeRIRs, false);
1765  }
1766 
1767  /*
1768  * Recurse into sublink subqueries, too. But we already did the ones in
1769  * the rtable and cteList.
1770  */
1771  if (parsetree->hasSubLinks)
1772  query_tree_walker(parsetree, fireRIRonSubLink, (void *) activeRIRs,
1774 
1775  /*
1776  * Apply any row level security policies. We do this last because it
1777  * requires special recursion detection if the new quals have sublink
1778  * subqueries, and if we did it in the loop above query_tree_walker would
1779  * then recurse into those quals a second time.
1780  */
1781  rt_index = 0;
1782  foreach(lc, parsetree->rtable)
1783  {
1784  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
1785  Relation rel;
1786  List *securityQuals;
1787  List *withCheckOptions;
1788  bool hasRowSecurity;
1789  bool hasSubLinks;
1790 
1791  ++rt_index;
1792 
1793  /* Only normal relations can have RLS policies */
1794  if (rte->rtekind != RTE_RELATION ||
1795  rte->relkind != RELKIND_RELATION)
1796  continue;
1797 
1798  rel = heap_open(rte->relid, NoLock);
1799 
1800  /*
1801  * Fetch any new security quals that must be applied to this RTE.
1802  */
1803  get_row_security_policies(parsetree, rte, rt_index,
1804  &securityQuals, &withCheckOptions,
1805  &hasRowSecurity, &hasSubLinks);
1806 
1807  if (securityQuals != NIL || withCheckOptions != NIL)
1808  {
1809  if (hasSubLinks)
1810  {
1812 
1813  /*
1814  * Recursively process the new quals, checking for infinite
1815  * recursion.
1816  */
1817  if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
1818  ereport(ERROR,
1819  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1820  errmsg("infinite recursion detected in policy for relation \"%s\"",
1821  RelationGetRelationName(rel))));
1822 
1823  activeRIRs = lcons_oid(RelationGetRelid(rel), activeRIRs);
1824 
1825  /*
1826  * get_row_security_policies just passed back securityQuals
1827  * and/or withCheckOptions, and there were SubLinks, make sure
1828  * we lock any relations which are referenced.
1829  *
1830  * These locks would normally be acquired by the parser, but
1831  * securityQuals and withCheckOptions are added post-parsing.
1832  */
1833  context.for_execute = true;
1834  (void) acquireLocksOnSubLinks((Node *) securityQuals, &context);
1835  (void) acquireLocksOnSubLinks((Node *) withCheckOptions,
1836  &context);
1837 
1838  /*
1839  * Now that we have the locks on anything added by
1840  * get_row_security_policies, fire any RIR rules for them.
1841  */
1842  expression_tree_walker((Node *) securityQuals,
1843  fireRIRonSubLink, (void *) activeRIRs);
1844 
1845  expression_tree_walker((Node *) withCheckOptions,
1846  fireRIRonSubLink, (void *) activeRIRs);
1847 
1848  activeRIRs = list_delete_first(activeRIRs);
1849  }
1850 
1851  /*
1852  * Add the new security barrier quals to the start of the RTE's
1853  * list so that they get applied before any existing barrier quals
1854  * (which would have come from a security-barrier view, and should
1855  * get lower priority than RLS conditions on the table itself).
1856  */
1857  rte->securityQuals = list_concat(securityQuals,
1858  rte->securityQuals);
1859 
1860  parsetree->withCheckOptions = list_concat(withCheckOptions,
1861  parsetree->withCheckOptions);
1862  }
1863 
1864  /*
1865  * Make sure the query is marked correctly if row level security
1866  * applies, or if the new quals had sublinks.
1867  */
1868  if (hasRowSecurity)
1869  parsetree->hasRowSecurity = true;
1870  if (hasSubLinks)
1871  parsetree->hasSubLinks = true;
1872 
1873  heap_close(rel, NoLock);
1874  }
1875 
1876  return parsetree;
1877 }
#define NIL
Definition: pg_list.h:69
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2257
int numLocks
Definition: prs2lock.h:42
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
static bool acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context)
void get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index, List **securityQuals, List **withCheckOptions, bool *hasRowSecurity, bool *hasSubLinks)
Definition: rowsecurity.c:106
List * withCheckOptions
Definition: parsenodes.h:160
List * securityQuals
Definition: parsenodes.h:1009
int resultRelation
Definition: parsenodes.h:113
#define RELKIND_MATVIEW
Definition: pg_class.h:165
Definition: nodes.h:518
int errcode(int sqlerrcode)
Definition: elog.c:575
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * lcons_oid(Oid datum, List *list)
Definition: list.c:295
#define heap_close(r, l)
Definition: heapam.h:97
static Query * ApplyRetrieveRule(Query *parsetree, RewriteRule *rule, int rt_index, Relation relation, List *activeRIRs, bool forUpdatePushedDown)
Definition: localtime.c:74
#define QTW_IGNORE_RC_SUBQUERIES
Definition: nodeFuncs.h:22
List * rtable
Definition: parsenodes.h:128
#define ERROR
Definition: elog.h:43
static bool fireRIRonSubLink(Node *node, List *activeRIRs)
#define NoLock
Definition: lockdefs.h:34
CmdType event
Definition: prs2lock.h:27
static struct rule * rules
Definition: zic.c:267
#define RelationGetRelationName(relation)
Definition: rel.h:433
RewriteRule ** rules
Definition: prs2lock.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * lappend(List *list, void *datum)
Definition: list.c:128
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
static Query * fireRIRrules(Query *parsetree, List *activeRIRs, bool forUpdatePushedDown)
RuleLock * rd_rules
Definition: rel.h:117
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1855
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:916
List * cteList
Definition: parsenodes.h:126
Query * subquery
Definition: parsenodes.h:934
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool hasSubLinks
Definition: parsenodes.h:119
int i
bool rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
Definition: rewriteManip.c:889
#define RELKIND_RELATION
Definition: pg_class.h:160
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:413
List * list_delete_first(List *list)
Definition: list.c:666
bool hasRowSecurity
Definition: parsenodes.h:124
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 1964 of file rewriteHandler.c.

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

Referenced by RewriteQuery().

1971 {
1972  List *results = NIL;
1973  ListCell *l;
1974 
1975  foreach(l, locks)
1976  {
1977  RewriteRule *rule_lock = (RewriteRule *) lfirst(l);
1978  Node *event_qual = rule_lock->qual;
1979  List *actions = rule_lock->actions;
1980  QuerySource qsrc;
1981  ListCell *r;
1982 
1983  /* Determine correct QuerySource value for actions */
1984  if (rule_lock->isInstead)
1985  {
1986  if (event_qual != NULL)
1987  qsrc = QSRC_QUAL_INSTEAD_RULE;
1988  else
1989  {
1990  qsrc = QSRC_INSTEAD_RULE;
1991  *instead_flag = true; /* report unqualified INSTEAD */
1992  }
1993  }
1994  else
1995  qsrc = QSRC_NON_INSTEAD_RULE;
1996 
1997  if (qsrc == QSRC_QUAL_INSTEAD_RULE)
1998  {
1999  /*
2000  * If there are INSTEAD rules with qualifications, the original
2001  * query is still performed. But all the negated rule
2002  * qualifications of the INSTEAD rules are added so it does its
2003  * actions only in cases where the rule quals of all INSTEAD rules
2004  * are false. Think of it as the default action in a case. We save
2005  * this in *qual_product so RewriteQuery() can add it to the query
2006  * list after we mangled it up enough.
2007  *
2008  * If we have already found an unqualified INSTEAD rule, then
2009  * *qual_product won't be used, so don't bother building it.
2010  */
2011  if (!*instead_flag)
2012  {
2013  if (*qual_product == NULL)
2014  *qual_product = copyObject(parsetree);
2015  *qual_product = CopyAndAddInvertedQual(*qual_product,
2016  event_qual,
2017  rt_index,
2018  event);
2019  }
2020  }
2021 
2022  /* Now process the rule's actions and add them to the result list */
2023  foreach(r, actions)
2024  {
2025  Query *rule_action = lfirst(r);
2026 
2027  if (rule_action->commandType == CMD_NOTHING)
2028  continue;
2029 
2030  rule_action = rewriteRuleAction(parsetree, rule_action,
2031  event_qual, rt_index, event,
2032  returning_flag);
2033 
2034  rule_action->querySource = qsrc;
2035  rule_action->canSetTag = false; /* might change later */
2036 
2037  results = lappend(results, rule_action);
2038  }
2039  }
2040 
2041  return results;
2042 }
#define NIL
Definition: pg_list.h:69
Node * qual
Definition: prs2lock.h:28
QuerySource
Definition: parsenodes.h:31
Definition: nodes.h:518
bool isInstead
Definition: prs2lock.h:31
void * copyObject(const void *from)
Definition: copyfuncs.c:4592
static Query * CopyAndAddInvertedQual(Query *parsetree, Node *rule_qual, int rt_index, CmdType event)
List * actions
Definition: prs2lock.h:29
List * lappend(List *list, void *datum)
Definition: list.c:128
CmdType commandType
Definition: parsenodes.h:103
#define NULL
Definition: c.h:229
QuerySource querySource
Definition: parsenodes.h:105
#define lfirst(lc)
Definition: pg_list.h:106
bool canSetTag
Definition: parsenodes.h:109
static Query * rewriteRuleAction(Query *parsetree, Query *rule_action, Node *rule_qual, int rt_index, CmdType event, bool *returning_flag)
Definition: pg_list.h:45
static Node * get_assignment_input ( Node node)
static

Definition at line 1011 of file rewriteHandler.c.

References FieldStore::arg, IsA, NULL, ArrayRef::refassgnexpr, and ArrayRef::refexpr.

Referenced by process_matched_tle().

1012 {
1013  if (node == NULL)
1014  return NULL;
1015  if (IsA(node, FieldStore))
1016  {
1017  FieldStore *fstore = (FieldStore *) node;
1018 
1019  return (Node *) fstore->arg;
1020  }
1021  else if (IsA(node, ArrayRef))
1022  {
1023  ArrayRef *aref = (ArrayRef *) node;
1024 
1025  if (aref->refassgnexpr == NULL)
1026  return NULL;
1027  return (Node *) aref->refexpr;
1028  }
1029  return NULL;
1030 }
Expr * refassgnexpr
Definition: primnodes.h:409
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
Expr * arg
Definition: primnodes.h:766
Definition: nodes.h:518
#define NULL
Definition: c.h:229
Expr * refexpr
Definition: primnodes.h:407
Query* get_view_query ( Relation  view)

Definition at line 2055 of file rewriteHandler.c.

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

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

2056 {
2057  int i;
2058 
2059  Assert(view->rd_rel->relkind == RELKIND_VIEW);
2060 
2061  for (i = 0; i < view->rd_rules->numLocks; i++)
2062  {
2063  RewriteRule *rule = view->rd_rules->rules[i];
2064 
2065  if (rule->event == CMD_SELECT)
2066  {
2067  /* A _RETURN rule should have only one action */
2068  if (list_length(rule->actions) != 1)
2069  elog(ERROR, "invalid _RETURN rule action specification");
2070 
2071  return (Query *) linitial(rule->actions);
2072  }
2073  }
2074 
2075  elog(ERROR, "failed to find _RETURN rule for view");
2076  return NULL; /* keep compiler quiet */
2077 }
int numLocks
Definition: prs2lock.h:42
Form_pg_class rd_rel
Definition: rel.h:113
Definition: localtime.c:74
#define linitial(l)
Definition: pg_list.h:110
#define ERROR
Definition: elog.h:43
CmdType event
Definition: prs2lock.h:27
RewriteRule ** rules
Definition: prs2lock.h:43
List * actions
Definition: prs2lock.h:29
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
RuleLock * rd_rules
Definition: rel.h:117
static int list_length(const List *l)
Definition: pg_list.h:89
#define RELKIND_VIEW
Definition: pg_class.h:164
int i
#define elog
Definition: elog.h:219
static void markQueryForLocking ( Query qry,
Node jtnode,
LockClauseStrength  strength,
LockWaitPolicy  waitPolicy,
bool  pushedDown 
)
static

Definition at line 1540 of file rewriteHandler.c.

References ACL_SELECT_FOR_UPDATE, applyLockingClause(), elog, ERROR, FromExpr::fromlist, IsA, Query::jointree, JoinExpr::larg, lfirst, nodeTag, NULL, JoinExpr::rarg, RangeTblEntry::requiredPerms, rt_fetch, Query::rtable, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, and RangeTblEntry::subquery.

Referenced by ApplyRetrieveRule().

1543 {
1544  if (jtnode == NULL)
1545  return;
1546  if (IsA(jtnode, RangeTblRef))
1547  {
1548  int rti = ((RangeTblRef *) jtnode)->rtindex;
1549  RangeTblEntry *rte = rt_fetch(rti, qry->rtable);
1550 
1551  if (rte->rtekind == RTE_RELATION)
1552  {
1553  applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
1555  }
1556  else if (rte->rtekind == RTE_SUBQUERY)
1557  {
1558  applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
1559  /* FOR UPDATE/SHARE of subquery is propagated to subquery's rels */
1561  strength, waitPolicy, true);
1562  }
1563  /* other RTE types are unaffected by FOR UPDATE */
1564  }
1565  else if (IsA(jtnode, FromExpr))
1566  {
1567  FromExpr *f = (FromExpr *) jtnode;
1568  ListCell *l;
1569 
1570  foreach(l, f->fromlist)
1571  markQueryForLocking(qry, lfirst(l), strength, waitPolicy, pushedDown);
1572  }
1573  else if (IsA(jtnode, JoinExpr))
1574  {
1575  JoinExpr *j = (JoinExpr *) jtnode;
1576 
1577  markQueryForLocking(qry, j->larg, strength, waitPolicy, pushedDown);
1578  markQueryForLocking(qry, j->rarg, strength, waitPolicy, pushedDown);
1579  }
1580  else
1581  elog(ERROR, "unrecognized node type: %d",
1582  (int) nodeTag(jtnode));
1583 }
static void markQueryForLocking(Query *qry, Node *jtnode, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
FromExpr * jointree
Definition: parsenodes.h:129
Definition: nodes.h:518
AclMode requiredPerms
Definition: parsenodes.h:1004
List * fromlist
Definition: primnodes.h:1455
Node * larg
Definition: primnodes.h:1435
List * rtable
Definition: parsenodes.h:128
#define ERROR
Definition: elog.h:43
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Node * rarg
Definition: primnodes.h:1436
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
#define ACL_SELECT_FOR_UPDATE
Definition: parsenodes.h:81
#define nodeTag(nodeptr)
Definition: nodes.h:523
RTEKind rtekind
Definition: parsenodes.h:916
Query * subquery
Definition: parsenodes.h:934
#define elog
Definition: elog.h:219
void applyLockingClause(Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
Definition: analyze.c:2827
static List * matchLocks ( CmdType  event,
RuleLock rulelocks,
int  varno,
Query parsetree,
bool hasUpdate 
)
static

Definition at line 1318 of file rewriteHandler.c.

References CMD_SELECT, CMD_UPDATE, Query::commandType, RewriteRule::enabled, RewriteRule::event, i, lappend(), NIL, NULL, RuleLock::numLocks, rangeTableEntry_used(), Query::resultRelation, RULE_DISABLED, RULE_FIRES_ON_ORIGIN, RULE_FIRES_ON_REPLICA, RuleLock::rules, SESSION_REPLICATION_ROLE_REPLICA, and SessionReplicationRole.

Referenced by RewriteQuery().

1323 {
1324  List *matching_locks = NIL;
1325  int nlocks;
1326  int i;
1327 
1328  if (rulelocks == NULL)
1329  return NIL;
1330 
1331  if (parsetree->commandType != CMD_SELECT)
1332  {
1333  if (parsetree->resultRelation != varno)
1334  return NIL;
1335  }
1336 
1337  nlocks = rulelocks->numLocks;
1338 
1339  for (i = 0; i < nlocks; i++)
1340  {
1341  RewriteRule *oneLock = rulelocks->rules[i];
1342 
1343  if (oneLock->event == CMD_UPDATE)
1344  *hasUpdate = true;
1345 
1346  /*
1347  * Suppress ON INSERT/UPDATE/DELETE rules that are disabled or
1348  * configured to not fire during the current sessions replication
1349  * role. ON SELECT rules will always be applied in order to keep views
1350  * working even in LOCAL or REPLICA role.
1351  */
1352  if (oneLock->event != CMD_SELECT)
1353  {
1355  {
1356  if (oneLock->enabled == RULE_FIRES_ON_ORIGIN ||
1357  oneLock->enabled == RULE_DISABLED)
1358  continue;
1359  }
1360  else /* ORIGIN or LOCAL ROLE */
1361  {
1362  if (oneLock->enabled == RULE_FIRES_ON_REPLICA ||
1363  oneLock->enabled == RULE_DISABLED)
1364  continue;
1365  }
1366  }
1367 
1368  if (oneLock->event == event)
1369  {
1370  if (parsetree->commandType != CMD_SELECT ||
1371  rangeTableEntry_used((Node *) parsetree, varno, 0))
1372  matching_locks = lappend(matching_locks, oneLock);
1373  }
1374  }
1375 
1376  return matching_locks;
1377 }
#define NIL
Definition: pg_list.h:69
int numLocks
Definition: prs2lock.h:42
int resultRelation
Definition: parsenodes.h:113
Definition: nodes.h:518
int SessionReplicationRole
Definition: trigger.c:64
CmdType event
Definition: prs2lock.h:27
RewriteRule ** rules
Definition: prs2lock.h:43
#define RULE_DISABLED
Definition: rewriteDefine.h:24
List * lappend(List *list, void *datum)
Definition: list.c:128
#define RULE_FIRES_ON_ORIGIN
Definition: rewriteDefine.h:21
#define SESSION_REPLICATION_ROLE_REPLICA
Definition: trigger.h:101
#define RULE_FIRES_ON_REPLICA
Definition: rewriteDefine.h:23
CmdType commandType
Definition: parsenodes.h:103
#define NULL
Definition: c.h:229
int i
bool rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
Definition: rewriteManip.c:889
Definition: pg_list.h:45
char enabled
Definition: prs2lock.h:30
static TargetEntry * process_matched_tle ( TargetEntry src_tle,
TargetEntry prior_tle,
const char *  attrName 
)
static

Definition at line 889 of file rewriteHandler.c.

References FieldStore::arg, elog, equal(), ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprType(), FieldStore::fieldnums, flatCopyTargetEntry(), get_assignment_input(), IsA, list_concat(), list_copy(), makeNode, FieldStore::newvals, NULL, and ArrayRef::refexpr.

Referenced by rewriteTargetListIU().

892 {
893  TargetEntry *result;
894  Node *src_expr;
895  Node *prior_expr;
896  Node *src_input;
897  Node *prior_input;
898  Node *priorbottom;
899  Node *newexpr;
900 
901  if (prior_tle == NULL)
902  {
903  /*
904  * Normal case where this is the first assignment to the attribute.
905  */
906  return src_tle;
907  }
908 
909  /*----------
910  * Multiple assignments to same attribute. Allow only if all are
911  * FieldStore or ArrayRef assignment operations. This is a bit
912  * tricky because what we may actually be looking at is a nest of
913  * such nodes; consider
914  * UPDATE tab SET col.fld1.subfld1 = x, col.fld2.subfld2 = y
915  * The two expressions produced by the parser will look like
916  * FieldStore(col, fld1, FieldStore(placeholder, subfld1, x))
917  * FieldStore(col, fld2, FieldStore(placeholder, subfld2, y))
918  * However, we can ignore the substructure and just consider the top
919  * FieldStore or ArrayRef from each assignment, because it works to
920  * combine these as
921  * FieldStore(FieldStore(col, fld1,
922  * FieldStore(placeholder, subfld1, x)),
923  * fld2, FieldStore(placeholder, subfld2, y))
924  * Note the leftmost expression goes on the inside so that the
925  * assignments appear to occur left-to-right.
926  *
927  * For FieldStore, instead of nesting we can generate a single
928  * FieldStore with multiple target fields. We must nest when
929  * ArrayRefs are involved though.
930  *----------
931  */
932  src_expr = (Node *) src_tle->expr;
933  prior_expr = (Node *) prior_tle->expr;
934  src_input = get_assignment_input(src_expr);
935  prior_input = get_assignment_input(prior_expr);
936  if (src_input == NULL ||
937  prior_input == NULL ||
938  exprType(src_expr) != exprType(prior_expr))
939  ereport(ERROR,
940  (errcode(ERRCODE_SYNTAX_ERROR),
941  errmsg("multiple assignments to same column \"%s\"",
942  attrName)));
943 
944  /*
945  * Prior TLE could be a nest of assignments if we do this more than once.
946  */
947  priorbottom = prior_input;
948  for (;;)
949  {
950  Node *newbottom = get_assignment_input(priorbottom);
951 
952  if (newbottom == NULL)
953  break; /* found the original Var reference */
954  priorbottom = newbottom;
955  }
956  if (!equal(priorbottom, src_input))
957  ereport(ERROR,
958  (errcode(ERRCODE_SYNTAX_ERROR),
959  errmsg("multiple assignments to same column \"%s\"",
960  attrName)));
961 
962  /*
963  * Looks OK to nest 'em.
964  */
965  if (IsA(src_expr, FieldStore))
966  {
967  FieldStore *fstore = makeNode(FieldStore);
968 
969  if (IsA(prior_expr, FieldStore))
970  {
971  /* combine the two */
972  memcpy(fstore, prior_expr, sizeof(FieldStore));
973  fstore->newvals =
974  list_concat(list_copy(((FieldStore *) prior_expr)->newvals),
975  list_copy(((FieldStore *) src_expr)->newvals));
976  fstore->fieldnums =
977  list_concat(list_copy(((FieldStore *) prior_expr)->fieldnums),
978  list_copy(((FieldStore *) src_expr)->fieldnums));
979  }
980  else
981  {
982  /* general case, just nest 'em */
983  memcpy(fstore, src_expr, sizeof(FieldStore));
984  fstore->arg = (Expr *) prior_expr;
985  }
986  newexpr = (Node *) fstore;
987  }
988  else if (IsA(src_expr, ArrayRef))
989  {
990  ArrayRef *aref = makeNode(ArrayRef);
991 
992  memcpy(aref, src_expr, sizeof(ArrayRef));
993  aref->refexpr = (Expr *) prior_expr;
994  newexpr = (Node *) aref;
995  }
996  else
997  {
998  elog(ERROR, "cannot happen");
999  newexpr = NULL;
1000  }
1001 
1002  result = flatCopyTargetEntry(src_tle);
1003  result->expr = (Expr *) newexpr;
1004  return result;
1005 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
Expr * arg
Definition: primnodes.h:766
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2923
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:518
int errcode(int sqlerrcode)
Definition: elog.c:575
List * list_concat(List *list1, List *list2)
Definition: list.c:321
#define ERROR
Definition: elog.h:43
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition: makefuncs.c:268
#define ereport(elevel, rest)
Definition: elog.h:122
List * newvals
Definition: primnodes.h:767
static Node * get_assignment_input(Node *node)
#define makeNode(_type_)
Definition: nodes.h:566
#define NULL
Definition: c.h:229
Expr * expr
Definition: primnodes.h:1352
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int errmsg(const char *fmt,...)
Definition: elog.c:797
List * fieldnums
Definition: primnodes.h:768
#define elog
Definition: elog.h:219
Expr * refexpr
Definition: primnodes.h:407
List* QueryRewrite ( Query parsetree)

Definition at line 3494 of file rewriteHandler.c.

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

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

3495 {
3496  uint32 input_query_id = parsetree->queryId;
3497  List *querylist;
3498  List *results;
3499  ListCell *l;
3500  CmdType origCmdType;
3501  bool foundOriginalQuery;
3502  Query *lastInstead;
3503 
3504  /*
3505  * This function is only applied to top-level original queries
3506  */
3507  Assert(parsetree->querySource == QSRC_ORIGINAL);
3508  Assert(parsetree->canSetTag);
3509 
3510  /*
3511  * Step 1
3512  *
3513  * Apply all non-SELECT rules possibly getting 0 or many queries
3514  */
3515  querylist = RewriteQuery(parsetree, NIL);
3516 
3517  /*
3518  * Step 2
3519  *
3520  * Apply all the RIR rules on each query
3521  *
3522  * This is also a handy place to mark each query with the original queryId
3523  */
3524  results = NIL;
3525  foreach(l, querylist)
3526  {
3527  Query *query = (Query *) lfirst(l);
3528 
3529  query = fireRIRrules(query, NIL, false);
3530 
3531  query->queryId = input_query_id;
3532 
3533  results = lappend(results, query);
3534  }
3535 
3536  /*
3537  * Step 3
3538  *
3539  * Determine which, if any, of the resulting queries is supposed to set
3540  * the command-result tag; and update the canSetTag fields accordingly.
3541  *
3542  * If the original query is still in the list, it sets the command tag.
3543  * Otherwise, the last INSTEAD query of the same kind as the original is
3544  * allowed to set the tag. (Note these rules can leave us with no query
3545  * setting the tag. The tcop code has to cope with this by setting up a
3546  * default tag based on the original un-rewritten query.)
3547  *
3548  * The Asserts verify that at most one query in the result list is marked
3549  * canSetTag. If we aren't checking asserts, we can fall out of the loop
3550  * as soon as we find the original query.
3551  */
3552  origCmdType = parsetree->commandType;
3553  foundOriginalQuery = false;
3554  lastInstead = NULL;
3555 
3556  foreach(l, results)
3557  {
3558  Query *query = (Query *) lfirst(l);
3559 
3560  if (query->querySource == QSRC_ORIGINAL)
3561  {
3562  Assert(query->canSetTag);
3563  Assert(!foundOriginalQuery);
3564  foundOriginalQuery = true;
3565 #ifndef USE_ASSERT_CHECKING
3566  break;
3567 #endif
3568  }
3569  else
3570  {
3571  Assert(!query->canSetTag);
3572  if (query->commandType == origCmdType &&
3573  (query->querySource == QSRC_INSTEAD_RULE ||
3575  lastInstead = query;
3576  }
3577  }
3578 
3579  if (!foundOriginalQuery && lastInstead != NULL)
3580  lastInstead->canSetTag = true;
3581 
3582  return results;
3583 }
#define NIL
Definition: pg_list.h:69
uint32 queryId
Definition: parsenodes.h:107
static List * RewriteQuery(Query *parsetree, List *rewrite_events)
unsigned int uint32
Definition: c.h:268
List * lappend(List *list, void *datum)
Definition: list.c:128
CmdType commandType
Definition: parsenodes.h:103
#define NULL
Definition: c.h:229
QuerySource querySource
Definition: parsenodes.h:105
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static Query * fireRIRrules(Query *parsetree, List *activeRIRs, bool forUpdatePushedDown)
bool canSetTag
Definition: parsenodes.h:109
Definition: pg_list.h:45
CmdType
Definition: nodes.h:651
int relation_is_updatable ( Oid  reloid,
bool  include_triggers,
Bitmapset include_cols 
)

Definition at line 2391 of file rewriteHandler.c.

References AccessShareLock, adjust_view_column_set(), ALL_EVENTS, Assert, bms_int_members(), bms_is_empty(), 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, linitial, NULL, RuleLock::numLocks, RewriteRule::qual, RelationData::rd_rel, RelationData::rd_rules, relation_close(), relation_is_updatable(), RangeTblEntry::relid, RangeTblEntry::relkind, RELKIND_FOREIGN_TABLE, RELKIND_RELATION, RELKIND_VIEW, 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(), pg_relation_is_updatable(), and relation_is_updatable().

2394 {
2395  int events = 0;
2396  Relation rel;
2397  RuleLock *rulelocks;
2398 
2399 #define ALL_EVENTS ((1 << CMD_INSERT) | (1 << CMD_UPDATE) | (1 << CMD_DELETE))
2400 
2401  rel = try_relation_open(reloid, AccessShareLock);
2402 
2403  /*
2404  * If the relation doesn't exist, return zero rather than throwing an
2405  * error. This is helpful since scanning an information_schema view under
2406  * MVCC rules can result in referencing rels that have actually been
2407  * deleted already.
2408  */
2409  if (rel == NULL)
2410  return 0;
2411 
2412  /* If the relation is a table, it is always updatable */
2413  if (rel->rd_rel->relkind == RELKIND_RELATION)
2414  {
2416  return ALL_EVENTS;
2417  }
2418 
2419  /* Look for unconditional DO INSTEAD rules, and note supported events */
2420  rulelocks = rel->rd_rules;
2421  if (rulelocks != NULL)
2422  {
2423  int i;
2424 
2425  for (i = 0; i < rulelocks->numLocks; i++)
2426  {
2427  if (rulelocks->rules[i]->isInstead &&
2428  rulelocks->rules[i]->qual == NULL)
2429  {
2430  events |= ((1 << rulelocks->rules[i]->event) & ALL_EVENTS);
2431  }
2432  }
2433 
2434  /* If we have rules for all events, we're done */
2435  if (events == ALL_EVENTS)
2436  {
2438  return events;
2439  }
2440  }
2441 
2442  /* Similarly look for INSTEAD OF triggers, if they are to be included */
2443  if (include_triggers)
2444  {
2445  TriggerDesc *trigDesc = rel->trigdesc;
2446 
2447  if (trigDesc)
2448  {
2449  if (trigDesc->trig_insert_instead_row)
2450  events |= (1 << CMD_INSERT);
2451  if (trigDesc->trig_update_instead_row)
2452  events |= (1 << CMD_UPDATE);
2453  if (trigDesc->trig_delete_instead_row)
2454  events |= (1 << CMD_DELETE);
2455 
2456  /* If we have triggers for all events, we're done */
2457  if (events == ALL_EVENTS)
2458  {
2460  return events;
2461  }
2462  }
2463  }
2464 
2465  /* If this is a foreign table, check which update events it supports */
2466  if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2467  {
2468  FdwRoutine *fdwroutine = GetFdwRoutineForRelation(rel, false);
2469 
2470  if (fdwroutine->IsForeignRelUpdatable != NULL)
2471  events |= fdwroutine->IsForeignRelUpdatable(rel);
2472  else
2473  {
2474  /* Assume presence of executor functions is sufficient */
2475  if (fdwroutine->ExecForeignInsert != NULL)
2476  events |= (1 << CMD_INSERT);
2477  if (fdwroutine->ExecForeignUpdate != NULL)
2478  events |= (1 << CMD_UPDATE);
2479  if (fdwroutine->ExecForeignDelete != NULL)
2480  events |= (1 << CMD_DELETE);
2481  }
2482 
2484  return events;
2485  }
2486 
2487  /* Check if this is an automatically updatable view */
2488  if (rel->rd_rel->relkind == RELKIND_VIEW)
2489  {
2490  Query *viewquery = get_view_query(rel);
2491 
2492  if (view_query_is_auto_updatable(viewquery, false) == NULL)
2493  {
2494  Bitmapset *updatable_cols;
2495  int auto_events;
2496  RangeTblRef *rtr;
2497  RangeTblEntry *base_rte;
2498  Oid baseoid;
2499 
2500  /*
2501  * Determine which of the view's columns are updatable. If there
2502  * are none within the set of columns we are looking at, then the
2503  * view doesn't support INSERT/UPDATE, but it may still support
2504  * DELETE.
2505  */
2507  &updatable_cols, NULL);
2508 
2509  if (include_cols != NULL)
2510  updatable_cols = bms_int_members(updatable_cols, include_cols);
2511 
2512  if (bms_is_empty(updatable_cols))
2513  auto_events = (1 << CMD_DELETE); /* May support DELETE */
2514  else
2515  auto_events = ALL_EVENTS; /* May support all events */
2516 
2517  /*
2518  * The base relation must also support these update commands.
2519  * Tables are always updatable, but for any other kind of base
2520  * relation we must do a recursive check limited to the columns
2521  * referenced by the locally updatable columns in this view.
2522  */
2523  rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
2524  base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
2525  Assert(base_rte->rtekind == RTE_RELATION);
2526 
2527  if (base_rte->relkind != RELKIND_RELATION)
2528  {
2529  baseoid = base_rte->relid;
2530  include_cols = adjust_view_column_set(updatable_cols,
2531  viewquery->targetList);
2532  auto_events &= relation_is_updatable(baseoid,
2533  include_triggers,
2534  include_cols);
2535  }
2536  events |= auto_events;
2537  }
2538  }
2539 
2540  /* If we reach here, the relation may support some update commands */
2542  return events;
2543 }
ExecForeignDelete_function ExecForeignDelete
Definition: fdwapi.h:199
Node * qual
Definition: prs2lock.h:28
int numLocks
Definition: prs2lock.h:42
FromExpr * jointree
Definition: parsenodes.h:129
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1150
ExecForeignInsert_function ExecForeignInsert
Definition: fdwapi.h:197
static const char * view_cols_are_auto_updatable(Query *viewquery, Bitmapset *required_cols, Bitmapset **updatable_cols, char **non_updatable_col)
#define AccessShareLock
Definition: lockdefs.h:36
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1263
List * fromlist
Definition: primnodes.h:1455
Form_pg_class rd_rel
Definition: rel.h:113
unsigned int Oid
Definition: postgres_ext.h:31
bool isInstead
Definition: prs2lock.h:31
List * targetList
Definition: parsenodes.h:131
bool trig_insert_instead_row
Definition: reltrigger.h:57
#define linitial(l)
Definition: pg_list.h:110
List * rtable
Definition: parsenodes.h:128
#define ALL_EVENTS
TriggerDesc * trigdesc
Definition: rel.h:119
int relation_is_updatable(Oid reloid, bool include_triggers, Bitmapset *include_cols)
CmdType event
Definition: prs2lock.h:27
RewriteRule ** rules
Definition: prs2lock.h:43
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
bool trig_update_instead_row
Definition: reltrigger.h:62
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:633
bool trig_delete_instead_row
Definition: reltrigger.h:67
static Bitmapset * adjust_view_column_set(Bitmapset *cols, List *targetlist)
ExecForeignUpdate_function ExecForeignUpdate
Definition: fdwapi.h:198
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
RuleLock * rd_rules
Definition: rel.h:117
RTEKind rtekind
Definition: parsenodes.h:916
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:395
#define RELKIND_VIEW
Definition: pg_class.h:164
Query * get_view_query(Relation view)
int i
IsForeignRelUpdatable_function IsForeignRelUpdatable
Definition: fdwapi.h:201
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:761
const char * view_query_is_auto_updatable(Query *viewquery, bool check_cols)
#define RELKIND_RELATION
Definition: pg_class.h:160
static List* RewriteQuery ( Query parsetree,
List rewrite_events 
)
static

Definition at line 3105 of file rewriteHandler.c.

References OnConflictExpr::action, Assert, Query::canSetTag, castNode, CMD_DELETE, CMD_INSERT, CMD_SELECT, CMD_UPDATE, CMD_UTILITY, Query::commandType, Query::cteList, CommonTableExpr::ctequery, elog, ereport, errcode(), errhint(), errmsg(), ERROR, rewrite_event::event, fireRules(), FromExpr::fromlist, heap_close, heap_open(), IsA, Query::jointree, lappend(), lcons(), lfirst, linitial, list_concat(), list_delete_first(), list_length(), matchLocks(), NIL, NoLock, NULL, Query::onConflict, ONCONFLICT_UPDATE, OnConflictExpr::onConflictSet, palloc(), QSRC_NON_INSTEAD_RULE, QSRC_QUAL_INSTEAD_RULE, Query::querySource, RelationData::rd_rel, RelationData::rd_rules, rewrite_event::relation, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, RELKIND_VIEW, Query::resultRelation, Query::returningList, rewriteTargetListIU(), rewriteTargetListUD(), rewriteTargetView(), rewriteValuesRTE(), rt_fetch, Query::rtable, RTE_RELATION, RTE_VALUES, RangeTblEntry::rtekind, RangeTblRef::rtindex, Query::targetList, and view_has_instead_trigger().

Referenced by QueryRewrite().

3106 {
3107  CmdType event = parsetree->commandType;
3108  bool instead = false;
3109  bool returning = false;
3110  bool updatableview = false;
3111  Query *qual_product = NULL;
3112  List *rewritten = NIL;
3113  ListCell *lc1;
3114 
3115  /*
3116  * First, recursively process any insert/update/delete statements in WITH
3117  * clauses. (We have to do this first because the WITH clauses may get
3118  * copied into rule actions below.)
3119  */
3120  foreach(lc1, parsetree->cteList)
3121  {
3122  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc1);
3123  Query *ctequery = castNode(Query, cte->ctequery);
3124  List *newstuff;
3125 
3126  if (ctequery->commandType == CMD_SELECT)
3127  continue;
3128 
3129  newstuff = RewriteQuery(ctequery, rewrite_events);
3130 
3131  /*
3132  * Currently we can only handle unconditional, single-statement DO
3133  * INSTEAD rules correctly; we have to get exactly one Query out of
3134  * the rewrite operation to stuff back into the CTE node.
3135  */
3136  if (list_length(newstuff) == 1)
3137  {
3138  /* Push the single Query back into the CTE node */
3139  ctequery = castNode(Query, linitial(newstuff));
3140  /* WITH queries should never be canSetTag */
3141  Assert(!ctequery->canSetTag);
3142  cte->ctequery = (Node *) ctequery;
3143  }
3144  else if (newstuff == NIL)
3145  {
3146  ereport(ERROR,
3147  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3148  errmsg("DO INSTEAD NOTHING rules are not supported for data-modifying statements in WITH")));
3149  }
3150  else
3151  {
3152  ListCell *lc2;
3153 
3154  /* examine queries to determine which error message to issue */
3155  foreach(lc2, newstuff)
3156  {
3157  Query *q = (Query *) lfirst(lc2);
3158 
3160  ereport(ERROR,
3161  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3162  errmsg("conditional DO INSTEAD rules are not supported for data-modifying statements in WITH")));
3164  ereport(ERROR,
3165  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3166  errmsg("DO ALSO rules are not supported for data-modifying statements in WITH")));
3167  }
3168 
3169  ereport(ERROR,
3170  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3171  errmsg("multi-statement DO INSTEAD rules are not supported for data-modifying statements in WITH")));
3172  }
3173  }
3174 
3175  /*
3176  * If the statement is an insert, update, or delete, adjust its targetlist
3177  * as needed, and then fire INSERT/UPDATE/DELETE rules on it.
3178  *
3179  * SELECT rules are handled later when we have all the queries that should
3180  * get executed. Also, utilities aren't rewritten at all (do we still
3181  * need that check?)
3182  */
3183  if (event != CMD_SELECT && event != CMD_UTILITY)
3184  {
3185  int result_relation;
3186  RangeTblEntry *rt_entry;
3187  Relation rt_entry_relation;
3188  List *locks;
3189  List *product_queries;
3190  bool hasUpdate = false;
3191 
3192  result_relation = parsetree->resultRelation;
3193  Assert(result_relation != 0);
3194  rt_entry = rt_fetch(result_relation, parsetree->rtable);
3195  Assert(rt_entry->rtekind == RTE_RELATION);
3196 
3197  /*
3198  * We can use NoLock here since either the parser or
3199  * AcquireRewriteLocks should have locked the rel already.
3200  */
3201  rt_entry_relation = heap_open(rt_entry->relid, NoLock);
3202 
3203  /*
3204  * Rewrite the targetlist as needed for the command type.
3205  */
3206  if (event == CMD_INSERT)
3207  {
3208  RangeTblEntry *values_rte = NULL;
3209 
3210  /*
3211  * If it's an INSERT ... VALUES (...), (...), ... there will be a
3212  * single RTE for the VALUES targetlists.
3213  */
3214  if (list_length(parsetree->jointree->fromlist) == 1)
3215  {
3216  RangeTblRef *rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
3217 
3218  if (IsA(rtr, RangeTblRef))
3219  {
3220  RangeTblEntry *rte = rt_fetch(rtr->rtindex,
3221  parsetree->rtable);
3222 
3223  if (rte->rtekind == RTE_VALUES)
3224  values_rte = rte;
3225  }
3226  }
3227 
3228  if (values_rte)
3229  {
3230  List *attrnos;
3231 
3232  /* Process the main targetlist ... */
3233  parsetree->targetList = rewriteTargetListIU(parsetree->targetList,
3234  parsetree->commandType,
3235  rt_entry_relation,
3236  parsetree->resultRelation,
3237  &attrnos);
3238  /* ... and the VALUES expression lists */
3239  rewriteValuesRTE(values_rte, rt_entry_relation, attrnos);
3240  }
3241  else
3242  {
3243  /* Process just the main targetlist */
3244  parsetree->targetList =
3245  rewriteTargetListIU(parsetree->targetList,
3246  parsetree->commandType,
3247  rt_entry_relation,
3248  parsetree->resultRelation, NULL);
3249  }
3250 
3251  if (parsetree->onConflict &&
3252  parsetree->onConflict->action == ONCONFLICT_UPDATE)
3253  {
3254  parsetree->onConflict->onConflictSet =
3256  CMD_UPDATE,
3257  rt_entry_relation,
3258  parsetree->resultRelation,
3259  NULL);
3260  }
3261  }
3262  else if (event == CMD_UPDATE)
3263  {
3264  parsetree->targetList =
3265  rewriteTargetListIU(parsetree->targetList,
3266  parsetree->commandType, rt_entry_relation,
3267  parsetree->resultRelation, NULL);
3268  rewriteTargetListUD(parsetree, rt_entry, rt_entry_relation);
3269  }
3270  else if (event == CMD_DELETE)
3271  {
3272  rewriteTargetListUD(parsetree, rt_entry, rt_entry_relation);
3273  }
3274  else
3275  elog(ERROR, "unrecognized commandType: %d", (int) event);
3276 
3277  /*
3278  * Collect and apply the appropriate rules.
3279  */
3280  locks = matchLocks(event, rt_entry_relation->rd_rules,
3281  result_relation, parsetree, &hasUpdate);
3282 
3283  product_queries = fireRules(parsetree,
3284  result_relation,
3285  event,
3286  locks,
3287  &instead,
3288  &returning,
3289  &qual_product);
3290 
3291  /*
3292  * If there were no INSTEAD rules, and the target relation is a view
3293  * without any INSTEAD OF triggers, see if the view can be
3294  * automatically updated. If so, we perform the necessary query
3295  * transformation here and add the resulting query to the
3296  * product_queries list, so that it gets recursively rewritten if
3297  * necessary.
3298  */
3299  if (!instead && qual_product == NULL &&
3300  rt_entry_relation->rd_rel->relkind == RELKIND_VIEW &&
3301  !view_has_instead_trigger(rt_entry_relation, event))
3302  {
3303  /*
3304  * This throws an error if the view can't be automatically
3305  * updated, but that's OK since the query would fail at runtime
3306  * anyway.
3307  */
3308  parsetree = rewriteTargetView(parsetree, rt_entry_relation);
3309 
3310  /*
3311  * At this point product_queries contains any DO ALSO rule
3312  * actions. Add the rewritten query before or after those. This
3313  * must match the handling the original query would have gotten
3314  * below, if we allowed it to be included again.
3315  */
3316  if (parsetree->commandType == CMD_INSERT)
3317  product_queries = lcons(parsetree, product_queries);
3318  else
3319  product_queries = lappend(product_queries, parsetree);
3320 
3321  /*
3322  * Set the "instead" flag, as if there had been an unqualified
3323  * INSTEAD, to prevent the original query from being included a
3324  * second time below. The transformation will have rewritten any
3325  * RETURNING list, so we can also set "returning" to forestall
3326  * throwing an error below.
3327  */
3328  instead = true;
3329  returning = true;
3330  updatableview = true;
3331  }
3332 
3333  /*
3334  * If we got any product queries, recursively rewrite them --- but
3335  * first check for recursion!
3336  */
3337  if (product_queries != NIL)
3338  {
3339  ListCell *n;
3340  rewrite_event *rev;
3341 
3342  foreach(n, rewrite_events)
3343  {
3344  rev = (rewrite_event *) lfirst(n);
3345  if (rev->relation == RelationGetRelid(rt_entry_relation) &&
3346  rev->event == event)
3347  ereport(ERROR,
3348  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3349  errmsg("infinite recursion detected in rules for relation \"%s\"",
3350  RelationGetRelationName(rt_entry_relation))));
3351  }
3352 
3353  rev = (rewrite_event *) palloc(sizeof(rewrite_event));
3354  rev->relation = RelationGetRelid(rt_entry_relation);
3355  rev->event = event;
3356  rewrite_events = lcons(rev, rewrite_events);
3357 
3358  foreach(n, product_queries)
3359  {
3360  Query *pt = (Query *) lfirst(n);
3361  List *newstuff;
3362 
3363  newstuff = RewriteQuery(pt, rewrite_events);
3364  rewritten = list_concat(rewritten, newstuff);
3365  }
3366 
3367  rewrite_events = list_delete_first(rewrite_events);
3368  }
3369 
3370  /*
3371  * If there is an INSTEAD, and the original query has a RETURNING, we
3372  * have to have found a RETURNING in the rule(s), else fail. (Because
3373  * DefineQueryRewrite only allows RETURNING in unconditional INSTEAD
3374  * rules, there's no need to worry whether the substituted RETURNING
3375  * will actually be executed --- it must be.)
3376  */
3377  if ((instead || qual_product != NULL) &&
3378  parsetree->returningList &&
3379  !returning)
3380  {
3381  switch (event)
3382  {
3383  case CMD_INSERT:
3384  ereport(ERROR,
3385  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3386  errmsg("cannot perform INSERT RETURNING on relation \"%s\"",
3387  RelationGetRelationName(rt_entry_relation)),
3388  errhint("You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.")));
3389  break;
3390  case CMD_UPDATE:
3391  ereport(ERROR,
3392  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3393  errmsg("cannot perform UPDATE RETURNING on relation \"%s\"",
3394  RelationGetRelationName(rt_entry_relation)),
3395  errhint("You need an unconditional ON UPDATE DO INSTEAD rule with a RETURNING clause.")));
3396  break;
3397  case CMD_DELETE:
3398  ereport(ERROR,
3399  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3400  errmsg("cannot perform DELETE RETURNING on relation \"%s\"",
3401  RelationGetRelationName(rt_entry_relation)),
3402  errhint("You need an unconditional ON DELETE DO INSTEAD rule with a RETURNING clause.")));
3403  break;
3404  default:
3405  elog(ERROR, "unrecognized commandType: %d",
3406  (int) event);
3407  break;
3408  }
3409  }
3410 
3411  /*
3412  * Updatable views are supported by ON CONFLICT, so don't prevent that
3413  * case from proceeding
3414  */
3415  if (parsetree->onConflict &&
3416  (product_queries != NIL || hasUpdate) &&
3417  !updatableview)
3418  ereport(ERROR,
3419  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3420  errmsg("INSERT with ON CONFLICT clause cannot be used with table that has INSERT or UPDATE rules")));
3421 
3422  heap_close(rt_entry_relation, NoLock);
3423  }
3424 
3425  /*
3426  * For INSERTs, the original query is done first; for UPDATE/DELETE, it is
3427  * done last. This is needed because update and delete rule actions might
3428  * not do anything if they are invoked after the update or delete is
3429  * performed. The command counter increment between the query executions
3430  * makes the deleted (and maybe the updated) tuples disappear so the scans
3431  * for them in the rule actions cannot find them.
3432  *
3433  * If we found any unqualified INSTEAD, the original query is not done at
3434  * all, in any form. Otherwise, we add the modified form if qualified
3435  * INSTEADs were found, else the unmodified form.
3436  */
3437  if (!instead)
3438  {
3439  if (parsetree->commandType == CMD_INSERT)
3440  {
3441  if (qual_product != NULL)
3442  rewritten = lcons(qual_product, rewritten);
3443  else
3444  rewritten = lcons(parsetree, rewritten);
3445  }
3446  else
3447  {
3448  if (qual_product != NULL)
3449  rewritten = lappend(rewritten, qual_product);
3450  else
3451  rewritten = lappend(rewritten, parsetree);
3452  }
3453  }
3454 
3455  /*
3456  * If the original query has a CTE list, and we generated more than one
3457  * non-utility result query, we have to fail because we'll have copied the
3458  * CTE list into each result query. That would break the expectation of
3459  * single evaluation of CTEs. This could possibly be fixed by
3460  * restructuring so that a CTE list can be shared across multiple Query
3461  * and PlannableStatement nodes.
3462  */
3463  if (parsetree->cteList != NIL)
3464  {
3465  int qcount = 0;
3466 
3467  foreach(lc1, rewritten)
3468  {
3469  Query *q = (Query *) lfirst(lc1);
3470 
3471  if (q->commandType != CMD_UTILITY)
3472  qcount++;
3473  }
3474  if (qcount > 1)
3475  ereport(ERROR,
3476  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3477  errmsg("WITH cannot be used in a query that is rewritten by rules into multiple queries")));
3478  }
3479 
3480  return rewritten;
3481 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
int errhint(const char *fmt,...)
Definition: elog.c:987
static bool view_has_instead_trigger(Relation view, CmdType event)
FromExpr * jointree
Definition: parsenodes.h:129
OnConflictExpr * onConflict
Definition: parsenodes.h:133
#define castNode(_type_, nodeptr)
Definition: nodes.h:587
int resultRelation
Definition: parsenodes.h:113
Definition: nodes.h:518
static List * RewriteQuery(Query *parsetree, List *rewrite_events)
int errcode(int sqlerrcode)
Definition: elog.c:575
List * list_concat(List *list1, List *list2)
Definition: list.c:321
static void rewriteValuesRTE(RangeTblEntry *rte, Relation target_relation, List *attrnos)
List * fromlist
Definition: primnodes.h:1455
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:113
static List * rewriteTargetListIU(List *targetList, CmdType commandType, Relation target_relation, int result_rti, List **attrno_list)
List * targetList
Definition: parsenodes.h:131
#define linitial(l)
Definition: pg_list.h:110
List * rtable
Definition: parsenodes.h:128
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
static Query * rewriteTargetView(Query *parsetree, Relation view)
#define RelationGetRelationName(relation)
Definition: rel.h:433
List * returningList
Definition: parsenodes.h:135
#define ereport(elevel, rest)
Definition: elog.h:122
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * lappend(List *list, void *datum)
Definition: list.c:128
OnConflictAction action
Definition: primnodes.h:1471
static List * fireRules(Query *parsetree, int rt_index, CmdType event, List *locks, bool *instead_flag, bool *returning_flag, Query **qual_product)
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
CmdType commandType
Definition: parsenodes.h:103
List * lcons(void *datum, List *list)
Definition: list.c:259
#define NULL
Definition: c.h:229
QuerySource querySource
Definition: parsenodes.h:105
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
RuleLock * rd_rules
Definition: rel.h:117
bool canSetTag
Definition: parsenodes.h:109
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:916
List * cteList
Definition: parsenodes.h:126
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define RELKIND_VIEW
Definition: pg_class.h:164
List * onConflictSet
Definition: primnodes.h:1480
static void rewriteTargetListUD(Query *parsetree, RangeTblEntry *target_rte, Relation target_relation)
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
static List * matchLocks(CmdType event, RuleLock *rulelocks, int varno, Query *parsetree, bool *hasUpdate)
#define RelationGetRelid(relation)
Definition: rel.h:413
CmdType
Definition: nodes.h:651
List * list_delete_first(List *list)
Definition: list.c:666
static Query * rewriteRuleAction ( Query parsetree,
Query rule_action,
Node rule_qual,
int  rt_index,
CmdType  event,
bool returning_flag 
)
static

Definition at line 332 of file rewriteHandler.c.

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

Referenced by fireRules().

338 {
339  int current_varno,
340  new_varno;
341  int rt_length;
342  Query *sub_action;
343  Query **sub_action_ptr;
345 
346  context.for_execute = true;
347 
348  /*
349  * Make modifiable copies of rule action and qual (what we're passed are
350  * the stored versions in the relcache; don't touch 'em!).
351  */
352  rule_action = (Query *) copyObject(rule_action);
353  rule_qual = (Node *) copyObject(rule_qual);
354 
355  /*
356  * Acquire necessary locks and fix any deleted JOIN RTE entries.
357  */
358  AcquireRewriteLocks(rule_action, true, false);
359  (void) acquireLocksOnSubLinks(rule_qual, &context);
360 
361  current_varno = rt_index;
362  rt_length = list_length(parsetree->rtable);
363  new_varno = PRS2_NEW_VARNO + rt_length;
364 
365  /*
366  * Adjust rule action and qual to offset its varnos, so that we can merge
367  * its rtable with the main parsetree's rtable.
368  *
369  * If the rule action is an INSERT...SELECT, the OLD/NEW rtable entries
370  * will be in the SELECT part, and we have to modify that rather than the
371  * top-level INSERT (kluge!).
372  */
373  sub_action = getInsertSelectQuery(rule_action, &sub_action_ptr);
374 
375  OffsetVarNodes((Node *) sub_action, rt_length, 0);
376  OffsetVarNodes(rule_qual, rt_length, 0);
377  /* but references to OLD should point at original rt_index */
378  ChangeVarNodes((Node *) sub_action,
379  PRS2_OLD_VARNO + rt_length, rt_index, 0);
380  ChangeVarNodes(rule_qual,
381  PRS2_OLD_VARNO + rt_length, rt_index, 0);
382 
383  /*
384  * Generate expanded rtable consisting of main parsetree's rtable plus
385  * rule action's rtable; this becomes the complete rtable for the rule
386  * action. Some of the entries may be unused after we finish rewriting,
387  * but we leave them all in place for two reasons:
388  *
389  * We'd have a much harder job to adjust the query's varnos if we
390  * selectively removed RT entries.
391  *
392  * If the rule is INSTEAD, then the original query won't be executed at
393  * all, and so its rtable must be preserved so that the executor will do
394  * the correct permissions checks on it.
395  *
396  * RT entries that are not referenced in the completed jointree will be
397  * ignored by the planner, so they do not affect query semantics. But any
398  * permissions checks specified in them will be applied during executor
399  * startup (see ExecCheckRTEPerms()). This allows us to check that the
400  * caller has, say, insert-permission on a view, when the view is not
401  * semantically referenced at all in the resulting query.
402  *
403  * When a rule is not INSTEAD, the permissions checks done on its copied
404  * RT entries will be redundant with those done during execution of the
405  * original query, but we don't bother to treat that case differently.
406  *
407  * NOTE: because planner will destructively alter rtable, we must ensure
408  * that rule action's rtable is separate and shares no substructure with
409  * the main rtable. Hence do a deep copy here.
410  */
411  sub_action->rtable = list_concat((List *) copyObject(parsetree->rtable),
412  sub_action->rtable);
413 
414  /*
415  * There could have been some SubLinks in parsetree's rtable, in which
416  * case we'd better mark the sub_action correctly.
417  */
418  if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
419  {
420  ListCell *lc;
421 
422  foreach(lc, parsetree->rtable)
423  {
424  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
425 
426  switch (rte->rtekind)
427  {
428  case RTE_RELATION:
429  sub_action->hasSubLinks =
431  break;
432  case RTE_FUNCTION:
433  sub_action->hasSubLinks =
435  break;
436  case RTE_TABLEFUNC:
437  sub_action->hasSubLinks =
439  break;
440  case RTE_VALUES:
441  sub_action->hasSubLinks =
443  break;
444  default:
445  /* other RTE types don't contain bare expressions */
446  break;
447  }
448  if (sub_action->hasSubLinks)
449  break; /* no need to keep scanning rtable */
450  }
451  }
452 
453  /*
454  * Also, we might have absorbed some RTEs with RLS conditions into the
455  * sub_action. If so, mark it as hasRowSecurity, whether or not those
456  * RTEs will be referenced after we finish rewriting. (Note: currently
457  * this is a no-op because RLS conditions aren't added till later, but it
458  * seems like good future-proofing to do this anyway.)
459  */
460  sub_action->hasRowSecurity |= parsetree->hasRowSecurity;
461 
462  /*
463  * Each rule action's jointree should be the main parsetree's jointree
464  * plus that rule's jointree, but usually *without* the original rtindex
465  * that we're replacing (if present, which it won't be for INSERT). Note
466  * that if the rule action refers to OLD, its jointree will add a
467  * reference to rt_index. If the rule action doesn't refer to OLD, but
468  * either the rule_qual or the user query quals do, then we need to keep
469  * the original rtindex in the jointree to provide data for the quals. We
470  * don't want the original rtindex to be joined twice, however, so avoid
471  * keeping it if the rule action mentions it.
472  *
473  * As above, the action's jointree must not share substructure with the
474  * main parsetree's.
475  */
476  if (sub_action->commandType != CMD_UTILITY)
477  {
478  bool keeporig;
479  List *newjointree;
480 
481  Assert(sub_action->jointree != NULL);
482  keeporig = (!rangeTableEntry_used((Node *) sub_action->jointree,
483  rt_index, 0)) &&
484  (rangeTableEntry_used(rule_qual, rt_index, 0) ||
485  rangeTableEntry_used(parsetree->jointree->quals, rt_index, 0));
486  newjointree = adjustJoinTreeList(parsetree, !keeporig, rt_index);
487  if (newjointree != NIL)
488  {
489  /*
490  * If sub_action is a setop, manipulating its jointree will do no
491  * good at all, because the jointree is dummy. (Perhaps someday
492  * we could push the joining and quals down to the member
493  * statements of the setop?)
494  */
495  if (sub_action->setOperations != NULL)
496  ereport(ERROR,
497  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
498  errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
499 
500  sub_action->jointree->fromlist =
501  list_concat(newjointree, sub_action->jointree->fromlist);
502 
503  /*
504  * There could have been some SubLinks in newjointree, in which
505  * case we'd better mark the sub_action correctly.
506  */
507  if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
508  sub_action->hasSubLinks =
509  checkExprHasSubLink((Node *) newjointree);
510  }
511  }
512 
513  /*
514  * If the original query has any CTEs, copy them into the rule action. But
515  * we don't need them for a utility action.
516  */
517  if (parsetree->cteList != NIL && sub_action->commandType != CMD_UTILITY)
518  {
519  ListCell *lc;
520 
521  /*
522  * Annoying implementation restriction: because CTEs are identified by
523  * name within a cteList, we can't merge a CTE from the original query
524  * if it has the same name as any CTE in the rule action.
525  *
526  * This could possibly be fixed by using some sort of internally
527  * generated ID, instead of names, to link CTE RTEs to their CTEs.
528  */
529  foreach(lc, parsetree->cteList)
530  {
531  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
532  ListCell *lc2;
533 
534  foreach(lc2, sub_action->cteList)
535  {
536  CommonTableExpr *cte2 = (CommonTableExpr *) lfirst(lc2);
537 
538  if (strcmp(cte->ctename, cte2->ctename) == 0)
539  ereport(ERROR,
540  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
541  errmsg("WITH query name \"%s\" appears in both a rule action and the query being rewritten",
542  cte->ctename)));
543  }
544  }
545 
546  /* OK, it's safe to combine the CTE lists */
547  sub_action->cteList = list_concat(sub_action->cteList,
548  copyObject(parsetree->cteList));
549  }
550 
551  /*
552  * Event Qualification forces copying of parsetree and splitting into two
553  * queries one w/rule_qual, one w/NOT rule_qual. Also add user query qual
554  * onto rule action
555  */
556  AddQual(sub_action, rule_qual);
557 
558  AddQual(sub_action, parsetree->jointree->quals);
559 
560  /*
561  * Rewrite new.attribute with right hand side of target-list entry for
562  * appropriate field name in insert/update.
563  *
564  * KLUGE ALERT: since ReplaceVarsFromTargetList returns a mutated copy, we
565  * can't just apply it to sub_action; we have to remember to update the
566  * sublink inside rule_action, too.
567  */
568  if ((event == CMD_INSERT || event == CMD_UPDATE) &&
569  sub_action->commandType != CMD_UTILITY)
570  {
571  sub_action = (Query *)
572  ReplaceVarsFromTargetList((Node *) sub_action,
573  new_varno,
574  0,
575  rt_fetch(new_varno, sub_action->rtable),
576  parsetree->targetList,
577  (event == CMD_UPDATE) ?
580  current_varno,
581  NULL);
582  if (sub_action_ptr)
583  *sub_action_ptr = sub_action;
584  else
585  rule_action = sub_action;
586  }
587 
588  /*
589  * If rule_action has a RETURNING clause, then either throw it away if the
590  * triggering query has no RETURNING clause, or rewrite it to emit what
591  * the triggering query's RETURNING clause asks for. Throw an error if
592  * more than one rule has a RETURNING clause.
593  */
594  if (!parsetree->returningList)
595  rule_action->returningList = NIL;
596  else if (rule_action->returningList)
597  {
598  if (*returning_flag)
599  ereport(ERROR,
600  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
601  errmsg("cannot have RETURNING lists in multiple rules")));
602  *returning_flag = true;
603  rule_action->returningList = (List *)
605  parsetree->resultRelation,
606  0,
607  rt_fetch(parsetree->resultRelation,
608  parsetree->rtable),
609  rule_action->returningList,
611  0,
612  &rule_action->hasSubLinks);
613 
614  /*
615  * There could have been some SubLinks in parsetree's returningList,
616  * in which case we'd better mark the rule_action correctly.
617  */
618  if (parsetree->hasSubLinks && !rule_action->hasSubLinks)
619  rule_action->hasSubLinks =
620  checkExprHasSubLink((Node *) rule_action->returningList);
621  }
622 
623  return rule_action;
624 }
#define NIL
Definition: pg_list.h:69
Query * getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
Definition: rewriteManip.c:921
void OffsetVarNodes(Node *node, int offset, int sublevels_up)
Definition: rewriteManip.c:424
static bool acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context)
FromExpr * jointree
Definition: parsenodes.h:129
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:277
int resultRelation
Definition: parsenodes.h:113
Definition: nodes.h:518
int errcode(int sqlerrcode)
Definition: elog.c:575
static List * adjustJoinTreeList(Query *parsetree, bool removert, int rt_index)
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * fromlist
Definition: primnodes.h:1455
List * values_lists
Definition: parsenodes.h:976
Node * quals
Definition: primnodes.h:1456
List * targetList
Definition: parsenodes.h:131
void * copyObject(const void *from)
Definition: copyfuncs.c:4592
List * rtable
Definition: parsenodes.h:128
#define ERROR
Definition: elog.h:43
TableFunc * tablefunc
Definition: parsenodes.h:971
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
List * returningList
Definition: parsenodes.h:135
#define ereport(elevel, rest)
Definition: elog.h:122
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define PRS2_OLD_VARNO
Definition: primnodes.h:160
Node * ReplaceVarsFromTargetList(Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
CmdType commandType
Definition: parsenodes.h:103
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
List * functions
Definition: parsenodes.h:965
void AddQual(Query *parsetree, Node *qual)
Definition: rewriteManip.c:976
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:916
List * cteList
Definition: parsenodes.h:126
Node * setOperations
Definition: parsenodes.h:154
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool hasSubLinks
Definition: parsenodes.h:119
void ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
Definition: rewriteManip.c:607
bool rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
Definition: rewriteManip.c:889
Definition: pg_list.h:45
struct TableSampleClause * tablesample
Definition: parsenodes.h:929
#define PRS2_NEW_VARNO
Definition: primnodes.h:161
bool hasRowSecurity
Definition: parsenodes.h:124
static List * rewriteTargetListIU ( List targetList,
CmdType  commandType,
Relation  target_relation,
int  result_rti,
List **  attrno_list 
)
static

Definition at line 710 of file rewriteHandler.c.

References tupleDesc::attrs, build_column_default(), CMD_INSERT, CMD_UPDATE, COERCE_IMPLICIT_CAST, coerce_to_domain(), elog, ERROR, TargetEntry::expr, flatCopyTargetEntry(), InvalidOid, IsA, lappend(), lappend_int(), lfirst, list_concat(), makeConst(), makeTargetEntry(), makeVar(), NameStr, NIL, NULL, palloc0(), pfree(), process_matched_tle(), pstrdup(), RelationData::rd_att, RelationData::rd_rel, RelationGetNumberOfAttributes, RELKIND_VIEW, TargetEntry::resjunk, TargetEntry::resno, and view_has_instead_trigger().

Referenced by RewriteQuery().

715 {
716  TargetEntry **new_tles;
717  List *new_tlist = NIL;
718  List *junk_tlist = NIL;
719  Form_pg_attribute att_tup;
720  int attrno,
721  next_junk_attrno,
722  numattrs;
723  ListCell *temp;
724 
725  if (attrno_list) /* initialize optional result list */
726  *attrno_list = NIL;
727 
728  /*
729  * We process the normal (non-junk) attributes by scanning the input tlist
730  * once and transferring TLEs into an array, then scanning the array to
731  * build an output tlist. This avoids O(N^2) behavior for large numbers
732  * of attributes.
733  *
734  * Junk attributes are tossed into a separate list during the same tlist
735  * scan, then appended to the reconstructed tlist.
736  */
737  numattrs = RelationGetNumberOfAttributes(target_relation);
738  new_tles = (TargetEntry **) palloc0(numattrs * sizeof(TargetEntry *));
739  next_junk_attrno = numattrs + 1;
740 
741  foreach(temp, targetList)
742  {
743  TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
744 
745  if (!old_tle->resjunk)
746  {
747  /* Normal attr: stash it into new_tles[] */
748  attrno = old_tle->resno;
749  if (attrno < 1 || attrno > numattrs)
750  elog(ERROR, "bogus resno %d in targetlist", attrno);
751  att_tup = target_relation->rd_att->attrs[attrno - 1];
752 
753  /* put attrno into attrno_list even if it's dropped */
754  if (attrno_list)
755  *attrno_list = lappend_int(*attrno_list, attrno);
756 
757  /* We can (and must) ignore deleted attributes */
758  if (att_tup->attisdropped)
759  continue;
760 
761  /* Merge with any prior assignment to same attribute */
762  new_tles[attrno - 1] =
763  process_matched_tle(old_tle,
764  new_tles[attrno - 1],
765  NameStr(att_tup->attname));
766  }
767  else
768  {
769  /*
770  * Copy all resjunk tlist entries to junk_tlist, and assign them
771  * resnos above the last real resno.
772  *
773  * Typical junk entries include ORDER BY or GROUP BY expressions
774  * (are these actually possible in an INSERT or UPDATE?), system
775  * attribute references, etc.
776  */
777 
778  /* Get the resno right, but don't copy unnecessarily */
779  if (old_tle->resno != next_junk_attrno)
780  {
781  old_tle = flatCopyTargetEntry(old_tle);
782  old_tle->resno = next_junk_attrno;
783  }
784  junk_tlist = lappend(junk_tlist, old_tle);
785  next_junk_attrno++;
786  }
787  }
788 
789  for (attrno = 1; attrno <= numattrs; attrno++)
790  {
791  TargetEntry *new_tle = new_tles[attrno - 1];
792 
793  att_tup = target_relation->rd_att->attrs[attrno - 1];
794 
795  /* We can (and must) ignore deleted attributes */
796  if (att_tup->attisdropped)
797  continue;
798 
799  /*
800  * Handle the two cases where we need to insert a default expression:
801  * it's an INSERT and there's no tlist entry for the column, or the
802  * tlist entry is a DEFAULT placeholder node.
803  */
804  if ((new_tle == NULL && commandType == CMD_INSERT) ||
805  (new_tle && new_tle->expr && IsA(new_tle->expr, SetToDefault)))
806  {
807  Node *new_expr;
808 
809  new_expr = build_column_default(target_relation, attrno);
810 
811  /*
812  * If there is no default (ie, default is effectively NULL), we
813  * can omit the tlist entry in the INSERT case, since the planner
814  * can insert a NULL for itself, and there's no point in spending
815  * any more rewriter cycles on the entry. But in the UPDATE case
816  * we've got to explicitly set the column to NULL.
817  */
818  if (!new_expr)
819  {
820  if (commandType == CMD_INSERT)
821  new_tle = NULL;
822  else
823  {
824  new_expr = (Node *) makeConst(att_tup->atttypid,
825  -1,
826  att_tup->attcollation,
827  att_tup->attlen,
828  (Datum) 0,
829  true, /* isnull */
830  att_tup->attbyval);
831  /* this is to catch a NOT NULL domain constraint */
832  new_expr = coerce_to_domain(new_expr,
833  InvalidOid, -1,
834  att_tup->atttypid,
836  -1,
837  false,
838  false);
839  }
840  }
841 
842  if (new_expr)
843  new_tle = makeTargetEntry((Expr *) new_expr,
844  attrno,
845  pstrdup(NameStr(att_tup->attname)),
846  false);
847  }
848 
849  /*
850  * For an UPDATE on a trigger-updatable view, provide a dummy entry
851  * whenever there is no explicit assignment.
852  */
853  if (new_tle == NULL && commandType == CMD_UPDATE &&
854  target_relation->rd_rel->relkind == RELKIND_VIEW &&
855  view_has_instead_trigger(target_relation, CMD_UPDATE))
856  {
857  Node *new_expr;
858 
859  new_expr = (Node *) makeVar(result_rti,
860  attrno,
861  att_tup->atttypid,
862  att_tup->atttypmod,
863  att_tup->attcollation,
864  0);
865 
866  new_tle = makeTargetEntry((Expr *) new_expr,
867  attrno,
868  pstrdup(NameStr(att_tup->attname)),
869  false);
870  }
871 
872  if (new_tle)
873  new_tlist = lappend(new_tlist, new_tle);
874  }
875 
876  pfree(new_tles);
877 
878  return list_concat(new_tlist, junk_tlist);
879 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
static bool view_has_instead_trigger(Relation view, CmdType event)
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:419
char * pstrdup(const char *in)
Definition: mcxt.c:1077
Form_pg_attribute * attrs
Definition: tupdesc.h:74
Definition: nodes.h:518
List * list_concat(List *list1, List *list2)
Definition: list.c:321
Form_pg_class rd_rel
Definition: rel.h:113
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:296
void pfree(void *pointer)
Definition: mcxt.c:950
bool resjunk
Definition: primnodes.h:1359
#define ERROR
Definition: elog.h:43
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionForm cformat, int location, bool hideInputCoercion, bool lengthCoercionDone)
Definition: parse_coerce.c:648
AttrNumber resno
Definition: primnodes.h:1353
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition: makefuncs.c:268
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * build_column_default(Relation rel, int attrno)
void * palloc0(Size size)
Definition: mcxt.c:878
uintptr_t Datum
Definition: postgres.h:372
TupleDesc rd_att
Definition: rel.h:114
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1352
#define RELKIND_VIEW
Definition: pg_class.h:164
static TargetEntry * process_matched_tle(TargetEntry *src_tle, TargetEntry *prior_tle, const char *attrName)
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
static void rewriteTargetListUD ( Query parsetree,
RangeTblEntry target_rte,
Relation  target_relation 
)
static

Definition at line 1230 of file rewriteHandler.c.

References FdwRoutine::AddForeignUpdateTargets, CMD_DELETE, CMD_UPDATE, Query::commandType, GetFdwRoutineForRelation(), InvalidOid, lappend(), list_length(), makeTargetEntry(), makeVar(), makeWholeRowVar(), NULL, pstrdup(), RelationData::rd_rel, RELKIND_FOREIGN_TABLE, RELKIND_MATVIEW, RELKIND_PARTITIONED_TABLE, RELKIND_RELATION, Query::resultRelation, SelfItemPointerAttributeNumber, Query::targetList, TIDOID, TriggerDesc::trig_delete_after_row, TriggerDesc::trig_delete_before_row, TriggerDesc::trig_update_after_row, TriggerDesc::trig_update_before_row, and RelationData::trigdesc.

Referenced by RewriteQuery().

1232 {
1233  Var *var = NULL;
1234  const char *attrname;
1235  TargetEntry *tle;
1236 
1237  if (target_relation->rd_rel->relkind == RELKIND_RELATION ||
1238  target_relation->rd_rel->relkind == RELKIND_MATVIEW ||
1239  target_relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1240  {
1241  /*
1242  * Emit CTID so that executor can find the row to update or delete.
1243  */
1244  var = makeVar(parsetree->resultRelation,
1246  TIDOID,
1247  -1,
1248  InvalidOid,
1249  0);
1250 
1251  attrname = "ctid";
1252  }
1253  else if (target_relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1254  {
1255  /*
1256  * Let the foreign table's FDW add whatever junk TLEs it wants.
1257  */
1258  FdwRoutine *fdwroutine;
1259 
1260  fdwroutine = GetFdwRoutineForRelation(target_relation, false);
1261 
1262  if (fdwroutine->AddForeignUpdateTargets != NULL)
1263  fdwroutine->AddForeignUpdateTargets(parsetree, target_rte,
1264  target_relation);
1265 
1266  /*
1267  * If we have a row-level trigger corresponding to the operation, emit
1268  * a whole-row Var so that executor will have the "old" row to pass to
1269  * the trigger. Alas, this misses system columns.
1270  */
1271  if (target_relation->trigdesc &&
1272  ((parsetree->commandType == CMD_UPDATE &&
1273  (target_relation->trigdesc->trig_update_after_row ||
1274  target_relation->trigdesc->trig_update_before_row)) ||
1275  (parsetree->commandType == CMD_DELETE &&
1276  (target_relation->trigdesc->trig_delete_after_row ||
1277  target_relation->trigdesc->trig_delete_before_row))))
1278  {
1279  var = makeWholeRowVar(target_rte,
1280  parsetree->resultRelation,
1281  0,
1282  false);
1283 
1284  attrname = "wholerow";
1285  }
1286  }
1287  else
1288  {
1289  /*
1290  * Emit whole-row Var so that executor will have the "old" view row to
1291  * pass to the INSTEAD OF trigger.
1292  */
1293  var = makeWholeRowVar(target_rte,
1294  parsetree->resultRelation,
1295  0,
1296  false);
1297 
1298  attrname = "wholerow";
1299  }
1300 
1301  if (var != NULL)
1302  {
1303  tle = makeTargetEntry((Expr *) var,
1304  list_length(parsetree->targetList) + 1,
1305  pstrdup(attrname),
1306  true);
1307 
1308  parsetree->targetList = lappend(parsetree->targetList, tle);
1309  }
1310 }
char * pstrdup(const char *in)
Definition: mcxt.c:1077
int resultRelation
Definition: parsenodes.h:113
#define RELKIND_MATVIEW
Definition: pg_class.h:165
Var * makeWholeRowVar(RangeTblEntry *rte, Index varno, Index varlevelsup, bool allowScalar)
Definition: makefuncs.c:132
Form_pg_class rd_rel
Definition: rel.h:113
Definition: primnodes.h:163
AddForeignUpdateTargets_function AddForeignUpdateTargets
Definition: fdwapi.h:194
List * targetList
Definition: parsenodes.h:131
#define TIDOID
Definition: pg_type.h:332
TriggerDesc * trigdesc
Definition: rel.h:119
bool trig_delete_after_row
Definition: reltrigger.h:66
bool trig_update_before_row
Definition: reltrigger.h:60
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
bool trig_update_after_row
Definition: reltrigger.h:61
List * lappend(List *list, void *datum)
Definition: list.c:128
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
#define InvalidOid
Definition: postgres_ext.h:36
CmdType commandType
Definition: parsenodes.h:103
#define NULL
Definition: c.h:229
static int list_length(const List *l)
Definition: pg_list.h:89
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:395
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
#define RELKIND_RELATION
Definition: pg_class.h:160
bool trig_delete_before_row
Definition: reltrigger.h:65
static Query* rewriteTargetView ( Query parsetree,
Relation  view 
)
static

Definition at line 2625 of file rewriteHandler.c.

References _, acquireLocksOnSubLinks(), AddQual(), adjust_view_column_set(), Assert, bms_add_member(), bms_is_empty(), WithCheckOption::cascaded, castNode, ChangeVarNodes(), RangeTblEntry::checkAsUser, checkExprHasSubLink(), CMD_DELETE, CMD_INSERT, CMD_UPDATE, Query::commandType, copyObject(), elog, ereport, errcode(), errdetail_internal(), errhint(), errmsg(), ERROR, TargetEntry::expr, FirstLowInvalidHeapAttributeNumber, acquireLocksOnSubLinks_context::for_execute, FromExpr::fromlist, get_tle_by_resno(), get_view_query(), Query::hasSubLinks, heap_close, heap_open(), RangeTblEntry::inh, RangeTblEntry::insertedCols, IsA, Query::jointree, WithCheckOption::kind, lappend(), lcons(), lfirst, linitial, list_delete_ptr(), list_length(), llast, makeNode, NIL, NoLock, NULL, Query::onConflict, OnConflictExpr::onConflictSet, WithCheckOption::polname, pstrdup(), QTW_IGNORE_RC_SUBQUERIES, WithCheckOption::qual, FromExpr::quals, query_tree_walker(), RelationData::rd_rel, RelationGetRelationName, RelationHasCascadedCheckOption, RelationHasCheckOption, RelationIsSecurityView, RangeTblEntry::relid, RangeTblEntry::relkind, WithCheckOption::relname, REPLACEVARS_REPORT_ERROR, ReplaceVarsFromTargetList(), RangeTblEntry::requiredPerms, TargetEntry::resjunk, TargetEntry::resno, Query::resultRelation, RowExclusiveLock, rt_fetch, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, RangeTblRef::rtindex, RangeTblEntry::securityQuals, Query::targetList, RangeTblEntry::updatedCols, view_cols_are_auto_updatable(), view_query_is_auto_updatable(), WCO_VIEW_CHECK, and Query::withCheckOptions.

Referenced by RewriteQuery().

2626 {
2627  Query *viewquery;
2628  const char *auto_update_detail;
2629  RangeTblRef *rtr;
2630  int base_rt_index;
2631  int new_rt_index;
2632  RangeTblEntry *base_rte;
2633  RangeTblEntry *view_rte;
2634  RangeTblEntry *new_rte;
2635  Relation base_rel;
2636  List *view_targetlist;
2637  ListCell *lc;
2638 
2639  /*
2640  * Get the Query from the view's ON SELECT rule. We're going to munge the
2641  * Query to change the view's base relation into the target relation,
2642  * along with various other changes along the way, so we need to make a
2643  * copy of it (get_view_query() returns a pointer into the relcache, so we
2644  * have to treat it as read-only).
2645  */
2646  viewquery = copyObject(get_view_query(view));
2647 
2648  /* The view must be updatable, else fail */
2649  auto_update_detail =
2650  view_query_is_auto_updatable(viewquery,
2651  parsetree->commandType != CMD_DELETE);
2652 
2653  if (auto_update_detail)
2654  {
2655  /* messages here should match execMain.c's CheckValidResultRel */
2656  switch (parsetree->commandType)
2657  {
2658  case CMD_INSERT:
2659  ereport(ERROR,
2660  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2661  errmsg("cannot insert into view \"%s\"",
2662  RelationGetRelationName(view)),
2663  errdetail_internal("%s", _(auto_update_detail)),
2664  errhint("To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.")));
2665  break;
2666  case CMD_UPDATE:
2667  ereport(ERROR,
2668  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2669  errmsg("cannot update view \"%s\"",
2670  RelationGetRelationName(view)),
2671  errdetail_internal("%s", _(auto_update_detail)),
2672  errhint("To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.")));
2673  break;
2674  case CMD_DELETE:
2675  ereport(ERROR,
2676  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2677  errmsg("cannot delete from view \"%s\"",
2678  RelationGetRelationName(view)),
2679  errdetail_internal("%s", _(auto_update_detail)),
2680  errhint("To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.")));
2681  break;
2682  default:
2683  elog(ERROR, "unrecognized CmdType: %d",
2684  (int) parsetree->commandType);
2685  break;
2686  }
2687  }
2688 
2689  /*
2690  * For INSERT/UPDATE the modified columns must all be updatable. Note that
2691  * we get the modified columns from the query's targetlist, not from the
2692  * result RTE's insertedCols and/or updatedCols set, since
2693  * rewriteTargetListIU may have added additional targetlist entries for
2694  * view defaults, and these must also be updatable.
2695  */
2696  if (parsetree->commandType != CMD_DELETE)
2697  {
2698  Bitmapset *modified_cols = NULL;
2699  char *non_updatable_col;
2700 
2701  foreach(lc, parsetree->targetList)
2702  {
2703  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2704 
2705  if (!tle->resjunk)
2706  modified_cols = bms_add_member(modified_cols,
2708  }
2709 
2710  if (parsetree->onConflict)
2711  {
2712  foreach(lc, parsetree->onConflict->onConflictSet)
2713  {
2714  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2715 
2716  if (!tle->resjunk)
2717  modified_cols = bms_add_member(modified_cols,
2719  }
2720  }
2721 
2722  auto_update_detail = view_cols_are_auto_updatable(viewquery,
2723  modified_cols,
2724  NULL,
2725  &non_updatable_col);
2726  if (auto_update_detail)
2727  {
2728  /*
2729  * This is a different error, caused by an attempt to update a
2730  * non-updatable column in an otherwise updatable view.
2731  */
2732  switch (parsetree->commandType)
2733  {
2734  case CMD_INSERT:
2735  ereport(ERROR,
2736  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2737  errmsg("cannot insert into column \"%s\" of view \"%s\"",
2738  non_updatable_col,
2739  RelationGetRelationName(view)),
2740  errdetail_internal("%s", _(auto_update_detail))));
2741  break;
2742  case CMD_UPDATE:
2743  ereport(ERROR,
2744  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2745  errmsg("cannot update column \"%s\" of view \"%s\"",
2746  non_updatable_col,
2747  RelationGetRelationName(view)),
2748  errdetail_internal("%s", _(auto_update_detail))));
2749  break;
2750  default:
2751  elog(ERROR, "unrecognized CmdType: %d",
2752  (int) parsetree->commandType);
2753  break;
2754  }
2755  }
2756  }
2757 
2758  /* Locate RTE describing the view in the outer query */
2759  view_rte = rt_fetch(parsetree->resultRelation, parsetree->rtable);
2760 
2761  /*
2762  * If we get here, view_query_is_auto_updatable() has verified that the
2763  * view contains a single base relation.
2764  */
2765  Assert(list_length(viewquery->jointree->fromlist) == 1);
2766  rtr = castNode(RangeTblRef, linitial(viewquery->jointree->fromlist));
2767 
2768  base_rt_index = rtr->rtindex;
2769  base_rte = rt_fetch(base_rt_index, viewquery->rtable);
2770  Assert(base_rte->rtekind == RTE_RELATION);
2771 
2772  /*
2773  * Up to now, the base relation hasn't been touched at all in our query.
2774  * We need to acquire lock on it before we try to do anything with it.
2775  * (The subsequent recursive call of RewriteQuery will suppose that we
2776  * already have the right lock!) Since it will become the query target
2777  * relation, RowExclusiveLock is always the right thing.
2778  */
2779  base_rel = heap_open(base_rte->relid, RowExclusiveLock);
2780 
2781  /*
2782  * While we have the relation open, update the RTE's relkind, just in case
2783  * it changed since this view was made (cf. AcquireRewriteLocks).
2784  */
2785  base_rte->relkind = base_rel->rd_rel->relkind;
2786 
2787  heap_close(base_rel, NoLock);
2788 
2789  /*
2790  * If the view query contains any sublink subqueries then we need to also
2791  * acquire locks on any relations they refer to. We know that there won't
2792  * be any subqueries in the range table or CTEs, so we can skip those, as
2793  * in AcquireRewriteLocks.
2794  */
2795  if (viewquery->hasSubLinks)
2796  {
2798 
2799  context.for_execute = true;
2800  query_tree_walker(viewquery, acquireLocksOnSubLinks, &context,
2802  }
2803 
2804  /*
2805  * Create a new target RTE describing the base relation, and add it to the
2806  * outer query's rangetable. (What's happening in the next few steps is
2807  * very much like what the planner would do to "pull up" the view into the
2808  * outer query. Perhaps someday we should refactor things enough so that
2809  * we can share code with the planner.)
2810  */
2811  new_rte = (RangeTblEntry *) base_rte;
2812  parsetree->rtable = lappend(parsetree->rtable, new_rte);
2813  new_rt_index = list_length(parsetree->rtable);
2814 
2815  /*
2816  * INSERTs never inherit. For UPDATE/DELETE, we use the view query's
2817  * inheritance flag for the base relation.
2818  */
2819  if (parsetree->commandType == CMD_INSERT)
2820  new_rte->inh = false;
2821 
2822  /*
2823  * Adjust the view's targetlist Vars to reference the new target RTE, ie
2824  * make their varnos be new_rt_index instead of base_rt_index. There can
2825  * be no Vars for other rels in the tlist, so this is sufficient to pull
2826  * up the tlist expressions for use in the outer query. The tlist will
2827  * provide the replacement expressions used by ReplaceVarsFromTargetList
2828  * below.
2829  */
2830  view_targetlist = viewquery->targetList;
2831 
2832  ChangeVarNodes((Node *) view_targetlist,
2833  base_rt_index,
2834  new_rt_index,
2835  0);
2836 
2837  /*
2838  * Mark the new target RTE for the permissions checks that we want to
2839  * enforce against the view owner, as distinct from the query caller. At
2840  * the relation level, require the same INSERT/UPDATE/DELETE permissions
2841  * that the query caller needs against the view. We drop the ACL_SELECT
2842  * bit that is presumably in new_rte->requiredPerms initially.
2843  *
2844  * Note: the original view RTE remains in the query's rangetable list.
2845  * Although it will be unused in the query plan, we need it there so that
2846  * the executor still performs appropriate permissions checks for the
2847  * query caller's use of the view.
2848  */
2849  new_rte->checkAsUser = view->rd_rel->relowner;
2850  new_rte->requiredPerms = view_rte->requiredPerms;
2851 
2852  /*
2853  * Now for the per-column permissions bits.
2854  *
2855  * Initially, new_rte contains selectedCols permission check bits for all
2856  * base-rel columns referenced by the view, but since the view is a SELECT
2857  * query its insertedCols/updatedCols is empty. We set insertedCols and
2858  * updatedCols to include all the columns the outer query is trying to
2859  * modify, adjusting the column numbers as needed. But we leave
2860  * selectedCols as-is, so the view owner must have read permission for all
2861  * columns used in the view definition, even if some of them are not read
2862  * by the outer query. We could try to limit selectedCols to only columns
2863  * used in the transformed query, but that does not correspond to what
2864  * happens in ordinary SELECT usage of a view: all referenced columns must
2865  * have read permission, even if optimization finds that some of them can
2866  * be discarded during query transformation. The flattening we're doing
2867  * here is an optional optimization, too. (If you are unpersuaded and
2868  * want to change this, note that applying adjust_view_column_set to
2869  * view_rte->selectedCols is clearly *not* the right answer, since that
2870  * neglects base-rel columns used in the view's WHERE quals.)
2871  *
2872  * This step needs the modified view targetlist, so we have to do things
2873  * in this order.
2874  */
2875  Assert(bms_is_empty(new_rte->insertedCols) &&
2876  bms_is_empty(new_rte->updatedCols));
2877 
2878  new_rte->insertedCols = adjust_view_column_set(view_rte->insertedCols,
2879  view_targetlist);
2880 
2881  new_rte->updatedCols = adjust_view_column_set(view_rte->updatedCols,
2882  view_targetlist);
2883 
2884  /*
2885  * Move any security barrier quals from the view RTE onto the new target
2886  * RTE. Any such quals should now apply to the new target RTE and will
2887  * not reference the original view RTE in the rewritten query.
2888  */
2889  new_rte->securityQuals = view_rte->securityQuals;
2890  view_rte->securityQuals = NIL;
2891 
2892  /*
2893  * For UPDATE/DELETE, rewriteTargetListUD will have added a wholerow junk
2894  * TLE for the view to the end of the targetlist, which we no longer need.
2895  * Remove it to avoid unnecessary work when we process the targetlist.
2896  * Note that when we recurse through rewriteQuery a new junk TLE will be
2897  * added to allow the executor to find the proper row in the new target
2898  * relation. (So, if we failed to do this, we might have multiple junk
2899  * TLEs with the same name, which would be disastrous.)
2900  */
2901  if (parsetree->commandType != CMD_INSERT)
2902  {
2903  TargetEntry *tle = (TargetEntry *) llast(parsetree->targetList);
2904 
2905  Assert(tle->resjunk);
2906  Assert(IsA(tle->expr, Var) &&
2907  ((Var *) tle->expr)->varno == parsetree->resultRelation &&
2908  ((Var *) tle->expr)->varattno == 0);
2909  parsetree->targetList = list_delete_ptr(parsetree->targetList, tle);
2910  }
2911 
2912  /*
2913  * Now update all Vars in the outer query that reference the view to
2914  * reference the appropriate column of the base relation instead.
2915  */
2916  parsetree = (Query *)
2917  ReplaceVarsFromTargetList((Node *) parsetree,
2918  parsetree->resultRelation,
2919  0,
2920  view_rte,
2921  view_targetlist,
2923  0,
2924  &parsetree->hasSubLinks);
2925 
2926  /*
2927  * Update all other RTI references in the query that point to the view
2928  * (for example, parsetree->resultRelation itself) to point to the new
2929  * base relation instead. Vars will not be affected since none of them
2930  * reference parsetree->resultRelation any longer.
2931  */
2932  ChangeVarNodes((Node *) parsetree,
2933  parsetree->resultRelation,
2934  new_rt_index,
2935  0);
2936  Assert(parsetree->resultRelation == new_rt_index);
2937 
2938  /*
2939  * For INSERT/UPDATE we must also update resnos in the targetlist to refer
2940  * to columns of the base relation, since those indicate the target
2941  * columns to be affected.
2942  *
2943  * Note that this destroys the resno ordering of the targetlist, but that
2944  * will be fixed when we recurse through rewriteQuery, which will invoke
2945  * rewriteTargetListIU again on the updated targetlist.
2946  */
2947  if (parsetree->commandType != CMD_DELETE)
2948  {
2949  foreach(lc, parsetree->targetList)
2950  {
2951  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2952  TargetEntry *view_tle;
2953 
2954  if (tle->resjunk)
2955  continue;
2956 
2957  view_tle = get_tle_by_resno(view_targetlist, tle->resno);
2958  if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
2959  tle->resno = ((Var *) view_tle->expr)->varattno;
2960  else
2961  elog(ERROR, "attribute number %d not found in view targetlist",
2962  tle->resno);
2963  }
2964  }
2965 
2966  /*
2967  * For UPDATE/DELETE, pull up any WHERE quals from the view. We know that
2968  * any Vars in the quals must reference the one base relation, so we need
2969  * only adjust their varnos to reference the new target (just the same as
2970  * we did with the view targetlist).
2971  *
2972  * If it's a security-barrier view, its WHERE quals must be applied before
2973  * quals from the outer query, so we attach them to the RTE as security
2974  * barrier quals rather than adding them to the main WHERE clause.
2975  *
2976  * For INSERT, the view's quals can be ignored in the main query.
2977  */
2978  if (parsetree->commandType != CMD_INSERT &&
2979  viewquery->jointree->quals != NULL)
2980  {
2981  Node *viewqual = (Node *) viewquery->jointree->quals;
2982 
2983  /*
2984  * Even though we copied viewquery already at the top of this
2985  * function, we must duplicate the viewqual again here, because we may
2986  * need to use the quals again below for a WithCheckOption clause.
2987  */
2988  viewqual = copyObject(viewqual);
2989 
2990  ChangeVarNodes(viewqual, base_rt_index, new_rt_index, 0);
2991 
2992  if (RelationIsSecurityView(view))
2993  {
2994  /*
2995  * The view's quals go in front of existing barrier quals: those
2996  * would have come from an outer level of security-barrier view,
2997  * and so must get evaluated later.
2998  *
2999  * Note: the parsetree has been mutated, so the new_rte pointer is
3000  * stale and needs to be re-computed.
3001  */
3002  new_rte = rt_fetch(new_rt_index, parsetree->rtable);
3003  new_rte->securityQuals = lcons(viewqual, new_rte->securityQuals);
3004 
3005  /*
3006  * Do not set parsetree->hasRowSecurity, because these aren't RLS
3007  * conditions (they aren't affected by enabling/disabling RLS).
3008  */
3009 
3010  /*
3011  * Make sure that the query is marked correctly if the added qual
3012  * has sublinks.
3013  */
3014  if (!parsetree->hasSubLinks)
3015  parsetree->hasSubLinks = checkExprHasSubLink(viewqual);
3016  }
3017  else
3018  AddQual(parsetree, (Node *) viewqual);
3019  }
3020 
3021  /*
3022  * For INSERT/UPDATE, if the view has the WITH CHECK OPTION, or any parent
3023  * view specified WITH CASCADED CHECK OPTION, add the quals from the view
3024  * to the query's withCheckOptions list.
3025  */
3026  if (parsetree->commandType != CMD_DELETE)
3027  {
3028  bool has_wco = RelationHasCheckOption(view);
3029  bool cascaded = RelationHasCascadedCheckOption(view);
3030 
3031  /*
3032  * If the parent view has a cascaded check option, treat this view as
3033  * if it also had a cascaded check option.
3034  *
3035  * New WithCheckOptions are added to the start of the list, so if
3036  * there is a cascaded check option, it will be the first item in the
3037  * list.
3038  */
3039  if (parsetree->withCheckOptions != NIL)
3040  {
3041  WithCheckOption *parent_wco =
3042  (WithCheckOption *) linitial(parsetree->withCheckOptions);
3043 
3044  if (parent_wco->cascaded)
3045  {
3046  has_wco = true;
3047  cascaded = true;
3048  }
3049  }
3050 
3051  /*
3052  * Add the new WithCheckOption to the start of the list, so that
3053  * checks on inner views are run before checks on outer views, as
3054  * required by the SQL standard.
3055  *
3056  * If the new check is CASCADED, we need to add it even if this view
3057  * has no quals, since there may be quals on child views. A LOCAL
3058  * check can be omitted if this view has no quals.
3059  */
3060  if (has_wco && (cascaded || viewquery->jointree->quals != NULL))
3061  {
3062  WithCheckOption *wco;
3063 
3064  wco = makeNode(WithCheckOption);
3065  wco->kind = WCO_VIEW_CHECK;
3066  wco->relname = pstrdup(RelationGetRelationName(view));
3067  wco->polname = NULL;
3068  wco->qual = NULL;
3069  wco->cascaded = cascaded;
3070 
3071  parsetree->withCheckOptions = lcons(wco,
3072  parsetree->withCheckOptions);
3073 
3074  if (viewquery->jointree->quals != NULL)
3075  {
3076  wco->qual = (Node *) viewquery->jointree->quals;
3077  ChangeVarNodes(wco->qual, base_rt_index, new_rt_index, 0);
3078 
3079  /*
3080  * Make sure that the query is marked correctly if the added
3081  * qual has sublinks. We can skip this check if the query is
3082  * already marked, or if the command is an UPDATE, in which
3083  * case the same qual will have already been added, and this
3084  * check will already have been done.
3085  */
3086  if (!parsetree->hasSubLinks &&
3087  parsetree->commandType != CMD_UPDATE)
3088  parsetree->hasSubLinks = checkExprHasSubLink(wco->qual);
3089  }
3090  }
3091  }
3092 
3093  return parsetree;
3094 }
#define NIL
Definition: pg_list.h:69
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2257
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
int errhint(const char *fmt,...)
Definition: elog.c:987
static bool acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context)
FromExpr * jointree
Definition: parsenodes.h:129
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:277
OnConflictExpr * onConflict
Definition: parsenodes.h:133
#define castNode(_type_, nodeptr)
Definition: nodes.h:587
List * withCheckOptions
Definition: parsenodes.h:160
List * securityQuals
Definition: parsenodes.h:1009
char * pstrdup(const char *in)
Definition: mcxt.c:1077
static const char * view_cols_are_auto_updatable(Query *viewquery, Bitmapset *required_cols, Bitmapset **updatable_cols, char **non_updatable_col)
int resultRelation
Definition: parsenodes.h:113
#define llast(l)
Definition: pg_list.h:126
Definition: nodes.h:518
int errcode(int sqlerrcode)
Definition: elog.c:575
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
AclMode requiredPerms
Definition: parsenodes.h:1004
List * fromlist
Definition: primnodes.h:1455
#define heap_close(r, l)
Definition: heapam.h:97
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
Form_pg_class rd_rel
Definition: rel.h:113
Definition: primnodes.h:163
Node * quals
Definition: primnodes.h:1456
int errdetail_internal(const char *fmt,...)
Definition: elog.c:900
List * targetList
Definition: parsenodes.h:131
#define QTW_IGNORE_RC_SUBQUERIES
Definition: nodeFuncs.h:22
void * copyObject(const void *from)
Definition: copyfuncs.c:4592
bool resjunk
Definition: primnodes.h:1359
#define linitial(l)
Definition: pg_list.h:110
List * rtable
Definition: parsenodes.h:128
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
AttrNumber resno
Definition: primnodes.h:1353
#define RelationGetRelationName(relation)
Definition: rel.h:433
#define ereport(elevel, rest)
Definition: elog.h:122
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * lappend(List *list, void *datum)
Definition: list.c:128
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:633
Node * ReplaceVarsFromTargetList(Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
Bitmapset * updatedCols
Definition: parsenodes.h:1008
static Bitmapset * adjust_view_column_set(Bitmapset *cols, List *targetlist)
CmdType commandType
Definition: parsenodes.h:103
List * lcons(void *datum, List *list)
Definition: list.c:259
#define makeNode(_type_)
Definition: nodes.h:566
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
void AddQual(Query *parsetree, Node *qual)
Definition: rewriteManip.c:976
Expr * expr
Definition: primnodes.h:1352
static int list_length(const List *l)
Definition: pg_list.h:89
#define RelationHasCascadedCheckOption(relation)
Definition: rel.h:374
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
RTEKind rtekind
Definition: parsenodes.h:916
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool hasSubLinks
Definition: parsenodes.h:119
Query * get_view_query(Relation view)
Bitmapset * insertedCols
Definition: parsenodes.h:1007
#define RelationIsSecurityView(relation)
Definition: rel.h:344
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
List * onConflictSet
Definition: primnodes.h:1480
void ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
Definition: rewriteManip.c:607
#define elog
Definition: elog.h:219
const char * view_query_is_auto_updatable(Query *viewquery, bool check_cols)
Definition: pg_list.h:45
#define _(x)
Definition: elog.c:84
#define RelationHasCheckOption(relation)
Definition: rel.h:353
static void rewriteValuesRTE ( RangeTblEntry rte,
Relation  target_relation,
List attrnos 
)
static

Definition at line 1142 of file rewriteHandler.c.

References Assert, tupleDesc::attrs, build_column_default(), COERCE_IMPLICIT_CAST, coerce_to_domain(), forboth, InvalidOid, IsA, lappend(), lfirst, lfirst_int, linitial, list_length(), makeConst(), NIL, NULL, RelationData::rd_att, searchForDefault(), and RangeTblEntry::values_lists.

Referenced by RewriteQuery().

1143 {
1144  List *newValues;
1145  ListCell *lc;
1146 
1147  /*
1148  * Rebuilding all the lists is a pretty expensive proposition in a big
1149  * VALUES list, and it's a waste of time if there aren't any DEFAULT
1150  * placeholders. So first scan to see if there are any.
1151  */
1152  if (!searchForDefault(rte))
1153  return; /* nothing to do */
1154 
1155  /* Check list lengths (we can assume all the VALUES sublists are alike) */
1156  Assert(list_length(attrnos) == list_length(linitial(rte->values_lists)));
1157 
1158  newValues = NIL;
1159  foreach(lc, rte->values_lists)
1160  {
1161  List *sublist = (List *) lfirst(lc);
1162  List *newList = NIL;
1163  ListCell *lc2;
1164  ListCell *lc3;
1165 
1166  forboth(lc2, sublist, lc3, attrnos)
1167  {
1168  Node *col = (Node *) lfirst(lc2);
1169  int attrno = lfirst_int(lc3);
1170 
1171  if (IsA(col, SetToDefault))
1172  {
1173  Form_pg_attribute att_tup;
1174  Node *new_expr;
1175 
1176  att_tup = target_relation->rd_att->attrs[attrno - 1];
1177 
1178  if (!att_tup->attisdropped)
1179  new_expr = build_column_default(target_relation, attrno);
1180  else
1181  new_expr = NULL; /* force a NULL if dropped */
1182 
1183  /*
1184  * If there is no default (ie, default is effectively NULL),
1185  * we've got to explicitly set the column to NULL.
1186  */
1187  if (!new_expr)
1188  {
1189  new_expr = (Node *) makeConst(att_tup->atttypid,
1190  -1,
1191  att_tup->attcollation,
1192  att_tup->attlen,
1193  (Datum) 0,
1194  true, /* isnull */
1195  att_tup->attbyval);
1196  /* this is to catch a NOT NULL domain constraint */
1197  new_expr = coerce_to_domain(new_expr,
1198  InvalidOid, -1,
1199  att_tup->atttypid,
1201  -1,
1202  false,
1203  false);
1204  }
1205  newList = lappend(newList, new_expr);
1206  }
1207  else
1208  newList = lappend(newList, col);
1209  }
1210  newValues = lappend(newValues, newList);
1211  }
1212  rte->values_lists = newValues;
1213 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
Form_pg_attribute * attrs
Definition: tupdesc.h:74
Definition: nodes.h:518
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:296
List * values_lists
Definition: parsenodes.h:976
#define linitial(l)
Definition: pg_list.h:110
static bool searchForDefault(RangeTblEntry *rte)
#define lfirst_int(lc)
Definition: pg_list.h:107
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionForm cformat, int location, bool hideInputCoercion, bool lengthCoercionDone)
Definition: parse_coerce.c:648
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * build_column_default(Relation rel, int attrno)
uintptr_t Datum
Definition: postgres.h:372
TupleDesc rd_att
Definition: rel.h:114
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
Definition: pg_list.h:45
static bool searchForDefault ( RangeTblEntry rte)
static

Definition at line 1110 of file rewriteHandler.c.

References IsA, lfirst, and RangeTblEntry::values_lists.

Referenced by rewriteValuesRTE().

1111 {
1112  ListCell *lc;
1113 
1114  foreach(lc, rte->values_lists)
1115  {
1116  List *sublist = (List *) lfirst(lc);
1117  ListCell *lc2;
1118 
1119  foreach(lc2, sublist)
1120  {
1121  Node *col = (Node *) lfirst(lc2);
1122 
1123  if (IsA(col, SetToDefault))
1124  return true;
1125  }
1126  }
1127  return false;
1128 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
Definition: nodes.h:518
List * values_lists
Definition: parsenodes.h:976
#define lfirst(lc)
Definition: pg_list.h:106
Definition: pg_list.h:45
static const char* view_col_is_auto_updatable ( RangeTblRef rtr,
TargetEntry tle 
)
static

Definition at line 2123 of file rewriteHandler.c.

References TargetEntry::expr, gettext_noop, IsA, NULL, TargetEntry::resjunk, RangeTblRef::rtindex, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by view_cols_are_auto_updatable(), and view_query_is_auto_updatable().

2124 {
2125  Var *var = (Var *) tle->expr;
2126 
2127  /*
2128  * For now, the only updatable columns we support are those that are Vars
2129  * referring to user columns of the underlying base relation.
2130  *
2131  * The view targetlist may contain resjunk columns (e.g., a view defined
2132  * like "SELECT * FROM t ORDER BY a+b" is auto-updatable) but such columns
2133  * are not auto-updatable, and in fact should never appear in the outer
2134  * query's targetlist.
2135  */
2136  if (tle->resjunk)
2137  return gettext_noop("Junk view columns are not updatable.");
2138 
2139  if (!IsA(var, Var) ||
2140  var->varno != rtr->rtindex ||
2141  var->varlevelsup != 0)
2142  return gettext_noop("View columns that are not columns of their base relation are not updatable.");
2143 
2144  if (var->varattno < 0)
2145  return gettext_noop("View columns that refer to system columns are not updatable.");
2146 
2147  if (var->varattno == 0)
2148  return gettext_noop("View columns that return whole-row references are not updatable.");
2149 
2150  return NULL; /* the view column is updatable */
2151 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
Index varlevelsup
Definition: primnodes.h:173
#define gettext_noop(x)
Definition: c.h:139
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
bool resjunk
Definition: primnodes.h:1359
Index varno
Definition: primnodes.h:166
#define NULL
Definition: c.h:229
Expr * expr
Definition: primnodes.h:1352
static const char* view_cols_are_auto_updatable ( Query viewquery,
Bitmapset required_cols,
Bitmapset **  updatable_cols,
char **  non_updatable_col 
)
static

Definition at line 2313 of file rewriteHandler.c.

References Assert, bms_add_member(), bms_is_member(), castNode, FirstLowInvalidHeapAttributeNumber, FromExpr::fromlist, Query::jointree, lfirst, linitial, list_length(), NULL, TargetEntry::resname, Query::targetList, and view_col_is_auto_updatable().

Referenced by relation_is_updatable(), and rewriteTargetView().

2317 {
2318  RangeTblRef *rtr;
2319  AttrNumber col;
2320  ListCell *cell;
2321 
2322  /*
2323  * The caller should have verified that this view is auto-updatable and so
2324  * there should be a single base relation.
2325  */
2326  Assert(list_length(viewquery->jointree->fromlist) == 1);
2327  rtr = castNode(RangeTblRef, linitial(viewquery->jointree->fromlist));
2328 
2329  /* Initialize the optional return values */
2330  if (updatable_cols != NULL)
2331  *updatable_cols = NULL;
2332  if (non_updatable_col != NULL)
2333  *non_updatable_col = NULL;
2334 
2335  /* Test each view column for updatability */
2337  foreach(cell, viewquery->targetList)
2338  {
2339  TargetEntry *tle = (TargetEntry *) lfirst(cell);
2340  const char *col_update_detail;
2341 
2342  col++;
2343  col_update_detail = view_col_is_auto_updatable(rtr, tle);
2344 
2345  if (col_update_detail == NULL)
2346  {
2347  /* The column is updatable */
2348  if (updatable_cols != NULL)
2349  *updatable_cols = bms_add_member(*updatable_cols, col);
2350  }
2351  else if (bms_is_member(col, required_cols))
2352  {
2353  /* The required column is not updatable */
2354  if (non_updatable_col != NULL)
2355  *non_updatable_col = tle->resname;
2356  return col_update_detail;
2357  }
2358  }
2359 
2360  return NULL; /* all the required view columns are updatable */
2361 }
FromExpr * jointree
Definition: parsenodes.h:129
#define castNode(_type_, nodeptr)
Definition: nodes.h:587
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
List * fromlist
Definition: primnodes.h:1455
char * resname
Definition: primnodes.h:1354
List * targetList
Definition: parsenodes.h:131
#define linitial(l)
Definition: pg_list.h:110
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
static const char * view_col_is_auto_updatable(RangeTblRef *rtr, TargetEntry *tle)
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
int16 AttrNumber
Definition: attnum.h:21
static bool view_has_instead_trigger ( Relation  view,
CmdType  event 
)
static

Definition at line 2088 of file rewriteHandler.c.

References CMD_DELETE, CMD_INSERT, CMD_UPDATE, elog, ERROR, TriggerDesc::trig_delete_instead_row, TriggerDesc::trig_insert_instead_row, TriggerDesc::trig_update_instead_row, and RelationData::trigdesc.

Referenced by RewriteQuery(), and rewriteTargetListIU().

2089 {
2090  TriggerDesc *trigDesc = view->trigdesc;
2091 
2092  switch (event)
2093  {
2094  case CMD_INSERT:
2095  if (trigDesc && trigDesc->trig_insert_instead_row)
2096  return true;
2097  break;
2098  case CMD_UPDATE:
2099  if (trigDesc && trigDesc->trig_update_instead_row)
2100  return true;
2101  break;
2102  case CMD_DELETE:
2103  if (trigDesc && trigDesc->trig_delete_instead_row)
2104  return true;
2105  break;
2106  default:
2107  elog(ERROR, "unrecognized CmdType: %d", (int) event);
2108  break;
2109  }
2110  return false;
2111 }
bool trig_insert_instead_row
Definition: reltrigger.h:57
#define ERROR
Definition: elog.h:43
TriggerDesc * trigdesc
Definition: rel.h:119
bool trig_update_instead_row
Definition: reltrigger.h:62
bool trig_delete_instead_row
Definition: reltrigger.h:67
#define elog
Definition: elog.h:219
const char* view_query_is_auto_updatable ( Query viewquery,
bool  check_cols 
)

Definition at line 2168 of file rewriteHandler.c.

References Query::cteList, Query::distinctClause, FromExpr::fromlist, gettext_noop, Query::groupClause, Query::groupingSets, Query::hasAggs, Query::hasTargetSRFs, Query::hasWindowFuncs, Query::havingQual, IsA, Query::jointree, lfirst, Query::limitCount, Query::limitOffset, linitial, list_length(), NIL, NULL, RangeTblEntry::relkind, RELKIND_FOREIGN_TABLE, RELKIND_PARTITIONED_TABLE, RELKIND_RELATION, RELKIND_VIEW, 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().

2169 {
2170  RangeTblRef *rtr;
2171  RangeTblEntry *base_rte;
2172 
2173  /*----------
2174  * Check if the view is simply updatable. According to SQL-92 this means:
2175  * - No DISTINCT clause.
2176  * - Each TLE is a column reference, and each column appears at most once.
2177  * - FROM contains exactly one base relation.
2178  * - No GROUP BY or HAVING clauses.
2179  * - No set operations (UNION, INTERSECT or EXCEPT).
2180  * - No sub-queries in the WHERE clause that reference the target table.
2181  *
2182  * We ignore that last restriction since it would be complex to enforce
2183  * and there isn't any actual benefit to disallowing sub-queries. (The
2184  * semantic issues that the standard is presumably concerned about don't
2185  * arise in Postgres, since any such sub-query will not see any updates
2186  * executed by the outer query anyway, thanks to MVCC snapshotting.)
2187  *
2188  * We also relax the second restriction by supporting part of SQL:1999
2189  * feature T111, which allows for a mix of updatable and non-updatable
2190  * columns, provided that an INSERT or UPDATE doesn't attempt to assign to
2191  * a non-updatable column.
2192  *
2193  * In addition we impose these constraints, involving features that are
2194  * not part of SQL-92:
2195  * - No CTEs (WITH clauses).
2196  * - No OFFSET or LIMIT clauses (this matches a SQL:2008 restriction).
2197  * - No system columns (including whole-row references) in the tlist.
2198  * - No window functions in the tlist.
2199  * - No set-returning functions in the tlist.
2200  *
2201  * Note that we do these checks without recursively expanding the view.
2202  * If the base relation is a view, we'll recursively deal with it later.
2203  *----------
2204  */
2205  if (viewquery->distinctClause != NIL)
2206  return gettext_noop("Views containing DISTINCT are not automatically updatable.");
2207 
2208  if (viewquery->groupClause != NIL || viewquery->groupingSets)
2209  return gettext_noop("Views containing GROUP BY are not automatically updatable.");
2210 
2211  if (viewquery->havingQual != NULL)
2212  return gettext_noop("Views containing HAVING are not automatically updatable.");
2213 
2214  if (viewquery->setOperations != NULL)
2215  return gettext_noop("Views containing UNION, INTERSECT, or EXCEPT are not automatically updatable.");
2216 
2217  if (viewquery->cteList != NIL)
2218  return gettext_noop("Views containing WITH are not automatically updatable.");
2219 
2220  if (viewquery->limitOffset != NULL || viewquery->limitCount != NULL)
2221  return gettext_noop("Views containing LIMIT or OFFSET are not automatically updatable.");
2222 
2223  /*
2224  * We must not allow window functions or set returning functions in the
2225  * targetlist. Otherwise we might end up inserting them into the quals of
2226  * the main query. We must also check for aggregates in the targetlist in
2227  * case they appear without a GROUP BY.
2228  *
2229  * These restrictions ensure that each row of the view corresponds to a
2230  * unique row in the underlying base relation.
2231  */
2232  if (viewquery->hasAggs)
2233  return gettext_noop("Views that return aggregate functions are not automatically updatable.");
2234 
2235  if (viewquery->hasWindowFuncs)
2236  return gettext_noop("Views that return window functions are not automatically updatable.");
2237 
2238  if (viewquery->hasTargetSRFs)
2239  return gettext_noop("Views that return set-returning functions are not automatically updatable.");
2240 
2241  /*
2242  * The view query should select from a single base relation, which must be
2243  * a table or another view.
2244  */
2245  if (list_length(viewquery->jointree->fromlist) != 1)
2246  return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2247 
2248  rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
2249  if (!IsA(rtr, RangeTblRef))
2250  return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2251 
2252  base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
2253  if (base_rte->rtekind != RTE_RELATION ||
2254  (base_rte->relkind != RELKIND_RELATION &&
2255  base_rte->relkind != RELKIND_FOREIGN_TABLE &&
2256  base_rte->relkind != RELKIND_VIEW &&
2257  base_rte->relkind != RELKIND_PARTITIONED_TABLE))
2258  return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2259 
2260  if (base_rte->tablesample)
2261  return gettext_noop("Views containing TABLESAMPLE are not automatically updatable.");
2262 
2263  /*
2264  * Check that the view has at least one updatable column. This is required
2265  * for INSERT/UPDATE but not for DELETE.
2266  */
2267  if (check_cols)
2268  {
2269  ListCell *cell;
2270  bool found;
2271 
2272  found = false;
2273  foreach(cell, viewquery->targetList)
2274  {
2275  TargetEntry *tle = (TargetEntry *) lfirst(cell);
2276 
2277  if (view_col_is_auto_updatable(rtr, tle) == NULL)
2278  {
2279  found = true;
2280  break;
2281  }
2282  }
2283 
2284  if (!found)
2285  return gettext_noop("Views that have no updatable columns are not automatically updatable.");
2286  }
2287 
2288  return NULL; /* the view is updatable */
2289 }
Node * limitOffset
Definition: parsenodes.h:149
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
FromExpr * jointree
Definition: parsenodes.h:129
bool hasAggs
Definition: parsenodes.h:116
List * groupingSets
Definition: parsenodes.h:139
#define gettext_noop(x)
Definition: c.h:139
List * fromlist
Definition: primnodes.h:1455
List * targetList
Definition: parsenodes.h:131
#define linitial(l)
Definition: pg_list.h:110
List * rtable
Definition: parsenodes.h:128
List * distinctClause
Definition: parsenodes.h:145
Node * limitCount
Definition: parsenodes.h:150
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
bool hasTargetSRFs
Definition: parsenodes.h:118
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
bool hasWindowFuncs
Definition: parsenodes.h:117
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:916
List * cteList
Definition: parsenodes.h:126
Node * setOperations
Definition: parsenodes.h:154
List * groupClause
Definition: parsenodes.h:137
#define RELKIND_VIEW
Definition: pg_class.h:164
static const char * view_col_is_auto_updatable(RangeTblRef *rtr, TargetEntry *tle)
Node * havingQual
Definition: parsenodes.h:141
#define RELKIND_RELATION
Definition: pg_class.h:160
struct TableSampleClause * tablesample
Definition: parsenodes.h:929