-- Result of POST /chats/{id}/messages per (user, method, path, Idempotency-Key). -- The row and the message are written in one transaction, so a failed attempt leaves no key behind. CREATE TABLE idempotency_keys ( user_id UUID NOT NULL REFERENCES users (id), method TEXT NOT NULL, path TEXT NOT NULL, key TEXT NOT NULL, request_hash TEXT NOT NULL, status_code INTEGER NOT NULL, response JSONB NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), PRIMARY KEY (user_id, method, path, key), CONSTRAINT idempotency_keys_key_format CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$') ); CREATE INDEX idempotency_keys_created_idx ON idempotency_keys (created_at);