Commit 1631598e authored by Robert Haas's avatar Robert Haas

pg_dump: Further reduce reliance on global variables.

This is another round of refactoring to make things simpler for parallel
pg_dump.  pg_dump.c now issues SQL queries through the relevant Archive
object, rather than relying on the global variable g_conn.  This commit
isn't quite enough to get rid of g_conn entirely, but it makes a big
dent in its utilization and, along the way, manages to be slightly less
code than before.
parent a347f96b
...@@ -1453,6 +1453,16 @@ die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) ...@@ -1453,6 +1453,16 @@ die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...)
va_end(ap); va_end(ap);
} }
/* As above, but with a complaint about a particular query. */
void
die_on_query_failure(ArchiveHandle *AH, const char *modulename,
const char *query)
{
write_msg(modulename, "query failed: %s",
PQerrorMessage(AH->connection));
die_horribly(AH, modulename, "query was: %s\n", query);
}
/* on some error, we may decide to go on... */ /* on some error, we may decide to go on... */
void void
warn_or_die_horribly(ArchiveHandle *AH, warn_or_die_horribly(ArchiveHandle *AH,
......
...@@ -325,6 +325,7 @@ typedef struct _tocEntry ...@@ -325,6 +325,7 @@ typedef struct _tocEntry
extern void die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4), noreturn)); extern void die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4), noreturn));
extern void die_on_query_failure(ArchiveHandle *AH, const char *modulename, const char *query) __attribute__((noreturn));
extern void warn_or_die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4))); extern void warn_or_die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
extern void WriteTOC(ArchiveHandle *AH); extern void WriteTOC(ArchiveHandle *AH);
......
...@@ -318,6 +318,30 @@ notice_processor(void *arg, const char *message) ...@@ -318,6 +318,30 @@ notice_processor(void *arg, const char *message)
} }
void
ExecuteSqlStatement(Archive *AHX, const char *query)
{
ArchiveHandle *AH = (ArchiveHandle *) AHX;
PGresult *res;
res = PQexec(AH->connection, query);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
die_on_query_failure(AH, modulename, query);
PQclear(res);
}
PGresult *
ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
{
ArchiveHandle *AH = (ArchiveHandle *) AHX;
PGresult *res;
res = PQexec(AH->connection, query);
if (PQresultStatus(res) != status)
die_on_query_failure(AH, modulename, query);
return res;
}
/* /*
* Convenience function to send a query. * Convenience function to send a query.
* Monitors result to detect COPY statements * Monitors result to detect COPY statements
......
...@@ -12,6 +12,10 @@ ...@@ -12,6 +12,10 @@
extern int ExecuteSqlCommandBuf(ArchiveHandle *AH, const char *buf, size_t bufLen); extern int ExecuteSqlCommandBuf(ArchiveHandle *AH, 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 void EndDBCopyMode(ArchiveHandle *AH, struct _tocEntry * te); extern void EndDBCopyMode(ArchiveHandle *AH, struct _tocEntry * te);
extern void StartTransaction(ArchiveHandle *AH); extern void StartTransaction(ArchiveHandle *AH);
......
...@@ -57,6 +57,7 @@ ...@@ -57,6 +57,7 @@
#include "libpq/libpq-fs.h" #include "libpq/libpq-fs.h"
#include "pg_backup_archiver.h" #include "pg_backup_archiver.h"
#include "pg_backup_db.h"
#include "dumpmem.h" #include "dumpmem.h"
#include "dumputils.h" #include "dumputils.h"
...@@ -150,7 +151,8 @@ static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode); ...@@ -150,7 +151,8 @@ static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode);
static void expand_schema_name_patterns(Archive *fout, static void expand_schema_name_patterns(Archive *fout,
SimpleStringList *patterns, SimpleStringList *patterns,
SimpleOidList *oids); SimpleOidList *oids);
static void expand_table_name_patterns(SimpleStringList *patterns, static void expand_table_name_patterns(Archive *fout,
SimpleStringList *patterns,
SimpleOidList *oids); SimpleOidList *oids);
static NamespaceInfo *findNamespace(Archive *fout, Oid nsoid, Oid objoid); static NamespaceInfo *findNamespace(Archive *fout, Oid nsoid, Oid objoid);
static void dumpTableData(Archive *fout, TableDataInfo *tdinfo); static void dumpTableData(Archive *fout, TableDataInfo *tdinfo);
...@@ -228,9 +230,9 @@ static char *format_function_signature(Archive *fout, ...@@ -228,9 +230,9 @@ static char *format_function_signature(Archive *fout,
static const char *convertRegProcReference(Archive *fout, static const char *convertRegProcReference(Archive *fout,
const char *proc); const char *proc);
static const char *convertOperatorReference(Archive *fout, const char *opr); static const char *convertOperatorReference(Archive *fout, const char *opr);
static const char *convertTSFunction(Oid funcOid); static const char *convertTSFunction(Archive *fout, Oid funcOid);
static Oid findLastBuiltinOid_V71(Archive *fout, const char *); static Oid findLastBuiltinOid_V71(Archive *fout, const char *);
static Oid findLastBuiltinOid_V70(void); static Oid findLastBuiltinOid_V70(Archive *fout);
static void selectSourceSchema(Archive *fout, const char *schemaName); static void selectSourceSchema(Archive *fout, const char *schemaName);
static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts); static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
static char *myFormatType(const char *typname, int32 typmod); static char *myFormatType(const char *typname, int32 typmod);
...@@ -242,20 +244,18 @@ static int dumpBlobs(Archive *fout, void *arg); ...@@ -242,20 +244,18 @@ static int dumpBlobs(Archive *fout, void *arg);
static void dumpDatabase(Archive *AH); static void dumpDatabase(Archive *AH);
static void dumpEncoding(Archive *AH); static void dumpEncoding(Archive *AH);
static void dumpStdStrings(Archive *AH); static void dumpStdStrings(Archive *AH);
static void binary_upgrade_set_type_oids_by_type_oid( static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
PQExpBuffer upgrade_buffer, Oid pg_type_oid); PQExpBuffer upgrade_buffer, Oid pg_type_oid);
static bool binary_upgrade_set_type_oids_by_rel_oid( static bool binary_upgrade_set_type_oids_by_rel_oid(Archive *fout,
PQExpBuffer upgrade_buffer, Oid pg_rel_oid); PQExpBuffer upgrade_buffer, Oid pg_rel_oid);
static void binary_upgrade_set_pg_class_oids(PQExpBuffer upgrade_buffer, static void binary_upgrade_set_pg_class_oids(Archive *fout,
PQExpBuffer upgrade_buffer,
Oid pg_class_oid, bool is_index); Oid pg_class_oid, bool is_index);
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
DumpableObject *dobj, DumpableObject *dobj,
const char *objlabel); const char *objlabel);
static const char *getAttrName(int attrnum, TableInfo *tblInfo); static const char *getAttrName(int attrnum, TableInfo *tblInfo);
static const char *fmtCopyColumnList(const TableInfo *ti); static const char *fmtCopyColumnList(const TableInfo *ti);
static void do_sql_command(PGconn *conn, const char *query);
static void check_sql_result(PGresult *res, PGconn *conn, const char *query,
ExecStatusType expected);
int int
main(int argc, char **argv) main(int argc, char **argv)
...@@ -626,19 +626,21 @@ main(int argc, char **argv) ...@@ -626,19 +626,21 @@ main(int argc, char **argv)
/* /*
* Start transaction-snapshot mode transaction to dump consistent data. * Start transaction-snapshot mode transaction to dump consistent data.
*/ */
do_sql_command(g_conn, "BEGIN"); ExecuteSqlStatement(fout, "BEGIN");
if (fout->remoteVersion >= 90100) if (fout->remoteVersion >= 90100)
{ {
if (serializable_deferrable) if (serializable_deferrable)
do_sql_command(g_conn, ExecuteSqlStatement(fout,
"SET TRANSACTION ISOLATION LEVEL SERIALIZABLE, " "SET TRANSACTION ISOLATION LEVEL "
"READ ONLY, DEFERRABLE"); "SERIALIZABLE, READ ONLY, DEFERRABLE");
else else
do_sql_command(g_conn, ExecuteSqlStatement(fout,
"SET TRANSACTION ISOLATION LEVEL REPEATABLE READ"); "SET TRANSACTION ISOLATION LEVEL "
"REPEATABLE READ");
} }
else else
do_sql_command(g_conn, "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE"); ExecuteSqlStatement(fout,
"SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
/* Select the appropriate subquery to convert user IDs to names */ /* Select the appropriate subquery to convert user IDs to names */
if (fout->remoteVersion >= 80100) if (fout->remoteVersion >= 80100)
...@@ -654,7 +656,7 @@ main(int argc, char **argv) ...@@ -654,7 +656,7 @@ main(int argc, char **argv)
if (fout->remoteVersion >= 70100) if (fout->remoteVersion >= 70100)
g_last_builtin_oid = findLastBuiltinOid_V71(fout, PQdb(g_conn)); g_last_builtin_oid = findLastBuiltinOid_V71(fout, PQdb(g_conn));
else else
g_last_builtin_oid = findLastBuiltinOid_V70(); g_last_builtin_oid = findLastBuiltinOid_V70(fout);
if (g_verbose) if (g_verbose)
write_msg(NULL, "last built-in OID is %u\n", g_last_builtin_oid); write_msg(NULL, "last built-in OID is %u\n", g_last_builtin_oid);
} }
...@@ -677,7 +679,7 @@ main(int argc, char **argv) ...@@ -677,7 +679,7 @@ main(int argc, char **argv)
/* Expand table selection patterns into OID lists */ /* Expand table selection patterns into OID lists */
if (table_include_patterns.head != NULL) if (table_include_patterns.head != NULL)
{ {
expand_table_name_patterns(&table_include_patterns, expand_table_name_patterns(fout, &table_include_patterns,
&table_include_oids); &table_include_oids);
if (table_include_oids.head == NULL) if (table_include_oids.head == NULL)
{ {
...@@ -685,10 +687,10 @@ main(int argc, char **argv) ...@@ -685,10 +687,10 @@ main(int argc, char **argv)
exit_nicely(); exit_nicely();
} }
} }
expand_table_name_patterns(&table_exclude_patterns, expand_table_name_patterns(fout, &table_exclude_patterns,
&table_exclude_oids); &table_exclude_oids);
expand_table_name_patterns(&tabledata_exclude_patterns, expand_table_name_patterns(fout, &tabledata_exclude_patterns,
&tabledata_exclude_oids); &tabledata_exclude_oids);
/* non-matching exclusion patterns aren't an error */ /* non-matching exclusion patterns aren't an error */
...@@ -896,44 +898,44 @@ setup_connection(Archive *AH, const char *dumpencoding, char *use_role) ...@@ -896,44 +898,44 @@ setup_connection(Archive *AH, const char *dumpencoding, char *use_role)
PQExpBuffer query = createPQExpBuffer(); PQExpBuffer query = createPQExpBuffer();
appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role)); appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
do_sql_command(g_conn, query->data); ExecuteSqlStatement(AH, query->data);
destroyPQExpBuffer(query); destroyPQExpBuffer(query);
} }
/* Set the datestyle to ISO to ensure the dump's portability */ /* Set the datestyle to ISO to ensure the dump's portability */
do_sql_command(g_conn, "SET DATESTYLE = ISO"); ExecuteSqlStatement(AH, "SET DATESTYLE = ISO");
/* Likewise, avoid using sql_standard intervalstyle */ /* Likewise, avoid using sql_standard intervalstyle */
if (AH->remoteVersion >= 80400) if (AH->remoteVersion >= 80400)
do_sql_command(g_conn, "SET INTERVALSTYLE = POSTGRES"); ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES");
/* /*
* If supported, set extra_float_digits so that we can dump float data * If supported, set extra_float_digits so that we can dump float data
* exactly (given correctly implemented float I/O code, anyway) * exactly (given correctly implemented float I/O code, anyway)
*/ */
if (AH->remoteVersion >= 90000) if (AH->remoteVersion >= 90000)
do_sql_command(g_conn, "SET extra_float_digits TO 3"); ExecuteSqlStatement(AH, "SET extra_float_digits TO 3");
else if (AH->remoteVersion >= 70400) else if (AH->remoteVersion >= 70400)
do_sql_command(g_conn, "SET extra_float_digits TO 2"); ExecuteSqlStatement(AH, "SET extra_float_digits TO 2");
/* /*
* If synchronized scanning is supported, disable it, to prevent * If synchronized scanning is supported, disable it, to prevent
* unpredictable changes in row ordering across a dump and reload. * unpredictable changes in row ordering across a dump and reload.
*/ */
if (AH->remoteVersion >= 80300) if (AH->remoteVersion >= 80300)
do_sql_command(g_conn, "SET synchronize_seqscans TO off"); ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off");
/* /*
* Disable timeouts if supported. * Disable timeouts if supported.
*/ */
if (AH->remoteVersion >= 70300) if (AH->remoteVersion >= 70300)
do_sql_command(g_conn, "SET statement_timeout = 0"); ExecuteSqlStatement(AH, "SET statement_timeout = 0");
/* /*
* Quote all identifiers, if requested. * Quote all identifiers, if requested.
*/ */
if (quote_all_identifiers && AH->remoteVersion >= 90100) if (quote_all_identifiers && AH->remoteVersion >= 90100)
do_sql_command(g_conn, "SET quote_all_identifiers = true"); ExecuteSqlStatement(AH, "SET quote_all_identifiers = true");
} }
static ArchiveFormat static ArchiveFormat
...@@ -1021,8 +1023,7 @@ expand_schema_name_patterns(Archive *fout, ...@@ -1021,8 +1023,7 @@ expand_schema_name_patterns(Archive *fout,
NULL); NULL);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
for (i = 0; i < PQntuples(res); i++) for (i = 0; i < PQntuples(res); i++)
{ {
...@@ -1038,7 +1039,8 @@ expand_schema_name_patterns(Archive *fout, ...@@ -1038,7 +1039,8 @@ expand_schema_name_patterns(Archive *fout,
* and append them to the given OID list. * and append them to the given OID list.
*/ */
static void static void
expand_table_name_patterns(SimpleStringList *patterns, SimpleOidList *oids) expand_table_name_patterns(Archive *fout,
SimpleStringList *patterns, SimpleOidList *oids)
{ {
PQExpBuffer query; PQExpBuffer query;
PGresult *res; PGresult *res;
...@@ -1071,8 +1073,7 @@ expand_table_name_patterns(SimpleStringList *patterns, SimpleOidList *oids) ...@@ -1071,8 +1073,7 @@ expand_table_name_patterns(SimpleStringList *patterns, SimpleOidList *oids)
"pg_catalog.pg_table_is_visible(c.oid)"); "pg_catalog.pg_table_is_visible(c.oid)");
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
for (i = 0; i < PQntuples(res); i++) for (i = 0; i < PQntuples(res); i++)
{ {
...@@ -1335,8 +1336,7 @@ dumpTableData_copy(Archive *fout, void *dcontext) ...@@ -1335,8 +1336,7 @@ dumpTableData_copy(Archive *fout, void *dcontext)
classname), classname),
column_list); column_list);
} }
res = PQexec(g_conn, q->data); res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT);
check_sql_result(res, g_conn, q->data, PGRES_COPY_OUT);
PQclear(res); PQclear(res);
for (;;) for (;;)
...@@ -1411,7 +1411,13 @@ dumpTableData_copy(Archive *fout, void *dcontext) ...@@ -1411,7 +1411,13 @@ dumpTableData_copy(Archive *fout, void *dcontext)
/* Check command status and return to normal libpq state */ /* Check command status and return to normal libpq state */
res = PQgetResult(g_conn); res = PQgetResult(g_conn);
check_sql_result(res, g_conn, q->data, PGRES_COMMAND_OK); if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
write_msg(NULL, "Dumping the contents of table \"%s\" failed: PQgetResult() failed.\n", classname);
write_msg(NULL, "Error message from server: %s", PQerrorMessage(g_conn));
write_msg(NULL, "The command was: %s\n", q->data);
exit_nicely();
}
PQclear(res); PQclear(res);
destroyPQExpBuffer(q); destroyPQExpBuffer(q);
...@@ -1465,16 +1471,12 @@ dumpTableData_insert(Archive *fout, void *dcontext) ...@@ -1465,16 +1471,12 @@ dumpTableData_insert(Archive *fout, void *dcontext)
if (tdinfo->filtercond) if (tdinfo->filtercond)
appendPQExpBuffer(q, " %s", tdinfo->filtercond); appendPQExpBuffer(q, " %s", tdinfo->filtercond);
res = PQexec(g_conn, q->data); ExecuteSqlStatement(fout, q->data);
check_sql_result(res, g_conn, q->data, PGRES_COMMAND_OK);
do while (1)
{ {
PQclear(res); res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor",
PGRES_TUPLES_OK);
res = PQexec(g_conn, "FETCH 100 FROM _pg_dump_cursor");
check_sql_result(res, g_conn, "FETCH 100 FROM _pg_dump_cursor",
PGRES_TUPLES_OK);
nfields = PQnfields(res); nfields = PQnfields(res);
for (tuple = 0; tuple < PQntuples(res); tuple++) for (tuple = 0; tuple < PQntuples(res); tuple++)
{ {
...@@ -1565,13 +1567,18 @@ dumpTableData_insert(Archive *fout, void *dcontext) ...@@ -1565,13 +1567,18 @@ dumpTableData_insert(Archive *fout, void *dcontext)
} }
archprintf(fout, ");\n"); archprintf(fout, ");\n");
} }
} while (PQntuples(res) > 0);
PQclear(res); if (PQntuples(res) <= 0)
{
PQclear(res);
break;
}
PQclear(res);
}
archprintf(fout, "\n\n"); archprintf(fout, "\n\n");
do_sql_command(g_conn, "CLOSE _pg_dump_cursor"); ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
destroyPQExpBuffer(q); destroyPQExpBuffer(q);
return 1; return 1;
...@@ -1917,8 +1924,7 @@ dumpDatabase(Archive *fout) ...@@ -1917,8 +1924,7 @@ dumpDatabase(Archive *fout)
appendStringLiteralAH(dbQry, datname, fout); appendStringLiteralAH(dbQry, datname, fout);
} }
res = PQexec(g_conn, dbQry->data); res = ExecuteSqlQuery(fout, dbQry->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, dbQry->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -2030,8 +2036,7 @@ dumpDatabase(Archive *fout) ...@@ -2030,8 +2036,7 @@ dumpDatabase(Archive *fout)
"WHERE oid = %u;\n", "WHERE oid = %u;\n",
LargeObjectRelationId); LargeObjectRelationId);
lo_res = PQexec(g_conn, loFrozenQry->data); lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK);
check_sql_result(lo_res, g_conn, loFrozenQry->data, PGRES_TUPLES_OK);
if (PQntuples(lo_res) != 1) if (PQntuples(lo_res) != 1)
{ {
...@@ -2069,8 +2074,7 @@ dumpDatabase(Archive *fout) ...@@ -2069,8 +2074,7 @@ dumpDatabase(Archive *fout)
"WHERE oid = %u;\n", "WHERE oid = %u;\n",
LargeObjectMetadataRelationId); LargeObjectMetadataRelationId);
lo_res = PQexec(g_conn, loFrozenQry->data); lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK);
check_sql_result(lo_res, g_conn, loFrozenQry->data, PGRES_TUPLES_OK);
if (PQntuples(lo_res) != 1) if (PQntuples(lo_res) != 1)
{ {
...@@ -2143,8 +2147,7 @@ dumpDatabase(Archive *fout) ...@@ -2143,8 +2147,7 @@ dumpDatabase(Archive *fout)
PQExpBuffer seclabelQry = createPQExpBuffer(); PQExpBuffer seclabelQry = createPQExpBuffer();
buildShSecLabelQuery(g_conn, "pg_database", dbCatId.oid, seclabelQry); buildShSecLabelQuery(g_conn, "pg_database", dbCatId.oid, seclabelQry);
res = PQexec(g_conn, seclabelQry->data); res = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, seclabelQry->data, PGRES_TUPLES_OK);
resetPQExpBuffer(seclabelQry); resetPQExpBuffer(seclabelQry);
emitShSecLabels(g_conn, res, seclabelQry, "DATABASE", datname); emitShSecLabels(g_conn, res, seclabelQry, "DATABASE", datname);
if (strlen(seclabelQry->data)) if (strlen(seclabelQry->data))
...@@ -2251,8 +2254,7 @@ getBlobs(Archive *fout) ...@@ -2251,8 +2254,7 @@ getBlobs(Archive *fout)
"SELECT oid, NULL::oid, NULL::oid" "SELECT oid, NULL::oid, NULL::oid"
" FROM pg_class WHERE relkind = 'l'"); " FROM pg_class WHERE relkind = 'l'");
res = PQexec(g_conn, blobQry->data); res = ExecuteSqlQuery(fout, blobQry->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, blobQry->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
if (ntups > 0) if (ntups > 0)
...@@ -2379,19 +2381,15 @@ dumpBlobs(Archive *fout, void *arg) ...@@ -2379,19 +2381,15 @@ dumpBlobs(Archive *fout, void *arg)
else else
blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_class WHERE relkind = 'l'"; blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_class WHERE relkind = 'l'";
res = PQexec(g_conn, blobQry); ExecuteSqlStatement(fout, blobQry);
check_sql_result(res, g_conn, blobQry, PGRES_COMMAND_OK);
/* Command to fetch from cursor */ /* Command to fetch from cursor */
blobFetchQry = "FETCH 1000 IN bloboid"; blobFetchQry = "FETCH 1000 IN bloboid";
do do
{ {
PQclear(res);
/* Do a fetch */ /* Do a fetch */
res = PQexec(g_conn, blobFetchQry); res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, blobFetchQry, PGRES_TUPLES_OK);
/* Process the tuples, if any */ /* Process the tuples, if any */
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -2430,6 +2428,8 @@ dumpBlobs(Archive *fout, void *arg) ...@@ -2430,6 +2428,8 @@ dumpBlobs(Archive *fout, void *arg)
EndBlob(fout, blobOid); EndBlob(fout, blobOid);
} }
PQclear(res);
} while (ntups > 0); } while (ntups > 0);
PQclear(res); PQclear(res);
...@@ -2438,7 +2438,8 @@ dumpBlobs(Archive *fout, void *arg) ...@@ -2438,7 +2438,8 @@ dumpBlobs(Archive *fout, void *arg)
} }
static void static void
binary_upgrade_set_type_oids_by_type_oid(PQExpBuffer upgrade_buffer, binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
PQExpBuffer upgrade_buffer,
Oid pg_type_oid) Oid pg_type_oid)
{ {
PQExpBuffer upgrade_query = createPQExpBuffer(); PQExpBuffer upgrade_query = createPQExpBuffer();
...@@ -2458,8 +2459,7 @@ binary_upgrade_set_type_oids_by_type_oid(PQExpBuffer upgrade_buffer, ...@@ -2458,8 +2459,7 @@ binary_upgrade_set_type_oids_by_type_oid(PQExpBuffer upgrade_buffer,
"WHERE pg_type.oid = '%u'::pg_catalog.oid;", "WHERE pg_type.oid = '%u'::pg_catalog.oid;",
pg_type_oid); pg_type_oid);
upgrade_res = PQexec(g_conn, upgrade_query->data); upgrade_res = ExecuteSqlQuery(fout, upgrade_query->data, PGRES_TUPLES_OK);
check_sql_result(upgrade_res, g_conn, upgrade_query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(upgrade_res); ntups = PQntuples(upgrade_res);
...@@ -2488,7 +2488,8 @@ binary_upgrade_set_type_oids_by_type_oid(PQExpBuffer upgrade_buffer, ...@@ -2488,7 +2488,8 @@ binary_upgrade_set_type_oids_by_type_oid(PQExpBuffer upgrade_buffer,
} }
static bool static bool
binary_upgrade_set_type_oids_by_rel_oid(PQExpBuffer upgrade_buffer, binary_upgrade_set_type_oids_by_rel_oid(Archive *fout,
PQExpBuffer upgrade_buffer,
Oid pg_rel_oid) Oid pg_rel_oid)
{ {
PQExpBuffer upgrade_query = createPQExpBuffer(); PQExpBuffer upgrade_query = createPQExpBuffer();
...@@ -2506,8 +2507,7 @@ binary_upgrade_set_type_oids_by_rel_oid(PQExpBuffer upgrade_buffer, ...@@ -2506,8 +2507,7 @@ binary_upgrade_set_type_oids_by_rel_oid(PQExpBuffer upgrade_buffer,
"WHERE c.oid = '%u'::pg_catalog.oid;", "WHERE c.oid = '%u'::pg_catalog.oid;",
pg_rel_oid); pg_rel_oid);
upgrade_res = PQexec(g_conn, upgrade_query->data); upgrade_res = ExecuteSqlQuery(fout, upgrade_query->data, PGRES_TUPLES_OK);
check_sql_result(upgrade_res, g_conn, upgrade_query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(upgrade_res); ntups = PQntuples(upgrade_res);
...@@ -2522,7 +2522,8 @@ binary_upgrade_set_type_oids_by_rel_oid(PQExpBuffer upgrade_buffer, ...@@ -2522,7 +2522,8 @@ binary_upgrade_set_type_oids_by_rel_oid(PQExpBuffer upgrade_buffer,
pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel"))); pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
binary_upgrade_set_type_oids_by_type_oid(upgrade_buffer, pg_type_oid); binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
pg_type_oid);
if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel"))) if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel")))
{ {
...@@ -2545,7 +2546,8 @@ binary_upgrade_set_type_oids_by_rel_oid(PQExpBuffer upgrade_buffer, ...@@ -2545,7 +2546,8 @@ binary_upgrade_set_type_oids_by_rel_oid(PQExpBuffer upgrade_buffer,
} }
static void static void
binary_upgrade_set_pg_class_oids(PQExpBuffer upgrade_buffer, Oid pg_class_oid, binary_upgrade_set_pg_class_oids(Archive *fout,
PQExpBuffer upgrade_buffer, Oid pg_class_oid,
bool is_index) bool is_index)
{ {
PQExpBuffer upgrade_query = createPQExpBuffer(); PQExpBuffer upgrade_query = createPQExpBuffer();
...@@ -2561,8 +2563,7 @@ binary_upgrade_set_pg_class_oids(PQExpBuffer upgrade_buffer, Oid pg_class_oid, ...@@ -2561,8 +2563,7 @@ binary_upgrade_set_pg_class_oids(PQExpBuffer upgrade_buffer, Oid pg_class_oid,
"WHERE c.oid = '%u'::pg_catalog.oid;", "WHERE c.oid = '%u'::pg_catalog.oid;",
pg_class_oid); pg_class_oid);
upgrade_res = PQexec(g_conn, upgrade_query->data); upgrade_res = ExecuteSqlQuery(fout, upgrade_query->data, PGRES_TUPLES_OK);
check_sql_result(upgrade_res, g_conn, upgrade_query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(upgrade_res); ntups = PQntuples(upgrade_res);
...@@ -2729,8 +2730,7 @@ getNamespaces(Archive *fout, int *numNamespaces) ...@@ -2729,8 +2730,7 @@ getNamespaces(Archive *fout, int *numNamespaces)
"nspacl FROM pg_namespace", "nspacl FROM pg_namespace",
username_subquery); username_subquery);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -2850,8 +2850,7 @@ getExtensions(Archive *fout, int *numExtensions) ...@@ -2850,8 +2850,7 @@ getExtensions(Archive *fout, int *numExtensions)
"FROM pg_extension x " "FROM pg_extension x "
"JOIN pg_namespace n ON n.oid = x.extnamespace"); "JOIN pg_namespace n ON n.oid = x.extnamespace");
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -3003,8 +3002,7 @@ getTypes(Archive *fout, int *numTypes) ...@@ -3003,8 +3002,7 @@ getTypes(Archive *fout, int *numTypes)
username_subquery); username_subquery);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -3213,8 +3211,7 @@ getOperators(Archive *fout, int *numOprs) ...@@ -3213,8 +3211,7 @@ getOperators(Archive *fout, int *numOprs)
username_subquery); username_subquery);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numOprs = ntups; *numOprs = ntups;
...@@ -3301,8 +3298,7 @@ getCollations(Archive *fout, int *numCollations) ...@@ -3301,8 +3298,7 @@ getCollations(Archive *fout, int *numCollations)
"FROM pg_collation", "FROM pg_collation",
username_subquery); username_subquery);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numCollations = ntups; *numCollations = ntups;
...@@ -3381,8 +3377,7 @@ getConversions(Archive *fout, int *numConversions) ...@@ -3381,8 +3377,7 @@ getConversions(Archive *fout, int *numConversions)
"FROM pg_conversion", "FROM pg_conversion",
username_subquery); username_subquery);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numConversions = ntups; *numConversions = ntups;
...@@ -3473,8 +3468,7 @@ getOpclasses(Archive *fout, int *numOpclasses) ...@@ -3473,8 +3468,7 @@ getOpclasses(Archive *fout, int *numOpclasses)
"FROM pg_opclass"); "FROM pg_opclass");
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numOpclasses = ntups; *numOpclasses = ntups;
...@@ -3562,8 +3556,7 @@ getOpfamilies(Archive *fout, int *numOpfamilies) ...@@ -3562,8 +3556,7 @@ getOpfamilies(Archive *fout, int *numOpfamilies)
"FROM pg_opfamily", "FROM pg_opfamily",
username_subquery); username_subquery);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numOpfamilies = ntups; *numOpfamilies = ntups;
...@@ -3704,8 +3697,7 @@ getAggregates(Archive *fout, int *numAggs) ...@@ -3704,8 +3697,7 @@ getAggregates(Archive *fout, int *numAggs)
g_last_builtin_oid); g_last_builtin_oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numAggs = ntups; *numAggs = ntups;
...@@ -3867,8 +3859,7 @@ getFuncs(Archive *fout, int *numFuncs) ...@@ -3867,8 +3859,7 @@ getFuncs(Archive *fout, int *numFuncs)
g_last_builtin_oid); g_last_builtin_oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -4288,8 +4279,7 @@ getTables(Archive *fout, int *numTables) ...@@ -4288,8 +4279,7 @@ getTables(Archive *fout, int *numTables)
RELKIND_RELATION, RELKIND_SEQUENCE); RELKIND_RELATION, RELKIND_SEQUENCE);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -4341,7 +4331,7 @@ getTables(Archive *fout, int *numTables) ...@@ -4341,7 +4331,7 @@ getTables(Archive *fout, int *numTables)
resetPQExpBuffer(query); resetPQExpBuffer(query);
appendPQExpBuffer(query, "SET statement_timeout = "); appendPQExpBuffer(query, "SET statement_timeout = ");
appendStringLiteralConn(query, lockWaitTimeout, g_conn); appendStringLiteralConn(query, lockWaitTimeout, g_conn);
do_sql_command(g_conn, query->data); ExecuteSqlStatement(fout, query->data);
} }
for (i = 0; i < ntups; i++) for (i = 0; i < ntups; i++)
...@@ -4415,7 +4405,7 @@ getTables(Archive *fout, int *numTables) ...@@ -4415,7 +4405,7 @@ getTables(Archive *fout, int *numTables)
fmtQualifiedId(fout, fmtQualifiedId(fout,
tblinfo[i].dobj.namespace->dobj.name, tblinfo[i].dobj.namespace->dobj.name,
tblinfo[i].dobj.name)); tblinfo[i].dobj.name));
do_sql_command(g_conn, query->data); ExecuteSqlStatement(fout, query->data);
} }
/* Emit notice if join for owner failed */ /* Emit notice if join for owner failed */
...@@ -4426,7 +4416,7 @@ getTables(Archive *fout, int *numTables) ...@@ -4426,7 +4416,7 @@ getTables(Archive *fout, int *numTables)
if (lockWaitTimeout && fout->remoteVersion >= 70300) if (lockWaitTimeout && fout->remoteVersion >= 70300)
{ {
do_sql_command(g_conn, "SET statement_timeout = 0"); ExecuteSqlStatement(fout, "SET statement_timeout = 0");
} }
PQclear(res); PQclear(res);
...@@ -4491,8 +4481,7 @@ getInherits(Archive *fout, int *numInherits) ...@@ -4491,8 +4481,7 @@ getInherits(Archive *fout, int *numInherits)
appendPQExpBuffer(query, "SELECT inhrelid, inhparent FROM pg_inherits"); appendPQExpBuffer(query, "SELECT inhrelid, inhparent FROM pg_inherits");
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -4741,8 +4730,7 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) ...@@ -4741,8 +4730,7 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
tbinfo->dobj.catId.oid); tbinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -4900,8 +4888,7 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables) ...@@ -4900,8 +4888,7 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
"WHERE conrelid = '%u'::pg_catalog.oid " "WHERE conrelid = '%u'::pg_catalog.oid "
"AND contype = 'f'", "AND contype = 'f'",
tbinfo->dobj.catId.oid); tbinfo->dobj.catId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -4995,8 +4982,7 @@ getDomainConstraints(Archive *fout, TypeInfo *tyinfo) ...@@ -4995,8 +4982,7 @@ getDomainConstraints(Archive *fout, TypeInfo *tyinfo)
"ORDER BY conname", "ORDER BY conname",
tyinfo->dobj.catId.oid); tyinfo->dobj.catId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -5102,8 +5088,7 @@ getRules(Archive *fout, int *numRules) ...@@ -5102,8 +5088,7 @@ getRules(Archive *fout, int *numRules)
"ORDER BY oid"); "ORDER BY oid");
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -5310,8 +5295,7 @@ getTriggers(Archive *fout, TableInfo tblinfo[], int numTables) ...@@ -5310,8 +5295,7 @@ getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
"WHERE tgrelid = '%u'::oid", "WHERE tgrelid = '%u'::oid",
tbinfo->dobj.catId.oid); tbinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -5494,8 +5478,7 @@ getProcLangs(Archive *fout, int *numProcLangs) ...@@ -5494,8 +5478,7 @@ getProcLangs(Archive *fout, int *numProcLangs)
"ORDER BY oid"); "ORDER BY oid");
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -5615,8 +5598,7 @@ getCasts(Archive *fout, int *numCasts) ...@@ -5615,8 +5598,7 @@ getCasts(Archive *fout, int *numCasts)
"ORDER BY 3,4"); "ORDER BY 3,4");
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -5882,8 +5864,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) ...@@ -5882,8 +5864,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
tbinfo->dobj.catId.oid); tbinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, q->data); res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, q->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -6006,8 +5987,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) ...@@ -6006,8 +5987,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
"WHERE adrelid = '%u'::oid", "WHERE adrelid = '%u'::oid",
tbinfo->dobj.catId.oid); tbinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, q->data); res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, q->data, PGRES_TUPLES_OK);
numDefaults = PQntuples(res); numDefaults = PQntuples(res);
attrdefs = (AttrDefInfo *) pg_malloc(numDefaults * sizeof(AttrDefInfo)); attrdefs = (AttrDefInfo *) pg_malloc(numDefaults * sizeof(AttrDefInfo));
...@@ -6166,8 +6146,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) ...@@ -6166,8 +6146,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
"ORDER BY rcname", "ORDER BY rcname",
tbinfo->dobj.catId.oid); tbinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, q->data); res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, q->data, PGRES_TUPLES_OK);
numConstrs = PQntuples(res); numConstrs = PQntuples(res);
if (numConstrs != tbinfo->ncheck) if (numConstrs != tbinfo->ncheck)
...@@ -6286,8 +6265,7 @@ getTSParsers(Archive *fout, int *numTSParsers) ...@@ -6286,8 +6265,7 @@ getTSParsers(Archive *fout, int *numTSParsers)
"prsend::oid, prsheadline::oid, prslextype::oid " "prsend::oid, prsheadline::oid, prslextype::oid "
"FROM pg_ts_parser"); "FROM pg_ts_parser");
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numTSParsers = ntups; *numTSParsers = ntups;
...@@ -6371,8 +6349,7 @@ getTSDictionaries(Archive *fout, int *numTSDicts) ...@@ -6371,8 +6349,7 @@ getTSDictionaries(Archive *fout, int *numTSDicts)
"FROM pg_ts_dict", "FROM pg_ts_dict",
username_subquery); username_subquery);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numTSDicts = ntups; *numTSDicts = ntups;
...@@ -6452,8 +6429,7 @@ getTSTemplates(Archive *fout, int *numTSTemplates) ...@@ -6452,8 +6429,7 @@ getTSTemplates(Archive *fout, int *numTSTemplates)
"tmplnamespace, tmplinit::oid, tmpllexize::oid " "tmplnamespace, tmplinit::oid, tmpllexize::oid "
"FROM pg_ts_template"); "FROM pg_ts_template");
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numTSTemplates = ntups; *numTSTemplates = ntups;
...@@ -6529,8 +6505,7 @@ getTSConfigurations(Archive *fout, int *numTSConfigs) ...@@ -6529,8 +6505,7 @@ getTSConfigurations(Archive *fout, int *numTSConfigs)
"FROM pg_ts_config", "FROM pg_ts_config",
username_subquery); username_subquery);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numTSConfigs = ntups; *numTSConfigs = ntups;
...@@ -6634,8 +6609,7 @@ getForeignDataWrappers(Archive *fout, int *numForeignDataWrappers) ...@@ -6634,8 +6609,7 @@ getForeignDataWrappers(Archive *fout, int *numForeignDataWrappers)
username_subquery); username_subquery);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numForeignDataWrappers = ntups; *numForeignDataWrappers = ntups;
...@@ -6723,8 +6697,7 @@ getForeignServers(Archive *fout, int *numForeignServers) ...@@ -6723,8 +6697,7 @@ getForeignServers(Archive *fout, int *numForeignServers)
"FROM pg_foreign_server", "FROM pg_foreign_server",
username_subquery); username_subquery);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numForeignServers = ntups; *numForeignServers = ntups;
...@@ -6808,8 +6781,7 @@ getDefaultACLs(Archive *fout, int *numDefaultACLs) ...@@ -6808,8 +6781,7 @@ getDefaultACLs(Archive *fout, int *numDefaultACLs)
"FROM pg_default_acl", "FROM pg_default_acl",
username_subquery); username_subquery);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
*numDefaultACLs = ntups; *numDefaultACLs = ntups;
...@@ -7158,8 +7130,7 @@ collectComments(Archive *fout, CommentItem **items) ...@@ -7158,8 +7130,7 @@ collectComments(Archive *fout, CommentItem **items)
"ORDER BY objoid"); "ORDER BY objoid");
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Construct lookup table containing OIDs in numeric form */ /* Construct lookup table containing OIDs in numeric form */
...@@ -7558,8 +7529,7 @@ dumpEnumType(Archive *fout, TypeInfo *tyinfo) ...@@ -7558,8 +7529,7 @@ dumpEnumType(Archive *fout, TypeInfo *tyinfo)
"ORDER BY oid", "ORDER BY oid",
tyinfo->dobj.catId.oid); tyinfo->dobj.catId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
num = PQntuples(res); num = PQntuples(res);
...@@ -7574,7 +7544,8 @@ dumpEnumType(Archive *fout, TypeInfo *tyinfo) ...@@ -7574,7 +7544,8 @@ dumpEnumType(Archive *fout, TypeInfo *tyinfo)
fmtId(tyinfo->dobj.name)); fmtId(tyinfo->dobj.name));
if (binary_upgrade) if (binary_upgrade)
binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid); binary_upgrade_set_type_oids_by_type_oid(fout, q,
tyinfo->dobj.catId.oid);
appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (", appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (",
fmtId(tyinfo->dobj.name)); fmtId(tyinfo->dobj.name));
...@@ -7682,8 +7653,7 @@ dumpRangeType(Archive *fout, TypeInfo *tyinfo) ...@@ -7682,8 +7653,7 @@ dumpRangeType(Archive *fout, TypeInfo *tyinfo)
"rngtypid = '%u'", "rngtypid = '%u'",
tyinfo->dobj.catId.oid); tyinfo->dobj.catId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) != 1) if (PQntuples(res) != 1)
{ {
write_msg(NULL, "query returned %d pg_range entries for range type \"%s\"\n", write_msg(NULL, "query returned %d pg_range entries for range type \"%s\"\n",
...@@ -7702,7 +7672,8 @@ dumpRangeType(Archive *fout, TypeInfo *tyinfo) ...@@ -7702,7 +7672,8 @@ dumpRangeType(Archive *fout, TypeInfo *tyinfo)
fmtId(tyinfo->dobj.name)); fmtId(tyinfo->dobj.name));
if (binary_upgrade) if (binary_upgrade)
binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid); binary_upgrade_set_type_oids_by_type_oid(fout,
q, tyinfo->dobj.catId.oid);
appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (", appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (",
fmtId(tyinfo->dobj.name)); fmtId(tyinfo->dobj.name));
...@@ -7990,8 +7961,7 @@ dumpBaseType(Archive *fout, TypeInfo *tyinfo) ...@@ -7990,8 +7961,7 @@ dumpBaseType(Archive *fout, TypeInfo *tyinfo)
tyinfo->dobj.catId.oid); tyinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -8047,7 +8017,8 @@ dumpBaseType(Archive *fout, TypeInfo *tyinfo) ...@@ -8047,7 +8017,8 @@ dumpBaseType(Archive *fout, TypeInfo *tyinfo)
/* We might already have a shell type, but setting pg_type_oid is harmless */ /* We might already have a shell type, but setting pg_type_oid is harmless */
if (binary_upgrade) if (binary_upgrade)
binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid); binary_upgrade_set_type_oids_by_type_oid(fout, q,
tyinfo->dobj.catId.oid);
appendPQExpBuffer(q, appendPQExpBuffer(q,
"CREATE TYPE %s (\n" "CREATE TYPE %s (\n"
...@@ -8221,8 +8192,7 @@ dumpDomain(Archive *fout, TypeInfo *tyinfo) ...@@ -8221,8 +8192,7 @@ dumpDomain(Archive *fout, TypeInfo *tyinfo)
tyinfo->dobj.catId.oid); tyinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -8249,7 +8219,8 @@ dumpDomain(Archive *fout, TypeInfo *tyinfo) ...@@ -8249,7 +8219,8 @@ dumpDomain(Archive *fout, TypeInfo *tyinfo)
typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation"))); typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation")));
if (binary_upgrade) if (binary_upgrade)
binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid); binary_upgrade_set_type_oids_by_type_oid(fout, q,
tyinfo->dobj.catId.oid);
appendPQExpBuffer(q, appendPQExpBuffer(q,
"CREATE DOMAIN %s AS %s", "CREATE DOMAIN %s AS %s",
...@@ -8407,8 +8378,7 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo) ...@@ -8407,8 +8378,7 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
tyinfo->dobj.catId.oid); tyinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -8424,8 +8394,9 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo) ...@@ -8424,8 +8394,9 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
{ {
Oid typrelid = atooid(PQgetvalue(res, 0, i_typrelid)); Oid typrelid = atooid(PQgetvalue(res, 0, i_typrelid));
binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid); binary_upgrade_set_type_oids_by_type_oid(fout, q,
binary_upgrade_set_pg_class_oids(q, typrelid, false); tyinfo->dobj.catId.oid);
binary_upgrade_set_pg_class_oids(fout, q, typrelid, false);
} }
appendPQExpBuffer(q, "CREATE TYPE %s AS (", appendPQExpBuffer(q, "CREATE TYPE %s AS (",
...@@ -8581,8 +8552,7 @@ dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo) ...@@ -8581,8 +8552,7 @@ dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo)
tyinfo->typrelid); tyinfo->typrelid);
/* Fetch column attnames */ /* Fetch column attnames */
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
if (ntups < 1) if (ntups < 1)
...@@ -8687,7 +8657,7 @@ dumpShellType(Archive *fout, ShellTypeInfo *stinfo) ...@@ -8687,7 +8657,7 @@ dumpShellType(Archive *fout, ShellTypeInfo *stinfo)
*/ */
if (binary_upgrade) if (binary_upgrade)
binary_upgrade_set_type_oids_by_type_oid(q, binary_upgrade_set_type_oids_by_type_oid(fout, q,
stinfo->baseType->dobj.catId.oid); stinfo->baseType->dobj.catId.oid);
appendPQExpBuffer(q, "CREATE TYPE %s;\n", appendPQExpBuffer(q, "CREATE TYPE %s;\n",
...@@ -9189,8 +9159,7 @@ dumpFunc(Archive *fout, FuncInfo *finfo) ...@@ -9189,8 +9159,7 @@ dumpFunc(Archive *fout, FuncInfo *finfo)
finfo->dobj.catId.oid); finfo->dobj.catId.oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -9743,8 +9712,7 @@ dumpOpr(Archive *fout, OprInfo *oprinfo) ...@@ -9743,8 +9712,7 @@ dumpOpr(Archive *fout, OprInfo *oprinfo)
oprinfo->dobj.catId.oid); oprinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -9989,7 +9957,7 @@ convertOperatorReference(Archive *fout, const char *opr) ...@@ -9989,7 +9957,7 @@ convertOperatorReference(Archive *fout, const char *opr)
* are search path dependent! * are search path dependent!
*/ */
static const char * static const char *
convertTSFunction(Oid funcOid) convertTSFunction(Archive *fout, Oid funcOid)
{ {
char *result; char *result;
char query[128]; char query[128];
...@@ -9998,8 +9966,7 @@ convertTSFunction(Oid funcOid) ...@@ -9998,8 +9966,7 @@ convertTSFunction(Oid funcOid)
snprintf(query, sizeof(query), snprintf(query, sizeof(query),
"SELECT '%u'::pg_catalog.regproc", funcOid); "SELECT '%u'::pg_catalog.regproc", funcOid);
res = PQexec(g_conn, query); res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
if (ntups != 1) if (ntups != 1)
...@@ -10115,8 +10082,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo) ...@@ -10115,8 +10082,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
opcinfo->dobj.catId.oid); opcinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -10262,8 +10228,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo) ...@@ -10262,8 +10228,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
opcinfo->dobj.catId.oid); opcinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -10343,8 +10308,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo) ...@@ -10343,8 +10308,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
opcinfo->dobj.catId.oid); opcinfo->dobj.catId.oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -10525,8 +10489,7 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo) ...@@ -10525,8 +10489,7 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
opfinfo->dobj.catId.oid); opfinfo->dobj.catId.oid);
} }
res_ops = PQexec(g_conn, query->data); res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res_ops, g_conn, query->data, PGRES_TUPLES_OK);
resetPQExpBuffer(query); resetPQExpBuffer(query);
...@@ -10542,8 +10505,7 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo) ...@@ -10542,8 +10505,7 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
"ORDER BY amprocnum", "ORDER BY amprocnum",
opfinfo->dobj.catId.oid); opfinfo->dobj.catId.oid);
res_procs = PQexec(g_conn, query->data); res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res_procs, g_conn, query->data, PGRES_TUPLES_OK);
if (PQntuples(res_ops) == 0 && PQntuples(res_procs) == 0) if (PQntuples(res_ops) == 0 && PQntuples(res_procs) == 0)
{ {
...@@ -10561,8 +10523,7 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo) ...@@ -10561,8 +10523,7 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
"LIMIT 1", "LIMIT 1",
opfinfo->dobj.catId.oid); opfinfo->dobj.catId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) == 0) if (PQntuples(res) == 0)
{ {
...@@ -10589,8 +10550,7 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo) ...@@ -10589,8 +10550,7 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
"WHERE oid = '%u'::pg_catalog.oid", "WHERE oid = '%u'::pg_catalog.oid",
opfinfo->dobj.catId.oid); opfinfo->dobj.catId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -10775,8 +10735,7 @@ dumpCollation(Archive *fout, CollInfo *collinfo) ...@@ -10775,8 +10735,7 @@ dumpCollation(Archive *fout, CollInfo *collinfo)
"WHERE c.oid = '%u'::pg_catalog.oid", "WHERE c.oid = '%u'::pg_catalog.oid",
collinfo->dobj.catId.oid); collinfo->dobj.catId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -10881,8 +10840,7 @@ dumpConversion(Archive *fout, ConvInfo *convinfo) ...@@ -10881,8 +10840,7 @@ dumpConversion(Archive *fout, ConvInfo *convinfo)
"WHERE c.oid = '%u'::pg_catalog.oid", "WHERE c.oid = '%u'::pg_catalog.oid",
convinfo->dobj.catId.oid); convinfo->dobj.catId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -11082,8 +11040,7 @@ dumpAgg(Archive *fout, AggInfo *agginfo) ...@@ -11082,8 +11040,7 @@ dumpAgg(Archive *fout, AggInfo *agginfo)
agginfo->aggfn.dobj.catId.oid); agginfo->aggfn.dobj.catId.oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -11250,16 +11207,16 @@ dumpTSParser(Archive *fout, TSParserInfo *prsinfo) ...@@ -11250,16 +11207,16 @@ dumpTSParser(Archive *fout, TSParserInfo *prsinfo)
fmtId(prsinfo->dobj.name)); fmtId(prsinfo->dobj.name));
appendPQExpBuffer(q, " START = %s,\n", appendPQExpBuffer(q, " START = %s,\n",
convertTSFunction(prsinfo->prsstart)); convertTSFunction(fout, prsinfo->prsstart));
appendPQExpBuffer(q, " GETTOKEN = %s,\n", appendPQExpBuffer(q, " GETTOKEN = %s,\n",
convertTSFunction(prsinfo->prstoken)); convertTSFunction(fout, prsinfo->prstoken));
appendPQExpBuffer(q, " END = %s,\n", appendPQExpBuffer(q, " END = %s,\n",
convertTSFunction(prsinfo->prsend)); convertTSFunction(fout, prsinfo->prsend));
if (prsinfo->prsheadline != InvalidOid) if (prsinfo->prsheadline != InvalidOid)
appendPQExpBuffer(q, " HEADLINE = %s,\n", appendPQExpBuffer(q, " HEADLINE = %s,\n",
convertTSFunction(prsinfo->prsheadline)); convertTSFunction(fout, prsinfo->prsheadline));
appendPQExpBuffer(q, " LEXTYPES = %s );\n", appendPQExpBuffer(q, " LEXTYPES = %s );\n",
convertTSFunction(prsinfo->prslextype)); convertTSFunction(fout, prsinfo->prslextype));
/* /*
* DROP must be fully qualified in case same name appears in pg_catalog * DROP must be fully qualified in case same name appears in pg_catalog
...@@ -11326,8 +11283,7 @@ dumpTSDictionary(Archive *fout, TSDictInfo *dictinfo) ...@@ -11326,8 +11283,7 @@ dumpTSDictionary(Archive *fout, TSDictInfo *dictinfo)
"FROM pg_ts_template p, pg_namespace n " "FROM pg_ts_template p, pg_namespace n "
"WHERE p.oid = '%u' AND n.oid = tmplnamespace", "WHERE p.oid = '%u' AND n.oid = tmplnamespace",
dictinfo->dicttemplate); dictinfo->dicttemplate);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
if (ntups != 1) if (ntups != 1)
{ {
...@@ -11421,9 +11377,9 @@ dumpTSTemplate(Archive *fout, TSTemplateInfo *tmplinfo) ...@@ -11421,9 +11377,9 @@ dumpTSTemplate(Archive *fout, TSTemplateInfo *tmplinfo)
if (tmplinfo->tmplinit != InvalidOid) if (tmplinfo->tmplinit != InvalidOid)
appendPQExpBuffer(q, " INIT = %s,\n", appendPQExpBuffer(q, " INIT = %s,\n",
convertTSFunction(tmplinfo->tmplinit)); convertTSFunction(fout, tmplinfo->tmplinit));
appendPQExpBuffer(q, " LEXIZE = %s );\n", appendPQExpBuffer(q, " LEXIZE = %s );\n",
convertTSFunction(tmplinfo->tmpllexize)); convertTSFunction(fout, tmplinfo->tmpllexize));
/* /*
* DROP must be fully qualified in case same name appears in pg_catalog * DROP must be fully qualified in case same name appears in pg_catalog
...@@ -11493,8 +11449,7 @@ dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo) ...@@ -11493,8 +11449,7 @@ dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo)
"FROM pg_ts_parser p, pg_namespace n " "FROM pg_ts_parser p, pg_namespace n "
"WHERE p.oid = '%u' AND n.oid = prsnamespace", "WHERE p.oid = '%u' AND n.oid = prsnamespace",
cfginfo->cfgparser); cfginfo->cfgparser);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
if (ntups != 1) if (ntups != 1)
{ {
...@@ -11531,8 +11486,7 @@ dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo) ...@@ -11531,8 +11486,7 @@ dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo)
"ORDER BY m.mapcfg, m.maptokentype, m.mapseqno", "ORDER BY m.mapcfg, m.maptokentype, m.mapseqno",
cfginfo->cfgparser, cfginfo->dobj.catId.oid); cfginfo->cfgparser, cfginfo->dobj.catId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
i_tokenname = PQfnumber(res, "tokenname"); i_tokenname = PQfnumber(res, "tokenname");
...@@ -11714,8 +11668,7 @@ dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo) ...@@ -11714,8 +11668,7 @@ dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo)
"FROM pg_foreign_data_wrapper w " "FROM pg_foreign_data_wrapper w "
"WHERE w.oid = '%u'", "WHERE w.oid = '%u'",
srvinfo->srvfdw); srvinfo->srvfdw);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
if (ntups != 1) if (ntups != 1)
{ {
...@@ -11841,8 +11794,7 @@ dumpUserMappings(Archive *fout, ...@@ -11841,8 +11794,7 @@ dumpUserMappings(Archive *fout,
"ORDER BY usename", "ORDER BY usename",
catalogId.oid); catalogId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
i_usename = PQfnumber(res, "usename"); i_usename = PQfnumber(res, "usename");
...@@ -12283,8 +12235,7 @@ collectSecLabels(Archive *fout, SecLabelItem **items) ...@@ -12283,8 +12235,7 @@ collectSecLabels(Archive *fout, SecLabelItem **items)
"FROM pg_catalog.pg_seclabel " "FROM pg_catalog.pg_seclabel "
"ORDER BY classoid, objoid, objsubid"); "ORDER BY classoid, objoid, objsubid");
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Construct lookup table containing OIDs in numeric form */ /* Construct lookup table containing OIDs in numeric form */
i_label = PQfnumber(res, "label"); i_label = PQfnumber(res, "label");
...@@ -12354,8 +12305,7 @@ dumpTable(Archive *fout, TableInfo *tbinfo) ...@@ -12354,8 +12305,7 @@ dumpTable(Archive *fout, TableInfo *tbinfo)
"WHERE attrelid = '%u' AND NOT attisdropped AND attacl IS NOT NULL " "WHERE attrelid = '%u' AND NOT attisdropped AND attacl IS NOT NULL "
"ORDER BY attnum", "ORDER BY attnum",
tbinfo->dobj.catId.oid); tbinfo->dobj.catId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
for (i = 0; i < PQntuples(res); i++) for (i = 0; i < PQntuples(res); i++)
{ {
...@@ -12409,7 +12359,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) ...@@ -12409,7 +12359,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name); selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
if (binary_upgrade) if (binary_upgrade)
binary_upgrade_set_type_oids_by_rel_oid(q, binary_upgrade_set_type_oids_by_rel_oid(fout, q,
tbinfo->dobj.catId.oid); tbinfo->dobj.catId.oid);
/* Is it a table or a view? */ /* Is it a table or a view? */
...@@ -12435,8 +12385,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) ...@@ -12435,8 +12385,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
appendPQExpBuffer(query, ";"); appendPQExpBuffer(query, ";");
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) != 1) if (PQntuples(res) != 1)
{ {
...@@ -12468,7 +12417,8 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) ...@@ -12468,7 +12417,8 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
fmtId(tbinfo->dobj.name)); fmtId(tbinfo->dobj.name));
if (binary_upgrade) if (binary_upgrade)
binary_upgrade_set_pg_class_oids(q, tbinfo->dobj.catId.oid, false); binary_upgrade_set_pg_class_oids(fout, q,
tbinfo->dobj.catId.oid, false);
appendPQExpBuffer(q, "CREATE VIEW %s", fmtId(tbinfo->dobj.name)); appendPQExpBuffer(q, "CREATE VIEW %s", fmtId(tbinfo->dobj.name));
if (tbinfo->reloptions && strlen(tbinfo->reloptions) > 0) if (tbinfo->reloptions && strlen(tbinfo->reloptions) > 0)
...@@ -12503,8 +12453,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) ...@@ -12503,8 +12453,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
"ON (fs.oid = ft.ftserver) " "ON (fs.oid = ft.ftserver) "
"WHERE ft.ftrelid = '%u'", "WHERE ft.ftrelid = '%u'",
tbinfo->dobj.catId.oid); tbinfo->dobj.catId.oid);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) != 1) if (PQntuples(res) != 1)
{ {
write_msg(NULL, ngettext("query returned %d foreign server entry for foreign table \"%s\"\n", write_msg(NULL, ngettext("query returned %d foreign server entry for foreign table \"%s\"\n",
...@@ -12541,7 +12490,8 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) ...@@ -12541,7 +12490,8 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
fmtId(tbinfo->dobj.name)); fmtId(tbinfo->dobj.name));
if (binary_upgrade) if (binary_upgrade)
binary_upgrade_set_pg_class_oids(q, tbinfo->dobj.catId.oid, false); binary_upgrade_set_pg_class_oids(fout, q,
tbinfo->dobj.catId.oid, false);
appendPQExpBuffer(q, "CREATE %s%s %s", appendPQExpBuffer(q, "CREATE %s%s %s",
tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ? tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
...@@ -13087,7 +13037,8 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo) ...@@ -13087,7 +13037,8 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
if (indxinfo->indexconstraint == 0) if (indxinfo->indexconstraint == 0)
{ {
if (binary_upgrade) if (binary_upgrade)
binary_upgrade_set_pg_class_oids(q, indxinfo->dobj.catId.oid, true); binary_upgrade_set_pg_class_oids(fout, q,
indxinfo->dobj.catId.oid, true);
/* Plain secondary index */ /* Plain secondary index */
appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef); appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef);
...@@ -13168,7 +13119,8 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo) ...@@ -13168,7 +13119,8 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
} }
if (binary_upgrade) if (binary_upgrade)
binary_upgrade_set_pg_class_oids(q, indxinfo->dobj.catId.oid, true); binary_upgrade_set_pg_class_oids(fout, q,
indxinfo->dobj.catId.oid, true);
appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n", appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
fmtId(tbinfo->dobj.name)); fmtId(tbinfo->dobj.name));
...@@ -13407,8 +13359,7 @@ findLastBuiltinOid_V71(Archive *fout, const char *dbname) ...@@ -13407,8 +13359,7 @@ findLastBuiltinOid_V71(Archive *fout, const char *dbname)
appendPQExpBuffer(query, "SELECT datlastsysoid from pg_database where datname = "); appendPQExpBuffer(query, "SELECT datlastsysoid from pg_database where datname = ");
appendStringLiteralAH(query, dbname, fout); appendStringLiteralAH(query, dbname, fout);
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
if (ntups < 1) if (ntups < 1)
...@@ -13436,17 +13387,15 @@ findLastBuiltinOid_V71(Archive *fout, const char *dbname) ...@@ -13436,17 +13387,15 @@ findLastBuiltinOid_V71(Archive *fout, const char *dbname)
* initdb won't be changing anymore, it'll do. * initdb won't be changing anymore, it'll do.
*/ */
static Oid static Oid
findLastBuiltinOid_V70(void) findLastBuiltinOid_V70(Archive *fout)
{ {
PGresult *res; PGresult *res;
int ntups; int ntups;
int last_oid; int last_oid;
res = PQexec(g_conn, res = ExecuteSqlQuery(fout,
"SELECT oid FROM pg_class WHERE relname = 'pg_indexes'"); "SELECT oid FROM pg_class WHERE relname = 'pg_indexes'",
check_sql_result(res, g_conn, PGRES_TUPLES_OK);
"SELECT oid FROM pg_class WHERE relname = 'pg_indexes'",
PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
if (ntups < 1) if (ntups < 1)
{ {
...@@ -13522,8 +13471,7 @@ dumpSequence(Archive *fout, TableInfo *tbinfo) ...@@ -13522,8 +13471,7 @@ dumpSequence(Archive *fout, TableInfo *tbinfo)
fmtId(tbinfo->dobj.name)); fmtId(tbinfo->dobj.name));
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) != 1) if (PQntuples(res) != 1)
{ {
...@@ -13580,8 +13528,10 @@ dumpSequence(Archive *fout, TableInfo *tbinfo) ...@@ -13580,8 +13528,10 @@ dumpSequence(Archive *fout, TableInfo *tbinfo)
if (binary_upgrade) if (binary_upgrade)
{ {
binary_upgrade_set_pg_class_oids(query, tbinfo->dobj.catId.oid, false); binary_upgrade_set_pg_class_oids(fout, query,
binary_upgrade_set_type_oids_by_rel_oid(query, tbinfo->dobj.catId.oid); tbinfo->dobj.catId.oid, false);
binary_upgrade_set_type_oids_by_rel_oid(fout, query,
tbinfo->dobj.catId.oid);
} }
appendPQExpBuffer(query, appendPQExpBuffer(query,
...@@ -13961,8 +13911,7 @@ dumpRule(Archive *fout, RuleInfo *rinfo) ...@@ -13961,8 +13911,7 @@ dumpRule(Archive *fout, RuleInfo *rinfo)
rinfo->dobj.name); rinfo->dobj.name);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) != 1) if (PQntuples(res) != 1)
{ {
...@@ -14074,8 +14023,7 @@ getExtensionMembership(Archive *fout, ExtensionInfo extinfo[], ...@@ -14074,8 +14023,7 @@ getExtensionMembership(Archive *fout, ExtensionInfo extinfo[],
"AND deptype = 'e' " "AND deptype = 'e' "
"ORDER BY 3,4"); "ORDER BY 3,4");
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -14241,8 +14189,7 @@ getDependencies(Archive *fout) ...@@ -14241,8 +14189,7 @@ getDependencies(Archive *fout)
"WHERE deptype != 'p' AND deptype != 'e' " "WHERE deptype != 'p' AND deptype != 'e' "
"ORDER BY 1,2"); "ORDER BY 1,2");
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -14356,7 +14303,7 @@ selectSourceSchema(Archive *fout, const char *schemaName) ...@@ -14356,7 +14303,7 @@ selectSourceSchema(Archive *fout, const char *schemaName)
if (strcmp(schemaName, "pg_catalog") != 0) if (strcmp(schemaName, "pg_catalog") != 0)
appendPQExpBuffer(query, ", pg_catalog"); appendPQExpBuffer(query, ", pg_catalog");
do_sql_command(g_conn, query->data); ExecuteSqlStatement(fout, query->data);
destroyPQExpBuffer(query); destroyPQExpBuffer(query);
if (curSchemaName) if (curSchemaName)
...@@ -14410,8 +14357,7 @@ getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts) ...@@ -14410,8 +14357,7 @@ getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
oid); oid);
} }
res = PQexec(g_conn, query->data); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */ /* Expecting a single result only */
ntups = PQntuples(res); ntups = PQntuples(res);
...@@ -14578,40 +14524,3 @@ fmtCopyColumnList(const TableInfo *ti) ...@@ -14578,40 +14524,3 @@ fmtCopyColumnList(const TableInfo *ti)
appendPQExpBuffer(q, ")"); appendPQExpBuffer(q, ")");
return q->data; return q->data;
} }
/*
* Convenience subroutine to execute a SQL command and check for
* COMMAND_OK status.
*/
static void
do_sql_command(PGconn *conn, const char *query)
{
PGresult *res;
res = PQexec(conn, query);
check_sql_result(res, conn, query, PGRES_COMMAND_OK);
PQclear(res);
}
/*
* Convenience subroutine to verify a SQL command succeeded,
* and exit with a useful error message if not.
*/
static void
check_sql_result(PGresult *res, PGconn *conn, const char *query,
ExecStatusType expected)
{
const char *err;
if (res && PQresultStatus(res) == expected)
return; /* A-OK */
write_msg(NULL, "SQL command failed\n");
if (res)
err = PQresultErrorMessage(res);
else
err = PQerrorMessage(conn);
write_msg(NULL, "Error message from server: %s", err);
write_msg(NULL, "The command was: %s\n", query);
exit_nicely();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment