Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 027b5a3

Browse files
committed
Call set_rel_pathlist_hook before generate_gather_paths, not after.
The previous ordering of these steps satisfied the nominal requirement that set_rel_pathlist_hook could editorialize on the whole set of Paths constructed for a base relation. In practice, though, trying to change the set of partial paths was impossible. Adding one didn't work because (a) it was too late to be included in Gather paths made by the core code, and (b) calling add_partial_path after generate_gather_paths is unsafe, because it might try to delete a path it thinks is dominated, but that is already embedded in some Gather path(s). Nor could the hook safely remove partial paths, for the same reason that they might already be embedded in Gathers. Better to call extensions first, let them add partial paths as desired, and then gather. In v11 and up, we already doubled down on that ordering by postponing gathering even further for single-relation queries; so even if the hook wished to editorialize on Gather path construction, it could not. Report and patch by KaiGai Kohei. Back-patch to 9.6 where Gather paths were added. Discussion: https://postgr.es/m/CAOP8fzahwpKJRTVVTqo2AE=mDTz_efVzV6Get_0=U3SO+-ha1A@mail.gmail.com
1 parent 920311a commit 027b5a3

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

doc/src/sgml/custom-scan.sgml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
<para>
3838
A custom scan provider will typically add paths for a base relation by
3939
setting the following hook, which is called after the core code has
40-
generated what it believes to be the complete and correct set of access
41-
paths for the relation.
40+
generated all the access paths it can for the relation (except for
41+
Gather paths, which are made after this call so that they can use
42+
partial paths added by the hook):
4243
<programlisting>
4344
typedef void (*set_rel_pathlist_hook_type) (PlannerInfo *root,
4445
RelOptInfo *rel,

src/backend/optimizer/path/allpaths.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -479,31 +479,34 @@ set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
479479
}
480480
}
481481

482+
/*
483+
* Allow a plugin to editorialize on the set of Paths for this base
484+
* relation. It could add new paths (such as CustomPaths) by calling
485+
* add_path(), or add_partial_path() if parallel aware. It could also
486+
* delete or modify paths added by the core code.
487+
*/
488+
if (set_rel_pathlist_hook)
489+
(*set_rel_pathlist_hook) (root, rel, rti, rte);
490+
482491
/*
483492
* If this is a baserel, we should normally consider gathering any partial
484-
* paths we may have created for it.
493+
* paths we may have created for it. We have to do this after calling the
494+
* set_rel_pathlist_hook, else it cannot add partial paths to be included
495+
* here.
485496
*
486497
* However, if this is an inheritance child, skip it. Otherwise, we could
487498
* end up with a very large number of gather nodes, each trying to grab
488499
* its own pool of workers. Instead, we'll consider gathering partial
489500
* paths for the parent appendrel.
490501
*
491502
* Also, if this is the topmost scan/join rel (that is, the only baserel),
492-
* we postpone this until the final scan/join targelist is available (see
493-
* grouping_planner).
503+
* we postpone gathering until the final scan/join targetlist is available
504+
* (see grouping_planner).
494505
*/
495506
if (rel->reloptkind == RELOPT_BASEREL &&
496507
bms_membership(root->all_baserels) != BMS_SINGLETON)
497508
generate_gather_paths(root, rel, false);
498509

499-
/*
500-
* Allow a plugin to editorialize on the set of Paths for this base
501-
* relation. It could add new paths (such as CustomPaths) by calling
502-
* add_path(), or delete or modify paths added by the core code.
503-
*/
504-
if (set_rel_pathlist_hook)
505-
(*set_rel_pathlist_hook) (root, rel, rti, rte);
506-
507510
/* Now find the cheapest of the paths for this rel */
508511
set_cheapest(rel);
509512

0 commit comments

Comments
 (0)