Skip to content

Commit 03e48a6

Browse files
authored
Merge pull request #19 from qonto/fix-access-exclusive-lock-size
chore(queries): pg_stat_user_tables: skip tables with an AccessExclusiveLock
2 parents 574f7f1 + b7c175e commit 03e48a6

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed

content/tutorials/postgresql-exporter.queries.yaml

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,24 +200,11 @@ pg_stat_user_tables:
200200
pg_indexes_size(pg_class.oid) AS index_size,
201201
pg_total_relation_size(pg_class.reltoastrelid) AS toast_size
202202
FROM
203-
pg_stat_user_tables u
204-
JOIN pg_class ON pg_class.oid = u.relid AND pg_class.relkind <> 'm' -- exclude matviews to prevent query being locked when refreshing MV
205-
UNION ALL
206-
SELECT
207-
c.oid,
208-
SUM(c.relpages::bigint*8192) AS table_size,
209-
coalesce(SUM(idx.index_bytes),0) as index_size,
210-
coalesce(SUM((c2.relpages+c3.relpages)::bigint*8192),0) AS toast_size
211-
FROM pg_stat_user_tables u
212-
JOIN pg_class c ON u.relid=c.oid AND c.relkind='m' -- matviews only
213-
LEFT JOIN pg_class c2 ON c2.oid = c.reltoastrelid
214-
LEFT JOIN pg_index it ON it.indrelid=c.reltoastrelid -- only one index per pg_toast table
215-
LEFT JOIN pg_class c3 ON c3.oid=it.indexrelid
216-
CROSS JOIN LATERAL (
217-
SELECT SUM(c4.relpages::bigint*8192) AS index_bytes
218-
FROM pg_index i JOIN pg_class c4 ON i.indrelid=c.oid AND c4.oid=i.indexrelid
219-
) idx
220-
GROUP BY c.oid
203+
pg_stat_user_tables ut
204+
JOIN pg_class ON pg_class.oid = ut.relid
205+
WHERE NOT EXISTS (
206+
SELECT 1 FROM pg_locks WHERE pg_locks.relation = ut.relid AND pg_locks.mode = 'AccessExclusiveLock'
207+
)
221208
) t ON u.relid = t.oid
222209
;
223210
metrics:

0 commit comments

Comments
 (0)