PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
rewriteHandler.h File Reference
#include "utils/relcache.h"
#include "nodes/parsenodes.h"
Include dependency graph for rewriteHandler.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ListQueryRewrite (Query *parsetree)
 
void AcquireRewriteLocks (Query *parsetree, bool forExecute, bool forUpdatePushedDown)
 
Nodebuild_column_default (Relation rel, int attrno)
 
Queryget_view_query (Relation view)
 
const char * view_query_is_auto_updatable (Query *viewquery, bool check_cols)
 
int relation_is_updatable (Oid reloid, bool include_triggers, Bitmapset *include_cols)
 

Function Documentation

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

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

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

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

1082 {
1083  TupleDesc rd_att = rel->rd_att;
1084  Form_pg_attribute att_tup = rd_att->attrs[attrno - 1];
1085  Oid atttype = att_tup->atttypid;
1086  int32 atttypmod = att_tup->atttypmod;
1087  Node *expr = NULL;
1088  Oid exprtype;
1089 
1090  /*
1091  * Scan to see if relation has a default for this column.
1092  */
1093  if (rd_att->constr && rd_att->constr->num_defval > 0)
1094  {
1095  AttrDefault *defval = rd_att->constr->defval;
1096  int ndef = rd_att->constr->num_defval;
1097 
1098  while (--ndef >= 0)
1099  {
1100  if (attrno == defval[ndef].adnum)
1101  {
1102  /*
1103  * Found it, convert string representation to node tree.
1104  */
1105  expr = stringToNode(defval[ndef].adbin);
1106  break;
1107  }
1108  }
1109  }
1110 
1111  if (expr == NULL)
1112  {
1113  /*
1114  * No per-column default, so look for a default for the type itself.
1115  */
1116  expr = get_typdefault(atttype);
1117  }
1118 
1119  if (expr == NULL)
1120  return NULL; /* No default anywhere */
1121 
1122  /*
1123  * Make sure the value is coerced to the target column type; this will
1124  * generally be true already, but there seem to be some corner cases
1125  * involving domain defaults where it might not be true. This should match
1126  * the parser's processing of non-defaulted expressions --- see
1127  * transformAssignedExpr().
1128  */
1129  exprtype = exprType(expr);
1130 
1131  expr = coerce_to_target_type(NULL, /* no UNKNOWN params here */
1132  expr, exprtype,
1133  atttype, atttypmod,
1136  -1);
1137  if (expr == NULL)
1138  ereport(ERROR,
1139  (errcode(ERRCODE_DATATYPE_MISMATCH),
1140  errmsg("column \"%s\" is of type %s"
1141  " but default expression is of type %s",
1142  NameStr(att_tup->attname),
1143  format_type_be(atttype),
1144  format_type_be(exprtype)),
1145  errhint("You will need to rewrite or cast the expression.")));
1146 
1147  return expr;
1148 }
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:509
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:187
#define ereport(elevel, rest)
Definition: elog.h:122
TupleDesc rd_att
Definition: rel.h:115
uint16 num_defval
Definition: tupdesc.h:41
#define NULL
Definition: c.h:229
Node * get_typdefault(Oid typid)
Definition: lsyscache.c:2198
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
Query* get_view_query ( Relation  view)

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

2100 {
2101  int i;
2102 
2103  Assert(view->rd_rel->relkind == RELKIND_VIEW);
2104 
2105  for (i = 0; i < view->rd_rules->numLocks; i++)
2106  {
2107  RewriteRule *rule = view->rd_rules->rules[i];
2108 
2109  if (rule->event == CMD_SELECT)
2110  {
2111  /* A _RETURN rule should have only one action */
2112  if (list_length(rule->actions) != 1)
2113  elog(ERROR, "invalid _RETURN rule action specification");
2114 
2115  return (Query *) linitial(rule->actions);
2116  }
2117  }
2118 
2119  elog(ERROR, "failed to find _RETURN rule for view");
2120  return NULL; /* keep compiler quiet */
2121 }
int numLocks
Definition: prs2lock.h:42
Form_pg_class rd_rel
Definition: rel.h:114
Definition: localtime.c:82
#define linitial(l)
Definition: pg_list.h:111
#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:118
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
List* QueryRewrite ( Query parsetree)

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

3546 {
3547  uint32 input_query_id = parsetree->queryId;
3548  List *querylist;
3549  List *results;
3550  ListCell *l;
3551  CmdType origCmdType;
3552  bool foundOriginalQuery;
3553  Query *lastInstead;
3554 
3555  /*
3556  * This function is only applied to top-level original queries
3557  */
3558  Assert(parsetree->querySource == QSRC_ORIGINAL);
3559  Assert(parsetree->canSetTag);
3560 
3561  /*
3562  * Step 1
3563  *
3564  * Apply all non-SELECT rules possibly getting 0 or many queries
3565  */
3566  querylist = RewriteQuery(parsetree, NIL);
3567 
3568  /*
3569  * Step 2
3570  *
3571  * Apply all the RIR rules on each query
3572  *
3573  * This is also a handy place to mark each query with the original queryId
3574  */
3575  results = NIL;
3576  foreach(l, querylist)
3577  {
3578  Query *query = (Query *) lfirst(l);
3579 
3580  query = fireRIRrules(query, NIL, false);
3581 
3582  query->queryId = input_query_id;
3583 
3584  results = lappend(results, query);
3585  }
3586 
3587  /*
3588  * Step 3
3589  *
3590  * Determine which, if any, of the resulting queries is supposed to set
3591  * the command-result tag; and update the canSetTag fields accordingly.
3592  *
3593  * If the original query is still in the list, it sets the command tag.
3594  * Otherwise, the last INSTEAD query of the same kind as the original is
3595  * allowed to set the tag. (Note these rules can leave us with no query
3596  * setting the tag. The tcop code has to cope with this by setting up a
3597  * default tag based on the original un-rewritten query.)
3598  *
3599  * The Asserts verify that at most one query in the result list is marked
3600  * canSetTag. If we aren't checking asserts, we can fall out of the loop
3601  * as soon as we find the original query.
3602  */
3603  origCmdType = parsetree->commandType;
3604  foundOriginalQuery = false;
3605  lastInstead = NULL;
3606 
3607  foreach(l, results)
3608  {
3609  Query *query = (Query *) lfirst(l);
3610 
3611  if (query->querySource == QSRC_ORIGINAL)
3612  {
3613  Assert(query->canSetTag);
3614  Assert(!foundOriginalQuery);
3615  foundOriginalQuery = true;
3616 #ifndef USE_ASSERT_CHECKING
3617  break;
3618 #endif
3619  }
3620  else
3621  {
3622  Assert(!query->canSetTag);
3623  if (query->commandType == origCmdType &&
3624  (query->querySource == QSRC_INSTEAD_RULE ||
3626  lastInstead = query;
3627  }
3628  }
3629 
3630  if (!foundOriginalQuery && lastInstead != NULL)
3631  lastInstead->canSetTag = true;
3632 
3633  return results;
3634 }
#define NIL
Definition: pg_list.h:69
uint32 queryId
Definition: parsenodes.h:114
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:110
#define NULL
Definition: c.h:229
QuerySource querySource
Definition: parsenodes.h:112
#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:116
Definition: pg_list.h:45
CmdType
Definition: nodes.h:649
int relation_is_updatable ( Oid  reloid,
bool  include_triggers,
Bitmapset include_cols 
)

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

2438 {
2439  int events = 0;
2440  Relation rel;
2441  RuleLock *rulelocks;
2442 
2443 #define ALL_EVENTS ((1 << CMD_INSERT) | (1 << CMD_UPDATE) | (1 << CMD_DELETE))
2444 
2445  rel = try_relation_open(reloid, AccessShareLock);
2446 
2447  /*
2448  * If the relation doesn't exist, return zero rather than throwing an
2449  * error. This is helpful since scanning an information_schema view under
2450  * MVCC rules can result in referencing rels that have actually been
2451  * deleted already.
2452  */
2453  if (rel == NULL)
2454  return 0;
2455 
2456  /* If the relation is a table, it is always updatable */
2457  if (rel->rd_rel->relkind == RELKIND_RELATION ||
2458  rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2459  {
2461  return ALL_EVENTS;
2462  }
2463 
2464  /* Look for unconditional DO INSTEAD rules, and note supported events */
2465  rulelocks = rel->rd_rules;
2466  if (rulelocks != NULL)
2467  {
2468  int i;
2469 
2470  for (i = 0; i < rulelocks->numLocks; i++)
2471  {
2472  if (rulelocks->rules[i]->isInstead &&
2473  rulelocks->rules[i]->qual == NULL)
2474  {
2475  events |= ((1 << rulelocks->rules[i]->event) & ALL_EVENTS);
2476  }
2477  }
2478 
2479  /* If we have rules for all events, we're done */
2480  if (events == ALL_EVENTS)
2481  {
2483  return events;
2484  }
2485  }
2486 
2487  /* Similarly look for INSTEAD OF triggers, if they are to be included */
2488  if (include_triggers)
2489  {
2490  TriggerDesc *trigDesc = rel->trigdesc;
2491 
2492  if (trigDesc)
2493  {
2494  if (trigDesc->trig_insert_instead_row)
2495  events |= (1 << CMD_INSERT);
2496  if (trigDesc->trig_update_instead_row)
2497  events |= (1 << CMD_UPDATE);
2498  if (trigDesc->trig_delete_instead_row)
2499  events |= (1 << CMD_DELETE);
2500 
2501  /* If we have triggers for all events, we're done */
2502  if (events == ALL_EVENTS)
2503  {
2505  return events;
2506  }
2507  }
2508  }
2509 
2510  /* If this is a foreign table, check which update events it supports */
2511  if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2512  {
2513  FdwRoutine *fdwroutine = GetFdwRoutineForRelation(rel, false);
2514 
2515  if (fdwroutine->IsForeignRelUpdatable != NULL)
2516  events |= fdwroutine->IsForeignRelUpdatable(rel);
2517  else
2518  {
2519  /* Assume presence of executor functions is sufficient */
2520  if (fdwroutine->ExecForeignInsert != NULL)
2521  events |= (1 << CMD_INSERT);
2522  if (fdwroutine->ExecForeignUpdate != NULL)
2523  events |= (1 << CMD_UPDATE);
2524  if (fdwroutine->ExecForeignDelete != NULL)
2525  events |= (1 << CMD_DELETE);
2526  }
2527 
2529  return events;
2530  }
2531 
2532  /* Check if this is an automatically updatable view */
2533  if (rel->rd_rel->relkind == RELKIND_VIEW)
2534  {
2535  Query *viewquery = get_view_query(rel);
2536 
2537  if (view_query_is_auto_updatable(viewquery, false) == NULL)
2538  {
2539  Bitmapset *updatable_cols;
2540  int auto_events;
2541  RangeTblRef *rtr;
2542  RangeTblEntry *base_rte;
2543  Oid baseoid;
2544 
2545  /*
2546  * Determine which of the view's columns are updatable. If there
2547  * are none within the set of columns we are looking at, then the
2548  * view doesn't support INSERT/UPDATE, but it may still support
2549  * DELETE.
2550  */
2552  &updatable_cols, NULL);
2553 
2554  if (include_cols != NULL)
2555  updatable_cols = bms_int_members(updatable_cols, include_cols);
2556 
2557  if (bms_is_empty(updatable_cols))
2558  auto_events = (1 << CMD_DELETE); /* May support DELETE */
2559  else
2560  auto_events = ALL_EVENTS; /* May support all events */
2561 
2562  /*
2563  * The base relation must also support these update commands.
2564  * Tables are always updatable, but for any other kind of base
2565  * relation we must do a recursive check limited to the columns
2566  * referenced by the locally updatable columns in this view.
2567  */
2568  rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
2569  base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
2570  Assert(base_rte->rtekind == RTE_RELATION);
2571 
2572  if (base_rte->relkind != RELKIND_RELATION &&
2573  base_rte->relkind != RELKIND_PARTITIONED_TABLE)
2574  {
2575  baseoid = base_rte->relid;
2576  include_cols = adjust_view_column_set(updatable_cols,
2577  viewquery->targetList);
2578  auto_events &= relation_is_updatable(baseoid,
2579  include_triggers,
2580  include_cols);
2581  }
2582  events |= auto_events;
2583  }
2584  }
2585 
2586  /* If we reach here, the relation may support some update commands */
2588  return events;
2589 }
ExecForeignDelete_function ExecForeignDelete
Definition: fdwapi.h:199
Node * qual
Definition: prs2lock.h:28
int numLocks
Definition: prs2lock.h:42
FromExpr * jointree
Definition: parsenodes.h:136
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1147
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:1260
List * fromlist
Definition: primnodes.h:1471
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
bool isInstead
Definition: prs2lock.h:31
List * targetList
Definition: parsenodes.h:138
bool trig_insert_instead_row
Definition: reltrigger.h:57
#define linitial(l)
Definition: pg_list.h:111
List * rtable
Definition: parsenodes.h:135
#define ALL_EVENTS
TriggerDesc * trigdesc
Definition: rel.h:120
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:663
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
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:118
RTEKind rtekind
Definition: parsenodes.h:936
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:791
const char * view_query_is_auto_updatable(Query *viewquery, bool check_cols)
#define RELKIND_RELATION
Definition: pg_class.h:160
const char* view_query_is_auto_updatable ( Query viewquery,
bool  check_cols 
)

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

2213 {
2214  RangeTblRef *rtr;
2215  RangeTblEntry *base_rte;
2216 
2217  /*----------
2218  * Check if the view is simply updatable. According to SQL-92 this means:
2219  * - No DISTINCT clause.
2220  * - Each TLE is a column reference, and each column appears at most once.
2221  * - FROM contains exactly one base relation.
2222  * - No GROUP BY or HAVING clauses.
2223  * - No set operations (UNION, INTERSECT or EXCEPT).
2224  * - No sub-queries in the WHERE clause that reference the target table.
2225  *
2226  * We ignore that last restriction since it would be complex to enforce
2227  * and there isn't any actual benefit to disallowing sub-queries. (The
2228  * semantic issues that the standard is presumably concerned about don't
2229  * arise in Postgres, since any such sub-query will not see any updates
2230  * executed by the outer query anyway, thanks to MVCC snapshotting.)
2231  *
2232  * We also relax the second restriction by supporting part of SQL:1999
2233  * feature T111, which allows for a mix of updatable and non-updatable
2234  * columns, provided that an INSERT or UPDATE doesn't attempt to assign to
2235  * a non-updatable column.
2236  *
2237  * In addition we impose these constraints, involving features that are
2238  * not part of SQL-92:
2239  * - No CTEs (WITH clauses).
2240  * - No OFFSET or LIMIT clauses (this matches a SQL:2008 restriction).
2241  * - No system columns (including whole-row references) in the tlist.
2242  * - No window functions in the tlist.
2243  * - No set-returning functions in the tlist.
2244  *
2245  * Note that we do these checks without recursively expanding the view.
2246  * If the base relation is a view, we'll recursively deal with it later.
2247  *----------
2248  */
2249  if (viewquery->distinctClause != NIL)
2250  return gettext_noop("Views containing DISTINCT are not automatically updatable.");
2251 
2252  if (viewquery->groupClause != NIL || viewquery->groupingSets)
2253  return gettext_noop("Views containing GROUP BY are not automatically updatable.");
2254 
2255  if (viewquery->havingQual != NULL)
2256  return gettext_noop("Views containing HAVING are not automatically updatable.");
2257 
2258  if (viewquery->setOperations != NULL)
2259  return gettext_noop("Views containing UNION, INTERSECT, or EXCEPT are not automatically updatable.");
2260 
2261  if (viewquery->cteList != NIL)
2262  return gettext_noop("Views containing WITH are not automatically updatable.");
2263 
2264  if (viewquery->limitOffset != NULL || viewquery->limitCount != NULL)
2265  return gettext_noop("Views containing LIMIT or OFFSET are not automatically updatable.");
2266 
2267  /*
2268  * We must not allow window functions or set returning functions in the
2269  * targetlist. Otherwise we might end up inserting them into the quals of
2270  * the main query. We must also check for aggregates in the targetlist in
2271  * case they appear without a GROUP BY.
2272  *
2273  * These restrictions ensure that each row of the view corresponds to a
2274  * unique row in the underlying base relation.
2275  */
2276  if (viewquery->hasAggs)
2277  return gettext_noop("Views that return aggregate functions are not automatically updatable.");
2278 
2279  if (viewquery->hasWindowFuncs)
2280  return gettext_noop("Views that return window functions are not automatically updatable.");
2281 
2282  if (viewquery->hasTargetSRFs)
2283  return gettext_noop("Views that return set-returning functions are not automatically updatable.");
2284 
2285  /*
2286  * The view query should select from a single base relation, which must be
2287  * a table or another view.
2288  */
2289  if (list_length(viewquery->jointree->fromlist) != 1)
2290  return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2291 
2292  rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
2293  if (!IsA(rtr, RangeTblRef))
2294  return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2295 
2296  base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
2297  if (base_rte->rtekind != RTE_RELATION ||
2298  (base_rte->relkind != RELKIND_RELATION &&
2299  base_rte->relkind != RELKIND_FOREIGN_TABLE &&
2300  base_rte->relkind != RELKIND_VIEW &&
2301  base_rte->relkind != RELKIND_PARTITIONED_TABLE))
2302  return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2303 
2304  if (base_rte->tablesample)
2305  return gettext_noop("Views containing TABLESAMPLE are not automatically updatable.");
2306 
2307  /*
2308  * Check that the view has at least one updatable column. This is required
2309  * for INSERT/UPDATE but not for DELETE.
2310  */
2311  if (check_cols)
2312  {
2313  ListCell *cell;
2314  bool found;
2315 
2316  found = false;
2317  foreach(cell, viewquery->targetList)
2318  {
2319  TargetEntry *tle = (TargetEntry *) lfirst(cell);
2320 
2321  if (view_col_is_auto_updatable(rtr, tle) == NULL)
2322  {
2323  found = true;
2324  break;
2325  }
2326  }
2327 
2328  if (!found)
2329  return gettext_noop("Views that have no updatable columns are not automatically updatable.");
2330  }
2331 
2332  return NULL; /* the view is updatable */
2333 }
Node * limitOffset
Definition: parsenodes.h:158
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
FromExpr * jointree
Definition: parsenodes.h:136
bool hasAggs
Definition: parsenodes.h:123
List * groupingSets
Definition: parsenodes.h:148
#define gettext_noop(x)
Definition: c.h:139
List * fromlist
Definition: primnodes.h:1471
List * targetList
Definition: parsenodes.h:138
#define linitial(l)
Definition: pg_list.h:111
List * rtable
Definition: parsenodes.h:135
List * distinctClause
Definition: parsenodes.h:154
Node * limitCount
Definition: parsenodes.h:159
#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:125
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
bool hasWindowFuncs
Definition: parsenodes.h:124
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:936
List * cteList
Definition: parsenodes.h:133
Node * setOperations
Definition: parsenodes.h:163
List * groupClause
Definition: parsenodes.h:146
#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:150
#define RELKIND_RELATION
Definition: pg_class.h:160
struct TableSampleClause * tablesample
Definition: parsenodes.h:954