Improve initdb's query for generating default descriptions a little.

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.
This commit is contained in:
Tom Lane 2018-05-08 15:59:01 -04:00
parent 831f5d11ec
commit dec10340d5
1 changed files with 21 additions and 14 deletions

View File

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