create unique index if not exists idx_tenant_usage_records_worker_unique
  on public.tenant_usage_records(tenant_id, metric_key, period_start, period_end)
  where metadata->>'source' = 'platform_usage_worker';

comment on index public.idx_tenant_usage_records_worker_unique is
  'Ensures platform usage worker can upsert one authoritative record per tenant, metric and period while manual adjustments remain append-only.';
