Skip site navigation (1) Skip section navigation (2)

PageGetMaxOffsetNumber on uninitialized pages

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org
Subject: PageGetMaxOffsetNumber on uninitialized pages
Date: 2004-06-03 15:29:38
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-hackers
I was just looking at this macro:

 * PageGetMaxOffsetNumber
 *        Returns the maximum offset number used by the given page.
 *        Since offset numbers are 1-based, this is also the number
 *        of items on the page.
 *        NOTE: to ensure sane behavior if the page is not initialized
 *        (pd_lower == 0), cast the unsigned values to int before dividing.
 *        That way we get -1 or so, not a huge positive number...
#define PageGetMaxOffsetNumber(page) \
    (((int) (((PageHeader) (page))->pd_lower - SizeOfPageHeaderData)) \
     / ((int) sizeof(ItemIdData)))

The macro does the right thing on its own terms when applied to a zeroed
page, but in some places it's used like this:

    OffsetNumber n;
    OffsetNumber maxoff;

    maxoff = PageGetMaxOffsetNumber(p);
    for (n = FirstOffsetNumber; n <= maxoff; n++)

and OffsetNumber is uint16 not int.  So a loop like this would go nuts
instead of treating the zeroed page as if it were empty.  This is not
good (see the comments for PageHeaderIsValid in bufpage.c if you
disremember why).

We could fix this by changing the declarations of the "maxoff" variables
to int, but I think it's probably cleaner to recode
PageGetMaxOffsetNumber like so:

#define PageGetMaxOffsetNumber(page) \
    (((PageHeader) (page))->pd_lower <= SizeOfPageHeaderData ? 0 : \
     ((((PageHeader) (page))->pd_lower - SizeOfPageHeaderData) \
      / sizeof(ItemIdData)))

This means evaluating the macro argument twice which is not real good,
but in all existing uses the argument is just a simple variable, so
no harm done.

Anyone see a better way?

			regards, tom lane


pgsql-hackers by date

Next:From: Tom LaneDate: 2004-06-03 15:31:51
Subject: Re: New btree_gist code has a few problems
Previous:From: Bruce MomjianDate: 2004-06-03 14:55:45
Subject: Re: Compiling libpq with VisualC

Privacy Policy | About PostgreSQL
Copyright © 1996-2018 The PostgreSQL Global Development Group