]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Use binary search instead of brute-force scan in findNamespace().
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 25 May 2012 18:35:59 +0000 (14:35 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 25 May 2012 18:35:59 +0000 (14:35 -0400)
The previous coding presented a significant bottleneck when dumping
databases containing many thousands of schemas, since the total time
spent searching would increase roughly as O(N^2) in the number of objects.
Noted by Jeff Janes, though I rewrote his proposed patch to use the
existing findObjectByOid infrastructure.

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 fb2889e9c082772489efd6453ad5597a25406bad..2a3a27a98a191767e56baa8ace46c45aa7ad6275 100644 (file)
@@ -51,14 +51,17 @@ static TableInfo *tblinfo;
 static TypeInfo *typinfo;
 static FuncInfo *funinfo;
 static OprInfo *oprinfo;
+static NamespaceInfo *nspinfo;
 static int     numTables;
 static int     numTypes;
 static int     numFuncs;
 static int     numOperators;
+static int     numNamespaces;
 static DumpableObject **tblinfoindex;
 static DumpableObject **typinfoindex;
 static DumpableObject **funinfoindex;
 static DumpableObject **oprinfoindex;
+static DumpableObject **nspinfoindex;
 
 
 static void flagInhTables(TableInfo *tbinfo, int numTables,
@@ -80,7 +83,6 @@ static int    strInArray(const char *pattern, char **arr, int arr_size);
 TableInfo *
 getSchemaData(int *numTablesPtr)
 {
-       NamespaceInfo *nsinfo;
        AggInfo    *agginfo;
        InhInfo    *inhinfo;
        RuleInfo   *ruleinfo;
@@ -93,7 +95,6 @@ getSchemaData(int *numTablesPtr)
        TSTemplateInfo *tmplinfo;
        TSDictInfo *dictinfo;
        TSConfigInfo *cfginfo;
-       int                     numNamespaces;
        int                     numAggregates;
        int                     numInherits;
        int                     numRules;
@@ -109,7 +110,8 @@ getSchemaData(int *numTablesPtr)
 
        if (g_verbose)
                write_msg(NULL, "reading schemas\n");
-       nsinfo = getNamespaces(&numNamespaces);
+       nspinfo = getNamespaces(&numNamespaces);
+       nspinfoindex = buildIndexArray(nspinfo, numNamespaces, sizeof(NamespaceInfo));
 
        /*
         * getTables should be done as soon as possible, so as to minimize the
@@ -736,6 +738,17 @@ findOprByOid(Oid oid)
        return (OprInfo *) findObjectByOid(oid, oprinfoindex, numOperators);
 }
 
+/*
+ * findNamespaceByOid
+ *       finds the entry (in nspinfo) of the namespace with the given oid
+ *       returns NULL if not found
+ */
+NamespaceInfo *
+findNamespaceByOid(Oid oid)
+{
+       return (NamespaceInfo *) findObjectByOid(oid, nspinfoindex, numNamespaces);
+}
+
 
 /*
  * findParentsByOid
index d4c82bea8be99cc9f446f732a0bfe1a7527287c6..e289f9e2ba7eaa8e565a0f9dfcfd4d440101efca 100644 (file)
@@ -111,10 +111,6 @@ char               g_comment_end[10];
 
 static const CatalogId nilCatalogId = {0, 0};
 
-/* these are to avoid passing around info for findNamespace() */
-static NamespaceInfo *g_namespaces;
-static int     g_numNamespaces;
-
 /* flag to turn on/off dollar quoting */
 static int     disable_dollar_quoting = 0;
 
@@ -1887,8 +1883,7 @@ getNamespaces(int *numNamespaces)
 
                selectDumpableNamespace(&nsinfo[1]);
 
-               g_namespaces = nsinfo;
-               g_numNamespaces = *numNamespaces = 2;
+               *numNamespaces = 2;
 
                return nsinfo;
        }
@@ -1941,8 +1936,7 @@ getNamespaces(int *numNamespaces)
        PQclear(res);
        destroyPQExpBuffer(query);
 
-       g_namespaces = nsinfo;
-       g_numNamespaces = *numNamespaces = ntups;
+       *numNamespaces = ntups;
 
        return nsinfo;
 }
@@ -1953,36 +1947,37 @@ getNamespaces(int *numNamespaces)
  *             getNamespaces
  *
  * NB: for pre-7.3 source database, we use object OID to guess whether it's
- * a system object or not.     In 7.3 and later there is no guessing.
+ * a system object or not.     In 7.3 and later there is no guessing, and we
+ * don't use objoid at all.
  */
 static NamespaceInfo *
 findNamespace(Oid nsoid, Oid objoid)
 {
-       int                     i;
+       NamespaceInfo *nsinfo;
 
        if (g_fout->remoteVersion >= 70300)
        {
-               for (i = 0; i < g_numNamespaces; i++)
-               {
-                       NamespaceInfo *nsinfo = &g_namespaces[i];
-
-                       if (nsoid == nsinfo->dobj.catId.oid)
-                               return nsinfo;
-               }
-               write_msg(NULL, "schema with OID %u does not exist\n", nsoid);
-               exit_nicely();
+               nsinfo = findNamespaceByOid(nsoid);
        }
        else
        {
-               /* This code depends on the layout set up by getNamespaces. */
+               /* This code depends on the dummy objects set up by getNamespaces. */
+               Oid             i;
+
                if (objoid > g_last_builtin_oid)
                        i = 0;                          /* user object */
                else
                        i = 1;                          /* system object */
-               return &g_namespaces[i];
+               nsinfo = findNamespaceByOid(i);
        }
 
-       return NULL;                            /* keep compiler quiet */
+       if (nsinfo == NULL)
+       {
+               write_msg(NULL, "schema with OID %u does not exist\n", nsoid);
+               exit_nicely();
+       }
+
+       return nsinfo;
 }
 
 /*
index 3d213db0744c989cdc3793c159db19b6a1e998dc..bd1cd72a4ec9f917377eb67f53194e607c2ccccd 100644 (file)
@@ -445,6 +445,7 @@ extern TableInfo *findTableByOid(Oid oid);
 extern TypeInfo *findTypeByOid(Oid oid);
 extern FuncInfo *findFuncByOid(Oid oid);
 extern OprInfo *findOprByOid(Oid oid);
+extern NamespaceInfo *findNamespaceByOid(Oid oid);
 
 extern void simple_oid_list_append(SimpleOidList *list, Oid val);
 extern void simple_string_list_append(SimpleStringList *list, const char *val);