Skip to content

Connection pool blocked while refreshing hosts state cache? #5789

Description

@Tommyknocker1982

Steps to reproduce

Add a dummy host to the beginning of the host list in the connection string, then continually make queries which require a connection from the pool, and you'll see a delay every 10 seconds.

The issue

We’ve spent a couple of days troubleshooting a performance issue while connecting to a PostgreSQL cluster from .NET, and believe it might be due to a bug, or possible improvement which could be made in the Npgsql library.

If you have multiple hosts defined in your connection string, then by default, Npgsql attempts to refresh its hosts state cache every 10 seconds (configurable via the ‘Host Recheck Seconds’ connection string parameter). We are seeing that during this time, connections are blocked from being released from the pool (or possibly the pool is emptied?), so queries cannot be performed. If the first host in the list is unavailable for any reason, then this refresh could take a significant amount of time before it gives up and tries the next host. Usually a couple of seconds, but we’ve seen some delays of 10 seconds or more.

So rather than testing hosts in the order defined in the connection string, wouldn’t it make more sense to test a known, working host first? In our case, the NpgsqlDataSource is defined using TargetSessionAttributes.Primary, so all it needs to do is re-test the current primary host (and only look at the others if that host is no longer primary). Instead, we’re seeing long blocks every 10 seconds while Npgsql keeps testing the first (unavailable) host.

Further technical details

Npgsql version: 8.0.3
PostgreSQL version: 16.3
Operating system: Win10

Activity

  1. vonzshik commented on Jul 18, 2024

    @vonzshik
    Contributor

    From what I remember, the original design was to specifically preserve the order because users might want to specify servers in the accessibility order (as in, the first one is the closest, then the one that's farther and etc). But yeah, I can see why it can be an issue if the only thing you want is to always connect to primary (which is always a single instance).
    We already have #3610 which is about opening connections in parallel but I do wonder whether it does make sense to additionally allow to move failed hosts to the back of the list so we try them the last.
    @roji what do you think?

  2. Tommyknocker1982 commented on Jul 18, 2024

    @Tommyknocker1982
    Author

    Yes, I can see why you might want to specify them in order of preference, it makes sense now.

    What doesn't make sense is why new connections are prevented while this periodic check takes place? It's extremely unlikely that the status of any of the hosts has changed within the last 10 seconds. So why not let the currrent connection pool operate normally until all the hosts have been tested? If there are no changes to the hosts then you don't need to reset the pool at all, and don't need to block any connections. That would give the best performance, I think?

  3. vonzshik commented on Jul 19, 2024

    @vonzshik
    Contributor

    That's because periodic check isn't done on a separate thread (via a timer) but instead at the moment we try to get a connection.
    At the time I didn't take into account that a host could be down for a long time and instead opted for a simpler solution rather than add another thread. While one argument for this was to not have a timer per each pool (and there could be a lot of them with multi-tennant), another was due to newer version of PG having an ability to send the cluster's state (that is whether it's a primary or a standby) on physical open and each time it changes. So ideally we wouldn't ever need to check the state for as long as each host is online. It didn't really worked out the way I hoped but that's a different thing altogether.
    I'm not entirely opposed to changing host state check logic to a timer, just have to think about it a bit more.

  4. self-assigned this
    on Aug 1, 2024
  5. added this to the 8.0.4 milestone on Aug 1, 2024
  6. added a commit that references this issue on Aug 1, 2024
    407ef89
  7. modified the milestones: 8.0.4, 8.0.5 on Sep 10, 2024
  8. modified the milestones: 8.0.5, 8.0.6 on Oct 13, 2024
  9. modified the milestones: 8.0.6, 8.0.7 on Nov 18, 2024
  10. modified the milestones: 8.0.7, 8.0.8 on Feb 24, 2025
  11. added theissue type on Jun 14, 2025
  12. modified the milestones: 8.0.8, Backlog on Oct 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions