PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
itemptr.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * itemptr.h
4  * POSTGRES disk item pointer definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/storage/itemptr.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef ITEMPTR_H
15 #define ITEMPTR_H
16 
17 #include "storage/block.h"
18 #include "storage/off.h"
19 
20 /*
21  * ItemPointer:
22  *
23  * This is a pointer to an item within a disk page of a known file
24  * (for example, a cross-link from an index to its parent table).
25  * blkid tells us which block, posid tells us which entry in the linp
26  * (ItemIdData) array we want.
27  *
28  * Note: because there is an item pointer in each tuple header and index
29  * tuple header on disk, it's very important not to waste space with
30  * structure padding bytes. The struct is designed to be six bytes long
31  * (it contains three int16 fields) but a few compilers will pad it to
32  * eight bytes unless coerced. We apply appropriate persuasion where
33  * possible. If your compiler can't be made to play along, you'll waste
34  * lots of space.
35  */
36 typedef struct ItemPointerData
37 {
40 }
41 /* If compiler understands packed and aligned pragmas, use those */
42 #if defined(pg_attribute_packed) && defined(pg_attribute_aligned)
43 pg_attribute_packed()
44 pg_attribute_aligned(2)
45 #endif
47 
48 typedef ItemPointerData *ItemPointer;
49 
50 /* ----------------
51  * support macros
52  * ----------------
53  */
54 
55 /*
56  * ItemPointerIsValid
57  * True iff the disk item pointer is not NULL.
58  */
59 #define ItemPointerIsValid(pointer) \
60  ((bool) (PointerIsValid(pointer) && ((pointer)->ip_posid != 0)))
61 
62 /*
63  * ItemPointerGetBlockNumberNoCheck
64  * Returns the block number of a disk item pointer.
65  */
66 #define ItemPointerGetBlockNumberNoCheck(pointer) \
67 ( \
68  BlockIdGetBlockNumber(&(pointer)->ip_blkid) \
69 )
70 
71 /*
72  * ItemPointerGetBlockNumber
73  * As above, but verifies that the item pointer looks valid.
74  */
75 #define ItemPointerGetBlockNumber(pointer) \
76 ( \
77  AssertMacro(ItemPointerIsValid(pointer)), \
78  ItemPointerGetBlockNumberNoCheck(pointer) \
79 )
80 
81 /*
82  * ItemPointerGetOffsetNumberNoCheck
83  * Returns the offset number of a disk item pointer.
84  */
85 #define ItemPointerGetOffsetNumberNoCheck(pointer) \
86 ( \
87  (pointer)->ip_posid \
88 )
89 
90 /*
91  * ItemPointerGetOffsetNumber
92  * As above, but verifies that the item pointer looks valid.
93  */
94 #define ItemPointerGetOffsetNumber(pointer) \
95 ( \
96  AssertMacro(ItemPointerIsValid(pointer)), \
97  ItemPointerGetOffsetNumberNoCheck(pointer) \
98 )
99 
100 /*
101  * ItemPointerSet
102  * Sets a disk item pointer to the specified block and offset.
103  */
104 #define ItemPointerSet(pointer, blockNumber, offNum) \
105 ( \
106  AssertMacro(PointerIsValid(pointer)), \
107  BlockIdSet(&((pointer)->ip_blkid), blockNumber), \
108  (pointer)->ip_posid = offNum \
109 )
110 
111 /*
112  * ItemPointerSetBlockNumber
113  * Sets a disk item pointer to the specified block.
114  */
115 #define ItemPointerSetBlockNumber(pointer, blockNumber) \
116 ( \
117  AssertMacro(PointerIsValid(pointer)), \
118  BlockIdSet(&((pointer)->ip_blkid), blockNumber) \
119 )
120 
121 /*
122  * ItemPointerSetOffsetNumber
123  * Sets a disk item pointer to the specified offset.
124  */
125 #define ItemPointerSetOffsetNumber(pointer, offsetNumber) \
126 ( \
127  AssertMacro(PointerIsValid(pointer)), \
128  (pointer)->ip_posid = (offsetNumber) \
129 )
130 
131 /*
132  * ItemPointerCopy
133  * Copies the contents of one disk item pointer to another.
134  *
135  * Should there ever be padding in an ItemPointer this would need to be handled
136  * differently as it's used as hash key.
137  */
138 #define ItemPointerCopy(fromPointer, toPointer) \
139 ( \
140  AssertMacro(PointerIsValid(toPointer)), \
141  AssertMacro(PointerIsValid(fromPointer)), \
142  *(toPointer) = *(fromPointer) \
143 )
144 
145 /*
146  * ItemPointerSetInvalid
147  * Sets a disk item pointer to be invalid.
148  */
149 #define ItemPointerSetInvalid(pointer) \
150 ( \
151  AssertMacro(PointerIsValid(pointer)), \
152  BlockIdSet(&((pointer)->ip_blkid), InvalidBlockNumber), \
153  (pointer)->ip_posid = InvalidOffsetNumber \
154 )
155 
156 /* ----------------
157  * externs
158  * ----------------
159  */
160 
161 extern bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2);
162 extern int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2);
163 
164 #endif /* ITEMPTR_H */
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:52
signed int int32
Definition: c.h:256
uint16 OffsetNumber
Definition: off.h:24
ItemPointerData * ItemPointer
Definition: itemptr.h:48
BlockIdData ip_blkid
Definition: itemptr.h:38
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
struct ItemPointerData ItemPointerData
OffsetNumber ip_posid
Definition: itemptr.h:39