David Rowley <david.row...@2ndquadrant.com> writes: > Thanks for explaining. I see where you're coming from now. I think > this point would carry more weight if using the Append instead of the > MergeAppend were worse in some cases as we could end up using an > inferior plan accidentally. However, that's not the case. The Append > plan should always perform better both for startup and pulling a > single row all the way to pulling the final row. The underlying > subplans are the same in each case, but Append has the additional > saving of not having to determine to perform a sort on the top row > from each subpath.
Uh, what? sorted-Append and MergeAppend would need pre-sorts on exactly the same set of children. It's true that the Append path might not have to actually execute some of those sorts, if it's able to stop in an earlier child. The problem here is basically that it's hard to predict whether that will happen. > Append always wins over MergeAppend, so I > don't quite understand your reasoning here. The problem is that the planner is likely to favor a "fast-start" Append *too much*, and prefer it over some other plan altogether. In cases where, say, the first child requires no sort but also doesn't emit very many rows, while the second child requires an expensive sort, the planner will have a ridiculously optimistic opinion of the cost of fetching slightly more rows than are available from the first child. This might lead it to wrongly choose a merge join over a hash for example. Yes, there are cases where Append-with-some-sorts is preferable to MergeAppend-with-some-sorts, and maybe I'd even believe that it always is. But I don't believe that it's necessarily preferable to plans that don't require a sort at all, and I'm afraid that we are likely to find the planner making seriously bad choices when it's presented with such situations. I'd rather we leave that case out for now, until we have some better way of modelling it. The fact that the patch also requires a lot of extra hacking just to support this case badly doesn't make me any more favorably disposed. regards, tom lane