Skip to content

Commit

Permalink
chore(queries): pg_stat_user_tables: remove optimization to compute s…
Browse files Browse the repository at this point in the history
…ize 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.
  • Loading branch information
chtitux committed Dec 14, 2023
1 parent 42f049b commit b7c175e
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions content/tutorials/postgresql-exporter.queries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -200,28 +200,12 @@ 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
WHERE NOT EXISTS (
SELECT 1 FROM pg_locks WHERE pg_locks.relation = u.relid AND pg_locks.mode = 'AccessExclusiveLock'
)
;
metrics:
- datname:
Expand Down

0 comments on commit b7c175e

Please sign in to comment.