Add TOAST statistics columns to pg_stat_all_tables

Started by Shinya Kato20 days ago1 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

appliessuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t253324
psql -h localhost -U postgres

Built from patchset v1 (message #1), August 25, 2026 at 12:16 PM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t253324_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t253324_1 && git checkout t253324_1

Patchset v1 (message #1) is on t253324_1

Jump to latest
#1Shinya Kato
shinya11.kato@gmail.com

Hi hackers,

I am proposing a patch that adds four columns to pg_stat_all_tables
(toast_relid, toast_n_dead_tup, toast_last_autovacuum, and
toast_autovacuum_count), so that the state of a table's TOAST table
can be observed from the owning table's row.

Motivation:

Statistics for TOAST tables already exist, but they are hard to reach
when troubleshooting. They appear only as separate rows in the
pg_toast schema, pg_stat_user_tables filters that schema out entirely,
and autovacuum processes a TOAST table independently of its main
table. As a result, a table can look perfectly healthy (n_dead_tup =
0, recently autovacuumed) while most of the dead space is in its TOAST
table, and nothing in the standard views hints that one should join
pg_class.reltoastrelid by hand. The typical symptom is that
pg_table_size() keeps growing while n_dead_tup stays near zero.

Design:

pg_statio_all_tables already exposes TOAST activity on the owning
table's row (toast_blks_read and toast_blks_hit) by joining pg_class
on reltoastrelid, so this patch applies the same pattern to
pg_stat_all_tables. The new columns call the existing statistics
functions on the TOAST relation's OID, so no new counters or C
functions are introduced, and tables without a TOAST table show NULLs.
The TOAST values are deliberately not folded into the table's own
counters, because TOAST tuples are chunks of long values and mixing
the units would make the numbers meaningless.

The patch is attached.

Thoughts?

--
Best regards,
Shinya Kato
NTT OSS Center

Attachments:

t253324_1
v1-0001-Add-TOAST-statistics-columns-to-pg_stat_all_table.patchapplication/octet-stream; name=v1-0001-Add-TOAST-statistics-columns-to-pg_stat_all_table.patchDownload+135-4