Edit on GitHub

backend.workers.cancel_query

Cancel a PostgreSQL query

 1"""
 2Cancel a PostgreSQL query
 3"""
 4from backend.lib.worker import BasicWorker
 5
 6
 7class QueryCanceller(BasicWorker):
 8	"""
 9	Cancel a PostgreSQL query
10
11	psycopg2 has some limitations when it comes to asynchronous database
12	manipulation. One of those is that while queries can be run sort-of
13	asynchronously, it is very complicated to run *another* query while
14	one is already active in the same thread, e.g. a pg_cancel_backend()
15	query. To solve this complexity, this worker, with its own PG
16	connection, takes the ID of a worker's Postgres connection as a
17	parameter and cancels all queries associated with that connection
18	"""
19	type = "cancel-pg-query"
20	max_workers = 1
21
22	def work(self):
23		"""
24		Send pg_cancel_backend query to cancel queries for given connections
25		"""
26		active_queries = self.db.fetchall("SELECT pid, application_name FROM pg_stat_activity")
27		pids_to_kill = [query["pid"] for query in active_queries if query["application_name"] == self.job.data["remote_id"]]
28
29		for pid in pids_to_kill:
30			self.log.info("Cancelling PostgreSQL query %s" % pid)
31			self.db.execute("SELECT pg_cancel_backend(%s)" % pid)
32
33		self.job.finish()
class QueryCanceller(backend.lib.worker.BasicWorker):
 8class QueryCanceller(BasicWorker):
 9	"""
10	Cancel a PostgreSQL query
11
12	psycopg2 has some limitations when it comes to asynchronous database
13	manipulation. One of those is that while queries can be run sort-of
14	asynchronously, it is very complicated to run *another* query while
15	one is already active in the same thread, e.g. a pg_cancel_backend()
16	query. To solve this complexity, this worker, with its own PG
17	connection, takes the ID of a worker's Postgres connection as a
18	parameter and cancels all queries associated with that connection
19	"""
20	type = "cancel-pg-query"
21	max_workers = 1
22
23	def work(self):
24		"""
25		Send pg_cancel_backend query to cancel queries for given connections
26		"""
27		active_queries = self.db.fetchall("SELECT pid, application_name FROM pg_stat_activity")
28		pids_to_kill = [query["pid"] for query in active_queries if query["application_name"] == self.job.data["remote_id"]]
29
30		for pid in pids_to_kill:
31			self.log.info("Cancelling PostgreSQL query %s" % pid)
32			self.db.execute("SELECT pg_cancel_backend(%s)" % pid)
33
34		self.job.finish()

Cancel a PostgreSQL query

psycopg2 has some limitations when it comes to asynchronous database manipulation. One of those is that while queries can be run sort-of asynchronously, it is very complicated to run another query while one is already active in the same thread, e.g. a pg_cancel_backend() query. To solve this complexity, this worker, with its own PG connection, takes the ID of a worker's Postgres connection as a parameter and cancels all queries associated with that connection

type = 'cancel-pg-query'
max_workers = 1
def work(self):
23	def work(self):
24		"""
25		Send pg_cancel_backend query to cancel queries for given connections
26		"""
27		active_queries = self.db.fetchall("SELECT pid, application_name FROM pg_stat_activity")
28		pids_to_kill = [query["pid"] for query in active_queries if query["application_name"] == self.job.data["remote_id"]]
29
30		for pid in pids_to_kill:
31			self.log.info("Cancelling PostgreSQL query %s" % pid)
32			self.db.execute("SELECT pg_cancel_backend(%s)" % pid)
33
34		self.job.finish()

Send pg_cancel_backend query to cancel queries for given connections