Review
[WiP] GiST intrapage indexing (v3)
- Proposal version
- 3
- Base commit
- 412848dac37f2e4455e1505f83f0af0f3d28a67d
v3 applies cleanly onto 412848dac37f2e4455e1505f83f0af0f3d28a67d. gist.c, gistbuild.c, gistget.c, gistutil.c, and gistvacuum.c compile with the existing debug warning flags and this tree's headers, with no warnings. The new src/test/modules/gist TAP test was not run here. cfbot's 64-bit Meson, Windows, and CompilerWarnings jobs passed; Autoconf, 32-bit Meson, and macOS failed, and those logs are not public. Andrey Borodin still calls this WiP: GIST_SKIP_GROUP_SIZE is a hardcoded 16, only point_ops was measured, and the question he asked is whether the representation is acceptable. There are no other replies.
Representation. A skip tuple is an ordinary IndexTuple on an internal page. GistTupleIsSkip is true when t_tid has InvalidBlockNumber and ip_posid is a valid offset; GistTupleSetSkip stores the count of following real downlinks there; GistTupleGetSkipCount reads it. Groups are contiguous and not nested. That is distinct from the pre-9.1 invalid downlink, which is offset 0xfffe (TUPLE_IS_INVALID) on a real child block, and from TUPLE_IS_VALID (0xffff). Both of those offsets fail OffsetNumberIsValid, because MaxOffsetNumber is BLCKSZ/sizeof(ItemIdData). A leaf heap TID cannot be InvalidBlockNumber either. Pages with no skip tuples keep today's behavior, so an existing GiST index, including one brought up by pg_upgrade, does not need a rewrite. I would keep this marker. It does not use INDEX_AM_RESERVED_BIT, and deleting every skip tuple leaves the same downlinks.
WAL. There is no new record type and XLOG_PAGE_MAGIC stays 0xD122. A group update in gistplacetopage deletes the skip tuple and its members with PageIndexMultiDelete and appends the rebuilt group with gistfillbuffer, then logs that through gistXLogUpdate. gistRedoPageUpdateRecord already redoes a multi-delete plus append, so primary and standby match. The call to XLogEnsureRecordSpace before the critical section uses 2 + nwritetup, which is the header chunk, the offset array, and one chunk per inserted tuple. Splits still go through gistXLogSplit of whole pages. gistvacuumungroup writes its own gistXLogUpdate before gistdeletepage removes the downlink. A crash between those records leaves the downlink outside the group and keeps the old union key on what remains. That key is a union of a superset, so Consistent can only match more often. Readers and writers still hold the usual shared or exclusive lock across the whole page, and NSN / F_FOLLOW_RIGHT are unchanged.
Search. gistScanPage calls gistindex_keytest on the skip tuple and, if that returns false, adds the skip count to the offset so the for-loop steps to the first tuple after the group. That is the same rule as a parent downlink: a union that is not consistent cannot hide a consistent member. A true result does not queue the skip tuple and does not use its distance; the following real downlinks are tested normally, including for an ordered scan. With no scan keys, Consistent succeeds and nothing is pruned, so pure KNN does not get faster. Pre-9.1 invalid tuples still take the always-match path in gistindex_keytest, and they are not skip tuples. This is the right shape for avoiding false misses. The TAP test checks one point <@ box predicate against a sequential scan across sorted build, buffered build, insert, VACUUM, standby replay, and crash recovery. That does not cover ORDER BY, a second opclass, NULLs, or whether the stored union actually contains the group.
Penalty. gistchoose does not have a matching contract. Once best_penalty[0] is at least 0, a skip tuple whose first-column gistpenalty is strictly worse causes the whole group to be skipped. The comment calls that penalty a lower bound on the members. It is not, for the penalty GiST actually ships. box_penalty is the increase in area. Member box (0,0)-(1,100) has area 100. A union that contains it, (0,0)-(100,100), has area 10000. New key (0,100)-(1,101) grows the member to area 101 (penalty 1) and the union to area 10100 (penalty 100). If some other downlink already has penalty 50, the better child is never scored. The same test drops equal-penalty alternatives whenever the union looks worse, which cuts out the random tie-break later in gistchoose. gistpenalty clamps negatives and NaNs to 0, so a best of 0 still prunes every group with a positive union penalty. Picking the wrong child does not drop rows: the chosen downlink is unioned on the way down, and scans use Consistent. It does change the tree. The reported 1.1x to 1.4x gains are uniform points in shared_buffers, where this mistake is rare. Nothing here measures an opclass whose Consistent is expensive, or a workload of wide unions over thin children.
On a well-formed page gistchoose still returns a real downlink. best_penalty[0] starts at -1, so the first group cannot be skipped, and the first real tuple replaces the initial FirstOffsetNumber result. gistdoinsert and gistProcessItup can keep taking ItemPointerGetBlockNumber of that result. An Assert(!GistTupleIsSkip) at those two sites would lock that in. gistFindCorrectParent and gistBufferingFindCorrectParent do not special-case skip tuples; a skip TID does not equal the child block, so the rescan falls through. gistFindPath, gistformdownlink, gistGetMaxLevel, and gistMemorizeAllDownlinks all skip them explicitly.
One-downlink split. Every internal split calls gistSplitPageWithSkipGroups with force_split true, after skip tuples have been stripped. If the page has one real downlink, len is 1. That arm Assert(!force_split) and otherwise returns a one-page layout, instead of gistSplit's ERRCODE_PROGRAM_LIMIT_EXCEEDED for a single tuple that cannot be split. It is reachable when that only downlink is replaced by a key that does not fit in the free space left beside the old tuple and its skip metadata, including when the new tuple alone would fit in GiSTPageSize. A point key will not do this. A large btree_gist or tsvector key can. In a cassert build the assertion fires before the page is modified. In a non-assert build, if the tuple fits on the replacement page, gistplacetopage still takes the split return, WAL-logs a one-page gistXLogSplit, and does not set F_FOLLOW_RIGHT. gistfinishsplit then does lsecond() on a one-element list. list_nth_cell only Asserts that the index is in range, so without assertions that reads off the end of the list and the parent update uses whatever it finds. If the new tuple does not fit at all, PageAddItem errors before the critical section, with the generic "failed to add item" message rather than the program-limit error.
The rest of the maintenance path is in better shape. Sorted and buffered builds both pack internal levels with gistSplitPageWithSkipGroups, and a root split adds skip groups only to the new internal root, not to leaf halves. An in-place replace rebuilds only the group that contains oldoffnum; if the metadata does not fit, the page splits and every skip tuple is stripped first. A downlink that was never in a group stays ungrouped until some later split. VACUUM moves the victim out of its group before PageIndexTupleDelete and will not delete the last real downlink. Leaf pages do not grow skip tuples, so LP_DEAD and tuple-level vacuum are unchanged. pgstattuple already ignores internal GiST pages. pageinspect will show these TIDs as (4294967295, count); the test keys off that text.
Thread. The 2018 message and this v3 note are the whole thread. v3 answers the old TODOs about buffered build and about not queueing skip tuples for ordered scans. It does not make Penalty sound, and the author did not ask for a code review yet. Nobody else has agreed or objected.
Score. Assessment 40. The nearest anchor is 50: this is a real patch, the on-disk marker is compatible with old pages and with pre-9.1 invalid tuples, and Consistent-based pruning does not introduce false misses. Minus 10 because it is still WiP, the insertion prune is not a lower bound for box_penalty, and the one-real-downlink internal update does not reuse gistSplit's error path. Not 30, because WAL replay, vacuum's two-step unlink, and the build paths are coherent. Consensus 10. The nearest anchor is 0: no review reply. Plus 10 because the only mail is the author's, he labels it WiP, and there is no objection to the representation. Not 30, because nobody else has spoken.