@@ -251,7 +251,7 @@ static char *convertRegProcReference(Archive *fout,
251
251
const char *proc);
252
252
static char *getFormattedOperatorName(Archive *fout, const char *oproid);
253
253
static char *convertTSFunction(Archive *fout, Oid funcOid);
254
- static Oid findLastBuiltinOid_V71(Archive *fout, const char * );
254
+ static Oid findLastBuiltinOid_V71(Archive *fout);
255
255
static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
256
256
static void getBlobs(Archive *fout);
257
257
static void dumpBlob(Archive *fout, BlobInfo *binfo);
@@ -735,8 +735,7 @@ main(int argc, char **argv)
735
735
* With 8.1 and above, we can just use FirstNormalObjectId - 1.
736
736
*/
737
737
if (fout->remoteVersion < 80100)
738
- g_last_builtin_oid = findLastBuiltinOid_V71(fout,
739
- PQdb(GetConnection(fout)));
738
+ g_last_builtin_oid = findLastBuiltinOid_V71(fout);
740
739
else
741
740
g_last_builtin_oid = FirstNormalObjectId - 1;
742
741
@@ -2538,6 +2537,7 @@ dumpDatabase(Archive *fout)
2538
2537
PGresult *res;
2539
2538
int i_tableoid,
2540
2539
i_oid,
2540
+ i_datname,
2541
2541
i_dba,
2542
2542
i_encoding,
2543
2543
i_collate,
@@ -2565,16 +2565,13 @@ dumpDatabase(Archive *fout)
2565
2565
minmxid;
2566
2566
char *qdatname;
2567
2567
2568
- datname = PQdb(conn);
2569
- qdatname = pg_strdup(fmtId(datname));
2570
-
2571
2568
if (g_verbose)
2572
2569
write_msg(NULL, "saving database definition\n");
2573
2570
2574
2571
/* Fetch the database-level properties for this database */
2575
2572
if (fout->remoteVersion >= 90600)
2576
2573
{
2577
- appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2574
+ appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2578
2575
"(%s datdba) AS dba, "
2579
2576
"pg_encoding_to_char(encoding) AS encoding, "
2580
2577
"datcollate, datctype, datfrozenxid, datminmxid, "
@@ -2591,13 +2588,12 @@ dumpDatabase(Archive *fout)
2591
2588
"shobj_description(oid, 'pg_database') AS description "
2592
2589
2593
2590
"FROM pg_database "
2594
- "WHERE datname = ",
2591
+ "WHERE datname = current_database() ",
2595
2592
username_subquery);
2596
- appendStringLiteralAH(dbQry, datname, fout);
2597
2593
}
2598
2594
else if (fout->remoteVersion >= 90300)
2599
2595
{
2600
- appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2596
+ appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2601
2597
"(%s datdba) AS dba, "
2602
2598
"pg_encoding_to_char(encoding) AS encoding, "
2603
2599
"datcollate, datctype, datfrozenxid, datminmxid, "
@@ -2606,13 +2602,12 @@ dumpDatabase(Archive *fout)
2606
2602
"shobj_description(oid, 'pg_database') AS description "
2607
2603
2608
2604
"FROM pg_database "
2609
- "WHERE datname = ",
2605
+ "WHERE datname = current_database() ",
2610
2606
username_subquery);
2611
- appendStringLiteralAH(dbQry, datname, fout);
2612
2607
}
2613
2608
else if (fout->remoteVersion >= 80400)
2614
2609
{
2615
- appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2610
+ appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2616
2611
"(%s datdba) AS dba, "
2617
2612
"pg_encoding_to_char(encoding) AS encoding, "
2618
2613
"datcollate, datctype, datfrozenxid, 0 AS datminmxid, "
@@ -2621,13 +2616,12 @@ dumpDatabase(Archive *fout)
2621
2616
"shobj_description(oid, 'pg_database') AS description "
2622
2617
2623
2618
"FROM pg_database "
2624
- "WHERE datname = ",
2619
+ "WHERE datname = current_database() ",
2625
2620
username_subquery);
2626
- appendStringLiteralAH(dbQry, datname, fout);
2627
2621
}
2628
2622
else if (fout->remoteVersion >= 80200)
2629
2623
{
2630
- appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2624
+ appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2631
2625
"(%s datdba) AS dba, "
2632
2626
"pg_encoding_to_char(encoding) AS encoding, "
2633
2627
"NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
@@ -2636,29 +2630,28 @@ dumpDatabase(Archive *fout)
2636
2630
"shobj_description(oid, 'pg_database') AS description "
2637
2631
2638
2632
"FROM pg_database "
2639
- "WHERE datname = ",
2633
+ "WHERE datname = current_database() ",
2640
2634
username_subquery);
2641
- appendStringLiteralAH(dbQry, datname, fout);
2642
2635
}
2643
2636
else
2644
2637
{
2645
- appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2638
+ appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2646
2639
"(%s datdba) AS dba, "
2647
2640
"pg_encoding_to_char(encoding) AS encoding, "
2648
2641
"NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
2649
2642
"datacl, '' as rdatacl, datistemplate, "
2650
2643
"-1 as datconnlimit, "
2651
2644
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace "
2652
2645
"FROM pg_database "
2653
- "WHERE datname = ",
2646
+ "WHERE datname = current_database() ",
2654
2647
username_subquery);
2655
- appendStringLiteralAH(dbQry, datname, fout);
2656
2648
}
2657
2649
2658
2650
res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
2659
2651
2660
2652
i_tableoid = PQfnumber(res, "tableoid");
2661
2653
i_oid = PQfnumber(res, "oid");
2654
+ i_datname = PQfnumber(res, "datname");
2662
2655
i_dba = PQfnumber(res, "dba");
2663
2656
i_encoding = PQfnumber(res, "encoding");
2664
2657
i_collate = PQfnumber(res, "datcollate");
@@ -2673,6 +2666,7 @@ dumpDatabase(Archive *fout)
2673
2666
2674
2667
dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
2675
2668
dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
2669
+ datname = PQgetvalue(res, 0, i_datname);
2676
2670
dba = PQgetvalue(res, 0, i_dba);
2677
2671
encoding = PQgetvalue(res, 0, i_encoding);
2678
2672
collate = PQgetvalue(res, 0, i_collate);
@@ -2685,6 +2679,8 @@ dumpDatabase(Archive *fout)
2685
2679
datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
2686
2680
tablespace = PQgetvalue(res, 0, i_tablespace);
2687
2681
2682
+ qdatname = pg_strdup(fmtId(datname));
2683
+
2688
2684
/*
2689
2685
* Prepare the CREATE DATABASE command. We must specify encoding, locale,
2690
2686
* and tablespace since those can't be altered later. Other DB properties
@@ -16586,23 +16582,19 @@ dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
16586
16582
* find the last built in oid
16587
16583
*
16588
16584
* 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.)
16590
16587
*/
16591
16588
static Oid
16592
- findLastBuiltinOid_V71(Archive *fout, const char *dbname )
16589
+ findLastBuiltinOid_V71(Archive *fout)
16593
16590
{
16594
16591
PGresult *res;
16595
16592
Oid last_oid;
16596
- PQExpBuffer query = createPQExpBuffer();
16597
16593
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()");
16603
16596
last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "datlastsysoid")));
16604
16597
PQclear(res);
16605
- destroyPQExpBuffer(query);
16606
16598
16607
16599
return last_oid;
16608
16600
}
0 commit comments