psqlpy: How to Resolve Issue with Prepared Statements: 'Statement Already Exists' on Server Deployment
When deploying my code using psqlpy on the server, I am encountering an intermittent issue where queries fail with the error cannot prepare statement: statement already exists. However, the same APIs work fine in the local environment. After a few retries, the queries run as expected.
I have tried the following to troubleshoot the issue:
- Disabled caching by removing the @alru_cache decorator.
- Set the prepared parameter to False when executing queries.
Despite these changes, the issue persists. The problem appears intermittently, failing a couple of times and then succeeding.
Code Context: The code executes queries using psqlpy and JinjaSQL for preparing the queries from templates. Here’s the relevant part of the code:
- DBManager is initialized and manages database queries.
- Prepared Statements are used in the form of queries generated from the Jinja2 templates.
sample code : `@asynccontextmanager async def get_db(pg_dsn: str) -> AsyncGenerator[Connection, None]:
db_pool = ConnectionPool(dsn=pg_dsn)
conn = await db_pool.connection()
try:
yield conn
finally:
db_pool.close()
`
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 30 (17 by maintainers)
@akanksha314e Hello!
Here are some updates for you: I’ve supported external connection pools like PgBouncer and SupaVisor.
Now I need to make more code changes to provide a more usable way of configuring prepared statements.
So, the new release will be next week.
For now, you can build psqlpy locally from this branch and test it.
@akanksha314e Hello! I’ve reproduced the problem and found a problem. PSQLPy by default will prepare all statements because we need to get type information from Postgres.
I’ll make sure to fix this. It will take some time though.
Thanks for finding this problem! I’ll text u when it’s fixed. For now, just use session mode in supavisor - it will be fine.
@akanksha314e Hello! Sorry for not getting back to you sooner, I had a lot of things to do.
I’ve tested psqlpy with Supavisor DB and cannot reproduce your problem, I’ll try one more time when I have time.
Did you try to change your
get_dbasynccontextmanager as I wrote before https://github.com/psqlpy-python/psqlpy/issues/122#issuecomment-2604549506?@akanksha314e Thanks, will check soon