CREATE TABLE IF NOT EXISTS creator_competitor_share_job ( id text PRIMARY KEY, platform text NOT NULL CHECK (platform IN ('douyin', 'xiaohongshu')), share_url text NOT NULL, tags text[] NOT NULL DEFAULT '{}', status text NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'processing', 'succeeded', 'failed')), attempts integer NOT NULL DEFAULT 0 CHECK (attempts >= 0 AND attempts <= 3), competitor_id text REFERENCES creator_competitor(id) ON DELETE SET NULL, failure_reason text NOT NULL DEFAULT '', lease_token text NOT NULL DEFAULT '', lease_until timestamptz, last_attempt_at timestamptz, completed_at timestamptz, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS creator_competitor_share_job_due_idx ON creator_competitor_share_job (status, lease_until, created_at);