diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2023-01-18 13:23:57 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2023-01-18 13:23:57 -0500 |
commit | 47bb9db75996232ea71fc1e1888ffb0e70579b54 (patch) | |
tree | 745e1a7755802a7e92cd267bce642358a1f37a0a /src/backend/rewrite/rewriteHandler.c | |
parent | 8d83a5d0a2673174dc478e707de1f502935391a5 (diff) | |
download | postgresql-47bb9db75996232ea71fc1e1888ffb0e70579b54.tar.gz postgresql-47bb9db75996232ea71fc1e1888ffb0e70579b54.zip |
Get rid of the "new" and "old" entries in a view's rangetable.
The rule system needs "old" and/or "new" pseudo-RTEs in rule actions
that are ON INSERT/UPDATE/DELETE. Historically it's put such entries
into the ON SELECT rules of views as well, but those are really quite
vestigial. The only thing we've used them for is to carry the
view's relid forward to AcquireExecutorLocks (so that we can
re-lock the view to verify it hasn't changed before re-using a plan)
and to carry its relid and permissions data forward to execution-time
permissions checks. What we can do instead of that is to retain
these fields of the RTE_RELATION RTE for the view even after we
convert it to an RTE_SUBQUERY RTE. This requires a tiny amount of
extra complication in the planner and AcquireExecutorLocks, but on
the other hand we can get rid of the logic that moves that data from
one place to another.
The principal immediate benefit of doing this, aside from a small
saving in the pg_rewrite data for views, is that these pseudo-RTEs
no longer trigger ruleutils.c's heuristic about qualifying variable
names when the rangetable's length is more than 1. That results
in quite a number of small simplifications in regression test outputs,
which are all to the good IMO.
Bump catversion because we need to dump a few more fields of
RTE_SUBQUERY RTEs. While those will always be zeroes anyway in
stored rules (because we'd never populate them until query rewrite)
they are useful for debugging, and it seems like we'd better make
sure to transmit such RTEs accurately in plans sent to parallel
workers. I don't think the executor actually examines these fields
after startup, but someday it might.
This is a second attempt at committing 1b4d280ea. The difference
from the first time is that now we can add some filtering rules to
AdjustUpgrade.pm to allow cross-version upgrade testing to pass
despite all the cosmetic changes in CREATE VIEW outputs.
Amit Langote (filtering rules by me)
Discussion: https://postgr.es/m/CA+HiwqEf7gPN4Hn+LoZ4tP2q_Qt7n3vw7-6fJKOf92tSEnX6Gg@mail.gmail.com
Discussion: https://postgr.es/m/891521.1673657296@sss.pgh.pa.us
Diffstat (limited to 'src/backend/rewrite/rewriteHandler.c')
-rw-r--r-- | src/backend/rewrite/rewriteHandler.c | 38 |
1 files changed, 12 insertions, 26 deletions
diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c index 1960dad7013..c74bac20b13 100644 --- a/src/backend/rewrite/rewriteHandler.c +++ b/src/backend/rewrite/rewriteHandler.c @@ -1715,10 +1715,7 @@ ApplyRetrieveRule(Query *parsetree, List *activeRIRs) { Query *rule_action; - RangeTblEntry *rte, - *subrte; - RTEPermissionInfo *perminfo, - *sub_perminfo; + RangeTblEntry *rte; RowMarkClause *rc; if (list_length(rule->actions) != 1) @@ -1830,32 +1827,20 @@ ApplyRetrieveRule(Query *parsetree, * original RTE to a subquery RTE. */ rte = rt_fetch(rt_index, parsetree->rtable); - perminfo = getRTEPermissionInfo(parsetree->rteperminfos, rte); rte->rtekind = RTE_SUBQUERY; rte->subquery = rule_action; rte->security_barrier = RelationIsSecurityView(relation); - /* Clear fields that should not be set in a subquery RTE */ - rte->relid = InvalidOid; - rte->relkind = 0; - rte->rellockmode = 0; - rte->tablesample = NULL; - rte->perminfoindex = 0; /* no permission checking for this RTE */ - rte->inh = false; /* must not be set for a subquery */ /* - * We move the view's permission check data down to its RTEPermissionInfo - * contained in the view query, which the OLD entry in its range table - * points to. + * Clear fields that should not be set in a subquery RTE. Note that we + * leave the relid, rellockmode, and perminfoindex fields set, so that the + * view relation can be appropriately locked before execution and its + * permissions checked. */ - subrte = rt_fetch(PRS2_OLD_VARNO, rule_action->rtable); - Assert(subrte->relid == relation->rd_id); - sub_perminfo = getRTEPermissionInfo(rule_action->rteperminfos, subrte); - sub_perminfo->requiredPerms = perminfo->requiredPerms; - sub_perminfo->checkAsUser = perminfo->checkAsUser; - sub_perminfo->selectedCols = perminfo->selectedCols; - sub_perminfo->insertedCols = perminfo->insertedCols; - sub_perminfo->updatedCols = perminfo->updatedCols; + rte->relkind = 0; + rte->tablesample = NULL; + rte->inh = false; /* must not be set for a subquery */ return parsetree; } @@ -1867,9 +1852,10 @@ ApplyRetrieveRule(Query *parsetree, * aggregate. We leave it to the planner to detect that. * * NB: this must agree with the parser's transformLockingClause() routine. - * However, unlike the parser we have to be careful not to mark a view's - * OLD and NEW rels for updating. The best way to handle that seems to be - * to scan the jointree to determine which rels are used. + * However, we used to have to avoid marking a view's OLD and NEW rels for + * updating, which motivated scanning the jointree to determine which rels + * are used. Possibly that could now be simplified into just scanning the + * rangetable as the parser does. */ static void markQueryForLocking(Query *qry, Node *jtnode, |