Skip to content

Commit f6d6b7b

Browse files
committed
Don't prematurely free the BufferAccessStrategy in pgstat_heap().
This function continued to use it after heap_endscan() freed it. In passing, don't explicit create a strategy here. Instead, use the one created by heap_beginscan_strat(), if any. Back-patch to 9.2, where use of a BufferAccessStrategy here was introduced.
1 parent 0cf1668 commit f6d6b7b

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

contrib/pgstattuple/pgstattuple.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,12 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
277277
BlockNumber tupblock;
278278
Buffer buffer;
279279
pgstattuple_type stat = {0};
280-
BufferAccessStrategy bstrategy;
281280

282281
/* Disable syncscan because we assume we scan from block zero upwards */
283282
scan = heap_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false);
284283

285284
nblocks = scan->rs_nblocks; /* # blocks to be scanned */
286285

287-
/* prepare access strategy for this table */
288-
bstrategy = GetAccessStrategy(BAS_BULKREAD);
289-
scan->rs_strategy = bstrategy;
290-
291286
/* scan the relation */
292287
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
293288
{
@@ -321,26 +316,28 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
321316
{
322317
CHECK_FOR_INTERRUPTS();
323318

324-
buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block, RBM_NORMAL, bstrategy);
319+
buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
320+
RBM_NORMAL, scan->rs_strategy);
325321
LockBuffer(buffer, BUFFER_LOCK_SHARE);
326322
stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
327323
UnlockReleaseBuffer(buffer);
328324
block++;
329325
}
330326
}
331-
heap_endscan(scan);
332327

333328
while (block < nblocks)
334329
{
335330
CHECK_FOR_INTERRUPTS();
336331

337-
buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block, RBM_NORMAL, bstrategy);
332+
buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
333+
RBM_NORMAL, scan->rs_strategy);
338334
LockBuffer(buffer, BUFFER_LOCK_SHARE);
339335
stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
340336
UnlockReleaseBuffer(buffer);
341337
block++;
342338
}
343339

340+
heap_endscan(scan);
344341
relation_close(rel, AccessShareLock);
345342

346343
stat.table_len = (uint64) nblocks *BLCKSZ;

0 commit comments

Comments
 (0)