=== Data volume messages_total | messages_in_big_chat ----------------+---------------------- 23017 | 15000 === History index indexdef ---------------------------------------------------------------------------------------- CREATE INDEX messages_history ON public.messages USING btree (chat_id, created_at, id) === 1. First page: limit=50, the app asks for 51 rows to know if there is a next page QUERY PLAN ------------------------------------------------------------------------------------------------ Limit (actual time=0.013..0.125 rows=51 loops=1) Buffers: shared hit=55 -> Index Scan using messages_history on messages (actual time=0.012..0.119 rows=51 loops=1) Index Cond: (chat_id = '88ae823e-42fe-42cd-b02e-4974b4bcbf0f'::uuid) Buffers: shared hit=55 Planning: Buffers: shared hit=6 Planning Time: 0.119 ms Execution Time: 0.141 ms === 2. Page after the 10000th message by keyset cursor (this is what the app does) QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Limit (actual time=0.011..0.030 rows=51 loops=1) Buffers: shared hit=6 -> Index Scan using messages_history on messages (actual time=0.010..0.025 rows=51 loops=1) Index Cond: ((chat_id = '88ae823e-42fe-42cd-b02e-4974b4bcbf0f'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 05:46:40+03'::timestamp with time zone, '1e03049f-75da-40e8-9821-2d439e107e51'::uuid))) Buffers: shared hit=6 Planning Time: 0.124 ms Execution Time: 0.058 ms === 2b. The same page exactly as the app sends it since lab4: with attachment_ids of every message QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Limit (actual time=0.036..0.142 rows=51 loops=1) Buffers: shared hit=60 -> Index Scan using messages_history on messages m (actual time=0.035..0.137 rows=51 loops=1) Index Cond: ((chat_id = '88ae823e-42fe-42cd-b02e-4974b4bcbf0f'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 05:46:40+03'::timestamp with time zone, '1e03049f-75da-40e8-9821-2d439e107e51'::uuid))) Buffers: shared hit=60 SubPlan 1 -> Sort (actual time=0.002..0.002 rows=0 loops=51) Sort Key: a."position" Sort Method: quicksort Memory: 25kB Buffers: shared hit=54 -> Bitmap Heap Scan on message_attachments a (actual time=0.001..0.001 rows=0 loops=51) Recheck Cond: (message_id = m.id) Buffers: shared hit=51 -> Bitmap Index Scan on message_attachments_message_id_attachment_id_key (actual time=0.000..0.000 rows=0 loops=51) Index Cond: (message_id = m.id) Buffers: shared hit=51 Planning: Buffers: shared hit=63 Planning Time: 0.255 ms Execution Time: 0.202 ms === 3. The same page with OFFSET 10000 (the app does NOT do this) QUERY PLAN --------------------------------------------------------------------------------------------------- Limit (actual time=2.823..2.840 rows=51 loops=1) Buffers: shared hit=311 -> Index Scan using messages_history on messages (actual time=0.020..2.302 rows=10051 loops=1) Index Cond: (chat_id = '88ae823e-42fe-42cd-b02e-4974b4bcbf0f'::uuid) Buffers: shared hit=311 Planning Time: 0.082 ms Execution Time: 2.859 ms === 4. Query 2 without the index (index is dropped only inside this transaction) QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (actual time=3.356..3.366 rows=51 loops=1) Buffers: shared hit=318 -> Sort (actual time=3.354..3.359 rows=51 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 31kB Buffers: shared hit=318 -> Seq Scan on messages (actual time=1.132..2.326 rows=5000 loops=1) Filter: ((chat_id = '88ae823e-42fe-42cd-b02e-4974b4bcbf0f'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 05:46:40+03'::timestamp with time zone, '1e03049f-75da-40e8-9821-2d439e107e51'::uuid))) Rows Removed by Filter: 18017 Buffers: shared hit=318 Planning: Buffers: shared hit=4 Planning Time: 0.103 ms Execution Time: 3.385 ms === ROLLBACK: test data removed, index is back explain_users_left -------------------- 0