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

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

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

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

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

duration: 0.135 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 = 'c5a461fa-1df5-48fc-99f3-07c2b4fc839c', $2 = '51'
Limit  (cost=0.29..11.43 rows=51 width=153) (actual time=0.035..0.104 rows=51.00 loops=1)
  Buffers: shared hit=27
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1092.83 rows=5000 width=153) (actual time=0.034..0.100 rows=51.00 loops=1)
        Index Cond: (chat_id = 'c5a461fa-1df5-48fc-99f3-07c2b4fc839c'::uuid)
        Index Searches: 1
        Buffers: shared hit=27
        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 5k: middle, после 2500

duration: 0.130 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 = 'c5a461fa-1df5-48fc-99f3-07c2b4fc839c', $2 = '2026-10-31 20:33:47.697992+00', $3 = 'd0685e16-8d05-433e-94b2-52f7f33fc60d', $4 = '51'
Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.021..0.099 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.020..0.097 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 5k: last, после 4900

duration: 0.119 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 = 'c5a461fa-1df5-48fc-99f3-07c2b4fc839c', $2 = '2026-10-31 20:33:55.782721+00', $3 = '56aa58ee-98f4-437c-86bd-dc713751a74e', $4 = '51'
Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.024..0.088 rows=51.00 loops=1)
  Buffers: shared hit=53
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.023..0.085 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=53
        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.118 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 = '32b1f6b8-cb91-4797-90f1-11562fd0d15c', $2 = '51'
Limit  (cost=0.29..39.43 rows=51 width=153) (actual time=0.021..0.087 rows=51.00 loops=1)
  Buffers: shared hit=47
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..767.87 rows=1000 width=153) (actual time=0.021..0.084 rows=51.00 loops=1)
        Index Cond: (chat_id = '32b1f6b8-cb91-4797-90f1-11562fd0d15c'::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)

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

duration: 0.122 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 = '32b1f6b8-cb91-4797-90f1-11562fd0d15c', $2 = '2026-10-31 20:33:47.678256+00', $3 = '5b0bf7b2-94b1-471b-b626-e907cff5d820', $4 = '51'
Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.015..0.094 rows=51.00 loops=1)
  Buffers: shared hit=49
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.014..0.091 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=49
        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 = '32b1f6b8-cb91-4797-90f1-11562fd0d15c', $2 = '2026-10-31 20:33:54.608508+00', $3 = 'b1c48e4e-55f2-4bb1-9e3d-6cefb21aca9c', $4 = '51'
Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.023..0.104 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.022..0.101 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        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 100: first, после 0

duration: 0.168 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 = '31bc7aa7-0ed7-4867-b3f9-6a2f17caa6fb', $2 = '51'
Limit  (cost=0.29..154.67 rows=51 width=153) (actual time=0.017..0.139 rows=51.00 loops=1)
  Buffers: shared hit=47
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..302.99 rows=100 width=153) (actual time=0.016..0.136 rows=51.00 loops=1)
        Index Cond: (chat_id = '31bc7aa7-0ed7-4867-b3f9-6a2f17caa6fb'::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 10: first, после 0

duration: 0.088 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 = '2a802f02-74f4-4937-bd97-9c4045417c5e', $2 = '51'
Limit  (cost=37.85..37.87 rows=10 width=153) (actual time=0.077..0.078 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Sort  (cost=37.85..37.87 rows=10 width=153) (actual time=0.077..0.077 rows=10.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=12
        ->  Bitmap Heap Scan on messages  (cost=4.36..37.68 rows=10 width=153) (actual time=0.032..0.071 rows=10.00 loops=1)
              Recheck Cond: (chat_id = '2a802f02-74f4-4937-bd97-9c4045417c5e'::uuid)
              Heap Blocks: exact=10
              Buffers: shared hit=12
              ->  Bitmap Index Scan on ix_messages_chat_id_created_at_id  (cost=0.00..4.36 rows=10 width=0) (actual time=0.007..0.007 rows=10.00 loops=1)
                    Index Cond: (chat_id = '2a802f02-74f4-4937-bd97-9c4045417c5e'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
              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 5k: middle, после 2500

#### psql

Limit  (cost=0.29..18.59 rows=51 width=153) (actual time=0.007..0.056 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..897.48 rows=2500 width=153) (actual time=0.007..0.053 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'c5a461fa-1df5-48fc-99f3-07c2b4fc839c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:33:47.697992+00'::timestamp with time zone, 'd0685e16-8d05-433e-94b2-52f7f33fc60d'::uuid)))
        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:
  Buffers: shared hit=3
Planning Time: 0.053 ms
Execution Time: 0.063 ms

#### generic

Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.009..0.056 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.008..0.053 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.005 ms
Execution Time: 0.064 ms

#### or-cursor

Limit  (cost=0.29..22.57 rows=51 width=153) (actual time=0.354..0.397 rows=51.00 loops=1)
  Buffers: shared hit=2555
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1092.83 rows=2500 width=153) (actual time=0.354..0.394 rows=51.00 loops=1)
        Index Cond: (chat_id = 'c5a461fa-1df5-48fc-99f3-07c2b4fc839c'::uuid)
        Filter: ((created_at > '2026-10-31 20:33:47.697992+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:33:47.697992+00'::timestamp with time zone) AND (id > 'd0685e16-8d05-433e-94b2-52f7f33fc60d'::uuid)))
        Rows Removed by Filter: 2500
        Index Searches: 1
        Buffers: shared hit=2555
        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.042 ms
Execution Time: 0.402 ms

#### no-index

Limit  (cost=455.66..455.78 rows=51 width=153) (actual time=2.231..2.236 rows=51.00 loops=1)
  Buffers: shared hit=186
  ->  Sort  (cost=455.66..461.91 rows=2500 width=153) (actual time=2.231..2.233 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 43kB
        Buffers: shared hit=186
        ->  Seq Scan on messages  (cost=0.00..372.25 rows=2500 width=153) (actual time=0.181..1.941 rows=2500.00 loops=1)
              Filter: ((chat_id = 'c5a461fa-1df5-48fc-99f3-07c2b4fc839c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:33:47.697992+00'::timestamp with time zone, 'd0685e16-8d05-433e-94b2-52f7f33fc60d'::uuid)))
              Rows Removed by Filter: 6000
              Buffers: shared hit=186
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=2500)
                      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=2500)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.043 ms
Execution Time: 2.243 ms

#### chat_only

Limit  (cost=455.66..455.78 rows=51 width=153) (actual time=2.007..2.011 rows=51.00 loops=1)
  Buffers: shared hit=186
  ->  Sort  (cost=455.66..461.91 rows=2500 width=153) (actual time=2.007..2.009 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 43kB
        Buffers: shared hit=186
        ->  Seq Scan on messages  (cost=0.00..372.25 rows=2500 width=153) (actual time=0.147..1.756 rows=2500.00 loops=1)
              Filter: ((chat_id = 'c5a461fa-1df5-48fc-99f3-07c2b4fc839c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:33:47.697992+00'::timestamp with time zone, 'd0685e16-8d05-433e-94b2-52f7f33fc60d'::uuid)))
              Rows Removed by Filter: 6000
              Buffers: shared hit=186
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=2500)
                      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=2500)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.040 ms
Execution Time: 2.017 ms

#### covering

Limit  (cost=0.41..6.28 rows=51 width=153) (actual time=0.006..0.041 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..287.91 rows=2500 width=153) (actual time=0.006..0.039 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'c5a461fa-1df5-48fc-99f3-07c2b4fc839c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:33:47.697992+00'::timestamp with time zone, 'd0685e16-8d05-433e-94b2-52f7f33fc60d'::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.039 ms
Execution Time: 0.047 ms

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

#### psql

Limit  (cost=0.29..74.22 rows=51 width=153) (actual time=0.006..0.052 rows=51.00 loops=1)
  Buffers: shared hit=49
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..728.07 rows=502 width=153) (actual time=0.006..0.050 rows=51.00 loops=1)
        Index Cond: ((chat_id = '32b1f6b8-cb91-4797-90f1-11562fd0d15c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:33:47.678256+00'::timestamp with time zone, '5b0bf7b2-94b1-471b-b626-e907cff5d820'::uuid)))
        Index Searches: 1
        Buffers: shared hit=49
        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.048 ms
Execution Time: 0.059 ms

#### generic

Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.013..0.063 rows=51.00 loops=1)
  Buffers: shared hit=49
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.013..0.060 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=49
        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.009 ms
Execution Time: 0.075 ms

#### or-cursor

Limit  (cost=0.29..78.27 rows=51 width=153) (actual time=0.083..0.127 rows=51.00 loops=1)
  Buffers: shared hit=531
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..767.90 rows=502 width=153) (actual time=0.083..0.125 rows=51.00 loops=1)
        Index Cond: (chat_id = '32b1f6b8-cb91-4797-90f1-11562fd0d15c'::uuid)
        Filter: ((created_at > '2026-10-31 20:33:47.678256+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:33:47.678256+00'::timestamp with time zone) AND (id > '5b0bf7b2-94b1-471b-b626-e907cff5d820'::uuid)))
        Rows Removed by Filter: 500
        Index Searches: 1
        Buffers: shared hit=531
        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.049 ms
Execution Time: 0.134 ms

#### no-index

Limit  (cost=359.03..359.16 rows=51 width=153) (actual time=0.554..0.558 rows=51.00 loops=1)
  Buffers: shared hit=186
  ->  Sort  (cost=359.03..360.28 rows=502 width=153) (actual time=0.553..0.555 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 39kB
        Buffers: shared hit=186
        ->  Seq Scan on messages  (cost=0.00..342.28 rows=502 width=153) (actual time=0.128..0.504 rows=500.00 loops=1)
              Filter: ((chat_id = '32b1f6b8-cb91-4797-90f1-11562fd0d15c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:33:47.678256+00'::timestamp with time zone, '5b0bf7b2-94b1-471b-b626-e907cff5d820'::uuid)))
              Rows Removed by Filter: 8000
              Buffers: shared hit=186
              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.074 ms
Execution Time: 0.569 ms

#### chat_only

Limit  (cost=243.69..243.82 rows=51 width=153) (actual time=0.455..0.459 rows=51.00 loops=1)
  Buffers: shared hit=187
  ->  Sort  (cost=243.69..244.94 rows=502 width=153) (actual time=0.455..0.456 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 39kB
        Buffers: shared hit=187
        ->  Bitmap Heap Scan on messages  (cost=15.91..226.94 rows=502 width=153) (actual time=0.077..0.404 rows=500.00 loops=1)
              Recheck Cond: (chat_id = '32b1f6b8-cb91-4797-90f1-11562fd0d15c'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-31 20:33:47.678256+00'::timestamp with time zone, '5b0bf7b2-94b1-471b-b626-e907cff5d820'::uuid))
              Rows Removed by Filter: 500
              Heap Blocks: exact=184
              Buffers: shared hit=187
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..15.79 rows=1000 width=0) (actual time=0.014..0.014 rows=1000.00 loops=1)
                    Index Cond: (chat_id = '32b1f6b8-cb91-4797-90f1-11562fd0d15c'::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.045 ms
Execution Time: 0.467 ms

#### covering

Limit  (cost=0.41..6.26 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..57.98 rows=502 width=153) (actual time=0.006..0.045 rows=51.00 loops=1)
        Index Cond: ((chat_id = '32b1f6b8-cb91-4797-90f1-11562fd0d15c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:33:47.678256+00'::timestamp with time zone, '5b0bf7b2-94b1-471b-b626-e907cff5d820'::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.047 ms
Execution Time: 0.055 ms

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

#### psql

Limit  (cost=0.29..154.67 rows=51 width=153) (actual time=0.021..0.072 rows=51.00 loops=1)
  Buffers: shared hit=47
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..302.99 rows=100 width=153) (actual time=0.021..0.069 rows=51.00 loops=1)
        Index Cond: (chat_id = '31bc7aa7-0ed7-4867-b3f9-6a2f17caa6fb'::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)
Planning:
  Buffers: shared hit=3
Planning Time: 0.069 ms
Execution Time: 0.080 ms

#### generic

Limit  (cost=0.29..39.82 rows=14 width=153) (actual time=0.024..0.083 rows=51.00 loops=1)
  Buffers: shared hit=47
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..381.48 rows=135 width=153) (actual time=0.023..0.080 rows=51.00 loops=1)
        Index Cond: (chat_id = $1)
        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.094 ms

#### no-index

Limit  (cost=297.07..297.20 rows=51 width=153) (actual time=0.353..0.356 rows=51.00 loops=1)
  Buffers: shared hit=186
  ->  Sort  (cost=297.07..297.32 rows=100 width=153) (actual time=0.353..0.354 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=186
        ->  Seq Scan on messages  (cost=0.00..293.75 rows=100 width=153) (actual time=0.028..0.336 rows=100.00 loops=1)
              Filter: (chat_id = '31bc7aa7-0ed7-4867-b3f9-6a2f17caa6fb'::uuid)
              Rows Removed by Filter: 8400
              Buffers: shared hit=186
              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.035 ms
Execution Time: 0.362 ms

#### chat_only

Limit  (cost=172.68..172.80 rows=51 width=153) (actual time=0.138..0.142 rows=51.00 loops=1)
  Buffers: shared hit=83
  ->  Sort  (cost=172.68..172.93 rows=100 width=153) (actual time=0.138..0.140 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=83
        ->  Bitmap Heap Scan on messages  (cost=5.06..169.35 rows=100 width=153) (actual time=0.013..0.124 rows=100.00 loops=1)
              Recheck Cond: (chat_id = '31bc7aa7-0ed7-4867-b3f9-6a2f17caa6fb'::uuid)
              Heap Blocks: exact=81
              Buffers: shared hit=83
              ->  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 = '31bc7aa7-0ed7-4867-b3f9-6a2f17caa6fb'::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.149 ms

#### covering

Limit  (cost=0.41..6.15 rows=51 width=153) (actual time=0.004..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.004..0.045 rows=51.00 loops=1)
        Index Cond: (chat_id = '31bc7aa7-0ed7-4867-b3f9-6a2f17caa6fb'::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.035 ms
Execution Time: 0.053 ms

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

#### psql

Limit  (cost=37.85..37.87 rows=10 width=153) (actual time=0.035..0.036 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Sort  (cost=37.85..37.87 rows=10 width=153) (actual time=0.034..0.035 rows=10.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=12
        ->  Bitmap Heap Scan on messages  (cost=4.36..37.68 rows=10 width=153) (actual time=0.020..0.031 rows=10.00 loops=1)
              Recheck Cond: (chat_id = '2a802f02-74f4-4937-bd97-9c4045417c5e'::uuid)
              Heap Blocks: exact=10
              Buffers: shared hit=12
              ->  Bitmap Index Scan on ix_messages_chat_id_created_at_id  (cost=0.00..4.36 rows=10 width=0) (actual time=0.005..0.005 rows=10.00 loops=1)
                    Index Cond: (chat_id = '2a802f02-74f4-4937-bd97-9c4045417c5e'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
              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.053 ms
Execution Time: 0.049 ms

#### generic

Limit  (cost=0.29..39.82 rows=14 width=153) (actual time=0.010..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..381.48 rows=135 width=153) (actual time=0.010..0.020 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.029 ms

#### no-index

Limit  (cost=292.57..292.59 rows=10 width=153) (actual time=0.199..0.200 rows=10.00 loops=1)
  Buffers: shared hit=186
  ->  Sort  (cost=292.57..292.59 rows=10 width=153) (actual time=0.199..0.199 rows=10.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=186
        ->  Seq Scan on messages  (cost=0.00..292.40 rows=10 width=153) (actual time=0.007..0.196 rows=10.00 loops=1)
              Filter: (chat_id = '2a802f02-74f4-4937-bd97-9c4045417c5e'::uuid)
              Rows Removed by Filter: 8490
              Buffers: shared hit=186
              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.205 ms

#### chat_only

Limit  (cost=37.85..37.87 rows=10 width=153) (actual time=0.019..0.020 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Sort  (cost=37.85..37.87 rows=10 width=153) (actual time=0.019..0.019 rows=10.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=12
        ->  Bitmap Heap Scan on messages  (cost=4.36..37.68 rows=10 width=153) (actual time=0.007..0.016 rows=10.00 loops=1)
              Recheck Cond: (chat_id = '2a802f02-74f4-4937-bd97-9c4045417c5e'::uuid)
              Heap Blocks: exact=10
              Buffers: shared hit=12
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..4.36 rows=10 width=0) (actual time=0.002..0.002 rows=10.00 loops=1)
                    Index Cond: (chat_id = '2a802f02-74f4-4937-bd97-9c4045417c5e'::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=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.044 ms
Execution Time: 0.026 ms

#### covering

Limit  (cost=0.41..4.74 rows=10 width=153) (actual time=0.006..0.014 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.013 rows=10.00 loops=1)
        Index Cond: (chat_id = '2a802f02-74f4-4937-bd97-9c4045417c5e'::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.035 ms
Execution Time: 0.020 ms
