Using statement_cache_size asyncpg setting / prepared statement name for asyncpg w pgbouncer
See original GitHub issueHi!
I use sqlalchemy 1.4 with asyncpg driver with pgbouncer.
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession
engine = create_async_engine(
f'postgresql+asyncpg://{username}:{password}@{host}:{port}/{dbname}',
echo=False,
)
session_maker = sessionmaker(
engine,
class_=AsyncSession,
)
I have an error:
asyncpg.exceptions.DuplicatePreparedStatementError: prepared statement "__asyncpg_stmt_a__" already exists
HINT:
NOTE: pgbouncer with pool_mode set to "transaction" or
"statement" does not support prepared statements properly.
You have two options:
* if you are using pgbouncer for connection pooling to a
single server, switch to the connection pool functionality
provided by asyncpg, it is a much better option for this
purpose;
* if you have no option of avoiding the use of pgbouncer,
then you can set statement_cache_size to 0 when creating
the asyncpg connection object.
How i can pass this setting (statement_cache_size=0) to asyncpg connection object?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:48 (30 by maintainers)
Top Results From Across the Web
Frequently Asked Questions — asyncpg Documentation
To create a cursor usable outside of a transaction, use the DECLARE ... CURSOR WITH HOLD SQL statement directly. Why am I getting...
Read more >getting a "prepared statement error" in asyncpg - Stack Overflow
I am trying work with some data from a PostgreSQL server,by using Connection Pools. There's a caveat mentioning this issue and it's ...
Read more >1M rows/s from Postgres to Python - magicstack
The most obvious option was psycopg2—the most popular Python driver for ... asyncpg extensively uses PostgreSQL prepared statements.
Read more >Getting A "Prepared Statement Error" In Asyncpg - ADocLib
When used with UPDATE and DELETE statements, it's possible to retrieve values of and to handle database errors. sakr@ulb. asyncpg is written almost...
Read more >How can I disable prepared statements for using pgbouncer ...
I want to use pgbouncer in transaction pool mode. To do this you cannot use prepared statements. I set the JDBC PSQL connection...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I do have a working setup in transaction mode using https://github.com/sqlalchemy/sqlalchemy/issues/6467#issuecomment-864943824 though:
We had a similar problem due to multiple web workers. They generated prepared statements with the same names - original function to generate IDs looks like this:
So we just changed the Connection class a bit
You need to provide it when you create the engine