EXPLAIN BUFFERS Shared Hit Versus Read
BUFFERS output splits page access into shared hit for pages already cached and shared read for disk or OS fetches; two runs expose the cache effect.
Code
-- BUFFERS 把每个节点的页访问拆开:
-- shared hit = 页已在 shared_buffers 里;shared read = 走 OS / 磁盘
EXPLAIN (ANALYZE, BUFFERS)
SELECT order_id, total_cents
FROM orders
WHERE customer_id = 42;
-- ANALYZE 会真实执行并打印实际行数与耗时,BUFFERS 数字就是这次运行的痕迹
-- 冷缓存第一轮 read 占多;同一会话重跑后同样的页大多变成 hit —— 缓存生效的直接证据
EXPLAIN (ANALYZE, BUFFERS)
SELECT SUM(total_cents)
FROM orders
WHERE placed_at >= '2026-01-01';
-- 大范围扫描的 read 偏高是冷启动的正常成本,它说明的是页来自磁盘,而不是"没走索引"
EXPLAIN (ANALYZE, BUFFERS)
SELECT COUNT(*)
FROM orders
WHERE customer_id IS NOT NULL;How it works
Several statements share this file — SELECT / EXPLAIN, with orders among the objects involved.
Breaking the statements into their working parts: a WHERE clause narrowing which rows one statement affects; COUNT, SUM aggregation; NOT NULL qualifiers on the required columns.
Applying it
Run the statement against the orders table.
Statements that matter
EXPLAIN (ANALYZE, BUFFERS)
SELECT SUM(total_cents)
FROM orders
WHERE placed_at >= '2026-01-01';
EXPLAIN (ANALYZE, BUFFERS)
SELECT order_id, total_cents
FROM orders
WHERE customer_id = 42;
EXPLAIN (ANALYZE, BUFFERS)
SELECT COUNT(*)
FROM orders
WHERE customer_id IS NOT NULL;
Interactive Tools for This Topic
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.GraphQL Query FormatterPretty-print a GraphQL query by indenting selection sets by brace depth and normalizing punctuation spacing.
FAQ
- Which tables does this file reference?
- The references are `orders`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.