aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Rowley <drowley@postgresql.org>2021-01-25 19:53:27 +1300
committerDavid Rowley <drowley@postgresql.org>2021-01-25 19:53:27 +1300
commit06cdfe21d3233e3010d5332d60f27886c28b1a25 (patch)
treef64f4bcca0baa6f9c4958470937ea6d879107fbb
parent5db6ba3034bfe03df860d1a26e40e3ed63ae47b9 (diff)
downloadpostgresql-06cdfe21d3233e3010d5332d60f27886c28b1a25.tar.gz
postgresql-06cdfe21d3233e3010d5332d60f27886c28b1a25.zip
Fix hypothetical bug in heap backward scans
Both heapgettup() and heapgettup_pagemode() incorrectly set the first page to scan in a backward scan in which the number of pages to scan was specified by heap_setscanlimits(). The code incorrectly started the scan at the end of the relation when startBlk was 0, or otherwise at startBlk - 1, neither of which is correct when only scanning a subset of pages. The fix here checks if heap_setscanlimits() has changed the number of pages to scan and if so we set the first page to scan as the final page in the specified range during backward scans. Proper adjustment of this code was forgotten when heap_setscanlimits() was added in 7516f5259 back in 9.5. However, practice, nowhere in core code performs backward scans after having used heap_setscanlimits(), yet, it is possible an extension uses the heap functions in this way, hence backpatch. An upcoming patch does use heap_setscanlimits() with backward scans, so this must be fixed before that can go in. Author: David Rowley Discussion: https://postgr.es/m/CAApHDvpGc9h0_oVD2CtgBcxCS1N-qDYZSeBRnUh+0CWJA9cMaA@mail.gmail.com Backpatch-through: 9.5, all supported versions
-rw-r--r--src/backend/access/heap/heapam.c20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 1e2536c70e7..86a2b8b57d8 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -582,8 +582,14 @@ heapgettup(HeapScanDesc scan,
* forward scanners.
*/
scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC;
- /* start from last page of the scan */
- if (scan->rs_startblock > 0)
+
+ /*
+ * Start from last page of the scan. Ensure we take into account
+ * rs_numblocks if it's been adjusted by heap_setscanlimits().
+ */
+ if (scan->rs_numblocks != InvalidBlockNumber)
+ page = (scan->rs_startblock + scan->rs_numblocks - 1) % scan->rs_nblocks;
+ else if (scan->rs_startblock > 0)
page = scan->rs_startblock - 1;
else
page = scan->rs_nblocks - 1;
@@ -893,8 +899,14 @@ heapgettup_pagemode(HeapScanDesc scan,
* forward scanners.
*/
scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC;
- /* start from last page of the scan */
- if (scan->rs_startblock > 0)
+
+ /*
+ * Start from last page of the scan. Ensure we take into account
+ * rs_numblocks if it's been adjusted by heap_setscanlimits().
+ */
+ if (scan->rs_numblocks != InvalidBlockNumber)
+ page = (scan->rs_startblock + scan->rs_numblocks - 1) % scan->rs_nblocks;
+ else if (scan->rs_startblock > 0)
page = scan->rs_startblock - 1;
else
page = scan->rs_nblocks - 1;