Subject: Re: vacuum analyze query performance - help me understand

From: Tom Lane <[email-masked]>
Date: Tue, 19 Aug 2025 14:19:56 -0400

Scot Kreienkamp <[email-masked]> writes:
> That command should vacuum analyze all tables in all databases, and it was proven that it worked as the last vacuum and analyze dates on the tables in all databases are showing dates from 2am today.

I take it from this that you have autovacuum turned off and you think
a once-a-day manual vacuum run is an adequate replacement?

> So here's the problem:  We ran a (admittedly poorly written) select query against a subset of tables which performed poorly.  Then we ran a vacuum analyze against just those tables involved in that query, then ran the same query again, which performed exponentially better.    I verified by comparing before and after explains that the query plan did not change between runs of the query, the only changes were stats like cost, rows, width, time, etc.

If the plan didn't change then the stats updates weren't very relevant.
I am guessing that the actual problem was that those tables were
full of dirty rows, and the VACUUM (not the ANALYZE part) got rid
of dead rows, set hint bits on recently-updated rows, and generally
did a lot of janitorial work that makes subsequent table scans faster.

Turning off autovacuum is an anti-pattern.

(Running a PG version that's four years past EOL is also an
anti-pattern, but you knew that.  Should I ask whether it's
at least the final 9.6 minor release?)

			regards, tom lane

---

From: Scot Kreienkamp <[email-masked]>
Date: Tue, 19 Aug 2025 20:09:40 +0000

Hi Tom,

[Scot Kreienkamp]
I thought dead rows were excluded from backup and the resulting restore?  All the janitorial was already done a few hours before by a vacuum analyze, so with no activity there shouldn't have been any to do.  That's why I'm still looking for answers.

[Scot Kreienkamp]
Agreed, that's why it's not disabled.  I'm out of date, not clueless.  The only time I turn off autovac is during full database restores.  Last I knew running a periodic reindex and a daily vac/analyze even with autovac enabled was considered best practice.  Is that no longer the case?

[Scot Kreienkamp]
It is the final release.  I guarantee I'm more irritated that we're still on this version than anyone else on earth.  They've been warned, many times and loudly, for much longer than 4 years.  All I can do is keep reminding and warning of the consequences, like not being able to get help with problems when they inevitably arise.  In the meantime, I still have to support it like a number of people's livelihoods (including mine) depends on it.  Because they do.

Scot Kreienkamp | Applications Infrastructure Architect | La-Z-Boy Corporate
One La-Z-Boy Drive | Monroe, Michigan 48162 | • (734) 384-6403 | |  • 1-734-915-1444  |  Email: [email-masked]

This message is intended only for the individual or entity to which it is addressed. It may contain privileged, confidential information which is exempt from disclosure under applicable laws. If you are not the intended recipient, you are strictly prohibited from disseminating or distributing this information (other than to the intended recipient) or copying this information. If you have received this communication in error, please notify us immediately by e-mail or by telephone at the above number. Thank you.

---

From: Greg Sabino Mullane <[email-masked]>
Date: Wed, 20 Aug 2025 12:26:19 -0400

Moving forward advice:

* Run the query more than once before doing a manual vacuum to rule out
caching.
* Change your flags for the vacuumdb from --quiet to --verbose and we can
see exactly what vacuum has done. Ideally have cron append to a file on disk
* Similarly, set log_autovacuum_min_duration to 0 (which logs all
autovacuum activity).
* As mentioned upthread, use explain (analyze, buffers, settings) for
better output
* Using the pg_buffercache extension can show you exactly what is in shared
buffers (for future debugging)

Cheers,
Greg

--
Crunchy Data - https://www.crunchydata.com
Enterprise Postgres Software Products & Tech Support