On Tue, Dec 1, 2026 at 3:27 PM Greg Burd <[email protected]> wrote:
> v19 attached, rebased onto e87b024bfb8.

Hi Greg,

I ran some insert/split benchmarks on v19. Random inserts on compressible
keys look good (~50% smaller index, ~50% fewer splits, 14%-25% faster), but
I noticed a problem with strictly ascending inserts (e.g. append-only
timestamped URLs or (tenant, ts, ...) keys).

Here is a small reproducer:

  CREATE EXTENSION IF NOT EXISTS pgstattuple;

  CREATE UNLOGGED TABLE src_seq AS
  SELECT 'https://api.service.example.com/v1/tenants/tenant_'
         || lpad(((g - 1) / (300000 / 50))::text, 3, '0')
         || '/region/us-east-1/events/ts_'
         || lpad(g::text, 12, '0')
         || '_payload_suffix' AS k
  FROM generate_series(1, 300000) AS g
  ORDER BY k;

  CREATE TABLE t_off (k text NOT NULL);
  CREATE INDEX idx_off ON t_off (k) WITH (prefix_compression = off);
  INSERT INTO t_off SELECT k FROM src_seq;

  CREATE TABLE t_on (k text NOT NULL);
  CREATE INDEX idx_on ON t_on (k) WITH (prefix_compression = on);
  INSERT INTO t_on SELECT k FROM src_seq;

  SELECT 'off' AS mode, pg_size_pretty(pg_relation_size('idx_off')) AS size,
         leaf_pages, round(avg_leaf_density::numeric, 1) AS leaf_density
  FROM pgstatindex('idx_off')
  UNION ALL
  SELECT 'on', pg_size_pretty(pg_relation_size('idx_on')),
         leaf_pages, round(avg_leaf_density::numeric, 1)
  FROM pgstatindex('idx_on');

On v19 (300k rows, 108B text with ~90B shared prefix, median of 3 runs,
shared_buffers=1GB, synchronous_commit=off):

  mode | insert time     | index size       | leaf pages | leaf density | WAL
  
-----+-----------------+------------------+------------+--------------+-----------------
  off  | 1222 ms         | 41.34 MB         | 5173       | 89.7%
 | 86.48 MB
  on   | 1157 ms (-5.3%) | 37.45 MB (-9.4%) | 4687       | 37.8%
 | 87.72 MB (+1.4%)

With prefix_compression = on, the index only shrinks by 9.4%, leaf density
sits at 37.8%, and WAL actually grows slightly (+1.4%).

Looking at the split path, this happens because _bt_prefix_split_needed()
splits the rightmost leaf once its decoded size nears 2 * usable (~129
tuples, ~5.8KB on disk), and then _bt_split() builds both halves as plain
pages first. Since _bt_prefix_findsplitloc() requires decoded left <= usable
and right <= usable, the split is forced near 50/50 (~64 tuples on the left
page) even with fillfactor = 90. Once _bt_prefix_compress_page() re-encodes
the left half, its physical occupancy drops to 37.8%. For strictly ascending
inserts, nothing ever gets inserted into that left page again, so every
finalized leaf stays stuck at ~64 tuples (compared to ~58 with compression
off).

Since btree_xlog_split() already builds a coded left page directly from
litems[] via _bt_prefix_form_page(), could rightmost splits pack leftpage
directly in coded form up to fillfactor / the 2 * usable budget instead of
capping it at one plain page?

Two smaller things I also noticed while looking at that path:

- _bt_search_insert() skips the rightmost fastpath whenever
  P_HASPREFIX(opaque) is set, so sequential inserts always descend from the
  root. Given that _bt_prefix_split_needed() and the code-overhead check are
  O(1), is there a reason we can't keep the fastpath for coded rightmost
  leaves?

- In _bt_split(), _bt_prefix_try_plain_page() decodes tuples into
  plainscratch_buf until PageAddItem() overflows and returns NULL, and then
  _bt_prefix_decode_items() decodes the whole page a second time. Checking
  hdr->decodedsz + hikeysz > usable up front would avoid that wasted decode
  pass.

One broader question about the split mechanism itself: why does _bt_split()
have to decode the whole page, split in plain space, and re-compress both
halves, instead of splitting directly on the coded page? I understand the
decode-then-recompress path makes it easy to reuse the existing split logic,
refresh stale anchors, and guarantee two plain halves if prefix_compression
is later turned off. Still, it pays full decode + re-encode overhead on
every split, caps compression at ~2x, and leads to the rightmost density
issue above. Have you considered splitting directly in coded space (e.g.,
partitioning and copying coded items while trimming/remapping unused anchors
per half, and only rebuilding the directory when compression degrades), or
do WAL redo and split-fit guarantees make that impractical?

Regards,
Yao


Reply via email to