| From: | Michael Banck <mbanck(at)gmx(dot)net> |
|---|---|
| To: | Álvaro Herrera <alvherre(at)kurilemu(dot)de> |
| Cc: | Shuntian Jiao <jiaoshuntian(at)gmail(dot)com>, Fujii Masao <masao(dot)fujii(at)oss(dot)nttdata(dot)com>, Daniel Gustafsson <daniel(at)yesql(dot)se>, pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Subject: | Re: reltuples kept decreasing with each autovacuum run Re: BUG #18773 |
| Date: | 2026-12-01 14:37:36 |
| Message-ID: | 20261201143736.GC3034@p46.dedyn.io;lightning.p46.dedyn.io |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs pgsql-hackers |
Hi,
I looked at this on and off over the last few weeks and think I came up
with a proposal that does not include doing additional work in
autovacuum.
On Thu, Nov 26, 2026 at 06:18:04PM +0100, Álvaro Herrera wrote:
> On 2026-Nov-26, Shuntian Jiao wrote:
> > Álvaro Herrera <alvherre(at)kurilemu(dot)de> 于2026年11月26日周四 20:45写道:
> > > Hmm, but if the number of recently dead tuples is even one less
> > > than the number of live tuples, the undesirable effect will
> > > continue to occur AFAICS?
> >
> > You're right. After testing, I believe the threshold-based approach in
> > v1 is not valid and is not a good solution.
>
> So, the design that doesn't include recently-dead tuples comes from
> commit 7c91a0364fcf
I bisected the change to that commit as well, the idea of that commit
was to harmonize what ANALYZE and VACUUM do to reltuples - ANALYZE set
them to the same as n_live_tup, while VACUUM also counted the current
dead tuples.
It was already mentioned back then that reltuples can deteriorate, but
it was deemed not relevant to the specific patch (which turned into
7c91a0364fcf later on) back then.
Peter Geoghegan later added some additional heuristics if the number of
scanned pages is less than 2% of the total pages In 74388a1ac36d2f.
However, I checked with some debug ereport()s what happens in the test
case I posted earlier and there the number of scanned pages is pretty
high, but the number of scanned tuples is extremely low:
|2026-12-01 15:11:29.622 CET [816343] WARNING: old_density: 44.517, old_rel_tuples: 7924, old_rel_pages: 178, total_pages: 178, unscanned_pages: 32, scanned_pages: 146, scanned_tuples: 1
|2026-12-01 15:11:29.622 CET [816343] WARNING: new_density: 8.009, new_rel_tuples: 1425, new_rel_pages: 178
So the new density is much lower, and we have only one scanned tuple,
while 146/178 pages are being scanned, way above the 2% threshold added
in 74388a1ac36d2f. The new density of just 8 results in a dropping the
number of reltuples from 7924 to 178, and in the next autovacuum this is
again dropped and so on until it reltuples is 1.
If I maximize the amount of scanned tuples by cutting it just at the
other side of a page boundary, I get:
|2026-12-01 15:14:34.128 CET [818243] WARNING: old_density: 44.517, old_rel_tuples: 7924, old_rel_pages: 178, total_pages: 178, unscanned_pages: 32, scanned_pages: 146, scanned_tuples: 45
|2026-12-01 15:14:34.128 CET [818243] WARNING: new_density: 8.256, new_rel_tuples: 1469, new_rel_pages: 178
Here, 45/7924 is no problem, but then autoanalyze comes by and drops the
number of live_tuples to
|2026-12-01 15:14:34.140 CET [818243] WARNING: in do_analyze_rel: running vac_update_relstats for relation: pg_class, num_tuples: 1414
and then the next autovacuum has a harder timer:
|2026-12-01 15:14:35.113 CET [818248] WARNING: old_density: 7.944, old_rel_tuples: 1414, old_rel_pages: 178, total_pages: 178, unscanned_pages: 32, scanned_pages: 146, scanned_tuples: 45
|2026-12-01 15:14:35.113 CET [818248] WARNING: new_density: 1.681, new_rel_tuples: 299, new_rel_pages: 178
45/1414 is 3.2%.
On the other hand of the spectrum, if I just drop a bit above 20% of the
tables, the density is (of course) just 20% lower:
|2026-12-01 15:05:23.920 CET [814645] WARNING: old_density: 36.860, old_rel_tuples: 6561, old_rel_pages: 178, total_pages: 178, unscanned_pages: 147, scanned_pages: 31, scanned_tuples: 17
|2026-12-01 15:05:23.920 CET [814645] WARNING: new_density: 30.536, new_rel_tuples: 5435, new_rel_pages: 178
So my proposal would be to add (yet another) heuristic, namely that if
the page count stays constant and if **either** less than 2% of tuples
(compared to the previous number of tuples) were scanned **or** the old
tuple density is at least three times higher than the new one, then also
stick to the old reltuples, something like the attached WIP patch. I am
attaching the (crude) test script I used as well.
Thoughts?
Michael
| Attachment | Content-Type | Size |
|---|---|---|
| 0001-WIP-Fix-deteriorating-reltuples-in-successive-VACUUM.patch | text/x-diff | 2.3 KB |
| vac_reltuples_test.sh | application/x-sh | 1.8 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Nazir Bilal Yavuz | 2026-12-01 14:43:12 | Re: Should we add timing output to pg_upgrade? |
| Previous Message | Markos Fountoulakis | 2026-12-01 14:35:08 | Re: enhancing pg_basebackup speeds up to ~23Gbps (small fixes + io_uring/Direct I/O) |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Fujii Masao | 2026-12-01 14:19:45 | Re: Lost walsender wakeup stalls synchronous commits until the standby's next reply (16+) |