Skip to content

fix(realtime): wait for postgres_changes system ready before emitting SUBSCRIBED - #2029

Closed
7ttp wants to merge 2 commits into
supabase:masterfrom
7ttp:fix/realtime-subscription-race-condition
Closed

7ttp wants to merge 2 commits into
supabase:masterfrom
7ttp:fix/realtime-subscription-race-condition

Conversation

@7ttp

@7ttp 7ttp commented Jan 17, 2026

Copy link
Copy Markdown
Member

Summary

Fixes a race condition where SUBSCRIBED status is emitted before PostgreSQL logical replication is ready,
causing early database writes to miss postgres_changes events.

Problem

The Realtime subscription has a two-stage backend process:

  1. Stage 1 (Fast): Phoenix WebSocket channel join completes
  2. Stage 2 (Slow): PostgreSQL replication slot creation and stream initialization

Previously, the client emitted SUBSCRIBED after Stage 1, but the replication stream wasn't ready until Stage 2 completed. Any database writes in this window (~1-3 seconds) would not trigger postgres_changes events.

Solution

Wait for the server's system message { extension: 'postgres_changes', status: 'ok' } before emitting SUBSCRIBED when postgres_changes bindings are present. This message is sent by the server after Realtime.Tenants.ReplicationConnection confirms the replication stream is active.

For channels without postgres_changes (broadcast/presence only), behavior is unchanged.

Changes

  • Store subscribe callback and track pending system confirmations
  • Add _maybeEmitSubscribed() that only fires when all confirmations received
  • Handle system message in _trigger() to clear pending state
  • Updated existing lifecycle tests to include system message trigger.

Related

@7ttp
7ttp requested review from a team as code owners January 17, 2026 21:03
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 81.014% (+0.02%) from 80.997%
when pulling 17add66 on 7ttp:fix/realtime-subscription-race-condition
into 09aa106 on supabase:master.

@mandarini

Copy link
Copy Markdown
Contributor

@7ttp I know you and @edgurgel discussed this internally, so I am putting a "do not merge" label

@mandarini mandarini added the do-not-merge Do not merge this PR. label Jan 22, 2026
@mandarini

Copy link
Copy Markdown
Contributor

@7ttp @edgurgel what is the status here? Should we keep open / should we close?

@edgurgel

edgurgel commented Jun 4, 2026

Copy link
Copy Markdown
Member

Hey @mandarini, funnily enough we were discussing this issue last week and I think we found a good way forward here that is good enough:

We think that we could change so that IF a channel has ONLY postgres changes (so no broadcast and presence on callbacks) we could then only consider SUBSCRIBED then the system ready message arrives.

Thoughts? @7ttp

@7ttp

7ttp commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

Hey @mandarini, funnily enough we were discussing this issue last week and I think we found a good way forward here that is good enough:

We think that we could change so that IF a channel has ONLY postgres changes (so no broadcast and presence on callbacks) we could then only consider SUBSCRIBED then the system ready message arrives.

Thoughts? @7ttp

looks good!!
should I put up a pr for that? :)

@edgurgel

Copy link
Copy Markdown
Member

looks good!! should I put up a pr for that? :)

Yes! That would be great ❤️

@mandarini

Copy link
Copy Markdown
Contributor

@7ttp should we close this PR, and you will open new one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge Do not merge this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Premature 'SUBSCRIBED' status for postgres_changes signals incorrect readiness

4 participants