Skip to content

Refactor parallel workers v2 - #86

Open
lfittl wants to merge 5 commits into
masterfrom
refactor-parallel-workers-v2
Open

lfittl wants to merge 5 commits into
masterfrom
refactor-parallel-workers-v2

Conversation

@lfittl

@lfittl lfittl commented Oct 31, 2026

Copy link
Copy Markdown
Owner

No description provided.

@lfittl
lfittl force-pushed the refactor-parallel-workers-v2 branch 2 times, most recently from c18c6ed to eb19ca8 Compare October 31, 2026 17:55
Commit 6fd5071 made parallel workers for VACUUM, BRIN and btree
index builds report the query ID of the leader, so that they show up
with the correct query_id in pg_stat_activity.  Parallel GIN index
builds were added later by 8492feb, but did not include this,
so GIN build workers have always reported a query ID of 0.

Fix by storing the leader's query ID in the shared build state and
reporting it in the worker, like the other index AMs already do.

Author: Lukas Fittl <lukas@fittl.com>
Discussion: https://postgr.es/m/CAP53Pkw3WcVbg61G29AmN=6XkuxGryoWKnhTyC1JmkT08i8qVA@mail.gmail.com
When a non-concurrent index build encounters a RECENTLY_DEAD tuple that
was HOT-updated, heapam_index_build_range_scan() skips it, indexing only
the live tuple at the end of the HOT chain, and sets ii_BrokenHotChain.
index_build() then marks the index with indcheckxmin, so that transactions
with a snapshot older than index creation don't use the index, as it may
be missing tuple versions still visible to them.

In parallel index builds this requires each worker to report back to the
leader whether it encountered a broken HOT chain. btree correctly does
this, but BRIN and GIN parallel index builds do not. As a result,
indcheckxmin was never set, and a transaction holding an older snapshot
could use the new index and get wrong results.

Fix by tracking the brokenhotchain flag in the shared state of both AMs,
like btree does, and copying it back into the ambuild IndexInfo in the
leader after all participants are done.

Oversight when parallel index builds were added for BRIN (b437571)
and GIN (8492feb) and backpatch to 17 and 18 respectively.

Author: Lukas Fittl <lukas@fittl.com>
Reviewed-by:
Discussion: https://postgr.es/m/CAP53Pkw3WcVbg61G29AmN=6XkuxGryoWKnhTyC1JmkT08i8qVA@mail.gmail.com
@lfittl
lfittl force-pushed the refactor-parallel-workers-v2 branch from eb19ca8 to af025d7 Compare October 31, 2026 19:11
The three in-tree index AMs that currently support parallel index builds
(nbtree, gin and brin) each share very similar code for initializing
parallel workers, allocating shared memory and communicating between the
leader and the workers.

To reduce duplication, introduce shared helpers for parallel index
builds, unifying the existing code.

Author: Lukas Fittl <lukas@fittl.com>
Reviewed-by: Sami Imseih <samimseih@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CAP53Pkw3WcVbg61G29AmN=6XkuxGryoWKnhTyC1JmkT08i8qVA@mail.gmail.com
Multiple parallel mechanisms outside of the executor (nbtree/GIN/BRIN
index builds, vacuum) all had the same implementation for passing query
text to the parallel workers.

To reduce duplicated code, introduce new helper functions to: (1) estimate
the needed shared memory space for the query text, (2) store the query
text in shared memory, and (3) restore the query text in the worker.

Author: Lukas Fittl <lukas@fittl.com>
Reviewed-by: Sami Imseih <samimseih@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CAP53Pkw3WcVbg61G29AmN=6XkuxGryoWKnhTyC1JmkT08i8qVA@mail.gmail.com
@lfittl
lfittl force-pushed the refactor-parallel-workers-v2 branch 2 times, most recently from 9ddc792 to 29127d2 Compare October 31, 2026 20:06
The parallel index build helpers left it to each AM to pass the
ii_BrokenHotChain flag from the IndexInfo used for its heap scan into
ParallelIndexBuildReportScanDone(), and to read it back out of
ParallelIndexBuildWaitForWorkers() into the IndexInfo passed to
ambuild. This is brittle and both GIN and BRIN have gotten this wrong
in the past, and there extensions that handle this incorrectly today.

Instead, introduce ParallelIndexBuildScan(), which builds the IndexInfo,
joins the shared parallel scan, runs table_index_build_scan() and
records a broken HOT chain itself. ParallelTableScanDescData is now stored
in a separate shmem TOC entry for easier handling.

Author: Lukas Fittl <lukas@fittl.com>
Reviewed-by:
Discussion: https://postgr.es/m/CAP53Pkw3WcVbg61G29AmN=6XkuxGryoWKnhTyC1JmkT08i8qVA@mail.gmail.com
@lfittl
lfittl force-pushed the refactor-parallel-workers-v2 branch from 29127d2 to 8946374 Compare October 31, 2026 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant