BTree Index Row Size Limit
B-tree entries have a hard row-size ceiling, so an index on an incompressible wide text column builds empty but fails on the first oversized insert.
Code
-- btree 索引条目有单行上限(8kB 页约 2704 字节):索引超宽文本列时,
-- 空表建索引能成功,第一条超宽行写入才失败
CREATE TEMP TABLE payloads (
payload_id integer PRIMARY KEY,
blob text NOT NULL
);
CREATE INDEX payloads_blob_idx ON payloads (blob); -- 空表:建索引成功
-- 4000 字符不可压缩 hex:heap 靠 TOAST 放得下,btree 条目放不下
INSERT INTO payloads
SELECT 1, string_agg(md5(random()::text), '') FROM generate_series(1, 125);
-- ERROR: index row size ... exceeds btree version 4 maximum 2704
DROP INDEX payloads_blob_idx;
-- 修复:去重/等值查找用 md5 表达式索引(32 字符摘要,远小于上限)
CREATE INDEX payloads_blob_hash_idx ON payloads ((md5(blob)));
INSERT INTO payloads
SELECT 1, string_agg(md5(random()::text), '') FROM generate_series(1, 125);
SELECT count(*) FROM payloads WHERE md5(blob) IS NOT NULL;How it works
What runs here is SELECT / INSERT / CREATE — payloads and generate_series are the objects involved.
payloads is created in this same file as session-local temp tables — nothing needs to exist beforehand, and it goes away at session end.
Mechanically, the statements are built from: a WHERE clause filtering the rows one statement applies to; COUNT aggregation; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity.
Putting it to use
Nothing outside the file is needed: the session-local tables it touches (payloads) are created by the file itself — point it at a scratch database and run the whole thing.
Core lines
INSERT INTO payloads
SELECT 1, string_agg(md5(random()::text), '') FROM generate_series(1, 125);
SELECT count(*) FROM payloads WHERE md5(blob) IS NOT NULL;
CREATE TEMP TABLE payloads (
payload_id integer PRIMARY KEY,
blob text NOT NULL
);
Interactive Tools for This Topic
FAQ
- Which objects does the SQL touch?
- `payloads`, `generate_series`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.