Age Owner TLA Line data Source code
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
9770 scrappy 35 GIC 30819753 : ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
36 : {
9345 bruce 37 CBC 61639506 : if (ItemPointerGetBlockNumber(pointer1) ==
38 48995736 : ItemPointerGetBlockNumber(pointer2) &&
39 18175983 : ItemPointerGetOffsetNumber(pointer1) ==
40 18175983 : ItemPointerGetOffsetNumber(pointer2))
8986 bruce 41 GIC 15455936 : return true;
9345 bruce 42 ECB : else
8986 bruce 43 GIC 15363817 : return false;
44 : }
45 :
46 : /*
47 : * ItemPointerCompare
48 : * Generic btree-style comparison for item pointers.
49 : */
6071 tgl 50 ECB : int32
6071 tgl 51 GIC 21593942 : ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
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.
6071 tgl 56 ECB : */
2203 alvherre 57 CBC 21593942 : BlockNumber b1 = ItemPointerGetBlockNumberNoCheck(arg1);
2203 alvherre 58 GIC 21593942 : BlockNumber b2 = ItemPointerGetBlockNumberNoCheck(arg2);
6031 bruce 59 ECB :
6071 tgl 60 CBC 21593942 : if (b1 < b2)
61 190826 : return -1;
62 21403116 : else if (b1 > b2)
63 11735938 : return 1;
2203 alvherre 64 19334356 : else if (ItemPointerGetOffsetNumberNoCheck(arg1) <
65 9667178 : ItemPointerGetOffsetNumberNoCheck(arg2))
6071 tgl 66 419312 : return -1;
2203 alvherre 67 18495732 : else if (ItemPointerGetOffsetNumberNoCheck(arg1) >
68 9247866 : ItemPointerGetOffsetNumberNoCheck(arg2))
6071 tgl 69 GIC 8542515 : return 1;
6071 tgl 70 ECB : else
6071 tgl 71 GIC 705351 : 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 : */
771 drowley 83 ECB : void
771 drowley 84 GIC 23 : ItemPointerInc(ItemPointer pointer)
771 drowley 85 ECB : {
771 drowley 86 CBC 23 : BlockNumber blk = ItemPointerGetBlockNumberNoCheck(pointer);
771 drowley 87 GIC 23 : OffsetNumber off = ItemPointerGetOffsetNumberNoCheck(pointer);
771 drowley 88 ECB :
771 drowley 89 GIC 23 : if (off == PG_UINT16_MAX)
771 drowley 90 ECB : {
771 drowley 91 GIC 6 : if (blk != InvalidBlockNumber)
771 drowley 92 ECB : {
771 drowley 93 CBC 3 : off = 0;
771 drowley 94 GIC 3 : blk++;
95 : }
96 : }
771 drowley 97 ECB : else
771 drowley 98 GIC 17 : off++;
771 drowley 99 ECB :
771 drowley 100 CBC 23 : ItemPointerSet(pointer, blk, off);
771 drowley 101 GIC 23 : }
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 : */
771 drowley 113 ECB : void
771 drowley 114 GIC 30 : ItemPointerDec(ItemPointer pointer)
771 drowley 115 ECB : {
771 drowley 116 CBC 30 : BlockNumber blk = ItemPointerGetBlockNumberNoCheck(pointer);
771 drowley 117 GIC 30 : OffsetNumber off = ItemPointerGetOffsetNumberNoCheck(pointer);
771 drowley 118 ECB :
771 drowley 119 GIC 30 : if (off == 0)
771 drowley 120 ECB : {
771 drowley 121 GIC 30 : if (blk != 0)
771 drowley 122 ECB : {
771 drowley 123 CBC 21 : off = PG_UINT16_MAX;
771 drowley 124 GIC 21 : blk--;
125 : }
126 : }
771 drowley 127 EUB : else
771 drowley 128 UIC 0 : off--;
771 drowley 129 ECB :
771 drowley 130 CBC 30 : ItemPointerSet(pointer, blk, off);
771 drowley 131 GIC 30 : }
|