In PostgreSQL, a GIN index on readings.payload keeps most inserts fast, but a few stall for seconds; what is the GIN pending list, and how do you tame it?
answer
- defer the inverted-index work
- an unsorted list searched too
- 4MB and then a flush
- vacuum, autoanalyze or a function
- steady latency costs throughput
basics
~20 sWith fastupdate on, the default, GIN appends new entries to an unsorted pending list and merges them in bulk later. The insert that pushes the list past gin_pending_list_limit (4MB) does the whole merge itself and stalls; searches also scan the list.
solid answer
~50 sA row inserted into a GIN index can add many keys, so with `fastupdate` on (the default) GIN puts new entries in an unsorted **pending list** and moves them into the main structure in bulk later. That happens when the table is vacuumed or autoanalyzed, when `gin_clean_pending_list()` is called, or when the list grows beyond `gin_pending_list_limit`, 4MB by default. In the last case the insert that crossed the limit does the cleanup in the foreground, which is the stall you see, and every search must also scan the pending list. To tame it: keep autovacuum and autoanalyze running often on `readings` so cleanup happens in the background; tune the limit per index with `ALTER INDEX ... SET (gin_pending_list_limit = ...)`; call `gin_clean_pending_list()` on a schedule; or set `fastupdate = off` when steady latency matters more than insert throughput. `pgstatginindex()` from pgstattuple shows the list's size.
code
sql · 11 linesCREATE EXTENSION IF NOT EXISTS pgstattuple;
-- how big is the pending list right now?
SELECT pending_pages, pending_tuples
FROM pgstatginindex('readings_payload_pgin');
-- per-index limit in kB: fewer foreground flushes, each larger
ALTER INDEX readings_payload_pgin SET (gin_pending_list_limit = 16384);
-- or flush on a schedule from a job
SELECT gin_clean_pending_list('readings_payload_pgin');go deeper
Recall that GIN indexes are slower to update than B-trees and that PostgreSQL buffers new GIN entries in a pending list by default.
Explain fastupdate, the 4MB default gin_pending_list_limit, the four ways the list is flushed, and why a large list slows searches as well as one unlucky insert.
Diagnose periodic insert stalls from the pending list with pgstatginindex, then choose between background cleanup, a per-index limit, scheduled flushes and fastupdate off.
Decide where GIN write cost should be paid on an ingest-heavy table, inline per row, in background maintenance or in batch rebuilds, against the latency targets readers and writers need.
## Why GIN needs a pending list A **GIN** index is inverted: it stores each key once with the set of rows containing it. Indexing one `payload` document with `jsonb_path_ops` can add a dozen entries, each in a different place in the index. Doing that work on every insert makes GIN much slower to update than a B-tree. The **fast update** technique postpones it. With the `fastupdate` storage parameter **on, the default**, new entries are appended to a temporary, unsorted **pending list** instead of the main structure. Later they are moved in bulk, using the same techniques as an index build, which is cheaper overall even counting the extra vacuum work. ## When the list is cleaned up The pending list is merged into the main index when: 1. the table is **vacuumed**, manually or by autovacuum; 2. the table is **autoanalyzed**; 3. someone calls **`gin_clean_pending_list(index)`**, which returns the number of pages removed from the list; 4. the list grows larger than **`gin_pending_list_limit`**. The first three run in the background or on your schedule. The fourth runs in the **foreground**, inside the insert that pushed the list over the limit. ## The two costs - **Latency spikes on writes.** Most inserts only append to the list, but the one that crosses the limit performs the whole cleanup and can take far longer than the rest. That is the pattern in the question: a fast median with occasional multi-second outliers. - **Slower searches.** A query must scan the unsorted pending list in addition to the main index, so a large list slows every search that uses the index. ## The settings involved | Setting | Where | Default | Effect | |---|---|---|---| | `fastupdate` | GIN storage parameter | `on` | uses the pending list at all | | `gin_pending_list_limit` | server parameter, settable per session | `4MB` (units of kB, minimum 64kB) | size that triggers a foreground flush | | `gin_pending_list_limit` | GIN storage parameter | inherits the server value | per-index override, in kB | ## How to tame it - **Make background cleanup frequent.** If autovacuum and autoanalyze process `readings` often enough, the list rarely reaches the limit; per-table autovacuum tuning is the autovacuum leaf's subject. - **Raise the limit on the hot index.** `ALTER INDEX readings_payload_pgin SET (gin_pending_list_limit = 16384);` makes foreground flushes rarer, but each one that does happen takes longer and searches scan a bigger list. - **Flush on a schedule.** A job calling `gin_clean_pending_list('readings_payload_pgin')` every few minutes keeps the list short without depending on autovacuum timing. - **Turn fast update off.** `ALTER INDEX ... SET (fastupdate = off)` gives every insert a steady, higher cost and no spikes. It does not flush existing entries; run `VACUUM` or `gin_clean_pending_list()` afterwards. - **Bulk loads.** For very large loads, dropping the GIN index and recreating it afterwards is often faster than inserting into it row by row. ## Measuring it The `pgstattuple` extension's `pgstatginindex(regclass)` returns `version`, `pending_pages` and `pending_tuples`. Sampling it next to insert latency shows whether stalls line up with the list approaching its limit. The symptom has a recognisable shape: - the median insert time is low and steady, while the slowest inserts are many times slower; - the slow inserts recur roughly each time another few megabytes of new GIN entries have built up; - the stalls fade for a while after a vacuum or autoanalyze of `readings`, then return; - searches using the GIN index get slower as `pending_pages` grows and faster right after a flush. ## Choosing a strategy | Situation | Usual choice | |---|---| | Steady ingest, autovacuum keeps up | leave `fastupdate` on with the default limit | | Ingest outpaces background cleanup | scheduled `gin_clean_pending_list()` or a higher per-index limit | | Strict per-insert latency target | `fastupdate = off`, accepting slower average inserts | | One-off load of millions of rows | drop the GIN index, load, recreate it | Whichever you pick, re-measure: the right limit depends on how fast keys arrive and how often background cleanup really runs.
- You set fastupdate = off on the index, but searches are still slow for a while. Why?Turning `fastupdate` off only stops new entries going to the pending list; it does not flush what is already there. Searches keep scanning the old list until a `VACUUM` of the table or a `gin_clean_pending_list()` call moves those entries into the main structure. Run one of them right after the `ALTER INDEX`.
- Why might raising gin_pending_list_limit make the worst stall worse?A larger limit makes foreground cleanups rarer, because background vacuum and autoanalyze get more chances to flush first. But when an insert does cross the larger limit, it has more entries to merge, so that single stall is longer, and searches meanwhile scan a bigger unsorted list. The setting moves the trade-off; it does not remove it.
The pending list is an in-tray: each new letter is dropped on the pile instead of being filed, which keeps the counter moving, but anyone searching must also leaf through the pile, and whoever drops the letter that overflows the tray has to file the whole pile before leaving.
- The pending list is only flushed by REINDEX.
- fastupdate is off by default, so GIN inserts are always slow.
- Turning fastupdate off immediately empties the pending list.
- Entries in the pending list are invisible to queries until flushed.
- gin_pending_list_limit is measured in 8 kB pages.