Re: vacuum analyze query performance - help me understand

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Scot Kreienkamp <Scot(dot)Kreienkamp(at)la-z-boy(dot)com>
Cc: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: vacuum analyze query performance - help me understand
Date: 2025-08-19 18:19:56
Message-ID: 601313.1755627596@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

Scot Kreienkamp <Scot(dot)Kreienkamp(at)la-z-boy(dot)com> 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2025-08-19 18:39:03 Re: Streaming replica hangs periodically for ~ 1 second - how to diagnose/debug
Previous Message Scot Kreienkamp 2025-08-19 17:37:16 vacuum analyze query performance - help me understand