Skip to content

View column statistics

SELECT
tablename,
attname,
null_frac,
avg_width,
n_distinct,
correlation
FROM pg_stats
WHERE
tablename = 'flight'
AND attname = 'departure_airport';
tablename | attname | null_frac | avg_width | n_distinct | correlation
-----------+-------------------+-----------+-----------+------------+-------------
flight | departure_airport | 0 | 4 | 647 | 0.049104065
(1 row)
WITH s AS (
SELECT
most_common_vals,
most_common_freqs
FROM pg_stats
WHERE
tablename = 'flight'
AND attname = 'departure_airport'
)
SELECT value, ROUND((ratio * 100)::numeric, 2) AS pct, ratio
FROM
unnest(
(SELECT most_common_vals::text::text [] FROM s),
(SELECT most_common_freqs FROM s)
)
AS x (value, ratio)
ORDER BY ratio DESC;
value | pct | ratio
-------+------+--------------
ATL | 2.33 | 0.023333333
ORD | 1.99 | 0.019933334
DEN | 1.79 | 0.0179
LAX | 1.76 | 0.017633334
CDG | 1.64 | 0.016366666
...
(100 rows)