From 42784a9053b801c3c62e97dbf18b66043a0ff5af Mon Sep 17 00:00:00 2001 From: Antoine Eiche Date: Thu, 20 Jun 2019 14:41:44 +0200 Subject: [PATCH 1/2] sql: refactor some sql statements to lowercase --- src/sql/hydra.sql | 4 ++-- src/sql/upgrade-57.sql | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sql/hydra.sql b/src/sql/hydra.sql index a39d9aac..869175d6 100644 --- a/src/sql/hydra.sql +++ b/src/sql/hydra.sql @@ -691,6 +691,6 @@ create index IndexBuildsOnNotificationPendingSince on Builds(notificationPending #ifdef POSTGRESQL -- Provide an index used by LIKE operator on builds.drvpath (search query) -CREATE EXTENSION pg_trgm; -CREATE INDEX IndexTrgmBuildsOnDrvpath ON builds USING gin (drvpath gin_trgm_ops); +create extension pg_trgm; +create index IndexTrgmBuildsOnDrvpath on builds using gin (drvpath gin_trgm_ops); #endif diff --git a/src/sql/upgrade-57.sql b/src/sql/upgrade-57.sql index f950a6e0..04826ab0 100644 --- a/src/sql/upgrade-57.sql +++ b/src/sql/upgrade-57.sql @@ -1,2 +1,2 @@ -CREATE EXTENSION pg_trgm; -CREATE INDEX IndexTrgmBuildsOnDrvpath ON builds USING gin (drvpath gin_trgm_ops); +create extension pg_trgm; +create index IndexTrgmBuildsOnDrvpath on builds using gin (drvpath gin_trgm_ops); From 8a0a5ec3a3200d4f4d4d38f87d0afdb49f092b39 Mon Sep 17 00:00:00 2001 From: Antoine Eiche Date: Thu, 20 Jun 2019 14:53:43 +0200 Subject: [PATCH 2/2] Create extension pg_trgm in the NixOS module The creation of the `pg_trgm` extension needs superuser power. So, this patch makes the extension creation in the Hydra NixOS module when a local database is used. If it is not possible to create this extension (remote database for instance with nosuperuser), the creation of the `pg_trgm` index is skipped (this index speedup queries on builds.drvpath) and warnings are emitted: initialising the Hydra database schema... WARNING: Can not create extension pg_trgm: permission denied to create extension "pg_trgm" WARNING: HINT: Temporary provide superuser role to your Hydra Postgresql user and run the script src/sql/upgrade-57.sql WARNING: The pg_trgm index on builds.drvpath has been skipped (slower complex queries on builds.drvpath) This allows to keep smooth migrations: the migration process doesn't require a manual step (but this manual step is recommended on big remote databases). --- hydra-module.nix | 3 +++ src/sql/hydra.sql | 19 ++++++++++++++++--- src/sql/upgrade-57.sql | 18 ++++++++++++++++-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/hydra-module.nix b/hydra-module.nix index ce5dc266..52eccd40 100644 --- a/hydra-module.nix +++ b/hydra-module.nix @@ -273,6 +273,7 @@ in runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/createdb -O hydra hydra touch ${baseDir}/.db-created fi + echo "create extension if not exists pg_trgm" | runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/psql hydra ''} if [ ! -e ${cfg.gcRootsDir} ]; then @@ -415,6 +416,8 @@ in hydra-users hydra-queue-runner hydra hydra-users hydra-www hydra hydra-users root hydra + # The postgres user is used to create the pg_trgm extension for the hydra database + hydra-users postgres postgres ''; services.postgresql.authentication = optionalString haveLocalDB diff --git a/src/sql/hydra.sql b/src/sql/hydra.sql index 869175d6..0c769a7e 100644 --- a/src/sql/hydra.sql +++ b/src/sql/hydra.sql @@ -690,7 +690,20 @@ create index IndexJobsetEvalsOnJobsetId on JobsetEvals(project, jobset, id desc) create index IndexBuildsOnNotificationPendingSince on Builds(notificationPendingSince) where notificationPendingSince is not null; #ifdef POSTGRESQL --- Provide an index used by LIKE operator on builds.drvpath (search query) -create extension pg_trgm; -create index IndexTrgmBuildsOnDrvpath on builds using gin (drvpath gin_trgm_ops); +-- The pg_trgm extension has to be created by a superuser. The NixOS +-- module creates this extension in the systemd prestart script. We +-- then ensure the extension has been created before creating the +-- index. If it is not possible to create the extension, a warning +-- message is emitted to inform the user the index creation is skipped +-- (slower complex queries on builds.drvpath). +do $$ +begin + create extension if not exists pg_trgm; + -- Provide an index used by LIKE operator on builds.drvpath (search query) + create index IndexTrgmBuildsOnDrvpath on builds using gin (drvpath gin_trgm_ops); +exception when others then + raise warning 'Can not create extension pg_trgm: %', SQLERRM; + raise warning 'HINT: Temporary provide superuser role to your Hydra Postgresql user and run the script src/sql/upgrade-57.sql'; + raise warning 'The pg_trgm index on builds.drvpath has been skipped (slower complex queries on builds.drvpath)'; +end$$; #endif diff --git a/src/sql/upgrade-57.sql b/src/sql/upgrade-57.sql index 04826ab0..cae873d4 100644 --- a/src/sql/upgrade-57.sql +++ b/src/sql/upgrade-57.sql @@ -1,2 +1,16 @@ -create extension pg_trgm; -create index IndexTrgmBuildsOnDrvpath on builds using gin (drvpath gin_trgm_ops); +-- The pg_trgm extension has to be created by a superuser. The NixOS +-- module creates this extension in the systemd prestart script. We +-- then ensure the extension has been created before creating the +-- index. If it is not possible to create the extension, a warning +-- message is emitted to inform the user the index creation is skipped +-- (slower complex queries on builds.drvpath). +do $$ +begin + create extension if not exists pg_trgm; + -- Provide an index used by LIKE operator on builds.drvpath (search query) + create index IndexTrgmBuildsOnDrvpath on builds using gin (drvpath gin_trgm_ops); +exception when others then + raise warning 'Can not create extension pg_trgm: %', SQLERRM; + raise warning 'HINT: Temporary provide superuser role to your Hydra Postgresql user and run the script src/sql/upgrade-57.sql'; + raise warning 'The pg_trgm index on builds.drvpath has been skipped (slower complex queries on builds.drvpath)'; +end$$;