#49515·keycloak

Keycloak and Patroni failover and targetServerType=primary on pgBouncer

Author: CojacfarCreated May 29, 2026Updated Sep 30, 2026
Labelskind/enhancementarea/dist/quarkushelp wantedteam/cloud-nativestatus/auto-expirestatus/auto-bumppriority/normal

Before reporting an issue

  • I have read and understood the above terms for submitting issues, and I understand that my issue may be closed without action if I do not follow them.

Area

dist/quarkus

Describe the bug

After a Patroni failover, the newly-promoted node's PgBouncer continues sending in_hot_standby=on in ParameterStatus messages to newly-authenticated clients, even though local Postgres is now primary and pg_is_in_recovery() returns false.

Now pgjdbc closes connections immediately after authentication (visible as 'X' Terminate in PgBouncer verbose logs) and throws Could not find a server with specified targetServerType: primary, even though SHOW transaction_read_only returns off when queried directly through the same PgBouncer.

This causes Keycloak to determine it's not able to reach the Primary PSQL instance that is behind PgBouncer.

This can be remedied immediately by reloading PgBouncer, but this would need to be performed after every failover event before Keycloak is usable. I suspect it's related to this issue.

Setting the DB connection string to include ?targetServerType=any resolves the issue, it seems that =primary is set by default behind the scenes. Probably this would be best included into the documentation to help with any Patroni and Keycloak setups.

Version

26.6

Regression

  • The issue is a regression

Expected behavior

Either targetServerType=primary shouldn't be set by default, or it should handle this situation without interruption. At the least, it should be documented that this behavior still exists.

Actual behavior

Keycloak determines that it cannot reach the Primary DB, and eventually terminates.

How to Reproduce?

  1. Create a Patroni cluster with three nodes including PGBouncer, version 4.1.3 (current latest).
  2. Connection a HA Keycloak deployment
  3. Connect the two with a normal connection string through a load balancer targeting the three pgbouncer instances and handling routing traffic to current primary
  4. Failover from the first master node to any other node
  5. Keycloak will be unable to reestablish a network connection

Overview of Stack: Patroni cluster with HAProxy LB, PgBouncer on each node (pool_mode=session for the affected DB), Keycloak 26.4+ (which sets targetServerType=primary by default in pgjdbc), Postgres 14+ (which emits in_hot_standby via GUC_REPORT)

Anything else?

No response