Correlation Statistic Diagnoses Physical Row Order
pg_stats.correlation measures how a column's physical row order matches its logical order: near ±1 scans stay sequential, near 0 rows scatter into random I/O.
Code
-- pg_stats.correlation compares a column's physical storage order with its
-- logical order (the order ORDER BY on the column would produce).
-- |correlation| near 1: scanning by this column reads the heap almost
-- sequentially. Near 0: rows are scattered and every fetch risks random I/O.
CREATE TABLE events_seq (
occurred_at timestamptz NOT NULL,
payload text NOT NULL DEFAULT 'event'
);
-- Rows land in descending time order here, so physical order mirrors the
-- logical order exactly (reversed): the statistic should come out near ±1.
INSERT INTO events_seq (occurred_at)
SELECT now() - (n || ' minutes')::interval
FROM generate_series(1, 20000) AS n;
-- The statistic is produced by ANALYZE: without it pg_stats has no row to show.
ANALYZE events_seq;
SELECT attname, correlation
FROM pg_stats
WHERE tablename = 'events_seq' AND attname = 'occurred_at';
-- Read the value as a storage-layout diagnostic: near ±1 the column is stored
-- contiguously in logical order, so ordered range scans stay sequential; near
-- 0 the same scans degrade into random I/O, and rebuilding the table with rows
-- in key order (CLUSTER on a matching index) restores the sequential layout.The snippet measures how a column’s physical storage order lines up with its logical order. It creates events_seq, loads 20000 rows through generate_series(1, 20000) so each occurred_at lands one minute earlier than the last, and then reads the correlation figure PostgreSQL recorded for that column in pg_stats.
What the numbers mean
The statistic lives in pg_stats under the attname = 'occurred_at' row for the table, and the SELECT in the snippet is the whole diagnostic. A value close to ±1 means heap pages follow the column’s sort order, so a range over occurred_at reads pages nearly sequentially. A value close to 0 means logically adjacent rows are scattered across distant pages, and the same range degenerates into random I/O. The sign only records whether storage order matches or reverses the sort order; the magnitude is what matters.
Getting a reliable reading
ANALYZE events_seq; is not decoration: the statistic is computed by sampling during ANALYZE, and a table that was just created and populated has no pg_stats row at all until it runs. The INSERT ... SELECT in the snippet writes in strictly descending time order, so the fresh reading should come out near ±1. After churn — random updates, deletes, and reinserts — the same query on the same table can drift toward 0, which is the signal that the storage layout no longer matches the logical order.
Interactive Tools for This Topic
FAQ
- What does a correlation value near ±1 tell you about the heap?
- The rows sit in the table in almost the same order the column sorts in, so reading a range of that column walks the heap front to back instead of jumping between pages — sequential I/O instead of random I/O.
- Why does the snippet run ANALYZE before querying pg_stats?
- The correlation figure in pg_stats is computed when PostgreSQL samples the table during ANALYZE. On a freshly created and loaded table like events_seq, no statistics row exists until ANALYZE runs, so the SELECT would return nothing without it.
- How does a value near 0 happen, and what fixes it?
- Heavy random updates, deletes, and reinserts scatter rows whose column values are logically adjacent across distant pages. Rebuilding the table with rows stored in column order — CLUSTER on an index over that column, or a VACUUM FULL rewrite — restores a high correlation.
Report a problem with this exampleOpens a pre-filled issue on GitHub.