Проверять содержимое изображений: байты фильтра PNG и настоящее декодирование JPEG

This commit is contained in:
2026-10-31 04:06:38 +03:00
parent d812d8f8d8
commit de5efb4457
26 changed files with 1023 additions and 426 deletions
+35 -35
View File
@@ -1,5 +1,5 @@
# Индекс истории: EXPLAIN (ANALYZE, BUFFERS), объём данных в первой таблице вывода
# дата: 2026-10-30T20:07Z, чистый clone
# дата: 2026-10-31T01:05Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
$ make explain
@@ -7,22 +7,22 @@ $ make explain
== Объём данных
messages_total | messages_in_chat | table_size | index_size
----------------+------------------+------------+------------
101045 | 10000 | 11 MB | 9544 kB
91163 | 10000 | 10120 kB | 8968 kB
(1 row)
== 1. Первая страница (limit 50, запрос берёт 51 строку)
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..729.05 rows=51 width=112) (actual time=0.028..0.228 rows=51 loops=1)
Buffers: shared hit=63
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..142912.03 rows=10003 width=112) (actual time=0.023..0.219 rows=51 loops=1)
Limit (cost=0.42..726.39 rows=51 width=112) (actual time=0.031..0.253 rows=51 loops=1)
Buffers: shared hit=64
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..145123.89 rows=10195 width=112) (actual time=0.031..0.249 rows=51 loops=1)
Index Cond: (chat_id = '00000000-0000-4000-8000-000000000100'::uuid)
Buffers: shared hit=63
Buffers: shared hit=64
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.004..0.004 rows=1 loops=51)
Buffers: shared hit=54
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.002..0.002 rows=0 loops=51)
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.003..0.003 rows=0 loops=51)
Sort Key: a."position"
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=54
@@ -33,24 +33,24 @@ $ make explain
Index Cond: (message_id = messages.id)
Buffers: shared hit=51
Planning:
Buffers: shared hit=76
Planning Time: 0.174 ms
Execution Time: 0.255 ms
Buffers: shared hit=78
Planning Time: 0.235 ms
Execution Time: 0.286 ms
(22 rows)
== 2. Страница из середины истории: keyset после 5000-го сообщения
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..738.40 rows=51 width=112) (actual time=0.013..0.198 rows=51 loops=1)
Limit (cost=0.42..735.98 rows=51 width=112) (actual time=0.017..0.251 rows=51 loops=1)
Buffers: shared hit=59
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..101986.67 rows=7048 width=112) (actual time=0.013..0.196 rows=51 loops=1)
Index Cond: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW('2026-10-30 11:47:36.896462+00'::timestamp with time zone, '0c56411c-acb3-420b-bd20-5ac5ed7ab8a2'::uuid)))
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..98594.86 rows=6836 width=112) (actual time=0.017..0.248 rows=51 loops=1)
Index Cond: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW('2026-10-30 16:45:29.612108+00'::timestamp with time zone, '6dacd396-f0bd-431e-8871-9e3bb7792539'::uuid)))
Buffers: shared hit=59
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.003..0.003 rows=1 loops=51)
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.004..0.004 rows=1 loops=51)
Buffers: shared hit=51
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.002..0.002 rows=0 loops=51)
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.003..0.003 rows=0 loops=51)
Sort Key: a."position"
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=51
@@ -60,19 +60,19 @@ $ make explain
-> Bitmap Index Scan on message_attachments_pkey (cost=0.00..4.20 rows=6 width=0) (actual time=0.000..0.000 rows=0 loops=51)
Index Cond: (message_id = messages.id)
Buffers: shared hit=51
Planning Time: 0.045 ms
Execution Time: 0.232 ms
Planning Time: 0.052 ms
Execution Time: 0.291 ms
(20 rows)
== 3. Та же страница через OFFSET (так делать не стали)
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=71434.79..72163.43 rows=51 width=112) (actual time=5.082..5.132 rows=51 loops=1)
Buffers: shared hit=5552
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..142912.03 rows=10003 width=112) (actual time=0.012..5.035 rows=5051 loops=1)
Limit (cost=71174.26..71900.24 rows=51 width=112) (actual time=6.185..6.246 rows=51 loops=1)
Buffers: shared hit=5551
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..145123.89 rows=10195 width=112) (actual time=0.012..6.130 rows=5051 loops=1)
Index Cond: (chat_id = '00000000-0000-4000-8000-000000000100'::uuid)
Buffers: shared hit=5552
Buffers: shared hit=5551
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.001..0.001 rows=1 loops=5051)
Buffers: shared hit=5051
@@ -86,26 +86,26 @@ $ make explain
-> Bitmap Index Scan on message_attachments_pkey (cost=0.00..4.20 rows=6 width=0) (actual time=0.000..0.000 rows=0 loops=5051)
Index Cond: (message_id = messages.id)
Buffers: shared hit=5051
Planning Time: 0.032 ms
Execution Time: 5.152 ms
Planning Time: 0.038 ms
Execution Time: 6.266 ms
(20 rows)
== 4. Запрос из пункта 2 без индекса messages_chat_history_idx (DROP INDEX внутри транзакции с ROLLBACK)
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=3400.42..4104.22 rows=51 width=112) (actual time=3.126..3.183 rows=51 loops=1)
Buffers: shared hit=1454
-> Result (cost=3400.42..100662.45 rows=7048 width=112) (actual time=3.125..3.180 rows=51 loops=1)
Buffers: shared hit=1454
-> Sort (cost=3400.42..3418.04 rows=7048 width=80) (actual time=3.098..3.100 rows=51 loops=1)
Limit (cost=3088.42..3792.21 rows=51 width=112) (actual time=3.362..3.425 rows=51 loops=1)
Buffers: shared hit=1322
-> Result (cost=3088.42..97424.85 rows=6836 width=112) (actual time=3.362..3.422 rows=51 loops=1)
Buffers: shared hit=1322
-> Sort (cost=3088.42..3105.51 rows=6836 width=80) (actual time=3.343..3.345 rows=51 loops=1)
Sort Key: messages.created_at, messages.id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=1403
-> Seq Scan on messages (cost=0.00..3165.29 rows=7048 width=80) (actual time=1.800..2.874 rows=5000 loops=1)
Filter: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW('2026-10-30 11:47:36.896462+00'::timestamp with time zone, '0c56411c-acb3-420b-bd20-5ac5ed7ab8a2'::uuid)))
Rows Removed by Filter: 96045
Buffers: shared hit=1397
Buffers: shared hit=1271
-> Seq Scan on messages (cost=0.00..2860.35 rows=6836 width=80) (actual time=1.866..3.076 rows=5000 loops=1)
Filter: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW('2026-10-30 16:45:29.612108+00'::timestamp with time zone, '6dacd396-f0bd-431e-8871-9e3bb7792539'::uuid)))
Rows Removed by Filter: 86163
Buffers: shared hit=1265
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.001..0.001 rows=1 loops=51)
Buffers: shared hit=51
@@ -121,8 +121,8 @@ $ make explain
Buffers: shared hit=51
Planning:
Buffers: shared hit=4
Planning Time: 0.050 ms
Execution Time: 3.202 ms
Planning Time: 0.057 ms
Execution Time: 3.455 ms
(29 rows)