]> granicus.if.org Git - postgresql/commitdiff
Fix O(N^2) behavior in pg_dump for large numbers of owned sequences.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 31 Mar 2012 18:42:23 +0000 (14:42 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 31 Mar 2012 18:42:23 +0000 (14:42 -0400)
The loop that matched owned sequences to their owning tables required time
proportional to number of owned sequences times number of tables; although
this work was only expended in selective-dump situations, which is probably
why the issue wasn't recognized long since.  Refactor slightly so that we
can perform this work after the index array for findTableByOid has been
set up, reducing the time to O(M log N).

Per gripe from Mike Roest.  Since this is a longstanding performance bug,
backpatch to all supported versions.

src/bin/pg_dump/common.c
src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_dump.h

index 54451fcafcd9f7829d20375cc7f509d4f0227216..d52566d64e4a7eb279079bdaed7604ebcc8c36a6 100644 (file)
@@ -116,6 +116,9 @@ getSchemaData(int *numTablesPtr)
        tblinfo = getTables(&numTables);
        tblinfoindex = buildIndexArray(tblinfo, numTables, sizeof(TableInfo));
 
+       /* Do this after we've built tblinfoindex */
+       getOwnedSeqs(tblinfo, numTables);
+
        if (g_verbose)
                write_msg(NULL, "reading extensions\n");
        extinfo = getExtensions(&numExtensions);
index fcfc19b966839feac0732ebeef795d9ffd5e315f..a4c6d420ec64b7007f38bd08cd94c47799a5ff3a 100644 (file)
@@ -4303,38 +4303,43 @@ getTables(int *numTables)
 
        PQclear(res);
 
+       destroyPQExpBuffer(query);
+
+       return tblinfo;
+}
+
+/*
+ * getOwnedSeqs
+ *       identify owned sequences and mark them as dumpable if owning table is
+ *
+ * We used to do this in getTables(), but it's better to do it after the
+ * index used by findTableByOid() has been set up.
+ */
+void
+getOwnedSeqs(TableInfo tblinfo[], int numTables)
+{
+       int                     i;
+
        /*
         * Force sequences that are "owned" by table columns to be dumped whenever
         * their owning table is being dumped.
         */
-       for (i = 0; i < ntups; i++)
+       for (i = 0; i < numTables; i++)
        {
                TableInfo  *seqinfo = &tblinfo[i];
-               int                     j;
+               TableInfo  *owning_tab;
 
                if (!OidIsValid(seqinfo->owning_tab))
                        continue;                       /* not an owned sequence */
                if (seqinfo->dobj.dump)
                        continue;                       /* no need to search */
-
-               /* can't use findTableByOid yet, unfortunately */
-               for (j = 0; j < ntups; j++)
+               owning_tab = findTableByOid(seqinfo->owning_tab);
+               if (owning_tab && owning_tab->dobj.dump)
                {
-                       if (tblinfo[j].dobj.catId.oid == seqinfo->owning_tab)
-                       {
-                               if (tblinfo[j].dobj.dump)
-                               {
-                                       seqinfo->interesting = true;
-                                       seqinfo->dobj.dump = true;
-                               }
-                               break;
-                       }
+                       seqinfo->interesting = true;
+                       seqinfo->dobj.dump = true;
                }
        }
-
-       destroyPQExpBuffer(query);
-
-       return tblinfo;
 }
 
 /*
index 1539685587e0ceffaf94d27b431b0b1d3b8c3ebb..37b19d2f12e55f539c186bc7f0b7ab4f5af52e64 100644 (file)
@@ -540,6 +540,7 @@ extern OpfamilyInfo *getOpfamilies(int *numOpfamilies);
 extern CollInfo *getCollations(int *numCollations);
 extern ConvInfo *getConversions(int *numConversions);
 extern TableInfo *getTables(int *numTables);
+extern void getOwnedSeqs(TableInfo tblinfo[], int numTables);
 extern InhInfo *getInherits(int *numInherits);
 extern void getIndexes(TableInfo tblinfo[], int numTables);
 extern void getConstraints(TableInfo tblinfo[], int numTables);