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 
42 /* If compiler understands packed and aligned pragmas, use those */
43 #if defined(pg_attribute_packed) && defined(pg_attribute_aligned)
44 pg_attribute_packed()
45 pg_attribute_aligned(2)
46 #endif
48 
49 typedef ItemPointerData *ItemPointer;
50 
51 /* ----------------
52  * support macros
53  * ----------------
54  */
55 
56 /*
57  * ItemPointerIsValid
58  * True iff the disk item pointer is not NULL.
59  */
60 #define ItemPointerIsValid(pointer) \
61  ((bool) (PointerIsValid(pointer) && ((pointer)->ip_posid != 0)))
62 
63 /*
64  * ItemPointerGetBlockNumberNoCheck
65  * Returns the block number of a disk item pointer.
66  */
67 #define ItemPointerGetBlockNumberNoCheck(pointer) \
68 ( \
69  BlockIdGetBlockNumber(&(pointer)->ip_blkid) \
70 )
71 
72 /*
73  * ItemPointerGetBlockNumber
74  * As above, but verifies that the item pointer looks valid.
75  */
76 #define ItemPointerGetBlockNumber(pointer) \
77 ( \
78  AssertMacro(ItemPointerIsValid(pointer)), \
79  ItemPointerGetBlockNumberNoCheck(pointer) \
80 )
81 
82 /*
83  * ItemPointerGetOffsetNumberNoCheck
84  * Returns the offset number of a disk item pointer.
85  */
86 #define ItemPointerGetOffsetNumberNoCheck(pointer) \
87 ( \
88  (pointer)->ip_posid \
89 )
90 
91 /*
92  * ItemPointerGetOffsetNumber
93  * As above, but verifies that the item pointer looks valid.
94  */
95 #define ItemPointerGetOffsetNumber(pointer) \
96 ( \
97  AssertMacro(ItemPointerIsValid(pointer)), \
98  ItemPointerGetOffsetNumberNoCheck(pointer) \
99 )
100 
101 /*
102  * ItemPointerSet
103  * Sets a disk item pointer to the specified block and offset.
104  */
105 #define ItemPointerSet(pointer, blockNumber, offNum) \
106 ( \
107  AssertMacro(PointerIsValid(pointer)), \
108  BlockIdSet(&((pointer)->ip_blkid), blockNumber), \
109  (pointer)->ip_posid = offNum \
110 )
111 
112 /*
113  * ItemPointerSetBlockNumber
114  * Sets a disk item pointer to the specified block.
115  */
116 #define ItemPointerSetBlockNumber(pointer, blockNumber) \
117 ( \
118  AssertMacro(PointerIsValid(pointer)), \
119  BlockIdSet(&((pointer)->ip_blkid), blockNumber) \
120 )
121 
122 /*
123  * ItemPointerSetOffsetNumber
124  * Sets a disk item pointer to the specified offset.
125  */
126 #define ItemPointerSetOffsetNumber(pointer, offsetNumber) \
127 ( \
128  AssertMacro(PointerIsValid(pointer)), \
129  (pointer)->ip_posid = (offsetNumber) \
130 )
131 
132 /*
133  * ItemPointerCopy
134  * Copies the contents of one disk item pointer to another.
135  *
136  * Should there ever be padding in an ItemPointer this would need to be handled
137  * differently as it's used as hash key.
138  */
139 #define ItemPointerCopy(fromPointer, toPointer) \
140 ( \
141  AssertMacro(PointerIsValid(toPointer)), \
142  AssertMacro(PointerIsValid(fromPointer)), \
143  *(toPointer) = *(fromPointer) \
144 )
145 
146 /*
147  * ItemPointerSetInvalid
148  * Sets a disk item pointer to be invalid.
149  */
150 #define ItemPointerSetInvalid(pointer) \
151 ( \
152  AssertMacro(PointerIsValid(pointer)), \
153  BlockIdSet(&((pointer)->ip_blkid), InvalidBlockNumber), \
154  (pointer)->ip_posid = InvalidOffsetNumber \
155 )
156 
157 /* ----------------
158  * externs
159  * ----------------
160  */
161 
162 extern bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2);
163 extern int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2);
164 
165 #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:49
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