PostgreSQL schema and transactions
Tables and invariants
users: UUID primary key, unique constrained username, display name, Argon2 password hash, server timestamp.chats: UUID primary key and title.chat_members: composite(chat_id, user_id)primary key, foreign keys to both parent tables, unique stable member position per chat. The(user_id, chat_id)index supports listing a user's chats.messages: UUID primary key and foreign keys to chat and sender.(chat_id, created_at, id)supports the API's ascending keyset history query.sessions: SHA-256 digest of a random opaque token as primary key, user foreign key, creation and expiry timestamps. Raw tokens are returned only once and are never stored.schema_migrations: migration filename and application timestamp; created by the migration runner.
All timestamps are TIMESTAMPTZ. PostgreSQL owns generated UUIDs and creation timestamps. Named migrations run once in lexicographic order; an advisory transaction lock serializes startup migration attempts.
Transaction boundaries
Each HTTP request uses one pooled PostgreSQL connection and one transaction. A successful response is written only after that transaction commits. Creating a chat and inserting every member is one transaction, so a failed member insert rolls back both the chat and any earlier member rows. Registration relies on the unique username constraint as the final arbiter of concurrent registrations; the losing request returns 409.
Message history uses WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4, matching messages_chat_history_idx. Cursors include their resource scope and the last row's timestamp and UUID.
Durability
Compose mounts PostgreSQL's data directory on the named postgres_data volume. The persistence hook recreates the API and database containers without removing that volume. Backups and destructive volume resets are deliberately separate operator actions.