BUG #19909: BRIN redo: BufferIsPinned assert in brin_xlog_insert_update() if index was dropped or truncated

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: hackerzheng666(at)gmail(dot)com
Subject: BUG #19909: BRIN redo: BufferIsPinned assert in brin_xlog_insert_update() if index was dropped or truncated
Date: 2026-11-17 05:03:04
Message-ID: 19909-6bd7fafaead0184c@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19909
Logged by: Zheng Hacker
Email address: hackerzheng666(at)gmail(dot)com
PostgreSQL version: 19rc2
Operating system: Linux x86_64 (Debian), clang 14.0.6, cassert
Description:

In an assert-enabled build, crash recovery (and a standby restart) aborts in
the startup process:

TRAP: failed Assert("BufferIsPinned(buffer)"), File: "bufmgr.c", Line:
4479

The stack is brin_redo -> brin_xlog_insert_update -> BufferGetBlockNumber.

Cause (source at 70c4a57; REL_18_STABLE and REL_11 through REL_18 have the
same lines):

src/backend/access/brin/brin_xlog.c
line 68: action = XLogReadBufferForRedo(record, 0, &buffer);
line 72: regpgno = BufferGetBlockNumber(buffer); (unconditional)
line 97: if (BufferIsValid(buffer)) UnlockReleaseBuffer(buffer);

XLogReadBufferForRedo() returns BLK_NOTFOUND with an InvalidBuffer when the
block is beyond the end of the file (xlogutils.c:527-530). The comment at
xlogutils.c:462-465 says this is expected: the caller should silently skip
the update. BufferGetBlockNumber(InvalidBuffer) fails the assertion at
bufmgr.c:4479. The validity check comes after the call.

Reproduction (assert-enabled build, the same binary settings as in the
Environment section, no harness in the loop):

-- settings: fsync=off, full_page_writes=off, wal_level=logical
CREATE TABLE t (a int);
INSERT INTO t VALUES (1);
CREATE INDEX ib ON t USING brin (a) WITH (pages_per_range = 1);
CHECKPOINT;
SELECT brin_desummarize_range('ib', 0);
SELECT brin_summarize_range('ib', 0);
DROP INDEX ib;
-- then, with no checkpoint in between:
-- pg_ctl -m immediate stop; pg_ctl start

Result: the startup process aborts with the TRAP above and the server does
not start. Observed 15 of 15 runs of this sequence (5 + 10).

The same pattern with a 30000-row table reproduced 20/20 with DROP TABLE,
20/20 with DROP INDEX, 20/20 with REINDEX INDEX and 20/20 with TRUNCATE. A
streaming standby that replays a DROP TABLE and is then killed and restarted
hit the same TRAP 8/8 times.

Controls (no TRAP, restart OK): no drop 0/10; CHECKPOINT after the drop 0/20
(the file is unlinked by the checkpoint); no CHECKPOINT between the index
build and the inserts 0/20; no brin_summarize (no BRIN records) 0/10;
full_page_writes=on 0/20; standby without a drop 0/4.

WAL at the crash (pg_waldump from the checkpoint REDO point): BRIN
DESUMMARIZE, then BRIN INSERT heapBlk 0 with blkref #0 = index block 2 and
no full-page image, then SAMEPAGE_UPDATE, then the COMMIT that drops the
index. The index file is 0 bytes at the crash. DROP/REINDEX/TRUNCATE
truncate the main fork to zero length at commit and unlink it only after the
next checkpoint (md.c, mdunlinkfork, non-redo path). So block 2 is beyond
EOF when the INSERT is replayed. The reading that the INSERT is the trapping
record is an inference (first non-SAMEPAGE BRIN record in the replayed
range; no debugger breakpoint was used).

Release-like behaviour: the same sequences on an ASan build without
assertions (no cassert, launched with the usual ASan settings) start
normally: restart OK and server ready in 10 of 10 runs for this sequence and
in 10 of 10 for the 30000-row DROP TABLE sequence, with no PANIC in the log.
This is an inference from the same code path; it was not checked at memory
level.

Suggested fix (not built or tested): replace line 72 with

XLogRecGetBlockTag(record, 0, NULL, NULL, &regpgno);

This is the same pattern that brin_xlog_revmap_extend() already uses (line
220). Optionally, also skip the revmap update (block 1) when block 0
returned BLK_NOTFOUND, since no index tuple was inserted. Whether block 1
can need redo while block 0 is missing has not been checked.

Severity: assertion-level in assert-enabled builds. In such a build, a
server cannot finish crash recovery, or a standby restart, while this WAL is
pending. In the ASan build tested, no effect was visible. The unguarded read
in non-assert builds (GetBufferDescriptor(-1)) is only used if block 1 also
needs redo, which has not been observed (not verified).

Environment and limits
- The binary is assumed to be built from 70c4a57. The line mapping of
brin_xlog.c:72 and bufmgr.c:4479 in the binary supports this, but the commit
was not proven from the binary.
- The non-assert comparison used a binary of unknown commit (not verified).
- Tested on git master (20devel, commit 70c4a57b64d3). Master HEAD
7fc2f0423320 (2026-11-16) has the identical code at the same lines (read
from master brin_xlog.c; not built).
- REL_11 through REL_18 have the same code at the same lines (gh contents),
but were not run.
- UPDATE records (brin_xlog_update calls brin_xlog_insert_update too) were
present in one test, but the trap was not isolated to them.
- No commit between 70c4a57 and master HEAD 7fc2f0423320 touches
src/backend/access/brin/brin_xlog.c (checked in the GitHub commit list).

A patch with a regression test will follow as a reply on this thread.

Browse pgsql-bugs by date

  From Date Subject
Next Message surya poondla 2026-11-17 05:36:41 Re: Two issues with REFRESH MATERIALIZED VIEW CONCURRENTLY
Previous Message Raja Sai pranav 2026-11-17 04:17:06 Re: BUG #19776: pg_combinebackup ignores the truncation_block_length of an intermediate incremental backup