You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Connection pool blocked while refreshing hosts state cache? #5789
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.
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?
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?
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.
Activity
vonzshik commentedon Jul 18, 2024
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?
Tommyknocker1982 commentedon Jul 18, 2024
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?
vonzshik commentedon Jul 19, 2024
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.
Treat offline hosts as unpreferred