Skip to content

Share connections from a pool with different databases in the same PG server #368

Open
@carlopires

Description

@carlopires

When we are using TLS connections to a PG server the cost to keep opened multiple connections to different databases is high. So I'd like to implement database switching to share the connections from a pool with different databases.

pool = await asyncpg.create_pool(database='postgres')

async def handle(queries):
    """Handle incoming queries."""

    results = defaultdict(list)

    for dbname, dbqueries in queries.items():
        async with pool.acquire() as connection:

            connection.set_database(dbname)  # I want this !!!

            async with connection.transaction():
                for query in dbqueries:
                    result = await connection.execute(query)
                    results[dbname].append(result)

Is it feasible with asyncpg?

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