fix: ReplicationPoller consumes slot if publication is not empty#1919
Open
edgurgel wants to merge 2 commits into
Open
fix: ReplicationPoller consumes slot if publication is not empty#1919edgurgel wants to merge 2 commits into
edgurgel wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts the Postgres CDC RLS replication poller lifecycle so it reacts to an empty/non-empty publication by pausing/resuming polling and dropping/recreating the replication slot as publication tables come and go, reducing unnecessary WAL work.
Changes:
- Add
Replications.drop_replication_slot/2and related tests. - Teach
ReplicationPollerto check publication table OIDs periodically and stop polling / drop the slot when the publication becomes empty, then resume when tables reappear. - Add unit + integration coverage for the poller’s empty-publication behavior and slot toggling.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/extensions/postgres_cdc_rls/replication_poller.ex | Adds publication OID tracking and slot drop/recreate behavior tied to publication emptiness. |
| lib/extensions/postgres_cdc_rls/replications.ex | Introduces drop_replication_slot/2 helper for removing logical replication slots. |
| test/realtime/extensions/cdc_rls/replication_poller_test.exs | Updates poller tests and adds coverage for “no tables → no polling” and toggling behavior. |
| test/realtime/extensions/cdc_rls/replications_test.exs | Adds unit tests for drop_replication_slot/2. |
| test/realtime/extensions/cdc_rls/cdc_rls_test.exs | Adds integration tests verifying slot toggling when publication/tables are changed. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+259
to
+263
| defp prepare_replication( | ||
| %{ | ||
| backoff: backoff, | ||
| conn: conn, | ||
| slot_name: slot_name, |
Comment on lines
+227
to
+240
| {n, 0} when n > 0 -> | ||
| cancel_timer(state.poll_ref) | ||
|
|
||
| case Replications.drop_replication_slot(conn, state.slot_name) do | ||
| {:error, reason} when reason != :slot_not_found -> | ||
| # The slot is a temporary logical replication slot tied to this connection, | ||
| # so stopping the process releases it without leaking WAL. | ||
| log_error("DropReplicationSlotFailed", reason) | ||
| {:stop, {:shutdown, :drop_replication_slot_failed}, state} | ||
|
|
||
| _ -> | ||
| cancel_timer(state.check_oid_ref) | ||
| {:noreply, %{state | oids: new_oids, poll_ref: nil, check_oid_ref: schedule_check_oids()}} | ||
| end |
filipecabaco
approved these changes
May 28, 2026
leandrocp
reviewed
May 28, 2026
| {:error, reason} when reason != :slot_not_found -> | ||
| # The slot is a temporary logical replication slot tied to this connection, | ||
| # so stopping the process releases it without leaking WAL. | ||
| log_error("DropReplicationSlotFailed", reason) |
Contributor
There was a problem hiding this comment.
Missing entry in ERROR_CODES.md :)
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 kind of change does this PR introduce?
The goal here is only to establish the replication slot when absolute necessary
Why do we even keep the ReplicationPoller up if there is no publication? If we don't whenever a postgres_changes join happens it will start it again. While this approach will leave this process up and running until it notices that there are no subscriptions (SubscriptionManager does this) and stop everything.