docs/published/handbook/engineering/databases/schema-changes.md
PostHog's database schema evolves constantly along with the app. Each schema change requires deliberation though, as a badly designed migration can cause pain for users and require extra effort from the engineering team.
For detailed patterns on writing safe Django migrations, see the Safe Django Migrations guide.
Before making a schema change, consider:
If you're doing anything tricky, make sure you know how the change will work operationally.
Deleting and renaming tables and columns, even completely unused ones, is strongly discouraged.
The reason is that the Django ORM always specifies tables and columns to fetch in its SELECT queries – so when a migration moves a table/column away, in between the migration having ran and the new server having deployed completely, there's a period where the old server is still live and tries to SELECT that column. The only thing it gets from the database though is an error, as the resource isn't there anymore! This situation results in a period of short-lived but very significant pain for users.
To avoid this pain, AVOID deleting/renaming models and fields. Instead:
# DEPRECATED comment in codeget_queryset in a Manager object. See this PR for an example.Migrations must run smoothly in local development, self-hosted instances, and PostHog Cloud. Avoid migrations that process rows individually on large tables (events, persons, person distinct IDs, logs) - they may take forever or lock the entire table.
For a quick overview of Cloud scale, see Vanity Metrics in Metabase.
ClickHouse is at the core of PostHog's scalable analytics capabilities. The ClickHouse schema can be changed just like the Postgres one – with migrations – but there are two important bits of complexity added:
ORDER BY clause of the table. This determines how data is laid out on disk, and ClickHouse reads data in the order it's laid out, so it's important that the sorting key is optimal for the table's use cases.To make sure that your new ClickHouse migration is A-OK – both above points having been addressed – make sure you loop in someone with extensive experience operating ClickHouse for review. Ask for feedback in the #team-clickhouse Slack channel.