summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomas Vondra2025-03-26 15:50:13 +0000
committerTomas Vondra2025-03-26 16:03:02 +0000
commite064b770c081c14703ff494893de1ec556d79b69 (patch)
treeec3ed11676391fe0eb45244fd78eef51c2b447de
parentb30c77a0e480352cce573195af819bd41a3c1b42 (diff)
Keep the decompressed filter in brin_bloom_union
The brin_bloom_union() function combines two BRIN summaries, by merging one filter into the other. With bloom, we have to decompress the filters first, but the function failed to update the summary to store the merged filter. As a consequence, the index may be missing some of the data, and return false negatives. This issue exists since BRIN bloom indexes were introduced in Postgres 14, but at that point the union function was called only when two sessions happened to summarize a range concurrently, which is rare. It got much easier to hit in 17, as parallel builds use the union function to merge summaries built by workers. Fixed by storing a pointer to the decompressed filter, and freeing the original one. Free the second filter too, if it was decompressed. The freeing is not strictly necessary, because the union is called in short-lived contexts, but it's tidy. Backpatch to 14, where BRIN bloom indexes were introduced. Reported by Arseniy Mukhin, investigation and fix by me. Reported-by: Arseniy Mukhin Discussion: https://postgr.es/m/18855-1cf1c8bcc22150e6%40postgresql.org Backpatch-through: 14
-rw-r--r--src/backend/access/brin/brin_bloom.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/backend/access/brin/brin_bloom.c b/src/backend/access/brin/brin_bloom.c
index 0bcf8e582c8..4526038b26d 100644
--- a/src/backend/access/brin/brin_bloom.c
+++ b/src/backend/access/brin/brin_bloom.c
@@ -662,6 +662,17 @@ brin_bloom_union(PG_FUNCTION_ARGS)
/* update the number of bits set in the filter */
filter_a->nbits_set = pg_popcount((const char *) filter_a->data, nbytes);
+ /* if we decompressed filter_a, update the summary */
+ if (PointerGetDatum(filter_a) != col_a->bv_values[0])
+ {
+ pfree(DatumGetPointer(col_a->bv_values[0]));
+ col_a->bv_values[0] = PointerGetDatum(filter_a);
+ }
+
+ /* also free filter_b, if it was decompressed */
+ if (PointerGetDatum(filter_b) != col_b->bv_values[0])
+ pfree(filter_b);
+
PG_RETURN_VOID();
}