# Postgres in CI doesn't need to survive a crash

> A CI test database is thrown away after every job, so it doesn't need crash safety. Turning it off cut our Django test stage from 6.0 to 4.7 minutes.

Every CI test job starts a new, empty Postgres database. When the job ends, CI throws
the database away.

But Postgres doesn't know that. It protects this database like real customer data: when
data is saved for good, Postgres waits until it is safely on the disk. This waiting is
slow.

We told Postgres to stop waiting. Our tests in CI went from 6.0 to 4.7 minutes.

## Why Postgres waits

When you save data, Postgres doesn't just keep it in memory. It writes it to the disk
and waits until the disk says "done". Only then does it tell your code that the save
worked.

This is a good thing in production. If the server suddenly loses power, no saved data
is lost.

Most Django tests don't trigger this wait. Each test runs inside a transaction that is
undone at the end, so its data is never saved for good. The waits come from the work
around the tests:

- loading the test database at the start of the job,
- making a copy of it for each parallel test worker,
- tests that really save their data (`transaction=True`) and then empty the tables
  afterwards.

We didn't measure which of these cost the most.

In CI, it is wasted time. If the CI machine crashes, nobody needs the data. CI deletes
the database and runs the job again.

## The three settings

The Postgres manual has a page for this case
([PostgreSQL 15, Non-Durable Settings](https://www.postgresql.org/docs/15/non-durability.html)).
We turned off three settings from it:

| Setting | What it does when on |
|---|---|
| `fsync` | Makes sure data is really on the disk |
| `synchronous_commit` | Waits for the disk on every save |
| `full_page_writes` | Writes extra copies of data, in case a write stops halfway |

Turn these off only for throwaway databases. With them off, a crash of the machine can
lose or break data.

## How we turned them off

Usually you pass these settings when the Postgres container starts. In GitLab CI, that
is the `command:` line of the Postgres service:

```yaml
services:
  - name: postgres:15-alpine
    command: ["postgres", "-c", "fsync=off", "-c", "synchronous_commit=off", "-c", "full_page_writes=off"]
```

We couldn't rely on this. Our CI jobs run on two kinds of machines, and one of them
(GitLab's AWS Fargate runner) ignores the Postgres service settings in the CI file
([GitLab Runner, Autoscaling on AWS Fargate](https://docs.gitlab.com/runner/configuration/runner_autoscale_aws_fargate/)).

So we change the settings later, from the job's script, after Postgres is already
running:

```bash
psql -c "ALTER SYSTEM SET fsync = off" \
     -c "ALTER SYSTEM SET synchronous_commit = off" \
     -c "ALTER SYSTEM SET full_page_writes = off" \
     -c "SELECT pg_reload_conf()"
echo "Postgres fsync=$(psql -tAc 'SHOW fsync')"
```

What each part does:

- `ALTER SYSTEM SET ...` saves a new value for a setting
  ([PostgreSQL 15, ALTER SYSTEM](https://www.postgresql.org/docs/15/sql-altersystem.html)).
- `SELECT pg_reload_conf()` tells Postgres to use the new values now. No restart is
  needed.
- The last line prints the value to the job log. That way you can check that it worked
  on every machine.

You need an admin database user to run `ALTER SYSTEM`. The official Postgres Docker
image makes its default user an admin, so this works out of the box.

This works on any CI machine, because it doesn't care how Postgres was started.

## How much time it saved

We compared our pipelines from the day before the change with the pipelines from the
ten days after it:

| | Before (12 pipelines) | After (15 pipelines) |
|---|---|---|
| Time from the first test job's start to the last one's end | 6.0 min | 4.7 min |
| Time of all test jobs added together | 74 min | 61 min |
| Test run time on one machine, our own servers | 177 s | 148 s |

All numbers are medians.

Two things to know about these numbers:

- In the same change, we also made tests keep uploaded files in memory instead of on the
  disk. Part of the gain may come from that.
- The gain depends on the machine. Where tests are limited by CPU, not by the disk,
  expect less.

## Going further

You can go one step further: keep the whole Postgres database in memory, so it never
touches the disk. But it needs a special setting on the database container, and not
every CI system lets you set it. The SQL above works anywhere you can connect to the
database.