Re: Mis-use of type BlockNumber?

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Rafia Sabih <rafia(dot)pghackers(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Mis-use of type BlockNumber?
Date: 2026-03-06 09:17:42
Message-ID: 055A4656-10B2-49F6-BB30-DFF5C127C9A3@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Mar 6, 2026, at 15:56, Rafia Sabih <rafia(dot)pghackers(at)gmail(dot)com> wrote:
>
>
>
> On Thu, 5 Mar 2026 at 19:26, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
> Hi,
>
> While reviewing [1], I noticed several cases where BlockNumber seems to be misused.
>
> Although BlockNumber is currently underlying defined as uint32, it has a special meaning. For example:
> ```
> #define InvalidBlockNumber ((BlockNumber) 0xFFFFFFFF)
> #define MaxBlockNumber ((BlockNumber) 0xFFFFFFFE)
> ```
> So my understanding is that BlockNumber should only be used to identify a block.
> However, I saw several places where variables of type BlockNumber are actually used as counts. For example:
> ```
> typedef struct LVRelState
> {
>
> BlockNumber blkno; <== correct usage
>
> BlockNumber rel_pages; /* total number of pages */ <== mis-use
> ```
> Actually, InvalidBlockNumber and MaxBlockNumber are special values, not the BlockNumber itself, it is as you said underlying uint32.
> AFAIk these types for typedef are done so that we understand them in a particular context and not just use them as any other uint32. Increases the code readability.
> There are other such examples too like Bucket in Hash.

Hi Rafia,

Thanks for sharing your opinion. However, I am not fully convinced.

When we use a type, we usually don’t need to care about its underlying actual type. If one day BlockNumber were redefined as a structure type, then all usages where it is treated as a counter would break, right? While such a change may be unlikely, it is not impossible. For example, there is an ongoing discussion [1] proposing to change Datum into a structure, while Datum is currently defined as uint64_t.

To me, this kind of misuse also hurts readability. As I mentioned earlier, I noticed the issue while reviewing a patch: I was confused by the definition of a struct field until I read the implementation and realized that it was actually being used as a counter.

[1] https://postgr.es/m/8246d7ff-f4b7-4363-913e-827dadfeb145@eisentraut.org

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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2026-03-06 09:23:11 Re: Make copyObject work in C++
Previous Message Pavel Stehule 2026-03-06 09:06:42 Re: proposal: schema variables