Commits

Anonymous committed d54a94b

Take buffer lock while inspecting btree index pages in contrib/pageinspect.

It's not safe to examine a shared buffer without any lock.

Comments (0)

Files changed (1)

contrib/pageinspect/btreefuncs.c

 }
 
 /* -----------------------------------------------
- * bt_page()
+ * bt_page_stats()
  *
- * Usage: SELECT * FROM bt_page('t1_pkey', 1);
+ * Usage: SELECT * FROM bt_page_stats('t1_pkey', 1);
  * -----------------------------------------------
  */
 Datum
 	CHECK_RELATION_BLOCK_RANGE(rel, blkno);
 
 	buffer = ReadBuffer(rel, blkno);
+	LockBuffer(buffer, BUFFER_LOCK_SHARE);
 
 	/* keep compiler quiet */
 	stat.btpo_prev = stat.btpo_next = InvalidBlockNumber;
 
 	GetBTPageStatistics(blkno, buffer, &stat);
 
+	UnlockReleaseBuffer(buffer);
+	relation_close(rel, AccessShareLock);
+
 	/* Build a tuple descriptor for our result type */
 	if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
 		elog(ERROR, "return type must be a row type");
 
 	result = HeapTupleGetDatum(tuple);
 
-	ReleaseBuffer(buffer);
-
-	relation_close(rel, AccessShareLock);
-
 	PG_RETURN_DATUM(result);
 }
 
 		CHECK_RELATION_BLOCK_RANGE(rel, blkno);
 
 		buffer = ReadBuffer(rel, blkno);
+		LockBuffer(buffer, BUFFER_LOCK_SHARE);
 
 		/*
 		 * We copy the page into local storage to avoid holding pin on the
 		uargs->page = palloc(BLCKSZ);
 		memcpy(uargs->page, BufferGetPage(buffer), BLCKSZ);
 
-		ReleaseBuffer(buffer);
+		UnlockReleaseBuffer(buffer);
 		relation_close(rel, AccessShareLock);
 
 		uargs->offset = FirstOffsetNumber;
 				 errmsg("cannot access temporary tables of other sessions")));
 
 	buffer = ReadBuffer(rel, 0);
+	LockBuffer(buffer, BUFFER_LOCK_SHARE);
+
 	page = BufferGetPage(buffer);
 	metad = BTPageGetMeta(page);
 
 
 	result = HeapTupleGetDatum(tuple);
 
-	ReleaseBuffer(buffer);
-
+	UnlockReleaseBuffer(buffer);
 	relation_close(rel, AccessShareLock);
 
 	PG_RETURN_DATUM(result);