Skip to content

Let replication handle_disconnect/1 update connection options - #810

Open
palermo-git wants to merge 1 commit into
elixir-ecto:masterfrom
palermo-git:replication-configure
Open

palermo-git wants to merge 1 commit into
elixir-ecto:masterfrom
palermo-git:replication-configure

Conversation

@palermo-git

@palermo-git palermo-git commented Nov 21, 2026 •

Copy link
Copy Markdown

Postgrex.ReplicationConnection reconnects with the options captured at start, so a long-lived replication connection keeps retrying a stale password after a credential rotation. This lets the callback module refresh them:

  • handle_disconnect/1 may return {:noreply, connect_options, state}. The keyword list is merged into the options used for all later connection attempts, so [password: refreshed] is enough. Merged options get the same normalization as start_link/3 (string ports become integers, nil falls back to the default, endpoint keys are replaced as a group). :parameters keeps replication: "database". :auto_reconnect, :reconnect_backoff and :sync_connect are startup-only and ignored. With auto_reconnect: false the process stops after the callback, so returned options are checked but not merged.
  • With auto_reconnect: true, handle_disconnect/1 also runs after each failed connection attempt, before the backoff, including the initial attempt. The state may never have been connected. This is listed under backwards incompatible changes in the CHANGELOG.

Tests (test/replication_reconnect_test.exs, real PostgreSQL, PG 10+): rotation after a live disconnect, recovery after failed attempts (sync and async initial connect, consecutive backoffs), option normalization and endpoint replacement, ignored startup options, invalid returns, and the auto_reconnect: false stop paths.

CI on my fork (this branch plus a trigger commit for push builds): https://github.com/palermo-git/postgrex/actions/runs/45190844985, PG 9.4–18 and lint all green.

@josevalim

Copy link
Copy Markdown
Member

Can you please provide more context here? Which status reports are including the process dictionary? What is logging the output of :sys.get_status?

The reason I ask is because, even if you put it in the configure callback, then if someone can execute code in the system, they will still be able to access the credentials somehow. Also we recently introduced opaque structs to hide credentials and we could use that instead of introducing new callbacks.

@palermo-git

palermo-git commented Nov 22, 2026 •

Copy link
Copy Markdown
Author

Thanks, you're right, and I overstated it in the description. I went back and measured (PG 16, OTP 28, Elixir 1.19.7; run and script below):

  • No status report includes it by default. The options sit in the process dictionary, so they appear in :sys.get_status/1 and in the proc_lib crash report, but Elixir only emits that report with handle_sasl_reports: true, and the console prints the dictionary section only at :debug. The gen_statem terminate report logged by default doesn't contain them.
  • Nothing logs :sys.get_status/1 unless someone calls it.

So that alone isn't worth new API. The reason I'd still like :configure is rotation: credentials are resolved on each connect attempt, so a reconnect after a password change uses the new one instead of retrying the stale one. That's the contract SimpleConnection and DBConnection already have. I'll fix the PR description to say that. If you'd rather keep ReplicationConnection as is, or prefer the opaque struct for the dictionary (it would need the db_connection requirement at ~> 2.10), LMK if you want me to rework or close it.

Run: https://github.com/palermo-git/postgrex/actions/runs/45023077058 · Script: https://github.com/palermo-git/postgrex/blob/postgrex-810-repro/repro.exs

@palermo-git

Copy link
Copy Markdown
Author

A concrete consumer, for context: replicant is a change-data-capture library built on Postgrex.ReplicationConnection. Its replication connections stay up for days and reconnect across database password rotations, so its next major version resolves credentials from a named source on every connect attempt, the same contract :configure gives pooled DBConnection and SimpleConnection users. Today a ReplicationConnection reconnect reuses the options captured at start (Protocol.connect(opts()) in replication_connection.ex), so a rotated password needs a process restart. Opaque structs would hide the stored value but would not fetch a fresh one per attempt. If you'd prefer a different shape for per-attempt resolution, I'm glad to rework the PR around it.

@josevalim

Copy link
Copy Markdown
Member

I am thinking I'd rather figure out a way to change the connection within the running process, since we control the process in this case, than setting it up via configure?

@palermo-git

Copy link
Copy Markdown
Author

That works for us: replicant's connection module is the callback module, so it can resolve credentials itself.

One gap to cover: a reconnect after a live disconnect runs handle_disconnect/1 first, so the callback could refresh the options there. But when Protocol.connect/1 fails, for example an auth failure right after a password rotation, the backoff retry calls Protocol.connect(opts()) again with no callback in between, so it keeps retrying the stale password.

Two shapes I can see:

  1. A function the callback calls from inside the process, e.g. Postgrex.ReplicationConnection.put_connect_opts/1, plus invoking handle_disconnect/1 (or a new optional callback) before each backoff retry.
  2. handle_disconnect/1 may return updated connect options, and it also runs before backoff retries.

Which would you prefer? I'll rework #810 to match.

@josevalim

Copy link
Copy Markdown
Member

Let's try 2.

@palermo-git palermo-git changed the title Configure replication connections before each connect attempt Let replication handle_disconnect/1 update connection options Nov 23, 2026
@palermo-git

Copy link
Copy Markdown
Author

Reworked as option 2:

  • handle_disconnect/1 may return {:noreply, connect_options, state}. The keyword list is merged into the options used for every later attempt, so [password: refreshed] is enough. Merged options get the same normalization as start_link/3 (string ports, nil falls back to defaults, endpoint keys replaced as a group); :parameters keeps replication: "database"; :auto_reconnect, :reconnect_backoff and :sync_connect are ignored. With auto_reconnect: false the process stops, so the options are checked but not merged.
  • With auto_reconnect: true, handle_disconnect/1 now also runs after each failed connection attempt, before the backoff. That includes the initial attempt, so the state may never have connected. I listed it under backwards incompatible changes in the CHANGELOG.
  • :configure is gone.

Tests in test/replication_reconnect_test.exs run against real PostgreSQL: rotation after a live disconnect, recovery after failed attempts (sync, async, and consecutive backoffs), option normalization, and the stop paths.

CI on my fork (this branch plus a trigger commit for push builds): https://github.com/palermo-git/postgrex/actions/runs/45190844985 (PG 9.4–18 and lint, all green). I also merged master to pick up v0.23.0.

handle_disconnect/1 may return {:noreply, connect_options, state}. The options are merged into those used for every later connection attempt, with the same normalization as start_link/3, so a callback module can refresh a rotated password. With auto_reconnect: true, handle_disconnect/1 also runs after each failed connection attempt, before the backoff.
@palermo-git
palermo-git force-pushed the replication-configure branch from 1cfda73 to f443f78 Compare December 1, 2026 16:54
@palermo-git

Copy link
Copy Markdown
Author

Squashed onto current master (daf1530) as one commit. No code change from the previous head: the diff against master is identical to the earlier net diff. CI on my fork (this commit plus a trigger line for push builds) is green on PG 9.4–18: https://github.com/palermo-git/postgrex/actions/runs/46692375858

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.

2 participants