Re: [HACKERS] Parallel tuplesort, partitioning, merging, and the future
On Wed, Aug 10, 2016 at 4:54 PM, Peter Geoghegan wrote: > On Wed, Aug 10, 2016 at 11:59 AM, Robert Haas wrote: >> My view on this - currently anyway - is that we shouldn't conflate the >> tuplesort with the subsequent index generation, but that we should try >> to use parallelism within the tuplesort itself to the greatest extent >> possible. If there is a single output stream that the leader uses to >> generate the final index, then none of the above problems arise. They >> only arise if you've got multiple processes actually writing to the >> index. > > I'm not sure if you're agreeing with my contention about parallel > CREATE INDEX not being a good target for partitioning here. Are you? No. I agree that writing to the index in parallel is bad, but I think it's entirely reasonable to try to set things up so that the leader does as little of the final merge work itself as possible, instead offloading that to workers. Unless, of course, we can prove that the overhead of the final merge pass is so low that it doesn't matter whether we offload it. > While all this speculation about choice of algorithm is fun, > realistically I'm not gong to write the patch for a rainy day (nor for > parallel CREATE INDEX, at least until we become very comfortable with > all the issues I raise, which could never happen). I'd be happy to > consider helping you improve parallel query by providing > infrastructure like this, but I need someone else to write the client > of the infrastructure (e.g. a parallel merge join patch), or to at > least agree to meet me half way with an interdependent prototype of > their own. It's going to be messy, and we'll have to do a bit of > stumbling to get to a good place. I can sign up to that if I'm not the > only one that has to stumble. Fair enough. > Serial merging still needs work, it seems. At the risk of stating the obvious, improving serial execution performance is always superior to comparable gains originating from parallelism, so no complaints here about work in that area. -- Robert Haas EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers
Re: [HACKERS] Parallel tuplesort, partitioning, merging, and the future
On Wed, Aug 10, 2016 at 11:59 AM, Robert Haas wrote: > I think that last part is a very important property; my intuition is > that dividing up the work between cooperating processes in a way that > should come out equal will often fail to do so, either due to the > operating system scheduler or due to some data being cached and other > data not being cached or due to the comparator running faster on some > data than other data or due to NUMA effects that make some processes > run faster than others or due to any number of other causes. So I > think that algorithms that allocate the work dynamically are going to > greatly outperform those that use a division of labor which is fixed > at the beginning of a computation phase. I agree that dynamic sampling has big advantages. Our Quicksort implementation does dynamic sampling, of course. You need to be strict about partition boundaries: they may not be drawn at a point in the key space that is not precisely defined, and in general there can be no ambiguity about what bucket a tuple can end up in ahead of time. In other words, you cannot carelessly allow equal tuples to go on either side of an equal boundary key. The reason for this restriction is that otherwise, stuff breaks when you later attempt to "align" boundaries across sort operations that are performed in parallel. I don't think you can introduce an artificial B-Tree style tie-breaker condition to avoid the problem, because that will slow things right down (B&M Quicksort does really well with many equal keys). When you have one really common value, load balancing for partitioning just isn't going to do very well. My point is that there will be a somewhat unpleasant worst case that will need to be accepted. It's not practical to go to the trouble of preventing it entirely. So, the comparison with quicksort works on a couple of levels. -- Peter Geoghegan -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers
Re: [HACKERS] Parallel tuplesort, partitioning, merging, and the future
On Wed, Aug 10, 2016 at 12:08 PM, Claudio Freire wrote: > I think it's a great design, but for that, per-worker final tapes have > to always be random-access. Thanks. I don't think I need to live with the randomAccess restriction, because I can be clever about reading only the first tuple on each logtape.c block initially. Much later, when the binary search gets down to seeking within a single block, everything in the block can be read at once into memory, and we can take the binary search to that other representation. This latter part only needs to happen once or twice per partition boundary per worker. > I'm not hugely familiar with the code, but IIUC there's some penalty > to making them random-access right? Yeah, there is. For one thing, you have to store the length of the tuple twice, to support incremental seeking in both directions. For another, you cannot perform the final merge on-the-fly; you must produce a serialized tape as output, which is used subsequently to support random seeks. There is no penalty when you manage to do the sort in memory, though (not that that has anything to do with parallel sort). -- Peter Geoghegan -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers
Re: [HACKERS] Parallel tuplesort, partitioning, merging, and the future
On Wed, Aug 10, 2016 at 11:59 AM, Robert Haas wrote: > My view on this - currently anyway - is that we shouldn't conflate the > tuplesort with the subsequent index generation, but that we should try > to use parallelism within the tuplesort itself to the greatest extent > possible. If there is a single output stream that the leader uses to > generate the final index, then none of the above problems arise. They > only arise if you've got multiple processes actually writing to the > index. I'm not sure if you're agreeing with my contention about parallel CREATE INDEX not being a good target for partitioning here. Are you? You can get some idea of how much a separate pass over the concatenated outputs would hurt by using the test GUC with my patch applied (the one that artificially forces randomAccess by B-Tree tuplesort callers). >> Suggested partitioning algorithm >> >> The basic idea I have in mind is that we create runs in workers in the >> same way that the parallel CREATE INDEX patch does (one output run per >> worker). However, rather than merging in the leader, we use a >> splitting algorithm to determine partition boundaries on-the-fly. The >> logical tape stuff then does a series of binary searches to find those >> exact split points within each worker's "final" tape. Each worker >> reports the boundary points of its original materialized output run in >> shared memory. Then, the leader instructs workers to "redistribute" >> slices of their final runs among each other, by changing the tapeset >> metadata to reflect that each worker has nworker input tapes with >> redrawn offsets into a unified BufFile. Workers immediately begin >> their own private on-the-fly merges. > > Yeah, this is pretty cool. You end up with the final merge segmented > into N submerges producing nonoverlapping ranges. So you could have > the leader perform submerge 0 itself, and while it's doing that the > other workers can perform submerges 1..N. By the time the leader > finishes submerge 0, the remaining submerges will likely be complete > and after that the leader can just read the outputs of those submerges > one after another and it has basically no additional work to do. Again, I'm a little puzzled by your remarks here. Surely the really great case for parallel sort with partitioning is the case where there remains minimal further IPC between workers? So, while "the leader can just read the outputs of those submerges", ideally it will be reading as little as possible from workers. For example, it's ideal when the workers were able to determine that their particular range in the parallel merge join has very few tuples to return, having also "synchronized" their range within two underlying relations (importantly, the merge join "synchronization" can begin per worker when the tuplesort.c on-the-fly merge begins and returns its first tuple -- that is, it can begin very soon). In short, partitioning when sorting is as much about avoiding a serial dependency for the entire query tree as it is about taking advantage of available CPU cores and disk spindles. That is my understanding, at any rate. While all this speculation about choice of algorithm is fun, realistically I'm not gong to write the patch for a rainy day (nor for parallel CREATE INDEX, at least until we become very comfortable with all the issues I raise, which could never happen). I'd be happy to consider helping you improve parallel query by providing infrastructure like this, but I need someone else to write the client of the infrastructure (e.g. a parallel merge join patch), or to at least agree to meet me half way with an interdependent prototype of their own. It's going to be messy, and we'll have to do a bit of stumbling to get to a good place. I can sign up to that if I'm not the only one that has to stumble. Remember how I said we should work on the merging bottleneck indirectly? I'm currently experimenting with having merging use sifting down to replace the root in the heap. This is very loosely based on the Jeremy Harris patch from 2014, I suppose. Anyway, this can be far, far faster, with perhaps none of the downsides that we saw in the context of building an initial replacement selection heap, because we have more control of the distribution of input (tapes have sorted tuples), and because this heap is so tiny and cache efficient to begin with. This does really well in the event of clustering of values, which is a common case, but also helps with totally random initially input. I need to do some more research before posting a patch, but right now I can see that it makes merging presorted numeric values more than 2x faster. And that's with 8 tapes, on my very I/O bound laptop. I bet that the benefits would also be large for text (temporal locality is improved, and so strcoll() comparison caching is more effective). Serial merging still needs work, it seems. -- Peter Geoghegan -- Sent via pgsql-hackers
Re: [HACKERS] Parallel tuplesort, partitioning, merging, and the future
On Mon, Aug 8, 2016 at 4:44 PM, Peter Geoghegan wrote: > The basic idea I have in mind is that we create runs in workers in the > same way that the parallel CREATE INDEX patch does (one output run per > worker). However, rather than merging in the leader, we use a > splitting algorithm to determine partition boundaries on-the-fly. The > logical tape stuff then does a series of binary searches to find those > exact split points within each worker's "final" tape. Each worker > reports the boundary points of its original materialized output run in > shared memory. Then, the leader instructs workers to "redistribute" > slices of their final runs among each other, by changing the tapeset > metadata to reflect that each worker has nworker input tapes with > redrawn offsets into a unified BufFile. Workers immediately begin > their own private on-the-fly merges. I think it's a great design, but for that, per-worker final tapes have to always be random-access. I'm not hugely familiar with the code, but IIUC there's some penalty to making them random-access right? -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers
Re: [HACKERS] Parallel tuplesort, partitioning, merging, and the future
On Mon, Aug 8, 2016 at 3:44 PM, Peter Geoghegan wrote: > I don't think partitioning is urgent for CREATE INDEX, and may be > inappropriate for CREATE INDEX under any circumstances, because: > > * Possible problems with parallel infrastructure and writes. > * Unbalanced B-Trees (or the risk thereof). > * What I've come up with is minimally divergent from the existing > approach to tuplesorting. My view on this - currently anyway - is that we shouldn't conflate the tuplesort with the subsequent index generation, but that we should try to use parallelism within the tuplesort itself to the greatest extent possible. If there is a single output stream that the leader uses to generate the final index, then none of the above problems arise. They only arise if you've got multiple processes actually writing to the index. > Suggested partitioning algorithm > > > I think a hybrid partitioning + merging approach would work well for > us. The paper "Parallel Sorting on a Shared-Nothing Architecture using > Probabilistic Splitting" [3] has influenced my thinking here (this was > written by prominent researchers from the influential UW-Madison > Wisconsin database group). Currently, I have in mind something that is > closer to what they call exact splitting to what they call > probabilistic splitting, because I don't think it's going to be > generally possible to have good statistics on partition boundaries > immediately available (e.g., through something like their > probabilistic splitting sampling the relation ahead of time). > > The basic idea I have in mind is that we create runs in workers in the > same way that the parallel CREATE INDEX patch does (one output run per > worker). However, rather than merging in the leader, we use a > splitting algorithm to determine partition boundaries on-the-fly. The > logical tape stuff then does a series of binary searches to find those > exact split points within each worker's "final" tape. Each worker > reports the boundary points of its original materialized output run in > shared memory. Then, the leader instructs workers to "redistribute" > slices of their final runs among each other, by changing the tapeset > metadata to reflect that each worker has nworker input tapes with > redrawn offsets into a unified BufFile. Workers immediately begin > their own private on-the-fly merges. Yeah, this is pretty cool. You end up with the final merge segmented into N submerges producing nonoverlapping ranges. So you could have the leader perform submerge 0 itself, and while it's doing that the other workers can perform submerges 1..N. By the time the leader finishes submerge 0, the remaining submerges will likely be complete and after that the leader can just read the outputs of those submerges one after another and it has basically no additional work to do. It might be a good idea to divide the work into a number of submerges substantially greater than the number of workers. For example, suppose we expect between 1 and 4 workers, but we partition the work into 64 submerges. The leader claims submerge 0, which is only 1/64 of the total. By the time it finishes consuming those tuples, submerge 1 will likely be done. Hopefully, even if there are only 1 or 2 workers, they can keep ahead of the leader so that very little of the merging happens in the leader. Also, if some submerges go faster than others, the distribution of work among workers remains even, because the ones that go quicker will handle more of the submerges and the ones that go slower will handle fewer. I think that last part is a very important property; my intuition is that dividing up the work between cooperating processes in a way that should come out equal will often fail to do so, either due to the operating system scheduler or due to some data being cached and other data not being cached or due to the comparator running faster on some data than other data or due to NUMA effects that make some processes run faster than others or due to any number of other causes. So I think that algorithms that allocate the work dynamically are going to greatly outperform those that use a division of labor which is fixed at the beginning of a computation phase. > Clearly it's really hard to be sure that this is the right thing at > this point, but my intuition is that this is the way to go (while > avoiding anything like this for CREATE INDEX). I'd like to know how > others feel about it. The number of others weighing in on these topics is surely less than either of us would like, but hopefully we can find a way to make progress anyhow. -- Robert Haas EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers