PostgreSQL Source Code  git master
ginblock.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------
2  * ginblock.h
3  * details of structures stored in GIN index blocks
4  *
5  * Copyright (c) 2006-2018, PostgreSQL Global Development Group
6  *
7  * src/include/access/ginblock.h
8  *--------------------------------------------------------------------------
9  */
10 #ifndef GINBLOCK_H
11 #define GINBLOCK_H
12 
13 #include "storage/block.h"
14 #include "storage/itemptr.h"
15 #include "storage/off.h"
16 
17 /*
18  * Page opaque data in an inverted index page.
19  *
20  * Note: GIN does not include a page ID word as do the other index types.
21  * This is OK because the opaque data is only 8 bytes and so can be reliably
22  * distinguished by size. Revisit this if the size ever increases.
23  * Further note: as of 9.2, SP-GiST also uses 8-byte special space, as does
24  * BRIN as of 9.5. This is still OK, as long as GIN isn't using all of the
25  * high-order bits in its flags word, because that way the flags word cannot
26  * match the page IDs used by SP-GiST and BRIN.
27  */
28 typedef struct GinPageOpaqueData
29 {
30  BlockNumber rightlink; /* next page if any */
31  OffsetNumber maxoff; /* number of PostingItems on GIN_DATA &
32  * ~GIN_LEAF page. On GIN_LIST page, number of
33  * heap tuples. */
34  uint16 flags; /* see bit definitions below */
36 
38 
39 #define GIN_DATA (1 << 0)
40 #define GIN_LEAF (1 << 1)
41 #define GIN_DELETED (1 << 2)
42 #define GIN_META (1 << 3)
43 #define GIN_LIST (1 << 4)
44 #define GIN_LIST_FULLROW (1 << 5) /* makes sense only on GIN_LIST page */
45 #define GIN_INCOMPLETE_SPLIT (1 << 6) /* page was split, but parent not
46  * updated */
47 #define GIN_COMPRESSED (1 << 7)
48 
49 /* Page numbers of fixed-location pages */
50 #define GIN_METAPAGE_BLKNO (0)
51 #define GIN_ROOT_BLKNO (1)
52 
53 typedef struct GinMetaPageData
54 {
55  /*
56  * Pointers to head and tail of pending list, which consists of GIN_LIST
57  * pages. These store fast-inserted entries that haven't yet been moved
58  * into the regular GIN structure.
59  */
62 
63  /*
64  * Free space in bytes in the pending list's tail page.
65  */
67 
68  /*
69  * We store both number of pages and number of heap tuples that are in the
70  * pending list.
71  */
74 
75  /*
76  * Statistics for planner use (accurate as of last VACUUM)
77  */
81  int64 nEntries;
82 
83  /*
84  * GIN version number (ideally this should have been at the front, but too
85  * late now. Don't move it!)
86  *
87  * Currently 2 (for indexes initialized in 9.4 or later)
88  *
89  * Version 1 (indexes initialized in version 9.1, 9.2 or 9.3), is
90  * compatible, but may contain uncompressed posting tree (leaf) pages and
91  * posting lists. They will be converted to compressed format when
92  * modified.
93  *
94  * Version 0 (indexes initialized in 9.0 or before) is compatible but may
95  * be missing null entries, including both null keys and placeholders.
96  * Reject full-index-scan attempts on such indexes.
97  */
100 
101 #define GIN_CURRENT_VERSION 2
102 
103 #define GinPageGetMeta(p) \
104  ((GinMetaPageData *) PageGetContents(p))
105 
106 /*
107  * Macros for accessing a GIN index page's opaque data
108  */
109 #define GinPageGetOpaque(page) ( (GinPageOpaque) PageGetSpecialPointer(page) )
110 
111 #define GinPageIsLeaf(page) ( (GinPageGetOpaque(page)->flags & GIN_LEAF) != 0 )
112 #define GinPageSetLeaf(page) ( GinPageGetOpaque(page)->flags |= GIN_LEAF )
113 #define GinPageSetNonLeaf(page) ( GinPageGetOpaque(page)->flags &= ~GIN_LEAF )
114 #define GinPageIsData(page) ( (GinPageGetOpaque(page)->flags & GIN_DATA) != 0 )
115 #define GinPageSetData(page) ( GinPageGetOpaque(page)->flags |= GIN_DATA )
116 #define GinPageIsList(page) ( (GinPageGetOpaque(page)->flags & GIN_LIST) != 0 )
117 #define GinPageSetList(page) ( GinPageGetOpaque(page)->flags |= GIN_LIST )
118 #define GinPageHasFullRow(page) ( (GinPageGetOpaque(page)->flags & GIN_LIST_FULLROW) != 0 )
119 #define GinPageSetFullRow(page) ( GinPageGetOpaque(page)->flags |= GIN_LIST_FULLROW )
120 #define GinPageIsCompressed(page) ( (GinPageGetOpaque(page)->flags & GIN_COMPRESSED) != 0 )
121 #define GinPageSetCompressed(page) ( GinPageGetOpaque(page)->flags |= GIN_COMPRESSED )
122 
123 #define GinPageIsDeleted(page) ( (GinPageGetOpaque(page)->flags & GIN_DELETED) != 0 )
124 #define GinPageSetDeleted(page) ( GinPageGetOpaque(page)->flags |= GIN_DELETED)
125 #define GinPageSetNonDeleted(page) ( GinPageGetOpaque(page)->flags &= ~GIN_DELETED)
126 #define GinPageIsIncompleteSplit(page) ( (GinPageGetOpaque(page)->flags & GIN_INCOMPLETE_SPLIT) != 0 )
127 
128 #define GinPageRightMost(page) ( GinPageGetOpaque(page)->rightlink == InvalidBlockNumber)
129 
130 /*
131  * We use our own ItemPointerGet(BlockNumber|OffsetNumber)
132  * to avoid Asserts, since sometimes the ip_posid isn't "valid"
133  */
134 #define GinItemPointerGetBlockNumber(pointer) \
135  (ItemPointerGetBlockNumberNoCheck(pointer))
136 
137 #define GinItemPointerGetOffsetNumber(pointer) \
138  (ItemPointerGetOffsetNumberNoCheck(pointer))
139 
140 #define GinItemPointerSetBlockNumber(pointer, blkno) \
141  (ItemPointerSetBlockNumber((pointer), (blkno)))
142 
143 #define GinItemPointerSetOffsetNumber(pointer, offnum) \
144  (ItemPointerSetOffsetNumber((pointer), (offnum)))
145 
146 
147 /*
148  * Special-case item pointer values needed by the GIN search logic.
149  * MIN: sorts less than any valid item pointer
150  * MAX: sorts greater than any valid item pointer
151  * LOSSY PAGE: indicates a whole heap page, sorts after normal item
152  * pointers for that page
153  * Note that these are all distinguishable from an "invalid" item pointer
154  * (which is InvalidBlockNumber/0) as well as from all normal item
155  * pointers (which have item numbers in the range 1..MaxHeapTuplesPerPage).
156  */
157 #define ItemPointerSetMin(p) \
158  ItemPointerSet((p), (BlockNumber)0, (OffsetNumber)0)
159 #define ItemPointerIsMin(p) \
160  (GinItemPointerGetOffsetNumber(p) == (OffsetNumber)0 && \
161  GinItemPointerGetBlockNumber(p) == (BlockNumber)0)
162 #define ItemPointerSetMax(p) \
163  ItemPointerSet((p), InvalidBlockNumber, (OffsetNumber)0xffff)
164 #define ItemPointerIsMax(p) \
165  (GinItemPointerGetOffsetNumber(p) == (OffsetNumber)0xffff && \
166  GinItemPointerGetBlockNumber(p) == InvalidBlockNumber)
167 #define ItemPointerSetLossyPage(p, b) \
168  ItemPointerSet((p), (b), (OffsetNumber)0xffff)
169 #define ItemPointerIsLossyPage(p) \
170  (GinItemPointerGetOffsetNumber(p) == (OffsetNumber)0xffff && \
171  GinItemPointerGetBlockNumber(p) != InvalidBlockNumber)
172 
173 /*
174  * Posting item in a non-leaf posting-tree page
175  */
176 typedef struct
177 {
178  /* We use BlockIdData not BlockNumber to avoid padding space wastage */
181 } PostingItem;
182 
183 #define PostingItemGetBlockNumber(pointer) \
184  BlockIdGetBlockNumber(&(pointer)->child_blkno)
185 
186 #define PostingItemSetBlockNumber(pointer, blockNumber) \
187  BlockIdSet(&((pointer)->child_blkno), (blockNumber))
188 
189 /*
190  * Category codes to distinguish placeholder nulls from ordinary NULL keys.
191  *
192  * The first two code values were chosen to be compatible with the usual usage
193  * of bool isNull flags. However, casting between bool and GinNullCategory is
194  * risky because of the possibility of different bit patterns and type sizes,
195  * so it is no longer done.
196  *
197  * GIN_CAT_EMPTY_QUERY is never stored in the index; and notice that it is
198  * chosen to sort before not after regular key values.
199  */
200 typedef signed char GinNullCategory;
201 
202 #define GIN_CAT_NORM_KEY 0 /* normal, non-null key value */
203 #define GIN_CAT_NULL_KEY 1 /* null key value */
204 #define GIN_CAT_EMPTY_ITEM 2 /* placeholder for zero-key item */
205 #define GIN_CAT_NULL_ITEM 3 /* placeholder for null item */
206 #define GIN_CAT_EMPTY_QUERY (-1) /* placeholder for full-scan query */
207 
208 /*
209  * Access macros for null category byte in entry tuples
210  */
211 #define GinCategoryOffset(itup,ginstate) \
212  (IndexInfoFindDataOffset((itup)->t_info) + \
213  ((ginstate)->oneCol ? 0 : sizeof(int16)))
214 #define GinGetNullCategory(itup,ginstate) \
215  (*((GinNullCategory *) ((char*)(itup) + GinCategoryOffset(itup,ginstate))))
216 #define GinSetNullCategory(itup,ginstate,c) \
217  (*((GinNullCategory *) ((char*)(itup) + GinCategoryOffset(itup,ginstate))) = (c))
218 
219 /*
220  * Access macros for leaf-page entry tuples (see discussion in README)
221  */
222 #define GinGetNPosting(itup) GinItemPointerGetOffsetNumber(&(itup)->t_tid)
223 #define GinSetNPosting(itup,n) ItemPointerSetOffsetNumber(&(itup)->t_tid,n)
224 #define GIN_TREE_POSTING ((OffsetNumber)0xffff)
225 #define GinIsPostingTree(itup) (GinGetNPosting(itup) == GIN_TREE_POSTING)
226 #define GinSetPostingTree(itup, blkno) ( GinSetNPosting((itup),GIN_TREE_POSTING), ItemPointerSetBlockNumber(&(itup)->t_tid, blkno) )
227 #define GinGetPostingTree(itup) GinItemPointerGetBlockNumber(&(itup)->t_tid)
228 
229 #define GIN_ITUP_COMPRESSED (1U << 31)
230 #define GinGetPostingOffset(itup) (GinItemPointerGetBlockNumber(&(itup)->t_tid) & (~GIN_ITUP_COMPRESSED))
231 #define GinSetPostingOffset(itup,n) ItemPointerSetBlockNumber(&(itup)->t_tid,(n)|GIN_ITUP_COMPRESSED)
232 #define GinGetPosting(itup) ((Pointer) ((char*)(itup) + GinGetPostingOffset(itup)))
233 #define GinItupIsCompressed(itup) ((GinItemPointerGetBlockNumber(&(itup)->t_tid) & GIN_ITUP_COMPRESSED) != 0)
234 
235 /*
236  * Maximum size of an item on entry tree page. Make sure that we fit at least
237  * three items on each page. (On regular B-tree indexes, we must fit at least
238  * three items: two data items and the "high key". In GIN entry tree, we don't
239  * currently store the high key explicitly, we just use the rightmost item on
240  * the page, so it would actually be enough to fit two items.)
241  */
242 #define GinMaxItemSize \
243  Min(INDEX_SIZE_MASK, \
244  MAXALIGN_DOWN(((BLCKSZ - \
245  MAXALIGN(SizeOfPageHeaderData + 3 * sizeof(ItemIdData)) - \
246  MAXALIGN(sizeof(GinPageOpaqueData))) / 3)))
247 
248 /*
249  * Access macros for non-leaf entry tuples
250  */
251 #define GinGetDownlink(itup) GinItemPointerGetBlockNumber(&(itup)->t_tid)
252 #define GinSetDownlink(itup,blkno) ItemPointerSet(&(itup)->t_tid, blkno, InvalidOffsetNumber)
253 
254 
255 /*
256  * Data (posting tree) pages
257  *
258  * Posting tree pages don't store regular tuples. Non-leaf pages contain
259  * PostingItems, which are pairs of ItemPointers and child block numbers.
260  * Leaf pages contain GinPostingLists and an uncompressed array of item
261  * pointers.
262  *
263  * In a leaf page, the compressed posting lists are stored after the regular
264  * page header, one after each other. Although we don't store regular tuples,
265  * pd_lower is used to indicate the end of the posting lists. After that, free
266  * space follows. This layout is compatible with the "standard" heap and
267  * index page layout described in bufpage.h, so that we can e.g set buffer_std
268  * when writing WAL records.
269  *
270  * In the special space is the GinPageOpaque struct.
271  */
272 #define GinDataLeafPageGetPostingList(page) \
273  (GinPostingList *) ((PageGetContents(page) + MAXALIGN(sizeof(ItemPointerData))))
274 #define GinDataLeafPageGetPostingListSize(page) \
275  (((PageHeader) page)->pd_lower - MAXALIGN(SizeOfPageHeaderData) - MAXALIGN(sizeof(ItemPointerData)))
276 
277 #define GinDataLeafPageIsEmpty(page) \
278  (GinPageIsCompressed(page) ? (GinDataLeafPageGetPostingListSize(page) == 0) : (GinPageGetOpaque(page)->maxoff < FirstOffsetNumber))
279 
280 #define GinDataLeafPageGetFreeSpace(page) PageGetExactFreeSpace(page)
281 
282 #define GinDataPageGetRightBound(page) ((ItemPointer) PageGetContents(page))
283 /*
284  * Pointer to the data portion of a posting tree page. For internal pages,
285  * that's the beginning of the array of PostingItems. For compressed leaf
286  * pages, the first compressed posting list. For uncompressed (pre-9.4) leaf
287  * pages, it's the beginning of the ItemPointer array.
288  */
289 #define GinDataPageGetData(page) \
290  (PageGetContents(page) + MAXALIGN(sizeof(ItemPointerData)))
291 /* non-leaf pages contain PostingItems */
292 #define GinDataPageGetPostingItem(page, i) \
293  ((PostingItem *) (GinDataPageGetData(page) + ((i)-1) * sizeof(PostingItem)))
294 
295 /*
296  * Note: there is no GinDataPageGetDataSize macro, because before version
297  * 9.4, we didn't set pd_lower on data pages. There can be pages in the index
298  * that were binary-upgraded from earlier versions and still have an invalid
299  * pd_lower, so we cannot trust it in general. Compressed posting tree leaf
300  * pages are new in 9.4, however, so we can trust them; see
301  * GinDataLeafPageGetPostingListSize.
302  */
303 #define GinDataPageSetDataSize(page, size) \
304  { \
305  Assert(size <= GinDataPageMaxDataSize); \
306  ((PageHeader) page)->pd_lower = (size) + MAXALIGN(SizeOfPageHeaderData) + MAXALIGN(sizeof(ItemPointerData)); \
307  }
308 
309 #define GinNonLeafDataPageGetFreeSpace(page) \
310  (GinDataPageMaxDataSize - \
311  GinPageGetOpaque(page)->maxoff * sizeof(PostingItem))
312 
313 #define GinDataPageMaxDataSize \
314  (BLCKSZ - MAXALIGN(SizeOfPageHeaderData) \
315  - MAXALIGN(sizeof(ItemPointerData)) \
316  - MAXALIGN(sizeof(GinPageOpaqueData)))
317 
318 /*
319  * List pages
320  */
321 #define GinListPageSize \
322  ( BLCKSZ - SizeOfPageHeaderData - MAXALIGN(sizeof(GinPageOpaqueData)) )
323 
324 /*
325  * A compressed posting list.
326  *
327  * Note: This requires 2-byte alignment.
328  */
329 typedef struct
330 {
331  ItemPointerData first; /* first item in this posting list (unpacked) */
332  uint16 nbytes; /* number of bytes that follow */
333  unsigned char bytes[FLEXIBLE_ARRAY_MEMBER]; /* varbyte encoded items */
335 
336 #define SizeOfGinPostingList(plist) (offsetof(GinPostingList, bytes) + SHORTALIGN((plist)->nbytes) )
337 #define GinNextPostingListSegment(cur) ((GinPostingList *) (((char *) (cur)) + SizeOfGinPostingList((cur))))
338 
339 #endif /* GINBLOCK_H */
BlockNumber nEntryPages
Definition: ginblock.h:79
struct GinMetaPageData GinMetaPageData
BlockNumber rightlink
Definition: ginblock.h:30
BlockNumber nTotalPages
Definition: ginblock.h:78
uint32 BlockNumber
Definition: block.h:31
int64 nEntries
Definition: ginblock.h:81
signed int int32
Definition: c.h:302
uint16 OffsetNumber
Definition: off.h:24
struct GinPageOpaqueData GinPageOpaqueData
int64 nPendingHeapTuples
Definition: ginblock.h:73
unsigned short uint16
Definition: c.h:313
ItemPointerData first
Definition: ginblock.h:331
signed char GinNullCategory
Definition: ginblock.h:200
BlockNumber head
Definition: ginblock.h:60
BlockNumber tail
Definition: ginblock.h:61
unsigned int uint32
Definition: c.h:314
OffsetNumber maxoff
Definition: ginblock.h:31
GinPageOpaqueData * GinPageOpaque
Definition: ginblock.h:37
ItemPointerData key
Definition: ginblock.h:180
uint16 nbytes
Definition: ginblock.h:332
uint32 tailFreeSize
Definition: ginblock.h:66
int32 ginVersion
Definition: ginblock.h:98
BlockIdData child_blkno
Definition: ginblock.h:179
BlockNumber nDataPages
Definition: ginblock.h:80
BlockNumber nPendingPages
Definition: ginblock.h:72