-- =============================================================================
-- Migration: 007_add_stage_metrics.sql
-- Add stage-level metrics to pipeline executions
-- =============================================================================
--
-- Adds a JSONB column to store per-stage execution metrics for profiling:
--   - duration_ms: execution time per stage
--   - records_processed: number of records handled
--   - errors: any stage-specific errors
--
-- Date: 2026-01-24
-- =============================================================================

-- Add stage_metrics column to store per-stage profiling data
ALTER TABLE pipeline.executions
ADD COLUMN IF NOT EXISTS stage_metrics JSONB;

COMMENT ON COLUMN pipeline.executions.stage_metrics IS 'Per-stage execution metrics (timing, records processed, errors)';

-- Example structure:
-- {
--   "normalize": {"duration_ms": 150, "records_in": 100, "records_out": 100, "success": true},
--   "classify": {"duration_ms": 2500, "records_in": 100, "records_out": 100, "success": true},
--   "route": {"duration_ms": 80, "records_in": 100, "records_out": 15, "success": true},
--   "aggregate": {"duration_ms": 45, "records_in": 100, "records_out": 1, "success": true}
-- }

-- Add total_duration_ms for quick access to overall execution time
ALTER TABLE pipeline.executions
ADD COLUMN IF NOT EXISTS total_duration_ms INTEGER;

COMMENT ON COLUMN pipeline.executions.total_duration_ms IS 'Total execution duration in milliseconds';

-- =============================================================================
-- DONE
-- =============================================================================
