ALTER TABLE outbox
    ADD COLUMN IF NOT EXISTS idempotency_key TEXT NOT NULL DEFAULT '',
    ADD COLUMN IF NOT EXISTS next_retry_at TIMESTAMPTZ;

CREATE UNIQUE INDEX IF NOT EXISTS idx_outbox_user_idempotency
    ON outbox (user_id, idempotency_key)
    WHERE idempotency_key <> '';

CREATE INDEX IF NOT EXISTS idx_outbox_queued_retry
    ON outbox (status, next_retry_at, created_at)
    WHERE status = 'queued';
