Parallel TID Range Scan returns wrong results on rescan

Started by Zsolt Parragiabout 1 month ago3 messageshackers
Jump to latest
#1Zsolt Parragi
zsolt.parragi@percona.com

Hello

I found a bug in parallel TID range can: if the gather above it gets
rescanned with different TID bounds, rows can go missing.

A simple way to reproduce it:

CREATE TABLE t (id int, pad text) WITH (fillfactor = 10);
INSERT INTO t SELECT i, repeat('x', 100) FROM generate_series(1, 200) i;
SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SET min_parallel_table_scan_size = 0;
SET max_parallel_workers_per_gather = 4;

SELECT r.lo, (SELECT count(*) FROM t WHERE ctid >= (SELECT r.lo)) c
FROM (VALUES ('(30,0)'::tid), ('(10,0)'), ('(0,0)')) r(lo);

This should return 50, 150 and 200 (that's also what 18 returns,
without the parallel scan), but master and 19rc1 return 50 for all 3
rows.

The cause:

phs_startblock and phs_numblock in ParallelBlockTableScanDesc are set
by whichever participant calls
table_block_parallelscan_startblock_init() first, but
table_block_parallelscan_reinitialize() only resets phs_nallocated. So
every rescan scans the block range of the first execution, and the
per-tuple TID check then throws away anything outside the new range.

The attached patch resets both fields to InvalidBlockNumber in
table_block_parallelscan_reinitialize(), and adds a test that fails
without the fix.

Attachments:

t254223_1
v1-0001-Fix-Parallel-TID-Range-Scan-rescans-with-a-differ.patchapplication/octet-stream; name=v1-0001-Fix-Parallel-TID-Range-Scan-rescans-with-a-differ.patchDownload+51-1
#2shihao zhong
zhong950419@gmail.com
In reply to: Zsolt Parragi (#1)
Re: Parallel TID Range Scan returns wrong results on rescan

Hi Zsolt,

Thanks for working on this.

v1 looks good to me. I can reproduce it on master and the patch
fixes it.

I think reinitialize is the right place for the fix. Before 19,
phs_startblock did not depend on the query, so keeping it across
rescans was fine.

Thanks,
Shihao

#3David Rowley
dgrowleyml@gmail.com
In reply to: Zsolt Parragi (#1)
Re: Parallel TID Range Scan returns wrong results on rescan

On Fri, 16 Oct 2026 at 08:54, Zsolt Parragi <zsolt.parragi@percona.com> wrote:

The attached patch resets both fields to InvalidBlockNumber in
table_block_parallelscan_reinitialize(), and adds a test that fails
without the fix.

Thanks. I've pushed that one to master and wrote an adapted version
for v19's lack of atomics for those fields.

David