PostgreSQL Source Code git master
Loading...
Searching...
No Matches
rewriteHandler.c File Reference
Include dependency graph for rewriteHandler.c:

Go to the source code of this file.

Data Structures

struct  rewrite_event
 
struct  acquireLocksOnSubLinks_context
 
struct  fireRIRonSubLink_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
 
typedef struct fireRIRonSubLink_context fireRIRonSubLink_context
 

Functions

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

Macro Definition Documentation

◆ ALL_EVENTS

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

Typedef Documentation

◆ acquireLocksOnSubLinks_context

◆ fireRIRonSubLink_context

◆ rewrite_event

Function Documentation

◆ acquireLocksOnSubLinks()

static bool acquireLocksOnSubLinks ( Node node,
acquireLocksOnSubLinks_context context 
)
static

Definition at line 312 of file rewriteHandler.c.

313{
314 if (node == NULL)
315 return false;
316 if (IsA(node, SubLink))
317 {
318 SubLink *sub = (SubLink *) node;
319
320 /* Do what we came for */
322 context->for_execute,
323 false);
324 /* Fall through to process lefthand args of SubLink */
325 }
326
327 /*
328 * Do NOT recurse into Query nodes, because AcquireRewriteLocks already
329 * processed subselects of subselects for us.
330 */
331 return expression_tree_walker(node, acquireLocksOnSubLinks, context);
332}
#define expression_tree_walker(n, w, c)
Definition nodeFuncs.h:153
#define IsA(nodeptr, _type_)
Definition nodes.h:164
static int fb(int x)
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
static bool acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context)

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

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

◆ AcquireRewriteLocks()

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

Definition at line 149 of file rewriteHandler.c.

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

References AccessShareLock, acquireLocksOnSubLinks(), AcquireRewriteLocks(), Assert, Query::cteList, CommonTableExpr::ctequery, elog, ERROR, fb(), acquireLocksOnSubLinks_context::for_execute, get_parse_rowmark(), get_rte_attribute_is_dropped(), IsA, lappend(), lfirst, NIL, NoLock, QTW_IGNORE_RC_SUBQUERIES, query_tree_walker, RelationData::rd_rel, relation_close(), relation_open(), RowShareLock, rt_fetch, Query::rtable, RTE_GRAPH_TABLE, RTE_JOIN, RTE_RELATION, RTE_SUBQUERY, and strip_implicit_coercions().

Referenced by acquireLocksOnSubLinks(), AcquireRewriteLocks(), ApplyRetrieveRule(), fmgr_sql_validator(), get_query_def(), inline_sql_function_in_from(), make_ruledef(), prepare_next_query(), print_function_sqlbody(), refresh_matview_datafill(), RevalidateCachedQuery(), rewriteGraphTable(), and rewriteRuleAction().

◆ adjust_view_column_set()

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

Definition at line 3073 of file rewriteHandler.c.

3074{
3076 int col;
3077
3078 col = -1;
3079 while ((col = bms_next_member(cols, col)) >= 0)
3080 {
3081 /* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
3083
3084 if (attno == InvalidAttrNumber)
3085 {
3086 /*
3087 * There's a whole-row reference to the view. For permissions
3088 * purposes, treat it as a reference to each column available from
3089 * the view. (We should *not* convert this to a whole-row
3090 * reference to the base relation, since the view may not touch
3091 * all columns of the base relation.)
3092 */
3093 ListCell *lc;
3094
3095 foreach(lc, targetlist)
3096 {
3098 Var *var;
3099
3100 if (tle->resjunk)
3101 continue;
3102 var = castNode(Var, tle->expr);
3105 }
3106 }
3107 else
3108 {
3109 /*
3110 * Views do not have system columns, so we do not expect to see
3111 * any other system attnos here. If we do find one, the error
3112 * case will apply.
3113 */
3114 TargetEntry *tle = get_tle_by_resno(targetlist, attno);
3115
3116 if (tle != NULL && !tle->resjunk && IsA(tle->expr, Var))
3117 {
3118 Var *var = (Var *) tle->expr;
3119
3121 var->varattno - FirstLowInvalidHeapAttributeNumber);
3122 }
3123 else
3124 elog(ERROR, "attribute number %d not found in view targetlist",
3125 attno);
3126 }
3127 }
3128
3129 return result;
3130}
int16 AttrNumber
Definition attnum.h:21
#define InvalidAttrNumber
Definition attnum.h:23
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
uint32 result
#define castNode(_type_, nodeptr)
Definition nodes.h:182
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define lfirst_node(type, lc)
Definition pg_list.h:176
AttrNumber varattno
Definition primnodes.h:275
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27

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

Referenced by relation_is_updatable(), and rewriteTargetView().

◆ adjustJoinTreeList()

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

Definition at line 728 of file rewriteHandler.c.

729{
731 ListCell *l;
732
733 if (removert)
734 {
735 foreach(l, newjointree)
736 {
737 RangeTblRef *rtr = lfirst(l);
738
739 if (IsA(rtr, RangeTblRef) &&
740 rtr->rtindex == rt_index)
741 {
743 break;
744 }
745 }
746 }
747 return newjointree;
748}
#define copyObject(obj)
Definition nodes.h:232
#define foreach_delete_current(lst, var_or_cell)
Definition pg_list.h:423
List * fromlist
Definition primnodes.h:2384
FromExpr * jointree
Definition parsenodes.h:185

References copyObject, fb(), foreach_delete_current, FromExpr::fromlist, IsA, Query::jointree, and lfirst.

Referenced by rewriteRuleAction().

◆ ApplyRetrieveRule()

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

Definition at line 1729 of file rewriteHandler.c.

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

References AcquireRewriteLocks(), ChangeVarNodes(), CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_UPDATE, Query::commandType, copyObject, elog, ereport, errcode(), errmsg, ERROR, ExecCleanTargetListLength(), fb(), fireRIRrules(), FirstNormalObjectId, get_parse_rowmark(), lappend(), linitial, list_length(), makeString(), makeTargetEntry(), makeWholeRowVar(), markQueryForLocking(), pstrdup(), RelationGetRelationName, RelationGetRelid, RelationIsSecurityView, restrict_nonsystem_relation_kind, RESTRICT_RELKIND_VIEW, Query::returningList, rt_fetch, Query::rtable, RTE_SUBQUERY, RowMarkClause::strength, Query::targetList, unlikely, and RowMarkClause::waitPolicy.

Referenced by fireRIRrules().

◆ build_column_default()

Node * build_column_default ( Relation  rel,
int  attrno 
)

Definition at line 1246 of file rewriteHandler.c.

1247{
1248 TupleDesc rd_att = rel->rd_att;
1250 Oid atttype = att_tup->atttypid;
1251 int32 atttypmod = att_tup->atttypmod;
1252 Node *expr = NULL;
1253 Oid exprtype;
1254
1255 if (att_tup->attidentity)
1256 {
1258
1259 nve->seqid = getIdentitySequence(rel, attrno, false);
1260 nve->typeId = att_tup->atttypid;
1261
1262 return (Node *) nve;
1263 }
1264
1265 /*
1266 * If relation has a default for this column, fetch that expression.
1267 */
1268 if (att_tup->atthasdef)
1269 {
1270 expr = TupleDescGetDefault(rd_att, attrno);
1271 if (expr == NULL)
1272 elog(ERROR, "default expression not found for attribute %d of relation \"%s\"",
1274 }
1275
1276 /*
1277 * No per-column default, so look for a default for the type itself. But
1278 * not for generated columns.
1279 */
1280 if (expr == NULL && !att_tup->attgenerated)
1281 expr = get_typdefault(atttype);
1282
1283 if (expr == NULL)
1284 return NULL; /* No default anywhere */
1285
1286 /*
1287 * Make sure the value is coerced to the target column type; this will
1288 * generally be true already, but there seem to be some corner cases
1289 * involving domain defaults where it might not be true. This should match
1290 * the parser's processing of non-defaulted expressions --- see
1291 * transformAssignedExpr().
1292 */
1293 exprtype = exprType(expr);
1294
1295 expr = coerce_to_target_type(NULL, /* no UNKNOWN params here */
1296 expr, exprtype,
1297 atttype, atttypmod,
1300 -1);
1301 if (expr == NULL)
1302 ereport(ERROR,
1304 errmsg("column \"%s\" is of type %s"
1305 " but default expression is of type %s",
1306 NameStr(att_tup->attname),
1307 format_type_be(atttype),
1308 format_type_be(exprtype)),
1309 errhint("You will need to rewrite or cast the expression.")));
1310
1311 return expr;
1312}
#define NameStr(name)
Definition c.h:835
int32_t int32
Definition c.h:620
int errhint(const char *fmt,...) pg_attribute_printf(1
char * format_type_be(Oid type_oid)
Node * get_typdefault(Oid typid)
Definition lsyscache.c:2670
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
#define makeNode(_type_)
Definition nodes.h:161
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
FormData_pg_attribute * Form_pg_attribute
Oid getIdentitySequence(Relation rel, AttrNumber attnum, bool missing_ok)
Definition pg_depend.c:1018
unsigned int Oid
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
@ COERCION_ASSIGNMENT
Definition primnodes.h:748
TupleDesc rd_att
Definition rel.h:112
Node * TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
Definition tupdesc.c:1152
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178

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

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

◆ build_generation_expression()

Node * build_generation_expression ( Relation  rel,
int  attrno 
)

Definition at line 4619 of file rewriteHandler.c.

4620{
4621 TupleDesc rd_att = RelationGetDescr(rel);
4623 Node *defexpr;
4624 Oid attcollid;
4625
4626 Assert(rd_att->constr && rd_att->constr->has_generated_virtual);
4627 Assert(att_tup->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL);
4628
4629 defexpr = build_column_default(rel, attrno);
4630 if (defexpr == NULL)
4631 elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
4633
4634 /*
4635 * If the column definition has a collation and it is different from the
4636 * collation of the generation expression, put a COLLATE clause around the
4637 * expression.
4638 */
4639 attcollid = att_tup->attcollation;
4640 if (attcollid && attcollid != exprCollation(defexpr))
4641 {
4643
4644 ce->arg = (Expr *) defexpr;
4645 ce->collOid = attcollid;
4646 ce->location = -1;
4647
4648 defexpr = (Node *) ce;
4649 }
4650
4651 return defexpr;
4652}
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
#define RelationGetDescr(relation)
Definition rel.h:542
Node * build_column_default(Relation rel, int attrno)
bool has_generated_virtual
Definition tupdesc.h:47
TupleConstr * constr
Definition tupdesc.h:159

References Assert, build_column_default(), TupleDescData::constr, elog, ERROR, exprCollation(), fb(), TupleConstr::has_generated_virtual, makeNode, RelationGetDescr, RelationGetRelationName, and TupleDescAttr().

Referenced by createTableConstraints(), ExecRelGenVirtualNotNull(), expand_generated_columns_internal(), and expand_virtual_generated_columns().

◆ CopyAndAddInvertedQual()

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

Definition at line 2348 of file rewriteHandler.c.

2352{
2353 /* Don't scribble on the passed qual (it's in the relcache!) */
2356
2357 context.for_execute = true;
2358
2359 /*
2360 * In case there are subqueries in the qual, acquire necessary locks and
2361 * fix any deleted JOIN RTE entries. (This is somewhat redundant with
2362 * rewriteRuleAction, but not entirely ... consider restructuring so that
2363 * we only need to process the qual this way once.)
2364 */
2366
2367 /* Fix references to OLD */
2368 ChangeVarNodes(new_qual, PRS2_OLD_VARNO, rt_index, 0);
2369 /* Fix references to NEW */
2370 if (event == CMD_INSERT || event == CMD_UPDATE)
2373 0,
2374 rt_fetch(rt_index,
2375 parsetree->rtable),
2376 parsetree->targetList,
2377 parsetree->resultRelation,
2378 (event == CMD_UPDATE) ?
2381 rt_index,
2382 &parsetree->hasSubLinks);
2383 /* And attach the fixed qual */
2384 AddInvertedQual(parsetree, new_qual);
2385
2386 return parsetree;
2387}
#define PRS2_OLD_VARNO
Definition primnodes.h:251
#define PRS2_NEW_VARNO
Definition primnodes.h:252
void AddInvertedQual(Query *parsetree, Node *qual)
Node * ReplaceVarsFromTargetList(Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, int result_relation, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
@ REPLACEVARS_SUBSTITUTE_NULL
@ REPLACEVARS_CHANGE_VARNO

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

Referenced by fireRules().

◆ error_view_not_updatable()

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

Definition at line 3147 of file rewriteHandler.c.

3151{
3152 TriggerDesc *trigDesc = view->trigdesc;
3153
3154 switch (command)
3155 {
3156 case CMD_INSERT:
3157 ereport(ERROR,
3159 errmsg("cannot insert into view \"%s\"",
3161 detail ? errdetail_internal("%s", _(detail)) : 0,
3162 errhint("To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule."));
3163 break;
3164 case CMD_UPDATE:
3165 ereport(ERROR,
3167 errmsg("cannot update view \"%s\"",
3169 detail ? errdetail_internal("%s", _(detail)) : 0,
3170 errhint("To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule."));
3171 break;
3172 case CMD_DELETE:
3173 ereport(ERROR,
3175 errmsg("cannot delete from view \"%s\"",
3177 detail ? errdetail_internal("%s", _(detail)) : 0,
3178 errhint("To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule."));
3179 break;
3180 case CMD_MERGE:
3181
3182 /*
3183 * Note that the error hints here differ from above, since MERGE
3184 * doesn't support rules.
3185 */
3186 foreach_node(MergeAction, action, mergeActionList)
3187 {
3188 switch (action->commandType)
3189 {
3190 case CMD_INSERT:
3191 if (!trigDesc || !trigDesc->trig_insert_instead_row)
3192 ereport(ERROR,
3194 errmsg("cannot insert into view \"%s\"",
3196 detail ? errdetail_internal("%s", _(detail)) : 0,
3197 errhint("To enable inserting into the view using MERGE, provide an INSTEAD OF INSERT trigger."));
3198 break;
3199 case CMD_UPDATE:
3200 if (!trigDesc || !trigDesc->trig_update_instead_row)
3201 ereport(ERROR,
3203 errmsg("cannot update view \"%s\"",
3205 detail ? errdetail_internal("%s", _(detail)) : 0,
3206 errhint("To enable updating the view using MERGE, provide an INSTEAD OF UPDATE trigger."));
3207 break;
3208 case CMD_DELETE:
3209 if (!trigDesc || !trigDesc->trig_delete_instead_row)
3210 ereport(ERROR,
3212 errmsg("cannot delete from view \"%s\"",
3214 detail ? errdetail_internal("%s", _(detail)) : 0,
3215 errhint("To enable deleting from the view using MERGE, provide an INSTEAD OF DELETE trigger."));
3216 break;
3217 case CMD_NOTHING:
3218 break;
3219 default:
3220 elog(ERROR, "unrecognized commandType: %d", action->commandType);
3221 break;
3222 }
3223 }
3224 break;
3225 default:
3226 elog(ERROR, "unrecognized CmdType: %d", (int) command);
3227 break;
3228 }
3229}
#define _(x)
Definition elog.c:95
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
@ CMD_NOTHING
Definition nodes.h:282
#define foreach_node(type, var, lst)
Definition pg_list.h:528
TriggerDesc * trigdesc
Definition rel.h:117

References _, CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_NOTHING, CMD_UPDATE, elog, ereport, errcode(), errdetail_internal(), errhint(), errmsg, ERROR, fb(), foreach_node, RelationGetRelationName, and RelationData::trigdesc.

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

◆ expand_generated_columns_in_expr()

Node * expand_generated_columns_in_expr ( Node node,
Relation  rel,
int  rt_index 
)

Definition at line 4593 of file rewriteHandler.c.

4594{
4595 TupleDesc tupdesc = RelationGetDescr(rel);
4596
4597 if (tupdesc->constr && tupdesc->constr->has_generated_virtual)
4598 {
4600
4602 /* eref needs to be set, but the actual name doesn't matter */
4604 rte->rtekind = RTE_RELATION;
4605 rte->relid = RelationGetRelid(rel);
4606
4607 node = expand_generated_columns_internal(node, rel, rt_index, rte, 0);
4608 }
4609
4610 return node;
4611}
Alias * makeAlias(const char *aliasname, List *colnames)
Definition makefuncs.c:438
static Node * expand_generated_columns_internal(Node *node, Relation rel, int rt_index, RangeTblEntry *rte, int result_relation)

References TupleDescData::constr, expand_generated_columns_internal(), fb(), TupleConstr::has_generated_virtual, makeAlias(), makeNode, NIL, RelationGetDescr, RelationGetRelationName, RelationGetRelid, and RTE_RELATION.

Referenced by ATExecAlterCheckConstrEnforceability(), ATPrepAlterColumnType(), ATRewriteTable(), createTableConstraints(), ExecRelCheck(), fetch_statentries_for_relation(), get_relation_constraints(), get_relation_statistics(), pgoutput_row_filter_init(), QueueCheckConstraintValidation(), TransformPubWhereClauses(), and TriggerEnabled().

◆ expand_generated_columns_internal()

static Node * expand_generated_columns_internal ( Node node,
Relation  rel,
int  rt_index,
RangeTblEntry rte,
int  result_relation 
)
static

Definition at line 4548 of file rewriteHandler.c.

4550{
4551 TupleDesc tupdesc;
4552
4553 tupdesc = RelationGetDescr(rel);
4554 if (tupdesc->constr && tupdesc->constr->has_generated_virtual)
4555 {
4556 List *tlist = NIL;
4557
4558 for (int i = 0; i < tupdesc->natts; i++)
4559 {
4560 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
4561
4562 if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
4563 {
4564 Node *defexpr;
4565 TargetEntry *te;
4566
4567 defexpr = build_generation_expression(rel, i + 1);
4568 ChangeVarNodes(defexpr, 1, rt_index, 0);
4569
4570 te = makeTargetEntry((Expr *) defexpr, i + 1, 0, false);
4571 tlist = lappend(tlist, te);
4572 }
4573 }
4574
4575 Assert(list_length(tlist) > 0);
4576
4577 node = ReplaceVarsFromTargetList(node, rt_index, 0, rte, tlist,
4578 result_relation,
4579 REPLACEVARS_CHANGE_VARNO, rt_index,
4580 NULL);
4581 }
4582
4583 return node;
4584}
int i
Definition isn.c:77
Node * build_generation_expression(Relation rel, int attrno)

References Assert, build_generation_expression(), ChangeVarNodes(), TupleDescData::constr, fb(), TupleConstr::has_generated_virtual, i, lappend(), list_length(), makeTargetEntry(), TupleDescData::natts, NIL, RelationGetDescr, REPLACEVARS_CHANGE_VARNO, ReplaceVarsFromTargetList(), and TupleDescAttr().

Referenced by expand_generated_columns_in_expr().

◆ findDefaultOnlyColumns()

static Bitmapset * findDefaultOnlyColumns ( RangeTblEntry rte)
static

Definition at line 1343 of file rewriteHandler.c.

1344{
1346 ListCell *lc;
1347
1348 foreach(lc, rte->values_lists)
1349 {
1350 List *sublist = (List *) lfirst(lc);
1351 ListCell *lc2;
1352 int i;
1353
1354 if (default_only_cols == NULL)
1355 {
1356 /* Populate the initial result bitmap from the first row */
1357 i = 0;
1358 foreach(lc2, sublist)
1359 {
1360 Node *col = (Node *) lfirst(lc2);
1361
1362 i++;
1363 if (IsA(col, SetToDefault))
1365 }
1366 }
1367 else
1368 {
1369 /* Update the result bitmap from this next row */
1370 i = 0;
1371 foreach(lc2, sublist)
1372 {
1373 Node *col = (Node *) lfirst(lc2);
1374
1375 i++;
1376 if (!IsA(col, SetToDefault))
1378 }
1379 }
1380
1381 /*
1382 * If no column in the rows read so far contains only DEFAULT items,
1383 * we are done.
1384 */
1386 break;
1387 }
1388
1389 return default_only_cols;
1390}
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:852
#define bms_is_empty(a)
Definition bitmapset.h:118

References bms_add_member(), bms_del_member(), bms_is_empty, fb(), i, IsA, and lfirst.

Referenced by rewriteTargetListIU().

◆ fireRIRonSubLink()

static bool fireRIRonSubLink ( Node node,
fireRIRonSubLink_context context 
)
static

Definition at line 1973 of file rewriteHandler.c.

1974{
1975 if (node == NULL)
1976 return false;
1977 if (IsA(node, SubLink))
1978 {
1979 SubLink *sub = (SubLink *) node;
1980
1981 /* Do what we came for */
1982 sub->subselect = (Node *) fireRIRrules((Query *) sub->subselect,
1983 context->activeRIRs);
1984
1985 /*
1986 * Remember if any of the sublinks have row security.
1987 */
1988 context->hasRowSecurity |= ((Query *) sub->subselect)->hasRowSecurity;
1989
1990 /* Fall through to process lefthand args of SubLink */
1991 }
1992
1993 /*
1994 * Do NOT recurse into Query nodes, because fireRIRrules already processed
1995 * subselects of subselects for us.
1996 */
1997 return expression_tree_walker(node, fireRIRonSubLink, context);
1998}
static bool fireRIRonSubLink(Node *node, fireRIRonSubLink_context *context)

References fireRIRonSubLink_context::activeRIRs, expression_tree_walker, fb(), fireRIRonSubLink(), fireRIRrules(), fireRIRonSubLink_context::hasRowSecurity, IsA, and SubLink::subselect.

Referenced by fireRIRonSubLink(), and fireRIRrules().

◆ fireRIRrules()

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

Definition at line 2009 of file rewriteHandler.c.

2010{
2011 int origResultRelation = parsetree->resultRelation;
2012 int rt_index;
2013 ListCell *lc;
2014
2015 /*
2016 * Expand SEARCH and CYCLE clauses in CTEs.
2017 *
2018 * This is just a convenient place to do this, since we are already
2019 * looking at each Query.
2020 */
2021 foreach(lc, parsetree->cteList)
2022 {
2024
2025 if (cte->search_clause || cte->cycle_clause)
2026 {
2027 cte = rewriteSearchAndCycle(cte);
2028 lfirst(lc) = cte;
2029 }
2030 }
2031
2032 /*
2033 * don't try to convert this into a foreach loop, because rtable list can
2034 * get changed each time through...
2035 */
2036 rt_index = 0;
2037 while (rt_index < list_length(parsetree->rtable))
2038 {
2040 Relation rel;
2041 List *locks;
2042 RuleLock *rules;
2044 int i;
2045
2046 ++rt_index;
2047
2048 rte = rt_fetch(rt_index, parsetree->rtable);
2049
2050 /*
2051 * Convert GRAPH_TABLE clause into a subquery using relational
2052 * operators. (This will change the rtekind to subquery, so it must
2053 * be done before the subquery handling below.)
2054 */
2055 if (rte->rtekind == RTE_GRAPH_TABLE)
2056 {
2057 parsetree = rewriteGraphTable(parsetree, rt_index);
2058 }
2059
2060 /*
2061 * A subquery RTE can't have associated rules, so there's nothing to
2062 * do to this level of the query, but we must recurse into the
2063 * subquery to expand any rule references in it.
2064 */
2065 if (rte->rtekind == RTE_SUBQUERY)
2066 {
2067 rte->subquery = fireRIRrules(rte->subquery, activeRIRs);
2068
2069 /*
2070 * While we are here, make sure the query is marked as having row
2071 * security if any of its subqueries do.
2072 */
2073 parsetree->hasRowSecurity |= rte->subquery->hasRowSecurity;
2074
2075 continue;
2076 }
2077
2078 /*
2079 * Joins and other non-relation RTEs can be ignored completely.
2080 */
2081 if (rte->rtekind != RTE_RELATION)
2082 continue;
2083
2084 /*
2085 * Always ignore RIR rules for materialized views referenced in
2086 * queries. (This does not prevent refreshing MVs, since they aren't
2087 * referenced in their own query definitions.)
2088 *
2089 * Note: in the future we might want to allow MVs to be conditionally
2090 * expanded as if they were regular views, if they are not scannable.
2091 * In that case this test would need to be postponed till after we've
2092 * opened the rel, so that we could check its state.
2093 */
2094 if (rte->relkind == RELKIND_MATVIEW)
2095 continue;
2096
2097 /*
2098 * In INSERT ... ON CONFLICT, ignore the EXCLUDED pseudo-relation;
2099 * even if it points to a view, we needn't expand it, and should not
2100 * because we want the RTE to remain of RTE_RELATION type. Otherwise,
2101 * it would get changed to RTE_SUBQUERY type, which is an
2102 * untested/unsupported situation.
2103 */
2104 if (parsetree->onConflict &&
2105 rt_index == parsetree->onConflict->exclRelIndex)
2106 continue;
2107
2108 /*
2109 * If the table is not referenced in the query, then we ignore it.
2110 * This prevents infinite expansion loop due to new rtable entries
2111 * inserted by expansion of a rule. A table is referenced if it is
2112 * part of the join set (a source table), or is referenced by any Var
2113 * nodes, or is the result table.
2114 */
2115 if (rt_index != parsetree->resultRelation &&
2116 !rangeTableEntry_used((Node *) parsetree, rt_index, 0))
2117 continue;
2118
2119 /*
2120 * Also, if this is a new result relation introduced by
2121 * ApplyRetrieveRule, we don't want to do anything more with it.
2122 */
2123 if (rt_index == parsetree->resultRelation &&
2124 rt_index != origResultRelation)
2125 continue;
2126
2127 /*
2128 * We can use NoLock here since either the parser or
2129 * AcquireRewriteLocks should have locked the rel already.
2130 */
2131 rel = relation_open(rte->relid, NoLock);
2132
2133 /*
2134 * Collect the RIR rules that we must apply
2135 */
2136 rules = rel->rd_rules;
2137 if (rules != NULL)
2138 {
2139 locks = NIL;
2140 for (i = 0; i < rules->numLocks; i++)
2141 {
2142 rule = rules->rules[i];
2143 if (rule->event != CMD_SELECT)
2144 continue;
2145
2146 locks = lappend(locks, rule);
2147 }
2148
2149 /*
2150 * If we found any, apply them --- but first check for recursion!
2151 */
2152 if (locks != NIL)
2153 {
2154 ListCell *l;
2155
2156 if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
2157 ereport(ERROR,
2159 errmsg("infinite recursion detected in rules for relation \"%s\"",
2161 activeRIRs = lappend_oid(activeRIRs, RelationGetRelid(rel));
2162
2163 foreach(l, locks)
2164 {
2165 rule = lfirst(l);
2166
2167 parsetree = ApplyRetrieveRule(parsetree,
2168 rule,
2169 rt_index,
2170 rel,
2171 activeRIRs);
2172 }
2173
2174 activeRIRs = list_delete_last(activeRIRs);
2175 }
2176 }
2177
2178 table_close(rel, NoLock);
2179 }
2180
2181 /* Recurse into subqueries in WITH */
2182 foreach(lc, parsetree->cteList)
2183 {
2185
2186 cte->ctequery = (Node *)
2187 fireRIRrules((Query *) cte->ctequery, activeRIRs);
2188
2189 /*
2190 * While we are here, make sure the query is marked as having row
2191 * security if any of its CTEs do.
2192 */
2193 parsetree->hasRowSecurity |= ((Query *) cte->ctequery)->hasRowSecurity;
2194 }
2195
2196 /*
2197 * Recurse into sublink subqueries, too. But we already did the ones in
2198 * the rtable and cteList.
2199 */
2200 if (parsetree->hasSubLinks)
2201 {
2203
2204 context.activeRIRs = activeRIRs;
2205 context.hasRowSecurity = false;
2206
2207 query_tree_walker(parsetree, fireRIRonSubLink, &context,
2209
2210 /*
2211 * Make sure the query is marked as having row security if any of its
2212 * sublinks do.
2213 */
2214 parsetree->hasRowSecurity |= context.hasRowSecurity;
2215 }
2216
2217 /*
2218 * Apply any row-level security policies. We do this last because it
2219 * requires special recursion detection if the new quals have sublink
2220 * subqueries, and if we did it in the loop above query_tree_walker would
2221 * then recurse into those quals a second time.
2222 */
2223 rt_index = 0;
2224 foreach(lc, parsetree->rtable)
2225 {
2227 Relation rel;
2230 bool hasRowSecurity;
2231 bool hasSubLinks;
2232
2233 ++rt_index;
2234
2235 /* Only normal relations can have RLS policies */
2236 if (rte->rtekind != RTE_RELATION ||
2237 (rte->relkind != RELKIND_RELATION &&
2238 rte->relkind != RELKIND_PARTITIONED_TABLE))
2239 continue;
2240
2241 rel = relation_open(rte->relid, NoLock);
2242
2243 /*
2244 * Fetch any new security quals that must be applied to this RTE.
2245 */
2246 get_row_security_policies(parsetree, rte, rt_index,
2248 &hasRowSecurity, &hasSubLinks);
2249
2251 {
2252 if (hasSubLinks)
2253 {
2256
2257 /*
2258 * Recursively process the new quals, checking for infinite
2259 * recursion.
2260 */
2261 if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
2262 ereport(ERROR,
2264 errmsg("infinite recursion detected in policy for relation \"%s\"",
2266
2267 activeRIRs = lappend_oid(activeRIRs, RelationGetRelid(rel));
2268
2269 /*
2270 * get_row_security_policies just passed back securityQuals
2271 * and/or withCheckOptions, and there were SubLinks, make sure
2272 * we lock any relations which are referenced.
2273 *
2274 * These locks would normally be acquired by the parser, but
2275 * securityQuals and withCheckOptions are added post-parsing.
2276 */
2277 context.for_execute = true;
2280 &context);
2281
2282 /*
2283 * Now that we have the locks on anything added by
2284 * get_row_security_policies, fire any RIR rules for them.
2285 */
2286 fire_context.activeRIRs = activeRIRs;
2287 fire_context.hasRowSecurity = false;
2288
2291
2294
2295 /*
2296 * We can ignore the value of fire_context.hasRowSecurity
2297 * since we only reach this code in cases where hasRowSecurity
2298 * is already true.
2299 */
2300 Assert(hasRowSecurity);
2301
2302 activeRIRs = list_delete_last(activeRIRs);
2303 }
2304
2305 /*
2306 * Add the new security barrier quals to the start of the RTE's
2307 * list so that they get applied before any existing barrier quals
2308 * (which would have come from a security-barrier view, and should
2309 * get lower priority than RLS conditions on the table itself).
2310 */
2311 rte->securityQuals = list_concat(securityQuals,
2312 rte->securityQuals);
2313
2314 parsetree->withCheckOptions = list_concat(withCheckOptions,
2315 parsetree->withCheckOptions);
2316 }
2317
2318 /*
2319 * Make sure the query is marked correctly if row-level security
2320 * applies, or if the new quals had sublinks.
2321 */
2322 if (hasRowSecurity)
2323 parsetree->hasRowSecurity = true;
2324 if (hasSubLinks)
2325 parsetree->hasSubLinks = true;
2326
2327 table_close(rel, NoLock);
2328 }
2329
2330 return parsetree;
2331}
List * list_concat(List *list1, const List *list2)
Definition list.c:561
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
List * list_delete_last(List *list)
Definition list.c:957
bool list_member_oid(const List *list, Oid datum)
Definition list.c:722
@ CMD_SELECT
Definition nodes.h:275
Query * rewriteGraphTable(Query *parsetree, int rt_index)
static Query * ApplyRetrieveRule(Query *parsetree, RewriteRule *rule, int rt_index, Relation relation, List *activeRIRs)
bool rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
CommonTableExpr * rewriteSearchAndCycle(CommonTableExpr *cte)
void get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index, List **securityQuals, List **withCheckOptions, bool *hasRowSecurity, bool *hasSubLinks)
Definition rowsecurity.c:98
OnConflictExpr * onConflict
Definition parsenodes.h:206
RuleLock * rd_rules
Definition rel.h:115
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
static struct rule * rules
Definition zic.c:286

References acquireLocksOnSubLinks(), fireRIRonSubLink_context::activeRIRs, ApplyRetrieveRule(), Assert, CMD_SELECT, Query::cteList, CommonTableExpr::ctequery, ereport, errcode(), errmsg, ERROR, OnConflictExpr::exclRelIndex, expression_tree_walker, fb(), fireRIRonSubLink(), fireRIRrules(), acquireLocksOnSubLinks_context::for_execute, get_row_security_policies(), fireRIRonSubLink_context::hasRowSecurity, i, lappend(), lappend_oid(), lfirst, lfirst_node, list_concat(), list_delete_last(), list_length(), list_member_oid(), NIL, NoLock, Query::onConflict, QTW_IGNORE_RC_SUBQUERIES, query_tree_walker, rangeTableEntry_used(), RelationData::rd_rules, relation_open(), RelationGetRelationName, RelationGetRelid, rewriteGraphTable(), rewriteSearchAndCycle(), rt_fetch, Query::rtable, RTE_GRAPH_TABLE, RTE_RELATION, RTE_SUBQUERY, rules, and table_close().

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

◆ fireRules()

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

Definition at line 2419 of file rewriteHandler.c.

2426{
2427 List *results = NIL;
2428 ListCell *l;
2429
2430 foreach(l, locks)
2431 {
2433 Node *event_qual = rule_lock->qual;
2434 List *actions = rule_lock->actions;
2436 ListCell *r;
2437
2438 /* Determine correct QuerySource value for actions */
2439 if (rule_lock->isInstead)
2440 {
2441 if (event_qual != NULL)
2443 else
2444 {
2446 *instead_flag = true; /* report unqualified INSTEAD */
2447 }
2448 }
2449 else
2451
2453 {
2454 /*
2455 * If there are INSTEAD rules with qualifications, the original
2456 * query is still performed. But all the negated rule
2457 * qualifications of the INSTEAD rules are added so it does its
2458 * actions only in cases where the rule quals of all INSTEAD rules
2459 * are false. Think of it as the default action in a case. We save
2460 * this in *qual_product so RewriteQuery() can add it to the query
2461 * list after we mangled it up enough.
2462 *
2463 * If we have already found an unqualified INSTEAD rule, then
2464 * *qual_product won't be used, so don't bother building it.
2465 */
2466 if (!*instead_flag)
2467 {
2468 if (*qual_product == NULL)
2469 *qual_product = copyObject(parsetree);
2471 event_qual,
2472 rt_index,
2473 event);
2474 }
2475 }
2476
2477 /* Now process the rule's actions and add them to the result list */
2478 foreach(r, actions)
2479 {
2480 Query *rule_action = lfirst(r);
2481
2482 if (rule_action->commandType == CMD_NOTHING)
2483 continue;
2484
2486 event_qual, rt_index, event,
2488
2489 rule_action->querySource = qsrc;
2490 rule_action->canSetTag = false; /* might change later */
2491
2492 results = lappend(results, rule_action);
2493 }
2494 }
2495
2496 return results;
2497}
QuerySource
Definition parsenodes.h:35
@ QSRC_NON_INSTEAD_RULE
Definition parsenodes.h:40
@ QSRC_QUAL_INSTEAD_RULE
Definition parsenodes.h:39
@ QSRC_INSTEAD_RULE
Definition parsenodes.h:38
static Query * rewriteRuleAction(Query *parsetree, Query *rule_action, Node *rule_qual, int rt_index, CmdType event, bool *returning_flag)
static Query * CopyAndAddInvertedQual(Query *parsetree, Node *rule_qual, int rt_index, CmdType event)

References CMD_NOTHING, CopyAndAddInvertedQual(), copyObject, fb(), lappend(), lfirst, NIL, QSRC_INSTEAD_RULE, QSRC_NON_INSTEAD_RULE, QSRC_QUAL_INSTEAD_RULE, and rewriteRuleAction().

Referenced by RewriteQuery().

◆ get_assignment_input()

static Node * get_assignment_input ( Node node)
static

Definition at line 1217 of file rewriteHandler.c.

1218{
1219 if (node == NULL)
1220 return NULL;
1221 if (IsA(node, FieldStore))
1222 {
1223 FieldStore *fstore = (FieldStore *) node;
1224
1225 return (Node *) fstore->arg;
1226 }
1227 else if (IsA(node, SubscriptingRef))
1228 {
1229 SubscriptingRef *sbsref = (SubscriptingRef *) node;
1230
1231 if (sbsref->refassgnexpr == NULL)
1232 return NULL;
1233
1234 return (Node *) sbsref->refexpr;
1235 }
1236
1237 return NULL;
1238}
Expr * arg
Definition primnodes.h:1194
Expr * refassgnexpr
Definition primnodes.h:736

References FieldStore::arg, fb(), IsA, SubscriptingRef::refassgnexpr, and SubscriptingRef::refexpr.

Referenced by process_matched_tle().

◆ get_view_query()

Query * get_view_query ( Relation  view)

Definition at line 2510 of file rewriteHandler.c.

2511{
2512 int i;
2513
2514 Assert(view->rd_rel->relkind == RELKIND_VIEW);
2515
2516 for (i = 0; i < view->rd_rules->numLocks; i++)
2517 {
2518 RewriteRule *rule = view->rd_rules->rules[i];
2519
2520 if (rule->event == CMD_SELECT)
2521 {
2522 /* A _RETURN rule should have only one action */
2523 if (list_length(rule->actions) != 1)
2524 elog(ERROR, "invalid _RETURN rule action specification");
2525
2526 return (Query *) linitial(rule->actions);
2527 }
2528 }
2529
2530 elog(ERROR, "failed to find _RETURN rule for view");
2531 return NULL; /* keep compiler quiet */
2532}
RewriteRule ** rules
Definition prs2lock.h:43
int numLocks
Definition prs2lock.h:42

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

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

◆ markQueryForLocking()

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

Definition at line 1909 of file rewriteHandler.c.

1912{
1913 if (jtnode == NULL)
1914 return;
1915 if (IsA(jtnode, RangeTblRef))
1916 {
1917 int rti = ((RangeTblRef *) jtnode)->rtindex;
1918 RangeTblEntry *rte = rt_fetch(rti, qry->rtable);
1919
1920 if (rte->rtekind == RTE_RELATION)
1921 {
1923
1924 applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
1925
1926 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
1927 perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
1928 }
1929 else if (rte->rtekind == RTE_SUBQUERY)
1930 {
1931 applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
1932 /* FOR UPDATE/SHARE of subquery is propagated to subquery's rels */
1933 markQueryForLocking(rte->subquery, (Node *) rte->subquery->jointree,
1934 strength, waitPolicy, true);
1935 }
1936 /* other RTE types are unaffected by FOR UPDATE */
1937 }
1938 else if (IsA(jtnode, FromExpr))
1939 {
1940 FromExpr *f = (FromExpr *) jtnode;
1941 ListCell *l;
1942
1943 foreach(l, f->fromlist)
1944 markQueryForLocking(qry, lfirst(l), strength, waitPolicy, pushedDown);
1945 }
1946 else if (IsA(jtnode, JoinExpr))
1947 {
1948 JoinExpr *j = (JoinExpr *) jtnode;
1949
1950 markQueryForLocking(qry, j->larg, strength, waitPolicy, pushedDown);
1951 markQueryForLocking(qry, j->rarg, strength, waitPolicy, pushedDown);
1952 }
1953 else
1954 elog(ERROR, "unrecognized node type: %d",
1955 (int) nodeTag(jtnode));
1956}
int j
Definition isn.c:78
#define nodeTag(nodeptr)
Definition nodes.h:139
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
#define ACL_SELECT_FOR_UPDATE
Definition parsenodes.h:94
void applyLockingClause(Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
Definition analyze.c:4020

References ACL_SELECT_FOR_UPDATE, applyLockingClause(), elog, ERROR, fb(), FromExpr::fromlist, getRTEPermissionInfo(), IsA, j, lfirst, markQueryForLocking(), nodeTag, rt_fetch, Query::rtable, RTE_RELATION, and RTE_SUBQUERY.

Referenced by ApplyRetrieveRule(), and markQueryForLocking().

◆ matchLocks()

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

Definition at line 1654 of file rewriteHandler.c.

1659{
1660 RuleLock *rulelocks = relation->rd_rules;
1662 int nlocks;
1663 int i;
1664
1665 if (rulelocks == NULL)
1666 return NIL;
1667
1668 if (parsetree->commandType != CMD_SELECT)
1669 {
1670 if (parsetree->resultRelation != varno)
1671 return NIL;
1672 }
1673
1674 nlocks = rulelocks->numLocks;
1675
1676 for (i = 0; i < nlocks; i++)
1677 {
1678 RewriteRule *oneLock = rulelocks->rules[i];
1679
1680 if (oneLock->event == CMD_UPDATE)
1681 *hasUpdate = true;
1682
1683 /*
1684 * Suppress ON INSERT/UPDATE/DELETE rules that are disabled or
1685 * configured to not fire during the current session's replication
1686 * role. ON SELECT rules will always be applied in order to keep views
1687 * working even in LOCAL or REPLICA role.
1688 */
1689 if (oneLock->event != CMD_SELECT)
1690 {
1692 {
1693 if (oneLock->enabled == RULE_FIRES_ON_ORIGIN ||
1694 oneLock->enabled == RULE_DISABLED)
1695 continue;
1696 }
1697 else /* ORIGIN or LOCAL ROLE */
1698 {
1699 if (oneLock->enabled == RULE_FIRES_ON_REPLICA ||
1700 oneLock->enabled == RULE_DISABLED)
1701 continue;
1702 }
1703
1704 /* Non-SELECT rules are not supported for MERGE */
1705 if (parsetree->commandType == CMD_MERGE)
1706 ereport(ERROR,
1708 errmsg("cannot execute MERGE on relation \"%s\"",
1709 RelationGetRelationName(relation)),
1710 errdetail("MERGE is not supported for relations with rules."));
1711 }
1712
1713 if (oneLock->event == event)
1714 {
1715 if (parsetree->commandType != CMD_SELECT ||
1716 rangeTableEntry_used((Node *) parsetree, varno, 0))
1718 }
1719 }
1720
1721 return matching_locks;
1722}
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define RULE_FIRES_ON_ORIGIN
#define RULE_FIRES_ON_REPLICA
#define RULE_DISABLED
int SessionReplicationRole
Definition trigger.c:65
#define SESSION_REPLICATION_ROLE_REPLICA
Definition trigger.h:143

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

Referenced by RewriteQuery(), and rewriteValuesRTE().

◆ process_matched_tle()

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

Definition at line 1064 of file rewriteHandler.c.

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

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

Referenced by rewriteTargetListIU().

◆ QueryRewrite()

List * QueryRewrite ( Query parsetree)

Definition at line 4665 of file rewriteHandler.c.

4666{
4667 int64 input_query_id = parsetree->queryId;
4668 List *querylist;
4669 List *results;
4670 ListCell *l;
4672 bool foundOriginalQuery;
4674
4675 /*
4676 * This function is only applied to top-level original queries
4677 */
4678 Assert(parsetree->querySource == QSRC_ORIGINAL);
4679 Assert(parsetree->canSetTag);
4680
4681 /*
4682 * Step 1
4683 *
4684 * Apply all non-SELECT rules possibly getting 0 or many queries
4685 */
4686 querylist = RewriteQuery(parsetree, NIL, 0, 0);
4687
4688 /*
4689 * Step 2
4690 *
4691 * Apply all the RIR rules on each query
4692 *
4693 * This is also a handy place to mark each query with the original queryId
4694 */
4695 results = NIL;
4696 foreach(l, querylist)
4697 {
4698 Query *query = (Query *) lfirst(l);
4699
4700 query = fireRIRrules(query, NIL);
4701
4702 query->queryId = input_query_id;
4703
4704 results = lappend(results, query);
4705 }
4706
4707 /*
4708 * Step 3
4709 *
4710 * Determine which, if any, of the resulting queries is supposed to set
4711 * the command-result tag; and update the canSetTag fields accordingly.
4712 *
4713 * If the original query is still in the list, it sets the command tag.
4714 * Otherwise, the last INSTEAD query of the same kind as the original is
4715 * allowed to set the tag. (Note these rules can leave us with no query
4716 * setting the tag. The tcop code has to cope with this by setting up a
4717 * default tag based on the original un-rewritten query.)
4718 *
4719 * The Asserts verify that at most one query in the result list is marked
4720 * canSetTag. If we aren't checking asserts, we can fall out of the loop
4721 * as soon as we find the original query.
4722 */
4723 origCmdType = parsetree->commandType;
4724 foundOriginalQuery = false;
4725 lastInstead = NULL;
4726
4727 foreach(l, results)
4728 {
4729 Query *query = (Query *) lfirst(l);
4730
4731 if (query->querySource == QSRC_ORIGINAL)
4732 {
4733 Assert(query->canSetTag);
4735 foundOriginalQuery = true;
4736#ifndef USE_ASSERT_CHECKING
4737 break;
4738#endif
4739 }
4740 else
4741 {
4742 Assert(!query->canSetTag);
4743 if (query->commandType == origCmdType &&
4744 (query->querySource == QSRC_INSTEAD_RULE ||
4745 query->querySource == QSRC_QUAL_INSTEAD_RULE))
4746 lastInstead = query;
4747 }
4748 }
4749
4751 lastInstead->canSetTag = true;
4752
4753 return results;
4754}
int64_t int64
Definition c.h:621
CmdType
Definition nodes.h:273
@ QSRC_ORIGINAL
Definition parsenodes.h:36
static List * RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length, int num_ctes_processed)

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

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

◆ relation_is_updatable()

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

Definition at line 2892 of file rewriteHandler.c.

2896{
2897 int events = 0;
2898 Relation rel;
2900
2901#define ALL_EVENTS ((1 << CMD_INSERT) | (1 << CMD_UPDATE) | (1 << CMD_DELETE))
2902
2903 /* Since this function recurses, it could be driven to stack overflow */
2905
2906 rel = try_relation_open(reloid, AccessShareLock);
2907
2908 /*
2909 * If the relation doesn't exist, return zero rather than throwing an
2910 * error. This is helpful since scanning an information_schema view under
2911 * MVCC rules can result in referencing rels that have actually been
2912 * deleted already.
2913 */
2914 if (rel == NULL)
2915 return 0;
2916
2917 /* If we detect a recursive view, report that it is not updatable */
2919 {
2921 return 0;
2922 }
2923
2924 /* If the relation is a table, it is always updatable */
2925 if (rel->rd_rel->relkind == RELKIND_RELATION ||
2926 rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2927 {
2929 return ALL_EVENTS;
2930 }
2931
2932 /* Look for unconditional DO INSTEAD rules, and note supported events */
2933 rulelocks = rel->rd_rules;
2934 if (rulelocks != NULL)
2935 {
2936 int i;
2937
2938 for (i = 0; i < rulelocks->numLocks; i++)
2939 {
2940 if (rulelocks->rules[i]->isInstead &&
2941 rulelocks->rules[i]->qual == NULL)
2942 {
2943 events |= ((1 << rulelocks->rules[i]->event) & ALL_EVENTS);
2944 }
2945 }
2946
2947 /* If we have rules for all events, we're done */
2948 if (events == ALL_EVENTS)
2949 {
2951 return events;
2952 }
2953 }
2954
2955 /* Similarly look for INSTEAD OF triggers, if they are to be included */
2956 if (include_triggers)
2957 {
2959
2960 if (trigDesc)
2961 {
2962 if (trigDesc->trig_insert_instead_row)
2963 events |= (1 << CMD_INSERT);
2964 if (trigDesc->trig_update_instead_row)
2965 events |= (1 << CMD_UPDATE);
2966 if (trigDesc->trig_delete_instead_row)
2967 events |= (1 << CMD_DELETE);
2968
2969 /* If we have triggers for all events, we're done */
2970 if (events == ALL_EVENTS)
2971 {
2973 return events;
2974 }
2975 }
2976 }
2977
2978 /* If this is a foreign table, check which update events it supports */
2979 if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2980 {
2981 FdwRoutine *fdwroutine = GetFdwRoutineForRelation(rel, false);
2982
2983 if (fdwroutine->IsForeignRelUpdatable != NULL)
2984 events |= fdwroutine->IsForeignRelUpdatable(rel);
2985 else
2986 {
2987 /* Assume presence of executor functions is sufficient */
2988 if (fdwroutine->ExecForeignInsert != NULL)
2989 events |= (1 << CMD_INSERT);
2990 if (fdwroutine->ExecForeignUpdate != NULL)
2991 events |= (1 << CMD_UPDATE);
2992 if (fdwroutine->ExecForeignDelete != NULL)
2993 events |= (1 << CMD_DELETE);
2994 }
2995
2997 return events;
2998 }
2999
3000 /* Check if this is an automatically updatable view */
3001 if (rel->rd_rel->relkind == RELKIND_VIEW)
3002 {
3004
3006 {
3008 int auto_events;
3011 Oid baseoid;
3012
3013 /*
3014 * Determine which of the view's columns are updatable. If there
3015 * are none within the set of columns we are looking at, then the
3016 * view doesn't support INSERT/UPDATE, but it may still support
3017 * DELETE.
3018 */
3021
3022 if (include_cols != NULL)
3024
3026 auto_events = (1 << CMD_DELETE); /* May support DELETE */
3027 else
3028 auto_events = ALL_EVENTS; /* May support all events */
3029
3030 /*
3031 * The base relation must also support these update commands.
3032 * Tables are always updatable, but for any other kind of base
3033 * relation we must do a recursive check limited to the columns
3034 * referenced by the locally updatable columns in this view.
3035 */
3036 rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
3037 base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
3038 Assert(base_rte->rtekind == RTE_RELATION);
3039
3040 if (base_rte->relkind != RELKIND_RELATION &&
3042 {
3043 baseoid = base_rte->relid;
3045 RelationGetRelid(rel));
3047 viewquery->targetList);
3051 include_cols);
3053 }
3054 events |= auto_events;
3055 }
3056 }
3057
3058 /* If we reach here, the relation may support some update commands */
3060 return events;
3061}
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1093
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition foreign.c:474
static const char * view_cols_are_auto_updatable(Query *viewquery, Bitmapset *required_cols, Bitmapset **updatable_cols, char **non_updatable_col)
int relation_is_updatable(Oid reloid, List *outer_reloids, bool include_triggers, Bitmapset *include_cols)
Query * get_view_query(Relation view)
static Bitmapset * adjust_view_column_set(Bitmapset *cols, List *targetlist)
const char * view_query_is_auto_updatable(Query *viewquery, bool check_cols)
#define ALL_EVENTS
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:89
void check_stack_depth(void)
Definition stack_depth.c:95
ExecForeignInsert_function ExecForeignInsert
Definition fdwapi.h:236
ExecForeignUpdate_function ExecForeignUpdate
Definition fdwapi.h:239
ExecForeignDelete_function ExecForeignDelete
Definition fdwapi.h:240
IsForeignRelUpdatable_function IsForeignRelUpdatable
Definition fdwapi.h:244

References AccessShareLock, adjust_view_column_set(), ALL_EVENTS, Assert, bms_int_members(), bms_is_empty, check_stack_depth(), CMD_DELETE, CMD_INSERT, CMD_UPDATE, FdwRoutine::ExecForeignDelete, FdwRoutine::ExecForeignInsert, FdwRoutine::ExecForeignUpdate, fb(), get_view_query(), GetFdwRoutineForRelation(), i, FdwRoutine::IsForeignRelUpdatable, lappend_oid(), linitial, list_delete_last(), list_member_oid(), RelationData::rd_rel, RelationData::rd_rules, relation_close(), relation_is_updatable(), RelationGetRelid, rt_fetch, RTE_RELATION, 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().

◆ RewriteQuery()

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

Definition at line 3932 of file rewriteHandler.c.

3934{
3935 CmdType event = parsetree->commandType;
3936 bool instead = false;
3937 bool returning = false;
3938 bool updatableview = false;
3940 List *rewritten = NIL;
3941 ListCell *lc1;
3942
3943 /*
3944 * First, recursively process any insert/update/delete/merge statements in
3945 * WITH clauses. (We have to do this first because the WITH clauses may
3946 * get copied into rule actions below.)
3947 *
3948 * Any new WITH clauses from rule actions are processed when we recurse
3949 * into product queries below. However, when recursing, we must take care
3950 * to avoid rewriting a CTE query more than once (because expanding
3951 * generated columns in the targetlist more than once would fail). Since
3952 * new CTEs from product queries are added to the start of the list (see
3953 * rewriteRuleAction), we just skip the last num_ctes_processed items.
3954 */
3955 foreach(lc1, parsetree->cteList)
3956 {
3958 Query *ctequery = castNode(Query, cte->ctequery);
3960 List *newstuff;
3961
3962 /* Skip already-processed CTEs at the end of the list */
3963 if (i >= list_length(parsetree->cteList) - num_ctes_processed)
3964 break;
3965
3966 if (ctequery->commandType == CMD_SELECT)
3967 continue;
3968
3969 newstuff = RewriteQuery(ctequery, rewrite_events, 0, 0);
3970
3971 /*
3972 * Currently we can only handle unconditional, single-statement DO
3973 * INSTEAD rules correctly; we have to get exactly one non-utility
3974 * Query out of the rewrite operation to stuff back into the CTE node.
3975 */
3976 if (list_length(newstuff) == 1)
3977 {
3978 /* Must check it's not a utility command */
3979 ctequery = linitial_node(Query, newstuff);
3980 if (!(ctequery->commandType == CMD_SELECT ||
3981 ctequery->commandType == CMD_UPDATE ||
3982 ctequery->commandType == CMD_INSERT ||
3983 ctequery->commandType == CMD_DELETE ||
3984 ctequery->commandType == CMD_MERGE))
3985 {
3986 /*
3987 * Currently it could only be NOTIFY; this error message will
3988 * need work if we ever allow other utility commands in rules.
3989 */
3990 ereport(ERROR,
3992 errmsg("DO INSTEAD NOTIFY rules are not supported for data-modifying statements in WITH")));
3993 }
3994 /* WITH queries should never be canSetTag */
3995 Assert(!ctequery->canSetTag);
3996 /* Push the single Query back into the CTE node */
3997 cte->ctequery = (Node *) ctequery;
3998 }
3999 else if (newstuff == NIL)
4000 {
4001 ereport(ERROR,
4003 errmsg("DO INSTEAD NOTHING rules are not supported for data-modifying statements in WITH")));
4004 }
4005 else
4006 {
4007 ListCell *lc2;
4008
4009 /* examine queries to determine which error message to issue */
4010 foreach(lc2, newstuff)
4011 {
4012 Query *q = (Query *) lfirst(lc2);
4013
4014 if (q->querySource == QSRC_QUAL_INSTEAD_RULE)
4015 ereport(ERROR,
4017 errmsg("conditional DO INSTEAD rules are not supported for data-modifying statements in WITH")));
4018 if (q->querySource == QSRC_NON_INSTEAD_RULE)
4019 ereport(ERROR,
4021 errmsg("DO ALSO rules are not supported for data-modifying statements in WITH")));
4022 }
4023
4024 ereport(ERROR,
4026 errmsg("multi-statement DO INSTEAD rules are not supported for data-modifying statements in WITH")));
4027 }
4028 }
4030
4031 /*
4032 * If the statement is an insert, update, delete, or merge, adjust its
4033 * targetlist as needed, and then fire INSERT/UPDATE/DELETE rules on it.
4034 *
4035 * SELECT rules are handled later when we have all the queries that should
4036 * get executed. Also, utilities aren't rewritten at all (do we still
4037 * need that check?)
4038 */
4039 if (event != CMD_SELECT && event != CMD_UTILITY)
4040 {
4041 int result_relation;
4044 List *locks;
4047 bool hasUpdate = false;
4048 int values_rte_index = 0;
4049 bool defaults_remaining = false;
4050
4051 result_relation = parsetree->resultRelation;
4052 Assert(result_relation != 0);
4053 rt_entry = rt_fetch(result_relation, parsetree->rtable);
4054 Assert(rt_entry->rtekind == RTE_RELATION);
4055
4056 /*
4057 * We can use NoLock here since either the parser or
4058 * AcquireRewriteLocks should have locked the rel already.
4059 */
4061
4062 /*
4063 * Rewrite the targetlist as needed for the command type.
4064 */
4065 if (event == CMD_INSERT)
4066 {
4067 ListCell *lc2;
4069
4070 /*
4071 * Test if it's a multi-row INSERT ... VALUES (...), (...), ... by
4072 * looking for a VALUES RTE in the fromlist. For product queries,
4073 * we must ignore any already-processed VALUES RTEs from the
4074 * original query. These appear at the start of the rangetable.
4075 */
4076 foreach(lc2, parsetree->jointree->fromlist)
4077 {
4079
4080 if (IsA(rtr, RangeTblRef) && rtr->rtindex > orig_rt_length)
4081 {
4082 RangeTblEntry *rte = rt_fetch(rtr->rtindex,
4083 parsetree->rtable);
4084
4085 if (rte->rtekind == RTE_VALUES)
4086 {
4087 /* should not find more than one VALUES RTE */
4088 if (values_rte != NULL)
4089 elog(ERROR, "more than one VALUES RTE found");
4090
4091 values_rte = rte;
4092 values_rte_index = rtr->rtindex;
4093 }
4094 }
4095 }
4096
4097 if (values_rte)
4098 {
4100
4101 /* Process the main targetlist ... */
4102 parsetree->targetList = rewriteTargetListIU(parsetree->targetList,
4103 parsetree->commandType,
4104 parsetree->override,
4106 values_rte,
4109 /* ... and the VALUES expression lists */
4113 defaults_remaining = true;
4114 }
4115 else
4116 {
4117 /* Process just the main targetlist */
4118 parsetree->targetList =
4120 parsetree->commandType,
4121 parsetree->override,
4123 NULL, 0, NULL);
4124 }
4125
4126 if (parsetree->onConflict &&
4127 parsetree->onConflict->action == ONCONFLICT_UPDATE)
4128 {
4129 parsetree->onConflict->onConflictSet =
4131 CMD_UPDATE,
4132 parsetree->override,
4134 NULL, 0, NULL);
4135 }
4136 }
4137 else if (event == CMD_UPDATE)
4138 {
4139 Assert(parsetree->override == OVERRIDING_NOT_SET);
4140
4141 if (parsetree->forPortionOf)
4142 {
4143 /*
4144 * Don't add FOR PORTION OF details until we're done rewriting
4145 * a view update, so that we don't add the same qual and TLE
4146 * on the recursion.
4147 *
4148 * Views don't need to do anything special here to remap Vars;
4149 * that is handled by the tree walker.
4150 */
4151 if (rt_entry_relation->rd_rel->relkind != RELKIND_VIEW)
4152 {
4153 ListCell *tl;
4154
4155 /*
4156 * Add qual: UPDATE FOR PORTION OF should be limited to
4157 * rows that overlap the target range.
4158 */
4159 AddQual(parsetree, parsetree->forPortionOf->overlapsExpr);
4160
4161 /* Update FOR PORTION OF column(s) automatically. */
4162 foreach(tl, parsetree->forPortionOf->rangeTargetList)
4163 {
4165
4166 parsetree->targetList = lappend(parsetree->targetList, tle);
4167 }
4168 }
4169 }
4170
4171 parsetree->targetList =
4173 parsetree->commandType,
4174 parsetree->override,
4176 NULL, 0, NULL);
4177 }
4178 else if (event == CMD_MERGE)
4179 {
4180 Assert(parsetree->override == OVERRIDING_NOT_SET);
4181
4182 /*
4183 * Rewrite each action targetlist separately
4184 */
4185 foreach(lc1, parsetree->mergeActionList)
4186 {
4188
4189 switch (action->commandType)
4190 {
4191 case CMD_NOTHING:
4192 case CMD_DELETE: /* Nothing to do here */
4193 break;
4194 case CMD_UPDATE:
4195 case CMD_INSERT:
4196
4197 /*
4198 * MERGE actions do not permit multi-row INSERTs, so
4199 * there is no VALUES RTE to deal with here.
4200 */
4201 action->targetList =
4202 rewriteTargetListIU(action->targetList,
4203 action->commandType,
4204 action->override,
4206 NULL, 0, NULL);
4207 break;
4208 default:
4209 elog(ERROR, "unrecognized commandType: %d", action->commandType);
4210 break;
4211 }
4212 }
4213 }
4214 else if (event == CMD_DELETE)
4215 {
4216 if (parsetree->forPortionOf)
4217 {
4218 /*
4219 * Don't add FOR PORTION OF details until we're done rewriting
4220 * a view delete, so that we don't add the same qual on the
4221 * recursion.
4222 *
4223 * Views don't need to do anything special here to remap Vars;
4224 * that is handled by the tree walker.
4225 */
4226 if (rt_entry_relation->rd_rel->relkind != RELKIND_VIEW)
4227 {
4228 /*
4229 * Add qual: DELETE FOR PORTION OF should be limited to
4230 * rows that overlap the target range.
4231 */
4232 AddQual(parsetree, parsetree->forPortionOf->overlapsExpr);
4233 }
4234 }
4235 }
4236 else
4237 elog(ERROR, "unrecognized commandType: %d", (int) event);
4238
4239 /*
4240 * Collect and apply the appropriate rules.
4241 */
4242 locks = matchLocks(event, rt_entry_relation,
4243 result_relation, parsetree, &hasUpdate);
4244
4246 product_queries = fireRules(parsetree,
4247 result_relation,
4248 event,
4249 locks,
4250 &instead,
4251 &returning,
4252 &qual_product);
4253
4254 /*
4255 * If we have a VALUES RTE with any remaining untouched DEFAULT items,
4256 * and we got any product queries, finalize the VALUES RTE for each
4257 * product query (replacing the remaining DEFAULT items with NULLs).
4258 * We don't do this for the original query, because we know that it
4259 * must be an auto-insert on a view, and so should use the base
4260 * relation's defaults for any remaining DEFAULT items.
4261 */
4263 {
4264 ListCell *n;
4265
4266 /*
4267 * Each product query has its own copy of the VALUES RTE at the
4268 * same index in the rangetable, so we must finalize each one.
4269 *
4270 * Note that if the product query is an INSERT ... SELECT, then
4271 * the VALUES RTE will be at the same index in the SELECT part of
4272 * the product query rather than the top-level product query
4273 * itself.
4274 */
4275 foreach(n, product_queries)
4276 {
4277 Query *pt = (Query *) lfirst(n);
4279
4280 if (pt->commandType == CMD_INSERT &&
4281 pt->jointree && IsA(pt->jointree, FromExpr) &&
4282 list_length(pt->jointree->fromlist) == 1)
4283 {
4284 Node *jtnode = (Node *) linitial(pt->jointree->fromlist);
4285
4286 if (IsA(jtnode, RangeTblRef))
4287 {
4288 int rtindex = ((RangeTblRef *) jtnode)->rtindex;
4289 RangeTblEntry *src_rte = rt_fetch(rtindex, pt->rtable);
4290
4291 if (src_rte->rtekind == RTE_SUBQUERY &&
4292 src_rte->subquery &&
4293 IsA(src_rte->subquery, Query) &&
4294 src_rte->subquery->commandType == CMD_SELECT)
4295 pt = src_rte->subquery;
4296 }
4297 }
4298
4300 if (values_rte->rtekind != RTE_VALUES)
4301 elog(ERROR, "failed to find VALUES RTE in product query");
4302
4304 }
4305 }
4306
4307 /*
4308 * If there was no unqualified INSTEAD rule, and the target relation
4309 * is a view without any INSTEAD OF triggers, see if the view can be
4310 * automatically updated. If so, we perform the necessary query
4311 * transformation here and add the resulting query to the
4312 * product_queries list, so that it gets recursively rewritten if
4313 * necessary. For MERGE, the view must be automatically updatable if
4314 * any of the merge actions lack a corresponding INSTEAD OF trigger.
4315 *
4316 * If the view cannot be automatically updated, we throw an error here
4317 * which is OK since the query would fail at runtime anyway. Throwing
4318 * the error here is preferable to the executor check since we have
4319 * more detailed information available about why the view isn't
4320 * updatable.
4321 */
4322 if (!instead &&
4323 rt_entry_relation->rd_rel->relkind == RELKIND_VIEW &&
4325 parsetree->mergeActionList))
4326 {
4327 /*
4328 * If there were any qualified INSTEAD rules, don't allow the view
4329 * to be automatically updated (an unqualified INSTEAD rule or
4330 * INSTEAD OF trigger is required).
4331 */
4332 if (qual_product != NULL)
4334 parsetree->commandType,
4335 parsetree->mergeActionList,
4336 gettext_noop("Views with conditional DO INSTEAD rules are not automatically updatable."));
4337
4338 /*
4339 * Attempt to rewrite the query to automatically update the view.
4340 * This throws an error if the view can't be automatically
4341 * updated.
4342 */
4343 parsetree = rewriteTargetView(parsetree, rt_entry_relation);
4344
4345 /*
4346 * At this point product_queries contains any DO ALSO rule
4347 * actions. Add the rewritten query before or after those. This
4348 * must match the handling the original query would have gotten
4349 * below, if we allowed it to be included again.
4350 */
4351 if (parsetree->commandType == CMD_INSERT)
4353 else
4355
4356 /*
4357 * Set the "instead" flag, as if there had been an unqualified
4358 * INSTEAD, to prevent the original query from being included a
4359 * second time below. The transformation will have rewritten any
4360 * RETURNING list, so we can also set "returning" to forestall
4361 * throwing an error below.
4362 */
4363 instead = true;
4364 returning = true;
4365 updatableview = true;
4366 }
4367
4368 /*
4369 * If we got any product queries, recursively rewrite them --- but
4370 * first check for recursion!
4371 */
4372 if (product_queries != NIL)
4373 {
4374 ListCell *n;
4376
4377 foreach(n, rewrite_events)
4378 {
4379 rev = (rewrite_event *) lfirst(n);
4380 if (rev->relation == RelationGetRelid(rt_entry_relation) &&
4381 rev->event == event)
4382 ereport(ERROR,
4384 errmsg("infinite recursion detected in rules for relation \"%s\"",
4386 }
4387
4390 rev->event = event;
4392
4393 foreach(n, product_queries)
4394 {
4395 Query *pt = (Query *) lfirst(n);
4396 List *newstuff;
4397
4398 /*
4399 * For an updatable view, pt might be the rewritten version of
4400 * the original query, in which case we pass on orig_rt_length
4401 * to finish processing any VALUES RTE it contained.
4402 *
4403 * Otherwise, we have a product query created by fireRules().
4404 * Any VALUES RTEs from the original query have been fully
4405 * processed, and must be skipped when we recurse.
4406 */
4408 pt == parsetree ?
4413 }
4414
4416 }
4417
4418 /*
4419 * If there is an INSTEAD, and the original query has a RETURNING, we
4420 * have to have found a RETURNING in the rule(s), else fail. (Because
4421 * DefineQueryRewrite only allows RETURNING in unconditional INSTEAD
4422 * rules, there's no need to worry whether the substituted RETURNING
4423 * will actually be executed --- it must be.)
4424 */
4425 if ((instead || qual_product != NULL) &&
4426 parsetree->returningList &&
4427 !returning)
4428 {
4429 switch (event)
4430 {
4431 case CMD_INSERT:
4432 ereport(ERROR,
4434 errmsg("cannot perform INSERT RETURNING on relation \"%s\"",
4436 errhint("You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.")));
4437 break;
4438 case CMD_UPDATE:
4439 ereport(ERROR,
4441 errmsg("cannot perform UPDATE RETURNING on relation \"%s\"",
4443 errhint("You need an unconditional ON UPDATE DO INSTEAD rule with a RETURNING clause.")));
4444 break;
4445 case CMD_DELETE:
4446 ereport(ERROR,
4448 errmsg("cannot perform DELETE RETURNING on relation \"%s\"",
4450 errhint("You need an unconditional ON DELETE DO INSTEAD rule with a RETURNING clause.")));
4451 break;
4452 default:
4453 elog(ERROR, "unrecognized commandType: %d",
4454 (int) event);
4455 break;
4456 }
4457 }
4458
4459 /*
4460 * Updatable views are supported by ON CONFLICT, so don't prevent that
4461 * case from proceeding
4462 */
4463 if (parsetree->onConflict &&
4464 (product_queries != NIL || hasUpdate) &&
4466 ereport(ERROR,
4468 errmsg("INSERT with ON CONFLICT clause cannot be used with table that has INSERT or UPDATE rules")));
4469
4471 }
4472
4473 /*
4474 * For INSERTs, the original query is done first; for UPDATE/DELETE, it is
4475 * done last. This is needed because update and delete rule actions might
4476 * not do anything if they are invoked after the update or delete is
4477 * performed. The command counter increment between the query executions
4478 * makes the deleted (and maybe the updated) tuples disappear so the scans
4479 * for them in the rule actions cannot find them.
4480 *
4481 * If we found any unqualified INSTEAD, the original query is not done at
4482 * all, in any form. Otherwise, we add the modified form if qualified
4483 * INSTEADs were found, else the unmodified form.
4484 */
4485 if (!instead)
4486 {
4487 if (parsetree->commandType == CMD_INSERT)
4488 {
4489 if (qual_product != NULL)
4491 else
4492 rewritten = lcons(parsetree, rewritten);
4493 }
4494 else
4495 {
4496 if (qual_product != NULL)
4498 else
4499 rewritten = lappend(rewritten, parsetree);
4500 }
4501 }
4502
4503 /*
4504 * If the original query has a CTE list, and we generated more than one
4505 * non-utility result query, we have to fail because we'll have copied the
4506 * CTE list into each result query. That would break the expectation of
4507 * single evaluation of CTEs. This could possibly be fixed by
4508 * restructuring so that a CTE list can be shared across multiple Query
4509 * and PlannableStatement nodes.
4510 */
4511 if (parsetree->cteList != NIL)
4512 {
4513 int qcount = 0;
4514
4515 foreach(lc1, rewritten)
4516 {
4517 Query *q = (Query *) lfirst(lc1);
4518
4519 if (q->commandType != CMD_UTILITY)
4520 qcount++;
4521 }
4522 if (qcount > 1)
4523 ereport(ERROR,
4525 errmsg("WITH cannot be used in a query that is rewritten by rules into multiple queries")));
4526 }
4527
4528 return rewritten;
4529}
#define gettext_noop(x)
Definition c.h:1285
#define palloc_object(type)
Definition fe_memutils.h:74
List * lcons(void *datum, List *list)
Definition list.c:495
@ ONCONFLICT_UPDATE
Definition nodes.h:430
@ CMD_UTILITY
Definition nodes.h:280
@ RTE_VALUES
#define linitial_node(type, l)
Definition pg_list.h:181
#define foreach_current_index(var_or_cell)
Definition pg_list.h:435
@ OVERRIDING_NOT_SET
Definition primnodes.h:29
bool view_has_instead_trigger(Relation view, CmdType event, List *mergeActionList)
static List * fireRules(Query *parsetree, int rt_index, CmdType event, List *locks, bool *instead_flag, bool *returning_flag, Query **qual_product)
static bool rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti, Relation target_relation, Bitmapset *unused_cols)
static Query * rewriteTargetView(Query *parsetree, Relation view)
static void rewriteValuesRTEToNulls(Query *parsetree, RangeTblEntry *rte)
static List * matchLocks(CmdType event, Relation relation, int varno, Query *parsetree, bool *hasUpdate)
void error_view_not_updatable(Relation view, CmdType command, List *mergeActionList, const char *detail)
static List * rewriteTargetListIU(List *targetList, CmdType commandType, OverridingKind override, Relation target_relation, RangeTblEntry *values_rte, int values_rte_index, Bitmapset **unused_values_attrnos)
void AddQual(Query *parsetree, Node *qual)
List * rangeTargetList
Definition primnodes.h:2448
OnConflictAction action
Definition primnodes.h:2400
List * onConflictSet
Definition primnodes.h:2412
ForPortionOfExpr * forPortionOf
Definition parsenodes.h:151
List * mergeActionList
Definition parsenodes.h:188

References OnConflictExpr::action, AddQual(), Assert, castNode, CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_NOTHING, CMD_SELECT, CMD_UPDATE, CMD_UTILITY, Query::commandType, Query::cteList, CommonTableExpr::ctequery, elog, ereport, errcode(), errhint(), errmsg, ERROR, error_view_not_updatable(), fb(), fireRules(), foreach_current_index, Query::forPortionOf, FromExpr::fromlist, gettext_noop, i, IsA, Query::jointree, lappend(), lcons(), lfirst, lfirst_node, linitial, linitial_node, list_concat(), list_delete_last(), list_length(), matchLocks(), Query::mergeActionList, NIL, NoLock, Query::onConflict, ONCONFLICT_UPDATE, OnConflictExpr::onConflictSet, ForPortionOfExpr::overlapsExpr, OVERRIDING_NOT_SET, palloc_object, QSRC_NON_INSTEAD_RULE, QSRC_QUAL_INSTEAD_RULE, ForPortionOfExpr::rangeTargetList, relation_open(), RelationGetRelationName, RelationGetRelid, Query::returningList, RewriteQuery(), rewriteTargetListIU(), rewriteTargetView(), rewriteValuesRTE(), rewriteValuesRTEToNulls(), rt_fetch, Query::rtable, RTE_RELATION, RTE_SUBQUERY, RTE_VALUES, table_close(), Query::targetList, and view_has_instead_trigger().

Referenced by QueryRewrite(), and RewriteQuery().

◆ rewriteRuleAction()

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

Definition at line 353 of file rewriteHandler.c.

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

References acquireLocksOnSubLinks(), AcquireRewriteLocks(), AddQual(), adjustJoinTreeList(), Assert, ChangeVarNodes(), checkExprHasSubLink(), CMD_INSERT, CMD_UPDATE, CMD_UTILITY, CombineRangeTables(), contain_vars_of_level(), copyObject, Query::cteList, CommonTableExpr::ctename, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), acquireLocksOnSubLinks_context::for_execute, getInsertSelectQuery(), Query::jointree, lfirst, list_concat(), list_length(), NIL, OffsetVarNodes(), ONCONFLICT_SELECT, PRS2_NEW_VARNO, PRS2_OLD_VARNO, FromExpr::quals, rangeTableEntry_used(), REPLACEVARS_CHANGE_VARNO, REPLACEVARS_REPORT_ERROR, REPLACEVARS_SUBSTITUTE_NULL, ReplaceVarsFromTargetList(), Query::returningList, rt_fetch, Query::rtable, RTE_FUNCTION, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, and Query::targetList.

Referenced by fireRules().

◆ rewriteTargetListIU()

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

Definition at line 790 of file rewriteHandler.c.

797{
799 List *new_tlist = NIL;
802 int attrno,
804 numattrs;
805 ListCell *temp;
807
808 /*
809 * We process the normal (non-junk) attributes by scanning the input tlist
810 * once and transferring TLEs into an array, then scanning the array to
811 * build an output tlist. This avoids O(N^2) behavior for large numbers
812 * of attributes.
813 *
814 * Junk attributes are tossed into a separate list during the same tlist
815 * scan, then appended to the reconstructed tlist.
816 */
818 new_tles = (TargetEntry **) palloc0(numattrs * sizeof(TargetEntry *));
820
821 foreach(temp, targetList)
822 {
824
825 if (!old_tle->resjunk)
826 {
827 /* Normal attr: stash it into new_tles[] */
830 elog(ERROR, "bogus resno %d in targetlist", attrno);
832
833 /* We can (and must) ignore deleted attributes */
834 if (att_tup->attisdropped)
835 continue;
836
837 /* Merge with any prior assignment to same attribute */
838 new_tles[attrno - 1] =
840 new_tles[attrno - 1],
841 NameStr(att_tup->attname));
842 }
843 else
844 {
845 /*
846 * Copy all resjunk tlist entries to junk_tlist, and assign them
847 * resnos above the last real resno.
848 *
849 * Typical junk entries include ORDER BY or GROUP BY expressions
850 * (are these actually possible in an INSERT or UPDATE?), system
851 * attribute references, etc.
852 */
853
854 /* Get the resno right, but don't copy unnecessarily */
855 if (old_tle->resno != next_junk_attrno)
856 {
858 old_tle->resno = next_junk_attrno;
859 }
862 }
863 }
864
865 for (attrno = 1; attrno <= numattrs; attrno++)
866 {
868 bool apply_default;
869
871
872 /* We can (and must) ignore deleted attributes */
873 if (att_tup->attisdropped)
874 continue;
875
876 /*
877 * Handle the two cases where we need to insert a default expression:
878 * it's an INSERT and there's no tlist entry for the column, or the
879 * tlist entry is a DEFAULT placeholder node.
880 */
881 apply_default = ((new_tle == NULL && commandType == CMD_INSERT) ||
882 (new_tle && new_tle->expr && IsA(new_tle->expr, SetToDefault)));
883
884 if (commandType == CMD_INSERT)
885 {
886 int values_attrno = 0;
887
888 /* Source attribute number for values that come from a VALUES RTE */
889 if (values_rte && new_tle && IsA(new_tle->expr, Var))
890 {
891 Var *var = (Var *) new_tle->expr;
892
893 if (var->varno == values_rte_index)
894 values_attrno = var->varattno;
895 }
896
897 /*
898 * Can only insert DEFAULT into GENERATED ALWAYS identity columns,
899 * unless either OVERRIDING USER VALUE or OVERRIDING SYSTEM VALUE
900 * is specified.
901 */
902 if (att_tup->attidentity == ATTRIBUTE_IDENTITY_ALWAYS && !apply_default)
903 {
904 if (override == OVERRIDING_USER_VALUE)
905 apply_default = true;
906 else if (override != OVERRIDING_SYSTEM_VALUE)
907 {
908 /*
909 * If this column's values come from a VALUES RTE, test
910 * whether it contains only SetToDefault items. Since the
911 * VALUES list might be quite large, we arrange to only
912 * scan it once.
913 */
914 if (values_attrno != 0)
915 {
916 if (default_only_cols == NULL)
918
920 apply_default = true;
921 }
922
923 if (!apply_default)
926 errmsg("cannot insert a non-DEFAULT value into column \"%s\"",
927 NameStr(att_tup->attname)),
928 errdetail("Column \"%s\" is an identity column defined as GENERATED ALWAYS.",
929 NameStr(att_tup->attname)),
930 errhint("Use OVERRIDING SYSTEM VALUE to override.")));
931 }
932 }
933
934 /*
935 * Although inserting into a GENERATED BY DEFAULT identity column
936 * is allowed, apply the default if OVERRIDING USER VALUE is
937 * specified.
938 */
939 if (att_tup->attidentity == ATTRIBUTE_IDENTITY_BY_DEFAULT &&
940 override == OVERRIDING_USER_VALUE)
941 apply_default = true;
942
943 /*
944 * Can only insert DEFAULT into generated columns. (The
945 * OVERRIDING clause does not apply to generated columns, so we
946 * don't consider it here.)
947 */
948 if (att_tup->attgenerated && !apply_default)
949 {
950 /*
951 * If this column's values come from a VALUES RTE, test
952 * whether it contains only SetToDefault items, as above.
953 */
954 if (values_attrno != 0)
955 {
956 if (default_only_cols == NULL)
958
960 apply_default = true;
961 }
962
963 if (!apply_default)
966 errmsg("cannot insert a non-DEFAULT value into column \"%s\"",
967 NameStr(att_tup->attname)),
968 errdetail("Column \"%s\" is a generated column.",
969 NameStr(att_tup->attname))));
970 }
971
972 /*
973 * For an INSERT from a VALUES RTE, return the attribute numbers
974 * of any VALUES columns that will no longer be used (due to the
975 * targetlist entry being replaced by a default expression).
976 */
980 }
981
982 /*
983 * Updates to identity and generated columns follow the same rules as
984 * above, except that UPDATE doesn't admit OVERRIDING clauses. Also,
985 * the source can't be a VALUES RTE, so we needn't consider that.
986 */
987 if (commandType == CMD_UPDATE)
988 {
989 if (att_tup->attidentity == ATTRIBUTE_IDENTITY_ALWAYS &&
993 errmsg("column \"%s\" can only be updated to DEFAULT",
994 NameStr(att_tup->attname)),
995 errdetail("Column \"%s\" is an identity column defined as GENERATED ALWAYS.",
996 NameStr(att_tup->attname))));
997
998 if (att_tup->attgenerated && new_tle && !apply_default)
1001 errmsg("column \"%s\" can only be updated to DEFAULT",
1002 NameStr(att_tup->attname)),
1003 errdetail("Column \"%s\" is a generated column.",
1004 NameStr(att_tup->attname))));
1005 }
1006
1007 if (att_tup->attgenerated)
1008 {
1009 /*
1010 * virtual generated column stores a null value; stored generated
1011 * column will be fixed in executor
1012 */
1013 new_tle = NULL;
1014 }
1015 else if (apply_default)
1016 {
1017 Node *new_expr;
1018
1020
1021 /*
1022 * If there is no default (ie, default is effectively NULL), we
1023 * can omit the tlist entry in the INSERT case, since the planner
1024 * can insert a NULL for itself, and there's no point in spending
1025 * any more rewriter cycles on the entry. But in the UPDATE case
1026 * we've got to explicitly set the column to NULL.
1027 */
1028 if (!new_expr)
1029 {
1030 if (commandType == CMD_INSERT)
1031 new_tle = NULL;
1032 else
1034 att_tup->atttypmod,
1035 att_tup->attcollation,
1036 att_tup->attlen,
1037 att_tup->attbyval);
1038 }
1039
1040 if (new_expr)
1042 attrno,
1043 pstrdup(NameStr(att_tup->attname)),
1044 false);
1045 }
1046
1047 if (new_tle)
1049 }
1050
1051 pfree(new_tles);
1052
1054}
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc0(Size size)
Definition mcxt.c:1417
Node * coerce_null_to_domain(Oid typid, int32 typmod, Oid collation, int typlen, bool typbyval)
@ OVERRIDING_SYSTEM_VALUE
Definition primnodes.h:31
@ OVERRIDING_USER_VALUE
Definition primnodes.h:30
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:522
static TargetEntry * process_matched_tle(TargetEntry *src_tle, TargetEntry *prior_tle, const char *attrName)
static Bitmapset * findDefaultOnlyColumns(RangeTblEntry *rte)
AttrNumber resno
Definition primnodes.h:2268

References bms_add_member(), bms_is_member(), build_column_default(), CMD_INSERT, CMD_UPDATE, coerce_null_to_domain(), elog, ereport, errcode(), errdetail(), errhint(), errmsg, ERROR, fb(), findDefaultOnlyColumns(), flatCopyTargetEntry(), IsA, lappend(), lfirst, list_concat(), makeTargetEntry(), NameStr, NIL, OVERRIDING_SYSTEM_VALUE, OVERRIDING_USER_VALUE, palloc0(), pfree(), process_matched_tle(), pstrdup(), RelationGetNumberOfAttributes, TargetEntry::resno, TupleDescAttr(), Var::varattno, and Var::varno.

Referenced by RewriteQuery().

◆ rewriteTargetView()

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

Definition at line 3242 of file rewriteHandler.c.

3243{
3245 bool insert_or_update;
3246 const char *auto_update_detail;
3248 int base_rt_index;
3249 int new_rt_index;
3258 ListCell *lc;
3259
3260 /*
3261 * Get the Query from the view's ON SELECT rule. We're going to munge the
3262 * Query to change the view's base relation into the target relation,
3263 * along with various other changes along the way, so we need to make a
3264 * copy of it (get_view_query() returns a pointer into the relcache, so we
3265 * have to treat it as read-only).
3266 */
3268
3269 /* Locate RTE and perminfo describing the view in the outer query */
3270 view_rte = rt_fetch(parsetree->resultRelation, parsetree->rtable);
3271 view_perminfo = getRTEPermissionInfo(parsetree->rteperminfos, view_rte);
3272
3273 /*
3274 * Are we doing INSERT/UPDATE, or MERGE containing INSERT/UPDATE? If so,
3275 * various additional checks on the view columns need to be applied, and
3276 * any view CHECK OPTIONs need to be enforced.
3277 */
3279 (parsetree->commandType == CMD_INSERT ||
3280 parsetree->commandType == CMD_UPDATE);
3281
3282 if (parsetree->commandType == CMD_MERGE)
3283 {
3284 foreach_node(MergeAction, action, parsetree->mergeActionList)
3285 {
3286 if (action->commandType == CMD_INSERT ||
3287 action->commandType == CMD_UPDATE)
3288 {
3289 insert_or_update = true;
3290 break;
3291 }
3292 }
3293 }
3294
3295 /* Check if the expansion of non-system views are restricted */
3298 ereport(ERROR,
3300 errmsg("access to non-system view \"%s\" is restricted",
3301 RelationGetRelationName(view))));
3302
3303 /*
3304 * The view must be updatable, else fail.
3305 *
3306 * If we are doing INSERT/UPDATE (or MERGE containing INSERT/UPDATE), we
3307 * also check that there is at least one updatable column.
3308 */
3311
3314 parsetree->commandType,
3315 parsetree->mergeActionList,
3317
3318 /*
3319 * For INSERT/UPDATE (or MERGE containing INSERT/UPDATE) the modified
3320 * columns must all be updatable.
3321 */
3322 if (insert_or_update)
3323 {
3325 char *non_updatable_col;
3326
3327 /*
3328 * Compute the set of modified columns as those listed in the result
3329 * RTE's insertedCols and/or updatedCols sets plus those that are
3330 * targets of the query's targetlist(s). We must consider the query's
3331 * targetlist because rewriteTargetListIU may have added additional
3332 * targetlist entries for view defaults, and these must also be
3333 * updatable. But rewriteTargetListIU can also remove entries if they
3334 * are DEFAULT markers and the column's default is NULL, so
3335 * considering only the targetlist would also be wrong.
3336 */
3337 modified_cols = bms_union(view_perminfo->insertedCols,
3338 view_perminfo->updatedCols);
3339
3340 foreach(lc, parsetree->targetList)
3341 {
3343
3344 if (!tle->resjunk)
3347 }
3348
3349 if (parsetree->onConflict)
3350 {
3351 foreach(lc, parsetree->onConflict->onConflictSet)
3352 {
3354
3355 if (!tle->resjunk)
3358 }
3359 }
3360
3361 foreach_node(MergeAction, action, parsetree->mergeActionList)
3362 {
3363 if (action->commandType == CMD_INSERT ||
3364 action->commandType == CMD_UPDATE)
3365 {
3366 foreach_node(TargetEntry, tle, action->targetList)
3367 {
3368 if (!tle->resjunk)
3371 }
3372 }
3373 }
3374
3377 NULL,
3380 {
3381 /*
3382 * This is a different error, caused by an attempt to update a
3383 * non-updatable column in an otherwise updatable view.
3384 */
3385 switch (parsetree->commandType)
3386 {
3387 case CMD_INSERT:
3388 ereport(ERROR,
3390 errmsg("cannot insert into column \"%s\" of view \"%s\"",
3394 break;
3395 case CMD_UPDATE:
3396 ereport(ERROR,
3398 errmsg("cannot update column \"%s\" of view \"%s\"",
3402 break;
3403 case CMD_MERGE:
3404 ereport(ERROR,
3406 errmsg("cannot merge into column \"%s\" of view \"%s\"",
3410 break;
3411 default:
3412 elog(ERROR, "unrecognized CmdType: %d",
3413 (int) parsetree->commandType);
3414 break;
3415 }
3416 }
3417 }
3418
3419 /*
3420 * For MERGE, there must not be any INSTEAD OF triggers on an otherwise
3421 * updatable view. The caller already checked that there isn't a full set
3422 * of INSTEAD OF triggers, so this is to guard against having a partial
3423 * set (mixing auto-update and trigger-update actions in a single command
3424 * isn't supported).
3425 */
3426 if (parsetree->commandType == CMD_MERGE)
3427 {
3428 foreach_node(MergeAction, action, parsetree->mergeActionList)
3429 {
3430 if (action->commandType != CMD_NOTHING &&
3431 view_has_instead_trigger(view, action->commandType, NIL))
3432 ereport(ERROR,
3434 errmsg("cannot merge into view \"%s\"",
3436 errdetail("MERGE is not supported for views with INSTEAD OF triggers for some actions but not all."),
3437 errhint("To enable merging into the view, either provide a full set of INSTEAD OF triggers or drop the existing INSTEAD OF triggers."));
3438 }
3439 }
3440
3441 /*
3442 * If we get here, view_query_is_auto_updatable() has verified that the
3443 * view contains a single base relation.
3444 */
3445 Assert(list_length(viewquery->jointree->fromlist) == 1);
3446 rtr = linitial_node(RangeTblRef, viewquery->jointree->fromlist);
3447
3448 base_rt_index = rtr->rtindex;
3450 Assert(base_rte->rtekind == RTE_RELATION);
3452
3453 /*
3454 * Up to now, the base relation hasn't been touched at all in our query.
3455 * We need to acquire lock on it before we try to do anything with it.
3456 * (The subsequent recursive call of RewriteQuery will suppose that we
3457 * already have the right lock!) Since it will become the query target
3458 * relation, RowExclusiveLock is always the right thing.
3459 */
3461
3462 /*
3463 * While we have the relation open, update the RTE's relkind, just in case
3464 * it changed since this view was made (cf. AcquireRewriteLocks).
3465 */
3466 base_rte->relkind = base_rel->rd_rel->relkind;
3467
3468 /*
3469 * If the view query contains any sublink subqueries then we need to also
3470 * acquire locks on any relations they refer to. We know that there won't
3471 * be any subqueries in the range table or CTEs, so we can skip those, as
3472 * in AcquireRewriteLocks.
3473 */
3474 if (viewquery->hasSubLinks)
3475 {
3477
3478 context.for_execute = true;
3481 }
3482
3483 /*
3484 * Create a new target RTE describing the base relation, and add it to the
3485 * outer query's rangetable. (What's happening in the next few steps is
3486 * very much like what the planner would do to "pull up" the view into the
3487 * outer query. Perhaps someday we should refactor things enough so that
3488 * we can share code with the planner.)
3489 *
3490 * Be sure to set rellockmode to the correct thing for the target table.
3491 * Since we copied the whole viewquery above, we can just scribble on
3492 * base_rte instead of copying it.
3493 */
3494 new_rte = base_rte;
3495 new_rte->rellockmode = RowExclusiveLock;
3496
3497 parsetree->rtable = lappend(parsetree->rtable, new_rte);
3498 new_rt_index = list_length(parsetree->rtable);
3499
3500 /*
3501 * INSERTs never inherit. For UPDATE/DELETE/MERGE, we use the view
3502 * query's inheritance flag for the base relation.
3503 */
3504 if (parsetree->commandType == CMD_INSERT)
3505 new_rte->inh = false;
3506
3507 /*
3508 * Adjust the view's targetlist Vars to reference the new target RTE, ie
3509 * make their varnos be new_rt_index instead of base_rt_index. There can
3510 * be no Vars for other rels in the tlist, so this is sufficient to pull
3511 * up the tlist expressions for use in the outer query. The tlist will
3512 * provide the replacement expressions used by ReplaceVarsFromTargetList
3513 * below.
3514 */
3515 view_targetlist = viewquery->targetList;
3516
3520 0);
3521
3522 /*
3523 * If the view has "security_invoker" set, mark the new target relation
3524 * for the permissions checks that we want to enforce against the query
3525 * caller. Otherwise we want to enforce them against the view owner.
3526 *
3527 * At the relation level, require the same INSERT/UPDATE/DELETE
3528 * permissions that the query caller needs against the view. We drop the
3529 * ACL_SELECT bit that is presumably in new_perminfo->requiredPerms
3530 * initially.
3531 *
3532 * Note: the original view's RTEPermissionInfo remains in the query's
3533 * rteperminfos so that the executor still performs appropriate
3534 * permissions checks for the query caller's use of the view.
3535 *
3536 * Disregard the perminfo in viewquery->rteperminfos that the base_rte
3537 * would currently be pointing at, because we'd like it to point now to a
3538 * new one that will be filled below. Must set perminfoindex to 0 to not
3539 * trip over the Assert in addRTEPermissionInfo().
3540 */
3541 new_rte->perminfoindex = 0;
3542 new_perminfo = addRTEPermissionInfo(&parsetree->rteperminfos, new_rte);
3544 new_perminfo->checkAsUser = InvalidOid;
3545 else
3546 new_perminfo->checkAsUser = view->rd_rel->relowner;
3547 new_perminfo->requiredPerms = view_perminfo->requiredPerms;
3548
3549 /*
3550 * Now for the per-column permissions bits.
3551 *
3552 * Initially, new_perminfo (base_perminfo) contains selectedCols
3553 * permission check bits for all base-rel columns referenced by the view,
3554 * but since the view is a SELECT query its insertedCols/updatedCols is
3555 * empty. We set insertedCols and updatedCols to include all the columns
3556 * the outer query is trying to modify, adjusting the column numbers as
3557 * needed. But we leave selectedCols as-is, so the view owner must have
3558 * read permission for all columns used in the view definition, even if
3559 * some of them are not read by the outer query. We could try to limit
3560 * selectedCols to only columns used in the transformed query, but that
3561 * does not correspond to what happens in ordinary SELECT usage of a view:
3562 * all referenced columns must have read permission, even if optimization
3563 * finds that some of them can be discarded during query transformation.
3564 * The flattening we're doing here is an optional optimization, too. (If
3565 * you are unpersuaded and want to change this, note that applying
3566 * adjust_view_column_set to view_perminfo->selectedCols is clearly *not*
3567 * the right answer, since that neglects base-rel columns used in the
3568 * view's WHERE quals.)
3569 *
3570 * This step needs the modified view targetlist, so we have to do things
3571 * in this order.
3572 */
3573 Assert(bms_is_empty(new_perminfo->insertedCols) &&
3574 bms_is_empty(new_perminfo->updatedCols));
3575
3576 new_perminfo->selectedCols = base_perminfo->selectedCols;
3577
3578 new_perminfo->insertedCols =
3580
3581 new_perminfo->updatedCols =
3583
3584 /*
3585 * Move any security barrier quals from the view RTE onto the new target
3586 * RTE. Any such quals should now apply to the new target RTE and will
3587 * not reference the original view RTE in the rewritten query.
3588 */
3589 new_rte->securityQuals = view_rte->securityQuals;
3590 view_rte->securityQuals = NIL;
3591
3592 /*
3593 * Now update all Vars in the outer query that reference the view to
3594 * reference the appropriate column of the base relation instead.
3595 */
3596 parsetree = (Query *)
3597 ReplaceVarsFromTargetList((Node *) parsetree,
3598 parsetree->resultRelation,
3599 0,
3600 view_rte,
3604 0,
3605 NULL);
3606
3607 /*
3608 * Update all other RTI references in the query that point to the view
3609 * (for example, parsetree->resultRelation itself) to point to the new
3610 * base relation instead. Vars will not be affected since none of them
3611 * reference parsetree->resultRelation any longer.
3612 */
3613 ChangeVarNodes((Node *) parsetree,
3614 parsetree->resultRelation,
3616 0);
3617 Assert(parsetree->resultRelation == new_rt_index);
3618
3619 /*
3620 * For INSERT/UPDATE we must also update resnos in the targetlist to refer
3621 * to columns of the base relation, since those indicate the target
3622 * columns to be affected. Similarly, for MERGE we must update the resnos
3623 * in the merge action targetlists of any INSERT/UPDATE actions.
3624 *
3625 * Note that this destroys the resno ordering of the targetlists, but that
3626 * will be fixed when we recurse through RewriteQuery, which will invoke
3627 * rewriteTargetListIU again on the updated targetlists.
3628 */
3629 if (parsetree->commandType != CMD_DELETE)
3630 {
3631 foreach(lc, parsetree->targetList)
3632 {
3635
3636 if (tle->resjunk)
3637 continue;
3638
3640 if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3641 tle->resno = ((Var *) view_tle->expr)->varattno;
3642 else
3643 elog(ERROR, "attribute number %d not found in view targetlist",
3644 tle->resno);
3645 }
3646
3647 foreach_node(MergeAction, action, parsetree->mergeActionList)
3648 {
3649 if (action->commandType == CMD_INSERT ||
3650 action->commandType == CMD_UPDATE)
3651 {
3652 foreach_node(TargetEntry, tle, action->targetList)
3653 {
3655
3656 if (tle->resjunk)
3657 continue;
3658
3660 if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3661 tle->resno = ((Var *) view_tle->expr)->varattno;
3662 else
3663 elog(ERROR, "attribute number %d not found in view targetlist",
3664 tle->resno);
3665 }
3666 }
3667 }
3668 }
3669
3670 /*
3671 * For INSERT .. ON CONFLICT .. DO SELECT/UPDATE, we must also update
3672 * assorted stuff in the onConflict data structure.
3673 */
3674 if (parsetree->onConflict &&
3675 (parsetree->onConflict->action == ONCONFLICT_UPDATE ||
3676 parsetree->onConflict->action == ONCONFLICT_SELECT))
3677 {
3682 List *tmp_tlist;
3683
3684 /*
3685 * For ON CONFLICT DO UPDATE, update the resnos in the auxiliary
3686 * UPDATE targetlist to refer to columns of the base relation.
3687 */
3688 foreach(lc, parsetree->onConflict->onConflictSet)
3689 {
3692
3693 if (tle->resjunk)
3694 continue;
3695
3697 if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3698 tle->resno = ((Var *) view_tle->expr)->varattno;
3699 else
3700 elog(ERROR, "attribute number %d not found in view targetlist",
3701 tle->resno);
3702 }
3703
3704 /*
3705 * Create a new RTE for the EXCLUDED pseudo-relation, using the
3706 * query's new base rel (which may well have a different column list
3707 * from the view, hence we need a new column alias list). This should
3708 * match transformOnConflictClause. In particular, note that the
3709 * relkind is set to composite to signal that we're not dealing with
3710 * an actual relation.
3711 */
3713
3715 base_rel,
3717 makeAlias("excluded", NIL),
3718 false, false);
3719 new_exclRte = new_exclNSItem->p_rte;
3721 /* Ignore the RTEPermissionInfo that would've been added. */
3722 new_exclRte->perminfoindex = 0;
3723
3724 parsetree->rtable = lappend(parsetree->rtable, new_exclRte);
3726 list_length(parsetree->rtable);
3727
3728 /*
3729 * Replace the targetlist for the EXCLUDED pseudo-relation with a new
3730 * one, representing the columns from the new base relation.
3731 */
3732 parsetree->onConflict->exclRelTlist =
3734
3735 /*
3736 * Update all Vars in the ON CONFLICT clause that refer to the old
3737 * EXCLUDED pseudo-relation. We want to use the column mappings
3738 * defined in the view targetlist, but we need the outputs to refer to
3739 * the new EXCLUDED pseudo-relation rather than the new target RTE.
3740 * Also notice that "EXCLUDED.*" will be expanded using the view's
3741 * rowtype, which seems correct.
3742 */
3744
3746 new_exclRelIndex, 0);
3747
3748 parsetree->onConflict = (OnConflictExpr *)
3751 0,
3752 view_rte,
3753 tmp_tlist,
3756 0,
3757 &parsetree->hasSubLinks);
3758 }
3759
3760 if (parsetree->forPortionOf && parsetree->commandType == CMD_UPDATE)
3761 {
3762 /*
3763 * Like the INSERT/UPDATE code above, update the resnos in the
3764 * auxiliary UPDATE targetlist to refer to columns of the base
3765 * relation.
3766 */
3767 foreach(lc, parsetree->forPortionOf->rangeTargetList)
3768 {
3771
3772 if (tle->resjunk)
3773 continue;
3774
3776 if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3777 tle->resno = ((Var *) view_tle->expr)->varattno;
3778 else
3779 elog(ERROR, "attribute number %d not found in view targetlist",
3780 tle->resno);
3781 }
3782 }
3783
3784 /*
3785 * For UPDATE/DELETE/MERGE, pull up any WHERE quals from the view. We
3786 * know that any Vars in the quals must reference the one base relation,
3787 * so we need only adjust their varnos to reference the new target (just
3788 * the same as we did with the view targetlist).
3789 *
3790 * If it's a security-barrier view, its WHERE quals must be applied before
3791 * quals from the outer query, so we attach them to the RTE as security
3792 * barrier quals rather than adding them to the main WHERE clause.
3793 *
3794 * For INSERT, the view's quals can be ignored in the main query.
3795 */
3796 if (parsetree->commandType != CMD_INSERT &&
3797 viewquery->jointree->quals != NULL)
3798 {
3799 Node *viewqual = (Node *) viewquery->jointree->quals;
3800
3801 /*
3802 * Even though we copied viewquery already at the top of this
3803 * function, we must duplicate the viewqual again here, because we may
3804 * need to use the quals again below for a WithCheckOption clause.
3805 */
3807
3809
3810 if (RelationIsSecurityView(view))
3811 {
3812 /*
3813 * The view's quals go in front of existing barrier quals: those
3814 * would have come from an outer level of security-barrier view,
3815 * and so must get evaluated later.
3816 *
3817 * Note: the parsetree has been mutated, so the new_rte pointer is
3818 * stale and needs to be re-computed.
3819 */
3820 new_rte = rt_fetch(new_rt_index, parsetree->rtable);
3821 new_rte->securityQuals = lcons(viewqual, new_rte->securityQuals);
3822
3823 /*
3824 * Do not set parsetree->hasRowSecurity, because these aren't RLS
3825 * conditions (they aren't affected by enabling/disabling RLS).
3826 */
3827
3828 /*
3829 * Make sure that the query is marked correctly if the added qual
3830 * has sublinks.
3831 */
3832 if (!parsetree->hasSubLinks)
3833 parsetree->hasSubLinks = checkExprHasSubLink(viewqual);
3834 }
3835 else
3836 AddQual(parsetree, viewqual);
3837 }
3838
3839 /*
3840 * For INSERT/UPDATE (or MERGE containing INSERT/UPDATE), if the view has
3841 * the WITH CHECK OPTION, or any parent view specified WITH CASCADED CHECK
3842 * OPTION, add the quals from the view to the query's withCheckOptions
3843 * list.
3844 */
3845 if (insert_or_update)
3846 {
3847 bool has_wco = RelationHasCheckOption(view);
3848 bool cascaded = RelationHasCascadedCheckOption(view);
3849
3850 /*
3851 * If the parent view has a cascaded check option, treat this view as
3852 * if it also had a cascaded check option.
3853 *
3854 * New WithCheckOptions are added to the start of the list, so if
3855 * there is a cascaded check option, it will be the first item in the
3856 * list.
3857 */
3858 if (parsetree->withCheckOptions != NIL)
3859 {
3861 (WithCheckOption *) linitial(parsetree->withCheckOptions);
3862
3863 if (parent_wco->cascaded)
3864 {
3865 has_wco = true;
3866 cascaded = true;
3867 }
3868 }
3869
3870 /*
3871 * Add the new WithCheckOption to the start of the list, so that
3872 * checks on inner views are run before checks on outer views, as
3873 * required by the SQL standard.
3874 *
3875 * If the new check is CASCADED, we need to add it even if this view
3876 * has no quals, since there may be quals on child views. A LOCAL
3877 * check can be omitted if this view has no quals.
3878 */
3879 if (has_wco && (cascaded || viewquery->jointree->quals != NULL))
3880 {
3882
3884 wco->kind = WCO_VIEW_CHECK;
3885 wco->relname = pstrdup(RelationGetRelationName(view));
3886 wco->polname = NULL;
3887 wco->qual = NULL;
3888 wco->cascaded = cascaded;
3889
3890 parsetree->withCheckOptions = lcons(wco,
3891 parsetree->withCheckOptions);
3892
3893 if (viewquery->jointree->quals != NULL)
3894 {
3895 wco->qual = (Node *) viewquery->jointree->quals;
3897
3898 /*
3899 * For INSERT, make sure that the query is marked correctly if
3900 * the added qual has sublinks. This can be skipped for
3901 * UPDATE/MERGE, since the same qual will have already been
3902 * added above, and the check will already have been done.
3903 */
3904 if (!parsetree->hasSubLinks &&
3905 parsetree->commandType == CMD_INSERT)
3906 parsetree->hasSubLinks = checkExprHasSubLink(wco->qual);
3907 }
3908 }
3909 }
3910
3912
3913 return parsetree;
3914}
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:251
#define RowExclusiveLock
Definition lockdefs.h:38
ParseState * make_parsestate(ParseState *parentParseState)
Definition parse_node.c:39
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, LOCKMODE lockmode, Alias *alias, bool inh, bool inFromCl)
RTEPermissionInfo * addRTEPermissionInfo(List **rteperminfos, RangeTblEntry *rte)
@ WCO_VIEW_CHECK
List * BuildOnConflictExcludedTargetlist(Relation targetrel, Index exclRelIndex)
Definition analyze.c:1620
#define InvalidOid
#define RelationHasCheckOption(relation)
Definition rel.h:459
#define RelationHasSecurityInvoker(relation)
Definition rel.h:449
#define RelationHasCascadedCheckOption(relation)
Definition rel.h:481
List * exclRelTlist
Definition primnodes.h:2417

References _, acquireLocksOnSubLinks(), OnConflictExpr::action, AddQual(), addRangeTableEntryForRelation(), addRTEPermissionInfo(), adjust_view_column_set(), Assert, bms_add_member(), bms_is_empty, bms_union(), BuildOnConflictExcludedTargetlist(), ChangeVarNodes(), checkExprHasSubLink(), CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_NOTHING, CMD_UPDATE, Query::commandType, copyObject, elog, ereport, errcode(), errdetail(), errdetail_internal(), errhint(), errmsg, ERROR, error_view_not_updatable(), OnConflictExpr::exclRelIndex, OnConflictExpr::exclRelTlist, fb(), FirstLowInvalidHeapAttributeNumber, FirstNormalObjectId, acquireLocksOnSubLinks_context::for_execute, foreach_node, Query::forPortionOf, get_tle_by_resno(), get_view_query(), getRTEPermissionInfo(), InvalidOid, IsA, lappend(), lcons(), lfirst, linitial, linitial_node, list_length(), make_parsestate(), makeAlias(), makeNode, Query::mergeActionList, NIL, NoLock, Query::onConflict, ONCONFLICT_SELECT, ONCONFLICT_UPDATE, OnConflictExpr::onConflictSet, pstrdup(), QTW_IGNORE_RC_SUBQUERIES, query_tree_walker, ForPortionOfExpr::rangeTargetList, RelationData::rd_rel, relation_open(), RelationGetRelationName, RelationGetRelid, RelationHasCascadedCheckOption, RelationHasCheckOption, RelationHasSecurityInvoker, RelationIsSecurityView, REPLACEVARS_REPORT_ERROR, ReplaceVarsFromTargetList(), restrict_nonsystem_relation_kind, RESTRICT_RELKIND_VIEW, RowExclusiveLock, rt_fetch, Query::rtable, RTE_RELATION, table_close(), Query::targetList, unlikely, view_cols_are_auto_updatable(), view_has_instead_trigger(), view_query_is_auto_updatable(), and WCO_VIEW_CHECK.

Referenced by RewriteQuery().

◆ rewriteValuesRTE()

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

Definition at line 1431 of file rewriteHandler.c.

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

References Assert, bms_is_member(), build_column_default(), CMD_INSERT, coerce_null_to_domain(), Query::commandType, elog, ERROR, fb(), i, IsA, lappend(), lfirst, linitial, list_length(), makeNullConst(), matchLocks(), NIL, palloc0(), pfree(), RTE_VALUES, searchForDefault(), Query::targetList, TupleDescAttr(), SetToDefault::typeId, Var::varattno, Var::varno, and view_has_instead_trigger().

Referenced by RewriteQuery().

◆ rewriteValuesRTEToNulls()

static void rewriteValuesRTEToNulls ( Query parsetree,
RangeTblEntry rte 
)
static

Definition at line 1616 of file rewriteHandler.c.

1617{
1618 List *newValues;
1619 ListCell *lc;
1620
1621 newValues = NIL;
1622 foreach(lc, rte->values_lists)
1623 {
1624 List *sublist = (List *) lfirst(lc);
1625 List *newList = NIL;
1626 ListCell *lc2;
1627
1628 foreach(lc2, sublist)
1629 {
1630 Node *col = (Node *) lfirst(lc2);
1631
1632 if (IsA(col, SetToDefault))
1633 {
1634 SetToDefault *def = (SetToDefault *) col;
1635
1637 def->typeMod,
1638 def->collation));
1639 }
1640 else
1642 }
1644 }
1645 rte->values_lists = newValues;
1646}

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

Referenced by RewriteQuery().

◆ searchForDefault()

static bool searchForDefault ( RangeTblEntry rte)
static

Definition at line 1317 of file rewriteHandler.c.

1318{
1319 ListCell *lc;
1320
1321 foreach(lc, rte->values_lists)
1322 {
1323 List *sublist = (List *) lfirst(lc);
1324 ListCell *lc2;
1325
1326 foreach(lc2, sublist)
1327 {
1328 Node *col = (Node *) lfirst(lc2);
1329
1330 if (IsA(col, SetToDefault))
1331 return true;
1332 }
1333 }
1334 return false;
1335}

References fb(), IsA, and lfirst.

Referenced by rewriteValuesRTE().

◆ view_col_is_auto_updatable()

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

Definition at line 2613 of file rewriteHandler.c.

2614{
2615 Var *var = (Var *) tle->expr;
2616
2617 /*
2618 * For now, the only updatable columns we support are those that are Vars
2619 * referring to user columns of the underlying base relation.
2620 *
2621 * The view targetlist may contain resjunk columns (e.g., a view defined
2622 * like "SELECT * FROM t ORDER BY a+b" is auto-updatable) but such columns
2623 * are not auto-updatable, and in fact should never appear in the outer
2624 * query's targetlist.
2625 */
2626 if (tle->resjunk)
2627 return gettext_noop("Junk view columns are not updatable.");
2628
2629 if (!IsA(var, Var) ||
2630 var->varno != rtr->rtindex ||
2631 var->varlevelsup != 0)
2632 return gettext_noop("View columns that are not columns of their base relation are not updatable.");
2633
2634 if (var->varattno < 0)
2635 return gettext_noop("View columns that refer to system columns are not updatable.");
2636
2637 if (var->varattno == 0)
2638 return gettext_noop("View columns that return whole-row references are not updatable.");
2639
2640 return NULL; /* the view column is updatable */
2641}

References fb(), gettext_noop, IsA, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by view_cols_are_auto_updatable(), and view_query_is_auto_updatable().

◆ view_cols_are_auto_updatable()

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

Definition at line 2809 of file rewriteHandler.c.

2813{
2816 ListCell *cell;
2817
2818 /*
2819 * The caller should have verified that this view is auto-updatable and so
2820 * there should be a single base relation.
2821 */
2822 Assert(list_length(viewquery->jointree->fromlist) == 1);
2823 rtr = linitial_node(RangeTblRef, viewquery->jointree->fromlist);
2824
2825 /* Initialize the optional return values */
2826 if (updatable_cols != NULL)
2828 if (non_updatable_col != NULL)
2830
2831 /* Test each view column for updatability */
2833 foreach(cell, viewquery->targetList)
2834 {
2835 TargetEntry *tle = (TargetEntry *) lfirst(cell);
2836 const char *col_update_detail;
2837
2838 col++;
2840
2841 if (col_update_detail == NULL)
2842 {
2843 /* The column is updatable */
2844 if (updatable_cols != NULL)
2846 }
2847 else if (bms_is_member(col, required_cols))
2848 {
2849 /* The required column is not updatable */
2850 if (non_updatable_col != NULL)
2851 *non_updatable_col = tle->resname;
2852 return col_update_detail;
2853 }
2854 }
2855
2856 return NULL; /* all the required view columns are updatable */
2857}
static const char * view_col_is_auto_updatable(RangeTblRef *rtr, TargetEntry *tle)

References Assert, bms_add_member(), bms_is_member(), fb(), FirstLowInvalidHeapAttributeNumber, lfirst, linitial_node, list_length(), and view_col_is_auto_updatable().

Referenced by relation_is_updatable(), and rewriteTargetView().

◆ view_has_instead_trigger()

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

Definition at line 2549 of file rewriteHandler.c.

2550{
2551 TriggerDesc *trigDesc = view->trigdesc;
2552
2553 switch (event)
2554 {
2555 case CMD_INSERT:
2556 if (trigDesc && trigDesc->trig_insert_instead_row)
2557 return true;
2558 break;
2559 case CMD_UPDATE:
2560 if (trigDesc && trigDesc->trig_update_instead_row)
2561 return true;
2562 break;
2563 case CMD_DELETE:
2564 if (trigDesc && trigDesc->trig_delete_instead_row)
2565 return true;
2566 break;
2567 case CMD_MERGE:
2568 foreach_node(MergeAction, action, mergeActionList)
2569 {
2570 switch (action->commandType)
2571 {
2572 case CMD_INSERT:
2573 if (!trigDesc || !trigDesc->trig_insert_instead_row)
2574 return false;
2575 break;
2576 case CMD_UPDATE:
2577 if (!trigDesc || !trigDesc->trig_update_instead_row)
2578 return false;
2579 break;
2580 case CMD_DELETE:
2581 if (!trigDesc || !trigDesc->trig_delete_instead_row)
2582 return false;
2583 break;
2584 case CMD_NOTHING:
2585 /* No trigger required */
2586 break;
2587 default:
2588 elog(ERROR, "unrecognized commandType: %d", action->commandType);
2589 break;
2590 }
2591 }
2592 return true; /* no actions without an INSTEAD OF trigger */
2593 default:
2594 elog(ERROR, "unrecognized CmdType: %d", (int) event);
2595 break;
2596 }
2597 return false;
2598}

References CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_NOTHING, CMD_UPDATE, elog, ERROR, fb(), foreach_node, and RelationData::trigdesc.

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

◆ view_query_is_auto_updatable()

const char * view_query_is_auto_updatable ( Query viewquery,
bool  check_cols 
)

Definition at line 2661 of file rewriteHandler.c.

2662{
2665
2666 /*----------
2667 * Check if the view is simply updatable. According to SQL-92 this means:
2668 * - No DISTINCT clause.
2669 * - Each TLE is a column reference, and each column appears at most once.
2670 * - FROM contains exactly one base relation.
2671 * - No GROUP BY or HAVING clauses.
2672 * - No set operations (UNION, INTERSECT or EXCEPT).
2673 * - No sub-queries in the WHERE clause that reference the target table.
2674 *
2675 * We ignore that last restriction since it would be complex to enforce
2676 * and there isn't any actual benefit to disallowing sub-queries. (The
2677 * semantic issues that the standard is presumably concerned about don't
2678 * arise in Postgres, since any such sub-query will not see any updates
2679 * executed by the outer query anyway, thanks to MVCC snapshotting.)
2680 *
2681 * We also relax the second restriction by supporting part of SQL:1999
2682 * feature T111, which allows for a mix of updatable and non-updatable
2683 * columns, provided that an INSERT or UPDATE doesn't attempt to assign to
2684 * a non-updatable column.
2685 *
2686 * In addition we impose these constraints, involving features that are
2687 * not part of SQL-92:
2688 * - No CTEs (WITH clauses).
2689 * - No OFFSET or LIMIT clauses (this matches a SQL:2008 restriction).
2690 * - No system columns (including whole-row references) in the tlist.
2691 * - No window functions in the tlist.
2692 * - No set-returning functions in the tlist.
2693 *
2694 * Note that we do these checks without recursively expanding the view.
2695 * If the base relation is a view, we'll recursively deal with it later.
2696 *----------
2697 */
2698 if (viewquery->distinctClause != NIL)
2699 return gettext_noop("Views containing DISTINCT are not automatically updatable.");
2700
2701 if (viewquery->groupClause != NIL || viewquery->groupingSets)
2702 return gettext_noop("Views containing GROUP BY are not automatically updatable.");
2703
2704 if (viewquery->havingQual != NULL)
2705 return gettext_noop("Views containing HAVING are not automatically updatable.");
2706
2707 if (viewquery->setOperations != NULL)
2708 return gettext_noop("Views containing UNION, INTERSECT, or EXCEPT are not automatically updatable.");
2709
2710 if (viewquery->cteList != NIL)
2711 return gettext_noop("Views containing WITH are not automatically updatable.");
2712
2713 if (viewquery->limitOffset != NULL || viewquery->limitCount != NULL)
2714 return gettext_noop("Views containing LIMIT or OFFSET are not automatically updatable.");
2715
2716 /*
2717 * We must not allow window functions or set returning functions in the
2718 * targetlist. Otherwise we might end up inserting them into the quals of
2719 * the main query. We must also check for aggregates in the targetlist in
2720 * case they appear without a GROUP BY.
2721 *
2722 * These restrictions ensure that each row of the view corresponds to a
2723 * unique row in the underlying base relation.
2724 */
2725 if (viewquery->hasAggs)
2726 return gettext_noop("Views that return aggregate functions are not automatically updatable.");
2727
2728 if (viewquery->hasWindowFuncs)
2729 return gettext_noop("Views that return window functions are not automatically updatable.");
2730
2731 if (viewquery->hasTargetSRFs)
2732 return gettext_noop("Views that return set-returning functions are not automatically updatable.");
2733
2734 /*
2735 * The view query should select from a single base relation, which must be
2736 * a table or another view.
2737 */
2738 if (list_length(viewquery->jointree->fromlist) != 1)
2739 return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2740
2741 rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
2742 if (!IsA(rtr, RangeTblRef))
2743 return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2744
2745 base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
2746 if (base_rte->rtekind != RTE_RELATION ||
2747 (base_rte->relkind != RELKIND_RELATION &&
2748 base_rte->relkind != RELKIND_FOREIGN_TABLE &&
2749 base_rte->relkind != RELKIND_VIEW &&
2751 return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2752
2753 if (base_rte->tablesample)
2754 return gettext_noop("Views containing TABLESAMPLE are not automatically updatable.");
2755
2756 /*
2757 * Check that the view has at least one updatable column. This is required
2758 * for INSERT/UPDATE but not for DELETE.
2759 */
2760 if (check_cols)
2761 {
2762 ListCell *cell;
2763 bool found;
2764
2765 found = false;
2766 foreach(cell, viewquery->targetList)
2767 {
2768 TargetEntry *tle = (TargetEntry *) lfirst(cell);
2769
2771 {
2772 found = true;
2773 break;
2774 }
2775 }
2776
2777 if (!found)
2778 return gettext_noop("Views that have no updatable columns are not automatically updatable.");
2779 }
2780
2781 return NULL; /* the view is updatable */
2782}

References fb(), gettext_noop, IsA, lfirst, linitial, list_length(), NIL, rt_fetch, RTE_RELATION, and view_col_is_auto_updatable().

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