diff --git a/doc/src/sgml/ref/create_index.sgml b/doc/src/sgml/ref/create_index.sgml new file mode 100644 index ce36a1b..9a7b5af *** a/doc/src/sgml/ref/create_index.sgml --- b/doc/src/sgml/ref/create_index.sgml *************** CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] *** 294,301 **** The optional WITH clause specifies storage parameters for the index. Each index method has its own set of allowed ! storage parameters. The B-tree, hash, GiST and SP-GiST index methods all ! accept this parameter: --- 294,301 ---- The optional WITH clause specifies storage parameters for the index. Each index method has its own set of allowed ! storage parameters. The B-tree, hash, GIN, GiST and SP-GiST index methods ! all accept this parameter: diff --git a/src/backend/access/common/reloptions.c b/src/backend/access/common/reloptions.c new file mode 100644 index 8176b6a..443b125 *** a/src/backend/access/common/reloptions.c --- b/src/backend/access/common/reloptions.c *************** *** 15,20 **** --- 15,21 ---- #include "postgres.h" + #include "access/gin_private.h" #include "access/gist_private.h" #include "access/hash.h" #include "access/htup_details.h" *************** static relopt_int intRelOpts[] = *** 133,138 **** --- 134,147 ---- }, { { + "fillfactor", + "Packs gin index pages only to this percentage", + RELOPT_KIND_GIN + }, + GIN_DEFAULT_FILLFACTOR, GIN_MIN_FILLFACTOR, 100 + }, + { + { "autovacuum_vacuum_threshold", "Minimum number of tuple updates or deletes prior to vacuum", RELOPT_KIND_HEAP | RELOPT_KIND_TOAST diff --git a/src/backend/access/gin/gindatapage.c b/src/backend/access/gin/gindatapage.c new file mode 100644 index ec8c94b..35c3761 *** a/src/backend/access/gin/gindatapage.c --- b/src/backend/access/gin/gindatapage.c *************** typedef struct *** 55,60 **** --- 55,62 ---- dlist_node *lastleft; /* last segment on left page */ int lsize; /* total size on left page */ int rsize; /* total size on right page */ + int maxdatasize; /* max data size per page + according to fillfactor */ bool oldformat; /* page is in pre-9.4 format on disk */ } disassembledLeaf; *************** GinPageDeletePostingItem(Page page, Offs *** 423,428 **** --- 425,461 ---- } /* + * Returns max data size on the page according to index fillfactor. + */ + int + GinGetMaxDataSize(Relation index, bool isBuild) + { + int fillfactor, result; + + /* Grab option value which affects only index build */ + if (!isBuild) + { + fillfactor = 100; + } + else if (index->rd_options) + { + GinOptions *options = (GinOptions *) index->rd_options; + fillfactor = options->fillfactor; + } + else + { + fillfactor = GIN_DEFAULT_FILLFACTOR; + } + + /* Caclculate max data size on page according to fillfactor */ + result = GinDataPageMaxDataSize - BLCKSZ * (100 - fillfactor) / 100; + /* Ensure that at least one posting list segment fits the page */ + result = Max(result, GinPostingListSegmentMaxSize); + + return result; + } + + /* * Places keys to leaf data page and fills WAL record. */ static GinPlaceToPageRC *************** dataPlaceToPageLeaf(GinBtree btree, Buff *** 486,491 **** --- 519,526 ---- leaf = disassembleLeaf(page); + leaf->maxdatasize = GinGetMaxDataSize(btree->index, btree->isBuild); + /* * Are we appending to the end of the page? IOW, are all the new items * larger than any of the existing items. *************** dataPlaceToPageLeaf(GinBtree btree, Buff *** 511,525 **** /* * If we're appending to the end of the page, we will append as many items ! * as we can fit (after splitting), and stop when the pages becomes full. ! * Otherwise we have to limit the number of new items to insert, because ! * once we start packing we can't just stop when we run out of space, ! * because we must make sure that all the old items still fit. */ if (GinPageIsCompressed(page)) freespace = GinDataLeafPageGetFreeSpace(page); else freespace = 0; if (append) { /* --- 546,571 ---- /* * If we're appending to the end of the page, we will append as many items ! * as we can fit up to the given fillfactor at build (after splitting), ! * and stop when the pages becomes full at this rate. Otherwise we have to ! * limit the number of new items to insert, because once we start packing ! * we can't just stop when we run out of space, because we must make sure ! * that all the old items still fit. */ if (GinPageIsCompressed(page)) + { freespace = GinDataLeafPageGetFreeSpace(page); + if (btree->isBuild) + { + freespace -= GinDataPageMaxDataSize - leaf->maxdatasize; + freespace = Max(0, freespace); + } + } else + { freespace = 0; + } + if (append) { /* *************** dataPlaceToPageLeaf(GinBtree btree, Buff *** 529,535 **** * a single byte, and we can use all the free space on the old page as * well as the new page. For simplicity, ignore segment overhead etc. */ ! maxitems = Min(maxitems, freespace + GinDataPageMaxDataSize); } else { --- 575,581 ---- * a single byte, and we can use all the free space on the old page as * well as the new page. For simplicity, ignore segment overhead etc. */ ! maxitems = Min(maxitems, freespace + leaf->maxdatasize); } else { *************** dataPlaceToPageLeaf(GinBtree btree, Buff *** 544,550 **** int nnewsegments; nnewsegments = freespace / GinPostingListSegmentMaxSize; ! nnewsegments += GinDataPageMaxDataSize / GinPostingListSegmentMaxSize; maxitems = Min(maxitems, nnewsegments * MinTuplesPerSegment); } --- 590,596 ---- int nnewsegments; nnewsegments = freespace / GinPostingListSegmentMaxSize; ! nnewsegments += leaf->maxdatasize / GinPostingListSegmentMaxSize; maxitems = Min(maxitems, nnewsegments * MinTuplesPerSegment); } *************** dataPlaceToPageLeaf(GinBtree btree, Buff *** 631,643 **** * until they're balanced. * * As a further heuristic, when appending items to the end of the ! * page, try make the left page 75% full, one the assumption that ! * subsequent insertions will probably also go to the end. This packs ! * the index somewhat tighter when appending to a table, which is very ! * common. */ if (!btree->isBuild) { while (dlist_has_prev(&leaf->segments, leaf->lastleft)) { lastleftinfo = dlist_container(leafSegmentInfo, node, leaf->lastleft); --- 677,692 ---- * until they're balanced. * * As a further heuristic, when appending items to the end of the ! * page, try make the left page fillfactor% full, on the assumption ! * that subsequent insertions will probably also go to the end. This ! * packs the posting tree as tight as during index build when appending ! * to a table, which is very common. */ if (!btree->isBuild) { + /* Calculate max data size like it's an index build */ + int lmaxsize = GinGetMaxDataSize(btree->index, true); + while (dlist_has_prev(&leaf->segments, leaf->lastleft)) { lastleftinfo = dlist_container(leafSegmentInfo, node, leaf->lastleft); *************** dataPlaceToPageLeaf(GinBtree btree, Buff *** 657,663 **** break; if (append) { ! if ((leaf->lsize - segsize) < (BLCKSZ * 3) / 4) break; } --- 706,712 ---- break; if (append) { ! if ((leaf->lsize - segsize) < lmaxsize) break; } *************** dataPlaceToPageLeaf(GinBtree btree, Buff *** 667,672 **** --- 716,722 ---- leaf->lastleft = dlist_prev_node(&leaf->segments, leaf->lastleft); } } + Assert(leaf->lsize <= GinDataPageMaxDataSize); Assert(leaf->rsize <= GinDataPageMaxDataSize); *************** disassembleLeaf(Page page) *** 1284,1289 **** --- 1334,1340 ---- Pointer segend; leaf = palloc0(sizeof(disassembledLeaf)); + leaf->maxdatasize = GinDataPageMaxDataSize; dlist_init(&leaf->segments); if (GinPageIsCompressed(page)) *************** leafRepackItems(disassembledLeaf *leaf, *** 1595,1601 **** * copying to the page. Did we exceed the size that fits on one page? */ segsize = SizeOfGinPostingList(seginfo->seg); ! if (pgused + segsize > GinDataPageMaxDataSize) { if (!needsplit) { --- 1646,1652 ---- * copying to the page. Did we exceed the size that fits on one page? */ segsize = SizeOfGinPostingList(seginfo->seg); ! if (pgused + segsize > leaf->maxdatasize) { if (!needsplit) { *************** createPostingTree(Relation index, ItemPo *** 1687,1692 **** --- 1738,1744 ---- Pointer ptr; int nrootitems; int rootsize; + int maxdatasize; /* Construct the new root page in memory first. */ tmppage = (Page) palloc(BLCKSZ); *************** createPostingTree(Relation index, ItemPo *** 1699,1704 **** --- 1751,1757 ---- */ nrootitems = 0; rootsize = 0; + maxdatasize = GinGetMaxDataSize(index, buildStats ? true: false); ptr = (Pointer) GinDataLeafPageGetPostingList(tmppage); while (nrootitems < nitems) { *************** createPostingTree(Relation index, ItemPo *** 1711,1717 **** GinPostingListSegmentMaxSize, &npacked); segsize = SizeOfGinPostingList(segment); ! if (rootsize + segsize > GinDataPageMaxDataSize) break; memcpy(ptr, segment, segsize); --- 1764,1770 ---- GinPostingListSegmentMaxSize, &npacked); segsize = SizeOfGinPostingList(segment); ! if (rootsize + segsize > maxdatasize) break; memcpy(ptr, segment, segsize); diff --git a/src/backend/access/gin/ginutil.c b/src/backend/access/gin/ginutil.c new file mode 100644 index cb4e32f..7403366 *** a/src/backend/access/gin/ginutil.c --- b/src/backend/access/gin/ginutil.c *************** ginoptions(PG_FUNCTION_ARGS) *** 527,533 **** static const relopt_parse_elt tab[] = { {"fastupdate", RELOPT_TYPE_BOOL, offsetof(GinOptions, useFastUpdate)}, {"gin_pending_list_limit", RELOPT_TYPE_INT, offsetof(GinOptions, ! pendingListCleanupSize)} }; options = parseRelOptions(reloptions, validate, RELOPT_KIND_GIN, --- 527,534 ---- static const relopt_parse_elt tab[] = { {"fastupdate", RELOPT_TYPE_BOOL, offsetof(GinOptions, useFastUpdate)}, {"gin_pending_list_limit", RELOPT_TYPE_INT, offsetof(GinOptions, ! pendingListCleanupSize)}, ! {"fillfactor", RELOPT_TYPE_INT, offsetof(GinOptions, fillfactor)} }; options = parseRelOptions(reloptions, validate, RELOPT_KIND_GIN, diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h new file mode 100644 index 5f214d7..586389d *** a/src/include/access/gin_private.h --- b/src/include/access/gin_private.h *************** typedef struct GinOptions *** 323,328 **** --- 323,329 ---- int32 vl_len_; /* varlena header (do not touch directly!) */ bool useFastUpdate; /* use fast updates? */ int pendingListCleanupSize; /* maximum size of pending list */ + int fillfactor; /* page fillfactor in percent (20..100) */ } GinOptions; #define GIN_DEFAULT_USE_FASTUPDATE true *************** typedef struct GinOptions *** 335,340 **** --- 336,343 ---- ((GinOptions *) (relation)->rd_options)->pendingListCleanupSize : \ gin_pending_list_limit) + #define GIN_MIN_FILLFACTOR 20 + #define GIN_DEFAULT_FILLFACTOR 90 /* Macros for buffer lock/unlock operations */ #define GIN_UNLOCK BUFFER_LOCK_UNLOCK *************** extern BlockNumber createPostingTree(Rel *** 724,729 **** --- 727,733 ---- GinStatsData *buildStats); extern void GinDataPageAddPostingItem(Page page, PostingItem *data, OffsetNumber offset); extern void GinPageDeletePostingItem(Page page, OffsetNumber offset); + extern int GinGetMaxDataSize(Relation index, bool isBuild); extern void ginInsertItemPointers(Relation index, BlockNumber rootBlkno, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats);