asyncpg icon indicating copy to clipboard operation
asyncpg copied to clipboard

Multihost bug when working with SqlAlchemy

Open Driim opened this issue 4 years ago • 5 comments

  • asyncpg version: 0.24.0
  • PostgreSQL version: 10
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce the issue with a local PostgreSQL install?:
  • Python version: 3.9.6
  • Platform: Doesn't matter
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes
  • If you built asyncpg locally, which version of Cython did you use?:
  • Can the issue be reproduced under both asyncio and uvloop?: doesn't matter

When we use asyncpg via sqlalchemy, we pass a dsn string to sqlalchemy and it parses this string and call asyncpg connect with host, port, username and etc instead of dsn. And it looks like asyncpg can't handle multiple hosts, separated by commas, passed in host param.

Driim avatar Oct 25 '21 09:10 Driim

self._engine = create_async_engine( dsn, future=True, connect_args={ "statement_cache_size": 0, "dsn": dsn.replace("+asyncpg", ""), "host": None, }, )

It works if I add connect_args, but it looks weird

Driim avatar Oct 25 '21 11:10 Driim

It's likely an SQLAlchemy bug. The host argument must be a list of strings. /cc @CaselIT, @zzzeek

elprans avatar Nov 07 '21 20:11 elprans

@Driim could you provide an example of the dsn you are using?

CaselIT avatar Nov 07 '21 21:11 CaselIT

it's not a "bug" in either project, it's a feature request for SQLAlchemy. We support multiple PostgreSQL hosts using the format described at https://docs.sqlalchemy.org/en/14/dialects/postgresql.html#specifying-multiple-fallback-hosts, and this would need to be implemented for our asyncpg dialect.

zzzeek avatar Nov 07 '21 21:11 zzzeek

also see https://github.com/sqlalchemy/sqlalchemy/issues/6083#issuecomment-831654538 for a fun dive into the history of libpq's URL format (hint: it was inspired at least in part by SQLAlchemy).

zzzeek avatar Nov 07 '21 21:11 zzzeek