create table chats( id uuid primary key, created timestamp with time zone not null default now() ); create table chat_messages( id uuid primary key, chat uuid not null, sender uuid not null references users(id), timestamp timestamp with time zone not null default now(), text text not null, extra jsonb ); create index chat_messages_chat_index on chat_messages(chat); create table chat_members( chat uuid not null, "user" uuid not null, read_until uuid references chat_messages(id), primary key(chat, "user") ); create index chat_members_user_index on chat_members("user"); create index chat_members_chat_user_index ON chat_members(chat, "user");