mirror of
https://github.com/postgres/postgres.git
synced 2025-08-25 20:23:07 +03:00
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. Amit Langote Discussion: https://postgr.es/m/CA+HiwqEf7gPN4Hn+LoZ4tP2q_Qt7n3vw7-6fJKOf92tSEnX6Gg@mail.gmail.com
This commit is contained in:
@@ -405,13 +405,15 @@ add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
|
||||
*
|
||||
* At top level, we must add all RTEs so that their indexes in the
|
||||
* flattened rangetable match up with their original indexes. When
|
||||
* recursing, we only care about extracting relation RTEs.
|
||||
* recursing, we only care about extracting relation RTEs (and subquery
|
||||
* RTEs that were once relation RTEs).
|
||||
*/
|
||||
foreach(lc, root->parse->rtable)
|
||||
{
|
||||
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
|
||||
|
||||
if (!recursing || rte->rtekind == RTE_RELATION)
|
||||
if (!recursing || rte->rtekind == RTE_RELATION ||
|
||||
(rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)))
|
||||
add_rte_to_flat_rtable(glob, root->parse->rteperminfos, rte);
|
||||
}
|
||||
|
||||
@@ -501,8 +503,9 @@ flatten_rtes_walker(Node *node, flatten_rtes_walker_context *cxt)
|
||||
{
|
||||
RangeTblEntry *rte = (RangeTblEntry *) node;
|
||||
|
||||
/* As above, we need only save relation RTEs */
|
||||
if (rte->rtekind == RTE_RELATION)
|
||||
/* As above, we need only save relation RTEs and former relations */
|
||||
if (rte->rtekind == RTE_RELATION ||
|
||||
(rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)))
|
||||
add_rte_to_flat_rtable(cxt->glob, cxt->query->rteperminfos, rte);
|
||||
return false;
|
||||
}
|
||||
@@ -560,7 +563,8 @@ add_rte_to_flat_rtable(PlannerGlobal *glob, List *rteperminfos,
|
||||
glob->finalrtable = lappend(glob->finalrtable, newrte);
|
||||
|
||||
/*
|
||||
* If it's a plain relation RTE, add the table to relationOids.
|
||||
* If it's a plain relation RTE (or a subquery that was once a view
|
||||
* reference), add the relation OID to relationOids.
|
||||
*
|
||||
* We do this even though the RTE might be unreferenced in the plan tree;
|
||||
* this would correspond to cases such as views that were expanded, child
|
||||
@@ -570,7 +574,8 @@ add_rte_to_flat_rtable(PlannerGlobal *glob, List *rteperminfos,
|
||||
* Note we don't bother to avoid making duplicate list entries. We could,
|
||||
* but it would probably cost more cycles than it would save.
|
||||
*/
|
||||
if (newrte->rtekind == RTE_RELATION)
|
||||
if (newrte->rtekind == RTE_RELATION ||
|
||||
(newrte->rtekind == RTE_SUBQUERY && OidIsValid(newrte->relid)))
|
||||
glob->relationOids = lappend_oid(glob->relationOids, newrte->relid);
|
||||
|
||||
/*
|
||||
@@ -3403,14 +3408,11 @@ extract_query_dependencies_walker(Node *node, PlannerInfo *context)
|
||||
{
|
||||
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
|
||||
|
||||
if (rte->rtekind == RTE_RELATION)
|
||||
if (rte->rtekind == RTE_RELATION ||
|
||||
(rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)) ||
|
||||
(rte->rtekind == RTE_NAMEDTUPLESTORE && OidIsValid(rte->relid)))
|
||||
context->glob->relationOids =
|
||||
lappend_oid(context->glob->relationOids, rte->relid);
|
||||
else if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
|
||||
OidIsValid(rte->relid))
|
||||
context->glob->relationOids =
|
||||
lappend_oid(context->glob->relationOids,
|
||||
rte->relid);
|
||||
}
|
||||
|
||||
/* And recurse into the query's subexpressions */
|
||||
|
Reference in New Issue
Block a user