| From: | "Matheus Alcantara" <matheusssilv97(at)gmail(dot)com> |
|---|---|
| To: | <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Performance improvements on rescan of Append/MergeAppend with run-time partition pruning |
| Date: | 2026-11-30 14:09:49 |
| Message-ID: | DN8LSZTETPZK.QLJSK66PQPNT@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
When a partitioned table is on the inner side of a parameterized Nested Loop
(or in a correlated subquery), the Append/MergeAppend is rescanned for every
outer row and run-time pruning picks the partition(s) to scan. With many
partitions, this is much slower than it needs to be. The attached patches
try to address this.
This builds on the discussion in "Improve the performance of nested loop
join in the case of partitioned inner table" [1], from 2023. 0002 is a
rework of that proposal following David's review. 0001 is a new
improvement that I found while looking at where the time actually goes.
--- 0001: Defer changed-parameter signaling of Append/MergeAppend children
On every rescan, ExecReScanAppend() loops over all of its children and
calls UpdateChangedParamSet() (or ExecReScan()) on each one, even though
run-time pruning will then pick only one or a few of them to execute.
That's O(number of partitions) per outer row, and each iteration
allocates and frees a small Bitmapset (via bms_intersect() +
bms_join()). Profiling a Nested Loop into a 1000-partition table on
master shows about 58% of the execution time in ExecReScanAppend(),
against about ~2% for the pruning itself.
0001 records each rescan with a generation counter instead, and delivers
the pending signaling to a child only just before that child is about to
be executed. Children that pruning skips are never touched, so a rescan
becomes O(1) plus the work for the children that actually run. Each
child ends up with exactly the same signaling as on master. If a rescan
has a different set of changed parameters than the pending one, the
lagging children are brought up to date first.
The deferral is only used when the Append/MergeAppend has run-time pruning.
Without it, every child runs after each rescan anyway, so deferring would only
add bookkeeping. In an earlier version that deferred unconditionally, that
showed up as a 2-3% slowdown for rescans of a 1000-child Append. Those nodes
now keep the existing eager signaling, and that case is within noise of
master.
--- 0002: Skip run-time partition re-pruning when the parameter values are unchanged
Nested Loop marks its parameters as changed for every outer row, even
when the value is the same as for the previous row, so Append throws
away its pruning result and prunes again. The values of the Params used
by run-time pruning are saved, copying pass-by-ref values into a small
memory context, and compared with datumIsEqual() on rescan. Pruning is
redone only if one of them actually changed. TOASTed values and InitPlan
outputs that haven't been computed yet are always treated as changed.
--- Benchmarks
I compared release builds (cassert off) of master at 73401447c34, master
+ 0001 and master + 0001 + 0002, each on a freshly restarted cluster
with the same data set. Each query ran in 3 to 5 rounds of 5 runs after
a warm-up, and the numbers are the median of all runs. Results and plans
are identical on all builds for every case. The cases cover:
- range, list and hash partitioning with 2 to 1000 partitions, a DEFAULT
partition, and two-level partitioning;
- int, bigint-vs-int, 300-byte text, uuid, timestamptz, TOASTed text and
NULL keys;
- inner, LEFT, semi and anti Nested Loop joins, and correlated SubPlans;
- corner cases: pruning disabled, run-time pruning that keeps 50 or about
90% of 1000 partitions, Parallel Append under a rescanned Gather, and
generic plans with initial + run-time pruning;
- postgres_fdw foreign partitions, with sync and async Append;
- Appends without a partitioned table (UNION ALL with 2 and 16 arms, and
inheritance with 16 and 1000 children), and Nested Loop rescans without
parameters.
"chg" means the outer key changes on every row, and "rep" means the same rows
sorted by the key, so it repeats on consecutive rows. Times in ms:
master 0001 0001+0002
list, 1000 partitions, chg 1120 114 115
list, 1000 partitions, rep 1039 59 49
hash, 1000 partitions, chg 1071 93 95
hash, 1000 partitions, rep 1015 55 47
timestamptz, 365 daily ranges, chg 398 77 77
timestamptz, 365 daily ranges, rep 381 50 42
hash, 16 partitions, chg 102 88 89
hash, 16 partitions, rep 70 56 51
text 300 bytes, hash 16, chg 151 131 134
text 300 bytes, hash 16, rep 105 92 74
correlated SubPlan, 1000 partitions 3243 2147 2235
range, 2 partitions, chg 81 81 81
range 1000, pruning keeps ~90%, chg 2529 2475 2423
UNION ALL, 16 arms 664 660 659
hash 1000, rescans without params 1288 1277 1315
The full results are in the attached PDF.
The improvement grows with the number of partitions: about 90-95% faster
with 1000 partitions, 40-90% with 90-365, and about 15% with 16. With
only 2 partitions there's no measurable difference. One option would be
to enable this only above some number of partitions, though since small
partition counts don't seem to regress, I'm not sure the extra knob is
worth it. Opinions welcome. When pruning keeps most partitions, there's
nothing to save and the patches stay within noise of master; the same
goes for Appends without a partitioned table, and for rescans without
parameters, which all stay within about +/-3% of master, which seems
noise level on my machine, but I might be interpreting the results
wrong. Other testing methodology feedback is welcome, I tried my best to
find cases where these patches introduce a considerable regression.
As David expected in [1], 0002 adds some overhead when the parameter changes
on every outer row: in a profile of the 1000-partition "chg" case, saving and
comparing the values is about 4% of the execution time. It doesn't show up
clearly in the end-to-end timings above, and with 0001 the combined patches
are faster than master in every "chg" case. On top of 0002, we could still add
the adaptive behavior David suggested: stop saving and comparing after the
values change N times in a row, similar to the heuristic in 3592e0ff9, and
start again once pruning keeps returning the same partition.
Thoughts?
--
Matheus Alcantara
EDB: https://www.enterprisedb.com
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Defer-changed-parameter-signaling-of-Append-Merge.patch | text/plain | 29.2 KB |
| v1-0002-Skip-run-time-partition-re-pruning-when-the-param.patch | text/plain | 16.2 KB |
| partprune-benchmark.pdf | application/pdf | 9.2 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Pavel Stehule | 2026-11-30 14:14:56 | Re: extremely slow planner when DISTINCT clause holds more columns |
| Previous Message | Zhijie Hou | 2026-11-30 13:57:37 | Re: Fix LogStandbySnapshot() race during logical decoding activation |