# План запроса истории: размер 10k

Commit b2fb88f
Читаемый чат — 10000 сообщений; фоновые чаты: 2 × 1k, 10 × 100, 50 × 10.
Страница — `limit` профиля; «после» — сколько сообщений чата до
страницы.

## Планы приложения

auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план,
с которыми его выполнило приложение (prepared statement asyncpg).

### read 10k: first, после 0

duration: 0.166 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id 
 LIMIT $2::INTEGER
Query Parameters: $1 = 'a15252a7-4b4d-4826-b9af-d40b3f396740', $2 = '51'
Limit  (cost=0.29..9.63 rows=51 width=153) (actual time=0.040..0.132 rows=51.00 loops=1)
  Buffers: shared hit=18
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1832.72 rows=10000 width=153) (actual time=0.038..0.128 rows=51.00 loops=1)
        Index Cond: (chat_id = 'a15252a7-4b4d-4826-b9af-d40b3f396740'::uuid)
        Index Searches: 1
        Buffers: shared hit=18
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)

### read 10k: middle, после 5000

duration: 0.159 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3::UUID) ORDER BY messages.created_at, messages.id 
 LIMIT $4::INTEGER
Query Parameters: $1 = 'a15252a7-4b4d-4826-b9af-d40b3f396740', $2 = '2026-10-31 20:35:09.519876+00', $3 = '6819996c-a9b7-49bf-a667-63e22465f335', $4 = '51'
Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.038..0.115 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..218.47 rows=71 width=153) (actual time=0.037..0.112 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=54
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)

### read 10k: last, после 9900

duration: 0.111 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3::UUID) ORDER BY messages.created_at, messages.id 
 LIMIT $4::INTEGER
Query Parameters: $1 = 'a15252a7-4b4d-4826-b9af-d40b3f396740', $2 = '2026-10-31 20:35:21.624448+00', $3 = '11df026f-1d67-40fd-9265-0191805efd2b', $4 = '51'
Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.019..0.081 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..218.47 rows=71 width=153) (actual time=0.019..0.078 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=54
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)

### bg 1k: first, после 0

duration: 0.147 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id 
 LIMIT $2::INTEGER
Query Parameters: $1 = '7efbb7d2-1786-4de5-9216-ec211d58e49a', $2 = '51'
Limit  (cost=0.29..54.21 rows=51 width=153) (actual time=0.024..0.110 rows=51.00 loops=1)
  Buffers: shared hit=47
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1057.63 rows=1000 width=153) (actual time=0.024..0.107 rows=51.00 loops=1)
        Index Cond: (chat_id = '7efbb7d2-1786-4de5-9216-ec211d58e49a'::uuid)
        Index Searches: 1
        Buffers: shared hit=47
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)

### bg 1k: middle, после 500

duration: 0.129 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3::UUID) ORDER BY messages.created_at, messages.id 
 LIMIT $4::INTEGER
Query Parameters: $1 = '7efbb7d2-1786-4de5-9216-ec211d58e49a', $2 = '2026-10-31 20:35:09.525761+00', $3 = '34ae19f0-3c97-4690-bc5e-a23eb57bddda', $4 = '51'
Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.020..0.095 rows=51.00 loops=1)
  Buffers: shared hit=46
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..218.47 rows=71 width=153) (actual time=0.019..0.092 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=46
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)

### bg 1k: last, после 900

duration: 0.131 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3::UUID) ORDER BY messages.created_at, messages.id 
 LIMIT $4::INTEGER
Query Parameters: $1 = '7efbb7d2-1786-4de5-9216-ec211d58e49a', $2 = '2026-10-31 20:35:19.321243+00', $3 = 'a8ca10b3-a372-48db-83d1-cb4cab0e70db', $4 = '51'
Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.020..0.095 rows=51.00 loops=1)
  Buffers: shared hit=47
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..218.47 rows=71 width=153) (actual time=0.020..0.092 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=47
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)

### bg 100: first, после 0

duration: 0.157 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id 
 LIMIT $2::INTEGER
Query Parameters: $1 = 'e071a7a6-8832-4ab0-b80e-e5830f099a95', $2 = '51'
Limit  (cost=0.29..151.04 rows=51 width=153) (actual time=0.024..0.130 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..295.88 rows=100 width=153) (actual time=0.024..0.127 rows=51.00 loops=1)
        Index Cond: (chat_id = 'e071a7a6-8832-4ab0-b80e-e5830f099a95'::uuid)
        Index Searches: 1
        Buffers: shared hit=51
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)

### bg 10: first, после 0

duration: 0.043 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id 
 LIMIT $2::INTEGER
Query Parameters: $1 = '1324dcfc-ac5c-4d64-b55b-d9f8febbff32', $2 = '51'
Limit  (cost=0.29..38.26 rows=10 width=153) (actual time=0.016..0.040 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..38.26 rows=10 width=153) (actual time=0.015..0.039 rows=10.00 loops=1)
        Index Cond: (chat_id = '1324dcfc-ac5c-4d64-b55b-d9f8febbff32'::uuid)
        Index Searches: 1
        Buffers: shared hit=12
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                      Filter: (message_id = messages.id)

## Страницы сравнения в psql: варианты

Тот же текст запроса и те же параметры, что у плана приложения этой
страницы, в одной транзакции, которая откатывается: индексы базы
после неё те же. Каждый вариант выполнен дважды, здесь — второе
выполнение. `psql` — индекс приложения, custom plan; `generic` — он
же с force_generic_plan; `or-cursor` — условие курсора через OR;
`no-index` — без индекса приложения; варианты индекса вместо него:
`chat_only` (chat_id), `covering` (chat_id, created_at, id) INCLUDE (sender_id, text).

### read 10k: middle, после 5000

#### psql

Limit  (cost=0.29..14.72 rows=51 width=153) (actual time=0.007..0.050 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1415.52 rows=5001 width=153) (actual time=0.007..0.047 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'a15252a7-4b4d-4826-b9af-d40b3f396740'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:35:09.519876+00'::timestamp with time zone, '6819996c-a9b7-49bf-a667-63e22465f335'::uuid)))
        Index Searches: 1
        Buffers: shared hit=54
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.060 ms
Execution Time: 0.056 ms

#### generic

Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.009..0.059 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..218.47 rows=71 width=153) (actual time=0.009..0.056 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=54
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning Time: 0.010 ms
Execution Time: 0.069 ms

#### or-cursor

Limit  (cost=0.29..18.97 rows=51 width=153) (actual time=0.578..0.614 rows=51.00 loops=1)
  Buffers: shared hit=5077
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1832.74 rows=5001 width=153) (actual time=0.578..0.611 rows=51.00 loops=1)
        Index Cond: (chat_id = 'a15252a7-4b4d-4826-b9af-d40b3f396740'::uuid)
        Filter: ((created_at > '2026-10-31 20:35:09.519876+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:35:09.519876+00'::timestamp with time zone) AND (id > '6819996c-a9b7-49bf-a667-63e22465f335'::uuid)))
        Rows Removed by Filter: 5000
        Index Searches: 1
        Buffers: shared hit=5077
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.070 ms
Execution Time: 0.621 ms

#### no-index

Limit  (cost=772.11..772.24 rows=51 width=153) (actual time=3.749..3.753 rows=51.00 loops=1)
  Buffers: shared hit=294
  ->  Sort  (cost=772.11..784.61 rows=5001 width=153) (actual time=3.749..3.751 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 45kB
        Buffers: shared hit=294
        ->  Seq Scan on messages  (cost=0.00..605.26 rows=5001 width=153) (actual time=0.248..3.288 rows=5000.00 loops=1)
              Filter: ((chat_id = 'a15252a7-4b4d-4826-b9af-d40b3f396740'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:35:09.519876+00'::timestamp with time zone, '6819996c-a9b7-49bf-a667-63e22465f335'::uuid)))
              Rows Removed by Filter: 8500
              Buffers: shared hit=294
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=5000)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=5000)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.059 ms
Execution Time: 3.760 ms

#### chat_only

Limit  (cost=772.11..772.24 rows=51 width=153) (actual time=3.711..3.715 rows=51.00 loops=1)
  Buffers: shared hit=294
  ->  Sort  (cost=772.11..784.61 rows=5001 width=153) (actual time=3.710..3.713 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 45kB
        Buffers: shared hit=294
        ->  Seq Scan on messages  (cost=0.00..605.26 rows=5001 width=153) (actual time=0.305..3.238 rows=5000.00 loops=1)
              Filter: ((chat_id = 'a15252a7-4b4d-4826-b9af-d40b3f396740'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:35:09.519876+00'::timestamp with time zone, '6819996c-a9b7-49bf-a667-63e22465f335'::uuid)))
              Rows Removed by Filter: 8500
              Buffers: shared hit=294
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=5000)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=5000)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.080 ms
Execution Time: 3.726 ms

#### covering

Limit  (cost=0.41..6.19 rows=51 width=153) (actual time=0.005..0.044 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..567.45 rows=5001 width=153) (actual time=0.005..0.041 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'a15252a7-4b4d-4826-b9af-d40b3f396740'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:35:09.519876+00'::timestamp with time zone, '6819996c-a9b7-49bf-a667-63e22465f335'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.050 ms
Execution Time: 0.051 ms

### bg 1k: middle, после 500

#### psql

Limit  (cost=0.29..96.23 rows=51 width=153) (actual time=0.007..0.047 rows=51.00 loops=1)
  Buffers: shared hit=46
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..940.93 rows=500 width=153) (actual time=0.006..0.045 rows=51.00 loops=1)
        Index Cond: ((chat_id = '7efbb7d2-1786-4de5-9216-ec211d58e49a'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:35:09.525761+00'::timestamp with time zone, '34ae19f0-3c97-4690-bc5e-a23eb57bddda'::uuid)))
        Index Searches: 1
        Buffers: shared hit=46
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.058 ms
Execution Time: 0.054 ms

#### generic

Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.012..0.054 rows=51.00 loops=1)
  Buffers: shared hit=46
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..218.47 rows=71 width=153) (actual time=0.011..0.051 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=46
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning Time: 0.010 ms
Execution Time: 0.066 ms

#### or-cursor

Limit  (cost=0.29..108.13 rows=51 width=153) (actual time=0.094..0.135 rows=51.00 loops=1)
  Buffers: shared hit=507
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1057.63 rows=500 width=153) (actual time=0.094..0.132 rows=51.00 loops=1)
        Index Cond: (chat_id = '7efbb7d2-1786-4de5-9216-ec211d58e49a'::uuid)
        Filter: ((created_at > '2026-10-31 20:35:09.525761+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:35:09.525761+00'::timestamp with time zone) AND (id > '34ae19f0-3c97-4690-bc5e-a23eb57bddda'::uuid)))
        Rows Removed by Filter: 500
        Index Searches: 1
        Buffers: shared hit=507
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.046 ms
Execution Time: 0.141 ms

#### no-index

Limit  (cost=554.43..554.56 rows=51 width=153) (actual time=1.044..1.047 rows=51.00 loops=1)
  Buffers: shared hit=294
  ->  Sort  (cost=554.43..555.68 rows=500 width=153) (actual time=1.043..1.045 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 39kB
        Buffers: shared hit=294
        ->  Seq Scan on messages  (cost=0.00..537.75 rows=500 width=153) (actual time=0.530..0.995 rows=500.00 loops=1)
              Filter: ((chat_id = '7efbb7d2-1786-4de5-9216-ec211d58e49a'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:35:09.525761+00'::timestamp with time zone, '34ae19f0-3c97-4690-bc5e-a23eb57bddda'::uuid)))
              Rows Removed by Filter: 13000
              Buffers: shared hit=294
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.104 ms
Execution Time: 1.061 ms

#### chat_only

Limit  (cost=351.59..351.72 rows=51 width=153) (actual time=0.456..0.460 rows=51.00 loops=1)
  Buffers: shared hit=286
  ->  Sort  (cost=351.59..352.84 rows=500 width=153) (actual time=0.456..0.458 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 39kB
        Buffers: shared hit=286
        ->  Bitmap Heap Scan on messages  (cost=15.91..334.91 rows=500 width=153) (actual time=0.083..0.407 rows=500.00 loops=1)
              Recheck Cond: (chat_id = '7efbb7d2-1786-4de5-9216-ec211d58e49a'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-31 20:35:09.525761+00'::timestamp with time zone, '34ae19f0-3c97-4690-bc5e-a23eb57bddda'::uuid))
              Rows Removed by Filter: 500
              Heap Blocks: exact=283
              Buffers: shared hit=286
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..15.79 rows=1000 width=0) (actual time=0.018..0.018 rows=1000.00 loops=1)
                    Index Cond: (chat_id = '7efbb7d2-1786-4de5-9216-ec211d58e49a'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=3
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.046 ms
Execution Time: 0.467 ms

#### covering

Limit  (cost=0.41..6.28 rows=51 width=153) (actual time=0.006..0.044 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..57.91 rows=500 width=153) (actual time=0.005..0.041 rows=51.00 loops=1)
        Index Cond: ((chat_id = '7efbb7d2-1786-4de5-9216-ec211d58e49a'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:35:09.525761+00'::timestamp with time zone, '34ae19f0-3c97-4690-bc5e-a23eb57bddda'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.040 ms
Execution Time: 0.051 ms

### bg 100: first, после 0

#### psql

Limit  (cost=0.29..151.04 rows=51 width=153) (actual time=0.005..0.053 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..295.88 rows=100 width=153) (actual time=0.005..0.051 rows=51.00 loops=1)
        Index Cond: (chat_id = 'e071a7a6-8832-4ab0-b80e-e5830f099a95'::uuid)
        Index Searches: 1
        Buffers: shared hit=51
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.045 ms
Execution Time: 0.060 ms

#### generic

Limit  (cost=0.29..53.03 rows=21 width=153) (actual time=0.010..0.064 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..537.81 rows=214 width=153) (actual time=0.009..0.061 rows=51.00 loops=1)
        Index Cond: (chat_id = $1)
        Index Searches: 1
        Buffers: shared hit=51
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning Time: 0.004 ms
Execution Time: 0.072 ms

#### no-index

Limit  (cost=467.57..467.70 rows=51 width=153) (actual time=0.405..0.409 rows=51.00 loops=1)
  Buffers: shared hit=294
  ->  Sort  (cost=467.57..467.82 rows=100 width=153) (actual time=0.405..0.406 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=294
        ->  Seq Scan on messages  (cost=0.00..464.25 rows=100 width=153) (actual time=0.013..0.390 rows=100.00 loops=1)
              Filter: (chat_id = 'e071a7a6-8832-4ab0-b80e-e5830f099a95'::uuid)
              Rows Removed by Filter: 13400
              Buffers: shared hit=294
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=100)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.036 ms
Execution Time: 0.415 ms

#### chat_only

Limit  (cost=215.59..215.72 rows=51 width=153) (actual time=0.118..0.122 rows=51.00 loops=1)
  Buffers: shared hit=95
  ->  Sort  (cost=215.59..215.84 rows=100 width=153) (actual time=0.118..0.120 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=95
        ->  Bitmap Heap Scan on messages  (cost=5.06..212.27 rows=100 width=153) (actual time=0.014..0.104 rows=100.00 loops=1)
              Recheck Cond: (chat_id = 'e071a7a6-8832-4ab0-b80e-e5830f099a95'::uuid)
              Heap Blocks: exact=93
              Buffers: shared hit=95
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..5.04 rows=100 width=0) (actual time=0.004..0.004 rows=100.00 loops=1)
                    Index Cond: (chat_id = 'e071a7a6-8832-4ab0-b80e-e5830f099a95'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.056 ms
Execution Time: 0.131 ms

#### covering

Limit  (cost=0.41..6.15 rows=51 width=153) (actual time=0.006..0.045 rows=51.00 loops=1)
  Buffers: shared hit=4
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..11.66 rows=100 width=153) (actual time=0.006..0.041 rows=51.00 loops=1)
        Index Cond: (chat_id = 'e071a7a6-8832-4ab0-b80e-e5830f099a95'::uuid)
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=4
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.039 ms
Execution Time: 0.051 ms

### bg 10: first, после 0

#### psql

Limit  (cost=0.29..38.26 rows=10 width=153) (actual time=0.006..0.017 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..38.26 rows=10 width=153) (actual time=0.005..0.016 rows=10.00 loops=1)
        Index Cond: (chat_id = '1324dcfc-ac5c-4d64-b55b-d9f8febbff32'::uuid)
        Index Searches: 1
        Buffers: shared hit=12
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.047 ms
Execution Time: 0.022 ms

#### generic

Limit  (cost=0.29..53.03 rows=21 width=153) (actual time=0.009..0.020 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..537.81 rows=214 width=153) (actual time=0.008..0.019 rows=10.00 loops=1)
        Index Cond: (chat_id = $1)
        Index Searches: 1
        Buffers: shared hit=12
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                      Filter: (message_id = messages.id)
Planning Time: 0.003 ms
Execution Time: 0.026 ms

#### no-index

Limit  (cost=463.07..463.09 rows=10 width=153) (actual time=0.352..0.353 rows=10.00 loops=1)
  Buffers: shared hit=294
  ->  Sort  (cost=463.07..463.09 rows=10 width=153) (actual time=0.352..0.352 rows=10.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=294
        ->  Seq Scan on messages  (cost=0.00..462.90 rows=10 width=153) (actual time=0.026..0.349 rows=10.00 loops=1)
              Filter: (chat_id = '1324dcfc-ac5c-4d64-b55b-d9f8febbff32'::uuid)
              Rows Removed by Filter: 13490
              Buffers: shared hit=294
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.037 ms
Execution Time: 0.358 ms

#### chat_only

Limit  (cost=33.48..33.50 rows=10 width=153) (actual time=0.017..0.018 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Sort  (cost=33.48..33.50 rows=10 width=153) (actual time=0.017..0.017 rows=10.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=12
        ->  Index Scan using bench_variant on messages  (cost=0.29..33.31 rows=10 width=153) (actual time=0.004..0.015 rows=10.00 loops=1)
              Index Cond: (chat_id = '1324dcfc-ac5c-4d64-b55b-d9f8febbff32'::uuid)
              Index Searches: 1
              Buffers: shared hit=12
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.033 ms
Execution Time: 0.023 ms

#### covering

Limit  (cost=0.41..4.74 rows=10 width=153) (actual time=0.005..0.013 rows=10.00 loops=1)
  Buffers: shared hit=4
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..4.74 rows=10 width=153) (actual time=0.005..0.012 rows=10.00 loops=1)
        Index Cond: (chat_id = '1324dcfc-ac5c-4d64-b55b-d9f8febbff32'::uuid)
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=4
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.040 ms
Execution Time: 0.019 ms
