Repository navigation
Conversation
With several hosts, connect() always tries them in the listed order, so every connection, including each one a pool opens, lands on the first host that accepts it. load_balance_hosts="random" shuffles the address list on every connect() call, like libpq's option of the same name. The default "disable" keeps the listed order. The value is read from the keyword argument, the DSN, the service file or PGLOADBALANCEHOSTS. A load_balance_hosts DSN option is no longer sent to the server as a setting.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With several hosts,
connect()always tries them in the order given. Every connection, including every connection a pool opens, goes to the first host that accepts it. Withtarget_session_attrs="read-only"or"prefer-standby"and several replicas, the first replica in the list gets all the load and the others sit idle.This PR adds
load_balance_hosts, the libpq option of the same name (PostgreSQL 16+):"disable"(default): hosts are tried in the order listed, as today."random": on everyconnect()call the address list is shuffled before the usual host loop runs. Failover andtarget_session_attrswork as before; only the order changes. A pool callsconnect()for each new connection, so pool connections end up spread across the hosts.The value is resolved like
target_session_attrs: keyword argument, thenload_balance_hostsin the DSN, then the service file, thenPGLOADBALANCEHOSTS, then"disable". Other values raiseClientConfigurationError. Aload_balance_hostsDSN option is no longer sent to the server as a setting. Today it fails withUndefinedObjectError.Why in asyncpg:
connect=forcreate_pool(), or anasync_creatorfor SQLAlchemy's asyncpg dialect, that reorders the hosts before each call. Downstream projects already do this (for example modern-python/db-retry).target_session_attrs,service,krbsrvnameandgsslib. A DSN that load-balances with psql and psycopg works the same here.libpq also shuffles the addresses a single host name resolves to. asyncpg leaves DNS resolution to the event loop's
create_connection(), so this PR shuffles only the (host, port) entries.Tests:
TestConnectParams: default, DSN, keyword argument, environment variable, service file and invalid values.TestLoadBalanceHosts: withrandomthe hosts are tried in the shuffled order and the order is drawn again on eachconnect(). With the default ordisable, the listed order is kept andrandomisn't called.tests.test_connecton Python 3.10. flake8 and mypy are clean.