From dec10340d5ba63cf338dea256ac0fa66c649bf9e Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 8 May 2018 15:59:01 -0400 Subject: [PATCH] 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. --- src/bin/initdb/initdb.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index b39115c346..ae22e7d9fb 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -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 "