BRIN minmax-multi summarize drops concurrently inserted values

From: Tomas Vondra <tomas(at)vondra(dot)me>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: BRIN minmax-multi summarize drops concurrently inserted values
Date: 2026-12-06 14:24:24
Message-ID: 27f171ed-7037-4dc7-aae2-aac3556da833@vondra.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

The brin_minmax_multi_ops opclass has a bug - it can lose values when
summarizing ranges, in which case the index produces incorrect results
(not matching some ranges).

The approximate issue is that the minmax-multi keeps two distinct
representations of the summary. The "serialized" value bv_values (same
as the other BRIN opclasses) and then "expanded" bv_mem_value (which is
specific to minmax-multi). There's a gap in how these values are handled
when merging concurrent updates, and some of the values can be lost.

A bit more precisely:

* add_value adds values to the "expanded" bv_mem_value representation
(which is serialized before writing to disk)

* If there's concurrent insert/update, and the placeholder tuple
changes, brin_doupdate fails, and the two states are merged by
union_tuples. So far everything is fine, this is normal BRIN stuff.

* brin_minmax_multi_union however installs the merged state into
bv_values, *but bv_mem_value still points at the old expanded state".

This confuses the next brin_form_tuple() call, which serializes it
again, overwriting the "merged" state with a stale summary. This loses
one of the union_tuples sides, pretty much.

The attached patch fixes that by resetting the bv_mem_value and
bv_serialize pointers in brin_minmax_multi_union. I think that's the
right fix. It also adds an isolation test.

The report and the patch comes from AI (Claude, to be precise), but I
went through it and I think it's correct. Barring objections, I'll get
it committed and backpatched soon.

regards

--
Tomas Vondra

Attachment Content-Type Size
v1-0001-Fix-BRIN-minmax-multi-losing-concurrently-inserte.patch text/x-patch 13.6 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Saheed Ajiboye 2026-12-06 14:41:08 Re: Proposal: "query_work_mem" GUC, to distribute working memory to the query's individual operators
Previous Message Chengpeng Yan 2026-12-06 12:52:34 Re: Tracking per-RelOptInfo uniqueness during planning