Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(migrations): retry product-database migrations and give them a lock_timeout (#90213)

posthog[bot]b667199e7dc0
diff --git a/bin/migrate b/bin/migrate
index f75ba7ad02ce..ee8b72c7995f 100755
--- a/bin/migrate
+++ b/bin/migrate
@@ -113,8 +113,10 @@ if [ "$RUN_POSTGRES" = "1" ]; then
while [ $MIGRATE_ATTEMPT -le $MIGRATE_MAX_RETRIES ]; do
echo "[migrate] Django migration attempt $MIGRATE_ATTEMPT/$MIGRATE_MAX_RETRIES"
-
- if $MIGRATE_CMD; then
+
+ # Product databases (products/db_routing.yaml) lose lock races the same way, so they
+ # share the retry. A retry re-runs the main migrate too, which is a no-op by then.
+ if $MIGRATE_CMD && python manage.py migrate_product_databases; then
MIGRATE_SUCCESS=true
break
fi
@@ -146,18 +148,7 @@ if [ "$RUN_POSTGRES" = "1" ]; then
[ -n "$CLICKHOUSE_STATUS" ] && rm -f $CLICKHOUSE_STATUS
exit 1
fi
-
-fi
-# Product database migrations via Django.
-# When product routes are configured, run each routed app's migrations against
-# its target writer database alias.
-if run_scope "postgres"; then
- python manage.py migrate_product_databases
- if [ $? -ne 0 ]; then
- echo "Error in migrate_product_databases, exiting."
- exit 1
- fi
fi
# Persons migrations via Django management command (connects to PERSONS_DB_WRITER_URL).
diff --git a/posthog/settings/data_stores.py b/posthog/settings/data_stores.py
index 187f55a9a008..d78c4a229fd7 100644
--- a/posthog/settings/data_stores.py
+++ b/posthog/settings/data_stores.py
@@ -128,6 +128,11 @@ if read_host:
DATABASES["replica"] = postgres_config(read_host)
DATABASE_ROUTERS.append("posthog.dbrouter.ReplicaRouter")
+# lock_timeout for every direct (migration) connection, main and product, so a migration that
+# loses a lock race fails fast and bin/migrate retries it, instead of queueing all later
+# queries on the table behind it.
+_migration_lock_timeout_option = f"-c lock_timeout={os.getenv('MIGRATE_LOCK_TIMEOUT', '20000')}"
+
# Configure a direct database connection bypassing PgBouncer.
# This allows using PGOPTIONS like lock_timeout which PgBouncer doesn't support.
# Used for migrations: python manage.py migrate --database=default_direct
@@ -140,9 +145,7 @@ if direct_host:
DATABASES["default_direct"]["PORT"] = os.getenv("POSTHOG_POSTGRES_DIRECT_PORT", "5432")
# Disable server-side cursors is not needed for direct connection
DATABASES["default_direct"]["DISABLE_SERVER_SIDE_CURSORS"] = False
- # Set lock_timeout for migrations to fail fast on lock contention
- lock_timeout_ms = os.getenv("MIGRATE_LOCK_TIMEOUT", "20000")
- DATABASES["default_direct"]["OPTIONS"] = {"options": f"-c lock_timeout={lock_timeout_ms}"}
+ DATABASES["default_direct"]["OPTIONS"] = {"options": _migration_lock_timeout_option}
# The persons database is not a Django connection. Person/group/cohort data lives behind
# the personhog service and is reached through the personhog client or off-Django psycopg
@@ -254,6 +257,7 @@ for route in product_routes:
direct_alias = f"{db}_db_direct"
DATABASES[direct_alias] = dict(dj_database_url.parse(direct_url, conn_max_age=0))
DATABASES[direct_alias].setdefault("OPTIONS", {})["connect_timeout"] = 10
+ DATABASES[direct_alias]["OPTIONS"]["options"] = _migration_lock_timeout_option
_apply_product_db_ssl_options(db, DATABASES[direct_alias]["OPTIONS"])
if DISABLE_SERVER_SIDE_CURSORS:
DATABASES[direct_alias]["DISABLE_SERVER_SIDE_CURSORS"] = True