Skip to content

Commit e43166a

Browse files
committed
pg_dump: Move connection-setup code to a separate function.
Parallel dump will need to repeat these steps for each new connection, so it's better to have this logic in its own function. Extracted (with some changes) from a much larger patch by Joachim Wieland.
1 parent 59c67ec commit e43166a

File tree

1 file changed

+74
-65
lines changed

1 file changed

+74
-65
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 74 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ static int serializable_deferrable = 0;
145145

146146

147147
static void help(const char *progname);
148+
static void setup_connection(Archive *AH, const char *dumpencoding,
149+
char *use_role);
148150
static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode);
149151
static void expand_schema_name_patterns(SimpleStringList *patterns,
150152
SimpleOidList *oids);
@@ -262,7 +264,6 @@ main(int argc, char **argv)
262264
const char *pgport = NULL;
263265
const char *username = NULL;
264266
const char *dumpencoding = NULL;
265-
const char *std_strings;
266267
bool oids = false;
267268
TableInfo *tblinfo;
268269
int numTables;
@@ -608,70 +609,7 @@ main(int argc, char **argv)
608609
g_conn = ConnectDatabase(g_fout, dbname, pghost, pgport,
609610
username, prompt_password);
610611

611-
/* Set the client encoding if requested */
612-
if (dumpencoding)
613-
{
614-
if (PQsetClientEncoding(g_conn, dumpencoding) < 0)
615-
{
616-
write_msg(NULL, "invalid client encoding \"%s\" specified\n",
617-
dumpencoding);
618-
exit(1);
619-
}
620-
}
621-
622-
/*
623-
* Get the active encoding and the standard_conforming_strings setting, so
624-
* we know how to escape strings.
625-
*/
626-
g_fout->encoding = PQclientEncoding(g_conn);
627-
628-
std_strings = PQparameterStatus(g_conn, "standard_conforming_strings");
629-
g_fout->std_strings = (std_strings && strcmp(std_strings, "on") == 0);
630-
631-
/* Set the role if requested */
632-
if (use_role && g_fout->remoteVersion >= 80100)
633-
{
634-
PQExpBuffer query = createPQExpBuffer();
635-
636-
appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
637-
do_sql_command(g_conn, query->data);
638-
destroyPQExpBuffer(query);
639-
}
640-
641-
/* Set the datestyle to ISO to ensure the dump's portability */
642-
do_sql_command(g_conn, "SET DATESTYLE = ISO");
643-
644-
/* Likewise, avoid using sql_standard intervalstyle */
645-
if (g_fout->remoteVersion >= 80400)
646-
do_sql_command(g_conn, "SET INTERVALSTYLE = POSTGRES");
647-
648-
/*
649-
* If supported, set extra_float_digits so that we can dump float data
650-
* exactly (given correctly implemented float I/O code, anyway)
651-
*/
652-
if (g_fout->remoteVersion >= 90000)
653-
do_sql_command(g_conn, "SET extra_float_digits TO 3");
654-
else if (g_fout->remoteVersion >= 70400)
655-
do_sql_command(g_conn, "SET extra_float_digits TO 2");
656-
657-
/*
658-
* If synchronized scanning is supported, disable it, to prevent
659-
* unpredictable changes in row ordering across a dump and reload.
660-
*/
661-
if (g_fout->remoteVersion >= 80300)
662-
do_sql_command(g_conn, "SET synchronize_seqscans TO off");
663-
664-
/*
665-
* Disable timeouts if supported.
666-
*/
667-
if (g_fout->remoteVersion >= 70300)
668-
do_sql_command(g_conn, "SET statement_timeout = 0");
669-
670-
/*
671-
* Quote all identifiers, if requested.
672-
*/
673-
if (quote_all_identifiers && g_fout->remoteVersion >= 90100)
674-
do_sql_command(g_conn, "SET quote_all_identifiers = true");
612+
setup_connection(g_fout, dumpencoding, use_role);
675613

676614
/*
677615
* Disable security label support if server version < v9.1.x (prevents
@@ -922,6 +860,77 @@ exit_nicely(void)
922860
exit(1);
923861
}
924862

863+
static void
864+
setup_connection(Archive *AH, const char *dumpencoding, char *use_role)
865+
{
866+
const char *std_strings;
867+
868+
/* Set the client encoding if requested */
869+
if (dumpencoding)
870+
{
871+
if (PQsetClientEncoding(g_conn, dumpencoding) < 0)
872+
{
873+
write_msg(NULL, "invalid client encoding \"%s\" specified\n",
874+
dumpencoding);
875+
exit(1);
876+
}
877+
}
878+
879+
/*
880+
* Get the active encoding and the standard_conforming_strings setting, so
881+
* we know how to escape strings.
882+
*/
883+
AH->encoding = PQclientEncoding(g_conn);
884+
885+
std_strings = PQparameterStatus(g_conn, "standard_conforming_strings");
886+
AH->std_strings = (std_strings && strcmp(std_strings, "on") == 0);
887+
888+
/* Set the role if requested */
889+
if (use_role && AH->remoteVersion >= 80100)
890+
{
891+
PQExpBuffer query = createPQExpBuffer();
892+
893+
appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
894+
do_sql_command(g_conn, query->data);
895+
destroyPQExpBuffer(query);
896+
}
897+
898+
/* Set the datestyle to ISO to ensure the dump's portability */
899+
do_sql_command(g_conn, "SET DATESTYLE = ISO");
900+
901+
/* Likewise, avoid using sql_standard intervalstyle */
902+
if (AH->remoteVersion >= 80400)
903+
do_sql_command(g_conn, "SET INTERVALSTYLE = POSTGRES");
904+
905+
/*
906+
* If supported, set extra_float_digits so that we can dump float data
907+
* exactly (given correctly implemented float I/O code, anyway)
908+
*/
909+
if (AH->remoteVersion >= 90000)
910+
do_sql_command(g_conn, "SET extra_float_digits TO 3");
911+
else if (AH->remoteVersion >= 70400)
912+
do_sql_command(g_conn, "SET extra_float_digits TO 2");
913+
914+
/*
915+
* If synchronized scanning is supported, disable it, to prevent
916+
* unpredictable changes in row ordering across a dump and reload.
917+
*/
918+
if (AH->remoteVersion >= 80300)
919+
do_sql_command(g_conn, "SET synchronize_seqscans TO off");
920+
921+
/*
922+
* Disable timeouts if supported.
923+
*/
924+
if (AH->remoteVersion >= 70300)
925+
do_sql_command(g_conn, "SET statement_timeout = 0");
926+
927+
/*
928+
* Quote all identifiers, if requested.
929+
*/
930+
if (quote_all_identifiers && AH->remoteVersion >= 90100)
931+
do_sql_command(g_conn, "SET quote_all_identifiers = true");
932+
}
933+
925934
static ArchiveFormat
926935
parseArchiveFormat(const char *format, ArchiveMode *mode)
927936
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy