Skip to content

LWLocks for dynamic shared memory, with an ad-hoc implementation for parallel foreign scans - #2395

Open
fkomauli wants to merge 1 commit into
pgcentralfoundation:developfrom
optionfactory:develop
Open

fkomauli wants to merge 1 commit into
pgcentralfoundation:developfrom
optionfactory:develop

Conversation

@fkomauli

Copy link
Copy Markdown

The primary goal of this PR is to implement a mechanism for locking dynamic shared memory (DSM) provided to worker processes during a parallel foreign scan.

The proposed solution is based on three layers:

  • a low level Rust wrapper over PostgreSQL LWLock, not tied to SHMEM hooks;
  • a mechanism for requesting a LWLock tranche on startup, stored as a static global, based on the existing PgSharedMemoryInitialization trait and the pg_shmem_init! macro;
  • high level components that are designed to be straightforwardly used in PostgreSQL FDW routines (pgrx_*_foreign_scan).

Tests

This PR implements the same test coverage of PgLwLock for the new lock mechanisms. I also provided an example implementation in pgrx-examples/parallel_scan_lwlock that executes parallel scans, whose workers access a shared resource (a naive integer counter) as the source of tuples for foreign tables. Parallelism is forced by not letting workers exceed a quota of produced rows (in slow testing environments, the leader may complete the query before the parallel workers finish their startup overhead).

Changes since #2141

The previous PR contained an error in the locks tranche id initialization, that didn't take into account EXEC_BACKEND-enabled builds where any forked worker execs a brand new instance, processing its own _PG_init(), therefore reinitializing the tranche id value. Tests run concurrently evidenced this behaviour on Windows builds.

The test suite was also rewritten to provide a separate lock for each test.

PG19 introduced a breaking change in LWLock C API, therefore the lower layer of LWLocks implementation in this PR required a different code path for stable versions (PG18 and older) and the currently developed one. This may be subject to changes before the release of the stable PG19 version.

…parallel foreign scans

The proposed solution is based on three layers:

* a low level Rust wrapper over PostgreSQL LWLock, not tied to SHMEM hooks;
* a mechanism for requesting a LWLock tranche on startup, stored as a static
  global, based on the existing PgSharedMemoryInitialization trait and the
  pg_shmem_init! macro;
* high level components that are designed to be straightforwardly used in
  PostgreSQL FDW routines (pgrx_*_foreign_scan).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant