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:
parent
9d1d1f0714
commit
af7cf4a0a0
2 changed files with 80 additions and 0 deletions
38
notes/psql/long-running-queries.md
Normal file
38
notes/psql/long-running-queries.md
Normal 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`.
|
42
notes/psql/running-queries.md
Normal file
42
notes/psql/running-queries.md
Normal 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
|
||||
```
|
Loading…
Reference in a new issue