Skip to content

WIP: Add per-process shadow variables for buffer pool synchronization#9

Draft
palak-chaturvedi wants to merge 2 commits into
ashutosh-bapat:dev/shbuf_resizefrom
palak-chaturvedi:dev/synn_bufresize
Draft

WIP: Add per-process shadow variables for buffer pool synchronization#9
palak-chaturvedi wants to merge 2 commits into
ashutosh-bapat:dev/shbuf_resizefrom
palak-chaturvedi:dev/synn_bufresize

Conversation

@palak-chaturvedi

Copy link
Copy Markdown

Introduce LocalCurrentNBuffers and LocalActiveNBuffers as per-process shadow copies of ShmemCtrl->currentNBuffers and
StrategyControl->activeNBuffers respectively. This allows backends to use local copies during normal operation, reducing contention on shared memory, and enables safe mid-operation updates when resize barriers are processed.

Key changes:

  • Add LocalCurrentNBuffers and LocalActiveNBuffers globals, initialized from shared state at backend startup.
  • Replace direct NBuffers usage with LocalCurrentNBuffers/LocalActiveNBuffers in buffer manager, freelist, pg_buffercache, autoprewarm, and access methods (hash, heap, tableam, xlog).
  • Coordinator in pg_resize_shared_buffers() updates its own shadows at each phase and asserts consistency at the end.
  • Each ProcessBarrier*() handler updates the local shadows and logs the transition (old -> new) at LOG level.
  • Add PROCSIGNAL_BARRIER_SHBUF_SHMEM_RESIZE_SHRINK barrier so backends update LocalCurrentNBuffers after shared structures are resized during shrink.
  • Add injection point pg-buffercache-scan-loop inside pg_buffercache scan for testing mid-scan resize synchronization.
  • Remove unimplemented pg_buffercache_lookup_table_entries from the 1.5->1.6 upgrade script.

@palak-chaturvedi palak-chaturvedi force-pushed the dev/synn_bufresize branch 3 times, most recently from 4006885 to 82e5693 Compare May 6, 2026 13:13
Introduce LocalCurrentNBuffers and LocalActiveNBuffers as per-process
shadow copies of ShmemCtrl->currentNBuffers and
StrategyControl->activeNBuffers respectively. This allows backends to
use local copies during normal operation, reducing contention on shared
memory, and enables safe mid-operation updates when resize barriers are
processed.

Key changes:
- Add LocalCurrentNBuffers and LocalActiveNBuffers globals, initialized
  from shared state at backend startup.
- Replace direct NBuffers usage with LocalCurrentNBuffers/LocalActiveNBuffers
  in buffer manager, freelist, pg_buffercache, autoprewarm, and access
  methods (hash, heap, tableam, xlog).
- Coordinator in pg_resize_shared_buffers() updates its own shadows at
  each phase and asserts consistency at the end.
- Each ProcessBarrier*() handler updates the local shadows and logs the
  transition (old -> new) at DEBUG1 level.
- Add PROCSIGNAL_BARRIER_SHBUF_SHMEM_RESIZE_SHRINK barrier so backends
  update LocalCurrentNBuffers after shared structures are resized during
  shrink.
- Add StrategyGetActiveNBuffers() accessor for freelist.c-private state.
- Revert ClockSweepTick() and StrategySyncStart() to use shared atomic
  reads of activeNBuffers instead of LocalActiveNBuffers.  All backends
  in the CAS loop on nextVictimBuffer must use the same modulus to avoid
  inconsistent wrap-around during the barrier propagation window.
  LocalActiveNBuffers is still used for the purely-local trycounter in
  StrategyGetBuffer() and the bounds check in GetBufferFromRing().

- Fix expand path ordering: update shared state (StrategyReset and
  ShmemCtrl->currentNBuffers) before coordinator's local shadows, so
  there is no window where the local view is ahead of shared state.

- Fix trailing whitespace in ProcessBarrierShmemShrink().

- Fix comment alignment for PROCSIGNAL_BARRIER_SHBUF_SHMEM_RESIZE_SHRINK
  in procsignal.h.

- Add detailed comment in GetBufferFromRing() explaining why
  LocalActiveNBuffers is safe for the ring buffer bounds check.

- Update ClockSweepTick() comment to explain why the shared atomic is
  used instead of LocalActiveNBuffers.
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