Skip to content

Commit

Permalink
Merge pull request #19 from qonto/fix-access-exclusive-lock-size
Browse files Browse the repository at this point in the history
chore(queries): pg_stat_user_tables: skip tables with an AccessExclusiveLock
  • Loading branch information
chtitux authored Dec 14, 2023
2 parents 574f7f1 + b7c175e commit 03e48a6
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions content/tutorials/postgresql-exporter.queries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -200,24 +200,11 @@ pg_stat_user_tables:
pg_indexes_size(pg_class.oid) AS index_size,
pg_total_relation_size(pg_class.reltoastrelid) AS toast_size
FROM
pg_stat_user_tables u
JOIN pg_class ON pg_class.oid = u.relid AND pg_class.relkind <> 'm' -- exclude matviews to prevent query being locked when refreshing MV
UNION ALL
SELECT
c.oid,
SUM(c.relpages::bigint*8192) AS table_size,
coalesce(SUM(idx.index_bytes),0) as index_size,
coalesce(SUM((c2.relpages+c3.relpages)::bigint*8192),0) AS toast_size
FROM pg_stat_user_tables u
JOIN pg_class c ON u.relid=c.oid AND c.relkind='m' -- matviews only
LEFT JOIN pg_class c2 ON c2.oid = c.reltoastrelid
LEFT JOIN pg_index it ON it.indrelid=c.reltoastrelid -- only one index per pg_toast table
LEFT JOIN pg_class c3 ON c3.oid=it.indexrelid
CROSS JOIN LATERAL (
SELECT SUM(c4.relpages::bigint*8192) AS index_bytes
FROM pg_index i JOIN pg_class c4 ON i.indrelid=c.oid AND c4.oid=i.indexrelid
) idx
GROUP BY c.oid
pg_stat_user_tables ut
JOIN pg_class ON pg_class.oid = ut.relid
WHERE NOT EXISTS (
SELECT 1 FROM pg_locks WHERE pg_locks.relation = ut.relid AND pg_locks.mode = 'AccessExclusiveLock'
)
) t ON u.relid = t.oid
;
metrics:
Expand Down

0 comments on commit 03e48a6

Please sign in to comment.