From be377dd55f8bcb9ed1f8eecea54b22b87fa405df Mon Sep 17 00:00:00 2001 From: Ritik Jain Date: Mon, 31 Aug 2026 11:33:09 +0530 Subject: [PATCH] chore(cohorts): drop the superseded profile-keyed summary MVs Migration 20 replaced profile_event_summary_mv and profile_event_property_summary_mv with pairs keyed for the queries that read them, migration 21 filled those with history, and cohort.service has read the new tables since #458. The old pair was left in place so the change stayed revertible by pointer while the new tables were verified. Nothing reads them now, so they are two MV triggers firing on every event insert with no consumer. Drop them. delete.service stops naming them, which it has to: an ALTER ... DELETE against a dropped table is UNKNOWN_TABLE, so leaving those entries would break project deletion. Their TABLE_NAMES entries stay, annotated, because migrations 13, 14 and 15 still reference them. Clustered installs carry two objects per MV, `` Distributed and `_replicated` the view. The Distributed table is dropped first so nothing can route a read at a view mid-drop, and dropping the view takes its implicit `.inner_id.` storage with it. Verified on standalone 26.1.3 and a keeper-backed 2-shard cluster 25.3, each built from an empty database through migration 21 first: both MVs and their inner tables are gone on every node, cohort reads still resolve from either node, ingestion continues, a second run is a no-op, and every statement delete.service now emits is accepted against the post-drop schema. Co-Authored-By: Claude Opus 5 --- .../23-drop-old-cohort-summary-mvs.ts | 48 +++++++++++++++++++ packages/db/src/clickhouse/client.ts | 2 + packages/db/src/services/delete.service.ts | 2 - 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 packages/db/code-migrations/23-drop-old-cohort-summary-mvs.ts diff --git a/packages/db/code-migrations/23-drop-old-cohort-summary-mvs.ts b/packages/db/code-migrations/23-drop-old-cohort-summary-mvs.ts new file mode 100644 index 000000000..ea60ec014 --- /dev/null +++ b/packages/db/code-migrations/23-drop-old-cohort-summary-mvs.ts @@ -0,0 +1,48 @@ +import { TABLE_NAMES } from '../src/clickhouse/client'; +import { + dropTable, + runClickhouseMigrationCommands, +} from '../src/clickhouse/migration'; +import { getIsCluster } from './helpers'; + +/** + * Drop the profile-keyed cohort summary MVs superseded by migration 20. + * + * - profile_event_summary_mv (created in migration 13) + * - profile_event_property_summary_mv (created in migration 14) + * + * Migration 20 created replacements keyed for the queries that read them, + * migration 21 filled them with history, and cohort.service has read the new + * tables since. The old pair kept receiving inserts so the change stayed + * revertible by pointer while the new tables were verified. Nothing reads + * them now, so they are two MV triggers firing on every event insert for no + * consumer. + * + * Clustered installs have two objects per MV: `` is the Distributed + * table and `_replicated` is the materialized view itself. The + * Distributed table goes first so nothing can route a read at a view that is + * mid-drop. Dropping the view takes its implicit `.inner_id.` storage + * with it, so there is no third name to clean up. + * + * Deliberately one-way. The aggregated history goes with the tables, and + * rerunning migrations 13 and 14 would only bring back empty structure, so a + * down() would be a rollback in name only. If you need them back, those two + * migrations hold the definitions and migration 15 the backfill. + */ + +const SUPERSEDED_MVS = [ + TABLE_NAMES.profile_event_summary_mv, + TABLE_NAMES.profile_event_property_summary_mv, +]; + +export async function up() { + const isClustered = getIsCluster(); + + const sqls = SUPERSEDED_MVS.flatMap((name) => + isClustered + ? [dropTable(name, true), dropTable(`${name}_replicated`, true)] + : [dropTable(name, false)], + ); + + await runClickhouseMigrationCommands(sqls); +} diff --git a/packages/db/src/clickhouse/client.ts b/packages/db/src/clickhouse/client.ts index d78a5220c..783830de7 100644 --- a/packages/db/src/clickhouse/client.ts +++ b/packages/db/src/clickhouse/client.ts @@ -66,6 +66,8 @@ export const TABLE_NAMES = { groups: 'groups', cohort_members: 'cohort_members', cohort_metadata: 'cohort_metadata', + // Superseded by the two event_*_summary_mv entries above and dropped in + // migration 23. Kept because migrations 13, 14 and 15 still name them. profile_event_summary_mv: 'profile_event_summary_mv', // Same content as the two MVs above, keyed for the cohort criteria that // read them (event + window first, profile last) rather than by profile. diff --git a/packages/db/src/services/delete.service.ts b/packages/db/src/services/delete.service.ts index 7dace2640..680898a8f 100644 --- a/packages/db/src/services/delete.service.ts +++ b/packages/db/src/services/delete.service.ts @@ -49,8 +49,6 @@ export async function deleteFromClickhouse(projectIds: string[]) { TABLE_NAMES.event_property_values_mv, TABLE_NAMES.cohort_members, TABLE_NAMES.cohort_metadata, - TABLE_NAMES.profile_event_summary_mv, - TABLE_NAMES.profile_event_property_summary_mv, TABLE_NAMES.event_profile_summary_mv, TABLE_NAMES.event_property_profile_summary_mv, ];