Transaction leaking after pg pool patch #14546
Closed
Weiko
started this conversation in
Engineering vision
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.
-
DB connection examples
Both need a connection to the DB, provided by the datasource.
Workspace datasource dynamic instantiation
in workspace-datasource.factory.ts
Each datasource manages its own pool of connection.
which is an issue in our multi-tenant arch where each workspace has its own datasources because 1000 datasources would mean 1000 pools (with each pool having a bunch of idle connections in them) so we needed to patch that behavior.
PgPool patch (04/06/25)
in pg-shared-pool.service.ts
Typeorm using the pool internally
Issue
https://node-postgres.com/apis/pool see
Do not use
pool.query
if you are using a transaction.The pool will dispatch every query passed to pool.query on the first available idle client. Transactions within PostgreSQL are scoped to a single client and so dispatching individual queries within a single transaction across multiple, random clients will cause big problems in your app and not work. For more info please read transactions .
As explained above, each individual query will run through the pool of connections and a transaction can be started and commit in different connections. The more we have ongoing requests, the more likely this can happen and some transactions can either:
Mitigation strategy
In the meantime, we should avoid transactions when it's possible, including the ones started by TypeORM.
This is the case for the save method that runs.
Long term strategy
Later on, we should remove our pg-pool patch. We should instead patch typeorm datasource to work with our multi-tenant and transaction needs (see SAVEPOINT: https://www.postgresql.org/docs/current/sql-savepoint.html if needed)
Beta Was this translation helpful? Give feedback.
All reactions