PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 54000: index row size exceeds btree maximum 2704

A value is too large for a B-tree index entry (max 2704 bytes).

PostgreSQL 17.12
54000: index row size 3248 exceeds btree version 4 maximum 2704 for index "pages_url_uk"

Jump to a cause

  1. 01UNIQUE index on a long text column (URL)
  2. 02INSERT of a long value into a column with a normal B-tree index

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17

SQLSTATE 54000 (program_limit_exceeded) here means a B-tree index entry would be larger than 2704 bytes. The HINT line says it directly: values larger than 1/3 of a buffer page cannot be indexed, and suggests an MD5 function index.

It can happen when you create the index (an existing row is too long) or later on INSERT/UPDATE (a new value is too long). The statement fails as a whole.

01UNIQUE index on a long text column (URL)

One stored URL is 3229 bytes. CREATE UNIQUE INDEX ... (url) would need an index entry of 3248 bytes, over the 2704 limit, so the index is not created.

Setup
Setuprun as lab_a
CREATE TABLE pages (page_id int PRIMARY KEY, url text NOT NULL);
INSERT INTO pages VALUES (1, 'https://example.com/a');
INSERT INTO pages
SELECT 2, 'https://example.com/search?q=' || string_agg(md5(i::text), '') FROM generate_series(1, 100) AS i;
OUTPUT
CREATE TABLE
INSERT 0 1
INSERT 0 1
Reproducerun as lab_a
CREATE UNIQUE INDEX pages_url_uk ON pages (url);
✕ ERROR
ERROR:  54000: index row size 3248 exceeds btree version 4 maximum 2704 for index "pages_url_uk"
DETAIL:  Index row references tuple (0,2) 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.
SCHEMA NAME:  lab_a
TABLE NAME:  pages
CONSTRAINT NAME:  pages_url_uk
Check the causerun as lab_a
SELECT page_id, length(url) AS chars, octet_length(url) AS bytes FROM pages ORDER BY page_id;
OUTPUT
 page_id | chars | bytes
---------+-------+-------
       1 |    21 |    21
       2 |  3229 |  3229
(2 rows)

octet_length(url) is 21 for page 1 and 3229 for page 2. Page 2 is the row that does not fit.

Fixrun as lab_a
CREATE UNIQUE INDEX pages_url_md5_uk ON pages (md5(url));
OUTPUT
CREATE INDEX

Index md5(url) instead of the text itself. The MD5 is always 32 characters and still enforces uniqueness of the URL. A new long URL (3236 characters) was then inserted, and \di shows pages_url_md5_uk.

After the fixrun as lab_a
INSERT INTO pages SELECT 3, url || '&page=2' FROM pages WHERE page_id = 2;
SELECT page_id, length(url) AS chars FROM pages ORDER BY page_id;
\di pages*
✓ SUCCESS
INSERT 0 1
 page_id | chars
---------+-------
       1 |    21
       2 |  3229
       3 |  3236
(3 rows)

                 List of relations
 Schema |       Name       | Type  | Owner | Table
--------+------------------+-------+-------+-------
 lab_a  | pages_pkey       | index | lab_a | pages
 lab_a  | pages_url_md5_uk | index | lab_a | pages
(2 rows)

02INSERT of a long value into a column with a normal B-tree index

request_body has a plain B-tree index for lookups. Short bodies work, but a 3213-character JSON body makes the INSERT fail with 54000 on api_logs_body_ix.

Setup
Setuprun as lab_a
CREATE TABLE api_logs (log_id int PRIMARY KEY, request_body text);
CREATE INDEX api_logs_body_ix ON api_logs (request_body);
INSERT INTO api_logs VALUES (1, '{"item": 1}');
OUTPUT
CREATE TABLE
CREATE INDEX
INSERT 0 1
Reproducerun as lab_a
INSERT INTO api_logs
SELECT 2, '{"items": "' || string_agg(md5(i::text), '') || '"}' FROM generate_series(1, 100) AS i;
✕ ERROR
ERROR:  54000: index row size 3232 exceeds btree version 4 maximum 2704 for index "api_logs_body_ix"
DETAIL:  Index row references tuple (0,2) in relation "api_logs".
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.
SCHEMA NAME:  lab_a
TABLE NAME:  api_logs
CONSTRAINT NAME:  api_logs_body_ix
Check the causerun as lab_a
SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'api_logs' ORDER BY indexname;
OUTPUT
    indexname     |                                  indexdef
------------------+----------------------------------------------------------------------------
 api_logs_body_ix | CREATE INDEX api_logs_body_ix ON lab_a.api_logs USING btree (request_body)
 api_logs_pkey    | CREATE UNIQUE INDEX api_logs_pkey ON lab_a.api_logs USING btree (log_id)
(2 rows)

pg_indexes shows api_logs_body_ix is a btree index on the whole request_body.

Fixrun as lab_a
DROP INDEX api_logs_body_ix;
CREATE INDEX api_logs_body_hix ON api_logs USING hash (request_body);
OUTPUT
DROP INDEX
CREATE INDEX

For equality lookups, a hash index has no such size limit on the value. After replacing it with USING hash, the same INSERT succeeded (row 2, 3213 characters).

After the fixrun as lab_a
INSERT INTO api_logs
SELECT 2, '{"items": "' || string_agg(md5(i::text), '') || '"}' FROM generate_series(1, 100) AS i;
SELECT log_id, length(request_body) AS chars FROM api_logs ORDER BY log_id;
✓ SUCCESS
INSERT 0 1
 log_id | chars
--------+-------
      1 |    11
      2 |  3213
(2 rows)

FAQ

How do I fix 'index row size exceeds btree version 4 maximum 2704'?

Do not index the long value directly. Index md5(col) (it keeps UNIQUE working), or replace the B-tree index with a hash index when you only look up by equality.

Why 2704 bytes?

A B-tree entry must fit in about 1/3 of an 8 kB page. The HINT says so: values larger than 1/3 of a buffer page cannot be indexed.

Does 54000 happen at CREATE INDEX or at INSERT?

Both. With a long row already in the table, CREATE INDEX fails. With the index already there, the INSERT of the long value fails.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12