Skip to content

Commit 49ac403

Browse files
committed
Simplify view-expansion code in rewriteHandler.c.
In the wake of commit 50c6bb0, it's not necessary for ApplyRetrieveRule to have a forUpdatePushedDown parameter. By the time control gets here for any given view-referencing RTE, we should already have pushed down the effects of any FOR UPDATE/SHARE clauses affecting the view from outer query levels. Hence if we don't find a RowMarkClause at the current query level, that's sufficient proof that there is no outer one either. This in turn means we need no forUpdatePushedDown parameter for fireRIRrules. I wonder whether we oughtn't also revert commit cba2d27, since it now seems likely that that was band-aiding around the bad effects of doing FOR UPDATE pushdown and view expansion in the wrong order. However, in the absence of evidence that the current coding of markQueryForLocking is actually buggy (i.e. missing RTEs it ought to mark), it seems best to leave it alone. Discussion: https://postgr.es/m/24db7b8f-3de5-e25f-7ab9-d8848351d42c@gmail.com
1 parent 4d64abc commit 49ac403

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/backend/rewrite/rewriteHandler.c

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ static void markQueryForLocking(Query *qry, Node *jtnode,
7777
bool pushedDown);
7878
static List *matchLocks(CmdType event, RuleLock *rulelocks,
7979
int varno, Query *parsetree, bool *hasUpdate);
80-
static Query *fireRIRrules(Query *parsetree, List *activeRIRs,
81-
bool forUpdatePushedDown);
80+
static Query *fireRIRrules(Query *parsetree, List *activeRIRs);
8281
static bool view_has_instead_trigger(Relation view, CmdType event);
8382
static Bitmapset *adjust_view_column_set(Bitmapset *cols, List *targetlist);
8483

@@ -1453,8 +1452,7 @@ ApplyRetrieveRule(Query *parsetree,
14531452
RewriteRule *rule,
14541453
int rt_index,
14551454
Relation relation,
1456-
List *activeRIRs,
1457-
bool forUpdatePushedDown)
1455+
List *activeRIRs)
14581456
{
14591457
Query *rule_action;
14601458
RangeTblEntry *rte,
@@ -1545,28 +1543,27 @@ ApplyRetrieveRule(Query *parsetree,
15451543
}
15461544

15471545
/*
1548-
* If FOR [KEY] UPDATE/SHARE of view, be sure we get right initial lock on
1549-
* the relations it references.
1546+
* Check if there's a FOR [KEY] UPDATE/SHARE clause applying to this view.
1547+
*
1548+
* Note: we needn't explicitly consider any such clauses appearing in
1549+
* ancestor query levels; their effects have already been pushed down to
1550+
* here by markQueryForLocking, and will be reflected in "rc".
15501551
*/
15511552
rc = get_parse_rowmark(parsetree, rt_index);
1552-
forUpdatePushedDown |= (rc != NULL);
15531553

15541554
/*
15551555
* Make a modifiable copy of the view query, and acquire needed locks on
1556-
* the relations it mentions.
1556+
* the relations it mentions. Force at least RowShareLock for all such
1557+
* rels if there's a FOR [KEY] UPDATE/SHARE clause affecting this view.
15571558
*/
15581559
rule_action = copyObject(linitial(rule->actions));
15591560

1560-
AcquireRewriteLocks(rule_action, true, forUpdatePushedDown);
1561+
AcquireRewriteLocks(rule_action, true, (rc != NULL));
15611562

15621563
/*
15631564
* If FOR [KEY] UPDATE/SHARE of view, mark all the contained tables as
15641565
* implicit FOR [KEY] UPDATE/SHARE, the same as the parser would have done
15651566
* if the view's subquery had been written out explicitly.
1566-
*
1567-
* Note: we needn't consider forUpdatePushedDown for this; if there was an
1568-
* ancestor query level with a relevant FOR [KEY] UPDATE/SHARE clause,
1569-
* that's already been pushed down to here and is reflected in "rc".
15701567
*/
15711568
if (rc != NULL)
15721569
markQueryForLocking(rule_action, (Node *) rule_action->jointree,
@@ -1581,7 +1578,7 @@ ApplyRetrieveRule(Query *parsetree,
15811578
* OLD rangetable entries by the action below (in a recursive call of this
15821579
* routine).
15831580
*/
1584-
rule_action = fireRIRrules(rule_action, activeRIRs, forUpdatePushedDown);
1581+
rule_action = fireRIRrules(rule_action, activeRIRs);
15851582

15861583
/*
15871584
* Now, plug the view query in as a subselect, replacing the relation's
@@ -1698,7 +1695,7 @@ fireRIRonSubLink(Node *node, List *activeRIRs)
16981695

16991696
/* Do what we came for */
17001697
sub->subselect = (Node *) fireRIRrules((Query *) sub->subselect,
1701-
activeRIRs, false);
1698+
activeRIRs);
17021699
/* Fall through to process lefthand args of SubLink */
17031700
}
17041701

@@ -1713,10 +1710,13 @@ fireRIRonSubLink(Node *node, List *activeRIRs)
17131710

17141711
/*
17151712
* fireRIRrules -
1716-
* Apply all RIR rules on each rangetable entry in a query
1713+
* Apply all RIR rules on each rangetable entry in the given query
1714+
*
1715+
* activeRIRs is a list of the OIDs of views we're already processing RIR
1716+
* rules for, used to detect/reject recursion.
17171717
*/
17181718
static Query *
1719-
fireRIRrules(Query *parsetree, List *activeRIRs, bool forUpdatePushedDown)
1719+
fireRIRrules(Query *parsetree, List *activeRIRs)
17201720
{
17211721
int origResultRelation = parsetree->resultRelation;
17221722
int rt_index;
@@ -1747,9 +1747,7 @@ fireRIRrules(Query *parsetree, List *activeRIRs, bool forUpdatePushedDown)
17471747
*/
17481748
if (rte->rtekind == RTE_SUBQUERY)
17491749
{
1750-
rte->subquery = fireRIRrules(rte->subquery, activeRIRs,
1751-
(forUpdatePushedDown ||
1752-
get_parse_rowmark(parsetree, rt_index) != NULL));
1750+
rte->subquery = fireRIRrules(rte->subquery, activeRIRs);
17531751
continue;
17541752
}
17551753

@@ -1835,8 +1833,7 @@ fireRIRrules(Query *parsetree, List *activeRIRs, bool forUpdatePushedDown)
18351833
rule,
18361834
rt_index,
18371835
rel,
1838-
activeRIRs,
1839-
forUpdatePushedDown);
1836+
activeRIRs);
18401837
}
18411838

18421839
activeRIRs = list_delete_first(activeRIRs);
@@ -1852,7 +1849,7 @@ fireRIRrules(Query *parsetree, List *activeRIRs, bool forUpdatePushedDown)
18521849
CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
18531850

18541851
cte->ctequery = (Node *)
1855-
fireRIRrules((Query *) cte->ctequery, activeRIRs, false);
1852+
fireRIRrules((Query *) cte->ctequery, activeRIRs);
18561853
}
18571854

18581855
/*
@@ -3604,7 +3601,7 @@ QueryRewrite(Query *parsetree)
36043601
{
36053602
Query *query = (Query *) lfirst(l);
36063603

3607-
query = fireRIRrules(query, NIL, false);
3604+
query = fireRIRrules(query, NIL);
36083605

36093606
query->queryId = input_query_id;
36103607

0 commit comments

Comments
 (0)