The side quest that freed 10% of our database incident.io · 10 points · 0 commen...
HN Best 200 @best
The side quest that freed 10% of our database incident.io · 10 points · 0 comments · HN thread
TL;DR An incident.io Reliability Engineering newcomer investigated why the replica's CPU was high and found one query, which fetches the current on-call entry per rotation layer, using about 34% of DB CPU time. A new index alone did not help because the planner ignored it, so rewriting the query with row-wise comparisons made Postgres use it, cutting the query's CPU share to 0.3%.
- The query finds who is on call for each rotation layer 'right now' and was reading the full history of each layer before picking the newest row.
- Over a 3.5-day window it accounted for 33.8% of node CPU time, at 44 ms per call and about 8,359 buffers per call, versus 0.03 ms for the most-called query.
- A five-column btree index (organisation_id, schedule_id, rotation_id, layer_id, start_at) gave about 100x improvement in a local test, but production kept using the existing GiST exclusion-constraint index.
- The planner assumed the four identifier columns were independent and estimated one row, so it chose the existing index.
- Rewriting the WHERE clause as row-wise comparisons plus an ORDER BY on the same columns let Postgres use the new btree index; the author notes this relies on prepared statements too.
- After the change, query time fell about 175x, its CPU share dropped from 33.8% to 0.3%, and buffers per call fell from 8,359 to 71.
Discussion No comments yet, so there is no discussion to summarise. The post has 10 points and the thread is empty at the time of capture.
#postgres #databasePerformance #queryPlanner #indexing #incidentIo