Re: Mis-use of type BlockNumber? - Mailing list pgsql-hackers

From Chao Li
Subject Re: Mis-use of type BlockNumber?
Date
Msg-id 055A4656-10B2-49F6-BB30-DFF5C127C9A3@gmail.com
Whole thread Raw
In response to Re: Mis-use of type BlockNumber?  (Rafia Sabih <rafia.pghackers@gmail.com>)
List pgsql-hackers

> On Mar 6, 2026, at 15:56, Rafia Sabih <rafia.pghackers@gmail.com> wrote:
>
>
>
> On Thu, 5 Mar 2026 at 19:26, Chao Li <li.evan.chao@gmail.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
underlyinguint32. 
> AFAIk these types for typedef are done so that we understand them in a particular context and not just use them as
anyother 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
redefinedas a structure type, then all usages where it is treated as a counter would break, right? While such a change
maybe 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:
Iwas confused by the definition of a struct field until I read the implementation and realized that it was actually
beingused 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/







pgsql-hackers by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: proposal: schema variables
Next
From: Peter Eisentraut
Date:
Subject: Re: Make copyObject work in C++