1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-25 12:03:53 +03:00

Increase width of RelFileNumbers from 32 bits to 56 bits.

RelFileNumbers are now assigned using a separate counter, instead of
being assigned from the OID counter. This counter never wraps around:
if all 2^56 possible RelFileNumbers are used, an internal error
occurs. As the cluster is limited to 2^64 total bytes of WAL, this
limitation should not cause a problem in practice.

If the counter were 64 bits wide rather than 56 bits wide, we would
need to increase the width of the BufferTag, which might adversely
impact buffer lookup performance. Also, this lets us use bigint for
pg_class.relfilenode and other places where these values are exposed
at the SQL level without worrying about overflow.

This should remove the need to keep "tombstone" files around until
the next checkpoint when relations are removed. We do that to keep
RelFileNumbers from being recycled, but now that won't happen
anyway. However, this patch doesn't actually change anything in
this area; it just makes it possible for a future patch to do so.

Dilip Kumar, based on an idea from Andres Freund, who also reviewed
some earlier versions of the patch. Further review and some
wordsmithing by me. Also reviewed at various points by Ashutosh
Sharma, Vignesh C, Amul Sul, Álvaro Herrera, and Tom Lane.

Discussion: http://postgr.es/m/CA+Tgmobp7+7kmi4gkq7Y+4AM9fTvL+O1oQ4-5gFTT+6Ng-dQ=g@mail.gmail.com
This commit is contained in:
Robert Haas
2022-09-27 13:25:21 -04:00
parent 2f47715cc8
commit 05d4cbf9b6
70 changed files with 693 additions and 289 deletions

View File

@@ -92,29 +92,66 @@ typedef struct buftag
{
Oid spcOid; /* tablespace oid */
Oid dbOid; /* database oid */
RelFileNumber relNumber; /* relation file number */
ForkNumber forkNum; /* fork number */
/*
* relForkDetails[] stores the fork number in the high 8 bits of the first
* integer; the remaining 56 bits are used to store the relfilenmber.
* Expanding the relfilenumber to a full 64 bits would require widening
* the BufferTag, which is undesirable for performance reasons. We use
* two 32-bit values here rather than a single 64-bit value to avoid
* padding the struct out to a multiple of 8 bytes.
*/
uint32 relForkDetails[2];
BlockNumber blockNum; /* blknum relative to begin of reln */
} BufferTag;
/* High relNumber bits in relForkDetails[0] */
#define BUFTAG_RELNUM_HIGH_BITS 24
/* Low relNumber bits in relForkDetails[1] */
#define BUFTAG_RELNUM_LOW_BITS 32
/* Mask to fetch high bits of relNumber from relForkDetails[0] */
#define BUFTAG_RELNUM_HIGH_MASK ((1U << BUFTAG_RELNUM_HIGH_BITS) - 1)
/* Mask to fetch low bits of relNumber from relForkDetails[1] */
#define BUFTAG_RELNUM_LOW_MASK 0XFFFFFFFF
static inline RelFileNumber
BufTagGetRelNumber(const BufferTag *tag)
{
return tag->relNumber;
uint64 relnum;
relnum = ((uint64) tag->relForkDetails[0]) & BUFTAG_RELNUM_HIGH_MASK;
relnum = (relnum << BUFTAG_RELNUM_LOW_BITS) | tag->relForkDetails[1];
Assert(relnum <= MAX_RELFILENUMBER);
return (RelFileNumber) relnum;
}
static inline ForkNumber
BufTagGetForkNum(const BufferTag *tag)
{
return tag->forkNum;
ForkNumber ret;
StaticAssertStmt(MAX_FORKNUM <= INT8_MAX,
"MAX_FORKNUM can't be greater than INT8_MAX");
ret = (int8) (tag->relForkDetails[0] >> BUFTAG_RELNUM_HIGH_BITS);
return ret;
}
static inline void
BufTagSetRelForkDetails(BufferTag *tag, RelFileNumber relnumber,
ForkNumber forknum)
{
tag->relNumber = relnumber;
tag->forkNum = forknum;
Assert(relnumber <= MAX_RELFILENUMBER);
Assert(forknum <= MAX_FORKNUM);
tag->relForkDetails[0] = (relnumber >> BUFTAG_RELNUM_LOW_BITS) &
BUFTAG_RELNUM_HIGH_MASK;
tag->relForkDetails[0] |= (forknum << BUFTAG_RELNUM_HIGH_BITS);
tag->relForkDetails[1] = relnumber & BUFTAG_RELNUM_LOW_MASK;
}
static inline RelFileLocator
@@ -153,9 +190,9 @@ BufferTagsEqual(const BufferTag *tag1, const BufferTag *tag2)
{
return (tag1->spcOid == tag2->spcOid) &&
(tag1->dbOid == tag2->dbOid) &&
(tag1->relNumber == tag2->relNumber) &&
(tag1->blockNum == tag2->blockNum) &&
(tag1->forkNum == tag2->forkNum);
(tag1->relForkDetails[0] == tag2->relForkDetails[0]) &&
(tag1->relForkDetails[1] == tag2->relForkDetails[1]) &&
(tag1->blockNum == tag2->blockNum);
}
static inline bool