Session Artifacts: session settings or objects that interfere with Supavisor/PgBouncer #40593
TheOtherBrian1
announced in
Troubleshooting
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When using connection poolers like PgBouncer or Supavisor, connections can be reassigned between clients. That means settings and objects created for one client may end up being present for others. This may result in three distinct errors.
Prepared statement errors:
Some external Postgres libraries, such as Prisma and Drizzle, cache query plans as named prepared statements to avoid overhead from re-parsing and planning queries repeatedly.
However, these prepared statements only persist within the database connection in which they were defined.
As connections are reassigned by Supavisor, it can result in the errors:
prepared_statement "some_name" does not existprepared_statement "some_name" already existsSolutions
For advice on how to disable them, check out the below troubleshooting guide
Cursor/Portal errors:
Cursors/Portals act like symbolic bookmarks for result sets, allowing partial retrieval of large queries:
They are one of many ways Postgres facilitates pagination(https://www.citusdata.com/blog/2016/03/30/five-ways-to-paginate/).
Some external ORMs, predominantly Django, may try to create these objects if not explicitly disabled.
Like prepared statements, cursors and portals only exist within a single connection.
When PgBouncer and Supavisor shuffle connections between clients, it can result in the errors:
cursor/portal "some_name" does not existcursor/portal "some_name" already existsImportant caveats:
cursorto refer to their query orchestrator. Despite sharing a name, they are not the same as Postgres cursor objects and are fine to use.Solutions:
Session-Level settings and characteristics errors
You can change Postgres settings within the context of a single connection:
Or set session characteristics that affect all subsequent transactions:
When a pooler reuses or reassigns connections, these session settings persist and can cause unpredictable behavior for other clients.
For instance, the Postgres interface DBeaver when set to read-only mode, will change a connections settings. When the connection is swapped by the serverside pooler, this will cause other clients to experience unexpected read-only errors.
Solutions
If you must create session variables, it is better to avoid shared poolers and instead use Supavisor in session mode or direct connections (port 5432), which do not share sessions between clients.
If you want a setting to be permanent (not just for a connection's lifetime), you should consider changing the setting at the role level instead:
It's possible to change a setting just for the duration of a transaction. For this to work, the query must be wrapped in a BEGIN/COMMIT block:
Beta Was this translation helpful? Give feedback.
All reactions