Skip to content

Conversation

@jwnx
Copy link

@jwnx jwnx commented Jan 26, 2026

This patch implements a mechanism to detect PostgreSQL primary changes.
When detected, we trigger PgBouncer pod deletions to force connection pool refresh,
preventing stale connections from routing traffic to demoted replicas after failover.

How it works: When a PgBouncer pod is deleted, it receives a SIGTERM,
forcing PgBouncer to enter SHUTDOWN WAIT_FOR_CLIENTS mode. This mode waits
for clients to disconnect for up to a grace period, then K8s sends a SIGKILL. When
PgBouncer restarts, it performs a fresh DNS lookup and connects to the correct
primary.

Note 1: This approach is more effective than the RECONNECT command for session
mode with persistent clients (MPG clusters) because RECONNECT waits for clients
to disconnect, which never happens for persistent clients. SIGTERM guarantees
termination and restart after grace period.

Note 2: For planned switchovers we could consider a PAUSE + RESUME operation,
but it would only work if we know a switchover is going to happen. This patch's
strategy works for both switchovers and failovers.

This strategy was suggested by a PgBouncer maintainer for handling failovers in
Kubernetes: pgbouncer/pgbouncer#1361.

Added a new e2e test.

Signed-off-by: Juliana Oliveira [email protected]

@jwnx jwnx changed the title feat(pgbouncer): faster failovers via primary tracking feat(pgbouncer): trigger reconnection on PostgreSQL primary failover Jan 27, 2026
@jwnx
Copy link
Author

jwnx commented Jan 27, 2026

To test:

  1. Build the image
DOCKER_PUSH=0 VERSION=dev IMAGE=localhost/percona-postgresql-operator:dev make build-docker-image
  1. Load it to your K8s cluster (here I'm using kind)
kind load docker-image localhost/percona-postgresql-operator:dev
  1. Run kuttl (you may need to install it)
IMAGE=localhost/percona-postgresql-operator:dev VERSION=2.6.0 IMAGE_BASE=percona/percona-postgresql-operator IMAGE_POSTGRESQL=percona/percona-postgresql-operator:2.6.0-ppg16.8-postgres IMAGE_PGBOUNCER=percona/percona-postgresql-operator:2.6.0-ppg16.8-pgbouncer1.24.0 IMAGE_BACKREST=percona/percona-postgresql-operator:2.6.0-ppg16.8-pgbackrest2.54.2 kubectl kuttl test --config e2e-tests/kuttl.yaml --test pgbouncer-failover

@jwnx jwnx requested review from senyosimpson and soulware January 27, 2026 18:26
Signed-off-by: Juliana Oliveira <[email protected]>
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