Re: "could not find pathkey item to sort" for TPC-DS queries 94-96
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Tomas Vondra <tomas.vondra@enterprisedb.com>
Cc: James Coleman <jtc331@gmail.com>, Luc Vlaming <luc@swarm64.com>,
PostgreSQL-development <pgsql-hackers@postgresql.org>,
Robert Haas <robertmhaas@gmail.com>
Date: 2021-04-17T19:39:45Z
Lists: pgsql-hackers
Attachments
- 0001-Fix-find_em_expr_usable_for_sorting_rel-2.patch (text/x-diff) patch 0001
- 0002-change-function-name.patch (text/x-diff) patch 0002
[ sorry for not getting to this thread till now ] Tomas Vondra <tomas.vondra@enterprisedb.com> writes: > 3) Shouldn't find_em_expr_usable_for_sorting_rel now mostly mimic what > prepare_sort_from_pathkeys does? That is, try to match the entries > directly first, before the new pull_vars() business? Yeah. I concur that the problem here is that find_em_expr_usable_for_sorting_rel isn't fully accounting for what prepare_sort_from_pathkeys can and can't do. However, I don't like this patch much: * As written, I think it may just move the pain somewhere else. The point of the logic in prepare_sort_from_pathkeys is to handle either full expression matches (e.g. sort by "A+B" when "A+B" is an expression in the input tlist) or computable expressions (sort by "A+B" when A and B are individually available). I think you've fixed the second case and broken the first one. Now it's possible that the case never arises, and certainly failing to generate an early sort isn't catastrophic anyway. But we ought to get it right. * If the goal is to match what prepare_sort_from_pathkeys can do, I think that doubling down on the strategy of having a duplicate copy is not the path to a maintainable fix. I think it's time for some refactoring of this code so that we can actually share the logic. Accordingly, I propose the attached. It's really not that hard to share, as long as you accept the idea that the list passed to the shared subroutine can be either a list of TargetEntries or of bare expressions. Also, I don't much care for either the name or API of find_em_expr_usable_for_sorting_rel. The sole current caller only really needs a boolean result, and if it did need more than that it'd likely need the whole EquivalenceMember not just the em_expr (certainly createplan.c does). So 0002 attached is some bikeshedding on that. I kept that separate because it might be wise to do it only in HEAD, just in case somebody out there is calling the function from an extension. (BTW, responding to an upthread question: I think the looping to remove multiple levels of RelabelType is probably now redundant, but I didn't remove it. If we want to do that there are more places to touch than just this code.) regards, tom lane
Commits
-
Rename find_em_expr_usable_for_sorting_rel.
- 7645376774c8 14.0 landed
-
Fix planner failure in some cases of sorting by an aggregate.
- 7bfba4f19330 13.3 landed
- 375398244168 14.0 landed