Skip to content

Commit b10b1fa

Browse files
committed
Take buffer lock while inspecting btree index pages in contrib/pageinspect.
It's not safe to examine a shared buffer without any lock.
1 parent 244413f commit b10b1fa

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

contrib/pageinspect/btreefuncs.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ GetBTPageStatistics(BlockNumber blkno, Buffer buffer, BTPageStat * stat)
154154
}
155155

156156
/* -----------------------------------------------
157-
* bt_page()
157+
* bt_page_stats()
158158
*
159-
* Usage: SELECT * FROM bt_page('t1_pkey', 1);
159+
* Usage: SELECT * FROM bt_page_stats('t1_pkey', 1);
160160
* -----------------------------------------------
161161
*/
162162
Datum
@@ -202,13 +202,17 @@ bt_page_stats(PG_FUNCTION_ARGS)
202202
CHECK_RELATION_BLOCK_RANGE(rel, blkno);
203203

204204
buffer = ReadBuffer(rel, blkno);
205+
LockBuffer(buffer, BUFFER_LOCK_SHARE);
205206

206207
/* keep compiler quiet */
207208
stat.btpo_prev = stat.btpo_next = InvalidBlockNumber;
208209
stat.btpo_flags = stat.free_size = stat.avg_item_size = 0;
209210

210211
GetBTPageStatistics(blkno, buffer, &stat);
211212

213+
UnlockReleaseBuffer(buffer);
214+
relation_close(rel, AccessShareLock);
215+
212216
/* Build a tuple descriptor for our result type */
213217
if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
214218
elog(ERROR, "return type must be a row type");
@@ -245,10 +249,6 @@ bt_page_stats(PG_FUNCTION_ARGS)
245249

246250
result = HeapTupleGetDatum(tuple);
247251

248-
ReleaseBuffer(buffer);
249-
250-
relation_close(rel, AccessShareLock);
251-
252252
PG_RETURN_DATUM(result);
253253
}
254254

@@ -320,6 +320,7 @@ bt_page_items(PG_FUNCTION_ARGS)
320320
CHECK_RELATION_BLOCK_RANGE(rel, blkno);
321321

322322
buffer = ReadBuffer(rel, blkno);
323+
LockBuffer(buffer, BUFFER_LOCK_SHARE);
323324

324325
/*
325326
* We copy the page into local storage to avoid holding pin on the
@@ -333,7 +334,7 @@ bt_page_items(PG_FUNCTION_ARGS)
333334
uargs->page = palloc(BLCKSZ);
334335
memcpy(uargs->page, BufferGetPage(buffer), BLCKSZ);
335336

336-
ReleaseBuffer(buffer);
337+
UnlockReleaseBuffer(buffer);
337338
relation_close(rel, AccessShareLock);
338339

339340
uargs->offset = FirstOffsetNumber;
@@ -464,6 +465,8 @@ bt_metap(PG_FUNCTION_ARGS)
464465
errmsg("cannot access temporary tables of other sessions")));
465466

466467
buffer = ReadBuffer(rel, 0);
468+
LockBuffer(buffer, BUFFER_LOCK_SHARE);
469+
467470
page = BufferGetPage(buffer);
468471
metad = BTPageGetMeta(page);
469472

@@ -490,8 +493,7 @@ bt_metap(PG_FUNCTION_ARGS)
490493

491494
result = HeapTupleGetDatum(tuple);
492495

493-
ReleaseBuffer(buffer);
494-
496+
UnlockReleaseBuffer(buffer);
495497
relation_close(rel, AccessShareLock);
496498

497499
PG_RETURN_DATUM(result);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy