Postgres CPU Spikes

Hi there!

I’m running a Rails app on Fly and have a Postgres DB.

In the last week, I’ve been troubleshooting CPU usage spikes on my Postgres app that I can’t figure out. Before this issue started happening, I’d see CPU utilization around 10%.

With this issue, out of nowhere the CPU will spike up to 40-50% and remain there. The only thing that I’ve been able to do to fix it is restart the server using flyctl postgres restart. After a restart, CPU drops down back to 10% for a random number of hours before it jumps back up again.

See this graph:

(where it drops off is when I restart the server)

I’m a bit stumped as to what could be causing this. When CPU utilization spikes, throughput + memory usage are the same as when the CPU is lower.

The only thing I’ve done configuration-wise with the Postgres server was enable pg_stat_statements to use PgHero. But I’ve since disabled that because I was worried it was the culprit.

Has anyone encountered something like this before? When the CPU utilization increases, my app becomes slower as each DB call starts to take more time.

Right now I’m able to restart the server and fix the issue, but I’d love to figure out something more permenant.

Appreciate any insight!