|
| 1 | +-- Event indexing optimization: add category, schema_version columns and supporting indexes. |
| 2 | +-- These columns are derived from the two-topic layout (category, event_name) emitted by the |
| 3 | +-- Soroban contract and enable efficient category-level filtering without JSON scanning. |
| 4 | + |
| 5 | +ALTER TABLE events |
| 6 | + ADD COLUMN IF NOT EXISTS category VARCHAR(20) NOT NULL DEFAULT 'unknown', |
| 7 | + ADD COLUMN IF NOT EXISTS schema_version SMALLINT NOT NULL DEFAULT 1; |
| 8 | + |
| 9 | +-- Backfill category from event_type for existing rows |
| 10 | +UPDATE events SET category = CASE |
| 11 | + WHEN event_type IN ( |
| 12 | + 'trade_created', 'trade_funded', 'trade_completed', 'trade_confirmed', |
| 13 | + 'trade_cancelled', 'time_released', 'metadata_updated', |
| 14 | + 'dispute_raised', 'dispute_resolved', 'partial_resolved' |
| 15 | + ) THEN 'trade' |
| 16 | + WHEN event_type IN ( |
| 17 | + 'arbitrator_registered', 'arbitrator_removed', |
| 18 | + 'arbitrator_rated', 'arbitrator_rep_updated' |
| 19 | + ) THEN 'arb' |
| 20 | + WHEN event_type IN ( |
| 21 | + 'fee_updated', 'fees_withdrawn', 'fees_distributed', 'custom_fee_set', |
| 22 | + 'tier_upgraded', 'tier_downgraded', 'tier_config_updated' |
| 23 | + ) THEN 'fee' |
| 24 | + WHEN event_type IN ( |
| 25 | + 'template_created', 'template_updated', 'template_deactivated', 'template_trade' |
| 26 | + ) THEN 'tmpl' |
| 27 | + WHEN event_type IN ( |
| 28 | + 'subscribed', 'subscription_renewed', 'subscription_cancelled' |
| 29 | + ) THEN 'sub' |
| 30 | + WHEN event_type IN ( |
| 31 | + 'proposal_created', 'vote_cast', 'proposal_executed', 'delegated' |
| 32 | + ) THEN 'gov' |
| 33 | + WHEN event_type IN ( |
| 34 | + 'insurance_provider_registered', 'insurance_provider_removed', |
| 35 | + 'insurance_purchased', 'insurance_claimed' |
| 36 | + ) THEN 'ins' |
| 37 | + WHEN event_type IN ( |
| 38 | + 'oracle_registered', 'oracle_removed', |
| 39 | + 'oracle_price_fetched', 'oracle_unavailable' |
| 40 | + ) THEN 'oracle' |
| 41 | + ELSE 'sys' |
| 42 | +END |
| 43 | +WHERE category = 'unknown'; |
| 44 | + |
| 45 | +-- Backfill schema_version from the JSON payload where available |
| 46 | +UPDATE events |
| 47 | +SET schema_version = (data->>'v')::SMALLINT |
| 48 | +WHERE data->>'v' IS NOT NULL |
| 49 | + AND (data->>'v') ~ '^\d+$'; |
| 50 | + |
| 51 | +-- Indexes for the new columns |
| 52 | +CREATE INDEX IF NOT EXISTS idx_events_category |
| 53 | + ON events (category); |
| 54 | + |
| 55 | +CREATE INDEX IF NOT EXISTS idx_events_category_ledger |
| 56 | + ON events (category, ledger DESC); |
| 57 | + |
| 58 | +CREATE INDEX IF NOT EXISTS idx_events_schema_version |
| 59 | + ON events (schema_version); |
| 60 | + |
| 61 | +-- Composite index for the most common query pattern: category + event_type + ledger range |
| 62 | +CREATE INDEX IF NOT EXISTS idx_events_category_type_ledger |
| 63 | + ON events (category, event_type, ledger DESC); |
| 64 | + |
| 65 | +-- Timestamp range index (already exists in initial migration, kept for reference) |
| 66 | +-- CREATE INDEX IF NOT EXISTS idx_events_timestamp ON events (timestamp DESC); |
| 67 | + |
| 68 | +-- Partial index for trade events (highest query volume) |
| 69 | +CREATE INDEX IF NOT EXISTS idx_events_trade_category |
| 70 | + ON events (ledger DESC, timestamp DESC) |
| 71 | + WHERE category = 'trade'; |
0 commit comments