@@ -86,7 +86,6 @@ typedef struct
86
86
/* global decls */
87
87
bool g_verbose ;/* User wants verbose narration of our
88
88
* activities. */
89
- PGconn * g_conn ;/* the database connection */
90
89
91
90
/* various user-settable parameters */
92
91
bool schemaOnly ;
@@ -614,9 +613,7 @@ main(int argc, char **argv)
614
613
* Open the database using the Archiver, so it knows about it. Errors mean
615
614
* death.
616
615
*/
617
- g_conn = ConnectDatabase (fout ,dbname ,pghost ,pgport ,
618
- username ,prompt_password );
619
-
616
+ ConnectDatabase (fout ,dbname ,pghost ,pgport ,username ,prompt_password );
620
617
setup_connection (fout ,dumpencoding ,use_role );
621
618
622
619
/*
@@ -657,7 +654,8 @@ main(int argc, char **argv)
657
654
if (fout -> remoteVersion < 70300 )
658
655
{
659
656
if (fout -> remoteVersion >=70100 )
660
- g_last_builtin_oid = findLastBuiltinOid_V71 (fout ,PQdb (g_conn ));
657
+ g_last_builtin_oid = findLastBuiltinOid_V71 (fout ,
658
+ PQdb (GetConnection (fout )));
661
659
else
662
660
g_last_builtin_oid = findLastBuiltinOid_V70 (fout );
663
661
if (g_verbose )
@@ -870,12 +868,13 @@ pgdump_cleanup_at_exit(int code, void *arg)
870
868
static void
871
869
setup_connection (Archive * AH ,const char * dumpencoding ,char * use_role )
872
870
{
871
+ PGconn * conn = GetConnection (AH );
873
872
const char * std_strings ;
874
873
875
874
/* Set the client encoding if requested */
876
875
if (dumpencoding )
877
876
{
878
- if (PQsetClientEncoding (g_conn ,dumpencoding )< 0 )
877
+ if (PQsetClientEncoding (conn ,dumpencoding )< 0 )
879
878
{
880
879
write_msg (NULL ,"invalid client encoding \"%s\" specified\n" ,
881
880
dumpencoding );
@@ -887,9 +886,9 @@ setup_connection(Archive *AH, const char *dumpencoding, char *use_role)
887
886
* Get the active encoding and the standard_conforming_strings setting, so
888
887
* we know how to escape strings.
889
888
*/
890
- AH -> encoding = PQclientEncoding (g_conn );
889
+ AH -> encoding = PQclientEncoding (conn );
891
890
892
- std_strings = PQparameterStatus (g_conn ,"standard_conforming_strings" );
891
+ std_strings = PQparameterStatus (conn ,"standard_conforming_strings" );
893
892
AH -> std_strings = (std_strings && strcmp (std_strings ,"on" )== 0 );
894
893
895
894
/* Set the role if requested */
@@ -1018,9 +1017,8 @@ expand_schema_name_patterns(Archive *fout,
1018
1017
appendPQExpBuffer (query ,"UNION ALL\n" );
1019
1018
appendPQExpBuffer (query ,
1020
1019
"SELECT oid FROM pg_catalog.pg_namespace n\n" );
1021
- processSQLNamePattern (g_conn ,query ,cell -> val , false, false,
1022
- NULL ,"n.nspname" ,NULL ,
1023
- NULL );
1020
+ processSQLNamePattern (GetConnection (fout ),query ,cell -> val , false,
1021
+ false,NULL ,"n.nspname" ,NULL ,NULL );
1024
1022
}
1025
1023
1026
1024
res = ExecuteSqlQuery (fout ,query -> data ,PGRES_TUPLES_OK );
@@ -1068,8 +1066,8 @@ expand_table_name_patterns(Archive *fout,
1068
1066
"\nWHERE c.relkind in ('%c', '%c', '%c', '%c')\n" ,
1069
1067
RELKIND_RELATION ,RELKIND_SEQUENCE ,RELKIND_VIEW ,
1070
1068
RELKIND_FOREIGN_TABLE );
1071
- processSQLNamePattern (g_conn ,query ,cell -> val , true, false ,
1072
- "n.nspname" ,"c.relname" ,NULL ,
1069
+ processSQLNamePattern (GetConnection ( fout ) ,query ,cell -> val , true,
1070
+ false, "n.nspname" ,"c.relname" ,NULL ,
1073
1071
"pg_catalog.pg_table_is_visible(c.oid)" );
1074
1072
}
1075
1073
@@ -1266,6 +1264,7 @@ dumpTableData_copy(Archive *fout, void *dcontext)
1266
1264
const bool hasoids = tbinfo -> hasoids ;
1267
1265
const bool oids = tdinfo -> oids ;
1268
1266
PQExpBuffer q = createPQExpBuffer ();
1267
+ PGconn * conn = GetConnection (fout );
1269
1268
PGresult * res ;
1270
1269
int ret ;
1271
1270
char * copybuf ;
@@ -1332,7 +1331,7 @@ dumpTableData_copy(Archive *fout, void *dcontext)
1332
1331
1333
1332
for (;;)
1334
1333
{
1335
- ret = PQgetCopyData (g_conn ,& copybuf ,0 );
1334
+ ret = PQgetCopyData (conn ,& copybuf ,0 );
1336
1335
1337
1336
if (ret < 0 )
1338
1337
break ;/* done or error */
@@ -1395,17 +1394,17 @@ dumpTableData_copy(Archive *fout, void *dcontext)
1395
1394
{
1396
1395
/* copy data transfer failed */
1397
1396
write_msg (NULL ,"Dumping the contents of table \"%s\" failed: PQgetCopyData() failed.\n" ,classname );
1398
- write_msg (NULL ,"Error message from server: %s" ,PQerrorMessage (g_conn ));
1397
+ write_msg (NULL ,"Error message from server: %s" ,PQerrorMessage (conn ));
1399
1398
write_msg (NULL ,"The command was: %s\n" ,q -> data );
1400
1399
exit_nicely (1 );
1401
1400
}
1402
1401
1403
1402
/* Check command status and return to normal libpq state */
1404
- res = PQgetResult (g_conn );
1403
+ res = PQgetResult (conn );
1405
1404
if (PQresultStatus (res )!= PGRES_COMMAND_OK )
1406
1405
{
1407
1406
write_msg (NULL ,"Dumping the contents of table \"%s\" failed: PQgetResult() failed.\n" ,classname );
1408
- write_msg (NULL ,"Error message from server: %s" ,PQerrorMessage (g_conn ));
1407
+ write_msg (NULL ,"Error message from server: %s" ,PQerrorMessage (conn ));
1409
1408
write_msg (NULL ,"The command was: %s\n" ,q -> data );
1410
1409
exit_nicely (1 );
1411
1410
}
@@ -1830,6 +1829,7 @@ dumpDatabase(Archive *fout)
1830
1829
PQExpBuffer dbQry = createPQExpBuffer ();
1831
1830
PQExpBuffer delQry = createPQExpBuffer ();
1832
1831
PQExpBuffer creaQry = createPQExpBuffer ();
1832
+ PGconn * conn = GetConnection (fout );
1833
1833
PGresult * res ;
1834
1834
int ntups ;
1835
1835
int i_tableoid ,
@@ -1850,7 +1850,7 @@ dumpDatabase(Archive *fout)
1850
1850
* tablespace ;
1851
1851
uint32 frozenxid ;
1852
1852
1853
- datname = PQdb (g_conn );
1853
+ datname = PQdb (conn );
1854
1854
1855
1855
if (g_verbose )
1856
1856
write_msg (NULL ,"saving database definition\n" );
@@ -2150,10 +2150,10 @@ dumpDatabase(Archive *fout)
2150
2150
{
2151
2151
PQExpBuffer seclabelQry = createPQExpBuffer ();
2152
2152
2153
- buildShSecLabelQuery (g_conn ,"pg_database" ,dbCatId .oid ,seclabelQry );
2153
+ buildShSecLabelQuery (conn ,"pg_database" ,dbCatId .oid ,seclabelQry );
2154
2154
res = ExecuteSqlQuery (fout ,seclabelQry -> data ,PGRES_TUPLES_OK );
2155
2155
resetPQExpBuffer (seclabelQry );
2156
- emitShSecLabels (g_conn ,res ,seclabelQry ,"DATABASE" ,datname );
2156
+ emitShSecLabels (conn ,res ,seclabelQry ,"DATABASE" ,datname );
2157
2157
if (strlen (seclabelQry -> data ))
2158
2158
ArchiveEntry (fout ,dbCatId ,createDumpId (),datname ,NULL ,NULL ,
2159
2159
dba , false,"SECURITY LABEL" ,SECTION_NONE ,
@@ -2362,6 +2362,7 @@ dumpBlobs(Archive *fout, void *arg)
2362
2362
{
2363
2363
const char * blobQry ;
2364
2364
const char * blobFetchQry ;
2365
+ PGconn * conn = GetConnection (fout );
2365
2366
PGresult * res ;
2366
2367
char buf [LOBBUFSIZE ];
2367
2368
int ntups ;
@@ -2404,11 +2405,11 @@ dumpBlobs(Archive *fout, void *arg)
2404
2405
2405
2406
blobOid = atooid (PQgetvalue (res ,i ,0 ));
2406
2407
/* Open the BLOB */
2407
- loFd = lo_open (g_conn ,blobOid ,INV_READ );
2408
+ loFd = lo_open (conn ,blobOid ,INV_READ );
2408
2409
if (loFd == -1 )
2409
2410
{
2410
2411
write_msg (NULL ,"could not open large object %u: %s" ,
2411
- blobOid ,PQerrorMessage (g_conn ));
2412
+ blobOid ,PQerrorMessage (conn ));
2412
2413
exit_nicely (1 );
2413
2414
}
2414
2415
@@ -2417,18 +2418,18 @@ dumpBlobs(Archive *fout, void *arg)
2417
2418
/* Now read it in chunks, sending data to archive */
2418
2419
do
2419
2420
{
2420
- cnt = lo_read (g_conn ,loFd ,buf ,LOBBUFSIZE );
2421
+ cnt = lo_read (conn ,loFd ,buf ,LOBBUFSIZE );
2421
2422
if (cnt < 0 )
2422
2423
{
2423
2424
write_msg (NULL ,"error reading large object %u: %s" ,
2424
- blobOid ,PQerrorMessage (g_conn ));
2425
+ blobOid ,PQerrorMessage (conn ));
2425
2426
exit_nicely (1 );
2426
2427
}
2427
2428
2428
2429
WriteData (fout ,buf ,cnt );
2429
2430
}while (cnt > 0 );
2430
2431
2431
- lo_close (g_conn ,loFd );
2432
+ lo_close (conn ,loFd );
2432
2433
2433
2434
EndBlob (fout ,blobOid );
2434
2435
}
@@ -4298,7 +4299,7 @@ getTables(Archive *fout, int *numTables)
4298
4299
*/
4299
4300
resetPQExpBuffer (query );
4300
4301
appendPQExpBuffer (query ,"SET statement_timeout = " );
4301
- appendStringLiteralConn (query ,lockWaitTimeout ,g_conn );
4302
+ appendStringLiteralConn (query ,lockWaitTimeout ,GetConnection ( fout ) );
4302
4303
ExecuteSqlStatement (fout ,query -> data );
4303
4304
}
4304
4305