4.2 Index-only scan
Index-only scan은 query에 필요한 column이 index에 있고 visibility map으로 heap page의 tuple visibility를 확인할 수 있을 때 heap 접근을 줄입니다.
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at DESC)
INCLUDE (status, amount);
EXPLAIN (ANALYZE, BUFFERS)
SELECT created_at, status, amount
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 20;
Plan의 Heap Fetches를 확인합니다. Index-only라는 이름이 있어도 visibility bit가 설정되지 않은 page는 heap을 읽습니다.
Vacuum과 visibility
Update가 잦은 table은 all-visible bit가 자주 해제됩니다. Autovacuum 상태와 workload의 update pattern을 함께 봅니다.
SELECT relname, n_tup_upd, n_tup_hot_upd,
last_autovacuum, autovacuum_count
FROM pg_stat_user_tables
WHERE relname = 'orders';
INCLUDE column은 검색 key가 아니며 index tuple 크기와 write cost를 늘립니다. Wide column을 무작정 포함하지 않습니다. 실제 buffer 감소와 index size를 비교합니다.