Skip to content

Avoid Repeated Type Introspection Queries in asyncpg with NullPool and PgBouncer (Session Mode) #1242

Closed
@garima-trivedi

Description

@garima-trivedi

asyncpg version: 0.30.0
PostgreSQL version: 16.6
Do you use a PostgreSQL SaaS? If so, which? AWS Postgres Aurora
Can you reproduce: yes

Python version: 3.11

Do you use pgbouncer?: yes
Did you install asyncpg with pip?: yes
If you built asyncpg locally, which version of Cython did you use?: N/A
Can the issue be reproduced under both asyncio and
uvloop?: yes

When using PgBouncer in session mode with asyncpg, setting NullPool in SQLAlchemy (asyncpg) is required to avoid per-process connection pools. However, this causes asyncpg to trigger type introspection queries on every connection checkout from PgBouncer Pool. Even when jit=off, type introspection queries take a few milliseconds each time, adding unnecessary overhead.

Since introspection happens only once per connection, a solution is needed to preload type codecs and prevent repeated introspection queries.

Provide a mechanism to preload type codecs once per connection, ensuring that asyncpg does not re-run introspection queries on every checkout from the PgBouncer pool. This would improve performance when using asyncpg with external connection pooling solutions like PgBouncer.

Would love to hear thoughts from the community on potential solutions or workarounds!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions