Anonymous avatar Anonymous committed 3b157cf

pg_dump: Remove global Archive pointer.

Instead, everything that needs the Archive object now gets it as a
parameter. This is necessary infrastructure for parallel pg_dump,
but is also amply justified by the ugliness of the current code
(though a lot more than this is needed to fix that problem).

Comments (0)

Files changed (4)

src/bin/pg_dump/common.c

  *	  Collect information about all potentially dumpable objects
  */
 TableInfo *
-getSchemaData(int *numTablesPtr)
+getSchemaData(Archive *fout, int *numTablesPtr)
 {
 	ExtensionInfo *extinfo;
 	InhInfo    *inhinfo;
 
 	if (g_verbose)
 		write_msg(NULL, "reading schemas\n");
-	getNamespaces(&numNamespaces);
+	getNamespaces(fout, &numNamespaces);
 
 	/*
 	 * getTables should be done as soon as possible, so as to minimize the
 	 */
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined tables\n");
-	tblinfo = getTables(&numTables);
+	tblinfo = getTables(fout, &numTables);
 	tblinfoindex = buildIndexArray(tblinfo, numTables, sizeof(TableInfo));
 
 	if (g_verbose)
 		write_msg(NULL, "reading extensions\n");
-	extinfo = getExtensions(&numExtensions);
+	extinfo = getExtensions(fout, &numExtensions);
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined functions\n");
-	funinfo = getFuncs(&numFuncs);
+	funinfo = getFuncs(fout, &numFuncs);
 	funinfoindex = buildIndexArray(funinfo, numFuncs, sizeof(FuncInfo));
 
 	/* this must be after getTables and getFuncs */
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined types\n");
-	typinfo = getTypes(&numTypes);
+	typinfo = getTypes(fout, &numTypes);
 	typinfoindex = buildIndexArray(typinfo, numTypes, sizeof(TypeInfo));
 
 	/* this must be after getFuncs, too */
 	if (g_verbose)
 		write_msg(NULL, "reading procedural languages\n");
-	getProcLangs(&numProcLangs);
+	getProcLangs(fout, &numProcLangs);
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined aggregate functions\n");
-	getAggregates(&numAggregates);
+	getAggregates(fout, &numAggregates);
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined operators\n");
-	oprinfo = getOperators(&numOperators);
+	oprinfo = getOperators(fout, &numOperators);
 	oprinfoindex = buildIndexArray(oprinfo, numOperators, sizeof(OprInfo));
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined operator classes\n");
-	getOpclasses(&numOpclasses);
+	getOpclasses(fout, &numOpclasses);
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined operator families\n");
-	getOpfamilies(&numOpfamilies);
+	getOpfamilies(fout, &numOpfamilies);
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined text search parsers\n");
-	getTSParsers(&numTSParsers);
+	getTSParsers(fout, &numTSParsers);
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined text search templates\n");
-	getTSTemplates(&numTSTemplates);
+	getTSTemplates(fout, &numTSTemplates);
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined text search dictionaries\n");
-	getTSDictionaries(&numTSDicts);
+	getTSDictionaries(fout, &numTSDicts);
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined text search configurations\n");
-	getTSConfigurations(&numTSConfigs);
+	getTSConfigurations(fout, &numTSConfigs);
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined foreign-data wrappers\n");
-	getForeignDataWrappers(&numForeignDataWrappers);
+	getForeignDataWrappers(fout, &numForeignDataWrappers);
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined foreign servers\n");
-	getForeignServers(&numForeignServers);
+	getForeignServers(fout, &numForeignServers);
 
 	if (g_verbose)
 		write_msg(NULL, "reading default privileges\n");
-	getDefaultACLs(&numDefaultACLs);
+	getDefaultACLs(fout, &numDefaultACLs);
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined collations\n");
-	collinfo = getCollations(&numCollations);
+	collinfo = getCollations(fout, &numCollations);
 	collinfoindex = buildIndexArray(collinfo, numCollations, sizeof(CollInfo));
 
 	if (g_verbose)
 		write_msg(NULL, "reading user-defined conversions\n");
-	getConversions(&numConversions);
+	getConversions(fout, &numConversions);
 
 	if (g_verbose)
 		write_msg(NULL, "reading type casts\n");
-	getCasts(&numCasts);
+	getCasts(fout, &numCasts);
 
 	if (g_verbose)
 		write_msg(NULL, "reading table inheritance information\n");
-	inhinfo = getInherits(&numInherits);
+	inhinfo = getInherits(fout, &numInherits);
 
 	if (g_verbose)
 		write_msg(NULL, "reading rewrite rules\n");
-	getRules(&numRules);
+	getRules(fout, &numRules);
 
 	/*
 	 * Identify extension member objects and mark them as not to be dumped.
 	 */
 	if (g_verbose)
 		write_msg(NULL, "finding extension members\n");
-	getExtensionMembership(extinfo, numExtensions);
+	getExtensionMembership(fout, extinfo, numExtensions);
 
 	/* Link tables to parents, mark parents of target tables interesting */
 	if (g_verbose)
 
 	if (g_verbose)
 		write_msg(NULL, "reading column info for interesting tables\n");
-	getTableAttrs(tblinfo, numTables);
+	getTableAttrs(fout, tblinfo, numTables);
 
 	if (g_verbose)
 		write_msg(NULL, "flagging inherited columns in subtables\n");
 
 	if (g_verbose)
 		write_msg(NULL, "reading indexes\n");
-	getIndexes(tblinfo, numTables);
+	getIndexes(fout, tblinfo, numTables);
 
 	if (g_verbose)
 		write_msg(NULL, "reading constraints\n");
-	getConstraints(tblinfo, numTables);
+	getConstraints(fout, tblinfo, numTables);
 
 	if (g_verbose)
 		write_msg(NULL, "reading triggers\n");
-	getTriggers(tblinfo, numTables);
+	getTriggers(fout, tblinfo, numTables);
 
 	*numTablesPtr = numTables;
 	return tblinfo;

src/bin/pg_dump/pg_backup.h

  *	We may want to have some more user-readable data, but in the mean
  *	time this gives us some abstraction and type checking.
  */
-typedef struct _Archive
+struct Archive
 {
 	int			verbose;
 	char	   *remoteVersionStr;		/* server's version string */
 	int			n_errors;		/* number of errors (if no die) */
 
 	/* The rest is private */
-} Archive;
+};
 
 typedef int (*DataDumperPtr) (Archive *AH, void *userArg);
 

src/bin/pg_dump/pg_dump.c

 /* global decls */
 bool		g_verbose;			/* User wants verbose narration of our
 								 * activities. */
-Archive    *g_fout;				/* the script file */
 PGconn	   *g_conn;				/* the database connection */
 
 /* various user-settable parameters */
 static void setup_connection(Archive *AH, const char *dumpencoding,
 				 char *use_role);
 static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode);
-static void expand_schema_name_patterns(SimpleStringList *patterns,
+static void expand_schema_name_patterns(Archive *fout,
+							SimpleStringList *patterns,
 							SimpleOidList *oids);
 static void expand_table_name_patterns(SimpleStringList *patterns,
 						   SimpleOidList *oids);
-static NamespaceInfo *findNamespace(Oid nsoid, Oid objoid);
+static NamespaceInfo *findNamespace(Archive *fout, Oid nsoid, Oid objoid);
 static void dumpTableData(Archive *fout, TableDataInfo *tdinfo);
 static void guessConstraintInheritance(TableInfo *tblinfo, int numTables);
 static void dumpComment(Archive *fout, const char *target,
 		const char *tag, const char *nspname, const char *owner,
 		const char *acls);
 
-static void getDependencies(void);
-static void getDomainConstraints(TypeInfo *tyinfo);
+static void getDependencies(Archive *fout);
+static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
 static void getTableData(TableInfo *tblinfo, int numTables, bool oids);
 static void makeTableDataInfo(TableInfo *tbinfo, bool oids);
 static void getTableDataFKConstraints(void);
 static char *format_function_arguments(FuncInfo *finfo, char *funcargs);
-static char *format_function_arguments_old(FuncInfo *finfo, int nallargs,
+static char *format_function_arguments_old(Archive *fout,
+							  FuncInfo *finfo, int nallargs,
 							  char **allargtypes,
 							  char **argmodes,
 							  char **argnames);
-static char *format_function_signature(FuncInfo *finfo, bool honor_quotes);
-static const char *convertRegProcReference(const char *proc);
-static const char *convertOperatorReference(const char *opr);
+static char *format_function_signature(Archive *fout,
+									   FuncInfo *finfo, bool honor_quotes);
+static const char *convertRegProcReference(Archive *fout,
+										   const char *proc);
+static const char *convertOperatorReference(Archive *fout, const char *opr);
 static const char *convertTSFunction(Oid funcOid);
-static Oid	findLastBuiltinOid_V71(const char *);
+static Oid	findLastBuiltinOid_V71(Archive *fout, const char *);
 static Oid	findLastBuiltinOid_V70(void);
-static void selectSourceSchema(const char *schemaName);
-static char *getFormattedTypeName(Oid oid, OidOptions opts);
+static void selectSourceSchema(Archive *fout, const char *schemaName);
+static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
 static char *myFormatType(const char *typname, int32 typmod);
-static const char *fmtQualifiedId(const char *schema, const char *id);
-static void getBlobs(Archive *AH);
-static void dumpBlob(Archive *AH, BlobInfo *binfo);
-static int	dumpBlobs(Archive *AH, void *arg);
+static const char *fmtQualifiedId(Archive *fout,
+								  const char *schema, const char *id);
+static void getBlobs(Archive *fout);
+static void dumpBlob(Archive *fout, BlobInfo *binfo);
+static int	dumpBlobs(Archive *fout, void *arg);
 static void dumpDatabase(Archive *AH);
 static void dumpEncoding(Archive *AH);
 static void dumpStdStrings(Archive *AH);
 	RestoreOptions *ropt;
 	ArchiveFormat archiveFormat = archUnknown;
 	ArchiveMode archiveMode;
+	Archive    *fout;				/* the script file */
 
 	static int	disable_triggers = 0;
 	static int	outputNoTablespaces = 0;
 	}
 
 	/* Open the output file */
-	g_fout = CreateArchive(filename, archiveFormat, compressLevel, archiveMode);
+	fout = CreateArchive(filename, archiveFormat, compressLevel, archiveMode);
 
-	if (g_fout == NULL)
+	if (fout == NULL)
 	{
 		write_msg(NULL, "could not open output file \"%s\" for writing\n", filename);
 		exit(1);
 	}
 
 	/* Let the archiver know how noisy to be */
-	g_fout->verbose = g_verbose;
+	fout->verbose = g_verbose;
 
 	my_version = parse_version(PG_VERSION);
 	if (my_version < 0)
 	 * We allow the server to be back to 7.0, and up to any minor release of
 	 * our own major version.  (See also version check in pg_dumpall.c.)
 	 */
-	g_fout->minRemoteVersion = 70000;
-	g_fout->maxRemoteVersion = (my_version / 100) * 100 + 99;
+	fout->minRemoteVersion = 70000;
+	fout->maxRemoteVersion = (my_version / 100) * 100 + 99;
 
 	/*
 	 * Open the database using the Archiver, so it knows about it. Errors mean
 	 * death.
 	 */
-	g_conn = ConnectDatabase(g_fout, dbname, pghost, pgport,
+	g_conn = ConnectDatabase(fout, dbname, pghost, pgport,
 							 username, prompt_password);
 
-	setup_connection(g_fout, dumpencoding, use_role);
+	setup_connection(fout, dumpencoding, use_role);
 
 	/*
 	 * Disable security label support if server version < v9.1.x (prevents
 	 * access to nonexistent pg_seclabel catalog)
 	 */
-	if (g_fout->remoteVersion < 90100)
+	if (fout->remoteVersion < 90100)
 		no_security_labels = 1;
 
 	/*
 	 * Start transaction-snapshot mode transaction to dump consistent data.
 	 */
 	do_sql_command(g_conn, "BEGIN");
-	if (g_fout->remoteVersion >= 90100)
+	if (fout->remoteVersion >= 90100)
 	{
 		if (serializable_deferrable)
 			do_sql_command(g_conn,
 		do_sql_command(g_conn, "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
 
 	/* Select the appropriate subquery to convert user IDs to names */
-	if (g_fout->remoteVersion >= 80100)
+	if (fout->remoteVersion >= 80100)
 		username_subquery = "SELECT rolname FROM pg_catalog.pg_roles WHERE oid =";
-	else if (g_fout->remoteVersion >= 70300)
+	else if (fout->remoteVersion >= 70300)
 		username_subquery = "SELECT usename FROM pg_catalog.pg_user WHERE usesysid =";
 	else
 		username_subquery = "SELECT usename FROM pg_user WHERE usesysid =";
 
 	/* Find the last built-in OID, if needed */
-	if (g_fout->remoteVersion < 70300)
+	if (fout->remoteVersion < 70300)
 	{
-		if (g_fout->remoteVersion >= 70100)
-			g_last_builtin_oid = findLastBuiltinOid_V71(PQdb(g_conn));
+		if (fout->remoteVersion >= 70100)
+			g_last_builtin_oid = findLastBuiltinOid_V71(fout, PQdb(g_conn));
 		else
 			g_last_builtin_oid = findLastBuiltinOid_V70();
 		if (g_verbose)
 	/* Expand schema selection patterns into OID lists */
 	if (schema_include_patterns.head != NULL)
 	{
-		expand_schema_name_patterns(&schema_include_patterns,
+		expand_schema_name_patterns(fout, &schema_include_patterns,
 									&schema_include_oids);
 		if (schema_include_oids.head == NULL)
 		{
 			exit_nicely();
 		}
 	}
-	expand_schema_name_patterns(&schema_exclude_patterns,
+	expand_schema_name_patterns(fout, &schema_exclude_patterns,
 								&schema_exclude_oids);
 	/* non-matching exclusion patterns aren't an error */
 
 	 * Now scan the database and create DumpableObject structs for all the
 	 * objects we intend to dump.
 	 */
-	tblinfo = getSchemaData(&numTables);
+	tblinfo = getSchemaData(fout, &numTables);
 
-	if (g_fout->remoteVersion < 80400)
+	if (fout->remoteVersion < 80400)
 		guessConstraintInheritance(tblinfo, numTables);
 
 	if (!schemaOnly)
 	}
 
 	if (outputBlobs)
-		getBlobs(g_fout);
+		getBlobs(fout);
 
 	/*
 	 * Collect dependency data to assist in ordering the objects.
 	 */
-	getDependencies();
+	getDependencies(fout);
 
 	/*
 	 * Sort the objects into a safe dump order (no forward references).
 	 */
 	getDumpableObjects(&dobjs, &numObjs);
 
-	if (g_fout->remoteVersion >= 70300)
+	if (fout->remoteVersion >= 70300)
 		sortDumpableObjectsByTypeName(dobjs, numObjs);
 	else
 		sortDumpableObjectsByTypeOid(dobjs, numObjs);
 	 */
 
 	/* First the special ENCODING and STDSTRINGS entries. */
-	dumpEncoding(g_fout);
-	dumpStdStrings(g_fout);
+	dumpEncoding(fout);
+	dumpStdStrings(fout);
 
 	/* The database item is always next, unless we don't want it at all */
 	if (include_everything && !dataOnly)
-		dumpDatabase(g_fout);
+		dumpDatabase(fout);
 
 	/* Now the rearrangeable objects. */
 	for (i = 0; i < numObjs; i++)
-		dumpDumpableObject(g_fout, dobjs[i]);
+		dumpDumpableObject(fout, dobjs[i]);
 
 	/*
 	 * And finally we can do the actual output.
 
 		ropt->suppressDumpWarnings = true;		/* We've already shown them */
 
-		RestoreArchive(g_fout, ropt);
+		RestoreArchive(fout, ropt);
 	}
 
-	CloseArchive(g_fout);
+	CloseArchive(fout);
 
 	PQfinish(g_conn);
 
  * and append them to the given OID list.
  */
 static void
-expand_schema_name_patterns(SimpleStringList *patterns, SimpleOidList *oids)
+expand_schema_name_patterns(Archive *fout,
+							SimpleStringList *patterns,
+							SimpleOidList *oids)
 {
 	PQExpBuffer query;
 	PGresult   *res;
 	if (patterns->head == NULL)
 		return;					/* nothing to do */
 
-	if (g_fout->remoteVersion < 70300)
+	if (fout->remoteVersion < 70300)
 	{
 		write_msg(NULL, "server version must be at least 7.3 to use schema selection switches\n");
 		exit_nicely();
 	 * this ensures reproducible results in case the table contains regproc,
 	 * regclass, etc columns.
 	 */
-	selectSourceSchema(tbinfo->dobj.namespace->dobj.name);
+	selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
 
 	/*
 	 * If possible, specify the column list explicitly so that we have no
 	if (oids && hasoids)
 	{
 		appendPQExpBuffer(q, "COPY %s %s WITH OIDS TO stdout;",
-						  fmtQualifiedId(tbinfo->dobj.namespace->dobj.name,
+						  fmtQualifiedId(fout,
+										 tbinfo->dobj.namespace->dobj.name,
 										 classname),
 						  column_list);
 	}
 		else
 			appendPQExpBufferStr(q, "* ");
 		appendPQExpBuffer(q, "FROM %s %s) TO stdout;",
-						  fmtQualifiedId(tbinfo->dobj.namespace->dobj.name,
+						  fmtQualifiedId(fout,
+										 tbinfo->dobj.namespace->dobj.name,
 										 classname),
 						  tdinfo->filtercond);
 	}
 	else
 	{
 		appendPQExpBuffer(q, "COPY %s %s TO stdout;",
-						  fmtQualifiedId(tbinfo->dobj.namespace->dobj.name,
+						  fmtQualifiedId(fout,
+										 tbinfo->dobj.namespace->dobj.name,
 										 classname),
 						  column_list);
 	}
 	 * this ensures reproducible results in case the table contains regproc,
 	 * regclass, etc columns.
 	 */
-	selectSourceSchema(tbinfo->dobj.namespace->dobj.name);
+	selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
 
 	if (fout->remoteVersion >= 70100)
 	{
 		appendPQExpBuffer(q, "DECLARE _pg_dump_cursor CURSOR FOR "
 						  "SELECT * FROM ONLY %s",
-						  fmtQualifiedId(tbinfo->dobj.namespace->dobj.name,
+						  fmtQualifiedId(fout,
+										 tbinfo->dobj.namespace->dobj.name,
 										 classname));
 	}
 	else
 	{
 		appendPQExpBuffer(q, "DECLARE _pg_dump_cursor CURSOR FOR "
 						  "SELECT * FROM %s",
-						  fmtQualifiedId(tbinfo->dobj.namespace->dobj.name,
+						  fmtQualifiedId(fout,
+										 tbinfo->dobj.namespace->dobj.name,
 										 classname));
 	}
 	if (tdinfo->filtercond)
  *	dump the database definition
  */
 static void
-dumpDatabase(Archive *AH)
+dumpDatabase(Archive *fout)
 {
 	PQExpBuffer dbQry = createPQExpBuffer();
 	PQExpBuffer delQry = createPQExpBuffer();
 		write_msg(NULL, "saving database definition\n");
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	/* Get the database owner and parameters from pg_database */
-	if (g_fout->remoteVersion >= 80400)
+	if (fout->remoteVersion >= 80400)
 	{
 		appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
 						  "(%s datdba) AS dba, "
 						  "FROM pg_database "
 						  "WHERE datname = ",
 						  username_subquery);
-		appendStringLiteralAH(dbQry, datname, AH);
+		appendStringLiteralAH(dbQry, datname, fout);
 	}
-	else if (g_fout->remoteVersion >= 80200)
+	else if (fout->remoteVersion >= 80200)
 	{
 		appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
 						  "(%s datdba) AS dba, "
 						  "FROM pg_database "
 						  "WHERE datname = ",
 						  username_subquery);
-		appendStringLiteralAH(dbQry, datname, AH);
+		appendStringLiteralAH(dbQry, datname, fout);
 	}
-	else if (g_fout->remoteVersion >= 80000)
+	else if (fout->remoteVersion >= 80000)
 	{
 		appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
 						  "(%s datdba) AS dba, "
 						  "FROM pg_database "
 						  "WHERE datname = ",
 						  username_subquery);
-		appendStringLiteralAH(dbQry, datname, AH);
+		appendStringLiteralAH(dbQry, datname, fout);
 	}
-	else if (g_fout->remoteVersion >= 70100)
+	else if (fout->remoteVersion >= 70100)
 	{
 		appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
 						  "(%s datdba) AS dba, "
 						  "FROM pg_database "
 						  "WHERE datname = ",
 						  username_subquery);
-		appendStringLiteralAH(dbQry, datname, AH);
+		appendStringLiteralAH(dbQry, datname, fout);
 	}
 	else
 	{
 						  "FROM pg_database "
 						  "WHERE datname = ",
 						  username_subquery);
-		appendStringLiteralAH(dbQry, datname, AH);
+		appendStringLiteralAH(dbQry, datname, fout);
 	}
 
 	res = PQexec(g_conn, dbQry->data);
 	if (strlen(encoding) > 0)
 	{
 		appendPQExpBuffer(creaQry, " ENCODING = ");
-		appendStringLiteralAH(creaQry, encoding, AH);
+		appendStringLiteralAH(creaQry, encoding, fout);
 	}
 	if (strlen(collate) > 0)
 	{
 		appendPQExpBuffer(creaQry, " LC_COLLATE = ");
-		appendStringLiteralAH(creaQry, collate, AH);
+		appendStringLiteralAH(creaQry, collate, fout);
 	}
 	if (strlen(ctype) > 0)
 	{
 		appendPQExpBuffer(creaQry, " LC_CTYPE = ");
-		appendStringLiteralAH(creaQry, ctype, AH);
+		appendStringLiteralAH(creaQry, ctype, fout);
 	}
 	if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0)
 		appendPQExpBuffer(creaQry, " TABLESPACE = %s",
 						  "SET datfrozenxid = '%u'\n"
 						  "WHERE	datname = ",
 						  frozenxid);
-		appendStringLiteralAH(creaQry, datname, AH);
+		appendStringLiteralAH(creaQry, datname, fout);
 		appendPQExpBuffer(creaQry, ";\n");
 
 	}
 
 	dbDumpId = createDumpId();
 
-	ArchiveEntry(AH,
+	ArchiveEntry(fout,
 				 dbCatId,		/* catalog ID */
 				 dbDumpId,		/* dump ID */
 				 datname,		/* Name */
 						  "WHERE oid = %u;\n",
 						  atoi(PQgetvalue(lo_res, 0, i_relfrozenxid)),
 						  LargeObjectRelationId);
-		ArchiveEntry(AH, nilCatalogId, createDumpId(),
+		ArchiveEntry(fout, nilCatalogId, createDumpId(),
 					 "pg_largeobject", NULL, NULL, "",
 					 false, "pg_largeobject", SECTION_PRE_DATA,
 					 loOutQry->data, "", NULL,
 		/*
 		 * pg_largeobject_metadata
 		 */
-		if (g_fout->remoteVersion >= 90000)
+		if (fout->remoteVersion >= 90000)
 		{
 			resetPQExpBuffer(loFrozenQry);
 			resetPQExpBuffer(loOutQry);
 							  "WHERE oid = %u;\n",
 							  atoi(PQgetvalue(lo_res, 0, i_relfrozenxid)),
 							  LargeObjectMetadataRelationId);
-			ArchiveEntry(AH, nilCatalogId, createDumpId(),
+			ArchiveEntry(fout, nilCatalogId, createDumpId(),
 						 "pg_largeobject_metadata", NULL, NULL, "",
 						 false, "pg_largeobject_metadata", SECTION_PRE_DATA,
 						 loOutQry->data, "", NULL,
 	}
 
 	/* Dump DB comment if any */
-	if (g_fout->remoteVersion >= 80200)
+	if (fout->remoteVersion >= 80200)
 	{
 		/*
 		 * 8.2 keeps comments on shared objects in a shared table, so we
 			 * database.
 			 */
 			appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", fmtId(datname));
-			appendStringLiteralAH(dbQry, comment, AH);
+			appendStringLiteralAH(dbQry, comment, fout);
 			appendPQExpBuffer(dbQry, ";\n");
 
-			ArchiveEntry(AH, dbCatId, createDumpId(), datname, NULL, NULL,
+			ArchiveEntry(fout, dbCatId, createDumpId(), datname, NULL, NULL,
 						 dba, false, "COMMENT", SECTION_NONE,
 						 dbQry->data, "", NULL,
 						 &dbDumpId, 1, NULL, NULL);
 	{
 		resetPQExpBuffer(dbQry);
 		appendPQExpBuffer(dbQry, "DATABASE %s", fmtId(datname));
-		dumpComment(AH, dbQry->data, NULL, "",
+		dumpComment(fout, dbQry->data, NULL, "",
 					dbCatId, 0, dbDumpId);
 	}
 
 	PQclear(res);
 
 	/* Dump shared security label. */
-	if (!no_security_labels && g_fout->remoteVersion >= 90200)
+	if (!no_security_labels && fout->remoteVersion >= 90200)
 	{
 		PQExpBuffer seclabelQry = createPQExpBuffer();
 
 		resetPQExpBuffer(seclabelQry);
 		emitShSecLabels(g_conn, res, seclabelQry, "DATABASE", datname);
 		if (strlen(seclabelQry->data))
-			ArchiveEntry(AH, dbCatId, createDumpId(), datname, NULL, NULL,
+			ArchiveEntry(fout, dbCatId, createDumpId(), datname, NULL, NULL,
 						 dba, false, "SECURITY LABEL", SECTION_NONE,
 						 seclabelQry->data, "", NULL,
 						 &dbDumpId, 1, NULL, NULL);
  *	Collect schema-level data about large objects
  */
 static void
-getBlobs(Archive *AH)
+getBlobs(Archive *fout)
 {
 	PQExpBuffer blobQry = createPQExpBuffer();
 	BlobInfo   *binfo;
 		write_msg(NULL, "reading large objects\n");
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	/* Fetch BLOB OIDs, and owner/ACL data if >= 9.0 */
-	if (AH->remoteVersion >= 90000)
+	if (fout->remoteVersion >= 90000)
 		appendPQExpBuffer(blobQry,
 						  "SELECT oid, (%s lomowner) AS rolname, lomacl"
 						  " FROM pg_largeobject_metadata",
 						  username_subquery);
-	else if (AH->remoteVersion >= 70100)
+	else if (fout->remoteVersion >= 70100)
 		appendPQExpBuffer(blobQry,
 						  "SELECT DISTINCT loid, NULL::oid, NULL::oid"
 						  " FROM pg_largeobject");
  * dump the definition (metadata) of the given large object
  */
 static void
-dumpBlob(Archive *AH, BlobInfo *binfo)
+dumpBlob(Archive *fout, BlobInfo *binfo)
 {
 	PQExpBuffer cquery = createPQExpBuffer();
 	PQExpBuffer dquery = createPQExpBuffer();
 					  "SELECT pg_catalog.lo_unlink('%s');\n",
 					  binfo->dobj.name);
 
-	ArchiveEntry(AH, binfo->dobj.catId, binfo->dobj.dumpId,
+	ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
 				 binfo->dobj.name,
 				 NULL, NULL,
 				 binfo->rolname, false,
 	appendPQExpBuffer(cquery, "LARGE OBJECT %s", binfo->dobj.name);
 
 	/* Dump comment if any */
-	dumpComment(AH, cquery->data,
+	dumpComment(fout, cquery->data,
 				NULL, binfo->rolname,
 				binfo->dobj.catId, 0, binfo->dobj.dumpId);
 
 	/* Dump security label if any */
-	dumpSecLabel(AH, cquery->data,
+	dumpSecLabel(fout, cquery->data,
 				 NULL, binfo->rolname,
 				 binfo->dobj.catId, 0, binfo->dobj.dumpId);
 
 	/* Dump ACL if any */
 	if (binfo->blobacl)
-		dumpACL(AH, binfo->dobj.catId, binfo->dobj.dumpId, "LARGE OBJECT",
+		dumpACL(fout, binfo->dobj.catId, binfo->dobj.dumpId, "LARGE OBJECT",
 				binfo->dobj.name, NULL, cquery->data,
 				NULL, binfo->rolname, binfo->blobacl);
 
  *	dump the data contents of all large objects
  */
 static int
-dumpBlobs(Archive *AH, void *arg)
+dumpBlobs(Archive *fout, void *arg)
 {
 	const char *blobQry;
 	const char *blobFetchQry;
 		write_msg(NULL, "saving large objects\n");
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	/*
 	 * Currently, we re-fetch all BLOB OIDs using a cursor.  Consider scanning
 	 * the already-in-memory dumpable objects instead...
 	 */
-	if (AH->remoteVersion >= 90000)
+	if (fout->remoteVersion >= 90000)
 		blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_largeobject_metadata";
-	else if (AH->remoteVersion >= 70100)
+	else if (fout->remoteVersion >= 70100)
 		blobQry = "DECLARE bloboid CURSOR FOR SELECT DISTINCT loid FROM pg_largeobject";
 	else
 		blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_class WHERE relkind = 'l'";
 				exit_nicely();
 			}
 
-			StartBlob(AH, blobOid);
+			StartBlob(fout, blobOid);
 
 			/* Now read it in chunks, sending data to archive */
 			do
 					exit_nicely();
 				}
 
-				WriteData(AH, buf, cnt);
+				WriteData(fout, buf, cnt);
 			} while (cnt > 0);
 
 			lo_close(g_conn, loFd);
 
-			EndBlob(AH, blobOid);
+			EndBlob(fout, blobOid);
 		}
 	} while (ntups > 0);
 
  *	numNamespaces is set to the number of namespaces read in
  */
 NamespaceInfo *
-getNamespaces(int *numNamespaces)
+getNamespaces(Archive *fout, int *numNamespaces)
 {
 	PGresult   *res;
 	int			ntups;
 	 * Before 7.3, there are no real namespaces; create two dummy entries, one
 	 * for user stuff and one for system stuff.
 	 */
-	if (g_fout->remoteVersion < 70300)
+	if (fout->remoteVersion < 70300)
 	{
 		nsinfo = (NamespaceInfo *) pg_malloc(2 * sizeof(NamespaceInfo));
 
 	query = createPQExpBuffer();
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	/*
 	 * we fetch all namespaces including system ones, so that every object we
  * a system object or not.	In 7.3 and later there is no guessing.
  */
 static NamespaceInfo *
-findNamespace(Oid nsoid, Oid objoid)
+findNamespace(Archive *fout, Oid nsoid, Oid objoid)
 {
 	int			i;
 
-	if (g_fout->remoteVersion >= 70300)
+	if (fout->remoteVersion >= 70300)
 	{
 		for (i = 0; i < g_numNamespaces; i++)
 		{
  *	numExtensions is set to the number of extensions read in
  */
 ExtensionInfo *
-getExtensions(int *numExtensions)
+getExtensions(Archive *fout, int *numExtensions)
 {
 	PGresult   *res;
 	int			ntups;
 	/*
 	 * Before 9.1, there are no extensions.
 	 */
-	if (g_fout->remoteVersion < 90100)
+	if (fout->remoteVersion < 90100)
 	{
 		*numExtensions = 0;
 		return NULL;
 	query = createPQExpBuffer();
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	appendPQExpBuffer(query, "SELECT x.tableoid, x.oid, "
 					  "x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x.extcondition "
  * findFuncByOid().
  */
 TypeInfo *
-getTypes(int *numTypes)
+getTypes(Archive *fout, int *numTypes)
 {
 	PGresult   *res;
 	int			ntups;
 	 */
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
-	if (g_fout->remoteVersion >= 80300)
+	if (fout->remoteVersion >= 80300)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
 						  "typnamespace, "
 						  "FROM pg_type",
 						  username_subquery);
 	}
-	else if (g_fout->remoteVersion >= 70300)
+	else if (fout->remoteVersion >= 70300)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
 						  "typnamespace, "
 						  "FROM pg_type",
 						  username_subquery);
 	}
-	else if (g_fout->remoteVersion >= 70100)
+	else if (fout->remoteVersion >= 70100)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
 						  "0::oid AS typnamespace, "
 		tyinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
 		AssignDumpId(&tyinfo[i].dobj);
 		tyinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname));
-		tyinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_typnamespace)),
-												 tyinfo[i].dobj.catId.oid);
+		tyinfo[i].dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_typnamespace)),
+						  tyinfo[i].dobj.catId.oid);
 		tyinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 		tyinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem));
 		tyinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid));
 		tyinfo[i].nDomChecks = 0;
 		tyinfo[i].domChecks = NULL;
 		if (tyinfo[i].dobj.dump && tyinfo[i].typtype == TYPTYPE_DOMAIN)
-			getDomainConstraints(&(tyinfo[i]));
+			getDomainConstraints(fout, &(tyinfo[i]));
 
 		/*
 		 * If it's a base type, make a DumpableObject representing a shell
 			 * typinput and typoutput since the other functions only exist
 			 * post-7.3.
 			 */
-			if (g_fout->remoteVersion < 70300)
+			if (fout->remoteVersion < 70300)
 			{
 				Oid			typinput;
 				Oid			typoutput;
  *	numOprs is set to the number of operators read in
  */
 OprInfo *
-getOperators(int *numOprs)
+getOperators(Archive *fout, int *numOprs)
 {
 	PGresult   *res;
 	int			ntups;
 	 */
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
-	if (g_fout->remoteVersion >= 70300)
+	if (fout->remoteVersion >= 70300)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, "
 						  "oprnamespace, "
 						  "FROM pg_operator",
 						  username_subquery);
 	}
-	else if (g_fout->remoteVersion >= 70100)
+	else if (fout->remoteVersion >= 70100)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, "
 						  "0::oid AS oprnamespace, "
 		oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
 		AssignDumpId(&oprinfo[i].dobj);
 		oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname));
-		oprinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_oprnamespace)),
-												  oprinfo[i].dobj.catId.oid);
+		oprinfo[i].dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_oprnamespace)),
+						  oprinfo[i].dobj.catId.oid);
 		oprinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 		oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0];
 		oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode));
  *	numCollations is set to the number of collations read in
  */
 CollInfo *
-getCollations(int *numCollations)
+getCollations(Archive *fout, int *numCollations)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_rolname;
 
 	/* Collations didn't exist pre-9.1 */
-	if (g_fout->remoteVersion < 90100)
+	if (fout->remoteVersion < 90100)
 	{
 		*numCollations = 0;
 		return NULL;
 	 */
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	appendPQExpBuffer(query, "SELECT tableoid, oid, collname, "
 					  "collnamespace, "
 		collinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
 		AssignDumpId(&collinfo[i].dobj);
 		collinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_collname));
-		collinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_collnamespace)),
-												 collinfo[i].dobj.catId.oid);
+		collinfo[i].dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_collnamespace)),
+						  collinfo[i].dobj.catId.oid);
 		collinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 
 		/* Decide whether we want to dump it */
  *	numConversions is set to the number of conversions read in
  */
 ConvInfo *
-getConversions(int *numConversions)
+getConversions(Archive *fout, int *numConversions)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_rolname;
 
 	/* Conversions didn't exist pre-7.3 */
-	if (g_fout->remoteVersion < 70300)
+	if (fout->remoteVersion < 70300)
 	{
 		*numConversions = 0;
 		return NULL;
 	 */
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
 					  "connamespace, "
 		convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
 		AssignDumpId(&convinfo[i].dobj);
 		convinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
-		convinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_connamespace)),
-												 convinfo[i].dobj.catId.oid);
+		convinfo[i].dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_connamespace)),
+						  convinfo[i].dobj.catId.oid);
 		convinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 
 		/* Decide whether we want to dump it */
  *	numOpclasses is set to the number of opclasses read in
  */
 OpclassInfo *
-getOpclasses(int *numOpclasses)
+getOpclasses(Archive *fout, int *numOpclasses)
 {
 	PGresult   *res;
 	int			ntups;
 	 */
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
-	if (g_fout->remoteVersion >= 70300)
+	if (fout->remoteVersion >= 70300)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, "
 						  "opcnamespace, "
 						  "FROM pg_opclass",
 						  username_subquery);
 	}
-	else if (g_fout->remoteVersion >= 70100)
+	else if (fout->remoteVersion >= 70100)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, "
 						  "0::oid AS opcnamespace, "
 		opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
 		AssignDumpId(&opcinfo[i].dobj);
 		opcinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opcname));
-		opcinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_opcnamespace)),
-												  opcinfo[i].dobj.catId.oid);
+		opcinfo[i].dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_opcnamespace)),
+						  opcinfo[i].dobj.catId.oid);
 		opcinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 
 		/* Decide whether we want to dump it */
 		selectDumpableObject(&(opcinfo[i].dobj));
 
-		if (g_fout->remoteVersion >= 70300)
+		if (fout->remoteVersion >= 70300)
 		{
 			if (strlen(opcinfo[i].rolname) == 0)
 				write_msg(NULL, "WARNING: owner of operator class \"%s\" appears to be invalid\n",
  *	numOpfamilies is set to the number of opfamilies read in
  */
 OpfamilyInfo *
-getOpfamilies(int *numOpfamilies)
+getOpfamilies(Archive *fout, int *numOpfamilies)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_rolname;
 
 	/* Before 8.3, there is no separate concept of opfamilies */
-	if (g_fout->remoteVersion < 80300)
+	if (fout->remoteVersion < 80300)
 	{
 		*numOpfamilies = 0;
 		return NULL;
 	 */
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	appendPQExpBuffer(query, "SELECT tableoid, oid, opfname, "
 					  "opfnamespace, "
 		opfinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
 		AssignDumpId(&opfinfo[i].dobj);
 		opfinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opfname));
-		opfinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_opfnamespace)),
-												  opfinfo[i].dobj.catId.oid);
+		opfinfo[i].dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_opfnamespace)),
+						  opfinfo[i].dobj.catId.oid);
 		opfinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 
 		/* Decide whether we want to dump it */
 		selectDumpableObject(&(opfinfo[i].dobj));
 
-		if (g_fout->remoteVersion >= 70300)
+		if (fout->remoteVersion >= 70300)
 		{
 			if (strlen(opfinfo[i].rolname) == 0)
 				write_msg(NULL, "WARNING: owner of operator family \"%s\" appears to be invalid\n",
  * numAggs is set to the number of aggregates read in
  */
 AggInfo *
-getAggregates(int *numAggs)
+getAggregates(Archive *fout, int *numAggs)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_aggacl;
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	/*
 	 * Find all user-defined aggregates.  See comment in getFuncs() for the
 	 * rationale behind the filtering logic.
 	 */
 
-	if (g_fout->remoteVersion >= 80200)
+	if (fout->remoteVersion >= 80200)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, proname AS aggname, "
 						  "pronamespace AS aggnamespace, "
 						  "(SELECT oid FROM pg_namespace "
 						  "WHERE nspname = 'pg_catalog')",
 						  username_subquery);
-		if (binary_upgrade && g_fout->remoteVersion >= 90100)
+		if (binary_upgrade && fout->remoteVersion >= 90100)
 			appendPQExpBuffer(query,
 							  " OR EXISTS(SELECT 1 FROM pg_depend WHERE "
 							  "classid = 'pg_proc'::regclass AND "
 							  "deptype = 'e')");
 		appendPQExpBuffer(query, ")");
 	}
-	else if (g_fout->remoteVersion >= 70300)
+	else if (fout->remoteVersion >= 70300)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, proname AS aggname, "
 						  "pronamespace AS aggnamespace, "
 			   "(SELECT oid FROM pg_namespace WHERE nspname = 'pg_catalog')",
 						  username_subquery);
 	}
-	else if (g_fout->remoteVersion >= 70100)
+	else if (fout->remoteVersion >= 70100)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, aggname, "
 						  "0::oid AS aggnamespace, "
 		agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
 		AssignDumpId(&agginfo[i].aggfn.dobj);
 		agginfo[i].aggfn.dobj.name = pg_strdup(PQgetvalue(res, i, i_aggname));
-		agginfo[i].aggfn.dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_aggnamespace)),
-											agginfo[i].aggfn.dobj.catId.oid);
+		agginfo[i].aggfn.dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_aggnamespace)),
+						  agginfo[i].aggfn.dobj.catId.oid);
 		agginfo[i].aggfn.rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 		if (strlen(agginfo[i].aggfn.rolname) == 0)
 			write_msg(NULL, "WARNING: owner of aggregate function \"%s\" appears to be invalid\n",
 		else
 		{
 			agginfo[i].aggfn.argtypes = (Oid *) pg_malloc(agginfo[i].aggfn.nargs * sizeof(Oid));
-			if (g_fout->remoteVersion >= 70300)
+			if (fout->remoteVersion >= 70300)
 				parseOidArray(PQgetvalue(res, i, i_proargtypes),
 							  agginfo[i].aggfn.argtypes,
 							  agginfo[i].aggfn.nargs);
  * numFuncs is set to the number of functions read in
  */
 FuncInfo *
-getFuncs(int *numFuncs)
+getFuncs(Archive *fout, int *numFuncs)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_proacl;
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	/*
 	 * Find all user-defined functions.  Normally we can exclude functions in
 	 * doesn't have; otherwise we might not get creation ordering correct.
 	 */
 
-	if (g_fout->remoteVersion >= 70300)
+	if (fout->remoteVersion >= 70300)
 	{
 		appendPQExpBuffer(query,
 						  "SELECT tableoid, oid, proname, prolang, "
 						  "(SELECT oid FROM pg_namespace "
 						  "WHERE nspname = 'pg_catalog')",
 						  username_subquery);
-		if (g_fout->remoteVersion >= 90200)
+		if (fout->remoteVersion >= 90200)
 			appendPQExpBuffer(query,
 							  "\n  AND NOT EXISTS (SELECT 1 FROM pg_depend "
 							  "WHERE classid = 'pg_proc'::regclass AND "
 							  "objid = p.oid AND deptype = 'i')");
-		if (binary_upgrade && g_fout->remoteVersion >= 90100)
+		if (binary_upgrade && fout->remoteVersion >= 90100)
 			appendPQExpBuffer(query,
 							  "\n  OR EXISTS(SELECT 1 FROM pg_depend WHERE "
 							  "classid = 'pg_proc'::regclass AND "
 							  "deptype = 'e')");
 		appendPQExpBuffer(query, ")");
 	}
-	else if (g_fout->remoteVersion >= 70100)
+	else if (fout->remoteVersion >= 70100)
 	{
 		appendPQExpBuffer(query,
 						  "SELECT tableoid, oid, proname, prolang, "
 		AssignDumpId(&finfo[i].dobj);
 		finfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_proname));
 		finfo[i].dobj.namespace =
-			findNamespace(atooid(PQgetvalue(res, i, i_pronamespace)),
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_pronamespace)),
 						  finfo[i].dobj.catId.oid);
 		finfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 		finfo[i].lang = atooid(PQgetvalue(res, i, i_prolang));
  * numTables is set to the number of tables read in
  */
 TableInfo *
-getTables(int *numTables)
+getTables(Archive *fout, int *numTables)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_reloftype;
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	/*
 	 * Find all the tables (including views and sequences).
 	 * we cannot correctly identify inherited columns, owned sequences, etc.
 	 */
 
-	if (g_fout->remoteVersion >= 90100)
+	if (fout->remoteVersion >= 90100)
 	{
 		/*
 		 * Left join to pick up dependency info linking sequences to their
 						  RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
 						  RELKIND_FOREIGN_TABLE);
 	}
-	else if (g_fout->remoteVersion >= 90000)
+	else if (fout->remoteVersion >= 90000)
 	{
 		/*
 		 * Left join to pick up dependency info linking sequences to their
 						  RELKIND_RELATION, RELKIND_SEQUENCE,
 						  RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
 	}
-	else if (g_fout->remoteVersion >= 80400)
+	else if (fout->remoteVersion >= 80400)
 	{
 		/*
 		 * Left join to pick up dependency info linking sequences to their
 						  RELKIND_RELATION, RELKIND_SEQUENCE,
 						  RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
 	}
-	else if (g_fout->remoteVersion >= 80200)
+	else if (fout->remoteVersion >= 80200)
 	{
 		/*
 		 * Left join to pick up dependency info linking sequences to their
 						  RELKIND_RELATION, RELKIND_SEQUENCE,
 						  RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
 	}
-	else if (g_fout->remoteVersion >= 80000)
+	else if (fout->remoteVersion >= 80000)
 	{
 		/*
 		 * Left join to pick up dependency info linking sequences to their
 						  RELKIND_RELATION, RELKIND_SEQUENCE,
 						  RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
 	}
-	else if (g_fout->remoteVersion >= 70300)
+	else if (fout->remoteVersion >= 70300)
 	{
 		/*
 		 * Left join to pick up dependency info linking sequences to their
 						  RELKIND_RELATION, RELKIND_SEQUENCE,
 						  RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
 	}
-	else if (g_fout->remoteVersion >= 70200)
+	else if (fout->remoteVersion >= 70200)
 	{
 		appendPQExpBuffer(query,
 						  "SELECT tableoid, oid, relname, relacl, relkind, "
 						  username_subquery,
 						  RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW);
 	}
-	else if (g_fout->remoteVersion >= 70100)
+	else if (fout->remoteVersion >= 70100)
 	{
 		/* all tables have oids in 7.1 */
 		appendPQExpBuffer(query,
 	i_toastreloptions = PQfnumber(res, "toast_reloptions");
 	i_reloftype = PQfnumber(res, "reloftype");
 
-	if (lockWaitTimeout && g_fout->remoteVersion >= 70300)
+	if (lockWaitTimeout && fout->remoteVersion >= 70300)
 	{
 		/*
 		 * Arrange to fail instead of waiting forever for a table lock.
 		tblinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_reloid));
 		AssignDumpId(&tblinfo[i].dobj);
 		tblinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_relname));
-		tblinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_relnamespace)),
-												  tblinfo[i].dobj.catId.oid);
+		tblinfo[i].dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_relnamespace)),
+						  tblinfo[i].dobj.catId.oid);
 		tblinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 		tblinfo[i].relacl = pg_strdup(PQgetvalue(res, i, i_relacl));
 		tblinfo[i].relkind = *(PQgetvalue(res, i, i_relkind));
 			resetPQExpBuffer(query);
 			appendPQExpBuffer(query,
 							  "LOCK TABLE %s IN ACCESS SHARE MODE",
-						 fmtQualifiedId(tblinfo[i].dobj.namespace->dobj.name,
+						 fmtQualifiedId(fout,
+										tblinfo[i].dobj.namespace->dobj.name,
 										tblinfo[i].dobj.name));
 			do_sql_command(g_conn, query->data);
 		}
 					  tblinfo[i].dobj.name);
 	}
 
-	if (lockWaitTimeout && g_fout->remoteVersion >= 70300)
+	if (lockWaitTimeout && fout->remoteVersion >= 70300)
 	{
 		do_sql_command(g_conn, "SET statement_timeout = 0");
 	}
  * numInherits is set to the number of pairs read in
  */
 InhInfo *
-getInherits(int *numInherits)
+getInherits(Archive *fout, int *numInherits)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_inhparent;
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	/* find all the inheritance information */
 
  * does get entered into the DumpableObject tables.
  */
 void
-getIndexes(TableInfo tblinfo[], int numTables)
+getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
 {
 	int			i,
 				j;
 					  tbinfo->dobj.name);
 
 		/* Make sure we are in proper schema so indexdef is right */
-		selectSourceSchema(tbinfo->dobj.namespace->dobj.name);
+		selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
 
 		/*
 		 * The point of the messy-looking outer join is to find a constraint
 		 * is not.
 		 */
 		resetPQExpBuffer(query);
-		if (g_fout->remoteVersion >= 90000)
+		if (fout->remoteVersion >= 90000)
 		{
 			appendPQExpBuffer(query,
 							  "SELECT t.tableoid, t.oid, "
 							  "ORDER BY indexname",
 							  tbinfo->dobj.catId.oid);
 		}
-		else if (g_fout->remoteVersion >= 80200)
+		else if (fout->remoteVersion >= 80200)
 		{
 			appendPQExpBuffer(query,
 							  "SELECT t.tableoid, t.oid, "
 							  "ORDER BY indexname",
 							  tbinfo->dobj.catId.oid);
 		}
-		else if (g_fout->remoteVersion >= 80000)
+		else if (fout->remoteVersion >= 80000)
 		{
 			appendPQExpBuffer(query,
 							  "SELECT t.tableoid, t.oid, "
 							  "ORDER BY indexname",
 							  tbinfo->dobj.catId.oid);
 		}
-		else if (g_fout->remoteVersion >= 70300)
+		else if (fout->remoteVersion >= 70300)
 		{
 			appendPQExpBuffer(query,
 							  "SELECT t.tableoid, t.oid, "
 							  "ORDER BY indexname",
 							  tbinfo->dobj.catId.oid);
 		}
-		else if (g_fout->remoteVersion >= 70100)
+		else if (fout->remoteVersion >= 70100)
 		{
 			appendPQExpBuffer(query,
 							  "SELECT t.tableoid, t.oid, "
  * while check constraints are processed in getTableAttrs().
  */
 void
-getConstraints(TableInfo tblinfo[], int numTables)
+getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
 {
 	int			i,
 				j;
 	int			ntups;
 
 	/* pg_constraint was created in 7.3, so nothing to do if older */
-	if (g_fout->remoteVersion < 70300)
+	if (fout->remoteVersion < 70300)
 		return;
 
 	query = createPQExpBuffer();
 		 * select table schema to ensure constraint expr is qualified if
 		 * needed
 		 */
-		selectSourceSchema(tbinfo->dobj.namespace->dobj.name);
+		selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
 
 		resetPQExpBuffer(query);
 		appendPQExpBuffer(query,
  * Get info about constraints on a domain.
  */
 static void
-getDomainConstraints(TypeInfo *tyinfo)
+getDomainConstraints(Archive *fout, TypeInfo *tyinfo)
 {
 	int			i;
 	ConstraintInfo *constrinfo;
 	int			ntups;
 
 	/* pg_constraint was created in 7.3, so nothing to do if older */
-	if (g_fout->remoteVersion < 70300)
+	if (fout->remoteVersion < 70300)
 		return;
 
 	/*
 	 * select appropriate schema to ensure names in constraint are properly
 	 * qualified
 	 */
-	selectSourceSchema(tyinfo->dobj.namespace->dobj.name);
+	selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
 
 	query = createPQExpBuffer();
 
-	if (g_fout->remoteVersion >= 90100)
+	if (fout->remoteVersion >= 90100)
 		appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
 						  "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
 						  "convalidated "
 						  "ORDER BY conname",
 						  tyinfo->dobj.catId.oid);
 
-	else if (g_fout->remoteVersion >= 70400)
+	else if (fout->remoteVersion >= 70400)
 		appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
 						  "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
 						  "true as convalidated "
  * numRules is set to the number of rules read in
  */
 RuleInfo *
-getRules(int *numRules)
+getRules(Archive *fout, int *numRules)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_ev_enabled;
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
-	if (g_fout->remoteVersion >= 80300)
+	if (fout->remoteVersion >= 80300)
 	{
 		appendPQExpBuffer(query, "SELECT "
 						  "tableoid, oid, rulename, "
 						  "FROM pg_rewrite "
 						  "ORDER BY oid");
 	}
-	else if (g_fout->remoteVersion >= 70100)
+	else if (fout->remoteVersion >= 70100)
 	{
 		appendPQExpBuffer(query, "SELECT "
 						  "tableoid, oid, rulename, "
  * does get entered into the DumpableObject tables.
  */
 void
-getTriggers(TableInfo tblinfo[], int numTables)
+getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
 {
 	int			i,
 				j;
 		/*
 		 * select table schema to ensure regproc name is qualified if needed
 		 */
-		selectSourceSchema(tbinfo->dobj.namespace->dobj.name);
+		selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
 
 		resetPQExpBuffer(query);
-		if (g_fout->remoteVersion >= 90000)
+		if (fout->remoteVersion >= 90000)
 		{
 			/*
 			 * NB: think not to use pretty=true in pg_get_triggerdef.  It
 							  "AND NOT tgisinternal",
 							  tbinfo->dobj.catId.oid);
 		}
-		else if (g_fout->remoteVersion >= 80300)
+		else if (fout->remoteVersion >= 80300)
 		{
 			/*
 			 * We ignore triggers that are tied to a foreign-key constraint
 							  "AND tgconstraint = 0",
 							  tbinfo->dobj.catId.oid);
 		}
-		else if (g_fout->remoteVersion >= 70300)
+		else if (fout->remoteVersion >= 70300)
 		{
 			/*
 			 * We ignore triggers that are tied to a foreign-key constraint,
 							  "   WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))",
 							  tbinfo->dobj.catId.oid);
 		}
-		else if (g_fout->remoteVersion >= 70100)
+		else if (fout->remoteVersion >= 70100)
 		{
 			appendPQExpBuffer(query,
 							  "SELECT tgname, tgfoid::regproc AS tgfname, "
  * findFuncByOid().
  */
 ProcLangInfo *
-getProcLangs(int *numProcLangs)
+getProcLangs(Archive *fout, int *numProcLangs)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_lanowner;
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
-	if (g_fout->remoteVersion >= 90000)
+	if (fout->remoteVersion >= 90000)
 	{
 		/* pg_language has a laninline column */
 		appendPQExpBuffer(query, "SELECT tableoid, oid, "
 						  "ORDER BY oid",
 						  username_subquery);
 	}
-	else if (g_fout->remoteVersion >= 80300)
+	else if (fout->remoteVersion >= 80300)
 	{
 		/* pg_language has a lanowner column */
 		appendPQExpBuffer(query, "SELECT tableoid, oid, "
 						  "ORDER BY oid",
 						  username_subquery);
 	}
-	else if (g_fout->remoteVersion >= 80100)
+	else if (fout->remoteVersion >= 80100)
 	{
 		/* Languages are owned by the bootstrap superuser, OID 10 */
 		appendPQExpBuffer(query, "SELECT tableoid, oid, *, "
 						  "ORDER BY oid",
 						  username_subquery);
 	}
-	else if (g_fout->remoteVersion >= 70400)
+	else if (fout->remoteVersion >= 70400)
 	{
 		/* Languages are owned by the bootstrap superuser, sysid 1 */
 		appendPQExpBuffer(query, "SELECT tableoid, oid, *, "
 						  "ORDER BY oid",
 						  username_subquery);
 	}
-	else if (g_fout->remoteVersion >= 70100)
+	else if (fout->remoteVersion >= 70100)
 	{
 		/* No clear notion of an owner at all before 7.4 ... */
 		appendPQExpBuffer(query, "SELECT tableoid, oid, * FROM pg_language "
 		else
 			planginfo[i].lanowner = pg_strdup("");
 
-		if (g_fout->remoteVersion < 70300)
+		if (fout->remoteVersion < 70300)
 		{
 			/*
 			 * We need to make a dependency to ensure the function will be
  * numCasts is set to the number of casts read in
  */
 CastInfo *
-getCasts(int *numCasts)
+getCasts(Archive *fout, int *numCasts)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_castmethod;
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
-	if (g_fout->remoteVersion >= 80400)
+	if (fout->remoteVersion >= 80400)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, "
 						  "castsource, casttarget, castfunc, castcontext, "
 						  "castmethod "
 						  "FROM pg_cast ORDER BY 3,4");
 	}
-	else if (g_fout->remoteVersion >= 70300)
+	else if (fout->remoteVersion >= 70300)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, "
 						  "castsource, casttarget, castfunc, castcontext, "
  *	modifies tblinfo
  */
 void
-getTableAttrs(TableInfo *tblinfo, int numTables)
+getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
 {
 	int			i,
 				j;
 		 * Make sure we are in proper schema for this table; this allows
 		 * correct retrieval of formatted type names and default exprs
 		 */
-		selectSourceSchema(tbinfo->dobj.namespace->dobj.name);
+		selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
 
 		/* find all the user attributes and their types */
 
 
 		resetPQExpBuffer(q);
 
-		if (g_fout->remoteVersion >= 90200)
+		if (fout->remoteVersion >= 90200)
 		{
 			/*
 			 * attfdwoptions is new in 9.2.
 							  "ORDER BY a.attrelid, a.attnum",
 							  tbinfo->dobj.catId.oid);
 		}
-		else if (g_fout->remoteVersion >= 90100)
+		else if (fout->remoteVersion >= 90100)
 		{
 			/*
 			 * attcollation is new in 9.1.	Since we only want to dump COLLATE
 							  "ORDER BY a.attrelid, a.attnum",
 							  tbinfo->dobj.catId.oid);
 		}
-		else if (g_fout->remoteVersion >= 90000)
+		else if (fout->remoteVersion >= 90000)
 		{
 			/* attoptions is new in 9.0 */
 			appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
 							  "ORDER BY a.attrelid, a.attnum",
 							  tbinfo->dobj.catId.oid);
 		}
-		else if (g_fout->remoteVersion >= 70300)
+		else if (fout->remoteVersion >= 70300)
 		{
 			/* need left join here to not fail on dropped columns ... */
 			appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
 							  "ORDER BY a.attrelid, a.attnum",
 							  tbinfo->dobj.catId.oid);
 		}
-		else if (g_fout->remoteVersion >= 70100)
+		else if (fout->remoteVersion >= 70100)
 		{
 			/*
 			 * attstattarget doesn't exist in 7.1.  It does exist in 7.2, but
 						  tbinfo->dobj.name);
 
 			resetPQExpBuffer(q);
-			if (g_fout->remoteVersion >= 70300)
+			if (fout->remoteVersion >= 70300)
 			{
 				appendPQExpBuffer(q, "SELECT tableoid, oid, adnum, "
 						   "pg_catalog.pg_get_expr(adbin, adrelid) AS adsrc "
 								  "WHERE adrelid = '%u'::pg_catalog.oid",
 								  tbinfo->dobj.catId.oid);
 			}
-			else if (g_fout->remoteVersion >= 70200)
+			else if (fout->remoteVersion >= 70200)
 			{
 				/* 7.2 did not have OIDs in pg_attrdef */
 				appendPQExpBuffer(q, "SELECT tableoid, 0 AS oid, adnum, "
 								  "WHERE adrelid = '%u'::oid",
 								  tbinfo->dobj.catId.oid);
 			}
-			else if (g_fout->remoteVersion >= 70100)
+			else if (fout->remoteVersion >= 70100)
 			{
 				/* no pg_get_expr, so must rely on adsrc */
 				appendPQExpBuffer(q, "SELECT tableoid, oid, adnum, adsrc "
 						  tbinfo->dobj.name);
 
 			resetPQExpBuffer(q);
-			if (g_fout->remoteVersion >= 90200)
+			if (fout->remoteVersion >= 90200)
 			{
 				/*
 				 * conisonly and convalidated are new in 9.2 (actually, the latter
 								  "ORDER BY conname",
 								  tbinfo->dobj.catId.oid);
 			}
-			else if (g_fout->remoteVersion >= 80400)
+			else if (fout->remoteVersion >= 80400)
 			{
 				appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
 						   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
 								  "ORDER BY conname",
 								  tbinfo->dobj.catId.oid);
 			}
-			else if (g_fout->remoteVersion >= 70400)
+			else if (fout->remoteVersion >= 70400)
 			{
 				appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
 						   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
 								  "ORDER BY conname",
 								  tbinfo->dobj.catId.oid);
 			}
-			else if (g_fout->remoteVersion >= 70300)
+			else if (fout->remoteVersion >= 70300)
 			{
 				/* no pg_get_constraintdef, must use consrc */
 				appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
 								  "ORDER BY conname",
 								  tbinfo->dobj.catId.oid);
 			}
-			else if (g_fout->remoteVersion >= 70200)
+			else if (fout->remoteVersion >= 70200)
 			{
 				/* 7.2 did not have OIDs in pg_relcheck */
 				appendPQExpBuffer(q, "SELECT tableoid, 0 AS oid, "
 								  "ORDER BY rcname",
 								  tbinfo->dobj.catId.oid);
 			}
-			else if (g_fout->remoteVersion >= 70100)
+			else if (fout->remoteVersion >= 70100)
 			{
 				appendPQExpBuffer(q, "SELECT tableoid, oid, "
 								  "rcname AS conname, "
  *	numTSParsers is set to the number of parsers read in
  */
 TSParserInfo *
-getTSParsers(int *numTSParsers)
+getTSParsers(Archive *fout, int *numTSParsers)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_prslextype;
 
 	/* Before 8.3, there is no built-in text search support */
-	if (g_fout->remoteVersion < 80300)
+	if (fout->remoteVersion < 80300)
 	{
 		*numTSParsers = 0;
 		return NULL;
 	 */
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	appendPQExpBuffer(query, "SELECT tableoid, oid, prsname, prsnamespace, "
 					  "prsstart::oid, prstoken::oid, "
 		prsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
 		AssignDumpId(&prsinfo[i].dobj);
 		prsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_prsname));
-		prsinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_prsnamespace)),
-												  prsinfo[i].dobj.catId.oid);
+		prsinfo[i].dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_prsnamespace)),
+						  prsinfo[i].dobj.catId.oid);
 		prsinfo[i].prsstart = atooid(PQgetvalue(res, i, i_prsstart));
 		prsinfo[i].prstoken = atooid(PQgetvalue(res, i, i_prstoken));
 		prsinfo[i].prsend = atooid(PQgetvalue(res, i, i_prsend));
  *	numTSDicts is set to the number of dictionaries read in
  */
 TSDictInfo *
-getTSDictionaries(int *numTSDicts)
+getTSDictionaries(Archive *fout, int *numTSDicts)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_dictinitoption;
 
 	/* Before 8.3, there is no built-in text search support */
-	if (g_fout->remoteVersion < 80300)
+	if (fout->remoteVersion < 80300)
 	{
 		*numTSDicts = 0;
 		return NULL;
 	}
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	appendPQExpBuffer(query, "SELECT tableoid, oid, dictname, "
 					  "dictnamespace, (%s dictowner) AS rolname, "
 		dictinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
 		AssignDumpId(&dictinfo[i].dobj);
 		dictinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_dictname));
-		dictinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_dictnamespace)),
-												 dictinfo[i].dobj.catId.oid);
+		dictinfo[i].dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_dictnamespace)),
+						  dictinfo[i].dobj.catId.oid);
 		dictinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 		dictinfo[i].dicttemplate = atooid(PQgetvalue(res, i, i_dicttemplate));
 		if (PQgetisnull(res, i, i_dictinitoption))
  *	numTSTemplates is set to the number of templates read in
  */
 TSTemplateInfo *
-getTSTemplates(int *numTSTemplates)
+getTSTemplates(Archive *fout, int *numTSTemplates)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_tmpllexize;
 
 	/* Before 8.3, there is no built-in text search support */
-	if (g_fout->remoteVersion < 80300)
+	if (fout->remoteVersion < 80300)
 	{
 		*numTSTemplates = 0;
 		return NULL;
 	}
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	appendPQExpBuffer(query, "SELECT tableoid, oid, tmplname, "
 					  "tmplnamespace, tmplinit::oid, tmpllexize::oid "
 		tmplinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
 		AssignDumpId(&tmplinfo[i].dobj);
 		tmplinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_tmplname));
-		tmplinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_tmplnamespace)),
-												 tmplinfo[i].dobj.catId.oid);
+		tmplinfo[i].dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_tmplnamespace)),
+						  tmplinfo[i].dobj.catId.oid);
 		tmplinfo[i].tmplinit = atooid(PQgetvalue(res, i, i_tmplinit));
 		tmplinfo[i].tmpllexize = atooid(PQgetvalue(res, i, i_tmpllexize));
 
  *	numTSConfigs is set to the number of configurations read in
  */
 TSConfigInfo *
-getTSConfigurations(int *numTSConfigs)
+getTSConfigurations(Archive *fout, int *numTSConfigs)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_cfgparser;
 
 	/* Before 8.3, there is no built-in text search support */
-	if (g_fout->remoteVersion < 80300)
+	if (fout->remoteVersion < 80300)
 	{
 		*numTSConfigs = 0;
 		return NULL;
 	}
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	appendPQExpBuffer(query, "SELECT tableoid, oid, cfgname, "
 					  "cfgnamespace, (%s cfgowner) AS rolname, cfgparser "
 		cfginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
 		AssignDumpId(&cfginfo[i].dobj);
 		cfginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_cfgname));
-		cfginfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_cfgnamespace)),
-												  cfginfo[i].dobj.catId.oid);
+		cfginfo[i].dobj.namespace =
+			findNamespace(fout,
+						  atooid(PQgetvalue(res, i, i_cfgnamespace)),
+						  cfginfo[i].dobj.catId.oid);
 		cfginfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 		cfginfo[i].cfgparser = atooid(PQgetvalue(res, i, i_cfgparser));
 
  *	numForeignDataWrappers is set to the number of fdws read in
  */
 FdwInfo *
-getForeignDataWrappers(int *numForeignDataWrappers)
+getForeignDataWrappers(Archive *fout, int *numForeignDataWrappers)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_fdwoptions;
 
 	/* Before 8.4, there are no foreign-data wrappers */
-	if (g_fout->remoteVersion < 80400)
+	if (fout->remoteVersion < 80400)
 	{
 		*numForeignDataWrappers = 0;
 		return NULL;
 	}
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
-	if (g_fout->remoteVersion >= 90100)
+	if (fout->remoteVersion >= 90100)
 	{
 		appendPQExpBuffer(query, "SELECT tableoid, oid, fdwname, "
 						  "(%s fdwowner) AS rolname, "
  *	numForeignServers is set to the number of servers read in
  */
 ForeignServerInfo *
-getForeignServers(int *numForeignServers)
+getForeignServers(Archive *fout, int *numForeignServers)
 {
 	PGresult   *res;
 	int			ntups;
 	int			i_srvoptions;
 
 	/* Before 8.4, there are no foreign servers */
-	if (g_fout->remoteVersion < 80400)
+	if (fout->remoteVersion < 80400)
 	{
 		*numForeignServers = 0;
 		return NULL;
 	}
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout,"pg_catalog");
 
 	appendPQExpBuffer(query, "SELECT tableoid, oid, srvname, "
 					  "(%s srvowner) AS rolname, "
  *	numDefaultACLs is set to the number of ACLs read in
  */
 DefaultACLInfo *
-getDefaultACLs(int *numDefaultACLs)
+getDefaultACLs(Archive *fout, int *numDefaultACLs)
 {
 	DefaultACLInfo *daclinfo;
 	PQExpBuffer query;
 	int			i,
 				ntups;
 
-	if (g_fout->remoteVersion < 90000)
+	if (fout->remoteVersion < 90000)
 	{
 		*numDefaultACLs = 0;
 		return NULL;
 	query = createPQExpBuffer();
 
 	/* Make sure we are in proper schema */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	appendPQExpBuffer(query, "SELECT oid, tableoid, "
 					  "(%s defaclrole) AS defaclrole, "
 		daclinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_defaclobjtype));
 
 		if (nspid != InvalidOid)
-			daclinfo[i].dobj.namespace = findNamespace(nspid,
+			daclinfo[i].dobj.namespace = findNamespace(fout, nspid,
 												 daclinfo[i].dobj.catId.oid);
 		else
 			daclinfo[i].dobj.namespace = NULL;
 	char	   *label;
 
 	/* Set proper schema search path */
-	selectSourceSchema("pg_catalog");
+	selectSourceSchema(fout, "pg_catalog");
 
 	if (fout->remoteVersion >= 90100)
 		appendPQExpBuffer(query, "SELECT oid, enumlabel "
 	 * select appropriate schema to ensure names in CREATE are properly
 	 * qualified
 	 */
-	selectSourceSchema(tyinfo->dobj.namespace->dobj.name);
+	selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
 
 	appendPQExpBuffer(query,
 					  "SELECT pg_catalog.format_type(rngsubtype, NULL) AS rngsubtype, "
 	bool		typdefault_is_literal = false;
 
 	/* Set proper schema search path so regproc references list correctly */
-	selectSourceSchema(tyinfo->dobj.namespace->dobj.name);
+	selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
 
 	/* Fetch type-specific details */
 	if (fout->remoteVersion >= 90100)