psqlpy icon indicating copy to clipboard operation
psqlpy copied to clipboard

How to Resolve Issue with Prepared Statements: 'Statement Already Exists' on Server Deployment

Open akanksha314e opened this issue 10 months ago • 30 comments
trafficstars

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()

`

akanksha314e avatar Jan 20 '25 18:01 akanksha314e