-- Indexes justified by the two keyset queries of the contract. -- -- Message history: -- WHERE chat_id = $1 AND (created_at, id) > ($2, $3) -- ORDER BY created_at, id LIMIT $4 -- The three-column index lets PostgreSQL start at the cursor position inside -- one chat instead of scanning the whole history for every page. CREATE INDEX messages_chat_created_id_idx ON messages (chat_id, created_at, id); -- Chat list: -- ORDER BY created_at, id LIMIT $1 -- after joining chat_members for the current user. CREATE INDEX chats_created_id_idx ON chats (created_at, id);