On Fri, Apr 15, 2022 at 05:16:44PM +0800, Richard Guo wrote: > Not related to this topic but I noticed another problem from the plan. > Note the first Sort node which is to unique-ify the result of the UNION. > Why cannot we re-arrange the sort keys from (a, b, c) to (a, c, b) so > that we can avoid the second Sort node?
I don't know, but it's possible there's a solution related to commit db0d67db2 "Optimize order of GROUP BY keys" - DISTINCT is the same as GROUP BY k1, ..., kN. I guess UNION [DISTINCT] should learn to use GROUP BY rather than DISTINCT? -- Justin