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 Listget_generated_columns (Relation rel, int rt_index, bool include_stored)
 
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 311 of file rewriteHandler.c.

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

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

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

◆ adjust_view_column_set()

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

Definition at line 3138 of file rewriteHandler.c.

3139{
3141 int col;
3142
3143 col = -1;
3144 while ((col = bms_next_member(cols, col)) >= 0)
3145 {
3146 /* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
3148
3149 if (attno == InvalidAttrNumber)
3150 {
3151 /*
3152 * There's a whole-row reference to the view. For permissions
3153 * purposes, treat it as a reference to each column available from
3154 * the view. (We should *not* convert this to a whole-row
3155 * reference to the base relation, since the view may not touch
3156 * all columns of the base relation.)
3157 */
3158 ListCell *lc;
3159
3160 foreach(lc, targetlist)
3161 {
3163 Var *var;
3164
3165 if (tle->resjunk)
3166 continue;
3167 var = castNode(Var, tle->expr);
3170 }
3171 }
3172 else
3173 {
3174 /*
3175 * Views do not have system columns, so we do not expect to see
3176 * any other system attnos here. If we do find one, the error
3177 * case will apply.
3178 */
3179 TargetEntry *tle = get_tle_by_resno(targetlist, attno);
3180
3181 if (tle != NULL && !tle->resjunk && IsA(tle->expr, Var))
3182 {
3183 Var *var = (Var *) tle->expr;
3184
3186 var->varattno - FirstLowInvalidHeapAttributeNumber);
3187 }
3188 else
3189 elog(ERROR, "attribute number %d not found in view targetlist",
3190 attno);
3191 }
3192 }
3193
3194 return result;
3195}
int16 AttrNumber
Definition attnum.h:21
#define InvalidAttrNumber
Definition attnum.h:23
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
uint32 result
#define castNode(_type_, nodeptr)
Definition nodes.h:182
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define lfirst_node(type, lc)
Definition pg_list.h:176
AttrNumber varattno
Definition primnodes.h:275
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27

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

Referenced by relation_is_updatable(), and rewriteTargetView().

◆ adjustJoinTreeList()

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

Definition at line 761 of file rewriteHandler.c.

762{
764 ListCell *l;
765
766 if (removert)
767 {
768 foreach(l, newjointree)
769 {
770 RangeTblRef *rtr = lfirst(l);
771
772 if (IsA(rtr, RangeTblRef) &&
773 rtr->rtindex == rt_index)
774 {
776 break;
777 }
778 }
779 }
780 return newjointree;
781}
#define copyObject(obj)
Definition nodes.h:232
#define foreach_delete_current(lst, var_or_cell)
Definition pg_list.h:423
List * fromlist
Definition primnodes.h:2384
FromExpr * jointree
Definition parsenodes.h:185

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

Referenced by rewriteRuleAction().

◆ ApplyRetrieveRule()

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

Definition at line 1762 of file rewriteHandler.c.

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

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

Referenced by fireRIRrules().

◆ build_column_default()

Node * build_column_default ( Relation  rel,
int  attrno 
)

Definition at line 1279 of file rewriteHandler.c.

1280{
1281 TupleDesc rd_att = rel->rd_att;
1283 Oid atttype = att_tup->atttypid;
1284 int32 atttypmod = att_tup->atttypmod;
1285 Node *expr = NULL;
1286 Oid exprtype;
1287
1288 if (att_tup->attidentity)
1289 {
1291
1292 nve->seqid = getIdentitySequence(rel, attrno, false);
1293 nve->typeId = att_tup->atttypid;
1294
1295 return (Node *) nve;
1296 }
1297
1298 /*
1299 * If relation has a default for this column, fetch that expression.
1300 */
1301 if (att_tup->atthasdef)
1302 {
1303 expr = TupleDescGetDefault(rd_att, attrno);
1304 if (expr == NULL)
1305 elog(ERROR, "default expression not found for attribute %d of relation \"%s\"",
1307 }
1308
1309 /*
1310 * No per-column default, so look for a default for the type itself. But
1311 * not for generated columns.
1312 */
1313 if (expr == NULL && !att_tup->attgenerated)
1314 expr = get_typdefault(atttype);
1315
1316 if (expr == NULL)
1317 return NULL; /* No default anywhere */
1318
1319 /*
1320 * Make sure the value is coerced to the target column type; this will
1321 * generally be true already, but there seem to be some corner cases
1322 * involving domain defaults where it might not be true. This should match
1323 * the parser's processing of non-defaulted expressions --- see
1324 * transformAssignedExpr().
1325 */
1326 exprtype = exprType(expr);
1327
1328 expr = coerce_to_target_type(NULL, /* no UNKNOWN params here */
1329 expr, exprtype,
1330 atttype, atttypmod,
1333 -1);
1334 if (expr == NULL)
1335 ereport(ERROR,
1337 errmsg("column \"%s\" is of type %s"
1338 " but default expression is of type %s",
1339 NameStr(att_tup->attname),
1340 format_type_be(atttype),
1341 format_type_be(exprtype)),
1342 errhint("You will need to rewrite or cast the expression.")));
1343
1344 return expr;
1345}
#define NameStr(name)
Definition c.h:835
int32_t int32
Definition c.h:620
int errhint(const char *fmt,...) pg_attribute_printf(1
char * format_type_be(Oid type_oid)
Node * get_typdefault(Oid typid)
Definition lsyscache.c:2643
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
#define makeNode(_type_)
Definition nodes.h:161
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
FormData_pg_attribute * Form_pg_attribute
Oid getIdentitySequence(Relation rel, AttrNumber attnum, bool missing_ok)
Definition pg_depend.c:1018
unsigned int Oid
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
@ COERCION_ASSIGNMENT
Definition primnodes.h:748
TupleDesc rd_att
Definition rel.h:112
Node * TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
Definition tupdesc.c:1152
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178

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

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

◆ build_generation_expression()

Node * build_generation_expression ( Relation  rel,
int  attrno 
)

Definition at line 4685 of file rewriteHandler.c.

4686{
4687 TupleDesc rd_att = RelationGetDescr(rel);
4689 Node *defexpr;
4690 Oid attcollid;
4691
4692 Assert(rd_att->constr &&
4693 (rd_att->constr->has_generated_virtual ||
4694 rd_att->constr->has_generated_stored));
4695 Assert(att_tup->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL ||
4696 att_tup->attgenerated == ATTRIBUTE_GENERATED_STORED);
4697
4698 defexpr = build_column_default(rel, attrno);
4699 if (defexpr == NULL)
4700 elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
4702
4703 /*
4704 * If the column definition has a collation and it is different from the
4705 * collation of the generation expression, put a COLLATE clause around the
4706 * expression.
4707 */
4708 attcollid = att_tup->attcollation;
4709 if (attcollid && attcollid != exprCollation(defexpr))
4710 {
4712
4713 ce->arg = (Expr *) defexpr;
4714 ce->collOid = attcollid;
4715 ce->location = -1;
4716
4717 defexpr = (Node *) ce;
4718 }
4719
4720 return defexpr;
4721}
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
#define RelationGetDescr(relation)
Definition rel.h:542
Node * build_column_default(Relation rel, int attrno)
bool has_generated_virtual
Definition tupdesc.h:47
bool has_generated_stored
Definition tupdesc.h:46
TupleConstr * constr
Definition tupdesc.h:159

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

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

◆ CopyAndAddInvertedQual()

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

Definition at line 2381 of file rewriteHandler.c.

2385{
2386 /* Don't scribble on the passed qual (it's in the relcache!) */
2389
2390 context.for_execute = true;
2391
2392 /*
2393 * In case there are subqueries in the qual, acquire necessary locks and
2394 * fix any deleted JOIN RTE entries. (This is somewhat redundant with
2395 * rewriteRuleAction, but not entirely ... consider restructuring so that
2396 * we only need to process the qual this way once.)
2397 */
2399
2400 /* Fix references to OLD */
2401 ChangeVarNodes(new_qual, PRS2_OLD_VARNO, rt_index, 0);
2402 /* Fix references to NEW */
2403 if (event == CMD_INSERT || event == CMD_UPDATE)
2404 {
2405 RangeTblEntry *rte = rt_fetch(rt_index, parsetree->rtable);
2406 Relation rel;
2407 List *gen_cols;
2408
2409 /*
2410 * As in rewriteRuleAction, build entries for generated columns so
2411 * that new.gen_col in the rule qualification can be rewritten
2412 * correctly.
2413 */
2414 rel = relation_open(rte->relid, NoLock);
2416 relation_close(rel, NoLock);
2417
2418 /*
2419 * The generated column expressions refer to new.attribute, so they
2420 * must be rewritten before they can be used as replacements.
2421 */
2422 gen_cols = (List *)
2425 0,
2426 rte,
2427 parsetree->targetList,
2428 parsetree->resultRelation,
2429 (event == CMD_UPDATE) ?
2432 rt_index,
2433 &parsetree->hasSubLinks);
2434
2437 0,
2438 rte,
2440 parsetree->targetList),
2441 parsetree->resultRelation,
2442 (event == CMD_UPDATE) ?
2445 rt_index,
2446 &parsetree->hasSubLinks);
2447 }
2448 /* And attach the fixed qual */
2449 AddInvertedQual(parsetree, new_qual);
2450
2451 return parsetree;
2452}
List * list_concat(List *list1, const List *list2)
Definition list.c:561
#define PRS2_OLD_VARNO
Definition primnodes.h:251
#define PRS2_NEW_VARNO
Definition primnodes.h:252
static List * get_generated_columns(Relation rel, int rt_index, bool include_stored)
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, get_generated_columns(), list_concat(), NoLock, PRS2_NEW_VARNO, PRS2_OLD_VARNO, relation_close(), relation_open(), 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 3212 of file rewriteHandler.c.

3216{
3217 TriggerDesc *trigDesc = view->trigdesc;
3218
3219 switch (command)
3220 {
3221 case CMD_INSERT:
3222 ereport(ERROR,
3224 errmsg("cannot insert into view \"%s\"",
3226 detail ? errdetail_internal("%s", _(detail)) : 0,
3227 errhint("To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule."));
3228 break;
3229 case CMD_UPDATE:
3230 ereport(ERROR,
3232 errmsg("cannot update view \"%s\"",
3234 detail ? errdetail_internal("%s", _(detail)) : 0,
3235 errhint("To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule."));
3236 break;
3237 case CMD_DELETE:
3238 ereport(ERROR,
3240 errmsg("cannot delete from view \"%s\"",
3242 detail ? errdetail_internal("%s", _(detail)) : 0,
3243 errhint("To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule."));
3244 break;
3245 case CMD_MERGE:
3246
3247 /*
3248 * Note that the error hints here differ from above, since MERGE
3249 * doesn't support rules.
3250 */
3251 foreach_node(MergeAction, action, mergeActionList)
3252 {
3253 switch (action->commandType)
3254 {
3255 case CMD_INSERT:
3256 if (!trigDesc || !trigDesc->trig_insert_instead_row)
3257 ereport(ERROR,
3259 errmsg("cannot insert into view \"%s\"",
3261 detail ? errdetail_internal("%s", _(detail)) : 0,
3262 errhint("To enable inserting into the view using MERGE, provide an INSTEAD OF INSERT trigger."));
3263 break;
3264 case CMD_UPDATE:
3265 if (!trigDesc || !trigDesc->trig_update_instead_row)
3266 ereport(ERROR,
3268 errmsg("cannot update view \"%s\"",
3270 detail ? errdetail_internal("%s", _(detail)) : 0,
3271 errhint("To enable updating the view using MERGE, provide an INSTEAD OF UPDATE trigger."));
3272 break;
3273 case CMD_DELETE:
3274 if (!trigDesc || !trigDesc->trig_delete_instead_row)
3275 ereport(ERROR,
3277 errmsg("cannot delete from view \"%s\"",
3279 detail ? errdetail_internal("%s", _(detail)) : 0,
3280 errhint("To enable deleting from the view using MERGE, provide an INSTEAD OF DELETE trigger."));
3281 break;
3282 case CMD_NOTHING:
3283 break;
3284 default:
3285 elog(ERROR, "unrecognized commandType: %d", action->commandType);
3286 break;
3287 }
3288 }
3289 break;
3290 default:
3291 elog(ERROR, "unrecognized CmdType: %d", (int) command);
3292 break;
3293 }
3294}
#define _(x)
Definition elog.c:96
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
@ CMD_NOTHING
Definition nodes.h:282
#define foreach_node(type, var, lst)
Definition pg_list.h:528
TriggerDesc * trigdesc
Definition rel.h:117

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

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

◆ expand_generated_columns_in_expr()

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

Definition at line 4646 of file rewriteHandler.c.

4647{
4648 TupleDesc tupdesc = RelationGetDescr(rel);
4649
4650 if (tupdesc->constr && tupdesc->constr->has_generated_virtual)
4651 {
4653 List *vcols;
4654
4656 /* eref needs to be set, but the actual name doesn't matter */
4658 rte->rtekind = RTE_RELATION;
4659 rte->relid = RelationGetRelid(rel);
4660
4661 vcols = get_generated_columns(rel, rt_index, false);
4662
4663 if (vcols)
4664 {
4665 /*
4666 * Passing NULL for outer_hasSubLinks is safe because generation
4667 * expressions cannot contain SubLinks, so the replacement cannot
4668 * introduce any.
4669 */
4670 node = ReplaceVarsFromTargetList(node, rt_index, 0, rte, vcols, 0,
4671 REPLACEVARS_CHANGE_VARNO, rt_index,
4672 NULL);
4673 }
4674 }
4675
4676 return node;
4677}
Alias * makeAlias(const char *aliasname, List *colnames)
Definition makefuncs.c:438

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

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

◆ findDefaultOnlyColumns()

static Bitmapset * findDefaultOnlyColumns ( RangeTblEntry rte)
static

Definition at line 1376 of file rewriteHandler.c.

1377{
1379 ListCell *lc;
1380
1381 foreach(lc, rte->values_lists)
1382 {
1383 List *sublist = (List *) lfirst(lc);
1384 ListCell *lc2;
1385 int i;
1386
1387 if (default_only_cols == NULL)
1388 {
1389 /* Populate the initial result bitmap from the first row */
1390 i = 0;
1391 foreach(lc2, sublist)
1392 {
1393 Node *col = (Node *) lfirst(lc2);
1394
1395 i++;
1396 if (IsA(col, SetToDefault))
1398 }
1399 }
1400 else
1401 {
1402 /* Update the result bitmap from this next row */
1403 i = 0;
1404 foreach(lc2, sublist)
1405 {
1406 Node *col = (Node *) lfirst(lc2);
1407
1408 i++;
1409 if (!IsA(col, SetToDefault))
1411 }
1412 }
1413
1414 /*
1415 * If no column in the rows read so far contains only DEFAULT items,
1416 * we are done.
1417 */
1419 break;
1420 }
1421
1422 return default_only_cols;
1423}
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:852
#define bms_is_empty(a)
Definition bitmapset.h:118
int i
Definition isn.c:77

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 2006 of file rewriteHandler.c.

2007{
2008 if (node == NULL)
2009 return false;
2010 if (IsA(node, SubLink))
2011 {
2012 SubLink *sub = (SubLink *) node;
2013
2014 /* Do what we came for */
2015 sub->subselect = (Node *) fireRIRrules((Query *) sub->subselect,
2016 context->activeRIRs);
2017
2018 /*
2019 * Remember if any of the sublinks have row security.
2020 */
2021 context->hasRowSecurity |= ((Query *) sub->subselect)->hasRowSecurity;
2022
2023 /* Fall through to process lefthand args of SubLink */
2024 }
2025
2026 /*
2027 * Do NOT recurse into Query nodes, because fireRIRrules already processed
2028 * subselects of subselects for us.
2029 */
2030 return expression_tree_walker(node, fireRIRonSubLink, context);
2031}
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 2042 of file rewriteHandler.c.

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

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

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

◆ fireRules()

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

Definition at line 2484 of file rewriteHandler.c.

2491{
2492 List *results = NIL;
2493 ListCell *l;
2494
2495 foreach(l, locks)
2496 {
2498 Node *event_qual = rule_lock->qual;
2499 List *actions = rule_lock->actions;
2501 ListCell *r;
2502
2503 /* Determine correct QuerySource value for actions */
2504 if (rule_lock->isInstead)
2505 {
2506 if (event_qual != NULL)
2508 else
2509 {
2511 *instead_flag = true; /* report unqualified INSTEAD */
2512 }
2513 }
2514 else
2516
2518 {
2519 /*
2520 * If there are INSTEAD rules with qualifications, the original
2521 * query is still performed. But all the negated rule
2522 * qualifications of the INSTEAD rules are added so it does its
2523 * actions only in cases where the rule quals of all INSTEAD rules
2524 * are false. Think of it as the default action in a case. We save
2525 * this in *qual_product so RewriteQuery() can add it to the query
2526 * list after we mangled it up enough.
2527 *
2528 * If we have already found an unqualified INSTEAD rule, then
2529 * *qual_product won't be used, so don't bother building it.
2530 */
2531 if (!*instead_flag)
2532 {
2533 if (*qual_product == NULL)
2534 *qual_product = copyObject(parsetree);
2536 event_qual,
2537 rt_index,
2538 event);
2539 }
2540 }
2541
2542 /* Now process the rule's actions and add them to the result list */
2543 foreach(r, actions)
2544 {
2545 Query *rule_action = lfirst(r);
2546
2547 if (rule_action->commandType == CMD_NOTHING)
2548 continue;
2549
2551 event_qual, rt_index, event,
2553
2554 rule_action->querySource = qsrc;
2555 rule_action->canSetTag = false; /* might change later */
2556
2557 results = lappend(results, rule_action);
2558 }
2559 }
2560
2561 return results;
2562}
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 1250 of file rewriteHandler.c.

1251{
1252 if (node == NULL)
1253 return NULL;
1254 if (IsA(node, FieldStore))
1255 {
1256 FieldStore *fstore = (FieldStore *) node;
1257
1258 return (Node *) fstore->arg;
1259 }
1260 else if (IsA(node, SubscriptingRef))
1261 {
1262 SubscriptingRef *sbsref = (SubscriptingRef *) node;
1263
1264 if (sbsref->refassgnexpr == NULL)
1265 return NULL;
1266
1267 return (Node *) sbsref->refexpr;
1268 }
1269
1270 return NULL;
1271}
Expr * arg
Definition primnodes.h:1194
Expr * refassgnexpr
Definition primnodes.h:736

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

Referenced by process_matched_tle().

◆ get_generated_columns()

static List * get_generated_columns ( Relation  rel,
int  rt_index,
bool  include_stored 
)
static

Definition at line 4607 of file rewriteHandler.c.

4608{
4609 List *gen_cols = NIL;
4610 TupleDesc tupdesc;
4611
4612 tupdesc = RelationGetDescr(rel);
4613 if (tupdesc->constr &&
4614 (tupdesc->constr->has_generated_virtual ||
4616 {
4617 for (int i = 0; i < tupdesc->natts; i++)
4618 {
4619 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
4620
4621 if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL ||
4622 (include_stored && attr->attgenerated == ATTRIBUTE_GENERATED_STORED))
4623 {
4624 Node *defexpr;
4625 TargetEntry *te;
4626
4627 defexpr = build_generation_expression(rel, i + 1);
4628 ChangeVarNodes(defexpr, 1, rt_index, 0);
4629
4630 te = makeTargetEntry((Expr *) defexpr, i + 1, 0, false);
4631 gen_cols = lappend(gen_cols, te);
4632 }
4633 }
4634 }
4635
4636 return gen_cols;
4637}
Node * build_generation_expression(Relation rel, int attrno)

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

Referenced by CopyAndAddInvertedQual(), expand_generated_columns_in_expr(), and rewriteRuleAction().

◆ get_view_query()

Query * get_view_query ( Relation  view)

Definition at line 2575 of file rewriteHandler.c.

2576{
2577 int i;
2578
2579 Assert(view->rd_rel->relkind == RELKIND_VIEW);
2580
2581 for (i = 0; i < view->rd_rules->numLocks; i++)
2582 {
2583 RewriteRule *rule = view->rd_rules->rules[i];
2584
2585 if (rule->event == CMD_SELECT)
2586 {
2587 /* A _RETURN rule should have only one action */
2588 if (list_length(rule->actions) != 1)
2589 elog(ERROR, "invalid _RETURN rule action specification");
2590
2591 return (Query *) linitial(rule->actions);
2592 }
2593 }
2594
2595 elog(ERROR, "failed to find _RETURN rule for view");
2596 return NULL; /* keep compiler quiet */
2597}
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 1942 of file rewriteHandler.c.

1945{
1946 if (jtnode == NULL)
1947 return;
1948 if (IsA(jtnode, RangeTblRef))
1949 {
1950 int rti = ((RangeTblRef *) jtnode)->rtindex;
1951 RangeTblEntry *rte = rt_fetch(rti, qry->rtable);
1952
1953 if (rte->rtekind == RTE_RELATION)
1954 {
1956
1957 applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
1958
1959 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
1960 perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
1961 }
1962 else if (rte->rtekind == RTE_SUBQUERY)
1963 {
1964 applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
1965 /* FOR UPDATE/SHARE of subquery is propagated to subquery's rels */
1966 markQueryForLocking(rte->subquery, (Node *) rte->subquery->jointree,
1967 strength, waitPolicy, true);
1968 }
1969 /* other RTE types are unaffected by FOR UPDATE */
1970 }
1971 else if (IsA(jtnode, FromExpr))
1972 {
1973 FromExpr *f = (FromExpr *) jtnode;
1974 ListCell *l;
1975
1976 foreach(l, f->fromlist)
1977 markQueryForLocking(qry, lfirst(l), strength, waitPolicy, pushedDown);
1978 }
1979 else if (IsA(jtnode, JoinExpr))
1980 {
1981 JoinExpr *j = (JoinExpr *) jtnode;
1982
1983 markQueryForLocking(qry, j->larg, strength, waitPolicy, pushedDown);
1984 markQueryForLocking(qry, j->rarg, strength, waitPolicy, pushedDown);
1985 }
1986 else
1987 elog(ERROR, "unrecognized node type: %d",
1988 (int) nodeTag(jtnode));
1989}
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:4036

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 1687 of file rewriteHandler.c.

1692{
1693 RuleLock *rulelocks = relation->rd_rules;
1695 int nlocks;
1696 int i;
1697
1698 if (rulelocks == NULL)
1699 return NIL;
1700
1701 if (parsetree->commandType != CMD_SELECT)
1702 {
1703 if (parsetree->resultRelation != varno)
1704 return NIL;
1705 }
1706
1707 nlocks = rulelocks->numLocks;
1708
1709 for (i = 0; i < nlocks; i++)
1710 {
1711 RewriteRule *oneLock = rulelocks->rules[i];
1712
1713 if (oneLock->event == CMD_UPDATE)
1714 *hasUpdate = true;
1715
1716 /*
1717 * Suppress ON INSERT/UPDATE/DELETE rules that are disabled or
1718 * configured to not fire during the current session's replication
1719 * role. ON SELECT rules will always be applied in order to keep views
1720 * working even in LOCAL or REPLICA role.
1721 */
1722 if (oneLock->event != CMD_SELECT)
1723 {
1725 {
1726 if (oneLock->enabled == RULE_FIRES_ON_ORIGIN ||
1727 oneLock->enabled == RULE_DISABLED)
1728 continue;
1729 }
1730 else /* ORIGIN or LOCAL ROLE */
1731 {
1732 if (oneLock->enabled == RULE_FIRES_ON_REPLICA ||
1733 oneLock->enabled == RULE_DISABLED)
1734 continue;
1735 }
1736
1737 /* Non-SELECT rules are not supported for MERGE */
1738 if (parsetree->commandType == CMD_MERGE)
1739 ereport(ERROR,
1741 errmsg("cannot execute MERGE on relation \"%s\"",
1742 RelationGetRelationName(relation)),
1743 errdetail("MERGE is not supported for relations with rules."));
1744 }
1745
1746 if (oneLock->event == event)
1747 {
1748 if (parsetree->commandType != CMD_SELECT ||
1749 rangeTableEntry_used((Node *) parsetree, varno, 0))
1751 }
1752 }
1753
1754 return matching_locks;
1755}
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define RULE_FIRES_ON_ORIGIN
#define RULE_FIRES_ON_REPLICA
#define RULE_DISABLED
int SessionReplicationRole
Definition trigger.c:65
#define SESSION_REPLICATION_ROLE_REPLICA
Definition trigger.h:143

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

Referenced by RewriteQuery(), and rewriteValuesRTE().

◆ process_matched_tle()

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

Definition at line 1097 of file rewriteHandler.c.

1100{
1103 Node *src_expr;
1105 Node *src_input;
1108 Node *newexpr;
1109
1110 if (prior_tle == NULL)
1111 {
1112 /*
1113 * Normal case where this is the first assignment to the attribute.
1114 */
1115 return src_tle;
1116 }
1117
1118 /*----------
1119 * Multiple assignments to same attribute. Allow only if all are
1120 * FieldStore or SubscriptingRef assignment operations. This is a bit
1121 * tricky because what we may actually be looking at is a nest of
1122 * such nodes; consider
1123 * UPDATE tab SET col.fld1.subfld1 = x, col.fld2.subfld2 = y
1124 * The two expressions produced by the parser will look like
1125 * FieldStore(col, fld1, FieldStore(placeholder, subfld1, x))
1126 * FieldStore(col, fld2, FieldStore(placeholder, subfld2, y))
1127 * However, we can ignore the substructure and just consider the top
1128 * FieldStore or SubscriptingRef from each assignment, because it works to
1129 * combine these as
1130 * FieldStore(FieldStore(col, fld1,
1131 * FieldStore(placeholder, subfld1, x)),
1132 * fld2, FieldStore(placeholder, subfld2, y))
1133 * Note the leftmost expression goes on the inside so that the
1134 * assignments appear to occur left-to-right.
1135 *
1136 * For FieldStore, instead of nesting we can generate a single
1137 * FieldStore with multiple target fields. We must nest when
1138 * SubscriptingRefs are involved though.
1139 *
1140 * As a further complication, the destination column might be a domain,
1141 * resulting in each assignment containing a CoerceToDomain node over a
1142 * FieldStore or SubscriptingRef. These should have matching target
1143 * domains, so we strip them and reconstitute a single CoerceToDomain over
1144 * the combined FieldStore/SubscriptingRef nodes. (Notice that this has
1145 * the result that the domain's checks are applied only after we do all
1146 * the field or element updates, not after each one. This is desirable.)
1147 *----------
1148 */
1149 src_expr = (Node *) src_tle->expr;
1150 prior_expr = (Node *) prior_tle->expr;
1151
1154 ((CoerceToDomain *) src_expr)->resulttype ==
1155 ((CoerceToDomain *) prior_expr)->resulttype)
1156 {
1157 /* we assume without checking that resulttypmod/resultcollid match */
1161 }
1162
1165 if (src_input == NULL ||
1166 prior_input == NULL ||
1168 ereport(ERROR,
1170 errmsg("multiple assignments to same column \"%s\"",
1171 attrName)));
1172
1173 /*
1174 * Prior TLE could be a nest of assignments if we do this more than once.
1175 */
1177 for (;;)
1178 {
1180
1181 if (newbottom == NULL)
1182 break; /* found the original Var reference */
1184 }
1186 ereport(ERROR,
1188 errmsg("multiple assignments to same column \"%s\"",
1189 attrName)));
1190
1191 /*
1192 * Looks OK to nest 'em.
1193 */
1194 if (IsA(src_expr, FieldStore))
1195 {
1196 FieldStore *fstore = makeNode(FieldStore);
1197
1199 {
1200 /* combine the two */
1201 memcpy(fstore, prior_expr, sizeof(FieldStore));
1202 fstore->newvals =
1203 list_concat_copy(((FieldStore *) prior_expr)->newvals,
1204 ((FieldStore *) src_expr)->newvals);
1205 fstore->fieldnums =
1208 }
1209 else
1210 {
1211 /* general case, just nest 'em */
1212 memcpy(fstore, src_expr, sizeof(FieldStore));
1213 fstore->arg = (Expr *) prior_expr;
1214 }
1215 newexpr = (Node *) fstore;
1216 }
1217 else if (IsA(src_expr, SubscriptingRef))
1218 {
1220
1221 memcpy(sbsref, src_expr, sizeof(SubscriptingRef));
1222 sbsref->refexpr = (Expr *) prior_expr;
1223 newexpr = (Node *) sbsref;
1224 }
1225 else
1226 {
1227 elog(ERROR, "cannot happen");
1228 newexpr = NULL;
1229 }
1230
1231 if (coerce_expr)
1232 {
1233 /* put back the CoerceToDomain */
1235
1237 newcoerce->arg = (Expr *) newexpr;
1238 newexpr = (Node *) newcoerce;
1239 }
1240
1242 result->expr = (Expr *) newexpr;
1243 return result;
1244}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
Datum arg
Definition elog.c:1323
bool equal(const void *a, const void *b)
Definition equalfuncs.c:223
List * list_concat_copy(const List *list1, const List *list2)
Definition list.c:598
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition makefuncs.c:322
static Node * get_assignment_input(Node *node)
List * newvals
Definition primnodes.h:1195

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

Referenced by rewriteTargetListIU().

◆ QueryRewrite()

List * QueryRewrite ( Query parsetree)

Definition at line 4734 of file rewriteHandler.c.

4735{
4736 int64 input_query_id = parsetree->queryId;
4737 List *querylist;
4738 List *results;
4739 ListCell *l;
4741 bool foundOriginalQuery;
4743
4744 /*
4745 * This function is only applied to top-level original queries
4746 */
4747 Assert(parsetree->querySource == QSRC_ORIGINAL);
4748 Assert(parsetree->canSetTag);
4749
4750 /*
4751 * Step 1
4752 *
4753 * Apply all non-SELECT rules possibly getting 0 or many queries
4754 */
4755 querylist = RewriteQuery(parsetree, NIL, 0, 0);
4756
4757 /*
4758 * Step 2
4759 *
4760 * Apply all the RIR rules on each query
4761 *
4762 * This is also a handy place to mark each query with the original queryId
4763 */
4764 results = NIL;
4765 foreach(l, querylist)
4766 {
4767 Query *query = (Query *) lfirst(l);
4768
4769 query = fireRIRrules(query, NIL);
4770
4771 query->queryId = input_query_id;
4772
4773 results = lappend(results, query);
4774 }
4775
4776 /*
4777 * Step 3
4778 *
4779 * Determine which, if any, of the resulting queries is supposed to set
4780 * the command-result tag; and update the canSetTag fields accordingly.
4781 *
4782 * If the original query is still in the list, it sets the command tag.
4783 * Otherwise, the last INSTEAD query of the same kind as the original is
4784 * allowed to set the tag. (Note these rules can leave us with no query
4785 * setting the tag. The tcop code has to cope with this by setting up a
4786 * default tag based on the original un-rewritten query.)
4787 *
4788 * The Asserts verify that at most one query in the result list is marked
4789 * canSetTag. If we aren't checking asserts, we can fall out of the loop
4790 * as soon as we find the original query.
4791 */
4792 origCmdType = parsetree->commandType;
4793 foundOriginalQuery = false;
4794 lastInstead = NULL;
4795
4796 foreach(l, results)
4797 {
4798 Query *query = (Query *) lfirst(l);
4799
4800 if (query->querySource == QSRC_ORIGINAL)
4801 {
4802 Assert(query->canSetTag);
4804 foundOriginalQuery = true;
4805#ifndef USE_ASSERT_CHECKING
4806 break;
4807#endif
4808 }
4809 else
4810 {
4811 Assert(!query->canSetTag);
4812 if (query->commandType == origCmdType &&
4813 (query->querySource == QSRC_INSTEAD_RULE ||
4814 query->querySource == QSRC_QUAL_INSTEAD_RULE))
4815 lastInstead = query;
4816 }
4817 }
4818
4820 lastInstead->canSetTag = true;
4821
4822 return results;
4823}
int64_t int64
Definition c.h:621
CmdType
Definition nodes.h:273
@ QSRC_ORIGINAL
Definition parsenodes.h:36
static List * RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length, int num_ctes_processed)

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

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

◆ relation_is_updatable()

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

Definition at line 2957 of file rewriteHandler.c.

2961{
2962 int events = 0;
2963 Relation rel;
2965
2966#define ALL_EVENTS ((1 << CMD_INSERT) | (1 << CMD_UPDATE) | (1 << CMD_DELETE))
2967
2968 /* Since this function recurses, it could be driven to stack overflow */
2970
2971 rel = try_relation_open(reloid, AccessShareLock);
2972
2973 /*
2974 * If the relation doesn't exist, return zero rather than throwing an
2975 * error. This is helpful since scanning an information_schema view under
2976 * MVCC rules can result in referencing rels that have actually been
2977 * deleted already.
2978 */
2979 if (rel == NULL)
2980 return 0;
2981
2982 /* If we detect a recursive view, report that it is not updatable */
2984 {
2986 return 0;
2987 }
2988
2989 /* If the relation is a table, it is always updatable */
2990 if (rel->rd_rel->relkind == RELKIND_RELATION ||
2991 rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2992 {
2994 return ALL_EVENTS;
2995 }
2996
2997 /* Look for unconditional DO INSTEAD rules, and note supported events */
2998 rulelocks = rel->rd_rules;
2999 if (rulelocks != NULL)
3000 {
3001 int i;
3002
3003 for (i = 0; i < rulelocks->numLocks; i++)
3004 {
3005 if (rulelocks->rules[i]->isInstead &&
3006 rulelocks->rules[i]->qual == NULL)
3007 {
3008 events |= ((1 << rulelocks->rules[i]->event) & ALL_EVENTS);
3009 }
3010 }
3011
3012 /* If we have rules for all events, we're done */
3013 if (events == ALL_EVENTS)
3014 {
3016 return events;
3017 }
3018 }
3019
3020 /* Similarly look for INSTEAD OF triggers, if they are to be included */
3021 if (include_triggers)
3022 {
3024
3025 if (trigDesc)
3026 {
3027 if (trigDesc->trig_insert_instead_row)
3028 events |= (1 << CMD_INSERT);
3029 if (trigDesc->trig_update_instead_row)
3030 events |= (1 << CMD_UPDATE);
3031 if (trigDesc->trig_delete_instead_row)
3032 events |= (1 << CMD_DELETE);
3033
3034 /* If we have triggers for all events, we're done */
3035 if (events == ALL_EVENTS)
3036 {
3038 return events;
3039 }
3040 }
3041 }
3042
3043 /* If this is a foreign table, check which update events it supports */
3044 if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
3045 {
3046 FdwRoutine *fdwroutine = GetFdwRoutineForRelation(rel, false);
3047
3048 if (fdwroutine->IsForeignRelUpdatable != NULL)
3049 events |= fdwroutine->IsForeignRelUpdatable(rel);
3050 else
3051 {
3052 /* Assume presence of executor functions is sufficient */
3053 if (fdwroutine->ExecForeignInsert != NULL)
3054 events |= (1 << CMD_INSERT);
3055 if (fdwroutine->ExecForeignUpdate != NULL)
3056 events |= (1 << CMD_UPDATE);
3057 if (fdwroutine->ExecForeignDelete != NULL)
3058 events |= (1 << CMD_DELETE);
3059 }
3060
3062 return events;
3063 }
3064
3065 /* Check if this is an automatically updatable view */
3066 if (rel->rd_rel->relkind == RELKIND_VIEW)
3067 {
3069
3071 {
3073 int auto_events;
3076 Oid baseoid;
3077
3078 /*
3079 * Determine which of the view's columns are updatable. If there
3080 * are none within the set of columns we are looking at, then the
3081 * view doesn't support INSERT/UPDATE, but it may still support
3082 * DELETE.
3083 */
3086
3087 if (include_cols != NULL)
3089
3091 auto_events = (1 << CMD_DELETE); /* May support DELETE */
3092 else
3093 auto_events = ALL_EVENTS; /* May support all events */
3094
3095 /*
3096 * The base relation must also support these update commands.
3097 * Tables are always updatable, but for any other kind of base
3098 * relation we must do a recursive check limited to the columns
3099 * referenced by the locally updatable columns in this view.
3100 */
3101 rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
3102 base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
3103 Assert(base_rte->rtekind == RTE_RELATION);
3104
3105 if (base_rte->relkind != RELKIND_RELATION &&
3107 {
3108 baseoid = base_rte->relid;
3110 RelationGetRelid(rel));
3112 viewquery->targetList);
3116 include_cols);
3118 }
3119 events |= auto_events;
3120 }
3121 }
3122
3123 /* If we reach here, the relation may support some update commands */
3125 return events;
3126}
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1093
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition foreign.c:474
static const char * view_cols_are_auto_updatable(Query *viewquery, Bitmapset *required_cols, Bitmapset **updatable_cols, char **non_updatable_col)
int relation_is_updatable(Oid reloid, List *outer_reloids, bool include_triggers, Bitmapset *include_cols)
Query * get_view_query(Relation view)
static Bitmapset * adjust_view_column_set(Bitmapset *cols, List *targetlist)
const char * view_query_is_auto_updatable(Query *viewquery, bool check_cols)
#define ALL_EVENTS
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:89
void check_stack_depth(void)
Definition stack_depth.c:95
ExecForeignInsert_function ExecForeignInsert
Definition fdwapi.h:236
ExecForeignUpdate_function ExecForeignUpdate
Definition fdwapi.h:239
ExecForeignDelete_function ExecForeignDelete
Definition fdwapi.h:240
IsForeignRelUpdatable_function IsForeignRelUpdatable
Definition fdwapi.h:244

References AccessShareLock, adjust_view_column_set(), ALL_EVENTS, Assert, bms_int_members(), bms_is_empty, check_stack_depth(), CMD_DELETE, CMD_INSERT, CMD_UPDATE, FdwRoutine::ExecForeignDelete, FdwRoutine::ExecForeignInsert, FdwRoutine::ExecForeignUpdate, fb(), get_view_query(), GetFdwRoutineForRelation(), i, FdwRoutine::IsForeignRelUpdatable, lappend_oid(), linitial, list_delete_last(), list_member_oid(), RelationData::rd_rel, RelationData::rd_rules, relation_close(), relation_is_updatable(), RelationGetRelid, rt_fetch, RTE_RELATION, RelationData::trigdesc, try_relation_open(), view_cols_are_auto_updatable(), and view_query_is_auto_updatable().

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

◆ RewriteQuery()

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

Definition at line 3997 of file rewriteHandler.c.

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

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

Referenced by QueryRewrite(), and RewriteQuery().

◆ rewriteRuleAction()

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

Definition at line 352 of file rewriteHandler.c.

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

References acquireLocksOnSubLinks(), AcquireRewriteLocks(), AddQual(), adjustJoinTreeList(), Assert, ChangeVarNodes(), checkExprHasSubLink(), CMD_INSERT, CMD_UPDATE, CMD_UTILITY, CombineRangeTables(), contain_vars_of_level(), copyObject, Query::cteList, CommonTableExpr::ctename, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), acquireLocksOnSubLinks_context::for_execute, get_generated_columns(), getInsertSelectQuery(), Query::jointree, lfirst, list_concat(), list_length(), NIL, NoLock, OffsetVarNodes(), ONCONFLICT_SELECT, PRS2_NEW_VARNO, PRS2_OLD_VARNO, FromExpr::quals, rangeTableEntry_used(), relation_close(), relation_open(), 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 823 of file rewriteHandler.c.

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

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

Referenced by RewriteQuery().

◆ rewriteTargetView()

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

Definition at line 3307 of file rewriteHandler.c.

3308{
3310 bool insert_or_update;
3311 const char *auto_update_detail;
3313 int base_rt_index;
3314 int new_rt_index;
3323 ListCell *lc;
3324
3325 /*
3326 * Get the Query from the view's ON SELECT rule. We're going to munge the
3327 * Query to change the view's base relation into the target relation,
3328 * along with various other changes along the way, so we need to make a
3329 * copy of it (get_view_query() returns a pointer into the relcache, so we
3330 * have to treat it as read-only).
3331 */
3333
3334 /* Locate RTE and perminfo describing the view in the outer query */
3335 view_rte = rt_fetch(parsetree->resultRelation, parsetree->rtable);
3336 view_perminfo = getRTEPermissionInfo(parsetree->rteperminfos, view_rte);
3337
3338 /*
3339 * Are we doing INSERT/UPDATE, or MERGE containing INSERT/UPDATE? If so,
3340 * various additional checks on the view columns need to be applied, and
3341 * any view CHECK OPTIONs need to be enforced.
3342 */
3344 (parsetree->commandType == CMD_INSERT ||
3345 parsetree->commandType == CMD_UPDATE);
3346
3347 if (parsetree->commandType == CMD_MERGE)
3348 {
3349 foreach_node(MergeAction, action, parsetree->mergeActionList)
3350 {
3351 if (action->commandType == CMD_INSERT ||
3352 action->commandType == CMD_UPDATE)
3353 {
3354 insert_or_update = true;
3355 break;
3356 }
3357 }
3358 }
3359
3360 /* Check if the expansion of non-system views are restricted */
3363 ereport(ERROR,
3365 errmsg("access to non-system view \"%s\" is restricted",
3366 RelationGetRelationName(view))));
3367
3368 /*
3369 * The view must be updatable, else fail.
3370 *
3371 * If we are doing INSERT/UPDATE (or MERGE containing INSERT/UPDATE), we
3372 * also check that there is at least one updatable column.
3373 */
3376
3379 parsetree->commandType,
3380 parsetree->mergeActionList,
3382
3383 /*
3384 * For INSERT/UPDATE (or MERGE containing INSERT/UPDATE) the modified
3385 * columns must all be updatable.
3386 */
3387 if (insert_or_update)
3388 {
3390 char *non_updatable_col;
3391
3392 /*
3393 * Compute the set of modified columns as those listed in the result
3394 * RTE's insertedCols and/or updatedCols sets plus those that are
3395 * targets of the query's targetlist(s). We must consider the query's
3396 * targetlist because rewriteTargetListIU may have added additional
3397 * targetlist entries for view defaults, and these must also be
3398 * updatable. But rewriteTargetListIU can also remove entries if they
3399 * are DEFAULT markers and the column's default is NULL, so
3400 * considering only the targetlist would also be wrong.
3401 */
3402 modified_cols = bms_union(view_perminfo->insertedCols,
3403 view_perminfo->updatedCols);
3404
3405 foreach(lc, parsetree->targetList)
3406 {
3408
3409 if (!tle->resjunk)
3412 }
3413
3414 if (parsetree->onConflict)
3415 {
3416 foreach(lc, parsetree->onConflict->onConflictSet)
3417 {
3419
3420 if (!tle->resjunk)
3423 }
3424 }
3425
3426 foreach_node(MergeAction, action, parsetree->mergeActionList)
3427 {
3428 if (action->commandType == CMD_INSERT ||
3429 action->commandType == CMD_UPDATE)
3430 {
3431 foreach_node(TargetEntry, tle, action->targetList)
3432 {
3433 if (!tle->resjunk)
3436 }
3437 }
3438 }
3439
3442 NULL,
3445 {
3446 /*
3447 * This is a different error, caused by an attempt to update a
3448 * non-updatable column in an otherwise updatable view.
3449 */
3450 switch (parsetree->commandType)
3451 {
3452 case CMD_INSERT:
3453 ereport(ERROR,
3455 errmsg("cannot insert into column \"%s\" of view \"%s\"",
3459 break;
3460 case CMD_UPDATE:
3461 ereport(ERROR,
3463 errmsg("cannot update column \"%s\" of view \"%s\"",
3467 break;
3468 case CMD_MERGE:
3469 ereport(ERROR,
3471 errmsg("cannot merge into column \"%s\" of view \"%s\"",
3475 break;
3476 default:
3477 elog(ERROR, "unrecognized CmdType: %d",
3478 (int) parsetree->commandType);
3479 break;
3480 }
3481 }
3482 }
3483
3484 /*
3485 * For MERGE, there must not be any INSTEAD OF triggers on an otherwise
3486 * updatable view. The caller already checked that there isn't a full set
3487 * of INSTEAD OF triggers, so this is to guard against having a partial
3488 * set (mixing auto-update and trigger-update actions in a single command
3489 * isn't supported).
3490 */
3491 if (parsetree->commandType == CMD_MERGE)
3492 {
3493 foreach_node(MergeAction, action, parsetree->mergeActionList)
3494 {
3495 if (action->commandType != CMD_NOTHING &&
3496 view_has_instead_trigger(view, action->commandType, NIL))
3497 ereport(ERROR,
3499 errmsg("cannot merge into view \"%s\"",
3501 errdetail("MERGE is not supported for views with INSTEAD OF triggers for some actions but not all."),
3502 errhint("To enable merging into the view, either provide a full set of INSTEAD OF triggers or drop the existing INSTEAD OF triggers."));
3503 }
3504 }
3505
3506 /*
3507 * If we get here, view_query_is_auto_updatable() has verified that the
3508 * view contains a single base relation.
3509 */
3510 Assert(list_length(viewquery->jointree->fromlist) == 1);
3511 rtr = linitial_node(RangeTblRef, viewquery->jointree->fromlist);
3512
3513 base_rt_index = rtr->rtindex;
3515 Assert(base_rte->rtekind == RTE_RELATION);
3517
3518 /*
3519 * Up to now, the base relation hasn't been touched at all in our query.
3520 * We need to acquire lock on it before we try to do anything with it.
3521 * (The subsequent recursive call of RewriteQuery will suppose that we
3522 * already have the right lock!) Since it will become the query target
3523 * relation, RowExclusiveLock is always the right thing.
3524 */
3526
3527 /*
3528 * While we have the relation open, update the RTE's relkind, just in case
3529 * it changed since this view was made (cf. AcquireRewriteLocks).
3530 */
3531 base_rte->relkind = base_rel->rd_rel->relkind;
3532
3533 /*
3534 * If the view query contains any sublink subqueries then we need to also
3535 * acquire locks on any relations they refer to. We know that there won't
3536 * be any subqueries in the range table or CTEs, so we can skip those, as
3537 * in AcquireRewriteLocks.
3538 */
3539 if (viewquery->hasSubLinks)
3540 {
3542
3543 context.for_execute = true;
3546 }
3547
3548 /*
3549 * Create a new target RTE describing the base relation, and add it to the
3550 * outer query's rangetable. (What's happening in the next few steps is
3551 * very much like what the planner would do to "pull up" the view into the
3552 * outer query. Perhaps someday we should refactor things enough so that
3553 * we can share code with the planner.)
3554 *
3555 * Be sure to set rellockmode to the correct thing for the target table.
3556 * Since we copied the whole viewquery above, we can just scribble on
3557 * base_rte instead of copying it.
3558 */
3559 new_rte = base_rte;
3560 new_rte->rellockmode = RowExclusiveLock;
3561
3562 parsetree->rtable = lappend(parsetree->rtable, new_rte);
3563 new_rt_index = list_length(parsetree->rtable);
3564
3565 /*
3566 * INSERTs never inherit. For UPDATE/DELETE/MERGE, we use the view
3567 * query's inheritance flag for the base relation.
3568 */
3569 if (parsetree->commandType == CMD_INSERT)
3570 new_rte->inh = false;
3571
3572 /*
3573 * Adjust the view's targetlist Vars to reference the new target RTE, ie
3574 * make their varnos be new_rt_index instead of base_rt_index. There can
3575 * be no Vars for other rels in the tlist, so this is sufficient to pull
3576 * up the tlist expressions for use in the outer query. The tlist will
3577 * provide the replacement expressions used by ReplaceVarsFromTargetList
3578 * below.
3579 */
3580 view_targetlist = viewquery->targetList;
3581
3585 0);
3586
3587 /*
3588 * If the view has "security_invoker" set, mark the new target relation
3589 * for the permissions checks that we want to enforce against the query
3590 * caller. Otherwise we want to enforce them against the view owner.
3591 *
3592 * At the relation level, require the same INSERT/UPDATE/DELETE
3593 * permissions that the query caller needs against the view. We drop the
3594 * ACL_SELECT bit that is presumably in new_perminfo->requiredPerms
3595 * initially.
3596 *
3597 * Note: the original view's RTEPermissionInfo remains in the query's
3598 * rteperminfos so that the executor still performs appropriate
3599 * permissions checks for the query caller's use of the view.
3600 *
3601 * Disregard the perminfo in viewquery->rteperminfos that the base_rte
3602 * would currently be pointing at, because we'd like it to point now to a
3603 * new one that will be filled below. Must set perminfoindex to 0 to not
3604 * trip over the Assert in addRTEPermissionInfo().
3605 */
3606 new_rte->perminfoindex = 0;
3607 new_perminfo = addRTEPermissionInfo(&parsetree->rteperminfos, new_rte);
3609 new_perminfo->checkAsUser = InvalidOid;
3610 else
3611 new_perminfo->checkAsUser = view->rd_rel->relowner;
3612 new_perminfo->requiredPerms = view_perminfo->requiredPerms;
3613
3614 /*
3615 * Now for the per-column permissions bits.
3616 *
3617 * Initially, new_perminfo (base_perminfo) contains selectedCols
3618 * permission check bits for all base-rel columns referenced by the view,
3619 * but since the view is a SELECT query its insertedCols/updatedCols is
3620 * empty. We set insertedCols and updatedCols to include all the columns
3621 * the outer query is trying to modify, adjusting the column numbers as
3622 * needed. But we leave selectedCols as-is, so the view owner must have
3623 * read permission for all columns used in the view definition, even if
3624 * some of them are not read by the outer query. We could try to limit
3625 * selectedCols to only columns used in the transformed query, but that
3626 * does not correspond to what happens in ordinary SELECT usage of a view:
3627 * all referenced columns must have read permission, even if optimization
3628 * finds that some of them can be discarded during query transformation.
3629 * The flattening we're doing here is an optional optimization, too. (If
3630 * you are unpersuaded and want to change this, note that applying
3631 * adjust_view_column_set to view_perminfo->selectedCols is clearly *not*
3632 * the right answer, since that neglects base-rel columns used in the
3633 * view's WHERE quals.)
3634 *
3635 * This step needs the modified view targetlist, so we have to do things
3636 * in this order.
3637 */
3638 Assert(bms_is_empty(new_perminfo->insertedCols) &&
3639 bms_is_empty(new_perminfo->updatedCols));
3640
3641 new_perminfo->selectedCols = base_perminfo->selectedCols;
3642
3643 new_perminfo->insertedCols =
3645
3646 new_perminfo->updatedCols =
3648
3649 /*
3650 * Move any security barrier quals from the view RTE onto the new target
3651 * RTE. Any such quals should now apply to the new target RTE and will
3652 * not reference the original view RTE in the rewritten query.
3653 */
3654 new_rte->securityQuals = view_rte->securityQuals;
3655 view_rte->securityQuals = NIL;
3656
3657 /*
3658 * Now update all Vars in the outer query that reference the view to
3659 * reference the appropriate column of the base relation instead.
3660 */
3661 parsetree = (Query *)
3662 ReplaceVarsFromTargetList((Node *) parsetree,
3663 parsetree->resultRelation,
3664 0,
3665 view_rte,
3669 0,
3670 NULL);
3671
3672 /*
3673 * Update all other RTI references in the query that point to the view
3674 * (for example, parsetree->resultRelation itself) to point to the new
3675 * base relation instead. Vars will not be affected since none of them
3676 * reference parsetree->resultRelation any longer.
3677 */
3678 ChangeVarNodes((Node *) parsetree,
3679 parsetree->resultRelation,
3681 0);
3682 Assert(parsetree->resultRelation == new_rt_index);
3683
3684 /*
3685 * For INSERT/UPDATE we must also update resnos in the targetlist to refer
3686 * to columns of the base relation, since those indicate the target
3687 * columns to be affected. Similarly, for MERGE we must update the resnos
3688 * in the merge action targetlists of any INSERT/UPDATE actions.
3689 *
3690 * Note that this destroys the resno ordering of the targetlists, but that
3691 * will be fixed when we recurse through RewriteQuery, which will invoke
3692 * rewriteTargetListIU again on the updated targetlists.
3693 */
3694 if (parsetree->commandType != CMD_DELETE)
3695 {
3696 foreach(lc, parsetree->targetList)
3697 {
3700
3701 if (tle->resjunk)
3702 continue;
3703
3705 if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3706 tle->resno = ((Var *) view_tle->expr)->varattno;
3707 else
3708 elog(ERROR, "attribute number %d not found in view targetlist",
3709 tle->resno);
3710 }
3711
3712 foreach_node(MergeAction, action, parsetree->mergeActionList)
3713 {
3714 if (action->commandType == CMD_INSERT ||
3715 action->commandType == CMD_UPDATE)
3716 {
3717 foreach_node(TargetEntry, tle, action->targetList)
3718 {
3720
3721 if (tle->resjunk)
3722 continue;
3723
3725 if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3726 tle->resno = ((Var *) view_tle->expr)->varattno;
3727 else
3728 elog(ERROR, "attribute number %d not found in view targetlist",
3729 tle->resno);
3730 }
3731 }
3732 }
3733 }
3734
3735 /*
3736 * For INSERT .. ON CONFLICT .. DO SELECT/UPDATE, we must also update
3737 * assorted stuff in the onConflict data structure.
3738 */
3739 if (parsetree->onConflict &&
3740 (parsetree->onConflict->action == ONCONFLICT_UPDATE ||
3741 parsetree->onConflict->action == ONCONFLICT_SELECT))
3742 {
3747 List *tmp_tlist;
3748
3749 /*
3750 * For ON CONFLICT DO UPDATE, update the resnos in the auxiliary
3751 * UPDATE targetlist to refer to columns of the base relation.
3752 */
3753 foreach(lc, parsetree->onConflict->onConflictSet)
3754 {
3757
3758 if (tle->resjunk)
3759 continue;
3760
3762 if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3763 tle->resno = ((Var *) view_tle->expr)->varattno;
3764 else
3765 elog(ERROR, "attribute number %d not found in view targetlist",
3766 tle->resno);
3767 }
3768
3769 /*
3770 * Create a new RTE for the EXCLUDED pseudo-relation, using the
3771 * query's new base rel (which may well have a different column list
3772 * from the view, hence we need a new column alias list). This should
3773 * match transformOnConflictClause. In particular, note that the
3774 * relkind is set to composite to signal that we're not dealing with
3775 * an actual relation.
3776 */
3778
3780 base_rel,
3782 makeAlias("excluded", NIL),
3783 false, false);
3784 new_exclRte = new_exclNSItem->p_rte;
3786 /* Ignore the RTEPermissionInfo that would've been added. */
3787 new_exclRte->perminfoindex = 0;
3788
3789 parsetree->rtable = lappend(parsetree->rtable, new_exclRte);
3791 list_length(parsetree->rtable);
3792
3793 /*
3794 * Replace the targetlist for the EXCLUDED pseudo-relation with a new
3795 * one, representing the columns from the new base relation.
3796 */
3797 parsetree->onConflict->exclRelTlist =
3799
3800 /*
3801 * Update all Vars in the ON CONFLICT clause that refer to the old
3802 * EXCLUDED pseudo-relation. We want to use the column mappings
3803 * defined in the view targetlist, but we need the outputs to refer to
3804 * the new EXCLUDED pseudo-relation rather than the new target RTE.
3805 * Also notice that "EXCLUDED.*" will be expanded using the view's
3806 * rowtype, which seems correct.
3807 */
3809
3811 new_exclRelIndex, 0);
3812
3813 parsetree->onConflict = (OnConflictExpr *)
3816 0,
3817 view_rte,
3818 tmp_tlist,
3821 0,
3822 &parsetree->hasSubLinks);
3823 }
3824
3825 if (parsetree->forPortionOf && parsetree->commandType == CMD_UPDATE)
3826 {
3827 /*
3828 * Like the INSERT/UPDATE code above, update the resnos in the
3829 * auxiliary UPDATE targetlist to refer to columns of the base
3830 * relation.
3831 */
3832 foreach(lc, parsetree->forPortionOf->rangeTargetList)
3833 {
3836
3837 if (tle->resjunk)
3838 continue;
3839
3841 if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3842 tle->resno = ((Var *) view_tle->expr)->varattno;
3843 else
3844 elog(ERROR, "attribute number %d not found in view targetlist",
3845 tle->resno);
3846 }
3847 }
3848
3849 /*
3850 * For UPDATE/DELETE/MERGE, pull up any WHERE quals from the view. We
3851 * know that any Vars in the quals must reference the one base relation,
3852 * so we need only adjust their varnos to reference the new target (just
3853 * the same as we did with the view targetlist).
3854 *
3855 * If it's a security-barrier view, its WHERE quals must be applied before
3856 * quals from the outer query, so we attach them to the RTE as security
3857 * barrier quals rather than adding them to the main WHERE clause.
3858 *
3859 * For INSERT, the view's quals can be ignored in the main query.
3860 */
3861 if (parsetree->commandType != CMD_INSERT &&
3862 viewquery->jointree->quals != NULL)
3863 {
3864 Node *viewqual = (Node *) viewquery->jointree->quals;
3865
3866 /*
3867 * Even though we copied viewquery already at the top of this
3868 * function, we must duplicate the viewqual again here, because we may
3869 * need to use the quals again below for a WithCheckOption clause.
3870 */
3872
3874
3875 if (RelationIsSecurityView(view))
3876 {
3877 /*
3878 * The view's quals go in front of existing barrier quals: those
3879 * would have come from an outer level of security-barrier view,
3880 * and so must get evaluated later.
3881 *
3882 * Note: the parsetree has been mutated, so the new_rte pointer is
3883 * stale and needs to be re-computed.
3884 */
3885 new_rte = rt_fetch(new_rt_index, parsetree->rtable);
3886 new_rte->securityQuals = lcons(viewqual, new_rte->securityQuals);
3887
3888 /*
3889 * Do not set parsetree->hasRowSecurity, because these aren't RLS
3890 * conditions (they aren't affected by enabling/disabling RLS).
3891 */
3892
3893 /*
3894 * Make sure that the query is marked correctly if the added qual
3895 * has sublinks.
3896 */
3897 if (!parsetree->hasSubLinks)
3898 parsetree->hasSubLinks = checkExprHasSubLink(viewqual);
3899 }
3900 else
3901 AddQual(parsetree, viewqual);
3902 }
3903
3904 /*
3905 * For INSERT/UPDATE (or MERGE containing INSERT/UPDATE), if the view has
3906 * the WITH CHECK OPTION, or any parent view specified WITH CASCADED CHECK
3907 * OPTION, add the quals from the view to the query's withCheckOptions
3908 * list.
3909 */
3910 if (insert_or_update)
3911 {
3912 bool has_wco = RelationHasCheckOption(view);
3913 bool cascaded = RelationHasCascadedCheckOption(view);
3914
3915 /*
3916 * If the parent view has a cascaded check option, treat this view as
3917 * if it also had a cascaded check option.
3918 *
3919 * New WithCheckOptions are added to the start of the list, so if
3920 * there is a cascaded check option, it will be the first item in the
3921 * list.
3922 */
3923 if (parsetree->withCheckOptions != NIL)
3924 {
3926 (WithCheckOption *) linitial(parsetree->withCheckOptions);
3927
3928 if (parent_wco->cascaded)
3929 {
3930 has_wco = true;
3931 cascaded = true;
3932 }
3933 }
3934
3935 /*
3936 * Add the new WithCheckOption to the start of the list, so that
3937 * checks on inner views are run before checks on outer views, as
3938 * required by the SQL standard.
3939 *
3940 * If the new check is CASCADED, we need to add it even if this view
3941 * has no quals, since there may be quals on child views. A LOCAL
3942 * check can be omitted if this view has no quals.
3943 */
3944 if (has_wco && (cascaded || viewquery->jointree->quals != NULL))
3945 {
3947
3949 wco->kind = WCO_VIEW_CHECK;
3950 wco->relname = pstrdup(RelationGetRelationName(view));
3951 wco->polname = NULL;
3952 wco->qual = NULL;
3953 wco->cascaded = cascaded;
3954
3955 parsetree->withCheckOptions = lcons(wco,
3956 parsetree->withCheckOptions);
3957
3958 if (viewquery->jointree->quals != NULL)
3959 {
3960 wco->qual = (Node *) viewquery->jointree->quals;
3962
3963 /*
3964 * For INSERT, make sure that the query is marked correctly if
3965 * the added qual has sublinks. This can be skipped for
3966 * UPDATE/MERGE, since the same qual will have already been
3967 * added above, and the check will already have been done.
3968 */
3969 if (!parsetree->hasSubLinks &&
3970 parsetree->commandType == CMD_INSERT)
3971 parsetree->hasSubLinks = checkExprHasSubLink(wco->qual);
3972 }
3973 }
3974 }
3975
3977
3978 return parsetree;
3979}
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:1628
#define InvalidOid
#define RelationHasCheckOption(relation)
Definition rel.h:459
#define RelationHasSecurityInvoker(relation)
Definition rel.h:449
#define RelationHasCascadedCheckOption(relation)
Definition rel.h:481
List * exclRelTlist
Definition primnodes.h:2417

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

Referenced by RewriteQuery().

◆ rewriteValuesRTE()

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

Definition at line 1464 of file rewriteHandler.c.

1467{
1468 List *newValues;
1469 ListCell *lc;
1471 bool allReplaced;
1472 int numattrs;
1473 int *attrnos;
1474
1475 /* Steps below are not sensible for non-INSERT queries */
1476 Assert(parsetree->commandType == CMD_INSERT);
1477 Assert(rte->rtekind == RTE_VALUES);
1478
1479 /*
1480 * Rebuilding all the lists is a pretty expensive proposition in a big
1481 * VALUES list, and it's a waste of time if there aren't any DEFAULT
1482 * placeholders. So first scan to see if there are any.
1483 */
1484 if (!searchForDefault(rte))
1485 return true; /* nothing to do */
1486
1487 /*
1488 * Scan the targetlist for entries referring to the VALUES RTE, and note
1489 * the target attributes. As noted above, we should only need to do this
1490 * for targetlist entries containing simple Vars --- nothing else in the
1491 * VALUES RTE should contain DEFAULT items (except possibly for unused
1492 * columns), and we complain if such a thing does occur.
1493 */
1494 numattrs = list_length(linitial(rte->values_lists));
1495 attrnos = (int *) palloc0(numattrs * sizeof(int));
1496
1497 foreach(lc, parsetree->targetList)
1498 {
1500
1501 if (IsA(tle->expr, Var))
1502 {
1503 Var *var = (Var *) tle->expr;
1504
1505 if (var->varno == rti)
1506 {
1507 int attrno = var->varattno;
1508
1509 Assert(attrno >= 1 && attrno <= numattrs);
1510 attrnos[attrno - 1] = tle->resno;
1511 }
1512 }
1513 }
1514
1515 /*
1516 * Check if the target relation is an auto-updatable view, in which case
1517 * unresolved defaults will be left untouched rather than being set to
1518 * NULL.
1519 */
1520 isAutoUpdatableView = false;
1521 if (target_relation->rd_rel->relkind == RELKIND_VIEW &&
1523 {
1524 List *locks;
1525 bool hasUpdate;
1526 bool found;
1527 ListCell *l;
1528
1529 /* Look for an unconditional DO INSTEAD rule */
1531 parsetree->resultRelation, parsetree, &hasUpdate);
1532
1533 found = false;
1534 foreach(l, locks)
1535 {
1537
1538 if (rule_lock->isInstead &&
1539 rule_lock->qual == NULL)
1540 {
1541 found = true;
1542 break;
1543 }
1544 }
1545
1546 /*
1547 * If we didn't find an unconditional DO INSTEAD rule, assume that the
1548 * view is auto-updatable. If it isn't, rewriteTargetView() will
1549 * throw an error.
1550 */
1551 if (!found)
1552 isAutoUpdatableView = true;
1553 }
1554
1555 newValues = NIL;
1556 allReplaced = true;
1557 foreach(lc, rte->values_lists)
1558 {
1559 List *sublist = (List *) lfirst(lc);
1560 List *newList = NIL;
1561 ListCell *lc2;
1562 int i;
1563
1565
1566 i = 0;
1567 foreach(lc2, sublist)
1568 {
1569 Node *col = (Node *) lfirst(lc2);
1570 int attrno = attrnos[i++];
1571
1572 if (IsA(col, SetToDefault))
1573 {
1575 Node *new_expr;
1576
1577 /*
1578 * If this column isn't used, just replace the DEFAULT with
1579 * NULL (attrno will be 0 in this case because the targetlist
1580 * entry will have been replaced by the default expression).
1581 */
1583 {
1584 SetToDefault *def = (SetToDefault *) col;
1585
1587 makeNullConst(def->typeId,
1588 def->typeMod,
1589 def->collation));
1590 continue;
1591 }
1592
1593 if (attrno == 0)
1594 elog(ERROR, "cannot set value in column %d to DEFAULT", i);
1595 Assert(attrno > 0 && attrno <= target_relation->rd_att->natts);
1597
1598 if (!att_tup->attisdropped)
1600 else
1601 new_expr = NULL; /* force a NULL if dropped */
1602
1603 /*
1604 * If there is no default (ie, default is effectively NULL),
1605 * we've got to explicitly set the column to NULL, unless the
1606 * target relation is an auto-updatable view.
1607 */
1608 if (!new_expr)
1609 {
1611 {
1612 /* Leave the value untouched */
1614 allReplaced = false;
1615 continue;
1616 }
1617
1619 att_tup->atttypmod,
1620 att_tup->attcollation,
1621 att_tup->attlen,
1622 att_tup->attbyval);
1623 }
1625 }
1626 else
1628 }
1630 }
1631 rte->values_lists = newValues;
1632
1633 pfree(attrnos);
1634
1635 return allReplaced;
1636}
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 1649 of file rewriteHandler.c.

1650{
1651 List *newValues;
1652 ListCell *lc;
1653
1654 newValues = NIL;
1655 foreach(lc, rte->values_lists)
1656 {
1657 List *sublist = (List *) lfirst(lc);
1658 List *newList = NIL;
1659 ListCell *lc2;
1660
1661 foreach(lc2, sublist)
1662 {
1663 Node *col = (Node *) lfirst(lc2);
1664
1665 if (IsA(col, SetToDefault))
1666 {
1667 SetToDefault *def = (SetToDefault *) col;
1668
1670 def->typeMod,
1671 def->collation));
1672 }
1673 else
1675 }
1677 }
1678 rte->values_lists = newValues;
1679}

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

Referenced by RewriteQuery().

◆ searchForDefault()

static bool searchForDefault ( RangeTblEntry rte)
static

Definition at line 1350 of file rewriteHandler.c.

1351{
1352 ListCell *lc;
1353
1354 foreach(lc, rte->values_lists)
1355 {
1356 List *sublist = (List *) lfirst(lc);
1357 ListCell *lc2;
1358
1359 foreach(lc2, sublist)
1360 {
1361 Node *col = (Node *) lfirst(lc2);
1362
1363 if (IsA(col, SetToDefault))
1364 return true;
1365 }
1366 }
1367 return false;
1368}

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 2678 of file rewriteHandler.c.

2679{
2680 Var *var = (Var *) tle->expr;
2681
2682 /*
2683 * For now, the only updatable columns we support are those that are Vars
2684 * referring to user columns of the underlying base relation.
2685 *
2686 * The view targetlist may contain resjunk columns (e.g., a view defined
2687 * like "SELECT * FROM t ORDER BY a+b" is auto-updatable) but such columns
2688 * are not auto-updatable, and in fact should never appear in the outer
2689 * query's targetlist.
2690 */
2691 if (tle->resjunk)
2692 return gettext_noop("Junk view columns are not updatable.");
2693
2694 if (!IsA(var, Var) ||
2695 var->varno != rtr->rtindex ||
2696 var->varlevelsup != 0)
2697 return gettext_noop("View columns that are not columns of their base relation are not updatable.");
2698
2699 if (var->varattno < 0)
2700 return gettext_noop("View columns that refer to system columns are not updatable.");
2701
2702 if (var->varattno == 0)
2703 return gettext_noop("View columns that return whole-row references are not updatable.");
2704
2705 return NULL; /* the view column is updatable */
2706}

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 2874 of file rewriteHandler.c.

2878{
2881 ListCell *cell;
2882
2883 /*
2884 * The caller should have verified that this view is auto-updatable and so
2885 * there should be a single base relation.
2886 */
2887 Assert(list_length(viewquery->jointree->fromlist) == 1);
2888 rtr = linitial_node(RangeTblRef, viewquery->jointree->fromlist);
2889
2890 /* Initialize the optional return values */
2891 if (updatable_cols != NULL)
2893 if (non_updatable_col != NULL)
2895
2896 /* Test each view column for updatability */
2898 foreach(cell, viewquery->targetList)
2899 {
2900 TargetEntry *tle = (TargetEntry *) lfirst(cell);
2901 const char *col_update_detail;
2902
2903 col++;
2905
2906 if (col_update_detail == NULL)
2907 {
2908 /* The column is updatable */
2909 if (updatable_cols != NULL)
2911 }
2912 else if (bms_is_member(col, required_cols))
2913 {
2914 /* The required column is not updatable */
2915 if (non_updatable_col != NULL)
2916 *non_updatable_col = tle->resname;
2917 return col_update_detail;
2918 }
2919 }
2920
2921 return NULL; /* all the required view columns are updatable */
2922}
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 2614 of file rewriteHandler.c.

2615{
2616 TriggerDesc *trigDesc = view->trigdesc;
2617
2618 switch (event)
2619 {
2620 case CMD_INSERT:
2621 if (trigDesc && trigDesc->trig_insert_instead_row)
2622 return true;
2623 break;
2624 case CMD_UPDATE:
2625 if (trigDesc && trigDesc->trig_update_instead_row)
2626 return true;
2627 break;
2628 case CMD_DELETE:
2629 if (trigDesc && trigDesc->trig_delete_instead_row)
2630 return true;
2631 break;
2632 case CMD_MERGE:
2633 foreach_node(MergeAction, action, mergeActionList)
2634 {
2635 switch (action->commandType)
2636 {
2637 case CMD_INSERT:
2638 if (!trigDesc || !trigDesc->trig_insert_instead_row)
2639 return false;
2640 break;
2641 case CMD_UPDATE:
2642 if (!trigDesc || !trigDesc->trig_update_instead_row)
2643 return false;
2644 break;
2645 case CMD_DELETE:
2646 if (!trigDesc || !trigDesc->trig_delete_instead_row)
2647 return false;
2648 break;
2649 case CMD_NOTHING:
2650 /* No trigger required */
2651 break;
2652 default:
2653 elog(ERROR, "unrecognized commandType: %d", action->commandType);
2654 break;
2655 }
2656 }
2657 return true; /* no actions without an INSTEAD OF trigger */
2658 default:
2659 elog(ERROR, "unrecognized CmdType: %d", (int) event);
2660 break;
2661 }
2662 return false;
2663}

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 2726 of file rewriteHandler.c.

2727{
2730
2731 /*----------
2732 * Check if the view is simply updatable. According to SQL-92 this means:
2733 * - No DISTINCT clause.
2734 * - Each TLE is a column reference, and each column appears at most once.
2735 * - FROM contains exactly one base relation.
2736 * - No GROUP BY or HAVING clauses.
2737 * - No set operations (UNION, INTERSECT or EXCEPT).
2738 * - No sub-queries in the WHERE clause that reference the target table.
2739 *
2740 * We ignore that last restriction since it would be complex to enforce
2741 * and there isn't any actual benefit to disallowing sub-queries. (The
2742 * semantic issues that the standard is presumably concerned about don't
2743 * arise in Postgres, since any such sub-query will not see any updates
2744 * executed by the outer query anyway, thanks to MVCC snapshotting.)
2745 *
2746 * We also relax the second restriction by supporting part of SQL:1999
2747 * feature T111, which allows for a mix of updatable and non-updatable
2748 * columns, provided that an INSERT or UPDATE doesn't attempt to assign to
2749 * a non-updatable column.
2750 *
2751 * In addition we impose these constraints, involving features that are
2752 * not part of SQL-92:
2753 * - No CTEs (WITH clauses).
2754 * - No OFFSET or LIMIT clauses (this matches a SQL:2008 restriction).
2755 * - No system columns (including whole-row references) in the tlist.
2756 * - No window functions in the tlist.
2757 * - No set-returning functions in the tlist.
2758 *
2759 * Note that we do these checks without recursively expanding the view.
2760 * If the base relation is a view, we'll recursively deal with it later.
2761 *----------
2762 */
2763 if (viewquery->distinctClause != NIL)
2764 return gettext_noop("Views containing DISTINCT are not automatically updatable.");
2765
2766 if (viewquery->groupClause != NIL || viewquery->groupingSets)
2767 return gettext_noop("Views containing GROUP BY are not automatically updatable.");
2768
2769 if (viewquery->havingQual != NULL)
2770 return gettext_noop("Views containing HAVING are not automatically updatable.");
2771
2772 if (viewquery->setOperations != NULL)
2773 return gettext_noop("Views containing UNION, INTERSECT, or EXCEPT are not automatically updatable.");
2774
2775 if (viewquery->cteList != NIL)
2776 return gettext_noop("Views containing WITH are not automatically updatable.");
2777
2778 if (viewquery->limitOffset != NULL || viewquery->limitCount != NULL)
2779 return gettext_noop("Views containing LIMIT or OFFSET are not automatically updatable.");
2780
2781 /*
2782 * We must not allow window functions or set returning functions in the
2783 * targetlist. Otherwise we might end up inserting them into the quals of
2784 * the main query. We must also check for aggregates in the targetlist in
2785 * case they appear without a GROUP BY.
2786 *
2787 * These restrictions ensure that each row of the view corresponds to a
2788 * unique row in the underlying base relation.
2789 */
2790 if (viewquery->hasAggs)
2791 return gettext_noop("Views that return aggregate functions are not automatically updatable.");
2792
2793 if (viewquery->hasWindowFuncs)
2794 return gettext_noop("Views that return window functions are not automatically updatable.");
2795
2796 if (viewquery->hasTargetSRFs)
2797 return gettext_noop("Views that return set-returning functions are not automatically updatable.");
2798
2799 /*
2800 * The view query should select from a single base relation, which must be
2801 * a table or another view.
2802 */
2803 if (list_length(viewquery->jointree->fromlist) != 1)
2804 return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2805
2806 rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
2807 if (!IsA(rtr, RangeTblRef))
2808 return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2809
2810 base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
2811 if (base_rte->rtekind != RTE_RELATION ||
2812 (base_rte->relkind != RELKIND_RELATION &&
2813 base_rte->relkind != RELKIND_FOREIGN_TABLE &&
2814 base_rte->relkind != RELKIND_VIEW &&
2816 return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2817
2818 if (base_rte->tablesample)
2819 return gettext_noop("Views containing TABLESAMPLE are not automatically updatable.");
2820
2821 /*
2822 * Check that the view has at least one updatable column. This is required
2823 * for INSERT/UPDATE but not for DELETE.
2824 */
2825 if (check_cols)
2826 {
2827 ListCell *cell;
2828 bool found;
2829
2830 found = false;
2831 foreach(cell, viewquery->targetList)
2832 {
2833 TargetEntry *tle = (TargetEntry *) lfirst(cell);
2834
2836 {
2837 found = true;
2838 break;
2839 }
2840 }
2841
2842 if (!found)
2843 return gettext_noop("Views that have no updatable columns are not automatically updatable.");
2844 }
2845
2846 return NULL; /* the view is updatable */
2847}

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().