PostgreSQL Source Code  git master
itemptr.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * itemptr.c
4  * POSTGRES disk item pointer code.
5  *
6  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/storage/page/itemptr.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "storage/itemptr.h"
18 
19 
20 /*
21  * We really want ItemPointerData to be exactly 6 bytes.
22  */
23 StaticAssertDecl(sizeof(ItemPointerData) == 3 * sizeof(uint16),
24  "ItemPointerData struct is improperly padded");
25 
26 /*
27  * ItemPointerEquals
28  * Returns true if both item pointers point to the same item,
29  * otherwise returns false.
30  *
31  * Note:
32  * Asserts that the disk item pointers are both valid!
33  */
34 bool
36 {
37  if (ItemPointerGetBlockNumber(pointer1) ==
38  ItemPointerGetBlockNumber(pointer2) &&
39  ItemPointerGetOffsetNumber(pointer1) ==
41  return true;
42  else
43  return false;
44 }
45 
46 /*
47  * ItemPointerCompare
48  * Generic btree-style comparison for item pointers.
49  */
50 int32
52 {
53  /*
54  * Use ItemPointerGet{Offset,Block}NumberNoCheck to avoid asserting
55  * ip_posid != 0, which may not be true for a user-supplied TID.
56  */
59 
60  if (b1 < b2)
61  return -1;
62  else if (b1 > b2)
63  return 1;
64  else if (ItemPointerGetOffsetNumberNoCheck(arg1) <
66  return -1;
67  else if (ItemPointerGetOffsetNumberNoCheck(arg1) >
69  return 1;
70  else
71  return 0;
72 }
73 
74 /*
75  * ItemPointerInc
76  * Increment 'pointer' by 1 only paying attention to the ItemPointer's
77  * type's range limits and not MaxOffsetNumber and FirstOffsetNumber.
78  * This may result in 'pointer' becoming !OffsetNumberIsValid.
79  *
80  * If the pointer is already the maximum possible values permitted by the
81  * range of the ItemPointer's types, then do nothing.
82  */
83 void
85 {
88 
89  if (off == PG_UINT16_MAX)
90  {
91  if (blk != InvalidBlockNumber)
92  {
93  off = 0;
94  blk++;
95  }
96  }
97  else
98  off++;
99 
100  ItemPointerSet(pointer, blk, off);
101 }
102 
103 /*
104  * ItemPointerDec
105  * Decrement 'pointer' by 1 only paying attention to the ItemPointer's
106  * type's range limits and not MaxOffsetNumber and FirstOffsetNumber.
107  * This may result in 'pointer' becoming !OffsetNumberIsValid.
108  *
109  * If the pointer is already the minimum possible values permitted by the
110  * range of the ItemPointer's types, then do nothing. This does rely on
111  * FirstOffsetNumber being 1 rather than 0.
112  */
113 void
115 {
118 
119  if (off == 0)
120  {
121  if (blk != 0)
122  {
123  off = PG_UINT16_MAX;
124  blk--;
125  }
126  }
127  else
128  off--;
129 
130  ItemPointerSet(pointer, blk, off);
131 }
uint32 BlockNumber
Definition: block.h:31
#define InvalidBlockNumber
Definition: block.h:33
unsigned short uint16
Definition: c.h:489
signed int int32
Definition: c.h:478
#define PG_UINT16_MAX
Definition: c.h:571
void ItemPointerDec(ItemPointer pointer)
Definition: itemptr.c:114
StaticAssertDecl(sizeof(ItemPointerData)==3 *sizeof(uint16), "ItemPointerData struct is improperly padded")
void ItemPointerInc(ItemPointer pointer)
Definition: itemptr.c:84
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:51
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:35
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition: itemptr.h:135
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition: itemptr.h:124
static OffsetNumber ItemPointerGetOffsetNumberNoCheck(const ItemPointerData *pointer)
Definition: itemptr.h:114
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition: itemptr.h:103
static BlockNumber ItemPointerGetBlockNumberNoCheck(const ItemPointerData *pointer)
Definition: itemptr.h:93
uint16 OffsetNumber
Definition: off.h:24