forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit9626068

Richard Guo
Avoid unnecessary post-sort projection
When generating paths for the ORDER BY clause, one thing we need toensure is that the output paths project the correct final_target. Toachieve this, in create_ordered_paths, we compare the pathtarget ofeach generated path with the given 'target', and add a post-sortprojection step if the two targets do not match.Currently we perform a simple pointer comparison between the twotargets. It turns out that this is not sufficient. Each sorted_pathgenerated in create_ordered_paths initially projects the correcttarget required by the preceding steps of sort. If it is the samepointer as sort_input_target, pointer comparison suffices, becausesort_input_target is always identical to final_target when nopost-sort projection is needed.However, sorted_path's initial pathtarget may not be the same pointeras sort_input_target, because in apply_scanjoin_target_to_paths, ifthe target to be applied has the same expressions as the existingreltarget, we only inject the sortgroupref info into the existingpathtargets, rather than create new projection paths. As a result,pointer comparison in create_ordered_paths is not reliable.Instead, we can compare PathTarget.exprs to determine whether aprojection step is needed. If the expressions match, we can beconfident that a post-sort projection is not required.It could be argued that this change adds extra check cost each time wedecide whether a post-sort projection is needed. However, asexplained in apply_scanjoin_target_to_paths, by avoiding the creationof projection paths, we save effort both immediately and at plancreation time. This, I think, justifies the extra check cost.There are two ensuing plan changes in the regression tests, but theylook reasonable and are exactly what we are fixing here. So noadditional test cases are added.No backpatch as this could result in plan changes.Author: Richard GuoReviewed-by: Peter Eisentraut, David Rowley, Tom LaneDiscussion:https://postgr.es/m/CAMbWs48TosSvmnz88663_2yg3hfeOFss-J2PtnENDH6J_rLnRQ@mail.gmail.com1 parent4f11245 commit9626068
File tree
2 files changed
+22
-18
lines changed- src
- backend/optimizer/plan
- test/regress/expected
2 files changed
+22
-18
lines changedLines changed: 10 additions & 4 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
5300 | 5300 |
| |
5301 | 5301 |
| |
5302 | 5302 |
| |
5303 |
| - | |
5304 |
| - | |
| 5303 | + | |
| 5304 | + | |
| 5305 | + | |
| 5306 | + | |
| 5307 | + | |
5305 | 5308 |
| |
5306 | 5309 |
| |
5307 | 5310 |
| |
| |||
5378 | 5381 |
| |
5379 | 5382 |
| |
5380 | 5383 |
| |
5381 |
| - | |
5382 |
| - | |
| 5384 | + | |
| 5385 | + | |
| 5386 | + | |
| 5387 | + | |
| 5388 | + | |
5383 | 5389 |
| |
5384 | 5390 |
| |
5385 | 5391 |
| |
|
Lines changed: 12 additions & 14 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
81 | 81 |
| |
82 | 82 |
| |
83 | 83 |
| |
84 |
| - | |
85 |
| - | |
86 |
| - | |
87 |
| - | |
88 |
| - | |
89 |
| - | |
90 |
| - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
91 | 90 |
| |
92 | 91 |
| |
93 | 92 |
| |
| |||
115 | 114 |
| |
116 | 115 |
| |
117 | 116 |
| |
118 |
| - | |
119 |
| - | |
120 |
| - | |
121 |
| - | |
122 |
| - | |
123 |
| - | |
124 |
| - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
125 | 123 |
|
0 commit comments
Comments
(0)