pg_column_size Reveals TOAST Storage

Compare octet_length and pg_column_size on a ~6KB incompressible column: the value goes out of line uncompressed and the heap row keeps only a pointer.

SqlData Integrity

Code

snippet
-- 高熵载荷让 pglz 压缩收益达不到默认策略的 25% 门槛(首块 1KB 内无有效匹配
-- 即放弃,见 src/common/pg_lzcompress.c strategy_default_data),
-- 于是值以【未压缩】形态越过约 2KB 行外阈值出行外:
-- 行外后堆里只留 18 字节指针,索引也永远只存这份指针
CREATE TABLE payloads (id integer, body text);

-- ~187 个 md5 串拼接出约 6KB 高熵文本(md5 随机串几乎不可压缩)
INSERT INTO payloads
SELECT 1, (SELECT string_agg(md5(random()::text), '')
           FROM generate_series(1, 187));  -- 约 6KB 不可压缩文本

INSERT INTO payloads
SELECT 2, md5(random()::text);          -- 32 字节小值,原地存放

-- pg_column_size 读磁盘上的存储形态;octet_length 强制 detoast 读全文
SELECT
    id,
    octet_length(body)   AS logical_bytes,
    pg_column_size(body) AS stored_bytes,
    pg_column_size(body) < octet_length(body) AS toasted
FROM payloads
ORDER BY id;

-- 行 1:逻辑约 5984 字节;pglz 收益不足放弃压缩,值未压缩出行外,
--       stored_bytes 等于逻辑长度(toast_datum_size 读 va_extinfo = 原始长度,
--       不含 TOAST 分块行开销),toasted 为 false;堆行只持有 18 字节指针
-- 行 2:小值不触发 TOAST,两列相同(33 与 32,差一个 varlena 头),toasted 为 false

Two storage paths

The demo inserts a ~6KB incompressible text built by string_agg over 187 md5(random()::text) rows, and a 32-byte md5 value. The wide value crosses the ~2KB out-of-line threshold. High-entropy md5 hex gives pglz too little gain to pass its default strategy — roughly 25% savings required, giving up within the first 1KB of no useful matches — so PostgreSQL stores the value out of line uncompressed, leaving an 18-byte pointer in the heap row. The small value stores inline and never detoasts.

Reading the numbers

octet_length forces detoast and reports logical bytes; pg_column_size reports the stored form. For row 1 the value is out of line but uncompressed, so stored_bytes equals the logical length and the toasted comparison is false — the value still lives in the TOAST table, the heap row just keeps a pointer. For row 2 both columns agree and toasted is false.

Why incompressible payload

A repeated-text payload would compress far below the threshold and stay inline, which would quietly falsify the demo; high-entropy text pins the out-of-line path without any compression changing the numbers. Whether pglz actually compresses depends on the payload and the strategy checks, so the demo reads the same either way: whatever pglz decides, pg_column_size reports the real stored form.

Interactive Tools for This Topic

FAQ

Why build the payload from md5(random()::text) instead of repeat()?
Repeated text compresses below the out-of-line threshold and stays inline; md5 hex is high-entropy, so pglz gains too little to bother and the value still moves out of line — just uncompressed.
What do the two size functions report for the out-of-line row?
octet_length returns the logical length (it detoasts); pg_column_size returns the stored form — for an uncompressed out-of-line value that equals the logical length, so toasted compares false even though the value lives in the TOAST table.