Incident Response

PostgreSQL: index row size exceeds btree version 4 maximum 2704

A B-tree entry must fit in a third of a page, and one long value is enough to break an insert or an index build. Where 2704 comes from, why the error appears years after the index was created, and which index to use instead.

· 13 min read · AG Data Team

PostgreSQL ERROR: index row size exceeds btree version 4 maximum 2704 means a single value, or combination of values, is too large to be stored as one B-tree index entry. The insert, update or index build that produced it is rolled back, and it will keep failing for that row until the index or the data changes.

ERROR:  index row size 3048 exceeds btree version 4 maximum 2704 for index "pages_url_key"
DETAIL:  Index row references tuple (88120,3) in relation "pages".
HINT:  Values larger than 1/3 of a buffer page cannot be indexed.
Consider a function index of an MD5 hash of the value, or use full text indexing.

Every part of the message is useful. 3048 is the size of the entry PostgreSQL tried to write, 2704 is the limit, the index name tells you which index to rethink, and DETAIL gives the physical address (ctid) of the offending row. The error is raised in _bt_check_third_page() with SQLSTATE 54000, program_limit_exceeded: it is a hard limit, not a configuration setting.

Where 2704 comes from

A B-tree page must be able to hold at least three entries, so nbtree.h caps one entry at a third of the usable space on a page. With the standard 8KB page that is 2712 bytes. Version 4 B-trees, the format used for indexes built since PostgreSQL 12, reserve another 8 bytes for a heap TID that can be added to an entry as a tiebreaker, which gives 2704.

You may still see btree version 3 maximum 2712. That is an index carried over by pg_upgrade from a release before 12, still in the old format. The PostgreSQL 12 release notes warn about exactly this: the maximum entry length dropped by eight bytes, so a REINDEX of an upgraded index can fail. If you plan to rebuild old indexes after an upgrade, check them first; our major version upgrade guide covers the rest of that process. bt_metap() from pageinspect shows the version of each index.

Why it appears years after the index was created

The limit applies to the stored entry, not to the declared column type. A text column has no length limit, so the index works for every row until the first value that does not fit. Two details make this hard to predict:

In our experience the usual victims are unique constraints on URLs, file paths, email bodies or API payloads, and composite indexes where several medium-length text columns add up. Non-key columns count as well: the CREATE INDEX documentation warns that wide INCLUDE columns can push an entry over the limit, and then data insertion fails.

Find the row

For a failed insert or update, the value is in the application's request. For a failed index build, use the ctid from DETAIL, then look for other long values before you try again, because the build stops at the first one.

-- candidates: long values in the indexed column (full scan: run it on a replica)
SELECT ctid, id,
       octet_length(url)   AS raw_bytes,
       pg_column_size(url) AS stored_bytes
FROM pages
WHERE octet_length(url) > 2000
ORDER BY raw_bytes DESC
LIMIT 20;

-- the exact row from DETAIL
SELECT id, left(url, 80) FROM pages WHERE ctid = '(88120,3)';

octet_length() is the uncompressed size in bytes; pg_column_size() shows how much the value takes in the table after compression. Neither is exactly the index entry size, so treat 2000 bytes as a margin, not a precise threshold.

Pick the right replacement

The fix depends on what the index is for. Ask that first, because the B-tree on the full value is often doing more than the application needs.

  1. Uniqueness. Index a hash of the value, as the error's own hint suggests. The entry is a 32-character MD5 hex string, whatever the input length.
    -- uniqueness on a hash of the value instead of the value itself
    CREATE UNIQUE INDEX CONCURRENTLY pages_url_md5_key ON pages (md5(url));
    
    -- queries must use the same expression to hit the index
    SELECT id FROM pages WHERE md5(url) = md5($1) AND url = $1;
    Keep the url = $1 condition so that the rare hash match on a different value is filtered out. Two different values with the same MD5 would be rejected as duplicates; accidental collisions are not a practical concern, but if users can craft input on purpose, weigh that.
  2. Equality lookups only. Hash indexes store a 32-bit hash code, so value length does not matter, and the planner uses them for =. They do not support ranges or sorting, and only B-tree indexes can be declared unique.
    -- equality lookups only, no uniqueness
    CREATE INDEX CONCURRENTLY pages_url_hash ON pages USING hash (url);
  3. Prefix searches and sorting. Index the first few hundred characters. Five hundred characters stay under 2000 bytes even in UTF-8 with four-byte characters.
    -- an index on the first characters, rechecked against the full value
    CREATE INDEX CONCURRENTLY pages_url_prefix ON pages (left(url, 500));
    
    SELECT id FROM pages WHERE left(url, 500) = left($1, 500) AND url = $1;
  4. Searching inside documents. Use full text search with a GIN index, the other option the hint mentions, instead of a B-tree on the raw text.
  5. No real use. If the index exists only because someone indexed every column, drop it.

Whichever you choose, consider a CHECK on the length if the business rules allow it. NOT VALID followed by VALIDATE adds it without holding a long lock on the table.

-- stop new oversized values at the door, without a long lock
ALTER TABLE pages ADD CONSTRAINT pages_url_len
  CHECK (octet_length(url) <= 2000) NOT VALID;
ALTER TABLE pages VALIDATE CONSTRAINT pages_url_len;

Larger pages are not the answer. The page size is fixed when PostgreSQL is compiled, so a bigger limit means a custom build and a full dump and reload. Changing the index is always cheaper.

On large tables

On a big, busy table the error usually shows up in the middle of a migration, and the cleanup matters as much as the fix.

-- leftovers from failed concurrent builds
SELECT indexrelid::regclass AS index_name, indrelid::regclass AS table_name
FROM pg_index
WHERE NOT indisvalid;

DROP INDEX CONCURRENTLY pages_url_md5_key;

Key takeaways

PostgreSQL Errors Indexes B-tree
Work with us

Index builds failing on long values?

We review PostgreSQL indexes on text and JSON columns, replace oversized B-tree indexes without downtime and keep uniqueness intact on large tables.

Get in touch More from the blog