Repository navigation
Description
Since #60, every subscription claim that loses still writes the subscription's row and then rolls it back. Storage.Lock.claim_subscription_lock/3 runs bump_lock_generation (UPDATE subscriptions SET lock_generation = lock_generation + 1) before pg_try_advisory_lock in one transaction, and rolls the transaction back when the advisory lock is already taken. At 1f9d1f8 the claim was one statement whose UPDATE ran only when pg_try_advisory_lock returned true, so a losing claim wrote nothing.
Every standby spends a transaction ID per subscription per retry
Severity: moderate at scale, nothing fails: extra transaction IDs and vacuum work. Likelihood: certain whenever more than one node subscribes to the same subscriptions.
NodeA owns subscription S and NodeB is the standby. NodeB retries its claim every subscription_retry_interval (60 s by default), and each retry now updates S's row, finds the lock taken and rolls back. No fault is needed: when several nodes subscribe to the same persistent subscription, each standby rolls back an update on every retry. With 7 nodes that all subscribe to the same 300 subscriptions and a 60 s interval, that is about 30 rolled-back updates per second, or about 2.6 million transaction IDs a day.
The cost that grows with nodes and subscriptions is transaction ID consumption, one per losing claim, on top of what the application already uses. At 2.6 million a day, these claims alone use up 200 million transaction IDs, the default autovacuum_freeze_max_age, in about 77 days, which ages every table on the server toward an anti-wraparound vacuum, the event tables included, even tables that get no writes.
How I measured it
I measured it on PostgreSQL 18.6 with two event store instances on one database: NodeA holding 50 subscriptions, and NodeB subscribing to the same 50 with subscription_retry_interval: 1000, sampled over 60 s windows.
-
At a2ce825, 3 of 3 runs: 2950 updates and 2950 rollbacks on
subscriptionsper window (49.2 per second, all HOT), andtxid_current()advanced 49.3 to 49.6 per second, against 0.1 to 0.5 with NodeA alone. -
At 1f9d1f8, 3 of 3 runs: 0 updates and 0 rollbacks, and
txid_current()advanced by 1 per window, while commits rose by 2957 to 3007 per window, so NodeB was retrying at the same rate. -
WAL and dead tuples stayed small at a2ce825: server-wide WAL ran at 5.8 to 6.1 KB/s during the retries; with NodeA alone it was 0.9 to 2.2 KB/s in two runs and 235 KB/s in the third, whose baseline window had the same updates and commits as the others, so that WAL came from something else on the server.
n_dead_tupended at 52 to 72, and autovacuum ran onsubscriptionsonce per window. At 1f9d1f8 the retry window added 96 bytes of WAL in 60 s.
A standby that dies mid-claim stalls the owner's checkpoints
Severity: high when it happens: the owner stops saving the subscription's position for up to about 2 h. Likelihood: rare: a standby must die within about two round trips of a losing claim.
NodeB's claim holds the row lock on S's row from the UPDATE until its ROLLBACK arrives, about two round trips later (the pg_try_advisory_lock query, then the ROLLBACK). If NodeB's host dies, or its network drops without a RST, inside that window, its backend stays idle in transaction with the row lock. NodeA's checkpoint UPDATE waits on it: the ack/2 calls of NodeA's subscribers exit with :timeout after 5 s, and after the 15 s query timeout the checkpoint tries again on a new connection and waits again. This lasts until the server closes NodeB's session through TCP keepalive (about 2 h 11 min by the Linux defaults of 7200 s plus 9 probes 75 s apart) or idle_in_transaction_session_timeout, which is off by default.
This one needs a standby's host to die, or its network to drop without a RST, within those two round trips of a losing claim, so it is rare. When it does happen, a healthy owner stops checkpointing for as long as the dead session lives: NodeA cannot save S's position while the row lock remains, and in my three proxy runs both of its ack/2 calls exited with :timeout and the position was saved only after the proxy healed.
How I reproduced it
The window is short, so I held it open by hand in two ways:
-
I ran the claim statements of a2ce825 on a separate connection (the bounded lock wait,
bump_lock_generation, then the try lock, which returned false) and left the transaction open. NodeA's ack exited with:timeoutafter 5001 ms, and its backend was waiting onLock/transactionidin the checkpointUPDATEbehind the idle claim. -
I put NodeB's session pool behind a TCP proxy, made a trigger delay the bump by 2 s, and froze the proxy during that delay (3 of 3 runs). NodeB's session showed
idle in transaction, both of NodeA's acks exited with:timeoutafter 5001 to 5015 ms, and 30 s after the freeze NodeA's checkpointUPDATEwas still waiting.last_seenwas written 4 to 103 ms after the proxy healed.
At 1f9d1f8 the same steps left the row alone: with the claim held open by hand the ack returned :ok in 1 ms, and in 3 of 3 proxy runs the losing claim never touched the row, the first ack returned :ok in 1 ms and the repeated ack returned {:error, :unexpected_ack} in 0 ms.
Activity