Catalogue / kylehailey-2023-postgres-partition-lock-manager

Daily partitions with 22 indexes each drove a Postgres 13 orders table into LockManager waits and application errors

Not named in the source, 2023. PostgreSQL database holding an orders table used as a work queue and order history, after the table's conversion to daily partitions.

Organisation
Not named in the source
Evidence
Incident report
System
PostgreSQL database holding an orders table used as a work queue and order history, after the table's conversion to daily partitions
Setup
One database

Near: trying to replicate that across to the replica

As of
2023-05
Source
Postgres Partition Pains - LockManager Waits

Kyle Hailey's blog, published 2023-06-13

Architecture

A Postgres database holds an orders table that queues new work requests from customers, records the completed work and serves look-ups of past orders. At the time of the incident the table was partitioned with all the old data in one partition and one partition for each day since April 1, with 22 indexes on each partition, and changes were replicated to a replica.

Near: The primary function of the orders table | Near: With each partition having 22 indexes | Near: trying to replicate that across to the replica

Figures, as the source states them

FigureAs statedNoteWhere
Queries on the database10,000 queries per secondWhole database before the conversion; peak or average not stated; passage framed as storytellingNear: We're talking 8TB, with 10,000 queries per
Database size8 TBWhole database before the conversion; given in a passage framed as storytellingNear: We're talking 8TB, with 10,000 queries per
New records10 million records per dayBefore the conversion; each updated three times on average; passage framed as storytellingNear: 10 million new records daily
Indexes on each partition of the orders table22 indexesNear: With each partition having 22 indexes
Application errors during the lock pile-up1,000 errors per secondNear: start hitting 1000 errors a second
Sessions waiting on LockManager during the pile-up500 sessionsNear: 500 sessions waiting on LockManager
Locks held during the pile-up150,000 locksNear: 500 sessions waiting on LockManager, 150,000 locks
Locks taken by one query against 40 partitions880 locks per queryThe author's arithmetic, 40 partitions times 22 indexesNear: equating to 880 locks per query

Records

Failure#f1

Lock requests grew with every daily partition until sessions piled up on LockManager and the work queue stalled

kylehailey-2023-postgres-partition-lock-manager#f1 at corpus version 26a1b3a7e3dd
Trigger

The orders table was converted to daily partitions on April 1, gaining one partition a day. Things ran smoothly for about four weeks, then on Saturday May 6 the database began to show severe LockManager waits. It hit the wall again on May 10, four days after the first week of partitions had been taken out.

Near: For about four weeks, everything was going | Near: a partition for each day from April | Near: On May 10th , four days after

How it failed

Each partition has 22 indexes, and on PG13 a query on the orders table took locks on every partition and every index even when it was limited to a one-day partition: 880 locks per query at 40 partitions. Sessions piled up waiting on LockManager. On a log scale the waits had begun on April 11, which the author reads as the database starting to struggle once it hit 12 partitions.

Near: it becomes apparent that these waits actually | Near: equating to 880 locks per query | Near: on PG13 even a query limited to

Why defences did not hold

The author's explicit limiting of queries to partition ranges did not stop the database locking every partition. He had expected latencies to rise slowly and gradually as partitions accumulated, not an abrupt wall.

Near: Despite my efforts to explicitly limit queries | Near: I had anticipated a slow, gradual increase

Impact

The application began hitting 1000 errors a second, with 500 sessions waiting on LockManager and 150,000 locks. The work queue, the database's main function, started to stall.

Near: start hitting 1000 errors a second | Near: The primary function of the database, the

Fix

The author mitigated the pile-up by detaching 7 partitions from the table; he then rebuilt all the daily partitions as weekly ones, after which things ran smoothly, though he reckons there are only 4 - 6 months before the problem returns. He says that lock monitoring added in mid May showed no obvious limit, but that lock allocations fell with the partition count.

Near: I managed to mitigate the pileup by | Near: there was no obvious limit. | Near: After successfully rebuilding all day partitions as

Failure#f2

Orders failed after a weekly-partition swap, with logs reporting the partition missing from the replication set

kylehailey-2023-postgres-partition-lock-manager#f2 at corpus version 26a1b3a7e3dd
Trigger

To cut the partition count, the author created a weekly table using LIKE orders, removed all its indexes, loaded a week of data, and swapped it in for the seven daily partitions.

Near: So I did a copy of one | Near: Took off all the indexes, loaded a

How it failed

Orders started failing, and the log files showed an error saying the replication set did not include the new partition. An attempt to add it by hand was refused with a message that replication is managed automatically. The author notes this was handled over the phone and that he never looked at the issue in detail.

Near: the log files were giving an error | Near: I never looked at this issue in

Impact

Orders failed until the author took the week offline. The database then ran with a week of data missing: order processing worked, but customers were upset that they could not find old orders.

Near: But I raced home and took the

Fix

The weekly partition was taken offline. For later rebuilds the author created the weekly table from a daily partition with LIKE and INCLUDING ALL, which picks up the replication definition. Making it unlogged to load faster did not work either: the swap waited and waited, which the author put down to the partition being propagated to the replica, so he built the weekly partitions the slow way.

Near: But I raced home and took the | Near: which picks up the replication definition | Near: its propagating the partition to the replica

Decision#d1

Partition a large Postgres queue-and-history table by day

kylehailey-2023-postgres-partition-lock-manager#d1 at corpus version 26a1b3a7e3dd
Chosen

The author converted the orders table into a partitioned table with a partition for each day, keeping all the old data in one partition.

Near: including all old data in one partition

What forced it

Some look-up queries on the table were starting to lag. Adding an index took about 12 hours when run concurrently, and dropping an unused index took a lock that stalled all new queries, which held back experiments with indexes.

Near: Implementing new indexes has proven time-consuming | Near: the speed of some of these lookup

Reasoning

The author believed partitioning would speed queries by confining them to specific partitions, make index experiments quicker, and allow maintenance one partition at a time.

Near: I believed partitioning could enhance query speed

What worked before

Before the conversion the orders table was not partitioned. In a passage the author frames as storytelling, the database is described at that point as growing beyond manageability, at 8TB with 10,000 queries per second and 10 million new records a day.

Near: We're talking 8TB, with 10,000 queries per | Near: My central focus was to partition the

Cost

The author spent two weeks making the conversion with minimal downtime, and four weeks later judged himself significantly worse off than before it. Detaching a partition locks the table, so running queries block the detach and new queries queue behind it, which stalled the whole application; he wrote a script that kills the blockers.

Near: After investing two weeks in ensuring minimal | Near: Postgres locks the table when detaching partitions | Near: I wrote a script to kill all

Outcome

After the conversion the team saw a general improvement in speed, and vacuuming could be done partition by partition. Ten days later LockManager waits had begun, and about four weeks after the conversion they disrupted the application.

Near: After successfully converting the partition, we observed | Near: it becomes apparent that these waits actually | Near: For about four weeks, everything was going

Reviewer's notes

Curator's reading: what made daily granularity costly here was its combination with 22 indexes per partition on PG13. See f1 for the failure and d2 for what was done next.

The reviewer's reading, not the source's claim

Curator's reading: the post links the author's earlier post on the April conversion of this same table, carded as kylehailey-2023-postgres-partition-conversion; that card covers the conversion and this one the weeks after it.

The reviewer's reading, not the source's claim

Decision#d2

Rebuild daily partitions as weekly partitions to cut the number of locks each query takes

kylehailey-2023-postgres-partition-lock-manager#d2 at corpus version 26a1b3a7e3dd
Chosen

After the first spike the author started building weekly partitions from the daily ones: a new table created like a daily partition, the seven days inserted into it, a check constraint for the week added as not valid and then validated, and the table attached for the week's range.

Near: I started to build week partitions from | Near: FOR VALUES FROM ('2023-05-03') TO ('2023-05-10')

What forced it

The LockManager wait pile-up of May 6, in which lock allocations and waits fell as partitions were taken out of the table.

Near: When the first spike occurred May 6 | Near: as I took partitions out of the

Reasoning

The aim was to have fewer partitions.

Near: in order to swap out the day

What worked before

Daily partitions ran smoothly for about four weeks after the April 1 conversion before the severe LockManager waits of May 6, though on a log scale the waits had begun on April 11.

Near: For about four weeks, everything was going | Near: it becomes apparent that these waits actually

Cost

Adding one day of data to a weekly partition took 2 hours, which surprised the author for only 10G of data, and rebuilding a week took 14 hours. Two attempts to speed this up failed: after the first, orders began failing (see f2), and with an unlogged table the swap hung, which the author put down to propagation to the replica.

Near: It took 2 hours to add one | Near: 14 hours to rebuild a week is | Near: a frantic call that orders were failing

Outcome

With every daily partition rebuilt as a weekly one, things were running smoothly at the time of writing. The author figures there are only 4 - 6 months before the problem is hit again.

Near: After successfully rebuilding all day partitions as

Review

Extracted by
Claude (feed 32)
Human review
Not started. Status recorded on 2026-12-02.