A couple old bugs in BRIN [de]summarization

From: Tomas Vondra <tomas(at)vondra(dot)me>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: A couple old bugs in BRIN [de]summarization
Date: 2026-12-01 23:51:56
Message-ID: 2ce3a4b7-61a0-4997-b684-f75912c22a43@vondra.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While investigating some issues related to BRIN, I found three
long-standing bugs in the summarization. The bugs are separate, but
small enough and related to the same are of code, so I'm submitting them
together. Each bug has a TAP test reproducing it, followed by a fix.

1) Desummarization could deadlock with a summary update

An update moving a summary to another page locks the regular page first,
and then locks the revmap page (to publish the new location). But the
brin_desummarize_page() acquires the locks in the opposite order (holds
the revmap lock while waiting for the regular page). That means a risk
of a deadlock, and it's enough if the summaries share a page.

Moreover, those are buffer content locks - the deadlock detector does
not handle those, so this deadlock is indefinite.

The fix changes the desummarization to retry. It copies the summary's
location under a shared revmap lock, and releases that lock. It then
locks the regular page before re-locking revmap, and rechecking if it's
still pointing to the expected block/offset, and retrying if not.

(I wonder if this needs some protection against indefinite retries.)

2) infinite loop after interrupting evacuation

The evacumation marks the page as BRIN_EVACUATE_PAGE. If the evacuation
gets interrupted in some way, we may end up with a page with the flag
bug still containing live summaries. This causes infinite loop, because
brininsert considers just size for in-place updates. But brin_doupdate
rejects the update because of the flag. It retries pretty much forever
(maybe the evacuation could complete and break the loop?). Anyway, it's
effectively a livelock.

To fix, both decisions can use brin_can_do_samepage_update, which checks
the flag under a buffer lock. A marked page is ineligible even for a
same-size replacement, so that the summary gets moved elsewhere.

3) index extension could initialize a new page without a lock

An update moving a summary may extend the index, and then realize the
page has already been added to the revmap. Before retrying,
brin_getinsertbuffer initializes the unused page as an empty regular
page so its free space could be reused. But it only holds a pin, not an
exclusive (content) lock needed for that. This triggers an assert in
MarkBufferDirty (without assertions the index can become corrupted).

The retry path needs to lock the destination, and check the page is
still uninitialized (and then initialize it, WAL-log it, etc).

I'm not entirely convinced the TAP tests are committable, I suspect it
maybe flaky on slower machines (it relies on timeouts etc.). To this may
need some changes. I've attached them mostly to demonstrate the issues.

regards

--
Tomas Vondra

Attachment Content-Type Size
v1-0001-Add-a-TAP-test-for-BRIN-desummarization-lock-orde.patch text/x-patch 7.8 KB
v1-0002-Lock-the-BRIN-regular-page-before-the-revmap-when.patch text/x-patch 2.9 KB
v1-0003-Add-a-TAP-test-for-an-interrupted-BRIN-page-evacu.patch text/x-patch 6.1 KB
v1-0004-Move-BRIN-updates-off-pages-marked-for-evacuation.patch text/x-patch 3.0 KB
v1-0005-Add-a-TAP-test-for-BRIN-index-extension-after-pag.patch text/x-patch 7.3 KB
v1-0006-Lock-new-BRIN-pages-before-initializing-them-on-r.patch text/x-patch 2.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Nikolay Samokhvalov 2026-12-01 23:57:37 Re: REPACK (CONCURRENTLY): do not block the table while waiting for the final lock
Previous Message Chao Li 2026-12-01 23:36:39 Re: REPACK (CONCURRENTLY) fails during parallel build of exclusion constraint indexes