SELECT 
    c.relname, 
    ns.nspname 
FROM 
    pg_catalog.pg_class c
JOIN 
    pg_catalog.pg_namespace ns 
    ON c.relnamespace = ns.oid
CROSS JOIN LATERAL 
    (SELECT c.relkind IN ('p', 'I')) AS p (inherited)
LEFT JOIN 
    pg_catalog.pg_class t 
    ON c.reltoastrelid = t.oid
WHERE 
    c.relpersistence != 't'
    AND c.relkind = ANY (array['r', 'm', 'p'])
    AND (
        EXISTS (
            SELECT NULL 
            FROM pg_catalog.pg_attribute a
            WHERE a.attrelid = c.oid
                AND a.attnum > 0
                AND NOT a.attisdropped
                AND a.attstattarget IS DISTINCT FROM 0
                AND a.attgenerated <> 'v'
                AND NOT EXISTS (
                    SELECT NULL 
                    FROM pg_catalog.pg_statistic s
                    WHERE s.starelid = a.attrelid
                        AND s.staattnum = a.attnum
                        AND s.stainherit = p.inherited
                )
        )
        OR EXISTS (
            SELECT NULL 
            FROM pg_catalog.pg_statistic_ext e
            WHERE e.stxrelid = c.oid
                AND e.stxstattarget IS DISTINCT FROM 0
                AND NOT EXISTS (
                    SELECT NULL 
                    FROM pg_catalog.pg_statistic_ext_data d
                    WHERE d.stxoid = e.oid
                        AND d.stxdinherit = p.inherited
                )
        )
        OR EXISTS (
            SELECT NULL 
            FROM pg_catalog.pg_attribute a
            JOIN pg_catalog.pg_index i 
                ON i.indexrelid = a.attrelid
            WHERE i.indrelid = c.oid
                AND i.indkey[a.attnum - 1] = 0
                AND a.attnum > 0
                AND NOT a.attisdropped
                AND a.attstattarget IS DISTINCT FROM 0
                AND NOT EXISTS (
                    SELECT NULL 
                    FROM pg_catalog.pg_statistic s
                    WHERE s.starelid = a.attrelid
                        AND s.staattnum = a.attnum
                        AND s.stainherit = p.inherited
                )
        )
        OR EXISTS (
            SELECT NULL 
            FROM pg_catalog.pg_attribute a
            WHERE a.attrelid = c.oid
                AND a.attnum > 0
                AND NOT a.attisdropped
                AND a.attstattarget IS DISTINCT FROM 0
                AND a.attgenerated <> 'v'
                AND c.relhassubclass
                AND NOT p.inherited
                AND EXISTS (
                    SELECT NULL 
                    FROM pg_catalog.pg_inherits h
                    WHERE h.inhparent = c.oid
                )
                AND NOT EXISTS (
                    SELECT NULL 
                    FROM pg_catalog.pg_statistic s
                    WHERE s.starelid = a.attrelid
                        AND s.staattnum = a.attnum
                        AND s.stainherit
                )
        )
        OR EXISTS (
            SELECT NULL 
            FROM pg_catalog.pg_statistic_ext e
            WHERE e.stxrelid = c.oid
                AND e.stxstattarget IS DISTINCT FROM 0
                AND c.relhassubclass
                AND NOT p.inherited
                AND EXISTS (
                    SELECT NULL 
                    FROM pg_catalog.pg_inherits h
                    WHERE h.inhparent = c.oid
                )
                AND NOT EXISTS (
                    SELECT NULL 
                    FROM pg_catalog.pg_statistic_ext_data d
                    WHERE d.stxoid = e.oid
                        AND d.stxdinherit
                )
        )
    )
ORDER BY 
    c.relpages DESC;
