perf(processor): ⚡ Add pre-deduplication logic to prevent duplicate aggregated metrics data before index creation
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
parent
19feedfb66
commit
842c180980
1 changed files with 11 additions and 0 deletions
|
|
@ -28,6 +28,17 @@ export class SchemaGuardService implements OnModuleInit {
|
|||
constructor(@InjectDataSource() private readonly dataSource: DataSource) {}
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
// 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")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue