Re: VACUUM/ANALYZE double-count the backend's own pending table stats

From: Staroverov Ilja <i(dot)staroverov(at)ftdata(dot)ru>
To: Manu <manuelreyesbravo(at)gmail(dot)com>, "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Cc: Sebastian Webber <sebastian(at)swebber(dot)me>
Subject: Re: VACUUM/ANALYZE double-count the backend's own pending table stats
Date: 2026-10-15 06:08:19
Message-ID: 1CBF745F-CB0D-4D35-9E25-D6B5208335E4@localhost.localdomain
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Manu, Sebastian,

Thanks a lot for the review, the stress testing and the autovacuum
numbers, and for finding 81e82a2bd48.

v2 attached:

0001 is v1 plus a pinned test. Pending stats are only flushed while
the backend is idle, so the TRUNCATE and ANALYZE cases now run inside
a procedure, with a COMMIT between the steps. Nothing can be flushed
in between, whatever the timing. I checked that on unpatched master
the test still fails with pg_sleep(1.5) between the steps (n_live_tup
12 and 13 instead of 6 and 3). VACUUM can't run in a procedure, so the
VACUUM case is still best effort: if a flush slips in between the
INSERT and the VACUUM, it passes without testing anything, but it
can't fail spuriously. Manu, if you had a way to pin that one too,
I'd be glad to use it.

0002 is Manu's ins_since_vacuum delta, unchanged. I kept it as a
separate patch with Manu as the author. It looks right to me:
ins_before_vacuum lives only in the pending entry, which is freed after
a successful flush. tuples_inserted only grows while the entry is
pending, so the difference can't go negative. A second VACUUM simply
re-assigns it. On top of v1, it also passes my edge cases (2PC
TRUNCATE, relcache invalidation between the INSERT and the ANALYZE,
TOAST, matview, partitioned table, ANALYZE of a column list, and a
database-wide VACUUM ANALYZE with assertions enabled).

Both apply cleanly to current master (73e69062c).

Manu, about the two pre-existing TRUNCATE/subtransaction cases: yes,
please send the repros. Since they aren't caused by this patch, maybe
they would be better handled as a separate patch, so that this one
stays small enough to back-patch. Sebastian's numbers show that 14-18
are affected too. I can prepare back-branch versions if a committer
wants them.

--
Ilya Staroverov

Attachment Content-Type Size
v2-0001-Fix-double-counting-of-pending-relation-stats-by-.patch application/octet-stream 11.1 KB
v2-0002-Don-t-count-pending-inserts-in-ins_since_vacuum-a.patch application/octet-stream 5.5 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Manu 2026-10-15 06:21:43 Re: use-after-free in jsonb_set_lax()
Previous Message John Naylor 2026-10-15 05:31:07 Re: use-after-free in jsonb_set_lax()