PostgreSQL Source Code  git master
gist.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * gist.h
4  * The public API for GiST indexes. This API is exposed to
5  * individuals implementing GiST indexes, so backward-incompatible
6  * changes should be made with care.
7  *
8  *
9  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * src/include/access/gist.h
13  *
14  *-------------------------------------------------------------------------
15  */
16 #ifndef GIST_H
17 #define GIST_H
18 
19 #include "access/itup.h"
20 #include "access/transam.h"
21 #include "access/xlog.h"
22 #include "access/xlogdefs.h"
23 #include "storage/block.h"
24 #include "storage/bufpage.h"
25 #include "utils/relcache.h"
26 
27 /*
28  * amproc indexes for GiST indexes.
29  */
30 #define GIST_CONSISTENT_PROC 1
31 #define GIST_UNION_PROC 2
32 #define GIST_COMPRESS_PROC 3
33 #define GIST_DECOMPRESS_PROC 4
34 #define GIST_PENALTY_PROC 5
35 #define GIST_PICKSPLIT_PROC 6
36 #define GIST_EQUAL_PROC 7
37 #define GIST_DISTANCE_PROC 8
38 #define GIST_FETCH_PROC 9
39 #define GIST_OPTIONS_PROC 10
40 #define GIST_SORTSUPPORT_PROC 11
41 #define GISTNProcs 11
42 
43 /*
44  * Page opaque data in a GiST index page.
45  */
46 #define F_LEAF (1 << 0) /* leaf page */
47 #define F_DELETED (1 << 1) /* the page has been deleted */
48 #define F_TUPLES_DELETED (1 << 2) /* some tuples on the page were
49  * deleted */
50 #define F_FOLLOW_RIGHT (1 << 3) /* page to the right has no downlink */
51 #define F_HAS_GARBAGE (1 << 4) /* some tuples on the page are dead,
52  * but not deleted yet */
53 
54 /* NSN - node sequence number, a special-purpose LSN */
56 
57 /*
58  * A bogus LSN / NSN value used during index build. Must be smaller than any
59  * real or fake unlogged LSN, so that after an index build finishes, all the
60  * splits are considered completed.
61  */
62 #define GistBuildLSN ((XLogRecPtr) 1)
63 
64 /*
65  * For on-disk compatibility with pre-9.3 servers, NSN is stored as two
66  * 32-bit fields on disk, same as LSNs.
67  */
69 
70 typedef struct GISTPageOpaqueData
71 {
72  PageGistNSN nsn; /* this value must change on page split */
73  BlockNumber rightlink; /* next page if any */
74  uint16 flags; /* see bit definitions above */
75  uint16 gist_page_id; /* for identification of GiST indexes */
77 
79 
80 /*
81  * Maximum possible sizes for GiST index tuple and index key. Calculation is
82  * based on assumption that GiST page should fit at least 4 tuples. In theory,
83  * GiST index can be functional when page can fit 3 tuples. But that seems
84  * rather inefficient, so we use a bit conservative estimate.
85  *
86  * The maximum size of index key is true for unicolumn index. Therefore, this
87  * estimation should be used to figure out which maximum size of GiST index key
88  * makes sense at all. For multicolumn indexes, user might be able to tune
89  * key size using opclass parameters.
90  */
91 #define GISTMaxIndexTupleSize \
92  MAXALIGN_DOWN((BLCKSZ - SizeOfPageHeaderData - sizeof(GISTPageOpaqueData)) / \
93  4 - sizeof(ItemIdData))
94 
95 #define GISTMaxIndexKeySize \
96  (GISTMaxIndexTupleSize - MAXALIGN(sizeof(IndexTupleData)))
97 
98 /*
99  * The page ID is for the convenience of pg_filedump and similar utilities,
100  * which otherwise would have a hard time telling pages of different index
101  * types apart. It should be the last 2 bytes on the page. This is more or
102  * less "free" due to alignment considerations.
103  */
104 #define GIST_PAGE_ID 0xFF81
105 
106 /*
107  * This is the Split Vector to be returned by the PickSplit method.
108  * PickSplit should fill the indexes of tuples to go to the left side into
109  * spl_left[], and those to go to the right into spl_right[] (note the method
110  * is responsible for palloc'ing both of these arrays!). The tuple counts
111  * go into spl_nleft/spl_nright, and spl_ldatum/spl_rdatum must be set to
112  * the union keys for each side.
113  *
114  * If spl_ldatum_exists and spl_rdatum_exists are true, then we are performing
115  * a "secondary split" using a non-first index column. In this case some
116  * decisions have already been made about a page split, and the set of tuples
117  * being passed to PickSplit is just the tuples about which we are undecided.
118  * spl_ldatum/spl_rdatum then contain the union keys for the tuples already
119  * chosen to go left or right. Ideally the PickSplit method should take those
120  * keys into account while deciding what to do with the remaining tuples, ie
121  * it should try to "build out" from those unions so as to minimally expand
122  * them. If it does so, it should union the given tuples' keys into the
123  * existing spl_ldatum/spl_rdatum values rather than just setting those values
124  * from scratch, and then set spl_ldatum_exists/spl_rdatum_exists to false to
125  * show it has done this.
126  *
127  * If the PickSplit method fails to clear spl_ldatum_exists/spl_rdatum_exists,
128  * the core GiST code will make its own decision about how to merge the
129  * secondary-split results with the previously-chosen tuples, and will then
130  * recompute the union keys from scratch. This is a workable though often not
131  * optimal approach.
132  */
133 typedef struct GIST_SPLITVEC
134 {
135  OffsetNumber *spl_left; /* array of entries that go left */
136  int spl_nleft; /* size of this array */
137  Datum spl_ldatum; /* Union of keys in spl_left */
138  bool spl_ldatum_exists; /* true, if spl_ldatum already exists. */
139 
140  OffsetNumber *spl_right; /* array of entries that go right */
141  int spl_nright; /* size of the array */
142  Datum spl_rdatum; /* Union of keys in spl_right */
143  bool spl_rdatum_exists; /* true, if spl_rdatum already exists. */
144 } GIST_SPLITVEC;
145 
146 /*
147  * An entry on a GiST node. Contains the key, as well as its own
148  * location (rel,page,offset) which can supply the matching pointer.
149  * leafkey is a flag to tell us if the entry is in a leaf node.
150  */
151 typedef struct GISTENTRY
152 {
157  bool leafkey;
158 } GISTENTRY;
159 
160 #define GistPageGetOpaque(page) ( (GISTPageOpaque) PageGetSpecialPointer(page) )
161 
162 #define GistPageIsLeaf(page) ( GistPageGetOpaque(page)->flags & F_LEAF)
163 #define GIST_LEAF(entry) (GistPageIsLeaf((entry)->page))
164 
165 #define GistPageIsDeleted(page) ( GistPageGetOpaque(page)->flags & F_DELETED)
166 
167 #define GistTuplesDeleted(page) ( GistPageGetOpaque(page)->flags & F_TUPLES_DELETED)
168 #define GistMarkTuplesDeleted(page) ( GistPageGetOpaque(page)->flags |= F_TUPLES_DELETED)
169 #define GistClearTuplesDeleted(page) ( GistPageGetOpaque(page)->flags &= ~F_TUPLES_DELETED)
170 
171 #define GistPageHasGarbage(page) ( GistPageGetOpaque(page)->flags & F_HAS_GARBAGE)
172 #define GistMarkPageHasGarbage(page) ( GistPageGetOpaque(page)->flags |= F_HAS_GARBAGE)
173 #define GistClearPageHasGarbage(page) ( GistPageGetOpaque(page)->flags &= ~F_HAS_GARBAGE)
174 
175 #define GistFollowRight(page) ( GistPageGetOpaque(page)->flags & F_FOLLOW_RIGHT)
176 #define GistMarkFollowRight(page) ( GistPageGetOpaque(page)->flags |= F_FOLLOW_RIGHT)
177 #define GistClearFollowRight(page) ( GistPageGetOpaque(page)->flags &= ~F_FOLLOW_RIGHT)
178 
179 #define GistPageGetNSN(page) ( PageXLogRecPtrGet(GistPageGetOpaque(page)->nsn))
180 #define GistPageSetNSN(page, val) ( PageXLogRecPtrSet(GistPageGetOpaque(page)->nsn, val))
181 
182 
183 /*
184  * On a deleted page, we store this struct. A deleted page doesn't contain any
185  * tuples, so we don't use the normal page layout with line pointers. Instead,
186  * this struct is stored right after the standard page header. pd_lower points
187  * to the end of this struct. If we add fields to this struct in the future, we
188  * can distinguish the old and new formats by pd_lower.
189  */
191 {
192  /* last xid which could see the page in a scan */
195 
196 static inline void
198 {
199  Assert(PageIsEmpty(page));
200 
201  GistPageGetOpaque(page)->flags |= F_DELETED;
202  ((PageHeader) page)->pd_lower = MAXALIGN(SizeOfPageHeaderData) + sizeof(GISTDeletedPageContents);
203 
204  ((GISTDeletedPageContents *) PageGetContents(page))->deleteXid = deletexid;
205 }
206 
207 static inline FullTransactionId
209 {
210  Assert(GistPageIsDeleted(page));
211 
212  /* Is the deleteXid field present? */
213  if (((PageHeader) page)->pd_lower >= MAXALIGN(SizeOfPageHeaderData) +
214  offsetof(GISTDeletedPageContents, deleteXid) + sizeof(FullTransactionId))
215  {
216  return ((GISTDeletedPageContents *) PageGetContents(page))->deleteXid;
217  }
218  else
220 }
221 
222 /*
223  * Vector of GISTENTRY structs; user-defined methods union and picksplit
224  * take it as one of their arguments
225  */
226 typedef struct
227 {
228  int32 n; /* number of elements */
231 
232 #define GEVHDRSZ (offsetof(GistEntryVector, vector))
233 
234 /*
235  * macro to initialize a GISTENTRY
236  */
237 #define gistentryinit(e, k, r, pg, o, l) \
238  do { (e).key = (k); (e).rel = (r); (e).page = (pg); \
239  (e).offset = (o); (e).leafkey = (l); } while (0)
240 
241 #endif /* GIST_H */
Relation rel
Definition: gist.h:154
bool spl_rdatum_exists
Definition: gist.h:143
#define GistPageIsDeleted(page)
Definition: gist.h:165
#define PageIsEmpty(page)
Definition: bufpage.h:222
struct GIST_SPLITVEC GIST_SPLITVEC
OffsetNumber * spl_left
Definition: gist.h:135
Datum spl_rdatum
Definition: gist.h:142
int32 n
Definition: gist.h:228
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:350
bool spl_ldatum_exists
Definition: gist.h:138
uint32 BlockNumber
Definition: block.h:31
int spl_nleft
Definition: gist.h:136
#define SizeOfPageHeaderData
Definition: bufpage.h:216
signed int int32
Definition: c.h:429
uint16 OffsetNumber
Definition: off.h:24
Page page
Definition: gist.h:155
uint16 gist_page_id
Definition: gist.h:75
PageXLogRecPtr PageGistNSN
Definition: gist.h:68
unsigned short uint16
Definition: c.h:440
struct GISTENTRY GISTENTRY
#define FirstNormalTransactionId
Definition: transam.h:34
int spl_nright
Definition: gist.h:141
Datum key
Definition: gist.h:153
struct GISTPageOpaqueData GISTPageOpaqueData
uint16 flags
Definition: gist.h:74
static void GistPageSetDeleted(Page page, FullTransactionId deletexid)
Definition: gist.h:197
bool leafkey
Definition: gist.h:157
FullTransactionId deleteXid
Definition: gist.h:193
#define PageGetContents(page)
Definition: bufpage.h:246
uintptr_t Datum
Definition: postgres.h:367
#define GistPageGetOpaque(page)
Definition: gist.h:160
PageHeaderData * PageHeader
Definition: bufpage.h:166
struct GISTDeletedPageContents GISTDeletedPageContents
Datum spl_ldatum
Definition: gist.h:137
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
OffsetNumber * spl_right
Definition: gist.h:140
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:71
#define MAXALIGN(LEN)
Definition: c.h:757
XLogRecPtr GistNSN
Definition: gist.h:55
PageGistNSN nsn
Definition: gist.h:72
static FullTransactionId GistPageGetDeleteXid(Page page)
Definition: gist.h:208
GISTPageOpaqueData * GISTPageOpaque
Definition: gist.h:78
BlockNumber rightlink
Definition: gist.h:73
#define F_DELETED
Definition: gist.h:47
OffsetNumber offset
Definition: gist.h:156
#define offsetof(type, field)
Definition: c.h:727
Pointer Page
Definition: bufpage.h:78