Skip to content

Commit b4da9d0

Browse files
committed
brin: Don't crash on auto-summarization
We were trying to free a pointer into a shared buffer, which never works; and we were failing to release the buffer lock appropriately. Fix those omissions. While at it, improve documentation for brinGetTupleForHeapBlock, the inadequacy of which evidently caused these bugs in the first place. Reported independently by Zhou Digoal (bug #14668) and Alexander Sosna. Discussion: https://postgr.es/m/[email protected] Discussion: https://postgr.es/m/[email protected]
1 parent e6785a5 commit b4da9d0

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/backend/access/brin/brin.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ brininsert(Relation idxRel, Datum *values, bool *nulls,
190190
AutoVacuumRequestWork(AVW_BRINSummarizeRange,
191191
RelationGetRelid(idxRel),
192192
lastPageRange);
193-
brin_free_tuple(lastPageTuple);
193+
else
194+
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
194195
}
195196

196197
brtup = brinGetTupleForHeapBlock(revmap, heapBlk, &buf, &off,

src/backend/access/brin/brin_revmap.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,16 @@ brinSetHeapBlockItemptr(Buffer buf, BlockNumber pagesPerRange,
179179
/*
180180
* Fetch the BrinTuple for a given heap block.
181181
*
182-
* The buffer containing the tuple is locked, and returned in *buf. As an
183-
* optimization, the caller can pass a pinned buffer *buf on entry, which will
184-
* avoid a pin-unpin cycle when the next tuple is on the same page as a
185-
* previous one.
182+
* The buffer containing the tuple is locked, and returned in *buf. The
183+
* returned tuple points to the shared buffer and must not be freed; if caller
184+
* wants to use it after releasing the buffer lock, it must create its own
185+
* palloc'ed copy. As an optimization, the caller can pass a pinned buffer
186+
* *buf on entry, which will avoid a pin-unpin cycle when the next tuple is on
187+
* the same page as a previous one.
186188
*
187189
* If no tuple is found for the given heap range, returns NULL. In that case,
188-
* *buf might still be updated, but it's not locked.
190+
* *buf might still be updated (and pin must be released by caller), but it's
191+
* not locked.
189192
*
190193
* The output tuple offset within the buffer is returned in *off, and its size
191194
* is returned in *size.

0 commit comments

Comments
 (0)