From 4bb12ad1e73927ee31683d728d5cdaebca6a53a6 Mon Sep 17 00:00:00 2001
From: Hari Babu <haribabuk@fast.au.fujitsu.com>
Date: Mon, 23 Jul 2018 14:44:35 +1000
Subject: [PATCH 1/2] Merge tableam.h and tableamapi.h

And also most tableam.c functions small inline functions.
Having one-line tableam.c wrappers makes this more expensive
than necessary.

The above change may added some internal headers also exposed
via tableam.h, may need another check.
---
 contrib/pgrowlocks/pgrowlocks.c          |   2 +-
 src/backend/access/heap/heapam.c         |   2 +-
 src/backend/access/heap/heapam_handler.c |   2 +-
 src/backend/access/heap/rewriteheap.c    |   1 +
 src/backend/access/nbtree/nbtsort.c      |   1 +
 src/backend/access/table/Makefile        |   2 +-
 src/backend/access/table/tableam.c       | 472 ---------------
 src/backend/access/table/tableamapi.c    |   2 +-
 src/backend/commands/cluster.c           |   2 +-
 src/backend/executor/execIndexing.c      |   1 +
 src/backend/executor/nodeSamplescan.c    |   1 +
 src/backend/optimizer/util/plancat.c     |   2 +-
 src/backend/postmaster/autovacuum.c      |   1 +
 src/backend/storage/lmgr/predicate.c     |   1 +
 src/backend/utils/adt/ri_triggers.c      |   1 +
 src/backend/utils/adt/selfuncs.c         |   1 +
 src/backend/utils/cache/relcache.c       |   2 +-
 src/include/access/relscan.h             |   1 -
 src/include/access/tableam.h             | 738 ++++++++++++++++++++---
 src/include/access/tableamapi.h          | 212 -------
 src/include/nodes/nodes.h                |   2 +-
 src/include/utils/tqual.h                |  15 -
 22 files changed, 669 insertions(+), 795 deletions(-)
 delete mode 100644 src/backend/access/table/tableam.c
 delete mode 100644 src/include/access/tableamapi.h

diff --git a/contrib/pgrowlocks/pgrowlocks.c b/contrib/pgrowlocks/pgrowlocks.c
index 3995a88397..959f5e7dc8 100644
--- a/contrib/pgrowlocks/pgrowlocks.c
+++ b/contrib/pgrowlocks/pgrowlocks.c
@@ -26,7 +26,7 @@
 
 #include "access/multixact.h"
 #include "access/relscan.h"
-#include "access/tableamapi.h"
+#include "access/tableam.h"
 #include "access/xact.h"
 #include "catalog/namespace.h"
 #include "catalog/pg_authid.h"
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 5b8155c911..40c1a5432d 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -45,7 +45,7 @@
 #include "access/multixact.h"
 #include "access/parallel.h"
 #include "access/relscan.h"
-#include "access/tableamapi.h"
+#include "access/tableam.h"
 #include "access/sysattr.h"
 #include "access/transam.h"
 #include "access/tuptoaster.h"
diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 94c64d8387..42eec2a2ab 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -23,7 +23,7 @@
 #include "access/heapam.h"
 #include "access/relscan.h"
 #include "access/rewriteheap.h"
-#include "access/tableamapi.h"
+#include "access/tableam.h"
 #include "catalog/pg_am_d.h"
 #include "pgstat.h"
 #include "storage/lmgr.h"
diff --git a/src/backend/access/heap/rewriteheap.c b/src/backend/access/heap/rewriteheap.c
index 2ddb421eb0..5dad191ab2 100644
--- a/src/backend/access/heap/rewriteheap.c
+++ b/src/backend/access/heap/rewriteheap.c
@@ -110,6 +110,7 @@
 #include "access/heapam.h"
 #include "access/heapam_xlog.h"
 #include "access/rewriteheap.h"
+#include "access/tableam.h"
 #include "access/transam.h"
 #include "access/tuptoaster.h"
 #include "access/xact.h"
diff --git a/src/backend/access/nbtree/nbtsort.c b/src/backend/access/nbtree/nbtsort.c
index 74f8e1bbeb..be74041df4 100644
--- a/src/backend/access/nbtree/nbtsort.c
+++ b/src/backend/access/nbtree/nbtsort.c
@@ -60,6 +60,7 @@
 #include "access/nbtree.h"
 #include "access/parallel.h"
 #include "access/relscan.h"
+#include "access/tableam.h"
 #include "access/xact.h"
 #include "access/xlog.h"
 #include "access/xloginsert.h"
diff --git a/src/backend/access/table/Makefile b/src/backend/access/table/Makefile
index fe22bf9208..ff0989ed24 100644
--- a/src/backend/access/table/Makefile
+++ b/src/backend/access/table/Makefile
@@ -12,6 +12,6 @@ subdir = src/backend/access/table
 top_builddir = ../../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS = tableam.o tableamapi.o tableam_common.o
+OBJS = tableamapi.o tableam_common.o
 
 include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/access/table/tableam.c b/src/backend/access/table/tableam.c
deleted file mode 100644
index 77c04aaa27..0000000000
--- a/src/backend/access/table/tableam.c
+++ /dev/null
@@ -1,472 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * tableam.c
- *	  table access method code
- *
- * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- *
- * IDENTIFICATION
- *	  src/backend/access/table/tableam.c
- *
- *-------------------------------------------------------------------------
- */
-#include "postgres.h"
-
-#include "access/tableam.h"
-#include "access/tableamapi.h"
-#include "access/relscan.h"
-#include "storage/bufmgr.h"
-#include "utils/rel.h"
-#include "utils/tqual.h"
-
-TupleTableSlot*
-table_gimmegimmeslot(Relation relation, List **reglist)
-{
-	TupleTableSlot *slot;
-
-	slot = relation->rd_tableamroutine->gimmegimmeslot(relation);
-
-	if (reglist)
-		*reglist = lappend(*reglist, slot);
-
-	return slot;
-}
-
-/*
- *	table_fetch_row_version		- retrieve tuple with given tid
- *
- *  XXX: This shouldn't just take a tid, but tid + additional information
- */
-bool
-table_fetch_row_version(Relation r,
-						ItemPointer tid,
-						Snapshot snapshot,
-						TupleTableSlot *slot,
-						Relation stats_relation)
-{
-	return r->rd_tableamroutine->tuple_fetch_row_version(r, tid,
-														 snapshot, slot,
-														 stats_relation);
-}
-
-
-/*
- *	table_lock_tuple - lock a tuple in shared or exclusive mode
- *
- *  XXX: This shouldn't just take a tid, but tid + additional information
- */
-HTSU_Result
-table_lock_tuple(Relation relation, ItemPointer tid, Snapshot snapshot,
-				 TupleTableSlot *slot, CommandId cid, LockTupleMode mode,
-				 LockWaitPolicy wait_policy, uint8 flags,
-				 HeapUpdateFailureData *hufd)
-{
-	return relation->rd_tableamroutine->tuple_lock(relation, tid, snapshot, slot,
-												cid, mode, wait_policy,
-												flags, hufd);
-}
-
-/* ----------------
- *		heap_beginscan_parallel - join a parallel scan
- *
- *		Caller must hold a suitable lock on the correct relation.
- * ----------------
- */
-TableScanDesc
-table_beginscan_parallel(Relation relation, ParallelHeapScanDesc parallel_scan)
-{
-	Snapshot	snapshot;
-
-	Assert(RelationGetRelid(relation) == parallel_scan->phs_relid);
-
-	if (!parallel_scan->phs_snapshot_any)
-	{
-		/* Snapshot was serialized -- restore it */
-		snapshot = RestoreSnapshot(parallel_scan->phs_snapshot_data);
-		RegisterSnapshot(snapshot);
-	}
-	else
-	{
-		/* SnapshotAny passed by caller (not serialized) */
-		snapshot = SnapshotAny;
-	}
-
-	return relation->rd_tableamroutine->scan_begin(relation, snapshot, 0, NULL, parallel_scan,
-												true, true, true, false, false, !parallel_scan->phs_snapshot_any);
-}
-
-ParallelHeapScanDesc
-tableam_get_parallelheapscandesc(TableScanDesc sscan)
-{
-	return sscan->rs_rd->rd_tableamroutine->scan_get_parallelheapscandesc(sscan);
-}
-
-HeapPageScanDesc
-tableam_get_heappagescandesc(TableScanDesc sscan)
-{
-	/*
-	 * Planner should have already validated whether the current storage
-	 * supports Page scans are not? This function will be called only from
-	 * Bitmap Heap scan and sample scan
-	 */
-	Assert(sscan->rs_rd->rd_tableamroutine->scan_get_heappagescandesc != NULL);
-
-	return sscan->rs_rd->rd_tableamroutine->scan_get_heappagescandesc(sscan);
-}
-
-void
-table_syncscan_report_location(Relation rel, BlockNumber location)
-{
-	return rel->rd_tableamroutine->sync_scan_report_location(rel, location);
-}
-
-/*
- * heap_setscanlimits - restrict range of a heapscan
- *
- * startBlk is the page to start at
- * numBlks is number of pages to scan (InvalidBlockNumber means "all")
- */
-void
-table_setscanlimits(TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks)
-{
-	sscan->rs_rd->rd_tableamroutine->scansetlimits(sscan, startBlk, numBlks);
-}
-
-
-/* ----------------
- *		heap_beginscan	- begin relation scan
- *
- * heap_beginscan is the "standard" case.
- *
- * heap_beginscan_catalog differs in setting up its own temporary snapshot.
- *
- * heap_beginscan_strat offers an extended API that lets the caller control
- * whether a nondefault buffer access strategy can be used, and whether
- * syncscan can be chosen (possibly resulting in the scan not starting from
- * block zero).  Both of these default to true with plain heap_beginscan.
- *
- * heap_beginscan_bm is an alternative entry point for setting up a
- * TableScanDesc for a bitmap heap scan.  Although that scan technology is
- * really quite unlike a standard seqscan, there is just enough commonality
- * to make it worth using the same data structure.
- *
- * heap_beginscan_sampling is an alternative entry point for setting up a
- * TableScanDesc for a TABLESAMPLE scan.  As with bitmap scans, it's worth
- * using the same data structure although the behavior is rather different.
- * In addition to the options offered by heap_beginscan_strat, this call
- * also allows control of whether page-mode visibility checking is used.
- * ----------------
- */
-TableScanDesc
-table_beginscan(Relation relation, Snapshot snapshot,
-				  int nkeys, ScanKey key)
-{
-	return relation->rd_tableamroutine->scan_begin(relation, snapshot, nkeys, key, NULL,
-												true, true, true, false, false, false);
-}
-
-TableScanDesc
-table_beginscan_catalog(Relation relation, int nkeys, ScanKey key)
-{
-	Oid			relid = RelationGetRelid(relation);
-	Snapshot	snapshot = RegisterSnapshot(GetCatalogSnapshot(relid));
-
-	return relation->rd_tableamroutine->scan_begin(relation, snapshot, nkeys, key, NULL,
-												true, true, true, false, false, true);
-}
-
-TableScanDesc
-table_beginscan_strat(Relation relation, Snapshot snapshot,
-						int nkeys, ScanKey key,
-						bool allow_strat, bool allow_sync)
-{
-	return relation->rd_tableamroutine->scan_begin(relation, snapshot, nkeys, key, NULL,
-												allow_strat, allow_sync, true,
-												false, false, false);
-}
-
-TableScanDesc
-table_beginscan_bm(Relation relation, Snapshot snapshot,
-					 int nkeys, ScanKey key)
-{
-	return relation->rd_tableamroutine->scan_begin(relation, snapshot, nkeys, key, NULL,
-												false, false, true, true, false, false);
-}
-
-TableScanDesc
-table_beginscan_sampling(Relation relation, Snapshot snapshot,
-						   int nkeys, ScanKey key,
-						   bool allow_strat, bool allow_sync, bool allow_pagemode)
-{
-	return relation->rd_tableamroutine->scan_begin(relation, snapshot, nkeys, key, NULL,
-												allow_strat, allow_sync, allow_pagemode,
-												false, true, false);
-}
-
-/* ----------------
- *		heap_rescan		- restart a relation scan
- * ----------------
- */
-void
-table_rescan(TableScanDesc scan,
-			   ScanKey key)
-{
-	scan->rs_rd->rd_tableamroutine->scan_rescan(scan, key, false, false, false, false);
-}
-
-/* ----------------
- *		heap_rescan_set_params	- restart a relation scan after changing params
- *
- * This call allows changing the buffer strategy, syncscan, and pagemode
- * options before starting a fresh scan.  Note that although the actual use
- * of syncscan might change (effectively, enabling or disabling reporting),
- * the previously selected startblock will be kept.
- * ----------------
- */
-void
-table_rescan_set_params(TableScanDesc scan, ScanKey key,
-						  bool allow_strat, bool allow_sync, bool allow_pagemode)
-{
-	scan->rs_rd->rd_tableamroutine->scan_rescan(scan, key, true,
-											 allow_strat, allow_sync, (allow_pagemode && IsMVCCSnapshot(scan->rs_snapshot)));
-}
-
-/* ----------------
- *		heap_endscan	- end relation scan
- *
- *		See how to integrate with index scans.
- *		Check handling if reldesc caching.
- * ----------------
- */
-void
-table_endscan(TableScanDesc scan)
-{
-	scan->rs_rd->rd_tableamroutine->scan_end(scan);
-}
-
-
-/* ----------------
- *		heap_update_snapshot
- *
- *		Update snapshot info in heap scan descriptor.
- * ----------------
- */
-void
-table_scan_update_snapshot(TableScanDesc scan, Snapshot snapshot)
-{
-	scan->rs_rd->rd_tableamroutine->scan_update_snapshot(scan, snapshot);
-}
-
-TupleTableSlot *
-table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
-{
-	return sscan->rs_rd->rd_tableamroutine->scan_getnextslot(sscan, direction, slot);
-}
-
-bool
-table_tuple_fetch_from_offset(TableScanDesc sscan, BlockNumber blkno, OffsetNumber offset, TupleTableSlot *slot)
-{
-	return sscan->rs_rd->rd_tableamroutine->scan_fetch_tuple_from_offset(sscan, blkno, offset, slot);
-}
-
-
-IndexFetchTableData*
-table_begin_index_fetch_table(Relation rel)
-{
-	return rel->rd_tableamroutine->begin_index_fetch(rel);
-}
-
-void
-table_reset_index_fetch_table(IndexFetchTableData* scan)
-{
-	scan->rel->rd_tableamroutine->reset_index_fetch(scan);
-}
-
-void
-table_end_index_fetch_table(IndexFetchTableData* scan)
-{
-	scan->rel->rd_tableamroutine->end_index_fetch(scan);
-}
-
-/*
- * Insert a tuple from a slot into table AM routine
- */
-Oid
-table_insert(Relation relation, TupleTableSlot *slot, CommandId cid,
-			   int options, BulkInsertState bistate)
-{
-	return relation->rd_tableamroutine->tuple_insert(relation, slot, cid, options,
-													 bistate);
-}
-
-Oid
-table_insert_speculative(Relation relation, TupleTableSlot *slot, CommandId cid,
-						 int options, BulkInsertState bistate, uint32 specToken)
-{
-	return relation->rd_tableamroutine->tuple_insert_speculative(relation, slot, cid, options,
-																 bistate, specToken);
-}
-
-void table_complete_speculative(Relation relation, TupleTableSlot *slot, uint32 specToken,
-								bool succeeded)
-{
-	return relation->rd_tableamroutine->tuple_complete_speculative(relation, slot, specToken, succeeded);
-}
-
-/*
- * Delete a tuple from tid using table AM routine
- */
-HTSU_Result
-table_delete(Relation relation, ItemPointer tid, CommandId cid,
-			 Snapshot crosscheck, bool wait,
-			 HeapUpdateFailureData *hufd, bool changingPart)
-{
-	return relation->rd_tableamroutine->tuple_delete(relation, tid, cid,
-												  crosscheck, wait, hufd, changingPart);
-}
-
-/*
- * update a tuple from tid using table AM routine
- */
-HTSU_Result
-table_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
-			 CommandId cid, Snapshot crosscheck, bool wait,
-			 HeapUpdateFailureData *hufd, LockTupleMode *lockmode,
-			 bool *update_indexes)
-{
-	return relation->rd_tableamroutine->tuple_update(relation, otid, slot,
-												  cid, crosscheck, wait, hufd,
-												  lockmode, update_indexes);
-}
-
-bool
-table_fetch_follow(struct IndexFetchTableData *scan,
-				   ItemPointer tid,
-				   Snapshot snapshot,
-				   TupleTableSlot *slot,
-				   bool *call_again, bool *all_dead)
-{
-
-	return scan->rel->rd_tableamroutine->tuple_fetch_follow(scan, tid, snapshot,
-														   slot, call_again,
-														   all_dead);
-}
-
-bool
-table_fetch_follow_check(Relation rel,
-						 ItemPointer tid,
-						 Snapshot snapshot,
-						 bool *all_dead)
-{
-	IndexFetchTableData *scan = table_begin_index_fetch_table(rel);
-	TupleTableSlot *slot = table_gimmegimmeslot(rel, NULL);
-	bool call_again = false;
-	bool found;
-
-	found = table_fetch_follow(scan, tid, snapshot, slot, &call_again, all_dead);
-
-	table_end_index_fetch_table(scan);
-	ExecDropSingleTupleTableSlot(slot);
-
-	return found;
-}
-
-/*
- *	table_multi_insert	- insert multiple tuple into a table
- */
-void
-table_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
-					 CommandId cid, int options, BulkInsertState bistate)
-{
-	relation->rd_tableamroutine->multi_insert(relation, tuples, ntuples,
-										   cid, options, bistate);
-}
-
-tuple_data
-table_tuple_get_data(Relation relation, TupleTableSlot *slot, tuple_data_flags flags)
-{
-	return relation->rd_tableamroutine->get_tuple_data(slot, flags);
-}
-
-void
-table_get_latest_tid(Relation relation,
-					   Snapshot snapshot,
-					   ItemPointer tid)
-{
-	relation->rd_tableamroutine->tuple_get_latest_tid(relation, snapshot, tid);
-}
-
-
-void
-table_vacuum_rel(Relation rel, int options,
-			 struct VacuumParams *params, BufferAccessStrategy bstrategy)
-{
-	rel->rd_tableamroutine->relation_vacuum(rel, options, params, bstrategy);
-}
-
-/*
- *	table_sync		- sync a heap, for use when no WAL has been written
- */
-void
-table_sync(Relation rel)
-{
-	rel->rd_tableamroutine->relation_sync(rel);
-}
-
-/*
- * -------------------
- * storage Bulk Insert functions
- * -------------------
- */
-BulkInsertState
-table_getbulkinsertstate(Relation rel)
-{
-	return rel->rd_tableamroutine->getbulkinsertstate();
-}
-
-void
-table_freebulkinsertstate(Relation rel, BulkInsertState bistate)
-{
-	rel->rd_tableamroutine->freebulkinsertstate(bistate);
-}
-
-void
-table_releasebulkinsertstate(Relation rel, BulkInsertState bistate)
-{
-	rel->rd_tableamroutine->releasebulkinsertstate(bistate);
-}
-
-/*
- * -------------------
- * storage tuple rewrite functions
- * -------------------
- */
-RewriteState
-table_begin_rewrite(Relation OldHeap, Relation NewHeap,
-				   TransactionId OldestXmin, TransactionId FreezeXid,
-				   MultiXactId MultiXactCutoff, bool use_wal)
-{
-	return NewHeap->rd_tableamroutine->begin_heap_rewrite(OldHeap, NewHeap,
-			OldestXmin, FreezeXid, MultiXactCutoff, use_wal);
-}
-
-void
-table_end_rewrite(Relation rel, RewriteState state)
-{
-	rel->rd_tableamroutine->end_heap_rewrite(state);
-}
-
-void
-table_rewrite_tuple(Relation rel, RewriteState state, HeapTuple oldTuple,
-				   HeapTuple newTuple)
-{
-	rel->rd_tableamroutine->rewrite_heap_tuple(state, oldTuple, newTuple);
-}
-
-bool
-table_rewrite_dead_tuple(Relation rel, RewriteState state, HeapTuple oldTuple)
-{
-	return rel->rd_tableamroutine->rewrite_heap_dead_tuple(state, oldTuple);
-}
diff --git a/src/backend/access/table/tableamapi.c b/src/backend/access/table/tableamapi.c
index f94660e306..91e5774a6e 100644
--- a/src/backend/access/table/tableamapi.c
+++ b/src/backend/access/table/tableamapi.c
@@ -13,7 +13,7 @@
 #include "postgres.h"
 
 #include "access/htup_details.h"
-#include "access/tableamapi.h"
+#include "access/tableam.h"
 #include "catalog/pg_am.h"
 #include "catalog/pg_proc.h"
 #include "utils/syscache.h"
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index 14ed2aa393..34f815c28f 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -21,7 +21,7 @@
 #include "access/multixact.h"
 #include "access/relscan.h"
 #include "access/rewriteheap.h"
-#include "access/tableamapi.h"
+#include "access/tableam.h"
 #include "access/transam.h"
 #include "access/tuptoaster.h"
 #include "access/xact.h"
diff --git a/src/backend/executor/execIndexing.c b/src/backend/executor/execIndexing.c
index 542210b29f..80b604821b 100644
--- a/src/backend/executor/execIndexing.c
+++ b/src/backend/executor/execIndexing.c
@@ -107,6 +107,7 @@
 #include "postgres.h"
 
 #include "access/relscan.h"
+#include "access/tableam.h"
 #include "access/xact.h"
 #include "catalog/index.h"
 #include "executor/executor.h"
diff --git a/src/backend/executor/nodeSamplescan.c b/src/backend/executor/nodeSamplescan.c
index 566dabaa00..b5d02983c5 100644
--- a/src/backend/executor/nodeSamplescan.c
+++ b/src/backend/executor/nodeSamplescan.c
@@ -16,6 +16,7 @@
 
 #include "access/hash.h"
 #include "access/relscan.h"
+#include "access/tableam.h"
 #include "access/tsmapi.h"
 #include "executor/executor.h"
 #include "executor/nodeSamplescan.h"
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index f3cd64cf62..8fe8312f29 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -21,7 +21,7 @@
 #include "access/heapam.h"
 #include "access/htup_details.h"
 #include "access/nbtree.h"
-#include "access/tableamapi.h"
+#include "access/tableam.h"
 #include "access/sysattr.h"
 #include "access/transam.h"
 #include "access/xlog.h"
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 4455b42875..7142a54ce9 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -69,6 +69,7 @@
 #include "access/htup_details.h"
 #include "access/multixact.h"
 #include "access/reloptions.h"
+#include "access/tableam.h"
 #include "access/transam.h"
 #include "access/xact.h"
 #include "catalog/dependency.h"
diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c
index e8390311d0..2960e21340 100644
--- a/src/backend/storage/lmgr/predicate.c
+++ b/src/backend/storage/lmgr/predicate.c
@@ -188,6 +188,7 @@
 #include "access/htup_details.h"
 #include "access/slru.h"
 #include "access/subtrans.h"
+#include "access/tableam.h"
 #include "access/transam.h"
 #include "access/twophase.h"
 #include "access/twophase_rmgr.h"
diff --git a/src/backend/utils/adt/ri_triggers.c b/src/backend/utils/adt/ri_triggers.c
index a661f4b047..254041cea7 100644
--- a/src/backend/utils/adt/ri_triggers.c
+++ b/src/backend/utils/adt/ri_triggers.c
@@ -31,6 +31,7 @@
 #include "postgres.h"
 
 #include "access/htup_details.h"
+#include "access/tableam.h"
 #include "access/sysattr.h"
 #include "access/xact.h"
 #include "catalog/pg_collation.h"
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 203b83ad06..7fcf077426 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -103,6 +103,7 @@
 #include "access/brin.h"
 #include "access/gin.h"
 #include "access/htup_details.h"
+#include "access/tableam.h"
 #include "access/sysattr.h"
 #include "catalog/index.h"
 #include "catalog/pg_am.h"
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 6360371493..ece332bd44 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -36,7 +36,7 @@
 #include "access/nbtree.h"
 #include "access/reloptions.h"
 #include "access/sysattr.h"
-#include "access/tableamapi.h"
+#include "access/tableam.h"
 #include "access/tupdesc_details.h"
 #include "access/xact.h"
 #include "access/xlog.h"
diff --git a/src/include/access/relscan.h b/src/include/access/relscan.h
index e9d8eed541..97208d4c44 100644
--- a/src/include/access/relscan.h
+++ b/src/include/access/relscan.h
@@ -16,7 +16,6 @@
 
 #include "access/genam.h"
 #include "access/heapam.h"
-#include "access/tableam.h"
 #include "access/htup_details.h"
 #include "access/itup.h"
 #include "access/tupdesc.h"
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index fd05018ee8..bf675ff881 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -14,10 +14,18 @@
 #ifndef TABLEAM_H
 #define TABLEAM_H
 
+#include "postgres.h"
+
 #include "access/heapam.h"
+#include "access/relscan.h"
 #include "executor/tuptable.h"
 #include "nodes/execnodes.h"
+#include "nodes/nodes.h"
+#include "fmgr.h"
 #include "utils/rel.h"
+#include "utils/snapmgr.h"
+#include "utils/snapshot.h"
+#include "utils/tqual.h"
 
 #define DEFAULT_TABLE_ACCESS_METHOD	"heap_tableam"
 
@@ -37,103 +45,661 @@ typedef enum tuple_data_flags
 	CTID
 }			tuple_data_flags;
 
-extern TupleTableSlot* table_gimmegimmeslot(Relation relation, List **reglist);
-extern TableScanDesc table_beginscan_parallel(Relation relation, ParallelHeapScanDesc parallel_scan);
-extern ParallelHeapScanDesc tableam_get_parallelheapscandesc(TableScanDesc sscan);
-extern HeapPageScanDesc tableam_get_heappagescandesc(TableScanDesc sscan);
-extern void table_syncscan_report_location(Relation rel, BlockNumber location);
-extern void table_setscanlimits(TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks);
-extern TableScanDesc table_beginscan(Relation relation, Snapshot snapshot,
-				  int nkeys, ScanKey key);
-extern TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, ScanKey key);
-extern TableScanDesc table_beginscan_strat(Relation relation, Snapshot snapshot,
-						int nkeys, ScanKey key,
-						bool allow_strat, bool allow_sync);
-extern TableScanDesc table_beginscan_bm(Relation relation, Snapshot snapshot,
-					 int nkeys, ScanKey key);
-extern TableScanDesc table_beginscan_sampling(Relation relation, Snapshot snapshot,
-						   int nkeys, ScanKey key,
-						   bool allow_strat, bool allow_sync, bool allow_pagemode);
 
-extern struct IndexFetchTableData* table_begin_index_fetch_table(Relation rel);
-extern void table_reset_index_fetch_table(struct IndexFetchTableData* scan);
-extern void table_end_index_fetch_table(struct IndexFetchTableData* scan);
+/*
+ * Storage routine function hooks
+ */
+typedef bool (*SnapshotSatisfies_function) (TupleTableSlot *slot, Snapshot snapshot);
+typedef HTSU_Result (*SnapshotSatisfiesUpdate_function) (TupleTableSlot *slot, CommandId curcid);
+typedef HTSV_Result (*SnapshotSatisfiesVacuum_function) (TupleTableSlot *slot, TransactionId OldestXmin);
 
-extern void table_endscan(TableScanDesc scan);
-extern void table_rescan(TableScanDesc scan, ScanKey key);
-extern void table_rescan_set_params(TableScanDesc scan, ScanKey key,
-						  bool allow_strat, bool allow_sync, bool allow_pagemode);
-extern void table_scan_update_snapshot(TableScanDesc scan, Snapshot snapshot);
+typedef Oid (*TupleInsert_function) (Relation rel, TupleTableSlot *slot, CommandId cid,
+									 int options, BulkInsertState bistate);
 
-extern TupleTableSlot *table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot);
-extern bool table_tuple_fetch_from_offset(TableScanDesc sscan, BlockNumber blkno, OffsetNumber offset, TupleTableSlot *slot);
+typedef Oid (*TupleInsertSpeculative_function) (Relation rel,
+												 TupleTableSlot *slot,
+												 CommandId cid,
+												 int options,
+												 BulkInsertState bistate,
+												 uint32 specToken);
 
-extern void storage_get_latest_tid(Relation relation,
-					   Snapshot snapshot,
-					   ItemPointer tid);
-
-extern bool table_fetch_row_version(Relation relation,
-			  ItemPointer tid,
-			  Snapshot snapshot,
-			  TupleTableSlot *slot,
-			  Relation stats_relation);
-
-extern bool table_fetch_follow(struct IndexFetchTableData *scan,
-							   ItemPointer tid,
-							   Snapshot snapshot,
-							   TupleTableSlot *slot,
-							   bool *call_again, bool *all_dead);
-
-extern bool table_fetch_follow_check(Relation rel,
-									 ItemPointer tid,
-									 Snapshot snapshot,
-									 bool *all_dead);
-
-extern HTSU_Result table_lock_tuple(Relation relation, ItemPointer tid, Snapshot snapshot,
-				   TupleTableSlot *slot, CommandId cid, LockTupleMode mode,
-				   LockWaitPolicy wait_policy, uint8 flags,
-				   HeapUpdateFailureData *hufd);
-
-extern Oid table_insert(Relation relation, TupleTableSlot *slot, CommandId cid,
-			   int options, BulkInsertState bistate);
-extern Oid table_insert_speculative(Relation relation, TupleTableSlot *slot, CommandId cid,
-									int options, BulkInsertState bistate, uint32 specToken);
-extern void table_complete_speculative(Relation relation, TupleTableSlot *slot, uint32 specToken,
-									   bool succeeded);
-
-extern HTSU_Result table_delete(Relation relation, ItemPointer tid, CommandId cid,
-			   Snapshot crosscheck, bool wait, HeapUpdateFailureData *hufd,
-			   bool changingPart);
-
-extern HTSU_Result table_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
-			   CommandId cid, Snapshot crosscheck, bool wait,
-			   HeapUpdateFailureData *hufd, LockTupleMode *lockmode,
-			   bool *upddate_indexes);
-
-extern void table_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
-					 CommandId cid, int options, BulkInsertState bistate);
-
-extern tuple_data table_tuple_get_data(Relation relation, TupleTableSlot *slot, tuple_data_flags flags);
-
-extern void table_get_latest_tid(Relation relation,
-					   Snapshot snapshot,
-					   ItemPointer tid);
 
-extern void table_sync(Relation rel);
+typedef void (*TupleCompleteSpeculative_function) (Relation rel,
+												  TupleTableSlot *slot,
+												  uint32 specToken,
+												  bool succeeded);
+
+typedef HTSU_Result (*TupleDelete_function) (Relation relation,
+											 ItemPointer tid,
+											 CommandId cid,
+											 Snapshot crosscheck,
+											 bool wait,
+											 HeapUpdateFailureData *hufd,
+											 bool changingPart);
+
+typedef HTSU_Result (*TupleUpdate_function) (Relation relation,
+											 ItemPointer otid,
+											 TupleTableSlot *slot,
+											 CommandId cid,
+											 Snapshot crosscheck,
+											 bool wait,
+											 HeapUpdateFailureData *hufd,
+											 LockTupleMode *lockmode,
+											 bool *update_indexes);
+
+typedef bool (*TupleFetchRowVersion_function) (Relation relation,
+											   ItemPointer tid,
+											   Snapshot snapshot,
+											   TupleTableSlot *slot,
+											   Relation stats_relation);
+
+typedef HTSU_Result (*TupleLock_function) (Relation relation,
+										   ItemPointer tid,
+										   Snapshot snapshot,
+										   TupleTableSlot *slot,
+										   CommandId cid,
+										   LockTupleMode mode,
+										   LockWaitPolicy wait_policy,
+										   uint8 flags,
+										   HeapUpdateFailureData *hufd);
+
+typedef void (*MultiInsert_function) (Relation relation, HeapTuple *tuples, int ntuples,
+									  CommandId cid, int options, BulkInsertState bistate);
+
+typedef void (*TupleGetLatestTid_function) (Relation relation,
+											Snapshot snapshot,
+											ItemPointer tid);
+
+typedef tuple_data(*GetTupleData_function) (TupleTableSlot *slot, tuple_data_flags flags);
+
 struct VacuumParams;
-extern void table_vacuum_rel(Relation onerel, int options,
+typedef void (*RelationVacuum_function)(Relation onerel, int options,
 				struct VacuumParams *params, BufferAccessStrategy bstrategy);
 
-extern BulkInsertState table_getbulkinsertstate(Relation rel);
-extern void table_freebulkinsertstate(Relation rel, BulkInsertState bistate);
-extern void table_releasebulkinsertstate(Relation rel, BulkInsertState bistate);
+typedef void (*RelationSync_function) (Relation relation);
 
-extern RewriteState table_begin_rewrite(Relation OldHeap, Relation NewHeap,
+typedef BulkInsertState (*GetBulkInsertState_function) (void);
+typedef void (*FreeBulkInsertState_function) (BulkInsertState bistate);
+typedef void (*ReleaseBulkInsertState_function) (BulkInsertState bistate);
+
+typedef RewriteState (*BeginHeapRewrite_function) (Relation OldHeap, Relation NewHeap,
 				   TransactionId OldestXmin, TransactionId FreezeXid,
 				   MultiXactId MultiXactCutoff, bool use_wal);
-extern void table_end_rewrite(Relation rel, RewriteState state);
-extern void table_rewrite_tuple(Relation rel, RewriteState state, HeapTuple oldTuple,
+typedef void (*EndHeapRewrite_function) (RewriteState state);
+typedef void (*RewriteHeapTuple_function) (RewriteState state, HeapTuple oldTuple,
 				   HeapTuple newTuple);
-extern bool table_rewrite_dead_tuple(Relation rel, RewriteState state, HeapTuple oldTuple);
+typedef bool (*RewriteHeapDeadTuple_function) (RewriteState state, HeapTuple oldTuple);
+
+typedef TupleTableSlot* (*Slot_function) (Relation relation);
+
+typedef TableScanDesc (*ScanBegin_function) (Relation relation,
+											Snapshot snapshot,
+											int nkeys, ScanKey key,
+											ParallelHeapScanDesc parallel_scan,
+											bool allow_strat,
+											bool allow_sync,
+											bool allow_pagemode,
+											bool is_bitmapscan,
+											bool is_samplescan,
+											bool temp_snap);
+
+typedef struct IndexFetchTableData* (*BeginIndexFetchTable_function) (Relation relation);
+typedef void (*ResetIndexFetchTable_function) (struct IndexFetchTableData* data);
+typedef void (*EndIndexFetchTable_function) (struct IndexFetchTableData* data);
+
+typedef ParallelHeapScanDesc (*ScanGetParallelheapscandesc_function) (TableScanDesc scan);
+typedef HeapPageScanDesc(*ScanGetHeappagescandesc_function) (TableScanDesc scan);
+typedef void (*SyncScanReportLocation_function) (Relation rel, BlockNumber location);
+typedef void (*ScanSetlimits_function) (TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks);
+
+typedef TupleTableSlot *(*ScanGetnextSlot_function) (TableScanDesc scan,
+													 ScanDirection direction, TupleTableSlot *slot);
+
+typedef bool (*ScanFetchTupleFromOffset_function) (TableScanDesc scan,
+												   BlockNumber blkno, OffsetNumber offset, TupleTableSlot *slot);
+
+typedef void (*ScanEnd_function) (TableScanDesc scan);
+
+
+typedef void (*ScanGetpage_function) (TableScanDesc scan, BlockNumber page);
+typedef void (*ScanRescan_function) (TableScanDesc scan, ScanKey key, bool set_params,
+									 bool allow_strat, bool allow_sync, bool allow_pagemode);
+typedef void (*ScanUpdateSnapshot_function) (TableScanDesc scan, Snapshot snapshot);
+
+typedef bool (*TupleFetchFollow_function)(struct IndexFetchTableData *scan,
+										  ItemPointer tid,
+										  Snapshot snapshot,
+										  TupleTableSlot *slot,
+										  bool *call_again, bool *all_dead);
+
+/*
+ * API struct for a table AM.  Note this must be stored in a single palloc'd
+ * chunk of memory.
+ */
+typedef struct TableAmRoutine
+{
+	NodeTag		type;
+
+	Slot_function gimmegimmeslot;
+
+	SnapshotSatisfies_function snapshot_satisfies;
+	SnapshotSatisfiesUpdate_function snapshot_satisfiesUpdate;
+	SnapshotSatisfiesVacuum_function snapshot_satisfiesVacuum;
+
+	/* Operations on physical tuples */
+	TupleInsert_function tuple_insert;
+	TupleInsertSpeculative_function tuple_insert_speculative;
+	TupleCompleteSpeculative_function tuple_complete_speculative;
+	TupleUpdate_function tuple_update;
+	TupleDelete_function tuple_delete;
+	TupleFetchRowVersion_function tuple_fetch_row_version;
+	TupleLock_function tuple_lock;
+	MultiInsert_function multi_insert;
+	TupleGetLatestTid_function tuple_get_latest_tid;
+	TupleFetchFollow_function tuple_fetch_follow;
+
+	GetTupleData_function get_tuple_data;
+
+	RelationVacuum_function relation_vacuum;
+	RelationSync_function relation_sync;
+
+	GetBulkInsertState_function getbulkinsertstate;
+	FreeBulkInsertState_function freebulkinsertstate;
+	ReleaseBulkInsertState_function releasebulkinsertstate;
+
+	BeginHeapRewrite_function begin_heap_rewrite;
+	EndHeapRewrite_function end_heap_rewrite;
+	RewriteHeapTuple_function rewrite_heap_tuple;
+	RewriteHeapDeadTuple_function rewrite_heap_dead_tuple;
+
+	/* Operations on relation scans */
+	ScanBegin_function scan_begin;
+	ScanGetParallelheapscandesc_function scan_get_parallelheapscandesc;
+	ScanGetHeappagescandesc_function scan_get_heappagescandesc;
+	SyncScanReportLocation_function sync_scan_report_location;
+	ScanSetlimits_function scansetlimits;
+	ScanGetnextSlot_function scan_getnextslot;
+	ScanFetchTupleFromOffset_function scan_fetch_tuple_from_offset;
+	ScanEnd_function scan_end;
+	ScanGetpage_function scan_getpage;
+	ScanRescan_function scan_rescan;
+	ScanUpdateSnapshot_function scan_update_snapshot;
+
+	BeginIndexFetchTable_function begin_index_fetch;
+	EndIndexFetchTable_function reset_index_fetch;
+	EndIndexFetchTable_function end_index_fetch;
+
+}			TableAmRoutine;
+
+/*
+ * INLINE functions
+ */
+static inline TupleTableSlot*
+table_gimmegimmeslot(Relation relation, List **reglist)
+{
+	TupleTableSlot *slot;
+
+	slot = relation->rd_tableamroutine->gimmegimmeslot(relation);
+
+	if (reglist)
+		*reglist = lappend(*reglist, slot);
+
+	return slot;
+}
+
+/*
+ *	table_fetch_row_version		- retrieve tuple with given tid
+ *
+ *  XXX: This shouldn't just take a tid, but tid + additional information
+ */
+static inline bool
+table_fetch_row_version(Relation r,
+						ItemPointer tid,
+						Snapshot snapshot,
+						TupleTableSlot *slot,
+						Relation stats_relation)
+{
+	return r->rd_tableamroutine->tuple_fetch_row_version(r, tid,
+														 snapshot, slot,
+														 stats_relation);
+}
+
+
+/*
+ *	table_lock_tuple - lock a tuple in shared or exclusive mode
+ *
+ *  XXX: This shouldn't just take a tid, but tid + additional information
+ */
+static inline HTSU_Result
+table_lock_tuple(Relation relation, ItemPointer tid, Snapshot snapshot,
+				 TupleTableSlot *slot, CommandId cid, LockTupleMode mode,
+				 LockWaitPolicy wait_policy, uint8 flags,
+				 HeapUpdateFailureData *hufd)
+{
+	return relation->rd_tableamroutine->tuple_lock(relation, tid, snapshot, slot,
+												cid, mode, wait_policy,
+												flags, hufd);
+}
+
+/* ----------------
+ *		heap_beginscan_parallel - join a parallel scan
+ *
+ *		Caller must hold a suitable lock on the correct relation.
+ * ----------------
+ */
+static inline TableScanDesc
+table_beginscan_parallel(Relation relation, ParallelHeapScanDesc parallel_scan)
+{
+	Snapshot	snapshot;
+
+	Assert(RelationGetRelid(relation) == parallel_scan->phs_relid);
+
+	if (!parallel_scan->phs_snapshot_any)
+	{
+		/* Snapshot was serialized -- restore it */
+		snapshot = RestoreSnapshot(parallel_scan->phs_snapshot_data);
+		RegisterSnapshot(snapshot);
+	}
+	else
+	{
+		/* SnapshotAny passed by caller (not serialized) */
+		snapshot = SnapshotAny;
+	}
+
+	return relation->rd_tableamroutine->scan_begin(relation, snapshot, 0, NULL, parallel_scan,
+												true, true, true, false, false, !parallel_scan->phs_snapshot_any);
+}
+
+static inline ParallelHeapScanDesc
+tableam_get_parallelheapscandesc(TableScanDesc sscan)
+{
+	return sscan->rs_rd->rd_tableamroutine->scan_get_parallelheapscandesc(sscan);
+}
+
+static inline HeapPageScanDesc
+tableam_get_heappagescandesc(TableScanDesc sscan)
+{
+	/*
+	 * Planner should have already validated whether the current storage
+	 * supports Page scans are not? This function will be called only from
+	 * Bitmap Heap scan and sample scan
+	 */
+	Assert(sscan->rs_rd->rd_tableamroutine->scan_get_heappagescandesc != NULL);
+
+	return sscan->rs_rd->rd_tableamroutine->scan_get_heappagescandesc(sscan);
+}
+
+static inline void
+table_syncscan_report_location(Relation rel, BlockNumber location)
+{
+	return rel->rd_tableamroutine->sync_scan_report_location(rel, location);
+}
+
+/*
+ * heap_setscanlimits - restrict range of a heapscan
+ *
+ * startBlk is the page to start at
+ * numBlks is number of pages to scan (InvalidBlockNumber means "all")
+ */
+static inline void
+table_setscanlimits(TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks)
+{
+	sscan->rs_rd->rd_tableamroutine->scansetlimits(sscan, startBlk, numBlks);
+}
+
+
+/* ----------------
+ *		heap_beginscan	- begin relation scan
+ *
+ * heap_beginscan is the "standard" case.
+ *
+ * heap_beginscan_catalog differs in setting up its own temporary snapshot.
+ *
+ * heap_beginscan_strat offers an extended API that lets the caller control
+ * whether a nondefault buffer access strategy can be used, and whether
+ * syncscan can be chosen (possibly resulting in the scan not starting from
+ * block zero).  Both of these default to true with plain heap_beginscan.
+ *
+ * heap_beginscan_bm is an alternative entry point for setting up a
+ * TableScanDesc for a bitmap heap scan.  Although that scan technology is
+ * really quite unlike a standard seqscan, there is just enough commonality
+ * to make it worth using the same data structure.
+ *
+ * heap_beginscan_sampling is an alternative entry point for setting up a
+ * TableScanDesc for a TABLESAMPLE scan.  As with bitmap scans, it's worth
+ * using the same data structure although the behavior is rather different.
+ * In addition to the options offered by heap_beginscan_strat, this call
+ * also allows control of whether page-mode visibility checking is used.
+ * ----------------
+ */
+static inline TableScanDesc
+table_beginscan(Relation relation, Snapshot snapshot,
+				  int nkeys, ScanKey key)
+{
+	return relation->rd_tableamroutine->scan_begin(relation, snapshot, nkeys, key, NULL,
+												true, true, true, false, false, false);
+}
+
+static inline TableScanDesc
+table_beginscan_catalog(Relation relation, int nkeys, ScanKey key)
+{
+	Oid			relid = RelationGetRelid(relation);
+	Snapshot	snapshot = RegisterSnapshot(GetCatalogSnapshot(relid));
+
+	return relation->rd_tableamroutine->scan_begin(relation, snapshot, nkeys, key, NULL,
+												true, true, true, false, false, true);
+}
+
+static inline TableScanDesc
+table_beginscan_strat(Relation relation, Snapshot snapshot,
+						int nkeys, ScanKey key,
+						bool allow_strat, bool allow_sync)
+{
+	return relation->rd_tableamroutine->scan_begin(relation, snapshot, nkeys, key, NULL,
+												allow_strat, allow_sync, true,
+												false, false, false);
+}
+
+static inline TableScanDesc
+table_beginscan_bm(Relation relation, Snapshot snapshot,
+					 int nkeys, ScanKey key)
+{
+	return relation->rd_tableamroutine->scan_begin(relation, snapshot, nkeys, key, NULL,
+												false, false, true, true, false, false);
+}
+
+static inline TableScanDesc
+table_beginscan_sampling(Relation relation, Snapshot snapshot,
+						   int nkeys, ScanKey key,
+						   bool allow_strat, bool allow_sync, bool allow_pagemode)
+{
+	return relation->rd_tableamroutine->scan_begin(relation, snapshot, nkeys, key, NULL,
+												allow_strat, allow_sync, allow_pagemode,
+												false, true, false);
+}
+
+/* ----------------
+ *		heap_rescan		- restart a relation scan
+ * ----------------
+ */
+static inline void
+table_rescan(TableScanDesc scan,
+			   ScanKey key)
+{
+	scan->rs_rd->rd_tableamroutine->scan_rescan(scan, key, false, false, false, false);
+}
+
+/* ----------------
+ *		heap_rescan_set_params	- restart a relation scan after changing params
+ *
+ * This call allows changing the buffer strategy, syncscan, and pagemode
+ * options before starting a fresh scan.  Note that although the actual use
+ * of syncscan might change (effectively, enabling or disabling reporting),
+ * the previously selected startblock will be kept.
+ * ----------------
+ */
+static inline void
+table_rescan_set_params(TableScanDesc scan, ScanKey key,
+						  bool allow_strat, bool allow_sync, bool allow_pagemode)
+{
+	scan->rs_rd->rd_tableamroutine->scan_rescan(scan, key, true,
+											 allow_strat, allow_sync, (allow_pagemode && IsMVCCSnapshot(scan->rs_snapshot)));
+}
+
+/* ----------------
+ *		heap_endscan	- end relation scan
+ *
+ *		See how to integrate with index scans.
+ *		Check handling if reldesc caching.
+ * ----------------
+ */
+static inline void
+table_endscan(TableScanDesc scan)
+{
+	scan->rs_rd->rd_tableamroutine->scan_end(scan);
+}
+
+
+/* ----------------
+ *		heap_update_snapshot
+ *
+ *		Update snapshot info in heap scan descriptor.
+ * ----------------
+ */
+static inline void
+table_scan_update_snapshot(TableScanDesc scan, Snapshot snapshot)
+{
+	scan->rs_rd->rd_tableamroutine->scan_update_snapshot(scan, snapshot);
+}
+
+static inline TupleTableSlot *
+table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
+{
+	return sscan->rs_rd->rd_tableamroutine->scan_getnextslot(sscan, direction, slot);
+}
+
+static inline bool
+table_tuple_fetch_from_offset(TableScanDesc sscan, BlockNumber blkno, OffsetNumber offset, TupleTableSlot *slot)
+{
+	return sscan->rs_rd->rd_tableamroutine->scan_fetch_tuple_from_offset(sscan, blkno, offset, slot);
+}
+
+
+static inline IndexFetchTableData*
+table_begin_index_fetch_table(Relation rel)
+{
+	return rel->rd_tableamroutine->begin_index_fetch(rel);
+}
+
+static inline void
+table_reset_index_fetch_table(struct IndexFetchTableData* scan)
+{
+	scan->rel->rd_tableamroutine->reset_index_fetch(scan);
+}
+
+static inline void
+table_end_index_fetch_table(struct IndexFetchTableData* scan)
+{
+	scan->rel->rd_tableamroutine->end_index_fetch(scan);
+}
+
+/*
+ * Insert a tuple from a slot into table AM routine
+ */
+static inline Oid
+table_insert(Relation relation, TupleTableSlot *slot, CommandId cid,
+			   int options, BulkInsertState bistate)
+{
+	return relation->rd_tableamroutine->tuple_insert(relation, slot, cid, options,
+													 bistate);
+}
+
+static inline Oid
+table_insert_speculative(Relation relation, TupleTableSlot *slot, CommandId cid,
+						 int options, BulkInsertState bistate, uint32 specToken)
+{
+	return relation->rd_tableamroutine->tuple_insert_speculative(relation, slot, cid, options,
+																 bistate, specToken);
+}
+
+static inline void
+table_complete_speculative(Relation relation, TupleTableSlot *slot, uint32 specToken,
+								bool succeeded)
+{
+	return relation->rd_tableamroutine->tuple_complete_speculative(relation, slot, specToken, succeeded);
+}
+
+/*
+ * Delete a tuple from tid using table AM routine
+ */
+static inline HTSU_Result
+table_delete(Relation relation, ItemPointer tid, CommandId cid,
+			 Snapshot crosscheck, bool wait,
+			 HeapUpdateFailureData *hufd, bool changingPart)
+{
+	return relation->rd_tableamroutine->tuple_delete(relation, tid, cid,
+												  crosscheck, wait, hufd, changingPart);
+}
+
+/*
+ * update a tuple from tid using table AM routine
+ */
+static inline HTSU_Result
+table_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
+			 CommandId cid, Snapshot crosscheck, bool wait,
+			 HeapUpdateFailureData *hufd, LockTupleMode *lockmode,
+			 bool *update_indexes)
+{
+	return relation->rd_tableamroutine->tuple_update(relation, otid, slot,
+												  cid, crosscheck, wait, hufd,
+												  lockmode, update_indexes);
+}
+
+static inline bool
+table_fetch_follow(struct IndexFetchTableData *scan,
+				   ItemPointer tid,
+				   Snapshot snapshot,
+				   TupleTableSlot *slot,
+				   bool *call_again, bool *all_dead)
+{
+
+	return scan->rel->rd_tableamroutine->tuple_fetch_follow(scan, tid, snapshot,
+														   slot, call_again,
+														   all_dead);
+}
+
+static inline bool
+table_fetch_follow_check(Relation rel,
+						 ItemPointer tid,
+						 Snapshot snapshot,
+						 bool *all_dead)
+{
+	IndexFetchTableData *scan = table_begin_index_fetch_table(rel);
+	TupleTableSlot *slot = table_gimmegimmeslot(rel, NULL);
+	bool call_again = false;
+	bool found;
+
+	found = table_fetch_follow(scan, tid, snapshot, slot, &call_again, all_dead);
+
+	table_end_index_fetch_table(scan);
+	ExecDropSingleTupleTableSlot(slot);
+
+	return found;
+}
+
+/*
+ *	table_multi_insert	- insert multiple tuple into a table
+ */
+static inline void
+table_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
+					 CommandId cid, int options, BulkInsertState bistate)
+{
+	relation->rd_tableamroutine->multi_insert(relation, tuples, ntuples,
+										   cid, options, bistate);
+}
+
+static inline tuple_data
+table_tuple_get_data(Relation relation, TupleTableSlot *slot, tuple_data_flags flags)
+{
+	return relation->rd_tableamroutine->get_tuple_data(slot, flags);
+}
+
+static inline void
+table_get_latest_tid(Relation relation,
+					   Snapshot snapshot,
+					   ItemPointer tid)
+{
+	relation->rd_tableamroutine->tuple_get_latest_tid(relation, snapshot, tid);
+}
+
+
+static inline void
+table_vacuum_rel(Relation rel, int options,
+			 struct VacuumParams *params, BufferAccessStrategy bstrategy)
+{
+	rel->rd_tableamroutine->relation_vacuum(rel, options, params, bstrategy);
+}
+
+/*
+ *	table_sync		- sync a heap, for use when no WAL has been written
+ */
+static inline void
+table_sync(Relation rel)
+{
+	rel->rd_tableamroutine->relation_sync(rel);
+}
+
+/*
+ * -------------------
+ * storage Bulk Insert functions
+ * -------------------
+ */
+static inline BulkInsertState
+table_getbulkinsertstate(Relation rel)
+{
+	return rel->rd_tableamroutine->getbulkinsertstate();
+}
+
+static inline void
+table_freebulkinsertstate(Relation rel, BulkInsertState bistate)
+{
+	rel->rd_tableamroutine->freebulkinsertstate(bistate);
+}
+
+static inline void
+table_releasebulkinsertstate(Relation rel, BulkInsertState bistate)
+{
+	rel->rd_tableamroutine->releasebulkinsertstate(bistate);
+}
+
+/*
+ * -------------------
+ * storage tuple rewrite functions
+ * -------------------
+ */
+static inline RewriteState
+table_begin_rewrite(Relation OldHeap, Relation NewHeap,
+				   TransactionId OldestXmin, TransactionId FreezeXid,
+				   MultiXactId MultiXactCutoff, bool use_wal)
+{
+	return NewHeap->rd_tableamroutine->begin_heap_rewrite(OldHeap, NewHeap,
+			OldestXmin, FreezeXid, MultiXactCutoff, use_wal);
+}
+
+static inline void
+table_end_rewrite(Relation rel, RewriteState state)
+{
+	rel->rd_tableamroutine->end_heap_rewrite(state);
+}
+
+static inline void
+table_rewrite_tuple(Relation rel, RewriteState state, HeapTuple oldTuple,
+				   HeapTuple newTuple)
+{
+	rel->rd_tableamroutine->rewrite_heap_tuple(state, oldTuple, newTuple);
+}
+
+static inline bool
+table_rewrite_dead_tuple(Relation rel, RewriteState state, HeapTuple oldTuple)
+{
+	return rel->rd_tableamroutine->rewrite_heap_dead_tuple(state, oldTuple);
+}
+
+/*
+ * HeapTupleSatisfiesVisibility
+ *		True iff heap tuple satisfies a time qual.
+ *
+ * Notes:
+ *	Assumes heap tuple is valid.
+ *	Beware of multiple evaluations of snapshot argument.
+ *	Hint bits in the HeapTuple's t_infomask may be updated as a side effect;
+ *	if so, the indicated buffer is marked dirty.
+ */
+#define HeapTupleSatisfiesVisibility(method, slot, snapshot) \
+	(((method)->snapshot_satisfies) (slot, snapshot))
+
+extern TableAmRoutine * GetTableAmRoutine(Oid amhandler);
+extern TableAmRoutine * GetTableAmRoutineByAmId(Oid amoid);
+extern TableAmRoutine * GetHeapamTableAmRoutine(void);
 
 #endif		/* TABLEAM_H */
diff --git a/src/include/access/tableamapi.h b/src/include/access/tableamapi.h
deleted file mode 100644
index a4a6e7fd23..0000000000
--- a/src/include/access/tableamapi.h
+++ /dev/null
@@ -1,212 +0,0 @@
-/*---------------------------------------------------------------------
- *
- * tableamapi.h
- *		API for Postgres table access methods
- *
- * Copyright (c) 2017, PostgreSQL Global Development Group
- *
- * src/include/access/tableamapi.h
- *---------------------------------------------------------------------
- */
-#ifndef TABLEEAMAPI_H
-#define TABLEEAMAPI_H
-
-#include "access/heapam.h"
-#include "access/tableam.h"
-#include "nodes/execnodes.h"
-#include "nodes/nodes.h"
-#include "fmgr.h"
-#include "utils/snapshot.h"
-
-struct IndexFetchTableData;
-
-/*
- * Storage routine function hooks
- */
-typedef bool (*SnapshotSatisfies_function) (TupleTableSlot *slot, Snapshot snapshot);
-typedef HTSU_Result (*SnapshotSatisfiesUpdate_function) (TupleTableSlot *slot, CommandId curcid);
-typedef HTSV_Result (*SnapshotSatisfiesVacuum_function) (TupleTableSlot *slot, TransactionId OldestXmin);
-
-typedef Oid (*TupleInsert_function) (Relation rel, TupleTableSlot *slot, CommandId cid,
-									 int options, BulkInsertState bistate);
-
-typedef Oid (*TupleInsertSpeculative_function) (Relation rel,
-												 TupleTableSlot *slot,
-												 CommandId cid,
-												 int options,
-												 BulkInsertState bistate,
-												 uint32 specToken);
-
-
-typedef void (*TupleCompleteSpeculative_function) (Relation rel,
-												  TupleTableSlot *slot,
-												  uint32 specToken,
-												  bool succeeded);
-
-typedef HTSU_Result (*TupleDelete_function) (Relation relation,
-											 ItemPointer tid,
-											 CommandId cid,
-											 Snapshot crosscheck,
-											 bool wait,
-											 HeapUpdateFailureData *hufd,
-											 bool changingPart);
-
-typedef HTSU_Result (*TupleUpdate_function) (Relation relation,
-											 ItemPointer otid,
-											 TupleTableSlot *slot,
-											 CommandId cid,
-											 Snapshot crosscheck,
-											 bool wait,
-											 HeapUpdateFailureData *hufd,
-											 LockTupleMode *lockmode,
-											 bool *update_indexes);
-
-typedef bool (*TupleFetchRowVersion_function) (Relation relation,
-											   ItemPointer tid,
-											   Snapshot snapshot,
-											   TupleTableSlot *slot,
-											   Relation stats_relation);
-
-typedef HTSU_Result (*TupleLock_function) (Relation relation,
-										   ItemPointer tid,
-										   Snapshot snapshot,
-										   TupleTableSlot *slot,
-										   CommandId cid,
-										   LockTupleMode mode,
-										   LockWaitPolicy wait_policy,
-										   uint8 flags,
-										   HeapUpdateFailureData *hufd);
-
-typedef void (*MultiInsert_function) (Relation relation, HeapTuple *tuples, int ntuples,
-									  CommandId cid, int options, BulkInsertState bistate);
-
-typedef void (*TupleGetLatestTid_function) (Relation relation,
-											Snapshot snapshot,
-											ItemPointer tid);
-
-typedef tuple_data(*GetTupleData_function) (TupleTableSlot *slot, tuple_data_flags flags);
-
-struct VacuumParams;
-typedef void (*RelationVacuum_function)(Relation onerel, int options,
-				struct VacuumParams *params, BufferAccessStrategy bstrategy);
-
-typedef void (*RelationSync_function) (Relation relation);
-
-typedef BulkInsertState (*GetBulkInsertState_function) (void);
-typedef void (*FreeBulkInsertState_function) (BulkInsertState bistate);
-typedef void (*ReleaseBulkInsertState_function) (BulkInsertState bistate);
-
-typedef RewriteState (*BeginHeapRewrite_function) (Relation OldHeap, Relation NewHeap,
-				   TransactionId OldestXmin, TransactionId FreezeXid,
-				   MultiXactId MultiXactCutoff, bool use_wal);
-typedef void (*EndHeapRewrite_function) (RewriteState state);
-typedef void (*RewriteHeapTuple_function) (RewriteState state, HeapTuple oldTuple,
-				   HeapTuple newTuple);
-typedef bool (*RewriteHeapDeadTuple_function) (RewriteState state, HeapTuple oldTuple);
-
-typedef TupleTableSlot* (*Slot_function) (Relation relation);
-
-typedef TableScanDesc (*ScanBegin_function) (Relation relation,
-											Snapshot snapshot,
-											int nkeys, ScanKey key,
-											ParallelHeapScanDesc parallel_scan,
-											bool allow_strat,
-											bool allow_sync,
-											bool allow_pagemode,
-											bool is_bitmapscan,
-											bool is_samplescan,
-											bool temp_snap);
-
-typedef struct IndexFetchTableData* (*BeginIndexFetchTable_function) (Relation relation);
-typedef void (*ResetIndexFetchTable_function) (struct IndexFetchTableData* data);
-typedef void (*EndIndexFetchTable_function) (struct IndexFetchTableData* data);
-
-typedef ParallelHeapScanDesc (*ScanGetParallelheapscandesc_function) (TableScanDesc scan);
-typedef HeapPageScanDesc(*ScanGetHeappagescandesc_function) (TableScanDesc scan);
-typedef void (*SyncScanReportLocation_function) (Relation rel, BlockNumber location);
-typedef void (*ScanSetlimits_function) (TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks);
-
-typedef TupleTableSlot *(*ScanGetnextSlot_function) (TableScanDesc scan,
-													 ScanDirection direction, TupleTableSlot *slot);
-
-typedef bool (*ScanFetchTupleFromOffset_function) (TableScanDesc scan,
-												   BlockNumber blkno, OffsetNumber offset, TupleTableSlot *slot);
-
-typedef void (*ScanEnd_function) (TableScanDesc scan);
-
-
-typedef void (*ScanGetpage_function) (TableScanDesc scan, BlockNumber page);
-typedef void (*ScanRescan_function) (TableScanDesc scan, ScanKey key, bool set_params,
-									 bool allow_strat, bool allow_sync, bool allow_pagemode);
-typedef void (*ScanUpdateSnapshot_function) (TableScanDesc scan, Snapshot snapshot);
-
-typedef bool (*TupleFetchFollow_function)(struct IndexFetchTableData *scan,
-										  ItemPointer tid,
-										  Snapshot snapshot,
-										  TupleTableSlot *slot,
-										  bool *call_again, bool *all_dead);
-
-/*
- * API struct for a table AM.  Note this must be stored in a single palloc'd
- * chunk of memory.
- */
-typedef struct TableAmRoutine
-{
-	NodeTag		type;
-
-	Slot_function gimmegimmeslot;
-
-	SnapshotSatisfies_function snapshot_satisfies;
-	SnapshotSatisfiesUpdate_function snapshot_satisfiesUpdate;
-	SnapshotSatisfiesVacuum_function snapshot_satisfiesVacuum;
-
-	/* Operations on physical tuples */
-	TupleInsert_function tuple_insert;
-	TupleInsertSpeculative_function tuple_insert_speculative;
-	TupleCompleteSpeculative_function tuple_complete_speculative;
-	TupleUpdate_function tuple_update;
-	TupleDelete_function tuple_delete;
-	TupleFetchRowVersion_function tuple_fetch_row_version;
-	TupleLock_function tuple_lock;
-	MultiInsert_function multi_insert;
-	TupleGetLatestTid_function tuple_get_latest_tid;
-	TupleFetchFollow_function tuple_fetch_follow;
-
-	GetTupleData_function get_tuple_data;
-
-	RelationVacuum_function relation_vacuum;
-	RelationSync_function relation_sync;
-
-	GetBulkInsertState_function getbulkinsertstate;
-	FreeBulkInsertState_function freebulkinsertstate;
-	ReleaseBulkInsertState_function releasebulkinsertstate;
-
-	BeginHeapRewrite_function begin_heap_rewrite;
-	EndHeapRewrite_function end_heap_rewrite;
-	RewriteHeapTuple_function rewrite_heap_tuple;
-	RewriteHeapDeadTuple_function rewrite_heap_dead_tuple;
-
-	/* Operations on relation scans */
-	ScanBegin_function scan_begin;
-	ScanGetParallelheapscandesc_function scan_get_parallelheapscandesc;
-	ScanGetHeappagescandesc_function scan_get_heappagescandesc;
-	SyncScanReportLocation_function sync_scan_report_location;
-	ScanSetlimits_function scansetlimits;
-	ScanGetnextSlot_function scan_getnextslot;
-	ScanFetchTupleFromOffset_function scan_fetch_tuple_from_offset;
-	ScanEnd_function scan_end;
-	ScanGetpage_function scan_getpage;
-	ScanRescan_function scan_rescan;
-	ScanUpdateSnapshot_function scan_update_snapshot;
-
-	BeginIndexFetchTable_function begin_index_fetch;
-	EndIndexFetchTable_function reset_index_fetch;
-	EndIndexFetchTable_function end_index_fetch;
-
-}			TableAmRoutine;
-
-extern TableAmRoutine * GetTableAmRoutine(Oid amhandler);
-extern TableAmRoutine * GetTableAmRoutineByAmId(Oid amoid);
-extern TableAmRoutine * GetHeapamTableAmRoutine(void);
-
-#endif							/* TABLEEAMAPI_H */
diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h
index 945bbc3ddf..c69ca99435 100644
--- a/src/include/nodes/nodes.h
+++ b/src/include/nodes/nodes.h
@@ -502,7 +502,7 @@ typedef enum NodeTag
 	T_InlineCodeBlock,			/* in nodes/parsenodes.h */
 	T_FdwRoutine,				/* in foreign/fdwapi.h */
 	T_IndexAmRoutine,			/* in access/amapi.h */
-	T_TableAmRoutine,			/* in access/tableamapi.h */
+	T_TableAmRoutine,			/* in access/tableam.h */
 	T_TsmRoutine,				/* in access/tsmapi.h */
 	T_ForeignKeyCacheInfo,		/* in utils/rel.h */
 	T_CallContext				/* in nodes/parsenodes.h */
diff --git a/src/include/utils/tqual.h b/src/include/utils/tqual.h
index 9739bed9e0..1fe9cc6402 100644
--- a/src/include/utils/tqual.h
+++ b/src/include/utils/tqual.h
@@ -16,10 +16,8 @@
 #define TQUAL_H
 
 #include "utils/snapshot.h"
-#include "access/tableamapi.h"
 #include "access/xlogdefs.h"
 
-
 /* Static variables representing various special snapshot semantics */
 extern PGDLLIMPORT SnapshotData SnapshotSelfData;
 extern PGDLLIMPORT SnapshotData SnapshotAnyData;
@@ -33,19 +31,6 @@ extern PGDLLIMPORT SnapshotData CatalogSnapshotData;
 	((snapshot)->visibility_type == MVCC_VISIBILITY || \
 	 (snapshot)->visibility_type == HISTORIC_MVCC_VISIBILITY)
 
-/*
- * HeapTupleSatisfiesVisibility
- *		True iff heap tuple satisfies a time qual.
- *
- * Notes:
- *	Assumes heap tuple is valid.
- *	Beware of multiple evaluations of snapshot argument.
- *	Hint bits in the HeapTuple's t_infomask may be updated as a side effect;
- *	if so, the indicated buffer is marked dirty.
- */
-#define HeapTupleSatisfiesVisibility(method, slot, snapshot) \
-	(((method)->snapshot_satisfies) (slot, snapshot))
-
 /*
  * To avoid leaking too much knowledge about reorderbuffer implementation
  * details this is implemented in reorderbuffer.c not tqual.c.
-- 
2.18.0.windows.1

