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

Go to the source code of this file.

Data Structures

struct  rewrite_event
 
struct  acquireLocksOnSubLinks_context
 
struct  fireRIRonSubLink_context
 

Macros

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

Typedefs

typedef struct rewrite_event rewrite_event
 
typedef struct acquireLocksOnSubLinks_context acquireLocksOnSubLinks_context
 
typedef struct fireRIRonSubLink_context fireRIRonSubLink_context
 

Functions

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

Macro Definition Documentation

◆ ALL_EVENTS

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

Typedef Documentation

◆ acquireLocksOnSubLinks_context

◆ fireRIRonSubLink_context

◆ rewrite_event

Function Documentation

◆ acquireLocksOnSubLinks()

static bool acquireLocksOnSubLinks ( Node node,
acquireLocksOnSubLinks_context context 
)
static

Definition at line 310 of file rewriteHandler.c.

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

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

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

◆ AcquireRewriteLocks()

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

Definition at line 148 of file rewriteHandler.c.

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

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

◆ adjust_view_column_set()

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

Definition at line 3061 of file rewriteHandler.c.

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

References bms_add_member(), bms_next_member(), castNode, elog, ERROR, fb(), FirstLowInvalidHeapAttributeNumber, get_tle_by_resno(), InvalidAttrNumber, IsA, lfirst_node, 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 726 of file rewriteHandler.c.

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

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

Referenced by rewriteRuleAction().

◆ ApplyRetrieveRule()

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

Definition at line 1727 of file rewriteHandler.c.

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

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

Referenced by fireRIRrules().

◆ build_column_default()

Node * build_column_default ( Relation  rel,
int  attrno 
)

Definition at line 1244 of file rewriteHandler.c.

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

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

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

◆ build_generation_expression()

Node * build_generation_expression ( Relation  rel,
int  attrno 
)

Definition at line 4534 of file rewriteHandler.c.

4535{
4536 TupleDesc rd_att = RelationGetDescr(rel);
4538 Node *defexpr;
4539 Oid attcollid;
4540
4541 Assert(rd_att->constr && rd_att->constr->has_generated_virtual);
4542 Assert(att_tup->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL);
4543
4544 defexpr = build_column_default(rel, attrno);
4545 if (defexpr == NULL)
4546 elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
4548
4549 /*
4550 * If the column definition has a collation and it is different from the
4551 * collation of the generation expression, put a COLLATE clause around the
4552 * expression.
4553 */
4554 attcollid = att_tup->attcollation;
4555 if (attcollid && attcollid != exprCollation(defexpr))
4556 {
4558
4559 ce->arg = (Expr *) defexpr;
4560 ce->collOid = attcollid;
4561 ce->location = -1;
4562
4563 defexpr = (Node *) ce;
4564 }
4565
4566 return defexpr;
4567}
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:821
#define RelationGetDescr(relation)
Definition rel.h:540
Node * build_column_default(Relation rel, int attrno)
bool has_generated_virtual
Definition tupdesc.h:47
TupleConstr * constr
Definition tupdesc.h:141

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

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

◆ CopyAndAddInvertedQual()

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

Definition at line 2336 of file rewriteHandler.c.

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

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

Referenced by fireRules().

◆ error_view_not_updatable()

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

Definition at line 3135 of file rewriteHandler.c.

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

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

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

◆ expand_generated_columns_in_expr()

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

Definition at line 4508 of file rewriteHandler.c.

4509{
4510 TupleDesc tupdesc = RelationGetDescr(rel);
4511
4512 if (tupdesc->constr && tupdesc->constr->has_generated_virtual)
4513 {
4515
4517 /* eref needs to be set, but the actual name doesn't matter */
4519 rte->rtekind = RTE_RELATION;
4520 rte->relid = RelationGetRelid(rel);
4521
4522 node = expand_generated_columns_internal(node, rel, rt_index, rte, 0);
4523 }
4524
4525 return node;
4526}
Alias * makeAlias(const char *aliasname, List *colnames)
Definition makefuncs.c:438
static Node * expand_generated_columns_internal(Node *node, Relation rel, int rt_index, RangeTblEntry *rte, int result_relation)

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

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

◆ expand_generated_columns_internal()

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

Definition at line 4463 of file rewriteHandler.c.

4465{
4466 TupleDesc tupdesc;
4467
4468 tupdesc = RelationGetDescr(rel);
4469 if (tupdesc->constr && tupdesc->constr->has_generated_virtual)
4470 {
4471 List *tlist = NIL;
4472
4473 for (int i = 0; i < tupdesc->natts; i++)
4474 {
4475 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
4476
4477 if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
4478 {
4479 Node *defexpr;
4480 TargetEntry *te;
4481
4482 defexpr = build_generation_expression(rel, i + 1);
4483 ChangeVarNodes(defexpr, 1, rt_index, 0);
4484
4485 te = makeTargetEntry((Expr *) defexpr, i + 1, 0, false);
4486 tlist = lappend(tlist, te);
4487 }
4488 }
4489
4490 Assert(list_length(tlist) > 0);
4491
4492 node = ReplaceVarsFromTargetList(node, rt_index, 0, rte, tlist,
4493 result_relation,
4494 REPLACEVARS_CHANGE_VARNO, rt_index,
4495 NULL);
4496 }
4497
4498 return node;
4499}
int i
Definition isn.c:77
Node * build_generation_expression(Relation rel, int attrno)

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

Referenced by expand_generated_columns_in_expr().

◆ findDefaultOnlyColumns()

static Bitmapset * findDefaultOnlyColumns ( RangeTblEntry rte)
static

Definition at line 1341 of file rewriteHandler.c.

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

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

Referenced by rewriteTargetListIU().

◆ fireRIRonSubLink()

static bool fireRIRonSubLink ( Node node,
fireRIRonSubLink_context context 
)
static

Definition at line 1971 of file rewriteHandler.c.

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

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

Referenced by fireRIRonSubLink(), and fireRIRrules().

◆ fireRIRrules()

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

Definition at line 2007 of file rewriteHandler.c.

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

References acquireLocksOnSubLinks(), fireRIRonSubLink_context::activeRIRs, ApplyRetrieveRule(), Assert, CMD_SELECT, Query::cteList, CommonTableExpr::ctequery, ereport, errcode(), errmsg(), ERROR, OnConflictExpr::exclRelIndex, expression_tree_walker, fb(), fireRIRonSubLink(), fireRIRrules(), acquireLocksOnSubLinks_context::for_execute, get_row_security_policies(), fireRIRonSubLink_context::hasRowSecurity, i, lappend(), lappend_oid(), lfirst, lfirst_node, list_concat(), list_delete_last(), list_length(), list_member_oid(), NIL, NoLock, Query::onConflict, QTW_IGNORE_RC_SUBQUERIES, query_tree_walker, rangeTableEntry_used(), RelationData::rd_rules, RelationGetRelationName, RelationGetRelid, rewriteSearchAndCycle(), rt_fetch, Query::rtable, RTE_RELATION, RTE_SUBQUERY, rules, 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 2407 of file rewriteHandler.c.

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

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

Referenced by RewriteQuery().

◆ get_assignment_input()

static Node * get_assignment_input ( Node node)
static

Definition at line 1215 of file rewriteHandler.c.

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

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

Referenced by process_matched_tle().

◆ get_view_query()

Query * get_view_query ( Relation  view)

Definition at line 2498 of file rewriteHandler.c.

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

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

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

◆ markQueryForLocking()

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

Definition at line 1907 of file rewriteHandler.c.

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

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

Referenced by ApplyRetrieveRule(), and markQueryForLocking().

◆ matchLocks()

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

Definition at line 1652 of file rewriteHandler.c.

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

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

Referenced by RewriteQuery(), and rewriteValuesRTE().

◆ process_matched_tle()

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

Definition at line 1062 of file rewriteHandler.c.

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

References arg, FieldStore::arg, elog, equal(), ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprType(), fb(), 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 4580 of file rewriteHandler.c.

4581{
4582 int64 input_query_id = parsetree->queryId;
4583 List *querylist;
4584 List *results;
4585 ListCell *l;
4587 bool foundOriginalQuery;
4589
4590 /*
4591 * This function is only applied to top-level original queries
4592 */
4593 Assert(parsetree->querySource == QSRC_ORIGINAL);
4594 Assert(parsetree->canSetTag);
4595
4596 /*
4597 * Step 1
4598 *
4599 * Apply all non-SELECT rules possibly getting 0 or many queries
4600 */
4601 querylist = RewriteQuery(parsetree, NIL, 0, 0);
4602
4603 /*
4604 * Step 2
4605 *
4606 * Apply all the RIR rules on each query
4607 *
4608 * This is also a handy place to mark each query with the original queryId
4609 */
4610 results = NIL;
4611 foreach(l, querylist)
4612 {
4613 Query *query = (Query *) lfirst(l);
4614
4615 query = fireRIRrules(query, NIL);
4616
4617 query->queryId = input_query_id;
4618
4619 results = lappend(results, query);
4620 }
4621
4622 /*
4623 * Step 3
4624 *
4625 * Determine which, if any, of the resulting queries is supposed to set
4626 * the command-result tag; and update the canSetTag fields accordingly.
4627 *
4628 * If the original query is still in the list, it sets the command tag.
4629 * Otherwise, the last INSTEAD query of the same kind as the original is
4630 * allowed to set the tag. (Note these rules can leave us with no query
4631 * setting the tag. The tcop code has to cope with this by setting up a
4632 * default tag based on the original un-rewritten query.)
4633 *
4634 * The Asserts verify that at most one query in the result list is marked
4635 * canSetTag. If we aren't checking asserts, we can fall out of the loop
4636 * as soon as we find the original query.
4637 */
4638 origCmdType = parsetree->commandType;
4639 foundOriginalQuery = false;
4640 lastInstead = NULL;
4641
4642 foreach(l, results)
4643 {
4644 Query *query = (Query *) lfirst(l);
4645
4646 if (query->querySource == QSRC_ORIGINAL)
4647 {
4648 Assert(query->canSetTag);
4650 foundOriginalQuery = true;
4651#ifndef USE_ASSERT_CHECKING
4652 break;
4653#endif
4654 }
4655 else
4656 {
4657 Assert(!query->canSetTag);
4658 if (query->commandType == origCmdType &&
4659 (query->querySource == QSRC_INSTEAD_RULE ||
4660 query->querySource == QSRC_QUAL_INSTEAD_RULE))
4661 lastInstead = query;
4662 }
4663 }
4664
4666 lastInstead->canSetTag = true;
4667
4668 return results;
4669}
int64_t int64
Definition c.h:555
CmdType
Definition nodes.h:273
@ QSRC_ORIGINAL
Definition parsenodes.h:36
static List * RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length, int num_ctes_processed)

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

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

◆ relation_is_updatable()

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

Definition at line 2880 of file rewriteHandler.c.

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

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

◆ RewriteQuery()

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

Definition at line 3896 of file rewriteHandler.c.

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

References 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(), fb(), fireRules(), foreach_current_index, FromExpr::fromlist, gettext_noop, i, IsA, Query::jointree, lappend(), lcons(), lfirst, lfirst_node, linitial, linitial_node, list_concat(), list_delete_last(), list_length(), matchLocks(), Query::mergeActionList, NIL, NoLock, Query::onConflict, ONCONFLICT_UPDATE, OnConflictExpr::onConflictSet, OVERRIDING_NOT_SET, palloc_object, QSRC_NON_INSTEAD_RULE, QSRC_QUAL_INSTEAD_RULE, RelationGetRelationName, RelationGetRelid, Query::returningList, RewriteQuery(), rewriteTargetListIU(), rewriteTargetView(), rewriteValuesRTE(), rewriteValuesRTEToNulls(), rt_fetch, Query::rtable, RTE_RELATION, RTE_SUBQUERY, RTE_VALUES, 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 351 of file rewriteHandler.c.

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

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

Referenced by fireRules().

◆ rewriteTargetListIU()

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

Definition at line 788 of file rewriteHandler.c.

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

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

Referenced by RewriteQuery().

◆ rewriteTargetView()

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

Definition at line 3230 of file rewriteHandler.c.

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

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

Referenced by RewriteQuery().

◆ rewriteValuesRTE()

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

Definition at line 1429 of file rewriteHandler.c.

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

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

Referenced by RewriteQuery().

◆ rewriteValuesRTEToNulls()

static void rewriteValuesRTEToNulls ( Query parsetree,
RangeTblEntry rte 
)
static

Definition at line 1614 of file rewriteHandler.c.

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

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

Referenced by RewriteQuery().

◆ searchForDefault()

static bool searchForDefault ( RangeTblEntry rte)
static

Definition at line 1315 of file rewriteHandler.c.

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

References fb(), IsA, and lfirst.

Referenced by rewriteValuesRTE().

◆ view_col_is_auto_updatable()

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

Definition at line 2601 of file rewriteHandler.c.

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

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

Referenced by view_cols_are_auto_updatable(), and view_query_is_auto_updatable().

◆ view_cols_are_auto_updatable()

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

Definition at line 2797 of file rewriteHandler.c.

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

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

Referenced by relation_is_updatable(), and rewriteTargetView().

◆ view_has_instead_trigger()

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

Definition at line 2537 of file rewriteHandler.c.

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

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

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

◆ view_query_is_auto_updatable()

const char * view_query_is_auto_updatable ( Query viewquery,
bool  check_cols 
)

Definition at line 2649 of file rewriteHandler.c.

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

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

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