Skip to content

Commit b7c175e

Browse files
committed
chore(queries): pg_stat_user_tables: remove optimization to compute size of the materialized views without pg_XXX_size
Now we skip tables that have an AccessExclusiveLock that prevents pg_XXX_size to be executed, we can rollback the complex optimization and use the simple form with pg_XXX_size for materialized views. Because we use JOIN, if a AccessExclusiveLock is on a table/MatView, the table will be missing from the query, and the line will be missed from the export page. We could use LEFT JOIN: - the line for the table will be kept, with NULL values for size fields. For now, we prefer having missing data rather than NaN number reported.
1 parent 42f049b commit b7c175e

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

content/tutorials/postgresql-exporter.queries.yaml

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -200,28 +200,12 @@ 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
222-
WHERE NOT EXISTS (
223-
SELECT 1 FROM pg_locks WHERE pg_locks.relation = u.relid AND pg_locks.mode = 'AccessExclusiveLock'
224-
)
225209
;
226210
metrics:
227211
- datname:

0 commit comments

Comments
 (0)