Re: BitmapHeapScan streaming read user and prelim refactoring
Melanie Plageman <melanieplageman@gmail.com>
Attachments
- v21-0001-table_scan_bitmap_next_block-counts-lossy-and-ex.patch (text/x-patch) patch v21-0001
- v21-0003-Make-table_scan_bitmap_next_block-async-friendly.patch (text/x-patch) patch v21-0003
- v21-0004-Add-common-interface-for-TBMIterators.patch (text/x-patch) patch v21-0004
- v21-0002-Remove-table_scan_bitmap_next_tuple-parameter-tb.patch (text/x-patch) patch v21-0002
- v21-0005-BitmapHeapScan-initialize-some-prefetch-state-el.patch (text/x-patch) patch v21-0005
- v21-0006-BitmapHeapScan-uses-unified-iterator.patch (text/x-patch) patch v21-0006
- v21-0007-BitmapHeapScan-Make-prefetch-sync-error-more-det.patch (text/x-patch) patch v21-0007
- v21-0008-Push-current-scan-descriptor-into-specialized-sc.patch (text/x-patch) patch v21-0008
- v21-0012-Update-variable-names-in-bitmap-scan-descriptors.patch (text/x-patch) patch v21-0012
- v21-0009-Remove-ss_current-prefix-from-ss_currentScanDesc.patch (text/x-patch) patch v21-0009
- v21-0010-Add-scan_in_progress-to-BitmapHeapScanState.patch (text/x-patch) patch v21-0010
- v21-0011-Make-new-bitmap-table-scan-and-bitmap-heap-scan-.patch (text/x-patch) patch v21-0011
- v21-0013-BitmapHeapScan-Push-primary-iterator-setup-into-.patch (text/x-patch) patch v21-0013
- v21-0014-BitmapHeapScan-lift-and-shift-prefetch-functions.patch (text/x-patch) patch v21-0014
- v21-0016-BitmapHeapScan-move-per-scan-setup-into-a-helper.patch (text/x-patch) patch v21-0016
- v21-0015-BitmapHeapScan-Push-prefetch-code-into-heap-AM.patch (text/x-patch) patch v21-0015
- v21-0018-Hard-code-TBMIterateResult-offsets-array-size.patch (text/x-patch) patch v21-0018
- v21-0017-Remove-table-AM-callback-scan_bitmap_next_block.patch (text/x-patch) patch v21-0017
- v21-0019-Separate-TBMIterator-and-TBMIterateResult.patch (text/x-patch) patch v21-0019
- v21-0020-Use-streaming-I-O-in-Bitmap-Heap-Scans.patch (text/x-patch) patch v21-0020
On Sun, Apr 7, 2024 at 12:17 AM Melanie Plageman <melanieplageman@gmail.com> wrote: > > After we decided not to pursue streaming bitmapheapscan for 17, I wanted > to make sure we removed the prefetch code table AM violation -- since we > weren't deleting that code. So what started out as me looking for a way > to clean up one commit ended up becoming a much larger project. Sorry > about that last minute code explosion! I do think there is a way to do > it right and make it nice. Also that violation would be gone if we > figure out how to get streaming bitmapheapscan behaving correctly. > > So, there's just more motivation to make streaming bitmapheapscan > awesome for 18! Attached v21 is the rest of the patches to make bitmap heap scan use the read stream API. Don't be alarmed by the 20 patches in the set. I tried to keep the individual patches as small and easy to review as possible. Patches 0001-0003 implement the async-friendly behavior needed both to push down the VM lookups for prefetching and eventually to use the read stream API. Patches 0004-0006 add and make use of a common interface for the shared and private (parallel and serial) bitmap iterator per Heikki's suggestion in [1]. Patches 0008 - 0012 make new scan descriptors for bitmap table scans and the heap AM implementation. It is not possible to remove the layering violations mentioned for bitmap table scans in tableam.h without passing more bitmap-specific parameters to table_begin/end/rescan(). Because bitmap heap scans use a fairly reduced set of the members in the HeapScanDescData, it made sense to enable specialized scan descriptors for bitmap table scans. 0013 pushes the primary iterator setup down into heap code. 0014 and 0015 push all of the prefetch code down into heap AM code as suggested by Heikki in [1]. 0017 removes scan_bitmap_next_block() per Heikki's suggestion in [1]. After all of these patches, we've removed the layering violations mentioned previously in tableam.h. There is no use of the visibility map anymore in generic bitmap table scan code. Almost all block-specific logic is gone. The table AMs own the iterator almost completely. The one relic of iterator ownership is that for parallel bitmap heap scan, a single process must scan the index, construct the bitmap, and call tbm_prepare_shared_iterate() to set up the iterator for all the processes. I didn't see a way to push this down (because to build the bitmap we have to scan the index and call ExecProcNode()). I wonder if this creates an odd split of responsibilities. I could use some other synchronization mechanism to communicate which process built the bitmap in the generic bitmap table scan code and thus should set up the iterator in the heap implementation, but that sounds like a pretty bad idea. Also, I'm not sure how a non-block-based table AM would use TBMIterators (or even TIDBitmap) anyway. As a side note, I've started naming all new structs in the bitmap table scan code with the `BitmapTableScan` convention. It seems like it might be a good time to rename the executor node from BitmapHeapScanState to BitmapTableScanState. I didn't because I wondered if I also needed to rename the file (nodeBitmapHeapscan.c -> nodeBitmapTablescan.c) and update all other places using the `BitmapHeapScan` convention. Patches 0018 and 0019 make some changes to the TIDBitmap API to support having multiple TBMIterateResults at the same time instead of reusing the same one when iterating. With async execution we may have more than one TBMIterateResult at a time. There is one MTODO in the whole patch set -- in 0019 -- related to resetting state in the TBMIterateResult. See that patch for the full question. And, finally, 0020 uses the read stream API and removes all the bespoke prefetching code from bitmap heap scan. Assuming we all get to a happy place with the code up until 0020, the next step is to go back and investigate the performance regression with bitmap heap scan and the read stream API first reported by Tomas Vondra in [2]. I'd be very happy for code review on any of the patches, answers to my questions above, or help investigating the regression Tomas found. - Melanie [1] https://www.postgresql.org/message-id/5a172d1e-d69c-409a-b1fa-6521214c81c2%40iki.fi [2] https://www.postgresql.org/message-id/5d5954ed-6f43-4f1a-8e19-ece75b2b7362@enterprisedb.com
Commits
-
Fix bitmapheapscan incorrect recheck of NULL tuples
- aea916fe555a 18.0 landed
-
Increase default maintenance_io_concurrency to 16
- cc6be07ebde2 18.0 landed
-
Separate TBM[Shared|Private]Iterator and TBMIterateResult
- 944e81bf99db 18.0 landed
-
Improve read_stream.c advice for dense streams.
- 7ea8cd15661e 18.0 landed
-
Increase default effective_io_concurrency to 16
- ff79b5b2aba0 18.0 landed
-
Delay extraction of TIDBitmap per page offsets
- bfe56cdf9a4e 18.0 landed
-
Add lossy indicator to TBMIterateResult
- b8778c4cd8bc 18.0 landed
-
Move BitmapTableScan per-scan setup into a helper
- a5358c14b2fe 18.0 landed
-
Add and use BitmapHeapScanDescData struct
- f7a8fc10ccb8 18.0 landed
-
Fix bitmap table scan crash on iterator release
- 754c610e13b8 18.0 landed
-
Bitmap Table Scans use unified TBMIterator
- 1a0da347a7ac 18.0 landed
-
Add common interface for TBMIterators
- 7f9d4187e7ba 18.0 landed
-
Make table_scan_bitmap_next_block() async-friendly
- de380a62b5da 18.0 landed
-
Move EXPLAIN counter increment to heapam_scan_bitmap_next_block
- 7bd7aa4d3067 18.0 landed
-
Refactor tidstore.c iterator buffering.
- f6bef362cac8 18.0 cited
-
BitmapHeapScan: Remove incorrect assert and reset field
- a3e6c6f92991 17.0 landed
-
Change BitmapAdjustPrefetchIterator to accept BlockNumber
- 92641d8d651e 17.0 landed
-
BitmapHeapScan: Use correct recheck flag for skip_fetch
- 1fdb0ce9b109 17.0 landed
-
BitmapHeapScan: Push skip_fetch optimization into table AM
- 04e72ed617be 17.0 landed
-
BitmapHeapScan: postpone setting can_skip_fetch
- fe1431e39cdd 17.0 landed
-
BitmapHeapScan: begin scan after bitmap creation
- 1577081e9614 17.0 landed
-
Fix EXPLAIN Bitmap heap scan to count pages with no visible tuples
- f3e4581acdc8 12.19 landed
- 992189a3e94d 13.15 landed
- 262757b73286 14.12 landed
- d3d95f583995 15.7 landed
- 1f4eb734200a 16.3 landed
- 0960ae1967d0 17.0 landed
-
Remove redundant snapshot copying from parallel leader to workers
- 84c18acaf690 17.0 landed
-
Remove some obsolete smgrcloseall() calls.
- 6a8ffe812d19 17.0 cited
-
Remove the "snapshot too old" feature.
- f691f5b80a85 17.0 cited
-
Compute XID horizon for page level index vacuum on primary.
- 558a9165e081 12.0 cited