Skip to content

fix(store): forward each Postgres notification as soon as it arrives - #6857

Open
cjorge-thegraph wants to merge 1 commit into
graphprotocol:masterfrom
cjorge-thegraph:cjorge-thegraph/notification-dispatch
Open

cjorge-thegraph wants to merge 1 commit into
graphprotocol:masterfrom
cjorge-thegraph:cjorge-thegraph/notification-dispatch

Conversation

@cjorge-thegraph

Copy link
Copy Markdown

What happens

NotificationListener reads Postgres notifications on a worker thread and forwards them to its receiver. At master (b87b4c0), store/postgres/src/notification_listener.rs:218-238 collects them like this:

let notifications: Vec<_> = conn
    .notifications()
    .timeout_iter(Duration::from_millis(500))
    .iterator()
    .take(128)
    .filter_map(|item| match item { /* ... */ })
    .filter(|notification| notification.channel() == channel_name.0)
    .collect();

and only then, at lines 241-287, parses and forwards them. timeout_iter(500ms) ends only after 500 ms without a notification, so .collect() returns after 128 notifications or after a quiet gap of 500 ms, whichever comes first. A notification is not forwarded until that batch ends. On a channel that receives notifications more often than about twice a second, the quiet gap never comes and every notification waits until 128 have piled up.

All chains share one channel: chain_head_updates (store/postgres/src/chain_head_listener.rs:25-26). One chain that notifies fast enough, for example a chain with sub-second blocks, therefore holds back the chain head notifications of every other chain on the node. Block streams of RPC chains are woken by those notifications; they stall for seconds and then catch up in a burst. In a multi-chain deployment where the channel carried about 8 notifications a second, deliveries came in steps of exactly 128 about 15 s apart, and subgraphs on RPC chains followed the chain head in bursts.

How it arose

The first release with the batching is v0.23.0 (2021-06-18), which carries both 102c8d4 and 63ae38e. v0.22.0 still forwarded each notification as it arrived. Every release since has the batching.

Reading everything before parsing is still needed. Waiting for a quiet gap is not.

Related reports

Since 2021 the chain head path has gained several workarounds for late or missing chain head updates. Each was framed as lost notifications, a race or a tokio deadlock, never as batching:

The symptom this fix addresses is bounded: a stream waits at most until a batch fills or the 30 s fallback fires, then catches up. Reports where the stored chain head itself was far behind have other causes, in the ingestor or the provider. Two reports match the mechanism:

Neither can be confirmed without the reporters' logs.

The fix

The collection moves into receive_notifications. It waits up to 500 ms for the first notification, so the worker still checks the terminate flag at least that often. Then it takes the notifications that have already arrived, without waiting for more, and stops at 128. As before, a pass is read in full before anything is parsed, and each notification is forwarded on its own.

If reading hits an error, notifications the client already has queued are taken too: exactly as many as the queue holds, which only pops the queue and never reads the failed connection again. They are forwarded before the reconnect.

Behaviour changes, from reading the old and new code side by side:

  • Latency: a notification is forwarded as soon as the worker reads it. Before, it waited until 128 had arrived or the channel had been quiet for 500 ms.
  • Termination latency: the worker now checks the terminate flag after at most 500 ms of waiting plus the time to forward one pass. Before, one batch could keep reading for up to about 128 × 500 ms (about 64 s) on a channel that stayed just under one notification every 500 ms, so dropping the listener could block for that long.
  • Empty channel: no change. Each pass waits 500 ms, returns nothing and loops, as before. There is no busy loop.
  • Connection error mid-pass: notifications received before the error, including the ones the client queued while reading up to it, are forwarded, then the worker reconnects. That matches the old outcome. The difference is that the old loop kept calling next() after an error until .take(128) was used up, so it consumed up to 128 error items. The new code stops at the first error. The crit! log is still written once per connection loss.
  • 128 cap: it still bounds how many notifications one pass collects and parses between two terminate checks. It no longer ends a waiting period, because there isn't one: notifications beyond the cap stay queued in the client and the next pass takes them without waiting.
  • Ordering: unchanged. Notifications are forwarded in the order the client received them, across passes and across a connection error. That includes notifications that arrive while a large notification is being looked up.
  • Closed receiver: when the receiver has been dropped, the worker now leaves its loop and ends, closing its connection. Before, it left only the batch and went on reading and listening until the listener was dropped, logging "DB notification listener closed" once per batch. Without this change it would log that once per pass, which is now usually once per notification.
  • Consumers: chain_head_updates and store_events receivers get notifications spread out in time instead of in bursts. Each notification still arrives as a separate item on the same mpsc channel, so the consumers needed no changes.

Tests

New tests in store/test-store/tests/postgres/notification_listener.rs. NotificationListener and SafeChannelName are exported through layout_for_tests, and test_store::shard_connection_url gives the tests the primary's URL.

  • steady_notifications_are_dispatched_without_waiting_for_a_batch: sends 60 notifications, one every 50 ms (about 3 s, never a 500 ms gap), and records when each one reaches the receiver. It requires the first to arrive before the last is sent, a median delay under 100 ms, and every delay under 1.5 s.
    • On master it fails in all 4 runs: "the first notification arrived 502.471358ms after the last was sent". In three runs with the delays printed, no notification out of 60 arrived before the last was sent. The first was delivered 3.45 s after it was sent, the median delay was 2.00 s, p90 3.20 s, maximum 3.45 s and minimum 0.50 s. Delays fall in 50 ms steps from 3.45 s to 0.50 s: the whole run is delivered in one batch, 500 ms after the last notification.
    • With the fix, over three runs: median 1.2 to 2.5 ms, p90 3.0 to 10.3 ms, maximum 9.1 to 19.7 ms. 59 of 60 arrived before the last was sent, and the last arrived 0.7 to 1.5 ms after it was sent. The first was delivered 1.1 to 1.9 ms after it was sent.
    • All runs were on a shared machine with a load average of 40 to 63 on 32 CPUs.
  • notifications_received_with_a_connection_error_are_delivered: fills the receiver's channel so that the worker waits to hand over a notification. It then sends three more and terminates the listener's backend, so that the three notifications and the error reach the client together. All 104 must arrive in order, and notifications must flow again after the reconnect. It passes on master and with the fix. With the fix but without taking the queued notifications on error, it fails: 0 to 100 arrive, and 101 to 103 are lost.
  • large_notifications_are_delivered_in_order: notifications of more than 7800 bytes, which go through large_notifications and need a second query on the listener's connection, alternate with small ones. All arrive intact and in order. It passes on master and with the fix.
  • store_events_reach_subscribers: three store events, the middle one large, sent on store_events, reach a SubscriptionManager subscriber in order. It passes on master and with the fix.

The whole test-store postgres test binary passes with the fix: 116 passed, 0 failed. That includes the chain head tests in chain_head.rs, and the store it builds runs the chain head listener and the store event listener. cargo fmt --all --check and cargo clippy -p graph-store-postgres -p test-store --all-targets -- -D warnings are clean.

Not fixed here

A block stream woken by a chain head notification reads the head through ChainStore::chain_head_ptr(), which serves it from ChainHeadPtrCache (store/postgres/src/chain_store.rs:2202-2203, checked first at 2731-2734) until the TTL expires. A notification does not expire that cache, so a woken stream can still read the previous head and go back to waiting. That is a separate issue and this PR leaves it alone.

🤖 Generated with Claude Code

NotificationListener collected notifications with
timeout_iter(500ms).take(128) and forwarded them only once that batch
ended: after 128 notifications or after 500ms without one. On a channel
that is never quiet for 500ms every notification waited for 128 to pile
up. chain_head_updates is shared by all chains, so one chain notifying
more than about twice a second delayed the chain head notifications of
every other chain by seconds, and their block streams advanced in bursts.

The worker now waits up to 500ms for the first notification, so that it
still checks regularly whether to stop, then takes those already
received, at most 128, without waiting for more, and forwards each. A
pass is still read in full before it is parsed, since looking up a large
notification needs the connection. Notifications queued in the client
when reading fails are forwarded before the reconnect, and the worker
stops once the receiver has been dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cjorge-thegraph
cjorge-thegraph marked this pull request as ready for review December 1, 2026 16:37
@cjorge-thegraph cjorge-thegraph changed the title fix(store): Forward each Postgres notification as soon as it arrives fix(store): forward each Postgres notification as soon as it arrives Dec 3, 2026

This branch has not been deployed

No deployments
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