@@ -156,9 +156,9 @@ GetBTPageStatistics(BlockNumber blkno, Buffer buffer, BTPageStat *stat)
156156}
157157
158158/* -----------------------------------------------
159- * bt_page ()
159+ * bt_page_stats ()
160160 *
161- * Usage: SELECT * FROM bt_page ('t1_pkey', 1);
161+ * Usage: SELECT * FROM bt_page_stats ('t1_pkey', 1);
162162 * -----------------------------------------------
163163 */
164164Datum
@@ -204,13 +204,17 @@ bt_page_stats(PG_FUNCTION_ARGS)
204204 CHECK_RELATION_BLOCK_RANGE (rel , blkno );
205205
206206 buffer = ReadBuffer (rel , blkno );
207+ LockBuffer (buffer , BUFFER_LOCK_SHARE );
207208
208209 /* keep compiler quiet */
209210 stat .btpo_prev = stat .btpo_next = InvalidBlockNumber ;
210211 stat .btpo_flags = stat .free_size = stat .avg_item_size = 0 ;
211212
212213 GetBTPageStatistics (blkno , buffer , & stat );
213214
215+ UnlockReleaseBuffer (buffer );
216+ relation_close (rel , AccessShareLock );
217+
214218 /* Build a tuple descriptor for our result type */
215219 if (get_call_result_type (fcinfo , NULL , & tupleDesc ) != TYPEFUNC_COMPOSITE )
216220 elog (ERROR , "return type must be a row type" );
@@ -247,10 +251,6 @@ bt_page_stats(PG_FUNCTION_ARGS)
247251
248252 result = HeapTupleGetDatum (tuple );
249253
250- ReleaseBuffer (buffer );
251-
252- relation_close (rel , AccessShareLock );
253-
254254 PG_RETURN_DATUM (result );
255255}
256256
@@ -322,6 +322,7 @@ bt_page_items(PG_FUNCTION_ARGS)
322322 CHECK_RELATION_BLOCK_RANGE (rel , blkno );
323323
324324 buffer = ReadBuffer (rel , blkno );
325+ LockBuffer (buffer , BUFFER_LOCK_SHARE );
325326
326327 /*
327328 * We copy the page into local storage to avoid holding pin on the
@@ -335,7 +336,7 @@ bt_page_items(PG_FUNCTION_ARGS)
335336 uargs -> page = palloc (BLCKSZ );
336337 memcpy (uargs -> page , BufferGetPage (buffer ), BLCKSZ );
337338
338- ReleaseBuffer (buffer );
339+ UnlockReleaseBuffer (buffer );
339340 relation_close (rel , AccessShareLock );
340341
341342 uargs -> offset = FirstOffsetNumber ;
@@ -466,6 +467,8 @@ bt_metap(PG_FUNCTION_ARGS)
466467 errmsg ("cannot access temporary tables of other sessions" )));
467468
468469 buffer = ReadBuffer (rel , 0 );
470+ LockBuffer (buffer , BUFFER_LOCK_SHARE );
471+
469472 page = BufferGetPage (buffer );
470473 metad = BTPageGetMeta (page );
471474
@@ -492,8 +495,7 @@ bt_metap(PG_FUNCTION_ARGS)
492495
493496 result = HeapTupleGetDatum (tuple );
494497
495- ReleaseBuffer (buffer );
496-
498+ UnlockReleaseBuffer (buffer );
497499 relation_close (rel , AccessShareLock );
498500
499501 PG_RETURN_DATUM (result );
0 commit comments