Unverified Commit 3b5e1ef5 authored by Leona Maroni's avatar Leona Maroni Committed by GitHub
Browse files

nixos/postgresql: implement auto-restart & rework dependencies of postgresql.target (#424625)

parents a74b4f51 6ae194e4
Loading
Loading
Loading
Loading
+24 −4
Original line number Diff line number Diff line
@@ -769,7 +769,7 @@ in
    systemd.targets.postgresql = {
      description = "PostgreSQL";
      wantedBy = [ "multi-user.target" ];
      bindsTo = [
      requires = [
        "postgresql.service"
        "postgresql-setup.service"
      ];
@@ -780,8 +780,13 @@ in

      after = [ "network.target" ];

      # To trigger the .target also on "systemctl start postgresql".
      bindsTo = [ "postgresql.target" ];
      # To trigger the .target also on "systemctl start postgresql" as well as on
      # restarts & stops.
      # Please note that postgresql.service & postgresql.target binding to
      # each other makes the Restart=always rule racy and results
      # in sometimes the service not being restarted.
      wants = [ "postgresql.target" ];
      partOf = [ "postgresql.target" ];

      environment.PGDATA = cfg.dataDir;

@@ -821,6 +826,8 @@ in

          ExecStart = "${cfg.finalPackage}/bin/postgres";

          Restart = "always";

          # Hardening
          CapabilityBoundingSet = [ "" ];
          DevicePolicy = "closed";
@@ -872,7 +879,20 @@ in
        })
      ];

      unitConfig.RequiresMountsFor = "${cfg.dataDir}";
      unitConfig =
        let
          inherit (config.systemd.services.postgresql.serviceConfig) TimeoutSec;
          maxTries = 5;
          bufferSec = 5;
        in
        {
          RequiresMountsFor = "${cfg.dataDir}";

          # The max. time needed to perform `maxTries` start attempts of systemd
          # plus a bit of buffer time (bufferSec) on top.
          StartLimitIntervalSec = TimeoutSec * maxTries + bufferSec;
          StartLimitBurst = maxTries;
        };
    };

    systemd.services.postgresql-setup = {
+6 −0
Original line number Diff line number Diff line
@@ -101,6 +101,12 @@ let
          machine.fail(check_count("SELECT * FROM sth;", 4))
          machine.succeed(check_count("SELECT xpath('/test/text()', doc) FROM xmltest;", 1))

          with subtest("killing postgres process should trigger an automatic restart"):
              machine.succeed("systemctl kill -s KILL postgresql")

              machine.wait_until_succeeds("systemctl is-active postgresql.service")
              machine.wait_until_succeeds("systemctl is-active postgresql.target")

          with subtest("Backup service works"):
              machine.succeed(
                  "systemctl start ${backupService}.service",