Re: generic plans and "initial" pruning
Amit Langote <amitlangote09@gmail.com>
On Thu, Jul 17, 2025 at 9:11 PM Amit Langote <amitlangote09@gmail.com> wrote: > The refinements I described in my email above might help mitigate some > of those executor-related issues. However, I'm starting to wonder if > it's worth reconsidering our decision to handle pruning, locking, and > validation entirely at executor startup, which was the approach taken > in the reverted patch. > > The alternative approach, doing initial pruning and locking within > plancache.c itself (which I floated a while ago), might be worth > revisiting. It avoids the complications we've discussed around the > executor API and preserves the clear separation of concerns that > plancache.c provides, though it does introduce some new layering > concerns, which I describe further below. > > To support this, we'd need a mechanism to pass pruning results to the > executor alongside each PlannedStmt. For each PartitionPruneInfo in > the plan, that would include the corresponding PartitionPruneState and > the bitmapset of surviving relids determined by initial pruning. Given > that a CachedPlan can contain multiple PlannedStmts, this would > effectively be a list of pruning results, one per statement. One > reasonable way to handle that might be to define a parallel data > structure, separate from PlannedStmt, constructed by plancache.c and > carried via QueryDesc. The memory and lifetime management would mirror > how ParamListInfo is handled today, leaving the executor API unchanged > and avoiding intrusive changes to PlannedStmt. > > However, one potentially problematic aspect of this design is managing > the lifecycle of the relations referenced by PartitionPruneState. > Currently, partitioned table relations are opened by the executor > after entering ExecutorStart() and closed automatically by > ExecEndPlan(), allowing cleanup of pruning states implicitly. If we > perform initial pruning earlier, we'd need to keep these relations > open longer, necessitating explicit cleanup calls (e.g., a new > FinishPartitionPruneState()) invoked by the caller of the executor, > such as from ExecutorEnd() or even higher-level callers. This > introduces some questionable layering by shifting responsibility for > relation management tasks, which ideally belong within the executor, > into its callers. > > My sense is that the complexity involved in carrying pruning results > via this parallel data structure was one of the concerns Tom raised > previously, alongside the significant pruning code refactoring that > the earlier patch required. The latter, at least, should no longer be > necessary given recent code improvements. One point I forgot to mention about this approach is that we'd also need to ensure permissions on parent relations are checked before performing initial pruning in plancache.c, since pruning may involve evaluating user-provided expressions. So in effect, we'd need to invoke not just ExecDoInitialPruning(), but also ExecCheckPermissions(), or some variant of it, prior to executor startup. While manageable, it does add slightly to the complexity. -- Thanks, Amit Langote
Commits
-
Stamp 19beta1.
- 4b0bf0788b06 19 (unreleased) cited
-
Revert "Don't lock partitions pruned by initial pruning"
- 1722d5eb05d8 18.0 landed
-
Ensure first ModifyTable rel initialized if all are pruned
- 28317de723b6 18.0 cited
-
Fix bug in cbc127917 to handle nested Append correctly
- cbb9086c9ef6 18.0 landed
-
Remove unstable test suite added by 525392d57
- 4f1b6e5bb4fe 18.0 landed
-
Don't lock partitions pruned by initial pruning
- 525392d5727f 18.0 landed
-
Fix an oversight in cbc127917 to handle MERGE correctly
- 75dfde13639a 18.0 landed
-
Track unpruned relids to avoid processing pruned relations
- cbc127917e04 18.0 landed
-
Perform runtime initial pruning outside ExecInitNode()
- d47cbf474ecb 18.0 landed
-
Move PartitionPruneInfo out of plan nodes into PlannedStmt
- bb3ec16e14de 18.0 landed
-
Fix setrefs.c's failure to do expression processing on prune steps.
- bf826ea06297 18.0 cited
-
Remove obsolete executor cleanup code
- d060e921ea5a 17.0 landed
-
Revert "Move PartitionPruneInfo out of plan nodes into PlannedStmt"
- 5472743d9e85 16.0 landed
-
Move PartitioPruneInfo out of plan nodes into PlannedStmt
- ec386948948c 16.0 landed
-
Refactor and cleanup runtime partition prune code a little
- 297daa9d4353 15.0 landed
-
Remove some unnecessary fields from Plan trees.
- 52ed730d511b 12.0 cited
-
Remove more redundant relation locking during executor startup.
- f2343653f5b2 12.0 cited
-
Shut down Gather's children before shutting down Gather itself.
- acf555bc53ac 10.0 cited