PgQue icon indicating copy to clipboard operation
PgQue copied to clipboard

fix(partition-keys): make claim_slot non-blocking under concurrent lease DML

Open NikolayS opened this issue 4 months ago • 1 comments

Severity

Medium — worker liveness / head-of-line blocking.

Problem

The partition-key contract says a busy claim_slot returns NULL so a worker can immediately try another slot. The implementation uses blocking SELECT ... FOR UPDATE on pgque.partition_slot.

If another transaction is claiming, renewing, receiving, or acknowledging that slot, the caller waits on the row lock instead of being steered onward.

Reproduction

  1. Session A begins, calls claim_slot, and leaves the transaction open.
  2. Session B sets statement_timeout = '750ms' and calls claim_slot for the same slot.

Observed:

ERROR: canceling statement due to statement timeout
CONTEXT: while locking tuple ... in relation "partition_slot"

Affected implementation: devel/sql/pgque-api/partition_keys.sql, claim_slot.

An infinite lease TTL is also currently accepted on PostgreSQL 19, which can make a crashed slot permanently unavailable.

Expected fix

Use a non-blocking row-lock transition (FOR UPDATE SKIP LOCKED / NOWAIT or equivalent) and return NULL for a currently locked/busy slot while still distinguishing an unsubscribed slot. Reject non-finite lease TTLs.

Add a real two-session regression with an uncommitted holder and a bounded assertion that the second claim returns promptly.

NikolayS avatar Jul 10 '26 09:07 NikolayS

Shipped in v0.3.0-rc.2 (merged to main via #395 at f4c7457): https://github.com/NikolayS/PgQue/releases/tag/v0.3.0-rc.2

NikolayS avatar Nov 21 '26 16:11 NikolayS