Currently using Postgres 14.
Running CLUSTER sometable USING someindex; is great, but is there a query to determine how "unclustered" the table has become after many updates and deletes?
Googling and reading https://www.postgresql.org/docs/14/sql-cluster.html just says "set up a periodic maintenance script ... so that the desired tables are periodically reclustered", which is unhelpful since it doesn't give any indication of how to determine how periodic the periodic reclustering should be.
EDIT: based on the accepted answer, I added this bash snippet to my weekly purge job:
FQTable=public.log
PKFld=log_id
IdxName=pk_log
sql="DO \$\$
DECLARE
_v_correlation numeric(3,2);
BEGIN
SELECT abs(correlation)::numeric(3,2)
INTO _v_correlation
FROM pg_stats
WHERE schemaname||'.'||tablename = '${FQTable}'
AND attname = '${PKFld}';
RAISE NOTICE 'Correlation = %', _v_correlation;
IF _v_correlation < 0.60 THEN
CLUSTER ${FQTable} USING ${IdxName};
END IF;
END \$\$;"
psql $DB -Xc "$sql"