views:

46

answers:

1

Hi, I'm not sure where to start on addressing this issue but if I have a AJAX web application that sends requests to the server and runs long queries on the database (postgresql in my case), is there a way to stop or kill the queries if while still running, the user refreshes the page or closes the session...etc?

+3  A: 

To stop the query:

SELECT pg_cancel_backend(proced_id);

To kill the database connection:

SELECT pg_termintate_backend(proced_id);

To get an overview of the current transactions, to get the proced id's:

SELECT * FROM pg_stat_activity;

http://www.postgresql.org/docs/current/interactive/functions-admin.html

Frank Heikens
i'm curious if there's any examples showing the use of those function in the web application layer so i can automate this?
Pydroid
pg_stat_activity shows you the starttime of a query. If you want to kill a transactions when it's too old, just do it. But you have to be a superuser or embed these queries inside a function and use SECURITY DEFINER.
Frank Heikens
right, thanks for the insight!
Pydroid