Repository navigation
fix(store): forward each Postgres notification as soon as it arrives - #6857
Open
cjorge-thegraph wants to merge 1 commit into
Open
cjorge-thegraph wants to merge 1 commit into
cjorge-thegraph wants to merge 1 commit into
Conversation
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
marked this pull request as ready for review
December 1, 2026 16:37
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What happens
NotificationListenerreads Postgres notifications on a worker thread and forwards them to its receiver. At master (b87b4c0),store/postgres/src/notification_listener.rs:218-238collects them like this: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
.collect()into aVec, so that the notifications are read before any of them is parsed. Parsing a large notification runs a query on the same connection. The comment at lines 208-212 still says this. Before,timeout_iter(500ms)only bounded how long the loop went without checking whether it should stop..take(64)"to guarantee progress on a busy system".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:
GRAPH_CHAIN_HEAD_WATCHER_TIMEOUT(chain head listener: Ensure periodic updates #2725);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:
no chain head update for 30 secondslogs" on a multi-chain node. The logs that remained are what this batching produces.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:
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. Thecrit!log is still written once per connection loss.chain_head_updatesandstore_eventsreceivers 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.NotificationListenerandSafeChannelNameare exported throughlayout_for_tests, andtest_store::shard_connection_urlgives 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.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 throughlarge_notificationsand 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 onstore_events, reach aSubscriptionManagersubscriber in order. It passes on master and with the fix.The whole
test-storepostgrestest binary passes with the fix: 116 passed, 0 failed. That includes the chain head tests inchain_head.rs, and the store it builds runs the chain head listener and the store event listener.cargo fmt --all --checkandcargo clippy -p graph-store-postgres -p test-store --all-targets -- -D warningsare clean.Not fixed here
A block stream woken by a chain head notification reads the head through
ChainStore::chain_head_ptr(), which serves it fromChainHeadPtrCache(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