Skip to content

Instantly share code, notes, and snippets.

@jarshwah
Created May 14, 2024 11:07
Show Gist options
  • Save jarshwah/2b49fdd4313541c20deb1152326e6c0d to your computer and use it in GitHub Desktop.
Save jarshwah/2b49fdd4313541c20deb1152326e6c0d to your computer and use it in GitHub Desktop.
Use postgres unlogged tables when testing

In theory this should speed up tests running on Postgres as no WAL will be written, at the cost of completely hosing the test database if the instance crashes (which shouldn't be a problem with a test database).

When comparing builds with and without this patch applied though I had really inconsistent findings. Some builds ran much faster, but most ran much slower.

We already start postgres with the following CLI options which minimises WAL anyway, which likely renders the UNLOGGED change mostly redundant:

-c max_wal_senders=0
-c wal_level=minimal
-c min_wal_size=1024
-c fsync=off
-c full_page_writes=off
# /tests/postgres_unlogged/base.py
from django.db.backends.postgresql.base import * # noqa: F403
from django.db.backends.postgresql.base import DatabaseWrapper as BaseDatabaseWrapper
from .schema import DatabaseSchemaEditor
class DatabaseWrapper(BaseDatabaseWrapper): # type: ignore [no-redef]
SchemaEditorClass = DatabaseSchemaEditor
# /tests/postgres_unlogged/schema.py
from django.db.backends.postgresql.schema import DatabaseSchemaEditor as BaseDatabaseSchemaEditor
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
sql_create_table = BaseDatabaseSchemaEditor.sql_create_table.replace(
"CREATE TABLE ", "CREATE UNLOGGED TABLE "
)
# /tests/settings.py
DATABASES = {
"default": {
"ENGINE": "tests.postgres_unlogged",
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment