1
Fork 0
mirror of https://github.com/RealOrangeOne/notes.git synced 2024-12-22 13:16:22 +00:00

Add some notes about killing long running queries etc

This commit is contained in:
Jake Howard 2020-10-07 17:41:28 +01:00
parent 9d1d1f0714
commit af7cf4a0a0
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 80 additions and 0 deletions

View file

@ -0,0 +1,38 @@
---
title: Find and kill long running queries
tags:
- PostgreSQL
link: https://medium.com/little-programming-joys/finding-and-killing-long-running-queries-on-postgres-7c4f0449e86d
---
# Running queries
View a list of queries running longer than 5 minutes:
```sql
SELECT
pid,
now() - pg_stat_activity.query_start AS duration,
query,
state
FROM pg_stat_activity
WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes';
```
Also see [running queries](../running-queries).
# Stopping a given connection
```sql
SELECT pg_cancel_backend(pid);
```
`pid` being the relevant value from `pg_stat_activity.pid`.
# Killing a given connection
```sql
SELECT pg_terminate_backend(pid);
```
Should be avoided, as it's synonymous with `kill -9`.

View file

@ -0,0 +1,42 @@
---
title: Monitor running queries
tags:
- PostgreSQL
link: https://techmango.org/2017/11/04/monitor-running-queries-postgresql/
---
View a list of running queries:
```sql
SELECT
datname,
pid,
usename,
client_addr,
client_port,
xact_start,
backend_start,
query_start,
state,
query
FROM pg_stat_activity
ORDER BY query_start ASC
```
View list of non-idle connections:
```sql
SELECT
datname,
pid,
usename,
client_addr,
client_port,
xact_start,
backend_start,
query_start
FROM pg_stat_activity
WHERE
state != 'idle'
ORDER BY query_start ASC
```