updated test
WITH RECURSIVE partition_hierarchy AS (
-- Start with the direct children of the parent table
SELECT
child.oid AS partition_oid,
child.relname AS partition_name,
parent.relname AS parent_name,
nsp.nspname AS schema_name
FROM
pg_inherits i
JOIN
pg_class child ON i.inhrelid = child.oid
JOIN
pg_class parent ON i.inhparent = parent.oid
JOIN
pg_namespace nsp ON child.relnamespace = nsp.oid
WHERE
parent.relname = '<your_parent_table_name>' -- Replace with your parent table name
AND nsp.nspname = '<your_schema_name>' -- Replace with your schema name
UNION ALL
-- Recursively find children of the current partitions
SELECT
child.oid AS partition_oid,
child.relname AS partition_name,
parent.relname AS parent_name,
nsp.nspname AS schema_name
FROM
pg_inherits i
JOIN
pg_class child ON i.inhrelid = child.oid
JOIN pg_class parent ON i.inhparent = parent.oid
JOIN pg_namespace nsp ON child.relnamespace = nsp.oid
JOIN partition_hierarchy ph ON parent.oid = ph.partition_oid
)
SELECT
ph.schema_name || '.' || ph.partition_name AS partition_table,
st.last_autovacuum,
st.last_autoanalyze,
st.n_dead_tup,
st.n_mod_since_analyze,
st.n_live_tup
FROM
partition_hierarchy ph
LEFT JOIN
pg_stat_user_tables st
ON ph.schema_name || '.' || ph.partition_name = st.schemaname || '.' || st.relname
ORDER BY
st.last_autoanalyze NULLS FIRST, -- Prioritize tables never autoanalyzed
st.n_mod_since_analyze DESC; -- Sort by modifications
Comments
Post a Comment