News for hackers. Stuff that matters.
Re:Tips for a Healthier Postgres Database by nicoburns on Saturday January 08, 2022 @09:20PM (#29859220)
The biggest Postgres related lesson I learnt recently is the importance of running ANALYZE and having up to date table statistics. In our case it was the difference between queries taking several seconds to run and taking <100ms.

We get much higher traffic during the daytime, so we now have a cronjob that runs VACUUM ANALYZE each night.

We also have some small metadata tables that are very update heavy as we sync these from another data store, replacing all rows each time. We now run VACUUM FULL on these after each sync (this locks the table but is fast (20-40ms) on such small tables) to avoid bloating them over time.

Re:Tips for a Healthier Postgres Database by saberience on Sunday January 09, 2022 @02:39AM (#29861167)
It seems to me that there's something major missing from Postgres if you need to manually run something daily or else the queries can go from 100ms to "several seconds."

If this is such a standard requirement and the statistics are so vital to performance, why isn't something built-in to the engine to keep these up-to-date without a user intervention?

I don't have to run "ANALYZE" on DynamoDB daily to ensure performance doesn't tank.

Re:Tips for a Healthier Postgres Database by fabian2k on Sunday January 09, 2022 @04:28AM (#29861779)
This is usually done automatically in Postgres in the background by the Autovacuum process. For high volumes of traffic or certain usage patterns you might have to tune the Autovacuum settings, or it might not be able to keep up with the more conservative default settings.

One issue is that there is some counter-intuitive behaviour here, if you see the Autovacuum taking significant resources, the worst thing you can do is to let it run less often. You actually need to make it more aggressive in that kind of situation, and/or fix your usage pattern or add more resources.

If a manual ANALYZE is necessary, this can often indicate a misconfiguration of Postgres, e.g. someone reducing the Autovacuum frequency or disabling it entirely. Postgres also got a lot better at this, so it also matters how old your Postgres version is.

Re:Tips for a Healthier Postgres Database by topicseed on Sunday January 09, 2022 @07:07AM (#29862554)
Agreed, I never faced these issues as our database is relatively small but it's weird to see so many manual commands used for maintenance when these could be built-in PG ongoing process.

Why isn't that so?

Re:Tips for a Healthier Postgres Database by tpetry on Sunday January 09, 2022 @07:29AM (#29862686)
It is built-in. The autovacuum process is doing this. When you have a large database you may need to tune some config parameters, as the default conservative setting for low/mid databases are naturally different.

Running these manual commands is just shifting the config tuning for large databases to periodic „manual“ operations.

Re:Tips for a Healthier Postgres Database by jdreaver on Saturday January 08, 2022 @09:48PM (#29859450)
+1 to both of these approaches. Maybe once per year we would have a query plan regress because of out of date column statistics. This happened on some pathological cases like tables that had old data evicted frequently. Once we committed to running VACUUM ANALYZE nightly on a cron, we never saw query plans regress out of of the blue again.
Re:Tips for a Healthier Postgres Database by skunkworker on Sunday January 09, 2022 @12:59AM (#29860673)
ANALYZE is the first thing I run when looking at a problematic slow query, if you aren't aware of it, you could spend hours trying to figure out why the planner was doing A when it should be doing B.
Re:Tips for a Healthier Postgres Database by jeltz on Sunday January 09, 2022 @07:37AM (#29862718)
Have you looked into tuning autovacuum? Autovacuum should handle it and even it does not you should look at why it failed to run often enough.
Re:Tips for a Healthier Postgres Database by nicoburns on Sunday January 09, 2022 @10:55AM (#29864319)
We could. But seeing as we have pretty much no query volume overnight it seemed simpler just to use the hammer and vacuum everything overnight.
Re:Tips for a Healthier Postgres Database by chkal on Sunday January 09, 2022 @02:21AM (#29861058)
> so we now have a cronjob that runs VACUUM ANALYZE each night.

Doesn't autovacuum typically handle this automatically?

Re:Tips for a Healthier Postgres Database by uhoh-itsmaciek on Sunday January 09, 2022 @03:36AM (#29861487)
It should, but it may need to be tuned to run aggressively enough, which can be tricky. Sometimes it's easier to just schedule a manual job that runs during your off hours. There are some interesting discussions on the Postgres development list on improving VACUUM performance [1] and other improvements to the overall MVCC mechanism to avoid the need for VACUUM in the first place if possible [2]. Postgres, like any complex system, does have pathological cases. It helps to keep an eye on performance and learn the system as your database grows. But it's good to see the community is thinking about how to improve the situation.

[1]: https://www.postgresql.org/message-id/flat/CA%2BTgmoZgapzekb... [2]: https://www.postgresql.org/message-id/flat/CAH2-Wz%3DsSvMX5H...