I'm currently working on a feature in our TypeORM and PostgreSQL setup where I need to handle cancellation of long-running and time-consuming requests. For this, I believe I need to track the Process ID (PID) of specific PostgreSQL connections to properly cancel queries that have been requested to stop. Does anyone have experience or know how to retrieve the PID for a query in TypeORM?
I'm considering using a custom query to call pg_backend_pid(), but I'm wondering if there's a more efficient way or standard practice for doing this, especially in a production environment. Any insights on managing this kind of query cancellation would be greatly appreciated!
There is this open issue in the TypeORM github so this is most likely not implemented in any way: https://github.com/typeorm/typeorm/issues/8552
I think using
pg_backend_pid()andpg_cancel_backend()is your best bet, you just have to make sure to run your query andpg_backend_pid()on the same connection.How I would go about it: