The previous coding here supposed that if run-time partitioning applied to a particular Append/MergeAppend plan, then all child plans of that node must be members of a single partitioning hierarchy. This is totally wrong, since an Append could be formed from a UNION ALL: we could have multiple hierarchies sharing the same Append, or child plans that aren't part of any hierarchy.
To fix, restructure the related plan-time and execution-time data structures so that we can have a separate list or array for each partitioning hierarchy. Also track subplans that are not part of any hierarchy, and make sure they don't get pruned.
Per reports from Phil Florent and others. Back-patch to v11, since the bug originated there.
David Rowley, with a lot of cosmetic adjustments by me; thanks also to Amit Langote for review.
1c2cb2744b Fix run-time partition pruning for appends with multiple source rels.
src/backend/executor/execPartition.c | 399 +++++++++++++++-----------
src/backend/executor/nodeAppend.c | 4 +-
src/backend/executor/nodeMergeAppend.c | 4 +-
src/backend/nodes/copyfuncs.c | 18 +-
src/backend/nodes/outfuncs.c | 18 +-
src/backend/nodes/readfuncs.c | 17 +-
src/backend/optimizer/path/allpaths.c | 27 +-
src/backend/optimizer/plan/createplan.c | 66 +++--
src/backend/optimizer/plan/planner.c | 1 +
src/backend/partitioning/partprune.c | 211 +++++++++++---
src/include/executor/execPartition.h | 68 +++--
src/include/nodes/nodes.h | 1 +
src/include/nodes/plannodes.h | 54 ++--
src/include/partitioning/partprune.h | 6 +-
src/test/regress/expected/partition_prune.out | 173 +++++++++++
src/test/regress/sql/partition_prune.sql | 62 ++++
16 files changed, 842 insertions(+), 287 deletions(-)