I want to better understand how to debug and fix performance issues in PostgreSQL.
I have a query that usually costs 1 second to run. However, for some reason, it started taking 30 seconds to run for a period of time! After some minutes and some explain analyze invocations, it went back to normal. I didn't do anything to fix it, nor did I understand why it got better all of a sudden.
How do I debug a problem like this? What kind of logging and instrumentation should I have in place to understand what caused this and how to fix it?