Erratum: the max_pred_locks_per_transaction post said the predicate lock tables have “no spillover and no summarization.” The second half is wrong. When PostgreSQL runs out of records for serializable transactions, it folds the SIREAD locks of the oldest committed ones into a single placeholder transaction, collapsing duplicates as it goes; that mechanism is the subject of this post. What the earlier post said about a full lock table stands. Filling the table does not trigger the folding, and the next lock request still fails.

On most PostgreSQL installations, serializable_buffers is 256kB of shared memory that has never been written to. It caches pg_serial, which is where Serializable Snapshot Isolation files what little it keeps about a committed transaction once it has run out of room to remember it properly. If nothing in your system runs at SERIALIZABLE, that never happens, and you can stop reading. If something does, whether it happens depends on how many other serializable transactions commit while your longest one is open.

This is one of the seven SLRU cache sizes that PostgreSQL 17 turned into parameters. commit_timestamp_buffers has the tour of what an SLRU is, and the multixact pair explains the banks of 16 buffers behind the rule that the value must be a multiple of 16. The default is 32 buffers of 8kB, the minimum is 16, and the maximum is 131072 (1GB). The context is postmaster, so changing it means a restart. Unlike transaction_buffers and subtransaction_buffers, it does not scale with shared_buffers; it is 32 on a laptop and 32 on a machine with a terabyte of RAM. On PostgreSQL 15 and 16 it is a constant in a header file, and the constant is 16.

Ten records per slot

SSI keeps a record for every serializable transaction, and has to go on keeping it after commit until every serializable transaction that overlapped it has finished, because any of them can still turn out to conflict with it. (The same rule keeps SIREAD locks alive after commit, which the max_pred_locks_per_page post covers.) The pool of records is fixed at ten per backend slot, which is 1,360 on a default PostgreSQL 18 or 19 install. Usually that is plenty, because records are freed about as fast as they are created. I ran 5,000 serializable read-write transactions back to back on 18.7, and nothing touched pg_serial. Then I left one serializable transaction idle in another session and ran them again. Commit number 1,359 was the first to touch it.

From that point on, every new serializable transaction has to evict the oldest committed one before it can take its snapshot. The evicted transaction’s SIREAD locks go to a placeholder that holds them on behalf of everything summarized so far; you can see them in pg_locks as SIReadLock rows with a virtualtransaction of -1/0. Its conflicts with transactions still in memory shrink to a flag on each of those, saying only that it conflicted with something summarized. And if it wrote anything, one 8-byte value is filed under its XID in pg_serial: the commit sequence number of the earliest-committing transaction it had a read/write conflict out to, or a marker meaning it had none.

What is lost is precision. A conflict involving a summarized transaction can no longer be examined in detail, so PostgreSQL assumes the worst, and some transactions get a serialization failure they would not otherwise have had. In exchange, nothing blocks and the pool of records cannot run out. The source’s README-SSI says of the cost that performance “can degrade in a pessimal situation, but it should be tolerable,” and I set out to see how pessimal. (The predicate lock table is another matter. Folding merges only locks on the same target, and that table can still fill.)

The transaction that sets this off does not have to be a forgotten session. The threshold is a count, not a duration: any serializable read-write transaction that is still open after about 1,360 later ones have committed. At a hundred serializable commits a second, that is fourteen seconds. With four clients committing 31,000 a second on my test machine, the pool turns over in about 45 milliseconds, and a single two-second transaction in the middle of that traffic caused 62,000 summarizations.

Not every long transaction counts:

  • A serializable read-write transaction does, whether or not it has written anything yet.
  • A prepared serializable transaction does, and it goes on doing so after the session that prepared it has disconnected.
  • A serializable READ ONLY transaction does not, provided no serializable read-write transaction was open when it took its snapshot. If one was, it counts until those transactions have finished and it runs its next query, at which point it normally drops out of SSI’s bookkeeping altogether.
  • A READ ONLY DEFERRABLE transaction never does.
  • REPEATABLE READ has nothing to do with any of this.

Eight bytes per XID, serializable or not

pg_serial is an array indexed by XID, 1,024 entries to a page. It has to span everything from the xmin of the oldest open serializable transaction to the newest summarized XID, and every XID in that range gets its eight bytes whether or not it belonged to a serializable transaction. Read-committed traffic counts. I held one serializable transaction open, burned ten million XIDs with no serializable activity at all, and then resumed. The commit that set off the first summarization took 52 milliseconds instead of the usual 0.17, because it had nearly ten thousand pages to zero, and pg_serial came to 76MB. The default cache covers 32,768 XIDs of a range like that.

Writing those pages is cheap. pg_serial is not WAL-logged, is never fsynced, is left out of base backups, and is truncated down to its newest segment at a checkpoint once the old transaction is gone. Reading them is the problem. When a serializable transaction comes across a row version it cannot see, or one that someone has since updated or deleted, it has to find out whether the transaction responsible was serializable and what it conflicted with. It looks in shared memory first; if the XID is not there and falls inside the range pg_serial covers, it looks there. The only XIDs it gets to skip are those older than its own snapshot’s xmin.

Who ends up asking about old XIDs therefore depends on whether the old transaction has written anything. If it has only read, it has no XID of its own; unless some other transaction of the same age holds one, everyone else’s snapshots have a recent xmin, and only the old transaction itself can ask about old pages. If it has written, it holds an XID, every snapshot taken since has that XID as its xmin, and every serializable transaction in the system, however short, goes to pg_serial for the dead row versions it walks past on its way to the live one. (Those versions are still there because the old transaction is holding back pruning as well.)

Whoever asks does so while holding SerializableXactHashLock in exclusive mode, which every serializable transaction also needs in order to get its snapshot and to commit. A cache miss is a file read performed under that lock.

A pessimal situation, measured

On 18.7, I opened a serializable transaction that only read, then ran two million single-row read-committed updates against a million-row table from eight clients, with a stream of serializable inserts into another table to keep summarization going. That consumed 2.4 million XIDs: about 2,350 pages of pg_serial, or 18MB. Then the old transaction ran SELECT count(*) on the table, which meant 2.86 million lookups. A REPEATABLE READ transaction of the same age did the same scan in 0.06 seconds.

serializable_buffers Scan time pg_serial reads per scan
32 2.2 s 852,000
256 2.3 s 742,000
1024 1.6 s 395,000
2048 0.85 s 62,000
4096 0.65 s 0

About a third of those lookups arrived in no particular XID order (the rest followed the order in which the rows had been written), and those are the ones that missed. For lookups like that, a cache covering a tenth of the range buys almost nothing; it has to cover most of it. The first two rows of the table are the same within noise.

That is on an otherwise idle system. With four clients running short serializable transactions during the scan, the scan took 5.7 seconds at 32 buffers and 1.2 at 4096. The clients suffered too, although on a two-core sandbox some of that is just the scan occupying a core: a CPU-bound query with no SSI involvement at all cut them from over 50,000 transactions a second to about 27,000. The serializable scan cut them to 14,500 at 4096 buffers and to 6,400 at 32, where half the samples I took of them showed a wait on LWLock:SerializableXactHash. PostgreSQL 17.12 and 19.0 produced the same picture.

That was the old transaction’s own scan, and that transaction had only read. When I repeated the setup with an old transaction that had written a row first, four clients doing nothing but single-row primary-key reads at SERIALIZABLE averaged 1.1 pg_serial file reads per transaction at 32 buffers, with almost no hits. At 4096 they did none. (Their throughput on this machine was too noisy for me to put a number on the difference.)

A larger cache makes the collision shorter and milder. It does not make it go away, partly because even with no misses at all that scan takes the lock 2.86 million times.

Three caveats about the tests. Everything was in the operating system’s page cache, so a miss cost about two microseconds, and it will cost more where that is not true. I set subtransaction_buffers to 2048 for every run, because the same check first maps each XID to its top-level transaction through pg_subtrans, and that cache has to cover the same range at 2,048 XIDs per page; left at the 128 buffers that this instance’s shared_buffers would have given it, the scan took 2.2 to 2.3 seconds even with serializable_buffers at 4096. And the primary-key readers needed max_pred_locks_per_transaction raised to 640 to run at all. At the default of 64, with one idle serializable transaction open, all four clients were dead of out of shared memory within 11,391 transactions, which is about a quarter of a second.

Whether any of this is you

Start with SELECT * FROM pg_stat_slru WHERE name = 'serializable' (the row is named Serial through PostgreSQL 16). If every counter is zero, pg_serial has not been used since the statistics were last reset, and the parameter has nothing to do. blks_zeroed times 1,024 is the number of XIDs pg_serial has had to span, and blks_read is the misses. There is a trap in that last one: a backend reports its SLRU counters only when it goes idle outside a transaction, so the reads done by the long transaction itself do not appear until it ends. In the scan test above, the row still showed no reads at all after the old transaction’s first scan, and 1.7 million as soon as it committed after its second.

For what is happening right now, look for SIReadLock rows in pg_locks with a virtualtransaction of -1/0, which belong to the placeholder (their absence proves nothing, since transactions that only write leave no locks to fold); for backends in IO:SlruRead (IO:SLRURead on 16) while others wait on LWLock:SerializableXactHash; and for serialization failures whose DETAIL line ends with “old pivot” or “old committed transaction” and an XID, which were decided from pg_serial. pg_stat_activity does not show a transaction’s isolation level, so finding the transaction responsible means sorting by xact_start and then checking pg_prepared_xacts.

If you find one, fix the transaction before you touch the parameter. idle_in_transaction_session_timeout deals with the forgotten session, and transaction_timeout (PostgreSQL 17 and later) with the runaway one. Neither applies to a prepared transaction; those have to be resolved by hand. A long report belongs in a READ ONLY DEFERRABLE transaction, which waits for a snapshot that needs no SSI tracking at all.

If long serializable read-write transactions are how the application works, take the number of XIDs the system consumes during the longest of them (age(backend_xmin) in pg_stat_activity, sampled late in its life, is that number), divide by 1,024, and round up to a multiple of 16. That is serializable_buffers, and subtransaction_buffers needs to be at least half of it. 4096 costs 32MB and covers 4.2 million XIDs. It takes a restart, so set it once and set it generously. On 15 and 16 there is nothing to set.

This is the third fixed-size SLRU cache in the series, after the multixact pair and notify_buffers, and the third time the default has turned out to be fine for everything except a straggler: a long-running writer, a stalled listener, and now a serializable transaction that stayed open. The cache size is for when you cannot get rid of the straggler. Try that first.