Problem:

A production PostgreSQL 13.4 cluster managed by Patroni (one primary, one local standby and one DR standby) slowed down for several hours. Updates on a small, frequently updated control table kept blocking each other, and application queries ran many times slower than usual. At the same time, WAL replay on both standbys stopped at the same LSN for about two hours, and replay lag grew to about 64 GB. Patroni still reported a lag of 0 MB, because it measures received WAL rather than replayed WAL. The customer opened a Severity 1 case and asked why the primary was slow, whether it would happen again and how to prevent it.

Process:

Step 1: Protect the cluster and collect a baseline

The first outputs showed both standbys streaming and receiving WAL normally, with replay stuck at one LSN. The customer was advised not to fail over, restart PostgreSQL or Patroni, or delete WAL files until the cause was known. The pg_wal directory held about 151 GB, which matched the configured wal_keep_size of 150 GB, and the replication slots retained no extra WAL, so disk usage was not part of the problem.

Step 2: Test and rule out a replay conflict on the standbys

The first hypothesis was a standby query holding a lock that WAL replay needed. The standby settings and logs ruled this out: max_standby_streaming_delay was 30 seconds, so any conflicting query would have been cancelled within 30 seconds, and the logs showed no cancelled or blocked queries during the stall. The hypothesis was withdrawn. Replay resumed on its own at about 13:54 on the local standby and about 14:00 on the DR standby. The stall began in the same second that a batch job lost its database connections, but a test reproduction of that event did not stop replay, and the exact cause was not determined. At the customer’s request, the investigation then focused on the primary.

Step 3: Analyse the primary’s logs and performance reports

Logs and performance reports from the incident day were compared with the same weekday one week earlier. A nightly clean-up batch job had opened a transaction at 02:04 and kept it open until 13:39, with one statement inside it running for about five hours. The application also wraps every statement in its own savepoint. Once a transaction holds more than 64 subtransactions, PostgreSQL must look them up in a small shared cache (the subtransaction SLRU), and while an old transaction is open, almost every row read needs that lookup. From about 09:30, 81% of active sessions were waiting on SubtransSLRU and SubtransBuffer, a wait that never appeared on the normal day. Common queries were 10 to 50 times slower, and the database completed 9.5 million transactions in five hours instead of 21.3 million.

Step 4: Explain the blocking on the control table

The same open transaction stopped vacuum from removing old row versions. By early afternoon, the main partition of the control table held about 224,000 dead row versions against about 30,000 live rows, and the table had grown by about 40%. Every update had to work through this dead data, so updates slowed down and sessions queued behind each other. This was the blocking the customer saw. Terminating blocking sessions on the primary did not help, because the long transaction was still open.

Step 5: Confirm the mechanism and the recovery point

The problem was reproduced on a PostgreSQL 13.4 test system. A savepoint-heavy workload ran normally on its own, but once one transaction was left open, the same SubtransSLRU waits appeared and read throughput fell from about 10,000 to about 25 rows per second. Ending that transaction restored it at once. On the customer’s system, performance returned to normal at 13:40, right after the long statement finished: statements taking over one second fell from 16% of the total in the ten minutes before to 0.2% in the ten minutes after.

Solution:

The customer received a runbook for future incidents: check for sessions waiting on SubtransSLRU or SubtransBuffer, find the oldest open transaction, and terminate it, which rolls it back and requires the job to be rerun. The permanent fixes recommended were:

  • Change the batch job to commit after each step instead of holding one transaction for its whole run.
  • Keep each transaction below 64 savepoints by turning off per-statement savepoints for batch jobs or committing more often.
  • Alert when sessions wait on the subtransaction cache for more than a few minutes, and alert on standby replay lag, not only on Patroni’s receive lag.
  • Set a statement_timeout for roles that run long manual queries on the primary.
  • Vacuum the bloated control table, and rebuild it in a maintenance window to return its space.
  • Close a separate transaction that had been left open for more than a month in another database.
  • Update to the latest 13.x minor release and plan an upgrade to a supported major version; from PostgreSQL 17 the subtransaction cache size can be raised.

Conclusion:

The slowdown was not caused by the control table itself. One batch transaction left open for more than eleven hours, combined with the application’s per-statement savepoints, made every session queue for the same subtransaction cache and stopped vacuum from cleaning up old row versions. Performance recovered as soon as that transaction ended. The customer accepted the analysis and took the recommended changes to its application team, and the case was closed.