Re: B-Tree support function number 3 (strxfrm() optimization) - Mailing list pgsql-hackers
From | Peter Geoghegan |
---|---|
Subject | Re: B-Tree support function number 3 (strxfrm() optimization) |
Date | |
Msg-id | CAM3SWZTa2EyFbMbzOvPO3pvDunqy2Kgmueb4oZYqupD0YNXD_Q@mail.gmail.com Whole thread Raw |
In response to | B-Tree support function number 3 (strxfrm() optimization) (Peter Geoghegan <pg@heroku.com>) |
Responses |
Re: B-Tree support function number 3 (strxfrm() optimization)
Re: B-Tree support function number 3 (strxfrm() optimization) |
List | pgsql-hackers |
On Wed, Mar 26, 2014 at 8:08 PM, Peter Geoghegan <pg@heroku.com> wrote: > The API I envisage is a new support function 3 that operator class > authors may optionally provide. I've built a prototype patch, attached, that extends SortSupport and tuplesort to support "poor man's normalized keys". All the regression tests pass, so while it's just a proof of concept, it is reasonably well put together for one. The primary shortcoming of the prototype (the main reason why I'm calling it a prototype rather than just a patch) is that it isn't sufficiently generalized (i.e. it only works for the cases currently covered by SortSupport - not B-Tree index builds, or B-Tree scanKeys). There is no B-Tree support function number 3 in the patch. I didn't spend too long on this. I'm pretty happy with the results for in-memory sorting of text (my development system uses 'en_US.UTF8', so please assume that any costs involved are for runs that use that collation). With the dellstore2 sample database [1] restored to my local development instance, the following example demonstrates just how much the technique can help performance. With master: pg@hamster:~/sort-tests$ cat sort.sql select * from (select * from customers order by firstname offset 100000) d; pg@hamster:~/sort-tests$ pgbench -f sort.sql -n -T 100 transaction type: Custom query scaling factor: 1 query mode: simple number of clients: 1 number of threads: 1 duration: 100 s number of transactions actually processed: 819 latency average: 122.100 ms tps = 8.186197 (including connections establishing) tps = 8.186522 (excluding connections establishing) With patch applied (requires initdb for new text SortSupport pg_proc entry): pg@hamster:~/sort-tests$ cat sort.sql select * from (select * from customers order by firstname offset 100000) d; pg@hamster:~/sort-tests$ pgbench -f sort.sql -n -T 100 transaction type: Custom query scaling factor: 1 query mode: simple number of clients: 1 number of threads: 1 duration: 100 s number of transactions actually processed: 2525 latency average: 39.604 ms tps = 25.241723 (including connections establishing) tps = 25.242447 (excluding connections establishing) It looks like this technique is very valuable indeed, at least in the average or best case. We're not just benefiting from following the advice of the standard, and using strxfrm() for sorting to amortize the cost of the strxfrm() transformation that strcoll() must do anyway. It stands to reason that there is also a lot of benefit from sorting tightly-packed keys. Quicksort is cache oblivious, and having it sort tightly-packed binary data, as opposed to going through all of that deferencing and deserialization indirection is probably also very helpful. A tool like Cachegrind might offer some additional insights, but I haven't gone to the trouble of trying that out. (By the way, my earlier recollection about how memory-frugal MinimalTuple/memtuple building is within tuplesort was incorrect, so there are no savings in memory to be had here). As I mentioned, something like a SortSupport for numeric, with poor man's normalized keys might also be compelling. I suggest we focus on how this technique can be further generalized, though. This prototype patch is derivative of Robert's abandoned SortSupport for text patch. If he wanted to take this off my hands, I'd have no objections - I don't think I'm going to have time to take this as far as I'd like. [1] http://pgfoundry.org/forum/forum.php?forum_id=603 -- Peter Geoghegan
Attachment
pgsql-hackers by date: