Implement lab2 PostgreSQL sessions and Redis cache

This commit is contained in:
Polusikkkk
2026-10-16 18:23:50 +03:00
parent 65003b6119
commit 083dc49564
25 changed files with 2782 additions and 29 deletions
+94
View File
@@ -0,0 +1,94 @@
\set ON_ERROR_STOP on
\pset pager off
\echo '=== PostgreSQL version ==='
SELECT version();
SELECT id AS user_id
FROM users
ORDER BY created_at, id
LIMIT 1
\gset fixture_
SELECT gen_random_uuid() AS chat_id
\gset fixture_
BEGIN;
INSERT INTO chats (id, title)
VALUES (
:'fixture_chat_id'::uuid,
'EXPLAIN fixture: 10000 messages'
);
INSERT INTO chat_members (chat_id, user_id)
VALUES (
:'fixture_chat_id'::uuid,
:'fixture_user_id'::uuid
);
INSERT INTO messages (
id,
chat_id,
sender_id,
text,
created_at
)
SELECT
gen_random_uuid(),
:'fixture_chat_id'::uuid,
:'fixture_user_id'::uuid,
'Synthetic message ' || n::text,
TIMESTAMPTZ '2000-01-01 00:00:00+00'
+ ((n - 1) / 10) * INTERVAL '1 millisecond'
FROM generate_series(1, 10000) AS series(n);
COMMIT;
ANALYZE messages;
\echo '=== Fixture size ==='
SELECT
chat_id,
count(*) AS message_count,
count(DISTINCT created_at) AS distinct_timestamps
FROM messages
WHERE chat_id = :'fixture_chat_id'::uuid
GROUP BY chat_id;
\echo '=== History index definition ==='
SELECT indexdef
FROM pg_indexes
WHERE schemaname = 'public'
AND tablename = 'messages'
AND indexname = 'messages_chat_created_id_idx';
\echo '=== First page: LIMIT 51 ==='
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = :'fixture_chat_id'::uuid
ORDER BY created_at, id
LIMIT 51;
SELECT
created_at AS created_at,
id AS id
FROM messages
WHERE chat_id = :'fixture_chat_id'::uuid
ORDER BY created_at, id
OFFSET 4999
LIMIT 1
\gset cursor_
\echo '=== Middle page: keyset cursor, LIMIT 51 ==='
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = :'fixture_chat_id'::uuid
AND (created_at, id) > (
:'cursor_created_at'::timestamptz,
:'cursor_id'::uuid
)
ORDER BY created_at, id
LIMIT 51;
+15
View File
@@ -0,0 +1,15 @@
#!/bin/sh
set -eu
PROJECT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
cd "$PROJECT_DIR"
docker compose --env-file .env stop --timeout 10 api
docker compose --env-file .env restart --timeout 10 postgres redis
docker compose --env-file .env up \
-d \
--wait \
--wait-timeout 60 \
api redis