diff --git a/src/bin/pg_upgrade/Makefile b/src/bin/pg_upgrade/Makefile
index d9c8145..153622d 100644
--- a/src/bin/pg_upgrade/Makefile
+++ b/src/bin/pg_upgrade/Makefile
@@ -11,8 +11,11 @@ OBJS = check.o controldata.o dump.o exec.o file.o function.o info.o \
        option.o page.o parallel.o pg_upgrade.o relfilenode.o server.o \
        tablespace.o util.o version.o $(WIN32RES)
 
+SUBDIRS = plugins
+
 override CPPFLAGS := -DDLSUFFIX=\"$(DLSUFFIX)\" -I$(srcdir) -I$(libpq_srcdir) $(CPPFLAGS)
 
+$(recurse)
 
 all: pg_upgrade
 
diff --git a/src/bin/pg_upgrade/file.c b/src/bin/pg_upgrade/file.c
index 9357ad8..4c4b955 100644
--- a/src/bin/pg_upgrade/file.c
+++ b/src/bin/pg_upgrade/file.c
@@ -14,6 +14,7 @@
 #include <fcntl.h>
 
 
+const static char *pg_copy_file(const char *src, const char *dst, bool force);
 
 #ifndef WIN32
 static int	copy_file(const char *fromfile, const char *tofile, bool force);
@@ -22,6 +23,8 @@ static int	win32_pghardlink(const char *src, const char *dst);
 #endif
 
 
+const char *convertVMFile(pageCnvCtx *pageConverter, const char *src, const char *dst);
+
 /*
  * copyAndUpdateFile()
  *
@@ -30,19 +33,11 @@ static int	win32_pghardlink(const char *src, const char *dst);
  */
 const char *
 copyAndUpdateFile(pageCnvCtx *pageConverter,
-				  const char *src, const char *dst, bool force)
+				  const char *src, const char *dst, const char *type_suffix,
+				  bool force)
 {
 	if (pageConverter == NULL)
-	{
-#ifndef WIN32
-		if (copy_file(src, dst, force) == -1)
-#else
-		if (CopyFile(src, dst, !force) == 0)
-#endif
-			return getErrorText();
-		else
-			return NULL;
-	}
+		return pg_copy_file(src, dst, force);
 	else
 	{
 		/*
@@ -55,12 +50,18 @@ copyAndUpdateFile(pageCnvCtx *pageConverter,
 		 * file and call the convertPage plugin function.
 		 */
 
-#ifdef PAGE_CONVERSION
-		if (pageConverter->convertFile)
-			return pageConverter->convertFile(pageConverter->pluginData,
-											  dst, src);
+		/* Process visibility map */
+		if (strcmp(type_suffix, "_vm") == 0)
+		{
+			if (pageConverter->convertVMFile == NULL)
+				return pg_copy_file(src, dst, force);
+			else
+				return convertVMFile(pageConverter, src, dst);
+		}
+		/* Process relation file */
+		else if (type_suffix == NULL && pageConverter->convertFile)
+			return pageConverter->convertFile(pageConverter->pluginData, dst, src);
 		else
-#endif
 		{
 			int			src_fd;
 			int			dstfd;
@@ -79,10 +80,9 @@ copyAndUpdateFile(pageCnvCtx *pageConverter,
 
 			while ((bytesRead = read(src_fd, buf, BLCKSZ)) == BLCKSZ)
 			{
-#ifdef PAGE_CONVERSION
-				if ((msg = pageConverter->convertPage(pageConverter->pluginData, buf, buf)) != NULL)
-					break;
-#endif
+				if (pageConverter->convertPage)
+					if ((msg = pageConverter->convertPage(pageConverter->pluginData, buf, buf)) != NULL)
+						break;
 				if (write(dstfd, buf, BLCKSZ) != BLCKSZ)
 				{
 					msg = "could not write new page to destination";
@@ -103,7 +103,6 @@ copyAndUpdateFile(pageCnvCtx *pageConverter,
 	}
 }
 
-
 /*
  * linkAndUpdateFile()
  *
@@ -115,15 +114,29 @@ copyAndUpdateFile(pageCnvCtx *pageConverter,
  */
 const char *
 linkAndUpdateFile(pageCnvCtx *pageConverter,
-				  const char *src, const char *dst)
+				  const char *src, const char *dst, const char *type_suffix,
+				  bool rewrite_vm)
 {
-	if (pageConverter != NULL)
+	if (convertRelfile(pageConverter))
 		return "Cannot in-place update this cluster, page-by-page conversion is required";
 
+	/* Convert page actually using additional pageConverter */
+	if (strcmp(type_suffix, "_vm") == 0)
+		return convertVMFile(pageConverter, src, dst);
+
 	if (pg_link_file(src, dst) == -1)
 		return getErrorText();
 	else
 		return NULL;
+
+/*
+	if (rewrite_vm)
+		return rewriteVisibilitymap(src, dst, true);
+	else if (pg_link_file(src, dst) == -1)
+		return getErrorText();
+	else
+		return NULL;
+*/
 }
 
 
@@ -204,6 +217,28 @@ copy_file(const char *srcfile, const char *dstfile, bool force)
 }
 #endif
 
+/*
+ * convertVMFile()
+ *
+ * This function fills information about checksum for rewriting VM, and execute
+ * plugin function.
+ */
+const char *
+convertVMFile(pageCnvCtx *pageConverter,  const char *src, const char *dst)
+
+{
+	bool	checksum_enabled = false;
+
+	/* Check whether checksum is enabled on both cluster */
+	if (old_cluster.controldata.data_checksum_version != 0 &&
+		new_cluster.controldata.data_checksum_version != 0)
+		checksum_enabled = true;
+
+	/* Convert visibility map file */
+	pageConverter->pluginData = (void *) &checksum_enabled;
+
+	return pageConverter->convertVMFile(pageConverter->pluginData, dst, src);
+}
 
 void
 check_hard_link(void)
@@ -224,6 +259,20 @@ check_hard_link(void)
 	unlink(new_link_file);
 }
 
+const static char *
+pg_copy_file(const char *src, const char *dst, bool force)
+{
+
+#ifndef WIN32
+		if (copy_file(src, dst, force) == -1)
+#else
+		if (CopyFile(src, dst, !force) == 0)
+#endif
+			return getErrorText();
+		else
+			return NULL;
+}
+
 #ifdef WIN32
 static int
 win32_pghardlink(const char *src, const char *dst)
diff --git a/src/bin/pg_upgrade/page.c b/src/bin/pg_upgrade/page.c
index e5686e5..423deae 100644
--- a/src/bin/pg_upgrade/page.c
+++ b/src/bin/pg_upgrade/page.c
@@ -13,15 +13,31 @@
 
 #include "storage/bufpage.h"
 
-
-#ifdef PAGE_CONVERSION
+#include <dlfcn.h>
 
 
 static void getPageVersion(
 			   uint16 *version, const char *pathName);
 static pageCnvCtx *loadConverterPlugin(
 					uint16 newPageVersion, uint16 oldPageVersion);
+static pageCnvCtx *loadAdditionalConverterPlugin(pageCnvCtx *converter,
+										const char *pluginName);
+static void initializePageConverter(pageCnvCtx *converter);
 
+/*
+ * initializePageConverter()
+ *
+ * Initialize pageConverter struct.
+ */
+static void
+initializePageConverter(pageCnvCtx *converter)
+{
+	converter->startup = NULL;
+	converter->convertFile = NULL;
+	converter->convertVMFile = NULL;
+	converter->convertPage = NULL;
+	converter->shutdown = NULL;
+}
 
 /*
  * setupPageConverter()
@@ -34,16 +50,16 @@ static pageCnvCtx *loadConverterPlugin(
  *	returns a NULL pageCnvCtx pointer to indicate that page-by-page conversion
  *	is not required.
  */
-pageCnvCtx *
+const pageCnvCtx *
 setupPageConverter(void)
 {
 	uint16		oldPageVersion;
 	uint16		newPageVersion;
-	pageCnvCtx *converter;
-	const char *msg;
+	pageCnvCtx *converter = NULL;
 	char		dstName[MAXPGPATH];
 	char		srcName[MAXPGPATH];
 
+
 	snprintf(dstName, sizeof(dstName), "%s/global/%u", new_cluster.pgdata,
 			 new_cluster.pg_database_oid);
 	snprintf(srcName, sizeof(srcName), "%s/global/%u", old_cluster.pgdata,
@@ -63,16 +79,33 @@ setupPageConverter(void)
 		 * plugin that knows how to convert from the old page layout to the
 		 * new page layout.
 		 */
-
 		if ((converter = loadConverterPlugin(newPageVersion, oldPageVersion)) == NULL)
 			pg_fatal("could not find plugin to convert from old page layout to new page layout\n");
+	}
 
-		return converter;
+
+	/*
+	 * Do we need to rewrite visibilitymap? if yes, load specific converter libarary.
+	 */
+	if (old_cluster.controldata.cat_ver < VISIBILITY_MAP_FROZEN_BIT_CAT_VER &&
+		new_cluster.controldata.cat_ver >= VISIBILITY_MAP_FROZEN_BIT_CAT_VER)
+	{
+		char	libpath[MAXPGPATH];
+		char	pluginName[MAXPGPATH];
+
+		get_lib_path(mypath, libpath);
+		snprintf(pluginName, sizeof(pluginName), "%s/plugins/convertLayoutVM_add_frozenbit%s",
+				 libpath, DLSUFFIX);
+
+		if ((converter = loadAdditionalConverterPlugin(converter, pluginName)) == NULL)
+			pg_fatal("could not find additional plugin to convert from old page layout to new page layout\n");
 	}
-	else
-		return NULL;
-}
 
+	if (converter)
+		return converter;
+
+	return NULL;
+}
 
 /*
  * getPageVersion()
@@ -118,8 +151,8 @@ getPageVersion(uint16 *version, const char *pathName)
 static pageCnvCtx *
 loadConverterPlugin(uint16 newPageVersion, uint16 oldPageVersion)
 {
-	char		pluginName[MAXPGPATH];
 	void	   *plugin;
+	char		pluginName[MAXPGPATH];
 
 	/*
 	 * Try to find a plugin that can convert pages of oldPageVersion into
@@ -135,19 +168,19 @@ loadConverterPlugin(uint16 newPageVersion, uint16 oldPageVersion)
 	snprintf(pluginName, sizeof(pluginName), "./plugins/convertLayout_%d_to_%d%s",
 			 oldPageVersion, newPageVersion, DLSUFFIX);
 
-	if ((plugin = pg_dlopen(pluginName)) == NULL)
+	if ((plugin = dlopen(pluginName, RTLD_NOW | RTLD_GLOBAL)) == NULL)
 		return NULL;
 	else
 	{
 		pageCnvCtx *result = (pageCnvCtx *) pg_malloc(sizeof(*result));
 
-		result->old.PageVersion = oldPageVersion;
-		result->new.PageVersion = newPageVersion;
+		result->oldPageVersion = oldPageVersion;
+		result->newPageVersion = newPageVersion;
 
-		result->startup = (pluginStartup) pg_dlsym(plugin, "init");
-		result->convertFile = (pluginConvertFile) pg_dlsym(plugin, "convertFile");
-		result->convertPage = (pluginConvertPage) pg_dlsym(plugin, "convertPage");
-		result->shutdown = (pluginShutdown) pg_dlsym(plugin, "fini");
+		result->startup = (pluginStartup) dlsym(plugin, "init");
+		result->convertFile = (pluginConvertFile) dlsym(plugin, "convertFile");
+		result->convertPage = (pluginConvertPage) dlsym(plugin, "convertPage");
+		result->shutdown = (pluginShutdown) dlsym(plugin, "fini");
 		result->pluginData = NULL;
 
 		/*
@@ -161,4 +194,29 @@ loadConverterPlugin(uint16 newPageVersion, uint16 oldPageVersion)
 	}
 }
 
-#endif
+/*
+ * loadAdditionalConverterPlugin()
+ *
+ * This function loads a additional page-converter plugin library for forks
+ * and grabs a pointer to each of the (interesting) functions provided by that
+ * plugin. converter is NULL means that we didn't load main page-converter and
+ * need to allocate page-converter struct.
+ */
+static pageCnvCtx *
+loadAdditionalConverterPlugin(pageCnvCtx *converter, const char *pluginName)
+{
+	void	*plugin;
+
+	if (!converter)
+	{
+		converter = (pageCnvCtx *) pg_malloc(sizeof(pageCnvCtx));
+		initializePageConverter(converter);
+	}
+
+	if ((plugin = dlopen(pluginName, RTLD_NOW | RTLD_GLOBAL)) == NULL)
+		return NULL;
+	else
+		converter->convertVMFile = (pluginConvertFile) dlsym(plugin, "convertVMFile");
+
+	return converter;
+}
diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c
index 984c395..71c69db 100644
--- a/src/bin/pg_upgrade/pg_upgrade.c
+++ b/src/bin/pg_upgrade/pg_upgrade.c
@@ -54,6 +54,7 @@ static void cleanup(void);
 ClusterInfo old_cluster,
 			new_cluster;
 OSInfo		os_info;
+char		mypath[MAXPGPATH];
 
 char	   *output_files[] = {
 	SERVER_LOG_FILE,
@@ -76,6 +77,9 @@ main(int argc, char **argv)
 
 	parseCommandLine(argc, argv);
 
+	if (find_my_exec(argv[0], mypath) != 0)
+		pg_fatal("could not find own program executable\n");
+
 	get_restricted_token(os_info.progname);
 
 	adjust_data_dir(&old_cluster);
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index bc733c4..4a500c5 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -110,6 +110,10 @@ extern char *output_files[];
 #define VISIBILITY_MAP_CRASHSAFE_CAT_VER 201107031
 
 /*
+ * The format of visibility map is changed with this 9.6 commit,
+ */
+#define VISIBILITY_MAP_FROZEN_BIT_CAT_VER 201602021
+/*
  * pg_multixact format changed in 9.3 commit 0ac5ad5134f2769ccbaefec73844f85,
  * ("Improve concurrency of foreign key locking") which also updated catalog
  * version to this value.  pg_upgrade behavior depends on whether old and new
@@ -322,6 +326,7 @@ extern UserOpts user_opts;
 extern ClusterInfo old_cluster,
 			new_cluster;
 extern OSInfo os_info;
+extern char mypath[MAXPGPATH];
 
 
 /* check.c */
@@ -364,7 +369,6 @@ bool		pid_lock_file_exists(const char *datadir);
 
 /* file.c */
 
-#ifdef PAGE_CONVERSION
 typedef const char *(*pluginStartup) (uint16 migratorVersion,
 								uint16 *pluginVersion, uint16 newPageVersion,
 								   uint16 oldPageVersion, void **pluginData);
@@ -383,21 +387,24 @@ typedef struct
 	pluginStartup startup;		/* Pointer to plugin's startup function */
 	pluginConvertFile convertFile;		/* Pointer to plugin's file converter
 										 * function */
+	pluginConvertFile convertVMFile;	/* Pointer to plugin's VM file converter
+										   function */
 	pluginConvertPage convertPage;		/* Pointer to plugin's page converter
 										 * function */
 	pluginShutdown shutdown;	/* Pointer to plugin's shutdown function */
 } pageCnvCtx;
 
 const pageCnvCtx *setupPageConverter(void);
-#else
-/* dummy */
-typedef void *pageCnvCtx;
-#endif
+
+#define convertRelfile(pageConverter) \
+	((pageConverter) && \
+	((pageCnvCtx *)(pageConverter)->convertFile || \
+	 (pageCnvCtx *)(pageConverter)->convertPage))
 
 const char *copyAndUpdateFile(pageCnvCtx *pageConverter, const char *src,
-				  const char *dst, bool force);
+							  const char *dst, const char *type_suffix, bool force);
 const char *linkAndUpdateFile(pageCnvCtx *pageConverter, const char *src,
-				  const char *dst);
+							  const char *dst, const char *type_suffix, bool rewrite_vm);
 
 void		check_hard_link(void);
 FILE	   *fopen_priv(const char *path, const char *mode);
diff --git a/src/bin/pg_upgrade/plugins/Makefile b/src/bin/pg_upgrade/plugins/Makefile
new file mode 100644
index 0000000..fb3f941
--- /dev/null
+++ b/src/bin/pg_upgrade/plugins/Makefile
@@ -0,0 +1,32 @@
+# src/bin/pg_upgrade/plugins/Makefile
+
+PGFILEDESC = "page conversion plugins for pg_upgrade"
+
+subdir = src/bin/pg_upgrade/plugins
+top_builddir = ../../../../
+include $(top_builddir)/src/Makefile.global
+
+#PG_CPPFLAGS=-I$(top_builddir)/src/bin/pg_upgrade
+override CPPFLAGS := -DDLSUFFIX=\"$(DLSUFFIX)\" -I$(srcdir) -I../ -I$(libpq_srcdir) $(CPPFLAGS)
+
+NAME = convertLayoutVM_add_frozenbit
+OBJS = convertLayoutVM_add_frozenbit.o
+plugindir = $(DESTDIR)$(libdir)/plugins
+
+all: all-shared-lib
+
+include $(top_srcdir)/src/Makefile.shlib
+
+install: all installdirs install-plugins
+
+installdirs:
+	$(MKDIR_P) '$(plugindir)'
+
+install-plugins:
+	$(INSTALL_SHLIB) $(NAME).so '$(plugindir)'
+
+uninstall:
+	rm -f '$(plugindir)/$(NAME).so'
+
+clean:
+	rm -f $(OBJS) $(NAME).so
\ No newline at end of file
diff --git a/src/bin/pg_upgrade/plugins/convertLayoutVM_add_frozenbit.c b/src/bin/pg_upgrade/plugins/convertLayoutVM_add_frozenbit.c
new file mode 100644
index 0000000..2245e30
--- /dev/null
+++ b/src/bin/pg_upgrade/plugins/convertLayoutVM_add_frozenbit.c
@@ -0,0 +1,159 @@
+/*
+ * convertLayoutVM_add_frozenbit.c
+ *
+ * Page converter plugin for Visibility Map
+ *
+ *	Copyright (c) 2016, PostgreSQL Global Development Group
+ *	src/bin/pg_upgrade/plugins.c
+ */
+
+#include "postgres_fe.h"
+
+#include "access/visibilitymap.h"
+#include "pg_upgrade.h"
+#include "storage/bufpage.h"
+#include "storage/checksum.h"
+#include "storage/checksum_impl.h"
+#include "port.h"
+
+#include <fcntl.h>
+
+/* plugin function */
+const char* convertVMFile(void *pluginData,
+							   const char *dstName, const char *srcName);
+
+static const int rewriteVisibilitymap(const char *fromfile, const char *tofile,
+									  bool checksum_enabled);
+
+/* table for fast rewriting vm file in order to add all-frozen information */
+static const uint16 rewrite_vm_table[256] = {
+	0,     1,     4,     5,     16,    17,    20,    21,    64,    65,    68,    69,    80,    81,    84,    85,
+	256,   257,   260,   261,   272,   273,   276,   277,   320,   321,   324,   325,   336,   337,   340,   341,
+	1024,  1025,  1028,  1029,  1040,  1041,  1044,  1045,  1088,  1089,  1092,  1093,  1104,  1105,  1108,  1109,
+	1280,  1281,  1284,  1285,  1296,  1297,  1300,  1301,  1344,  1345,  1348,  1349,  1360,  1361,  1364,  1365,
+	4096,  4097,  4100,  4101,  4112,  4113,  4116,  4117,  4160,  4161,  4164,  4165,  4176,  4177,  4180,  4181,
+	4352,  4353,  4356,  4357,  4368,  4369,  4372,  4373,  4416,  4417,  4420,  4421,  4432,  4433,  4436,  4437,
+	5120,  5121,  5124,  5125,  5136,  5137,  5140,  5141,  5184,  5185,  5188,  5189,  5200,  5201,  5204,  5205,
+	5376,  5377,  5380,  5381,  5392,  5393,  5396,  5397,  5440,  5441,  5444,  5445,  5456,  5457,  5460,  5461,
+	16384, 16385, 16388, 16389, 16400, 16401, 16404, 16405, 16448, 16449, 16452, 16453, 16464, 16465, 16468, 16469,
+	16640, 16641, 16644, 16645, 16656, 16657, 16660, 16661, 16704, 16705, 16708, 16709, 16720, 16721, 16724, 16725,
+	17408, 17409, 17412, 17413, 17424, 17425, 17428, 17429, 17472, 17473, 17476, 17477, 17488, 17489, 17492, 17493,
+	17664, 17665, 17668, 17669, 17680, 17681, 17684, 17685, 17728, 17729, 17732, 17733, 17744, 17745, 17748, 17749,
+	20480, 20481, 20484, 20485, 20496, 20497, 20500, 20501, 20544, 20545, 20548, 20549, 20560, 20561, 20564, 20565,
+	20736, 20737, 20740, 20741, 20752, 20753, 20756, 20757, 20800, 20801, 20804, 20805, 20816, 20817, 20820, 20821,
+	21504, 21505, 21508, 21509, 21520, 21521, 21524, 21525, 21568, 21569, 21572, 21573, 21584, 21585, 21588, 21589,
+	21760, 21761, 21764, 21765, 21776, 21777, 21780, 21781, 21824, 21825, 21828, 21829, 21840, 21841, 21844, 21845
+};
+
+/*
+ * convertVMFile()
+ *
+ * This plugin function is loaded by main procedure if required.
+ * pluginData has the information about that checksum are enabled on both
+ * cluster or not. If rewriting function failed then return error messages.
+ */
+const char *
+convertVMFile(void *pluginData, const char *dstName, const char *srcName)
+{
+	bool	checksum_enabled;
+
+	checksum_enabled = *(bool *)pluginData;
+
+	if (rewriteVisibilitymap(srcName, dstName, checksum_enabled) == -1)
+	{
+#ifdef WIN32
+		_dosmaperr(GetLastError());
+#endif
+		return strdup(strerror(errno));
+	}
+
+	return NULL;
+}
+
+/*
+ * rewriteVisibilitymap()
+ *
+ * Copies a visibility map file while adding all-frozen bit(0) into each bit.
+ */
+static const int
+rewriteVisibilitymap(const char *fromfile, const char *tofile, bool checksum_enabled)
+{
+	int			src_fd = 0;
+	int			dst_fd = 0;
+	char		buffer[BLCKSZ];
+	ssize_t 	bytesRead;
+	int			rewriteVmBytesPerPage = (BLCKSZ - SizeOfPageHeaderData) / 2;
+	BlockNumber	blkno = 0;
+
+	/* Reset errno */
+	errno = 0;
+
+	if ((fromfile == NULL) || (tofile == NULL))
+		goto err;
+
+	if ((src_fd = open(fromfile, O_RDONLY, 0)) < 0)
+		goto err;
+
+	if ((dst_fd = open(tofile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
+		goto err;
+
+	/* Perform data rewriting per page */
+	while ((bytesRead = read(src_fd, buffer, BLCKSZ)) == BLCKSZ)
+	{
+		char	*cur, *end, *blkend;
+		PageHeaderData	pageheader;
+		uint16	vm_bits;
+
+		/* Save the page header data */
+		memcpy(&pageheader, buffer, SizeOfPageHeaderData);
+
+		cur = buffer + SizeOfPageHeaderData;
+		end = buffer + SizeOfPageHeaderData + rewriteVmBytesPerPage;
+		blkend = buffer + bytesRead;
+
+		while (blkend >= end)
+		{
+			char	vmbuf[BLCKSZ];
+			char	*vmtmp = vmbuf;
+
+			/* Copy page header in advance */
+			memcpy(vmbuf, &pageheader, SizeOfPageHeaderData);
+
+			vmtmp += SizeOfPageHeaderData;
+
+			/* Rewrite visibility map bit one by one */
+			while (end > cur)
+			{
+				/* Write rewritten bit from table and its string representation */
+				vm_bits = rewrite_vm_table[(uint8) *cur];
+				memcpy(vmtmp, &vm_bits, BITS_PER_HEAPBLOCK);
+
+				cur++;
+				vmtmp += BITS_PER_HEAPBLOCK;
+			}
+
+			/* Set new checksum for a visibility map page, If enabled */
+			if (checksum_enabled)
+				((PageHeader) vmbuf)->pd_checksum = pg_checksum_page(vmbuf, blkno);
+
+			if (write(dst_fd, vmbuf, BLCKSZ) != BLCKSZ)
+			{
+					if (errno == 0)
+						errno = ENOSPC;
+					goto err;
+			}
+
+			end += rewriteVmBytesPerPage;
+			blkno++;
+		}
+	}
+
+err:
+	if (src_fd != 0)
+		close(src_fd);
+
+	if (dst_fd != 0)
+		close(dst_fd);
+
+	return (errno == 0) ? 0 : -1;
+}
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index c059c5b..c4bf77b 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -18,7 +18,7 @@
 static void transfer_single_new_db(pageCnvCtx *pageConverter,
 					   FileNameMap *maps, int size, char *old_tablespace);
 static void transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
-				 const char *suffix);
+							 const char *suffix);
 
 
 /*
@@ -82,6 +82,10 @@ transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
 {
 	int			old_dbnum,
 				new_dbnum;
+	pageCnvCtx *pageConverter = NULL;
+
+	/* Set up page-converter and load necessary plugin */
+	pageConverter = (pageCnvCtx *) setupPageConverter();
 
 	/* Scan the old cluster databases and transfer their files */
 	for (old_dbnum = new_dbnum = 0;
@@ -92,7 +96,6 @@ transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
 				   *new_db = NULL;
 		FileNameMap *mappings;
 		int			n_maps;
-		pageCnvCtx *pageConverter = NULL;
 
 		/*
 		 * Advance past any databases that exist in the new cluster but not in
@@ -115,10 +118,6 @@ transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
 		if (n_maps)
 		{
 			print_maps(mappings, n_maps, new_db->db_name);
-
-#ifdef PAGE_CONVERSION
-			pageConverter = setupPageConverter();
-#endif
 			transfer_single_new_db(pageConverter, mappings, n_maps,
 								   old_tablespace);
 		}
@@ -144,15 +143,9 @@ get_pg_database_relfilenode(ClusterInfo *cluster)
 	int			i_relfile;
 
 	res = executeQueryOrDie(conn,
-							"SELECT c.relname, c.relfilenode "
-							"FROM	pg_catalog.pg_class c, "
-							"		pg_catalog.pg_namespace n "
-							"WHERE	c.relnamespace = n.oid AND "
-							"		n.nspname = 'pg_catalog' AND "
-							"		c.relname = 'pg_database' "
-							"ORDER BY c.relname");
-
-	i_relfile = PQfnumber(res, "relfilenode");
+							"SELECT pg_relation_filenode('pg_database') AS filenode");
+
+	i_relfile = PQfnumber(res, "filenode");
 	cluster->pg_database_oid = atooid(PQgetvalue(res, 0, i_relfile));
 
 	PQclear(res);
@@ -268,7 +261,7 @@ transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
 		/* Copying files might take some time, so give feedback. */
 		pg_log(PG_STATUS, "%s", old_file);
 
-		if ((user_opts.transfer_mode == TRANSFER_MODE_LINK) && (pageConverter != NULL))
+		if ((user_opts.transfer_mode == TRANSFER_MODE_LINK) && (convertRelfile(pageConverter)))
 			pg_fatal("This upgrade requires page-by-page conversion, "
 					 "you must use copy mode instead of link mode.\n");
 
@@ -276,7 +269,7 @@ transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
 		{
 			pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\"\n", old_file, new_file);
 
-			if ((msg = copyAndUpdateFile(pageConverter, old_file, new_file, true)) != NULL)
+			if ((msg = copyAndUpdateFile(pageConverter, old_file, new_file, type_suffix, true)) != NULL)
 				pg_fatal("error while copying relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
 						 map->nspname, map->relname, old_file, new_file, msg);
 		}
@@ -284,7 +277,7 @@ transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
 		{
 			pg_log(PG_VERBOSE, "linking \"%s\" to \"%s\"\n", old_file, new_file);
 
-			if ((msg = linkAndUpdateFile(pageConverter, old_file, new_file)) != NULL)
+			if ((msg = linkAndUpdateFile(pageConverter, old_file, new_file, type_suffix, true)) != NULL)
 				pg_fatal("error while creating link for relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
 						 map->nspname, map->relname, old_file, new_file, msg);
 		}
diff --git a/src/bin/pg_upgrade/test.sh b/src/bin/pg_upgrade/test.sh
index ba79fb3..cd9b17e 100644
--- a/src/bin/pg_upgrade/test.sh
+++ b/src/bin/pg_upgrade/test.sh
@@ -174,6 +174,11 @@ if "$MAKE" -C "$oldsrc" installcheck; then
 		mv "$temp_root"/dump1.sql "$temp_root"/dump1.sql.orig
 		sed "s;$oldsrc;$newsrc;g" "$temp_root"/dump1.sql.orig >"$temp_root"/dump1.sql
 	fi
+
+	vm_sql="SELECT c.relname, c.relallvisible FROM pg_class as c, pg_namespace as n WHERE c.relnamespace = n.oid AND n.nspname NOT IN ('information_schema', 'pg_toast', 'pg_catalog') ORDER BY c.relname;"
+	# Test for rewriting visibility map
+	vacuumdb -d regression || visibilitymap_vacuum1_status=$?
+	psql -d regression -c "$vm_sql" > "$temp_root"/vm_test1.txt || visibilitymap_test1_status=$?
 else
 	make_installcheck_status=$?
 fi
@@ -188,6 +193,14 @@ if [ -n "$pg_dumpall1_status" ]; then
 	echo "pg_dumpall of pre-upgrade database cluster failed"
 	exit 1
 fi
+if [ -n "$visibilitymap_vacuum1_status" ];then
+	echo "VACUUM of pre-upgrade database cluster for visibility map test failed"
+	exit 1
+fi
+if [ -n "$visibilitymap_test1_status" ];then
+	echo "SELECT of pre-upgrade database cluster for visibility map test failed"
+	exit 1
+fi
 
 PGDATA=$BASE_PGDATA
 
@@ -203,6 +216,8 @@ case $testhost in
 esac
 
 pg_dumpall -f "$temp_root"/dump2.sql || pg_dumpall2_status=$?
+vacuumdb -d regression || visibilitymap_vacuum2_status=$?
+psql -d regression -c "$vm_sql" > "$temp_root"/vm_test2.txt || visibilitymap_test2_status=$?
 pg_ctl -m fast stop
 
 # no need to echo commands anymore
@@ -214,11 +229,26 @@ if [ -n "$pg_dumpall2_status" ]; then
 	exit 1
 fi
 
+if [ -n "$visibilitymap_vacuum2_status" ];then
+	echo "VACUUM of post-upgrade database cluster for visibility map test failed"
+	exit 1
+fi
+
+if [ -n "$visibilitymap_test2_status" ];then
+	echo "SELECT of post-upgrade database cluster for visibility map test failed"
+	exit 1
+fi
+
 case $testhost in
 	MINGW*)	cmd /c delete_old_cluster.bat ;;
 	*)	    sh ./delete_old_cluster.sh ;;
 esac
 
+if ! diff "$temp_root"/vm_test1.txt "$temp_root"/vm_test2.txt >/dev/null; then
+	echo "Visibility map rewriting test failed"
+	exit 1
+fi
+
 if diff "$temp_root"/dump1.sql "$temp_root"/dump2.sql >/dev/null; then
 	echo PASSED
 	exit 0
