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
The multiple hosts feature was originally implemented in #732. We have the possible following improvements in mind:
Update cluster state with KeepAlive (requires more discussion)
@roji: I think given that keepalive is opt-in only and doesn't support executing commands in the failed transaction state, and that PG14 pushes async notifications on state changes, we don't really need to do this.
Add support for multiple hosts with multiplexing
Add support for multiple hosts without pooling
Preliminarily update the cluster's state on connector's return (once per expiration cycle), useful with postgresql < 14.0
Add a queue for idle connectors (instead of renting from every pool)
@roji: for clarity, this is specifically the case where the primary has gone down and a secondary hasn't yet been fully promoted. In this state - when there's no primary at all - we can attempt to wait for a while until a primary appears rather than throw immediately. This would probably involve repeatedly polling secondary states with the assumption that one of them will become the primary.
Related/similar: when all hosts are down, we probably shouldn't wait for hostCheckSeconds before retrying again
@vonzshik are you planning to work on anything here for 6.0.0? I think what we have is probably more than good enough, we can always improve in 7.0 based on user feedback.
@roji at the very least we should add an API to allow users to manage cluster state cache themselves (it shouldn't be that hard + it can be used as a workaround for some possible bugs).
@JulianRooze nope, there is also the part where we write each query to multiplexing data source. I'm pretty sure we're going to get InvalidCastException here since it's NpgsqlMultiHostDataSource and not MultiplexingDataSource.
@JulianRooze the main problem is that at no point we're actually going to refresh the state for each child pool (because currently it's done only on Open).
@vonzshik would you be able to suggest the way to detect the connection state from an application perspective? I'd like to use multiplexing as well in combination with multiple nodes, manually changing the connection from the application then seems like to best way forward for now?
Hmm, while it's definitely not trivial, it might be doable? There are two main problems: the first one is to determine whether the server is primary/standy, while the second is to mark offline servers as inaccessible.
The first problem is a problem with multiplexing due to the way NpgsqlConnection.Open works: while the first call will actually open a physical connection and check access, that's not true for the next calls.
One way to work around this is by starting a transaction. In that case, you'll get a physical connection from the pool, and because we do not send anything to postgres until the first query, it should be very fast to do per each Open.
Then, you have to check whether the server is primary or standby/readonly. This should be doable via NpgsqlConnection.PostgresParamerers, default_transaction_read_only and in_hot_standby. Every 10 (for example) seconds you instead should send a query to get the actual values from the database.
For the second problem, the main issue is that you can only track errors by wrapping every single query in try/catch. Though, it might be not important since by working around the first problem you will either get errors there, or you'll get a connection to a readonly server.
Activity
roji commentedon Oct 5, 2021
@vonzshik are you planning to work on anything here for 6.0.0? I think what we have is probably more than good enough, we can always improve in 7.0 based on user feedback.
[-]List of QOL changes for the multiple hosts feature[/-][+]Further improvements to multiple hosts[/+]vonzshik commentedon Oct 6, 2021
@roji at the very least we should add an API to allow users to manage cluster state cache themselves (it shouldn't be that hard + it can be used as a workaround for some possible bugs).
roji commentedon Oct 6, 2021
Created issue specifically for manual cluster state control (#4018), and will backlog the rest (this issue).
JulianRooze commentedon Apr 8, 2025
@roji For multiple hosts + multiplexing, I assume this is not as simple as changing this line to
settings.Multiplexing ? new MultiplexingDataSource? 🙂vonzshik commentedon Apr 8, 2025
@JulianRooze nope, there is also the part where we write each query to multiplexing data source. I'm pretty sure we're going to get
InvalidCastExceptionhere since it'sNpgsqlMultiHostDataSourceand notMultiplexingDataSource.npgsql/src/Npgsql/NpgsqlCommand.cs
Lines 1548 to 1549 in cf9d243
JulianRooze commentedon Apr 9, 2025
@vonzshik I see, something like this?
https://github.com/npgsql/npgsql/compare/main...JulianRooze:npgsql:multi-host-multiplex?expand=1
Not proposing this as an actual change, just to get a feeling for the size of the change.
vonzshik commentedon Apr 9, 2025
@JulianRooze the main problem is that at no point we're actually going to refresh the state for each child pool (because currently it's done only on
Open).kvisscher commentedon May 2, 2025
@vonzshik would you be able to suggest the way to detect the connection state from an application perspective? I'd like to use multiplexing as well in combination with multiple nodes, manually changing the connection from the application then seems like to best way forward for now?
vonzshik commentedon May 3, 2025
Hmm, while it's definitely not trivial, it might be doable? There are two main problems: the first one is to determine whether the server is primary/standy, while the second is to mark offline servers as inaccessible.
The first problem is a problem with multiplexing due to the way
NpgsqlConnection.Openworks: while the first call will actually open a physical connection and check access, that's not true for the next calls.One way to work around this is by starting a transaction. In that case, you'll get a physical connection from the pool, and because we do not send anything to postgres until the first query, it should be very fast to do per each
Open.Then, you have to check whether the server is primary or standby/readonly. This should be doable via
NpgsqlConnection.PostgresParamerers,default_transaction_read_onlyandin_hot_standby. Every 10 (for example) seconds you instead should send a query to get the actual values from the database.npgsql/src/Npgsql/Internal/NpgsqlConnector.cs
Lines 641 to 680 in 7320260
For the second problem, the main issue is that you can only track errors by wrapping every single query in try/catch. Though, it might be not important since by working around the first problem you will either get errors there, or you'll get a connection to a readonly server.