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.

perf(tasks): lower the autovacuum trigger on posthog_task_run (#93233)

posthog[bot]d1d8ccae6593
diff --git a/products/tasks/backend/migrations/0114_taskrun_autovacuum_scale_factor.py b/products/tasks/backend/migrations/0114_taskrun_autovacuum_scale_factor.py
new file mode 100644
index 000000000000..a0cb31d90cb4
--- /dev/null
+++ b/products/tasks/backend/migrations/0114_taskrun_autovacuum_scale_factor.py
@@ -0,0 +1,37 @@
+from django.db import migrations
+
+# Every task run rewrites the wide `state` and `output` JSONB columns many times through the
+# locked read-modify-write helpers, and `updated_at` is both indexed and `auto_now`, so no update
+# can take the heap-only tuple path: each one writes a new heap tuple plus entries in all the
+# indexes. Nothing hard-deletes rows either, so the dead tuples only accumulate.
+# The global scale factor of 0.1 makes autovacuum wait for 10% dead tuples on a table with that
+# churn, which is a large steady-state bloat load on the index scans behind the run list and the
+# PR webhook lookups. 0.02 keeps those scans on live tuples, and the matching analyze factor keeps
+# the planner statistics current for the partial indexes.
+#
+# SET (...) takes SHARE UPDATE EXCLUSIVE, which does not conflict with the run writes.
+TABLE = "posthog_task_run"
+SCALE_FACTOR = 0.02
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("tasks", "0113_task_team_live_list_indexes"),
+ ]
+
+ operations = [
+ migrations.RunSQL(
+ sql=[
+ # Fail fast rather than queue: this waits behind an in-progress autovacuum
+ # on the same table, and a retry is cheaper than holding the lock request.
+ "SET LOCAL lock_timeout = '5s'",
+ f"ALTER TABLE {TABLE} SET ("
+ f"autovacuum_vacuum_scale_factor = {SCALE_FACTOR}, "
+ f"autovacuum_analyze_scale_factor = {SCALE_FACTOR})",
+ ],
+ reverse_sql=[
+ "SET LOCAL lock_timeout = '5s'",
+ f"ALTER TABLE {TABLE} RESET (autovacuum_vacuum_scale_factor, autovacuum_analyze_scale_factor)",
+ ],
+ ),
+ ]
diff --git a/products/tasks/backend/migrations/max_migration.txt b/products/tasks/backend/migrations/max_migration.txt
index aef20cb90776..675d75f7fb67 100644
--- a/products/tasks/backend/migrations/max_migration.txt
+++ b/products/tasks/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0113_task_team_live_list_indexes
+0114_taskrun_autovacuum_scale_factor