Re: Fix exclusion of the init fork in table_block_relation_size()

From: Steven Niu <niushiji(at)gmail(dot)com>
To: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, Pgsql Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix exclusion of the init fork in table_block_relation_size()
Date: 2026-11-18 08:34:53
Message-ID: EAYPR19MB4817117A772A5A09CF55535E9EA77A2@EAYPR19MB481711.namprd19.prod.outlook.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

________________________________________
From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Sent: Wednesday, November 18, 2026 15:46
To: Pgsql Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
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.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexandra Wang 2026-11-18 08:44:39 Re: Is there value in having optimizer stats for joins/foreignkeys?
Previous Message vignesh C 2026-11-18 08:33:51 Re: Hash index: stop registering pin-only bucket pages in WAL (remove REGBUF_NO_CHANGE)