Re: ZSTD TOAST compression, and an extensible compression method encoding

From: Greg Burd <greg(at)burd(dot)me>
To: Hannu Krosing <hannuk(at)google(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, Manu <manuelreyesbravo(at)gmail(dot)com>, Nikhil Kumar Veldanda <veldanda(dot)nikhilkumar17(at)gmail(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz>, Japin Li <japinli(at)hotmail(dot)com>, Dharin Shah <dharinshah95(at)gmail(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>
Subject: Re: ZSTD TOAST compression, and an extensible compression method encoding
Date: 2026-11-27 02:16:50
Message-ID: 179564151233.3329924.11495797862073296879@burd.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, Nov 22, 2026 at 4:52 PM Greg Burd <greg(at)burd(dot)me> wrote:
> v21 is attached, rebased onto 3ed10f282f9.

Hi all,

v22 is attached, rebased onto 4483697be0e.

Dictionary compression for TOAST stalled in 2025 on one question: a
value compressed with a dictionary can be copied out of its table, so
when is it safe to drop the dictionary? That proposal tracked the
tables that might hold a dictionary's values in pg_depend, which
spreads to every table a value can reach, changes catalogs under
RowExclusiveLock, and still depends on a list of copy paths nobody can
prove complete [1]. The alternative, keeping values from leaking out
of their table at all, had been tried for lz4 in 2021 and given up,
partly over the cost to every compressed value and partly because
nobody was sure every exit could be found [2].

This series takes that alternative, but only for dictionary values, and
enforces it at the exits TOAST pointers already need. A dictionary
belongs to one column, the way a pg_statistic row does, and a value
compressed with it never leaves that column compressed.
heap_fill_tuple() sets HEAP_HASEXTERNAL on a tuple that holds such a
value, and the places that test one value use a new
VARATT_IS_EXTERNAL_OR_DICT(), so composites, tuplestores, holdable
cursors, the syscache, and index tuples decompress it where they already
detoast TOAST pointers. A PL/pgSQL record, which may keep TOAST
pointers until the transaction ends, always decompresses it. A store
into another column decompresses it too; an UPDATE that leaves the
column alone, or a rewrite while the dictionary is current, keeps it
compressed. So no dependencies are tracked, a table's dictionaries go
with the table, and a superseded dictionary is deleted at the next
rewrite.

The cost for people who don't use dictionaries, on a bare-metal host
with base and tip alternating over five rounds: INSERT, INSERT ...
SELECT, non-key UPDATE, and DELETE with pglz, lz4, and uncompressed
narrow rows take at most 1.004 times master's time, while the A/A
comparison itself moves by up to 1.7%. The narrow-row cases, which come
out fastest at 0.94 and 0.97, stay within 0.002% of master's instruction
count under callgrind, so I take them to be code layout.

On JSONB data averaging 2.6kB per document: zstd_dict stores 200,000 of
them in 56MB, against 109MB for zstd and 145MB for pglz, and reads them
back faster than zstd. lz4_dict keeps lz4's read speed in 40% less
space. The main price is a copy into a column that does not own the
dictionary, which decompresses and recompresses: INSERT ... SELECT of
3.4kB documents into another table is 9x slower for zstd_dict and 2.6x
for lz4_dict than copying plain zstd or lz4 values as they are. Content
a dictionary cannot predict also compresses more slowly with one
(notes.txt, section 3).

0001-0003 (decoding TOAST pointers in one place, the long-form header
that makes room for more than four methods, and zstd) have the same
patch-ids as in v21 and no new findings for several versions, and I
believe they are ready for a committer. 0004 and 0005 (dictionaries and
their containment, and retraining from ANALYZE) are, I believe, ready
too. They carry more risk only because they add new kinds of
complexity, not because of any known problem: a catalog whose OIDs every
dictionary-compressed value stores, a wider meaning for HEAP_HASEXTERNAL
that every path copying a tuple out of its table now relies on, and
catalog writes from ANALYZE.

Apart from new tests, v22 changes only how lz4_dict compresses: with lz4
1.10 or later it attaches a dictionary prepared once
(LZ4_attach_dictionary()) instead of rehashing it for every value.
Loading the 200,000 2.6kB documents drops from 4.9s to 1.4s, against
1.2s for lz4 without a dictionary.

Two questions about rollback have come up since v21, and both are now
tested. A dictionary trained in a transaction that aborts leaves
nothing a later reader can reach, since the rows that used it die with
it. A value held in a PL/pgSQL record across that rollback stays
readable even after a different dictionary takes the same OID; a new TAP
test forces exactly that, and fails without the record flattening v21
added.

For a reviewer of 0004, the places to check are the single-value tests
of VARATT_IS_EXTERNAL() in core and contrib, each classified in section
6 of the attached notes.txt, and the rule that an extension type which
embeds other varlenas must detoast them, as it already must for TOAST
pointers.

Besides the per-patch builds and test-world, v22 passed valgrind
memcheck over the compression suites and, on s390x, make check and
amcheck. I also ran a 5.4-hour soak of the v21 code (v22 differs only
in lz4_dict compression), a release build on master, with crash cycles,
a streaming standby, and logical decoding, each checked row by row.
Setups and numbers are in notes.txt.

best.

-greg

[1] https://postgr.es/m/CA+Tgmob1Ux7R2_0mbZmTbsVmUda04X1AdHGjpu6FkZfdbztugQ@mail.gmail.com
[2] https://postgr.es/m/20210316185455.5gp3c5zvvvq66iyj@alap3.anarazel.de

Attachment Content-Type Size
v22-0001-Decode-TOAST-pointers-fully-in-toast_external_in.patch text/x-patch 13.0 KB
v22-0003-Add-zstd-as-a-TOAST-compression-method.patch text/x-patch 97.1 KB
notes.txt text/plain 15.6 KB
v22-0002-Allow-more-than-four-TOAST-compression-methods.patch text/x-patch 52.8 KB
v22-0005-Add-automatic-dictionary-evolution-during-ANALYZ.patch text/x-patch 101.5 KB
v22-0004-Add-TOAST-compression-dictionaries-for-zstd-and-.patch text/x-patch 226.4 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro Horiguchi 2026-11-27 02:28:01 Re: Safer and simpler HTAB macros (foreach_hash and hash_make), take two
Previous Message Greg Burd 2026-11-27 02:00:56 Re: nbtree: opt-in leaf key prefix compression