From 842c180980caf2fd7fb16db9e608fb097b90590b Mon Sep 17 00:00:00 2001 From: autocommit Date: Wed, 10 Jun 2026 18:18:54 -0700 Subject: [PATCH] =?UTF-8?q?perf(processor):=20=E2=9A=A1=20Add=20pre-dedupl?= =?UTF-8?q?ication=20logic=20to=20prevent=20duplicate=20aggregated=20metri?= =?UTF-8?q?cs=20data=20before=20index=20creation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Lilith Autocommit --- services/processor/src/schema-guard.service.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/services/processor/src/schema-guard.service.ts b/services/processor/src/schema-guard.service.ts index b560d9c..d0f431d 100644 --- a/services/processor/src/schema-guard.service.ts +++ b/services/processor/src/schema-guard.service.ts @@ -28,6 +28,17 @@ export class SchemaGuardService implements OnModuleInit { constructor(@InjectDataSource() private readonly dataSource: DataSource) {} async onModuleInit(): Promise { + // Pre-dedupe: legacy rows without the index can block CREATE UNIQUE INDEX. + await this.dataSource.query(` + DELETE FROM aggregated_metrics a + USING aggregated_metrics b + WHERE a.ctid < b.ctid + AND a."metricType" = b."metricType" + AND a."granularity" = b."granularity" + AND a."timestamp" = b."timestamp" + AND a."dimension" IS NOT DISTINCT FROM b."dimension" + AND a."dimensionValue" IS NOT DISTINCT FROM b."dimensionValue" + `); await this.dataSource.query(` CREATE UNIQUE INDEX IF NOT EXISTS uq_aggregated_metrics_dedup ON aggregated_metrics ("metricType", "granularity", "timestamp", "dimension", "dimensionValue")