GIN and RUM indexes make searches faster, but maintaining them comes at a cost. This post explores write amplification and how PostgreSQL maintains the two index types differently.

This is the third and final post in a three-part series on PostgreSQL inverted indexes:

  1. Three Typical GIN Index Use Cases in PostgreSQL
  2. How RUM Overcomes GIN’s Limitations in PostgreSQL Full-Text Search
  3. The Cost of Maintaining GIN and RUM Indexes in PostgreSQL

Write Amplification and GIN’s Pending List

To illustrate write amplification in GIN and RUM indexes, let’s revisit the GIN diagram from the first post. It shows index entries for two rows: Rivers carry boats at TID (0,1) and Boats cross rivers at TID (0,2). Each TID identifies a heap tuple by its block number and position within that block.

Now insert a third sentence, Boats navigate rivers, with TID (0,3). It contains three lexemes: boats, navigate, and rivers. The existing posting lists for boats and rivers each gain the new TID, while navigate becomes a new index entry with a posting list containing only (0,3). After these changes reach the main index, its entries look like this:

One heap insertion therefore adds three key-to-TID associations: two under existing keys and one under a new key. A document with 100 distinct lexemes requires 100 such associations; repeated occurrences of a lexeme do not create additional GIN keys for that row.

This fan-out from one heap tuple to many index associations explains the source of write amplification in both index types.

To reduce write amplification, GIN uses a pending list, which acts as a buffer for new index entries. This mechanism is called fast update and is enabled by default through the fastupdate setting. New entries go into the pending list first. When its size exceeds gin_pending_list_limit—4 MB by default—an insertion triggers cleanup. Cleanup can also run during vacuuming, including autovacuum, or through an explicit call to gin_clean_pending_list(). PostgreSQL sorts the pending entries and merges them into the main index in batches. This can reduce scattered page updates and improve insertion throughput, but adds the work of writing and later reading pending entries.

To illustrate the pending list, start again with the original two-row index and insert another row containing Boats navigate rivers, with a new TID (0,3). With fast update enabled, its three key-to-TID associations enter the pending list first. The main index remains unchanged until cleanup merges those entries. The diagram shows the extracted index entries stored in the pending list.

After cleanup, the pending list is empty, and the main index matches the second diagram above.

RUM has no pending list and updates its main index structures directly. This is a major difference in how the two index types handle writes in PostgreSQL.

How GIN’s Pending List Affects Searches

With fastupdate = on, GIN buffers new entries in a pending list before merging them into its main index. Searches must examine both structures, so pending entries can add work even when a query returns no rows. RUM updates its main index directly and has no pending-list scan.

The following transcript compares the same unsuccessful GIN lookup before and after explicit cleanup.

First, insert 100,000 rows and search for the lexeme 1:

postgres=# INSERT INTO films (title, release_date, category, actors_names)
SELECT md5(random()::text), '2020-01-01', 'test', md5(random()::text)
FROM generate_series(1, 100000);
INSERT 0 100000
postgres=# EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
SELECT count(*) FROM films
WHERE tsv @@ to_tsquery('simple', '1');
QUERY PLAN
-------------------------------------------------------------------------------------------------
Aggregate (actual time=0.107..0.107 rows=1.00 loops=1)
Buffers: shared hit=14
-> Bitmap Heap Scan on films (actual time=0.105..0.105 rows=0.00 loops=1)
Recheck Cond: (tsv @@ '''1'''::tsquery)
Buffers: shared hit=14
-> Bitmap Index Scan on films_tsv_gin_idx (actual time=0.100..0.100 rows=0.00 loops=1)
Index Cond: (tsv @@ '''1'''::tsquery)
Index Searches: 1
Buffers: shared hit=14
Planning:
Buffers: shared hit=3
Planning Time: 0.117 ms
Execution Time: 0.129 ms
(13 rows)

The query finds no matching rows, but its bitmap index scan reports 14 shared buffer hits. Those accesses include the work of checking the pending list.

Next, merge the pending entries into the main index:

postgres=# SELECT gin_clean_pending_list('films_tsv_gin_idx');
gin_clean_pending_list
------------------------
10
(1 row)

gin_clean_pending_list() returns the number of pending-list pages removed after their entries were merged. Here it returns 10.

Now repeat the same query:

postgres=# EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
SELECT count(*) FROM films
WHERE tsv @@ to_tsquery('simple', '1');
QUERY PLAN
-------------------------------------------------------------------------------------------------
Aggregate (actual time=0.020..0.021 rows=1.00 loops=1)
Buffers: shared hit=4
-> Bitmap Heap Scan on films (actual time=0.018..0.019 rows=0.00 loops=1)
Recheck Cond: (tsv @@ '''1'''::tsquery)
Buffers: shared hit=4
-> Bitmap Index Scan on films_tsv_gin_idx (actual time=0.013..0.013 rows=0.00 loops=1)
Index Cond: (tsv @@ '''1'''::tsquery)
Index Searches: 1
Buffers: shared hit=4
Planning:
Buffers: shared hit=1
Planning Time: 0.102 ms
Execution Time: 0.046 ms
(13 rows)

After cleanup, the bitmap index scan reports 4 shared buffer hits, down from 14. The reduction of 10 is consistent with eliminating the pending-list scan. Execution time also falls from 0.129 ms to 0.046 ms in this run.

Pending entries can also be merged during VACUUM, including autovacuum, or when an insertion triggers cleanup after the list exceeds gin_pending_list_limit.

RUM avoids this particular search overhead because it has no pending list, but it also cannot use GIN’s pending-list batching mechanism.

How Dead Tuple References Affect GIN and RUM Searches

DELETE marks heap tuple versions as deleted; it does not immediately remove their TIDs from GIN or RUM. Both indexes can return candidate TIDs that heap access rejects as invisible under MVCC. Updates that require new index entries can leave similar obsolete references. RUM’s lack of a pending list does not remove this maintenance requirement.

This experiment compares both indexes on the same table, before and after a single VACUUM. It starts from a fresh 100,000-row data set, independently of the pending-list experiment above. First, check the available indexes:

postgres=# SELECT
indexname,
indexdef
FROM
pg_indexes
WHERE
schemaname = 'public'
AND tablename = 'films';
indexname | indexdef
-------------------+-----------------------------------------------------------------
films_pkey | CREATE UNIQUE INDEX films_pkey ON public.films USING btree (id)
films_tsv_gin_idx | CREATE INDEX films_tsv_gin_idx ON public.films USING gin (tsv)
films_tsv_rum_idx | CREATE INDEX films_tsv_rum_idx ON public.films USING rum (tsv)
(3 rows)

Both full-text indexes exist before deletion. Delete rows with even IDs and inspect the table statistics:

postgres=# SELECT n_live_tup, n_dead_tup FROM pg_stat_user_tables WHERE relname = 'films';
n_live_tup | n_dead_tup
------------+------------
100000 | 0
(1 row)
postgres=# DELETE FROM films WHERE id % 2 = 0;
DELETE 50000
postgres=# SELECT n_live_tup, n_dead_tup FROM pg_stat_user_tables WHERE relname = 'films';
n_live_tup | n_dead_tup
------------+------------
50000 | 50000
(1 row)

The statistics report 50,000 live tuples and 50,000 dead tuples after deletion.

To examine each index separately, the experiment temporarily drops the competing index inside a transaction. Each ROLLBACK restores the dropped index before the next test. This lets both indexes be tested against the same deleted and surviving rows without rebuilding either index. The execution plans confirm which index PostgreSQL uses.

Before VACUUM: compare candidate TIDs with visible matches

First, temporarily remove RUM and run the search through GIN:

postgres=# begin;
BEGIN
postgres=*# drop index films_tsv_rum_idx;
DROP INDEX
postgres=*# EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
SELECT count(*) FROM films
WHERE tsv @@ to_tsquery('simple', 'drama');
QUERY PLAN
-----------------------------------------------------------------------------------------------------
Aggregate (actual time=16.282..16.291 rows=1.00 loops=1)
Buffers: shared hit=2717
-> Bitmap Heap Scan on films (actual time=3.447..15.343 rows=10034.00 loops=1)
Recheck Cond: (tsv @@ '''drama'''::tsquery)
Heap Blocks: exact=2709
Buffers: shared hit=2717
-> Bitmap Index Scan on films_tsv_gin_idx (actual time=2.987..2.988 rows=20114.00 loops=1)
Index Cond: (tsv @@ '''drama'''::tsquery)
Index Searches: 1
Buffers: shared hit=8
Planning:
Buffers: shared hit=33
Planning Time: 0.451 ms
Execution Time: 16.321 ms
(14 rows)
postgres=*# rollback;
ROLLBACK

The GIN bitmap index scan returns 20,114 candidate TIDs, but the heap scan returns only 10,034 visible matches, roughly half the candidate count. This is consistent with deleting half the table’s rows.

Next, temporarily remove GIN and run the same search through RUM:

postgres=# begin;
BEGIN
postgres=*# drop index films_tsv_gin_idx;
DROP INDEX
postgres=*# EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
SELECT count(*) FROM films
WHERE tsv @@ to_tsquery('simple', 'drama');
QUERY PLAN
-----------------------------------------------------------------------------------------------------
Aggregate (actual time=11.427..11.428 rows=1.00 loops=1)
Buffers: shared hit=2725
-> Bitmap Heap Scan on films (actual time=5.698..10.408 rows=10034.00 loops=1)
Recheck Cond: (tsv @@ '''drama'''::tsquery)
Heap Blocks: exact=2709
Buffers: shared hit=2725
-> Bitmap Index Scan on films_tsv_rum_idx (actual time=5.241..5.241 rows=20114.00 loops=1)
Index Cond: (tsv @@ '''drama'''::tsquery)
Index Searches: 0
Buffers: shared hit=16
Planning:
Buffers: shared hit=25
Planning Time: 0.384 ms
Execution Time: 11.646 ms
(14 rows)
postgres=*# rollback;
ROLLBACK

RUM also returns 20,114 candidate TIDs and 10,034 visible matches. Both indexes therefore expose the same obsolete matching references in this experiment.

Vacuum the table and both indexes

Both temporary index drops have been rolled back, so both indexes are present when VACUUM runs.

postgres=# VACUUM (VERBOSE, ANALYZE) films;
INFO: vacuuming "postgres.public.films"
...
index scan needed: 2710 pages from table (100.00% of total) had 50000 dead item identifiers removed
...
index "films_tsv_gin_idx": pages: 4329 in total, 0 newly deleted, 0 currently deleted, 0 reusable
index "films_tsv_rum_idx": pages: 3979 in total, 0 newly deleted, 0 currently deleted, 0 reusable
...
INFO: "films": scanned 2710 of 2710 pages, containing 50000 live rows and 0 dead rows; 30000 rows in sample, 50000 estimated total rows
...
VACUUM

The output lists both films_tsv_gin_idx and films_tsv_rum_idx and reports that 50,000 dead item identifiers were removed. The subsequent analysis finds 50,000 live rows and zero dead rows.

After VACUUM: repeat both searches

Use the same transaction pattern to test GIN again:

postgres=# begin;
BEGIN
postgres=*# drop index films_tsv_rum_idx ;
DROP INDEX
postgres=*# EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
SELECT count(*) FROM films
WHERE tsv @@ to_tsquery('simple', 'drama');
QUERY PLAN
-----------------------------------------------------------------------------------------------------
Aggregate (actual time=7.336..7.337 rows=1.00 loops=1)
Buffers: shared hit=2664
-> Bitmap Heap Scan on films (actual time=2.050..6.390 rows=10034.00 loops=1)
Recheck Cond: (tsv @@ '''drama'''::tsquery)
Heap Blocks: exact=2656
Buffers: shared hit=2664
-> Bitmap Index Scan on films_tsv_gin_idx (actual time=1.601..1.601 rows=10034.00 loops=1)
Index Cond: (tsv @@ '''drama'''::tsquery)
Index Searches: 1
Buffers: shared hit=8
Planning:
Buffers: shared hit=9 dirtied=1
Planning Time: 0.222 ms
Execution Time: 7.361 ms
(14 rows)
postgres=*# rollback;
ROLLBACK

Both GIN scan nodes now report 10,034 rows. The index no longer supplies the extra 10,080 obsolete candidates.

Repeat the test with RUM:

postgres=# begin;
BEGIN
postgres=*# drop index films_tsv_gin_idx ;
DROP INDEX
postgres=*# EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
SELECT count(*) FROM films
WHERE tsv @@ to_tsquery('simple', 'drama');
QUERY PLAN
-----------------------------------------------------------------------------------------------------
Aggregate (actual time=8.086..8.087 rows=1.00 loops=1)
Buffers: shared hit=2672
-> Bitmap Heap Scan on films (actual time=2.765..7.064 rows=10034.00 loops=1)
Recheck Cond: (tsv @@ '''drama'''::tsquery)
Heap Blocks: exact=2656
Buffers: shared hit=2672
-> Bitmap Index Scan on films_tsv_rum_idx (actual time=2.315..2.315 rows=10034.00 loops=1)
Index Cond: (tsv @@ '''drama'''::tsquery)
Index Searches: 0
Buffers: shared hit=16
Planning:
Buffers: shared hit=16
Planning Time: 0.278 ms
Execution Time: 8.120 ms
(14 rows)
postgres=*# rollback;
ROLLBACK
postgres=#

RUM now also returns 10,034 rows from both scan nodes.

The table below summarizes the effect of VACUUM: both indexes return fewer candidate TIDs after cleanup, while the number of visible matches remains unchanged.

IndexCandidates before VACUUMVisible matches before VACUUMCandidates after VACUUMVisible matches after VACUUM
GIN20,11410,03410,03410,034
RUM20,11410,03410,03410,034

GIN pending-list cleanup merges buffered entries into the main index. Dead-tuple cleanup removes obsolete references from both GIN and RUM. A clean GIN pending list therefore does not imply that vacuuming is unnecessary.

What These Maintenance Costs Mean for Your Workload

Both GIN and RUM incur write amplification because of their inverted structure: inserting one row can require adding postings under many keys. GIN mitigates this cost by buffering new entries in a pending list and merging them into the main index in batches. RUM updates its main index directly, without this batching mechanism.

Both indexes also retain references to deleted or obsolete tuple versions until cleanup. Once those tuples are removable, VACUUM removes their index references and can improve search efficiency by reducing the number of candidates that queries must process. In normal operation, autovacuum performs this maintenance.

These write and maintenance costs can make both index types a poor fit for write-intensive applications. RUM is particularly costly because it updates the main index directly.

2 responses to “The Cost of Maintaining GIN and RUM Indexes in PostgreSQL”

  1. Three Typical GIN Index Use Cases in PostgreSQL – Yuan Yao, An Oracle ACE's Blog avatar

    […] The Cost of Maintaining GIN and RUM Indexes in PostgreSQL […]

    Like

Leave a Reply

I’m Yuan

Welcome to the blog of a database performance maven. Feel free to contact me to share and discuss database knowledge.

My Oracle Book

My 26 Oracle Certifications

A screenshot from certview.oracle.com

My MySQL Book

MySQL 8.0 Operations and Optimization

Let’s connect