diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index c1a4de14a59e..00d4ecf804f0 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -483,13 +483,17 @@ page_collect_tuples(HeapScanDesc scan, Snapshot snapshot, BlockNumber block, int lines, bool all_visible, bool check_serializable) { + HeapTupleData loctup; int ntup = 0; OffsetNumber lineoff; + /* block and tableOid is the same for all tuples, set it once outside the loop */ + ItemPointerSetBlockNumber(&loctup.t_self, block); + loctup.t_tableOid = RelationGetRelid(scan->rs_base.rs_rd); + for (lineoff = FirstOffsetNumber; lineoff <= lines; lineoff++) { ItemId lpp = PageGetItemId(page, lineoff); - HeapTupleData loctup; bool valid; if (!ItemIdIsNormal(lpp)) @@ -497,8 +501,7 @@ page_collect_tuples(HeapScanDesc scan, Snapshot snapshot, loctup.t_data = (HeapTupleHeader) PageGetItem(page, lpp); loctup.t_len = ItemIdGetLength(lpp); - loctup.t_tableOid = RelationGetRelid(scan->rs_base.rs_rd); - ItemPointerSet(&(loctup.t_self), block, lineoff); + ItemPointerSetOffsetNumber(&loctup.t_self, lineoff); if (all_visible) valid = true; @@ -906,6 +909,10 @@ heapgettup(HeapScanDesc scan, LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE); page = heapgettup_start_page(scan, dir, &linesleft, &lineoff); + + /* block is the same for all tuples, set it once outside the loop */ + ItemPointerSetBlockNumber(&tuple->t_self, scan->rs_cblock); + continue_page: /* @@ -925,7 +932,7 @@ heapgettup(HeapScanDesc scan, tuple->t_data = (HeapTupleHeader) PageGetItem(page, lpp); tuple->t_len = ItemIdGetLength(lpp); - ItemPointerSet(&(tuple->t_self), scan->rs_cblock, lineoff); + ItemPointerSetOffsetNumber(&tuple->t_self, lineoff); visible = HeapTupleSatisfiesVisibility(tuple, scan->rs_base.rs_snapshot, @@ -1716,6 +1723,10 @@ heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer, Assert(TransactionIdIsValid(RecentXmin)); Assert(BufferGetBlockNumber(buffer) == blkno); + /* block and tableOid is the same for all tuples, set it once outside the loop */ + ItemPointerSetBlockNumber(&heapTuple->t_self, blkno); + heapTuple->t_tableOid = RelationGetRelid(relation); + /* Scan through possible multiple members of HOT-chain */ for (;;) { @@ -1750,8 +1761,7 @@ heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer, */ heapTuple->t_data = (HeapTupleHeader) PageGetItem(page, lp); heapTuple->t_len = ItemIdGetLength(lp); - heapTuple->t_tableOid = RelationGetRelid(relation); - ItemPointerSet(&heapTuple->t_self, blkno, offnum); + ItemPointerSetOffsetNumber(&heapTuple->t_self, offnum); /* * Shouldn't see a HEAP_ONLY tuple at chain start. diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index ac082fefa77a..d03d15ce6582 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -1031,6 +1031,7 @@ heapam_scan_analyze_next_tuple(TableScanDesc scan, TransactionId OldestXmin, TupleTableSlot *slot) { HeapScanDesc hscan = (HeapScanDesc) scan; + HeapTuple targtuple; Page targpage; OffsetNumber maxoffset; BufferHeapTupleTableSlot *hslot; @@ -1038,14 +1039,18 @@ heapam_scan_analyze_next_tuple(TableScanDesc scan, TransactionId OldestXmin, Assert(TTS_IS_BUFFERTUPLE(slot)); hslot = (BufferHeapTupleTableSlot *) slot; + targtuple = &hslot->base.tupdata; targpage = BufferGetPage(hscan->rs_cbuf); maxoffset = PageGetMaxOffsetNumber(targpage); + /* block and tableOid is the same for all tuples, set it once outside the loop */ + ItemPointerSetBlockNumber(&targtuple->t_self, hscan->rs_cblock); + targtuple->t_tableOid = RelationGetRelid(scan->rs_rd); + /* Inner loop over all tuples on the selected page */ for (; hscan->rs_cindex <= maxoffset; hscan->rs_cindex++) { ItemId itemid; - HeapTuple targtuple = &hslot->base.tupdata; bool sample_it = false; itemid = PageGetItemId(targpage, hscan->rs_cindex); @@ -1063,11 +1068,9 @@ heapam_scan_analyze_next_tuple(TableScanDesc scan, TransactionId OldestXmin, continue; } - ItemPointerSet(&targtuple->t_self, hscan->rs_cblock, hscan->rs_cindex); - - targtuple->t_tableOid = RelationGetRelid(scan->rs_rd); targtuple->t_data = (HeapTupleHeader) PageGetItem(targpage, itemid); targtuple->t_len = ItemIdGetLength(itemid); + ItemPointerSetOffsetNumber(&targtuple->t_self, hscan->rs_cindex); switch (HeapTupleSatisfiesVacuum(targtuple, OldestXmin, hscan->rs_cbuf)) @@ -2265,6 +2268,7 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, TupleTableSlot *slot) { HeapScanDesc hscan = (HeapScanDesc) scan; + HeapTuple tuple = &(hscan->rs_ctup); TsmRoutine *tsm = scanstate->tsmroutine; BlockNumber blockno = hscan->rs_cblock; bool pagemode = (scan->rs_flags & SO_ALLOW_PAGEMODE) != 0; @@ -2285,6 +2289,9 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, !scan->rs_snapshot->takenDuringRecovery; maxoffset = PageGetMaxOffsetNumber(page); + /* block is the same for all tuples, set it once outside the loop */ + ItemPointerSetBlockNumber(&tuple->t_self, blockno); + for (;;) { OffsetNumber tupoffset; @@ -2300,7 +2307,6 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, { ItemId itemid; bool visible; - HeapTuple tuple = &(hscan->rs_ctup); /* Skip invalid tuple pointers. */ itemid = PageGetItemId(page, tupoffset); @@ -2309,8 +2315,7 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, tuple->t_data = (HeapTupleHeader) PageGetItem(page, itemid); tuple->t_len = ItemIdGetLength(itemid); - ItemPointerSet(&(tuple->t_self), blockno, tupoffset); - + ItemPointerSetOffsetNumber(&tuple->t_self, blockno); if (all_visible) visible = true; @@ -2536,18 +2541,21 @@ BitmapHeapScanNextBlock(TableScanDesc scan, * tbmres; but we have to follow any HOT chain starting at each such * offset. */ + ItemPointerData tid; int curslot; /* We must have extracted the tuple offsets by now */ Assert(noffsets > -1); + /* block is the same for all tuples, set it once outside the loop */ + ItemPointerSetBlockNumber(&tid, block); + for (curslot = 0; curslot < noffsets; curslot++) { OffsetNumber offnum = offsets[curslot]; - ItemPointerData tid; HeapTupleData heapTuple; - ItemPointerSet(&tid, block, offnum); + ItemPointerSetOffsetNumber(&tid, offnum); if (heap_hot_search_buffer(&tid, scan->rs_rd, buffer, snapshot, &heapTuple, NULL, true)) hscan->rs_vistuples[ntup++] = ItemPointerGetOffsetNumber(&tid); @@ -2562,11 +2570,15 @@ BitmapHeapScanNextBlock(TableScanDesc scan, Page page = BufferGetPage(buffer); OffsetNumber maxoff = PageGetMaxOffsetNumber(page); OffsetNumber offnum; + HeapTupleData loctup; + + /* block and tableOid is the same for all tuples, set it once outside the loop */ + ItemPointerSetBlockNumber(&loctup.t_self, block); + loctup.t_tableOid = scan->rs_rd->rd_id; for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum)) { ItemId lp; - HeapTupleData loctup; bool valid; lp = PageGetItemId(page, offnum); @@ -2574,8 +2586,8 @@ BitmapHeapScanNextBlock(TableScanDesc scan, continue; loctup.t_data = (HeapTupleHeader) PageGetItem(page, lp); loctup.t_len = ItemIdGetLength(lp); - loctup.t_tableOid = scan->rs_rd->rd_id; - ItemPointerSet(&loctup.t_self, block, offnum); + ItemPointerSetOffsetNumber(&loctup.t_self, offnum); + valid = HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer); if (valid) {