Drop detached ai_events_counts partitions before the table
What does this MR do and why?
Fixes DropAiEventsCounts from !268857 (merged), reported in !268857 (comment 4263464533).
Partitions that the partition manager detached, but DropDetachedPartitionsWorker hasn't dropped yet, still use ai_events_counts_id_seq for their id default. So DROP TABLE ai_events_counts fails with PG::DependentObjectsStillExist and the upgrade stops.
Detached partitions are kept for a week, and the worker runs once a day. Since 19.5 nothing detaches new ones, but an instance that upgrades through the 19.5 stop to 19.6 or later within a week of a detach, or where the worker didn't run, still has them. That's what happened on the performance test environment in the report: it's stopped between runs.
The migration now does this before dropping the parent:
- Finds
ai_events_counts_*tables ingitlab_partitions_dynamicthat are no longer attached to the parent. - Drops their foreign keys (they keep their own copy of
fk_rails_99a3f4a8f1toorganizations), then the table, each insidewith_lock_retries. - Deletes their
detached_partitionsrows.
A pending detach can't happen for this table: Ai::EventsCount used strategy: :monthly without detach_concurrently, so it never ran DETACH ... CONCURRENTLY.
The fix edits the existing migration instead of adding a new one: on a failed upgrade the broken migration runs first, so a later migration would never get the chance. On GitLab.com the migration has already run and there was nothing left to drop.
Changelog: fixed. The original migration shipped in 19.6.0, so this is a bug fix and gets a backport to 19-6-stable-ee after it merges.
Data deletion
- Records affected: only
ai_events_counts_*partitions that the partition manager already detached andDropDetachedPartitionsWorkerhasn't dropped yet, plus theirdetached_partitionsrows. The worker would delete them within a week anyway. On GitLab.com, none: the migration already ran and nothing was left. The rest of the table is dropped by !268857 (merged), see its data deletion section. - User impact: none. Nothing reads
ai_events_countssince 19.5. - Recovery: not needed, this data was already scheduled for deletion. Rolling back recreates the empty table, as before.
References
Screenshots or screen recordings
Not applicable, no UI change.
How to set up and validate locally
-
Roll back the migration and create a detached partition:
bin/rails db:migrate:down:main VERSION=20261106101405 gdk psql -c "CREATE TABLE gitlab_partitions_dynamic.ai_events_counts_209902 PARTITION OF ai_events_counts FOR VALUES FROM ('2099-02-01') TO ('2099-03-01'); ALTER TABLE ai_events_counts DETACH PARTITION gitlab_partitions_dynamic.ai_events_counts_209902;" -
On
masterthis fails withPG::DependentObjectsStillExist. On this branch it drops the detached partition and then the table:bin/rails db:migrate:up:main VERSION=20261106101405
Database testing
db:gitlabcom-database-testing can't exercise this: the migration already ran on GitLab.com and there were no detached partitions left. The spec covers attached and detached partitions, with and without the foreign key, and keeps the detached_partitions rows of other tables.
Local up on GDK, after rolling the migration back and detaching a partition that keeps its own fk_rails_99a3f4a8f1 and has a detached_partitions row (as in the steps above, plus the FK and the row). Afterwards the table, the partition and the row are gone:
main: == 20261106101405 DropAiEventsCounts: migrating ===============================
main: -- select_all("SELECT c.oid, n.nspname AS schema, c.relname AS name\nFROM pg_class c\nJOIN pg_namespace n ON n.oid = c.relnamespace\nWHERE n.nspname = 'gitlab_partitions_dynamic'\n AND c.relkind = 'r'\n AND c.relname LIKE 'ai\\_events\\_counts\\_%'\n AND NOT EXISTS (SELECT 1 FROM pg_inherits i WHERE i.inhrelid = c.oid)\n")
main: -> 0.0315s
main: -- select_values("SELECT conname FROM pg_constraint WHERE conrelid = 13704513 AND contype = 'f'\n")
main: -> 0.0009s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- quote_table_name("gitlab_partitions_dynamic.ai_events_counts_209902")
main: -> 0.0000s
main: -- quote_column_name("fk_rails_99a3f4a8f1")
main: -> 0.0000s
main: -- execute("ALTER TABLE IF EXISTS \"gitlab_partitions_dynamic\".\"ai_events_counts_209902\" DROP CONSTRAINT IF EXISTS \"fk_rails_99a3f4a8f1\"")
main: -> 0.0008s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- drop_table("gitlab_partitions_dynamic.ai_events_counts_209902", {if_exists: true})
main: -> 0.0008s
main: -- quote("ai_events_counts_209902")
main: -> 0.0000s
main: -- execute("DELETE FROM detached_partitions WHERE table_name = 'ai_events_counts_209902'")
main: -> 0.0005s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- drop_table(:ai_events_counts, {if_exists: true})
main: -> 0.0004s
main: == 20261106101405 DropAiEventsCounts: migrated (0.0504s) ======================MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.