1 /*--------------------------------------------------------------------------
3 * header file for postgres inverted index access method implementation.
5 * Copyright (c) 2006-2014, PostgreSQL Global Development Group
7 * src/include/access/gin_private.h
8 *--------------------------------------------------------------------------
13 #include "access/genam.h"
14 #include "access/gin.h"
15 #include "access/itup.h"
17 #include "storage/bufmgr.h"
18 #include "utils/rbtree.h"
22 * Page opaque data in an inverted index page.
24 * Note: GIN does not include a page ID word as do the other index types.
25 * This is OK because the opaque data is only 8 bytes and so can be reliably
26 * distinguished by size. Revisit this if the size ever increases.
27 * Further note: as of 9.2, SP-GiST also uses 8-byte special space. This is
28 * still OK, as long as GIN isn't using all of the high-order bits in its
29 * flags word, because that way the flags word cannot match the page ID used
32 typedef struct GinPageOpaqueData
34 BlockNumber rightlink; /* next page if any */
35 OffsetNumber maxoff; /* number of PostingItems on GIN_DATA &
36 * ~GIN_LEAF page. On GIN_LIST page, number of
38 uint16 flags; /* see bit definitions below */
41 typedef GinPageOpaqueData *GinPageOpaque;
43 #define GIN_DATA (1 << 0)
44 #define GIN_LEAF (1 << 1)
45 #define GIN_DELETED (1 << 2)
46 #define GIN_META (1 << 3)
47 #define GIN_LIST (1 << 4)
48 #define GIN_LIST_FULLROW (1 << 5) /* makes sense only on GIN_LIST page */
49 #define GIN_INCOMPLETE_SPLIT (1 << 6) /* page was split, but parent not
51 #define GIN_COMPRESSED (1 << 7)
53 /* Page numbers of fixed-location pages */
54 #define GIN_METAPAGE_BLKNO (0)
55 #define GIN_ROOT_BLKNO (1)
57 typedef struct GinMetaPageData
60 * Pointers to head and tail of pending list, which consists of GIN_LIST
61 * pages. These store fast-inserted entries that haven't yet been moved
62 * into the regular GIN structure.
68 * Free space in bytes in the pending list's tail page.
73 * We store both number of pages and number of heap tuples that are in the
76 BlockNumber nPendingPages;
77 int64 nPendingHeapTuples;
80 * Statistics for planner use (accurate as of last VACUUM)
82 BlockNumber nTotalPages;
83 BlockNumber nEntryPages;
84 BlockNumber nDataPages;
88 * GIN version number (ideally this should have been at the front, but too
89 * late now. Don't move it!)
91 * Currently 2 (for indexes initialized in 9.4 or later)
93 * Version 1 (indexes initialized in version 9.1, 9.2 or 9.3), is
94 * compatible, but may contain uncompressed posting tree (leaf) pages and
95 * posting lists. They will be converted to compressed format when
98 * Version 0 (indexes initialized in 9.0 or before) is compatible but may
99 * be missing null entries, including both null keys and placeholders.
100 * Reject full-index-scan attempts on such indexes.
105 #define GIN_CURRENT_VERSION 2
107 #define GinPageGetMeta(p) \
108 ((GinMetaPageData *) PageGetContents(p))
111 * Macros for accessing a GIN index page's opaque data
113 #define GinPageGetOpaque(page) ( (GinPageOpaque) PageGetSpecialPointer(page) )
115 #define GinPageIsLeaf(page) ( GinPageGetOpaque(page)->flags & GIN_LEAF )
116 #define GinPageSetLeaf(page) ( GinPageGetOpaque(page)->flags |= GIN_LEAF )
117 #define GinPageSetNonLeaf(page) ( GinPageGetOpaque(page)->flags &= ~GIN_LEAF )
118 #define GinPageIsData(page) ( GinPageGetOpaque(page)->flags & GIN_DATA )
119 #define GinPageSetData(page) ( GinPageGetOpaque(page)->flags |= GIN_DATA )
120 #define GinPageIsList(page) ( GinPageGetOpaque(page)->flags & GIN_LIST )
121 #define GinPageSetList(page) ( GinPageGetOpaque(page)->flags |= GIN_LIST )
122 #define GinPageHasFullRow(page) ( GinPageGetOpaque(page)->flags & GIN_LIST_FULLROW )
123 #define GinPageSetFullRow(page) ( GinPageGetOpaque(page)->flags |= GIN_LIST_FULLROW )
124 #define GinPageIsCompressed(page) ( GinPageGetOpaque(page)->flags & GIN_COMPRESSED )
125 #define GinPageSetCompressed(page) ( GinPageGetOpaque(page)->flags |= GIN_COMPRESSED )
127 #define GinPageIsDeleted(page) ( GinPageGetOpaque(page)->flags & GIN_DELETED)
128 #define GinPageSetDeleted(page) ( GinPageGetOpaque(page)->flags |= GIN_DELETED)
129 #define GinPageSetNonDeleted(page) ( GinPageGetOpaque(page)->flags &= ~GIN_DELETED)
130 #define GinPageIsIncompleteSplit(page) ( GinPageGetOpaque(page)->flags & GIN_INCOMPLETE_SPLIT)
132 #define GinPageRightMost(page) ( GinPageGetOpaque(page)->rightlink == InvalidBlockNumber)
135 * We use our own ItemPointerGet(BlockNumber|OffsetNumber)
136 * to avoid Asserts, since sometimes the ip_posid isn't "valid"
138 #define GinItemPointerGetBlockNumber(pointer) \
139 BlockIdGetBlockNumber(&(pointer)->ip_blkid)
141 #define GinItemPointerGetOffsetNumber(pointer) \
142 ((pointer)->ip_posid)
145 * Special-case item pointer values needed by the GIN search logic.
146 * MIN: sorts less than any valid item pointer
147 * MAX: sorts greater than any valid item pointer
148 * LOSSY PAGE: indicates a whole heap page, sorts after normal item
149 * pointers for that page
150 * Note that these are all distinguishable from an "invalid" item pointer
151 * (which is InvalidBlockNumber/0) as well as from all normal item
152 * pointers (which have item numbers in the range 1..MaxHeapTuplesPerPage).
154 #define ItemPointerSetMin(p) \
155 ItemPointerSet((p), (BlockNumber)0, (OffsetNumber)0)
156 #define ItemPointerIsMin(p) \
157 (GinItemPointerGetOffsetNumber(p) == (OffsetNumber)0 && \
158 GinItemPointerGetBlockNumber(p) == (BlockNumber)0)
159 #define ItemPointerSetMax(p) \
160 ItemPointerSet((p), InvalidBlockNumber, (OffsetNumber)0xffff)
161 #define ItemPointerIsMax(p) \
162 (GinItemPointerGetOffsetNumber(p) == (OffsetNumber)0xffff && \
163 GinItemPointerGetBlockNumber(p) == InvalidBlockNumber)
164 #define ItemPointerSetLossyPage(p, b) \
165 ItemPointerSet((p), (b), (OffsetNumber)0xffff)
166 #define ItemPointerIsLossyPage(p) \
167 (GinItemPointerGetOffsetNumber(p) == (OffsetNumber)0xffff && \
168 GinItemPointerGetBlockNumber(p) != InvalidBlockNumber)
171 * Posting item in a non-leaf posting-tree page
175 /* We use BlockIdData not BlockNumber to avoid padding space wastage */
176 BlockIdData child_blkno;
180 #define PostingItemGetBlockNumber(pointer) \
181 BlockIdGetBlockNumber(&(pointer)->child_blkno)
183 #define PostingItemSetBlockNumber(pointer, blockNumber) \
184 BlockIdSet(&((pointer)->child_blkno), (blockNumber))
187 * Category codes to distinguish placeholder nulls from ordinary NULL keys.
188 * Note that the datatype size and the first two code values are chosen to be
189 * compatible with the usual usage of bool isNull flags.
191 * GIN_CAT_EMPTY_QUERY is never stored in the index; and notice that it is
192 * chosen to sort before not after regular key values.
194 typedef signed char GinNullCategory;
196 #define GIN_CAT_NORM_KEY 0 /* normal, non-null key value */
197 #define GIN_CAT_NULL_KEY 1 /* null key value */
198 #define GIN_CAT_EMPTY_ITEM 2 /* placeholder for zero-key item */
199 #define GIN_CAT_NULL_ITEM 3 /* placeholder for null item */
200 #define GIN_CAT_EMPTY_QUERY (-1) /* placeholder for full-scan query */
203 * Access macros for null category byte in entry tuples
205 #define GinCategoryOffset(itup,ginstate) \
206 (IndexInfoFindDataOffset((itup)->t_info) + \
207 ((ginstate)->oneCol ? 0 : sizeof(int16)))
208 #define GinGetNullCategory(itup,ginstate) \
209 (*((GinNullCategory *) ((char*)(itup) + GinCategoryOffset(itup,ginstate))))
210 #define GinSetNullCategory(itup,ginstate,c) \
211 (*((GinNullCategory *) ((char*)(itup) + GinCategoryOffset(itup,ginstate))) = (c))
214 * Access macros for leaf-page entry tuples (see discussion in README)
216 #define GinGetNPosting(itup) GinItemPointerGetOffsetNumber(&(itup)->t_tid)
217 #define GinSetNPosting(itup,n) ItemPointerSetOffsetNumber(&(itup)->t_tid,n)
218 #define GIN_TREE_POSTING ((OffsetNumber)0xffff)
219 #define GinIsPostingTree(itup) (GinGetNPosting(itup) == GIN_TREE_POSTING)
220 #define GinSetPostingTree(itup, blkno) ( GinSetNPosting((itup),GIN_TREE_POSTING), ItemPointerSetBlockNumber(&(itup)->t_tid, blkno) )
221 #define GinGetPostingTree(itup) GinItemPointerGetBlockNumber(&(itup)->t_tid)
223 #define GIN_ITUP_COMPRESSED (1U << 31)
224 #define GinGetPostingOffset(itup) (GinItemPointerGetBlockNumber(&(itup)->t_tid) & (~GIN_ITUP_COMPRESSED))
225 #define GinSetPostingOffset(itup,n) ItemPointerSetBlockNumber(&(itup)->t_tid,(n)|GIN_ITUP_COMPRESSED)
226 #define GinGetPosting(itup) ((Pointer) ((char*)(itup) + GinGetPostingOffset(itup)))
227 #define GinItupIsCompressed(itup) (GinItemPointerGetBlockNumber(&(itup)->t_tid) & GIN_ITUP_COMPRESSED)
230 * Maximum size of an item on entry tree page. Make sure that we fit at least
231 * three items on each page. (On regular B-tree indexes, we must fit at least
232 * three items: two data items and the "high key". In GIN entry tree, we don't
233 * currently store the high key explicitly, we just use the rightmost item on
234 * the page, so it would actually be enough to fit two items.)
236 #define GinMaxItemSize \
237 Min(INDEX_SIZE_MASK, \
238 MAXALIGN_DOWN(((BLCKSZ - \
239 MAXALIGN(SizeOfPageHeaderData + 3 * sizeof(ItemIdData)) - \
240 MAXALIGN(sizeof(GinPageOpaqueData))) / 3)))
243 * Access macros for non-leaf entry tuples
245 #define GinGetDownlink(itup) GinItemPointerGetBlockNumber(&(itup)->t_tid)
246 #define GinSetDownlink(itup,blkno) ItemPointerSet(&(itup)->t_tid, blkno, InvalidOffsetNumber)
250 * Data (posting tree) pages
252 * Posting tree pages don't store regular tuples. Non-leaf pages contain
253 * PostingItems, which are pairs of ItemPointers and child block numbers.
254 * Leaf pages contain GinPostingLists and an uncompressed array of item
257 * In a leaf page, the compressed posting lists are stored after the regular
258 * page header, one after each other. Although we don't store regular tuples,
259 * pd_lower is used to indicate the end of the posting lists. After that, free
260 * space follows. This layout is compatible with the "standard" heap and
261 * index page layout described in bufpage.h, so that we can e.g set buffer_std
262 * when writing WAL records.
264 * In the special space is the GinPageOpaque struct.
266 #define GinDataLeafPageGetPostingList(page) \
267 (GinPostingList *) ((PageGetContents(page) + MAXALIGN(sizeof(ItemPointerData))))
268 #define GinDataLeafPageGetPostingListSize(page) \
269 (((PageHeader) page)->pd_lower - MAXALIGN(SizeOfPageHeaderData) - MAXALIGN(sizeof(ItemPointerData)))
271 #define GinDataLeafPageIsEmpty(page) \
272 (GinPageIsCompressed(page) ? (GinDataLeafPageGetPostingListSize(page) == 0) : (GinPageGetOpaque(page)->maxoff < FirstOffsetNumber))
274 #define GinDataLeafPageGetFreeSpace(page) PageGetExactFreeSpace(page)
276 #define GinDataPageGetRightBound(page) ((ItemPointer) PageGetContents(page))
278 * Pointer to the data portion of a posting tree page. For internal pages,
279 * that's the beginning of the array of PostingItems. For compressed leaf
280 * pages, the first compressed posting list. For uncompressed (pre-9.4) leaf
281 * pages, it's the beginning of the ItemPointer array.
283 #define GinDataPageGetData(page) \
284 (PageGetContents(page) + MAXALIGN(sizeof(ItemPointerData)))
285 /* non-leaf pages contain PostingItems */
286 #define GinDataPageGetPostingItem(page, i) \
287 ((PostingItem *) (GinDataPageGetData(page) + ((i)-1) * sizeof(PostingItem)))
290 * Note: there is no GinDataPageGetDataSize macro, because before version
291 * 9.4, we didn't set pd_lower on data pages. There can be pages in the index
292 * that were binary-upgraded from earlier versions and still have an invalid
293 * pd_lower, so we cannot trust it in general. Compressed posting tree leaf
294 * pages are new in 9.4, however, so we can trust them; see
295 * GinDataLeafPageGetPostingListSize.
297 #define GinDataPageSetDataSize(page, size) \
299 Assert(size <= GinDataPageMaxDataSize); \
300 ((PageHeader) page)->pd_lower = (size) + MAXALIGN(SizeOfPageHeaderData) + MAXALIGN(sizeof(ItemPointerData)); \
303 #define GinNonLeafDataPageGetFreeSpace(page) \
304 (GinDataPageMaxDataSize - \
305 GinPageGetOpaque(page)->maxoff * sizeof(PostingItem))
307 #define GinDataPageMaxDataSize \
308 (BLCKSZ - MAXALIGN(SizeOfPageHeaderData) \
309 - MAXALIGN(sizeof(ItemPointerData)) \
310 - MAXALIGN(sizeof(GinPageOpaqueData)))
315 #define GinListPageSize \
316 ( BLCKSZ - SizeOfPageHeaderData - MAXALIGN(sizeof(GinPageOpaqueData)) )
319 * Storage type for GIN's reloptions
321 typedef struct GinOptions
323 int32 vl_len_; /* varlena header (do not touch directly!) */
324 bool useFastUpdate; /* use fast updates? */
327 #define GIN_DEFAULT_USE_FASTUPDATE true
328 #define GinGetUseFastUpdate(relation) \
329 ((relation)->rd_options ? \
330 ((GinOptions *) (relation)->rd_options)->useFastUpdate : GIN_DEFAULT_USE_FASTUPDATE)
333 /* Macros for buffer lock/unlock operations */
334 #define GIN_UNLOCK BUFFER_LOCK_UNLOCK
335 #define GIN_SHARE BUFFER_LOCK_SHARE
336 #define GIN_EXCLUSIVE BUFFER_LOCK_EXCLUSIVE
340 * GinState: working data structure describing the index being worked on
342 typedef struct GinState
345 bool oneCol; /* true if single-column index */
348 * origTupDesc is the nominal tuple descriptor of the index, ie, the i'th
349 * attribute shows the key type (not the input data type!) of the i'th
350 * index column. In a single-column index this describes the actual leaf
351 * index tuples. In a multi-column index, the actual leaf tuples contain
352 * a smallint column number followed by a key datum of the appropriate
353 * type for that column. We set up tupdesc[i] to describe the actual
354 * rowtype of the index tuples for the i'th column, ie, (int2, keytype).
355 * Note that in any case, leaf tuples contain more data than is known to
356 * the TupleDesc; see access/gin/README for details.
358 TupleDesc origTupdesc;
359 TupleDesc tupdesc[INDEX_MAX_KEYS];
362 * Per-index-column opclass support functions
364 FmgrInfo compareFn[INDEX_MAX_KEYS];
365 FmgrInfo extractValueFn[INDEX_MAX_KEYS];
366 FmgrInfo extractQueryFn[INDEX_MAX_KEYS];
367 FmgrInfo consistentFn[INDEX_MAX_KEYS];
368 FmgrInfo triConsistentFn[INDEX_MAX_KEYS];
369 FmgrInfo comparePartialFn[INDEX_MAX_KEYS]; /* optional method */
370 /* canPartialMatch[i] is true if comparePartialFn[i] is valid */
371 bool canPartialMatch[INDEX_MAX_KEYS];
372 /* Collations to pass to the support functions */
373 Oid supportCollation[INDEX_MAX_KEYS];
378 * A compressed posting list.
380 * Note: This requires 2-byte alignment.
384 ItemPointerData first; /* first item in this posting list (unpacked) */
385 uint16 nbytes; /* number of bytes that follow */
386 unsigned char bytes[1]; /* varbyte encoded items (variable length) */
389 #define SizeOfGinPostingList(plist) (offsetof(GinPostingList, bytes) + SHORTALIGN((plist)->nbytes) )
390 #define GinNextPostingListSegment(cur) ((GinPostingList *) (((char *) (cur)) + SizeOfGinPostingList((cur))))
395 #define XLOG_GIN_CREATE_INDEX 0x00
397 #define XLOG_GIN_CREATE_PTREE 0x10
399 typedef struct ginxlogCreatePostingTree
404 /* A compressed posting list follows */
405 } ginxlogCreatePostingTree;
407 #define XLOG_GIN_INSERT 0x20
410 * The format of the insertion record varies depending on the page type.
411 * ginxlogInsert is the common part between all variants.
417 uint16 flags; /* GIN_SPLIT_ISLEAF and/or GIN_SPLIT_ISDATA */
422 * 1. if not leaf page, block numbers of the left and right child pages
423 * whose split this insertion finishes. As BlockIdData[2] (beware of
424 * adding fields before this that would make them not 16-bit aligned)
426 * 2. an ginxlogInsertEntry or ginxlogRecompressDataLeaf struct, depending
429 * NB: the below structs are only 16-bit aligned when appended to a
430 * ginxlogInsert struct! Beware of adding fields to them that require
431 * stricter alignment.
439 IndexTupleData tuple; /* variable length */
440 } ginxlogInsertEntry;
447 /* Variable number of 'actions' follow */
448 } ginxlogRecompressDataLeaf;
451 * Note: this struct is currently not used in code, and only acts as
452 * documentation. The WAL record format is as specified here, but the code
453 * uses straight access through a Pointer and memcpy to read/write these.
457 uint8 segno; /* segment this action applies to */
458 char type; /* action type (see below) */
461 * Action-specific data follows. For INSERT and REPLACE actions that is a
462 * GinPostingList struct. For ADDITEMS, a uint16 for the number of items
463 * added, followed by the items themselves as ItemPointers. DELETE actions
464 * have no further data.
466 } ginxlogSegmentAction;
469 #define GIN_SEGMENT_UNMODIFIED 0 /* no action (not used in WAL records) */
470 #define GIN_SEGMENT_DELETE 1 /* a whole segment is removed */
471 #define GIN_SEGMENT_INSERT 2 /* a whole segment is added */
472 #define GIN_SEGMENT_REPLACE 3 /* a segment is replaced */
473 #define GIN_SEGMENT_ADDITEMS 4 /* items are added to existing segment */
479 } ginxlogInsertDataInternal;
482 #define XLOG_GIN_SPLIT 0x30
484 typedef struct ginxlogSplit
489 BlockNumber rrlink; /* right link, or root's blocknumber if root
491 BlockNumber leftChildBlkno; /* valid on a non-leaf split */
492 BlockNumber rightChildBlkno;
495 /* follows: one of the following structs */
499 * Flags used in ginxlogInsert and ginxlogSplit records
501 #define GIN_INSERT_ISDATA 0x01 /* for both insert and split records */
502 #define GIN_INSERT_ISLEAF 0x02 /* .. */
503 #define GIN_SPLIT_ROOT 0x04 /* only for split records */
507 OffsetNumber separator;
510 /* FOLLOWS: IndexTuples */
517 ItemPointerData lrightbound; /* new right bound of left page */
518 ItemPointerData rrightbound; /* new right bound of right page */
520 /* FOLLOWS: new compressed posting lists of left and right page */
522 } ginxlogSplitDataLeaf;
526 OffsetNumber separator;
528 ItemPointerData rightbound;
530 /* FOLLOWS: array of PostingItems */
531 } ginxlogSplitDataInternal;
534 * Vacuum simply WAL-logs the whole page, when anything is modified. This
535 * functionally identical heap_newpage records, but is kept separate for
536 * debugging purposes. (When inspecting the WAL stream, it's easier to see
537 * what's going on when GIN vacuum records are marked as such, not as heap
538 * records.) This is currently only used for entry tree leaf pages.
540 #define XLOG_GIN_VACUUM_PAGE 0x40
542 typedef struct ginxlogVacuumPage
546 uint16 hole_offset; /* number of bytes before "hole" */
547 uint16 hole_length; /* number of bytes in "hole" */
548 /* entire page contents (minus the hole) follow at end of record */
552 * Vacuuming posting tree leaf page is WAL-logged like recompression caused
555 #define XLOG_GIN_VACUUM_DATA_LEAF_PAGE 0x90
557 typedef struct ginxlogVacuumDataLeafPage
562 ginxlogRecompressDataLeaf data;
563 } ginxlogVacuumDataLeafPage;
565 #define XLOG_GIN_DELETE_PAGE 0x50
567 typedef struct ginxlogDeletePage
571 BlockNumber parentBlkno;
572 OffsetNumber parentOffset;
573 BlockNumber leftBlkno;
574 BlockNumber rightLink;
577 #define XLOG_GIN_UPDATE_META_PAGE 0x60
579 typedef struct ginxlogUpdateMeta
582 GinMetaPageData metadata;
583 BlockNumber prevTail;
584 BlockNumber newRightlink;
585 int32 ntuples; /* if ntuples > 0 then metadata.tail was
586 * updated with that many tuples; else new sub
587 * list was inserted */
588 /* array of inserted tuples follows */
591 #define XLOG_GIN_INSERT_LISTPAGE 0x70
593 typedef struct ginxlogInsertListPage
597 BlockNumber rightlink;
599 /* array of inserted tuples follows */
600 } ginxlogInsertListPage;
602 #define XLOG_GIN_DELETE_LISTPAGE 0x80
604 #define GIN_NDELETE_AT_ONCE 16
605 typedef struct ginxlogDeleteListPages
608 GinMetaPageData metadata;
610 BlockNumber toDelete[GIN_NDELETE_AT_ONCE];
611 } ginxlogDeleteListPages;
615 extern Datum ginoptions(PG_FUNCTION_ARGS);
616 extern void initGinState(GinState *state, Relation index);
617 extern Buffer GinNewBuffer(Relation index);
618 extern void GinInitBuffer(Buffer b, uint32 f);
619 extern void GinInitPage(Page page, uint32 f, Size pageSize);
620 extern void GinInitMetabuffer(Buffer b);
621 extern int ginCompareEntries(GinState *ginstate, OffsetNumber attnum,
622 Datum a, GinNullCategory categorya,
623 Datum b, GinNullCategory categoryb);
624 extern int ginCompareAttEntries(GinState *ginstate,
625 OffsetNumber attnuma, Datum a, GinNullCategory categorya,
626 OffsetNumber attnumb, Datum b, GinNullCategory categoryb);
627 extern Datum *ginExtractEntries(GinState *ginstate, OffsetNumber attnum,
628 Datum value, bool isNull,
629 int32 *nentries, GinNullCategory **categories);
631 extern OffsetNumber gintuple_get_attrnum(GinState *ginstate, IndexTuple tuple);
632 extern Datum gintuple_get_key(GinState *ginstate, IndexTuple tuple,
633 GinNullCategory *category);
636 extern Datum ginbuild(PG_FUNCTION_ARGS);
637 extern Datum ginbuildempty(PG_FUNCTION_ARGS);
638 extern Datum gininsert(PG_FUNCTION_ARGS);
639 extern void ginEntryInsert(GinState *ginstate,
640 OffsetNumber attnum, Datum key, GinNullCategory category,
641 ItemPointerData *items, uint32 nitem,
642 GinStatsData *buildStats);
646 typedef struct GinBtreeStack
651 ItemPointerData iptr;
652 /* predictNumber contains predicted number of pages on current level */
653 uint32 predictNumber;
654 struct GinBtreeStack *parent;
657 typedef struct GinBtreeData *GinBtree;
659 /* Return codes for GinBtreeData.placeToPage method */
667 typedef struct GinBtreeData
670 BlockNumber (*findChildPage) (GinBtree, GinBtreeStack *);
671 BlockNumber (*getLeftMostChild) (GinBtree, Page);
672 bool (*isMoveRight) (GinBtree, Page);
673 bool (*findItem) (GinBtree, GinBtreeStack *);
676 OffsetNumber (*findChildPtr) (GinBtree, Page, BlockNumber, OffsetNumber);
677 GinPlaceToPageRC (*placeToPage) (GinBtree, Buffer, GinBtreeStack *, void *, BlockNumber, XLogRecData **, Page *, Page *);
678 void *(*prepareDownlink) (GinBtree, Buffer);
679 void (*fillRoot) (GinBtree, Page, BlockNumber, Page, BlockNumber, Page);
684 BlockNumber rootBlkno;
685 GinState *ginstate; /* not valid in a data scan */
689 /* Search key for Entry tree */
690 OffsetNumber entryAttnum;
692 GinNullCategory entryCategory;
694 /* Search key for data tree (posting tree) */
695 ItemPointerData itemptr;
698 /* This represents a tuple to be inserted to entry tree. */
701 IndexTuple entry; /* tuple to insert */
702 bool isDelete; /* delete old tuple at same offset? */
703 } GinBtreeEntryInsertData;
706 * This represents an itempointer, or many itempointers, to be inserted to
707 * a data (posting tree) leaf page
711 ItemPointerData *items;
714 } GinBtreeDataLeafInsertData;
717 * For internal data (posting tree) pages, the insertion payload is a
721 extern GinBtreeStack *ginFindLeafPage(GinBtree btree, bool searchMode);
722 extern Buffer ginStepRight(Buffer buffer, Relation index, int lockmode);
723 extern void freeGinBtreeStack(GinBtreeStack *stack);
724 extern void ginInsertValue(GinBtree btree, GinBtreeStack *stack,
725 void *insertdata, GinStatsData *buildStats);
728 extern IndexTuple GinFormTuple(GinState *ginstate,
729 OffsetNumber attnum, Datum key, GinNullCategory category,
730 Pointer data, Size dataSize, int nipd, bool errorTooBig);
731 extern void ginPrepareEntryScan(GinBtree btree, OffsetNumber attnum,
732 Datum key, GinNullCategory category,
734 extern void ginEntryFillRoot(GinBtree btree, Page root, BlockNumber lblkno, Page lpage, BlockNumber rblkno, Page rpage);
735 extern ItemPointer ginReadTuple(GinState *ginstate, OffsetNumber attnum,
736 IndexTuple itup, int *nitems);
739 extern ItemPointer GinDataLeafPageGetItems(Page page, int *nitems, ItemPointerData advancePast);
740 extern int GinDataLeafPageGetItemsToTbm(Page page, TIDBitmap *tbm);
741 extern BlockNumber createPostingTree(Relation index,
742 ItemPointerData *items, uint32 nitems,
743 GinStatsData *buildStats);
744 extern void GinDataPageAddPostingItem(Page page, PostingItem *data, OffsetNumber offset);
745 extern void GinPageDeletePostingItem(Page page, OffsetNumber offset);
746 extern void ginInsertItemPointers(Relation index, BlockNumber rootBlkno,
747 ItemPointerData *items, uint32 nitem,
748 GinStatsData *buildStats);
749 extern GinBtreeStack *ginScanBeginPostingTree(GinBtree btree, Relation index, BlockNumber rootBlkno);
750 extern void ginDataFillRoot(GinBtree btree, Page root, BlockNumber lblkno, Page lpage, BlockNumber rblkno, Page rpage);
751 extern void ginPrepareDataScan(GinBtree btree, Relation index, BlockNumber rootBlkno);
754 * This is declared in ginvacuum.c, but is passed between ginVacuumItemPointers
755 * and ginVacuumPostingTreeLeaf and as an opaque struct, so we need a forward
756 * declaration for it.
758 typedef struct GinVacuumState GinVacuumState;
760 extern void ginVacuumPostingTreeLeaf(Relation rel, Buffer buf, GinVacuumState *gvs);
765 * GinScanKeyData describes a single GIN index qualifier expression.
767 * From each qual expression, we extract one or more specific index search
768 * conditions, which are represented by GinScanEntryData. It's quite
769 * possible for identical search conditions to be requested by more than
770 * one qual expression, in which case we merge such conditions to have just
771 * one unique GinScanEntry --- this is particularly important for efficiency
772 * when dealing with full-index-scan entries. So there can be multiple
773 * GinScanKeyData.scanEntry pointers to the same GinScanEntryData.
775 * In each GinScanKeyData, nentries is the true number of entries, while
776 * nuserentries is the number that extractQueryFn returned (which is what
777 * we report to consistentFn). The "user" entries must come first.
779 typedef struct GinScanKeyData *GinScanKey;
781 typedef struct GinScanEntryData *GinScanEntry;
783 typedef struct GinScanKeyData
785 /* Real number of entries in scanEntry[] (always > 0) */
787 /* Number of entries that extractQueryFn and consistentFn know about */
790 /* array of GinScanEntry pointers, one per extracted search condition */
791 GinScanEntry *scanEntry;
794 * At least one of the entries in requiredEntries must be present for a
795 * tuple to match the overall qual.
797 * additionalEntries contains entries that are needed by the consistent
798 * function to decide if an item matches, but are not sufficient to
799 * satisfy the qual without entries from requiredEntries.
801 GinScanEntry *requiredEntries;
803 GinScanEntry *additionalEntries;
806 /* array of check flags, reported to consistentFn */
808 bool (*boolConsistentFn) (GinScanKey key);
809 GinTernaryValue (*triConsistentFn) (GinScanKey key);
810 FmgrInfo *consistentFmgrInfo;
811 FmgrInfo *triConsistentFmgrInfo;
814 /* other data needed for calling consistentFn */
816 /* NB: these three arrays have only nuserentries elements! */
818 GinNullCategory *queryCategories;
820 StrategyNumber strategy;
825 * Match status data. curItem is the TID most recently tested (could be a
826 * lossy-page pointer). curItemMatches is TRUE if it passes the
827 * consistentFn test; if so, recheckCurItem is the recheck flag.
828 * isFinished means that all the input entry streams are finished, so this
829 * key cannot succeed for any later TIDs.
831 ItemPointerData curItem;
837 typedef struct GinScanEntryData
839 /* query key and other information from extractQueryFn */
841 GinNullCategory queryCategory;
844 StrategyNumber strategy;
848 /* Current page in posting tree */
851 /* current ItemPointer to heap */
852 ItemPointerData curItem;
854 /* for a partial-match or full-scan query, we accumulate all TIDs here */
855 TIDBitmap *matchBitmap;
856 TBMIterator *matchIterator;
857 TBMIterateResult *matchResult;
859 /* used for Posting list and one page in Posting tree */
860 ItemPointerData *list;
866 uint32 predictNumberResult;
870 typedef struct GinScanOpaqueData
872 MemoryContext tempCtx;
875 GinScanKey keys; /* one per scan qualifier expr */
878 GinScanEntry *entries; /* one per index search condition */
880 uint32 allocentries; /* allocated length of entries[] */
882 bool isVoidRes; /* true if query is unsatisfiable */
885 typedef GinScanOpaqueData *GinScanOpaque;
887 extern Datum ginbeginscan(PG_FUNCTION_ARGS);
888 extern Datum ginendscan(PG_FUNCTION_ARGS);
889 extern Datum ginrescan(PG_FUNCTION_ARGS);
890 extern Datum ginmarkpos(PG_FUNCTION_ARGS);
891 extern Datum ginrestrpos(PG_FUNCTION_ARGS);
892 extern void ginNewScanKey(IndexScanDesc scan);
893 extern void ginFreeScanKeys(GinScanOpaque so);
896 extern Datum gingetbitmap(PG_FUNCTION_ARGS);
899 extern void ginInitConsistentFunction(GinState *ginstate, GinScanKey key);
902 extern Datum ginbulkdelete(PG_FUNCTION_ARGS);
903 extern Datum ginvacuumcleanup(PG_FUNCTION_ARGS);
904 extern ItemPointer ginVacuumItemPointers(GinVacuumState *gvs,
905 ItemPointerData *items, int nitem, int *nremaining);
908 typedef struct GinEntryAccumulator
912 GinNullCategory category;
915 ItemPointerData *list;
916 uint32 maxcount; /* allocated size of list[] */
917 uint32 count; /* current number of list[] entries */
918 } GinEntryAccumulator;
923 long allocatedMemory;
924 GinEntryAccumulator *entryallocator;
929 extern void ginInitBA(BuildAccumulator *accum);
930 extern void ginInsertBAEntries(BuildAccumulator *accum,
931 ItemPointer heapptr, OffsetNumber attnum,
932 Datum *entries, GinNullCategory *categories,
934 extern void ginBeginBAScan(BuildAccumulator *accum);
935 extern ItemPointerData *ginGetBAEntry(BuildAccumulator *accum,
936 OffsetNumber *attnum, Datum *key, GinNullCategory *category,
941 typedef struct GinTupleCollector
949 extern void ginHeapTupleFastInsert(GinState *ginstate,
950 GinTupleCollector *collector);
951 extern void ginHeapTupleFastCollect(GinState *ginstate,
952 GinTupleCollector *collector,
953 OffsetNumber attnum, Datum value, bool isNull,
954 ItemPointer ht_ctid);
955 extern void ginInsertCleanup(GinState *ginstate,
956 bool vac_delay, IndexBulkDeleteResult *stats);
958 /* ginpostinglist.c */
960 extern GinPostingList *ginCompressPostingList(const ItemPointer ptrs, int nptrs,
961 int maxsize, int *nwritten);
962 extern int ginPostingListDecodeAllSegmentsToTbm(GinPostingList *ptr, int totalsize, TIDBitmap *tbm);
964 extern ItemPointer ginPostingListDecodeAllSegments(GinPostingList *ptr, int len, int *ndecoded);
965 extern ItemPointer ginPostingListDecode(GinPostingList *ptr, int *ndecoded);
966 extern ItemPointer ginMergeItemPointers(ItemPointerData *a, uint32 na,
967 ItemPointerData *b, uint32 nb,
971 * Merging the results of several gin scans compares item pointers a lot,
972 * so we want this to be inlined. But if the compiler doesn't support that,
973 * fall back on the non-inline version from itemptr.c. See STATIC_IF_INLINE in
978 ginCompareItemPointers(ItemPointer a, ItemPointer b)
980 uint64 ia = (uint64) a->ip_blkid.bi_hi << 32 | (uint64) a->ip_blkid.bi_lo << 16 | a->ip_posid;
981 uint64 ib = (uint64) b->ip_blkid.bi_hi << 32 | (uint64) b->ip_blkid.bi_lo << 16 | b->ip_posid;
991 #define ginCompareItemPointers(a, b) ItemPointerCompare(a, b)
992 #endif /* PG_USE_INLINE */
994 #endif /* GIN_PRIVATE_H */