From 2982d89e825c334d07aa14e8e5038ea02034e581 Mon Sep 17 00:00:00 2001
From: kommih <haribabuk@fast.au.fujitsu.com>
Date: Mon, 22 Oct 2018 16:06:10 +1100
Subject: [PATCH 1/3] Further fixes and cleanup

1. Remove the old slot interface file and also update the Makefile.
2. CREATE AS USING method grammer support
3. Materialized view grammer support

This change was missed during earlier USING grammer support.

3. Remove the extra Tuple visibility check

In heapgettup_pagemode the tuple visiblity check is added
during the early devlopment of pluggable storage, but the visibility
check is already carried out in heapgetpage function itself.

4. Handling HeapTupleInvisible case

In update/delete scenarios, when the tuple is
concurrently updated/deleted, sometimes locking
of a tuple may return HeapTupleInvisible. Handle
that case as nothing to do.

regression fixes

1. scan start offset fix during analyze
2. Materialize the slot before they are processed using intorel_receive
3. ROW_MARK_COPY support by force store of heap tuple
4. partition prune extra heap page fix
5. Basic syntax usage tests addition
---
 contrib/pg_visibility/pg_visibility.c       |  5 +-
 src/backend/access/heap/heapam.c            | 28 +++-----
 src/backend/access/heap/heapam_handler.c    |  6 +-
 src/backend/access/heap/heapam_visibility.c |  4 +-
 src/backend/access/index/genam.c            |  3 +-
 src/backend/access/table/Makefile           |  2 +-
 src/backend/access/table/tableam_common.c   |  0
 src/backend/catalog/heap.c                  | 17 +++++
 src/backend/commands/cluster.c              |  6 +-
 src/backend/commands/createas.c             |  5 ++
 src/backend/executor/execExprInterp.c       | 16 ++---
 src/backend/executor/execMain.c             | 13 +---
 src/backend/executor/execReplication.c      |  3 -
 src/backend/executor/execTuples.c           | 21 ++++++
 src/backend/executor/nodeBitmapHeapscan.c   | 12 ++++
 src/backend/executor/nodeModifyTable.c      | 22 +++++-
 src/backend/parser/gram.y                   | 18 ++---
 src/include/executor/tuptable.h             |  1 +
 src/include/nodes/primnodes.h               |  1 +
 src/test/regress/expected/create_am.out     | 78 +++++++++++++++++++++
 src/test/regress/sql/create_am.sql          | 46 ++++++++++++
 21 files changed, 243 insertions(+), 64 deletions(-)
 delete mode 100644 src/backend/access/table/tableam_common.c

diff --git a/contrib/pg_visibility/pg_visibility.c b/contrib/pg_visibility/pg_visibility.c
index dce5262e34..88ca4fd2af 100644
--- a/contrib/pg_visibility/pg_visibility.c
+++ b/contrib/pg_visibility/pg_visibility.c
@@ -563,12 +563,13 @@ collect_corrupt_items(Oid relid, bool all_visible, bool all_frozen)
 
 	rel = relation_open(relid, AccessShareLock);
 
+	/* Only some relkinds have a visibility map */
+	check_relation_relkind(rel);
+
 	if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
 		ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 						errmsg("only heap AM is supported")));
 
-	/* Only some relkinds have a visibility map */
-	check_relation_relkind(rel);
 
 	nblocks = RelationGetNumberOfBlocks(rel);
 
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index ec99d0bcae..ef6b4c3e54 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -947,31 +947,25 @@ heapgettup_pagemode(HeapScanDesc scan,
 			/*
 			 * if current tuple qualifies, return it.
 			 */
-			if (HeapTupleSatisfies(tuple, scan->rs_scan.rs_snapshot, scan->rs_cbuf))
+			if (key != NULL)
 			{
-				/*
-				 * if current tuple qualifies, return it.
-				 */
-				if (key != NULL)
-				{
-					bool		valid;
+				bool		valid;
 
-					HeapKeyTest(tuple, RelationGetDescr(scan->rs_scan.rs_rd),
-								nkeys, key, valid);
-					if (valid)
-					{
-						scan->rs_cindex = lineindex;
-						LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
-						return;
-					}
-				}
-				else
+				HeapKeyTest(tuple, RelationGetDescr(scan->rs_scan.rs_rd),
+							nkeys, key, valid);
+				if (valid)
 				{
 					scan->rs_cindex = lineindex;
 					LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
 					return;
 				}
 			}
+			else
+			{
+				scan->rs_cindex = lineindex;
+				LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
+				return;
+			}
 
 			/*
 			 * otherwise move to the next item on the page
diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index c3960dc91f..3254e30a45 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -1741,7 +1741,7 @@ heapam_scan_analyze_next_tuple(TableScanDesc sscan, TransactionId OldestXmin, do
 {
 	HeapScanDesc scan = (HeapScanDesc) sscan;
 	Page		targpage;
-	OffsetNumber targoffset = scan->rs_cindex;
+	OffsetNumber targoffset;
 	OffsetNumber maxoffset;
 	BufferHeapTupleTableSlot *hslot;
 
@@ -1751,7 +1751,9 @@ heapam_scan_analyze_next_tuple(TableScanDesc sscan, TransactionId OldestXmin, do
 	maxoffset = PageGetMaxOffsetNumber(targpage);
 
 	/* Inner loop over all tuples on the selected page */
-	for (targoffset = scan->rs_cindex; targoffset <= maxoffset; targoffset++)
+	for (targoffset = scan->rs_cindex ? scan->rs_cindex : FirstOffsetNumber;
+			targoffset <= maxoffset;
+			targoffset++)
 	{
 		ItemId		itemid;
 		HeapTuple	targtuple = &hslot->base.tupdata;
diff --git a/src/backend/access/heap/heapam_visibility.c b/src/backend/access/heap/heapam_visibility.c
index 8233475aa0..7bad246f55 100644
--- a/src/backend/access/heap/heapam_visibility.c
+++ b/src/backend/access/heap/heapam_visibility.c
@@ -1838,8 +1838,10 @@ HeapTupleSatisfies(HeapTuple stup, Snapshot snapshot, Buffer buffer)
 		case NON_VACUUMABLE_VISIBILTY:
 			return HeapTupleSatisfiesNonVacuumable(stup, snapshot, buffer);
 			break;
-		default:
+		case END_OF_VISIBILITY:
 			Assert(0);
 			break;
 	}
+
+	return false; /* keep compiler quiet */
 }
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c
index e06bd0479f..94c9702dc1 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -455,10 +455,9 @@ systable_recheck_tuple(SysScanDesc sysscan, HeapTuple tup)
 
 	if (sysscan->irel)
 	{
-		IndexScanDesc scan = sysscan->iscan;
 		IndexFetchHeapData *hscan = (IndexFetchHeapData *) sysscan->iscan->xs_heapfetch;
 
-		Assert(IsMVCCSnapshot(scan->xs_snapshot));
+		Assert(IsMVCCSnapshot(sysscan->iscan->xs_snapshot));
 		//Assert(tup == &hscan->xs_ctup); replace by peeking into slot?
 		Assert(BufferIsValid(hscan->xs_cbuf));
 		/* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
diff --git a/src/backend/access/table/Makefile b/src/backend/access/table/Makefile
index fe22bf9208..006ba99182 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 = tableam.o tableamapi.o
 
 include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/access/table/tableam_common.c b/src/backend/access/table/tableam_common.c
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 38b368f916..398f90775f 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -42,6 +42,7 @@
 #include "catalog/index.h"
 #include "catalog/objectaccess.h"
 #include "catalog/partition.h"
+#include "catalog/pg_am.h"
 #include "catalog/pg_attrdef.h"
 #include "catalog/pg_collation.h"
 #include "catalog/pg_constraint.h"
@@ -1388,6 +1389,22 @@ heap_create_with_catalog(const char *relname,
 			recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
 		}
 
+		/*
+		 * Make a dependency link to force the relation to be deleted if its
+		 * access method is. Do this only for relation and materialized views.
+		 *
+		 * No need to add an explicit dependency with toast, as the original
+		 * table depends on it.
+		 */
+		if ((relkind == RELKIND_RELATION) ||
+				(relkind == RELKIND_MATVIEW))
+		{
+			referenced.classId = AccessMethodRelationId;
+			referenced.objectId = accessmtd;
+			referenced.objectSubId = 0;
+			recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
+		}
+
 		if (relacl != NULL)
 		{
 			int			nnewmembers;
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index 63974979da..3ce8862a01 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -755,8 +755,6 @@ copy_table_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
 	Relation	relRelation;
 	HeapTuple	reltup;
 	Form_pg_class relform;
-	TupleDesc	oldTupDesc;
-	TupleDesc	newTupDesc;
 	TransactionId OldestXmin;
 	TransactionId FreezeXid;
 	MultiXactId MultiXactCutoff;
@@ -784,9 +782,7 @@ copy_table_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
 	 * Their tuple descriptors should be exactly alike, but here we only need
 	 * assume that they have the same number of columns.
 	 */
-	oldTupDesc = RelationGetDescr(OldHeap);
-	newTupDesc = RelationGetDescr(NewHeap);
-	Assert(newTupDesc->natts == oldTupDesc->natts);
+	Assert(RelationGetDescr(NewHeap)->natts == RelationGetDescr(OldHeap)->natts);
 
 	/*
 	 * If the OldHeap has a toast table, get lock on the toast table to keep
diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 84de804175..82c0eb2824 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -108,6 +108,7 @@ create_ctas_internal(List *attrList, IntoClause *into)
 	create->oncommit = into->onCommit;
 	create->tablespacename = into->tableSpaceName;
 	create->if_not_exists = false;
+	create->accessMethod = into->accessMethod;
 
 	// PBORKED: toast options
 
@@ -593,6 +594,10 @@ intorel_receive(TupleTableSlot *slot, DestReceiver *self)
 	if (myState->rel->rd_rel->relhasoids)
 		slot->tts_tupleOid = InvalidOid;
 
+	/* Materialize the slot */
+	if (!TTS_IS_VIRTUAL(slot))
+		ExecMaterializeSlot(slot);
+
 	table_insert(myState->rel,
 				 slot,
 				 myState->output_cid,
diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c
index ef94ac4aa0..8df85c2f48 100644
--- a/src/backend/executor/execExprInterp.c
+++ b/src/backend/executor/execExprInterp.c
@@ -529,20 +529,13 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
 			Assert(TTS_IS_HEAPTUPLE(outerslot) ||
 				   TTS_IS_BUFFERTUPLE(outerslot));
 
-			/* The slot should have a valid heap tuple. */
-#if FIXME
-			/* The slot should have a valid heap tuple. */
-			Assert(hslot->tuple != NULL);
-#endif
-
-			/*
-			 * hari
-			 * Assert(outerslot->tts_storageslotam->slot_is_physical_tuple(outerslot));
-			 */
 			if (attnum == TableOidAttributeNumber)
 				d = ObjectIdGetDatum(outerslot->tts_tableOid);
 			else
 			{
+				/* The slot should have a valid heap tuple. */
+				Assert(hslot->tuple != NULL);
+
 				/* heap_getsysattr has sufficient defenses against bad attnums */
 				d = heap_getsysattr(hslot->tuple, attnum,
 									outerslot->tts_tupleDescriptor,
@@ -570,6 +563,9 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
 				Assert(TTS_IS_HEAPTUPLE(scanslot) ||
 					   TTS_IS_BUFFERTUPLE(scanslot));
 
+				if (hslot->tuple == NULL)
+					ExecMaterializeSlot(scanslot);
+
 				d = heap_getsysattr(hslot->tuple, attnum,
 									scanslot->tts_tupleDescriptor,
 									op->resnull);
diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index e055c0a7c6..34ef86a5bd 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -2594,7 +2594,7 @@ EvalPlanQual(EState *estate, EPQState *epqstate,
 	 * datums that may be present in copyTuple).  As with the next step, this
 	 * is to guard against early re-use of the EPQ query.
 	 */
-	if (!TupIsNull(slot))
+	if (!TupIsNull(slot) && !TTS_IS_VIRTUAL(slot))
 		ExecMaterializeSlot(slot);
 
 #if FIXME
@@ -2787,16 +2787,7 @@ EvalPlanQualFetchRowMarks(EPQState *epqstate)
 			if (isNull)
 				continue;
 
-			elog(ERROR, "frak, need to implement ROW_MARK_COPY");
-#ifdef FIXME
-			// FIXME: this should just deform the tuple and store it as a
-			// virtual one.
-			tuple = table_tuple_by_datum(erm->relation, datum, erm->relid);
-
-			/* store tuple */
-			EvalPlanQualSetTuple(epqstate, erm->rti, tuple);
-#endif
-
+			ExecForceStoreHeapTupleDatum(datum, slot);
 		}
 	}
 }
diff --git a/src/backend/executor/execReplication.c b/src/backend/executor/execReplication.c
index 452973e4ca..489e7d42a2 100644
--- a/src/backend/executor/execReplication.c
+++ b/src/backend/executor/execReplication.c
@@ -239,9 +239,6 @@ RelationFindReplTupleSeq(Relation rel, LockTupleMode lockmode,
 	SnapshotData snap;
 	TransactionId xwait;
 	bool		found;
-	TupleDesc	desc = RelationGetDescr(rel);
-
-	Assert(equalTupleDescs(desc, outslot->tts_tupleDescriptor));
 
 	/* Start a heap scan. */
 	InitDirtySnapshot(snap);
diff --git a/src/backend/executor/execTuples.c b/src/backend/executor/execTuples.c
index 917bf80f71..74149cc3ad 100644
--- a/src/backend/executor/execTuples.c
+++ b/src/backend/executor/execTuples.c
@@ -1364,6 +1364,27 @@ ExecStoreAllNullTuple(TupleTableSlot *slot)
 	return ExecFinishStoreSlotValues(slot);
 }
 
+void
+ExecForceStoreHeapTupleDatum(Datum data, TupleTableSlot *slot)
+{
+	HeapTuple	tuple;
+	HeapTupleHeader td;
+
+	td = DatumGetHeapTupleHeader(data);
+
+	tuple = (HeapTuple) palloc(HEAPTUPLESIZE + HeapTupleHeaderGetDatumLength(td));
+	tuple->t_len = HeapTupleHeaderGetDatumLength(td);
+	tuple->t_self = td->t_ctid;
+	tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
+	memcpy((char *) tuple->t_data, (char *) td, tuple->t_len);
+
+	ExecClearTuple(slot);
+
+	heap_deform_tuple(tuple, slot->tts_tupleDescriptor,
+					  slot->tts_values, slot->tts_isnull);
+	ExecFinishStoreSlotValues(slot);
+}
+
 /* --------------------------------
  *		ExecFetchSlotTuple
  *			Fetch the slot's regular physical tuple.
diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c
index 56880e3d16..36ca07beb2 100644
--- a/src/backend/executor/nodeBitmapHeapscan.c
+++ b/src/backend/executor/nodeBitmapHeapscan.c
@@ -224,6 +224,18 @@ BitmapHeapNext(BitmapHeapScanState *node)
 
 			BitmapAdjustPrefetchIterator(node, tbmres);
 
+			/*
+			 * Ignore any claimed entries past what we think is the end of the
+			 * relation.  (This is probably not necessary given that we got at
+			 * least AccessShareLock on the table before performing any of the
+			 * indexscans, but let's be safe.)
+			 */
+			if (tbmres->blockno >= scan->rs_nblocks)
+			{
+				node->tbmres = tbmres = NULL;
+				continue;
+			}
+
 			/*
 			 * We can skip fetching the heap page if we don't need any fields
 			 * from the heap, and the bitmap entries don't need rechecking,
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c
index 3cc9092413..b3851b180d 100644
--- a/src/backend/executor/nodeModifyTable.c
+++ b/src/backend/executor/nodeModifyTable.c
@@ -607,7 +607,7 @@ ExecDelete(ModifyTableState *mtstate,
 		   bool canSetTag,
 		   bool changingPart,
 		   bool *tupleDeleted,
-		   TupleTableSlot **epqslot)
+		   TupleTableSlot **epqreturnslot)
 {
 	ResultRelInfo *resultRelInfo;
 	Relation	resultRelationDesc;
@@ -632,7 +632,7 @@ ExecDelete(ModifyTableState *mtstate,
 		bool		dodelete;
 
 		dodelete = ExecBRDeleteTriggers(estate, epqstate, resultRelInfo,
-										tupleid, oldtuple, epqslot);
+										tupleid, oldtuple, epqreturnslot);
 
 		if (!dodelete)			/* "do nothing" */
 			return NULL;
@@ -724,8 +724,21 @@ ldelete:;
 					/* Tuple no more passing quals, exiting... */
 					return NULL;
 				}
+
+				/**/
+				if (epqreturnslot)
+				{
+					*epqreturnslot = epqslot;
+					return NULL;
+				}
+
 				goto ldelete;
 			}
+			else if (result == HeapTupleInvisible)
+			{
+				/* tuple is not visible; nothing to do */
+				return NULL;
+			}
 		}
 
 		switch (result)
@@ -1196,6 +1209,11 @@ lreplace:;
 				slot = ExecFilterJunk(resultRelInfo->ri_junkFilter, epqslot);
 				goto lreplace;
 			}
+			else if (result == HeapTupleInvisible)
+			{
+				/* tuple is not visible; nothing to do */
+				return NULL;
+			}
 		}
 
 		switch (result)
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 54382aba88..ea48e1d6e8 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -4037,7 +4037,6 @@ CreateStatsStmt:
  *
  *****************************************************************************/
 
-// PBORKED: storage option
 CreateAsStmt:
 		CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
 				{
@@ -4068,14 +4067,16 @@ CreateAsStmt:
 		;
 
 create_as_target:
-			qualified_name opt_column_list OptWith OnCommitOption OptTableSpace
+			qualified_name opt_column_list table_access_method_clause
+			OptWith OnCommitOption OptTableSpace
 				{
 					$$ = makeNode(IntoClause);
 					$$->rel = $1;
 					$$->colNames = $2;
-					$$->options = $3;
-					$$->onCommit = $4;
-					$$->tableSpaceName = $5;
+					$$->accessMethod = $3;
+					$$->options = $4;
+					$$->onCommit = $5;
+					$$->tableSpaceName = $6;
 					$$->viewQuery = NULL;
 					$$->skipData = false;		/* might get changed later */
 				}
@@ -4125,14 +4126,15 @@ CreateMatViewStmt:
 		;
 
 create_mv_target:
-			qualified_name opt_column_list opt_reloptions OptTableSpace
+			qualified_name opt_column_list table_access_method_clause opt_reloptions OptTableSpace
 				{
 					$$ = makeNode(IntoClause);
 					$$->rel = $1;
 					$$->colNames = $2;
-					$$->options = $3;
+					$$->accessMethod = $3;
+					$$->options = $4;
 					$$->onCommit = ONCOMMIT_NOOP;
-					$$->tableSpaceName = $4;
+					$$->tableSpaceName = $5;
 					$$->viewQuery = NULL;		/* filled at analysis time */
 					$$->skipData = false;		/* might get changed later */
 				}
diff --git a/src/include/executor/tuptable.h b/src/include/executor/tuptable.h
index 05f38cfd0d..20fc425a27 100644
--- a/src/include/executor/tuptable.h
+++ b/src/include/executor/tuptable.h
@@ -476,6 +476,7 @@ extern TupleTableSlot *ExecCopySlot(TupleTableSlot *dstslot,
 
 extern void ExecForceStoreHeapTuple(HeapTuple tuple,
 			   TupleTableSlot *slot);
+extern void ExecForceStoreHeapTupleDatum(Datum data, TupleTableSlot *slot);
 
 extern void slot_getmissingattrs(TupleTableSlot *slot, int startAttNum, int lastAttNum);
 extern Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull);
diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h
index 40f6eb03d2..4d194a8c2a 100644
--- a/src/include/nodes/primnodes.h
+++ b/src/include/nodes/primnodes.h
@@ -111,6 +111,7 @@ typedef struct IntoClause
 
 	RangeVar   *rel;			/* target relation name */
 	List	   *colNames;		/* column names to assign, or NIL */
+	char	   *accessMethod;	/* table access method */
 	List	   *options;		/* options from WITH clause */
 	OnCommitAction onCommit;	/* what do we do at COMMIT? */
 	char	   *tableSpaceName; /* table space to use, or NULL */
diff --git a/src/test/regress/expected/create_am.out b/src/test/regress/expected/create_am.out
index 47dd885c4e..a4094ca3f1 100644
--- a/src/test/regress/expected/create_am.out
+++ b/src/test/regress/expected/create_am.out
@@ -99,3 +99,81 @@ HINT:  Use DROP ... CASCADE to drop the dependent objects too.
 -- Drop access method cascade
 DROP ACCESS METHOD gist2 CASCADE;
 NOTICE:  drop cascades to index grect2ind2
+-- Create a heap2 table am handler with heapam handler
+CREATE ACCESS METHOD heap2 TYPE TABLE HANDLER heap_tableam_handler;
+SELECT * FROM pg_am where amtype = 't';
+ amname |      amhandler       | amtype 
+--------+----------------------+--------
+ heap   | heap_tableam_handler | t
+ heap2  | heap_tableam_handler | t
+(2 rows)
+
+CREATE TABLE tbl_heap2(f1 int, f2 char(100)) using heap2;
+INSERT INTO tbl_heap2 VALUES(generate_series(1,10), 'Test series');
+SELECT count(*) FROM tbl_heap2;
+ count 
+-------
+    10
+(1 row)
+
+SELECT r.relname, r.relkind, a.amname from pg_class as r, pg_am as a
+		where a.oid = r.relam AND r.relname = 'tbl_heap2';
+  relname  | relkind | amname 
+-----------+---------+--------
+ tbl_heap2 | r       | heap2
+(1 row)
+
+-- create table as using heap2
+CREATE TABLE tblas_heap2 using heap2 AS select * from tbl_heap2;
+SELECT r.relname, r.relkind, a.amname from pg_class as r, pg_am as a
+		where a.oid = r.relam AND r.relname = 'tblas_heap2';
+   relname   | relkind | amname 
+-------------+---------+--------
+ tblas_heap2 | r       | heap2
+(1 row)
+
+--
+-- select into doesn't support new syntax, so it should be
+-- default access method.
+--
+SELECT INTO tblselectinto_heap from tbl_heap2;
+SELECT r.relname, r.relkind, a.amname from pg_class as r, pg_am as a
+		where a.oid = r.relam AND r.relname = 'tblselectinto_heap';
+      relname       | relkind | amname 
+--------------------+---------+--------
+ tblselectinto_heap | r       | heap
+(1 row)
+
+DROP TABLE tblselectinto_heap;
+-- create materialized view using heap2
+CREATE MATERIALIZED VIEW mv_heap2 USING heap2 AS
+		SELECT * FROM tbl_heap2;
+SELECT r.relname, r.relkind, a.amname from pg_class as r, pg_am as a
+		where a.oid = r.relam AND r.relname = 'mv_heap2';
+ relname  | relkind | amname 
+----------+---------+--------
+ mv_heap2 | m       | heap2
+(1 row)
+
+-- Try creating the unsupported relation kinds with using syntax
+CREATE VIEW test_view USING heap2 AS SELECT * FROM tbl_heap2;
+ERROR:  syntax error at or near "USING"
+LINE 1: CREATE VIEW test_view USING heap2 AS SELECT * FROM tbl_heap2...
+                              ^
+CREATE SEQUENCE test_seq USING heap2;
+ERROR:  syntax error at or near "USING"
+LINE 1: CREATE SEQUENCE test_seq USING heap2;
+                                 ^
+-- Drop table access method, but fails as objects depends on it
+DROP ACCESS METHOD heap2;
+ERROR:  cannot drop access method heap2 because other objects depend on it
+DETAIL:  table tbl_heap2 depends on access method heap2
+table tblas_heap2 depends on access method heap2
+materialized view mv_heap2 depends on access method heap2
+HINT:  Use DROP ... CASCADE to drop the dependent objects too.
+-- Drop table access method with cascade
+DROP ACCESS METHOD heap2 CASCADE;
+NOTICE:  drop cascades to 3 other objects
+DETAIL:  drop cascades to table tbl_heap2
+drop cascades to table tblas_heap2
+drop cascades to materialized view mv_heap2
diff --git a/src/test/regress/sql/create_am.sql b/src/test/regress/sql/create_am.sql
index 3e0ac104f3..0472a60f20 100644
--- a/src/test/regress/sql/create_am.sql
+++ b/src/test/regress/sql/create_am.sql
@@ -66,3 +66,49 @@ DROP ACCESS METHOD gist2;
 
 -- Drop access method cascade
 DROP ACCESS METHOD gist2 CASCADE;
+
+-- Create a heap2 table am handler with heapam handler
+CREATE ACCESS METHOD heap2 TYPE TABLE HANDLER heap_tableam_handler;
+
+SELECT * FROM pg_am where amtype = 't';
+
+CREATE TABLE tbl_heap2(f1 int, f2 char(100)) using heap2;
+INSERT INTO tbl_heap2 VALUES(generate_series(1,10), 'Test series');
+SELECT count(*) FROM tbl_heap2;
+
+SELECT r.relname, r.relkind, a.amname from pg_class as r, pg_am as a
+		where a.oid = r.relam AND r.relname = 'tbl_heap2';
+
+-- create table as using heap2
+CREATE TABLE tblas_heap2 using heap2 AS select * from tbl_heap2;
+SELECT r.relname, r.relkind, a.amname from pg_class as r, pg_am as a
+		where a.oid = r.relam AND r.relname = 'tblas_heap2';
+
+--
+-- select into doesn't support new syntax, so it should be
+-- default access method.
+--
+SELECT INTO tblselectinto_heap from tbl_heap2;
+SELECT r.relname, r.relkind, a.amname from pg_class as r, pg_am as a
+		where a.oid = r.relam AND r.relname = 'tblselectinto_heap';
+
+DROP TABLE tblselectinto_heap;
+
+-- create materialized view using heap2
+CREATE MATERIALIZED VIEW mv_heap2 USING heap2 AS
+		SELECT * FROM tbl_heap2;
+
+SELECT r.relname, r.relkind, a.amname from pg_class as r, pg_am as a
+		where a.oid = r.relam AND r.relname = 'mv_heap2';
+
+-- Try creating the unsupported relation kinds with using syntax
+CREATE VIEW test_view USING heap2 AS SELECT * FROM tbl_heap2;
+
+CREATE SEQUENCE test_seq USING heap2;
+
+
+-- Drop table access method, but fails as objects depends on it
+DROP ACCESS METHOD heap2;
+
+-- Drop table access method with cascade
+DROP ACCESS METHOD heap2 CASCADE;
-- 
2.18.0.windows.1

