Re: BitmapHeapScan streaming read user and prelim refactoring

Andres Freund <andres@anarazel.de>

From: Andres Freund <andres@anarazel.de>
To: Alexander Lakhin <exclusion@gmail.com>
Cc: Melanie Plageman <melanieplageman@gmail.com>, Thomas Munro <thomas.munro@gmail.com>, Tomas Vondra <tomas@vondra.me>, Nazir Bilal Yavuz <byavuz81@gmail.com>, Dilip Kumar <dilipbalaut@gmail.com>, Heikki Linnakangas <hlinnaka@iki.fi>, Pg Hackers <pgsql-hackers@postgresql.org>
Date: 2025-03-22T21:04:55Z
Lists: pgsql-hackers
Hi,

On 2025-03-22 16:42:42 -0400, Andres Freund wrote:
> Hm, it's clearly related to the all-visible path, but I think the bug is
> actually independent of what was reported there. The bug you reported is
> perfectly reproducible, without any concurrency, after all.
> 
> Here's a smaller reproducer:
> 
> CREATE TABLE bmscantest (a int, t text);
> 
> INSERT INTO bmscantest
>   SELECT (r%53), 'foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'
>   FROM generate_series(1,70000) r;
> 
> CREATE INDEX i_bmtest_a ON bmscantest(a);
> set enable_indexscan=false;
> set enable_seqscan=false;
> 
> -- Lower work_mem to trigger use of lossy bitmaps
> set work_mem = 64;
> 
> SELECT count(*) FROM bmscantest WHERE a = 1;
> vacuum freeze bmscantest;
> SELECT count(*) FROM bmscantest WHERE a = 1;
> 
> -- clean up
> DROP TABLE bmscantest;
> 
> 
> The first SELECT reports 1321, the second 572 tuples.

The problem is that sometimes recheck is performed for pending empty
tuples. The comment about empty tuples says:

        /*
         * Bitmap is exhausted. Time to emit empty tuples if relevant. We emit
         * all empty tuples at the end instead of emitting them per block we
         * skip fetching. This is necessary because the streaming read API
         * will only return TBMIterateResults for blocks actually fetched.
         * When we skip fetching a block, we keep track of how many empty
         * tuples to emit at the end of the BitmapHeapScan. We do not recheck
         * all NULL tuples.
         */
        *recheck = false;
        return bscan->rs_empty_tuples_pending > 0;

But we actually emit empty tuples at each page boundary:

	/*
	 * Out of range?  If so, nothing more to look at on this page
	 */
	while (hscan->rs_cindex >= hscan->rs_ntuples)
	{
		/*
		 * Emit empty tuples before advancing to the next block
		 */
		if (bscan->rs_empty_tuples_pending > 0)
		{
			/*
			 * If we don't have to fetch the tuple, just return nulls.
			 */
			ExecStoreAllNullTuple(slot);
			bscan->rs_empty_tuples_pending--;
			return true;
		}

		/*
		 * Returns false if the bitmap is exhausted and there are no further
		 * blocks we need to scan.
		 */
		if (!BitmapHeapScanNextBlock(scan, recheck, lossy_pages, exact_pages))
			return false;
	}

So we don't just process tuples at the end of the scan, but at each page
boundary.

This leads to wrong results whenever there is a rs_empty_tuples_pending > 0
after a previous page that needed rechecks, because nothing will reset
*recheck = false.

And indeed, if I add a *recheck = false in that inside the
		/*
		 * Emit empty tuples before advancing to the next block
		 */
		if (bscan->rs_empty_tuples_pending > 0)
		{
the problem goes away.


A fix should do slightly more, given that the "at the end" comment is wrong.


But I'm wondering if it's worth fixing it, or if we should just rip the logic
out alltogether, due to the whole VM checking being unsound as we learned in
the thread I referenced earlie, without even bothering to fix the bug here.

Greetings,

Andres Freund



Commits

  1. Fix bitmapheapscan incorrect recheck of NULL tuples

  2. Increase default maintenance_io_concurrency to 16

  3. Separate TBM[Shared|Private]Iterator and TBMIterateResult

  4. Improve read_stream.c advice for dense streams.

  5. Increase default effective_io_concurrency to 16

  6. Delay extraction of TIDBitmap per page offsets

  7. Add lossy indicator to TBMIterateResult

  8. Move BitmapTableScan per-scan setup into a helper

  9. Add and use BitmapHeapScanDescData struct

  10. Fix bitmap table scan crash on iterator release

  11. Bitmap Table Scans use unified TBMIterator

  12. Add common interface for TBMIterators

  13. Make table_scan_bitmap_next_block() async-friendly

  14. Move EXPLAIN counter increment to heapam_scan_bitmap_next_block

  15. Refactor tidstore.c iterator buffering.

  16. BitmapHeapScan: Remove incorrect assert and reset field

  17. Change BitmapAdjustPrefetchIterator to accept BlockNumber

  18. BitmapHeapScan: Use correct recheck flag for skip_fetch

  19. BitmapHeapScan: Push skip_fetch optimization into table AM

  20. BitmapHeapScan: postpone setting can_skip_fetch

  21. BitmapHeapScan: begin scan after bitmap creation

  22. Fix EXPLAIN Bitmap heap scan to count pages with no visible tuples

  23. Remove redundant snapshot copying from parallel leader to workers

  24. Remove some obsolete smgrcloseall() calls.

  25. Remove the "snapshot too old" feature.

  26. Compute XID horizon for page level index vacuum on primary.