diff --git a/src/backend/access/spgist/spgscan.c b/src/backend/access/spgist/spgscan.c
index 854032d..76c7513 100644
--- a/src/backend/access/spgist/spgscan.c
+++ b/src/backend/access/spgist/spgscan.c
@@ -194,6 +194,9 @@ spgbeginscan(Relation rel, int keysz, int orderbysz)
 	so->tempCxt = AllocSetContextCreate(CurrentMemoryContext,
 										"SP-GiST search temporary context",
 										ALLOCSET_DEFAULT_SIZES);
+	so->traversalCxt = AllocSetContextCreate(CurrentMemoryContext,
+											 "SP-GiST traversal temporary context",
+											 ALLOCSET_DEFAULT_SIZES);
 
 	/* Set up indexTupDesc and xs_hitupdesc in case it's an index-only scan */
 	so->indexTupDesc = scan->xs_hitupdesc = RelationGetDescr(rel);
@@ -209,6 +212,9 @@ spgrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
 {
 	SpGistScanOpaque so = (SpGistScanOpaque) scan->opaque;
 
+	/* clear traversal context before proceeding to the next scan */
+	MemoryContextReset(so->traversalCxt);
+
 	/* copy scankeys into local storage */
 	if (scankey && scan->numberOfKeys > 0)
 	{
@@ -229,6 +235,7 @@ spgendscan(IndexScanDesc scan)
 	SpGistScanOpaque so = (SpGistScanOpaque) scan->opaque;
 
 	MemoryContextDelete(so->tempCxt);
+	MemoryContextDelete(so->traversalCxt);
 }
 
 /*
@@ -463,7 +470,7 @@ redirect:
 			in.scankeys = so->keyData;
 			in.nkeys = so->numberOfKeys;
 			in.reconstructedValue = stackEntry->reconstructedValue;
-			in.traversalMemoryContext = oldCtx;
+			in.traversalMemoryContext = so->traversalCxt;
 			in.traversalValue = stackEntry->traversalValue;
 			in.level = stackEntry->level;
 			in.returnData = so->want_itup;
diff --git a/src/include/access/spgist_private.h b/src/include/access/spgist_private.h
index 6d5f1c6..767348b 100644
--- a/src/include/access/spgist_private.h
+++ b/src/include/access/spgist_private.h
@@ -137,6 +137,7 @@ typedef struct SpGistScanOpaqueData
 {
 	SpGistState state;			/* see above */
 	MemoryContext tempCxt;		/* short-lived memory context */
+	MemoryContext traversalCxt;	/* traversal memory context for inner consistent method */
 
 	/* Control flags showing whether to search nulls and/or non-nulls */
 	bool		searchNulls;	/* scan matches (all) null entries */
