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