Re: nbtree: opt-in leaf key prefix compression

From: Greg Burd <greg(at)burd(dot)me>
To: Yao Feng <fengyao0087(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, Peter Geoghegan <pg(at)bowt(dot)ie>
Subject: Re: nbtree: opt-in leaf key prefix compression
Date: 2026-11-27 02:00:56
Message-ID: 179571334264.179421.14888910008050865319@burd.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Nov 23, 2026 at 6:56 PM Greg Burd <greg(at)burd(dot)me> wrote:
> v14 attached, rebased onto 19db7e5b36e.

v17 attached, rebased onto 31878ccc2cd.

For anyone joining at this stage: this adds an opt-in prefix_compression
storage parameter to nbtree, default off. A leaf written as a whole
(CREATE INDEX, or either half of a split) stores the leading bytes that
runs of its keys share once, in a per-page directory, and each item
keeps only its remainder. Sharing is by identical stored bytes, so
decoding reproduces every tuple exactly, any type or collation works
without equalimage, and WAL redo rebuilds coded pages from the record
without the catalog or full-page images.

The one design issue I dug into was what to do with variable-length keys
(varlena). Their length header spoils the prefix: it changes with each
datum's length, so a run of keys that share a long value but differ in
length has the differing header break the shared span. I found a way
that compresses them and keeps all the properties we've had until now,
save one that has to do with splits and the left page.

The coder shares across the header by carving out the header of the one
column where a member first differs from its group's representative.
Every earlier column the member shares whole has the same length, hence
an identical header and alignment, and shares as ordinary bytes; only
the differing column's header stays with the member. So a single-column
text key shares its common prefix past the length header, and a
multi-column key, say (tenant, order_id) or (path, filename), shares the
leading column whole and then the next column's common prefix. Keys
that differ inside the first column still gain little. In my opinion
this was the last bit of "juice worth the squeeze," and the cost was a
bit of WAL.

Decoding a tuple needs the key-column layout (attlen, attalign,
by-value) to find and skip that header. The layout is not in the page
bytes, so the coder records it in the directory, written by the primary
from the catalog; decoding reads it from the page and stays off the
catalog. Redo rebuilds a coded left half the same way: from the
original page's directory when that page was coded, and when the
original was plain, from a small layout the primary logs after the split
record (XLH_SPLIT_LEFT_DESC). Either way redo needs no catalog and no
full-page image.

Measurements, which were tasked to an LLM so have a "LLM voice" below. I
rebuilt master, v14's scheme, and v16 (single-column carving) as release
builds and ran them on a quiesced c7i.metal-24xl (governor performance,
turbo off, THP never, postmaster pinned), 2M rows, six interleaved reps
per arm, with an A/A noise floor. The A/A floor was 0.1-0.6% between
two runs of the same binary, so the differences below are real.

key shape index size CREATE INDEX warm index-only scan
(v16/master) (v16/master) (v16/master)
url, varlen 0.47 1.44 1.50
url, fixed-width 0.47 1.44 1.49
path, varlen 0.76 1.41 1.44
low-card tenant 0.83 1.42 1.58
120-byte prefix 0.46 1.60 1.15

So it is a space-for-time trade: indexes on compressible keys shrink to
roughly half, and both building and scanning them cost about 1.4-1.6x
master. The path shape is where the varlena carving earns itself: v14
could not compress it at all (size 1.00, scan 1.01). The scan cost is
per-tuple decode work on coded leaves. Off-by-default means an index
pays nothing until someone opts in, and an index with the option off is
byte-identical to master.

v17 extends the carving to the second and later variable-length columns.
On a two-column (text, text) key where a low-cardinality leading column
repeats and the next column varies, the index goes from 0.80 of master
under single-column carving to 0.66 under v17 (1M rows, same build and
machine family; this one I have not yet put through the full A/A rig).

Whether that trade is worth carrying is the design question.

(Other) Open questions.

1. Is a per-page directory plus a per-item code header the format to
carry forever? Yao's prefix and suffix zones avoid the per-item header
but need a page-header field; Peter's prototype shares whole leading
columns. The header plus MAXALIGN erases the gain on short keys, e.g.
(int4, int8, timestamptz), which is why short keys are not in the table
above.

2. Anchors come from the keys on the page when it is written; later
inserts that do not match stay uncoded until the next whole-page write.
Is that acceptable, or should insert or deduplication be able to regroup
a page?

best.

-greg

Attachment Content-Type Size
v17-0001-nbtree-add-opt-in-leaf-key-prefix-compression.patch application/octet-stream 265.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Burd 2026-11-27 02:16:50 Re: ZSTD TOAST compression, and an extensible compression method encoding
Previous Message Chao Li 2026-11-27 01:38:20 Skip Var resolution when checking expression type dependencies