HASH INDEX builds seems confused
HASH INDEX builds seems confused
От:
John Naylor <johncnaylorls@gmail.com>
Дата:
hashbuild() says: * If we just insert the tuples into the index in scan order, then * (assuming their hash codes are pretty random) there will be no locality * of access to the index, and if the index is bigger than available RAM * then we'll thrash horribly. To prevent that scenario, we can sort the * tuples by (expected) bucket number. However, such a sort is useless * overhead when the index does fit in RAM. We choose to sort if the * initial index size exceeds maintenance_work_mem, or the number of * buffers usable for the index, whichever is less. (Limiting by the However, since commit e09d7a126 it's harder to believe sorts are ever useless, since we then decided that sorts should have a more strict sort order for the sake of sequential access. Further, d09dbeb9b built upon that to remove wasteful binary search when inserting into the page. Looking at some of the numbers in the linked threads, I wonder if all test environments were actually hitting the sort path at all, since you'd have to exceed m_w_m or s_b to take advantage. Unless I'm missing something, it seems like we should just sort unconditionally. That would be a nice simplification, and might speed up index builds even when there's plenty of memory. (If I am in fact missing something, maybe comments need updating) Now that I'm looking, I'm also wondering how hard it would be to have datum1 contain both the bucket (high bits) and hash (lower bits), since we can now count on Datums being 8 bytes on all platforms. It might be harder in turn to hack things so that the appropriate sort specialization could be applied (it'd need a fake sortKey at least), but that would be a possible future project. -- John Naylor Amazon Web Services
Re: HASH INDEX builds seems confused
От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Tue, Nov 11, 2025 at 11:27 AM John Naylor wrote: > > hashbuild() says: > > * If we just insert the tuples into the index in scan order, then > * (assuming their hash codes are pretty random) there will be no locality > * of access to the index, and if the index is bigger than available RAM > * then we'll thrash horribly. To prevent that scenario, we can sort the > * tuples by (expected) bucket number. However, such a sort is useless > * overhead when the index does fit in RAM. We choose to sort if the > * initial index size exceeds maintenance_work_mem, or the number of > * buffers usable for the index, whichever is less. (Limiting by the > > However, since commit e09d7a126 it's harder to believe sorts are ever > useless, since we then decided that sorts should have a more strict > sort order for the sake of sequential access. Further, d09dbeb9b built > upon that to remove wasteful binary search when inserting into the > page. Looking at some of the numbers in the linked threads, I wonder > if all test environments were actually hitting the sort path at all, > since you'd have to exceed m_w_m or s_b to take advantage. Unless I'm > missing something, it seems like we should just sort unconditionally. > That would be a nice simplification, and might speed up index builds > even when there's plenty of memory. (If I am in fact missing > something, maybe comments need updating) > +1. It seems worth pursuing this. We can establish the benefits by taking some performance data. > Now that I'm looking, I'm also wondering how hard it would be to have > datum1 contain both the bucket (high bits) and hash (lower bits), > since we can now count on Datums being 8 bytes on all platforms. It > might be harder in turn to hack things so that the appropriate sort > specialization could be applied (it'd need a fake sortKey at least), > but that would be a possible future project. > Yeah that also sounds worth exploring but what benefit are you expecting out of it? -- With Regards, Amit Kapila.
Re: HASH INDEX builds seems confused
От:
John Naylor <johncnaylorls@gmail.com>
Дата:
On Tue, Nov 11, 2025 at 6:08 PM Amit Kapila wrote: > > On Tue, Nov 11, 2025 at 11:27 AM John Naylor wrote: > > However, since commit e09d7a126 it's harder to believe sorts are ever > > useless, since we then decided that sorts should have a more strict > > sort order for the sake of sequential access. Further, d09dbeb9b built > > upon that to remove wasteful binary search when inserting into the > > page. Looking at some of the numbers in the linked threads, I wonder > > if all test environments were actually hitting the sort path at all, > > since you'd have to exceed m_w_m or s_b to take advantage. Unless I'm > > missing something, it seems like we should just sort unconditionally. > > That would be a nice simplification, and might speed up index builds > > even when there's plenty of memory. (If I am in fact missing > > something, maybe comments need updating) > > > > +1. It seems worth pursuing this. We can establish the benefits by > taking some performance data. I'll investigate this. > > Now that I'm looking, I'm also wondering how hard it would be to have > > datum1 contain both the bucket (high bits) and hash (lower bits), > > since we can now count on Datums being 8 bytes on all platforms. It > > might be harder in turn to hack things so that the appropriate sort > > specialization could be applied (it'd need a fake sortKey at least), > > but that would be a possible future project. > > > > Yeah that also sounds worth exploring but what benefit are you > expecting out of it? In this case, anything that benefits unsigned integer sorts would automatically benefit building hash indexes. It seems more invasive to get there though, and I'm not planning on looking at this in the short term. In the thread that lead to the two commits mentioned above https://www.postgresql.org/message-id/CANbhV-FG-1ZNMBuwhUF7AxxJz3u5137dYL-o6hchK1V_dMw86g%40mail.gmail.com ...Simon mentioned currently "...the Tuplesortstate fixes the size of max_buckets at tuplesort_begin() time rather than tuplesort_performsort(), forcing us to estimate the number of tuples ahead of time rather than using the exact number. Next trick would be to alter the APIs to allow exact values to be used for sorting, which would allow page at a time builds." I'm not quite sure of the details here, but if this presents an additional benefit from a different API, that could make it more worthwhile. -- John Naylor Amazon Web Services
Re: HASH INDEX builds seems confused
On Tue, Sep 22, 2026 at 7:16 PM Amit Kapila wrote: > > On Tue, Nov 11, 2025 at 11:27 AM John Naylor wrote: > > > > hashbuild() says: > > > > * If we just insert the tuples into the index in scan order, then > > * (assuming their hash codes are pretty random) there will be no locality > > * of access to the index, and if the index is bigger than available RAM > > * then we'll thrash horribly. To prevent that scenario, we can sort the > > * tuples by (expected) bucket number. However, such a sort is useless > > * overhead when the index does fit in RAM. We choose to sort if the > > * initial index size exceeds maintenance_work_mem, or the number of > > * buffers usable for the index, whichever is less. (Limiting by the > > > > However, since commit e09d7a126 it's harder to believe sorts are ever > > useless, since we then decided that sorts should have a more strict > > sort order for the sake of sequential access. Further, d09dbeb9b built > > upon that to remove wasteful binary search when inserting into the > > page. Looking at some of the numbers in the linked threads, I wonder > > if all test environments were actually hitting the sort path at all, > > since you'd have to exceed m_w_m or s_b to take advantage. Unless I'm > > missing something, it seems like we should just sort unconditionally. > > That would be a nice simplification, and might speed up index builds > > even when there's plenty of memory. (If I am in fact missing > > something, maybe comments need updating) > > > > +1. It seems worth pursuing this. We can establish the benefits by > taking some performance data. > > > Now that I'm looking, I'm also wondering how hard it would be to have > > datum1 contain both the bucket (high bits) and hash (lower bits), > > since we can now count on Datums being 8 bytes on all platforms. It > > might be harder in turn to hack things so that the appropriate sort > > specialization could be applied (it'd need a fake sortKey at least), > > but that would be a possible future project. > > > > Yeah that also sounds worth exploring but what benefit are you > expecting out of it? > I measured the fits-in-RAM case that you are questioning and my result shows that sorting is not free. Result: sorting costs about 8-9ms: unlogged 84.36ms sorted -> 74.91ms unsorted -11.2% logged 118.10ms sorted -> 110.24ms unsorted -6.7% For this experiment, the server is patched with a test GUC to force sorting on or off, bypassing the questioned gated logic (num_buckets >= sort_threshold). The test ran 7200 (18 configurations × 2 modes × 200 reps) times. The following are the configurations: Logged and unlogged Column type int, bigint, text m_w_m: 4, 32, 128MB shared_buffers is kept constant 128MB through server configuration. Row count=100k, 512 buckets and ~4MB indexes measured using pgstathashindex on a separate untimed build. The test is vondra_bench.sh extended to toggle sort mode and use pgstathashindex for sizing. Attached: 0001-hash_build_sort_mode.patch - the test GUC force sorting on or off results_100k_r200.csv.gz - results for 100k rows rep=200 vondra_bench_sortmode.sh - test script index_sizes.csv - index sizes from results_100k_r200.csv.gz run
Re: HASH INDEX builds seems confused
От:
John Naylor <johncnaylorls@gmail.com>
Дата:
On Wed, Sep 23, 2026 at 9:20 AM Khoa Nguyen wrote: > I measured the fits-in-RAM case that you are questioning and my result > shows that sorting is not free. > > Result: sorting costs about 8-9ms: > unlogged 84.36ms sorted -> 74.91ms unsorted -11.2% > logged 118.10ms sorted -> 110.24ms unsorted -6.7% > > For this experiment, the server is patched with a test GUC to force > sorting on or off, bypassing the questioned gated logic (num_buckets > >= sort_threshold). > > The test ran 7200 (18 configurations × 2 modes × 200 reps) times. The > following are the configurations: > Logged and unlogged > Column type int, bigint, text > m_w_m: 4, 32, 128MB > > shared_buffers is kept constant 128MB through server configuration. > Row count=100k, 512 buckets and ~4MB indexes measured using > pgstathashindex on a separate untimed build. Thanks for testing! 4MB is awfully tiny -- does this finding scale if m_w_m is 64MB or 1GB, and the index is 95% of that? (it'd be easier to restrict it to only unlogged and ints for that). -- John Naylor Amazon Web Services
Re: HASH INDEX builds seems confused
От:
Khoa Nguyen <kdnguyen9.oss@gmail.com>
Дата:
On Wed, Sep 23, 2026 at 4:07 AM John Naylor wrote: > > On Wed, Sep 23, 2026 at 9:20 AM Khoa Nguyen wrote: > > I measured the fits-in-RAM case that you are questioning and my result > > shows that sorting is not free. > > > > Result: sorting costs about 8-9ms: > > unlogged 84.36ms sorted -> 74.91ms unsorted -11.2% > > logged 118.10ms sorted -> 110.24ms unsorted -6.7% > > > > For this experiment, the server is patched with a test GUC to force > > sorting on or off, bypassing the questioned gated logic (num_buckets > > >= sort_threshold). > > > > The test ran 7200 (18 configurations × 2 modes × 200 reps) times. The > > following are the configurations: > > Logged and unlogged > > Column type int, bigint, text > > m_w_m: 4, 32, 128MB > > > > shared_buffers is kept constant 128MB through server configuration. > > Row count=100k, 512 buckets and ~4MB indexes measured using > > pgstathashindex on a separate untimed build. > > Thanks for testing! 4MB is awfully tiny -- does this finding scale if > m_w_m is 64MB or 1GB, and the index is 95% of that? (it'd be easier to > restrict it to only unlogged and ints for that). I wanted to target an index size that is indisputably cache-friendly to see whether presorting still wins. However, I do agree with you that the result from a 4MB index is not usable in determining whether the presort gate is needed. I had to move to another machine so I reran the 4MB test for reference. Run configurations: 200 reps, force sort GUC, unlogged int. Fixed shared_buffers=64GB rows=100K mwm=4|32|128MB sorted/unsorted=1.243 Idx size at ~95% of mwm=64MB|1GB rows=1.8M idx_size=62MB mwm=64MB sorted/unsorted=1.21 rows=28.5M idx_size=979MB mwm=1GB sorted/unsorted=0.49 -Khoa