]> granicus.if.org Git - postgresql/commitdiff
Improve initdb's query for generating default descriptions a little.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 8 May 2018 19:59:01 +0000 (15:59 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 8 May 2018 19:59:05 +0000 (15:59 -0400)
While poking into initdb's performance, I noticed that this query
wasn't being done very intelligently.  By forcing it to execute
obj_description() for each pg_proc/pg_operator join row, we were
essentially setting up a nestloop join to pg_description, which
is not a bright query plan when there are hundreds of outer rows.
Convert the check for a "deprecated" operator into a NOT EXISTS
so that it can be done as a hashed antijoin.  On my workstation
this reduces the time for this query from ~ 35ms to ~ 10ms.
Which is not a huge win, but it adds up over buildfarm runs.

In passing, insert forced query breaks (\n\n, in single-user mode)
after each SQL-query file that initdb sources, and after some
relatively new queries in setup_privileges().  This doesn't make
a lot of difference normally, but it will result in briefer, saner
error messages if anything goes wrong.

src/bin/initdb/initdb.c

index b39115c346d51833ab7e24f8fd50fdc8f9a9bda5..ae22e7d9fb89db67d187aa2e43cfe5db42011d9d 100644 (file)
@@ -1669,6 +1669,8 @@ setup_sysviews(FILE *cmdfd)
                free(*line);
        }
 
+       PG_CMD_PUTS("\n\n");
+
        free(sysviews_setup);
 }
 
@@ -1707,16 +1709,17 @@ setup_description(FILE *cmdfd)
 
        /* Create default descriptions for operator implementation functions */
        PG_CMD_PUTS("WITH funcdescs AS ( "
-                               "SELECT p.oid as p_oid, oprname, "
-                               "coalesce(obj_description(o.oid, 'pg_operator'),'') as opdesc "
+                               "SELECT p.oid as p_oid, o.oid as o_oid, oprname "
                                "FROM pg_proc p JOIN pg_operator o ON oprcode = p.oid ) "
                                "INSERT INTO pg_description "
                                "  SELECT p_oid, 'pg_proc'::regclass, 0, "
                                "    'implementation of ' || oprname || ' operator' "
                                "  FROM funcdescs "
-                               "  WHERE opdesc NOT LIKE 'deprecated%' AND "
-                               "  NOT EXISTS (SELECT 1 FROM pg_description "
-                               "    WHERE objoid = p_oid AND classoid = 'pg_proc'::regclass);\n\n");
+                               "  WHERE NOT EXISTS (SELECT 1 FROM pg_description "
+                               "   WHERE objoid = p_oid AND classoid = 'pg_proc'::regclass) "
+                               "  AND NOT EXISTS (SELECT 1 FROM pg_description "
+                               "   WHERE objoid = o_oid AND classoid = 'pg_operator'::regclass"
+                               "         AND description LIKE 'deprecated%');\n\n");
 
        /*
         * Even though the tables are temp, drop them explicitly so they don't get
@@ -1780,6 +1783,8 @@ setup_dictionary(FILE *cmdfd)
                free(*line);
        }
 
+       PG_CMD_PUTS("\n\n");
+
        free(conv_lines);
 }
 
@@ -1836,7 +1841,7 @@ setup_privileges(FILE *cmdfd)
                "        relacl IS NOT NULL"
                "        AND relkind IN (" CppAsString2(RELKIND_RELATION) ", "
                CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
-               CppAsString2(RELKIND_SEQUENCE) ");",
+               CppAsString2(RELKIND_SEQUENCE) ");\n\n",
                "INSERT INTO pg_init_privs "
                "  (objoid, classoid, objsubid, initprivs, privtype)"
                "    SELECT"
@@ -1852,7 +1857,7 @@ setup_privileges(FILE *cmdfd)
                "        pg_attribute.attacl IS NOT NULL"
                "        AND pg_class.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
                CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
-               CppAsString2(RELKIND_SEQUENCE) ");",
+               CppAsString2(RELKIND_SEQUENCE) ");\n\n",
                "INSERT INTO pg_init_privs "
                "  (objoid, classoid, objsubid, initprivs, privtype)"
                "    SELECT"
@@ -1864,7 +1869,7 @@ setup_privileges(FILE *cmdfd)
                "    FROM"
                "        pg_proc"
                "    WHERE"
-               "        proacl IS NOT NULL;",
+               "        proacl IS NOT NULL;\n\n",
                "INSERT INTO pg_init_privs "
                "  (objoid, classoid, objsubid, initprivs, privtype)"
                "    SELECT"
@@ -1876,7 +1881,7 @@ setup_privileges(FILE *cmdfd)
                "    FROM"
                "        pg_type"
                "    WHERE"
-               "        typacl IS NOT NULL;",
+               "        typacl IS NOT NULL;\n\n",
                "INSERT INTO pg_init_privs "
                "  (objoid, classoid, objsubid, initprivs, privtype)"
                "    SELECT"
@@ -1888,7 +1893,7 @@ setup_privileges(FILE *cmdfd)
                "    FROM"
                "        pg_language"
                "    WHERE"
-               "        lanacl IS NOT NULL;",
+               "        lanacl IS NOT NULL;\n\n",
                "INSERT INTO pg_init_privs "
                "  (objoid, classoid, objsubid, initprivs, privtype)"
                "    SELECT"
@@ -1901,7 +1906,7 @@ setup_privileges(FILE *cmdfd)
                "    FROM"
                "        pg_largeobject_metadata"
                "    WHERE"
-               "        lomacl IS NOT NULL;",
+               "        lomacl IS NOT NULL;\n\n",
                "INSERT INTO pg_init_privs "
                "  (objoid, classoid, objsubid, initprivs, privtype)"
                "    SELECT"
@@ -1913,7 +1918,7 @@ setup_privileges(FILE *cmdfd)
                "    FROM"
                "        pg_namespace"
                "    WHERE"
-               "        nspacl IS NOT NULL;",
+               "        nspacl IS NOT NULL;\n\n",
                "INSERT INTO pg_init_privs "
                "  (objoid, classoid, objsubid, initprivs, privtype)"
                "    SELECT"
@@ -1926,7 +1931,7 @@ setup_privileges(FILE *cmdfd)
                "    FROM"
                "        pg_foreign_data_wrapper"
                "    WHERE"
-               "        fdwacl IS NOT NULL;",
+               "        fdwacl IS NOT NULL;\n\n",
                "INSERT INTO pg_init_privs "
                "  (objoid, classoid, objsubid, initprivs, privtype)"
                "    SELECT"
@@ -1939,7 +1944,7 @@ setup_privileges(FILE *cmdfd)
                "    FROM"
                "        pg_foreign_server"
                "    WHERE"
-               "        srvacl IS NOT NULL;",
+               "        srvacl IS NOT NULL;\n\n",
                NULL
        };
 
@@ -1994,6 +1999,8 @@ setup_schema(FILE *cmdfd)
                free(*line);
        }
 
+       PG_CMD_PUTS("\n\n");
+
        free(lines);
 
        PG_CMD_PRINTF1("UPDATE information_schema.sql_implementation_info "