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-2017, 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  * Note that the datatype size and the first two code values are chosen to be
192  * compatible with the usual usage of bool isNull flags.
193  *
194  * GIN_CAT_EMPTY_QUERY is never stored in the index; and notice that it is
195  * chosen to sort before not after regular key values.
196  */
197 typedef signed char GinNullCategory;
198 
199 #define GIN_CAT_NORM_KEY 0 /* normal, non-null key value */
200 #define GIN_CAT_NULL_KEY 1 /* null key value */
201 #define GIN_CAT_EMPTY_ITEM 2 /* placeholder for zero-key item */
202 #define GIN_CAT_NULL_ITEM 3 /* placeholder for null item */
203 #define GIN_CAT_EMPTY_QUERY (-1) /* placeholder for full-scan query */
204 
205 /*
206  * Access macros for null category byte in entry tuples
207  */
208 #define GinCategoryOffset(itup,ginstate) \
209  (IndexInfoFindDataOffset((itup)->t_info) + \
210  ((ginstate)->oneCol ? 0 : sizeof(int16)))
211 #define GinGetNullCategory(itup,ginstate) \
212  (*((GinNullCategory *) ((char*)(itup) + GinCategoryOffset(itup,ginstate))))
213 #define GinSetNullCategory(itup,ginstate,c) \
214  (*((GinNullCategory *) ((char*)(itup) + GinCategoryOffset(itup,ginstate))) = (c))
215 
216 /*
217  * Access macros for leaf-page entry tuples (see discussion in README)
218  */
219 #define GinGetNPosting(itup) GinItemPointerGetOffsetNumber(&(itup)->t_tid)
220 #define GinSetNPosting(itup,n) ItemPointerSetOffsetNumber(&(itup)->t_tid,n)
221 #define GIN_TREE_POSTING ((OffsetNumber)0xffff)
222 #define GinIsPostingTree(itup) (GinGetNPosting(itup) == GIN_TREE_POSTING)
223 #define GinSetPostingTree(itup, blkno) ( GinSetNPosting((itup),GIN_TREE_POSTING), ItemPointerSetBlockNumber(&(itup)->t_tid, blkno) )
224 #define GinGetPostingTree(itup) GinItemPointerGetBlockNumber(&(itup)->t_tid)
225 
226 #define GIN_ITUP_COMPRESSED (1U << 31)
227 #define GinGetPostingOffset(itup) (GinItemPointerGetBlockNumber(&(itup)->t_tid) & (~GIN_ITUP_COMPRESSED))
228 #define GinSetPostingOffset(itup,n) ItemPointerSetBlockNumber(&(itup)->t_tid,(n)|GIN_ITUP_COMPRESSED)
229 #define GinGetPosting(itup) ((Pointer) ((char*)(itup) + GinGetPostingOffset(itup)))
230 #define GinItupIsCompressed(itup) ((GinItemPointerGetBlockNumber(&(itup)->t_tid) & GIN_ITUP_COMPRESSED) != 0)
231 
232 /*
233  * Maximum size of an item on entry tree page. Make sure that we fit at least
234  * three items on each page. (On regular B-tree indexes, we must fit at least
235  * three items: two data items and the "high key". In GIN entry tree, we don't
236  * currently store the high key explicitly, we just use the rightmost item on
237  * the page, so it would actually be enough to fit two items.)
238  */
239 #define GinMaxItemSize \
240  Min(INDEX_SIZE_MASK, \
241  MAXALIGN_DOWN(((BLCKSZ - \
242  MAXALIGN(SizeOfPageHeaderData + 3 * sizeof(ItemIdData)) - \
243  MAXALIGN(sizeof(GinPageOpaqueData))) / 3)))
244 
245 /*
246  * Access macros for non-leaf entry tuples
247  */
248 #define GinGetDownlink(itup) GinItemPointerGetBlockNumber(&(itup)->t_tid)
249 #define GinSetDownlink(itup,blkno) ItemPointerSet(&(itup)->t_tid, blkno, InvalidOffsetNumber)
250 
251 
252 /*
253  * Data (posting tree) pages
254  *
255  * Posting tree pages don't store regular tuples. Non-leaf pages contain
256  * PostingItems, which are pairs of ItemPointers and child block numbers.
257  * Leaf pages contain GinPostingLists and an uncompressed array of item
258  * pointers.
259  *
260  * In a leaf page, the compressed posting lists are stored after the regular
261  * page header, one after each other. Although we don't store regular tuples,
262  * pd_lower is used to indicate the end of the posting lists. After that, free
263  * space follows. This layout is compatible with the "standard" heap and
264  * index page layout described in bufpage.h, so that we can e.g set buffer_std
265  * when writing WAL records.
266  *
267  * In the special space is the GinPageOpaque struct.
268  */
269 #define GinDataLeafPageGetPostingList(page) \
270  (GinPostingList *) ((PageGetContents(page) + MAXALIGN(sizeof(ItemPointerData))))
271 #define GinDataLeafPageGetPostingListSize(page) \
272  (((PageHeader) page)->pd_lower - MAXALIGN(SizeOfPageHeaderData) - MAXALIGN(sizeof(ItemPointerData)))
273 
274 #define GinDataLeafPageIsEmpty(page) \
275  (GinPageIsCompressed(page) ? (GinDataLeafPageGetPostingListSize(page) == 0) : (GinPageGetOpaque(page)->maxoff < FirstOffsetNumber))
276 
277 #define GinDataLeafPageGetFreeSpace(page) PageGetExactFreeSpace(page)
278 
279 #define GinDataPageGetRightBound(page) ((ItemPointer) PageGetContents(page))
280 /*
281  * Pointer to the data portion of a posting tree page. For internal pages,
282  * that's the beginning of the array of PostingItems. For compressed leaf
283  * pages, the first compressed posting list. For uncompressed (pre-9.4) leaf
284  * pages, it's the beginning of the ItemPointer array.
285  */
286 #define GinDataPageGetData(page) \
287  (PageGetContents(page) + MAXALIGN(sizeof(ItemPointerData)))
288 /* non-leaf pages contain PostingItems */
289 #define GinDataPageGetPostingItem(page, i) \
290  ((PostingItem *) (GinDataPageGetData(page) + ((i)-1) * sizeof(PostingItem)))
291 
292 /*
293  * Note: there is no GinDataPageGetDataSize macro, because before version
294  * 9.4, we didn't set pd_lower on data pages. There can be pages in the index
295  * that were binary-upgraded from earlier versions and still have an invalid
296  * pd_lower, so we cannot trust it in general. Compressed posting tree leaf
297  * pages are new in 9.4, however, so we can trust them; see
298  * GinDataLeafPageGetPostingListSize.
299  */
300 #define GinDataPageSetDataSize(page, size) \
301  { \
302  Assert(size <= GinDataPageMaxDataSize); \
303  ((PageHeader) page)->pd_lower = (size) + MAXALIGN(SizeOfPageHeaderData) + MAXALIGN(sizeof(ItemPointerData)); \
304  }
305 
306 #define GinNonLeafDataPageGetFreeSpace(page) \
307  (GinDataPageMaxDataSize - \
308  GinPageGetOpaque(page)->maxoff * sizeof(PostingItem))
309 
310 #define GinDataPageMaxDataSize \
311  (BLCKSZ - MAXALIGN(SizeOfPageHeaderData) \
312  - MAXALIGN(sizeof(ItemPointerData)) \
313  - MAXALIGN(sizeof(GinPageOpaqueData)))
314 
315 /*
316  * List pages
317  */
318 #define GinListPageSize \
319  ( BLCKSZ - SizeOfPageHeaderData - MAXALIGN(sizeof(GinPageOpaqueData)) )
320 
321 /*
322  * A compressed posting list.
323  *
324  * Note: This requires 2-byte alignment.
325  */
326 typedef struct
327 {
328  ItemPointerData first; /* first item in this posting list (unpacked) */
329  uint16 nbytes; /* number of bytes that follow */
330  unsigned char bytes[FLEXIBLE_ARRAY_MEMBER]; /* varbyte encoded items */
332 
333 #define SizeOfGinPostingList(plist) (offsetof(GinPostingList, bytes) + SHORTALIGN((plist)->nbytes) )
334 #define GinNextPostingListSegment(cur) ((GinPostingList *) (((char *) (cur)) + SizeOfGinPostingList((cur))))
335 
336 #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:284
uint16 OffsetNumber
Definition: off.h:24
struct GinPageOpaqueData GinPageOpaqueData
int64 nPendingHeapTuples
Definition: ginblock.h:73
unsigned short uint16
Definition: c.h:295
ItemPointerData first
Definition: ginblock.h:328
signed char GinNullCategory
Definition: ginblock.h:197
BlockNumber head
Definition: ginblock.h:60
BlockNumber tail
Definition: ginblock.h:61
unsigned int uint32
Definition: c.h:296
OffsetNumber maxoff
Definition: ginblock.h:31
GinPageOpaqueData * GinPageOpaque
Definition: ginblock.h:37
ItemPointerData key
Definition: ginblock.h:180
uint16 nbytes
Definition: ginblock.h:329
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