CREATE TABLE idempotency_records ( id UUID PRIMARY KEY, user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, method VARCHAR(8) NOT NULL, path VARCHAR(200) NOT NULL, idempotency_key VARCHAR(128) NOT NULL, request_hash CHAR(64) NOT NULL, response_body JSONB NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, expires_at TIMESTAMPTZ NOT NULL, CONSTRAINT idempotency_records_scope_key_uq UNIQUE (user_id, method, path, idempotency_key), CHECK (method = 'POST'), CHECK (char_length(idempotency_key) BETWEEN 1 AND 128) ); CREATE INDEX idempotency_records_expires_at_idx ON idempotency_records(expires_at);