Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1cd2445

Browse files
committed
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.
1 parent 2a9e04f commit 1cd2445

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static char *convertRegProcReference(Archive *fout,
251251
const char *proc);
252252
static char *getFormattedOperatorName(Archive *fout, const char *oproid);
253253
static char *convertTSFunction(Archive *fout, Oid funcOid);
254-
static Oid findLastBuiltinOid_V71(Archive *fout, const char *);
254+
static Oid findLastBuiltinOid_V71(Archive *fout);
255255
static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
256256
static void getBlobs(Archive *fout);
257257
static void dumpBlob(Archive *fout, BlobInfo *binfo);
@@ -735,8 +735,7 @@ main(int argc, char **argv)
735735
* With 8.1 and above, we can just use FirstNormalObjectId - 1.
736736
*/
737737
if (fout->remoteVersion < 80100)
738-
g_last_builtin_oid = findLastBuiltinOid_V71(fout,
739-
PQdb(GetConnection(fout)));
738+
g_last_builtin_oid = findLastBuiltinOid_V71(fout);
740739
else
741740
g_last_builtin_oid = FirstNormalObjectId - 1;
742741

@@ -2538,6 +2537,7 @@ dumpDatabase(Archive *fout)
25382537
PGresult *res;
25392538
int i_tableoid,
25402539
i_oid,
2540+
i_datname,
25412541
i_dba,
25422542
i_encoding,
25432543
i_collate,
@@ -2565,16 +2565,13 @@ dumpDatabase(Archive *fout)
25652565
minmxid;
25662566
char *qdatname;
25672567

2568-
datname = PQdb(conn);
2569-
qdatname = pg_strdup(fmtId(datname));
2570-
25712568
if (g_verbose)
25722569
write_msg(NULL, "saving database definition\n");
25732570

25742571
/* Fetch the database-level properties for this database */
25752572
if (fout->remoteVersion >= 90600)
25762573
{
2577-
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2574+
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
25782575
"(%s datdba) AS dba, "
25792576
"pg_encoding_to_char(encoding) AS encoding, "
25802577
"datcollate, datctype, datfrozenxid, datminmxid, "
@@ -2591,13 +2588,12 @@ dumpDatabase(Archive *fout)
25912588
"shobj_description(oid, 'pg_database') AS description "
25922589

25932590
"FROM pg_database "
2594-
"WHERE datname = ",
2591+
"WHERE datname = current_database()",
25952592
username_subquery);
2596-
appendStringLiteralAH(dbQry, datname, fout);
25972593
}
25982594
else if (fout->remoteVersion >= 90300)
25992595
{
2600-
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2596+
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
26012597
"(%s datdba) AS dba, "
26022598
"pg_encoding_to_char(encoding) AS encoding, "
26032599
"datcollate, datctype, datfrozenxid, datminmxid, "
@@ -2606,13 +2602,12 @@ dumpDatabase(Archive *fout)
26062602
"shobj_description(oid, 'pg_database') AS description "
26072603

26082604
"FROM pg_database "
2609-
"WHERE datname = ",
2605+
"WHERE datname = current_database()",
26102606
username_subquery);
2611-
appendStringLiteralAH(dbQry, datname, fout);
26122607
}
26132608
else if (fout->remoteVersion >= 80400)
26142609
{
2615-
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2610+
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
26162611
"(%s datdba) AS dba, "
26172612
"pg_encoding_to_char(encoding) AS encoding, "
26182613
"datcollate, datctype, datfrozenxid, 0 AS datminmxid, "
@@ -2621,13 +2616,12 @@ dumpDatabase(Archive *fout)
26212616
"shobj_description(oid, 'pg_database') AS description "
26222617

26232618
"FROM pg_database "
2624-
"WHERE datname = ",
2619+
"WHERE datname = current_database()",
26252620
username_subquery);
2626-
appendStringLiteralAH(dbQry, datname, fout);
26272621
}
26282622
else if (fout->remoteVersion >= 80200)
26292623
{
2630-
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2624+
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
26312625
"(%s datdba) AS dba, "
26322626
"pg_encoding_to_char(encoding) AS encoding, "
26332627
"NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
@@ -2636,29 +2630,28 @@ dumpDatabase(Archive *fout)
26362630
"shobj_description(oid, 'pg_database') AS description "
26372631

26382632
"FROM pg_database "
2639-
"WHERE datname = ",
2633+
"WHERE datname = current_database()",
26402634
username_subquery);
2641-
appendStringLiteralAH(dbQry, datname, fout);
26422635
}
26432636
else
26442637
{
2645-
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2638+
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
26462639
"(%s datdba) AS dba, "
26472640
"pg_encoding_to_char(encoding) AS encoding, "
26482641
"NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
26492642
"datacl, '' as rdatacl, datistemplate, "
26502643
"-1 as datconnlimit, "
26512644
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace "
26522645
"FROM pg_database "
2653-
"WHERE datname = ",
2646+
"WHERE datname = current_database()",
26542647
username_subquery);
2655-
appendStringLiteralAH(dbQry, datname, fout);
26562648
}
26572649

26582650
res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
26592651

26602652
i_tableoid = PQfnumber(res, "tableoid");
26612653
i_oid = PQfnumber(res, "oid");
2654+
i_datname = PQfnumber(res, "datname");
26622655
i_dba = PQfnumber(res, "dba");
26632656
i_encoding = PQfnumber(res, "encoding");
26642657
i_collate = PQfnumber(res, "datcollate");
@@ -2673,6 +2666,7 @@ dumpDatabase(Archive *fout)
26732666

26742667
dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
26752668
dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
2669+
datname = PQgetvalue(res, 0, i_datname);
26762670
dba = PQgetvalue(res, 0, i_dba);
26772671
encoding = PQgetvalue(res, 0, i_encoding);
26782672
collate = PQgetvalue(res, 0, i_collate);
@@ -2685,6 +2679,8 @@ dumpDatabase(Archive *fout)
26852679
datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
26862680
tablespace = PQgetvalue(res, 0, i_tablespace);
26872681

2682+
qdatname = pg_strdup(fmtId(datname));
2683+
26882684
/*
26892685
* Prepare the CREATE DATABASE command. We must specify encoding, locale,
26902686
* and tablespace since those can't be altered later. Other DB properties
@@ -16586,23 +16582,19 @@ dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
1658616582
* find the last built in oid
1658716583
*
1658816584
* For 7.1 through 8.0, we do this by retrieving datlastsysoid from the
16589-
* pg_database entry for the current database.
16585+
* pg_database entry for the current database. (Note: current_database()
16586+
* requires 7.3; pg_dump requires 8.0 now.)
1659016587
*/
1659116588
static Oid
16592-
findLastBuiltinOid_V71(Archive *fout, const char *dbname)
16589+
findLastBuiltinOid_V71(Archive *fout)
1659316590
{
1659416591
PGresult *res;
1659516592
Oid last_oid;
16596-
PQExpBuffer query = createPQExpBuffer();
1659716593

16598-
resetPQExpBuffer(query);
16599-
appendPQExpBufferStr(query, "SELECT datlastsysoid from pg_database where datname = ");
16600-
appendStringLiteralAH(query, dbname, fout);
16601-
16602-
res = ExecuteSqlQueryForSingleRow(fout, query->data);
16594+
res = ExecuteSqlQueryForSingleRow(fout,
16595+
"SELECT datlastsysoid FROM pg_database WHERE datname = current_database()");
1660316596
last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "datlastsysoid")));
1660416597
PQclear(res);
16605-
destroyPQExpBuffer(query);
1660616598

1660716599
return last_oid;
1660816600
}

0 commit comments

Comments
 (0)