tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.5k stars 368 forks source link

Selecting schema not worked with pgbouncer #346

Open skyline-gleb opened 4 years ago

skyline-gleb commented 4 years ago

Describe the bug Get error from asyncpg when set custom schema in connection credentials: asyncpg.exceptions.ProtocolViolationError: unsupported startup parameter: search_path

To Reproduce CREATE SCHEMA IF NOT EXISTS notpublic

await Tortoise.init(
    {
        "connections": {
            __name__: {
                "engine": "tortoise.backends.asyncpg",
                "credentials": {
                    **credentials,
                    "schema": "notpublic"
                }
            }
        },
        "apps": {"models": {"models": [__name__], "default_connection": __name__}},
    },
)

Expected behavior Successfully init

Additional context Support schema added in https://github.com/tortoise/tortoise-orm/pull/172 and changed in https://github.com/tortoise/tortoise-orm/pull/229

grigi commented 4 years ago

Seems that asyncpg + pgbouncer doesn't work well together unless using pool_mode=session as per https://github.com/MagicStack/asyncpg/issues/339

It seems the issue is that asyncpg sets up remote state for performance, and pgbouncer is stateless. This is an issue because to do safe parametrised queries postgres requires a two-step instruction (prepare + call) and asyncpg assumes that this is available.

I see there is a suggestion to make pgbouncer work a bit better over here: https://github.com/MagicStack/asyncpg/pull/348#issuecomment-416058033

If the problem goes away if you set the pool_mode, then it is very likely the issue is what is descrivbed there, and we can implement a work-around so it at least works.