GIN: JSON, Arrays and Full-Text Postings

CREATE INDEX ... USING gin creates one of two kinds of entry in the engine-wide SecondaryIndexRegistry, depending on the indexed column. Both are inverted indexes held in memory and rebuilt from a table scan; neither is persisted. A separate GIN structure in src/index/gin.rs is not used by SQL. See Overview for index choice and Full-text search for the text functions.

Which GIN you get

Indexed key Catalog key Serves
A tsvector column __catalog__:ftsidx:{table}:{name} = col\x1f col @@ query
to_tsvector('cfg', col) __catalog__:ftsidx:{table}:{name} = col\x1fcfg:{config} to_tsvector('cfg', col) @@ query
Any other plain column (JSON, jsonb, arrays) __catalog__:ginidx:{table}:{name} @>, ?, ?\|, ?&

(source: src/query/engine.rs, execute_create_index) An operator class such as jsonb_ops is accepted and the default behaviour applies. (source: src/query/engine.rs, execute_create_index)

JSON and array GIN

Terms

gin_extract_value produces path-free terms, in the style of PostgreSQL's jsonb_ops:

Term Meaning
kx\x1fkey Object has top-level key key; also emitted for each string element of an array, so ? works on arrays
kv\x1fkey\x1fenc Object has key key with encoded scalar value enc
ae\x1fenc Array contains encoded element enc

(source: src/query/engine.rs, gin_extract_value)

Terms are stored in the entry's gin_terms map (term to row keys). Rows whose value cannot be decomposed go to gin_always and are always candidates. (source: src/storage/mod.rs, SecondaryIndexEntry)

Lookup

try_gin_candidates looks for a top-level AND conjunct of the form col @> const, col ? 'k', col ?| array or col ?& array. Containment and ?& intersect the posting lists of their terms; ? and ?| take the union. The candidates are refetched and the original predicate is re-evaluated, so a term set that is too broad only costs time. (source: src/query/engine.rs, try_gin_candidates) The path is skipped inside a transaction. (source: src/query/engine.rs, try_gin_candidates)

Not verified: no test was found that compares a JSON or array GIN lookup with a full scan or checks that the GIN path is taken. The unit test test_gin_operator_extraction belongs to the unwired PhysicalPlanner, not to this path. (source: src/plan/physical_planner.rs, test_gin_operator_extraction)

Full-text GIN

Postings live in the same gin_terms map under ts\x1f{source}\x1f{lexeme}, where source is empty for a tsvector column or cfg:{config} for the expression form. fts_sources records each row's source text and fts_always holds rows that could not be indexed. (source: src/storage/mod.rs, SecondaryIndexEntry; src/query/engine.rs, FTS entry rebuild)

try_fts_candidates serves @@ when the left side matches the indexed column or expression:

tsquery node Candidate set
Lexeme Its posting list
lex:* prefix Union of postings for lexemes starting with lex
a & b, a <-> b Intersection
a \| b Union
!a No restriction (all rows)

(source: src/query/engine.rs, try_fts_candidates) Phrase distance is checked only by the re-run @@; the index treats <-> as &. (source: src/query/engine.rs, try_fts_candidates)

Tests: fts_index_matches_brute_force_exactly, fts_index_maintained_on_insert_update_delete, fts_accelerated_path_is_actually_used, fts_index_rebuilt_after_restart, fts_index_speedup_report, test_fts_gin_index_accepted. (source: src/query/engine.rs, those tests)

Maintenance and concurrency

GIN entries share the secondary-index lifecycle: a full rebuild on first use or when dirty, incremental adds on autocommit INSERT, and dirty marking on UPDATE, DELETE, upsert, INSERT ... SELECT and commit. Each entry has its own mutex, and the GIN and full-text paths are not used inside a transaction. (source: src/query/engine.rs, rebuild_secondary_entry_locked, try_gin_candidates, try_fts_candidates) The details are on B-tree and hash secondary indexes.

Unwired: src/index/gin.rs

GinIndex keys postings by GinKey (JsonPathValue, JsonPathExists, ArrayElement, Token) with HashSet posting lists and a GinIndexType. (source: src/index/gin.rs, GinIndex, GinKey) Tests: test_gin_jsonb_insert_search, test_gin_array_insert_search, test_gin_json_exists, test_gin_delete, test_gin_fulltext. (source: src/index/gin.rs, tests)

Not wired: it is only created by IndexManager::create_gin_index (never called) and looked up by the test-only PhysicalPlanner. (source: src/index/mod.rs, create_gin_index; src/plan/physical_planner.rs, get_gin_index)