Skip to content

Commit acb8d65

Browse files
committed
Add terminate and cancel backend queries
Add pg_stat_activity query to look for queries that are waiting. If they are waiting for a long time it may be worth cancelling or terminating them
1 parent 0a33ca1 commit acb8d65

File tree

4 files changed

+8
-0
lines changed

4 files changed

+8
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ Calling out some specifc ways I've used these.
4040

4141
* `list_schemata.sql` - List the schemas
4242

43+
* Cancel and terminate backend process IDs (PIDs)
44+
45+
* `waiting_queries.sql` - View waiting queries
46+
4347
## Links
4448

4549
* [Deep dive into postgres stats: pg_stat_all_tables](https://dataegret.com/2017/04/deep-dive-into-postgres-stats-pg_stat_all_tables/)
@@ -50,3 +54,4 @@ Calling out some specifc ways I've used these.
5054
* [Postgres Index stats and Query Optimization](https://sgerogia.github.io/Postgres-Index-And-Queries/)
5155
* [Some SQL Tricks of an Application DBA](https://hakibenita.com/sql-tricks-application-dba)
5256
* [Lessons Learned From 5 Years of Scaling PostgreSQL](https://onesignal.com/blog/lessons-learned-from-5-years-of-scaling-postgresql/)
57+
* [Understanding PostgreSQL Query Performance](https://pgdash.io/blog/understanding-postgres-query-performance.html)

cancel_backend.sql

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT pg_terminate_backend(123);

terminate_backend.sql

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT pg_cancel_backend(123);

waiting_queries.sql

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select pid, wait_event_type, wait_event, left(query, 60) as query, backend_start, query_start, (current_timestamp - query_start) as ago from pg_stat_activity where datname='rideshare_development';

0 commit comments

Comments
 (0)