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

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

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

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

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

duration: 0.183 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 = '99ea412a-276b-4e62-817a-131787be68f4', $2 = '51'
Limit  (cost=0.29..9.61 rows=51 width=153) (actual time=0.066..0.141 rows=51.00 loops=1)
  Buffers: shared hit=10
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1828.70 rows=10000 width=153) (actual time=0.065..0.137 rows=51.00 loops=1)
        Index Cond: (chat_id = '99ea412a-276b-4e62-817a-131787be68f4'::uuid)
        Index Searches: 1
        Buffers: shared hit=10
        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.173 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 = '99ea412a-276b-4e62-817a-131787be68f4', $2 = '2026-10-31 20:25:58.972716+00', $3 = 'a8013679-c33e-427a-bd32-ce55583660ec', $4 = '51'
Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.033..0.139 rows=51.00 loops=1)
  Buffers: shared hit=50
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..218.46 rows=71 width=153) (actual time=0.032..0.135 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=50
        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.165 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 = '99ea412a-276b-4e62-817a-131787be68f4', $2 = '2026-10-31 20:26:12.424471+00', $3 = '1181b092-52d6-4ca4-be5f-638e84c56409', $4 = '51'
Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.033..0.129 rows=51.00 loops=1)
  Buffers: shared hit=48
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..218.46 rows=71 width=153) (actual time=0.032..0.125 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=48
        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.150 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 = '46b04d36-1a11-4041-9b6f-9851466b16d5', $2 = '51'
Limit  (cost=0.29..54.21 rows=51 width=153) (actual time=0.026..0.114 rows=51.00 loops=1)
  Buffers: shared hit=48
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1057.60 rows=1000 width=153) (actual time=0.025..0.111 rows=51.00 loops=1)
        Index Cond: (chat_id = '46b04d36-1a11-4041-9b6f-9851466b16d5'::uuid)
        Index Searches: 1
        Buffers: shared hit=48
        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.174 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 = '46b04d36-1a11-4041-9b6f-9851466b16d5', $2 = '2026-10-31 20:25:58.952532+00', $3 = '76134beb-2514-483c-9e57-35cca348654e', $4 = '51'
Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.034..0.135 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.46 rows=71 width=153) (actual time=0.033..0.131 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 1k: last, после 900

duration: 0.136 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 = '46b04d36-1a11-4041-9b6f-9851466b16d5', $2 = '2026-10-31 20:26:10.027592+00', $3 = 'd40867ec-a74c-405c-a995-63fd352c51eb', $4 = '51'
Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.032..0.108 rows=51.00 loops=1)
  Buffers: shared hit=48
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..218.46 rows=71 width=153) (actual time=0.031..0.105 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=48
        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.182 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 = '569aadba-e8bf-42b0-badc-792ce65b3d18', $2 = '51'
Limit  (cost=0.29..151.03 rows=51 width=153) (actual time=0.015..0.151 rows=51.00 loops=1)
  Buffers: shared hit=43
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..295.87 rows=100 width=153) (actual time=0.015..0.149 rows=51.00 loops=1)
        Index Cond: (chat_id = '569aadba-e8bf-42b0-badc-792ce65b3d18'::uuid)
        Index Searches: 1
        Buffers: shared hit=43
        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.086 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 = 'af5d41fb-ffe2-459e-b929-6a4ab3569116', $2 = '51'
Limit  (cost=0.29..38.26 rows=10 width=153) (actual time=0.010..0.081 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.010..0.080 rows=10.00 loops=1)
        Index Cond: (chat_id = 'af5d41fb-ffe2-459e-b929-6a4ab3569116'::uuid)
        Index Searches: 1
        Buffers: shared hit=12
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.002..0.002 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.68 rows=51 width=153) (actual time=0.007..0.054 rows=51.00 loops=1)
  Buffers: shared hit=50
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1411.46 rows=5000 width=153) (actual time=0.007..0.051 rows=51.00 loops=1)
        Index Cond: ((chat_id = '99ea412a-276b-4e62-817a-131787be68f4'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:25:58.972716+00'::timestamp with time zone, 'a8013679-c33e-427a-bd32-ce55583660ec'::uuid)))
        Index Searches: 1
        Buffers: shared hit=50
        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.052 ms
Execution Time: 0.063 ms

#### generic

Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.006..0.057 rows=51.00 loops=1)
  Buffers: shared hit=50
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..218.46 rows=71 width=153) (actual time=0.006..0.054 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=50
        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.006 ms
Execution Time: 0.065 ms

#### or-cursor

Limit  (cost=0.29..18.93 rows=51 width=153) (actual time=0.682..0.725 rows=51.00 loops=1)
  Buffers: shared hit=4713
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1828.70 rows=5000 width=153) (actual time=0.682..0.723 rows=51.00 loops=1)
        Index Cond: (chat_id = '99ea412a-276b-4e62-817a-131787be68f4'::uuid)
        Filter: ((created_at > '2026-10-31 20:25:58.972716+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:25:58.972716+00'::timestamp with time zone) AND (id > 'a8013679-c33e-427a-bd32-ce55583660ec'::uuid)))
        Rows Removed by Filter: 5000
        Index Searches: 1
        Buffers: shared hit=4713
        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.051 ms
Execution Time: 0.732 ms

#### no-index

Limit  (cost=772.06..772.19 rows=51 width=153) (actual time=3.998..4.002 rows=51.00 loops=1)
  Buffers: shared hit=294
  ->  Sort  (cost=772.06..784.56 rows=5000 width=153) (actual time=3.998..4.000 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 43kB
        Buffers: shared hit=294
        ->  Seq Scan on messages  (cost=0.00..605.25 rows=5000 width=153) (actual time=0.232..3.473 rows=5000.00 loops=1)
              Filter: ((chat_id = '99ea412a-276b-4e62-817a-131787be68f4'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:25:58.972716+00'::timestamp with time zone, 'a8013679-c33e-427a-bd32-ce55583660ec'::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.052 ms
Execution Time: 4.012 ms

#### chat_only

Limit  (cost=772.06..772.19 rows=51 width=153) (actual time=3.749..3.754 rows=51.00 loops=1)
  Buffers: shared hit=294
  ->  Sort  (cost=772.06..784.56 rows=5000 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: 43kB
        Buffers: shared hit=294
        ->  Seq Scan on messages  (cost=0.00..605.25 rows=5000 width=153) (actual time=0.228..3.243 rows=5000.00 loops=1)
              Filter: ((chat_id = '99ea412a-276b-4e62-817a-131787be68f4'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:25:58.972716+00'::timestamp with time zone, 'a8013679-c33e-427a-bd32-ce55583660ec'::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.061 ms
Execution Time: 3.763 ms

#### covering

Limit  (cost=0.41..6.19 rows=51 width=153) (actual time=0.006..0.045 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..567.41 rows=5000 width=153) (actual time=0.006..0.043 rows=51.00 loops=1)
        Index Cond: ((chat_id = '99ea412a-276b-4e62-817a-131787be68f4'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:25:58.972716+00'::timestamp with time zone, 'a8013679-c33e-427a-bd32-ce55583660ec'::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.045 ms
Execution Time: 0.051 ms

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

#### psql

Limit  (cost=0.29..96.04 rows=51 width=153) (actual time=0.007..0.048 rows=51.00 loops=1)
  Buffers: shared hit=47
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..940.94 rows=501 width=153) (actual time=0.006..0.046 rows=51.00 loops=1)
        Index Cond: ((chat_id = '46b04d36-1a11-4041-9b6f-9851466b16d5'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:25:58.952532+00'::timestamp with time zone, '76134beb-2514-483c-9e57-35cca348654e'::uuid)))
        Index Searches: 1
        Buffers: shared hit=47
        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.056 ms

#### generic

Limit  (cost=0.29..21.80 rows=7 width=153) (actual time=0.007..0.054 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.46 rows=71 width=153) (actual time=0.007..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=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)
Planning Time: 0.005 ms
Execution Time: 0.061 ms

#### or-cursor

Limit  (cost=0.29..107.92 rows=51 width=153) (actual time=0.080..0.117 rows=51.00 loops=1)
  Buffers: shared hit=514
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1057.62 rows=501 width=153) (actual time=0.080..0.115 rows=51.00 loops=1)
        Index Cond: (chat_id = '46b04d36-1a11-4041-9b6f-9851466b16d5'::uuid)
        Filter: ((created_at > '2026-10-31 20:25:58.952532+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:25:58.952532+00'::timestamp with time zone) AND (id > '76134beb-2514-483c-9e57-35cca348654e'::uuid)))
        Rows Removed by Filter: 500
        Index Searches: 1
        Buffers: shared hit=514
        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.073 ms
Execution Time: 0.125 ms

#### no-index

Limit  (cost=554.48..554.61 rows=51 width=153) (actual time=0.698..0.702 rows=51.00 loops=1)
  Buffers: shared hit=294
  ->  Sort  (cost=554.48..555.73 rows=501 width=153) (actual time=0.698..0.699 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.76 rows=501 width=153) (actual time=0.171..0.639 rows=500.00 loops=1)
              Filter: ((chat_id = '46b04d36-1a11-4041-9b6f-9851466b16d5'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:25:58.952532+00'::timestamp with time zone, '76134beb-2514-483c-9e57-35cca348654e'::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.053 ms
Execution Time: 0.708 ms

#### chat_only

Limit  (cost=351.64..351.77 rows=51 width=153) (actual time=0.473..0.476 rows=51.00 loops=1)
  Buffers: shared hit=285
  ->  Sort  (cost=351.64..352.89 rows=501 width=153) (actual time=0.473..0.474 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 39kB
        Buffers: shared hit=285
        ->  Bitmap Heap Scan on messages  (cost=15.91..334.93 rows=501 width=153) (actual time=0.098..0.423 rows=500.00 loops=1)
              Recheck Cond: (chat_id = '46b04d36-1a11-4041-9b6f-9851466b16d5'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-31 20:25:58.952532+00'::timestamp with time zone, '76134beb-2514-483c-9e57-35cca348654e'::uuid))
              Rows Removed by Filter: 500
              Heap Blocks: exact=283
              Buffers: shared hit=285
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..15.79 rows=1000 width=0) (actual time=0.022..0.022 rows=1000.00 loops=1)
                    Index Cond: (chat_id = '46b04d36-1a11-4041-9b6f-9851466b16d5'::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=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.041 ms
Execution Time: 0.484 ms

#### covering

Limit  (cost=0.41..6.27 rows=51 width=153) (actual time=0.008..0.047 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..57.94 rows=501 width=153) (actual time=0.008..0.044 rows=51.00 loops=1)
        Index Cond: ((chat_id = '46b04d36-1a11-4041-9b6f-9851466b16d5'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:25:58.952532+00'::timestamp with time zone, '76134beb-2514-483c-9e57-35cca348654e'::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.053 ms
Execution Time: 0.055 ms

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

#### psql

Limit  (cost=0.29..151.03 rows=51 width=153) (actual time=0.014..0.058 rows=51.00 loops=1)
  Buffers: shared hit=43
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..295.87 rows=100 width=153) (actual time=0.014..0.056 rows=51.00 loops=1)
        Index Cond: (chat_id = '569aadba-e8bf-42b0-badc-792ce65b3d18'::uuid)
        Index Searches: 1
        Buffers: shared hit=43
        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:
  Buffers: shared hit=3
Planning Time: 0.043 ms
Execution Time: 0.063 ms

#### generic

Limit  (cost=0.29..53.03 rows=21 width=153) (actual time=0.009..0.057 rows=51.00 loops=1)
  Buffers: shared hit=43
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..537.79 rows=214 width=153) (actual time=0.009..0.054 rows=51.00 loops=1)
        Index Cond: (chat_id = $1)
        Index Searches: 1
        Buffers: shared hit=43
        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.003 ms
Execution Time: 0.064 ms

#### no-index

Limit  (cost=467.57..467.70 rows=51 width=153) (actual time=0.392..0.395 rows=51.00 loops=1)
  Buffers: shared hit=294
  ->  Sort  (cost=467.57..467.82 rows=100 width=153) (actual time=0.392..0.393 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.012..0.377 rows=100.00 loops=1)
              Filter: (chat_id = '569aadba-e8bf-42b0-badc-792ce65b3d18'::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.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.050 ms
Execution Time: 0.403 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=85
  ->  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=85
        ->  Bitmap Heap Scan on messages  (cost=5.06..212.27 rows=100 width=153) (actual time=0.015..0.104 rows=100.00 loops=1)
              Recheck Cond: (chat_id = '569aadba-e8bf-42b0-badc-792ce65b3d18'::uuid)
              Heap Blocks: exact=83
              Buffers: shared hit=85
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..5.04 rows=100 width=0) (actual time=0.005..0.005 rows=100.00 loops=1)
                    Index Cond: (chat_id = '569aadba-e8bf-42b0-badc-792ce65b3d18'::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.038 ms
Execution Time: 0.129 ms

#### covering

Limit  (cost=0.41..6.15 rows=51 width=153) (actual time=0.006..0.048 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..11.66 rows=100 width=153) (actual time=0.006..0.045 rows=51.00 loops=1)
        Index Cond: (chat_id = '569aadba-e8bf-42b0-badc-792ce65b3d18'::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.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:
  Buffers: shared hit=3
Planning Time: 0.045 ms
Execution Time: 0.055 ms

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

#### psql

Limit  (cost=0.29..38.26 rows=10 width=153) (actual time=0.009..0.021 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.009..0.020 rows=10.00 loops=1)
        Index Cond: (chat_id = 'af5d41fb-ffe2-459e-b929-6a4ab3569116'::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.056 ms
Execution Time: 0.028 ms

#### generic

Limit  (cost=0.29..53.03 rows=21 width=153) (actual time=0.009..0.018 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.79 rows=214 width=153) (actual time=0.008..0.017 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.004 ms
Execution Time: 0.024 ms

#### no-index

Limit  (cost=463.07..463.09 rows=10 width=153) (actual time=0.324..0.325 rows=10.00 loops=1)
  Buffers: shared hit=294
  ->  Sort  (cost=463.07..463.09 rows=10 width=153) (actual time=0.324..0.324 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.022..0.322 rows=10.00 loops=1)
              Filter: (chat_id = 'af5d41fb-ffe2-459e-b929-6a4ab3569116'::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.033 ms
Execution Time: 0.329 ms

#### chat_only

Limit  (cost=33.48..33.50 rows=10 width=153) (actual time=0.019..0.020 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Sort  (cost=33.48..33.50 rows=10 width=153) (actual time=0.018..0.019 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.005..0.016 rows=10.00 loops=1)
              Index Cond: (chat_id = 'af5d41fb-ffe2-459e-b929-6a4ab3569116'::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.051 ms
Execution Time: 0.027 ms

#### covering

Limit  (cost=0.41..4.74 rows=10 width=153) (actual time=0.006..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.006..0.012 rows=10.00 loops=1)
        Index Cond: (chat_id = 'af5d41fb-ffe2-459e-b929-6a4ab3569116'::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.052 ms
Execution Time: 0.019 ms
