Skip to content

Commit 5e902aa

Browse files
Merge pull request #248 from NuschtOS/pg_repack
postgres: add pgRepackTimer option
2 parents a361718 + aff281c commit 5e902aa

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

modules/postgres.nix

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ in
3636
});
3737
};
3838

39+
pgRepackTimer = {
40+
enable = libS.mkOpinionatedOption "install pg_repack and configure a systemd timer to run it periodically on all DBs";
41+
42+
timerConfig = lib.mkOption {
43+
type = lib.types.nullOr (lib.types.attrsOf utils.systemdUtils.unitOptions.unitOption);
44+
default = {
45+
OnCalendar = "02:00";
46+
Persistent = true;
47+
RandomizedDelaySec = "10m";
48+
};
49+
example = {
50+
OnCalendar = "06:00";
51+
Persistent = true;
52+
RandomizedDelaySec = "5h";
53+
};
54+
description = ''
55+
When to run the VACUUM ANALYZE.
56+
See {manpage}`systemd.timer(5)` for details.
57+
'';
58+
};
59+
};
60+
3961
preloadAllExtensions = libS.mkOpinionatedOption "load all installed extensions through `shared_preload_libraries`";
4062

4163
recommendedDefaults = libS.mkOpinionatedOption "set recommended default settings";
@@ -93,14 +115,14 @@ in
93115
};
94116

95117
vacuumAnalyzeTimer = {
96-
enable = libS.mkOpinionatedOption "timer to run VACUUM ANALYZE on all DBs";
118+
enable = libS.mkOpinionatedOption "configure a systemd timer to run `VACUUM ANALYZE` periodically on all DBs";
97119

98120
timerConfig = lib.mkOption {
99121
type = lib.types.nullOr (lib.types.attrsOf utils.systemdUtils.unitOptions.unitOption);
100122
default = {
101123
OnCalendar = "03:00";
102124
Persistent = true;
103-
RandomizedDelaySec = "30m";
125+
RandomizedDelaySec = "10m";
104126
};
105127
example = {
106128
OnCalendar = "06:00";
@@ -194,6 +216,7 @@ in
194216
postgresql = {
195217
databases = [ "postgres" ] ++ config.services.postgresql.ensureDatabases;
196218
enableJIT = lib.mkIf cfg.recommendedDefaults true;
219+
extensions = lib.mkIf cfg.pgRepackTimer.enable (ps: with ps; [ pg_repack ]);
197220
settings.shared_preload_libraries =
198221
lib.optional cfg.configurePgStatStatements "pg_stat_statements"
199222
# TODO: upstream, this probably requires a new entry in passthru to pick if the object name doesn't match the plugin name or there are multiple
@@ -210,8 +233,10 @@ in
210233
name = lib.getName so;
211234
in {
212235
postgis = "postgis-3";
236+
# withJIT installs the postgres' jit output as an extension but that is no shared object to load
237+
postgresql = null;
213238
}.${name} or name;
214-
in lib.optionals cfg.preloadAllExtensions (map getSoOrFallback finalPackage.installedExtensions));
239+
in lib.optionals cfg.preloadAllExtensions (lib.filter (x: x != null) (map getSoOrFallback finalPackage.installedExtensions)));
215240
upgrade.stopServices = with config.services; lib.mkMerge [
216241
(lib.mkIf (atuin.enable && atuin.database.createLocally) [ "atuin" ])
217242
(lib.mkIf (gancio.enable && gancio.settings.db.dialect == "postgres") [ "gancio" ])
@@ -305,10 +330,19 @@ in
305330
stopIfChanged = lib.mkIf cfg.recommendedDefaults false;
306331
};
307332

333+
postgresql-pg-repack = lib.mkIf cfg.vacuumAnalyzeTimer.enable {
334+
description = "Repack all PostgreSQL databases";
335+
after = [ "postgresql.service" ];
336+
serviceConfig = {
337+
ExecStart = "${lib.getExe cfg.package.pkgs.pg_repack} --port=${builtins.toString cfg.settings.port} --all";
338+
User = "postgres";
339+
};
340+
wantedBy = [ "timers.target" ];
341+
};
342+
308343
postgresql-vacuum-analyze = lib.mkIf cfg.vacuumAnalyzeTimer.enable {
309344
description = "Vacuum and analyze all PostgreSQL databases";
310345
after = [ "postgresql.service" ];
311-
requires = [ "postgresql.service" ];
312346
serviceConfig = {
313347
ExecStart = "${lib.getExe' cfg.package "psql"} --port=${builtins.toString cfg.settings.port} -tAc 'VACUUM ANALYZE'";
314348
User = "postgres";
@@ -317,9 +351,15 @@ in
317351
};
318352
};
319353

320-
timers.postgresql-vacuum-analyze = lib.mkIf cfg.vacuumAnalyzeTimer.enable {
321-
inherit (cfg.vacuumAnalyzeTimer) timerConfig;
322-
wantedBy = [ "timers.target" ];
354+
timers = {
355+
postgresql-pg-repack = lib.mkIf cfg.pgRepackTimer.enable {
356+
inherit (cfg.vacuumAnalyzeTimer) timerConfig;
357+
wantedBy = [ "timers.target" ];
358+
};
359+
postgresql-vacuum-analyze = lib.mkIf cfg.vacuumAnalyzeTimer.enable {
360+
inherit (cfg.vacuumAnalyzeTimer) timerConfig;
361+
wantedBy = [ "timers.target" ];
362+
};
323363
};
324364
};
325365
};

0 commit comments

Comments
 (0)