Clean up some minor inefficiencies in parallel dump/restore.
authorTom Lane <[email protected]>
Wed, 1 Jun 2016 20:14:21 +0000 (16:14 -0400)
committerTom Lane <[email protected]>
Wed, 1 Jun 2016 20:14:21 +0000 (16:14 -0400)
Parallel dump did a totally pointless query to find out the name of each
table to be dumped, which it already knows.  Parallel restore runs issued
lots of redundant SET commands because _doSetFixedOutputState() was invoked
once per TOC item rather than just once at connection start.  While the
extra queries are insignificant if you're dumping or restoring large
tables, it still seems worth getting rid of them.

Also, give the responsibility for selecting the right client_encoding for
a parallel dump worker to setup_connection() where it naturally belongs,
instead of having ad-hoc code for that in CloneArchive().  And fix some
minor bugs like use of strdup() where pg_strdup() would be safer.

Back-patch to 9.3, mostly to keep the branches in sync in an area that
we're still finding bugs in.

Discussion: <5086.1464793073@sss.pgh.pa.us>

src/bin/pg_dump/parallel.c
src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_dump.c

index 908f696cc510ca669aaa0a29c5cec4af282958b7..993575de4ed86fbac60c7c56339c35f4b72fdfa6 100644 (file)
@@ -804,7 +804,6 @@ IsEveryWorkerIdle(ParallelState *pstate)
 static void
 lockTableForWorker(ArchiveHandle *AH, TocEntry *te)
 {
-       Archive    *AHX = (Archive *) AH;
        const char *qualId;
        PQExpBuffer query;
        PGresult   *res;
@@ -815,33 +814,10 @@ lockTableForWorker(ArchiveHandle *AH, TocEntry *te)
 
        query = createPQExpBuffer();
 
-       /*
-        * XXX this is an unbelievably expensive substitute for knowing how to dig
-        * a table name out of a TocEntry.
-        */
-       appendPQExpBuffer(query,
-                                         "SELECT pg_namespace.nspname,"
-                                         "       pg_class.relname "
-                                         "  FROM pg_class "
-                                       "  JOIN pg_namespace on pg_namespace.oid = relnamespace "
-                                         " WHERE pg_class.oid = %u", te->catalogId.oid);
-
-       res = PQexec(AH->connection, query->data);
-
-       if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
-               exit_horribly(modulename,
-                                         "could not get relation name for OID %u: %s\n",
-                                         te->catalogId.oid, PQerrorMessage(AH->connection));
-
-       resetPQExpBuffer(query);
-
-       qualId = fmtQualifiedId(AHX->remoteVersion,
-                                                       PQgetvalue(res, 0, 0),
-                                                       PQgetvalue(res, 0, 1));
+       qualId = fmtQualifiedId(AH->public.remoteVersion, te->namespace, te->tag);
 
        appendPQExpBuffer(query, "LOCK TABLE %s IN ACCESS SHARE MODE NOWAIT",
                                          qualId);
-       PQclear(res);
 
        res = PQexec(AH->connection, query->data);
 
index b46abb2cb608460bbd0fd22006ba8424b1be9da2..3ccbdf25cf589fe4241506dc9199b74d151299ff 100644 (file)
@@ -3647,6 +3647,7 @@ restore_toc_entries_postfork(ArchiveHandle *AH, TocEntry *pending_list)
                                        ropt->pghost, ropt->pgport, ropt->username,
                                        ropt->promptPassword);
 
+       /* re-establish fixed state */
        _doSetFixedOutputState(AH);
 
        /*
@@ -3826,10 +3827,9 @@ parallel_restore(ParallelArgs *args)
        RestoreOptions *ropt = AH->ropt;
        int                     status;
 
-       _doSetFixedOutputState(AH);
-
        Assert(AH->connection != NULL);
 
+       /* Count only errors associated with this TOC entry */
        AH->public.n_errors = 0;
 
        /* Restore the TOC item */
@@ -4198,10 +4198,14 @@ CloneArchive(ArchiveHandle *AH)
                RestoreOptions *ropt = AH->ropt;
 
                Assert(AH->connection == NULL);
+
                /* this also sets clone->connection */
                ConnectDatabase((Archive *) clone, ropt->dbname,
                                                ropt->pghost, ropt->pgport, ropt->username,
                                                ropt->promptPassword);
+
+               /* re-establish fixed state */
+               _doSetFixedOutputState(clone);
        }
        else
        {
@@ -4209,7 +4213,6 @@ CloneArchive(ArchiveHandle *AH)
                char       *pghost;
                char       *pgport;
                char       *username;
-               const char *encname;
 
                Assert(AH->connection != NULL);
 
@@ -4223,18 +4226,11 @@ CloneArchive(ArchiveHandle *AH)
                pghost = PQhost(AH->connection);
                pgport = PQport(AH->connection);
                username = PQuser(AH->connection);
-               encname = pg_encoding_to_char(AH->public.encoding);
 
                /* this also sets clone->connection */
                ConnectDatabase((Archive *) clone, dbname, pghost, pgport, username, TRI_NO);
 
-               /*
-                * Set the same encoding, whatever we set here is what we got from
-                * pg_encoding_to_char(), so we really shouldn't run into an error
-                * setting that very same value. Also see the comment in
-                * SetupConnection().
-                */
-               PQsetClientEncoding(clone->connection, encname);
+               /* setupDumpWorker will fix up connection state */
        }
 
        /* Let the format-specific code have a chance too */
index f4e2bfe2386fc655d265a4e34c12f20f46d80a96..be2e26dd352c0120c2c7754dc26ea44c762ea872 100644 (file)
@@ -940,10 +940,7 @@ setup_connection(Archive *AH, const char *dumpencoding, char *use_role)
        const char *std_strings;
 
        /*
-        * Set the client encoding if requested. If dumpencoding == NULL then
-        * either it hasn't been requested or we're a cloned connection and then
-        * this has already been set in CloneArchive according to the original
-        * connection encoding.
+        * Set the client encoding if requested.
         */
        if (dumpencoding)
        {
@@ -961,7 +958,11 @@ setup_connection(Archive *AH, const char *dumpencoding, char *use_role)
        std_strings = PQparameterStatus(conn, "standard_conforming_strings");
        AH->std_strings = (std_strings && strcmp(std_strings, "on") == 0);
 
-       /* Set the role if requested */
+       /*
+        * Set the role if requested.  In a parallel dump worker, we'll be passed
+        * use_role == NULL, but AH->use_role is already set (if user specified it
+        * originally) and we should use that.
+        */
        if (!use_role && AH->use_role)
                use_role = AH->use_role;
 
@@ -974,9 +975,9 @@ setup_connection(Archive *AH, const char *dumpencoding, char *use_role)
                ExecuteSqlStatement(AH, query->data);
                destroyPQExpBuffer(query);
 
-               /* save this for later use on parallel connections */
+               /* save it for possible later use by parallel workers */
                if (!AH->use_role)
-                       AH->use_role = strdup(use_role);
+                       AH->use_role = pg_strdup(use_role);
        }
 
        /* Set the datestyle to ISO to ensure the dump's portability */
@@ -1068,21 +1069,30 @@ setup_connection(Archive *AH, const char *dumpencoding, char *use_role)
        }
 }
 
+/* Set up connection for a parallel worker process */
 static void
-setupDumpWorker(Archive *AHX, RestoreOptions *ropt)
+setupDumpWorker(Archive *AH, RestoreOptions *ropt)
 {
-       setup_connection(AHX, NULL, NULL);
+       /*
+        * We want to re-select all the same values the master connection is
+        * using.  We'll have inherited directly-usable values in
+        * AH->sync_snapshot_id and AH->use_role, but we need to translate the
+        * inherited encoding value back to a string to pass to setup_connection.
+        */
+       setup_connection(AH,
+                                        pg_encoding_to_char(AH->encoding),
+                                        NULL);
 }
 
 static char *
 get_synchronized_snapshot(Archive *fout)
 {
-       char       *query = "SELECT pg_export_snapshot()";
+       char       *query = "SELECT pg_catalog.pg_export_snapshot()";
        char       *result;
        PGresult   *res;
 
        res = ExecuteSqlQueryForSingleRow(fout, query);
-       result = strdup(PQgetvalue(res, 0, 0));
+       result = pg_strdup(PQgetvalue(res, 0, 0));
        PQclear(res);
 
        return result;