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

Go to the source code of this file.

Data Structures

struct  rewrite_event
 
struct  acquireLocksOnSubLinks_context
 
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)
 
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 char * view_col_is_auto_updatable (RangeTblRef *rtr, TargetEntry *tle)
 
const char * view_query_is_auto_updatable (Query *viewquery, bool check_cols)
 
static const char * view_cols_are_auto_updatable (Query *viewquery, Bitmapset *required_cols, Bitmapset **updatable_cols, char **non_updatable_col)
 
int relation_is_updatable (Oid reloid, List *outer_reloids, bool include_triggers, Bitmapset *include_cols)
 
void error_view_not_updatable (Relation view, CmdType command, List *mergeActionList, const char *detail)
 
static QueryrewriteTargetView (Query *parsetree, Relation view)
 
static ListRewriteQuery (Query *parsetree, List *rewrite_events, int orig_rt_length)
 
ListQueryRewrite (Query *parsetree)
 

Macro Definition Documentation

◆ ALL_EVENTS

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

Typedef Documentation

◆ acquireLocksOnSubLinks_context

◆ fireRIRonSubLink_context

◆ rewrite_event

typedef struct rewrite_event rewrite_event

Function Documentation

◆ acquireLocksOnSubLinks()

static bool acquireLocksOnSubLinks ( Node node,
acquireLocksOnSubLinks_context context 
)
static

Definition at line 308 of file rewriteHandler.c.

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

References acquireLocksOnSubLinks(), AcquireRewriteLocks(), expression_tree_walker, 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 146 of file rewriteHandler.c.

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

References AccessShareLock, acquireLocksOnSubLinks(), AcquireRewriteLocks(), Assert, Query::cteList, CommonTableExpr::ctequery, elog, ERROR, 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, RangeTblEntry::relid, RowShareLock, rt_fetch, Query::rtable, RTE_JOIN, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, strip_implicit_coercions(), RangeTblEntry::subquery, table_close(), table_open(), Var::varattno, Var::varlevelsup, and Var::varno.

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

◆ adjust_view_column_set()

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

Definition at line 3063 of file rewriteHandler.c.

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

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

Referenced by relation_is_updatable(), and rewriteTargetView().

◆ adjustJoinTreeList()

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

Definition at line 708 of file rewriteHandler.c.

709{
710 List *newjointree = copyObject(parsetree->jointree->fromlist);
711 ListCell *l;
712
713 if (removert)
714 {
715 foreach(l, newjointree)
716 {
717 RangeTblRef *rtr = lfirst(l);
718
719 if (IsA(rtr, RangeTblRef) &&
720 rtr->rtindex == rt_index)
721 {
722 newjointree = foreach_delete_current(newjointree, l);
723 break;
724 }
725 }
726 }
727 return newjointree;
728}
#define copyObject(obj)
Definition: nodes.h:224
#define foreach_delete_current(lst, var_or_cell)
Definition: pg_list.h:391
List * fromlist
Definition: primnodes.h:2363
FromExpr * jointree
Definition: parsenodes.h:177

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

Referenced by rewriteRuleAction().

◆ ApplyRetrieveRule()

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

Definition at line 1729 of file rewriteHandler.c.

1734{
1735 Query *rule_action;
1736 RangeTblEntry *rte;
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,
1749 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
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 {
1778 RangeTblEntry *newrte;
1779 Var *var;
1780 TargetEntry *tle;
1781
1782 rte = rt_fetch(rt_index, parsetree->rtable);
1783 newrte = copyObject(rte);
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)
1846 markQueryForLocking(rule_action, (Node *) rule_action->jointree,
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:333
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
int ExecCleanTargetListLength(List *targetlist)
Definition: execUtils.c:1164
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:242
char * pstrdup(const char *in)
Definition: mcxt.c:1696
@ CMD_MERGE
Definition: nodes.h:269
@ CMD_INSERT
Definition: nodes.h:267
@ CMD_DELETE
Definition: nodes.h:268
@ CMD_UPDATE
Definition: nodes.h:266
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
int restrict_nonsystem_relation_kind
Definition: postgres.c:104
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define RelationIsSecurityView(relation)
Definition: rel.h:426
static void markQueryForLocking(Query *qry, Node *jtnode, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
static Query * fireRIRrules(Query *parsetree, List *activeRIRs)
void ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
Definition: rewriteManip.c:668
List * returningList
Definition: parsenodes.h:209
CmdType commandType
Definition: parsenodes.h:121
List * targetList
Definition: parsenodes.h:193
struct TableSampleClause * tablesample
Definition: parsenodes.h:1107
LockClauseStrength strength
Definition: parsenodes.h:1589
LockWaitPolicy waitPolicy
Definition: parsenodes.h:1590
Definition: localtime.c:73
#define RESTRICT_RELKIND_VIEW
Definition: tcopprot.h:43
#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(), fireRIRrules(), FirstNormalObjectId, get_parse_rowmark(), RangeTblEntry::inh, Query::jointree, 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, RangeTblEntry::rtekind, RowMarkClause::strength, RangeTblEntry::subquery, RangeTblEntry::tablesample, Query::targetList, unlikely, and RowMarkClause::waitPolicy.

Referenced by fireRIRrules().

◆ build_column_default()

Node * build_column_default ( Relation  rel,
int  attrno 
)

Definition at line 1236 of file rewriteHandler.c.

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

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

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

◆ CopyAndAddInvertedQual()

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

Definition at line 2338 of file rewriteHandler.c.

2342{
2343 /* Don't scribble on the passed qual (it's in the relcache!) */
2344 Node *new_qual = copyObject(rule_qual);
2346
2347 context.for_execute = true;
2348
2349 /*
2350 * In case there are subqueries in the qual, acquire necessary locks and
2351 * fix any deleted JOIN RTE entries. (This is somewhat redundant with
2352 * rewriteRuleAction, but not entirely ... consider restructuring so that
2353 * we only need to process the qual this way once.)
2354 */
2355 (void) acquireLocksOnSubLinks(new_qual, &context);
2356
2357 /* Fix references to OLD */
2358 ChangeVarNodes(new_qual, PRS2_OLD_VARNO, rt_index, 0);
2359 /* Fix references to NEW */
2360 if (event == CMD_INSERT || event == CMD_UPDATE)
2361 new_qual = ReplaceVarsFromTargetList(new_qual,
2363 0,
2364 rt_fetch(rt_index,
2365 parsetree->rtable),
2366 parsetree->targetList,
2367 parsetree->resultRelation,
2368 (event == CMD_UPDATE) ?
2371 rt_index,
2372 &parsetree->hasSubLinks);
2373 /* And attach the fixed qual */
2374 AddInvertedQual(parsetree, new_qual);
2375
2376 return parsetree;
2377}
#define PRS2_OLD_VARNO
Definition: primnodes.h:249
#define PRS2_NEW_VARNO
Definition: primnodes.h:250
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
Definition: rewriteManip.h:40
@ REPLACEVARS_CHANGE_VARNO
Definition: rewriteManip.h:39

References acquireLocksOnSubLinks(), AddInvertedQual(), ChangeVarNodes(), CMD_INSERT, CMD_UPDATE, copyObject, 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 3137 of file rewriteHandler.c.

3141{
3142 TriggerDesc *trigDesc = view->trigdesc;
3143
3144 switch (command)
3145 {
3146 case CMD_INSERT:
3147 ereport(ERROR,
3148 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3149 errmsg("cannot insert into view \"%s\"",
3151 detail ? errdetail_internal("%s", _(detail)) : 0,
3152 errhint("To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule."));
3153 break;
3154 case CMD_UPDATE:
3155 ereport(ERROR,
3156 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3157 errmsg("cannot update view \"%s\"",
3159 detail ? errdetail_internal("%s", _(detail)) : 0,
3160 errhint("To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule."));
3161 break;
3162 case CMD_DELETE:
3163 ereport(ERROR,
3164 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3165 errmsg("cannot delete from view \"%s\"",
3167 detail ? errdetail_internal("%s", _(detail)) : 0,
3168 errhint("To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule."));
3169 break;
3170 case CMD_MERGE:
3171
3172 /*
3173 * Note that the error hints here differ from above, since MERGE
3174 * doesn't support rules.
3175 */
3176 foreach_node(MergeAction, action, mergeActionList)
3177 {
3178 switch (action->commandType)
3179 {
3180 case CMD_INSERT:
3181 if (!trigDesc || !trigDesc->trig_insert_instead_row)
3182 ereport(ERROR,
3183 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3184 errmsg("cannot insert into view \"%s\"",
3186 detail ? errdetail_internal("%s", _(detail)) : 0,
3187 errhint("To enable inserting into the view using MERGE, provide an INSTEAD OF INSERT trigger."));
3188 break;
3189 case CMD_UPDATE:
3190 if (!trigDesc || !trigDesc->trig_update_instead_row)
3191 ereport(ERROR,
3192 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3193 errmsg("cannot update view \"%s\"",
3195 detail ? errdetail_internal("%s", _(detail)) : 0,
3196 errhint("To enable updating the view using MERGE, provide an INSTEAD OF UPDATE trigger."));
3197 break;
3198 case CMD_DELETE:
3199 if (!trigDesc || !trigDesc->trig_delete_instead_row)
3200 ereport(ERROR,
3201 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3202 errmsg("cannot delete from view \"%s\"",
3204 detail ? errdetail_internal("%s", _(detail)) : 0,
3205 errhint("To enable deleting from the view using MERGE, provide an INSTEAD OF DELETE trigger."));
3206 break;
3207 case CMD_NOTHING:
3208 break;
3209 default:
3210 elog(ERROR, "unrecognized commandType: %d", action->commandType);
3211 break;
3212 }
3213 }
3214 break;
3215 default:
3216 elog(ERROR, "unrecognized CmdType: %d", (int) command);
3217 break;
3218 }
3219}
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1230
#define _(x)
Definition: elog.c:90
@ CMD_NOTHING
Definition: nodes.h:272
#define foreach_node(type, var, lst)
Definition: pg_list.h:496
TriggerDesc * trigdesc
Definition: rel.h:117
bool trig_update_instead_row
Definition: reltrigger.h:63
bool trig_delete_instead_row
Definition: reltrigger.h:68
bool trig_insert_instead_row
Definition: reltrigger.h:58

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

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

◆ findDefaultOnlyColumns()

static Bitmapset * findDefaultOnlyColumns ( RangeTblEntry rte)
static

Definition at line 1333 of file rewriteHandler.c.

1334{
1335 Bitmapset *default_only_cols = NULL;
1336 ListCell *lc;
1337
1338 foreach(lc, rte->values_lists)
1339 {
1340 List *sublist = (List *) lfirst(lc);
1341 ListCell *lc2;
1342 int i;
1343
1344 if (default_only_cols == NULL)
1345 {
1346 /* Populate the initial result bitmap from the first row */
1347 i = 0;
1348 foreach(lc2, sublist)
1349 {
1350 Node *col = (Node *) lfirst(lc2);
1351
1352 i++;
1353 if (IsA(col, SetToDefault))
1354 default_only_cols = bms_add_member(default_only_cols, i);
1355 }
1356 }
1357 else
1358 {
1359 /* Update the result bitmap from this next row */
1360 i = 0;
1361 foreach(lc2, sublist)
1362 {
1363 Node *col = (Node *) lfirst(lc2);
1364
1365 i++;
1366 if (!IsA(col, SetToDefault))
1367 default_only_cols = bms_del_member(default_only_cols, i);
1368 }
1369 }
1370
1371 /*
1372 * If no column in the rows read so far contains only DEFAULT items,
1373 * we are done.
1374 */
1375 if (bms_is_empty(default_only_cols))
1376 break;
1377 }
1378
1379 return default_only_cols;
1380}
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
#define bms_is_empty(a)
Definition: bitmapset.h:118
int i
Definition: isn.c:72
List * values_lists
Definition: parsenodes.h:1199

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

Referenced by rewriteTargetListIU().

◆ fireRIRonSubLink()

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

References acquireLocksOnSubLinks(), fireRIRonSubLink_context::activeRIRs, ApplyRetrieveRule(), Assert, CMD_SELECT, Query::cteList, CommonTableExpr::ctequery, ereport, errcode(), errmsg(), ERROR, OnConflictExpr::exclRelIndex, expression_tree_walker, 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, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, rewriteSearchAndCycle(), rt_fetch, Query::rtable, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, rules, RangeTblEntry::subquery, table_close(), and table_open().

Referenced by ApplyRetrieveRule(), fireRIRonSubLink(), 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 2409 of file rewriteHandler.c.

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

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

Referenced by RewriteQuery().

◆ get_assignment_input()

static Node * get_assignment_input ( Node node)
static

Definition at line 1207 of file rewriteHandler.c.

1208{
1209 if (node == NULL)
1210 return NULL;
1211 if (IsA(node, FieldStore))
1212 {
1213 FieldStore *fstore = (FieldStore *) node;
1214
1215 return (Node *) fstore->arg;
1216 }
1217 else if (IsA(node, SubscriptingRef))
1218 {
1219 SubscriptingRef *sbsref = (SubscriptingRef *) node;
1220
1221 if (sbsref->refassgnexpr == NULL)
1222 return NULL;
1223
1224 return (Node *) sbsref->refexpr;
1225 }
1226
1227 return NULL;
1228}
Expr * arg
Definition: primnodes.h:1175
Expr * refassgnexpr
Definition: primnodes.h:719
Expr * refexpr
Definition: primnodes.h:717

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

Referenced by process_matched_tle().

◆ get_view_query()

Query * get_view_query ( Relation  view)

Definition at line 2500 of file rewriteHandler.c.

2501{
2502 int i;
2503
2504 Assert(view->rd_rel->relkind == RELKIND_VIEW);
2505
2506 for (i = 0; i < view->rd_rules->numLocks; i++)
2507 {
2508 RewriteRule *rule = view->rd_rules->rules[i];
2509
2510 if (rule->event == CMD_SELECT)
2511 {
2512 /* A _RETURN rule should have only one action */
2513 if (list_length(rule->actions) != 1)
2514 elog(ERROR, "invalid _RETURN rule action specification");
2515
2516 return (Query *) linitial(rule->actions);
2517 }
2518 }
2519
2520 elog(ERROR, "failed to find _RETURN rule for view");
2521 return NULL; /* keep compiler quiet */
2522}
RewriteRule ** rules
Definition: prs2lock.h:43
int numLocks
Definition: prs2lock.h:42

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

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

◆ markQueryForLocking()

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

Definition at line 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 {
1922 RTEPermissionInfo *perminfo;
1923
1924 applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
1925
1926 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
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 */
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:73
#define nodeTag(nodeptr)
Definition: nodes.h:133
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
#define ACL_SELECT_FOR_UPDATE
Definition: parsenodes.h:94
void applyLockingClause(Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
Definition: analyze.c:3723
AclMode requiredPerms
Definition: parsenodes.h:1300

References ACL_SELECT_FOR_UPDATE, applyLockingClause(), elog, ERROR, FromExpr::fromlist, getRTEPermissionInfo(), IsA, j, Query::jointree, lfirst, markQueryForLocking(), nodeTag, RTEPermissionInfo::requiredPerms, rt_fetch, Query::rtable, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, and RangeTblEntry::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;
1661 List *matching_locks = NIL;
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,
1707 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
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))
1717 matching_locks = lappend(matching_locks, oneLock);
1718 }
1719 }
1720
1721 return matching_locks;
1722}
int errdetail(const char *fmt,...)
Definition: elog.c:1203
#define RULE_FIRES_ON_ORIGIN
Definition: rewriteDefine.h:21
#define RULE_FIRES_ON_REPLICA
Definition: rewriteDefine.h:23
#define RULE_DISABLED
Definition: rewriteDefine.h:24
CmdType event
Definition: prs2lock.h:27
char enabled
Definition: prs2lock.h:30
int SessionReplicationRole
Definition: trigger.c:63
#define SESSION_REPLICATION_ROLE_REPLICA
Definition: trigger.h:141

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

Referenced by RewriteQuery(), and rewriteValuesRTE().

◆ process_matched_tle()

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

Definition at line 1054 of file rewriteHandler.c.

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

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

Referenced by rewriteTargetListIU().

◆ QueryRewrite()

List * QueryRewrite ( Query parsetree)

Definition at line 4441 of file rewriteHandler.c.

4442{
4443 uint64 input_query_id = parsetree->queryId;
4444 List *querylist;
4445 List *results;
4446 ListCell *l;
4447 CmdType origCmdType;
4448 bool foundOriginalQuery;
4449 Query *lastInstead;
4450
4451 /*
4452 * This function is only applied to top-level original queries
4453 */
4454 Assert(parsetree->querySource == QSRC_ORIGINAL);
4455 Assert(parsetree->canSetTag);
4456
4457 /*
4458 * Step 1
4459 *
4460 * Apply all non-SELECT rules possibly getting 0 or many queries
4461 */
4462 querylist = RewriteQuery(parsetree, NIL, 0);
4463
4464 /*
4465 * Step 2
4466 *
4467 * Apply all the RIR rules on each query
4468 *
4469 * This is also a handy place to mark each query with the original queryId
4470 */
4471 results = NIL;
4472 foreach(l, querylist)
4473 {
4474 Query *query = (Query *) lfirst(l);
4475
4476 query = fireRIRrules(query, NIL);
4477
4478 query->queryId = input_query_id;
4479
4480 results = lappend(results, query);
4481 }
4482
4483 /*
4484 * Step 3
4485 *
4486 * Determine which, if any, of the resulting queries is supposed to set
4487 * the command-result tag; and update the canSetTag fields accordingly.
4488 *
4489 * If the original query is still in the list, it sets the command tag.
4490 * Otherwise, the last INSTEAD query of the same kind as the original is
4491 * allowed to set the tag. (Note these rules can leave us with no query
4492 * setting the tag. The tcop code has to cope with this by setting up a
4493 * default tag based on the original un-rewritten query.)
4494 *
4495 * The Asserts verify that at most one query in the result list is marked
4496 * canSetTag. If we aren't checking asserts, we can fall out of the loop
4497 * as soon as we find the original query.
4498 */
4499 origCmdType = parsetree->commandType;
4500 foundOriginalQuery = false;
4501 lastInstead = NULL;
4502
4503 foreach(l, results)
4504 {
4505 Query *query = (Query *) lfirst(l);
4506
4507 if (query->querySource == QSRC_ORIGINAL)
4508 {
4509 Assert(query->canSetTag);
4510 Assert(!foundOriginalQuery);
4511 foundOriginalQuery = true;
4512#ifndef USE_ASSERT_CHECKING
4513 break;
4514#endif
4515 }
4516 else
4517 {
4518 Assert(!query->canSetTag);
4519 if (query->commandType == origCmdType &&
4520 (query->querySource == QSRC_INSTEAD_RULE ||
4521 query->querySource == QSRC_QUAL_INSTEAD_RULE))
4522 lastInstead = query;
4523 }
4524 }
4525
4526 if (!foundOriginalQuery && lastInstead != NULL)
4527 lastInstead->canSetTag = true;
4528
4529 return results;
4530}
uint64_t uint64
Definition: c.h:489
CmdType
Definition: nodes.h:263
@ QSRC_ORIGINAL
Definition: parsenodes.h:36
static List * RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length)

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

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

◆ relation_is_updatable()

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

Definition at line 2882 of file rewriteHandler.c.

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

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

Referenced by pg_column_is_updatable(), pg_relation_is_updatable(), and relation_is_updatable().

◆ RewriteQuery()

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

Definition at line 3895 of file rewriteHandler.c.

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

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

Referenced by QueryRewrite(), 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 349 of file rewriteHandler.c.

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

Referenced by fireRules().

◆ rewriteTargetListIU()

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

Definition at line 770 of file rewriteHandler.c.

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

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

Referenced by RewriteQuery().

◆ rewriteTargetView()

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

Definition at line 3232 of file rewriteHandler.c.

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

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

Referenced by RewriteQuery().

◆ rewriteValuesRTE()

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

Definition at line 1421 of file rewriteHandler.c.

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

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

Referenced by RewriteQuery().

◆ rewriteValuesRTEToNulls()

static void rewriteValuesRTEToNulls ( Query parsetree,
RangeTblEntry rte 
)
static

Definition at line 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
1636 newList = lappend(newList, makeNullConst(def->typeId,
1637 def->typeMod,
1638 def->collation));
1639 }
1640 else
1641 newList = lappend(newList, col);
1642 }
1643 newValues = lappend(newValues, newList);
1644 }
1645 rte->values_lists = newValues;
1646}

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

Referenced by RewriteQuery().

◆ searchForDefault()

static bool searchForDefault ( RangeTblEntry rte)
static

Definition at line 1307 of file rewriteHandler.c.

1308{
1309 ListCell *lc;
1310
1311 foreach(lc, rte->values_lists)
1312 {
1313 List *sublist = (List *) lfirst(lc);
1314 ListCell *lc2;
1315
1316 foreach(lc2, sublist)
1317 {
1318 Node *col = (Node *) lfirst(lc2);
1319
1320 if (IsA(col, SetToDefault))
1321 return true;
1322 }
1323 }
1324 return false;
1325}

References IsA, lfirst, and RangeTblEntry::values_lists.

Referenced by rewriteValuesRTE().

◆ view_col_is_auto_updatable()

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

Definition at line 2603 of file rewriteHandler.c.

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

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

Referenced by view_cols_are_auto_updatable(), and view_query_is_auto_updatable().

◆ view_cols_are_auto_updatable()

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

Definition at line 2799 of file rewriteHandler.c.

2803{
2804 RangeTblRef *rtr;
2805 AttrNumber col;
2806 ListCell *cell;
2807
2808 /*
2809 * The caller should have verified that this view is auto-updatable and so
2810 * there should be a single base relation.
2811 */
2812 Assert(list_length(viewquery->jointree->fromlist) == 1);
2813 rtr = linitial_node(RangeTblRef, viewquery->jointree->fromlist);
2814
2815 /* Initialize the optional return values */
2816 if (updatable_cols != NULL)
2817 *updatable_cols = NULL;
2818 if (non_updatable_col != NULL)
2819 *non_updatable_col = NULL;
2820
2821 /* Test each view column for updatability */
2823 foreach(cell, viewquery->targetList)
2824 {
2825 TargetEntry *tle = (TargetEntry *) lfirst(cell);
2826 const char *col_update_detail;
2827
2828 col++;
2829 col_update_detail = view_col_is_auto_updatable(rtr, tle);
2830
2831 if (col_update_detail == NULL)
2832 {
2833 /* The column is updatable */
2834 if (updatable_cols != NULL)
2835 *updatable_cols = bms_add_member(*updatable_cols, col);
2836 }
2837 else if (bms_is_member(col, required_cols))
2838 {
2839 /* The required column is not updatable */
2840 if (non_updatable_col != NULL)
2841 *non_updatable_col = tle->resname;
2842 return col_update_detail;
2843 }
2844 }
2845
2846 return NULL; /* all the required view columns are updatable */
2847}
static const char * view_col_is_auto_updatable(RangeTblRef *rtr, TargetEntry *tle)

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

Referenced by relation_is_updatable(), and rewriteTargetView().

◆ view_has_instead_trigger()

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

Definition at line 2539 of file rewriteHandler.c.

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

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

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

◆ view_query_is_auto_updatable()

const char * view_query_is_auto_updatable ( Query viewquery,
bool  check_cols 
)

Definition at line 2651 of file rewriteHandler.c.

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

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

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