Skip to content

Keycloak and Patroni failover and targetServerType=primary on pgBouncer #49515

Description

@Cojacfar

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

Activity

  1. added theissue type on May 29, 2026
  2. added
    kind/bug
    on May 29, 2026
  3. added
    area/dist/quarkus
    on May 29, 2026
  4. ahus1 commented on Jun 4, 2026

    @ahus1
    Member

    I'm sorry this is causing you trouble.

    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.

    The behavior is documented here: https://www.keycloak.org/server/db#_writer_and_reader_instances

    Given the link you provided, this seems to be a long-standing issue with pgBouncer. While I'm sorry to see this, the targetType parameter serves as an important protection to avoid connections to a read replica on switchover when people use for example DNS or other routing mechanisms.

    My understanding of pgBouncer is limited, so all I could do is AI for workarounds, but I wouldn't be able to judge which suggestions would be useful.

    Could there be a workaround to have an instance of pgBouncer to exclusively provide connections to the primary node? Reading the issue, the observed behavior seems to be caused by pgBouncer caching some information from a connection that was opened to a standby instance before the switchover. I'm also surprised that the node promoted to primary would keep its (read-only) connections open, or demoted now read-only node would keep any connections open.

    AFAIK there is no means of Keycloak to detect pgBouncer / this behavior.

  5. Cojacfar commented on Jun 10, 2026

    @Cojacfar
    Author

    I definitely agree the root of this issue is on the pgBouncer side. Thanks for the documentation. I was surprised to see the issue pop up and took a while to figure out exactly what was going weird!

  6. 36 remaining items

  7. self-assigned this
    on Sep 30, 2026
  8. added a commit that references this issue on Sep 30, 2026
    c0ac727
  9. nils-jakobs commented on Oct 1, 2026

    @nils-jakobs

    @atiqur-rahman-pro Hey, we already used the targetServerType=any. But if you have a HA psql DB with for example two pods you would have one primary and one secondary. If you choose targetServerType=any the chance is 50/50 that he will choose the secondary pod which is read-only. I could watch this a couple of times on our clusters. After a node drain or something like that where the DB pods restarted and also the primary/secondary switched we had problems with keycloak.
    or doesnt make it sense? :D

  10. atiqur-rahman-pro commented on Oct 1, 2026

    @atiqur-rahman-pro
    Contributor

    Hi @nils-jakobs, yes, that makes sense. targetServerType=any disables the JDBC driver's primary check; it does not ensure that connections go to the writable node. If PgBouncer can route to both the primary and a read-only standby, Keycloak may connect to the standby after a restart or failover.

    So targetServerType=any is only a safe workaround when PgBouncer or the load balancer routes Keycloak exclusively to the current primary. Otherwise, the primary check should stay enabled and the failover path needs to ensure PgBouncer reports the new node's correct state. Your experience is useful context for the documentation.

  11. ahus1 commented on Oct 1, 2026

    @ahus1
    Member

    I would like to understand why people are using pgBouncer with Keycloak. With that bug open for years, I might recommend not to use pgBouncer, at least not with Keycloak. Comments welcome.

  12. atiqur-rahman-pro commented on Oct 1, 2026

    @atiqur-rahman-pro
    Contributor

    Hi @ahus1, I can’t speak for every deployment, but one reason teams use PgBouncer is to manage PostgreSQL connections across multiple Keycloak replicas, especially when their database operator provides or recommends a pooler. The benefit depends on the pool mode and workload; for example, session pooling does not multiplex connections in the same way as transaction pooling.

    I agree that PgBouncer adds failover complexity.

    If it is used with Keycloak, the routing needs to keep Keycloak connected to the writable primary, and targetServerType=any is not a safe general workaround when standby nodes may also receive connections. A note in the DB guide about these trade-offs would help operators make an informed choice.

  13. ahus1 commented on Oct 2, 2026

    @ahus1
    Member

    I've created a follow-up issue #53452 to continue the discussion, and also extend it to AWS RDS Proxy.

    My current gut-feeling: Don't use a DB proxy for Keycloak, instead size your connection pool on each instance.

  14. merwan commented on Oct 2, 2026

    @merwan

    We had an issue somewhat related to this with a setup involving a pgpool between Keycloak and two PSQL servers set as primary/replica .

    We solved it after upgrading to Keycloak 26.4+ by passing the JDBC connection parameter: targetServerType=preferPrimary

    The value preferPrimary tries to connect to primary if it is available, otherwise allows falls back to connecting to secondaries available.

  15. added a commit that references this issue on Oct 2, 2026
    96da5cc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/dist/quarkusbackport/26.8help wantedkind/enhancementpriority/normalrelease/27.0.0team/cloud-native

Fields

Value

None yet

Effort

None yet

Votes

None yet

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions