diff --git a/src/backend/access/gist/gistvacuum.c b/src/backend/access/gist/gistvacuum.c
index bf754ea..31e2571 100644
--- a/src/backend/access/gist/gistvacuum.c
+++ b/src/backend/access/gist/gistvacuum.c
@@ -169,6 +169,7 @@ gistvacuumscan(IndexVacuumInfo *info, GistBulkDeleteResult *stats,
 	BlockNumber num_pages;
 	bool		needLock;
 	BlockNumber blkno;
+	MemoryContext oldctx = NULL;
 
 	/*
 	 * Reset counts that will be incremented during the scan; needed in case
@@ -179,8 +180,17 @@ gistvacuumscan(IndexVacuumInfo *info, GistBulkDeleteResult *stats,
 	stats->stats.pages_deleted = 0;
 	stats->stats.pages_free = 0;
 	MemoryContextReset(stats->page_set_context);
+
+	/*
+	 * Create the internal_page_set and the empty_leaf_set in the
+	 * page_set_context.  Internally, integer set will remember this context
+	 * so the subsequent allocations for these integer sets will be done from
+	 * the same context.
+	 */
+	oldctx = MemoryContextSwitchTo(stats->page_set_context);
 	stats->internal_page_set = intset_create();
 	stats->empty_leaf_set = intset_create();
+	MemoryContextSwitchTo(oldctx);
 
 	/* Set up info to pass down to gistvacuumpage */
 	vstate.info = info;
