pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: hackerzheng666@gmail.com
Subject: BUG #19909: BRIN redo: BufferIsPinned assert in brin_xlog_insert_update() if index was dropped or truncated
Date: Tue, 17 Nov 2026 05:03:04 +0000
Message-ID: <19909-6bd7fafaead0184c@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      19909
Logged by:          Zheng Hacker
Email address:      hackerzheng666@gmail.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.








Message-ID: <19909-6bd7fafaead0184c@postgresql.org>
Permalink:  ../19909-6bd7fafaead0184c@postgresql.org/
Also on:    postgresql.org/message-id/19909-6bd7fafaead0184c@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, hackerzheng666@gmail.com
  Subject: Re: BUG #19909: BRIN redo: BufferIsPinned assert in brin_xlog_insert_update() if index was dropped or truncated
  In-Reply-To: <19909-6bd7fafaead0184c@postgresql.org>
  References: <19909-6bd7fafaead0184c@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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