From 32e471ebf262c7e9d1edb9960907f1ec74321cef Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Mon, 4 Jan 2021 12:44:20 -0800
Subject: [PATCH v3 2/9] Refactoring ExecuteSqlQuery and related functions.

ExecuteSqlQuery, ExecuteSqlQueryForSingleRow, and
ExecuteSqlStatement in the pg_dump project were defined to take a
pointer to struct Archive, which is a struct unused outside pg_dump.
In preparation for moving these functions to fe_utils, refactoring
these functions to take a PGconn pointer.  These functions also
embedded pg_dump assumptions about the correct error handling
behavior, specifically to do with logging error messages before
calling exit_nicely().  Refactoring the error handling logic into a
handler function.  The full design of the handler is not yet
present, as it will be developed further after moving to fe_utils,
but the idea is that callers will ultimately be able to override the
error handling behavior by defining alternate handlers.

To minimize changes to pg_dump and friends, creating thin wrappers
around these functions that take an Archive pointer.  It might be
marginally cleaner in the long run to refactor pg_dump.c to call
with a PGconn pointer in all relevant call sites, but that would
result in a nontrivially larger patch and more code churn, so not
doing that here.  Another option might be to define the thin
wrappers as static inline functions, but that seems inconsistent
with the rest of the pg_dump project style, so not doing that
either.  Should we?
---
 src/bin/pg_dump/pg_backup_db.c | 144 ++++++++++++++-----
 src/bin/pg_dump/pg_backup_db.h |  26 +++-
 src/bin/pg_dump/pg_dump.c      | 248 ++++++++++++++++-----------------
 3 files changed, 253 insertions(+), 165 deletions(-)

diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c
index 5ba43441f5..b55a968da2 100644
--- a/src/bin/pg_dump/pg_backup_db.c
+++ b/src/bin/pg_dump/pg_backup_db.c
@@ -61,7 +61,7 @@ _check_database_version(ArchiveHandle *AH)
 	 */
 	if (remoteversion >= 90000)
 	{
-		res = ExecuteSqlQueryForSingleRow((Archive *) AH, "SELECT pg_catalog.pg_is_in_recovery()");
+		res = ExecuteSqlQueryForSingleRowAH((Archive *) AH, "SELECT pg_catalog.pg_is_in_recovery()");
 
 		AH->public.isStandby = (strcmp(PQgetvalue(res, 0, 0), "t") == 0);
 		PQclear(res);
@@ -198,8 +198,8 @@ ConnectDatabase(Archive *AHX,
 	}
 
 	/* Start strict; later phases may override this. */
-	PQclear(ExecuteSqlQueryForSingleRow((Archive *) AH,
-										ALWAYS_SECURE_SEARCH_PATH_SQL));
+	PQclear(ExecuteSqlQueryForSingleRowAH((Archive *) AH,
+										  ALWAYS_SECURE_SEARCH_PATH_SQL));
 
 	if (password && password != AH->savedPassword)
 		free(password);
@@ -271,59 +271,129 @@ notice_processor(void *arg, const char *message)
 	pg_log_generic(PG_LOG_INFO, "%s", message);
 }
 
-/* Like fatal(), but with a complaint about a particular query. */
-static void
-die_on_query_failure(ArchiveHandle *AH, const char *query)
+/*
+ * The exiting query result handler embeds the historical pg_dump behavior
+ * under query error conditions, including exiting nicely.  The 'conn' object
+ * is unused here, but is included in the interface for alternate query result
+ * handler implementations.
+ *
+ * Whether the query was successful is determined by comparing the returned
+ * status code against the expected status code, and by comparing the number of
+ * tuples returned from the query against expected_ntups.  Special negative
+ * values of expected_ntups can be used to require at least one row or to
+ * disables ntup checking.
+ *
+ * Exits on failure.  On successful query completion, returns the 'res'
+ * argument as a notational convenience.
+ */
+PGresult *
+exiting_handler(PGresult *res, PGconn *conn, ExecStatusType expected_status,
+				  int expected_ntups, const char *query)
 {
-	pg_log_error("query failed: %s",
-				 PQerrorMessage(AH->connection));
-	fatal("query was: %s", query);
+	if (PQresultStatus(res) != expected_status)
+	{
+		pg_log_error("query failed: %s", PQerrorMessage(conn));
+		pg_log_error("query was: %s", query);
+		PQfinish(conn);
+		exit_nicely(1);
+	}
+	if (expected_ntups == POSITIVE_NTUPS || expected_ntups >= 0)
+	{
+		int ntups = PQntuples(res);
+
+		if (expected_ntups == POSITIVE_NTUPS)
+		{
+			if (ntups == 0)
+				fatal("query returned no rows: %s", query);
+		}
+		else if (ntups != expected_ntups)
+		{
+			/*
+			 * Preserve historical message behavior of spelling "one" as the
+			 * expected row count.
+			 */
+			if (expected_ntups == 1)
+				fatal(ngettext("query returned %d row instead of one: %s",
+							   "query returned %d rows instead of one: %s",
+							   ntups),
+					ntups, query);
+			fatal(ngettext("query returned %d row instead of %d: %s",
+						   "query returned %d rows instead of %d: %s",
+						   ntups),
+				ntups, expected_ntups, query);
+		}
+	}
+	return res;
 }
 
+/*
+ * Executes the given SQL query statement.
+ *
+ * Invokes the exiting handler for any but PGRES_COMMAND_OK status.
+ */
 void
-ExecuteSqlStatement(Archive *AHX, const char *query)
+ExecuteSqlStatement(PGconn *conn, const char *query)
 {
-	ArchiveHandle *AH = (ArchiveHandle *) AHX;
-	PGresult   *res;
+	PQclear(exiting_handler(PQexec(conn, query),
+							conn,
+							PGRES_COMMAND_OK,
+							ANY_NTUPS,
+							query));
+}
 
-	res = PQexec(AH->connection, query);
-	if (PQresultStatus(res) != PGRES_COMMAND_OK)
-		die_on_query_failure(AH, query);
-	PQclear(res);
+/*
+ * Executes the given SQL query.
+ *
+ * Invokes the exiting handler unless the given 'status' results.
+ *
+ * If successful, returns the query result.
+ */
+PGresult *
+ExecuteSqlQuery(PGconn *conn, const char *query, ExecStatusType status)
+{
+	return exiting_handler(PQexec(conn, query),
+						   conn,
+						   status,
+						   ANY_NTUPS,
+						   query);
 }
 
+/*
+ * Like ExecuteSqlQuery, but requires PGRES_TUPLES_OK status and
+ * requires that exactly one row be returned.
+ */
 PGresult *
-ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
+ExecuteSqlQueryForSingleRow(PGconn *conn, const char *query)
+{
+	return exiting_handler(PQexec(conn, query),
+						   conn,
+						   PGRES_TUPLES_OK,
+						   1,
+						   query);
+}
+
+void
+ExecuteSqlStatementAH(Archive *AHX, const char *query)
 {
 	ArchiveHandle *AH = (ArchiveHandle *) AHX;
-	PGresult   *res;
 
-	res = PQexec(AH->connection, query);
-	if (PQresultStatus(res) != status)
-		die_on_query_failure(AH, query);
-	return res;
+	ExecuteSqlStatement(AH->connection, query);
 }
 
-/*
- * Execute an SQL query and verify that we got exactly one row back.
- */
 PGresult *
-ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
+ExecuteSqlQueryAH(Archive *AHX, const char *query, ExecStatusType status)
 {
-	PGresult   *res;
-	int			ntups;
+	ArchiveHandle *AH = (ArchiveHandle *) AHX;
 
-	res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
+	return ExecuteSqlQuery(AH->connection, query, status);
+}
 
-	/* Expecting a single result only */
-	ntups = PQntuples(res);
-	if (ntups != 1)
-		fatal(ngettext("query returned %d row instead of one: %s",
-					   "query returned %d rows instead of one: %s",
-					   ntups),
-			  ntups, query);
+PGresult *
+ExecuteSqlQueryForSingleRowAH(Archive *AHX, const char *query)
+{
+	ArchiveHandle *AH = (ArchiveHandle *) AHX;
 
-	return res;
+	return ExecuteSqlQueryForSingleRow(AH->connection, query);
 }
 
 /*
diff --git a/src/bin/pg_dump/pg_backup_db.h b/src/bin/pg_dump/pg_backup_db.h
index 8888dd34b9..1aac600ece 100644
--- a/src/bin/pg_dump/pg_backup_db.h
+++ b/src/bin/pg_dump/pg_backup_db.h
@@ -13,10 +13,28 @@
 
 extern int	ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen);
 
-extern void ExecuteSqlStatement(Archive *AHX, const char *query);
-extern PGresult *ExecuteSqlQuery(Archive *AHX, const char *query,
-								 ExecStatusType status);
-extern PGresult *ExecuteSqlQueryForSingleRow(Archive *fout, const char *query);
+#define POSITIVE_NTUPS (-1)
+#define ANY_NTUPS (-2)
+typedef PGresult *(*PGresultHandler) (PGresult *res,
+									  PGconn *conn,
+									  ExecStatusType expected_status,
+									  int expected_ntups,
+									  const char *query);
+
+extern PGresult *exiting_handler(PGresult *res, PGconn *conn,
+								 ExecStatusType expected_status,
+								 int expected_ntups, const char *query);
+
+extern void ExecuteSqlStatement(PGconn *conn, const char *query);
+extern PGresult *ExecuteSqlQuery(PGconn *conn, const char *query,
+								 ExecStatusType expected_status);
+extern PGresult *ExecuteSqlQueryForSingleRow(PGconn *conn, const char *query);
+
+extern void ExecuteSqlStatementAH(Archive *AHX, const char *query);
+extern PGresult *ExecuteSqlQueryAH(Archive *AHX, const char *query,
+								   ExecStatusType status);
+extern PGresult *ExecuteSqlQueryForSingleRowAH(Archive *fout,
+											   const char *query);
 
 extern void EndDBCopyMode(Archive *AHX, const char *tocEntryTag);
 
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 1f70653c02..e8985a834f 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -1084,7 +1084,7 @@ setup_connection(Archive *AH, const char *dumpencoding,
 	PGconn	   *conn = GetConnection(AH);
 	const char *std_strings;
 
-	PQclear(ExecuteSqlQueryForSingleRow(AH, ALWAYS_SECURE_SEARCH_PATH_SQL));
+	PQclear(ExecuteSqlQueryForSingleRowAH(AH, ALWAYS_SECURE_SEARCH_PATH_SQL));
 
 	/*
 	 * Set the client encoding if requested.
@@ -1119,7 +1119,7 @@ setup_connection(Archive *AH, const char *dumpencoding,
 		PQExpBuffer query = createPQExpBuffer();
 
 		appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
-		ExecuteSqlStatement(AH, query->data);
+		ExecuteSqlStatementAH(AH, query->data);
 		destroyPQExpBuffer(query);
 
 		/* save it for possible later use by parallel workers */
@@ -1128,11 +1128,11 @@ setup_connection(Archive *AH, const char *dumpencoding,
 	}
 
 	/* Set the datestyle to ISO to ensure the dump's portability */
-	ExecuteSqlStatement(AH, "SET DATESTYLE = ISO");
+	ExecuteSqlStatementAH(AH, "SET DATESTYLE = ISO");
 
 	/* Likewise, avoid using sql_standard intervalstyle */
 	if (AH->remoteVersion >= 80400)
-		ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES");
+		ExecuteSqlStatementAH(AH, "SET INTERVALSTYLE = POSTGRES");
 
 	/*
 	 * Use an explicitly specified extra_float_digits if it has been provided.
@@ -1145,35 +1145,35 @@ setup_connection(Archive *AH, const char *dumpencoding,
 
 		appendPQExpBuffer(q, "SET extra_float_digits TO %d",
 						  extra_float_digits);
-		ExecuteSqlStatement(AH, q->data);
+		ExecuteSqlStatementAH(AH, q->data);
 		destroyPQExpBuffer(q);
 	}
 	else if (AH->remoteVersion >= 90000)
-		ExecuteSqlStatement(AH, "SET extra_float_digits TO 3");
+		ExecuteSqlStatementAH(AH, "SET extra_float_digits TO 3");
 	else
-		ExecuteSqlStatement(AH, "SET extra_float_digits TO 2");
+		ExecuteSqlStatementAH(AH, "SET extra_float_digits TO 2");
 
 	/*
 	 * If synchronized scanning is supported, disable it, to prevent
 	 * unpredictable changes in row ordering across a dump and reload.
 	 */
 	if (AH->remoteVersion >= 80300)
-		ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off");
+		ExecuteSqlStatementAH(AH, "SET synchronize_seqscans TO off");
 
 	/*
 	 * Disable timeouts if supported.
 	 */
-	ExecuteSqlStatement(AH, "SET statement_timeout = 0");
+	ExecuteSqlStatementAH(AH, "SET statement_timeout = 0");
 	if (AH->remoteVersion >= 90300)
-		ExecuteSqlStatement(AH, "SET lock_timeout = 0");
+		ExecuteSqlStatementAH(AH, "SET lock_timeout = 0");
 	if (AH->remoteVersion >= 90600)
-		ExecuteSqlStatement(AH, "SET idle_in_transaction_session_timeout = 0");
+		ExecuteSqlStatementAH(AH, "SET idle_in_transaction_session_timeout = 0");
 
 	/*
 	 * Quote all identifiers, if requested.
 	 */
 	if (quote_all_identifiers && AH->remoteVersion >= 90100)
-		ExecuteSqlStatement(AH, "SET quote_all_identifiers = true");
+		ExecuteSqlStatementAH(AH, "SET quote_all_identifiers = true");
 
 	/*
 	 * Adjust row-security mode, if supported.
@@ -1181,15 +1181,15 @@ setup_connection(Archive *AH, const char *dumpencoding,
 	if (AH->remoteVersion >= 90500)
 	{
 		if (dopt->enable_row_security)
-			ExecuteSqlStatement(AH, "SET row_security = on");
+			ExecuteSqlStatementAH(AH, "SET row_security = on");
 		else
-			ExecuteSqlStatement(AH, "SET row_security = off");
+			ExecuteSqlStatementAH(AH, "SET row_security = off");
 	}
 
 	/*
 	 * Start transaction-snapshot mode transaction to dump consistent data.
 	 */
-	ExecuteSqlStatement(AH, "BEGIN");
+	ExecuteSqlStatementAH(AH, "BEGIN");
 	if (AH->remoteVersion >= 90100)
 	{
 		/*
@@ -1201,17 +1201,17 @@ setup_connection(Archive *AH, const char *dumpencoding,
 		 * guarantees.  This is a kluge, but safe for back-patching.
 		 */
 		if (dopt->serializable_deferrable && AH->sync_snapshot_id == NULL)
-			ExecuteSqlStatement(AH,
+			ExecuteSqlStatementAH(AH,
 								"SET TRANSACTION ISOLATION LEVEL "
 								"SERIALIZABLE, READ ONLY, DEFERRABLE");
 		else
-			ExecuteSqlStatement(AH,
+			ExecuteSqlStatementAH(AH,
 								"SET TRANSACTION ISOLATION LEVEL "
 								"REPEATABLE READ, READ ONLY");
 	}
 	else
 	{
-		ExecuteSqlStatement(AH,
+		ExecuteSqlStatementAH(AH,
 							"SET TRANSACTION ISOLATION LEVEL "
 							"SERIALIZABLE, READ ONLY");
 	}
@@ -1230,7 +1230,7 @@ setup_connection(Archive *AH, const char *dumpencoding,
 
 		appendPQExpBufferStr(query, "SET TRANSACTION SNAPSHOT ");
 		appendStringLiteralConn(query, AH->sync_snapshot_id, conn);
-		ExecuteSqlStatement(AH, query->data);
+		ExecuteSqlStatementAH(AH, query->data);
 		destroyPQExpBuffer(query);
 	}
 	else if (AH->numWorkers > 1 &&
@@ -1270,7 +1270,7 @@ get_synchronized_snapshot(Archive *fout)
 	char	   *result;
 	PGresult   *res;
 
-	res = ExecuteSqlQueryForSingleRow(fout, query);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query);
 	result = pg_strdup(PQgetvalue(res, 0, 0));
 	PQclear(res);
 
@@ -1343,7 +1343,7 @@ expand_schema_name_patterns(Archive *fout,
 		processSQLNamePattern(GetConnection(fout), query, cell->val, false,
 							  false, NULL, "n.nspname", NULL, NULL);
 
-		res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 		if (strict_names && PQntuples(res) == 0)
 			fatal("no matching schemas were found for pattern \"%s\"", cell->val);
 
@@ -1390,7 +1390,7 @@ expand_foreign_server_name_patterns(Archive *fout,
 		processSQLNamePattern(GetConnection(fout), query, cell->val, false,
 							  false, NULL, "s.srvname", NULL, NULL);
 
-		res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 		if (PQntuples(res) == 0)
 			fatal("no matching foreign servers were found for pattern \"%s\"", cell->val);
 
@@ -1450,9 +1450,9 @@ expand_table_name_patterns(Archive *fout,
 							  false, "n.nspname", "c.relname", NULL,
 							  "pg_catalog.pg_table_is_visible(c.oid)");
 
-		ExecuteSqlStatement(fout, "RESET search_path");
-		res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
-		PQclear(ExecuteSqlQueryForSingleRow(fout,
+		ExecuteSqlStatementAH(fout, "RESET search_path");
+		res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
+		PQclear(ExecuteSqlQueryForSingleRowAH(fout,
 											ALWAYS_SECURE_SEARCH_PATH_SQL));
 		if (strict_names && PQntuples(res) == 0)
 			fatal("no matching tables were found for pattern \"%s\"", cell->val);
@@ -1907,7 +1907,7 @@ dumpTableData_copy(Archive *fout, void *dcontext)
 						  fmtQualifiedDumpable(tbinfo),
 						  column_list);
 	}
-	res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT);
+	res = ExecuteSqlQueryAH(fout, q->data, PGRES_COPY_OUT);
 	PQclear(res);
 	destroyPQExpBuffer(clistBuf);
 
@@ -2028,11 +2028,11 @@ dumpTableData_insert(Archive *fout, void *dcontext)
 	if (tdinfo->filtercond)
 		appendPQExpBuffer(q, " %s", tdinfo->filtercond);
 
-	ExecuteSqlStatement(fout, q->data);
+	ExecuteSqlStatementAH(fout, q->data);
 
 	while (1)
 	{
-		res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor",
+		res = ExecuteSqlQueryAH(fout, "FETCH 100 FROM _pg_dump_cursor",
 							  PGRES_TUPLES_OK);
 		nfields = PQnfields(res);
 
@@ -2220,7 +2220,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
 
 	archputs("\n\n", fout);
 
-	ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
+	ExecuteSqlStatementAH(fout, "CLOSE _pg_dump_cursor");
 
 	destroyPQExpBuffer(q);
 	if (insertStmt != NULL)
@@ -2520,7 +2520,7 @@ buildMatViewRefreshDependencies(Archive *fout)
 						 "FROM w "
 						 "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -2847,7 +2847,7 @@ dumpDatabase(Archive *fout)
 						  username_subquery);
 	}
 
-	res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, dbQry->data);
 
 	i_tableoid = PQfnumber(res, "tableoid");
 	i_oid = PQfnumber(res, "oid");
@@ -2992,7 +2992,7 @@ dumpDatabase(Archive *fout)
 		seclabelQry = createPQExpBuffer();
 
 		buildShSecLabelQuery("pg_database", dbCatId.oid, seclabelQry);
-		shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
+		shres = ExecuteSqlQueryAH(fout, seclabelQry->data, PGRES_TUPLES_OK);
 		resetPQExpBuffer(seclabelQry);
 		emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
 		if (seclabelQry->len > 0)
@@ -3103,7 +3103,7 @@ dumpDatabase(Archive *fout)
 							  "WHERE oid = %u;\n",
 							  LargeObjectRelationId);
 
-		lo_res = ExecuteSqlQueryForSingleRow(fout, loFrozenQry->data);
+		lo_res = ExecuteSqlQueryForSingleRowAH(fout, loFrozenQry->data);
 
 		i_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
 		i_relminmxid = PQfnumber(lo_res, "relminmxid");
@@ -3162,7 +3162,7 @@ dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
 		else
 			printfPQExpBuffer(buf, "SELECT datconfig[%d] FROM pg_database WHERE oid = '%u'::oid", count, dboid);
 
-		res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(AH, buf->data, PGRES_TUPLES_OK);
 
 		if (PQntuples(res) == 1 &&
 			!PQgetisnull(res, 0, 0))
@@ -3189,7 +3189,7 @@ dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
 						  "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
 						  dboid);
 
-		res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(AH, buf->data, PGRES_TUPLES_OK);
 
 		if (PQntuples(res) > 0)
 		{
@@ -3277,7 +3277,7 @@ dumpSearchPath(Archive *AH)
 	 * listing schemas that may appear in search_path but not actually exist,
 	 * which seems like a prudent exclusion.
 	 */
-	res = ExecuteSqlQueryForSingleRow(AH,
+	res = ExecuteSqlQueryForSingleRowAH(AH,
 									  "SELECT pg_catalog.current_schemas(false)");
 
 	if (!parsePGArray(PQgetvalue(res, 0, 0), &schemanames, &nschemanames))
@@ -3391,7 +3391,7 @@ getBlobs(Archive *fout)
 							 "NULL::oid AS initrlomacl "
 							 " FROM pg_largeobject");
 
-	res = ExecuteSqlQuery(fout, blobQry->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, blobQry->data, PGRES_TUPLES_OK);
 
 	i_oid = PQfnumber(res, "oid");
 	i_lomowner = PQfnumber(res, "rolname");
@@ -3537,7 +3537,7 @@ dumpBlobs(Archive *fout, void *arg)
 			"DECLARE bloboid CURSOR FOR "
 			"SELECT DISTINCT loid FROM pg_largeobject ORDER BY 1";
 
-	ExecuteSqlStatement(fout, blobQry);
+	ExecuteSqlStatementAH(fout, blobQry);
 
 	/* Command to fetch from cursor */
 	blobFetchQry = "FETCH 1000 IN bloboid";
@@ -3545,7 +3545,7 @@ dumpBlobs(Archive *fout, void *arg)
 	do
 	{
 		/* Do a fetch */
-		res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, blobFetchQry, PGRES_TUPLES_OK);
 
 		/* Process the tuples, if any */
 		ntups = PQntuples(res);
@@ -3678,7 +3678,7 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
 							  "FROM pg_catalog.pg_policy pol "
 							  "WHERE polrelid = '%u'",
 							  tbinfo->dobj.catId.oid);
-		res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 		ntups = PQntuples(res);
 
@@ -3914,7 +3914,7 @@ getPublications(Archive *fout)
 						  "FROM pg_publication p",
 						  username_subquery);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -4112,7 +4112,7 @@ getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
 						  "WHERE pr.prrelid = '%u'"
 						  "  AND p.oid = pr.prpubid",
 						  tbinfo->dobj.catId.oid);
-		res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 		ntups = PQntuples(res);
 
@@ -4237,7 +4237,7 @@ getSubscriptions(Archive *fout)
 	{
 		int			n;
 
-		res = ExecuteSqlQuery(fout,
+		res = ExecuteSqlQueryAH(fout,
 							  "SELECT count(*) FROM pg_subscription "
 							  "WHERE subdbid = (SELECT oid FROM pg_database"
 							  "                 WHERE datname = current_database())",
@@ -4274,7 +4274,7 @@ getSubscriptions(Archive *fout)
 						 "WHERE s.subdbid = (SELECT oid FROM pg_database\n"
 						 "                   WHERE datname = current_database())");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -4446,7 +4446,7 @@ append_depends_on_extension(Archive *fout,
 						  "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
 						  catalog,
 						  dobj->catId.oid);
-		res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 		ntups = PQntuples(res);
 		i_extname = PQfnumber(res, "extname");
 		for (i = 0; i < ntups; i++)
@@ -4485,7 +4485,7 @@ get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
 						  "FROM pg_catalog.pg_type "
 						  "WHERE oid = '%u'::pg_catalog.oid);",
 						  next_possible_free_oid);
-		res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+		res = ExecuteSqlQueryForSingleRowAH(fout, upgrade_query->data);
 		is_dup = (PQgetvalue(res, 0, 0)[0] == 't');
 		PQclear(res);
 	} while (is_dup);
@@ -4518,7 +4518,7 @@ binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
 					  "WHERE oid = '%u'::pg_catalog.oid;",
 					  pg_type_oid);
 
-	res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, upgrade_query->data);
 
 	pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
 
@@ -4551,7 +4551,7 @@ binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
 							  "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
 							  pg_type_oid);
 
-			res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+			res = ExecuteSqlQueryForSingleRowAH(fout, upgrade_query->data);
 
 			pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
 			pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
@@ -4594,7 +4594,7 @@ binary_upgrade_set_type_oids_by_rel_oid(Archive *fout,
 					  "WHERE c.oid = '%u'::pg_catalog.oid;",
 					  pg_rel_oid);
 
-	upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+	upgrade_res = ExecuteSqlQueryForSingleRowAH(fout, upgrade_query->data);
 
 	pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
 
@@ -4645,7 +4645,7 @@ binary_upgrade_set_pg_class_oids(Archive *fout,
 						  "WHERE c.oid = '%u'::pg_catalog.oid;",
 						  pg_class_oid);
 
-		upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+		upgrade_res = ExecuteSqlQueryForSingleRowAH(fout, upgrade_query->data);
 
 		pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
 												   PQfnumber(upgrade_res, "reltoastrelid")));
@@ -4803,7 +4803,7 @@ getNamespaces(Archive *fout, int *numNamespaces)
 						  "FROM pg_namespace",
 						  username_subquery);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -4916,7 +4916,7 @@ getExtensions(Archive *fout, int *numExtensions)
 						 "FROM pg_extension x "
 						 "JOIN pg_namespace n ON n.oid = x.extnamespace");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -5093,7 +5093,7 @@ getTypes(Archive *fout, int *numTypes)
 						  username_subquery);
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -5249,7 +5249,7 @@ getOperators(Archive *fout, int *numOprs)
 					  "FROM pg_operator",
 					  username_subquery);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numOprs = ntups;
@@ -5336,7 +5336,7 @@ getCollations(Archive *fout, int *numCollations)
 					  "FROM pg_collation",
 					  username_subquery);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numCollations = ntups;
@@ -5408,7 +5408,7 @@ getConversions(Archive *fout, int *numConversions)
 					  "FROM pg_conversion",
 					  username_subquery);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numConversions = ntups;
@@ -5481,7 +5481,7 @@ getAccessMethods(Archive *fout, int *numAccessMethods)
 						 "amhandler::pg_catalog.regproc AS amhandler "
 						 "FROM pg_am");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numAccessMethods = ntups;
@@ -5552,7 +5552,7 @@ getOpclasses(Archive *fout, int *numOpclasses)
 					  "FROM pg_opclass",
 					  username_subquery);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numOpclasses = ntups;
@@ -5635,7 +5635,7 @@ getOpfamilies(Archive *fout, int *numOpfamilies)
 					  "FROM pg_opfamily",
 					  username_subquery);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numOpfamilies = ntups;
@@ -5804,7 +5804,7 @@ getAggregates(Archive *fout, int *numAggs)
 						  username_subquery);
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numAggs = ntups;
@@ -6035,7 +6035,7 @@ getFuncs(Archive *fout, int *numFuncs)
 		appendPQExpBufferChar(query, ')');
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -6721,7 +6721,7 @@ getTables(Archive *fout, int *numTables)
 						  RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -6791,7 +6791,7 @@ getTables(Archive *fout, int *numTables)
 		resetPQExpBuffer(query);
 		appendPQExpBufferStr(query, "SET statement_timeout = ");
 		appendStringLiteralConn(query, dopt->lockWaitTimeout, GetConnection(fout));
-		ExecuteSqlStatement(fout, query->data);
+		ExecuteSqlStatementAH(fout, query->data);
 	}
 
 	for (i = 0; i < ntups; i++)
@@ -6915,7 +6915,7 @@ getTables(Archive *fout, int *numTables)
 			appendPQExpBuffer(query,
 							  "LOCK TABLE %s IN ACCESS SHARE MODE",
 							  fmtQualifiedDumpable(&tblinfo[i]));
-			ExecuteSqlStatement(fout, query->data);
+			ExecuteSqlStatementAH(fout, query->data);
 		}
 
 		/* Emit notice if join for owner failed */
@@ -6926,7 +6926,7 @@ getTables(Archive *fout, int *numTables)
 
 	if (dopt->lockWaitTimeout)
 	{
-		ExecuteSqlStatement(fout, "SET statement_timeout = 0");
+		ExecuteSqlStatementAH(fout, "SET statement_timeout = 0");
 	}
 
 	PQclear(res);
@@ -7021,7 +7021,7 @@ getInherits(Archive *fout, int *numInherits)
 	/* find all the inheritance information */
 	appendPQExpBufferStr(query, "SELECT inhrelid, inhparent FROM pg_inherits");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -7365,7 +7365,7 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
 							  tbinfo->dobj.catId.oid);
 		}
 
-		res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 		ntups = PQntuples(res);
 
@@ -7512,7 +7512,7 @@ getExtendedStatistics(Archive *fout)
 						  "FROM pg_catalog.pg_statistic_ext",
 						  username_subquery);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -7610,7 +7610,7 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
 							  "WHERE conrelid = '%u'::pg_catalog.oid "
 							  "AND contype = 'f'",
 							  tbinfo->dobj.catId.oid);
-		res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 		ntups = PQntuples(res);
 
@@ -7746,7 +7746,7 @@ getDomainConstraints(Archive *fout, TypeInfo *tyinfo)
 						  "ORDER BY conname",
 						  tyinfo->dobj.catId.oid);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -7839,7 +7839,7 @@ getRules(Archive *fout, int *numRules)
 							 "ORDER BY oid");
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -8013,7 +8013,7 @@ getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
 							  tbinfo->dobj.catId.oid);
 		}
 
-		res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 		ntups = PQntuples(res);
 
@@ -8148,7 +8148,7 @@ getEventTriggers(Archive *fout, int *numEventTriggers)
 					  "ORDER BY e.oid",
 					  username_subquery);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -8318,7 +8318,7 @@ getProcLangs(Archive *fout, int *numProcLangs)
 						  username_subquery);
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -8427,7 +8427,7 @@ getCasts(Archive *fout, int *numCasts)
 							 "FROM pg_cast ORDER BY 3,4");
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -8495,7 +8495,7 @@ get_language_name(Archive *fout, Oid langid)
 
 	query = createPQExpBuffer();
 	appendPQExpBuffer(query, "SELECT lanname FROM pg_language WHERE oid = %u", langid);
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 	lanname = pg_strdup(fmtId(PQgetvalue(res, 0, 0)));
 	destroyPQExpBuffer(query);
 	PQclear(res);
@@ -8538,7 +8538,7 @@ getTransforms(Archive *fout, int *numTransforms)
 						 "FROM pg_transform "
 						 "ORDER BY 3,4");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -8720,7 +8720,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
 						  "ORDER BY a.attnum",
 						  tbinfo->dobj.catId.oid);
 
-		res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, q->data, PGRES_TUPLES_OK);
 
 		ntups = PQntuples(res);
 
@@ -8797,7 +8797,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
 							  "WHERE adrelid = '%u'::pg_catalog.oid",
 							  tbinfo->dobj.catId.oid);
 
-			res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
+			res = ExecuteSqlQueryAH(fout, q->data, PGRES_TUPLES_OK);
 
 			numDefaults = PQntuples(res);
 			attrdefs = (AttrDefInfo *) pg_malloc(numDefaults * sizeof(AttrDefInfo));
@@ -8919,7 +8919,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
 								  tbinfo->dobj.catId.oid);
 			}
 
-			res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
+			res = ExecuteSqlQueryAH(fout, q->data, PGRES_TUPLES_OK);
 
 			numConstrs = PQntuples(res);
 			if (numConstrs != tbinfo->ncheck)
@@ -9062,7 +9062,7 @@ getTSParsers(Archive *fout, int *numTSParsers)
 						 "prsend::oid, prsheadline::oid, prslextype::oid "
 						 "FROM pg_ts_parser");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numTSParsers = ntups;
@@ -9146,7 +9146,7 @@ getTSDictionaries(Archive *fout, int *numTSDicts)
 					  "FROM pg_ts_dict",
 					  username_subquery);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numTSDicts = ntups;
@@ -9226,7 +9226,7 @@ getTSTemplates(Archive *fout, int *numTSTemplates)
 						 "tmplnamespace, tmplinit::oid, tmpllexize::oid "
 						 "FROM pg_ts_template");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numTSTemplates = ntups;
@@ -9302,7 +9302,7 @@ getTSConfigurations(Archive *fout, int *numTSConfigs)
 					  "FROM pg_ts_config",
 					  username_subquery);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numTSConfigs = ntups;
@@ -9455,7 +9455,7 @@ getForeignDataWrappers(Archive *fout, int *numForeignDataWrappers)
 						  username_subquery);
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numForeignDataWrappers = ntups;
@@ -9603,7 +9603,7 @@ getForeignServers(Archive *fout, int *numForeignServers)
 						  username_subquery);
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numForeignServers = ntups;
@@ -9742,7 +9742,7 @@ getDefaultACLs(Archive *fout, int *numDefaultACLs)
 						  username_subquery);
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	*numDefaultACLs = ntups;
@@ -10098,7 +10098,7 @@ collectComments(Archive *fout, CommentItem **items)
 						 "FROM pg_catalog.pg_description "
 						 "ORDER BY classoid, objoid, objsubid");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	/* Construct lookup table containing OIDs in numeric form */
 
@@ -10546,7 +10546,7 @@ dumpEnumType(Archive *fout, TypeInfo *tyinfo)
 						  "ORDER BY oid",
 						  tyinfo->dobj.catId.oid);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	num = PQntuples(res);
 
@@ -10684,7 +10684,7 @@ dumpRangeType(Archive *fout, TypeInfo *tyinfo)
 					  "rngtypid = '%u'",
 					  tyinfo->dobj.catId.oid);
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
 	qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
@@ -10942,7 +10942,7 @@ dumpBaseType(Archive *fout, TypeInfo *tyinfo)
 					  "WHERE oid = '%u'::pg_catalog.oid",
 					  tyinfo->dobj.catId.oid);
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
 	typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
@@ -11165,7 +11165,7 @@ dumpDomain(Archive *fout, TypeInfo *tyinfo)
 						  tyinfo->dobj.catId.oid);
 	}
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull"));
 	typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn"));
@@ -11357,7 +11357,7 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
 						  tyinfo->dobj.catId.oid);
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -11536,7 +11536,7 @@ dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo)
 					  tyinfo->typrelid);
 
 	/* Fetch column attnames */
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	if (ntups < 1)
@@ -12064,7 +12064,7 @@ dumpFunc(Archive *fout, FuncInfo *finfo)
 					  "WHERE oid = '%u'::pg_catalog.oid",
 					  finfo->dobj.catId.oid);
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset"));
 	prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc"));
@@ -12754,7 +12754,7 @@ dumpOpr(Archive *fout, OprInfo *oprinfo)
 						  oprinfo->dobj.catId.oid);
 	}
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	i_oprkind = PQfnumber(res, "oprkind");
 	i_oprcode = PQfnumber(res, "oprcode");
@@ -12977,7 +12977,7 @@ convertTSFunction(Archive *fout, Oid funcOid)
 
 	snprintf(query, sizeof(query),
 			 "SELECT '%u'::pg_catalog.regproc", funcOid);
-	res = ExecuteSqlQueryForSingleRow(fout, query);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query);
 
 	result = pg_strdup(PQgetvalue(res, 0, 0));
 
@@ -13140,7 +13140,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
 						  opcinfo->dobj.catId.oid);
 	}
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	i_opcintype = PQfnumber(res, "opcintype");
 	i_opckeytype = PQfnumber(res, "opckeytype");
@@ -13268,7 +13268,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
 						  opcinfo->dobj.catId.oid);
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -13347,7 +13347,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
 						  opcinfo->dobj.catId.oid);
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -13530,7 +13530,7 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
 						  opfinfo->dobj.catId.oid);
 	}
 
-	res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res_ops = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	resetPQExpBuffer(query);
 
@@ -13546,7 +13546,7 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
 					  "ORDER BY amprocnum",
 					  opfinfo->dobj.catId.oid);
 
-	res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res_procs = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	/* Get additional fields from the pg_opfamily row */
 	resetPQExpBuffer(query);
@@ -13557,7 +13557,7 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
 					  "WHERE oid = '%u'::pg_catalog.oid",
 					  opfinfo->dobj.catId.oid);
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	i_amname = PQfnumber(res, "amname");
 
@@ -13744,7 +13744,7 @@ dumpCollation(Archive *fout, CollInfo *collinfo)
 					  "WHERE c.oid = '%u'::pg_catalog.oid",
 					  collinfo->dobj.catId.oid);
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	i_collprovider = PQfnumber(res, "collprovider");
 	i_collisdeterministic = PQfnumber(res, "collisdeterministic");
@@ -13861,7 +13861,7 @@ dumpConversion(Archive *fout, ConvInfo *convinfo)
 					  "WHERE c.oid = '%u'::pg_catalog.oid",
 					  convinfo->dobj.catId.oid);
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	i_conforencoding = PQfnumber(res, "conforencoding");
 	i_contoencoding = PQfnumber(res, "contoencoding");
@@ -14078,7 +14078,7 @@ dumpAgg(Archive *fout, AggInfo *agginfo)
 					  "AND p.oid = '%u'::pg_catalog.oid",
 					  agginfo->aggfn.dobj.catId.oid);
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	i_agginitval = PQfnumber(res, "agginitval");
 	i_aggminitval = PQfnumber(res, "aggminitval");
@@ -14413,7 +14413,7 @@ dumpTSDictionary(Archive *fout, TSDictInfo *dictinfo)
 					  "FROM pg_ts_template p, pg_namespace n "
 					  "WHERE p.oid = '%u' AND n.oid = tmplnamespace",
 					  dictinfo->dicttemplate);
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 	nspname = PQgetvalue(res, 0, 0);
 	tmplname = PQgetvalue(res, 0, 1);
 
@@ -14555,7 +14555,7 @@ dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo)
 					  "FROM pg_ts_parser p, pg_namespace n "
 					  "WHERE p.oid = '%u' AND n.oid = prsnamespace",
 					  cfginfo->cfgparser);
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 	nspname = PQgetvalue(res, 0, 0);
 	prsname = PQgetvalue(res, 0, 1);
 
@@ -14578,7 +14578,7 @@ dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo)
 					  "ORDER BY m.mapcfg, m.maptokentype, m.mapseqno",
 					  cfginfo->cfgparser, cfginfo->dobj.catId.oid);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 	ntups = PQntuples(res);
 
 	i_tokenname = PQfnumber(res, "tokenname");
@@ -14742,7 +14742,7 @@ dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo)
 					  "FROM pg_foreign_data_wrapper w "
 					  "WHERE w.oid = '%u'",
 					  srvinfo->srvfdw);
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 	fdwname = PQgetvalue(res, 0, 0);
 
 	appendPQExpBuffer(q, "CREATE SERVER %s", qsrvname);
@@ -14858,7 +14858,7 @@ dumpUserMappings(Archive *fout,
 					  "ORDER BY usename",
 					  catalogId.oid);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 	i_usename = PQfnumber(res, "usename");
@@ -15379,7 +15379,7 @@ collectSecLabels(Archive *fout, SecLabelItem **items)
 						 "FROM pg_catalog.pg_seclabel "
 						 "ORDER BY classoid, objoid, objsubid");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	/* Construct lookup table containing OIDs in numeric form */
 	i_label = PQfnumber(res, "label");
@@ -15515,7 +15515,7 @@ dumpTable(Archive *fout, TableInfo *tbinfo)
 							  tbinfo->dobj.catId.oid);
 		}
 
-		res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 		for (i = 0; i < PQntuples(res); i++)
 		{
@@ -15565,7 +15565,7 @@ createViewAsClause(Archive *fout, TableInfo *tbinfo)
 					  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
 					  tbinfo->dobj.catId.oid);
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	if (PQntuples(res) != 1)
 	{
@@ -15740,7 +15740,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
 									  "ON (fs.oid = ft.ftserver) "
 									  "WHERE ft.ftrelid = '%u'",
 									  tbinfo->dobj.catId.oid);
-					res = ExecuteSqlQueryForSingleRow(fout, query->data);
+					res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 					i_srvname = PQfnumber(res, "srvname");
 					i_ftoptions = PQfnumber(res, "ftoptions");
 					srvname = pg_strdup(PQgetvalue(res, 0, i_srvname));
@@ -16693,7 +16693,7 @@ dumpStatisticsExt(Archive *fout, StatsExtInfo *statsextinfo)
 					  "pg_catalog.pg_get_statisticsobjdef('%u'::pg_catalog.oid)",
 					  statsextinfo->dobj.catId.oid);
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	stxdef = PQgetvalue(res, 0, 0);
 
@@ -17061,7 +17061,7 @@ findLastBuiltinOid_V71(Archive *fout)
 	PGresult   *res;
 	Oid			last_oid;
 
-	res = ExecuteSqlQueryForSingleRow(fout,
+	res = ExecuteSqlQueryForSingleRowAH(fout,
 									  "SELECT datlastsysoid FROM pg_database WHERE datname = current_database()");
 	last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "datlastsysoid")));
 	PQclear(res);
@@ -17130,7 +17130,7 @@ dumpSequence(Archive *fout, TableInfo *tbinfo)
 						  fmtQualifiedDumpable(tbinfo));
 	}
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	if (PQntuples(res) != 1)
 	{
@@ -17353,7 +17353,7 @@ dumpSequenceData(Archive *fout, TableDataInfo *tdinfo)
 					  "SELECT last_value, is_called FROM %s",
 					  fmtQualifiedDumpable(tbinfo));
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	if (PQntuples(res) != 1)
 	{
@@ -17761,7 +17761,7 @@ dumpRule(Archive *fout, RuleInfo *rinfo)
 						  "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid)",
 						  rinfo->dobj.catId.oid);
 
-		res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+		res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 		if (PQntuples(res) != 1)
 		{
@@ -17891,7 +17891,7 @@ getExtensionMembership(Archive *fout, ExtensionInfo extinfo[],
 						 "AND deptype = 'e' "
 						 "ORDER BY 3");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -18090,7 +18090,7 @@ processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
 					  "AND refclassid = 'pg_extension'::regclass "
 					  "AND classid = 'pg_class'::regclass;");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 	ntups = PQntuples(res);
 
 	i_conrelid = PQfnumber(res, "conrelid");
@@ -18196,7 +18196,7 @@ getDependencies(Archive *fout)
 	/* Sort the output for efficiency below */
 	appendPQExpBufferStr(query, "ORDER BY 1,2");
 
-	res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+	res = ExecuteSqlQueryAH(fout, query->data, PGRES_TUPLES_OK);
 
 	ntups = PQntuples(res);
 
@@ -18549,7 +18549,7 @@ getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
 	appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)",
 					  oid);
 
-	res = ExecuteSqlQueryForSingleRow(fout, query->data);
+	res = ExecuteSqlQueryForSingleRowAH(fout, query->data);
 
 	/* result of format_type is already quoted */
 	result = pg_strdup(PQgetvalue(res, 0, 0));
-- 
2.21.1 (Apple Git-122.3)

