Skip to main content

fetch_table_health

Function fetch_table_health 

Source
pub async fn fetch_table_health(
    pool: &DjogiPool,
) -> Result<Vec<TableHealth>, AnalyzeError>
Expand description

Pull live-DB stats from pg_stat_user_tables and (optionally) pg_partman.show_partitions(...) for every user table.

§Query design

The primary query lists schema-qualified table names plus visibility counters and last-analyze timestamps from pg_stat_user_tables. That catalogue is part of every Postgres install, so this query is always available and never errors with UNDEFINED_TABLE. The per-row partition lookup uses partman.show_partitions($1) which is part of the optional pg_partman extension. Many adopters will not have it installed; rather than refusing to run analyze on those clusters, we catch SQLSTATE UNDEFINED_FUNCTION (42883), UNDEFINED_TABLE (42P01), and INVALID_SCHEMA_NAME (3F000) from the partman call and fall back to partition_count = 0. The fallback collapses the partman path to “no partitions reported” rather than producing a hard error — which is the right semantic because a table that pg_partman doesn’t know about is, from analyze’s perspective, indistinguishable from an unpartitioned table.

§Determinism

ORDER BY table_name in the primary query plus a defensive Vec::sort_by after collection (in run) means the output ordering does not depend on Postgres planner decisions.

§Read-only

Both queries are SELECT-only with positional binds. No DDL, no DML — analyze never writes.