From 20bff0fbdb4da235b9a3e7d2cefbfd45e3bdabc0 Mon Sep 17 00:00:00 2001 From: Edmund Horner Date: Fri, 12 Oct 2018 16:28:58 +1300 Subject: [PATCH 3/5] Support backward scans over restricted ranges in heap access method This is required for backward Tid scans. --- src/backend/access/heap/heapam.c | 48 +++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 9650145..dbe6045 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -575,11 +575,27 @@ heapgettup(HeapScanDesc scan, * forward scanners. */ scan->rs_syncscan = false; - /* start from last page of the scan */ - if (scan->rs_startblock > 0) - page = scan->rs_startblock - 1; + + /* + * When scanning the whole relation, start from the last page of + * the scan. + */ + if (scan->rs_numblocks == InvalidBlockNumber) + { + if (scan->rs_startblock > 0) + page = scan->rs_startblock - 1; + else + page = scan->rs_nblocks - 1; + } else - page = scan->rs_nblocks - 1; + { + /* + * Otherwise, if scanning just a subset of the relation, start + * at the final block in the range. + */ + page = scan->rs_startblock + scan->rs_numblocks - 1; + } + heapgetpage(scan, page); } else @@ -876,11 +892,27 @@ heapgettup_pagemode(HeapScanDesc scan, * forward scanners. */ scan->rs_syncscan = false; - /* start from last page of the scan */ - if (scan->rs_startblock > 0) - page = scan->rs_startblock - 1; + + /* + * When scanning the whole relation, start from the last page of + * the scan. + */ + if (scan->rs_numblocks == InvalidBlockNumber) + { + if (scan->rs_startblock > 0) + page = scan->rs_startblock - 1; + else + page = scan->rs_nblocks - 1; + } else - page = scan->rs_nblocks - 1; + { + /* + * Otherwise, if scanning just a subset of the relation, start + * at the final block in the range. + */ + page = scan->rs_startblock + scan->rs_numblocks - 1; + } + heapgetpage(scan, page); } else -- 2.7.4