Fix durable logout and add grade-5 research
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
Executable
+35
@@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
# Grade-5 track 2: build the 100k uneven fixture, compare two index choices,
|
||||
# then backup and restore into a separate database and compare integrity sums.
|
||||
# The normal messenger database and its named volume are never removed.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
ENV_FILE="${COMPOSE_ENV_FILE:-.env}"
|
||||
mkdir -p evidence
|
||||
|
||||
docker compose --env-file "$ENV_FILE" exec -T postgres \
|
||||
psql -X -U messenger -d messenger -v ON_ERROR_STOP=1 -f - \
|
||||
< scripts/research-history-backup.sql > evidence/research-history-backup.txt 2>&1
|
||||
|
||||
{
|
||||
printf '%s\n' '=== backup ==='
|
||||
docker compose --env-file "$ENV_FILE" exec -T postgres sh -ceu '
|
||||
rm -f /tmp/lab2-grade5.dump
|
||||
pg_dump -U messenger -d messenger --format=custom --file=/tmp/lab2-grade5.dump
|
||||
dropdb -U messenger --if-exists messenger_grade5_restore
|
||||
createdb -U messenger messenger_grade5_restore
|
||||
pg_restore -U messenger -d messenger_grade5_restore --exit-on-error /tmp/lab2-grade5.dump
|
||||
'
|
||||
printf '%s\n' '=== source integrity ==='
|
||||
docker compose --env-file "$ENV_FILE" exec -T postgres \
|
||||
psql -X -U messenger -d messenger -At -F '|' -c \
|
||||
"SELECT count(*), min(created_at), max(created_at), sum(length(text)) FROM messages WHERE sender_id = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';"
|
||||
printf '%s\n' '=== restored integrity ==='
|
||||
docker compose --env-file "$ENV_FILE" exec -T postgres \
|
||||
psql -X -U messenger -d messenger_grade5_restore -At -F '|' -c \
|
||||
"SELECT count(*), min(created_at), max(created_at), sum(length(text)) FROM messages WHERE sender_id = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';"
|
||||
} >> evidence/research-history-backup.txt 2>&1
|
||||
|
||||
cat evidence/research-history-backup.txt
|
||||
printf '%s\n' 'saved evidence/research-history-backup.txt'
|
||||
@@ -0,0 +1,105 @@
|
||||
-- Grade-5 track 2: query-plan comparison on >=100k messages with highly
|
||||
-- uneven chat sizes. The deterministic fixture is isolated by username/title
|
||||
-- prefixes and can safely be re-run on the same persistent volume.
|
||||
\set ON_ERROR_STOP on
|
||||
\timing on
|
||||
|
||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', 'research_seed_user',
|
||||
'Research seed user', 'not-a-usable-hash', now())
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO chats (id, title, created_at)
|
||||
VALUES ('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', 'research-hot-chat', now())
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO chat_members (chat_id, user_id, joined_at)
|
||||
VALUES ('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb',
|
||||
'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', now())
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- One hot chat with 90k messages and 100 cold chats with 100 messages each.
|
||||
-- md5() yields stable 32 hex characters and PostgreSQL accepts it as UUID.
|
||||
INSERT INTO chats (id, title, created_at)
|
||||
SELECT md5('research-cold-chat-' || n)::uuid, 'research-cold-chat-' || n, now()
|
||||
FROM generate_series(1, 100) AS n
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO chat_members (chat_id, user_id, joined_at)
|
||||
SELECT md5('research-cold-chat-' || n)::uuid,
|
||||
'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', now()
|
||||
FROM generate_series(1, 100) AS n
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT md5('research-hot-message-' || n)::uuid,
|
||||
'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb',
|
||||
'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',
|
||||
'hot message ' || n,
|
||||
timestamptz '2026-02-01 00:00:00+00' + make_interval(secs => n * 0.001)
|
||||
FROM generate_series(1, 90000) AS n
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT md5('research-cold-message-' || chat_n || '-' || message_n)::uuid,
|
||||
md5('research-cold-chat-' || chat_n)::uuid,
|
||||
'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',
|
||||
'cold message ' || chat_n || '-' || message_n,
|
||||
timestamptz '2026-02-02 00:00:00+00'
|
||||
+ make_interval(secs => (chat_n * 100 + message_n) * 0.001)
|
||||
FROM generate_series(1, 100) AS chat_n,
|
||||
generate_series(1, 100) AS message_n
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
ANALYZE messages;
|
||||
|
||||
\echo '=== dataset shape ==='
|
||||
SELECT count(*) AS research_messages,
|
||||
count(DISTINCT chat_id) AS research_chats,
|
||||
max(per_chat.count) AS largest_chat_messages,
|
||||
min(per_chat.count) AS smallest_chat_messages
|
||||
FROM messages
|
||||
JOIN (
|
||||
SELECT chat_id, count(*)
|
||||
FROM messages
|
||||
WHERE sender_id = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
|
||||
GROUP BY chat_id
|
||||
) AS per_chat USING (chat_id)
|
||||
WHERE messages.sender_id = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';
|
||||
|
||||
\echo '=== source integrity summary ==='
|
||||
SELECT count(*) AS count,
|
||||
min(created_at) AS first_message,
|
||||
max(created_at) AS last_message,
|
||||
sum(length(text)) AS text_bytes
|
||||
FROM messages
|
||||
WHERE sender_id = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';
|
||||
|
||||
\echo '=== index A: production composite (chat_id, created_at, id) ==='
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
|
||||
AND (created_at, id) > (
|
||||
timestamptz '2026-02-01 00:01:20+00',
|
||||
md5('research-hot-message-80000')::uuid
|
||||
)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 50;
|
||||
|
||||
\echo '=== index B: chat_id-only alternative (temporary, rolled back) ==='
|
||||
BEGIN;
|
||||
DROP INDEX messages_chat_order_idx;
|
||||
CREATE INDEX messages_research_chat_only_idx ON messages (chat_id);
|
||||
ANALYZE messages;
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
|
||||
AND (created_at, id) > (
|
||||
timestamptz '2026-02-01 00:01:20+00',
|
||||
md5('research-hot-message-80000')::uuid
|
||||
)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 50;
|
||||
ROLLBACK;
|
||||
Reference in New Issue
Block a user