Commit 1cd2445c authored by Peter Eisentraut's avatar Peter Eisentraut

pg_dump: Use current_database() instead of PQdb()

For querying pg_database about information about the database being
dumped, look up by using current_database() instead of the value
obtained from PQdb().  When using a connection proxy, the value from
PQdb() might not be the real name of the database.
parent 2a9e04f0
...@@ -251,7 +251,7 @@ static char *convertRegProcReference(Archive *fout, ...@@ -251,7 +251,7 @@ static char *convertRegProcReference(Archive *fout,
const char *proc); const char *proc);
static char *getFormattedOperatorName(Archive *fout, const char *oproid); static char *getFormattedOperatorName(Archive *fout, const char *oproid);
static char *convertTSFunction(Archive *fout, Oid funcOid); static char *convertTSFunction(Archive *fout, Oid funcOid);
static Oid findLastBuiltinOid_V71(Archive *fout, const char *); static Oid findLastBuiltinOid_V71(Archive *fout);
static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts); static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
static void getBlobs(Archive *fout); static void getBlobs(Archive *fout);
static void dumpBlob(Archive *fout, BlobInfo *binfo); static void dumpBlob(Archive *fout, BlobInfo *binfo);
...@@ -735,8 +735,7 @@ main(int argc, char **argv) ...@@ -735,8 +735,7 @@ main(int argc, char **argv)
* With 8.1 and above, we can just use FirstNormalObjectId - 1. * With 8.1 and above, we can just use FirstNormalObjectId - 1.
*/ */
if (fout->remoteVersion < 80100) if (fout->remoteVersion < 80100)
g_last_builtin_oid = findLastBuiltinOid_V71(fout, g_last_builtin_oid = findLastBuiltinOid_V71(fout);
PQdb(GetConnection(fout)));
else else
g_last_builtin_oid = FirstNormalObjectId - 1; g_last_builtin_oid = FirstNormalObjectId - 1;
...@@ -2538,6 +2537,7 @@ dumpDatabase(Archive *fout) ...@@ -2538,6 +2537,7 @@ dumpDatabase(Archive *fout)
PGresult *res; PGresult *res;
int i_tableoid, int i_tableoid,
i_oid, i_oid,
i_datname,
i_dba, i_dba,
i_encoding, i_encoding,
i_collate, i_collate,
...@@ -2565,16 +2565,13 @@ dumpDatabase(Archive *fout) ...@@ -2565,16 +2565,13 @@ dumpDatabase(Archive *fout)
minmxid; minmxid;
char *qdatname; char *qdatname;
datname = PQdb(conn);
qdatname = pg_strdup(fmtId(datname));
if (g_verbose) if (g_verbose)
write_msg(NULL, "saving database definition\n"); write_msg(NULL, "saving database definition\n");
/* Fetch the database-level properties for this database */ /* Fetch the database-level properties for this database */
if (fout->remoteVersion >= 90600) if (fout->remoteVersion >= 90600)
{ {
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, " appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
"(%s datdba) AS dba, " "(%s datdba) AS dba, "
"pg_encoding_to_char(encoding) AS encoding, " "pg_encoding_to_char(encoding) AS encoding, "
"datcollate, datctype, datfrozenxid, datminmxid, " "datcollate, datctype, datfrozenxid, datminmxid, "
...@@ -2591,13 +2588,12 @@ dumpDatabase(Archive *fout) ...@@ -2591,13 +2588,12 @@ dumpDatabase(Archive *fout)
"shobj_description(oid, 'pg_database') AS description " "shobj_description(oid, 'pg_database') AS description "
"FROM pg_database " "FROM pg_database "
"WHERE datname = ", "WHERE datname = current_database()",
username_subquery); username_subquery);
appendStringLiteralAH(dbQry, datname, fout);
} }
else if (fout->remoteVersion >= 90300) else if (fout->remoteVersion >= 90300)
{ {
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, " appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
"(%s datdba) AS dba, " "(%s datdba) AS dba, "
"pg_encoding_to_char(encoding) AS encoding, " "pg_encoding_to_char(encoding) AS encoding, "
"datcollate, datctype, datfrozenxid, datminmxid, " "datcollate, datctype, datfrozenxid, datminmxid, "
...@@ -2606,13 +2602,12 @@ dumpDatabase(Archive *fout) ...@@ -2606,13 +2602,12 @@ dumpDatabase(Archive *fout)
"shobj_description(oid, 'pg_database') AS description " "shobj_description(oid, 'pg_database') AS description "
"FROM pg_database " "FROM pg_database "
"WHERE datname = ", "WHERE datname = current_database()",
username_subquery); username_subquery);
appendStringLiteralAH(dbQry, datname, fout);
} }
else if (fout->remoteVersion >= 80400) else if (fout->remoteVersion >= 80400)
{ {
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, " appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
"(%s datdba) AS dba, " "(%s datdba) AS dba, "
"pg_encoding_to_char(encoding) AS encoding, " "pg_encoding_to_char(encoding) AS encoding, "
"datcollate, datctype, datfrozenxid, 0 AS datminmxid, " "datcollate, datctype, datfrozenxid, 0 AS datminmxid, "
...@@ -2621,13 +2616,12 @@ dumpDatabase(Archive *fout) ...@@ -2621,13 +2616,12 @@ dumpDatabase(Archive *fout)
"shobj_description(oid, 'pg_database') AS description " "shobj_description(oid, 'pg_database') AS description "
"FROM pg_database " "FROM pg_database "
"WHERE datname = ", "WHERE datname = current_database()",
username_subquery); username_subquery);
appendStringLiteralAH(dbQry, datname, fout);
} }
else if (fout->remoteVersion >= 80200) else if (fout->remoteVersion >= 80200)
{ {
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, " appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
"(%s datdba) AS dba, " "(%s datdba) AS dba, "
"pg_encoding_to_char(encoding) AS encoding, " "pg_encoding_to_char(encoding) AS encoding, "
"NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, " "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
...@@ -2636,13 +2630,12 @@ dumpDatabase(Archive *fout) ...@@ -2636,13 +2630,12 @@ dumpDatabase(Archive *fout)
"shobj_description(oid, 'pg_database') AS description " "shobj_description(oid, 'pg_database') AS description "
"FROM pg_database " "FROM pg_database "
"WHERE datname = ", "WHERE datname = current_database()",
username_subquery); username_subquery);
appendStringLiteralAH(dbQry, datname, fout);
} }
else else
{ {
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, " appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
"(%s datdba) AS dba, " "(%s datdba) AS dba, "
"pg_encoding_to_char(encoding) AS encoding, " "pg_encoding_to_char(encoding) AS encoding, "
"NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, " "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
...@@ -2650,15 +2643,15 @@ dumpDatabase(Archive *fout) ...@@ -2650,15 +2643,15 @@ dumpDatabase(Archive *fout)
"-1 as datconnlimit, " "-1 as datconnlimit, "
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace " "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace "
"FROM pg_database " "FROM pg_database "
"WHERE datname = ", "WHERE datname = current_database()",
username_subquery); username_subquery);
appendStringLiteralAH(dbQry, datname, fout);
} }
res = ExecuteSqlQueryForSingleRow(fout, dbQry->data); res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
i_tableoid = PQfnumber(res, "tableoid"); i_tableoid = PQfnumber(res, "tableoid");
i_oid = PQfnumber(res, "oid"); i_oid = PQfnumber(res, "oid");
i_datname = PQfnumber(res, "datname");
i_dba = PQfnumber(res, "dba"); i_dba = PQfnumber(res, "dba");
i_encoding = PQfnumber(res, "encoding"); i_encoding = PQfnumber(res, "encoding");
i_collate = PQfnumber(res, "datcollate"); i_collate = PQfnumber(res, "datcollate");
...@@ -2673,6 +2666,7 @@ dumpDatabase(Archive *fout) ...@@ -2673,6 +2666,7 @@ dumpDatabase(Archive *fout)
dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid)); dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid)); dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
datname = PQgetvalue(res, 0, i_datname);
dba = PQgetvalue(res, 0, i_dba); dba = PQgetvalue(res, 0, i_dba);
encoding = PQgetvalue(res, 0, i_encoding); encoding = PQgetvalue(res, 0, i_encoding);
collate = PQgetvalue(res, 0, i_collate); collate = PQgetvalue(res, 0, i_collate);
...@@ -2685,6 +2679,8 @@ dumpDatabase(Archive *fout) ...@@ -2685,6 +2679,8 @@ dumpDatabase(Archive *fout)
datconnlimit = PQgetvalue(res, 0, i_datconnlimit); datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
tablespace = PQgetvalue(res, 0, i_tablespace); tablespace = PQgetvalue(res, 0, i_tablespace);
qdatname = pg_strdup(fmtId(datname));
/* /*
* Prepare the CREATE DATABASE command. We must specify encoding, locale, * Prepare the CREATE DATABASE command. We must specify encoding, locale,
* and tablespace since those can't be altered later. Other DB properties * and tablespace since those can't be altered later. Other DB properties
...@@ -16586,23 +16582,19 @@ dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo) ...@@ -16586,23 +16582,19 @@ dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
* find the last built in oid * find the last built in oid
* *
* For 7.1 through 8.0, we do this by retrieving datlastsysoid from the * For 7.1 through 8.0, we do this by retrieving datlastsysoid from the
* pg_database entry for the current database. * pg_database entry for the current database. (Note: current_database()
* requires 7.3; pg_dump requires 8.0 now.)
*/ */
static Oid static Oid
findLastBuiltinOid_V71(Archive *fout, const char *dbname) findLastBuiltinOid_V71(Archive *fout)
{ {
PGresult *res; PGresult *res;
Oid last_oid; Oid last_oid;
PQExpBuffer query = createPQExpBuffer();
resetPQExpBuffer(query); res = ExecuteSqlQueryForSingleRow(fout,
appendPQExpBufferStr(query, "SELECT datlastsysoid from pg_database where datname = "); "SELECT datlastsysoid FROM pg_database WHERE datname = current_database()");
appendStringLiteralAH(query, dbname, fout);
res = ExecuteSqlQueryForSingleRow(fout, query->data);
last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "datlastsysoid"))); last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "datlastsysoid")));
PQclear(res); PQclear(res);
destroyPQExpBuffer(query);
return last_oid; return last_oid;
} }
......
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