Skip to content

chore(tasks): drop the redundant team_id index on sandbox environments - #95866

Draft
posthog[bot] wants to merge 1 commit into
masterfrom
posthog-self-driving/choretasks-drop-the-redundant-sandbox-74f870
Draft

chore(tasks): drop the redundant team_id index on sandbox environments#95866
posthog[bot] wants to merge 1 commit into
masterfrom
posthog-self-driving/choretasks-drop-the-redundant-sandbox-74f870

Conversation

@posthog

@posthog posthog Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Problem

  • Nobody is hurt yet. This is schema hygiene on posthog_sandbox_environment, one row per user-defined sandbox environment per team.
  • pganalyze flagged a team_id index on the table that no query has scanned in 35 days.
  • The index is redundant, not merely cold: the Meta index on (team, created_by) leads with team_id, so it already serves every team_id lookup and the cascade check when a team row is deleted.
  • Cost today is small but permanent: storage plus index maintenance on every insert, update, and delete.

Changes

  • Nothing changes for a person using the tasks product. Query plans keep the composite index for the same lookups.
  • Mechanism: db_index=False on the team foreign key stops Django from declaring the single-column index, and migration 0118 drops it with DROP INDEX CONCURRENTLY, so no table lock.
  • The migration wraps the AlterField in SeparateDatabaseAndState, because the concurrent drop cannot run inside the migration transaction.
Index Before After
posthog_sandbox_environment_team_id_d94b6a9a (team_id) present, 0 scans dropped
posthog_san_team_id_817c0d_idx (team_id, created_by_id) present kept, serves both cases
posthog_sandbox_environment_custom_image_id_a62b5702 present kept

custom_image_id stays. It also reports no scans, but it serves the on_delete=SET_NULL cascade, and custom images are archived rather than deleted, so the index only looks idle.

How did you test this code?

  • No new tests. A dropped redundant index has no behavior to assert that the existing model and API tests do not already cover.
  • makemigrations tasks --dry-run reports no state drift.
  • sqlmigrate tasks 0118 emits exactly DROP INDEX CONCURRENTLY IF EXISTS "posthog_sandbox_environment_team_id_d94b6a9a".
  • analyze_migration_risk classifies the migration as Safe.
  • Applied forward and backward against a local dev Postgres and read pg_indexes each time: forward removes the index, backward recreates the same definition.
  • Not checked: the product test suites. The sandbox had no ClickHouse or Redis, so products/tasks/backend/tests could not start. CI runs them.

Automatic notifications

  • Publish to changelog?

Docs update

None. No user-facing behavior, API, or documented workflow changes.

🤖 Agent context

Autonomy: Fully autonomous

  • Written by Claude Code from a PostHog inbox report. Skills invoked: /django-migrations, /writing-pr-descriptions, /writing-code-comments, /writing-simplified-technical-english.
  • No duplicate: gh pr list --state open --search found no open PR touching this table. Several merged PRs do the same cleanup on other tables.
  • The first draft used a plain RunSQL drop, copying an older merged migration. It now uses the DropIndexConcurrently helper instead, which is idempotent under bin/migrate retries and can rebuild the index on reverse.
  • Public artifact: the diff and this description carry only schema facts from this repository and index names read from a local dev database.

Created with PostHog Desktop from this inbox report.

The (team, created_by) index on posthog_sandbox_environment leads with
team_id, so the auto-created single-column FK index serves no query and
no cascade check. Set db_index=False on the team FK and drop the index
concurrently.

Generated-By: PostHog Desktop
Task-Id: 2fbb6166-26d3-4e71-9454-8fe4b64bc4cb
@posthog posthog Bot added the skip-agent-review Save $$$, skip auto agent reviews (Greptile) — use for trivial or chore PRs label Sep 7, 2026
@trunk-io

trunk-io Bot commented Sep 7, 2026

Copy link
Copy Markdown

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@posthog

posthog Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

🦔 PostHog Review reviewed this pull request

Nothing worth raising this time, so here's a calming picture instead:

A panda relaxing and waving

@github-actions github-actions Bot added the feature/desktop Feature Tag: Desktop label Sep 7, 2026
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

🤖 CI report

⚠️ Trunk lane — backend Python lane

This PR is assigned to the backend Python lane. It runs backend Python tests and may merge in parallel with PRs in other lanes.

⚠️ Django migration SQL — 1 new migration to review

We've detected new migrations on this PR. Review the SQL output for each migration:

products/tasks/backend/migrations/0118_drop_sandboxenvironment_team_id_idx.py

--
-- Custom state/database change combination
--
SET lock_timeout = 0;
SET statement_timeout = 0;
DROP INDEX CONCURRENTLY IF EXISTS "posthog_sandbox_environment_team_id_d94b6a9a";

Last updated: 2026-09-07 05:53 UTC (807767b)

Django migration risk — migration analysis complete

We've analyzed your migrations for potential risks.

Summary: 1 Safe | 0 Needs Review | 0 Blocked

✅ Safe

Brief or no lock, backwards compatible

tasks.0118_drop_sandboxenvironment_team_id_idx
  └─ #1 ✅ SeparateDatabaseAndState
     Wrapper operation - see nested operations for risk: DropIndexConcurrently
     database_operations: DropIndexConcurrently
     └─ #2 ✅ DropIndexConcurrently
        PostHog concurrent-index helper: idempotent (timeout disabling + invalid-leftover recovery)
        model: None, index: posthog_sandbox_environment_team_id_d94b6a9a

Last updated: 2026-09-07 05:53 UTC (807767b)

@stamphog stamphog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approved yet — waiting on the conditions below.

The Migration risk check has not finished for this commit, so stamphog cannot tell a safe migration from a risky one yet. The review runs again on the next push, or you can re-request it once the check reports.

Gate mechanics and policy version
Gate Result
prerequisites all clear
deny-list matches: migrations
size 39L, 2F substantive, 41L/3F incl. docs/generated/snapshots — within ceiling
tier classified as T2-never: T2-never (41L, 3F, single-area, chore)
stamphog 2.0.0b4 .stamphog/policy.yml @ 807767b · reviewed head 807767b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature/desktop Feature Tag: Desktop skip-agent-review Save $$$, skip auto agent reviews (Greptile) — use for trivial or chore PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants