________________________________________
From: Chao Li <[email protected]>
Sent: Wednesday, November 18, 2026 15:46
To: Pgsql Hackers <[email protected]>
Subject: Fix exclusion of the init fork in table_block_relation_size()


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/






Agreed. I searched the source code with grep -r MAX_FORKNUM and found this was 
the only occurrence of < MAX_FORKNUM; all other comparisons used <=
  MAX_FORKNUM.



Reply via email to