# EXPLAIN (ANALYZE, BUFFERS): история сообщений, рабочий индекс messages_chat_history_idx
# {"date": "2026-11-06T09:31:42Z", "docker_cpus": 12, "docker_memory_gib": 1.9, "docker_server": "27.2.0", "postgres": "17.11", "shared_buffers": "128MB"}
# messages: 200000; big chat: 100000, medium: 3000, small: 10

## big_first
SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = 'bab8f878-617d-4988-a42a-c28b81490ed2' ORDER BY created_at, id LIMIT 51
Limit  (cost=0.42..27.64 rows=51 width=462) (actual time=0.023..0.043 rows=51 loops=1)
  Buffers: shared hit=9
  ->  Index Scan using messages_chat_history_idx on messages  (cost=0.42..53519.06 rows=100267 width=462) (actual time=0.022..0.038 rows=51 loops=1)
        Index Cond: (chat_id = 'bab8f878-617d-4988-a42a-c28b81490ed2'::uuid)
        Buffers: shared hit=9
Planning:
  Buffers: shared hit=139
Planning Time: 1.200 ms
Execution Time: 0.107 ms

## big_d90_keyset
SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = 'bab8f878-617d-4988-a42a-c28b81490ed2' AND (created_at, id) > ('2026-09-28 00:28:21.157057+00', '68768260-63ef-498c-a995-6183f15116f1') ORDER BY created_at, id LIMIT 51
Limit  (cost=0.42..143.21 rows=51 width=462) (actual time=0.031..0.056 rows=51 loops=1)
  Buffers: shared hit=11
  ->  Index Scan using messages_chat_history_idx on messages  (cost=0.42..28399.10 rows=10143 width=462) (actual time=0.030..0.052 rows=51 loops=1)
        Index Cond: ((chat_id = 'bab8f878-617d-4988-a42a-c28b81490ed2'::uuid) AND (ROW(created_at, id) > ROW('2026-09-28 00:28:21.157057+00'::timestamp with time zone, '68768260-63ef-498c-a995-6183f15116f1'::uuid)))
        Buffers: shared hit=11
Planning:
  Buffers: shared hit=138
Planning Time: 0.939 ms
Execution Time: 0.128 ms

## big_d90_offset
SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = 'bab8f878-617d-4988-a42a-c28b81490ed2' ORDER BY created_at, id OFFSET 90000 LIMIT 51
Limit  (cost=36336.63..36336.74 rows=1 width=462) (actual time=86.122..93.620 rows=51 loops=1)
  Buffers: shared hit=12716, temp read=5489 written=5853
  I/O Timings: temp read=6.704 write=28.200
  ->  Gather Merge  (cost=26587.75..36336.63 rows=83556 width=462) (actual time=57.285..91.018 rows=90051 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=12716, temp read=5489 written=5853
        I/O Timings: temp read=6.704 write=28.200
        ->  Sort  (cost=25587.72..25692.17 rows=41778 width=462) (actual time=52.688..58.040 rows=30085 loops=3)
              Sort Key: created_at, id
              Sort Method: external merge  Disk: 15856kB
              Buffers: shared hit=12716, temp read=5489 written=5853
              I/O Timings: temp read=6.704 write=28.200
              Worker 0:  Sort Method: external merge  Disk: 15016kB
              Worker 1:  Sort Method: external merge  Disk: 15816kB
              ->  Parallel Seq Scan on messages  (cost=0.00..13669.67 rows=41778 width=462) (actual time=0.009..16.948 rows=33333 loops=3)
                    Filter: (chat_id = 'bab8f878-617d-4988-a42a-c28b81490ed2'::uuid)
                    Rows Removed by Filter: 33333
                    Buffers: shared hit=12628
Planning:
  Buffers: shared hit=139
Planning Time: 1.009 ms
Execution Time: 95.985 ms

## small_first
SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = 'fb9c80b1-749e-4946-aec5-cd8b3ab4eab1' ORDER BY created_at, id LIMIT 51
Limit  (cost=0.42..51.00 rows=12 width=462) (actual time=0.034..0.078 rows=10 loops=1)
  Buffers: shared hit=13
  ->  Index Scan using messages_chat_history_idx on messages  (cost=0.42..51.00 rows=12 width=462) (actual time=0.033..0.075 rows=10 loops=1)
        Index Cond: (chat_id = 'fb9c80b1-749e-4946-aec5-cd8b3ab4eab1'::uuid)
        Buffers: shared hit=13
Planning:
  Buffers: shared hit=139
Planning Time: 0.955 ms
Execution Time: 0.145 ms
