#include "libpq/libpq-fs.h"
#include "pg_backup_archiver.h"
+#include "pg_backup_db.h"
#include "dumpmem.h"
#include "dumputils.h"
static void expand_schema_name_patterns(Archive *fout,
SimpleStringList *patterns,
SimpleOidList *oids);
-static void expand_table_name_patterns(SimpleStringList *patterns,
+static void expand_table_name_patterns(Archive *fout,
+ SimpleStringList *patterns,
SimpleOidList *oids);
static NamespaceInfo *findNamespace(Archive *fout, Oid nsoid, Oid objoid);
static void dumpTableData(Archive *fout, TableDataInfo *tdinfo);
static const char *convertRegProcReference(Archive *fout,
const char *proc);
static const char *convertOperatorReference(Archive *fout, const char *opr);
-static const char *convertTSFunction(Oid funcOid);
+static const char *convertTSFunction(Archive *fout, Oid funcOid);
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 char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
static char *myFormatType(const char *typname, int32 typmod);
static void dumpDatabase(Archive *AH);
static void dumpEncoding(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);
-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);
-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);
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
DumpableObject *dobj,
const char *objlabel);
static const char *getAttrName(int attrnum, TableInfo *tblInfo);
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
main(int argc, char **argv)
/*
* Start transaction-snapshot mode transaction to dump consistent data.
*/
- do_sql_command(g_conn, "BEGIN");
+ ExecuteSqlStatement(fout, "BEGIN");
if (fout->remoteVersion >= 90100)
{
if (serializable_deferrable)
- do_sql_command(g_conn,
- "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE, "
- "READ ONLY, DEFERRABLE");
+ ExecuteSqlStatement(fout,
+ "SET TRANSACTION ISOLATION LEVEL "
+ "SERIALIZABLE, READ ONLY, DEFERRABLE");
else
- do_sql_command(g_conn,
- "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ");
+ ExecuteSqlStatement(fout,
+ "SET TRANSACTION ISOLATION LEVEL "
+ "REPEATABLE READ");
}
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 */
if (fout->remoteVersion >= 80100)
if (fout->remoteVersion >= 70100)
g_last_builtin_oid = findLastBuiltinOid_V71(fout, PQdb(g_conn));
else
- g_last_builtin_oid = findLastBuiltinOid_V70();
+ g_last_builtin_oid = findLastBuiltinOid_V70(fout);
if (g_verbose)
write_msg(NULL, "last built-in OID is %u\n", g_last_builtin_oid);
}
/* Expand table selection patterns into OID lists */
if (table_include_patterns.head != NULL)
{
- expand_table_name_patterns(&table_include_patterns,
+ expand_table_name_patterns(fout, &table_include_patterns,
&table_include_oids);
if (table_include_oids.head == NULL)
{
exit_nicely();
}
}
- expand_table_name_patterns(&table_exclude_patterns,
+ expand_table_name_patterns(fout, &table_exclude_patterns,
&table_exclude_oids);
- expand_table_name_patterns(&tabledata_exclude_patterns,
+ expand_table_name_patterns(fout, &tabledata_exclude_patterns,
&tabledata_exclude_oids);
/* non-matching exclusion patterns aren't an error */
PQExpBuffer query = createPQExpBuffer();
appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
- do_sql_command(g_conn, query->data);
+ ExecuteSqlStatement(AH, query->data);
destroyPQExpBuffer(query);
}
/* 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 */
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
* exactly (given correctly implemented float I/O code, anyway)
*/
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)
- 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
* unpredictable changes in row ordering across a dump and reload.
*/
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.
*/
if (AH->remoteVersion >= 70300)
- do_sql_command(g_conn, "SET statement_timeout = 0");
+ ExecuteSqlStatement(AH, "SET statement_timeout = 0");
/*
* Quote all identifiers, if requested.
*/
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
NULL);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
for (i = 0; i < PQntuples(res); i++)
{
* and append them to the given OID list.
*/
static void
-expand_table_name_patterns(SimpleStringList *patterns, SimpleOidList *oids)
+expand_table_name_patterns(Archive *fout,
+ SimpleStringList *patterns, SimpleOidList *oids)
{
PQExpBuffer query;
PGresult *res;
"pg_catalog.pg_table_is_visible(c.oid)");
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
for (i = 0; i < PQntuples(res); i++)
{
classname),
column_list);
}
- res = PQexec(g_conn, q->data);
- check_sql_result(res, g_conn, q->data, PGRES_COPY_OUT);
+ res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT);
PQclear(res);
for (;;)
/* Check command status and return to normal libpq state */
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);
destroyPQExpBuffer(q);
if (tdinfo->filtercond)
appendPQExpBuffer(q, " %s", tdinfo->filtercond);
- res = PQexec(g_conn, q->data);
- check_sql_result(res, g_conn, q->data, PGRES_COMMAND_OK);
+ ExecuteSqlStatement(fout, q->data);
- do
+ while (1)
{
- PQclear(res);
-
- 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);
+ res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor",
+ PGRES_TUPLES_OK);
nfields = PQnfields(res);
for (tuple = 0; tuple < PQntuples(res); tuple++)
{
}
archprintf(fout, ");\n");
}
- } while (PQntuples(res) > 0);
- PQclear(res);
+ if (PQntuples(res) <= 0)
+ {
+ PQclear(res);
+ break;
+ }
+ PQclear(res);
+ }
archprintf(fout, "\n\n");
- do_sql_command(g_conn, "CLOSE _pg_dump_cursor");
+ ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
destroyPQExpBuffer(q);
return 1;
appendStringLiteralAH(dbQry, datname, fout);
}
- res = PQexec(g_conn, dbQry->data);
- check_sql_result(res, g_conn, dbQry->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, dbQry->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
"WHERE oid = %u;\n",
LargeObjectRelationId);
- lo_res = PQexec(g_conn, loFrozenQry->data);
- check_sql_result(lo_res, g_conn, loFrozenQry->data, PGRES_TUPLES_OK);
+ lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK);
if (PQntuples(lo_res) != 1)
{
"WHERE oid = %u;\n",
LargeObjectMetadataRelationId);
- lo_res = PQexec(g_conn, loFrozenQry->data);
- check_sql_result(lo_res, g_conn, loFrozenQry->data, PGRES_TUPLES_OK);
+ lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK);
if (PQntuples(lo_res) != 1)
{
PQExpBuffer seclabelQry = createPQExpBuffer();
buildShSecLabelQuery(g_conn, "pg_database", dbCatId.oid, seclabelQry);
- res = PQexec(g_conn, seclabelQry->data);
- check_sql_result(res, g_conn, seclabelQry->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
resetPQExpBuffer(seclabelQry);
emitShSecLabels(g_conn, res, seclabelQry, "DATABASE", datname);
if (strlen(seclabelQry->data))
"SELECT oid, NULL::oid, NULL::oid"
" FROM pg_class WHERE relkind = 'l'");
- res = PQexec(g_conn, blobQry->data);
- check_sql_result(res, g_conn, blobQry->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, blobQry->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
if (ntups > 0)
else
blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_class WHERE relkind = 'l'";
- res = PQexec(g_conn, blobQry);
- check_sql_result(res, g_conn, blobQry, PGRES_COMMAND_OK);
+ ExecuteSqlStatement(fout, blobQry);
/* Command to fetch from cursor */
blobFetchQry = "FETCH 1000 IN bloboid";
do
{
- PQclear(res);
-
/* Do a fetch */
- res = PQexec(g_conn, blobFetchQry);
- check_sql_result(res, g_conn, blobFetchQry, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
/* Process the tuples, if any */
ntups = PQntuples(res);
EndBlob(fout, blobOid);
}
+
+ PQclear(res);
} while (ntups > 0);
PQclear(res);
}
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)
{
PQExpBuffer upgrade_query = createPQExpBuffer();
"WHERE pg_type.oid = '%u'::pg_catalog.oid;",
pg_type_oid);
- upgrade_res = PQexec(g_conn, upgrade_query->data);
- check_sql_result(upgrade_res, g_conn, upgrade_query->data, PGRES_TUPLES_OK);
+ upgrade_res = ExecuteSqlQuery(fout, upgrade_query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(upgrade_res);
}
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)
{
PQExpBuffer upgrade_query = createPQExpBuffer();
"WHERE c.oid = '%u'::pg_catalog.oid;",
pg_rel_oid);
- upgrade_res = PQexec(g_conn, upgrade_query->data);
- check_sql_result(upgrade_res, g_conn, upgrade_query->data, PGRES_TUPLES_OK);
+ upgrade_res = ExecuteSqlQuery(fout, upgrade_query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(upgrade_res);
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")))
{
}
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)
{
PQExpBuffer upgrade_query = createPQExpBuffer();
"WHERE c.oid = '%u'::pg_catalog.oid;",
pg_class_oid);
- upgrade_res = PQexec(g_conn, upgrade_query->data);
- check_sql_result(upgrade_res, g_conn, upgrade_query->data, PGRES_TUPLES_OK);
+ upgrade_res = ExecuteSqlQuery(fout, upgrade_query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(upgrade_res);
"nspacl FROM pg_namespace",
username_subquery);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
"FROM pg_extension x "
"JOIN pg_namespace n ON n.oid = x.extnamespace");
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
username_subquery);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
username_subquery);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numOprs = ntups;
"FROM pg_collation",
username_subquery);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numCollations = ntups;
"FROM pg_conversion",
username_subquery);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numConversions = ntups;
"FROM pg_opclass");
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numOpclasses = ntups;
"FROM pg_opfamily",
username_subquery);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numOpfamilies = ntups;
g_last_builtin_oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numAggs = ntups;
g_last_builtin_oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
RELKIND_RELATION, RELKIND_SEQUENCE);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
resetPQExpBuffer(query);
appendPQExpBuffer(query, "SET statement_timeout = ");
appendStringLiteralConn(query, lockWaitTimeout, g_conn);
- do_sql_command(g_conn, query->data);
+ ExecuteSqlStatement(fout, query->data);
}
for (i = 0; i < ntups; i++)
fmtQualifiedId(fout,
tblinfo[i].dobj.namespace->dobj.name,
tblinfo[i].dobj.name));
- do_sql_command(g_conn, query->data);
+ ExecuteSqlStatement(fout, query->data);
}
/* Emit notice if join for owner failed */
if (lockWaitTimeout && fout->remoteVersion >= 70300)
{
- do_sql_command(g_conn, "SET statement_timeout = 0");
+ ExecuteSqlStatement(fout, "SET statement_timeout = 0");
}
PQclear(res);
appendPQExpBuffer(query, "SELECT inhrelid, inhparent FROM pg_inherits");
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
tbinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
"WHERE conrelid = '%u'::pg_catalog.oid "
"AND contype = 'f'",
tbinfo->dobj.catId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
"ORDER BY conname",
tyinfo->dobj.catId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
"ORDER BY oid");
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
"WHERE tgrelid = '%u'::oid",
tbinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
"ORDER BY oid");
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
"ORDER BY 3,4");
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
tbinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, q->data);
- check_sql_result(res, g_conn, q->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
"WHERE adrelid = '%u'::oid",
tbinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, q->data);
- check_sql_result(res, g_conn, q->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
numDefaults = PQntuples(res);
attrdefs = (AttrDefInfo *) pg_malloc(numDefaults * sizeof(AttrDefInfo));
"ORDER BY rcname",
tbinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, q->data);
- check_sql_result(res, g_conn, q->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
numConstrs = PQntuples(res);
if (numConstrs != tbinfo->ncheck)
"prsend::oid, prsheadline::oid, prslextype::oid "
"FROM pg_ts_parser");
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numTSParsers = ntups;
"FROM pg_ts_dict",
username_subquery);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numTSDicts = ntups;
"tmplnamespace, tmplinit::oid, tmpllexize::oid "
"FROM pg_ts_template");
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numTSTemplates = ntups;
"FROM pg_ts_config",
username_subquery);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numTSConfigs = ntups;
username_subquery);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numForeignDataWrappers = ntups;
"FROM pg_foreign_server",
username_subquery);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numForeignServers = ntups;
"FROM pg_default_acl",
username_subquery);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
*numDefaultACLs = ntups;
"ORDER BY objoid");
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Construct lookup table containing OIDs in numeric form */
"ORDER BY oid",
tyinfo->dobj.catId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
num = PQntuples(res);
fmtId(tyinfo->dobj.name));
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 (",
fmtId(tyinfo->dobj.name));
"rngtypid = '%u'",
tyinfo->dobj.catId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) != 1)
{
write_msg(NULL, "query returned %d pg_range entries for range type \"%s\"\n",
fmtId(tyinfo->dobj.name));
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 (",
fmtId(tyinfo->dobj.name));
tyinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(res);
/* We might already have a shell type, but setting pg_type_oid is harmless */
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 (\n"
tyinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(res);
typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation")));
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 DOMAIN %s AS %s",
tyinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
{
Oid typrelid = atooid(PQgetvalue(res, 0, i_typrelid));
- binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid);
- binary_upgrade_set_pg_class_oids(q, typrelid, false);
+ binary_upgrade_set_type_oids_by_type_oid(fout, q,
+ tyinfo->dobj.catId.oid);
+ binary_upgrade_set_pg_class_oids(fout, q, typrelid, false);
}
appendPQExpBuffer(q, "CREATE TYPE %s AS (",
tyinfo->typrelid);
/* Fetch column attnames */
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
if (ntups < 1)
*/
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);
appendPQExpBuffer(q, "CREATE TYPE %s;\n",
finfo->dobj.catId.oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(res);
oprinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(res);
* are search path dependent!
*/
static const char *
-convertTSFunction(Oid funcOid)
+convertTSFunction(Archive *fout, Oid funcOid)
{
char *result;
char query[128];
snprintf(query, sizeof(query),
"SELECT '%u'::pg_catalog.regproc", funcOid);
- res = PQexec(g_conn, query);
- check_sql_result(res, g_conn, query, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
ntups = PQntuples(res);
if (ntups != 1)
opcinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(res);
opcinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
opcinfo->dobj.catId.oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
opfinfo->dobj.catId.oid);
}
- res_ops = PQexec(g_conn, query->data);
- check_sql_result(res_ops, g_conn, query->data, PGRES_TUPLES_OK);
+ res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
resetPQExpBuffer(query);
"ORDER BY amprocnum",
opfinfo->dobj.catId.oid);
- res_procs = PQexec(g_conn, query->data);
- check_sql_result(res_procs, g_conn, query->data, PGRES_TUPLES_OK);
+ res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
if (PQntuples(res_ops) == 0 && PQntuples(res_procs) == 0)
{
"LIMIT 1",
opfinfo->dobj.catId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) == 0)
{
"WHERE oid = '%u'::pg_catalog.oid",
opfinfo->dobj.catId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(res);
"WHERE c.oid = '%u'::pg_catalog.oid",
collinfo->dobj.catId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(res);
"WHERE c.oid = '%u'::pg_catalog.oid",
convinfo->dobj.catId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(res);
agginfo->aggfn.dobj.catId.oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(res);
fmtId(prsinfo->dobj.name));
appendPQExpBuffer(q, " START = %s,\n",
- convertTSFunction(prsinfo->prsstart));
+ convertTSFunction(fout, prsinfo->prsstart));
appendPQExpBuffer(q, " GETTOKEN = %s,\n",
- convertTSFunction(prsinfo->prstoken));
+ convertTSFunction(fout, prsinfo->prstoken));
appendPQExpBuffer(q, " END = %s,\n",
- convertTSFunction(prsinfo->prsend));
+ convertTSFunction(fout, prsinfo->prsend));
if (prsinfo->prsheadline != InvalidOid)
appendPQExpBuffer(q, " HEADLINE = %s,\n",
- convertTSFunction(prsinfo->prsheadline));
+ convertTSFunction(fout, prsinfo->prsheadline));
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
"FROM pg_ts_template p, pg_namespace n "
"WHERE p.oid = '%u' AND n.oid = tmplnamespace",
dictinfo->dicttemplate);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
if (ntups != 1)
{
if (tmplinfo->tmplinit != InvalidOid)
appendPQExpBuffer(q, " INIT = %s,\n",
- convertTSFunction(tmplinfo->tmplinit));
+ convertTSFunction(fout, tmplinfo->tmplinit));
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
"FROM pg_ts_parser p, pg_namespace n "
"WHERE p.oid = '%u' AND n.oid = prsnamespace",
cfginfo->cfgparser);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
if (ntups != 1)
{
"ORDER BY m.mapcfg, m.maptokentype, m.mapseqno",
cfginfo->cfgparser, cfginfo->dobj.catId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
i_tokenname = PQfnumber(res, "tokenname");
"FROM pg_foreign_data_wrapper w "
"WHERE w.oid = '%u'",
srvinfo->srvfdw);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
if (ntups != 1)
{
"ORDER BY usename",
catalogId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
i_usename = PQfnumber(res, "usename");
"FROM pg_catalog.pg_seclabel "
"ORDER BY classoid, objoid, objsubid");
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Construct lookup table containing OIDs in numeric form */
i_label = PQfnumber(res, "label");
"WHERE attrelid = '%u' AND NOT attisdropped AND attacl IS NOT NULL "
"ORDER BY attnum",
tbinfo->dobj.catId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
for (i = 0; i < PQntuples(res); i++)
{
selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
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);
/* Is it a table or a view? */
appendPQExpBuffer(query, ";");
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) != 1)
{
fmtId(tbinfo->dobj.name));
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));
if (tbinfo->reloptions && strlen(tbinfo->reloptions) > 0)
"ON (fs.oid = ft.ftserver) "
"WHERE ft.ftrelid = '%u'",
tbinfo->dobj.catId.oid);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) != 1)
{
write_msg(NULL, ngettext("query returned %d foreign server entry for foreign table \"%s\"\n",
fmtId(tbinfo->dobj.name));
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",
tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
if (indxinfo->indexconstraint == 0)
{
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 */
appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef);
}
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",
fmtId(tbinfo->dobj.name));
appendPQExpBuffer(query, "SELECT datlastsysoid from pg_database where datname = ");
appendStringLiteralAH(query, dbname, fout);
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
if (ntups < 1)
* initdb won't be changing anymore, it'll do.
*/
static Oid
-findLastBuiltinOid_V70(void)
+findLastBuiltinOid_V70(Archive *fout)
{
PGresult *res;
int ntups;
int last_oid;
- res = PQexec(g_conn,
- "SELECT oid FROM pg_class WHERE relname = 'pg_indexes'");
- check_sql_result(res, g_conn,
- "SELECT oid FROM pg_class WHERE relname = 'pg_indexes'",
- PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout,
+ "SELECT oid FROM pg_class WHERE relname = 'pg_indexes'",
+ PGRES_TUPLES_OK);
ntups = PQntuples(res);
if (ntups < 1)
{
fmtId(tbinfo->dobj.name));
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) != 1)
{
if (binary_upgrade)
{
- binary_upgrade_set_pg_class_oids(query, tbinfo->dobj.catId.oid, false);
- binary_upgrade_set_type_oids_by_rel_oid(query, tbinfo->dobj.catId.oid);
+ binary_upgrade_set_pg_class_oids(fout, query,
+ tbinfo->dobj.catId.oid, false);
+ binary_upgrade_set_type_oids_by_rel_oid(fout, query,
+ tbinfo->dobj.catId.oid);
}
appendPQExpBuffer(query,
rinfo->dobj.name);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) != 1)
{
"AND deptype = 'e' "
"ORDER BY 3,4");
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
"WHERE deptype != 'p' AND deptype != 'e' "
"ORDER BY 1,2");
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
ntups = PQntuples(res);
if (strcmp(schemaName, "pg_catalog") != 0)
appendPQExpBuffer(query, ", pg_catalog");
- do_sql_command(g_conn, query->data);
+ ExecuteSqlStatement(fout, query->data);
destroyPQExpBuffer(query);
if (curSchemaName)
oid);
}
- res = PQexec(g_conn, query->data);
- check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(res);
appendPQExpBuffer(q, ")");
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();
-}