From 8f9252a5c1890f9928876f8fcb986845d63830b5 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:25:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20(c?= =?UTF-8?q?hat=5Fid,=20id)=20=D0=BA=D0=BB=D1=8E=D1=87=D0=BE=D0=BC=20=D1=81?= =?UTF-8?q?=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20=D1=81=D1=86?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=D1=80=D0=B8=D0=B9=20EXPLAIN=20=D0=B8=D1=81?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- migrations/00001_init.sql | 12 +++--- scripts/explain-history.sql | 75 +++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 scripts/explain-history.sql diff --git a/migrations/00001_init.sql b/migrations/00001_init.sql index e19bbf97..400b762e 100644 --- a/migrations/00001_init.sql +++ b/migrations/00001_init.sql @@ -22,17 +22,19 @@ CREATE TABLE chat_members ( -- Lists the chats of one user in (created_at, id) order: chat ids are UUIDv7. CREATE INDEX chat_members_user_chat_idx ON chat_members (user_id, chat_id); +-- The key is (chat_id, id) and serves the history query directly: +-- WHERE chat_id = $1 AND id > $2 ORDER BY id LIMIT $3. Nothing looks a +-- message up by id alone, and a separate index on id would only cost writes +-- and tempt the planner into scanning every chat's messages in id order. CREATE TABLE messages ( - id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats (id), + id uuid NOT NULL, sender_id uuid NOT NULL REFERENCES users (id), text text NOT NULL, - created_at timestamptz NOT NULL + created_at timestamptz NOT NULL, + PRIMARY KEY (chat_id, id) ); --- Keyset pagination of one chat's history: WHERE chat_id = $1 AND id > $2 ORDER BY id. -CREATE INDEX messages_chat_id_idx ON messages (chat_id, id); - -- +goose Down DROP TABLE messages; DROP TABLE chat_members; diff --git a/scripts/explain-history.sql b/scripts/explain-history.sql new file mode 100644 index 00000000..f455eedd --- /dev/null +++ b/scripts/explain-history.sql @@ -0,0 +1,75 @@ +-- EXPLAIN evidence for the message history query (make explain). +-- +-- Everything runs in one transaction that is rolled back: it seeds 12 000 +-- messages in one chat and 90 000 in 300 other chats, so the plan is measured +-- on a realistic volume without leaving data on the stand. The PREPARE text +-- is the statement of Messages.ListByChat in internal/chat/store/postgres.go; +-- keep them identical. +\set ON_ERROR_STOP on +\pset pager off +BEGIN; + +SELECT uuidv7() AS author \gset +SELECT uuidv7() AS big_chat \gset + +INSERT INTO users (id, username, display_name, password_hash, created_at) +VALUES (:'author', 'explain_' || substr(md5(random()::text), 1, 12), 'EXPLAIN', 'not-a-password-hash', + uuid_extract_timestamp(:'author')); + +CREATE TEMP TABLE noise_chats ON COMMIT DROP AS +SELECT uuidv7() AS id FROM generate_series(1, 300); + +INSERT INTO chats (id, title, created_at) +SELECT :'big_chat'::uuid, 'explain: big chat', uuid_extract_timestamp(:'big_chat') +UNION ALL +SELECT id, 'explain: other chat', uuid_extract_timestamp(id) FROM noise_chats; + +INSERT INTO chat_members (chat_id, user_id) +SELECT id, :'author' FROM chats WHERE title LIKE 'explain:%'; + +-- Rows are inserted ordered by n, so the messages of all chats interleave in +-- the heap as they would under real traffic; ids stay increasing per chat. +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT id, chat_id, :'author', 'message ' || n, uuid_extract_timestamp(id) +FROM ( + SELECT uuidv7() AS id, chat_id, n + FROM ( + SELECT :'big_chat'::uuid AS chat_id, n FROM generate_series(1, 12000) AS n + UNION ALL + SELECT c.id, n FROM noise_chats AS c, generate_series(1, 300) AS n + ) AS planned + ORDER BY n +) AS generated; + +ANALYZE users, chats, chat_members, messages; + +SELECT (SELECT count(*) FROM messages WHERE chat_id = :'big_chat') AS big_chat_messages, + (SELECT count(*) FROM messages) AS all_messages; + +PREPARE history(uuid, uuid, int) AS +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = $1 + AND id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid) +ORDER BY id +LIMIT $3; + +\echo +\echo == First page: no cursor, limit 50 (+1 row that tells whether a next page exists) +EXPLAIN (ANALYZE, BUFFERS) EXECUTE history(:'big_chat', NULL, 51); + +SELECT id AS deep_cursor FROM messages WHERE chat_id = :'big_chat' ORDER BY id OFFSET 10000 LIMIT 1 \gset +\echo +\echo == Deep page: cursor after the 10 001st message of the chat +EXPLAIN (ANALYZE, BUFFERS) EXECUTE history(:'big_chat', :'deep_cursor', 51); + +\echo +\echo == For contrast, OFFSET pagination of the same page reads every skipped row +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = :'big_chat' +ORDER BY id +OFFSET 10001 LIMIT 51; + +ROLLBACK;