pgstattuple Measures Dead And Free Space
Inspect dead tuples and reusable free space with pgstattuple before and after VACUUM: deletes raise dead_tuple_percent, VACUUM converts them into free_percent.
Code
CREATE EXTENSION IF NOT EXISTS pgstattuple;
CREATE TABLE page_probe (id int PRIMARY KEY, payload text NOT NULL);
INSERT INTO page_probe
SELECT g, repeat('x', 200) FROM generate_series(1, 20000) g;
-- 半数行被删除:空间先挂在 dead_tuple_percent 上,free_percent 还没涨
DELETE FROM page_probe WHERE id % 2 = 0;
SELECT table_len, tuple_count, tuple_percent, dead_tuple_percent, free_percent
FROM pgstattuple('page_probe');
-- VACUUM 把死元组腾成可复用空间:dead 归零、free_percent 上升,表长不变
VACUUM page_probe;
SELECT table_len, tuple_count, tuple_percent, dead_tuple_percent, free_percent
FROM pgstattuple('page_probe');Inside the snippet
The file runs SELECT / INSERT / DELETE / CREATE statements, touching page_probe, generate_series and pgstattuple.
Breaking the statements into their working parts: a WHERE clause that keeps only the rows a statement is meant to touch; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity.
Putting it to use
Run the statement against the page_probe table.
Core lines
INSERT INTO page_probe
SELECT g, repeat('x', 200) FROM generate_series(1, 20000) g;
SELECT table_len, tuple_count, tuple_percent, dead_tuple_percent, free_percent
FROM pgstattuple('page_probe');
CREATE TABLE page_probe (id int PRIMARY KEY, payload text NOT NULL);
Interactive Tools for This Topic
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.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.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.HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.
FAQ
- Which relations are involved here?
- The references are `page_probe`, `generate_series`, `pgstattuple`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.