Hi Chao,

Thanks for spotting this. I reviewed v1 and I agree with your
analysis: in table_block_relation_size(), the InvalidForkNumber
branch is documented as "returning the size for all forks", but
the loop bound i < MAX_FORKNUM stops at VISIBILITYMAP_FORKNUM,
because MAX_FORKNUM is #defined as INIT_FORKNUM. The fix, making
the bound inclusive, is at the right layer and matches how every
other all-forks loop in the tree is written. With one question on 
backpatching below, which I think is for a committer to decide.

Since there is no in-tree caller passing InvalidForkNumber, this
review is mostly an audit, so here is what I checked
independently (master a20d50a00ed, v1 applies cleanly):

1. Scope check. I enumerated all 48 uses of MAX_FORKNUM in src/
and contrib/. Every loop or comparison over fork numbers uses
<= MAX_FORKNUM (heapam_handler.c, storage.c, tablecmds.c,
smgr.c, md.c, bufmgr.c, dbsize.c, relpath.c, among others);
tableam.c is the only place using < MAX_FORKNUM. The remaining
uses are bounds checks that are also inclusive of INIT, the
definition itself, and array sizing, which is a different
pattern. This confirms Steven's grep, and there is no sibling
site with the same bug.
2. Fork ordering. The enum is MAIN_FORKNUM = 0, FSM_FORKNUM = 1,
VISIBILITYMAP_FORKNUM = 2, INIT_FORKNUM = 3, and MAX_FORKNUM is
INIT_FORKNUM, so i <= MAX_FORKNUM iterates exactly the four
real forks and nothing beyond. smgrnblocks() returns 0 for a
fork that does not exist, so relations without an init fork are
unaffected.
3. Callers. table_block_relation_size() is used in-tree only as
the heap AM's relation_size callback (heapam_handler.c), and
the only caller of that callback's wrapper,
RelationGetNumberOfBlocksInFork(), always receives a specific
fork number from its own callers (MAIN_FORKNUM via
RelationGetNumberOfBlocks(), or an explicit fork from hash,
pageinspect, pg_prewarm and the checksum code). None passes
InvalidForkNumber, as you said. The SQL size functions do not
go through this helper at all: dbsize.c stats the files
directly and already loops with <= MAX_FORKNUM.
4. History. I could not find anything suggesting the exclusion
was ever deliberate. The InvalidForkNumber = total-size
semantics were added on purpose in the PG 12 table AM work
(7f44ede59414), and the helper was split out in 554106b11638
already carrying the < bound, while its own comment says "all
forks". It looks like a plain off-by-one that has been there
since the helper was introduced.

On behaviour, I want to be precise rather than overclaim. I
built master and master + v1 and compared the size functions
on logged and unlogged tables in fresh databases: the outputs
are identical, as expected, because no in-tree path reaches
the changed branch. I also measured the forks themselves: a
heap table's init fork is an empty reset image (0 bytes), so
for heap the excluded fork would add nothing today; an
unlogged index's init fork does hold a block, but indexes do
not use this helper. So the case for the patch is contract
correctness for block-based table AMs that rely on this
helper, plus consistency with the rest of the tree, not a
live mismeasurement in heap. I do not think a regression test
is feasible without adding a test-only in-tree caller passing
InvalidForkNumber, so I am not asking for one.

Tests run (meson build, TAP tests enabled): on unpatched
master and on master + v1 alike, the regression suite passed
243/243 subtests and the isolation suite passed 139/139
subtests, with no failures; on the patched build the recovery
TAP test 001_stream_rep also passed (67 subtests). The patch
touches no documentation, and none is needed.

One question, for the committer rather than for Chao: the
same < MAX_FORKNUM line is present in every supported branch
(REL_14_STABLE through REL_18_STABLE), and the commitfest
entry targets "stable". Against backpatching, as Chao notes,
no in-tree caller is affected today, so master-only would
lose nothing in practice. For backpatching, the helper is a
documented contract for out-of-tree table AMs, and an AM
whose init fork is non-empty would silently under-count on
stable branches. I lean towards this being a bug worth
fixing in the branches, but I am happy with the committer's
call either way.

I used AI assistance: the greps, builds
and test runs above were executed with its help, and the
results are reported as run.

Regards,
Prashant Chinnam

Reply via email to