Table shape

CREATE TABLE adk_messages (
  session_id  uuid NOT NULL REFERENCES adk_sessions(id) ON DELETE CASCADE,
  turn        bigint NOT NULL,
  role        text NOT NULL,   -- user, assistant, tool_call, tool_result
  content     jsonb NOT NULL,
  created_at  timestamptz NOT NULL DEFAULT now(),
  PRIMARY KEY (session_id, turn)
);
Advertisement

Composite PK matters

Compound (session_id, turn) makes 'load messages for session ordered by turn' an index-only scan. This query runs on every turn — make it O(k), not O(n).

Advertisement

Content as JSONB

Messages have variable shape — text, multimodal, tool calls with args. JSONB accommodates without schema churn. Query specific fields with content->>'text'.