forked from mtusi2026/mtusi2026lab2
115 lines
4.4 KiB
PL/PgSQL
115 lines
4.4 KiB
PL/PgSQL
-- Synthetic, repeatable dataset for the Track 2 experiments.
|
|
-- Usage:
|
|
-- psql -v target_messages=300000 -v chat_count=5000 -v user_count=2000 -f seed.sql
|
|
-- The distribution is deliberately skewed: a few chats hold most messages,
|
|
-- which is closer to a real messenger than one evenly filled chat.
|
|
\set ON_ERROR_STOP on
|
|
\if :{?target_messages}
|
|
\else
|
|
\set target_messages 300000
|
|
\endif
|
|
\if :{?chat_count}
|
|
\else
|
|
\set chat_count 5000
|
|
\endif
|
|
\if :{?user_count}
|
|
\else
|
|
\set user_count 2000
|
|
\endif
|
|
|
|
-- Deterministic, readable UUIDs. The version/variant nibbles are valid.
|
|
CREATE OR REPLACE FUNCTION research_user_uuid(i integer)
|
|
RETURNS uuid LANGUAGE sql IMMUTABLE AS $$
|
|
SELECT ('00000000-0000-4000-8000-' || lpad(to_hex(i), 12, '0'))::uuid
|
|
$$;
|
|
|
|
CREATE OR REPLACE FUNCTION research_chat_uuid(i integer)
|
|
RETURNS uuid LANGUAGE sql IMMUTABLE AS $$
|
|
SELECT ('10000000-0000-4000-8000-' || lpad(to_hex(i), 12, '0'))::uuid
|
|
$$;
|
|
|
|
DROP TABLE IF EXISTS research_chat_sender;
|
|
TRUNCATE messages, chat_members, chats, users CASCADE;
|
|
|
|
-- Reproducible pseudo-random layout for a given PostgreSQL build.
|
|
SELECT setseed(0.4242);
|
|
|
|
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
|
SELECT research_user_uuid(g),
|
|
'u' || lpad(g::text, 4, '0'),
|
|
'User ' || g,
|
|
'not-a-login-hash',
|
|
'2026-01-01T00:00:00Z'::timestamptz
|
|
FROM generate_series(1, :user_count) AS g;
|
|
|
|
INSERT INTO chats (id, title, created_at)
|
|
SELECT research_chat_uuid(g),
|
|
'Research chat ' || g,
|
|
'2026-01-01T00:00:00Z'::timestamptz + (g || ' seconds')::interval
|
|
FROM generate_series(1, :chat_count) AS g;
|
|
|
|
-- Each chat gets a small random membership set (5..24 distinct users).
|
|
INSERT INTO chat_members (chat_id, user_id)
|
|
SELECT research_chat_uuid(c),
|
|
research_user_uuid(1 + floor(random() * :user_count)::int)
|
|
FROM generate_series(1, :chat_count) AS c
|
|
CROSS JOIN generate_series(1, 5 + (c % 20)) AS m
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
-- Probe user 1 is a member of the ten largest chats and every 100th chat.
|
|
-- user_chats and user_feed queries therefore always have rows to return.
|
|
INSERT INTO chat_members (chat_id, user_id)
|
|
SELECT research_chat_uuid(c), research_user_uuid(1)
|
|
FROM generate_series(1, :chat_count) AS c
|
|
WHERE c <= 10 OR c % 100 = 0
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
-- One valid sender per chat, so every synthetic message has a member sender.
|
|
CREATE TABLE research_chat_sender AS
|
|
SELECT chat_id, (array_agg(user_id ORDER BY random()))[1] AS sender_id
|
|
FROM chat_members
|
|
GROUP BY chat_id;
|
|
ALTER TABLE research_chat_sender ADD PRIMARY KEY (chat_id);
|
|
|
|
-- AGENT: a uniform chat size would make every query look cheap, so sizes are
|
|
-- skewed. The probe user is deliberately in the largest chats to exercise the
|
|
-- worst realistic case for the cross-chat feed.
|
|
-- floor(random()^3 * chat_count) makes chat 1 hold roughly 6% of all
|
|
-- messages at 300k, while most chats stay small. Timestamps span 90 days.
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
SELECT gen_random_uuid(),
|
|
s.chat_id,
|
|
cs.sender_id,
|
|
'research message ' || s.g || ' ' || repeat('x', 20 + (s.g % 60)),
|
|
'2026-01-01T00:00:00Z'::timestamptz + interval '90 days' * s.r
|
|
FROM (
|
|
SELECT g,
|
|
research_chat_uuid(floor(power(random(), 3) * :chat_count)::int + 1) AS chat_id,
|
|
random() AS r
|
|
FROM generate_series(1, :target_messages) AS g
|
|
) AS s
|
|
JOIN research_chat_sender cs USING (chat_id);
|
|
|
|
-- Rebuild the two indexes so their reported sizes are not inflated by the
|
|
-- random insertion order. This keeps the index comparison and the dump size
|
|
-- on the same, compact baseline.
|
|
REINDEX INDEX chat_members_user_idx;
|
|
REINDEX INDEX messages_chat_created_id_idx;
|
|
VACUUM (ANALYZE) messages, chat_members, chats, users, research_chat_sender;
|
|
|
|
SELECT 'messages' AS item, count(*)::text AS value FROM messages
|
|
UNION ALL SELECT 'chat_members', count(*)::text FROM chat_members
|
|
UNION ALL SELECT 'chats', count(*)::text FROM chats
|
|
UNION ALL SELECT 'users', count(*)::text FROM users
|
|
UNION ALL SELECT 'messages_table_bytes', pg_total_relation_size('messages')::text
|
|
UNION ALL SELECT 'chat_members_user_idx_bytes', pg_relation_size('chat_members_user_idx')::text
|
|
UNION ALL SELECT 'messages_chat_created_id_idx_bytes', pg_relation_size('messages_chat_created_id_idx')::text;
|
|
|
|
-- Shape check: largest chats should dominate the message count.
|
|
SELECT c.id, c.title, count(m.id) AS messages
|
|
FROM chats c
|
|
LEFT JOIN messages m ON m.chat_id = c.id
|
|
GROUP BY c.id, c.title
|
|
ORDER BY messages DESC
|
|
LIMIT 5;
|