[PATCH] Use the table AM relation_size callback in the SQL size functions
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
Hi,
pg_relation_size() and pg_table_size() measure a relation by summing up its
files. That works for heap, but an AM may keep its data elsewhere, and
then the functions report 0 while pg_database_size() sees the files just
fine.
The patch makes them ask the AM for the main fork instead, and the same for
the toast table's main fork, since relation_toast_am() can point to a
different AM. Other forks, indexes and relations without a table AM are
left alone.
Heap doesn't change: heapam's callback already reports nblocks * BLCKSZ.
I know this was attempted before and didn't go far. Those versions also
changed what the functions return for views and what psql prints, and the
discussion went there. This one only changes who measures the main fork.
The callback is user-visible now, so I've written down what it should
return: the physical footprint of the relation's own storage for
MAIN_FORKNUM, without the toast table and its indexes, the overall
footprint for InvalidForkNumber, and file sizes for the other forks. There
is a sentence in the docs too.
There's a test module, src/test/modules/dummy_table_am -- a copy of heapam
whose relation_size() returns half of the heap footprint. The test checks
that pg_relation_size() returns that number, that pg_table_size() and
pg_total_relation_size() count it once instead of adding it to the file
sizes, and that fsm and vm come on top. Toast isn't covered: the module's
toast table is a plain heap one.
Two things I couldn't decide on:
- table_relation_size() returns uint64, the SQL functions return int64, and
the patch just casts. Fine as is, or should it be handled differently?
- pg_table_size() still sums the per-fork sizes instead of asking the AM
for InvalidForkNumber. An AM's overall size need not be the sum of the
forks (table_block_relation_size() skips the init fork, for one), and
pg_table_size() has always added them up, so I left the loop in place.
Regards,
Aidar Imamov
On Wed, 25 Nov 2026 at 16:25, Aidar Imamov <imamovaj22@gmail.com> wrote:
pg_relation_size() and pg_table_size() measure a relation by summing up its
files. That works for heap, but an AM may keep its data elsewhere, and
then the functions report 0 while pg_database_size() sees the files just
fine.
Where does such a hypothetical AM store its data, if not in the files
it's designated to use through its relfilenode association? And why
would pg_database_size() be guaranteed to know where to look for the
relation's data, if pg_relation_size() itself isn't able to find this
data?
Lastly, why wouldn't CREATE DATABASE ... TEMPLATE break if relation
data in the template database isn't stored in the place that
PostgreSQL expects it to be stored? Shouldn't that be accomodated,
too, if we were to allow table AMs to store their data in nonstandard
places?
Kind regards,
Matthias van de Meent
Databricks (https://www.databricks.com)
On Nov 25, 2026, at 18:59, Matthias van de Meent <boekewurm+postgres@gmail.com> wrote:
On Wed, 25 Nov 2026 at 16:25, Aidar Imamov <imamovaj22@gmail.com> wrote:
pg_relation_size() and pg_table_size() measure a relation by summing up its
files. That works for heap, but an AM may keep its data elsewhere, and
then the functions report 0 while pg_database_size() sees the files just
fine.Where does such a hypothetical AM store its data, if not in the files
it's designated to use through its relfilenode association? And why
would pg_database_size() be guaranteed to know where to look for the
relation's data, if pg_relation_size() itself isn't able to find this
data?Lastly, why wouldn't CREATE DATABASE ... TEMPLATE break if relation
data in the template database isn't stored in the place that
PostgreSQL expects it to be stored? Shouldn't that be accomodated,
too, if we were to allow table AMs to store their data in nonstandard
places?Kind regards,
Matthias van de Meent
Databricks (https://www.databricks.com)
My wording was bad: "an AM may keep its data elsewhere" makes it sound
like the AM picks its own storage location, and it doesn't.
What actually happens is that the AM gets a relfilenode from
relation_set_new_filelocator() and puts its own files next to the
standard forks, in the same database (or tablespace) directory, named
after that relfilenode -- <relfilenode>.dat.N or whatever suffix it
uses. The relfilenode association is there; the files are just not one
of the forks core knows how to look for. The questions below came from
that bad wording.
pg_database_size() just sums what sits in the database directory, like
du, so it sees those files without knowing anything about them.
CREATE DATABASE only breaks with WAL_LOG, which copies relations block by
block and knows nothing about extra files; FILE_COPY works. The
extension behind this refuses WAL_LOG for such templates today, and a
core-side fallback is a separate patch.
And v2 fixes a bug in v1: pg_relation_size() of a heap table whose main
fork file is missing -- an unlogged table on a standby -- started to
fail. Heap keeps the file-based measurement now, the callback is only
asked for custom table AMs; the only code change is in
calculate_main_relation_size().
Regards,
Aidar Imamov