pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #19794: Assert in btvacuumcleanup() fails when page counted in pages_free twice
3+ messages / 3 participants
[nested] [flat]

* BUG #19794: Assert in btvacuumcleanup() fails when page counted in pages_free twice
@ 2026-10-24 13:00 PG Bug reporting form <noreply@postgresql.org>
  2026-10-25 00:38 ` Re: BUG #19794: Assert in btvacuumcleanup() fails when page counted in pages_free twice Andrey Borodin <x4mmm@yandex-team.ru>
  0 siblings, 1 reply; 3+ messages in thread

From: PG Bug reporting form @ 2026-10-24 13:00 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: exclusion@gmail.com

The following bug has been logged on the website:

Bug reference:      19794
Logged by:          Alexander Lakhin
Email address:      exclusion@gmail.com
PostgreSQL version: 19rc1
Operating system:   Ubuntu 24.04
Description:        

The following script:
echo "
CREATE TABLE t (id int);
CREATE INDEX t_i1 ON t (id);
CREATE INDEX t_i2 ON t (id);
INSERT INTO t SELECT g FROM generate_series(2000000, 1, -1) g;
DELETE FROM t WHERE id > 999000 AND id < 1000000;
VACUUM (INDEX_CLEANUP ON);
DELETE FROM t WHERE id > 1000000;
" | psql

echo "
SELECT 'SELECT txid_current();' FROM generate_series(1, 10000)
\gexec
" | psql >/dev/null &
psql -c 'VACUUM t'
wait

triggers:
TRAP: failed Assert("stats->pages_deleted >= stats->pages_free"), File:
"nbtree.c", Line: 1208, PID: 3843334
ExceptionalCondition at assert.c:51:13
btvacuumcleanup at nbtree.c:1209:22
index_vacuum_cleanup at indexam.c:698:1
vac_cleanup_one_index at vacuum.c:2743:10
parallel_vacuum_process_one_index at vacuumparallel.c:1171:16
parallel_vacuum_process_unsafe_indexes at vacuumparallel.c:1100:3
parallel_vacuum_process_all_indexes at vacuumparallel.c:981:2
parallel_vacuum_cleanup_all_indexes at vacuumparallel.c:636:1
lazy_cleanup_all_indexes at vacuumlazy.c:3019:2
lazy_scan_heap at vacuumlazy.c:1634:1
heap_vacuum_rel at vacuumlazy.c:878:31
table_relation_vacuum at tableam.h:1803:1
vacuum_rel at vacuum.c:2360:2
vacuum at vacuum.c:635:10
ExecVacuum at vacuum.c:470:2
standard_ProcessUtility at utility.c:861:4
ProcessUtility at utility.c:525:3
PortalRunUtility at pquery.c:1153:2
PortalRunMulti at pquery.c:1311:5
PortalRun at pquery.c:792:5
exec_simple_query at postgres.c:1304:11
PostgresMain at postgres.c:4950:27
BackendInitialize at backend_startup.c:142:1
postmaster_child_launch at launch_backend.c:269:3
BackendStartup at postmaster.c:3650:8
ServerLoop at postmaster.c:1730:10
PostmasterMain at postmaster.c:1414:11
main at main.c:232:2
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x8b)[0x7a3a2f82a28b]
postgres: postgres regression [local] VACUUM(_start+0x25)[0x5a37f1dd61b5]
2026-10-24 14:40:20.803 EEST [3843333:4] psql LOG:  disconnection: session
time: 0:00:00.517 user=postgres database=regression host=[local]
2026-10-24 14:40:20.898 EEST [3843260:6] LOG:  client backend (PID 3843334)
was terminated by signal 6: Aborted

With diagnostic log added in btvacuumcleanup(), I can see:
2026-10-24 14:33:48.916 EEST [3829584:7] psql LOG:  !!!btvacuumcleanup|
stats->pages_deleted: 4927, stats->pages_free: 4928
2026-10-24 14:33:48.916 EEST [3829584:8] psql CONTEXT:  while scanning
relation "public.t"
2026-10-24 14:33:48.916 EEST [3829584:9] psql STATEMENT:  VACUUM t
TRAP: failed Assert("stats->pages_deleted >= stats->pages_free"), File:
"nbtree.c", Line: 1209, PID: 3829584

Reproduced starting from 0f0cfb494.








^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #19794: Assert in btvacuumcleanup() fails when page counted in pages_free twice
  2026-10-24 13:00 BUG #19794: Assert in btvacuumcleanup() fails when page counted in pages_free twice PG Bug reporting form <noreply@postgresql.org>
@ 2026-10-25 00:38 ` Andrey Borodin <x4mmm@yandex-team.ru>
  2026-10-25 15:05   ` Re: BUG #19794: Assert in btvacuumcleanup() fails when page counted in pages_free twice shihao zhong <zhong950419@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Andrey Borodin @ 2026-10-25 00:38 UTC (permalink / raw)
  To: Alexander Lakhin <exclusion@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

Hi Alexander,

On 24 Oct 2026, Alexander Lakhin wrote:
> stats->pages_deleted: 4927, stats->pages_free: 4928

It looks like _bt_pagedel() can delete a page ahead of the physical
scan. If it becomes recyclable before the scan reaches it, both
btvacuumpage() and _bt_pendingfsm_finalize() can count it as free.

0f0cfb494 seems to expose this by making parallel VACUUM workers inherit
PROC_IN_VACUUM. Their xmin previously held back the recycling horizon.
The pending-FSM optimization itself dates to 9dd963ae2534.

Without assertions, pages_deleted - pages_free underflows and stores a
huge btm_last_cleanup_num_delpages in the metapage, prompting an extra
cleanup-only index scan. I don't see an unsafe page reuse here:
_bt_allocbuf() rechecks recyclability under the page lock.

PFA a small fix: only queue pages at or behind scanblkno, matching the
existing pages_deleted accounting. Pages ahead are left to the normal
scan. This may defer recycling some pages until the next VACUUM if they
are not yet recyclable when the scan visits them.

Thank you!


Best regards, Andrey Borodin.

Attachments:

v1-0001-Avoid-counting-deleted-B-tree-pages-as-free-twice.patch (application/octet-stream, 2.1K) download From 706bb5399a4d656d8dfb0868f1ae8b0497660ab3 Mon Sep 17 00:00:00 2001 From: Andrey Borodin <amborodin@acm.org> Date: Sun, 25 Oct 2026 05:28:11 +0500 Subject: [PATCH v1] Avoid counting deleted B-tree pages as free twice. VACUUM can delete pages ahead of its physical scan position. If such a page becomes recyclable before the scan reaches it, btvacuumpage() counts it as free, and _bt_pendingfsm_finalize() can count it again. This can make pages_free exceed pages_deleted, failing an assertion or underflowing the count of deleted pages not yet available for reuse. Only save pending FSM entries for pages at or behind the scan position, matching the existing accounting for pages_deleted. Leave pages ahead of the scan to btvacuumpage(). Reported-by: Alexander Lakhin <exclusion@gmail.com> Discussion: https://postgr.es/m/19794-b44efb1b558039cd@postgresql.org Backpatch-through: 14 --- src/backend/access/nbtree/nbtpage.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c index 410514d8e10..f56f9e0d905 100644 --- a/src/backend/access/nbtree/nbtpage.c +++ b/src/backend/access/nbtree/nbtpage.c @@ -2779,15 +2779,17 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, BlockNumber scanblkno, */ stats->pages_newly_deleted++; if (target <= scanblkno) + { stats->pages_deleted++; - /* - * Remember information about the target page (now a newly deleted page) - * in dedicated vstate space for later. The page will be considered as a - * candidate to place in the FSM at the end of the current btvacuumscan() - * call. - */ - _bt_pendingfsm_add(vstate, target, safexid); + /* + * Remember the page for possible recycling at the end of the scan. + * Pages ahead of scanblkno will be visited by btvacuumpage(), which + * might count them as free. Don't add those pages here, or + * _bt_pendingfsm_finalize() could count them as free a second time. + */ + _bt_pendingfsm_add(vstate, target, safexid); + } /* Success - hold on to lock on leafbuf (might also have been target) */ return true; -- That's all, folks. May the source be with you. =
^ permalink raw reply [nested|flat] 3+ messages in thread

* Re: BUG #19794: Assert in btvacuumcleanup() fails when page counted in pages_free twice
  2026-10-24 13:00 BUG #19794: Assert in btvacuumcleanup() fails when page counted in pages_free twice PG Bug reporting form <noreply@postgresql.org>
  2026-10-25 00:38 ` Re: BUG #19794: Assert in btvacuumcleanup() fails when page counted in pages_free twice Andrey Borodin <x4mmm@yandex-team.ru>
@ 2026-10-25 15:05   ` shihao zhong <zhong950419@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: shihao zhong @ 2026-10-25 15:05 UTC (permalink / raw)
  To: Andrey Borodin <x4mmm@yandex-team.ru>; +Cc: Alexander Lakhin <exclusion@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

Hi Andrey,

Thanks for the quick fix. I tried v1 with Alexander's script. On
master it hit the assertion in 12 of 15 runs. With v1 it never did,
on master and on 14 through 19.

The patch looks good to me.

Thanks,
Shihao

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2026-10-25 15:05 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-24 13:00 BUG #19794: Assert in btvacuumcleanup() fails when page counted in pages_free twice PG Bug reporting form <noreply@postgresql.org>
2026-10-25 00:38 ` Andrey Borodin <x4mmm@yandex-team.ru>
2026-10-25 15:05   ` shihao zhong <zhong950419@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox