]> granicus.if.org Git - postgresql/commitdiff
Prefetch blocks during lazy vacuum's truncation scan
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 23 Jan 2017 15:55:18 +0000 (12:55 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 23 Jan 2017 15:55:18 +0000 (12:55 -0300)
Vacuum truncation scan can be sped up on rotating media by prefetching
blocks in forward direction.  That makes the blocks already present in
memory by the time they are needed, while also letting OS read-ahead
kick in.

The truncate scan has been measured to be five times faster than without
this patch (that was on a slow disk, but it shouldn't hurt on fast
disks.)

Author: Álvaro Herrera, loosely based on a submission by Claudio Freire
Discussion: https://postgr.es/m/CAGTBQpa6NFGO_6g_y_7zQx8L9GcHDSQKYdo1tGuh791z6PYgEg@mail.gmail.com

src/backend/commands/vacuumlazy.c

index a2999b3bf7767312960664a77f2ecffe4284f98b..005440eb39279ea6ee1e5da953a0306d34bea45c 100644 (file)
  */
 #define SKIP_PAGES_THRESHOLD   ((BlockNumber) 32)
 
+/*
+ * Size of the prefetch window for lazy vacuum backwards truncation scan.
+ * Needs to be a power of 2.
+ */
+#define PREFETCH_SIZE                  ((BlockNumber) 32)
+
 typedef struct LVRelStats
 {
        /* hasindex = true means two-pass strategy; false means one-pass */
@@ -1826,13 +1832,22 @@ static BlockNumber
 count_nondeletable_pages(Relation onerel, LVRelStats *vacrelstats)
 {
        BlockNumber blkno;
+       BlockNumber prefetchedUntil;
        instr_time      starttime;
 
        /* Initialize the starttime if we check for conflicting lock requests */
        INSTR_TIME_SET_CURRENT(starttime);
 
-       /* Strange coding of loop control is needed because blkno is unsigned */
+       /*
+        * Start checking blocks at what we believe relation end to be and move
+        * backwards.  (Strange coding of loop control is needed because blkno is
+        * unsigned.)  To make the scan faster, we prefetch a few blocks at a time
+        * in forward direction, so that OS-level readahead can kick in.
+        */
        blkno = vacrelstats->rel_pages;
+       StaticAssertStmt((PREFETCH_SIZE & (PREFETCH_SIZE - 1)) == 0,
+                                        "prefetch size must be power of 2");
+       prefetchedUntil = InvalidBlockNumber;
        while (blkno > vacrelstats->nonempty_pages)
        {
                Buffer          buf;
@@ -1882,6 +1897,21 @@ count_nondeletable_pages(Relation onerel, LVRelStats *vacrelstats)
 
                blkno--;
 
+               /* If we haven't prefetched this lot yet, do so now. */
+               if (prefetchedUntil > blkno)
+               {
+                       BlockNumber     prefetchStart;
+                       BlockNumber     pblkno;
+
+                       prefetchStart = blkno & ~(PREFETCH_SIZE - 1);
+                       for (pblkno = prefetchStart; pblkno <= blkno; pblkno++)
+                       {
+                               PrefetchBuffer(onerel, MAIN_FORKNUM, pblkno);
+                               CHECK_FOR_INTERRUPTS();
+                       }
+                       prefetchedUntil = prefetchStart;
+               }
+
                buf = ReadBufferExtended(onerel, MAIN_FORKNUM, blkno,
                                                                 RBM_NORMAL, vac_strategy);