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-12-01 15:27:09
Message-ID: 179606094018.3032808.12955334360302614488@burd.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Nov 28, 2026 at 1:38 AM Greg Burd <greg(at)burd(dot)me> wrote:
> v18 attached, rebased onto e347dae2d40. One fix.

Hello.

cfbot's Linux Meson 64-bit job builds with AddressSanitizer and caught a
stack-use-after-scope in the same split path v18 touched. v19 attached,
rebased onto e87b024bfb8.

I don't think I've covered the design of a few important concepts for
this project, the largest of which is how does a leaf page store keys
when they are prefix-compressed with this patch?

Let me introduce: GROUPS, ANCHOR, and the PREFIX DIRECTORY.

A leaf page flagged BTP_PREFIX has its data items are divided into
GROUPS of consecutive items in key order.

If the keys on that page were:
https://www.postgresql.org/message-id/CAA1%40mail.gmail.com
https://www.postgresql.org/message-id/CAB2%40mail.gmail.com
https://www.postgresql.org/message-id/CAC3%40mail.gmail.com

The first group would be ANCHOR'ed with:
"https://www.postgresql.org/message-id/CAA1%40mail.gmail.com"
a full copy of the group's first key tuple.

If the page then continued with:
https://www.postgresql.org/message-id/FRE1%40mail.gmail.com
https://www.postgresql.org/message-id/FRE2%40mail.gmail.com

The next group would be anchored with:
"https://www.postgresql.org/message-id/FRE1%40mail.gmail.com"
and so on.

Every member of the group is stored "coded", holding only the bytes it
does not share with its anchor. So for the second key in the first
group the coded portion is: "2%40mail.gmail.com" and so on.

These anchors live in one PREFIX DIRECTORY item which can be located
using lp[1] is the high key, lp[2] is the directory (BTPgeGetPrefixOff
returns the high-key slot, i.e. 1, on a non-rightmost page). Nothing
in the page header or opaque area changes; the only structural change is
that one slot before the data keys and the flag (BTP_PREFIX set in
btpo_flags).

The prefix-compressed leaf thus far looks like:

off +--------------------------------------------------------------+
| PageHeaderData |
24 +--------------------------------------------------------------+
| lp[1]=high key lp[2]=DIRECTORY linp[n..]=coded data items |
+--------------------------------------------------------------+
| free space mid-page |
+--------------------------------------------------------------+
| [ coded member M ] |
| ... |
P_FIRSTDATAKEY (3) -> [ coded member 1 ] |
| +------------------------------------------+ |
dir slot (2) ---> | PREFIX DIRECTORY (one IndexTuple) | |
| +------------------------------------------+ |
P_HIKEY (1) -> [ high key ] (never coded) |
special +------------------------------------------------------------+
| BTPageOpaqueData btpo_flags |= BTP_PREFIX (0x200) |
+--------------------------------------------------------------+

So, what is inside the prefix directory item (BTPrefixDirHdr)?

byte +--------------------------------------------------------+
0 | IndexTupleData header (t_tid invalid; t_info = size) | 8 bytes
8 | ngroups (uint16) |
10 | decodedsz (uint16) sum of decoded item sizes + LPs |
12 | maxdecoded (uint16) largest decoded item + LP | -> O(1) split-fit
14 | natts (uint16) key-column count |
16 | BTPrefixAttr[0] {int16 attlen, uint8 alignby, flags} | 4 bytes each
| BTPrefixAttr[1] ... | (one per key col)
+--------------------------------------------------------+
| anchoroff[0] (uint16) anchoroff[1] ... [ngroups-1] | offsets into item
+--------------------------------------------------------+
| BTPrefixAnchorDisk[0]: {uint16 repsz}[pad] + rep tuple | MAXALIGN'd
| BTPrefixAnchorDisk[1]: ... |
+--------------------------------------------------------+

An ordinary IndexTupleData header (invalid t_tid, t_info = whole item
size) followed by a fixed header, the key-column layout, the anchor
offset array, then the packed anchors.

The BTPrefixAttr layout is what makes decode page-only possible. You
cannot parse a data area into columns from the bytes alone (a 1-byte
short-varlena header is indistinguishable from fixed data), so the
per-column attlen/alignment/by-value is stored here. 'rep' is a full
copy of the group's first key.

Inside one coded member (BTPrefixCode)

byte +--------------------------------------------------------+
0 | IndexTupleData header (t_tid = member's heap TID / | 8 bytes
| posting-list metadata) |
8 | group (uint16) which anchor |
10 | lead_shared (uint16) = D: data-area bytes shared | code header
12 | shared_len (uint16) = sv: divergent col's shared | (8 bytes)
| value bytes |
14 | orig_info (uint16) original tuple's t_info |
16 | null bitmap (if the original had nulls) |
| divergent column's own length header | residual
| residual value bytes (from divergent col onward) |
| posting list, standard layout (if a posting tuple) |
+--------------------------------------------------------+

Now that we know the layout, how does a member decode against its anchor?

Decode reconstructs the original data area by splicing four runs.
D = lead_shared, sv = shared_len;
ahdr/mhdr are the anchor's and member's divergent-column length-header
widths.

anchor data area: [ ==== shared leading columns [0..D) ==== | hdr | value...... ]
D D+ahdr

member stores: [ null bitmap | mhdr | residual (divergent col tail + later cols) ]

decoded tuple = anchor[0 .. D) (fully-shared leading columns)
+ member's mhdr (divergent col's own length header)
+ anchor[D+ahdr .. D+ahdr+sv) (shared value prefix of divergent col)
+ member residual (the rest)

lead_shared > 0 is the multi-column behavior. Sharing continues across
across a fully-equal leading column into the next one. A single-column
key always has lead_shared = 0.

This long email is really a short/small fix and a past-due explanation
of the core design elements.

-greg

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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2026-12-01 15:29:42 Re: psql single-step mode woes
Previous Message Rajesh Kumar Thandapani 2026-12-01 15:24:13 Add aborted_txns and aborted_bytes to pg_stat_replication_slots