94 lines
1.9 KiB
PL/PgSQL
94 lines
1.9 KiB
PL/PgSQL
\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; |