diff --git a/config/c-library.m4 b/config/c-library.m4
index 8f45010..9aee904 100644
--- a/config/c-library.m4
+++ b/config/c-library.m4
@@ -367,3 +367,50 @@ if test "$pgac_cv_type_locale_t" = 'yes (in xlocale.h)'; then
   AC_DEFINE(LOCALE_T_IN_XLOCALE, 1,
             [Define to 1 if `locale_t' requires <xlocale.h>.])
 fi])])# PGAC_HEADER_XLOCALE
+
+
+# PGAC_FUNC_AIO_ATOMIC_BUILTIN_COMP_SWAP
+# ---------------------------------------
+# test whether this system has both the librt-style async io and the gcc atomic compare_and_swap
+#      and test operation  of the latter.
+#
+AC_DEFUN([PGAC_FUNC_AIO_ATOMIC_BUILTIN_COMP_SWAP],
+[AC_MSG_CHECKING([whether have both librt-style async io and the gcc atomic compare_and_swap])
+AC_CACHE_VAL(pgac_cv_aio_atomic_builtin_comp_swap,
+pgac_save_LIBS=$LIBS
+LIBS=" -lrt $pgac_save_LIBS"
+[AC_TRY_RUN([#include <stdio.h>
+#include <unistd.h>
+#include "aio.h"
+
+int main (int argc, char *argv[])
+ {
+   int rc;
+   struct aiocb volatile * first_aiocb;
+   struct aiocb volatile * second_aiocb;
+   struct aiocb volatile * my_aiocbp = (struct aiocb *)20000008;
+
+   first_aiocb = (struct aiocb *)20000008;
+   second_aiocb = (struct aiocb *)40000008;
+
+   /*  return zero as success if two comp-swaps both worked as expected -
+   **  first compares equal and swaps,  second compares unequal
+   */
+   rc = (__sync_bool_compare_and_swap (&my_aiocbp, first_aiocb, second_aiocb));
+   if (rc) {
+      rc = (__sync_bool_compare_and_swap (&my_aiocbp, first_aiocb, second_aiocb));
+   } else {
+      rc = -1;
+   }
+
+   return rc;
+}],
+[pgac_cv_aio_atomic_builtin_comp_swap=yes],
+[pgac_cv_aio_atomic_builtin_comp_swap=no],
+[pgac_cv_aio_atomic_builtin_comp_swap=cross])
+])dnl AC_CACHE_VAL
+AC_MSG_RESULT([$pgac_cv_aio_atomic_builtin_comp_swap])
+if test x"$pgac_cv_aio_atomic_builtin_comp_swap" != x"yes"; then
+LIBS=$pgac_save_LIBS
+fi
+])# PGAC_FUNC_AIO_ATOMIC_BUILTIN_COMP_SWAP
diff --git a/configure b/configure
index 3663e50..5ad28a1 100755
--- a/configure
+++ b/configure
@@ -13840,6 +13840,69 @@ operating system;  use --disable-thread-safety to disable thread safety." "$LINE
 fi
 fi
 
+#  test whether this system has both the librt-style async io and the gcc atomic compare_and_swap
+#      and test operation  of the latter.
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether have both librt-style async io and the gcc atomic compare_and_swap" >&5
+$as_echo_n "checking whether have both librt-style async io and the gcc atomic compare_and_swap... " >&6; }
+if ${pgac_cv_aio_atomic_builtin_comp_swap+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  pgac_save_LIBS=$LIBS
+LIBS=" -lrt $pgac_save_LIBS"
+if test "$cross_compiling" = yes; then :
+  pgac_cv_aio_atomic_builtin_comp_swap=cross
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <stdio.h>
+#include <unistd.h>
+#include "aio.h"
+
+int main (int argc, char *argv[])
+ {
+   int rc;
+   struct aiocb volatile * first_aiocb;
+   struct aiocb volatile * second_aiocb;
+   struct aiocb volatile * my_aiocbp = (struct aiocb *)20000008;
+
+   first_aiocb = (struct aiocb *)20000008;
+   second_aiocb = (struct aiocb *)40000008;
+
+   /*  return zero as success if two comp-swaps both worked as expected -
+   **  first compares equal and swaps,  second compares unequal
+   */
+   rc = (__sync_bool_compare_and_swap (&my_aiocbp, first_aiocb, second_aiocb));
+   if (rc) {
+      rc = (__sync_bool_compare_and_swap (&my_aiocbp, first_aiocb, second_aiocb));
+   } else {
+      rc = -1;
+   }
+
+   return rc;
+}
+_ACEOF
+if ac_fn_c_try_run "$LINENO"; then :
+  pgac_cv_aio_atomic_builtin_comp_swap=yes
+else
+  pgac_cv_aio_atomic_builtin_comp_swap=no
+fi
+rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \
+  conftest.$ac_objext conftest.beam conftest.$ac_ext
+fi
+
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_aio_atomic_builtin_comp_swap" >&5
+$as_echo "$pgac_cv_aio_atomic_builtin_comp_swap" >&6; }
+if test x"$pgac_cv_aio_atomic_builtin_comp_swap" != x"yes"; then
+LIBS=$pgac_save_LIBS
+fi
+
+if test x"$pgac_cv_aio_atomic_builtin_comp_swap" = x"yes"; then
+
+$as_echo "#define USE_AIO_ATOMIC_BUILTIN_COMP_SWAP 1" >>confdefs.h
+
+fi
 
 # Select semaphore implementation type.
 if test "$PORTNAME" != "win32"; then
diff --git a/configure.in b/configure.in
index 80df1d7..b0876bd 100644
--- a/configure.in
+++ b/configure.in
@@ -1771,6 +1771,12 @@ operating system;  use --disable-thread-safety to disable thread safety.])
 fi
 fi
 
+#  test whether this system has both the librt-style async io and the gcc atomic compare_and_swap
+#      and test operation  of the latter.
+PGAC_FUNC_AIO_ATOMIC_BUILTIN_COMP_SWAP
+if test x"$pgac_cv_aio_atomic_builtin_comp_swap" = x"yes"; then
+      AC_DEFINE(USE_AIO_ATOMIC_BUILTIN_COMP_SWAP, 1, [Define to select librt-style async io and the gcc atomic compare_and_swap.])
+fi
 
 # Select semaphore implementation type.
 if test "$PORTNAME" != "win32"; then
diff --git a/contrib/pg_prewarm/pg_prewarm.c b/contrib/pg_prewarm/pg_prewarm.c
index df20e88..b52772f 100644
--- a/contrib/pg_prewarm/pg_prewarm.c
+++ b/contrib/pg_prewarm/pg_prewarm.c
@@ -159,7 +159,7 @@ pg_prewarm(PG_FUNCTION_ARGS)
 		 */
 		for (block = first_block; block <= last_block; ++block)
 		{
-			PrefetchBuffer(rel, forkNumber, block);
+			PrefetchBuffer(rel, forkNumber, block, 0);
 			++blocks_done;
 		}
 #else
diff --git a/contrib/pg_stat_statements/Makefile b/contrib/pg_stat_statements/Makefile
index 95a2767..ae52fa5 100644
--- a/contrib/pg_stat_statements/Makefile
+++ b/contrib/pg_stat_statements/Makefile
@@ -4,7 +4,8 @@ MODULE_big = pg_stat_statements
 OBJS = pg_stat_statements.o
 
 EXTENSION = pg_stat_statements
-DATA = pg_stat_statements--1.2.sql pg_stat_statements--1.1--1.2.sql \
+DATA = pg_stat_statements--1.3.sql pg_stat_statements--1.2--1.3.sql \
+	pg_stat_statements--1.2.sql pg_stat_statements--1.1--1.2.sql \
 	pg_stat_statements--1.0--1.1.sql pg_stat_statements--unpackaged--1.0.sql
 
 ifdef USE_PGXS
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 32d16cc..b4bfb5a 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -117,6 +117,7 @@ typedef enum pgssVersion
 	PGSS_V1_0 = 0,
 	PGSS_V1_1,
 	PGSS_V1_2
+	,PGSS_V1_3
 } pgssVersion;
 
 /*
@@ -148,6 +149,16 @@ typedef struct Counters
 	int64		local_blks_written;		/* # of local disk blocks written */
 	int64		temp_blks_read; /* # of temp blocks read */
 	int64		temp_blks_written;		/* # of temp blocks written */
+
+	int64		aio_read_noneed;		/* # of prefetches for which no need for prefetch as block already in buffer pool  */
+	int64		aio_read_discrd;		/* # of prefetches for which buffer not subsequently read and therefore discarded  */
+	int64		aio_read_forgot;		/* # of prefetches for which buffer not subsequently read and then forgotten about */
+	int64		aio_read_noblok;		/* # of prefetches for which no available BufferAiocb  control block               */
+	int64		aio_read_failed;		/* # of aio reads for which aio itself failed or the read failed with an errno     */
+	int64		aio_read_wasted;		/* # of aio reads for which in-progress aio cancelled and disk block not used      */
+	int64		aio_read_waited;		/* # of aio reads for which disk block used but had to wait for it                 */
+	int64		aio_read_ontime;		/* # of aio reads for which disk block used and ready on time when requested       */
+
 	double		blk_read_time;	/* time spent reading, in msec */
 	double		blk_write_time; /* time spent writing, in msec */
 	double		usage;			/* usage factor */
@@ -274,7 +285,7 @@ void		_PG_init(void);
 void		_PG_fini(void);
 
 PG_FUNCTION_INFO_V1(pg_stat_statements_reset);
-PG_FUNCTION_INFO_V1(pg_stat_statements_1_2);
+PG_FUNCTION_INFO_V1(pg_stat_statements_1_3);
 PG_FUNCTION_INFO_V1(pg_stat_statements);
 
 static void pgss_shmem_startup(void);
@@ -1026,7 +1037,25 @@ pgss_ProcessUtility(Node *parsetree, const char *queryString,
 		bufusage.temp_blks_read =
 			pgBufferUsage.temp_blks_read - bufusage_start.temp_blks_read;
 		bufusage.temp_blks_written =
-			pgBufferUsage.temp_blks_written - bufusage_start.temp_blks_written;
+			pgBufferUsage.temp_blks_written - bufusage.temp_blks_written;
+
+		bufusage.aio_read_noneed =
+			pgBufferUsage.aio_read_noneed - bufusage.aio_read_noneed;
+		bufusage.aio_read_discrd =
+			pgBufferUsage.aio_read_discrd - bufusage.aio_read_discrd;
+		bufusage.aio_read_forgot =
+			pgBufferUsage.aio_read_forgot - bufusage.aio_read_forgot;
+		bufusage.aio_read_noblok =
+			pgBufferUsage.aio_read_noblok - bufusage.aio_read_noblok;
+		bufusage.aio_read_failed =
+			pgBufferUsage.aio_read_failed - bufusage.aio_read_failed;
+		bufusage.aio_read_wasted =
+			pgBufferUsage.aio_read_wasted - bufusage.aio_read_wasted;
+		bufusage.aio_read_waited =
+			pgBufferUsage.aio_read_waited - bufusage.aio_read_waited;
+		bufusage.aio_read_ontime =
+			pgBufferUsage.aio_read_ontime - bufusage.aio_read_ontime;
+
 		bufusage.blk_read_time = pgBufferUsage.blk_read_time;
 		INSTR_TIME_SUBTRACT(bufusage.blk_read_time, bufusage_start.blk_read_time);
 		bufusage.blk_write_time = pgBufferUsage.blk_write_time;
@@ -1041,6 +1070,7 @@ pgss_ProcessUtility(Node *parsetree, const char *queryString,
 				   rows,
 				   &bufusage,
 				   NULL);
+
 	}
 	else
 	{
@@ -1224,6 +1254,16 @@ pgss_store(const char *query, uint32 queryId,
 		e->counters.local_blks_written += bufusage->local_blks_written;
 		e->counters.temp_blks_read += bufusage->temp_blks_read;
 		e->counters.temp_blks_written += bufusage->temp_blks_written;
+
+		e->counters.aio_read_noneed     += bufusage->aio_read_noneed;
+		e->counters.aio_read_discrd     += bufusage->aio_read_discrd;
+		e->counters.aio_read_forgot     += bufusage->aio_read_forgot;
+		e->counters.aio_read_noblok     += bufusage->aio_read_noblok;
+		e->counters.aio_read_failed     += bufusage->aio_read_failed;
+		e->counters.aio_read_wasted     += bufusage->aio_read_wasted;
+		e->counters.aio_read_waited     += bufusage->aio_read_waited;
+		e->counters.aio_read_ontime     += bufusage->aio_read_ontime;
+
 		e->counters.blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->blk_read_time);
 		e->counters.blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->blk_write_time);
 		e->counters.usage += USAGE_EXEC(total_time);
@@ -1257,7 +1297,8 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS)
 #define PG_STAT_STATEMENTS_COLS_V1_0	14
 #define PG_STAT_STATEMENTS_COLS_V1_1	18
 #define PG_STAT_STATEMENTS_COLS_V1_2	19
-#define PG_STAT_STATEMENTS_COLS			19		/* maximum of above */
+#define PG_STAT_STATEMENTS_COLS_V1_3	27
+#define PG_STAT_STATEMENTS_COLS			27		/* maximum of above */
 
 /*
  * Retrieve statement statistics.
@@ -1270,6 +1311,16 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS)
  * function.  Unfortunately we weren't bright enough to do that for 1.1.
  */
 Datum
+pg_stat_statements_1_3(PG_FUNCTION_ARGS)
+{
+	bool		showtext = PG_GETARG_BOOL(0);
+
+	pg_stat_statements_internal(fcinfo, PGSS_V1_3, showtext);
+
+	return (Datum) 0;
+}
+
+Datum
 pg_stat_statements_1_2(PG_FUNCTION_ARGS)
 {
 	bool		showtext = PG_GETARG_BOOL(0);
@@ -1358,6 +1409,10 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
 			if (api_version != PGSS_V1_2)
 				elog(ERROR, "incorrect number of output arguments");
 			break;
+		case PG_STAT_STATEMENTS_COLS_V1_3:
+			if (api_version != PGSS_V1_3)
+				elog(ERROR, "incorrect number of output arguments");
+			break;
 		default:
 			elog(ERROR, "incorrect number of output arguments");
 	}
@@ -1534,11 +1589,24 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
 		{
 			values[i++] = Float8GetDatumFast(tmp.blk_read_time);
 			values[i++] = Float8GetDatumFast(tmp.blk_write_time);
+
+			if (api_version >= PGSS_V1_3)
+			{
+				values[i++] = Int64GetDatumFast(tmp.aio_read_noneed);
+				values[i++] = Int64GetDatumFast(tmp.aio_read_discrd);
+				values[i++] = Int64GetDatumFast(tmp.aio_read_forgot);
+				values[i++] = Int64GetDatumFast(tmp.aio_read_noblok);
+				values[i++] = Int64GetDatumFast(tmp.aio_read_failed);
+				values[i++] = Int64GetDatumFast(tmp.aio_read_wasted);
+				values[i++] = Int64GetDatumFast(tmp.aio_read_waited);
+				values[i++] = Int64GetDatumFast(tmp.aio_read_ontime);
+			}
 		}
 
 		Assert(i == (api_version == PGSS_V1_0 ? PG_STAT_STATEMENTS_COLS_V1_0 :
 					 api_version == PGSS_V1_1 ? PG_STAT_STATEMENTS_COLS_V1_1 :
 					 api_version == PGSS_V1_2 ? PG_STAT_STATEMENTS_COLS_V1_2 :
+					 api_version == PGSS_V1_3 ? PG_STAT_STATEMENTS_COLS_V1_3 :
 					 -1 /* fail if you forget to update this assert */ ));
 
 		tuplestore_putvalues(tupstore, tupdesc, values, nulls);
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index b77c32c..1d43917 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -71,6 +71,28 @@
 #include "utils/syscache.h"
 #include "utils/tqual.h"
 
+#ifdef USE_PREFETCH
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "executor/instrument.h"
+
+extern unsigned int prefetch_dbOid; /*  database oid of relations on which prefetching to be done - 0 means all */
+extern unsigned int prefetch_heap_scans; /*  boolean whether to prefetch non-bitmap heap scans         */
+
+/*  special values for scan->rs_prefetch_target indicating as follows :               */
+#define PREFETCH_MAYBE 0xffffffff      /*   prefetch permitted but not yet in effect  */
+#define PREFETCH_DISABLED 0xfffffffe   /*   prefetch disabled and not permitted       */
+/*  PREFETCH_WRAP_POINT indicates a pretcher who has reached the point where the scan would wrap -
+**  at this point the prefetcher runs on the spot until scan catches up.
+**  This *must* be < maximum valid setting of target_prefetch_pages aka effective_io_concurrency.
+*/
+#define PREFETCH_WRAP_POINT 0x0fffffff
+
+#endif   /* USE_PREFETCH */
+
 
 /* GUC variable */
 bool		synchronize_seqscans = true;
@@ -115,6 +137,8 @@ static XLogRecPtr log_heap_new_cid(Relation relation, HeapTuple tup);
 static HeapTuple ExtractReplicaIdentity(Relation rel, HeapTuple tup, bool key_modified,
 					   bool *copy);
 
+static void heap_unread_add(HeapScanDesc scan, BlockNumber blockno);
+static void heap_unread_subtract(HeapScanDesc scan, BlockNumber blockno);
 
 /*
  * Each tuple lock mode has a corresponding heavyweight lock, and one or two
@@ -292,10 +316,149 @@ initscan(HeapScanDesc scan, ScanKey key, bool is_rescan)
 	 * Currently, we don't have a stats counter for bitmap heap scans (but the
 	 * underlying bitmap index scans will be counted).
 	 */
-	if (!scan->rs_bitmapscan)
+#ifdef USE_PREFETCH
+        /*    by default,  no prefetching on any scan  */
+        scan->rs_prefetch_target = PREFETCH_DISABLED;  /*  tentatively disable  */
+        scan->rs_pfchblock = 0; /*  scanner will reset this to be ahead of scan */
+        scan->rs_Unread_Pfetched_base = (BlockNumber *)0; /*  list of prefetched but unread blocknos */
+        scan->rs_Unread_Pfetched_next = 0; /*  next unread blockno */
+        scan->rs_Unread_Pfetched_count = 0; /* number of valid unread blocknos */
+#endif   /* USE_PREFETCH */
+	if (!scan->rs_bitmapscan) {
+
 		pgstat_count_heap_scan(scan->rs_rd);
+#ifdef USE_PREFETCH
+                /*    bitmap scans do their own prefetching -
+                **    for others,  set up prefetching now
+                */
+                if (    prefetch_heap_scans
+                     && (target_prefetch_pages > 0)
+                     &&	(!RelationUsesLocalBuffers(scan->rs_rd))
+                   ) {
+                      /*   prefetch_dbOid may be set to a database Oid to specify only prefetch in that db */
+                      if (    (   (prefetch_dbOid > 0)
+                               && (prefetch_dbOid == scan->rs_rd->rd_node.dbNode)
+                              )
+                          ||  (prefetch_dbOid == 0)
+                         ) {
+                          scan->rs_prefetch_target = PREFETCH_MAYBE;    /*  permitted but let the scan decide */
+                      }
+                      else {
+                      }
+                }
+#endif   /* USE_PREFETCH */
+        }
+}
+
+/* add this blockno to list of prefetched and unread blocknos
+** use the one identified by the (next+count|modulo circumference) index if it is unused,
+** else search for the first available slot if there is one,
+** else error.
+*/
+static void
+heap_unread_add(HeapScanDesc scan, BlockNumber blockno)
+{
+      BlockNumber *available_P;   /*  where to store new blockno */
+      unsigned int Unread_Pfetched_index = scan->rs_Unread_Pfetched_next
+                                         + scan->rs_Unread_Pfetched_count; /* index of next unused slot */
+
+      /*  caller is not supposed to pass InvalidBlockNumber but check anyway */
+      if (blockno != InvalidBlockNumber) {
+
+		  /*  ensure there is some room somewhere   */
+		  if (scan->rs_Unread_Pfetched_count < target_prefetch_pages) {
+
+			  /*  try the "next+count" one */
+			  if (Unread_Pfetched_index >= target_prefetch_pages) {
+				  Unread_Pfetched_index -= target_prefetch_pages;  /* modulo circumference */
+			  }
+			  available_P = (scan->rs_Unread_Pfetched_base + Unread_Pfetched_index);   /*  where to store new blockno */
+			  if (*available_P == InvalidBlockNumber) { /* unused */
+				  goto store_blockno;
+			  } else {
+                  /*  slow-search the entire list  */
+				  for (Unread_Pfetched_index = 0; Unread_Pfetched_index < target_prefetch_pages; Unread_Pfetched_index++) {
+					  available_P = (scan->rs_Unread_Pfetched_base + Unread_Pfetched_index);   /*  where to store new blockno */
+					  if (*available_P == InvalidBlockNumber) { /* unused */
+                          /*  before storing this blockno,
+                          **  since the next pointer did not locate an unused slot,
+                          **  set it to one which is more likely to be so for the next time
+                          */
+                          scan->rs_Unread_Pfetched_next = Unread_Pfetched_index;
+						  goto store_blockno;
+					  }
+				  }
+			  }
+		  }
+
+          /*  if we reach here,    either there was no available slot
+          **  or we thought there was one and didn't find any  --
+          */
+  		  ereport(ERROR,
+			  (errcode(ERRCODE_INTERNAL_ERROR),
+                                errmsg("heap_unread_add overflowed list cannot add blockno %d", blockno)));
+
+  		  return;
+      }
+
+    store_blockno:
+      *available_P = blockno;
+      scan->rs_Unread_Pfetched_count++;  /*  update count */
+
+    return;
+}
+
+/* remove specified blockno from list of prefetched and unread blocknos.
+/* Usually this will be found at the rs_Unread_Pfetched_next item -
+** else search for it.    If not found,   inore it  -  no error results.
+*/
+static void
+heap_unread_subtract(HeapScanDesc scan, BlockNumber blockno)
+{
+      unsigned int Unread_Pfetched_index = scan->rs_Unread_Pfetched_next; /* index of next unread blockno */
+      BlockNumber *candidate_P;   /*  location of callers blockno - maybe */
+      BlockNumber nextUnreadPfetched;
+
+      /*  caller is not supposed to pass InvalidBlockNumber but check anyway */
+      if (    (blockno != InvalidBlockNumber)
+		   && ( scan->rs_Unread_Pfetched_count > 0 )   /*  if the list is not empty  */
+         ) {
+
+			  /*  take modulo of the circumference.
+			  **  actually rs_Unread_Pfetched_next should never exceed the circumference but check anyway.
+			  */
+			  if (Unread_Pfetched_index >= target_prefetch_pages) {
+				  Unread_Pfetched_index  -= target_prefetch_pages;
+}
+			  candidate_P = (scan->rs_Unread_Pfetched_base + Unread_Pfetched_index);
+			  nextUnreadPfetched = *candidate_P;
+
+			  if ( nextUnreadPfetched == blockno ) {
+				  goto remove_blockno;
+			  } else {
+                  /*  slow-search the entire list  */
+				  for (Unread_Pfetched_index = 0; Unread_Pfetched_index < target_prefetch_pages; Unread_Pfetched_index++) {
+					  candidate_P = (scan->rs_Unread_Pfetched_base + Unread_Pfetched_index);   /*  where to store new blockno */
+					  if (*candidate_P == blockno) { /* unused */
+						  goto remove_blockno;
+					  }
+				  }
+			  }
+
+    remove_blockno:
+			  *candidate_P = InvalidBlockNumber;
+
+			  scan->rs_Unread_Pfetched_next = (Unread_Pfetched_index+1);  /*  update next pfchd unread */
+			  if (scan->rs_Unread_Pfetched_next >= target_prefetch_pages) {
+					  scan->rs_Unread_Pfetched_next = 0;
+			  }
+			  scan->rs_Unread_Pfetched_count--;  /*  update count */
+	  }
+
+      return;
 }
 
+
 /*
  * heapgetpage - subroutine for heapgettup()
  *
@@ -304,7 +467,7 @@ initscan(HeapScanDesc scan, ScanKey key, bool is_rescan)
  * which tuples on the page are visible.
  */
 static void
-heapgetpage(HeapScanDesc scan, BlockNumber page)
+heapgetpage(HeapScanDesc scan, BlockNumber page , BlockNumber prefetchHWM)
 {
 	Buffer		buffer;
 	Snapshot	snapshot;
@@ -314,6 +477,10 @@ heapgetpage(HeapScanDesc scan, BlockNumber page)
 	OffsetNumber lineoff;
 	ItemId		lpp;
 	bool		all_visible;
+#ifdef USE_PREFETCH
+	int             PrefetchBufferRc;  /*   indicates whether requested prefetch block already in a buffer and if pin count on buffer has been increased */
+#endif   /* USE_PREFETCH */
+
 
 	Assert(page < scan->rs_nblocks);
 
@@ -336,6 +503,98 @@ heapgetpage(HeapScanDesc scan, BlockNumber page)
 									   RBM_NORMAL, scan->rs_strategy);
 	scan->rs_cblock = page;
 
+#ifdef USE_PREFETCH
+
+        heap_unread_subtract(scan, page);
+
+        /*    maybe prefetch some pages  starting with rs_pfchblock */
+        if (scan->rs_prefetch_target >= 0) {       /*   prefetching enabled on this scan ?                         */
+            int next_block_to_be_read = (page+1);  /*   next block to be read = lowest possible prefetchable block */
+            int num_to_pfch_this_time;             /*   eventually holds the number of blocks to prefetch now      */
+            int prefetchable_range;                /*   size of the area ahead of the current prefetch position    */
+
+            /*  check if prefetcher reached wrap point and the scan has now wrapped */
+            if (  (page == 0) && (scan->rs_prefetch_target == PREFETCH_WRAP_POINT)  ) {
+                scan->rs_prefetch_target = 1;
+                scan->rs_pfchblock = next_block_to_be_read;
+            } else
+            if (scan->rs_pfchblock < next_block_to_be_read) {
+                scan->rs_pfchblock = next_block_to_be_read; /* next block to be prefetched must be ahead of one we just read */
+            }
+
+            /* now we know where we would start prefetching -
+            ** next question   -  if this is a sync scan,  ensure we do not prefetch behind the HWM
+            ** debatable whether to require strict inequality or >=  -   >= works better in practice
+            */
+            if ( (!scan->rs_syncscan) || (scan->rs_pfchblock >= prefetchHWM) ) {
+
+                /* now we know where we will start prefetching -
+                ** next question   -  how many?
+                ** apply two limits :
+                **  1.   target prefetch distance
+                **  2.   number of available blocks ahead of us
+                */
+
+                /*  1.   target prefetch distance   */
+                num_to_pfch_this_time = next_block_to_be_read + scan->rs_prefetch_target; /* page beyond prefetch target */
+                num_to_pfch_this_time -= scan->rs_pfchblock;                              /*  convert to offset        */
+
+                /*   first do prefetching up to our current limit  ...
+                **   highest page number that a scan (pre)-fetches is scan->rs_nblocks-1
+                **   note  -  prefetcher does not wrap a prefetch range -
+                **            instead just stop and then start again if and when main scan wraps
+                */
+                if (scan->rs_pfchblock <= scan->rs_startblock) {  /*  if on second leg towards startblock */
+                    prefetchable_range = ((int)(scan->rs_startblock) - (int)(scan->rs_pfchblock));
+                }
+                else {                                            /*     on first leg towards nblocks     */
+                    prefetchable_range = ((int)(scan->rs_nblocks) - (int)(scan->rs_pfchblock));
+                }
+                if (prefetchable_range > 0) {           /*  if theres a range to prefetch */
+
+                    /*  2.   number of available blocks ahead of us        */
+                    if (num_to_pfch_this_time > prefetchable_range) {
+                        num_to_pfch_this_time = prefetchable_range;
+                    }
+                    while (num_to_pfch_this_time-- > 0) {
+                        PrefetchBufferRc = PrefetchBuffer(scan->rs_rd, MAIN_FORKNUM, scan->rs_pfchblock, scan->rs_strategy);
+                        /*  if pin acquired on buffer,  then remember in case of future Discard */
+                        if (PrefetchBufferRc & PREFTCHRC_BUF_PIN_INCREASED) {
+                            heap_unread_add(scan, scan->rs_pfchblock);
+						}
+                        scan->rs_pfchblock++;
+                        /*  if syncscan and requested block was already in buffer pool,
+                        **  this suggests that another scanner is ahead of us and we should advance
+                        */
+                        if ( (scan->rs_syncscan) && (PrefetchBufferRc & PREFTCHRC_BLK_ALREADY_PRESENT) ) {
+                            scan->rs_pfchblock++;
+                            num_to_pfch_this_time--;
+                        }
+                    }
+                }
+                else {
+                    /*   we must not modify scan->rs_pfchblock here
+                    **   because it is needed for possible DiscardBuffer at end of scan  ...
+                    **   ... instead ...
+                    */
+                    scan->rs_prefetch_target = PREFETCH_WRAP_POINT;  /*   mark this prefetcher as waiting to wrap */
+                }
+
+                /*   ...  then adjust prefetching limit : by doubling on each iteration */
+                if (scan->rs_prefetch_target == 0) {
+                    scan->rs_prefetch_target = 1;
+                }
+                else {
+                    scan->rs_prefetch_target *= 2;
+                    if (scan->rs_prefetch_target > target_prefetch_pages) {
+                        scan->rs_prefetch_target = target_prefetch_pages;
+                    }
+                }
+            }
+        }
+#endif   /* USE_PREFETCH */
+
+
 	if (!scan->rs_pageatatime)
 		return;
 
@@ -452,6 +711,8 @@ heapgettup(HeapScanDesc scan,
 	OffsetNumber lineoff;
 	int			linesleft;
 	ItemId		lpp;
+    BlockNumber  prefetchHWM = 0;   /*  HWM of prefetch BlockNum for all participants in same sync scan */
+    int          ix;
 
 	/*
 	 * calculate next starting lineoff, given scan direction
@@ -470,7 +731,25 @@ heapgettup(HeapScanDesc scan,
 				return;
 			}
 			page = scan->rs_startblock; /* first page */
-			heapgetpage(scan, page);
+#ifdef USE_PREFETCH
+                        /*  decide if we shall do prefetching :  only if :
+                        **   .  prefetching enabled for this scan
+                        **   .  not a bitmap scan (which do their own)
+                        **   .  sufficient number of blocks - at least twice the target_prefetch_pages
+                        */
+                        if (    (!scan->rs_bitmapscan)
+                             && (scan->rs_prefetch_target >= PREFETCH_MAYBE)
+                             && (scan->rs_nblocks > (2*target_prefetch_pages))
+                           )  {
+                                scan->rs_prefetch_target = 1;  /*  do prefetching on forward non-bitmap scan */
+                                scan->rs_Unread_Pfetched_base = (BlockNumber *)palloc(sizeof(BlockNumber)*target_prefetch_pages);
+                                /*  Initialise the list */
+                                for (ix = 0; ix < target_prefetch_pages; ix++) {
+									*(scan->rs_Unread_Pfetched_base + ix) = InvalidBlockNumber;
+								}
+                        }
+#endif   /* USE_PREFETCH */
+			heapgetpage(scan, page, prefetchHWM);
 			lineoff = FirstOffsetNumber;		/* first offnum */
 			scan->rs_inited = true;
 		}
@@ -516,7 +795,7 @@ heapgettup(HeapScanDesc scan,
 				page = scan->rs_startblock - 1;
 			else
 				page = scan->rs_nblocks - 1;
-			heapgetpage(scan, page);
+			heapgetpage(scan, page, prefetchHWM);
 		}
 		else
 		{
@@ -557,7 +836,7 @@ heapgettup(HeapScanDesc scan,
 
 		page = ItemPointerGetBlockNumber(&(tuple->t_self));
 		if (page != scan->rs_cblock)
-			heapgetpage(scan, page);
+			heapgetpage(scan, page, prefetchHWM);
 
 		/* Since the tuple was previously fetched, needn't lock page here */
 		dp = (Page) BufferGetPage(scan->rs_cbuf);
@@ -660,8 +939,10 @@ heapgettup(HeapScanDesc scan,
 			 * a little bit backwards on every invocation, which is confusing.
 			 * We don't guarantee any specific ordering in general, though.
 			 */
-			if (scan->rs_syncscan)
-				ss_report_location(scan->rs_rd, page);
+			if (scan->rs_syncscan) {
+                                prefetchHWM = scan->rs_pfchblock;
+				ss_report_location(scan->rs_rd, page, &prefetchHWM);
+                        }
 		}
 
 		/*
@@ -671,6 +952,22 @@ heapgettup(HeapScanDesc scan,
 		{
 			if (BufferIsValid(scan->rs_cbuf))
 				ReleaseBuffer(scan->rs_cbuf);
+#ifdef USE_PREFETCH
+                        if (  (scan->rs_pfchblock > 0) && (scan->rs_cblock != InvalidBlockNumber)  ) {
+                            BlockNumber *Unread_Pfetched_base = scan->rs_Unread_Pfetched_base;
+                            unsigned int Unread_Pfetched_next = scan->rs_Unread_Pfetched_next;   /*  where the next unread blockno is relative to start */
+                            unsigned int Unread_Pfetched_count = scan->rs_Unread_Pfetched_count;
+
+                            /*  we have prefetched but not read the range from cblock+1 to pfchblock-1 if that is non-empty */
+                            while ((Unread_Pfetched_count--) > 0) {
+                                DiscardBuffer( scan->rs_rd, MAIN_FORKNUM, *(Unread_Pfetched_base+Unread_Pfetched_next));
+                                heap_unread_subtract(scan, *(Unread_Pfetched_base+Unread_Pfetched_next)); /* remove this blockno from list of prefetched and unread blocknos */
+                                Unread_Pfetched_next++;
+                                if (Unread_Pfetched_next >= target_prefetch_pages)
+                                    Unread_Pfetched_next = 0;
+                            }
+                        }
+#endif   /* USE_PREFETCH */
 			scan->rs_cbuf = InvalidBuffer;
 			scan->rs_cblock = InvalidBlockNumber;
 			tuple->t_data = NULL;
@@ -678,7 +975,7 @@ heapgettup(HeapScanDesc scan,
 			return;
 		}
 
-		heapgetpage(scan, page);
+		heapgetpage(scan, page, prefetchHWM);
 
 		LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
 
@@ -727,6 +1024,8 @@ heapgettup_pagemode(HeapScanDesc scan,
 	OffsetNumber lineoff;
 	int			linesleft;
 	ItemId		lpp;
+    BlockNumber  prefetchHWM = 0;   /*  HWM of prefetch BlockNum for all participants in same sync scan */
+    int          ix;
 
 	/*
 	 * calculate next starting lineindex, given scan direction
@@ -745,7 +1044,25 @@ heapgettup_pagemode(HeapScanDesc scan,
 				return;
 			}
 			page = scan->rs_startblock; /* first page */
-			heapgetpage(scan, page);
+#ifdef USE_PREFETCH
+                        /*  decide if we shall do prefetching :  only if :
+                        **   .  prefetching enabled for this scan
+                        **   .  not a bitmap scan (which do their own)
+                        **   .  sufficient number of blocks - at least twice the target_prefetch_pages
+                        */
+                        if (    (!scan->rs_bitmapscan)
+                             && (scan->rs_prefetch_target >= PREFETCH_MAYBE)
+                             && (scan->rs_nblocks > (2*target_prefetch_pages))
+                           )  {
+                                scan->rs_prefetch_target = 1;  /*  do prefetching on forward non-bitmap scan */
+                                scan->rs_Unread_Pfetched_base = (BlockNumber *)palloc(sizeof(BlockNumber)*target_prefetch_pages);
+                                /*  Initialise the list */
+                                for (ix = 0; ix < target_prefetch_pages; ix++) {
+									*(scan->rs_Unread_Pfetched_base + ix) = InvalidBlockNumber;
+								}
+                        }
+#endif   /* USE_PREFETCH */
+			heapgetpage(scan, page, prefetchHWM);
 			lineindex = 0;
 			scan->rs_inited = true;
 		}
@@ -788,7 +1105,7 @@ heapgettup_pagemode(HeapScanDesc scan,
 				page = scan->rs_startblock - 1;
 			else
 				page = scan->rs_nblocks - 1;
-			heapgetpage(scan, page);
+			heapgetpage(scan, page, prefetchHWM);
 		}
 		else
 		{
@@ -826,7 +1143,7 @@ heapgettup_pagemode(HeapScanDesc scan,
 
 		page = ItemPointerGetBlockNumber(&(tuple->t_self));
 		if (page != scan->rs_cblock)
-			heapgetpage(scan, page);
+			heapgetpage(scan, page, prefetchHWM);
 
 		/* Since the tuple was previously fetched, needn't lock page here */
 		dp = (Page) BufferGetPage(scan->rs_cbuf);
@@ -921,8 +1238,10 @@ heapgettup_pagemode(HeapScanDesc scan,
 			 * a little bit backwards on every invocation, which is confusing.
 			 * We don't guarantee any specific ordering in general, though.
 			 */
-			if (scan->rs_syncscan)
-				ss_report_location(scan->rs_rd, page);
+			if (scan->rs_syncscan) {
+                                prefetchHWM = scan->rs_pfchblock;
+				ss_report_location(scan->rs_rd, page, &prefetchHWM);
+                        }
 		}
 
 		/*
@@ -932,6 +1251,22 @@ heapgettup_pagemode(HeapScanDesc scan,
 		{
 			if (BufferIsValid(scan->rs_cbuf))
 				ReleaseBuffer(scan->rs_cbuf);
+#ifdef USE_PREFETCH
+                        if (  (scan->rs_pfchblock > 0) && (scan->rs_cblock != InvalidBlockNumber)  ) {
+                            BlockNumber *Unread_Pfetched_base = scan->rs_Unread_Pfetched_base;
+                            unsigned int Unread_Pfetched_next = scan->rs_Unread_Pfetched_next;   /*  where the next unread blockno is relative to start */
+                            unsigned int Unread_Pfetched_count = scan->rs_Unread_Pfetched_count;
+
+                            /*  we have prefetched but not read the range from cblock+1 to pfchblock-1 if that is non-empty */
+                             while ((Unread_Pfetched_count--) > 0) {
+                                DiscardBuffer( scan->rs_rd, MAIN_FORKNUM, *(Unread_Pfetched_base+Unread_Pfetched_next));
+                                heap_unread_subtract(scan, *(Unread_Pfetched_base+Unread_Pfetched_next)); /* remove this blockno from list of prefetched and unread blocknos */
+                                Unread_Pfetched_next++;
+                                if (Unread_Pfetched_next >= target_prefetch_pages)
+                                    Unread_Pfetched_next = 0;
+                            }
+                        }
+#endif   /* USE_PREFETCH */
 			scan->rs_cbuf = InvalidBuffer;
 			scan->rs_cblock = InvalidBlockNumber;
 			tuple->t_data = NULL;
@@ -939,7 +1274,7 @@ heapgettup_pagemode(HeapScanDesc scan,
 			return;
 		}
 
-		heapgetpage(scan, page);
+		heapgetpage(scan, page, prefetchHWM);
 
 		dp = (Page) BufferGetPage(scan->rs_cbuf);
 		lines = scan->rs_ntuples;
@@ -1394,6 +1729,23 @@ void
 heap_rescan(HeapScanDesc scan,
 			ScanKey key)
 {
+
+#ifdef USE_PREFETCH
+         if (  (scan->rs_pfchblock > 0) && (scan->rs_cblock != InvalidBlockNumber)  ) {
+                            BlockNumber *Unread_Pfetched_base = scan->rs_Unread_Pfetched_base;
+                            unsigned int Unread_Pfetched_next = scan->rs_Unread_Pfetched_next;   /*  where the next unread blockno is relative to start */
+                            unsigned int Unread_Pfetched_count = scan->rs_Unread_Pfetched_count;
+
+                       /*  we have prefetched but not read the range from cblock+1 to pfchblock-1 if that is non-empty */
+                            while ((Unread_Pfetched_count--) > 0) {
+                                DiscardBuffer( scan->rs_rd, MAIN_FORKNUM, *(Unread_Pfetched_base+Unread_Pfetched_next));
+                                heap_unread_subtract(scan, *(Unread_Pfetched_base+Unread_Pfetched_next)); /* remove this blockno from list of prefetched and unread blocknos */
+                                Unread_Pfetched_next++;
+                                if (Unread_Pfetched_next >= target_prefetch_pages)
+                                    Unread_Pfetched_next = 0;
+                            }
+         }
+#endif   /* USE_PREFETCH */
 	/*
 	 * unpin scan buffers
 	 */
@@ -1418,6 +1770,23 @@ heap_endscan(HeapScanDesc scan)
 {
 	/* Note: no locking manipulations needed */
 
+
+#ifdef USE_PREFETCH
+         if (  (scan->rs_pfchblock > 0) && (scan->rs_cblock != InvalidBlockNumber)  ) {
+                            BlockNumber *Unread_Pfetched_base = scan->rs_Unread_Pfetched_base;
+                            unsigned int Unread_Pfetched_next = scan->rs_Unread_Pfetched_next;   /*  where the next unread blockno is relative to start */
+                            unsigned int Unread_Pfetched_count = scan->rs_Unread_Pfetched_count;
+
+                       /*  we have prefetched but not read the range from cblock+1 to pfchblock-1 if that is non-empty */
+                            while ((Unread_Pfetched_count--) > 0) {
+                                DiscardBuffer( scan->rs_rd, MAIN_FORKNUM, *(Unread_Pfetched_base+Unread_Pfetched_next));
+                                heap_unread_subtract(scan, *(Unread_Pfetched_base+Unread_Pfetched_next)); /* remove this blockno from list of prefetched and unread blocknos */
+                                Unread_Pfetched_next++;
+                                if (Unread_Pfetched_next >= target_prefetch_pages)
+                                    Unread_Pfetched_next = 0;
+                            }
+         }
+#endif   /* USE_PREFETCH */
 	/*
 	 * unpin scan buffers
 	 */
@@ -1435,6 +1804,10 @@ heap_endscan(HeapScanDesc scan)
 	if (scan->rs_strategy != NULL)
 		FreeAccessStrategy(scan->rs_strategy);
 
+    if (scan->rs_Unread_Pfetched_base) {
+        pfree(scan->rs_Unread_Pfetched_base);
+    }
+
 	if (scan->rs_temp_snap)
 		UnregisterSnapshot(scan->rs_snapshot);
 
@@ -1464,7 +1837,6 @@ heap_endscan(HeapScanDesc scan)
 #define HEAPDEBUG_3
 #endif   /* !defined(HEAPDEBUGALL) */
 
-
 HeapTuple
 heap_getnext(HeapScanDesc scan, ScanDirection direction)
 {
@@ -6347,6 +6719,25 @@ heap_markpos(HeapScanDesc scan)
 void
 heap_restrpos(HeapScanDesc scan)
 {
+
+
+#ifdef USE_PREFETCH
+                        if (  (scan->rs_pfchblock > 0) && (scan->rs_cblock != InvalidBlockNumber)  ) {
+                            BlockNumber *Unread_Pfetched_base = scan->rs_Unread_Pfetched_base;
+                            unsigned int Unread_Pfetched_next = scan->rs_Unread_Pfetched_next;   /*  where the next unread blockno is relative to start */
+                            unsigned int Unread_Pfetched_count = scan->rs_Unread_Pfetched_count;
+
+                            /*  we have prefetched but not read the range from cblock+1 to pfchblock-1 if that is non-empty */
+                             while ((Unread_Pfetched_count--) > 0) {
+                                DiscardBuffer( scan->rs_rd, MAIN_FORKNUM, *(Unread_Pfetched_base+Unread_Pfetched_next));
+                                heap_unread_subtract(scan, *(Unread_Pfetched_base+Unread_Pfetched_next)); /* remove this blockno from list of prefetched and unread blocknos */
+                                Unread_Pfetched_next++;
+                                if (Unread_Pfetched_next >= target_prefetch_pages)
+                                    Unread_Pfetched_next = 0;
+                            }
+                        }
+#endif   /* USE_PREFETCH */
+
 	/* XXX no amrestrpos checking that ammarkpos called */
 
 	if (!ItemPointerIsValid(&scan->rs_mctid))
diff --git a/src/backend/access/heap/syncscan.c b/src/backend/access/heap/syncscan.c
index 7ea1ead..59e5255 100644
--- a/src/backend/access/heap/syncscan.c
+++ b/src/backend/access/heap/syncscan.c
@@ -90,6 +90,7 @@ typedef struct ss_scan_location_t
 {
 	RelFileNode relfilenode;	/* identity of a relation */
 	BlockNumber location;		/* last-reported location in the relation */
+	BlockNumber prefetchHWM;	/* high-water-mark of prefetched Blocknum */
 } ss_scan_location_t;
 
 typedef struct ss_lru_item_t
@@ -113,7 +114,7 @@ static ss_scan_locations_t *scan_locations;
 
 /* prototypes for internal functions */
 static BlockNumber ss_search(RelFileNode relfilenode,
-		  BlockNumber location, bool set);
+		  BlockNumber location, bool set , BlockNumber *prefetchHWMp);
 
 
 /*
@@ -160,6 +161,7 @@ SyncScanShmemInit(void)
 			item->location.relfilenode.dbNode = InvalidOid;
 			item->location.relfilenode.relNode = InvalidOid;
 			item->location.location = InvalidBlockNumber;
+			item->location.prefetchHWM = InvalidBlockNumber;
 
 			item->prev = (i > 0) ?
 				(&scan_locations->items[i - 1]) : NULL;
@@ -185,7 +187,7 @@ SyncScanShmemInit(void)
  * data structure.
  */
 static BlockNumber
-ss_search(RelFileNode relfilenode, BlockNumber location, bool set)
+ss_search(RelFileNode relfilenode, BlockNumber location, bool set , BlockNumber *prefetchHWMp)
 {
 	ss_lru_item_t *item;
 
@@ -206,6 +208,22 @@ ss_search(RelFileNode relfilenode, BlockNumber location, bool set)
 			{
 				item->location.relfilenode = relfilenode;
 				item->location.location = location;
+                                /*  if prefetch information requested,
+                                **  then reconcile and either update or report back the new HWM.
+                                */
+                                if (prefetchHWMp)
+                                {
+                                    if (   (item->location.prefetchHWM == InvalidBlockNumber)
+                                        || (item->location.prefetchHWM < *prefetchHWMp)
+                                       )
+                                    {
+                                      item->location.prefetchHWM = *prefetchHWMp;
+                                    }
+                                    else
+                                    {
+                                      *prefetchHWMp = item->location.prefetchHWM;
+                                    }
+                                }
 			}
 			else if (set)
 				item->location.location = location;
@@ -252,7 +270,7 @@ ss_get_location(Relation rel, BlockNumber relnblocks)
 	BlockNumber startloc;
 
 	LWLockAcquire(SyncScanLock, LW_EXCLUSIVE);
-	startloc = ss_search(rel->rd_node, 0, false);
+	startloc = ss_search(rel->rd_node, 0, false , 0);
 	LWLockRelease(SyncScanLock);
 
 	/*
@@ -282,7 +300,7 @@ ss_get_location(Relation rel, BlockNumber relnblocks)
  * same relfilenode.
  */
 void
-ss_report_location(Relation rel, BlockNumber location)
+ss_report_location(Relation rel, BlockNumber location , BlockNumber *prefetchHWMp)
 {
 #ifdef TRACE_SYNCSCAN
 	if (trace_syncscan)
@@ -306,7 +324,7 @@ ss_report_location(Relation rel, BlockNumber location)
 	{
 		if (LWLockConditionalAcquire(SyncScanLock, LW_EXCLUSIVE))
 		{
-			(void) ss_search(rel->rd_node, location, true);
+			(void) ss_search(rel->rd_node, location, true , prefetchHWMp);
 			LWLockRelease(SyncScanLock);
 		}
 #ifdef TRACE_SYNCSCAN
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c
index 850008b..53af580 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -77,6 +77,12 @@ RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
 
 	scan = (IndexScanDesc) palloc(sizeof(IndexScanDescData));
 
+#ifdef USE_PREFETCH
+        scan->do_prefetch = 0;             /*  no prefetching by default  */
+        scan->pfch_index_page_list = (struct pfch_index_pagelist*)0;
+        scan->pfch_block_item_list = (struct pfch_block_item*)0;
+#endif   /* USE_PREFETCH */
+
 	scan->heapRelation = NULL;	/* may be set later */
 	scan->indexRelation = indexRelation;
 	scan->xs_snapshot = InvalidSnapshot;		/* caller must initialize this */
@@ -139,6 +145,19 @@ RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
 void
 IndexScanEnd(IndexScanDesc scan)
 {
+#ifdef USE_PREFETCH
+	if (scan->do_prefetch) {
+		if ( (struct pfch_block_item*)0 != scan->pfch_block_item_list ) {
+			pfree(scan->pfch_block_item_list);
+			scan->pfch_block_item_list = (struct pfch_block_item*)0;
+		}
+		if ( (struct pfch_index_pagelist*)0 != scan->pfch_index_page_list ) {
+			pfree(scan->pfch_index_page_list);
+			scan->pfch_index_page_list = (struct pfch_index_pagelist*)0;
+		}
+	}
+#endif   /* USE_PREFETCH */
+
 	if (scan->keyData != NULL)
 		pfree(scan->keyData);
 	if (scan->orderByData != NULL)
diff --git a/src/backend/access/index/indexam.c b/src/backend/access/index/indexam.c
index 53cf96f..94f716c 100644
--- a/src/backend/access/index/indexam.c
+++ b/src/backend/access/index/indexam.c
@@ -79,6 +79,55 @@
 #include "utils/tqual.h"
 
 
+#ifdef USE_PREFETCH
+bool BlocknotinBuffer(Buffer buffer, Relation relation, BlockNumber blockNum);
+BlockNumber BlocknumOfBuffer(Buffer buffer);
+void index_mark_or_evict_block(IndexScanDesc scan , BlockNumber blocknumber , int markit);
+
+extern unsigned int prefetch_index_scans; /*  boolean whether to prefetch bitmap heap scans         */
+
+/*  if specified block number is present in the prefetch array,
+**  then either mark it as not to be discarded or evict it according to input param
+*/
+void index_mark_or_evict_block(IndexScanDesc scan , BlockNumber blocknumber , int markit)
+{
+        unsigned short int pfchx , pfchy , pfchz; /*  indexes in BlockIdData array   */
+
+        if (    scan->do_prefetch
+             && ((struct pfch_block_item*)0 != scan->pfch_block_item_list)
+             /* no need to check for scan->pfch_next < prefetch_index_scans
+             ** since we will do nothing if scan->pfch_used == 0
+             */
+           ) {
+            /*  search the prefetch list to find if the block is a member */
+            for (pfchx = 0; pfchx < scan->pfch_used; pfchx++) {
+                if (BlockIdGetBlockNumber(&(((scan->pfch_block_item_list)+pfchx)->pfch_blockid)) == blocknumber) {
+                      if (markit) {
+						      /*  mark it as not to be discarded */
+						      ((scan->pfch_block_item_list)+pfchx)->pfch_discard &= ~PREFTCHRC_BUF_PIN_INCREASED;
+					  } else {
+							  /*  shuffle all following the evictee to the left
+							  **  and update next pointer if its element moves
+							  */
+							  pfchy = (scan->pfch_used - 1); /*  current rightmost */
+							  scan->pfch_used = pfchy;
+
+							  while (pfchy > pfchx) {
+								  pfchz = pfchx + 1;
+								  BlockIdCopy((&(((scan->pfch_block_item_list)+pfchx)->pfch_blockid)), (&(((scan->pfch_block_item_list)+pfchz)->pfch_blockid)));
+								  ((scan->pfch_block_item_list)+pfchx)->pfch_discard = ((scan->pfch_block_item_list)+pfchz)->pfch_discard;
+								  if (scan->pfch_next == pfchz) {
+									  scan->pfch_next = pfchx;
+								  }
+								  pfchx = pfchz; /* advance */
+							  }
+                      }
+                }
+            }
+        }
+}
+#endif /* USE_PREFETCH */
+
 /* ----------------------------------------------------------------
  *					macros used in index_ routines
  *
@@ -253,6 +302,11 @@ index_beginscan(Relation heapRelation,
 	 */
 	scan->heapRelation = heapRelation;
 	scan->xs_snapshot = snapshot;
+#ifdef USE_PREFETCH
+	scan->do_prefetch = 0;             /*  no prefetching by default  */
+        scan->pfch_index_page_list = (struct pfch_index_pagelist*)0;
+	scan->pfch_block_item_list = (struct pfch_block_item*)0;
+#endif   /* USE_PREFETCH */
 
 	return scan;
 }
@@ -277,6 +331,11 @@ index_beginscan_bitmap(Relation indexRelation,
 	 * up by RelationGetIndexScan.
 	 */
 	scan->xs_snapshot = snapshot;
+#ifdef USE_PREFETCH
+	scan->do_prefetch = 0;             /*  no prefetching by default  */
+        scan->pfch_index_page_list = (struct pfch_index_pagelist*)0;
+	scan->pfch_block_item_list = (struct pfch_block_item*)0;
+#endif   /* USE_PREFETCH */
 
 	return scan;
 }
@@ -311,6 +370,9 @@ index_beginscan_internal(Relation indexRelation,
 									  Int32GetDatum(nkeys),
 									  Int32GetDatum(norderbys)));
 
+	scan->heap_tids_seen = 0;
+	scan->heap_tids_fetched = 0;
+	
 	return scan;
 }
 
@@ -342,6 +404,12 @@ index_rescan(IndexScanDesc scan,
 	/* Release any held pin on a heap page */
 	if (BufferIsValid(scan->xs_cbuf))
 	{
+#ifdef USE_PREFETCH
+                /*   if specified block number is present in the prefetch array,  then evict it */
+                if (scan->do_prefetch) {
+                      index_mark_or_evict_block(scan , BlocknumOfBuffer(scan->xs_cbuf) , 0);
+                }
+#endif   /* USE_PREFETCH */
 		ReleaseBuffer(scan->xs_cbuf);
 		scan->xs_cbuf = InvalidBuffer;
 	}
@@ -373,10 +441,30 @@ index_endscan(IndexScanDesc scan)
 	/* Release any held pin on a heap page */
 	if (BufferIsValid(scan->xs_cbuf))
 	{
+#ifdef USE_PREFETCH
+                /*   if specified block number is present in the prefetch array,  then evict it */
+                if (scan->do_prefetch) {
+                      index_mark_or_evict_block(scan , BlocknumOfBuffer(scan->xs_cbuf) , 0);
+                }
+#endif   /* USE_PREFETCH */
 		ReleaseBuffer(scan->xs_cbuf);
 		scan->xs_cbuf = InvalidBuffer;
 	}
 
+#ifdef USE_PREFETCH
+        /*   discard prefetched but unread buffers */
+        if (    scan->do_prefetch
+             && ((struct pfch_block_item*)0 != scan->pfch_block_item_list)
+           ) {
+            unsigned short int pfchx; /*  index in BlockIdData array   */
+                for (pfchx = 0; pfchx < scan->pfch_used; pfchx++) {
+                  if (((scan->pfch_block_item_list)+pfchx)->pfch_discard) {
+                      DiscardBuffer(scan->heapRelation, MAIN_FORKNUM, BlockIdGetBlockNumber(&(((scan->pfch_block_item_list)+pfchx)->pfch_blockid)));
+                  }
+                }
+        }
+#endif   /* USE_PREFETCH */
+
 	/* End the AM's scan */
 	FunctionCall1(procedure, PointerGetDatum(scan));
 
@@ -472,6 +560,12 @@ index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
 		/* ... but first, release any held pin on a heap page */
 		if (BufferIsValid(scan->xs_cbuf))
 		{
+#ifdef USE_PREFETCH
+                    /*   if specified block number is present in the prefetch array,  then evict it */
+                    if (scan->do_prefetch) {
+                          index_mark_or_evict_block(scan , BlocknumOfBuffer(scan->xs_cbuf) , 0);
+                    }
+#endif   /* USE_PREFETCH */
 			ReleaseBuffer(scan->xs_cbuf);
 			scan->xs_cbuf = InvalidBuffer;
 		}
@@ -479,6 +573,11 @@ index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
 	}
 
 	pgstat_count_index_tuples(scan->indexRelation, 1);
+	if (scan->heap_tids_seen++ >= (~0)) {
+		/* Avoid integer overflow */
+		scan->heap_tids_seen = 1;
+		scan->heap_tids_fetched = 0;
+	}
 
 	/* Return the TID of the tuple we found. */
 	return &scan->xs_ctup.t_self;
@@ -502,6 +601,10 @@ index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
  * enough information to do it efficiently in the general case.
  * ----------------
  */
+#if defined(USE_PREFETCH) && defined(AVOID_CATALOG_MIGRATION_FOR_ASYNCIO)
+extern Datum btpeeknexttuple(IndexScanDesc scan);
+#endif /* USE_PREFETCH */
+
 HeapTuple
 index_fetch_heap(IndexScanDesc scan)
 {
@@ -509,16 +612,109 @@ index_fetch_heap(IndexScanDesc scan)
 	bool		all_dead = false;
 	bool		got_heap_tuple;
 
+
+
 	/* We can skip the buffer-switching logic if we're in mid-HOT chain. */
 	if (!scan->xs_continue_hot)
 	{
 		/* Switch to correct buffer if we don't have it already */
 		Buffer		prev_buf = scan->xs_cbuf;
 
+#ifdef USE_PREFETCH
+
+                /*   If the old block is different from new block, then evict old
+                **   block from prefetched array.   It is arguable we should leave it
+                **   in the array because it's likely to remain in the buffer pool
+                **   for a while,  but in that case , if we encounter the block
+                **   again,  prefetching it again does no harm.
+                **   (and note that,  if it's not pinned,  prefetching it will try to
+                **   pin it since prefetch tries to bank a pin for a buffer in the buffer pool).
+                **   therefore it should usually win.
+                */
+                if (    scan->do_prefetch
+                     && ( BufferIsValid(prev_buf) )
+                     && (BlocknotinBuffer(prev_buf,scan->heapRelation,ItemPointerGetBlockNumber(tid)))
+                     && (scan->pfch_next < prefetch_index_scans)  /* ensure there is an entry */
+                        ) {
+                          index_mark_or_evict_block(scan , BlocknumOfBuffer(prev_buf) , 0);
+                }
+
+#endif   /* USE_PREFETCH  */
 		scan->xs_cbuf = ReleaseAndReadBuffer(scan->xs_cbuf,
 											 scan->heapRelation,
 											 ItemPointerGetBlockNumber(tid));
 
+                /*   If the new block had been prefetched and pinned,
+                **   then mark that it no longer requires to be discarded.
+                **   Of course,  we don't evict the entry,
+                **   because we want to remember that it was recently prefetched.
+                */
+                index_mark_or_evict_block(scan , BlocknumOfBuffer(prev_buf) , 1);
+
+				scan->heap_tids_fetched++;
+
+#ifdef USE_PREFETCH
+                /*  try prefetching next data block
+                **    (next meaning one containing TIDs from matching keys
+                **     in same index page and different from any block
+                **     we previously prefetched and listed in prefetched array)
+                */
+                {
+                    FmgrInfo   *procedure;
+                    bool	found;             /*  did we find the "next" heap tid in current index page */
+                    int         PrefetchBufferRc;  /*  indicates whether requested prefetch block already in a buffer and if pin count on buffer has been increased */
+
+                    if (scan->do_prefetch) {
+#ifdef AVOID_CATALOG_MIGRATION_FOR_ASYNCIO
+                        procedure = &scan->indexRelation->rd_aminfo->ampeeknexttuple; /* is incorrect but avoids adding function to catalog */
+#else /*  not AVOID_CATALOG_MIGRATION_FOR_ASYNCIO */
+                        if (RegProcedureIsValid(scan->indexRelation->rd_am->ampeeknexttuple)) {
+                            GET_SCAN_PROCEDURE(ampeeknexttuple); /* is correct but requires adding function to catalog */
+                        } else {
+                            procedure = 0;
+                        }
+#endif /* not AVOID_CATALOG_MIGRATION_FOR_ASYNCIO */
+
+                        if (    procedure          /* does the index access method support peektuple? */
+#ifndef AVOID_CATALOG_MIGRATION_FOR_ASYNCIO
+                             && procedure->fn_addr /* procedure->fn_addr is non-null only if in catalog */
+#endif /* not AVOID_CATALOG_MIGRATION_FOR_ASYNCIO */
+                           ) {
+                            int iterations = 1;      /*  how many iterations of prefetching shall we try  -
+                                                     **  if used entries in prefetch list is < target_prefetch_pages
+                                                     **  then 2,  else 1
+                                                     **  this should result in gradually and smoothly increasing up to target_prefetch_pages
+                                                     */
+                            /*  note we trust InitIndexScan verified this scan is forwards only and so set that */
+                            if (scan->pfch_used < target_prefetch_pages) {
+                                iterations = 2;
+                            }
+                            do {
+                                found =  DatumGetBool(
+#ifdef AVOID_CATALOG_MIGRATION_FOR_ASYNCIO
+                                                         btpeeknexttuple(scan)     /*  pass scan as direct parameter since cant use fmgr because not in catalog */
+#else /*  not AVOID_CATALOG_MIGRATION_FOR_ASYNCIO */
+                                                         FunctionCall1(procedure, PointerGetDatum(scan)) /* use fmgr to call it because in catalog  */
+#endif /* not AVOID_CATALOG_MIGRATION_FOR_ASYNCIO */
+                                                     );
+                                if (found) {
+                                    /*    btpeeknexttuple set pfch_next to point to the item in block_item_list to be prefetched */
+                                    PrefetchBufferRc = PrefetchBuffer(scan->heapRelation, MAIN_FORKNUM, BlockIdGetBlockNumber((&((scan->pfch_block_item_list + scan->pfch_next))->pfch_blockid)) , 0);
+                                    /* elog(LOG,"index_fetch_heap prefetched rel %u blockNum %u"
+                                       ,scan->heapRelation->rd_node.relNode ,BlockIdGetBlockNumber(scan->pfch_block_item_list + scan->pfch_next));
+                                    */
+
+                                    /*  if pin acquired on buffer,  then remember in case of future Discard */
+                                    (scan->pfch_block_item_list + scan->pfch_next)->pfch_discard = (PrefetchBufferRc & PREFTCHRC_BUF_PIN_INCREASED);
+
+
+                                }
+                            } while (--iterations > 0);
+                        }
+                    }
+                }
+#endif   /* USE_PREFETCH */
+
 		/*
 		 * Prune page, but only if we weren't already on this page
 		 */
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index ecee5ac..e32a79f 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -793,7 +793,7 @@ _bt_insertonpg(Relation rel,
 		{
 			Assert(!P_ISLEAF(lpageop));
 
-			metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE);
+			metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE , (struct pfch_index_pagelist*)0);
 			metapg = BufferGetPage(metabuf);
 			metad = BTPageGetMeta(metapg);
 
@@ -972,7 +972,7 @@ _bt_split(Relation rel, Buffer buf, Buffer cbuf, OffsetNumber firstright,
 	bool		isleaf;
 
 	/* Acquire a new page to split into */
-	rbuf = _bt_getbuf(rel, P_NEW, BT_WRITE);
+	rbuf = _bt_getbuf(rel, P_NEW, BT_WRITE , (struct pfch_index_pagelist*)0);
 
 	/*
 	 * origpage is the original page to be split.  leftpage is a temporary
@@ -1175,7 +1175,7 @@ _bt_split(Relation rel, Buffer buf, Buffer cbuf, OffsetNumber firstright,
 
 	if (!P_RIGHTMOST(oopaque))
 	{
-		sbuf = _bt_getbuf(rel, oopaque->btpo_next, BT_WRITE);
+		sbuf = _bt_getbuf(rel, oopaque->btpo_next, BT_WRITE , (struct pfch_index_pagelist*)0);
 		spage = BufferGetPage(sbuf);
 		sopaque = (BTPageOpaque) PageGetSpecialPointer(spage);
 		if (sopaque->btpo_prev != origpagenumber)
@@ -1817,7 +1817,7 @@ _bt_finish_split(Relation rel, Buffer lbuf, BTStack stack)
 	Assert(P_INCOMPLETE_SPLIT(lpageop));
 
 	/* Lock right sibling, the one missing the downlink */
-	rbuf = _bt_getbuf(rel, lpageop->btpo_next, BT_WRITE);
+	rbuf = _bt_getbuf(rel, lpageop->btpo_next, BT_WRITE , (struct pfch_index_pagelist*)0);
 	rpage = BufferGetPage(rbuf);
 	rpageop = (BTPageOpaque) PageGetSpecialPointer(rpage);
 
@@ -1829,7 +1829,7 @@ _bt_finish_split(Relation rel, Buffer lbuf, BTStack stack)
 		BTMetaPageData *metad;
 
 		/* acquire lock on the metapage */
-		metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE);
+		metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE , (struct pfch_index_pagelist*)0);
 		metapg = BufferGetPage(metabuf);
 		metad = BTPageGetMeta(metapg);
 
@@ -1877,7 +1877,7 @@ _bt_getstackbuf(Relation rel, BTStack stack, int access)
 		Page		page;
 		BTPageOpaque opaque;
 
-		buf = _bt_getbuf(rel, blkno, access);
+		buf = _bt_getbuf(rel, blkno, access , (struct pfch_index_pagelist*)0);
 		page = BufferGetPage(buf);
 		opaque = (BTPageOpaque) PageGetSpecialPointer(page);
 
@@ -2008,12 +2008,12 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
 	lopaque = (BTPageOpaque) PageGetSpecialPointer(lpage);
 
 	/* get a new root page */
-	rootbuf = _bt_getbuf(rel, P_NEW, BT_WRITE);
+	rootbuf = _bt_getbuf(rel, P_NEW, BT_WRITE , (struct pfch_index_pagelist*)0);
 	rootpage = BufferGetPage(rootbuf);
 	rootblknum = BufferGetBlockNumber(rootbuf);
 
 	/* acquire lock on the metapage */
-	metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE);
+	metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE , (struct pfch_index_pagelist*)0);
 	metapg = BufferGetPage(metabuf);
 	metad = BTPageGetMeta(metapg);
 
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index bab5a49..afa0263 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -127,7 +127,7 @@ _bt_getroot(Relation rel, int access)
 		Assert(rootblkno != P_NONE);
 		rootlevel = metad->btm_fastlevel;
 
-		rootbuf = _bt_getbuf(rel, rootblkno, BT_READ);
+		rootbuf = _bt_getbuf(rel, rootblkno, BT_READ , (struct pfch_index_pagelist*)0);
 		rootpage = BufferGetPage(rootbuf);
 		rootopaque = (BTPageOpaque) PageGetSpecialPointer(rootpage);
 
@@ -153,7 +153,7 @@ _bt_getroot(Relation rel, int access)
 		rel->rd_amcache = NULL;
 	}
 
-	metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ);
+	metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ , (struct pfch_index_pagelist*)0);
 	metapg = BufferGetPage(metabuf);
 	metaopaque = (BTPageOpaque) PageGetSpecialPointer(metapg);
 	metad = BTPageGetMeta(metapg);
@@ -209,7 +209,7 @@ _bt_getroot(Relation rel, int access)
 		 * the new root page.  Since this is the first page in the tree, it's
 		 * a leaf as well as the root.
 		 */
-		rootbuf = _bt_getbuf(rel, P_NEW, BT_WRITE);
+		rootbuf = _bt_getbuf(rel, P_NEW, BT_WRITE , (struct pfch_index_pagelist*)0);
 		rootblkno = BufferGetBlockNumber(rootbuf);
 		rootpage = BufferGetPage(rootbuf);
 		rootopaque = (BTPageOpaque) PageGetSpecialPointer(rootpage);
@@ -350,7 +350,7 @@ _bt_gettrueroot(Relation rel)
 		pfree(rel->rd_amcache);
 	rel->rd_amcache = NULL;
 
-	metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ);
+	metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ , (struct pfch_index_pagelist*)0);
 	metapg = BufferGetPage(metabuf);
 	metaopaque = (BTPageOpaque) PageGetSpecialPointer(metapg);
 	metad = BTPageGetMeta(metapg);
@@ -436,7 +436,7 @@ _bt_getrootheight(Relation rel)
 		Page		metapg;
 		BTPageOpaque metaopaque;
 
-		metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ);
+		metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ , (struct pfch_index_pagelist*)0);
 		metapg = BufferGetPage(metabuf);
 		metaopaque = (BTPageOpaque) PageGetSpecialPointer(metapg);
 		metad = BTPageGetMeta(metapg);
@@ -562,6 +562,170 @@ _bt_log_reuse_page(Relation rel, BlockNumber blkno, TransactionId latestRemovedX
 }
 
 /*
+ *	_bt_prefetchbuf() -- Prefetch a buffer by block number
+ *                           and keep track of prefetched and unread blocknums in pagelist.
+ *   input parms  :
+ *       rel and blockno identify block to be prefetched as usual
+ *       pfch_index_page_list_P points to the pointer anchoring the head of the index page list
+ *             Since the pagelist is a kind of optimization,
+ *             handle palloc failure by quietly omitting the keeping track.
+ */
+void
+_bt_prefetchbuf(Relation rel, BlockNumber blkno , struct pfch_index_pagelist** pfch_index_page_list_P)
+{
+
+    int rc = 0;
+    struct pfch_index_pagelist* pfch_index_plp;      /*  pointer to current chunk */
+    struct pfch_index_item* found_item = 0;
+
+		if ((struct pfch_index_pagelist**)0 == pfch_index_page_list_P) {
+			pfch_index_plp = (struct pfch_index_pagelist*)0;
+		} else {
+			pfch_index_plp = *pfch_index_page_list_P;
+		}
+
+    	if (blkno != P_NEW && blkno != P_NONE)
+    	{
+            /* prefetch an existing block of the relation
+            ** but first,  check it has not recently already been prefetched and not yet read
+            */
+            found_item = _bt_find_block(blkno , pfch_index_plp);
+			if ((struct pfch_index_item*)0 == found_item) {  /*  not found */
+
+		        rc = PrefetchBuffer(rel, MAIN_FORKNUM, blkno , 0);
+
+                /*  add the pagenum to the list ,  indicating its discard status
+                **  since it's only an optimization,  ignore failure such as exceeded allowed space
+				*/
+                _bt_add_block( blkno , pfch_index_page_list_P , (uint32)(rc & PREFTCHRC_BUF_PIN_INCREASED));
+
+            }
+	    }
+        return;
+}
+
+/*   _bt_find_block finds the item referencing specified Block in index page list if present
+**   and returns the pointer to the pfch_index_item if found,  or null if not
+*/
+struct pfch_index_item*
+_bt_find_block(BlockNumber blkno , struct pfch_index_pagelist* pfch_index_page_list)
+{
+
+    struct pfch_index_item* found_item = 0;
+    struct pfch_index_pagelist* pfch_index_plp;      /*  pointer to current chunk */
+    int ix, tx;
+
+		pfch_index_plp = pfch_index_page_list;
+
+		while (     (struct pfch_index_pagelist*)0 != pfch_index_plp
+                &&  ( (struct pfch_index_item*)0 == found_item)
+              ) {
+			ix = 0;
+			tx = pfch_index_plp->pfch_index_item_count;
+			while (     (ix < tx)
+                    &&  ( (struct pfch_index_item*)0 == found_item)
+                  ) {
+				if (pfch_index_plp->pfch_indexid[ix].pfch_blocknum == blkno) {
+					found_item = &pfch_index_plp->pfch_indexid[ix];
+				}
+                ix++;
+			}
+			pfch_index_plp = pfch_index_plp->pfch_index_pagelist_next;
+		}
+
+     return found_item;
+}
+
+/*   _bt_add_block adds the specified Block to the index page list
+**   and returns 0 if successful,  non-zero if not
+*/
+int
+_bt_add_block(BlockNumber blkno , struct pfch_index_pagelist** pfch_index_page_list_P , uint32 discard_status)
+{
+    int rc = 1;
+    int ix;
+    struct pfch_index_pagelist* pfch_index_plp;      /*  pointer to current chunk */
+    struct pfch_index_pagelist* pfch_index_page_list_anchor; /*  pointer to first chunk if any  */
+	/*  allow expansion of pagelist to 16 chunks
+	**  which accommodates backwards-sequential index scans
+	**  where the scanner increases target_prefetch_pages by a factor of up to 16
+	**   see code in _bt_steppage
+	**  note - this creates an undesirable weak dependency on this number in _bt_steppage,
+	**         but :
+	**           there is no disaster if the numbers disagree  -  just sub-optimal use of the list
+	**           to implement a proper interface would require that chunks have a variable size
+	**           which would require an extra size variable in each chunk
+	*/
+	int num_chunks = 16;
+
+		if ((struct pfch_index_pagelist**)0 == pfch_index_page_list_P) {
+			pfch_index_page_list_anchor = (struct pfch_index_pagelist*)0;
+		} else {
+			pfch_index_page_list_anchor = *pfch_index_page_list_P;
+		}
+		pfch_index_plp = pfch_index_page_list_anchor;       /* pointer to current chunk */
+
+		while ( (struct pfch_index_pagelist*)0 != pfch_index_plp ) {
+			ix = pfch_index_plp->pfch_index_item_count;
+			if (ix < target_prefetch_pages) {
+				pfch_index_plp->pfch_indexid[ix].pfch_blocknum = blkno;
+				pfch_index_plp->pfch_indexid[ix].pfch_discard = discard_status;
+				pfch_index_plp->pfch_index_item_count = (ix+1);
+                rc = 0;
+				goto stored_pagenum;
+			}
+			pfch_index_plp = pfch_index_plp->pfch_index_pagelist_next;
+			num_chunks--;              /*  keep track of number of chunks */
+		}
+
+		/*   we did not find any free space in existing chunks -
+		**   create new chunk if within our limit and we have a pfch_index_page_list
+		*/
+		if ( (num_chunks > 0) && ((struct pfch_index_pagelist*)0 != pfch_index_page_list_anchor) ) {
+			pfch_index_plp = (struct pfch_index_pagelist*)palloc( sizeof(struct pfch_index_pagelist) + ( (target_prefetch_pages-1) * sizeof(struct pfch_index_item) ) );
+			if ( (struct pfch_index_pagelist*)0 != pfch_index_plp ) {
+				pfch_index_plp->pfch_index_pagelist_next = pfch_index_page_list_anchor;  /* old head of list is next after this */
+				pfch_index_plp->pfch_indexid[0].pfch_blocknum = blkno;
+				pfch_index_plp->pfch_indexid[0].pfch_discard = discard_status;
+				pfch_index_plp->pfch_index_item_count = 1;
+				pfch_index_page_list_P = &pfch_index_plp;   /*  new head of list is new chunk */
+                rc = 0;
+			}
+		}
+
+    stored_pagenum:;
+     return rc;
+}
+
+/*  _bt_subtract_block removes a block from the prefetched-but-unread pagelist if present */
+void
+_bt_subtract_block(BlockNumber blkno , struct pfch_index_pagelist* pfch_index_page_list)
+{
+    struct pfch_index_pagelist* pfch_index_plp = pfch_index_page_list;
+	if ( (blkno != P_NEW) && (blkno != P_NONE) ) {
+            int ix , jx;
+                while ( (struct pfch_index_pagelist*)0 != pfch_index_plp ) {
+                    ix = pfch_index_plp->pfch_index_item_count;
+                    while (ix-- > 0) {
+                        if (pfch_index_plp->pfch_indexid[ix].pfch_blocknum == blkno) {
+                            /*   move the last item to the curent (now deleted) position and decrement count */
+                            jx = (pfch_index_plp->pfch_index_item_count-1); /*  index of last item ... */
+                            if (jx > ix) {                                  /*  ... is not the current one so move is required */
+                                pfch_index_plp->pfch_indexid[ix].pfch_blocknum = pfch_index_plp->pfch_indexid[jx].pfch_blocknum;
+                                pfch_index_plp->pfch_indexid[ix].pfch_discard = pfch_index_plp->pfch_indexid[jx].pfch_discard;
+                                ix = jx;
+                            }
+                            pfch_index_plp->pfch_index_item_count = ix;
+                            goto done_subtract;
+                        }
+                    }
+                    pfch_index_plp = pfch_index_plp->pfch_index_pagelist_next;
+                }
+        }
+    done_subtract:  return;
+}
+
+/*
  *	_bt_getbuf() -- Get a buffer by block number for read or write.
  *
  *		blkno == P_NEW means to get an unallocated index page.  The page
@@ -573,7 +737,7 @@ _bt_log_reuse_page(Relation rel, BlockNumber blkno, TransactionId latestRemovedX
  *		_bt_checkpage to sanity-check the page (except in P_NEW case).
  */
 Buffer
-_bt_getbuf(Relation rel, BlockNumber blkno, int access)
+_bt_getbuf(Relation rel, BlockNumber blkno, int access , struct pfch_index_pagelist* pfch_index_page_list)
 {
 	Buffer		buf;
 
@@ -581,6 +745,10 @@ _bt_getbuf(Relation rel, BlockNumber blkno, int access)
 	{
 		/* Read an existing block of the relation */
 		buf = ReadBuffer(rel, blkno);
+
+        /*  if the block is in the prefetched-but-unread pagelist,  remove it */
+        _bt_subtract_block( blkno , pfch_index_page_list);
+
 		LockBuffer(buf, access);
 		_bt_checkpage(rel, buf);
 	}
@@ -702,6 +870,10 @@ _bt_getbuf(Relation rel, BlockNumber blkno, int access)
  * bufmgr when one would do.  However, now it's mainly just a notational
  * convenience.  The only case where it saves work over _bt_relbuf/_bt_getbuf
  * is when the target page is the same one already in the buffer.
+ *
+ * if prefetching of index pages is changed to use this function,
+ * then it should be extended to take the index_page_list as parameter
+ * and call_bt_subtract_block in the same way that _bt_getbuf does.
  */
 Buffer
 _bt_relandgetbuf(Relation rel, Buffer obuf, BlockNumber blkno, int access)
@@ -712,6 +884,7 @@ _bt_relandgetbuf(Relation rel, Buffer obuf, BlockNumber blkno, int access)
 	if (BufferIsValid(obuf))
 		LockBuffer(obuf, BUFFER_LOCK_UNLOCK);
 	buf = ReleaseAndReadBuffer(obuf, rel, blkno);
+
 	LockBuffer(buf, access);
 	_bt_checkpage(rel, buf);
 	return buf;
@@ -965,7 +1138,7 @@ _bt_is_page_halfdead(Relation rel, BlockNumber blk)
 	BTPageOpaque opaque;
 	bool		result;
 
-	buf = _bt_getbuf(rel, blk, BT_READ);
+	buf = _bt_getbuf(rel, blk, BT_READ , (struct pfch_index_pagelist*)0);
 	page = BufferGetPage(buf);
 	opaque = (BTPageOpaque) PageGetSpecialPointer(page);
 
@@ -1069,7 +1242,7 @@ _bt_lock_branch_parent(Relation rel, BlockNumber child, BTStack stack,
 				Page		lpage;
 				BTPageOpaque lopaque;
 
-				lbuf = _bt_getbuf(rel, leftsib, BT_READ);
+				lbuf = _bt_getbuf(rel, leftsib, BT_READ, (struct pfch_index_pagelist*)0);
 				lpage = BufferGetPage(lbuf);
 				lopaque = (BTPageOpaque) PageGetSpecialPointer(lpage);
 
@@ -1265,7 +1438,7 @@ _bt_pagedel(Relation rel, Buffer buf)
 					BTPageOpaque lopaque;
 					Page		lpage;
 
-					lbuf = _bt_getbuf(rel, leftsib, BT_READ);
+					lbuf = _bt_getbuf(rel, leftsib, BT_READ, (struct pfch_index_pagelist*)0);
 					lpage = BufferGetPage(lbuf);
 					lopaque = (BTPageOpaque) PageGetSpecialPointer(lpage);
 					/*
@@ -1340,7 +1513,7 @@ _bt_pagedel(Relation rel, Buffer buf)
 		if (!rightsib_empty)
 			break;
 
-		buf = _bt_getbuf(rel, rightsib, BT_WRITE);
+		buf = _bt_getbuf(rel, rightsib, BT_WRITE, (struct pfch_index_pagelist*)0);
 	}
 
 	return ndeleted;
@@ -1593,7 +1766,7 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, bool *rightsib_empty)
 		target = topblkno;
 
 		/* fetch the block number of the topmost parent's left sibling */
-		buf = _bt_getbuf(rel, topblkno, BT_READ);
+		buf = _bt_getbuf(rel, topblkno, BT_READ, (struct pfch_index_pagelist*)0);
 		page = BufferGetPage(buf);
 		opaque = (BTPageOpaque) PageGetSpecialPointer(page);
 		leftsib = opaque->btpo_prev;
@@ -1632,7 +1805,7 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, bool *rightsib_empty)
 		LockBuffer(leafbuf, BT_WRITE);
 	if (leftsib != P_NONE)
 	{
-		lbuf = _bt_getbuf(rel, leftsib, BT_WRITE);
+		lbuf = _bt_getbuf(rel, leftsib, BT_WRITE , (struct pfch_index_pagelist*)0);
 		page = BufferGetPage(lbuf);
 		opaque = (BTPageOpaque) PageGetSpecialPointer(page);
 		while (P_ISDELETED(opaque) || opaque->btpo_next != target)
@@ -1646,7 +1819,7 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, bool *rightsib_empty)
 					 RelationGetRelationName(rel));
 				return false;
 			}
-			lbuf = _bt_getbuf(rel, leftsib, BT_WRITE);
+			lbuf = _bt_getbuf(rel, leftsib, BT_WRITE , (struct pfch_index_pagelist*)0);
 			page = BufferGetPage(lbuf);
 			opaque = (BTPageOpaque) PageGetSpecialPointer(page);
 		}
@@ -1701,7 +1874,7 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, bool *rightsib_empty)
 	 * And next write-lock the (current) right sibling.
 	 */
 	rightsib = opaque->btpo_next;
-	rbuf = _bt_getbuf(rel, rightsib, BT_WRITE);
+	rbuf = _bt_getbuf(rel, rightsib, BT_WRITE , (struct pfch_index_pagelist*)0);
 	page = BufferGetPage(rbuf);
 	opaque = (BTPageOpaque) PageGetSpecialPointer(page);
 	if (opaque->btpo_prev != target)
@@ -1731,7 +1904,7 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, bool *rightsib_empty)
 		if (P_RIGHTMOST(opaque))
 		{
 			/* rightsib will be the only one left on the level */
-			metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE);
+			metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE , (struct pfch_index_pagelist*)0);
 			metapg = BufferGetPage(metabuf);
 			metad = BTPageGetMeta(metapg);
 
diff --git a/src/backend/access/nbtree/nbtree.c b/src/backend/access/nbtree/nbtree.c
index 36dc6c2..ceb5af9 100644
--- a/src/backend/access/nbtree/nbtree.c
+++ b/src/backend/access/nbtree/nbtree.c
@@ -30,6 +30,18 @@
 #include "tcop/tcopprot.h"
 #include "utils/memutils.h"
 
+#ifdef USE_PREFETCH
+extern unsigned int prefetch_index_scans; /* boolean whether to prefetch non-bitmap index scans also numeric size of pfch_list */
+#endif   /* USE_PREFETCH */
+
+Datum
+btpeeknexttuple(
+#ifdef AVOID_CATALOG_MIGRATION_FOR_ASYNCIO
+        IndexScanDesc scan
+#else /*  not AVOID_CATALOG_MIGRATION_FOR_ASYNCIO */
+        PG_FUNCTION_ARGS
+#endif /* not AVOID_CATALOG_MIGRATION_FOR_ASYNCIO */
+);
 
 /* Working state for btbuild and its callback */
 typedef struct
@@ -332,6 +344,74 @@ btgettuple(PG_FUNCTION_ARGS)
 }
 
 /*
+ *	btpeeknexttuple() -- peek at the next tuple different from any blocknum in pfch_block_item_list
+ *                           without reading a new index page
+ *                       and without causing any side-effects such as altering values in control blocks
+ *               if found,     store blocknum in next element of pfch_block_item_list
+ */
+Datum
+btpeeknexttuple(
+#ifdef AVOID_CATALOG_MIGRATION_FOR_ASYNCIO
+        IndexScanDesc scan
+#else /*  not AVOID_CATALOG_MIGRATION_FOR_ASYNCIO */
+        PG_FUNCTION_ARGS
+#endif /* not AVOID_CATALOG_MIGRATION_FOR_ASYNCIO */
+)
+{
+#ifndef AVOID_CATALOG_MIGRATION_FOR_ASYNCIO
+    IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
+#endif /* not AVOID_CATALOG_MIGRATION_FOR_ASYNCIO */
+	BTScanOpaque so = (BTScanOpaque) scan->opaque;
+	bool		res = false;
+	int		itemIndex;		/* current index in items[] */
+
+        /*
+         * If we've already initialized this scan, we can just advance it in
+         * the appropriate direction.  If we haven't done so yet, bail out
+         */
+        if ( BTScanPosIsValid(so->currPos) ) {
+
+            itemIndex = so->currPos.itemIndex+1;    /*   next item */
+
+            /* This loop handles advancing till we find different data block or end of index page */
+            while (itemIndex <= so->currPos.lastItem) {
+                unsigned short int pfchx; /*  index in BlockIdData array   */
+                for (pfchx = 0; pfchx < scan->pfch_used; pfchx++) {
+                        if (BlockIdEquals((&(((scan->pfch_block_item_list)+pfchx)->pfch_blockid)) , &(so->currPos.items[itemIndex].heapTid.ip_blkid))) {
+                             goto block_match;
+                        }
+                }
+
+                /*  if we reach here,  no block in list matched this item  */
+                res = true;
+                /*   set item in prefetch list
+                **   prefer unused entry if there is one,  else overwrite
+                */
+                if (scan->pfch_used < prefetch_index_scans) {
+                    scan->pfch_next = scan->pfch_used;
+                } else {
+                    scan->pfch_next++;
+                    if (scan->pfch_next >= prefetch_index_scans) {
+                        scan->pfch_next = 0;
+                    }
+                }
+
+                BlockIdCopy((&((scan->pfch_block_item_list + scan->pfch_next)->pfch_blockid)) , &(so->currPos.items[itemIndex].heapTid.ip_blkid));
+                if (scan->pfch_used <= scan->pfch_next) {
+                     scan->pfch_used = (scan->pfch_next + 1);
+                }
+
+                goto peek_complete;
+
+      block_match:         itemIndex++;
+            }
+	}
+
+ peek_complete:
+	PG_RETURN_BOOL(res);
+}
+
+/*
  * btgetbitmap() -- gets all matching tuples, and adds them to a bitmap
  */
 Datum
@@ -425,6 +505,12 @@ btbeginscan(PG_FUNCTION_ARGS)
 	so->killedItems = NULL;		/* until needed */
 	so->numKilled = 0;
 
+	so->backSeqRun = 0;
+	so->backSeqPos = 0;
+	so->prefetchItemIndex = 0;
+	so->lastHeapPrefetchBlkno = P_NONE;
+	so->prefetchBlockCount = 0;
+	
 	/*
 	 * We don't know yet whether the scan will be index-only, so we do not
 	 * allocate the tuple workspace arrays until btrescan.  However, we set up
@@ -516,6 +602,23 @@ btendscan(PG_FUNCTION_ARGS)
 {
 	IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
 	BTScanOpaque so = (BTScanOpaque) scan->opaque;
+        struct pfch_index_pagelist* pfch_index_plp;
+        int ix;
+
+#ifdef USE_PREFETCH
+
+	/* discard all prefetched but unread index pages listed in the pagelist */
+        pfch_index_plp = scan->pfch_index_page_list;
+        while ( (struct pfch_index_pagelist*)0 != pfch_index_plp ) {
+                    ix = pfch_index_plp->pfch_index_item_count;
+                    while (ix-- > 0) {
+                        if (pfch_index_plp->pfch_indexid[ix].pfch_discard) {
+                            DiscardBuffer( scan->indexRelation , MAIN_FORKNUM , pfch_index_plp->pfch_indexid[ix].pfch_blocknum);
+                        }
+                    }
+                    pfch_index_plp = pfch_index_plp->pfch_index_pagelist_next;
+        }
+#endif /* USE_PREFETCH */
 
 	/* we aren't holding any read locks, but gotta drop the pins */
 	if (BTScanPosIsValid(so->currPos))
diff --git a/src/backend/access/nbtree/nbtsearch.c b/src/backend/access/nbtree/nbtsearch.c
index 203b969..bee1f12 100644
--- a/src/backend/access/nbtree/nbtsearch.c
+++ b/src/backend/access/nbtree/nbtsearch.c
@@ -23,13 +23,16 @@
 #include "utils/lsyscache.h"
 #include "utils/rel.h"
 
+extern unsigned int prefetch_btree_heaps;  /*  boolean whether to prefetch heap pages in _bt_next for non-bitmap index scans   */
+extern unsigned int prefetch_sequential_index_scans;  /*  boolean whether to prefetch sequential-access non-bitmap index scans  */
 
 static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir,
 			 OffsetNumber offnum);
 static void _bt_saveitem(BTScanOpaque so, int itemIndex,
 			 OffsetNumber offnum, IndexTuple itup);
-static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir);
-static Buffer _bt_walk_left(Relation rel, Buffer buf);
+static bool _bt_steppage(IndexScanDesc scan, ScanDirection dir, 
+			 bool prefetch);
+static Buffer _bt_walk_left(IndexScanDesc scan, Relation rel, Buffer buf);
 static bool _bt_endpoint(IndexScanDesc scan, ScanDirection dir);
 
 
@@ -226,7 +229,7 @@ _bt_moveright(Relation rel,
 				_bt_relbuf(rel, buf);
 
 			/* re-acquire the lock in the right mode, and re-check */
-			buf = _bt_getbuf(rel, blkno, access);
+			buf = _bt_getbuf(rel, blkno, access , (struct pfch_index_pagelist*)0);
 			continue;
 		}
 
@@ -1005,7 +1008,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
 		 * There's no actually-matching data on this page.  Try to advance to
 		 * the next page.  Return false if there's no matching data at all.
 		 */
-		if (!_bt_steppage(scan, dir))
+		if (!_bt_steppage(scan, dir, false))
 			return false;
 	}
 
@@ -1040,6 +1043,8 @@ _bt_next(IndexScanDesc scan, ScanDirection dir)
 {
 	BTScanOpaque so = (BTScanOpaque) scan->opaque;
 	BTScanPosItem *currItem;
+	BlockNumber prevblkno = ItemPointerGetBlockNumber(
+		&scan->xs_ctup.t_self);
 
 	/*
 	 * Advance to next tuple on current page; or if there's no more, try to
@@ -1052,11 +1057,53 @@ _bt_next(IndexScanDesc scan, ScanDirection dir)
 			/* We must acquire lock before applying _bt_steppage */
 			Assert(BufferIsValid(so->currPos.buf));
 			LockBuffer(so->currPos.buf, BT_READ);
-			if (!_bt_steppage(scan, dir))
+			if (!_bt_steppage(scan, dir, target_prefetch_pages > 0))
 				return false;
 			/* Drop the lock, but not pin, on the new page */
 			LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK);
 		}
+		
+		if (prefetch_btree_heaps && (target_prefetch_pages > 0)) {
+			BlockNumber currblkno = ItemPointerGetBlockNumber(
+				&so->currPos.items[so->currPos.itemIndex].heapTid);
+		
+			if (currblkno != prevblkno) {
+				if (so->prefetchBlockCount > 0)
+					so->prefetchBlockCount--;
+			
+				/* If we have heap fetch frequency stats, and it's above ~94%,
+				 * initiate heap prefetches */
+				if (so->currPos.moreRight
+					&& scan->heap_tids_seen > 256 
+					&& ( (scan->heap_tids_seen - scan->heap_tids_seen/16)
+						 <= scan->heap_tids_fetched ) )
+				{
+					bool nonsequential = false;
+				
+					if (so->prefetchItemIndex <= so->currPos.itemIndex)
+						so->prefetchItemIndex = so->currPos.itemIndex + 1;
+					while (    (so->prefetchItemIndex <= so->currPos.lastItem)
+							&& (so->prefetchBlockCount < target_prefetch_pages) )
+					{
+						ItemPointer tid = &so->currPos.items[so->prefetchItemIndex++].heapTid;
+						BlockNumber blkno = ItemPointerGetBlockNumber(tid);
+						if (blkno != so->lastHeapPrefetchBlkno) {  /*  if not a repetition of previous block */
+							/*   start prefetch on next page, providing :
+                            **   EITHER  .  we're reading non-sequentially previously or for this block
+                            **   OR      .  user explicitly specified to prefetch for sequential pattern
+							**   as it may be counterproductive otherwise
+                            */
+							nonsequential = (nonsequential || blkno != (so->lastHeapPrefetchBlkno+1));
+							if (prefetch_sequential_index_scans || nonsequential) {
+								_bt_prefetchbuf(scan->heapRelation, blkno , &scan->pfch_index_page_list );
+							}
+							so->lastHeapPrefetchBlkno = blkno;
+							so->prefetchBlockCount++;
+						}
+					}
+				}
+			}
+		}
 	}
 	else
 	{
@@ -1065,11 +1112,53 @@ _bt_next(IndexScanDesc scan, ScanDirection dir)
 			/* We must acquire lock before applying _bt_steppage */
 			Assert(BufferIsValid(so->currPos.buf));
 			LockBuffer(so->currPos.buf, BT_READ);
-			if (!_bt_steppage(scan, dir))
+			if (!_bt_steppage(scan, dir, target_prefetch_pages > 0))
 				return false;
 			/* Drop the lock, but not pin, on the new page */
 			LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK);
 		}
+		
+		if (prefetch_btree_heaps && (target_prefetch_pages > 0)) {
+			BlockNumber currblkno = ItemPointerGetBlockNumber(
+				&so->currPos.items[so->currPos.itemIndex].heapTid);
+		
+			if (currblkno != prevblkno) {
+				if (so->prefetchBlockCount > 0)
+					so->prefetchBlockCount--;
+			
+				/* If we have heap fetch frequency stats, and it's above ~94%,
+				 * initiate heap prefetches */
+				if (so->currPos.moreLeft
+					&& scan->heap_tids_seen > 256 
+					&& ( (scan->heap_tids_seen - scan->heap_tids_seen/16)
+						 <= scan->heap_tids_fetched ) )
+				{
+					bool nonsequential = false;
+			
+					if (so->prefetchItemIndex >= so->currPos.itemIndex)
+						so->prefetchItemIndex = so->currPos.itemIndex - 1;
+					while (    (so->prefetchItemIndex >= so->currPos.firstItem)
+							&& (so->prefetchBlockCount < target_prefetch_pages) )
+					{
+						ItemPointer tid = &so->currPos.items[so->prefetchItemIndex--].heapTid;
+						BlockNumber blkno = ItemPointerGetBlockNumber(tid);
+						if (blkno != so->lastHeapPrefetchBlkno) {  /*  if not a repetition of previous block */
+							/*   start prefetch on next page, providing :
+                            **   EITHER  .  we're reading non-sequentially previously or for this block
+                            **   OR      .  user explicitly specified to prefetch for sequential pattern
+							**   as it may be counterproductive otherwise
+                            */
+							nonsequential = (nonsequential || blkno != (so->lastHeapPrefetchBlkno+1));
+							if (prefetch_sequential_index_scans || nonsequential) {
+								_bt_prefetchbuf(scan->heapRelation, blkno , &scan->pfch_index_page_list );
+							}
+							so->lastHeapPrefetchBlkno = blkno;
+							so->prefetchBlockCount++;
+						}
+					}
+				}
+			}
+		}
 	}
 
 	/* OK, itemIndex says what to return */
@@ -1119,9 +1208,11 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum)
 	/*
 	 * we must save the page's right-link while scanning it; this tells us
 	 * where to step right to after we're done with these items.  There is no
-	 * corresponding need for the left-link, since splits always go right.
+	 * corresponding need for the left-link, since splits always go right,
+	 * but we need it for back-sequential scan detection.
 	 */
 	so->currPos.nextPage = opaque->btpo_next;
+	so->currPos.prevPage = opaque->btpo_prev;
 
 	/* initialize tuple workspace to empty */
 	so->currPos.nextTupleOffset = 0;
@@ -1156,6 +1247,7 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum)
 		so->currPos.firstItem = 0;
 		so->currPos.lastItem = itemIndex - 1;
 		so->currPos.itemIndex = 0;
+		so->prefetchItemIndex = 0;
 	}
 	else
 	{
@@ -1187,6 +1279,7 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum)
 		so->currPos.firstItem = itemIndex;
 		so->currPos.lastItem = MaxIndexTuplesPerPage - 1;
 		so->currPos.itemIndex = MaxIndexTuplesPerPage - 1;
+		so->prefetchItemIndex = MaxIndexTuplesPerPage - 1;
 	}
 
 	return (so->currPos.firstItem <= so->currPos.lastItem);
@@ -1224,7 +1317,7 @@ _bt_saveitem(BTScanOpaque so, int itemIndex,
  * locks and pins, set so->currPos.buf to InvalidBuffer, and return FALSE.
  */
 static bool
-_bt_steppage(IndexScanDesc scan, ScanDirection dir)
+_bt_steppage(IndexScanDesc scan, ScanDirection dir, bool prefetch)
 {
 	BTScanOpaque so = (BTScanOpaque) scan->opaque;
 	Relation	rel;
@@ -1278,7 +1371,7 @@ _bt_steppage(IndexScanDesc scan, ScanDirection dir)
 			/* check for interrupts while we're not holding any buffer lock */
 			CHECK_FOR_INTERRUPTS();
 			/* step right one page */
-			so->currPos.buf = _bt_getbuf(rel, blkno, BT_READ);
+			so->currPos.buf = _bt_getbuf(rel, blkno, BT_READ , scan->pfch_index_page_list);
 			/* check for deleted page */
 			page = BufferGetPage(so->currPos.buf);
 			opaque = (BTPageOpaque) PageGetSpecialPointer(page);
@@ -1287,9 +1380,20 @@ _bt_steppage(IndexScanDesc scan, ScanDirection dir)
 				PredicateLockPage(rel, blkno, scan->xs_snapshot);
 				/* see if there are any matches on this page */
 				/* note that this will clear moreRight if we can stop */
-				if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque)))
+				if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque))) {
+					if (    prefetch && so->currPos.moreRight
+						/*   start prefetch on next page, providing :
+						**   EITHER  .  we're reading non-sequentially for this block
+						**   OR      .  user explicitly specified to prefetch for sequential pattern
+						**   as it may be counterproductive otherwise
+						*/
+						&& (prefetch_sequential_index_scans || opaque->btpo_next != (blkno+1))
+                       ) {
+ 						  _bt_prefetchbuf(rel, opaque->btpo_next , &scan->pfch_index_page_list);
+					}
 					break;
 			}
+			}
 			/* nope, keep going */
 			blkno = opaque->btpo_next;
 		}
@@ -1317,7 +1421,7 @@ _bt_steppage(IndexScanDesc scan, ScanDirection dir)
 			}
 
 			/* Step to next physical page */
-			so->currPos.buf = _bt_walk_left(rel, so->currPos.buf);
+			so->currPos.buf = _bt_walk_left(scan , rel, so->currPos.buf);
 
 			/* if we're physically at end of index, return failure */
 			if (so->currPos.buf == InvalidBuffer)
@@ -1332,14 +1436,58 @@ _bt_steppage(IndexScanDesc scan, ScanDirection dir)
 			opaque = (BTPageOpaque) PageGetSpecialPointer(page);
 			if (!P_IGNORE(opaque))
 			{
+				/* We must rely on the previously saved prevPage link! */
+				BlockNumber blkno = so->currPos.prevPage;
+				
 				PredicateLockPage(rel, BufferGetBlockNumber(so->currPos.buf), scan->xs_snapshot);
 				/* see if there are any matches on this page */
 				/* note that this will clear moreLeft if we can stop */
-				if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page)))
+				if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page))) {
+					if (prefetch && so->currPos.moreLeft) {
+						/* detect back-sequential runs and increase prefetch window blindly 
+						 * downwards 2 blocks at a time. This only works in our favor
+						 * for index-only scans, by merging read requests at the kernel,
+						 * so we want to inflate target_prefetch_pages since merged 
+						 * back-sequential requests are about as expensive as a single one 
+						 */
+						if (scan->xs_want_itup && blkno > 0 && opaque->btpo_prev == (blkno-1)) {
+							BlockNumber backPos;
+							unsigned int back_prefetch_pages = target_prefetch_pages * 16;
+							if (back_prefetch_pages > 64)
+								back_prefetch_pages = 64;
+							
+							if (so->backSeqRun == 0)
+								backPos = (blkno-1);
+							else
+								backPos = so->backSeqPos;
+							so->backSeqRun++;
+							
+							if (backPos > 0 && (blkno - backPos) <= back_prefetch_pages) {
+								_bt_prefetchbuf(rel, backPos-- , &scan->pfch_index_page_list);
+								/* don't start back-seq prefetch too early */
+								if (so->backSeqRun >= back_prefetch_pages
+										&& backPos > 0 
+										&& (blkno - backPos) <= back_prefetch_pages)
+								{
+									_bt_prefetchbuf(rel, backPos-- , &scan->pfch_index_page_list);
+								}
+							}
+							
+							so->backSeqPos = backPos;
+						} else {
+							/* start prefetch on next page */
+							if (so->backSeqRun != 0) {
+								if (opaque->btpo_prev > blkno || opaque->btpo_prev < so->backSeqPos)
+									so->backSeqRun = 0;
+							}
+							_bt_prefetchbuf(rel, opaque->btpo_prev , &scan->pfch_index_page_list);
+						}
+					}
 					break;
 			}
 		}
 	}
+	}
 
 	return true;
 }
@@ -1359,7 +1507,7 @@ _bt_steppage(IndexScanDesc scan, ScanDirection dir)
  * again if it's important.
  */
 static Buffer
-_bt_walk_left(Relation rel, Buffer buf)
+_bt_walk_left(IndexScanDesc scan, Relation rel, Buffer buf)
 {
 	Page		page;
 	BTPageOpaque opaque;
@@ -1387,7 +1535,7 @@ _bt_walk_left(Relation rel, Buffer buf)
 		_bt_relbuf(rel, buf);
 		/* check for interrupts while we're not holding any buffer lock */
 		CHECK_FOR_INTERRUPTS();
-		buf = _bt_getbuf(rel, blkno, BT_READ);
+		buf = _bt_getbuf(rel, blkno, BT_READ , scan->pfch_index_page_list );
 		page = BufferGetPage(buf);
 		opaque = (BTPageOpaque) PageGetSpecialPointer(page);
 
@@ -1631,7 +1779,7 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
 		 * There's no actually-matching data on this page.  Try to advance to
 		 * the next page.  Return false if there's no matching data at all.
 		 */
-		if (!_bt_steppage(scan, dir))
+		if (!_bt_steppage(scan, dir, false))
 			return false;
 	}
 
diff --git a/src/backend/executor/instrument.c b/src/backend/executor/instrument.c
index a5118a4..c8c6fee 100644
--- a/src/backend/executor/instrument.c
+++ b/src/backend/executor/instrument.c
@@ -41,6 +41,14 @@ InstrAlloc(int n, int instrument_options)
 		{
 			instr[i].need_bufusage = need_buffers;
 			instr[i].need_timer = need_timer;
+			instr[i].bufusage_start.aio_read_noneed = 0;
+			instr[i].bufusage_start.aio_read_discrd = 0;
+			instr[i].bufusage_start.aio_read_forgot = 0;
+			instr[i].bufusage_start.aio_read_noblok = 0;
+			instr[i].bufusage_start.aio_read_failed = 0;
+			instr[i].bufusage_start.aio_read_wasted = 0;
+			instr[i].bufusage_start.aio_read_waited = 0;
+			instr[i].bufusage_start.aio_read_ontime = 0;
 		}
 	}
 
@@ -143,6 +151,16 @@ BufferUsageAccumDiff(BufferUsage *dst,
 	dst->local_blks_written += add->local_blks_written - sub->local_blks_written;
 	dst->temp_blks_read += add->temp_blks_read - sub->temp_blks_read;
 	dst->temp_blks_written += add->temp_blks_written - sub->temp_blks_written;
+
+	dst->aio_read_noneed       += add->aio_read_noneed - sub->aio_read_noneed;
+	dst->aio_read_discrd       += add->aio_read_discrd - sub->aio_read_discrd;
+	dst->aio_read_forgot       += add->aio_read_forgot - sub->aio_read_forgot;
+	dst->aio_read_noblok       += add->aio_read_noblok - sub->aio_read_noblok;
+	dst->aio_read_failed       += add->aio_read_failed - sub->aio_read_failed;
+	dst->aio_read_wasted       += add->aio_read_wasted - sub->aio_read_wasted;
+	dst->aio_read_waited       += add->aio_read_waited - sub->aio_read_waited;
+	dst->aio_read_ontime       += add->aio_read_ontime - sub->aio_read_ontime;
+
 	INSTR_TIME_ACCUM_DIFF(dst->blk_read_time,
 						  add->blk_read_time, sub->blk_read_time);
 	INSTR_TIME_ACCUM_DIFF(dst->blk_write_time,
diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c
index 9b1e975..f5e6fd8 100644
--- a/src/backend/executor/nodeBitmapHeapscan.c
+++ b/src/backend/executor/nodeBitmapHeapscan.c
@@ -34,6 +34,8 @@
  *		ExecEndBitmapHeapScan		releases all storage.
  */
 #include "postgres.h"
+#include <sys/types.h> 
+#include <unistd.h>
 
 #include "access/relscan.h"
 #include "access/transam.h"
@@ -47,6 +49,10 @@
 #include "utils/snapmgr.h"
 #include "utils/tqual.h"
 
+#ifdef USE_PREFETCH
+extern unsigned int prefetch_dbOid; /*  database oid of relations on which prefetching to be done - 0 means all */
+extern unsigned int prefetch_bitmap_scans; /*  boolean whether to prefetch bitmap heap scans         */
+#endif   /* USE_PREFETCH */
 
 static TupleTableSlot *BitmapHeapNext(BitmapHeapScanState *node);
 static void bitgetpage(HeapScanDesc scan, TBMIterateResult *tbmres);
@@ -111,10 +117,21 @@ BitmapHeapNext(BitmapHeapScanState *node)
 		node->tbmres = tbmres = NULL;
 
 #ifdef USE_PREFETCH
-		if (target_prefetch_pages > 0)
-		{
+		if (    prefetch_bitmap_scans
+                     && (target_prefetch_pages > 0)
+                     && (    (   (prefetch_dbOid > 0)
+                               && (prefetch_dbOid == scan->rs_rd->rd_node.dbNode)
+                             )
+                         ||  (prefetch_dbOid == 0)
+                        )
+                        /* sufficient number of blocks - at least twice the target_prefetch_pages */
+                     && (scan->rs_nblocks > (2*target_prefetch_pages))
+                   ) {
 			node->prefetch_iterator = prefetch_iterator = tbm_begin_iterate(tbm);
 			node->prefetch_pages = 0;
+                        if (prefetch_iterator) {
+                          tbm_zero(prefetch_iterator);  /* zero list of prefetched and unread blocknos */
+                        }
 			node->prefetch_target = -1;
 		}
 #endif   /* USE_PREFETCH */
@@ -138,12 +155,14 @@ BitmapHeapNext(BitmapHeapScanState *node)
 			}
 
 #ifdef USE_PREFETCH
+                        if (prefetch_iterator) {
 			if (node->prefetch_pages > 0)
 			{
 				/* The main iterator has closed the distance by one page */
 				node->prefetch_pages--;
+                                tbm_subtract(prefetch_iterator, tbmres->blockno); /* remove this blockno from list of prefetched and unread blocknos */
 			}
-			else if (prefetch_iterator)
+                            else
 			{
 				/* Do not let the prefetch iterator get behind the main one */
 				TBMIterateResult *tbmpre = tbm_iterate(prefetch_iterator);
@@ -151,6 +170,7 @@ BitmapHeapNext(BitmapHeapScanState *node)
 				if (tbmpre == NULL || tbmpre->blockno != tbmres->blockno)
 					elog(ERROR, "prefetch and main iterators are out of sync");
 			}
+                        }
 #endif   /* USE_PREFETCH */
 
 			/*
@@ -239,16 +259,26 @@ BitmapHeapNext(BitmapHeapScanState *node)
 			while (node->prefetch_pages < node->prefetch_target)
 			{
 				TBMIterateResult *tbmpre = tbm_iterate(prefetch_iterator);
+                                int             PrefetchBufferRc; /*  return value from PrefetchBuffer  - refer to bufmgr.h */
+
 
 				if (tbmpre == NULL)
 				{
 					/* No more pages to prefetch */
-					tbm_end_iterate(prefetch_iterator);
-					node->prefetch_iterator = prefetch_iterator = NULL;
+                                        /* let ExecEndBitmapHeapScan terminate the prefetch_iterator
+				        **	tbm_end_iterate(prefetch_iterator);
+					**      node->prefetch_iterator = NULL;
+                                        */
+                                        prefetch_iterator = NULL;
 					break;
 				}
 				node->prefetch_pages++;
-				PrefetchBuffer(scan->rs_rd, MAIN_FORKNUM, tbmpre->blockno);
+				PrefetchBufferRc = PrefetchBuffer(scan->rs_rd, MAIN_FORKNUM, tbmpre->blockno , 0);
+                                /*  add this blockno to list of prefetched and unread blocknos
+                                **  if pin count did not increase then indicate so in the Unread_Pfetched list
+                                */
+                                tbm_add(prefetch_iterator
+                                   ,( (PrefetchBufferRc & PREFTCHRC_BUF_PIN_INCREASED) ? tbmpre->blockno : InvalidBlockNumber ) );
 			}
 		}
 #endif   /* USE_PREFETCH */
@@ -482,12 +512,31 @@ ExecEndBitmapHeapScan(BitmapHeapScanState *node)
 {
 	Relation	relation;
 	HeapScanDesc scanDesc;
+	TBMIterator *prefetch_iterator;
 
 	/*
 	 * extract information from the node
 	 */
 	relation = node->ss.ss_currentRelation;
 	scanDesc = node->ss.ss_currentScanDesc;
+	prefetch_iterator = node->prefetch_iterator;
+
+#ifdef USE_PREFETCH
+        /*  before any other cleanup,  discard any prefetched but unread buffers  */
+        if (prefetch_iterator != NULL) {
+            TBMIterateResult *tbmpre = tbm_locate_IterateResult(prefetch_iterator);
+            BlockNumber *Unread_Pfetched_base = tbmpre->Unread_Pfetched_base;
+            unsigned int Unread_Pfetched_next = tbmpre->Unread_Pfetched_next;   /*  where the next unread blockno is relative to start */
+            unsigned int Unread_Pfetched_count = tbmpre->Unread_Pfetched_count;
+
+            while ((Unread_Pfetched_count--) > 0) {
+                DiscardBuffer( scanDesc->rs_rd, MAIN_FORKNUM, *(Unread_Pfetched_base+Unread_Pfetched_next));
+                Unread_Pfetched_next++;
+                if (Unread_Pfetched_next >= target_prefetch_pages)
+                    Unread_Pfetched_next = 0;
+            }
+        }
+#endif   /* USE_PREFETCH */
 
 	/*
 	 * Free the exprcontext
diff --git a/src/backend/executor/nodeIndexscan.c b/src/backend/executor/nodeIndexscan.c
index 2b89dc6..c4fec3b 100644
--- a/src/backend/executor/nodeIndexscan.c
+++ b/src/backend/executor/nodeIndexscan.c
@@ -35,8 +35,13 @@
 #include "utils/rel.h"
 
 
+
 static TupleTableSlot *IndexNext(IndexScanState *node);
 
+#ifdef USE_PREFETCH
+extern unsigned int prefetch_dbOid; /*  database oid of relations on which prefetching to be done - 0 means all */
+extern unsigned int prefetch_index_scans; /*  boolean whether to prefetch bitmap heap scans         */
+#endif   /* USE_PREFETCH */
 
 /* ----------------------------------------------------------------
  *		IndexNext
@@ -418,7 +423,12 @@ ExecEndIndexScan(IndexScanState *node)
 	 * close the index relation (no-op if we didn't open it)
 	 */
 	if (indexScanDesc)
+        {
 		index_endscan(indexScanDesc);
+
+        /*  note  -  at this point all scan controlblock resources have been freed by IndexScanEnd called by index_endscan */
+
+        }
 	if (indexRelationDesc)
 		index_close(indexRelationDesc, NoLock);
 
@@ -609,6 +619,33 @@ ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
 											   indexstate->iss_NumScanKeys,
 											 indexstate->iss_NumOrderByKeys);
 
+#ifdef USE_PREFETCH
+        /*  initialize prefetching   */
+                indexstate->iss_ScanDesc->pfch_index_page_list =  (struct pfch_index_pagelist*)0;
+                indexstate->iss_ScanDesc->pfch_block_item_list = (struct pfch_block_item*)0;
+		if (    prefetch_index_scans
+			 && (target_prefetch_pages > 0)
+			 &&	(!RelationUsesLocalBuffers(indexstate->iss_ScanDesc->heapRelation)) /* I think this must always be true for an indexed heap ? */
+			 && (    (   (prefetch_dbOid > 0)
+					   && (prefetch_dbOid == indexstate->iss_ScanDesc->heapRelation->rd_node.dbNode)
+					 )
+				 ||  (prefetch_dbOid == 0)
+				)
+		   ) {
+			indexstate->iss_ScanDesc->pfch_index_page_list = palloc( sizeof(struct pfch_index_pagelist) + ( (target_prefetch_pages-1) * sizeof(struct pfch_index_item) ) );
+			indexstate->iss_ScanDesc->pfch_block_item_list = palloc( prefetch_index_scans * sizeof(struct pfch_block_item) );
+			if (     ( (struct pfch_index_pagelist*)0 != indexstate->iss_ScanDesc->pfch_index_page_list )
+                  && ( (struct pfch_block_item*)0 != indexstate->iss_ScanDesc->pfch_block_item_list )
+               ) {
+                          indexstate->iss_ScanDesc->pfch_used = 0;
+                          indexstate->iss_ScanDesc->pfch_next = prefetch_index_scans; /* ensure first entry is at index 0 */
+                          indexstate->iss_ScanDesc->pfch_index_page_list->pfch_index_pagelist_next = (struct pfch_index_pagelist*)0;
+                          indexstate->iss_ScanDesc->pfch_index_page_list->pfch_index_item_count = 0;
+                          indexstate->iss_ScanDesc->do_prefetch = 1;
+            }
+		}
+#endif   /* USE_PREFETCH */
+
 	/*
 	 * If no run-time keys to calculate, go ahead and pass the scankeys to the
 	 * index AM.
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index a880c81..1e34d6a 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -44,6 +44,9 @@
 #include "nodes/bitmapset.h"
 #include "nodes/tidbitmap.h"
 #include "utils/hsearch.h"
+#ifdef USE_PREFETCH
+extern int	target_prefetch_pages;
+#endif   /* USE_PREFETCH */
 
 /*
  * The maximum number of tuples per page is not large (typically 256 with
@@ -572,7 +575,12 @@ tbm_begin_iterate(TIDBitmap *tbm)
 	 * needs of the TBMIterateResult sub-struct.
 	 */
 	iterator = (TBMIterator *) palloc(sizeof(TBMIterator) +
-								 MAX_TUPLES_PER_PAGE * sizeof(OffsetNumber));
+								 MAX_TUPLES_PER_PAGE * sizeof(OffsetNumber)
+#ifdef USE_PREFETCH
+                                          		      /*  space for remembering every prefetched but unread blockno */
+                                          		      +  (target_prefetch_pages * sizeof(BlockNumber))
+#endif   /* USE_PREFETCH */
+                                         );
 	iterator->tbm = tbm;
 
 	/*
@@ -1020,3 +1028,68 @@ tbm_comparator(const void *left, const void *right)
 		return 1;
 	return 0;
 }
+
+void
+tbm_zero(TBMIterator *iterator) /* zero list of prefetched and unread blocknos */
+{
+      /* locate the list of prefetched but unread blocknos immediately following the array of offsets
+      ** and note that tbm_begin_iterate allocates space for (1 + MAX_TUPLES_PER_PAGE) offsets  -
+      ** 1 included in struct TBMIterator and MAX_TUPLES_PER_PAGE additional
+      */
+      iterator->output.Unread_Pfetched_base = ((BlockNumber *)(&(iterator->output.offsets[MAX_TUPLES_PER_PAGE+1])));
+      iterator->output.Unread_Pfetched_next = iterator->output.Unread_Pfetched_count = 0;
+}
+
+void
+tbm_add(TBMIterator *iterator, BlockNumber blockno) /* add this blockno to list of prefetched and unread blocknos */
+{
+      unsigned int Unread_Pfetched_index = iterator->output.Unread_Pfetched_next + iterator->output.Unread_Pfetched_count++;
+
+      if (iterator->output.Unread_Pfetched_count > target_prefetch_pages) {
+		ereport(ERROR,
+			(errcode(ERRCODE_INTERNAL_ERROR),
+                                errmsg("tbm_add overflowed list cannot add blockno %d", blockno)));
+      }
+
+      if (Unread_Pfetched_index >= target_prefetch_pages)
+          Unread_Pfetched_index -= target_prefetch_pages;
+      *(iterator->output.Unread_Pfetched_base + Unread_Pfetched_index) = blockno;
+}
+
+void
+tbm_subtract(TBMIterator *iterator, BlockNumber blockno) /* remove this blockno from list of prefetched and unread blocknos */
+{
+      unsigned int Unread_Pfetched_index = iterator->output.Unread_Pfetched_next++;
+      BlockNumber nextUnreadPfetched;
+
+      /*    make a weak check that the next blockno is the one to be removed,
+      **    although actually in case of disagreement,   we ignore callers blockno and remove next anyway,
+      **    which is really what caller wants
+      */
+      if ( iterator->output.Unread_Pfetched_count == 0 ) {
+		ereport(ERROR,
+			(errcode(ERRCODE_INTERNAL_ERROR),
+                                errmsg("tbm_subtract empty list cannot subtract blockno %d", blockno)));
+      }
+
+      if (Unread_Pfetched_index >= target_prefetch_pages)
+          Unread_Pfetched_index = 0;
+      nextUnreadPfetched = *(iterator->output.Unread_Pfetched_base + Unread_Pfetched_index);
+      if (   ( nextUnreadPfetched != blockno ) 
+          && ( nextUnreadPfetched != InvalidBlockNumber ) /* dont report it if the block in the list was InvalidBlockNumber */
+         ) {
+		ereport(NOTICE,
+			(errcode(ERRCODE_INTERNAL_ERROR),
+                                errmsg("tbm_subtract will subtract blockno %d not %d",
+					nextUnreadPfetched, blockno)));
+      }
+      if (iterator->output.Unread_Pfetched_next >= target_prefetch_pages)
+          iterator->output.Unread_Pfetched_next = 0;
+      iterator->output.Unread_Pfetched_count--;
+}
+
+TBMIterateResult *
+tbm_locate_IterateResult(TBMIterator *iterator)
+{
+   return &(iterator->output);
+}
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index a5d5c2d..a16467d 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -123,6 +123,11 @@
 #include "storage/spin.h"
 #endif
 
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+void ReportFreeBAiocbs(void);
+int CountInuseBAiocbs(void);
+extern int hwmBufferAiocbs;         /*  high water mark of in-use  BufferAiocbs in pool           */
+#endif /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
 
 /*
  * Possible types of a backend. Beyond being the possible bkend_type values in
@@ -1493,9 +1498,15 @@ ServerLoop(void)
 	fd_set		readmask;
 	int			nSockets;
 	time_t		now,
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+                           count_baiocb_time,
+#endif /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
 				last_touch_time;
 
 	last_touch_time = time(NULL);
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+        count_baiocb_time = time(NULL);
+#endif /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
 
 	nSockets = initMasks(&readmask);
 
@@ -1654,6 +1665,19 @@ ServerLoop(void)
 			last_touch_time = now;
 		}
 
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+                /*   maintain the hwm of used baiocbs every 10 seconds  */
+		if ((now - count_baiocb_time) >= 10)
+		{
+                        int inuseBufferAiocbs;         /*  current in-use  BufferAiocbs in pool */
+                        inuseBufferAiocbs = CountInuseBAiocbs();
+                        if (inuseBufferAiocbs > hwmBufferAiocbs) {
+			    hwmBufferAiocbs = inuseBufferAiocbs;
+			}
+			count_baiocb_time = now;
+		}
+#endif /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
+
 		/*
 		 * If we already sent SIGQUIT to children and they are slow to shut
 		 * down, it's time to send them SIGKILL.  This doesn't happen
@@ -3444,6 +3468,9 @@ PostmasterStateMachine(void)
 						signal_child(PgStatPID, SIGQUIT);
 				}
 			}
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+                        ReportFreeBAiocbs();
+#endif /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
 		}
 	}
 
diff --git a/src/backend/storage/buffer/Makefile b/src/backend/storage/buffer/Makefile
index 2c10fba..96aa9e0 100644
--- a/src/backend/storage/buffer/Makefile
+++ b/src/backend/storage/buffer/Makefile
@@ -12,6 +12,6 @@ subdir = src/backend/storage/buffer
 top_builddir = ../../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS = buf_table.o buf_init.o bufmgr.o freelist.o localbuf.o
+OBJS = buf_table.o buf_init.o bufmgr.o freelist.o localbuf.o buf_async.o
 
 include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/storage/buffer/buf_init.c b/src/backend/storage/buffer/buf_init.c
index e03394c..658764d 100644
--- a/src/backend/storage/buffer/buf_init.c
+++ b/src/backend/storage/buffer/buf_init.c
@@ -13,15 +13,89 @@
  *-------------------------------------------------------------------------
  */
 #include "postgres.h"
+#include <sys/types.h> 
+#include <unistd.h>
 
 #include "storage/bufmgr.h"
 #include "storage/buf_internals.h"
-
+#include <stdlib.h> /* for getenv() */
+#include <errno.h> /* for strtoul() */
 
 BufferDesc *BufferDescriptors;
 char	   *BufferBlocks;
-int32	   *PrivateRefCount;
+int32	   *PrivateRefCount;       /*  array of counts per buffer of how many times this task has pinned this buffer */
+
+volatile struct BAiocbAnchor *BAiocbAnchr = (struct BAiocbAnchor *)0;  /*  anchor for all control blocks pertaining to aio  */
+
+int CountInuseBAiocbs(void);     /*  keep compiler happy */
+void ReportFreeBAiocbs(void);    /*  keep compiler happy */
+
+extern int	MaxConnections;  /*  max number of client connections which postmaster will allow  */
+int numBufferAiocbs = 0;         /*  total number of  BufferAiocbs in pool (0 <=> no async io) */
+int hwmBufferAiocbs = 0;         /*  high water mark of in-use  BufferAiocbs in pool
+                                 **  (not required to be accurate, kindly maintained for us somehow by postmaster)
+                                 */
+
+#ifdef USE_PREFETCH
+unsigned int prefetch_dbOid = 0; /*  database oid of relations on which prefetching to be done - 0 means all */
+unsigned int prefetch_bitmap_scans = 1; /*  boolean whether to prefetch bitmap heap scans        */
+unsigned int prefetch_heap_scans = 0;   /*  boolean whether to prefetch non-bitmap heap scans    */
+unsigned int prefetch_sequential_index_scans = 0;  /*  boolean whether to prefetch sequential-access non-bitmap index scans  */
+unsigned int prefetch_index_scans = 256;  /*  boolean whether to prefetch non-bitmap index scans also numeric size of pfch_list  */
+unsigned int prefetch_btree_heaps = 1;  /*  boolean whether to prefetch heap pages in _bt_next for non-bitmap index scans   */
+#endif /* USE_PREFETCH */
 
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+int maxGetBAiocbTries = 1;       /*  max times we will try to get a free BufferAiocb */
+int maxRelBAiocbTries = 1;       /*  max times we will try to release a BufferAiocb back to freelist */
+
+/*  locking protocol for manipulating the BufferAiocbs and FreeBAiocbs list :
+**    1.    ownership of a BufferAiocb :
+**          to gain ownership of a BufferAiocb, a task must
+**          EITHER    remove it from FreeBAiocbs (it is now temporary owner and no other task can find it)
+**                    if decision is to attach it to a buffer descriptor header, then
+**                       .   lock the buffer descriptor header
+**                       .   check  NOT flags & BM_AIO_IN_PROGRESS
+**                       .   attach to buffer descriptor header
+**                       .   increment the BufferAiocb.dependent_count
+**                       .   unlock the buffer descriptor header
+**                     and ownership scope is from lock to unlock
+***          OR        locate it by dereferencing the pointer in a buffer descriptor,
+**                    in which case :
+**                       .   lock the buffer descriptor header
+**                       .   check  flags & BM_AIO_IN_PROGRESS
+**                       .   increment the BufferAiocb.dependent_count
+**                       .   if decision is to return to FreeBAiocbs,
+**                           then   (with buffer descriptor header still locked)
+**                                  .  turn off BM_AIO_IN_PROGRESS
+**                       .   IF        the BufferAiocb.dependent_count == 1 (I am sole dependent)
+**                       .   THEN
+**                       .       .  decrement the BufferAiocb.dependent_count
+**                               .  return to FreeBAiocbs (see below)
+**                       .   unlock the buffer descriptor header
+**                     and ownership scope is from lock to either return to FreeBAiocbs or unlock
+**    2.    adding and removing from FreeBAiocbs :
+**      two alternative methods - controlled by conditional macro definition LOCK_BAIOCB_FOR_GET_REL
+**       2.1 LOCK_BAIOCB_FOR_GET_REL is defined - use a lock
+**          .   lock BufFreelistLock exclusive
+**          .   add / remove from FreeBAiocbs
+**          .   unlock BufFreelistLock exclusive
+**          advantage of this method  -  never fails to add or remove
+**       2.2  LOCK_BAIOCB_FOR_GET_REL is not defined - use compare_and_swap
+**          .   retrieve the current Freelist pointer and validate
+**          .   compare_and_swap on/off the FreeBAiocb list
+**          .   unlock BufFreelistLock exclusive
+**          advantage of this method  -  never waits
+**          to avoid losing a FreeBAiocbs,   save it in a process-local cache and reuse
+*/
+#else   /*  not USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
+        /*   this dummy structure is to ensure that references to these fields in other bufmgr runtime code
+        **   that is not conditionally ifdefd on USE_AIO_ATOMIC_BUILTIN_COMP_SWAP compiles and runs correctly
+        */
+        struct BufferAiocb dummy_BAiocbAnchr = { (struct BufferAiocb*)0 , (struct BufferAiocb*)0 };
+int maxGetBAiocbTries = -1;       /*  max times we will try to get a free BufferAiocb */
+int maxRelBAiocbTries = -1;       /*  max times we will try to release a BufferAiocb back to freelist */
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
 
 /*
  * Data Structures:
@@ -73,7 +147,14 @@ void
 InitBufferPool(void)
 {
 	bool		foundBufs,
-				foundDescs;
+				foundDescs
+                        , foundAiocbs
+          ;
+#if defined(USE_PREFETCH) || defined(USE_AIO_ATOMIC_BUILTIN_COMP_SWAP)
+        char *envvarpointer = (char *)0;  /*  might point to an environment variable string */
+        char *charptr;
+#endif /* USE_PREFETCH */
+
 
 	BufferDescriptors = (BufferDesc *)
 		ShmemInitStruct("Buffer Descriptors",
@@ -83,6 +164,142 @@ InitBufferPool(void)
 		ShmemInitStruct("Buffer Blocks",
 						NBuffers * (Size) BLCKSZ, &foundBufs);
 
+
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+        BAiocbAnchr = (struct BAiocbAnchor *)0; /*  anchor for all control blocks pertaining to aio  */
+        if (max_async_io_prefetchers < 0) {  /*  negative value indicates to initialize to something sensible during buf_init */
+            max_async_io_prefetchers = MaxConnections/6;  /*  default allows for average of MaxConnections/6 concurrent prefetchers  - reasonable ??? */
+        }
+
+        if ((target_prefetch_pages > 0) && (max_async_io_prefetchers > 0)) {
+            int ix;
+            volatile struct BufferAiocb *BufferAiocbs;
+            volatile struct BufferAiocb * volatile FreeBAiocbs;
+
+            numBufferAiocbs = (target_prefetch_pages*max_async_io_prefetchers);  /*  target_prefetch_pages per prefetcher */
+            BAiocbAnchr = (struct BAiocbAnchor *)
+		ShmemInitStruct("Buffer Aiocbs",
+                          sizeof(struct BAiocbAnchor) + (numBufferAiocbs * sizeof(struct BufferAiocb)), &foundAiocbs);
+            if (BAiocbAnchr) {
+                BufferAiocbs = BAiocbAnchr->BufferAiocbs = (struct BufferAiocb*)(((char *)BAiocbAnchr) + sizeof(struct BAiocbAnchor));
+                FreeBAiocbs = (struct BufferAiocb*)0;
+                for (ix = (numBufferAiocbs-1);  ix>=0;  ix--) {
+                    (BufferAiocbs+ix)->BAiocbnext = FreeBAiocbs;   /*  init the free list,  last one -> 0  */
+                    (BufferAiocbs+ix)->BAiocbbufh = (struct sbufdesc*)0;
+                    (BufferAiocbs+ix)->BAiocbDependentCount = 0;
+                    (BufferAiocbs+ix)->pidOfAio = 0;
+                    FreeBAiocbs = (BufferAiocbs+ix);
+
+                }
+                BAiocbAnchr->FreeBAiocbs = FreeBAiocbs;
+                envvarpointer = getenv("PG_MAX_GET_BAIOCB_TRIES");
+                if (    (envvarpointer != (char *)0)
+                     && (   ('1' <= *envvarpointer) && ('9' >= *envvarpointer)   )
+                   ) {
+                      maxGetBAiocbTries = strtol(envvarpointer, 0, 10);
+                }
+                envvarpointer = getenv("PG_MAX_REL_BAIOCB_TRIES");
+                if (    (envvarpointer != (char *)0)
+                     && (   ('1' <= *envvarpointer) && ('9' >= *envvarpointer)   )
+                   ) {
+                      maxRelBAiocbTries = strtol(envvarpointer, 0, 10);
+                }
+
+                /*   init the aio subsystem max number of threads and max number of requests
+                **   max number of threads   <-->  max_async_io_prefetchers
+                **   max number of requests  <-->  numBufferAiocbs = (target_prefetch_pages*max_async_io_prefetchers)
+                **   there is no return code so we just hope.
+                */
+                smgrinitaio(max_async_io_prefetchers , numBufferAiocbs);
+
+            }
+        }
+#else   /*  not USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
+        /*   this dummy structure is to ensure that references to these fields in other bufmgr runtime code
+        **   that is not conditionally ifdefd on USE_AIO_ATOMIC_BUILTIN_COMP_SWAP compiles and runs correctly
+        */
+        BAiocbAnchr = &dummy_BAiocbAnchr;
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
+
+
+#ifdef USE_PREFETCH
+        envvarpointer = getenv("PG_TRY_PREFETCHING_FOR_BITMAP");
+        if (envvarpointer != (char *)0) {
+             if (    ('N' == *envvarpointer) || ('n' == *envvarpointer)   ) {
+                 prefetch_bitmap_scans = 0;
+             } else
+             if (    ('Y' == *envvarpointer) || ('y' == *envvarpointer)   ) {
+                 prefetch_bitmap_scans = 1;
+             }
+        }
+        envvarpointer = getenv("PG_TRY_PREFETCHING_FOR_ISCAN");
+        if (envvarpointer != (char *)0) {
+             if (    ('N' == *envvarpointer) || ('n' == *envvarpointer)   ) {
+                 prefetch_index_scans = 0;
+             } else
+             if (    ('Y' == *envvarpointer) || ('y' == *envvarpointer)   ) {
+                 prefetch_index_scans = 1;
+             } else
+             if (   ('1' <= *envvarpointer) && ('9' >= *envvarpointer)   ) {
+                 prefetch_index_scans = strtol(envvarpointer, &charptr, 10);
+                 if (charptr && (',' == *charptr)) {   /*  optional sequential prefetch in index scans */
+					 charptr++;        /*   following the comma ... */
+					 if ( ('Y' == *charptr) || ('y' == *charptr) || ('1' == *charptr) ) {
+                         prefetch_sequential_index_scans = 1;
+					 }
+				 }
+             }
+             /*  if prefeching for ISCAN,  then we require size of pfch_list to be at least target_prefetch_pages */
+             if (   (prefetch_index_scans > 0)
+                 && (prefetch_index_scans < target_prefetch_pages)
+                ) {
+                 prefetch_index_scans = target_prefetch_pages;
+             }
+        }
+        envvarpointer = getenv("PG_TRY_PREFETCHING_FOR_BTREE");
+        if (envvarpointer != (char *)0) {
+             if (    ('N' == *envvarpointer) || ('n' == *envvarpointer)   ) {
+                 prefetch_btree_heaps = 0;
+             } else
+             if (    ('Y' == *envvarpointer) || ('y' == *envvarpointer)   ) {
+                 prefetch_btree_heaps = 1;
+             }
+        }
+        envvarpointer = getenv("PG_TRY_PREFETCHING_FOR_HEAP");
+        if (envvarpointer != (char *)0) {
+             if (    ('N' == *envvarpointer) || ('n' == *envvarpointer)   ) {
+                 prefetch_heap_scans = 0;
+             } else
+             if (    ('Y' == *envvarpointer) || ('y' == *envvarpointer)   ) {
+              prefetch_heap_scans = 1;
+             }
+        }
+        envvarpointer = getenv("PG_PREFETCH_DBOID");
+        if (    (envvarpointer != (char *)0)
+             && (   ('1' <= *envvarpointer) && ('9' >= *envvarpointer)   )
+           ) {
+              errno = 0;   /*  required in order to distinguish error from 0 */
+              prefetch_dbOid = (unsigned int)strtoul((const char *)envvarpointer, 0, 10);
+              if (errno) {
+                  prefetch_dbOid = 0;
+              }
+        }
+        elog(LOG, "prefetching initialised with target_prefetch_pages= %d "
+                  ", max_async_io_prefetchers= %d implying aio concurrency= %d "
+                  ", prefetching_for_bitmap= %s "
+                  ", prefetching_for_heap= %s "
+                  ", prefetching_for_iscan= %d with sequential_index_page_prefetching= %s "
+                  ", prefetching_for_btree= %s"
+                   ,target_prefetch_pages ,max_async_io_prefetchers ,numBufferAiocbs
+                   ,(prefetch_bitmap_scans ? "Y" : "N")
+                   ,(prefetch_heap_scans ? "Y" : "N")
+                   ,prefetch_index_scans
+                   ,(prefetch_sequential_index_scans ? "Y" : "N")
+                   ,(prefetch_btree_heaps ? "Y" : "N")
+            );
+#endif /* USE_PREFETCH */
+
+
 	if (foundDescs || foundBufs)
 	{
 		/* both should be present or neither */
@@ -176,3 +393,80 @@ BufferShmemSize(void)
 
 	return size;
 }
+
+/*     imprecise count of number of in-use BAiocbs at any time
+ *     we scan the array read-only without latching so are subject to unstable result
+ *     (but since the array is in well-known contiguous storage,
+ *     we are not subject to segmentation violation)
+ *     This function may be called at any time and just does its best
+ *     return the count of what we counted.
+ */
+int
+CountInuseBAiocbs(void)
+{
+        volatile struct BufferAiocb *BAiocb;
+        int count = 0;
+        int ix;
+
+
+        if (BAiocbAnchr != (struct BAiocbAnchor *)0 ) {
+            BAiocb = BAiocbAnchr->BufferAiocbs;             /*   start of list */
+
+            for (ix = (numBufferAiocbs-1);  ix>=0;  ix--) {
+                    if ((struct BufferAiocb*)BAIOCB_OCCUPIED == (BAiocb+ix)->BAiocbnext) {   /* not on freelist ? */
+                        count++;
+                    }
+            }
+        }
+        return count;
+}
+
+/*
+ * report how many free BAiocbs at shutdown
+ * DO NOT call this while backends are actively working!!
+ * this report is useful when compare_and_swap method used (see above)
+ * as it can be used to deduce how many BAiocbs were in process-local caches -
+ * (original_number_on_freelist_at_startup - this_reported_number_at_shutdown)
+ */
+void
+ReportFreeBAiocbs(void)
+{
+        volatile struct BufferAiocb *BAiocb;
+        volatile struct BufferAiocb *BufferAiocbs;
+        int count = 0;
+        int fx , ix;
+
+
+        if (BAiocbAnchr != (struct BAiocbAnchor *)0 ) {
+            BAiocb = BAiocbAnchr->FreeBAiocbs;             /*   start of free list */
+            BufferAiocbs = BAiocbAnchr->BufferAiocbs;
+
+            for (ix = (numBufferAiocbs-1);  ix>=0;  ix--) {
+                    (BufferAiocbs+ix)->BAiocbDependentCount = 0;  /* use this as marker for finding it on freelist */
+            }
+            for (fx = (numBufferAiocbs-1);  ( (fx>=0) &&  ( BAiocb != (struct BufferAiocb*)0 ) );  fx--) {
+                    
+                    /*  check if it is a valid BufferAiocb */
+                    for (ix = (numBufferAiocbs-1);  ix>=0;  ix--) {
+                        if ((BufferAiocbs+ix) == BAiocb) { /*  is it this one ? */
+                             break;
+                        }
+                    }
+                    if (ix >= 0) {
+                        if (BAiocb->BAiocbDependentCount) {   /* seen it already ? */
+                            elog(LOG, "ReportFreeBAiocbs closed cycle on AIO control block freelist %p"
+                                          ,BAiocb);
+                            fx = 0; /* give up at this point */
+                        }
+                        BAiocb->BAiocbDependentCount = 1;  /* use this as marker for finding it on freelist */
+                        count++;
+                        BAiocb = BAiocb->BAiocbnext;
+                    } else {
+                        elog(LOG, "ReportFreeBAiocbs invalid item on AIO control block freelist %p"
+                                          ,BAiocb);
+                        fx = 0; /* give up at this point */
+                    }
+            }
+        }
+        elog(LOG, "ReportFreeBAiocbs AIO control block list : poolsize= %d  in-use-hwm= %d  final-free= %d" ,numBufferAiocbs , hwmBufferAiocbs , count);
+}
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index c070278..e4ef7f9 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -29,7 +29,7 @@
  *		buf_table.c -- manages the buffer lookup table
  */
 #include "postgres.h"
-
+#include <sys/types.h> 
 #include <sys/file.h>
 #include <unistd.h>
 
@@ -50,7 +50,6 @@
 #include "utils/resowner_private.h"
 #include "utils/timestamp.h"
 
-
 /* Note: these two macros only work on shared buffers, not local ones! */
 #define BufHdrGetBlock(bufHdr)	((Block) (BufferBlocks + ((Size) (bufHdr)->buf_id) * BLCKSZ))
 #define BufferGetLSN(bufHdr)	(PageGetLSN(BufHdrGetBlock(bufHdr)))
@@ -63,6 +62,8 @@
 #define BUF_WRITTEN				0x01
 #define BUF_REUSABLE			0x02
 
+extern volatile struct BAiocbAnchor *BAiocbAnchr;  /*  anchor for all control blocks pertaining to aio  */
+
 #define DROP_RELS_BSEARCH_THRESHOLD		20
 
 /* GUC variables */
@@ -78,26 +79,33 @@ bool		track_io_timing = false;
  */
 int			target_prefetch_pages = 0;
 
-/* local state for StartBufferIO and related functions */
+/* local state for StartBufferIO and related functions
+**  but ONLY for synchronous IO  -  not altered for aio
+*/
 static volatile BufferDesc *InProgressBuf = NULL;
 static bool IsForInput;
+pid_t this_backend_pid = 0;    /*    pid of this backend */
 
 /* local state for LockBufferForCleanup */
 static volatile BufferDesc *PinCountWaitBuf = NULL;
 
-
-static Buffer ReadBuffer_common(SMgrRelation reln, char relpersistence,
+extern int
+BufStartAsync(Relation reln, ForkNumber forkNum, BlockNumber blockNum , BufferAccessStrategy strategy);
+extern int
+BufCheckAsync(SMgrRelation caller_smgr, Relation caller_reln, volatile BufferDesc *buf_desc, int intention
+        ,BufferAccessStrategy strategy , int index_for_aio , bool spinLockHeld , LWLockId PartitionLock );
+Buffer ReadBuffer_common(SMgrRelation reln, char relpersistence,
 				  ForkNumber forkNum, BlockNumber blockNum,
 				  ReadBufferMode mode, BufferAccessStrategy strategy,
-				  bool *hit);
-static bool PinBuffer(volatile BufferDesc *buf, BufferAccessStrategy strategy);
-static void PinBuffer_Locked(volatile BufferDesc *buf);
-static void UnpinBuffer(volatile BufferDesc *buf, bool fixOwner);
+				  bool *hit , int index_for_aio);
+bool PinBuffer(volatile BufferDesc *buf, BufferAccessStrategy strategy);
+void PinBuffer_Locked(volatile BufferDesc *buf);
+void UnpinBuffer(volatile BufferDesc *buf, bool fixOwner);
 static void BufferSync(int flags);
 static int	SyncOneBuffer(int buf_id, bool skip_recently_used);
 static void WaitIO(volatile BufferDesc *buf);
-static bool StartBufferIO(volatile BufferDesc *buf, bool forInput);
-static void TerminateBufferIO(volatile BufferDesc *buf, bool clear_dirty,
+static bool StartBufferIO(volatile BufferDesc *buf, bool forInput , int index_for_aio );
+void TerminateBufferIO(volatile BufferDesc *buf, bool clear_dirty,
 				  int set_flag_bits);
 static void shared_buffer_write_error_callback(void *arg);
 static void local_buffer_write_error_callback(void *arg);
@@ -106,24 +114,66 @@ static volatile BufferDesc *BufferAlloc(SMgrRelation smgr,
 			ForkNumber forkNum,
 			BlockNumber blockNum,
 			BufferAccessStrategy strategy,
-			bool *foundPtr);
+			int *foundPtr , int index_for_aio );
 static void FlushBuffer(volatile BufferDesc *buf, SMgrRelation reln);
 static void AtProcExit_Buffers(int code, Datum arg);
 static int	rnode_comparator(const void *p1, const void *p2);
 
+BlockNumber BlocknumOfBuffer(Buffer buffer);
+bool BlocknotinBuffer(Buffer buffer, Relation relation, BlockNumber blockNum);
 
 /*
  * PrefetchBuffer -- initiate asynchronous read of a block of a relation
  *
- * This is named by analogy to ReadBuffer but doesn't actually allocate a
- * buffer.  Instead it tries to ensure that a future ReadBuffer for the given
- * block will not be delayed by the I/O.  Prefetching is optional.
+ * This is named by analogy to ReadBuffer but allocates a buffer only if using asynchronous I/O.
+ * Its purpose  is to try to ensure that a future ReadBuffer for the given block
+ * will not be delayed by the I/O.  Prefetching is optional.
  * No-op if prefetching isn't compiled in.
+ *
+ * Originally the prefetch simply called posix_fadvise() to recommend read-ahead into kernel page cache.
+ * Extended to provide an alternative of issuing an asynchronous aio_read() to read into a buffer.
+ * This extension has an implication on how this bufmgr component manages concurrent requests
+ * for the same disk block.
+ *
+ * Synchronous IO (read()) does not provide a means for waiting on another task's read if in progress,
+ * and bufmgr implements its own scheme in StartBufferIO, WaitIO, and TerminateBufferIO.
+ *
+ * Asynchronous IO (aio_read()) provides a means for waiting on this or another task's read if in progress,
+ * namely aio_suspend(),  which this extension uses.    Therefore,  although StartBufferIO and TerminateBufferIO
+ * are called as part of asynchronous prefetching,   their role is limited to maintaining the buffer desc flags,
+ * and they do not track the asynchronous IO itself.    Instead,   asynchronous IOs are tracked in 
+ * a separate set of shared control blocks,  the BufferAiocb list -
+ *   refer to     include/storage/buf_internals.h and storage/buffer/buf_init.c
+ *
+ * Another implication of asynchronous IO concerns buffer pinning.
+ * The buffer used for the prefetch is pinned before aio_read is issued.
+ * It is expected that the same task (and possibly others) will later ask to read the page
+ * and eventually release and unpin the buffer.
+ * However,  if the task which issued the aio_read later decides not to read the page,
+ * and return code indicates delta_pin_count > 0 (see below)
+ * it *must* instead issue a DiscardBuffer() (see function later in this file)
+ * so that its pin is released.
+ * Therefore,  each client which uses the PrefetchBuffer service must either always read all
+ * prefetched pages,  or keep track of prefetched pages and discard unread ones at end of scan.
+ *
+ * return code:   is an int bitmask defined in bufmgr.h
+        PREFTCHRC_BUF_PIN_INCREASED 0x01      pin count on buffer has been increased by 1
+        PREFTCHRC_BLK_ALREADY_PRESENT 0x02    block was already present in a buffer
+ *
+ * PREFTCHRC_BLK_ALREADY_PRESENT is a hint to caller that the prefetch may be unnecessary
  */
-void
-PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
+int
+PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum , BufferAccessStrategy strategy)
 {
+	Buffer		buf_id; /* indicates buffer containing the requested block  */
+        int             PrefetchBufferRc = 0; /*  return value as described above  */
+        int             PinCountOnEntry = 0;  /*  pin count on entry           */
+        int             PinCountdelta = 0;    /*  pin count delta increase     */
+
+
 #ifdef USE_PREFETCH
+
+	buf_id = -1;
 	Assert(RelationIsValid(reln));
 	Assert(BlockNumberIsValid(blockNum));
 
@@ -145,8 +195,13 @@ PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
 	{
 		BufferTag	newTag;		/* identity of requested block */
 		uint32		newHash;	/* hash value for newTag */
+        int         BufStartAsyncrc = -1;  /*  retcode from BufStartAsync :
+                                                       **        0 if started successfully (which implies buffer was newly pinned )
+                                                       **       -1 if failed for some reason
+                                                       **        1+PrivateRefCount if we found desired buffer in buffer pool
+                                                       **  and we set it likewise if we find buffer in buffer pool
+                                                       */
 		LWLock	   *newPartitionLock;	/* buffer partition lock for it */
-		int			buf_id;
 
 		/* create a tag so we can lookup the buffer */
 		INIT_BUFFERTAG(newTag, reln->rd_smgr->smgr_rnode.node,
@@ -158,28 +213,119 @@ PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
 
 		/* see if the block is in the buffer pool already */
 		LWLockAcquire(newPartitionLock, LW_SHARED);
-		buf_id = BufTableLookup(&newTag, newHash);
+		buf_id = (Buffer)BufTableLookup(&newTag, newHash);
+                if (buf_id >= 0) {
+                    PinCountOnEntry = PrivateRefCount[buf_id];  /*  pin count on entry               */
+                    BufStartAsyncrc = 1 + PinCountOnEntry; /* indicate this backends pin count - see above comment */
+                    PrefetchBufferRc = PREFTCHRC_BLK_ALREADY_PRESENT;       /* indicate buffer present */
+                } else {
+                    PrefetchBufferRc = 0;                                   /* indicate buffer not present */
+                }
 		LWLockRelease(newPartitionLock);
 
+     not_in_buffers:
 		/* If not in buffers, initiate prefetch */
-		if (buf_id < 0)
+		if (buf_id < 0) {
+                    /*    try using async aio_read with a buffer */
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+                    BufStartAsyncrc = BufStartAsync( reln, forkNum, blockNum , strategy );
+                    if (BufStartAsyncrc < 0) {
+                            pgBufferUsage.aio_read_noblok++;
+                    }
+#else /* not USE_AIO_ATOMIC_BUILTIN_COMP_SWAP so try the alternative that does not read the block into a postgresql buffer */
 			smgrprefetch(reln->rd_smgr, forkNum, blockNum);
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
+		}
 
-		/*
-		 * If the block *is* in buffers, we do nothing.  This is not really
-		 * ideal: the block might be just about to be evicted, which would be
-		 * stupid since we know we are going to need it soon.  But the only
-		 * easy answer is to bump the usage_count, which does not seem like a
-		 * great solution: when the caller does ultimately touch the block,
-		 * usage_count would get bumped again, resulting in too much
-		 * favoritism for blocks that are involved in a prefetch sequence. A
-		 * real fix would involve some additional per-buffer state, and it's
-		 * not clear that there's enough of a problem to justify that.
+        if (  (buf_id >= 0) || (BufStartAsyncrc >= 1)  ) {
+                        /* The block *is* in buffers.    */
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+                        pgBufferUsage.aio_read_noneed++;
+#ifndef USE_PREFETCH_BUT_DONT_PIN_ALREADY_PRESENT /* jury is out on whether the following wins but it ought to ...  */
+                        /*
+                        ** If this backend already had pinned it,
+                        ** or another backend had banked a pin on it,
+                        ** or there is an IO in progress,
+                        ** or it is not marked valid,
+                        ** then do nothing.
+                        ** Otherwise pin it and mark the buffer's pin as banked by this backend.
+                        ** Note  -  it may or not be pinned by another backend -
+                        **          it is ok for us to bank a pin on it
+                        **          *provided* the other backend did not bank its pin.
+                        **          The reason for this is that the banked-pin indicator is global -
+                        **          it can identify at most one process.
+                        */
+                        /* pgBufferUsage.aio_read_wasted++;  overload counter - only for debugging */
+                        if (BufStartAsyncrc == 1) {            /*   not pinned by me  */
+                              /*  pgBufferUsage.aio_read_wasted++;  overload counter - only for debugging */
+                              /*  note   -   all we can say with certainty is that the buffer is not pinned by me
+                              **             we cannot be sure that it is still in buffer pool
+                              **             so must go through the entire locking and searching all over again ...
 		 */
+                            LWLockAcquire(newPartitionLock, LW_SHARED);
+                            buf_id = (Buffer)BufTableLookup(&newTag, newHash);
+                            /* If in buffers, proceed */
+                            if (buf_id >= 0) {
+                                /*  since the block is now present,
+                                **  save the current pin count to ensure final delta is calculated correctly
+                                */
+                                PinCountOnEntry = PrivateRefCount[buf_id];  /*  pin count on entry               */
+                                if ( PinCountOnEntry == 0) { /*  paranoid check it's still not pinned by me */
+                                    volatile        BufferDesc *buf_desc;
+
+                                    buf_desc = &BufferDescriptors[buf_id];        /* found buffer descriptor */
+                                    LockBufHdr(buf_desc);
+                                    if (    (buf_desc->flags & BM_VALID)           /* buffer is valid        */
+                                         && (!(buf_desc->flags & (BM_IO_IN_PROGRESS|BM_AIO_IN_PROGRESS|BM_AIO_PREFETCH_PIN_BANKED))) /* buffer is not any of ... */
+                                       ) {
+                                        buf_desc->flags |= BM_AIO_PREFETCH_PIN_BANKED; /* bank the pin for next use by this task */
+                                        /*  note  - we can call PinBuffer_Locked with the BM_AIO_PREFETCH_PIN_BANKED flag set because it is not yet pinned by me */
+                                        buf_desc->freeNext = -(this_backend_pid);       /* remember which pid banked it */
+                                        /*  pgBufferUsage.aio_read_wasted--;      overload counter - not wasted after all - only for debugging */
+
+                                        /* Make sure we will have room to remember the buffer pin */
+                                        ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
+
+                                        PinBuffer_Locked(buf_desc);
 	}
-#endif   /* USE_PREFETCH */
+                                    else {
+                                        UnlockBufHdr(buf_desc);
+                                    }
+                                }
+                            }
+                            LWLockRelease(newPartitionLock);
+                            /*  although unlikely,  maybe it was evicted while we were puttering about  */
+                            if (buf_id < 0) {
+                                pgBufferUsage.aio_read_noneed--;   /*   back out the accounting */
+                                goto not_in_buffers;               /*   and try again           */
+                            }
+                        }
+#endif /*  USE_PREFETCH_BUT_DONT_PIN_ALREADY_PRESENT */
+
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
+
+		}
+
+		if (buf_id >= 0) {
+			PinCountdelta = PrivateRefCount[buf_id] - PinCountOnEntry;  /*  pin count delta increase    */
+			if (  (PinCountdelta < 0) || (PinCountdelta > 1) ) {
+				  elog(ERROR,
+						 "PrefetchBuffer #%d : incremented pin count by %d on bufdesc %p refcount %u localpins %d\n"
+								  ,(buf_id+1) , PinCountdelta , &BufferDescriptors[buf_id] ,BufferDescriptors[buf_id].refcount , PrivateRefCount[buf_id]);
 }
+		} else
+		if (BufStartAsyncrc == 0) {  /* aio started successfully (which implies buffer was newly pinned ) */
+			PinCountdelta = 1;
+		}
+
+		/*  set final PrefetchBufferRc according to previous value */
+		PrefetchBufferRc |= PinCountdelta;  /* set the PREFTCHRC_BUF_PIN_INCREASED bit */
+	}
+
+#endif   /* USE_PREFETCH */
 
+	return PrefetchBufferRc; /*  return value as described above */
+}
 
 /*
  * ReadBuffer -- a shorthand for ReadBufferExtended, for reading from main
@@ -252,7 +398,7 @@ ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum,
 	 */
 	pgstat_count_buffer_read(reln);
 	buf = ReadBuffer_common(reln->rd_smgr, reln->rd_rel->relpersistence,
-							forkNum, blockNum, mode, strategy, &hit);
+							forkNum, blockNum, mode, strategy, &hit , 0);
 	if (hit)
 		pgstat_count_buffer_hit(reln);
 	return buf;
@@ -280,7 +426,7 @@ ReadBufferWithoutRelcache(RelFileNode rnode, ForkNumber forkNum,
 	Assert(InRecovery);
 
 	return ReadBuffer_common(smgr, RELPERSISTENCE_PERMANENT, forkNum, blockNum,
-							 mode, strategy, &hit);
+							 mode, strategy, &hit , 0);
 }
 
 
@@ -288,15 +434,18 @@ ReadBufferWithoutRelcache(RelFileNode rnode, ForkNumber forkNum,
  * ReadBuffer_common -- common logic for all ReadBuffer variants
  *
  * *hit is set to true if the request was satisfied from shared buffer cache.
+ * index_for_aio ,  if -ve , is negative of ( index of the aiocb in the BufferAiocbs array + 3 )
+ *     which is passed through to StartBufferIO
  */
-static Buffer
+Buffer
 ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 				  BlockNumber blockNum, ReadBufferMode mode,
-				  BufferAccessStrategy strategy, bool *hit)
+				  BufferAccessStrategy strategy, bool *hit , int index_for_aio )
 {
 	volatile BufferDesc *bufHdr;
 	Block		bufBlock;
 	bool		found;
+        int             allocrc;             /*  retcode from BufferAlloc */
 	bool		isExtend;
 	bool		isLocalBuf = SmgrIsTemp(smgr);
 
@@ -328,16 +477,40 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 	}
 	else
 	{
+                allocrc = mode; /* pass mode to BufferAlloc since it must not wait for async io if RBM_NOREAD_FOR_PREFETCH */
 		/*
 		 * lookup the buffer.  IO_IN_PROGRESS is set if the requested block is
 		 * not currently in memory.
 		 */
 		bufHdr = BufferAlloc(smgr, relpersistence, forkNum, blockNum,
-							 strategy, &found);
-		if (found)
-			pgBufferUsage.shared_blks_hit++;
+							 strategy, &allocrc , index_for_aio );
+		if (allocrc < 0) {
+                    if (mode == RBM_ZERO_ON_ERROR || zero_damaged_pages)
+                    {
+                        ereport(WARNING,
+                                (errcode(ERRCODE_DATA_CORRUPTED),
+                                 errmsg("invalid page header in block %u of relation %s; zeroing out page",
+                                        blockNum,
+                                        relpath(smgr->smgr_rnode, forkNum))));
+                        bufBlock = BufHdrGetBlock(bufHdr);
+                        MemSet((char *) bufBlock, 0, BLCKSZ);
+                    }
 		else
+                      ereport(ERROR,
+                              (errcode(ERRCODE_DATA_CORRUPTED),
+                               errmsg("invalid page header in block %u of relation %s",
+                                      blockNum,
+                                      relpath(smgr->smgr_rnode, forkNum))));
+                        found = true;
+                }
+		else if (allocrc > 0) {
+			pgBufferUsage.shared_blks_hit++;
+                        found = true;
+                }
+		else {
 			pgBufferUsage.shared_blks_read++;
+                        found = false;
+                }
 	}
 
 	/* At this point we do NOT hold any locks. */
@@ -410,7 +583,7 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 				Assert(bufHdr->flags & BM_VALID);
 				bufHdr->flags &= ~BM_VALID;
 				UnlockBufHdr(bufHdr);
-			} while (!StartBufferIO(bufHdr, true));
+			} while (!StartBufferIO(bufHdr, true, 0));
 		}
 	}
 
@@ -430,6 +603,7 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 
 	bufBlock = isLocalBuf ? LocalBufHdrGetBlock(bufHdr) : BufHdrGetBlock(bufHdr);
 
+        if (mode != RBM_NOREAD_FOR_PREFETCH) {
 	if (isExtend)
 	{
 		/* new buffers are zero-filled */
@@ -499,6 +673,7 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 	VacuumPageMiss++;
 	if (VacuumCostActive)
 		VacuumCostBalance += VacuumCostPageMiss;
+	}
 
 	TRACE_POSTGRESQL_BUFFER_READ_DONE(forkNum, blockNum,
 									  smgr->smgr_rnode.node.spcNode,
@@ -520,21 +695,39 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
  * the default strategy.  The selected buffer's usage_count is advanced when
  * using the default strategy, but otherwise possibly not (see PinBuffer).
  *
- * The returned buffer is pinned and is already marked as holding the
- * desired page.  If it already did have the desired page, *foundPtr is
- * set TRUE.  Otherwise, *foundPtr is set FALSE and the buffer is marked
+ * index_for_aio is an input parm which,  if non-zero,  identifies a BufferAiocb
+ * acquired by caller and to be used for any StartBufferIO performed by this routine.
+ * In this case,  if block not found in buffer pool and we allocate a new buffer,
+ * then we must maintain the spinlock on the buffer and pass it back to caller.
+ *
+ * foundPtr is input and output :
+ *  . input   -  indicates the read-buffer mode  ( see bufmgr.h )
+ *  . output  -  indicates the status of the buffer - see below
+ *
+ * Except for the case of RBM_NOREAD_FOR_PREFETCH and buffer is found,
+ * the returned buffer is pinned and is already marked as holding the
+ * desired page.
+ *  If it already did have the desired page and page content is valid,
+ *  *foundPtr is set to 1
+ *  If it already did have the desired page and mode is RBM_NOREAD_FOR_PREFETCH
+ *    and StartBufferIO returned false
+ *    (meaning it could not initialise the buffer for aio)
+ *  *foundPtr is set to 2
+ *  If it already did have the desired page but page content is invalid,
+ *  *foundPtr is set to -1
+ *   this can happen only if the buffer was read by an async read
+ *   and the aio is still in progress or pinned by the issuer of the startaio.
+ *  Otherwise, *foundPtr is set to 0 and the buffer is marked
  * as IO_IN_PROGRESS; ReadBuffer will now need to do I/O to fill it.
  *
- * *foundPtr is actually redundant with the buffer's BM_VALID flag, but
- * we keep it for simplicity in ReadBuffer.
- *
- * No locks are held either at entry or exit.
+ * No locks are held either at entry or exit EXCEPT for case noted above
+ * of passing an empty buffer back to async io caller ( index_for_aio set ).
  */
 static volatile BufferDesc *
 BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			BlockNumber blockNum,
 			BufferAccessStrategy strategy,
-			bool *foundPtr)
+			int *foundPtr , int index_for_aio )
 {
 	BufferTag	newTag;			/* identity of requested block */
 	uint32		newHash;		/* hash value for newTag */
@@ -546,6 +739,13 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 	int			buf_id;
 	volatile BufferDesc *buf;
 	bool		valid;
+        int             IntentionBufferrc;      /* retcode from BufCheckAsync */
+        bool            StartBufferIOrc;        /* retcode from StartBufferIO */
+        ReadBufferMode mode;
+
+
+        mode = *foundPtr;
+        *foundPtr = 0;
 
 	/* create a tag so we can lookup the buffer */
 	INIT_BUFFERTAG(newTag, smgr->smgr_rnode.node, forkNum, blockNum);
@@ -560,21 +760,53 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 	if (buf_id >= 0)
 	{
 		/*
-		 * Found it.  Now, pin the buffer so no one can steal it from the
-		 * buffer pool, and check to see if the correct data has been loaded
-		 * into the buffer.
+		 * Found it.
 		 */
+                *foundPtr = 1;
 		buf = &BufferDescriptors[buf_id];
 
-		valid = PinBuffer(buf, strategy);
-
-		/* Can release the mapping lock as soon as we've pinned it */
+                /*   If prefetch mode,  then return immediately indicating found,
+                **   and NOTE in this case only,  we did not pin buffer.
+                **   In theory we might try to check whether the buffer is valid,  io in progress,  etc
+                **   but in practice it is simpler to abandon the prefetch if the buffer exists
+                */
+                if (mode == RBM_NOREAD_FOR_PREFETCH) {
+                    /* release the mapping lock and return    */
 		LWLockRelease(newPartitionLock);
-
-		*foundPtr = TRUE;
-
-		if (!valid)
-		{
+                } else {
+                    /*   note that the current request is for same tag as the one associated with the aio -
+                    **   so simply complete the aio and we have our buffer.
+                    **         If an aio was started on this buffer,
+                    **         check complete and wait for it if not.
+                    **         And,  if aio had been started,  then the task
+                    **         which issued the start aio already pinned it for this read,
+                    **         so if that task was me and the aio was successful,
+                    **         pass the current pin to this read without dropping and re-acquiring.
+                    **         this is all done by BufCheckAsync
+                    */
+                    IntentionBufferrc = BufCheckAsync(smgr , 0 , buf, BUF_INTENTION_WANT , strategy , index_for_aio , false , newPartitionLock );
+
+                    /*       check to see if the correct data has been loaded into the buffer.  */
+                    valid = (IntentionBufferrc == BUF_INTENT_RC_VALID);
+
+                    /*  check for serious IO errors   */
+                    if (!valid) {
+                        if (    (IntentionBufferrc != BUF_INTENT_RC_INVALID_NO_AIO)
+                             && (IntentionBufferrc != BUF_INTENT_RC_INVALID_AIO)
+                           ) {
+                            *foundPtr = -1;  /*  inform caller of serious error */
+                        }
+                        else
+                        if (IntentionBufferrc == BUF_INTENT_RC_INVALID_AIO) {
+                            goto proceed_with_not_found;  /*  yes,  I know,  a goto ... think of it as a break out of the if */
+                        }
+                     }
+
+                    /* BufCheckAsync pinned the buffer            */
+                    /* so can now release the mapping lock               */
+                    LWLockRelease(newPartitionLock);
+
+                    if (!valid) {
 			/*
 			 * We can only get here if (a) someone else is still reading in
 			 * the page, or (b) a previous read attempt failed.  We have to
@@ -582,19 +814,21 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			 * own read attempt if the page is still not BM_VALID.
 			 * StartBufferIO does it all.
 			 */
-			if (StartBufferIO(buf, true))
+                                if (StartBufferIO(buf, true, index_for_aio))
 			{
 				/*
 				 * If we get here, previous attempts to read the buffer must
 				 * have failed ... but we shall bravely try again.
 				 */
-				*foundPtr = FALSE;
+                                        *foundPtr = 0;
+                                }
 			}
 		}
 
 		return buf;
 	}
 
+  proceed_with_not_found:
 	/*
 	 * Didn't find it in the buffer pool.  We'll have to initialize a new
 	 * buffer.  Remember to unlock the mapping lock while doing the work.
@@ -619,8 +853,10 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 		/* Must copy buffer flags while we still hold the spinlock */
 		oldFlags = buf->flags;
 
-		/* Pin the buffer and then release the buffer spinlock */
-		PinBuffer_Locked(buf);
+                /*         If an aio was started on this buffer,
+                **         check complete and cancel it if not.
+                */
+                BufCheckAsync(smgr , 0 , buf, BUF_INTENTION_REJECT_OBTAIN_PIN , 0 , index_for_aio, true , 0 );
 
 		/* Now it's safe to release the freelist lock */
 		if (lock_held)
@@ -791,13 +1027,18 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 				 * then set up our own read attempt if the page is still not
 				 * BM_VALID.  StartBufferIO does it all.
 				 */
-				if (StartBufferIO(buf, true))
+                                StartBufferIOrc = StartBufferIO(buf, true , index_for_aio); /* retcode from StartBufferIO */
+				if (StartBufferIOrc)
 				{
 					/*
 					 * If we get here, previous attempts to read the buffer
 					 * must have failed ... but we shall bravely try again.
 					 */
-					*foundPtr = FALSE;
+					*foundPtr = 0;
+                                } else
+                                if (mode == RBM_NOREAD_FOR_PREFETCH) {
+					UnpinBuffer(buf, true); /* must unpin if RBM_NOREAD_FOR_PREFETCH and found */
+                                        *foundPtr = 2;  /*  inform BufStartAsync that buffer must not be used */
 				}
 			}
 
@@ -860,10 +1101,17 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 	 * lock.  If StartBufferIO returns false, then someone else managed to
 	 * read it before we did, so there's nothing left for BufferAlloc() to do.
 	 */
-	if (StartBufferIO(buf, true))
-		*foundPtr = FALSE;
-	else
-		*foundPtr = TRUE;
+        StartBufferIOrc = StartBufferIO(buf, true , index_for_aio); /* retcode from StartBufferIO */
+        if (StartBufferIOrc) {
+		*foundPtr = 0;
+        } else {
+                if (mode == RBM_NOREAD_FOR_PREFETCH) {
+                    UnpinBuffer(buf, true); /* must unpin if RBM_NOREAD_FOR_PREFETCH and found */
+                    *foundPtr = 2;  /*  inform BufStartAsync that buffer must not be used */
+                } else {
+                    *foundPtr = 1;
+                }
+        }
 
 	return buf;
 }
@@ -970,6 +1218,10 @@ retry:
 	/*
 	 * Insert the buffer at the head of the list of free buffers.
 	 */
+        /*   avoid confusing freelist with strange-looking freeNext */
+        if (buf->freeNext <= FREENEXT_BAIOCB_ORIGIN) { /* means was used for aiocb index */
+            buf->freeNext = FREENEXT_NOT_IN_LIST;
+        }
 	StrategyFreeBuffer(buf);
 }
 
@@ -1022,6 +1274,56 @@ MarkBufferDirty(Buffer buffer)
 	UnlockBufHdr(bufHdr);
 }
 
+/*  return the blocknum of the block in a buffer if it is valid
+**  if a shared buffer,  it must be pinned
+*/
+BlockNumber
+BlocknumOfBuffer(Buffer buffer)
+{
+	volatile BufferDesc *bufHdr;
+        BlockNumber rc = 0;
+
+	if (BufferIsValid(buffer)) {
+            if  (BufferIsLocal(buffer)) {
+                bufHdr = &LocalBufferDescriptors[-buffer - 1];
+            } else {
+                bufHdr = &BufferDescriptors[buffer - 1];
+            }
+
+            rc = bufHdr->tag.blockNum;
+        }
+
+        return rc;
+}
+
+/*  report whether specified buffer contains same or different block
+**  if a shared buffer,  it must be pinned
+*/
+bool
+BlocknotinBuffer(Buffer buffer,
+					 Relation relation,
+					 BlockNumber blockNum)
+{
+	volatile BufferDesc *bufHdr;
+        bool rc = false;
+
+	if (BufferIsValid(buffer)) {
+            if  (BufferIsLocal(buffer)) {
+                bufHdr = &LocalBufferDescriptors[-buffer - 1];
+            } else {
+                bufHdr = &BufferDescriptors[buffer - 1];
+            }
+
+            rc =
+                  (   (bufHdr->tag.blockNum != blockNum)
+                   || (!(RelFileNodeEquals(bufHdr->tag.rnode, relation->rd_node) ))
+                   || (bufHdr->tag.forkNum != MAIN_FORKNUM)
+                  );
+        }
+
+        return rc;
+}
+
 /*
  * ReleaseAndReadBuffer -- combine ReleaseBuffer() and ReadBuffer()
  *
@@ -1040,18 +1342,18 @@ ReleaseAndReadBuffer(Buffer buffer,
 					 Relation relation,
 					 BlockNumber blockNum)
 {
-	ForkNumber	forkNum = MAIN_FORKNUM;
 	volatile BufferDesc *bufHdr;
+        bool isDifferentBlock;   /*       requesting different block from that already in buffer ? */
 
 	if (BufferIsValid(buffer))
 	{
+	    /* if a shared buff, we have pin, so it's ok to examine tag without spinlock */
+            isDifferentBlock = BlocknotinBuffer(buffer,relation,blockNum); /*  requesting different block from that already in buffer ? */
 		if (BufferIsLocal(buffer))
 		{
 			Assert(LocalRefCount[-buffer - 1] > 0);
 			bufHdr = &LocalBufferDescriptors[-buffer - 1];
-			if (bufHdr->tag.blockNum == blockNum &&
-				RelFileNodeEquals(bufHdr->tag.rnode, relation->rd_node) &&
-				bufHdr->tag.forkNum == forkNum)
+			if (!isDifferentBlock)
 				return buffer;
 			ResourceOwnerForgetBuffer(CurrentResourceOwner, buffer);
 			LocalRefCount[-buffer - 1]--;
@@ -1060,12 +1362,12 @@ ReleaseAndReadBuffer(Buffer buffer,
 		{
 			Assert(PrivateRefCount[buffer - 1] > 0);
 			bufHdr = &BufferDescriptors[buffer - 1];
-			/* we have pin, so it's ok to examine tag without spinlock */
-			if (bufHdr->tag.blockNum == blockNum &&
-				RelFileNodeEquals(bufHdr->tag.rnode, relation->rd_node) &&
-				bufHdr->tag.forkNum == forkNum)
+                        BufCheckAsync(0 , relation , bufHdr , ( isDifferentBlock ? BUF_INTENTION_REJECT_FORGET
+                                                                                 : BUF_INTENTION_REJECT_KEEP_PIN )
+                                                            , 0 , 0 , false , 0 ); /* end any IO and maybe unpin */
+			if (!isDifferentBlock) {
 				return buffer;
-			UnpinBuffer(bufHdr, true);
+                        }
 		}
 	}
 
@@ -1090,11 +1392,12 @@ ReleaseAndReadBuffer(Buffer buffer,
  * Returns TRUE if buffer is BM_VALID, else FALSE.  This provision allows
  * some callers to avoid an extra spinlock cycle.
  */
-static bool
+bool
 PinBuffer(volatile BufferDesc *buf, BufferAccessStrategy strategy)
 {
 	int			b = buf->buf_id;
 	bool		result;
+        bool       pin_already_banked_by_me = 0;  /* buffer is already pinned by me and redeemable */
 
 	if (PrivateRefCount[b] == 0)
 	{
@@ -1116,12 +1419,34 @@ PinBuffer(volatile BufferDesc *buf, BufferAccessStrategy strategy)
 	else
 	{
 		/* If we previously pinned the buffer, it must surely be valid */
+                /* Errr  -   is that really true  ???    I don't think so  :
+                ** what if I pin,  try an IO,  in progress,  then mistakenly pin again
 		result = true;
+                */
+		LockBufHdr(buf);
+                pin_already_banked_by_me = (    (PrivateRefCount[b] > 0) && (buf->flags & BM_AIO_PREFETCH_PIN_BANKED)
+                                             && (   (    (buf->flags & BM_AIO_IN_PROGRESS) ? ( ((BAiocbAnchr->BufferAiocbs)+(FREENEXT_BAIOCB_ORIGIN - buf->freeNext))->pidOfAio )
+                                                                                     : (-(buf->freeNext))  )  == this_backend_pid )
+                                           );
+                if (pin_already_banked_by_me) {
+                        elog(LOG, "PinBuffer : on buffer %d with banked pin rel=%s, blockNum=%u, with flags %X refcount=%u"
+                       ,buf->buf_id,relpathbackend(buf->tag.rnode, InvalidBackendId, buf->tag.forkNum)
+                       ,buf->tag.blockNum, buf->flags, buf->refcount);
+                    buf->flags &= ~BM_AIO_PREFETCH_PIN_BANKED;
+                    if (!(buf->flags & BM_AIO_IN_PROGRESS)) {
+                        buf->freeNext = FREENEXT_NOT_IN_LIST;      /*  forget the bank client */
+	}
+                }
+		result = (buf->flags & BM_VALID) != 0;
+		UnlockBufHdr(buf);
 	}
+
+        if (!pin_already_banked_by_me) {
 	PrivateRefCount[b]++;
 	Assert(PrivateRefCount[b] > 0);
 	ResourceOwnerRememberBuffer(CurrentResourceOwner,
 								BufferDescriptorGetBuffer(buf));
+        }
 	return result;
 }
 
@@ -1138,19 +1463,36 @@ PinBuffer(volatile BufferDesc *buf, BufferAccessStrategy strategy)
  * to save a spin lock/unlock cycle, because we need to pin a buffer before
  * its state can change under us.
  */
-static void
+void
 PinBuffer_Locked(volatile BufferDesc *buf)
 {
 	int			b = buf->buf_id;
+        bool       pin_already_banked_by_me;  /* buffer is already pinned by me and redeemable */
 
-	if (PrivateRefCount[b] == 0)
+        pin_already_banked_by_me = (    (PrivateRefCount[b] > 0) && (buf->flags & BM_AIO_PREFETCH_PIN_BANKED)
+                                     && (   (    (buf->flags & BM_AIO_IN_PROGRESS) ? ( ((BAiocbAnchr->BufferAiocbs)+(FREENEXT_BAIOCB_ORIGIN - buf->freeNext))->pidOfAio )
+                                                                             : (-(buf->freeNext))  )  == this_backend_pid )
+                             );
+	if (PrivateRefCount[b] == 0) {
 		buf->refcount++;
+        }
+        if (pin_already_banked_by_me) {
+                        elog(LOG, "PinBuffer_Locked : on buffer %d with banked pin rel=%s, blockNum=%u, with flags %X refcount=%u"
+                       ,buf->buf_id,relpathbackend(buf->tag.rnode, InvalidBackendId, buf->tag.forkNum)
+                       ,buf->tag.blockNum, buf->flags, buf->refcount);
+            buf->flags &= ~BM_AIO_PREFETCH_PIN_BANKED;
+                    if (!(buf->flags & BM_AIO_IN_PROGRESS)) {
+                        buf->freeNext = FREENEXT_NOT_IN_LIST;      /*  forget the bank client */
+                    }
+        }
 	UnlockBufHdr(buf);
+        if (!pin_already_banked_by_me) {
 	PrivateRefCount[b]++;
 	Assert(PrivateRefCount[b] > 0);
 	ResourceOwnerRememberBuffer(CurrentResourceOwner,
 								BufferDescriptorGetBuffer(buf));
 }
+}
 
 /*
  * UnpinBuffer -- make buffer available for replacement.
@@ -1160,29 +1502,68 @@ PinBuffer_Locked(volatile BufferDesc *buf)
  * Most but not all callers want CurrentResourceOwner to be adjusted.
  * Those that don't should pass fixOwner = FALSE.
  */
-static void
+void
 UnpinBuffer(volatile BufferDesc *buf, bool fixOwner)
 {
+
 	int			b = buf->buf_id;
+        bool       pin_already_banked_by_me;  /* buffer is already pinned by me and redeemable */
 
-	if (fixOwner)
+	if (fixOwner) {
 		ResourceOwnerForgetBuffer(CurrentResourceOwner,
 								  BufferDescriptorGetBuffer(buf));
+        }
 
 	Assert(PrivateRefCount[b] > 0);
 	PrivateRefCount[b]--;
 	if (PrivateRefCount[b] == 0)
 	{
+
 		/* I'd better not still hold any locks on the buffer */
 		Assert(!LWLockHeldByMe(buf->content_lock));
 		Assert(!LWLockHeldByMe(buf->io_in_progress_lock));
 
 		LockBufHdr(buf);
 
+		/* this backend has released last pin - buffer should not have pin banked by me
+                ** and if AIO in progress then there should be another backend pin
+                */
+                pin_already_banked_by_me = (       (buf->flags & BM_AIO_PREFETCH_PIN_BANKED)
+                                             &&    (   (    (buf->flags & BM_AIO_IN_PROGRESS)
+                                                         ? ( ((BAiocbAnchr->BufferAiocbs)+(FREENEXT_BAIOCB_ORIGIN - buf->freeNext))->pidOfAio )
+                                                         : (-(buf->freeNext))
+                                                       )  == this_backend_pid )
+                                           );
+                if (pin_already_banked_by_me) {
+                        /*  this is a strange situation  -  caller had a banked pin (which callers are supposed not to know about)
+                        **                                  but either discovered it had it or has over-counted how many pins it has
+                        */
+                        buf->flags &= ~BM_AIO_PREFETCH_PIN_BANKED;   /*   redeem the pin although it is now of no use since about to release */
+                        if (!(buf->flags & BM_AIO_IN_PROGRESS)) {
+                            buf->freeNext = FREENEXT_NOT_IN_LIST;      /*  forget the bank client */
+                        }
+
+                        /*     temporarily suppress logging error to avoid performance degradation -
+                        **     either this task really does not need the buffer in which case the error is harmless
+                        **     or a more severe error will be detected later (possibly immediately below)
+                        elog(LOG, "UnpinBuffer :  released last this-backend pin on buffer %d rel=%s, blockNum=%u, but had banked pin flags %X refcount=%u"
+                       ,buf->buf_id,relpathbackend(buf->tag.rnode, InvalidBackendId, buf->tag.forkNum)
+                       ,buf->tag.blockNum, buf->flags, buf->refcount);
+                        */
+                }
+
 		/* Decrement the shared reference count */
 		Assert(buf->refcount > 0);
 		buf->refcount--;
 
+                if (   (buf->refcount == 0) && (buf->flags & BM_AIO_IN_PROGRESS)  ) {
+
+                        elog(ERROR, "UnpinBuffer :  released last any-backend pin on buffer %d rel=%s, blockNum=%u, but AIO in progress flags %X refcount=%u"
+                            ,buf->buf_id,relpathbackend(buf->tag.rnode, InvalidBackendId, buf->tag.forkNum)
+                            ,buf->tag.blockNum, buf->flags, buf->refcount);
+                }
+
+
 		/* Support LockBufferForCleanup() */
 		if ((buf->flags & BM_PIN_COUNT_WAITER) &&
 			buf->refcount == 1)
@@ -1657,6 +2038,7 @@ SyncOneBuffer(int buf_id, bool skip_recently_used)
 	volatile BufferDesc *bufHdr = &BufferDescriptors[buf_id];
 	int			result = 0;
 
+
 	/*
 	 * Check whether buffer needs writing.
 	 *
@@ -1789,6 +2171,8 @@ PrintBufferLeakWarning(Buffer buffer)
 	char	   *path;
 	BackendId	backend;
 
+
+
 	Assert(BufferIsValid(buffer));
 	if (BufferIsLocal(buffer))
 	{
@@ -1799,12 +2183,28 @@ PrintBufferLeakWarning(Buffer buffer)
 	else
 	{
 		buf = &BufferDescriptors[buffer - 1];
+#ifdef USE_PREFETCH
+                /*   If reason that this buffer is pinned
+                **   is that it was prefetched with async_io
+                **   and never read or discarded, then omit the
+                **   warning,  because this is expected in some
+                **   cases when a scan is closed abnormally.
+                **   Note that the buffer will be released soon by our caller.
+                */
+                if (buf->flags & BM_AIO_PREFETCH_PIN_BANKED) {
+                    pgBufferUsage.aio_read_forgot++; /* account for it */
+                    return;
+                }
+#endif /*  USE_PREFETCH */
 		loccount = PrivateRefCount[buffer - 1];
 		backend = InvalidBackendId;
 	}
 
+/* #if defined(DO_ISCAN_DISCARD_BUFFER) && defined(DO_HEAPSCAN_DISCARD_BUFFER) not yet working safely 130530 */
 	/* theoretically we should lock the bufhdr here */
 	path = relpathbackend(buf->tag.rnode, backend, buf->tag.forkNum);
+
+
 	elog(WARNING,
 		 "buffer refcount leak: [%03d] "
 		 "(rel=%s, blockNum=%u, flags=0x%x, refcount=%u %d)",
@@ -1812,6 +2212,7 @@ PrintBufferLeakWarning(Buffer buffer)
 		 buf->tag.blockNum, buf->flags,
 		 buf->refcount, loccount);
 	pfree(path);
+/* #endif defined(DO_ISCAN_DISCARD_BUFFER) && defined(DO_HEAPSCAN_DISCARD_BUFFER) not yet working safely 130530 */
 }
 
 /*
@@ -1928,7 +2329,7 @@ FlushBuffer(volatile BufferDesc *buf, SMgrRelation reln)
 	 * false, then someone else flushed the buffer before we could, so we need
 	 * not do anything.
 	 */
-	if (!StartBufferIO(buf, false))
+	if (!StartBufferIO(buf, false, 0))
 		return;
 
 	/* Setup error traceback support for ereport() */
@@ -2512,6 +2913,70 @@ FlushDatabaseBuffers(Oid dbid)
 	}
 }
 
+#ifdef USE_PREFETCH
+/*
+ * DiscardBuffer -- discard shared buffer used for a previously
+ *                  prefetched but unread block of a relation
+ *
+ * If the buffer is found and pinned with a banked pin,  then :
+ *      .  if AIO in progress, terminate AIO without waiting
+ *      .  if AIO had already completed successfully,
+ *         then mark buffer valid (in case someone else wants it)
+ *      .  redeem the banked pin and unpin it.
+ *
+ * This function is similar in purpose to ReleaseBuffer (below)
+ * but sufficiently different that it is a separate function.
+ * Two important differences are :
+ *   .   caller identifies buffer by blocknumber,  not buffer number
+ *   .   we unpin buffer *only* if the pin is banked,
+ *                      *never* if pinned but not banked.
+ *       This is essential as caller may perform a sequence of
+ *  SCAN1   . PrefetchBuffer   (and remember block was prefetched)
+ *  SCAN2   . ReadBuffer       (but fails to connect this read to the prefetch by SCAN1)
+ *  SCAN1   . DiscardBuffer    (SCAN1 terminates early)
+ *  SCAN2   . access tuples in buffer
+ *        Clearly the Discard *must not* unpin the buffer since SCAN2 needs it!
+ *
+ *
+ * caller may pass InvalidBlockNumber as blockNum to mean do nothing
+ */
+void
+DiscardBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
+{
+        BufferTag	newTag;		   /* identity of requested block */
+        uint32		newHash;	   /* hash value for newTag */
+        LWLockId	newPartitionLock;  /* buffer partition lock for it */
+        Buffer		buf_id;
+        volatile        BufferDesc *buf_desc;
+
+    if (!SmgrIsTemp(reln->rd_smgr)) {
+	Assert(RelationIsValid(reln));
+	if (BlockNumberIsValid(blockNum)) {
+
+            /* create a tag so we can lookup the buffer */
+            INIT_BUFFERTAG(newTag, reln->rd_smgr->smgr_rnode.node,
+                                       forkNum, blockNum);
+
+            /* determine its hash code and partition lock ID */
+            newHash = BufTableHashCode(&newTag);
+            newPartitionLock = BufMappingPartitionLock(newHash);
+
+            /* see if the block is in the buffer pool already */
+            LWLockAcquire(newPartitionLock, LW_SHARED);
+            buf_id = BufTableLookup(&newTag, newHash);
+            LWLockRelease(newPartitionLock);
+
+            /* If in buffers, proceed */
+            if (buf_id >= 0) {
+                buf_desc = &BufferDescriptors[buf_id];        /* found buffer descriptor */
+                BufCheckAsync(0 , reln, buf_desc , BUF_INTENTION_REJECT_UNBANK , 0 , 0 , false , 0); /* end the IO and unpin if banked */
+                pgBufferUsage.aio_read_discrd++; /* account for it */
+            }
+        }
+    }
+}
+#endif   /* USE_PREFETCH */
+
 /*
  * ReleaseBuffer -- release the pin on a buffer
  */
@@ -2520,26 +2985,23 @@ ReleaseBuffer(Buffer buffer)
 {
 	volatile BufferDesc *bufHdr;
 
+
 	if (!BufferIsValid(buffer))
 		elog(ERROR, "bad buffer ID: %d", buffer);
 
-	ResourceOwnerForgetBuffer(CurrentResourceOwner, buffer);
 
 	if (BufferIsLocal(buffer))
 	{
+                ResourceOwnerForgetBuffer(CurrentResourceOwner, buffer);
 		Assert(LocalRefCount[-buffer - 1] > 0);
 		LocalRefCount[-buffer - 1]--;
 		return;
 	}
-
-	bufHdr = &BufferDescriptors[buffer - 1];
-
-	Assert(PrivateRefCount[buffer - 1] > 0);
-
-	if (PrivateRefCount[buffer - 1] > 1)
-		PrivateRefCount[buffer - 1]--;
 	else
-		UnpinBuffer(bufHdr, false);
+        {
+                bufHdr = &BufferDescriptors[buffer - 1];
+                BufCheckAsync(0 , 0 , bufHdr , BUF_INTENTION_REJECT_NOADJUST , 0 , 0 , false , 0 );
+        }
 }
 
 /*
@@ -2565,14 +3027,41 @@ UnlockReleaseBuffer(Buffer buffer)
 void
 IncrBufferRefCount(Buffer buffer)
 {
+        bool       pin_already_banked_by_me = false;  /* buffer is already pinned by me and redeemable */
+        volatile BufferDesc *buf;                     /* descriptor for a shared buffer */
+
 	Assert(BufferIsPinned(buffer));
+
+        if (!(BufferIsLocal(buffer))) {
+                buf = &BufferDescriptors[buffer - 1];
+		LockBufHdr(buf);
+                pin_already_banked_by_me =
+                      (    (buf->flags & BM_AIO_PREFETCH_PIN_BANKED)
+                        && (   (    (buf->flags & BM_AIO_IN_PROGRESS) ? ( ((BAiocbAnchr->BufferAiocbs)+(FREENEXT_BAIOCB_ORIGIN - buf->freeNext))->pidOfAio )
+                                                                      : (-(buf->freeNext))  )  == this_backend_pid )
+                      );
+        }
+
+        if (!pin_already_banked_by_me) {
 	ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
 	ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer);
+        }
+
 	if (BufferIsLocal(buffer))
 		LocalRefCount[-buffer - 1]++;
-	else
+	else {
+                if (pin_already_banked_by_me) {
+                    buf->flags &= ~BM_AIO_PREFETCH_PIN_BANKED;
+                    if (!(buf->flags & BM_AIO_IN_PROGRESS)) {
+                        buf->freeNext = FREENEXT_NOT_IN_LIST;      /*  forget the bank client */
+                    }
+                }
+		UnlockBufHdr(buf);
+                if (!pin_already_banked_by_me) {
 		PrivateRefCount[buffer - 1]++;
 }
+        }
+}
 
 /*
  * MarkBufferDirtyHint
@@ -2994,61 +3483,138 @@ WaitIO(volatile BufferDesc *buf)
  *
  * In some scenarios there are race conditions in which multiple backends
  * could attempt the same I/O operation concurrently.  If someone else
- * has already started I/O on this buffer then we will block on the
+ * has already started synchronous I/O on this buffer then we will block on the
  * io_in_progress lock until he's done.
  *
+ * if an async io is in progress and we are doing synchronous io,
+ * then readbuffer uses call to smgrcompleteaio to wait,
+ * and so we treat this request as if no io in progress
+ *
  * Input operations are only attempted on buffers that are not BM_VALID,
  * and output operations only on buffers that are BM_VALID and BM_DIRTY,
  * so we can always tell if the work is already done.
  *
+ * index_for_aio is an input parm which,  if non-zero,  identifies a BufferAiocb
+ * acquired by caller and to be attached to the buffer header for use with async io
+ *
  * Returns TRUE if we successfully marked the buffer as I/O busy,
  * FALSE if someone else already did the work.
  */
 static bool
-StartBufferIO(volatile BufferDesc *buf, bool forInput)
+StartBufferIO(volatile BufferDesc *buf, bool forInput , int index_for_aio )
 {
+#ifdef USE_PREFETCH
+        struct BufferAiocb volatile * BAiocb = (struct BufferAiocb*)0;  /*  BufferAiocb for use with aio */
+#endif   /* USE_PREFETCH */
+ 
+        if (!index_for_aio)
 	Assert(!InProgressBuf);
 
 	for (;;)
 	{
+                if (!index_for_aio) {
 		/*
 		 * Grab the io_in_progress lock so that other processes can wait for
 		 * me to finish the I/O.
 		 */
 		LWLockAcquire(buf->io_in_progress_lock, LW_EXCLUSIVE);
+                }
 
 		LockBufHdr(buf);
 
-		if (!(buf->flags & BM_IO_IN_PROGRESS))
+                /*     the following test is intended to distinguish between :
+                **      .   buffer which : 
+                **           .     has io in progress
+                **             AND is not associated with a current aio
+                **      .   not the above
+                **     Here,  "recent" means an aio marked by buf->freeNext <= FREENEXT_BAIOCB_ORIGIN but no longer in progress -
+                **          this situation arises when the aio has just been cancelled and this process now wishes to recycle the buffer.
+                **          In this case,  the first such would-be recycler (i.e. me) must :
+                **             . avoid waiting for the cancelled aio to complete
+                **             . if not myself doing async read, then assume responsibility for posting other future readbuffers.
+                */
+		if (    (buf->flags & BM_AIO_IN_PROGRESS)
+                     || (!(buf->flags & BM_IO_IN_PROGRESS))
+                   )
 			break;
 
 		/*
-		 * The only way BM_IO_IN_PROGRESS could be set when the io_in_progress
+		 * The only way BM_IO_IN_PROGRESS without AIO in progress could be set when the io_in_progress
 		 * lock isn't held is if the process doing the I/O is recovering from
 		 * an error (see AbortBufferIO).  If that's the case, we must wait for
 		 * him to get unwedged.
 		 */
 		UnlockBufHdr(buf);
+                if (!index_for_aio) {
 		LWLockRelease(buf->io_in_progress_lock);
+                }
 		WaitIO(buf);
 	}
 
-	/* Once we get here, there is definitely no I/O active on this buffer */
-
+#ifdef USE_PREFETCH
+	/* Once we get here, there is definitely no synchronous I/O active on this buffer
+        ** but if being asked to attach a BufferAiocb to the buf header,
+        ** then we must also check if there is any async io currently
+        ** in progress or pinned started by a different task.
+        */
+        if (index_for_aio) {
+            BAiocb = (BAiocbAnchr->BufferAiocbs)+(FREENEXT_BAIOCB_ORIGIN - buf->freeNext);
+            if (    (buf->flags & (BM_AIO_IN_PROGRESS|BM_AIO_PREFETCH_PIN_BANKED))
+                 && (buf->freeNext <= FREENEXT_BAIOCB_ORIGIN)
+                 && (BAiocb->pidOfAio != this_backend_pid)
+               ) {
+                    /* someone else already doing async I/O */
+                    UnlockBufHdr(buf);
+                    return false;
+            }
+	}
+#endif   /* USE_PREFETCH */
 	if (forInput ? (buf->flags & BM_VALID) : !(buf->flags & BM_DIRTY))
 	{
 		/* someone else already did the I/O */
 		UnlockBufHdr(buf);
+                if (!index_for_aio) {
 		LWLockRelease(buf->io_in_progress_lock);
+                }
 		return false;
 	}
 
 	buf->flags |= BM_IO_IN_PROGRESS;
 
+#ifdef USE_PREFETCH
+        if (index_for_aio) {
+            BAiocb = (BAiocbAnchr->BufferAiocbs)+(FREENEXT_BAIOCB_ORIGIN - index_for_aio);
+            /*   insist that no other buffer is using this BufferAiocb for async IO */
+            if (BAiocb->BAiocbbufh == (struct sbufdesc *)0) {
+                BAiocb->BAiocbbufh = buf;
+            }
+            if (BAiocb->BAiocbbufh != buf) {
+                               ereport(ERROR,
+                                    (errcode(ERRCODE_INTERNAL_ERROR),
+                                     errmsg("AIO control block %p to be used by %p already in use by %p"
+                                              ,BAiocb ,buf , BAiocb->BAiocbbufh)));
+            }
+            /*   note - there is no need to register self as an dependent of BAiocb
+            **   as we shall not unlock buf_desc before we free the BAiocb
+            */
+
+            buf->flags |= BM_AIO_IN_PROGRESS;
+            buf->freeNext = index_for_aio;
+            /*  at this point,  this buffer appears to have an in-progress aio_read,
+            **  and any other task which is able to look inside the buffer might try waiting on that aio -
+            **  except we have not yet issued the aio!   So we must keep the buffer header locked
+            **  from here all the way back to the BufStartAsync caller
+            */
+        } else {
+#endif   /* USE_PREFETCH */
+
 	UnlockBufHdr(buf);
 
 	InProgressBuf = buf;
 	IsForInput = forInput;
+#ifdef USE_PREFETCH
+        }
+#endif   /* USE_PREFETCH */
 
 	return true;
 }
@@ -3058,7 +3624,7 @@ StartBufferIO(volatile BufferDesc *buf, bool forInput)
  *	(Assumptions)
  *	My process is executing IO for the buffer
  *	BM_IO_IN_PROGRESS bit is set for the buffer
- *	We hold the buffer's io_in_progress lock
+ *	if no async IO in progress,  then We hold the buffer's io_in_progress lock
  *	The buffer is Pinned
  *
  * If clear_dirty is TRUE and BM_JUST_DIRTIED is not set, we clear the
@@ -3070,26 +3636,32 @@ StartBufferIO(volatile BufferDesc *buf, bool forInput)
  * BM_IO_ERROR in a failure case.  For successful completion it could
  * be 0, or BM_VALID if we just finished reading in the page.
  */
-static void
+void
 TerminateBufferIO(volatile BufferDesc *buf, bool clear_dirty,
 				  int set_flag_bits)
 {
-	Assert(buf == InProgressBuf);
+        int flags_on_entry;
 
 	LockBufHdr(buf);
 
+        flags_on_entry = buf->flags;
+
+        if (! (flags_on_entry & BM_AIO_IN_PROGRESS) )
+            Assert( buf == InProgressBuf );
+
 	Assert(buf->flags & BM_IO_IN_PROGRESS);
-	buf->flags &= ~(BM_IO_IN_PROGRESS | BM_IO_ERROR);
+	buf->flags &= ~(BM_IO_IN_PROGRESS | BM_IO_ERROR | BM_AIO_IN_PROGRESS);
 	if (clear_dirty && !(buf->flags & BM_JUST_DIRTIED))
 		buf->flags &= ~(BM_DIRTY | BM_CHECKPOINT_NEEDED);
 	buf->flags |= set_flag_bits;
 
 	UnlockBufHdr(buf);
 
+        if (! (flags_on_entry & BM_AIO_IN_PROGRESS) ) {
 	InProgressBuf = NULL;
-
 	LWLockRelease(buf->io_in_progress_lock);
 }
+}
 
 /*
  * AbortBufferIO: Clean up any active buffer I/O after an error.
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index 1f69c9e..4133506 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -77,6 +77,9 @@
 #include "utils/guc.h"
 #include "utils/resowner_private.h"
 
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+#include "aio.h"
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
 
 /*
  * We must leave some file descriptors free for system(), the dynamic loader,
@@ -1239,6 +1242,10 @@ FileClose(File file)
  * We could add an implementation using libaio in the future; but note that
  * this API is inappropriate for libaio, which wants to have a buffer provided
  * to read into.
+ * Also note that a new, different implementation of asynchronous prefetch
+ * using librt,  not libaio,  is provided by the two functions following this one,
+ * FileStartaio and FileCompleteaio.   These also require to have a buffer provided
+ * to read into,  which the new async_io support provides.
  */
 int
 FilePrefetch(File file, off_t offset, int amount)
@@ -1266,6 +1273,139 @@ FilePrefetch(File file, off_t offset, int amount)
 #endif
 }
 
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+/*
+ * FileInitaio - initialize the aio subsystem max number of threads and max number of requests
+ *  input parms
+ *  max_aio_threads;    maximum number of threads
+ *  max_aio_num;        maximum number of concurrent aio read requests
+ *
+ *  on linux, man page for the librt implemenation of aio_init() says :
+ *         This function is a GNU extension.
+ *  If your posix aio does not have it,   then add the following line to 
+ *        src/include/pg_config_manual.h
+ *    #define DONT_HAVE_AIO_INIT
+ *  to render it as a no-op
+ */
+void
+FileInitaio(int max_aio_threads, int max_aio_num )
+{
+#ifndef DONT_HAVE_AIO_INIT
+    struct aioinit aioinit_struct;  /*  structure to pass to aio_init */
+
+    aioinit_struct.aio_threads = max_aio_threads; /*     maximum number of threads */
+    aioinit_struct.aio_num = max_aio_num;         /*     maximum number of concurrent aio read requests */
+    aioinit_struct.aio_idle_time = 1;             /*     we dont want to alter this but aio_init does not ignore it so set to the default */
+    aio_init(&aioinit_struct);
+#endif  /* ndef DONT_HAVE_AIO_INIT */
+    return;
+}
+
+/*
+ * FileStartaio - initiate asynchronous read of a given range of the file.
+ * The logical seek position is unaffected.
+ *
+ * use standard posix aio (librt)
+ *  ASSUME   BufferAiocb.aio_buf already set to -> buffer by caller
+ *  return 0 if successfully started,  else non-zero
+ */
+int
+FileStartaio(File file, off_t offset, int amount , char *aiocbp )
+{
+	int	returnCode;
+        struct aiocb *my_aiocbp = (struct aiocb *)aiocbp;
+
+	Assert(FileIsValid(file));
+
+	DO_DB(elog(LOG, "FileStartaio: %d (%s) " INT64_FORMAT " %d",
+			   file, VfdCache[file].fileName,
+			   (int64) offset, amount));
+
+	returnCode = FileAccess(file);
+	if (returnCode >= 0) {
+
+            my_aiocbp->aio_fildes = VfdCache[file].fd;
+            my_aiocbp->aio_lio_opcode = LIO_READ;
+            my_aiocbp->aio_nbytes = amount;
+            my_aiocbp->aio_offset = offset;
+            returnCode = aio_read(my_aiocbp);
+        }
+
+	return returnCode;
+}
+
+/*
+ * FileCompleteaio - complete asynchronous aio read
+ * normal_wait indicates whether to cancel or wait -
+ *                 0 <=> cancel
+ *                 1 <=> wait
+ *
+ * use standard posix aio (librt)
+ *  return 0 if successfull and did not have to wait,
+ *         1 if successfull and had to wait,
+ *    else x'ff'
+ */
+int
+FileCompleteaio( char *aiocbp , int normal_wait )
+{
+	int	returnCode;
+	int	aio_errno;
+        struct aiocb *my_aiocbp = (struct aiocb *)aiocbp;
+        const struct aiocb const*cblist[1];
+        int fd;
+        struct timespec my_timeout = { 0 , 10000 };
+        int max_polls;
+
+        fd = my_aiocbp->aio_fildes;
+        cblist[0] = my_aiocbp;
+        returnCode = aio_errno = aio_error(my_aiocbp);
+        /* note that aio_error returns 0 if op already completed successfully */
+
+        /*  first handle normal case of waiting for op to complete  */
+        if (normal_wait) {
+            while (aio_errno == EINPROGRESS) {
+                max_polls = 256;
+                my_timeout.tv_sec = 0; my_timeout.tv_nsec = 10000;
+                returnCode = aio_suspend(cblist , 1 , &my_timeout);
+                while ((returnCode < 0) && (EAGAIN == errno) && (max_polls-- > 0)) {
+                    my_timeout.tv_sec = 0; my_timeout.tv_nsec = 10000;
+                    returnCode = aio_suspend(cblist , 1 , &my_timeout);
+                }
+                returnCode = aio_errno = aio_error(my_aiocbp);
+                /*  now return_code is from aio_error  */
+                if (returnCode == 0) {
+                    returnCode = 1;    /*  successful but had to wait */
+                }
+            }
+            if (aio_errno) {
+                elog(LOG, "FileCompleteaio: %d %d", fd, returnCode);
+                returnCode = 0xff; /*  unsuccessful */
+            }
+        } else {
+            if (aio_errno == EINPROGRESS) {
+                do {
+                        max_polls = 256;
+                        my_timeout.tv_sec = 0; my_timeout.tv_nsec = 10000;
+                        returnCode = aio_cancel(fd, my_aiocbp);
+                        while ((returnCode == AIO_NOTCANCELED) && (max_polls-- > 0)) {
+                            my_timeout.tv_sec = 0; my_timeout.tv_nsec = 10000;
+                            returnCode = aio_cancel(fd, my_aiocbp);
+                        }
+                    returnCode = aio_errno = aio_error(my_aiocbp);
+                } while (aio_errno == EINPROGRESS);
+                returnCode = 0xff; /*  unsuccessful */
+            }
+            if (returnCode != 0)
+                returnCode = 0xff; /*  unsuccessful */
+        }
+
+	DO_DB(elog(LOG, "FileCompleteaio: %d %d",
+			   fd, returnCode));
+
+	return returnCode;
+}
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
+
 int
 FileRead(File file, char *buffer, int amount)
 {
diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index 266b0da..3f13ded 100644
--- a/src/backend/storage/lmgr/proc.c
+++ b/src/backend/storage/lmgr/proc.c
@@ -52,6 +52,7 @@
 #include "utils/timeout.h"
 #include "utils/timestamp.h"
 
+extern pid_t this_backend_pid;     /*   pid of this backend */
 
 /* GUC variables */
 int			DeadlockTimeout = 1000;
@@ -361,6 +362,7 @@ InitProcess(void)
 	MyPgXact->xid = InvalidTransactionId;
 	MyPgXact->xmin = InvalidTransactionId;
 	MyProc->pid = MyProcPid;
+        this_backend_pid = getpid();    /*    pid of this backend */
 	/* backendId, databaseId and roleId will be filled in later */
 	MyProc->backendId = InvalidBackendId;
 	MyProc->databaseId = InvalidOid;
diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c
index 3c1c81a..e0ca942 100644
--- a/src/backend/storage/smgr/md.c
+++ b/src/backend/storage/smgr/md.c
@@ -647,6 +647,62 @@ mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
 }
 
 
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+/*
+ *	mdinitaio() --  init the aio subsystem max number of threads and max number of requests
+ */
+void
+mdinitaio(int max_aio_threads, int max_aio_num)
+{
+     FileInitaio( max_aio_threads, max_aio_num );
+}
+
+/*
+ *	mdstartaio() -- start aio read of the specified block of a relation
+ */
+void
+mdstartaio(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum , char *aiocbp , int *retcode )
+{
+#ifdef USE_PREFETCH
+	off_t		seekpos;
+	MdfdVec    *v;
+        int local_retcode;
+
+	v = _mdfd_getseg(reln, forknum, blocknum, false, EXTENSION_FAIL);
+
+	seekpos = (off_t) BLCKSZ *(blocknum % ((BlockNumber) RELSEG_SIZE));
+
+	Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
+
+	local_retcode = FileStartaio(v->mdfd_vfd, seekpos, BLCKSZ , aiocbp);
+	if (retcode) {
+            *retcode = local_retcode;
+        }
+#endif   /* USE_PREFETCH */
+}
+
+
+/*
+ *	mdcompleteaio() -- complete aio read of the specified block of a relation
+ *      on entry, *inoutcode should indicate :
+ *           .  non-0  <=>   check if complete and wait if not
+ *           .  0      <=>   cancel io immediately
+ */
+void
+mdcompleteaio( char *aiocbp , int *inoutcode )
+{
+#ifdef USE_PREFETCH
+        int local_retcode;
+
+	local_retcode = FileCompleteaio(aiocbp, (inoutcode ? *inoutcode : 0));
+	if (inoutcode) {
+            *inoutcode = local_retcode;
+        }
+#endif   /* USE_PREFETCH */
+}
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
+
+
 /*
  *	mdread() -- Read the specified block from a relation.
  */
diff --git a/src/backend/storage/smgr/smgr.c b/src/backend/storage/smgr/smgr.c
index d16f559..7f3e6ff 100644
--- a/src/backend/storage/smgr/smgr.c
+++ b/src/backend/storage/smgr/smgr.c
@@ -49,6 +49,12 @@ typedef struct f_smgr
 						 BlockNumber blocknum, char *buffer, bool skipFsync);
 	void		(*smgr_prefetch) (SMgrRelation reln, ForkNumber forknum,
 											  BlockNumber blocknum);
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+	void		(*smgr_initaio) (int max_aio_threads, int max_aio_num);
+	void		(*smgr_startaio) (SMgrRelation reln, ForkNumber forknum,
+											  BlockNumber blocknum , char *aiocbp , int *retcode );
+	void		(*smgr_completeaio) ( char *aiocbp , int *inoutcode );
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
 	void		(*smgr_read) (SMgrRelation reln, ForkNumber forknum,
 										  BlockNumber blocknum, char *buffer);
 	void		(*smgr_write) (SMgrRelation reln, ForkNumber forknum,
@@ -66,7 +72,11 @@ typedef struct f_smgr
 static const f_smgr smgrsw[] = {
 	/* magnetic disk */
 	{mdinit, NULL, mdclose, mdcreate, mdexists, mdunlink, mdextend,
-		mdprefetch, mdread, mdwrite, mdnblocks, mdtruncate, mdimmedsync,
+		mdprefetch
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+                        ,mdinitaio, mdstartaio, mdcompleteaio
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
+              , mdread, mdwrite, mdnblocks, mdtruncate, mdimmedsync,
 		mdpreckpt, mdsync, mdpostckpt
 	}
 };
@@ -612,6 +622,35 @@ smgrprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
 	(*(smgrsw[reln->smgr_which].smgr_prefetch)) (reln, forknum, blocknum);
 }
 
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+/*
+ *	smgrinitaio() -- initialize the aio subsystem max number of threads and max number of requests
+ */
+void
+smgrinitaio(int max_aio_threads, int max_aio_num)
+{
+	(*(smgrsw[0].smgr_initaio)) ( max_aio_threads, max_aio_num );
+}
+
+/*
+ *	smgrstartaio() -- Initiate aio read of the specified block of a relation.
+ */
+void
+smgrstartaio(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum , char *aiocbp , int *retcode )
+{
+	(*(smgrsw[reln->smgr_which].smgr_startaio)) (reln, forknum, blocknum , aiocbp , retcode );
+}
+
+/*
+ *	smgrcompleteaio() -- Complete aio read of the specified block of a relation.
+ */
+void
+smgrcompleteaio(SMgrRelation reln,  char *aiocbp , int *inoutcode )
+{
+	(*(smgrsw[reln->smgr_which].smgr_completeaio)) ( aiocbp , inoutcode );
+}
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
+
 /*
  *	smgrread() -- read a particular block from a relation into the supplied
  *				  buffer.
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 1d094f0..d36b77d 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -2264,6 +2264,25 @@ static struct config_int ConfigureNamesInt[] =
 	},
 
 	{
+		{"max_async_io_prefetchers",
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+			PGC_USERSET,
+#else
+			PGC_INTERNAL,
+#endif
+			RESOURCES_ASYNCHRONOUS,
+			gettext_noop("Maximum number of background processes concurrently using asynchronous librt threads to prefetch pages into shared memory buffers."),
+		},
+		&max_async_io_prefetchers,
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+	        -1, 0, 8192,      /*  boot val -1 indicates to initialize to something sensible during buf_init */
+#else
+		0, 0, 0,
+#endif
+		NULL, NULL, NULL
+	},
+
+	{
 		{"max_worker_processes",
 			PGC_POSTMASTER,
 			RESOURCES_ASYNCHRONOUS,
diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
index 743455e..e392390 100644
--- a/src/backend/utils/mmgr/aset.c
+++ b/src/backend/utils/mmgr/aset.c
@@ -733,6 +733,48 @@ AllocSetAlloc(MemoryContext context, Size size)
 	 */
 	fidx = AllocSetFreeIndex(size);
 	chunk = set->freelist[fidx];
+#ifdef MEMORY_CONTEXT_CHECKING
+        /*    an instance of segfault caused by a rogue value in set->freelist[fidx]
+        **    has been seen - check for it using crude sanity check based on neighbours :
+        **    if at least one is sufficiently close, then pass,  else fail
+        */
+        if (chunk != 0) {
+            int frx, nrx; /*  frx is index,  nrx is index of failing neighbour for errmsg */
+            for (nrx = -1, frx = 0; (frx < ALLOCSET_NUM_FREELISTS); frx++) {
+                if (   (frx != fidx)     /*  not the chosen one */
+                    && ( ( (unsigned long)(set->freelist[frx]) ) != 0 ) /* not empty */
+                   ) {
+                    if (   ( (unsigned long)chunk < ( ( (unsigned long)(set->freelist[frx]) ) / 2 ) )
+                        && (  ( (unsigned long)(set->freelist[frx]) ) < 0x4000000  )
+               /***     || ( (unsigned long)chunk > ( ( (unsigned long)(set->freelist[frx]) ) * 2 ) )  ***/
+                       ) {
+                       nrx = frx;
+                    } else {
+                       nrx = -1;
+                       break;
+                    }
+                }
+            }
+
+            if (nrx >= 0) {
+                     /*  this must be a rogue value  -  put this list in the garbage */
+                     /*  build message but be careful to avoid recursively triggering same fault */
+                     set->freelist[fidx] = NULL;   /*  mark this list empty */
+                     elog(WARNING, "detected rogue value %p in freelist index %d compared with neighbour %p whose chunksize %d"
+				 , chunk , fidx , set->freelist[nrx] , set->freelist[nrx]->size);
+                     chunk = NULL;
+            }
+        }
+#else /* if not MEMORY_CONTEXT_CHECKING make very simple-minded check*/
+        if ( (chunk != 0) && ( (unsigned long)chunk <  0x40000 ) ) {
+                     /*  this must be a rogue value  -  put this list in the garbage */
+                     /*  build message but be careful to avoid recursively triggering same fault */
+                     set->freelist[fidx] = NULL;   /*  mark this list empty */
+                     elog(WARNING, "detected rogue value %p in freelist index %d"
+				 , chunk , fidx);
+                     chunk = NULL;
+        }
+#endif
 	if (chunk != NULL)
 	{
 		Assert(chunk->size >= size);
diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h
index 493839f..3ce5618 100644
--- a/src/include/access/heapam.h
+++ b/src/include/access/heapam.h
@@ -175,7 +175,7 @@ extern void heap_page_prune_execute(Buffer buffer,
 extern void heap_get_root_tuples(Page page, OffsetNumber *root_offsets);
 
 /* in heap/syncscan.c */
-extern void ss_report_location(Relation rel, BlockNumber location);
+extern void ss_report_location(Relation rel, BlockNumber location , BlockNumber *prefetchHWMp);
 extern BlockNumber ss_get_location(Relation rel, BlockNumber relnblocks);
 extern void SyncScanShmemInit(void);
 extern Size SyncScanShmemSize(void);
diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h
index f281759..34cf496 100644
--- a/src/include/access/nbtree.h
+++ b/src/include/access/nbtree.h
@@ -19,6 +19,7 @@
 #include "access/sdir.h"
 #include "access/xlog.h"
 #include "access/xlogutils.h"
+#include "access/relscan.h"
 #include "catalog/pg_index.h"
 
 /* There's room for a 16-bit vacuum cycle ID in BTPageOpaqueData */
@@ -524,6 +525,7 @@ typedef struct BTScanPosData
 	Buffer		buf;			/* if valid, the buffer is pinned */
 
 	BlockNumber nextPage;		/* page's right link when we scanned it */
+	BlockNumber prevPage;		/* page's left link when we scanned it */
 
 	/*
 	 * moreLeft and moreRight track whether we think there may be matching
@@ -603,6 +605,15 @@ typedef struct BTScanOpaqueData
 	 */
 	int			markItemIndex;	/* itemIndex, or -1 if not valid */
 
+	/* prefetch logic state */
+	unsigned int	backSeqRun;	/* number of back-sequential pages in a run */
+	BlockNumber		backSeqPos;	/* blkid last prefetched in back-sequential 
+				          		   runs */
+	BlockNumber		lastHeapPrefetchBlkno;	/* blkid last prefetched from heap */
+	int				prefetchItemIndex; /* item index within currPos last
+					                      fetched by heap prefetch */
+	int				prefetchBlockCount; /* number of prefetched heap blocks */
+	
 	/* keep these last in struct for efficiency */
 	BTScanPosData currPos;		/* current position data */
 	BTScanPosData markPos;		/* marked position, if any */
@@ -655,7 +666,11 @@ extern Buffer _bt_getroot(Relation rel, int access);
 extern Buffer _bt_gettrueroot(Relation rel);
 extern int	_bt_getrootheight(Relation rel);
 extern void _bt_checkpage(Relation rel, Buffer buf);
-extern Buffer _bt_getbuf(Relation rel, BlockNumber blkno, int access);
+extern Buffer _bt_getbuf(Relation rel, BlockNumber blkno, int access , struct pfch_index_pagelist* pfch_index_page_list);
+extern void _bt_prefetchbuf(Relation rel, BlockNumber blkno , struct pfch_index_pagelist** pfch_index_page_list_P);
+extern struct pfch_index_item* _bt_find_block(BlockNumber blkno , struct pfch_index_pagelist* pfch_index_page_list);
+extern int _bt_add_block(BlockNumber blkno , struct pfch_index_pagelist** pfch_index_page_list_P , uint32 discard_status);
+extern void _bt_subtract_block(BlockNumber blkno , struct pfch_index_pagelist* pfch_index_page_list);
 extern Buffer _bt_relandgetbuf(Relation rel, Buffer obuf,
 				 BlockNumber blkno, int access);
 extern void _bt_relbuf(Relation rel, Buffer buf);
diff --git a/src/include/access/relscan.h b/src/include/access/relscan.h
index 8a57698..cbf3100 100644
--- a/src/include/access/relscan.h
+++ b/src/include/access/relscan.h
@@ -44,6 +44,24 @@ typedef struct HeapScanDescData
 	bool		rs_inited;		/* false = scan not init'd yet */
 	HeapTupleData rs_ctup;		/* current tuple in scan, if any */
 	BlockNumber rs_cblock;		/* current block # in scan, if any */
+#ifdef USE_PREFETCH
+	int	    rs_prefetch_target; /* target distance (numblocks) for prefetch to reach beyond main scan */
+	BlockNumber rs_pfchblock;	/* next block # to be prefetched in scan, if any */
+
+        /*   Unread_Pfetched is a "mostly" circular list of recently prefetched blocknos of size target_prefetch_pages
+        **   the index of the first unread block is held in Unread_Pfetched_next
+        **         and is advanced when a block is read
+        **   the count of number of unread blocks is in Unread_Pfetched_count (and this subset can wrap around)
+        **   "mostly" means that there may be gaps caused by storing entries for blocks which do not need to be discarded -
+        **   these are indicated by blockno = InvalidBlockNumber,  and these slots are reused when found.
+        */
+        BlockNumber *rs_Unread_Pfetched_base;   /*  where the list of prefetched but unread blocknos starts */
+        unsigned int rs_Unread_Pfetched_next;   /*  where the next unread blockno probably is relative to start --
+                                                **  this is only a hint which may be temporarily stale.
+                                                */
+        unsigned int rs_Unread_Pfetched_count;  /*  number of valid unread blocknos in list */
+#endif   /* USE_PREFETCH */
+
 	Buffer		rs_cbuf;		/* current buffer in scan, if any */
 	/* NB: if rs_cbuf is not InvalidBuffer, we hold a pin on that buffer */
 	ItemPointerData rs_mctid;	/* marked scan position, if any */
@@ -55,6 +73,27 @@ typedef struct HeapScanDescData
 	OffsetNumber rs_vistuples[MaxHeapTuplesPerPage];	/* their offsets */
 }	HeapScanDescData;
 
+/* pfch_index_items track prefetched and unread index pages -   chunks of blocknumbers are chained in singly-linked list from scan->pfch_index_item_list */
+struct pfch_index_item {                              /*  index-relation BlockIds which we will/have prefetched */
+       BlockNumber         pfch_blocknum;    /* Blocknum which we will/have prefetched */
+       uint32              pfch_discard;     /* whether block is to be discarded when scan is closed */
+};
+
+struct pfch_block_item {
+       struct BlockIdData  pfch_blockid;     /* BlockId which we will/have prefetched */
+       uint32              pfch_discard;     /* whether block is to be discarded when scan is closed */
+};
+
+/* pfch_index_page_items track prefetched and unread index pages -
+** chunks of blocknumbers are chained backwards (newest first,  oldest last)
+** in singly-linked list from scan->pfch_index_item_list
+*/
+struct pfch_index_pagelist {                          /*  index-relation BlockIds which we will/have prefetched */
+       struct pfch_index_pagelist* pfch_index_pagelist_next;  /*  pointer to next chunk if any */
+       unsigned int    pfch_index_item_count;         /*  number of used entries in this chunk */
+       struct pfch_index_item pfch_indexid[1];        /*  in-line list of Blocknums which we will/have prefetched and whether to be discarded */
+};
+
 /*
  * We use the same IndexScanDescData structure for both amgettuple-based
  * and amgetbitmap-based index scans.  Some fields are only relevant in
@@ -75,8 +114,15 @@ typedef struct IndexScanDescData
 	/* signaling to index AM about killing index tuples */
 	bool		kill_prior_tuple;		/* last-returned tuple is dead */
 	bool		ignore_killed_tuples;	/* do not return killed entries */
-	bool		xactStartedInRecovery;	/* prevents killing/seeing killed
-										 * tuples */
+	bool		xactStartedInRecovery;	/* prevents killing/seeing killed tuples */
+										 
+#ifdef USE_PREFETCH
+        struct pfch_index_pagelist* pfch_index_page_list;  /* array of index-relation BlockIds which we will/have prefetched */
+        struct pfch_block_item* pfch_block_item_list;   /* array of heap-relation BlockIds which we will/have prefetched */
+        unsigned short int     pfch_used;	/*  number of used elements in BlockIdData array   */
+        unsigned short int     pfch_next;	/*  next element for prefetch in BlockIdData array */
+	int             do_prefetch;    /*  should I prefetch ? */
+#endif   /* USE_PREFETCH */
 
 	/* index access method's private state */
 	void	   *opaque;			/* access-method-specific info */
@@ -91,6 +137,10 @@ typedef struct IndexScanDescData
 	/* NB: if xs_cbuf is not InvalidBuffer, we hold a pin on that buffer */
 	bool		xs_recheck;		/* T means scan keys must be rechecked */
 
+	/* heap fetch statistics for read-ahead logic */
+	unsigned int heap_tids_seen;
+	unsigned int heap_tids_fetched;
+
 	/* state data for traversing HOT chains in index_getnext */
 	bool		xs_continue_hot;	/* T if must keep walking HOT chain */
 }	IndexScanDescData;
diff --git a/src/include/catalog/pg_am.h b/src/include/catalog/pg_am.h
index 759ea70..dc461b6 100644
--- a/src/include/catalog/pg_am.h
+++ b/src/include/catalog/pg_am.h
@@ -67,6 +67,7 @@ CATALOG(pg_am,2601)
 	regproc		amcanreturn;	/* can indexscan return IndexTuples? */
 	regproc		amcostestimate; /* estimate cost of an indexscan */
 	regproc		amoptions;		/* parse AM-specific parameters */
+	regproc		ampeeknexttuple;	/* peek at the next tuple different from any blocknum in pfch_list without reading a new index page */
 } FormData_pg_am;
 
 /* ----------------
@@ -117,19 +118,19 @@ typedef FormData_pg_am *Form_pg_am;
  * ----------------
  */
 
-DATA(insert OID = 403 (  btree		5 2 t f t t t t t t f t t 0 btinsert btbeginscan btgettuple btgetbitmap btrescan btendscan btmarkpos btrestrpos btbuild btbuildempty btbulkdelete btvacuumcleanup btcanreturn btcostestimate btoptions ));
+DATA(insert OID = 403 (  btree		5 2 t f t t t t t t f t t 0 btinsert btbeginscan btgettuple btgetbitmap btrescan btendscan btmarkpos btrestrpos btbuild btbuildempty btbulkdelete btvacuumcleanup btcanreturn btcostestimate btoptions btpeeknexttuple ));
 DESCR("b-tree index access method");
 #define BTREE_AM_OID 403
-DATA(insert OID = 405 (  hash		1 1 f f t f f f f f f f f 23 hashinsert hashbeginscan hashgettuple hashgetbitmap hashrescan hashendscan hashmarkpos hashrestrpos hashbuild hashbuildempty hashbulkdelete hashvacuumcleanup - hashcostestimate hashoptions ));
+DATA(insert OID = 405 (  hash		1 1 f f t f f f f f f f f 23 hashinsert hashbeginscan hashgettuple hashgetbitmap hashrescan hashendscan hashmarkpos hashrestrpos hashbuild hashbuildempty hashbulkdelete hashvacuumcleanup - hashcostestimate hashoptions - ));
 DESCR("hash index access method");
 #define HASH_AM_OID 405
-DATA(insert OID = 783 (  gist		0 8 f t f f t t f t t t f 0 gistinsert gistbeginscan gistgettuple gistgetbitmap gistrescan gistendscan gistmarkpos gistrestrpos gistbuild gistbuildempty gistbulkdelete gistvacuumcleanup - gistcostestimate gistoptions ));
+DATA(insert OID = 783 (  gist		0 8 f t f f t t f t t t f 0 gistinsert gistbeginscan gistgettuple gistgetbitmap gistrescan gistendscan gistmarkpos gistrestrpos gistbuild gistbuildempty gistbulkdelete gistvacuumcleanup - gistcostestimate gistoptions - ));
 DESCR("GiST index access method");
 #define GIST_AM_OID 783
-DATA(insert OID = 2742 (  gin		0 6 f f f f t t f f t f f 0 gininsert ginbeginscan - gingetbitmap ginrescan ginendscan ginmarkpos ginrestrpos ginbuild ginbuildempty ginbulkdelete ginvacuumcleanup - gincostestimate ginoptions ));
+DATA(insert OID = 2742 (  gin		0 6 f f f f t t f f t f f 0 gininsert ginbeginscan - gingetbitmap ginrescan ginendscan ginmarkpos ginrestrpos ginbuild ginbuildempty ginbulkdelete ginvacuumcleanup - gincostestimate ginoptions - ));
 DESCR("GIN index access method");
 #define GIN_AM_OID 2742
-DATA(insert OID = 4000 (  spgist	0 5 f f f f f t f t f f f 0 spginsert spgbeginscan spggettuple spggetbitmap spgrescan spgendscan spgmarkpos spgrestrpos spgbuild spgbuildempty spgbulkdelete spgvacuumcleanup spgcanreturn spgcostestimate spgoptions ));
+DATA(insert OID = 4000 (  spgist	0 5 f f f f f t f t f f f 0 spginsert spgbeginscan spggettuple spggetbitmap spgrescan spgendscan spgmarkpos spgrestrpos spgbuild spgbuildempty spgbulkdelete spgvacuumcleanup spgcanreturn spgcostestimate spgoptions - ));
 DESCR("SP-GiST index access method");
 #define SPGIST_AM_OID 4000
 
diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h
index 72170af..f61c05a 100644
--- a/src/include/catalog/pg_proc.h
+++ b/src/include/catalog/pg_proc.h
@@ -536,6 +536,12 @@ DESCR("convert float4 to int4");
 
 DATA(insert OID = 330 (  btgettuple		   PGNSP PGUID 12 1 0 0 0 f f f f t f v 2 0 16 "2281 2281" _null_ _null_ _null_ _null_	btgettuple _null_ _null_ _null_ ));
 DESCR("btree(internal)");
+
+#ifndef AVOID_CATALOG_MIGRATION_FOR_ASYNCIO
+DATA(insert OID = 3251 (  btpeeknexttuple	   PGNSP PGUID 12 1 0 0 0 f f f f t f v 1 0 16 "2281" _null_ _null_ _null_ _null_ btpeeknexttuple _null_ _null_ _null_ ));
+DESCR("btree(internal)");
+#endif /* not AVOID_CATALOG_MIGRATION_FOR_ASYNCIO */
+
 DATA(insert OID = 636 (  btgetbitmap	   PGNSP PGUID 12 1 0 0 0 f f f f t f v 2 0 20 "2281 2281" _null_ _null_ _null_ _null_	btgetbitmap _null_ _null_ _null_ ));
 DESCR("btree(internal)");
 DATA(insert OID = 331 (  btinsert		   PGNSP PGUID 12 1 0 0 0 f f f f t f v 6 0 16 "2281 2281 2281 2281 2281 2281" _null_ _null_ _null_ _null_	btinsert _null_ _null_ _null_ ));
diff --git a/src/include/executor/instrument.h b/src/include/executor/instrument.h
index 8ec1033..8c86781 100644
--- a/src/include/executor/instrument.h
+++ b/src/include/executor/instrument.h
@@ -28,8 +28,18 @@ typedef struct BufferUsage
 	long		local_blks_written;		/* # of local disk blocks written */
 	long		temp_blks_read; /* # of temp blocks read */
 	long		temp_blks_written;		/* # of temp blocks written */
+
 	instr_time	blk_read_time;	/* time spent reading */
 	instr_time	blk_write_time; /* time spent writing */
+
+	long		aio_read_noneed;		/* # of prefetches for which no need for prefetch as block already in buffer pool */
+	long		aio_read_discrd;		/* # of prefetches for which no need for prefetch as block already in buffer pool */
+	long		aio_read_forgot;		/* # of prefetches for which no need for prefetch as block already in buffer pool */
+	long		aio_read_noblok;		/* # of prefetches for which no available BufferAiocb */
+	long		aio_read_failed;		/* # of aio reads for which aio itself failed or the read failed with an errno */
+	long		aio_read_wasted;		/* # of aio reads for which disk block not used */
+	long		aio_read_waited;		/* # of aio reads for which disk block used but had to wait for it */
+	long		aio_read_ontime;		/* # of aio reads for which disk block used and ready on time when requested */
 } BufferUsage;
 
 /* Flag bits included in InstrAlloc's instrument_options bitmask */
diff --git a/src/include/nodes/tidbitmap.h b/src/include/nodes/tidbitmap.h
index 444d4d8..4998dbc 100644
--- a/src/include/nodes/tidbitmap.h
+++ b/src/include/nodes/tidbitmap.h
@@ -41,6 +41,16 @@ typedef struct
 	int			ntuples;		/* -1 indicates lossy result */
 	bool		recheck;		/* should the tuples be rechecked? */
 	/* Note: recheck is always true if ntuples < 0 */
+#ifdef USE_PREFETCH
+        /*   Unread_Pfetched is a circular list of recently prefetched blocknos of size target_prefetch_pages
+        **   the index of the first unread block is held in Unread_Pfetched_next
+        **         and is advanced when a block is read
+        **   the count of number of unread blocks is in Unread_Pfetched_count (and this subset can wrap around)
+        */
+        BlockNumber *Unread_Pfetched_base;   /*  where the list of prefetched but unread blocknos starts */
+        unsigned int Unread_Pfetched_next;   /*  where the next unread blockno is relative to start */
+        unsigned int Unread_Pfetched_count;  /*  number of valid unread blocknos in list */
+#endif   /* USE_PREFETCH */
 	OffsetNumber offsets[1];	/* VARIABLE LENGTH ARRAY */
 } TBMIterateResult;				/* VARIABLE LENGTH STRUCT */
 
@@ -62,5 +72,8 @@ extern bool tbm_is_empty(const TIDBitmap *tbm);
 extern TBMIterator *tbm_begin_iterate(TIDBitmap *tbm);
 extern TBMIterateResult *tbm_iterate(TBMIterator *iterator);
 extern void tbm_end_iterate(TBMIterator *iterator);
-
+extern void tbm_zero(TBMIterator *iterator); /* zero list of prefetched and unread blocknos */
+extern void tbm_add(TBMIterator *iterator, BlockNumber blockno); /* add this blockno to list of prefetched and unread blocknos */
+extern void tbm_subtract(TBMIterator *iterator, BlockNumber blockno); /* remove this blockno from list of prefetched and unread blocknos */
+extern TBMIterateResult *tbm_locate_IterateResult(TBMIterator *iterator); /* locate the TBMIterateResult of an iterator */
 #endif   /* TIDBITMAP_H */
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 5ff9e41..01cfa81 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -1,4 +1,4 @@
-/* src/include/pg_config.h.in.  Generated from configure.in by autoheader.  */
+/* src/include/pg_config.h.in.  Generated from - by autoheader.  */
 
 /* Define to the type of arg 1 of 'accept' */
 #undef ACCEPT_TYPE_ARG1
@@ -748,6 +748,10 @@
 /* Define to the appropriate snprintf format for unsigned 64-bit ints. */
 #undef UINT64_FORMAT
 
+/* Define to select librt-style async io and the gcc atomic compare_and_swap.
+   */
+#undef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+
 /* Define to 1 to build with assertion checks. (--enable-cassert) */
 #undef USE_ASSERT_CHECKING
 
diff --git a/src/include/pg_config_manual.h b/src/include/pg_config_manual.h
index d1f99fb..6040a5d 100644
--- a/src/include/pg_config_manual.h
+++ b/src/include/pg_config_manual.h
@@ -138,9 +138,11 @@
 /*
  * USE_PREFETCH code should be compiled only if we have a way to implement
  * prefetching.  (This is decoupled from USE_POSIX_FADVISE because there
- * might in future be support for alternative low-level prefetch APIs.)
+ * might in future be support for alternative low-level prefetch APIs  --
+ * -- update October 2013  -- now there is such a new prefetch capability --
+ *   async_io into postgres buffers  -   configuration parameter max_async_io_threads)
  */
-#ifdef USE_POSIX_FADVISE
+#if defined(USE_POSIX_FADVISE) || defined(USE_AIO_ATOMIC_BUILTIN_COMP_SWAP)
 #define USE_PREFETCH
 #endif
 
diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h
index c019013..1482c5a 100644
--- a/src/include/storage/buf_internals.h
+++ b/src/include/storage/buf_internals.h
@@ -22,7 +22,9 @@
 #include "storage/smgr.h"
 #include "storage/spin.h"
 #include "utils/relcache.h"
-
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+#include "aio.h"
+#endif /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
 
 /*
  * Flags for buffer descriptors
@@ -38,8 +40,23 @@
 #define BM_JUST_DIRTIED			(1 << 5)		/* dirtied since write started */
 #define BM_PIN_COUNT_WAITER		(1 << 6)		/* have waiter for sole pin */
 #define BM_CHECKPOINT_NEEDED	(1 << 7)		/* must write for checkpoint */
-#define BM_PERMANENT			(1 << 8)		/* permanent relation (not
-												 * unlogged) */
+#define BM_PERMANENT			(1 << 8)	/* permanent relation (not unlogged) */
+#define BM_AIO_IN_PROGRESS		(1 << 9)	/* aio in progress    */
+#define BM_AIO_PREFETCH_PIN_BANKED	(1 << 10)	/* pinned when prefetch issued
+                                                        ** and this pin is banked - i.e.
+                                                        ** redeemable by the next use by same task
+                                                        ** note that for any one buffer, a pin can be banked
+                                                        **      by at most one process globally,
+                                                        **      that is,   only one process may bank a pin on the buffer
+                                                        **                 and it may do so only once (may not be stacked)
+                                                        */
+
+/*********
+for asynchronous aio-read prefetching, two golden rules concerning buffer pinning and buffer-header flags must be observed:
+  R1.  a buffer marked as BM_AIO_IN_PROGRESS must be pinned by at least one backend
+  R2.  a buffer marked as BM_AIO_PREFETCH_PIN_BANKED must be pinned by the backend identified by
+               (buf->flags & BM_AIO_IN_PROGRESS) ? ( ((BAiocbAnchr->BufferAiocbs)+(FREENEXT_BAIOCB_ORIGIN - buf->freeNext))->pidOfAio : (-(buf->freeNext))
+*********/
 
 typedef bits16 BufFlags;
 
@@ -140,17 +157,83 @@ typedef struct sbufdesc
 	BufFlags	flags;			/* see bit definitions above */
 	uint16		usage_count;	/* usage counter for clock sweep code */
 	unsigned	refcount;		/* # of backends holding pins on buffer */
-	int			wait_backend_pid;		/* backend PID of pin-count waiter */
+	int		wait_backend_pid;	/*  if     flags & BM_PIN_COUNT_WAITER
+                                                **  then   backend PID of pin-count waiter
+                                                **  else   not set
+                                                */
 
 	slock_t		buf_hdr_lock;	/* protects the above fields */
 
 	int			buf_id;			/* buffer's index number (from 0) */
-	int			freeNext;		/* link in freelist chain */
+        int    	volatile	freeNext;	/* overloaded and much-abused field :
+                                                ** EITHER
+                                                **     if     >= 0
+                                                **     then   link in freelist chain
+                                                **  OR
+                                                **     if     <  0
+                                                **     then    EITHER
+                                                **             if     flags & BM_AIO_IN_PROGRESS
+                                                **             then   negative of (the index of the aiocb in the BufferAiocbs array + 3)
+                                                **             else   if flags & BM_AIO_PREFETCH_PIN_BANKED
+                                                **             then   -(pid of task that issued aio_read and pinned buffer)
+                                                **             else   one of the special values -1 or -2 listed below
+                                                */
 
 	LWLock	   *io_in_progress_lock;	/* to wait for I/O to complete */
 	LWLock	   *content_lock;	/* to lock access to buffer contents */
 } BufferDesc;
 
+/*  structures for control blocks for our implementation of async io */
+
+/*  if USE_AIO_ATOMIC_BUILTIN_COMP_SWAP is not defined,  the following struct is not put into use at runtime
+**  but it is easier to let the compiler find the definition but hide the reference to aiocb
+**  which is the only type it would not understand
+*/
+
+struct BufferAiocb {
+       struct BufferAiocb volatile * volatile BAiocbnext;  /*    next free entry or value of BAIOCB_OCCUPIED means in use  */
+       struct sbufdesc    volatile * volatile BAiocbbufh;  /*    there can be at most one BufferDesc marked BM_AIO_IN_PROGRESS
+                                                           **    and using this BufferAiocb -
+                                                           **    if there is one, BAiocbbufh points to it, else BAiocbbufh is zero
+                                                           **    NOTE  BAiocbbufh should be zero for every BufferAiocb on the free list
+                                                           */
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+       struct aiocb       volatile            BAiocbthis;  /*    the aio library's control block for one async io */
+#endif /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
+       int                volatile  BAiocbDependentCount;  /*    count of tasks who depend on this BufferAiocb
+                                                           **    in the sense that they are waiting for io completion.
+                                                           **    only a Dependent may move the BufferAiocb onto the freelist
+                                                           **    and only when that Dependent is the *only* Dependent (count == 1)
+                                                           **    BAiocbDependentCount is protected by bufferheader spinlock
+                                                           **    and must be updated only when that spinlock is held
+                                                           */
+       pid_t              volatile  pidOfAio;              /*    pid of backend who issued an aio_read using this BAiocb -
+                                                           **    this backend must have pinned the associated buffer.
+                                                           */
+};
+
+#define BAIOCB_OCCUPIED 0x75f1        /*  distinct indicator of a BufferAiocb.BAiocbnext that is NOT on free list */
+#define BAIOCB_FREE 0x7b9d            /*  distinct indicator of a BufferAiocb.BAiocbbufh that IS     on free list */
+
+struct BAiocbAnchor {                 /*  anchor for all control blocks pertaining to aio  */
+       volatile struct BufferAiocb* BufferAiocbs;          /*  aiocbs ...                   */
+       volatile struct BufferAiocb* volatile FreeBAiocbs; /* ... and their free list   */
+};
+
+/*   values for BufCheckAsync input and retcode */
+#define BUF_INTENTION_WANT 		 1  /* wants the buffer, wait for in-progress aio and then pin */
+#define BUF_INTENTION_REJECT_KEEP_PIN 	-1  /* pin already held, do not unpin */
+#define BUF_INTENTION_REJECT_OBTAIN_PIN	-2  /* obtain pin,  caller wants it for same buffer */
+#define BUF_INTENTION_REJECT_FORGET	-3  /* unpin and tell resource owner to forget */
+#define BUF_INTENTION_REJECT_NOADJUST	-4  /* unpin and call ResourceOwnerForgetBuffer */
+#define BUF_INTENTION_REJECT_UNBANK   	-5  /* unpin only if pin banked by caller */
+
+#define BUF_INTENT_RC_CHANGED_TAG	-5
+#define BUF_INTENT_RC_BADPAGE		-4
+#define BUF_INTENT_RC_INVALID_AIO	-3    /*  invalid and aio was in progress */
+#define BUF_INTENT_RC_INVALID_NO_AIO	-1    /*  invalid and no aio was in progress */
+#define BUF_INTENT_RC_VALID		 1
+
 #define BufferDescriptorGetBuffer(bdesc) ((bdesc)->buf_id + 1)
 
 /*
@@ -159,6 +242,7 @@ typedef struct sbufdesc
  */
 #define FREENEXT_END_OF_LIST	(-1)
 #define FREENEXT_NOT_IN_LIST	(-2)
+#define FREENEXT_BAIOCB_ORIGIN	(-3)
 
 /*
  * Macros for acquiring/releasing a shared buffer header's spinlock.
diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h
index 89447d0..c652841 100644
--- a/src/include/storage/bufmgr.h
+++ b/src/include/storage/bufmgr.h
@@ -41,6 +41,7 @@ typedef enum
 	RBM_ZERO_ON_ERROR,			/* Read, but return an all-zeros page on error */
 	RBM_NORMAL_NO_LOG			/* Don't log page as invalid during WAL
 								 * replay; otherwise same as RBM_NORMAL */
+       ,RBM_NOREAD_FOR_PREFETCH   /* Don't read from disk, don't zero buffer, find buffer only */
 } ReadBufferMode;
 
 /* in globals.c ... this duplicates miscadmin.h */
@@ -57,6 +58,9 @@ extern int	target_prefetch_pages;
 extern PGDLLIMPORT char *BufferBlocks;
 extern PGDLLIMPORT int32 *PrivateRefCount;
 
+/*  in buf_async.c  */;
+extern int      max_async_io_prefetchers; /* Maximum number of backends using asynchronous librt threads to read pages into our buffers */
+
 /* in localbuf.c */
 extern PGDLLIMPORT int NLocBuffer;
 extern PGDLLIMPORT Block *LocalBufferBlockPointers;
@@ -159,9 +163,15 @@ extern PGDLLIMPORT int32 *LocalRefCount;
 #define BufferGetPage(buffer) ((Page)BufferGetBlock(buffer))
 
 /*
- * prototypes for functions in bufmgr.c
+ * prototypes for external functions in bufmgr.c and buf_async.c
  */
-extern void PrefetchBuffer(Relation reln, ForkNumber forkNum,
+extern int PrefetchBuffer(Relation reln, ForkNumber forkNum,
+			   BlockNumber blockNum , BufferAccessStrategy strategy);
+/*   return code  is an int bitmask : */
+#define PREFTCHRC_BUF_PIN_INCREASED 0x01    /*  pin count on buffer has been increased by 1 */
+#define PREFTCHRC_BLK_ALREADY_PRESENT 0x02  /*  block was already present in a buffer       */
+
+extern void DiscardBuffer(Relation reln, ForkNumber forkNum,
 			   BlockNumber blockNum);
 extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
 extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
diff --git a/src/include/storage/fd.h b/src/include/storage/fd.h
index a6df8fb..d394c6a 100644
--- a/src/include/storage/fd.h
+++ b/src/include/storage/fd.h
@@ -69,6 +69,11 @@ extern File PathNameOpenFile(FileName fileName, int fileFlags, int fileMode);
 extern File OpenTemporaryFile(bool interXact);
 extern void FileClose(File file);
 extern int	FilePrefetch(File file, off_t offset, int amount);
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+extern void FileInitaio(int max_aio_threads, int max_aio_num );
+extern int  FileStartaio(File file, off_t offset, int amount , char *aiocbp);
+extern int  FileCompleteaio( char *aiocbp , int normal_wait );
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
 extern int	FileRead(File file, char *buffer, int amount);
 extern int	FileWrite(File file, char *buffer, int amount);
 extern int	FileSync(File file);
diff --git a/src/include/storage/smgr.h b/src/include/storage/smgr.h
index ba7c909..5e8d645 100644
--- a/src/include/storage/smgr.h
+++ b/src/include/storage/smgr.h
@@ -92,6 +92,12 @@ extern void smgrextend(SMgrRelation reln, ForkNumber forknum,
 		   BlockNumber blocknum, char *buffer, bool skipFsync);
 extern void smgrprefetch(SMgrRelation reln, ForkNumber forknum,
 			 BlockNumber blocknum);
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+extern void smgrinitaio(int max_aio_threads, int max_aio_num);
+extern void smgrstartaio(SMgrRelation reln, ForkNumber forknum,
+			 BlockNumber blocknum , char *aiocbp , int *retcode);
+extern void smgrcompleteaio( SMgrRelation reln, char *aiocbp , int *inoutcode );
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
 extern void smgrread(SMgrRelation reln, ForkNumber forknum,
 		 BlockNumber blocknum, char *buffer);
 extern void smgrwrite(SMgrRelation reln, ForkNumber forknum,
@@ -118,6 +124,11 @@ extern void mdextend(SMgrRelation reln, ForkNumber forknum,
 		 BlockNumber blocknum, char *buffer, bool skipFsync);
 extern void mdprefetch(SMgrRelation reln, ForkNumber forknum,
 		   BlockNumber blocknum);
+#ifdef USE_AIO_ATOMIC_BUILTIN_COMP_SWAP
+extern void mdinitaio(int max_aio_threads, int max_aio_num);
+extern void mdstartaio(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum , char *aiocbp , int *retcode );
+extern void mdcompleteaio( char *aiocbp , int *inoutcode );
+#endif  /*  USE_AIO_ATOMIC_BUILTIN_COMP_SWAP */
 extern void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
 	   char *buffer);
 extern void mdwrite(SMgrRelation reln, ForkNumber forknum,
diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h
index af4f53f..720d3f3 100644
--- a/src/include/utils/rel.h
+++ b/src/include/utils/rel.h
@@ -61,6 +61,7 @@ typedef struct RelationAmInfo
 	FmgrInfo	ammarkpos;
 	FmgrInfo	amrestrpos;
 	FmgrInfo	amcanreturn;
+	FmgrInfo	ampeeknexttuple;	/* peek at the next tuple different from any blocknum in pfch_list without reading a new index page */
 } RelationAmInfo;
 
 
