skip to content

GIN

an inverted index: one entry per key, a posting list of rows, and the pending list that makes writes cheap.

address
everything.index_internals.gin
part
part 13, inside indexes
chapter
chapter 8 of 12
reading time
8 min

GIN is an inverted index: one entry per distinct key (a tag, a word, a jsonb path), each with the list of rows that contain it. the keys sit in a b-tree, the entry tree. a key's rows are stored as a compressed posting list right in the entry, or, when there are too many, as a posting tree of their own. since one row can add dozens of keys, writes are expensive, so new rows first go to a pending list and get merged in bulk later.

the picture

you know what GIN serves: "contains" questions, @> on arrays and jsonb, @@ for text search. a b-tree can't answer those, because it has one entry per row, keyed on the whole value. GIN turns that inside out: one entry per element, pointing at every row that has it.

it's the index at the back of a cookbook. "garlic: 12, 40, 41, 97, 213". one line per ingredient, a list of pages each. a recipe with nine ingredients appears in nine lines. and when a new recipe arrives, updating nine lines all over the index is a chore, so the publisher sticks it on an "addenda" page first (the pending list) and re-sorts the index properly at the next printing.

here's a real one. 20,000 posts tagged sql, every tenth also postgres, every thousandth also gin:

sqlempty db · ext=pageinspect · session g
CREATE TABLE posts (id int, tags text[]);
INSERT INTO posts
SELECT g, ARRAY['sql'] || CASE WHEN g % 10 = 0 THEN ARRAY['postgres'] ELSE ARRAY[]::text[] END
          || CASE WHEN g % 1000 = 0 THEN ARRAY['gin'] ELSE ARRAY[]::text[] END
FROM generate_series(1, 20000) AS g;
CREATE INDEX posts_tags ON posts USING gin (tags);
SELECT * FROM gin_metapage_info(get_raw_page('posts_tags', 0));
SELECT b AS blkno, o.flags, o.rightlink, o.maxoff
FROM generate_series(1, pg_relation_size('posts_tags') / 8192 - 1) AS b,
     gin_page_opaque_info(get_raw_page('posts_tags', b)) AS o
ORDER BY b;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 20000
#3 · CREATE INDEX
#4 · SELECT 1
pending_headint8pending_tailint8tail_free_sizeint4n_pending_pagesint8n_pending_tuplesint8n_total_pagesint8n_entry_pagesint8n_data_pagesint8n_entriesint8versionint4
4294967295429496729500061432
(1 row)
#5 · SELECT 5
blknoint8flagstext[]rightlinkint8maxoffint4
1{leaf}42949672950
2{data}42949672953
3{data,leaf,compressed}50
4{data,leaf,compressed}30
5{data,leaf,compressed}42949672950
(5 rows)
  • page 0: metapage3 entries
  • ├── gin: 20 rows, inline
  • ├── postgres: 2,000 rows, inline
  • └──
  • └──
  • └── pages 4 → 3 → 5compressed leaves
drawn by hand · from gin_metapage_info and gin_page_opaque_info in the run block above
the index above, page by page. three keys in one entry page. two keys keep their rows inline as posting lists; sql, in every row, got a posting tree of its own: a root over three compressed leaves, linked left to right.

n_entries is 3: three distinct tags for 20,000 rows. the flags tell the page types apart. {leaf} without data is an entry-tree leaf: here the only entry page, so it's also the root. {data} is a posting tree's inner page, and {data,leaf,compressed} its leaves, chained by rightlink (4 → 3 → 5, same as a b-tree's sibling links). a key with few rows keeps its posting list inline in the entry tuple; once the list won't fit in a fraction of a page, it moves out into a posting tree, which is just a b-tree of heap addresses.

compressed posting lists

sqlempty db · ext=pageinspect · session g
continues session g (1 earlier block run first)
SELECT first_tid, nbytes, cardinality(tids) AS rows
FROM gin_leafpage_items(get_raw_page('posts_tags', 3))
ORDER BY first_tid LIMIT 5;
output, from postgres 18.6
SELECT 5
first_tidtidnbytesint2rowsint4
(66,64)248247
(68,75)248247
(70,86)248247
(72,97)248247
(74,108)248247
(5 rows)

each item on a posting tree leaf is a run of addresses: the first one, then the gaps between them, varbyte-encoded (small gaps take one byte). 247 row addresses in 248 bytes: about one byte per row. a b-tree's posting list spends 6 bytes on each (an 808-byte tuple: a 16-byte key and header, then 132 addresses).

GIN, compressed1 bytes
b-tree posting list6 bytes
b-tree, no posting list20 bytes
bytes per row address: a GIN posting tree leaf from the block above, and a b-tree posting list tuple from the deduplication chapter ((808 − 16) / 132).

(the last bar is a plain b-tree entry: a 16-byte tuple plus its 4-byte line pointer, for every row.) that's why GIN copes with keys that match half the table, like a common word in text search.

predict the output
a GIN index on a text[] column. you insert one row with 10 distinct tags. how many entries can that touch in the index?
sql
INSERT INTO posts VALUES (1, ARRAY['a','b','c','d','e','f','g','h','i','j']);

the pending list

with fastupdate on (the default), an INSERT doesn't touch the entry tree at all. it appends its keys to the pending list, a plain unsorted chain of pages hanging off the metapage. searches read the whole pending list on top of the tree. the list is merged into the tree in one sorted batch when it grows past gin_pending_list_limit (4 MB by default), when VACUUM or autovacuum runs, or when you call gin_clean_pending_list():

sqlempty db · ext=pageinspect · session g
continues session g (2 earlier blocks run first)
INSERT INTO posts SELECT g, ARRAY['sql', 'new'] FROM generate_series(20001, 20500) AS g;
SELECT pending_head, pending_tail, n_pending_pages, n_pending_tuples, n_entries
FROM gin_metapage_info(get_raw_page('posts_tags', 0));
SELECT gin_clean_pending_list('posts_tags') AS pages_cleaned;
SELECT pending_head, pending_tail, n_pending_pages, n_pending_tuples, n_entries
FROM gin_metapage_info(get_raw_page('posts_tags', 0));
output, from postgres 18.6
#1 · INSERT 0 500
#2 · SELECT 1
pending_headint8pending_tailint8n_pending_pagesint8n_pending_tuplesint8n_entriesint8
6835003
(1 row)
#3 · SELECT 1
pages_cleanedint8
3
(1 row)
#4 · SELECT 1
pending_headint8pending_tailint8n_pending_pagesint8n_pending_tuplesint8n_entriesint8
42949672954294967295003
(1 row)

500 new rows went to 3 pending pages (6 to 8). the cleanup merged them and emptied the list. merging in bulk is cheaper because the keys arrive sorted and each posting list is rewritten once for many rows, not once per row. here's the difference in WAL, the cleanest measure of work done, for 5,000 rows with 3 tags each:

sqlempty db · ext=pageinspect
CREATE TABLE fu (id int, tags text[]);
CREATE INDEX fu_on ON fu USING gin (tags);
CREATE TABLE fo (id int, tags text[]);
CREATE INDEX fo_off ON fo USING gin (tags) WITH (fastupdate = off);
SELECT 1 AS marked FROM set_config('my.a', pg_current_wal_insert_lsn()::text, false);
INSERT INTO fu SELECT g, ARRAY['t' || g % 50, 't' || g % 7, 'x'] FROM generate_series(1, 5000) AS g;
SELECT 1 AS marked FROM set_config('my.b', pg_current_wal_insert_lsn()::text, false);
INSERT INTO fo SELECT g, ARRAY['t' || g % 50, 't' || g % 7, 'x'] FROM generate_series(1, 5000) AS g;
SELECT 1 AS marked FROM set_config('my.c', pg_current_wal_insert_lsn()::text, false);
SELECT n_pending_pages, n_pending_tuples FROM gin_metapage_info(get_raw_page('fu_on', 0));
SELECT gin_clean_pending_list('fu_on') AS pages_cleaned;
SELECT 1 AS marked FROM set_config('my.d', pg_current_wal_insert_lsn()::text, false);
SELECT pg_size_pretty(pg_wal_lsn_diff(current_setting('my.b')::pg_lsn, current_setting('my.a')::pg_lsn)) AS on_inserts,
       pg_size_pretty(pg_wal_lsn_diff(current_setting('my.d')::pg_lsn, current_setting('my.c')::pg_lsn)) AS on_cleanup,
       pg_size_pretty(pg_wal_lsn_diff(current_setting('my.c')::pg_lsn, current_setting('my.b')::pg_lsn)) AS off_inserts;
SELECT pg_relation_size('fu_on') / 8192 AS pages_on, pg_relation_size('fo_off') / 8192 AS pages_off;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · CREATE INDEX
#3 · CREATE TABLE
#4 · CREATE INDEX
#5 · SELECT 1
markedint4
1
(1 row)
#6 · INSERT 0 5000
#7 · SELECT 1
markedint4
1
(1 row)
#8 · INSERT 0 5000
#9 · SELECT 1
markedint4
1
(1 row)
#10 · SELECT 1
n_pending_pagesint8n_pending_tuplesint8
375000
(1 row)
#11 · SELECT 1
pages_cleanedint8
37
(1 row)
#12 · SELECT 1
markedint4
1
(1 row)
#13 · SELECT 1
on_insertstexton_cleanuptextoff_insertstext
1450 kB62 kB7847 kB
(1 row)
#14 · SELECT 1
pages_onint8pages_offint8
436
(1 row)
fastupdate on: inserts1,450 kB
fastupdate on: merge62 kB
fastupdate off7,847 kB
WAL written by 5,000 inserts of three tags each into a GIN index, from the run block above. with fastupdate, the inserts and the later merge together; without it, each insert updates the tree itself.

about 1.5 MB against 7.8 MB: five times less WAL. (the index file kept the 37 pending pages after the merge, empty and marked for reuse, so pages_on is bigger. pending pages are recycled, not returned.)

why updates are slow anyway

the pending list moves the cost; it doesn't remove it:

  • the merge has to happen sometime. if it happens in a foreground insert (the list hit its limit), that one insert pays for everyone's, and its latency spikes. a smaller gin_pending_list_limit, or autovacuum keeping up, evens that out.
  • reads pay while the list is long. every search scans the whole pending list, unsorted, on top of the tree. a list near 4 MB makes every query slower.
  • an UPDATE is a delete plus an insert. GIN has no HOT shortcut for a changed array: every key of the new version goes in again (and HOT only applies when no indexed column changed at all).
  • deletes do nothing until VACUUM. dead rows stay in every posting list they're in, and VACUUM has to visit each one.

so a GIN index on a write-heavy table is a deliberate trade: much faster "contains" reads, for writes that cost several times a b-tree's.

one level deeperwhat else is in the box

GIN is a framework: an operator class says how to pull keys out of a value (extractValue), out of a query (extractQuery), and whether a row with some of the keys matches (consistent). array_ops, jsonb_ops, jsonb_path_ops (one hashed key per path, smaller, @> only) and tsvector_ops are built in; pg_trgm adds trigrams, and btree_gin plain scalars. a search for several keys reads each key's posting list and intersects or unions them, without visiting the heap until the end. the result is a bitmap, so GIN only ever feeds a bitmap heap scan, never a plain index scan.

keylines

  • GIN keeps one entry per distinct key in a b-tree, each with the rows that contain it.
  • short row lists are compressed posting lists inline; long ones become posting trees. about a byte per row.
  • one row can add many keys, so inserts are expensive.
  • fastupdate puts new rows in a pending list and merges them in bulk: five times less WAL here.
  • the merge still has to happen, searches read the pending list, and VACUUM cleans the dead.