CREATE TABLE idempotency_keys ( user_id uuid NOT NULL REFERENCES users (id), scope text NOT NULL, key text NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'), request_hash bytea NOT NULL CHECK (octet_length(request_hash) = 32), message_id uuid REFERENCES messages (id), created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (user_id, scope, key) ); CREATE INDEX idempotency_keys_created_idx ON idempotency_keys (created_at);