PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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  BlockIdGetBlockNumber(&(pointer)->ip_blkid)
136 
137 #define GinItemPointerGetOffsetNumber(pointer) \
138  ((pointer)->ip_posid)
139 
140 /*
141  * Special-case item pointer values needed by the GIN search logic.
142  * MIN: sorts less than any valid item pointer
143  * MAX: sorts greater than any valid item pointer
144  * LOSSY PAGE: indicates a whole heap page, sorts after normal item
145  * pointers for that page
146  * Note that these are all distinguishable from an "invalid" item pointer
147  * (which is InvalidBlockNumber/0) as well as from all normal item
148  * pointers (which have item numbers in the range 1..MaxHeapTuplesPerPage).
149  */
150 #define ItemPointerSetMin(p) \
151  ItemPointerSet((p), (BlockNumber)0, (OffsetNumber)0)
152 #define ItemPointerIsMin(p) \
153  (GinItemPointerGetOffsetNumber(p) == (OffsetNumber)0 && \
154  GinItemPointerGetBlockNumber(p) == (BlockNumber)0)
155 #define ItemPointerSetMax(p) \
156  ItemPointerSet((p), InvalidBlockNumber, (OffsetNumber)0xffff)
157 #define ItemPointerIsMax(p) \
158  (GinItemPointerGetOffsetNumber(p) == (OffsetNumber)0xffff && \
159  GinItemPointerGetBlockNumber(p) == InvalidBlockNumber)
160 #define ItemPointerSetLossyPage(p, b) \
161  ItemPointerSet((p), (b), (OffsetNumber)0xffff)
162 #define ItemPointerIsLossyPage(p) \
163  (GinItemPointerGetOffsetNumber(p) == (OffsetNumber)0xffff && \
164  GinItemPointerGetBlockNumber(p) != InvalidBlockNumber)
165 
166 /*
167  * Posting item in a non-leaf posting-tree page
168  */
169 typedef struct
170 {
171  /* We use BlockIdData not BlockNumber to avoid padding space wastage */
174 } PostingItem;
175 
176 #define PostingItemGetBlockNumber(pointer) \
177  BlockIdGetBlockNumber(&(pointer)->child_blkno)
178 
179 #define PostingItemSetBlockNumber(pointer, blockNumber) \
180  BlockIdSet(&((pointer)->child_blkno), (blockNumber))
181 
182 /*
183  * Category codes to distinguish placeholder nulls from ordinary NULL keys.
184  * Note that the datatype size and the first two code values are chosen to be
185  * compatible with the usual usage of bool isNull flags.
186  *
187  * GIN_CAT_EMPTY_QUERY is never stored in the index; and notice that it is
188  * chosen to sort before not after regular key values.
189  */
190 typedef signed char GinNullCategory;
191 
192 #define GIN_CAT_NORM_KEY 0 /* normal, non-null key value */
193 #define GIN_CAT_NULL_KEY 1 /* null key value */
194 #define GIN_CAT_EMPTY_ITEM 2 /* placeholder for zero-key item */
195 #define GIN_CAT_NULL_ITEM 3 /* placeholder for null item */
196 #define GIN_CAT_EMPTY_QUERY (-1) /* placeholder for full-scan query */
197 
198 /*
199  * Access macros for null category byte in entry tuples
200  */
201 #define GinCategoryOffset(itup,ginstate) \
202  (IndexInfoFindDataOffset((itup)->t_info) + \
203  ((ginstate)->oneCol ? 0 : sizeof(int16)))
204 #define GinGetNullCategory(itup,ginstate) \
205  (*((GinNullCategory *) ((char*)(itup) + GinCategoryOffset(itup,ginstate))))
206 #define GinSetNullCategory(itup,ginstate,c) \
207  (*((GinNullCategory *) ((char*)(itup) + GinCategoryOffset(itup,ginstate))) = (c))
208 
209 /*
210  * Access macros for leaf-page entry tuples (see discussion in README)
211  */
212 #define GinGetNPosting(itup) GinItemPointerGetOffsetNumber(&(itup)->t_tid)
213 #define GinSetNPosting(itup,n) ItemPointerSetOffsetNumber(&(itup)->t_tid,n)
214 #define GIN_TREE_POSTING ((OffsetNumber)0xffff)
215 #define GinIsPostingTree(itup) (GinGetNPosting(itup) == GIN_TREE_POSTING)
216 #define GinSetPostingTree(itup, blkno) ( GinSetNPosting((itup),GIN_TREE_POSTING), ItemPointerSetBlockNumber(&(itup)->t_tid, blkno) )
217 #define GinGetPostingTree(itup) GinItemPointerGetBlockNumber(&(itup)->t_tid)
218 
219 #define GIN_ITUP_COMPRESSED (1U << 31)
220 #define GinGetPostingOffset(itup) (GinItemPointerGetBlockNumber(&(itup)->t_tid) & (~GIN_ITUP_COMPRESSED))
221 #define GinSetPostingOffset(itup,n) ItemPointerSetBlockNumber(&(itup)->t_tid,(n)|GIN_ITUP_COMPRESSED)
222 #define GinGetPosting(itup) ((Pointer) ((char*)(itup) + GinGetPostingOffset(itup)))
223 #define GinItupIsCompressed(itup) ((GinItemPointerGetBlockNumber(&(itup)->t_tid) & GIN_ITUP_COMPRESSED) != 0)
224 
225 /*
226  * Maximum size of an item on entry tree page. Make sure that we fit at least
227  * three items on each page. (On regular B-tree indexes, we must fit at least
228  * three items: two data items and the "high key". In GIN entry tree, we don't
229  * currently store the high key explicitly, we just use the rightmost item on
230  * the page, so it would actually be enough to fit two items.)
231  */
232 #define GinMaxItemSize \
233  Min(INDEX_SIZE_MASK, \
234  MAXALIGN_DOWN(((BLCKSZ - \
235  MAXALIGN(SizeOfPageHeaderData + 3 * sizeof(ItemIdData)) - \
236  MAXALIGN(sizeof(GinPageOpaqueData))) / 3)))
237 
238 /*
239  * Access macros for non-leaf entry tuples
240  */
241 #define GinGetDownlink(itup) GinItemPointerGetBlockNumber(&(itup)->t_tid)
242 #define GinSetDownlink(itup,blkno) ItemPointerSet(&(itup)->t_tid, blkno, InvalidOffsetNumber)
243 
244 
245 /*
246  * Data (posting tree) pages
247  *
248  * Posting tree pages don't store regular tuples. Non-leaf pages contain
249  * PostingItems, which are pairs of ItemPointers and child block numbers.
250  * Leaf pages contain GinPostingLists and an uncompressed array of item
251  * pointers.
252  *
253  * In a leaf page, the compressed posting lists are stored after the regular
254  * page header, one after each other. Although we don't store regular tuples,
255  * pd_lower is used to indicate the end of the posting lists. After that, free
256  * space follows. This layout is compatible with the "standard" heap and
257  * index page layout described in bufpage.h, so that we can e.g set buffer_std
258  * when writing WAL records.
259  *
260  * In the special space is the GinPageOpaque struct.
261  */
262 #define GinDataLeafPageGetPostingList(page) \
263  (GinPostingList *) ((PageGetContents(page) + MAXALIGN(sizeof(ItemPointerData))))
264 #define GinDataLeafPageGetPostingListSize(page) \
265  (((PageHeader) page)->pd_lower - MAXALIGN(SizeOfPageHeaderData) - MAXALIGN(sizeof(ItemPointerData)))
266 
267 #define GinDataLeafPageIsEmpty(page) \
268  (GinPageIsCompressed(page) ? (GinDataLeafPageGetPostingListSize(page) == 0) : (GinPageGetOpaque(page)->maxoff < FirstOffsetNumber))
269 
270 #define GinDataLeafPageGetFreeSpace(page) PageGetExactFreeSpace(page)
271 
272 #define GinDataPageGetRightBound(page) ((ItemPointer) PageGetContents(page))
273 /*
274  * Pointer to the data portion of a posting tree page. For internal pages,
275  * that's the beginning of the array of PostingItems. For compressed leaf
276  * pages, the first compressed posting list. For uncompressed (pre-9.4) leaf
277  * pages, it's the beginning of the ItemPointer array.
278  */
279 #define GinDataPageGetData(page) \
280  (PageGetContents(page) + MAXALIGN(sizeof(ItemPointerData)))
281 /* non-leaf pages contain PostingItems */
282 #define GinDataPageGetPostingItem(page, i) \
283  ((PostingItem *) (GinDataPageGetData(page) + ((i)-1) * sizeof(PostingItem)))
284 
285 /*
286  * Note: there is no GinDataPageGetDataSize macro, because before version
287  * 9.4, we didn't set pd_lower on data pages. There can be pages in the index
288  * that were binary-upgraded from earlier versions and still have an invalid
289  * pd_lower, so we cannot trust it in general. Compressed posting tree leaf
290  * pages are new in 9.4, however, so we can trust them; see
291  * GinDataLeafPageGetPostingListSize.
292  */
293 #define GinDataPageSetDataSize(page, size) \
294  { \
295  Assert(size <= GinDataPageMaxDataSize); \
296  ((PageHeader) page)->pd_lower = (size) + MAXALIGN(SizeOfPageHeaderData) + MAXALIGN(sizeof(ItemPointerData)); \
297  }
298 
299 #define GinNonLeafDataPageGetFreeSpace(page) \
300  (GinDataPageMaxDataSize - \
301  GinPageGetOpaque(page)->maxoff * sizeof(PostingItem))
302 
303 #define GinDataPageMaxDataSize \
304  (BLCKSZ - MAXALIGN(SizeOfPageHeaderData) \
305  - MAXALIGN(sizeof(ItemPointerData)) \
306  - MAXALIGN(sizeof(GinPageOpaqueData)))
307 
308 /*
309  * List pages
310  */
311 #define GinListPageSize \
312  ( BLCKSZ - SizeOfPageHeaderData - MAXALIGN(sizeof(GinPageOpaqueData)) )
313 
314 /*
315  * A compressed posting list.
316  *
317  * Note: This requires 2-byte alignment.
318  */
319 typedef struct
320 {
321  ItemPointerData first; /* first item in this posting list (unpacked) */
322  uint16 nbytes; /* number of bytes that follow */
323  unsigned char bytes[FLEXIBLE_ARRAY_MEMBER]; /* varbyte encoded items */
325 
326 #define SizeOfGinPostingList(plist) (offsetof(GinPostingList, bytes) + SHORTALIGN((plist)->nbytes) )
327 #define GinNextPostingListSegment(cur) ((GinPostingList *) (((char *) (cur)) + SizeOfGinPostingList((cur))))
328 
329 #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:253
uint16 OffsetNumber
Definition: off.h:24
struct GinPageOpaqueData GinPageOpaqueData
int64 nPendingHeapTuples
Definition: ginblock.h:73
unsigned short uint16
Definition: c.h:264
ItemPointerData first
Definition: ginblock.h:321
signed char GinNullCategory
Definition: ginblock.h:190
BlockNumber head
Definition: ginblock.h:60
BlockNumber tail
Definition: ginblock.h:61
unsigned int uint32
Definition: c.h:265
OffsetNumber maxoff
Definition: ginblock.h:31
GinPageOpaqueData * GinPageOpaque
Definition: ginblock.h:37
ItemPointerData key
Definition: ginblock.h:173
uint16 nbytes
Definition: ginblock.h:322
uint32 tailFreeSize
Definition: ginblock.h:66
int32 ginVersion
Definition: ginblock.h:98
BlockIdData child_blkno
Definition: ginblock.h:172
BlockNumber nDataPages
Definition: ginblock.h:80
BlockNumber nPendingPages
Definition: ginblock.h:72