Skip to content

Instantly share code, notes, and snippets.

@robcowie
Last active February 18, 2020 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robcowie/5c4d805463a4c58f363456cc66ee7fb3 to your computer and use it in GitHub Desktop.
Save robcowie/5c4d805463a4c58f363456cc66ee7fb3 to your computer and use it in GitHub Desktop.
PostgreSQL vacuuming monitoring queries

Postgresql Vacuuming

To monitor progress of autovacuum

SELECT
    heap_blks_scanned/cast(heap_blks_total as numeric) * 100 as heap_blks_percent,
    progress.*,
    activity.query
FROM
    pg_stat_progress_vacuum AS progress
INNER JOIN
    pg_stat_activity AS activity
ON
    activity.pid = progress.pid;

DOES NOT SEEM TO WORK OFTEN!

To list active transactions

SELECT
    pid,
    left(query, 90),
    (now() - xact_start) as t
FROM
    pg_stat_activity
WHERE
    state IN ('idle in transaction', 'active')
ORDER BY t DESC;

To list only active/queued autiovacuum transactions

SELECT
    pid,
    left(query, 90),
    (now() - xact_start) as t
FROM
    pg_stat_activity
WHERE
    state IN ('idle in transaction', 'active')
AND
    query like 'autovacuum:%'
ORDER BY t DESC;

To kill active/queued autovacuum transactions

SELECT
     pg_terminate_backend(pid)
 FROM
     pg_stat_activity
 WHERE
     state IN ('idle in transaction', 'active')
 AND
     query like 'autovacuum:%';

To change autovacuum related config per-table

alter table keywords_report SET (autovacuum_analyze_threshold = 10000, autovacuum_vacuum_threshold = 10000);
-- reset to default
alter table keywords_report reset (autovacuum_analyze_threshold, autovacuum_vacuum_threshold);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment