Fix exclusion of the init fork in table_block_relation_size()

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Pgsql Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Fix exclusion of the init fork in table_block_relation_size()
Date: 2026-11-18 07:46:01
Message-ID: 1B0943B5-0329-495D-B693-564A6F72C158@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I spotted this issue with TAM while working on patch [1].

Looking at the code:
```
uint64
table_block_relation_size(Relation rel, ForkNumber forkNumber)
{
uint64 nblocks = 0;

/* InvalidForkNumber indicates returning the size for all forks */
if (forkNumber == InvalidForkNumber)
{
for (int i = 0; i < MAX_FORKNUM; i++)
nblocks += smgrnblocks(RelationGetSmgr(rel), i);
}
```

When forkNumber is InvalidForkNumber, the comment clearly says to return the size for all forks. But the code uses i < MAX_FORKNUM, and MAX_FORKNUM is defined as:
```
#define MAX_FORKNUM INIT_FORKNUM
```

Thus, INIT_FORKNUM is excluded.

As a comparison, calculate_toast_table_size() in dbsize.c includes MAX_FORKNUM:
```
/* toast heap size, including FSM and VM size */
for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
size += calculate_relation_size(&(toastRel->rd_locator),
toastRel->rd_backend, forkNum);
```

This trivial patch only changes "i < MAX_FORKNUM" to "i <= MAX_FORKNUM", so that INIT_FORKNUM is included.

This doesn't affect current in-tree callers, because the current source tree has no call site that passes InvalidForkNumber to table_block_relation_size(). So, maybe it’s okay to fix this only on master.

[1] https://www.postgresql.org/message-id/DE69B65E-4621-4AF4-9A00-6A042E565C90%40gmail.com

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachment Content-Type Size
v1-0001-Fix-exclusion-of-the-init-fork-in-table_block_rel.patch application/octet-stream 1.2 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bertrand Drouvot 2026-11-18 07:47:15 Re: Fix ReadStream handling when heap rescan strategy changes
Previous Message Amit Langote 2026-11-18 07:41:17 Re: Two more RI fast-path issues