| From: | Aidar Imamov <imamovaj22(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | [PATCH] Use the table AM relation_size callback in the SQL size functions |
| Date: | 2026-11-25 15:24:42 |
| Message-ID: | DB09BD26-06FA-42B5-BAC5-A52864AA22C8@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
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
| Attachment | Content-Type | Size |
|---|---|---|
| v1_relation_size_am_callback.patch | application/octet-stream | 20.2 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Nathan Bossart | 2026-11-25 15:25:21 | Re: Should we skip foreign key checks in binary upgrade mode? |
| Previous Message | Greg Sabino Mullane | 2026-11-25 15:12:06 | Re: [PATCH] pg_upgrade: add --initdb option to create the new cluster automatically |