Skip to content

Split a comma-joined host list into a sequence for asyncpg - #2267

Closed
afonsojanu wants to merge 1 commit into
tortoise:developfrom
afonsojanu:fix/asyncpg-multi-host-dsn-1924
Closed

afonsojanu wants to merge 1 commit into
tortoise:developfrom
afonsojanu:fix/asyncpg-multi-host-dsn-1924

Conversation

@afonsojanu

Copy link
Copy Markdown

Description

A postgres DB URL with more than one host, e.g. postgres://user:pass@host1,host2:5432/db, is libpq's own multi-host syntax for primary/replica failover. urlparse reads the whole comma-joined string back as a single hostname, so asyncpg.connect() ended up being asked to resolve host1,host2 as one DNS name and failed with a plain socket.gaierror instead of trying each host in turn.

expand_db_url now splits the hostname into a list when it contains a comma and the backend is one of postgres/postgresql/asyncpg, since that's the form asyncpg's own host parameter accepts for exactly this case (it documents accepting "a sequence" of hosts, tried in order). psycopg is left untouched: it forwards its kwargs straight to libpq, which already parses the joined string correctly on its own, so splitting it there would just make libpq see a Python list where it expects the original string.

Motivation and Context

Fixes #1924

How Has This Been Tested?

Added test_postgres_multi_host to tests/backends/test_db_url.py, covering all four postgres-family schemes (postgres, postgresql, asyncpg, psycopg) against the same multi-host URL, asserting the asyncpg-family schemes get a list and psycopg keeps the joined string. Confirmed via git stash that it fails against the unmodified code (asserts a list where the code still returns the joined string) and passes with the fix.

  • python -m pytest tests/backends/test_db_url.py tests/test_connection.py: 59 passed.
  • ruff check and ruff format --check: clean.
  • mypy on the changed file: 0 errors.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

A postgres DB URL with more than one host, e.g. host1,host2:5432, is
libpq's own multi-host connection string syntax for primary/replica
failover. urlparse reads the whole comma-joined string back as a
single hostname, so asyncpg.connect() ended up being asked to resolve
"host1,host2" as one DNS name and failed with a plain gaierror instead
of trying each host in turn.

psycopg forwards its kwargs straight through to libpq, which already
understands the joined string on its own, so only the asyncpg path
needed the split into an actual list of hosts, which is the form
asyncpg's own host parameter accepts for this exact case.
@codspeed

codspeed Bot commented Sep 1, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing afonsojanu:fix/asyncpg-multi-host-dsn-1924 (905a7ee) with develop (a324edc)

Open in CodSpeed

@waketzheng

Copy link
Copy Markdown
Contributor

Thanks for this fix — the root cause analysis is clear and the implementation is minimal and correct.

The bug was that urlparse returns the comma-joined string as a single hostname, so asyncpg.connect() tried to resolve host1,host2 as one DNS name. Splitting it into a list only for the asyncpg-family schemes is the right call, and the comment explaining why psycopg is left alone is helpful.

Test coverage looks solid: test_postgres_multi_host covers all four postgres-family schemes and asserts the list-vs-string distinction correctly, and you've confirmed it fails on unmodified code.

A few non-blocking notes:

1. Per-host ports. The current implementation handles the case where the port appears only on the last host (host1,host2:54321). libpq's multi-host syntax also allows different ports per host (host1:5433,host2:5432), and urlparse may not parse that netloc correctly. If that's a known limitation or planned for a follow-up, a sentence in the PR description would help.

2. db_backend check vs engine check. db_backend in ("postgres", "postgresql", "asyncpg") mirrors the DB_LOOKUP aliases, which is correct today. Checking db["engine"] == "tortoise.backends.asyncpg" instead would be more robust — it keys off the actual driver rather than the scheme name, and won't need updating if a new postgres-family scheme alias is added later.

3. Docs. The checklist says documentation was updated, but I only see code and test changes in the diff. If the doc change is in another commit, fine — just confirming it's included.

CI is green and CodSpeed shows no impact. Nice work.

@afonsojanu

Copy link
Copy Markdown
Author

Closing this for now as part of a clean slate — stepping back from everything I had open across these repos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Postgres connection string (dsn) with multi-host does not work

2 participants