diff --git a/.dockerignore b/.dockerignore index bc16e556..665fdc00 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,3 +18,9 @@ __pycache__ node_modules evidence .DS_Store +student-tests +research +build +**/__pycache__ +**/*.py[cod] +*.log diff --git a/.env.example b/.env.example index 714c8076..16e2300b 100644 --- a/.env.example +++ b/.env.example @@ -9,3 +9,7 @@ DATABASE_URL_FILE=/run/secrets/database_url # Optional on 3, required use on 4 in lab2; Redis OR Valkey: SESSION_STORE=postgres CACHE_URL_FILE=/run/secrets/cache_url +# Published host port: lab1 may continue running on 8080. +HOST_PORT=8082 +PROFILE_CACHE_TTL_SECONDS=300 +DB_POOL_MAX=8 diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 8ceefa10..6ef13499 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,18 +1,20 @@ -## Лабораторная 2 +# ЛР 2 — БВТ2301 — Абдурашидов — на 5 -Автор / группа: TODO -Целевая оценка (3/4/5): TODO -Трек 5, если заявлен: TODO +Алихан Абдурашидов, БВТ2301. Целевая оценка 5, трек 2 — план запроса и восстановление. -Что реализовано и что перенесено: TODO -Команды запуска из чистого clone: TODO -Результаты проверок и ссылка на REPORT.md: TODO -Ограничения / известные ошибки: TODO -Источники и использование ИИ: TODO +Мессенджер из lab1 перенесён в PostgreSQL: пользователи, чаты, членство, сообщения и действующие сессии переживают рестарт. Добавлены scrypt, Basic-вход, opaque Bearer с expiry/logout, транзакционные миграции, пул соединений и Valkey-кэш публичных профилей с безопасным fallback. -- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. -- [ ] Контракт и публичные тесты не ослаблены. -- [ ] make check и make test выполнены; приложен реальный вывод. -- [ ] Сценарии отказов/безопасности своего уровня воспроизведены. -- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. -- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. +Docker Compose запускает весь стенд из репозитория. Исходные contracts/ и публичные tests/ не изменены. Команды: `make up`, `make check test student-test`, `make infra-test`, `make research`. Подробности запуска — SOLUTION.md, архитектура, доказательства и ограничения — REPORT.md. + +Проверено: публичные smoke/persistence, 11 собственных HTTP-тестов, гонки регистрации, 100 сообщений от 10 клиентов, SQL rollback, TTL 2 с, logout, остановка Valkey/PostgreSQL, чистый clone с отдельными volumes. + +Исследование: 100 000 сообщений, неравномерные чаты, 3 индекса, keyset/OFFSET, стоимость committed INSERT и WAL. Для чата 80 000 сообщений p95 поздней страницы снизился с 7,1933 до 0,2545 мс. Backup восстановлен в новый PostgreSQL-контейнер/volume; сверены все counts, 7 samples и SHA-256 всей истории. Запись после снимка ожидаемо не входит в восстановление. + +Источники: собственная lab1, контракт курса, Python stdlib, psycopg, redis-py и официальная документация; matplotlib/NumPy — только рисунок. Ограничения: локальный одиночный PostgreSQL, без HA/PITR; измерения SQL не являются HTTP capacity/SLA. + +- [x] Минимум lab1 сохранён с объявленным переходом на PostgreSQL/Bearer. +- [x] Контракт и публичные тесты не ослаблены. +- [x] Smoke и полный restart выполнены, вывод сохранён. +- [x] Критерии уровня 4 и трек 2 проверены воспроизводимыми сценариями. +- [x] Secrets, volumes, dump и credentials исключены из Git. +- [x] README задания сохранён; REPORT.md заполнен. diff --git a/.github/workflows/application.yml b/.github/workflows/application.yml new file mode 100644 index 00000000..c056554d --- /dev/null +++ b/.github/workflows/application.yml @@ -0,0 +1,19 @@ +name: Python PostgreSQL messenger +on: [push, pull_request] +permissions: + contents: read +jobs: + application: + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.13.12' + - run: python3 student-tests/clean_clone.py + - uses: actions/upload-artifact@v4 + if: always() + with: + name: lab2-evidence + path: evidence/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..bee77978 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.13.12-slim-bookworm@sha256:a58daefb915e1e03ad48f3ca4df8832065412c5c35cacb9d39f4229184de12b6 +ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1 APP_HOST=0.0.0.0 APP_PORT=8080 +WORKDIR /app +COPY requirements.txt /app/ +RUN pip install --no-cache-dir -r requirements.txt +COPY --chown=10001:10001 src/ /app/src/ +COPY --chown=10001:10001 migrations/ /app/migrations/ +USER 10001:10001 +EXPOSE 8080 +STOPSIGNAL SIGTERM +HEALTHCHECK --interval=5s --timeout=4s --start-period=10s --retries=3 CMD ["python", "src/app.py", "--healthcheck"] +ENTRYPOINT ["python", "src/app.py"] diff --git a/Makefile b/Makefile index 9ecb223e..ef4f5737 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ PYTHON ?= python3 -BASE_URL ?= http://localhost:8080 +BASE_URL ?= http://localhost:8082 CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) @@ -24,3 +24,26 @@ load: test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +.PHONY: bootstrap up down student-test infra-test research +bootstrap: + $(PYTHON) scripts/bootstrap.py + +up: bootstrap + docker compose up -d --build --wait + +down: + docker compose down + +student-test: + BASE_URL="$(BASE_URL)" $(PYTHON) -m unittest discover -s student-tests -p 'test_*.py' -v + +infra-test: + $(PYTHON) student-tests/infrastructure.py + +research: + $(PYTHON) research/run.py + +.PHONY: clean-clone-test +clean-clone-test: + $(PYTHON) student-tests/clean_clone.py diff --git a/PR_DESCRIPTION.md b/PR_DESCRIPTION.md new file mode 100644 index 00000000..6ef13499 --- /dev/null +++ b/PR_DESCRIPTION.md @@ -0,0 +1,20 @@ +# ЛР 2 — БВТ2301 — Абдурашидов — на 5 + +Алихан Абдурашидов, БВТ2301. Целевая оценка 5, трек 2 — план запроса и восстановление. + +Мессенджер из lab1 перенесён в PostgreSQL: пользователи, чаты, членство, сообщения и действующие сессии переживают рестарт. Добавлены scrypt, Basic-вход, opaque Bearer с expiry/logout, транзакционные миграции, пул соединений и Valkey-кэш публичных профилей с безопасным fallback. + +Docker Compose запускает весь стенд из репозитория. Исходные contracts/ и публичные tests/ не изменены. Команды: `make up`, `make check test student-test`, `make infra-test`, `make research`. Подробности запуска — SOLUTION.md, архитектура, доказательства и ограничения — REPORT.md. + +Проверено: публичные smoke/persistence, 11 собственных HTTP-тестов, гонки регистрации, 100 сообщений от 10 клиентов, SQL rollback, TTL 2 с, logout, остановка Valkey/PostgreSQL, чистый clone с отдельными volumes. + +Исследование: 100 000 сообщений, неравномерные чаты, 3 индекса, keyset/OFFSET, стоимость committed INSERT и WAL. Для чата 80 000 сообщений p95 поздней страницы снизился с 7,1933 до 0,2545 мс. Backup восстановлен в новый PostgreSQL-контейнер/volume; сверены все counts, 7 samples и SHA-256 всей истории. Запись после снимка ожидаемо не входит в восстановление. + +Источники: собственная lab1, контракт курса, Python stdlib, psycopg, redis-py и официальная документация; matplotlib/NumPy — только рисунок. Ограничения: локальный одиночный PostgreSQL, без HA/PITR; измерения SQL не являются HTTP capacity/SLA. + +- [x] Минимум lab1 сохранён с объявленным переходом на PostgreSQL/Bearer. +- [x] Контракт и публичные тесты не ослаблены. +- [x] Smoke и полный restart выполнены, вывод сохранён. +- [x] Критерии уровня 4 и трек 2 проверены воспроизводимыми сценариями. +- [x] Secrets, volumes, dump и credentials исключены из Git. +- [x] README задания сохранён; REPORT.md заполнен. diff --git a/REPORT.md b/REPORT.md index e42f2b05..6ae02c72 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,212 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор: Алихан Абдурашидов. Группа: БВТ2301. +- Целевая оценка: **5**, трек 2 «План запроса и восстановление». +- Основа: `mtusi2026/mtusi2026lab2`, commit `65003b61195361aac238f19c9bff0027ca58e09e`. +- Ветка решения: `solution/bvt2301-abdurashidov`. Проверенный commit записывается в `evidence/clean-clone.json`; хеши исходников измерений — в `evidence/research.json`. +- Дата проверок: 30 октября 2026 года. +- Источники и библиотеки: контракт курса, Python stdlib, psycopg 3, redis-py, официальная документация PostgreSQL/Docker/Valkey. matplotlib/NumPy нужны только для рисунка. Собственное Python-решение lab1 использовано как основа HTTP-транспорта; чужие студенческие решения не переносились. + +## Что выполнено + +1. Изучены новый README, COURSE, OpenAPI, семантика сессий, инфраструктурные роли и публичные проверки. Исходный `make check` прошёл; smoke на ещё отсутствовавшем lab2-сервере ожидаемо получил ошибку соединения. +2. До SQL-запросов описаны [таблицы, ограничения, индексы и транзакции](infra/SCHEMA.md). Добавлена версионируемая миграция `001_initial.sql`; повторный запуск проверяет SHA-256 и не дублирует данные. +3. Из lab1 перенесены строгий HTTP/JSON-транспорт, Unicode-валидация, единый формат ошибок, Request-Id и обработка остановки. Память процесса как источник данных заменена PostgreSQL. +4. Добавлены регистрация с scrypt, Basic-вход без JSON body, непрозрачные 256-битные Bearer-токены, абсолютный expiry и logout. Сессии сохраняются в PostgreSQL; X-User-Id не влияет на идентификацию. +5. Подготовлены Dockerfile и Compose с API, PostgreSQL и Valkey, именованным volume, локальными secret-файлами, healthchecks и закреплёнными версиями/digest. +6. Valkey используется как cache-aside неизменяемого публичного профиля с TTL. История и действительность сессии всегда читаются из PostgreSQL; отказ кэша допускает fallback. +7. Добавлены собственные HTTP-тесты, автоматические проверки отказов/expiry/rollback, публичный hook реального перезапуска всех трёх компонентов и проверка отдельного чистого clone. +8. Проведено исследование на трёх изолированных БД по 100 000 сообщений; сравниваются три индекса, keyset/OFFSET и стоимость committed INSERT. Backup восстановлен в отдельный новый PostgreSQL-контейнер с новым volume; сверена вся история. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Требуются Git, Docker с Compose, Python 3.11+ и make. Внешняя установленная БД не нужна. Исходный README задания, contracts/ и публичные tests/ сохранены. -## Архитектура и решения +```sh +# В корне checkout решения: +make up +make check test student-test +make infra-test +# Отдельно публичный persistence: +make test-persistence ACTION_SCRIPT=scripts/restart.sh +# Исследование: +make research +``` -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +`make up` вызывает `scripts/bootstrap.py`: создаются `.env` из примера и случайные локальные credentials только при их отсутствии. Затем `docker compose up -d --build --wait` собирает исходники и ждёт healthcheck. Миграции применяются при старте API до bind HTTP-порта. Оператор не создаёт таблицы вручную и не передаёт пароли в командной строке. + +Приложение слушает `0.0.0.0:8080` внутри контейнера, опубликовано только на `127.0.0.1:8082`. Другие сервисы не публикуют host-порты. 8082 выбран для сосуществования с lab1 на 8080; `make` передаёт BASE_URL явно. При смене HOST_PORT изменить также BASE_URL команд тестов. + +| Компонент | Закреплённая версия | Назначение | +| --- | --- | --- | +| API | Python 3.13.12 slim-bookworm, digest в Dockerfile | HTTP и драйверы, UID 10001, read-only rootfs | +| PostgreSQL | 17.9-bookworm, digest в compose.yaml | Источник истины; persistent postgres_data | +| Valkey | 8.1.3-alpine, digest в compose.yaml | Только профильный кэш; память 64 MB, allkeys-lru | +| psycopg / psycopg-binary | 3.3.6 | Параметризованный SQL и бинарный libpq | +| psycopg-pool | 3.3.1 | Ограниченный потокобезопасный пул | +| redis-py | 6.4.0 | Valkey protocol client | +| typing_extensions | 4.16.0 | Закреплённая зависимость пула | + +Все Python-зависимости приложения закреплены в `requirements.txt`. Dockerfile копирует только исходники и миграции; `.dockerignore` исключает Git, .env, secrets, backups, локальные окружения, тесты, исследования и bytecode. Build/runtime для компилируемого языка здесь неприменимы. + +### Конфигурация и секреты + +| Переменная | Реальное поведение | +| --- | --- | +| APP_HOST / APP_PORT | В Compose фиксированы 0.0.0.0 / 8080, внутренний HTTP bind | +| HOST_PORT | Host published port; по умолчанию 8082 | +| APP_ENV | Метка лабораторного окружения в .env; не переключает хранилище | +| SESSION_TTL_SECONDS | Абсолютный срок сессии, 3600 с по умолчанию; тест отдельно использует 2 с | +| SESSION_STORE | Поддерживается и проверяется postgres; Valkey не является хранилищем сессий | +| PROFILE_CACHE_TTL_SECONDS | TTL публичного профиля, по умолчанию 300 с | +| DB_POOL_MAX | Максимум 8 соединений по умолчанию; допустимо 1–32 | +| DATABASE_URL_FILE / CACHE_URL_FILE | DSN читаются из явно смонтированных `/run/secrets/...` | + +Папка secrets/ имеет режим 0700 на хосте; файлы читаемы сервисными UID только в тех контейнерах, куда явно смонтированы. Compose secrets локального стенда — файловые mounts, не шифрованное secret-хранилище. .env содержит настройки без credentials. Сами пароли/DSN не печатаются и не входят в Git или отчёт. + +```sh +# Остановка с сохранением volume и secret-файлов: +docker compose down +# Возобновление: +docker compose up -d --wait +# Намеренное удаление учебных данных только этого проекта: +docker compose down --volumes +``` + +Удаление volume не удаляет secret-файлы. Удаление/регенерация файлов при сохранённой БД не меняет её пароль автоматически. Секреты следует хранить вместе с инструкцией эксплуатации вне Git. + +## Архитектура и транзакции + +```text +Клиент → HTTP API (Python, один процесс, потоки) + ├→ PostgreSQL: пользователи, чаты, членство, история, + │ хеши сессий/expiry, cursor key, миграции + └→ Valkey: public User по user_id, TTL 300 с +``` + +Реализация хранит `users`, `chats`, `chat_members`, `messages`, `sessions`, `app_settings`, `schema_migrations`. PK/FK, уникальность username и CHECK обеспечивают базовые инварианты. У сообщения составной FK `(chat_id,sender_id)` на участника чата. Индекс членства `(user_id,chat_id)` поддерживает список доступных чатов. Схема и её обоснование сохранены отдельно до SQL-реализации. + +Регистрация атомарно вставляет пользователя, конфликт уникальности возвращает 409. Создание чата и всех участников — одна транзакция; неизвестный участник вызывает FK violation и полный rollback. Отправка сообщения обновляет `last_message_at` чата и вставляет сообщение в одной транзакции. Контекст соединения фиксирует COMMIT до возврата успешного HTTP-ответа; исключения откатывают операцию. SQL-параметры передаются драйверу отдельно от текста запроса. + +Пул: минимум 1, максимум 8 соединений, очередь не более 32 ожидающих, ожидание 2 с. Connect timeout 2 с, statement timeout 2 с, lock timeout 1 с, idle-in-transaction 5 с. Исчерпание пула/таймаут/потеря БД дают 503 без внутренних деталей. Потоки HTTP не являются отдельными независимыми источниками состояния. + +Миграции инициализируются под транзакционной advisory lock; применённые версии и SHA-256 хранятся в БД. Повторное применение не создаёт бизнес-данные. Изменение уже применённого файла обнаруживается; изменение схемы требует новой миграции. Все pending migrations одного запуска фиксируются вместе с созданием постоянного cursor key. + +### Пароли, сессии и доступ + +Пароли: scrypt N=16384, r=8, p=1, dklen=32, случайная соль 16 байт; параметры, соль и hash записаны вместе. Длина 12–128 Unicode code points; пароль кодируется в UTF-8 без усечения, двоеточие допускается. Сравнение constant-time. Для неизвестного username выполняется dummy KDF; текст и challenge 401 совпадают с неверным паролем существующего пользователя. + +`POST /auth/sessions` принимает Basic только в Authorization, без JSON body. Выдаётся `secrets.token_urlsafe(32)` — 256 бит случайности, не UUID и не username. В БД сохраняется SHA-256 токена, user_id и абсолютное expires_at по часам PostgreSQL. Здесь быстрый SHA-256 уместен для случайного высокоэнтропийного токена; пароль обрабатывается медленным password KDF. + +Каждый защищённый запрос проверяет Bearer и expiry в PostgreSQL. Logout удаляет только текущую сессию; другая сессия пользователя остаётся действительной. X-User-Id игнорируется. Сначала аутентификация, затем доступ; чужой/отсутствующий чат одинаково возвращает 404. Пароль, hash и session-данные в User не включаются. HTTP используется только для локального синтетического стенда. + +### Кэш и read-after-write + +Valkey хранит только четыре поля public User: id, username, display_name, created_at. В текущем контракте профили не изменяются, поэтому TTL 300 с и cache miss безопасны; при добавлении UPDATE/DELETE потребуется явная инвалидация/версионирование ключей. Ключ имеет namespace `profile:v1:`. + +Сначала PostgreSQL подтверждает сессию, затем разрешается cache hit профиля. Поэтому cache hit никогда не позволяет обойти expiry или logout. При недоступности Valkey timeout 200 мс, автоматических retry нет: профиль читается из PostgreSQL, cache set может быть пропущен. Потеря всего кэша не теряет сессии. Valkey persistence намеренно выключен: это восстанавливаемый кэш, а не единственное хранилище данных или сессий. + +Сообщения и история вообще не кэшируются. Следующий GET обращается к primary PostgreSQL после завершённого COMMIT записи. Членство тоже проверяется в БД. Это обеспечивает read-after-write без риска устаревшей страницы из Valkey. + +### Пагинация и часы + +Cursor подписан HMAC-SHA256 постоянным ключом из app_settings, включает resource scope и `(created_at,id)`. Он переживает рестарт API/БД, привязан к чату либо текущему пользователю для списка чатов. Неверная подпись/scope — 400; limit 1–100 может меняться между страницами. + +```sql +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = $1 AND (created_at, id) > ($2, $3) +ORDER BY created_at, id +LIMIT $4; +``` + +Рабочий индекс — `(chat_id,created_at,id)`. API читает limit+1 строку для определения next_cursor, возвращает только limit. OFFSET в API не используется. Timestamp сообщений монотонен внутри чата: row lock и `greatest(clock_timestamp(), last_message_at + interval '1 microsecond')` защищают порядок конкурентных вставок и откат часов. Время может слегка опережать физические часы; это не распределённые часы. + +Snapshot isolation между HTTP-страницами не обещается. Подтверждённое между страницами новое сообщение может попасть в последующие страницы. Отсутствие повторов гарантируется для неизменной истории. Для создаваемых конкурентно чатов порядок определяется парой, но страницы не являются снимком всех будущих COMMIT. + +### Health и остановка + +Live проверяет HTTP-процесс. Ready проверяет PostgreSQL; Valkey некритичен, поскольку есть fallback. SIGTERM прекращает приём новых запросов и дренирует потоки; socket timeout 3 с, общий срок 7,5 с с закрытием оставшихся сокетов, stop_grace_period 10 с. Содержимое сообщений, Authorization и DSN не логируются; лог содержит request_id, метод, статус и длительность. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | +| Критерий | Команда / сценарий | Результат | Доказательство | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +| Целостность контракта и схемы | make check validate PYTHON=.venv/bin/python | PASS | [contracts.txt](evidence/contracts.txt) | +| Обязательный lab2 API | make test | Публичный сценарий PASS | [smoke.txt](evidence/smoke.txt) | +| PostgreSQL, миграции, сессии переживают рестарт | make test-persistence ACTION_SCRIPT=scripts/restart.sh | Тот же токен читает прежние данные | [persistence.txt](evidence/persistence.txt) | +| Все реальные процессы/хранилища перезапущены | make infra-test | StartedAt изменился у API, PostgreSQL и Valkey; volumes не удалены | [infrastructure.json](evidence/infrastructure.json) | +| Valkey используется осмысленно | make infra-test | Есть profile keys с положительным TTL, только public поля; flush/restart восстанавливается | [infrastructure.json](evidence/infrastructure.json) | +| Параметризованные SQL, pool/timeouts | src/store.py; отказ PostgreSQL | 503 примерно за 2,01 с; анонимные запросы не доверены | [store.py](src/store.py), [infrastructure.json](evidence/infrastructure.json) | +| Гонка username | make student-test | 10 одновременных запросов → ровно 1×201 и 9×409 | [student-tests.txt](evidence/student-tests.txt) | +| Rollback составной операции | make student-test; make infra-test | Ошибка неизвестного участника не оставляет чат и членство; количества сверяются в SQL | [student-tests.txt](evidence/student-tests.txt), [clean-clone.json](evidence/clean-clone.json) | +| Конкурентная история | make student-test | 100 сообщений / 10 клиентов, без потерь и дублей | [student-tests.txt](evidence/student-tests.txt) | +| Expiry/logout/неверный пароль | make student-test; make infra-test | 401, отдельный TTL 2 с, другая сессия сохраняется | [student-tests.txt](evidence/student-tests.txt), [infrastructure.json](evidence/infrastructure.json) | +| Отказ Valkey и PostgreSQL | make infra-test | Cache fallback; PG→503; live 200 / ready 503; восстановление с прежней сессией | [infrastructure.json](evidence/infrastructure.json) | +| Индекс и EXPLAIN на ≥10 000 | make research | Измерены 100 000 сообщений; index condition по chat_id и tuple; 50 строк вместо фильтрации 79 900 | [explain-plans.json](evidence/explain-plans.json) | +| Чистый clone / новая БД / воспроизводимость | make clean-clone-test | Отдельные clone, порт, credentials, сеть и volumes; сборка, HTTP и infrastructure проверки | [clean-clone.json](evidence/clean-clone.json), [clean-clone-run.txt](evidence/clean-clone-run.txt) | +| Трек 2: ≥100 000, неравномерные чаты, ≥2 индекса/запроса | make research | 3 индекса × 4 чата × 2 запроса × 30 чтений; 10 write-batch на индекс | [research.json](evidence/research.json), [CSV](evidence/query-measurements.csv) | +| Backup → новое хранилище → сверка | make research | Новый контейнер и volume; 100 000 сообщений, все counts, 7 samples и полный SHA-256 совпали | [research.json](evidence/research.json) | -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +`make clean-clone-test` проверяет текущий commit, а не незакоммиченные изменения. Он удаляет только свои одноразовые тестовые volumes. CI workflow приложения добавлен отдельно; удалённый запуск GitHub Actions на Gitea не заявляется. -## Эксперименты и отказы +## Исследование и фактические результаты -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Гипотеза, полный метод и ограничения: [research/README.md](research/README.md). Аппаратная среда: Apple M4, 16 GiB RAM, Docker Desktop 29.4.3, Linux arm64. PostgreSQL и клиент имеют по 1 CPU / 512 MiB; Valkey — 0,5 CPU / 128 MiB, внутренний cache maxmemory 64 MB. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | +Исследуются три отдельных БД с одинаковой схемой и 100 000 сообщениями в чатах 80 000 / 15 000 / 4 000 / 1 000. Структурное отличие — только индекс: `chat_id`, составной keyset, либо covering с INCLUDE. После 3 прогревов выполняется по 30 последовательных чтений; сохраняются все времена и nearest-rank p50/p95/p99. Стоимость записи — 10 реальных транзакций по 1000 сообщений, время включает EXPLAIN ANALYZE и COMMIT. Write-строки после измерения удаляются только в исследовательской БД; VACUUM ANALYZE предшествует чтению. Это не нагрузочный HTTP-тест. + +| Индекс / запрос, чат 80 000 | p50, мс | p95, мс | p99, мс | +| --- | ---: | ---: | ---: | +| chat_id / keyset | 7,0761 | 7,1933 | 7,1945 | +| составной / keyset | 0,2249 | 0,2545 | 0,3508 | +| covering / keyset | 0,1993 | 0,3393 | 0,4040 | +| составной / OFFSET | 6,9243 | 12,6501 | 15,0282 | + +При поздней странице большого чата p95 составного keyset-запроса меньше исходного примерно в **28,26 раза**. EXPLAIN показывает: исходный индекс фильтрует 79 900 строк и сортирует, затрагивает 2017 shared hit blocks; составной — Index Scan с tuple в Index Cond, только 50 строк и 6 shared hit blocks. Covering даёт Index Only Scan, 0 heap fetches, 5 blocks, но это не гарантирует лучший p95 в короткой выборке. + +| Индекс | Все индексы messages, MiB | INSERT 1000 + COMMIT p50, мс | p95, мс | Средний WAL bytes | +| --- | ---: | ---: | ---: | ---: | +| chat_id | 5,203 | 9,6394 | 11,5286 | 379 956 | +| составной | 11,156 | 10,2890 | 12,1678 | 453 492 | +| covering | 27,609 | 13,9626 | 64,6203 | 708 616 | + +Выбран узкий составной индекс: он устраняет линейный просмотр поздней страницы, а covering увеличивает размер индексов примерно в 2,47 раза относительно составного и медиану write-batch примерно в 1,36 раза. Малые разницы субмиллисекундных чтений не перевешивают этот расход в данном стенде. OFFSET остаётся затратным даже с составным индексом. Планировщик и реальные buffers подтверждают механизм изменения; один факт наличия индекса этого не доказывает. + +![Индексы, задержки и стоимость записи](evidence/research.png) + +Backup custom-format: **3 004 503 bytes**, создание **0,5288 с**. Подготовка нового PostgreSQL с новым volume — **1,3216 с**, restore — **0,6135 с**, вместе **1,9351 с** без последующей проверки. После восстановления совпали counts всех семи таблиц, 7 выбранных сообщений и SHA-256 всей истории. Orphan messages — 0. Сессия и постоянный cursor key также присутствуют в снимке; функциональная сохранность реальных API-сессий отдельно проверена публичным restart-тестом. + +После dump в источник добавлена canary-запись. Источник содержит 100 001 сообщение, восстановленная БД — исходные 100 000. Это ожидаемая граница snapshot backup: подтверждённая после снимка запись в этот backup не входит. Нулевой RPO не заявляется; нужны WAL archive/PITR. Время здесь относится к маленькому локальному синтетическому набору, не к полному production RTO. + +Полные результаты: [таблица](evidence/research-summary.md), [raw JSON](evidence/research.json), [EXPLAIN](evidence/explain-plans.json), [CSV](evidence/query-measurements.csv). Dump хранится только локально в игнорируемом backups/; реальные рабочие данные не экспортировались. + +## Отказы, ограничения и дальнейшие шаги + +| Воздействие | Ожидание | Наблюдение | Потери / восстановление | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Valkey down | Fallback в PostgreSQL, logout действует | Сценарий 0,85 с, корректные profile/history и 401 после logout | Потерян только восстанавливаемый кэш | +| Valkey flush/restart | Перезаполнение профилей; revoked token не оживает | PASS | Пользователи/сообщения/сессии не теряются | +| PostgreSQL down | 503, ready 503, live 200, без доверия анониму | Первый защищённый запрос 2,01 с | Данные на volume; прежняя сессия работает после старта | +| API + все хранилища restart | Данные и неизменённый expiry сохраняются | Публичный persistence PASS; все StartedAt изменились | Воспроизведённое восстановление около 7,924 с | +| SESSION_TTL_SECONDS=2 | 401 после expiry | PASS в отдельном API-контейнере | Истекает только сессия | +| Неизвестный участник в создании чата | Полный rollback | HTTP и SQL-проверка отсутствия остаточных строк | Нет частичного чата | +| Restore снимка | Совпадение состояния на момент dump | 100 000 сообщений и полный checksum совпали | Canary после снимка отсутствует ожидаемо | -## Ограничения и следующие шаги +Ограничения: один локальный PostgreSQL, нет репликации/HA/PITR; именованный volume не защищает от потери диска/хоста. Synchronous COMMIT опирается на настройки PostgreSQL и честность fsync нижележащего хранилища. Истёкшие сессии логически не дают доступ, но фоновая очистка их строк пока не реализована. Кэш не измерялся на stampede/stale read — трек 1 не заявляется. Обновление/удаление пользователей, вложения, idempotency и балансировка относятся к следующим работам. -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Короткие последовательные SQL-измерения с прогретыми страницами и фиксированным порядком вариантов не дают production SLA, максимальный throughput или статистически устойчивые tail-percentiles. CPU-профилирование не проводилось; ограничения ресурсов заданы явно. Полноценный HTTP-сервер промышленного развёртывания потребовал бы дополнительных ограничений соединений и внешнего TLS. + +## Ответы на вопросы защиты + +- **COMMIT:** фиксирует всю транзакцию либо не фиксирует её при ошибке; возврат API происходит после завершения контекста транзакции. Durability зависит от synchronous_commit, fsync, full_page_writes, диска и сохранности volume. Ответ на неопределённый сетевой обрыв ещё не гарантирует отсутствие совершённой записи. +- **Сессии и Redis/Valkey:** сессии находятся в PostgreSQL, Valkey содержит только public User. Flush/restart теряет cache hit, но не токен/expiry. Logout всегда проверяется по БД. +- **Аутентификация и доступ:** Bearer устанавливает текущего пользователя; членство решает, может ли он читать/писать конкретный чат. Действительный токен постороннего всё равно получает 404. +- **Password KDF и checksum:** человеческий пароль низкоэнтропийный, нужен salted memory-cost KDF; случайный 256-битный токен можно искать по SHA-256. SHA-256 истории в backup-проверке обнаруживает отличие данных и не заменяет password KDF. + +## Первичные источники + +- [PostgreSQL transactions](https://www.postgresql.org/docs/17/tutorial-transactions.html), [EXPLAIN](https://www.postgresql.org/docs/17/using-explain.html), [SQL dump](https://www.postgresql.org/docs/17/backup-dump.html). +- [Psycopg connection pools](https://www.psycopg.org/psycopg3/docs/advanced/pool.html), [query parameters](https://www.psycopg.org/psycopg3/docs/basic/params.html). +- [Python scrypt](https://docs.python.org/3.13/library/hashlib.html#hashlib.scrypt), [Basic authentication RFC 7617](https://www.rfc-editor.org/rfc/rfc7617). +- [Valkey expiration](https://valkey.io/commands/expire/), [Dockerfile reference](https://docs.docker.com/reference/dockerfile/). diff --git a/SOLUTION.md b/SOLUTION.md new file mode 100644 index 00000000..0146053b --- /dev/null +++ b/SOLUTION.md @@ -0,0 +1,44 @@ +# Лабораторная 2 на Python + +Алихан Абдурашидов, БВТ2301. Целевая оценка 5, трек 2 — план запроса и восстановление. + +Нужны Docker Compose, Python 3.11+ и make. PostgreSQL/Valkey устанавливаются контейнерами из этого репозитория. Рабочий HTTP-порт по умолчанию 8082; первая лабораторная может продолжать работать на 8080. + +```sh +make up +# bootstrap автоматически создаст .env и secrets/ только при их отсутствии. +# up дождётся Docker healthcheck. +make check test student-test +make infra-test +``` + +`make infra-test` намеренно останавливает PostgreSQL и Valkey текущего проекта, проверяет 503/cache fallback/expiry и затем запускает публичный полный restart-сценарий. Учебные данные и именованный volume сохраняются. + +```sh +# Отдельно публичная проверка сохранности: +make test-persistence ACTION_SCRIPT=scripts/restart.sh +# Повторяемое исследование на изолированных синтетических БД: +make research +# Полная проверка схемы: +python3 -m venv .venv +.venv/bin/python -m pip install -r requirements-dev.txt +make validate PYTHON=.venv/bin/python +# Необязательный рисунок по сохранённым данным: +.venv/bin/python -m pip install -r research/requirements.txt +.venv/bin/python research/summarize.py +``` + +Для другого порта: задать `HOST_PORT` в `.env`, пересоздать API командой `docker compose up -d`, передавать тестам `BASE_URL=http://localhost:<порт>`. `APP_PORT=8080` — внутренний порт контейнера. Для короткого TTL используется отдельный тестовый контейнер; обычный стенд оставляет SESSION_TTL_SECONDS=3600. + +```sh +# Корректная остановка с сохранением данных: +docker compose down +# Повторный запуск с тем же volume: +docker compose up -d --wait +# Только если нужно намеренно уничтожить все учебные данные этого проекта: +docker compose down --volumes +``` + +Секреты не печатаются и не коммитятся. Не удалять secrets/ при сохранённом volume: смена пароля в файлах не меняет пароль уже инициализированной PostgreSQL. `make clean-clone-test` проверяет именно текущий commit в отдельном clone, со своими секретами, портом и временными volumes; незакоммиченные изменения в этот clone не попадают. + +Схема до реализации запросов: [infra/SCHEMA.md](infra/SCHEMA.md). Подробный отчёт: [REPORT.md](REPORT.md). Методика исследования: [research/README.md](research/README.md). diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..f61d56e7 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,73 @@ +name: mtusi-lab2 +services: + api: + build: . + image: mtusi-messenger:lab2 + environment: + APP_HOST: 0.0.0.0 + APP_PORT: 8080 + APP_ENV: ${APP_ENV:-lab} + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + SESSION_STORE: postgres + PROFILE_CACHE_TTL_SECONDS: ${PROFILE_CACHE_TTL_SECONDS:-300} + DB_POOL_MAX: ${DB_POOL_MAX:-8} + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + ports: + - "127.0.0.1:${HOST_PORT:-8082}:8080" + secrets: [database_url, cache_url] + depends_on: + postgres: + condition: service_healthy + valkey: + condition: service_started + read_only: true + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + cpus: 1 + mem_limit: 512m + pids_limit: 256 + stop_grace_period: 10s + postgres: + image: postgres:17.9-bookworm@sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641 + environment: + POSTGRES_DB: messenger + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/db_password + secrets: [db_password] + volumes: [postgres_data:/var/lib/postgresql/data] + healthcheck: + test: [CMD-SHELL, "pg_isready -U messenger -d messenger"] + interval: 2s + timeout: 2s + retries: 20 + command: [postgres, -c, shared_buffers=128MB, -c, max_connections=40] + cpus: 1 + mem_limit: 512m + pids_limit: 128 + stop_grace_period: 10s + valkey: + image: valkey/valkey:8.1.3-alpine@sha256:d827e7f7552cdee40cc7482dbae9da020f42bc47669af6f71182a4ef76a22773 + command: [valkey-server, /run/secrets/valkey_config] + secrets: + - source: valkey_config + target: valkey_config + healthcheck: + test: [CMD-SHELL, "valkey-cli ping 2>&1 | grep -q NOAUTH"] + interval: 2s + timeout: 2s + retries: 15 + cpus: 0.5 + mem_limit: 128m + pids_limit: 64 +volumes: + postgres_data: +secrets: + db_password: + file: ./secrets/db_password + database_url: + file: ./secrets/database_url + cache_url: + file: ./secrets/cache_url + valkey_config: + file: ./secrets/valkey.conf diff --git a/evidence/contracts.txt b/evidence/contracts.txt new file mode 100644 index 00000000..ed9cd475 --- /dev/null +++ b/evidence/contracts.txt @@ -0,0 +1,4 @@ +.venv/bin/python scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +.venv/bin/python scripts/validate_openapi.py +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. diff --git a/evidence/explain-plans.json b/evidence/explain-plans.json new file mode 100644 index 00000000..ee4de104 --- /dev/null +++ b/evidence/explain-plans.json @@ -0,0 +1,2252 @@ +[ + { + "index": "chat_only", + "chat_size": 80000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4583.62, + "Total Cost": 4583.74, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 6.726, + "Actual Total Time": 6.732, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4583.62, + "Total Cost": 4623.51, + "Plan Rows": 15957, + "Plan Width": 162, + "Actual Startup Time": 6.726, + "Actual Total Time": 6.728, + "Actual Rows": 50, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 4053.53, + "Plan Rows": 15957, + "Plan Width": 162, + "Actual Startup Time": 6.692, + "Actual Total Time": 6.715, + "Actual Rows": 100, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000001'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0799+00'::timestamp with time zone, '4c258fbb-7ab4-ca0e-1a48-23111d989887'::uuid))", + "Rows Removed by Filter": 79900, + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.044, + "Triggers": [], + "Execution Time": 6.74 + } + }, + { + "index": "chat_only", + "chat_size": 80000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 16971.36, + "Total Cost": 16971.49, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 51.018, + "Actual Total Time": 51.027, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 1742, + "Temp Written Blocks": 1747, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 16771.61, + "Total Cost": 16972.2, + "Plan Rows": 80233, + "Plan Width": 162, + "Actual Startup Time": 39.315, + "Actual Total Time": 48.181, + "Actual Rows": 79950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "external merge", + "Sort Space Used": 13936, + "Sort Space Type": "Disk", + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 1742, + "Temp Written Blocks": 1747, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 3652.37, + "Plan Rows": 80233, + "Plan Width": 162, + "Actual Startup Time": 0.01, + "Actual Total Time": 7.926, + "Actual Rows": 80000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000001'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.072, + "Triggers": [], + "Execution Time": 51.932 + } + }, + { + "index": "chat_only", + "chat_size": 15000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 779.4, + "Total Cost": 779.52, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 1.266, + "Actual Total Time": 1.271, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 779.4, + "Total Cost": 781.27, + "Plan Rows": 747, + "Plan Width": 162, + "Actual Startup Time": 1.265, + "Actual Total Time": 1.267, + "Actual Rows": 50, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 754.59, + "Plan Rows": 747, + "Plan Width": 162, + "Actual Startup Time": 1.243, + "Actual Total Time": 1.255, + "Actual Rows": 100, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000002'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0949+00'::timestamp with time zone, '83309d4e-6a9c-bf24-ec2c-d0ad8d47e31e'::uuid))", + "Rows Removed by Filter": 14900, + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.074, + "Triggers": [], + "Execution Time": 1.279 + } + }, + { + "index": "chat_only", + "chat_size": 15000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 1743.69, + "Total Cost": 1743.69, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 6.012, + "Actual Total Time": 6.02, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 328, + "Temp Written Blocks": 329, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 1706.66, + "Total Cost": 1743.69, + "Plan Rows": 14813, + "Plan Width": 162, + "Actual Startup Time": 4.225, + "Actual Total Time": 5.504, + "Actual Rows": 14950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "external merge", + "Sort Space Used": 2624, + "Sort Space Type": "Disk", + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 328, + "Temp Written Blocks": 329, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 680.52, + "Plan Rows": 14813, + "Plan Width": 162, + "Actual Startup Time": 0.006, + "Actual Total Time": 1.521, + "Actual Rows": 15000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000002'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.027, + "Triggers": [], + "Execution Time": 6.17 + } + }, + { + "index": "chat_only", + "chat_size": 4000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 208.17, + "Total Cost": 208.27, + "Plan Rows": 43, + "Plan Width": 162, + "Actual Startup Time": 0.346, + "Actual Total Time": 0.352, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 208.17, + "Total Cost": 208.27, + "Plan Rows": 43, + "Plan Width": 162, + "Actual Startup Time": 0.346, + "Actual Total Time": 0.348, + "Actual Rows": 50, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 207.0, + "Plan Rows": 43, + "Plan Width": 162, + "Actual Startup Time": 0.324, + "Actual Total Time": 0.336, + "Actual Rows": 100, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000003'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0989+00'::timestamp with time zone, '3ca7f544-85f6-3d47-6ce5-72ab53f46142'::uuid))", + "Rows Removed by Filter": 3900, + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.077, + "Triggers": [], + "Execution Time": 0.36 + } + }, + { + "index": "chat_only", + "chat_size": 4000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 435.26, + "Total Cost": 435.38, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.976, + "Actual Total Time": 0.982, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 425.51, + "Total Cost": 435.48, + "Plan Rows": 3987, + "Plan Width": 162, + "Actual Startup Time": 0.671, + "Actual Total Time": 0.84, + "Actual Rows": 3950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 1128, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 187.06, + "Plan Rows": 3987, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.364, + "Actual Rows": 4000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000003'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.019, + "Triggers": [], + "Execution Time": 0.989 + } + }, + { + "index": "chat_only", + "chat_size": 1000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 53.06, + "Total Cost": 53.06, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.101, + "Actual Total Time": 0.107, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 53.06, + "Total Cost": 53.06, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.101, + "Actual Total Time": 0.103, + "Actual Rows": 50, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 53.05, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.08, + "Actual Total Time": 0.092, + "Actual Rows": 100, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000004'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0999+00'::timestamp with time zone, '7fa13929-f6d0-fc18-76f9-46306abe42ca'::uuid))", + "Rows Removed by Filter": 900, + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.067, + "Triggers": [], + "Execution Time": 0.115 + } + }, + { + "index": "chat_only", + "chat_size": 1000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 98.42, + "Total Cost": 98.54, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.298, + "Actual Total Time": 0.304, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 96.17, + "Total Cost": 98.58, + "Plan Rows": 967, + "Plan Width": 162, + "Actual Startup Time": 0.163, + "Actual Total Time": 0.242, + "Actual Rows": 950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 282, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 48.21, + "Plan Rows": 967, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.091, + "Actual Rows": 1000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000004'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.019, + "Triggers": [], + "Execution Time": 0.312 + } + }, + { + "index": "keyset", + "chat_size": 80000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 16.91, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.014, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 5376.21, + "Plan Rows": 16299, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.01, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000001'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0799+00'::timestamp with time zone, '4c258fbb-7ab4-ca0e-1a48-23111d989887'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.019, + "Triggers": [], + "Execution Time": 0.02 + } + }, + { + "index": "keyset", + "chat_size": 80000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 9467.91, + "Total Cost": 9468.02, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 11.335, + "Actual Total Time": 11.345, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 2524, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 9467.91, + "Plan Rows": 79610, + "Plan Width": 162, + "Actual Startup Time": 0.008, + "Actual Total Time": 8.54, + "Actual Rows": 79950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000001'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 2524, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.092, + "Triggers": [], + "Execution Time": 11.357 + } + }, + { + "index": "keyset", + "chat_size": 15000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 79.7, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.014, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 1218.15, + "Plan Rows": 768, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.01, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000002'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0949+00'::timestamp with time zone, '83309d4e-6a9c-bf24-ec2c-d0ad8d47e31e'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.049, + "Triggers": [], + "Execution Time": 0.022 + } + }, + { + "index": "keyset", + "chat_size": 15000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4412.7, + "Total Cost": 4412.83, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 7.498, + "Actual Total Time": 7.508, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 476, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 328, + "Temp Written Blocks": 329, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4375.45, + "Total Cost": 4414.49, + "Plan Rows": 15617, + "Plan Width": 162, + "Actual Startup Time": 5.713, + "Actual Total Time": 6.979, + "Actual Rows": 14950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "external merge", + "Sort Space Used": 2624, + "Sort Space Type": "Disk", + "Shared Hit Blocks": 476, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 328, + "Temp Written Blocks": 329, + "Plans": [ + { + "Node Type": "Bitmap Heap Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 653.45, + "Total Cost": 3287.66, + "Plan Rows": 15617, + "Plan Width": 162, + "Actual Startup Time": 0.546, + "Actual Total Time": 1.806, + "Actual Rows": 15000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '00000000-0000-0000-0000-000000000002'::uuid)", + "Rows Removed by Index Recheck": 0, + "Exact Heap Blocks": 366, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 476, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Bitmap Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Index Name": "messages_chat_created_id_idx", + "Startup Cost": 0.0, + "Total Cost": 649.54, + "Plan Rows": 15617, + "Plan Width": 0, + "Actual Startup Time": 0.503, + "Actual Total Time": 0.503, + "Actual Rows": 15000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000002'::uuid)", + "Shared Hit Blocks": 110, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.071, + "Triggers": [], + "Execution Time": 7.708 + } + }, + { + "index": "keyset", + "chat_size": 4000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 79.8, + "Plan Rows": 41, + "Plan Width": 162, + "Actual Startup Time": 0.006, + "Actual Total Time": 0.014, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 79.8, + "Plan Rows": 41, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.01, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000003'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0989+00'::timestamp with time zone, '3ca7f544-85f6-3d47-6ce5-72ab53f46142'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.081, + "Triggers": [], + "Execution Time": 0.021 + } + }, + { + "index": "keyset", + "chat_size": 4000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2991.44, + "Total Cost": 2991.44, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 1.111, + "Actual Total Time": 1.117, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 131, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2982.0, + "Total Cost": 2991.44, + "Plan Rows": 3777, + "Plan Width": 162, + "Actual Startup Time": 0.79, + "Actual Total Time": 0.976, + "Actual Rows": 3950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 1128, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 131, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Bitmap Heap Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 161.69, + "Total Cost": 2757.59, + "Plan Rows": 3777, + "Plan Width": 162, + "Actual Startup Time": 0.152, + "Actual Total Time": 0.46, + "Actual Rows": 4000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '00000000-0000-0000-0000-000000000003'::uuid)", + "Rows Removed by Index Recheck": 0, + "Exact Heap Blocks": 99, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 131, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Bitmap Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Index Name": "messages_chat_created_id_idx", + "Startup Cost": 0.0, + "Total Cost": 160.74, + "Plan Rows": 3777, + "Plan Width": 0, + "Actual Startup Time": 0.139, + "Actual Total Time": 0.139, + "Actual Rows": 4000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000003'::uuid)", + "Shared Hit Blocks": 32, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.068, + "Triggers": [], + "Execution Time": 1.129 + } + }, + { + "index": "keyset", + "chat_size": 1000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 8.44, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.013, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 8.44, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.009, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000004'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0999+00'::timestamp with time zone, '7fa13929-f6d0-fc18-76f9-46306abe42ca'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.061, + "Triggers": [], + "Execution Time": 0.019 + } + }, + { + "index": "keyset", + "chat_size": 1000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 1370.91, + "Total Cost": 1447.05, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.128, + "Actual Total Time": 0.136, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 33, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 1518.62, + "Plan Rows": 997, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.101, + "Actual Rows": 950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000004'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 33, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.062, + "Triggers": [], + "Execution Time": 0.145 + } + }, + { + "index": "covering", + "chat_size": 80000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 7.34, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.006, + "Actual Total Time": 0.015, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 2212.08, + "Plan Rows": 15983, + "Plan Width": 162, + "Actual Startup Time": 0.006, + "Actual Total Time": 0.011, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000001'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0799+00'::timestamp with time zone, '4c258fbb-7ab4-ca0e-1a48-23111d989887'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.035, + "Triggers": [], + "Execution Time": 0.024 + } + }, + { + "index": "covering", + "chat_size": 80000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 10853.18, + "Total Cost": 10859.97, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 11.598, + "Actual Total Time": 11.608, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 2002, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 10891.62, + "Plan Rows": 80183, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 8.823, + "Actual Rows": 79950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000001'::uuid)", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 2002, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.04, + "Triggers": [], + "Execution Time": 11.616 + } + }, + { + "index": "covering", + "chat_size": 15000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 7.36, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.014, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 103.22, + "Plan Rows": 740, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.01, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000002'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0949+00'::timestamp with time zone, '83309d4e-6a9c-bf24-ec2c-d0ad8d47e31e'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.043, + "Triggers": [], + "Execution Time": 0.021 + } + }, + { + "index": "covering", + "chat_size": 15000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2024.08, + "Total Cost": 2029.92, + "Plan Rows": 43, + "Plan Width": 162, + "Actual Startup Time": 2.174, + "Actual Total Time": 2.182, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 378, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 2029.92, + "Plan Rows": 14943, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 1.656, + "Actual Rows": 14950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000002'::uuid)", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 378, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.048, + "Triggers": [], + "Execution Time": 2.19 + } + }, + { + "index": "covering", + "chat_size": 4000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 9.32, + "Plan Rows": 45, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.013, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 9.32, + "Plan Rows": 45, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.009, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000003'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0989+00'::timestamp with time zone, '3ca7f544-85f6-3d47-6ce5-72ab53f46142'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.026, + "Triggers": [], + "Execution Time": 0.019 + } + }, + { + "index": "covering", + "chat_size": 4000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 514.62, + "Total Cost": 514.76, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.605, + "Actual Total Time": 0.614, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 103, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 514.62, + "Plan Rows": 3783, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.474, + "Actual Rows": 3950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000003'::uuid)", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 103, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.048, + "Triggers": [], + "Execution Time": 0.625 + } + }, + { + "index": "covering", + "chat_size": 1000, + "query": "keyset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 4.44, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.014, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 4.44, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.01, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000004'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0999+00'::timestamp with time zone, '7fa13929-f6d0-fc18-76f9-46306abe42ca'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.052, + "Triggers": [], + "Execution Time": 0.02 + } + }, + { + "index": "covering", + "chat_size": 1000, + "query": "offset", + "explain_analyze_buffers": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 125.16, + "Total Cost": 132.09, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.143, + "Actual Total Time": 0.151, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 28, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 151.49, + "Plan Rows": 1090, + "Plan Width": 162, + "Actual Startup Time": 0.003, + "Actual Total Time": 0.116, + "Actual Rows": 950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000004'::uuid)", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 28, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.039, + "Triggers": [], + "Execution Time": 0.157 + } + } +] diff --git a/evidence/infrastructure-run.txt b/evidence/infrastructure-run.txt new file mode 100644 index 00000000..c4cfa5da --- /dev/null +++ b/evidence/infrastructure-run.txt @@ -0,0 +1,49 @@ +{ + "date_utc": "2026-10-30T11:00:43.989800+00:00", + "checks": { + "valkey_really_caches_profiles": { + "cached_profiles": 48, + "all_profiles_public_fields_only": true, + "positive_ttl": true + }, + "password_kdf_and_hashed_tokens": { + "all_passwords_scrypt": true, + "all_token_digests_32_bytes": true + }, + "cache_outage": { + "fallback_profile_and_history": true, + "anonymous_rejected": true, + "logout_effective": true, + "scenario_seconds": 0.85 + }, + "cache_restart_no_revoked_session_resurrection": true, + "cache_flush_repopulates_from_postgres": true, + "postgres_outage": { + "authenticated_request_503_seconds": 2.01, + "live_200": true, + "ready_503": true, + "no_anonymous_trust": true + }, + "postgres_recovery_same_session": true, + "expiry_ttl_2_seconds": true, + "repeat_migration_does_not_duplicate_data": { + "migrations": 1, + "users": 50 + }, + "full_stack_persistence": { + "api_and_all_stores_restarted": true, + "before": { + "api": "2026-10-30T10:58:17.679191091Z", + "postgres": "2026-10-30T11:00:54.063549678Z", + "valkey": "2026-10-30T11:00:46.458543341Z" + }, + "after": { + "api": "2026-10-30T11:01:04.399852418Z", + "postgres": "2026-10-30T11:01:04.247473126Z", + "valkey": "2026-10-30T11:01:04.435663252Z" + }, + "elapsed_seconds": 7.924 + } + }, + "status": "PASS" +} diff --git a/evidence/infrastructure.json b/evidence/infrastructure.json new file mode 100644 index 00000000..c4cfa5da --- /dev/null +++ b/evidence/infrastructure.json @@ -0,0 +1,49 @@ +{ + "date_utc": "2026-10-30T11:00:43.989800+00:00", + "checks": { + "valkey_really_caches_profiles": { + "cached_profiles": 48, + "all_profiles_public_fields_only": true, + "positive_ttl": true + }, + "password_kdf_and_hashed_tokens": { + "all_passwords_scrypt": true, + "all_token_digests_32_bytes": true + }, + "cache_outage": { + "fallback_profile_and_history": true, + "anonymous_rejected": true, + "logout_effective": true, + "scenario_seconds": 0.85 + }, + "cache_restart_no_revoked_session_resurrection": true, + "cache_flush_repopulates_from_postgres": true, + "postgres_outage": { + "authenticated_request_503_seconds": 2.01, + "live_200": true, + "ready_503": true, + "no_anonymous_trust": true + }, + "postgres_recovery_same_session": true, + "expiry_ttl_2_seconds": true, + "repeat_migration_does_not_duplicate_data": { + "migrations": 1, + "users": 50 + }, + "full_stack_persistence": { + "api_and_all_stores_restarted": true, + "before": { + "api": "2026-10-30T10:58:17.679191091Z", + "postgres": "2026-10-30T11:00:54.063549678Z", + "valkey": "2026-10-30T11:00:46.458543341Z" + }, + "after": { + "api": "2026-10-30T11:01:04.399852418Z", + "postgres": "2026-10-30T11:01:04.247473126Z", + "valkey": "2026-10-30T11:01:04.435663252Z" + }, + "elapsed_seconds": 7.924 + } + }, + "status": "PASS" +} diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..79082d17 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,3 @@ +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8082" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/query-measurements.csv b/evidence/query-measurements.csv new file mode 100644 index 00000000..5e5344f1 --- /dev/null +++ b/evidence/query-measurements.csv @@ -0,0 +1,25 @@ +index,chat_size,query,p50,p95,p99,mean,qps +chat_only,80000,keyset,7.0761,7.1933,7.1945,7.0884,141.064 +chat_only,80000,offset,24.9378,28.3299,29.9997,25.4597,39.275 +chat_only,15000,keyset,1.4773,1.5489,1.5723,1.4867,672.438 +chat_only,15000,offset,4.1769,8.9294,9.3571,4.5287,220.78 +chat_only,4000,keyset,0.6269,0.8833,0.9718,0.6489,1539.583 +chat_only,4000,offset,0.6617,1.123,1.5037,0.729,1370.731 +chat_only,1000,keyset,0.3048,0.443,0.6305,0.325,3072.865 +chat_only,1000,offset,0.3366,0.4242,0.4286,0.3492,2860.548 +keyset,80000,keyset,0.2249,0.2545,0.3508,0.2318,4307.199 +keyset,80000,offset,6.9243,12.6501,15.0282,7.8992,126.569 +keyset,15000,keyset,0.2069,0.2373,0.2455,0.2108,4736.312 +keyset,15000,offset,1.3274,6.6483,6.7603,2.32,430.747 +keyset,4000,keyset,0.2088,0.2822,0.3122,0.2174,4590.519 +keyset,4000,offset,0.5051,0.657,0.7066,0.5265,1897.228 +keyset,1000,keyset,0.1998,0.235,0.2685,0.205,4868.845 +keyset,1000,offset,0.2825,0.517,0.5368,0.3155,3164.696 +covering,80000,keyset,0.1993,0.3393,0.404,0.2336,4272.262 +covering,80000,offset,6.7655,7.1888,7.3155,6.8453,146.073 +covering,15000,keyset,0.2027,0.2262,0.2397,0.206,4840.044 +covering,15000,offset,1.374,1.5291,1.5962,1.3926,717.922 +covering,4000,keyset,0.1897,0.2183,0.2226,0.1935,5160.995 +covering,4000,offset,0.508,0.7336,0.8249,0.5293,1888.336 +covering,1000,keyset,0.1999,0.2138,0.2153,0.2028,4925.03 +covering,1000,offset,0.2774,0.3663,0.4765,0.2948,3388.315 diff --git a/evidence/research-run.txt b/evidence/research-run.txt new file mode 100644 index 00000000..08989045 --- /dev/null +++ b/evidence/research-run.txt @@ -0,0 +1,2 @@ +Benchmarking 100000 messages, 4 skewed chats, 3 indexes and 2 queries... +PASS: restored 100000 messages to a new PostgreSQL container/volume; counts, 7 samples, complete SHA256 and foreign keys verified. diff --git a/evidence/research-summary.md b/evidence/research-summary.md new file mode 100644 index 00000000..accac9c2 --- /dev/null +++ b/evidence/research-summary.md @@ -0,0 +1,38 @@ +# План запроса и восстановление + +| Индекс | Сообщений в чате | Запрос | p50, мс | p95, мс | p99, мс | Sequential QPS | +| --- | ---: | --- | ---: | ---: | ---: | ---: | +| chat_only | 80000 | keyset | 7.0761 | 7.1933 | 7.1945 | 141.064 | +| chat_only | 80000 | offset | 24.9378 | 28.3299 | 29.9997 | 39.275 | +| chat_only | 15000 | keyset | 1.4773 | 1.5489 | 1.5723 | 672.438 | +| chat_only | 15000 | offset | 4.1769 | 8.9294 | 9.3571 | 220.78 | +| chat_only | 4000 | keyset | 0.6269 | 0.8833 | 0.9718 | 1539.583 | +| chat_only | 4000 | offset | 0.6617 | 1.123 | 1.5037 | 1370.731 | +| chat_only | 1000 | keyset | 0.3048 | 0.443 | 0.6305 | 3072.865 | +| chat_only | 1000 | offset | 0.3366 | 0.4242 | 0.4286 | 2860.548 | +| keyset | 80000 | keyset | 0.2249 | 0.2545 | 0.3508 | 4307.199 | +| keyset | 80000 | offset | 6.9243 | 12.6501 | 15.0282 | 126.569 | +| keyset | 15000 | keyset | 0.2069 | 0.2373 | 0.2455 | 4736.312 | +| keyset | 15000 | offset | 1.3274 | 6.6483 | 6.7603 | 430.747 | +| keyset | 4000 | keyset | 0.2088 | 0.2822 | 0.3122 | 4590.519 | +| keyset | 4000 | offset | 0.5051 | 0.657 | 0.7066 | 1897.228 | +| keyset | 1000 | keyset | 0.1998 | 0.235 | 0.2685 | 4868.845 | +| keyset | 1000 | offset | 0.2825 | 0.517 | 0.5368 | 3164.696 | +| covering | 80000 | keyset | 0.1993 | 0.3393 | 0.404 | 4272.262 | +| covering | 80000 | offset | 6.7655 | 7.1888 | 7.3155 | 146.073 | +| covering | 15000 | keyset | 0.2027 | 0.2262 | 0.2397 | 4840.044 | +| covering | 15000 | offset | 1.374 | 1.5291 | 1.5962 | 717.922 | +| covering | 4000 | keyset | 0.1897 | 0.2183 | 0.2226 | 5160.995 | +| covering | 4000 | offset | 0.508 | 0.7336 | 0.8249 | 1888.336 | +| covering | 1000 | keyset | 0.1999 | 0.2138 | 0.2153 | 4925.03 | +| covering | 1000 | offset | 0.2774 | 0.3663 | 0.4765 | 3388.315 | + +| Индекс | Индексы messages, MiB | INSERT 1000 + COMMIT p50, мс | p95, мс | Средний WAL на 1000 строк, bytes | +| --- | ---: | ---: | ---: | ---: | +| chat_only | 5.203 | 9.6394 | 11.5286 | 379956 | +| keyset | 11.156 | 10.289 | 12.1678 | 453492 | +| covering | 27.609 | 13.9626 | 64.6203 | 708616 | + +Backup: 3004503 bytes, 0.5288 с. Новый PostgreSQL/volume: 1.3216 с. Restore: 0.6135 с. Provision + restore: 1.9351 с. + +Сверены количества всех таблиц, 7 выбранных сообщений, SHA-256 всей истории из 100 000 сообщений, отсутствие нарушенных связей. Запись после снимка ожидаемо отсутствует в восстановленной БД. diff --git a/evidence/research.json b/evidence/research.json new file mode 100644 index 00000000..3893b6e8 --- /dev/null +++ b/evidence/research.json @@ -0,0 +1,3627 @@ +{ + "date_utc": "2026-10-30T11:04:35.421935+00:00", + "source_sha256": { + "src/store.py": "162782315e7df8a4e2a442b5accaa0620d5b6214cc5c361017d69b306f76cfc6", + "migrations/001_initial.sql": "a009840617a4d08a7b18bdcffb588920838678e19a19b3918cbb0849c2ac81a9", + "research/worker.py": "52ec875ab656277887cc1239f05ca4ba8200228de8a1dabfbdbbca8c339e1cc5" + }, + "method": "Isolated skewed PostgreSQL datasets; 30 sequential reads after 3 warmups; 10 committed INSERT batches of 1000 rows; fresh restore volume.", + "limitations": [ + "Local single-host measurements; warm buffers; no concurrent HTTP capacity claim.", + "EXPLAIN insert wall times include plan collection and COMMIT; 10 batches do not establish a write SLA.", + "Logical backup is a snapshot, not PITR or HA; the post-dump canary is intentionally absent after restore." + ], + "query_experiment": { + "postgres_version": "17.9 (Debian 17.9-1.pgdg12+1)", + "dataset_messages": 100000, + "chat_sizes": [ + 80000, + 15000, + 4000, + 1000 + ], + "read_repeats": 30, + "warmup_queries": 3, + "write_batches": 10, + "write_batch_size": 1000, + "limits": { + "postgres_cpus": 1, + "postgres_memory_mib": 512, + "client_cpus": 1, + "client_memory_mib": 512 + }, + "variants": [ + { + "name": "chat_only", + "database": "lab2_research_e09aa1b4_chat_only", + "seed_seconds": 0.8756, + "table_bytes": 20021248, + "indexes_bytes": 5455872, + "write_runs": [ + { + "commit_wall_ms": 11.5286, + "execution_ms": 9.524, + "wal_bytes": 386092, + "shared_dirtied_blocks": 31 + }, + { + "commit_wall_ms": 9.9018, + "execution_ms": 8.992, + "wal_bytes": 400880, + "shared_dirtied_blocks": 34 + }, + { + "commit_wall_ms": 9.6394, + "execution_ms": 8.765, + "wal_bytes": 390012, + "shared_dirtied_blocks": 32 + }, + { + "commit_wall_ms": 9.8604, + "execution_ms": 8.889, + "wal_bytes": 379068, + "shared_dirtied_blocks": 28 + }, + { + "commit_wall_ms": 9.7735, + "execution_ms": 8.874, + "wal_bytes": 367380, + "shared_dirtied_blocks": 24 + }, + { + "commit_wall_ms": 9.3423, + "execution_ms": 8.538, + "wal_bytes": 375332, + "shared_dirtied_blocks": 28 + }, + { + "commit_wall_ms": 8.9664, + "execution_ms": 8.28, + "wal_bytes": 368072, + "shared_dirtied_blocks": 25 + }, + { + "commit_wall_ms": 9.5945, + "execution_ms": 8.681, + "wal_bytes": 382676, + "shared_dirtied_blocks": 30 + }, + { + "commit_wall_ms": 9.9285, + "execution_ms": 8.947, + "wal_bytes": 371756, + "shared_dirtied_blocks": 26 + }, + { + "commit_wall_ms": 9.2812, + "execution_ms": 8.499, + "wal_bytes": 378296, + "shared_dirtied_blocks": 27 + } + ], + "write_commit_ms": { + "p50": 9.6394, + "p95": 11.5286, + "p99": 11.5286, + "mean": 9.7817 + }, + "reads": [ + { + "chat_size": 80000, + "query": "keyset", + "latency_ms": { + "p50": 7.0761, + "p95": 7.1933, + "p99": 7.1945, + "mean": 7.0884 + }, + "raw_latency_ms": [ + 7.1945, + 7.0456, + 7.0677, + 6.9425, + 6.9956, + 7.0325, + 6.9857, + 7.1158, + 7.0837, + 7.0636, + 7.1851, + 7.112, + 7.1655, + 7.1372, + 7.0726, + 7.1933, + 7.0881, + 7.0583, + 7.0782, + 7.0714, + 7.172, + 7.0634, + 7.0387, + 7.0761, + 7.1624, + 7.0884, + 7.0871, + 7.0658, + 7.0226, + 7.1865 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 141.064, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4583.62, + "Total Cost": 4583.74, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 6.726, + "Actual Total Time": 6.732, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4583.62, + "Total Cost": 4623.51, + "Plan Rows": 15957, + "Plan Width": 162, + "Actual Startup Time": 6.726, + "Actual Total Time": 6.728, + "Actual Rows": 50, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 4053.53, + "Plan Rows": 15957, + "Plan Width": 162, + "Actual Startup Time": 6.692, + "Actual Total Time": 6.715, + "Actual Rows": 100, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000001'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0799+00'::timestamp with time zone, '4c258fbb-7ab4-ca0e-1a48-23111d989887'::uuid))", + "Rows Removed by Filter": 79900, + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.044, + "Triggers": [], + "Execution Time": 6.74 + } + }, + { + "chat_size": 80000, + "query": "offset", + "latency_ms": { + "p50": 24.9378, + "p95": 28.3299, + "p99": 29.9997, + "mean": 25.4597 + }, + "raw_latency_ms": [ + 24.7768, + 24.6414, + 24.9378, + 24.4358, + 24.1052, + 24.2144, + 24.6242, + 24.3533, + 25.1993, + 25.3574, + 25.3675, + 29.9997, + 28.3299, + 25.9315, + 24.3103, + 23.9882, + 26.5002, + 27.6485, + 27.0724, + 27.0823, + 27.9675, + 27.3795, + 26.1129, + 26.4398, + 26.2524, + 23.8179, + 23.6142, + 23.2457, + 22.9133, + 23.1718 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 39.275, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 16971.36, + "Total Cost": 16971.49, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 51.018, + "Actual Total Time": 51.027, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 1742, + "Temp Written Blocks": 1747, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 16771.61, + "Total Cost": 16972.2, + "Plan Rows": 80233, + "Plan Width": 162, + "Actual Startup Time": 39.315, + "Actual Total Time": 48.181, + "Actual Rows": 79950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "external merge", + "Sort Space Used": 13936, + "Sort Space Type": "Disk", + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 1742, + "Temp Written Blocks": 1747, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 3652.37, + "Plan Rows": 80233, + "Plan Width": 162, + "Actual Startup Time": 0.01, + "Actual Total Time": 7.926, + "Actual Rows": 80000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000001'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 2017, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.072, + "Triggers": [], + "Execution Time": 51.932 + } + }, + { + "chat_size": 15000, + "query": "keyset", + "latency_ms": { + "p50": 1.4773, + "p95": 1.5489, + "p99": 1.5723, + "mean": 1.4867 + }, + "raw_latency_ms": [ + 1.4891, + 1.4852, + 1.5267, + 1.5221, + 1.4782, + 1.4716, + 1.4668, + 1.478, + 1.4559, + 1.4603, + 1.492, + 1.4773, + 1.459, + 1.4697, + 1.4777, + 1.4993, + 1.4817, + 1.4659, + 1.4485, + 1.4569, + 1.4668, + 1.5444, + 1.4605, + 1.4554, + 1.4745, + 1.5723, + 1.5445, + 1.5489, + 1.4955, + 1.4772 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 672.438, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 779.4, + "Total Cost": 779.52, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 1.266, + "Actual Total Time": 1.271, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 779.4, + "Total Cost": 781.27, + "Plan Rows": 747, + "Plan Width": 162, + "Actual Startup Time": 1.265, + "Actual Total Time": 1.267, + "Actual Rows": 50, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 754.59, + "Plan Rows": 747, + "Plan Width": 162, + "Actual Startup Time": 1.243, + "Actual Total Time": 1.255, + "Actual Rows": 100, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000002'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0949+00'::timestamp with time zone, '83309d4e-6a9c-bf24-ec2c-d0ad8d47e31e'::uuid))", + "Rows Removed by Filter": 14900, + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.074, + "Triggers": [], + "Execution Time": 1.279 + } + }, + { + "chat_size": 15000, + "query": "offset", + "latency_ms": { + "p50": 4.1769, + "p95": 8.9294, + "p99": 9.3571, + "mean": 4.5287 + }, + "raw_latency_ms": [ + 3.9992, + 4.1769, + 4.1085, + 9.3571, + 4.3402, + 4.0119, + 3.9902, + 4.159, + 4.0303, + 4.2165, + 4.5519, + 4.7326, + 4.3163, + 4.1272, + 4.8157, + 8.9294, + 4.4488, + 4.278, + 4.4118, + 4.1779, + 3.9815, + 4.3006, + 4.6323, + 3.932, + 3.8745, + 3.8335, + 3.9477, + 3.8701, + 3.9719, + 4.3378 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 220.78, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 1743.69, + "Total Cost": 1743.69, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 6.012, + "Actual Total Time": 6.02, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 328, + "Temp Written Blocks": 329, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 1706.66, + "Total Cost": 1743.69, + "Plan Rows": 14813, + "Plan Width": 162, + "Actual Startup Time": 4.225, + "Actual Total Time": 5.504, + "Actual Rows": 14950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "external merge", + "Sort Space Used": 2624, + "Sort Space Type": "Disk", + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 328, + "Temp Written Blocks": 329, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 680.52, + "Plan Rows": 14813, + "Plan Width": 162, + "Actual Startup Time": 0.006, + "Actual Total Time": 1.521, + "Actual Rows": 15000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000002'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 379, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.027, + "Triggers": [], + "Execution Time": 6.17 + } + }, + { + "chat_size": 4000, + "query": "keyset", + "latency_ms": { + "p50": 0.6269, + "p95": 0.8833, + "p99": 0.9718, + "mean": 0.6489 + }, + "raw_latency_ms": [ + 0.6667, + 0.6633, + 0.8833, + 0.6693, + 0.6508, + 0.7086, + 0.6538, + 0.7583, + 0.6407, + 0.7791, + 0.5865, + 0.6176, + 0.7591, + 0.9718, + 0.751, + 0.6269, + 0.6428, + 0.6371, + 0.5982, + 0.5791, + 0.5791, + 0.5623, + 0.5722, + 0.5835, + 0.5617, + 0.5422, + 0.5575, + 0.5559, + 0.546, + 0.5618 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 1539.583, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 208.17, + "Total Cost": 208.27, + "Plan Rows": 43, + "Plan Width": 162, + "Actual Startup Time": 0.346, + "Actual Total Time": 0.352, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 208.17, + "Total Cost": 208.27, + "Plan Rows": 43, + "Plan Width": 162, + "Actual Startup Time": 0.346, + "Actual Total Time": 0.348, + "Actual Rows": 50, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 207.0, + "Plan Rows": 43, + "Plan Width": 162, + "Actual Startup Time": 0.324, + "Actual Total Time": 0.336, + "Actual Rows": 100, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000003'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0989+00'::timestamp with time zone, '3ca7f544-85f6-3d47-6ce5-72ab53f46142'::uuid))", + "Rows Removed by Filter": 3900, + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.077, + "Triggers": [], + "Execution Time": 0.36 + } + }, + { + "chat_size": 4000, + "query": "offset", + "latency_ms": { + "p50": 0.6617, + "p95": 1.123, + "p99": 1.5037, + "mean": 0.729 + }, + "raw_latency_ms": [ + 0.6681, + 0.6432, + 0.6471, + 0.6594, + 0.6617, + 0.6541, + 0.6335, + 0.6689, + 0.6442, + 0.6426, + 0.6442, + 0.658, + 0.7162, + 0.7088, + 0.8265, + 0.7309, + 0.677, + 0.668, + 0.651, + 0.6417, + 0.6567, + 0.6553, + 0.6392, + 0.6815, + 0.6943, + 0.7807, + 1.5037, + 0.8532, + 1.123, + 0.8366 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 1370.731, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 435.26, + "Total Cost": 435.38, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.976, + "Actual Total Time": 0.982, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 425.51, + "Total Cost": 435.48, + "Plan Rows": 3987, + "Plan Width": 162, + "Actual Startup Time": 0.671, + "Actual Total Time": 0.84, + "Actual Rows": 3950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 1128, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 187.06, + "Plan Rows": 3987, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.364, + "Actual Rows": 4000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000003'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 104, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.019, + "Triggers": [], + "Execution Time": 0.989 + } + }, + { + "chat_size": 1000, + "query": "keyset", + "latency_ms": { + "p50": 0.3048, + "p95": 0.443, + "p99": 0.6305, + "mean": 0.325 + }, + "raw_latency_ms": [ + 0.308, + 0.319, + 0.2923, + 0.294, + 0.3007, + 0.3048, + 0.3013, + 0.3191, + 0.3412, + 0.443, + 0.6305, + 0.3525, + 0.3222, + 0.3087, + 0.3078, + 0.3036, + 0.3035, + 0.3029, + 0.3091, + 0.2937, + 0.2918, + 0.3296, + 0.3577, + 0.3153, + 0.3032, + 0.3124, + 0.294, + 0.2951, + 0.3019, + 0.2896 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 3072.865, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 53.06, + "Total Cost": 53.06, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.101, + "Actual Total Time": 0.107, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 53.06, + "Total Cost": 53.06, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.101, + "Actual Total Time": 0.103, + "Actual Rows": 50, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 53.05, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.08, + "Actual Total Time": 0.092, + "Actual Rows": 100, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000004'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0999+00'::timestamp with time zone, '7fa13929-f6d0-fc18-76f9-46306abe42ca'::uuid))", + "Rows Removed by Filter": 900, + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.067, + "Triggers": [], + "Execution Time": 0.115 + } + }, + { + "chat_size": 1000, + "query": "offset", + "latency_ms": { + "p50": 0.3366, + "p95": 0.4242, + "p99": 0.4286, + "mean": 0.3492 + }, + "raw_latency_ms": [ + 0.3735, + 0.3238, + 0.3473, + 0.3185, + 0.319, + 0.3433, + 0.3205, + 0.3139, + 0.3342, + 0.3158, + 0.3344, + 0.3477, + 0.3227, + 0.3212, + 0.3645, + 0.3983, + 0.3515, + 0.354, + 0.3337, + 0.3295, + 0.4286, + 0.414, + 0.4242, + 0.389, + 0.3699, + 0.3508, + 0.3351, + 0.3229, + 0.3366, + 0.3368 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 2860.548, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 98.42, + "Total Cost": 98.54, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.298, + "Actual Total Time": 0.304, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 96.17, + "Total Cost": 98.58, + "Plan Rows": 967, + "Plan Width": 162, + "Actual Startup Time": 0.163, + "Actual Total Time": 0.242, + "Actual Rows": 950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 282, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_only_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.29, + "Total Cost": 48.21, + "Plan Rows": 967, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.091, + "Actual Rows": 1000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000004'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 27, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.019, + "Triggers": [], + "Execution Time": 0.312 + } + } + ] + }, + { + "name": "keyset", + "database": "lab2_research_e09aa1b4_keyset", + "seed_seconds": 0.9453, + "table_bytes": 20021248, + "indexes_bytes": 11698176, + "write_runs": [ + { + "commit_wall_ms": 12.1678, + "execution_ms": 10.483, + "wal_bytes": 460176, + "shared_dirtied_blocks": 43 + }, + { + "commit_wall_ms": 11.4934, + "execution_ms": 10.325, + "wal_bytes": 474664, + "shared_dirtied_blocks": 46 + }, + { + "commit_wall_ms": 10.289, + "execution_ms": 9.276, + "wal_bytes": 463744, + "shared_dirtied_blocks": 44 + }, + { + "commit_wall_ms": 9.9325, + "execution_ms": 9.007, + "wal_bytes": 452800, + "shared_dirtied_blocks": 40 + }, + { + "commit_wall_ms": 9.5215, + "execution_ms": 8.692, + "wal_bytes": 437944, + "shared_dirtied_blocks": 36 + }, + { + "commit_wall_ms": 9.6942, + "execution_ms": 8.842, + "wal_bytes": 452928, + "shared_dirtied_blocks": 41 + }, + { + "commit_wall_ms": 9.3175, + "execution_ms": 8.613, + "wal_bytes": 441856, + "shared_dirtied_blocks": 37 + }, + { + "commit_wall_ms": 10.5368, + "execution_ms": 8.682, + "wal_bytes": 456408, + "shared_dirtied_blocks": 42 + }, + { + "commit_wall_ms": 11.4386, + "execution_ms": 10.19, + "wal_bytes": 445488, + "shared_dirtied_blocks": 38 + }, + { + "commit_wall_ms": 10.5862, + "execution_ms": 9.668, + "wal_bytes": 448912, + "shared_dirtied_blocks": 39 + } + ], + "write_commit_ms": { + "p50": 10.289, + "p95": 12.1678, + "p99": 12.1678, + "mean": 10.4977 + }, + "reads": [ + { + "chat_size": 80000, + "query": "keyset", + "latency_ms": { + "p50": 0.2249, + "p95": 0.2545, + "p99": 0.3508, + "mean": 0.2318 + }, + "raw_latency_ms": [ + 0.2507, + 0.2534, + 0.3508, + 0.2332, + 0.2198, + 0.2226, + 0.2395, + 0.2545, + 0.2281, + 0.2288, + 0.2328, + 0.2252, + 0.2284, + 0.225, + 0.2247, + 0.2362, + 0.2197, + 0.2186, + 0.2248, + 0.2344, + 0.22, + 0.222, + 0.2189, + 0.2172, + 0.2151, + 0.2249, + 0.2205, + 0.2218, + 0.2325, + 0.2104 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 4307.199, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 16.91, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.014, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 5376.21, + "Plan Rows": 16299, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.01, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000001'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0799+00'::timestamp with time zone, '4c258fbb-7ab4-ca0e-1a48-23111d989887'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.019, + "Triggers": [], + "Execution Time": 0.02 + } + }, + { + "chat_size": 80000, + "query": "offset", + "latency_ms": { + "p50": 6.9243, + "p95": 12.6501, + "p99": 15.0282, + "mean": 7.8992 + }, + "raw_latency_ms": [ + 6.9314, + 6.6215, + 6.6125, + 7.0876, + 6.4968, + 6.9243, + 6.571, + 6.2754, + 6.1013, + 6.1353, + 6.0583, + 6.721, + 6.1335, + 6.7827, + 10.0007, + 10.1573, + 9.4869, + 12.6501, + 10.1362, + 15.0282, + 12.1204, + 9.08, + 6.7563, + 6.2658, + 6.9835, + 8.125, + 7.2746, + 7.5422, + 7.3887, + 6.5264 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 126.569, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 9467.91, + "Total Cost": 9468.02, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 11.335, + "Actual Total Time": 11.345, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 2524, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 9467.91, + "Plan Rows": 79610, + "Plan Width": 162, + "Actual Startup Time": 0.008, + "Actual Total Time": 8.54, + "Actual Rows": 79950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000001'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 2524, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.092, + "Triggers": [], + "Execution Time": 11.357 + } + }, + { + "chat_size": 15000, + "query": "keyset", + "latency_ms": { + "p50": 0.2069, + "p95": 0.2373, + "p99": 0.2455, + "mean": 0.2108 + }, + "raw_latency_ms": [ + 0.2373, + 0.2455, + 0.2286, + 0.2179, + 0.227, + 0.2261, + 0.202, + 0.1996, + 0.1972, + 0.1978, + 0.2108, + 0.1955, + 0.1995, + 0.196, + 0.1958, + 0.2084, + 0.1987, + 0.193, + 0.2089, + 0.2119, + 0.2058, + 0.2095, + 0.1917, + 0.1995, + 0.1935, + 0.2069, + 0.2285, + 0.2295, + 0.2328, + 0.2282 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 4736.312, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 79.7, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.014, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 1218.15, + "Plan Rows": 768, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.01, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000002'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0949+00'::timestamp with time zone, '83309d4e-6a9c-bf24-ec2c-d0ad8d47e31e'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.049, + "Triggers": [], + "Execution Time": 0.022 + } + }, + { + "chat_size": 15000, + "query": "offset", + "latency_ms": { + "p50": 1.3274, + "p95": 6.6483, + "p99": 6.7603, + "mean": 2.32 + }, + "raw_latency_ms": [ + 5.0241, + 4.5037, + 4.6073, + 4.7289, + 6.3138, + 6.6483, + 6.7603, + 1.871, + 1.3498, + 1.3344, + 1.2695, + 1.2741, + 1.3163, + 1.3274, + 1.2907, + 1.2879, + 1.3007, + 1.478, + 1.2978, + 1.2856, + 1.3245, + 1.2953, + 1.3345, + 1.3287, + 1.2874, + 1.2853, + 1.298, + 1.379, + 1.3168, + 1.4797 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 430.747, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4412.7, + "Total Cost": 4412.83, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 7.498, + "Actual Total Time": 7.508, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 476, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 328, + "Temp Written Blocks": 329, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4375.45, + "Total Cost": 4414.49, + "Plan Rows": 15617, + "Plan Width": 162, + "Actual Startup Time": 5.713, + "Actual Total Time": 6.979, + "Actual Rows": 14950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "external merge", + "Sort Space Used": 2624, + "Sort Space Type": "Disk", + "Shared Hit Blocks": 476, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 328, + "Temp Written Blocks": 329, + "Plans": [ + { + "Node Type": "Bitmap Heap Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 653.45, + "Total Cost": 3287.66, + "Plan Rows": 15617, + "Plan Width": 162, + "Actual Startup Time": 0.546, + "Actual Total Time": 1.806, + "Actual Rows": 15000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '00000000-0000-0000-0000-000000000002'::uuid)", + "Rows Removed by Index Recheck": 0, + "Exact Heap Blocks": 366, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 476, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Bitmap Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Index Name": "messages_chat_created_id_idx", + "Startup Cost": 0.0, + "Total Cost": 649.54, + "Plan Rows": 15617, + "Plan Width": 0, + "Actual Startup Time": 0.503, + "Actual Total Time": 0.503, + "Actual Rows": 15000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000002'::uuid)", + "Shared Hit Blocks": 110, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.071, + "Triggers": [], + "Execution Time": 7.708 + } + }, + { + "chat_size": 4000, + "query": "keyset", + "latency_ms": { + "p50": 0.2088, + "p95": 0.2822, + "p99": 0.3122, + "mean": 0.2174 + }, + "raw_latency_ms": [ + 0.2822, + 0.2479, + 0.3122, + 0.2194, + 0.212, + 0.2153, + 0.2106, + 0.2297, + 0.2096, + 0.2075, + 0.2083, + 0.2098, + 0.2088, + 0.206, + 0.2078, + 0.2039, + 0.2222, + 0.2058, + 0.2023, + 0.2045, + 0.2015, + 0.2246, + 0.2021, + 0.2044, + 0.2014, + 0.2063, + 0.2127, + 0.201, + 0.2134, + 0.2276 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 4590.519, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 79.8, + "Plan Rows": 41, + "Plan Width": 162, + "Actual Startup Time": 0.006, + "Actual Total Time": 0.014, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 79.8, + "Plan Rows": 41, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.01, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000003'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0989+00'::timestamp with time zone, '3ca7f544-85f6-3d47-6ce5-72ab53f46142'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.081, + "Triggers": [], + "Execution Time": 0.021 + } + }, + { + "chat_size": 4000, + "query": "offset", + "latency_ms": { + "p50": 0.5051, + "p95": 0.657, + "p99": 0.7066, + "mean": 0.5265 + }, + "raw_latency_ms": [ + 0.4957, + 0.4786, + 0.5126, + 0.5663, + 0.5825, + 0.5162, + 0.4861, + 0.5071, + 0.7066, + 0.657, + 0.5481, + 0.5051, + 0.489, + 0.4827, + 0.5018, + 0.5004, + 0.483, + 0.4886, + 0.4746, + 0.4971, + 0.4819, + 0.6243, + 0.559, + 0.5098, + 0.5098, + 0.4855, + 0.4931, + 0.5078, + 0.627, + 0.5188 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 1897.228, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2991.44, + "Total Cost": 2991.44, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 1.111, + "Actual Total Time": 1.117, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 131, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2982.0, + "Total Cost": 2991.44, + "Plan Rows": 3777, + "Plan Width": 162, + "Actual Startup Time": 0.79, + "Actual Total Time": 0.976, + "Actual Rows": 3950, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 1128, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 131, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Bitmap Heap Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 161.69, + "Total Cost": 2757.59, + "Plan Rows": 3777, + "Plan Width": 162, + "Actual Startup Time": 0.152, + "Actual Total Time": 0.46, + "Actual Rows": 4000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '00000000-0000-0000-0000-000000000003'::uuid)", + "Rows Removed by Index Recheck": 0, + "Exact Heap Blocks": 99, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 131, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Bitmap Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Index Name": "messages_chat_created_id_idx", + "Startup Cost": 0.0, + "Total Cost": 160.74, + "Plan Rows": 3777, + "Plan Width": 0, + "Actual Startup Time": 0.139, + "Actual Total Time": 0.139, + "Actual Rows": 4000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000003'::uuid)", + "Shared Hit Blocks": 32, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.068, + "Triggers": [], + "Execution Time": 1.129 + } + }, + { + "chat_size": 1000, + "query": "keyset", + "latency_ms": { + "p50": 0.1998, + "p95": 0.235, + "p99": 0.2685, + "mean": 0.205 + }, + "raw_latency_ms": [ + 0.2064, + 0.2084, + 0.1998, + 0.2078, + 0.2144, + 0.1987, + 0.1991, + 0.1996, + 0.1965, + 0.2004, + 0.1998, + 0.2007, + 0.1957, + 0.1992, + 0.2052, + 0.1958, + 0.1978, + 0.1942, + 0.1949, + 0.2148, + 0.1981, + 0.1933, + 0.1977, + 0.1944, + 0.2685, + 0.235, + 0.2088, + 0.2044, + 0.2183, + 0.203 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 4868.845, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 8.44, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.013, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 8.44, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.009, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000004'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0999+00'::timestamp with time zone, '7fa13929-f6d0-fc18-76f9-46306abe42ca'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.061, + "Triggers": [], + "Execution Time": 0.019 + } + }, + { + "chat_size": 1000, + "query": "offset", + "latency_ms": { + "p50": 0.2825, + "p95": 0.517, + "p99": 0.5368, + "mean": 0.3155 + }, + "raw_latency_ms": [ + 0.2628, + 0.2779, + 0.2633, + 0.2621, + 0.273, + 0.2787, + 0.2618, + 0.2817, + 0.2825, + 0.2827, + 0.2667, + 0.2641, + 0.2825, + 0.2986, + 0.3225, + 0.5368, + 0.3892, + 0.4551, + 0.3943, + 0.517, + 0.3484, + 0.2773, + 0.2941, + 0.2827, + 0.377, + 0.2994, + 0.303, + 0.2738, + 0.2835, + 0.2713 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 3164.696, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 1370.91, + "Total Cost": 1447.05, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.128, + "Actual Total Time": 0.136, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 33, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_created_id_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 1518.62, + "Plan Rows": 997, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.101, + "Actual Rows": 950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000004'::uuid)", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 33, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.062, + "Triggers": [], + "Execution Time": 0.145 + } + } + ] + }, + { + "name": "covering", + "database": "lab2_research_e09aa1b4_covering", + "seed_seconds": 1.1557, + "table_bytes": 20021248, + "indexes_bytes": 28950528, + "write_runs": [ + { + "commit_wall_ms": 15.6168, + "execution_ms": 13.013, + "wal_bytes": 715344, + "shared_dirtied_blocks": 74 + }, + { + "commit_wall_ms": 44.015, + "execution_ms": 42.11, + "wal_bytes": 729608, + "shared_dirtied_blocks": 77 + }, + { + "commit_wall_ms": 14.7893, + "execution_ms": 12.95, + "wal_bytes": 719000, + "shared_dirtied_blocks": 75 + }, + { + "commit_wall_ms": 13.9626, + "execution_ms": 12.09, + "wal_bytes": 704008, + "shared_dirtied_blocks": 70 + }, + { + "commit_wall_ms": 13.266, + "execution_ms": 11.369, + "wal_bytes": 700848, + "shared_dirtied_blocks": 69 + }, + { + "commit_wall_ms": 64.6203, + "execution_ms": 61.422, + "wal_bytes": 700376, + "shared_dirtied_blocks": 70 + }, + { + "commit_wall_ms": 11.2113, + "execution_ms": 9.845, + "wal_bytes": 700848, + "shared_dirtied_blocks": 69 + }, + { + "commit_wall_ms": 11.6234, + "execution_ms": 10.102, + "wal_bytes": 707616, + "shared_dirtied_blocks": 72 + }, + { + "commit_wall_ms": 11.3013, + "execution_ms": 9.977, + "wal_bytes": 700744, + "shared_dirtied_blocks": 69 + }, + { + "commit_wall_ms": 60.1216, + "execution_ms": 59.042, + "wal_bytes": 707768, + "shared_dirtied_blocks": 71 + } + ], + "write_commit_ms": { + "p50": 13.9626, + "p95": 64.6203, + "p99": 64.6203, + "mean": 26.0528 + }, + "reads": [ + { + "chat_size": 80000, + "query": "keyset", + "latency_ms": { + "p50": 0.1993, + "p95": 0.3393, + "p99": 0.404, + "mean": 0.2336 + }, + "raw_latency_ms": [ + 0.2973, + 0.2685, + 0.3393, + 0.3388, + 0.2843, + 0.3033, + 0.2897, + 0.2492, + 0.2006, + 0.1993, + 0.1923, + 0.1936, + 0.1787, + 0.1749, + 0.1877, + 0.1749, + 0.2028, + 0.1818, + 0.1763, + 0.1728, + 0.1942, + 0.1722, + 0.1774, + 0.1911, + 0.1755, + 0.2407, + 0.3043, + 0.271, + 0.2699, + 0.404 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 4272.262, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 7.34, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.006, + "Actual Total Time": 0.015, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 2212.08, + "Plan Rows": 15983, + "Plan Width": 162, + "Actual Startup Time": 0.006, + "Actual Total Time": 0.011, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000001'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0799+00'::timestamp with time zone, '4c258fbb-7ab4-ca0e-1a48-23111d989887'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.035, + "Triggers": [], + "Execution Time": 0.024 + } + }, + { + "chat_size": 80000, + "query": "offset", + "latency_ms": { + "p50": 6.7655, + "p95": 7.1888, + "p99": 7.3155, + "mean": 6.8453 + }, + "raw_latency_ms": [ + 6.7905, + 6.9868, + 7.1641, + 6.7145, + 6.6758, + 6.662, + 6.7611, + 6.7655, + 6.5623, + 6.5497, + 6.7386, + 7.0508, + 7.3155, + 6.9717, + 6.9465, + 7.09, + 7.1888, + 7.1567, + 6.6796, + 7.0895, + 7.0182, + 6.9527, + 6.6477, + 6.5414, + 6.8389, + 6.5921, + 6.9738, + 6.6531, + 6.5765, + 6.7053 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 146.073, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 10853.18, + "Total Cost": 10859.97, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 11.598, + "Actual Total Time": 11.608, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 2002, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 10891.62, + "Plan Rows": 80183, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 8.823, + "Actual Rows": 79950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000001'::uuid)", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 2002, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.04, + "Triggers": [], + "Execution Time": 11.616 + } + }, + { + "chat_size": 15000, + "query": "keyset", + "latency_ms": { + "p50": 0.2027, + "p95": 0.2262, + "p99": 0.2397, + "mean": 0.206 + }, + "raw_latency_ms": [ + 0.2262, + 0.2397, + 0.205, + 0.2123, + 0.1969, + 0.2053, + 0.1981, + 0.2071, + 0.2027, + 0.2019, + 0.2212, + 0.2054, + 0.2026, + 0.1926, + 0.217, + 0.204, + 0.2045, + 0.2001, + 0.2007, + 0.201, + 0.2068, + 0.2013, + 0.2023, + 0.2004, + 0.1986, + 0.2043, + 0.203, + 0.1992, + 0.2018, + 0.2174 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 4840.044, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 7.36, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.014, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 103.22, + "Plan Rows": 740, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.01, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000002'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0949+00'::timestamp with time zone, '83309d4e-6a9c-bf24-ec2c-d0ad8d47e31e'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.043, + "Triggers": [], + "Execution Time": 0.021 + } + }, + { + "chat_size": 15000, + "query": "offset", + "latency_ms": { + "p50": 1.374, + "p95": 1.5291, + "p99": 1.5962, + "mean": 1.3926 + }, + "raw_latency_ms": [ + 1.4307, + 1.4245, + 1.5962, + 1.4651, + 1.3905, + 1.4193, + 1.5291, + 1.3768, + 1.3905, + 1.4379, + 1.374, + 1.376, + 1.3632, + 1.3709, + 1.4246, + 1.3796, + 1.3833, + 1.3908, + 1.3711, + 1.3649, + 1.3535, + 1.3539, + 1.3552, + 1.349, + 1.361, + 1.3386, + 1.3488, + 1.3593, + 1.3514, + 1.3469 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 717.922, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2024.08, + "Total Cost": 2029.92, + "Plan Rows": 43, + "Plan Width": 162, + "Actual Startup Time": 2.174, + "Actual Total Time": 2.182, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 378, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 2029.92, + "Plan Rows": 14943, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 1.656, + "Actual Rows": 14950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000002'::uuid)", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 378, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.048, + "Triggers": [], + "Execution Time": 2.19 + } + }, + { + "chat_size": 4000, + "query": "keyset", + "latency_ms": { + "p50": 0.1897, + "p95": 0.2183, + "p99": 0.2226, + "mean": 0.1935 + }, + "raw_latency_ms": [ + 0.2042, + 0.1914, + 0.1933, + 0.1903, + 0.1881, + 0.2226, + 0.1908, + 0.1906, + 0.193, + 0.1909, + 0.2183, + 0.1887, + 0.1887, + 0.1891, + 0.1897, + 0.2016, + 0.1895, + 0.1887, + 0.1882, + 0.1892, + 0.2019, + 0.1897, + 0.1883, + 0.1885, + 0.1879, + 0.1982, + 0.1983, + 0.1892, + 0.1875, + 0.1883 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 5160.995, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 9.32, + "Plan Rows": 45, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.013, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 9.32, + "Plan Rows": 45, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.009, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000003'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0989+00'::timestamp with time zone, '3ca7f544-85f6-3d47-6ce5-72ab53f46142'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.026, + "Triggers": [], + "Execution Time": 0.019 + } + }, + { + "chat_size": 4000, + "query": "offset", + "latency_ms": { + "p50": 0.508, + "p95": 0.7336, + "p99": 0.8249, + "mean": 0.5293 + }, + "raw_latency_ms": [ + 0.5041, + 0.5104, + 0.5184, + 0.512, + 0.5033, + 0.5045, + 0.5045, + 0.5029, + 0.511, + 0.4992, + 0.5117, + 0.4985, + 0.5143, + 0.4954, + 0.5082, + 0.4949, + 0.5114, + 0.4978, + 0.5138, + 0.4957, + 0.508, + 0.4949, + 0.5196, + 0.497, + 0.5098, + 0.4977, + 0.5486, + 0.8249, + 0.7336, + 0.632 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 1888.336, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 514.62, + "Total Cost": 514.76, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.605, + "Actual Total Time": 0.614, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 103, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 514.62, + "Plan Rows": 3783, + "Plan Width": 162, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.474, + "Actual Rows": 3950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000003'::uuid)", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 103, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.048, + "Triggers": [], + "Execution Time": 0.625 + } + }, + { + "chat_size": 1000, + "query": "keyset", + "latency_ms": { + "p50": 0.1999, + "p95": 0.2138, + "p99": 0.2153, + "mean": 0.2028 + }, + "raw_latency_ms": [ + 0.2033, + 0.2018, + 0.2092, + 0.2134, + 0.1999, + 0.2019, + 0.1978, + 0.1956, + 0.212, + 0.1998, + 0.199, + 0.1997, + 0.202, + 0.2108, + 0.1981, + 0.1999, + 0.1938, + 0.1989, + 0.2117, + 0.2138, + 0.2048, + 0.1965, + 0.2067, + 0.2068, + 0.1985, + 0.199, + 0.1949, + 0.1968, + 0.2153, + 0.2018 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 4925.03, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 4.44, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.014, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 4.44, + "Plan Rows": 1, + "Plan Width": 162, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.01, + "Actual Rows": 50, + "Actual Loops": 1, + "Index Cond": "((chat_id = '00000000-0000-0000-0000-000000000004'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0999+00'::timestamp with time zone, '7fa13929-f6d0-fc18-76f9-46306abe42ca'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 5, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.052, + "Triggers": [], + "Execution Time": 0.02 + } + }, + { + "chat_size": 1000, + "query": "offset", + "latency_ms": { + "p50": 0.2774, + "p95": 0.3663, + "p99": 0.4765, + "mean": 0.2948 + }, + "raw_latency_ms": [ + 0.2676, + 0.2789, + 0.2774, + 0.2675, + 0.2674, + 0.3249, + 0.3167, + 0.3663, + 0.4765, + 0.3109, + 0.3213, + 0.3049, + 0.2712, + 0.269, + 0.2758, + 0.3097, + 0.3083, + 0.3037, + 0.2809, + 0.2744, + 0.2719, + 0.2951, + 0.2722, + 0.2711, + 0.2714, + 0.2699, + 0.2692, + 0.2675, + 0.2922, + 0.2889 + ], + "successful_queries": 30, + "failed_queries": 0, + "observed_sequential_qps": 3388.315, + "explain": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 125.16, + "Total Cost": 132.09, + "Plan Rows": 50, + "Plan Width": 162, + "Actual Startup Time": 0.143, + "Actual Total Time": 0.151, + "Actual Rows": 50, + "Actual Loops": 1, + "Shared Hit Blocks": 28, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Only Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_covering_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 151.49, + "Plan Rows": 1090, + "Plan Width": 162, + "Actual Startup Time": 0.003, + "Actual Total Time": 0.116, + "Actual Rows": 950, + "Actual Loops": 1, + "Index Cond": "(chat_id = '00000000-0000-0000-0000-000000000004'::uuid)", + "Rows Removed by Index Recheck": 0, + "Heap Fetches": 0, + "Shared Hit Blocks": 28, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.039, + "Triggers": [], + "Execution Time": 0.157 + } + } + ] + } + ], + "databases": [ + "lab2_research_e09aa1b4_chat_only", + "lab2_research_e09aa1b4_keyset", + "lab2_research_e09aa1b4_covering" + ], + "backup_source": "lab2_research_e09aa1b4_keyset", + "before_backup": { + "counts": { + "users": 1, + "chats": 4, + "chat_members": 4, + "messages": 100000, + "sessions": 1, + "schema_migrations": 1, + "app_settings": 1 + }, + "all_messages_sha256": "dfdcdff4be5c439c47e7d26869631c0242760c92e0706d5977ccbad9d2d2ab7f", + "selected_messages": [ + [ + "7bb1fec0-75f7-3359-0707-a34b46f176e9", + "00000000-0000-0000-0000-000000000001", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:1", + "2026-01-01 00:00:00.000001+00:00" + ], + [ + "7db39161-2ccb-46b3-0e44-acfd673d8fad", + "00000000-0000-0000-0000-000000000001", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:500", + "2026-01-01 00:00:00.000500+00:00" + ], + [ + "5c88fd17-f50a-b61f-355c-031cb79944a9", + "00000000-0000-0000-0000-000000000001", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:80000", + "2026-01-01 00:00:00.080000+00:00" + ], + [ + "666950e5-a708-152a-411b-ce0ad25ac71b", + "00000000-0000-0000-0000-000000000002", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:80001", + "2026-01-01 00:00:00.080001+00:00" + ], + [ + "15c2c27f-473a-ec26-9e9c-2716b128f3ea", + "00000000-0000-0000-0000-000000000002", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:95000", + "2026-01-01 00:00:00.095000+00:00" + ], + [ + "d1d9c2f9-e7cf-1922-c139-76c555b5bac8", + "00000000-0000-0000-0000-000000000003", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:99000", + "2026-01-01 00:00:00.099000+00:00" + ], + [ + "def7ad13-3c77-47ce-172f-39ecad05bab1", + "00000000-0000-0000-0000-000000000004", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:100000", + "2026-01-01 00:00:00.100000+00:00" + ] + ] + } + }, + "after_snapshot_write": { + "source_messages_after_dump": 100001, + "expected_messages_missing_from_snapshot": 1 + }, + "backup_restore": { + "backup_seconds": 0.5288, + "dump_bytes": 3004503, + "dump_sha256": "f5ae26a1ed569f5027d5fafa26001a3429e90a18db1589e394d1a368aa990f46", + "new_container_and_volume": true, + "provision_seconds": 1.3216, + "restore_seconds": 0.6135, + "provision_plus_restore_seconds": 1.9351, + "verification": { + "counts": { + "users": 1, + "chats": 4, + "chat_members": 4, + "messages": 100000, + "sessions": 1, + "schema_migrations": 1, + "app_settings": 1 + }, + "all_messages_sha256": "dfdcdff4be5c439c47e7d26869631c0242760c92e0706d5977ccbad9d2d2ab7f", + "selected_messages": [ + [ + "7bb1fec0-75f7-3359-0707-a34b46f176e9", + "00000000-0000-0000-0000-000000000001", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:1", + "2026-01-01 00:00:00.000001+00:00" + ], + [ + "7db39161-2ccb-46b3-0e44-acfd673d8fad", + "00000000-0000-0000-0000-000000000001", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:500", + "2026-01-01 00:00:00.000500+00:00" + ], + [ + "5c88fd17-f50a-b61f-355c-031cb79944a9", + "00000000-0000-0000-0000-000000000001", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:80000", + "2026-01-01 00:00:00.080000+00:00" + ], + [ + "666950e5-a708-152a-411b-ce0ad25ac71b", + "00000000-0000-0000-0000-000000000002", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:80001", + "2026-01-01 00:00:00.080001+00:00" + ], + [ + "15c2c27f-473a-ec26-9e9c-2716b128f3ea", + "00000000-0000-0000-0000-000000000002", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:95000", + "2026-01-01 00:00:00.095000+00:00" + ], + [ + "d1d9c2f9-e7cf-1922-c139-76c555b5bac8", + "00000000-0000-0000-0000-000000000003", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:99000", + "2026-01-01 00:00:00.099000+00:00" + ], + [ + "def7ad13-3c77-47ce-172f-39ecad05bab1", + "00000000-0000-0000-0000-000000000004", + "11111111-1111-4111-8111-111111111111", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:100000", + "2026-01-01 00:00:00.100000+00:00" + ] + ], + "orphan_messages": 0 + }, + "all_counts_and_samples_equal": true, + "all_messages_checksum_equal": true, + "after_backup_canary_intentionally_absent": true + }, + "status": "PASS" +} diff --git a/evidence/research.png b/evidence/research.png new file mode 100644 index 00000000..8425bbf7 Binary files /dev/null and b/evidence/research.png differ diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..ee602dc1 --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://localhost:8082" +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/student-tests.txt b/evidence/student-tests.txt new file mode 100644 index 00000000..89258ecc --- /dev/null +++ b/evidence/student-tests.txt @@ -0,0 +1,17 @@ +python3 -m unittest discover -s student-tests -p 'test_*.py' -v +test_100_messages_10_clients (test_api.APItests.test_100_messages_10_clients) ... ok +test_authentication_before_access_and_foreign_chat (test_api.APItests.test_authentication_before_access_and_foreign_chat) ... ok +test_bad_password_unknown_username_and_basic_only_for_login (test_api.APItests.test_bad_password_unknown_username_and_basic_only_for_login) ... ok +test_chat_rollback_and_membership_validation (test_api.APItests.test_chat_rollback_and_membership_validation) ... ok +test_empty_message_unicode_and_sender (test_api.APItests.test_empty_message_unicode_and_sender) ... ok +test_logout_revokes_only_current_session (test_api.APItests.test_logout_revokes_only_current_session) ... ok +test_malformed_json_and_types (test_api.APItests.test_malformed_json_and_types) ... ok +test_multiple_pages_bad_limits_and_cross_resource_cursor (test_api.APItests.test_multiple_pages_bad_limits_and_cross_resource_cursor) ... ok +test_racing_registration (test_api.APItests.test_racing_registration) ... ok +test_read_after_write_during_pagination (test_api.APItests.test_read_after_write_during_pagination) ... ok +test_unicode_password_128_codepoints_and_colon (test_api.APItests.test_unicode_password_128_codepoints_and_colon) ... ok + +---------------------------------------------------------------------- +Ran 11 tests in 6.839s + +OK diff --git a/infra/SCHEMA.md b/infra/SCHEMA.md new file mode 100644 index 00000000..d7ca9361 --- /dev/null +++ b/infra/SCHEMA.md @@ -0,0 +1,14 @@ +# Схема и транзакционные границы до реализации запросов + +- `users`: UUID PK, уникальный username, display_name, scrypt hash (соль и параметры в строке), created_at. Длина и шаблон username проверяются также CHECK в PostgreSQL. +- `chats`: UUID PK, title, created_at, last_message_at для монотонного порядка сообщений одного чата. +- `chat_members`: составной PK (chat_id,user_id), FK на chats/users; индекс (user_id,chat_id) для списка доступных чатов. +- `messages`: UUID PK, chat_id/sender_id/text/created_at; составной FK (chat_id,sender_id) на членство. Индекс (chat_id,created_at,id) поддерживает WHERE chat_id=? AND (created_at,id)>(?,?) ORDER BY created_at,id LIMIT ?. +- `sessions`: SHA-256 случайного 256-битного токена как PK; FK user_id; абсолютное expires_at. Оригинал токена только у клиента. Источник истины — PostgreSQL; logout удаляет строку, expiry сравнивается с часами БД. +- `app_settings`: постоянный секрет подписи cursor. Миграции — schema_migrations с SHA-256 файлов; применение под advisory lock и одной транзакцией на запуск миграций. + +Транзакции: регистрация и unique username; создание чата и всех участников; обновление last_message_at и вставка сообщения; создание или отзыв сессии. Исключение внутри контекста соединения откатывает всю операцию. Отказ неизвестного участника не оставляет чат. Все значения параметризованы. + +Пул: 1–8 соединений, максимум 32 ожидающих, ожидание 2 с; connect_timeout=2 с, statement_timeout=2 с, lock_timeout=1 с, idle-in-transaction=5 с. История читается из PostgreSQL без кэша, поэтому подтверждённая запись не скрывается устаревшим cache hit. + +Valkey: cache-aside только неизменяемого публичного User, TTL 300 с. Сессия всегда проверяется в PostgreSQL до чтения кэша; logout/expiry не зависят от Valkey. При отказе/flush/restart — запрос профиля в PostgreSQL и последующее заполнение. Обновлений/удалений пользователей в контракте нет; для их добавления потребуется инвалидация. Пароли, их хеши, оригиналы токенов в кэш не попадают. diff --git a/migrations/001_initial.sql b/migrations/001_initial.sql new file mode 100644 index 00000000..97333849 --- /dev/null +++ b/migrations/001_initial.sql @@ -0,0 +1,37 @@ +CREATE TABLE users ( + id uuid PRIMARY KEY, + username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'), + display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100 AND display_name !~ '^\s*$'), + password_hash text NOT NULL, + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); +CREATE TABLE chats ( + id uuid PRIMARY KEY, + title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100 AND title !~ '^\s*$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + last_message_at timestamptz NOT NULL DEFAULT '-infinity' +); +CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats(id), + user_id uuid NOT NULL REFERENCES users(id), + PRIMARY KEY (chat_id, user_id) +); +CREATE INDEX chat_members_user_chat_idx ON chat_members(user_id, chat_id); +CREATE INDEX chats_created_id_idx ON chats(created_at,id); +CREATE TABLE messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL, + sender_id uuid NOT NULL, + text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000 AND text !~ '^\s*$'), + created_at timestamptz NOT NULL, + FOREIGN KEY (chat_id,sender_id) REFERENCES chat_members(chat_id,user_id) +); +CREATE INDEX messages_chat_created_id_idx ON messages(chat_id,created_at,id); +CREATE TABLE sessions ( + token_hash bytea PRIMARY KEY CHECK (octet_length(token_hash)=32), + user_id uuid NOT NULL REFERENCES users(id), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + expires_at timestamptz NOT NULL +); +CREATE INDEX sessions_expires_idx ON sessions(expires_at); +CREATE TABLE app_settings (name text PRIMARY KEY, value text NOT NULL); diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..fd3a9267 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +psycopg==3.3.6 +psycopg-binary==3.3.6 +psycopg-pool==3.3.1 +redis==6.4.0 +typing_extensions==4.16.0 diff --git a/research/README.md b/research/README.md new file mode 100644 index 00000000..221cdd0d --- /dev/null +++ b/research/README.md @@ -0,0 +1,23 @@ +# Трек 2: план запроса, стоимость записи, backup и восстановление + +Гипотеза: индекс только `chat_id` не может быстро продолжить позднюю страницу; составной `(chat_id,created_at,id)` устранит фильтрацию всей истории и сортировку. `INCLUDE(sender_id,text)` может дать Index Only Scan, но увеличит размер индекса и цену записи. OFFSET останется линейным даже с правильным индексом. + +```sh +make up +make research +# Рисунок необязателен для выполнения эксперимента: +.venv/bin/python -m pip install -r research/requirements.txt +.venv/bin/python research/summarize.py +``` + +Скрипт использует три новые временные базы в PostgreSQL текущего Compose-проекта. Рабочую `messenger` не очищает и её индексы не меняет. Каждая исследовательская БД содержит одинаковые 100 000 синтетических сообщений в чатах размером 80 000 / 15 000 / 4 000 / 1 000. Все FK, PK и CHECK соответствуют рабочей миграции. Индекс истории — единственная структурная разница: chat_only, keyset, covering. `text` имеет примерно 100 ASCII-символов; идентификаторы и исходные данные детерминированы. Исследовательский пользователь не предназначен для входа через API. + +Измерения выполняются драйвером psycopg из отдельного процесса внутри API-контейнера, PostgreSQL — в другом контейнере. На оба выделено по 1 CPU и 512 MiB. Для каждого индекса, размера чата и варианта keyset/OFFSET: 3 прогрева и 30 последовательных чтений по 50 строк возле конца исходной истории. Сохраняются все задержки, p50/p95/p99 nearest-rank, фактические последовательные QPS, ошибки, EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON). Это измерения SQL, не HTTP-пропускной способности. + +Стоимость записи: 10 транзакций по 1000 строк, EXPLAIN (ANALYZE, BUFFERS, WAL) реального INSERT и настоящий COMMIT каждой транзакции. Сохраняются SQL execution time, WAL bytes, dirtied blocks, wall time с COMMIT. После этого только исследовательские write-строки удаляются, выполняется VACUUM ANALYZE; чтение и backup снова используют ровно 100 000 исходных сообщений. Таблицы всех трёх вариантов проходят одинаковую процедуру. Время включает сбор плана, поэтому не является чистой оценкой одиночного INSERT. + +Backup: `pg_dump -Fc` базы с рабочим составным индексом, файл `backups/lab2-research.dump` исключён из Git. После dump добавляется canary-запись. Затем создаются НОВЫЙ контейнер PostgreSQL и НОВЫЙ volume; `pg_restore --exit-on-error` загружает снимок. Сверяются количества всех таблиц, 7 выбранных сообщений разных чатов, SHA-256 всей истории и отсутствие сообщений без членства. Canary после снимка ожидаемо не восстанавливается. Время подготовки пустого сервера и самого restore учитывается отдельно. + +Все временные исследовательские БД, целевой контейнер и его volume удаляются в finally. Рабочий volume не удаляется. Dump остаётся локально для повторного изучения, не содержит реальных данных. Доказательства: `evidence/research.json`, `explain-plans.json`, CSV, таблица и рисунок. + +Ограничения: один компьютер, последовательный клиент, фиксированный порядок вариантов, прогретые страницы, 30 чтений и 10 write-batch. Перцентили малыми выборками не задают SLA; результаты не доказывают HTTP capacity или влияние конкурентных записей. Измеряется локальный логический backup без сетевого копирования и восстановления настроек ОС/ролей. RPO — момент снимка, а не ноль; для восстановления более поздних COMMIT потребуются WAL-архив и PITR. Новое хранилище запущено на том же хосте, отказ самого хоста не моделируется. Размеры CPU/RAM заданы явно, отдельное профилирование CPU не проводилось. diff --git a/research/requirements.txt b/research/requirements.txt new file mode 100644 index 00000000..0cb4092e --- /dev/null +++ b/research/requirements.txt @@ -0,0 +1,3 @@ +# Optional: plotting only; SQL experiment itself uses the API container's driver. +matplotlib==3.10.9 +numpy==2.4.4 diff --git a/research/run.py b/research/run.py new file mode 100644 index 00000000..129a4350 --- /dev/null +++ b/research/run.py @@ -0,0 +1,103 @@ +"""Measure indexes, back up, restore to a NEW PostgreSQL container+volume, verify.""" +import datetime as dt +import hashlib +import json +import os +from pathlib import Path +import subprocess +import sys +import time +import uuid + +ROOT=Path(__file__).resolve().parents[1] +IMAGE='postgres:17.9-bookworm@sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641' + + +def execute(command,**kwargs): + return subprocess.run(command,cwd=ROOT,check=True,capture_output=True,**kwargs) + + +def compose(*args,**kwargs): + return execute(['docker','compose',*args],**kwargs) + + +def main(): + prefix='lab2_research_'+uuid.uuid4().hex[:8] + target='mtusi-lab2-restore-'+uuid.uuid4().hex[:8] + volume=target+'-data' + worker=(ROOT/'research/worker.py').read_text() + def action(name,**variables): + flags=[] + for key,value in {'RESEARCH_ACTION':name,'RESEARCH_PREFIX':prefix,**variables}.items(): + flags.extend(['-e',key+'='+value]) + return json.loads(compose('exec','-T',*flags,'api','python','-',input=worker,text=True).stdout) + report={'date_utc':dt.datetime.now(dt.timezone.utc).isoformat(), + 'source_sha256':{str(p.relative_to(ROOT)):hashlib.sha256(p.read_bytes()).hexdigest() for p in [ROOT/'src/store.py',ROOT/'migrations/001_initial.sql',ROOT/'research/worker.py']}, + 'method':'Isolated skewed PostgreSQL datasets; 30 sequential reads after 3 warmups; 10 committed INSERT batches of 1000 rows; fresh restore volume.', + 'limitations':['Local single-host measurements; warm buffers; no concurrent HTTP capacity claim.', + 'EXPLAIN insert wall times include plan collection and COMMIT; 10 batches do not establish a write SLA.', + 'Logical backup is a snapshot, not PITR or HA; the post-dump canary is intentionally absent after restore.']} + print('Benchmarking 100000 messages, 4 skewed chats, 3 indexes and 2 queries...',flush=True) + try: + report['query_experiment']=action('benchmark') + source=report['query_experiment']['backup_source'] + backup_dir=ROOT/'backups' + backup_dir.mkdir(mode=0o700,exist_ok=True) + dump=backup_dir/'lab2-research.dump' + started=time.perf_counter() + with dump.open('wb') as output: + subprocess.run(['docker','compose','exec','-T','postgres','pg_dump','-U','messenger','-d',source,'-Fc'],cwd=ROOT,stdout=output,stderr=subprocess.PIPE,check=True) + dump.chmod(0o600) + backup_seconds=time.perf_counter()-started + report['after_snapshot_write']=action('canary',RESEARCH_SOURCE=source) + cid=compose('ps','-q','postgres',text=True).stdout.strip() + inspect=json.loads(execute(['docker','inspect',cid],text=True).stdout)[0] + networks=list(inspect['NetworkSettings']['Networks']) + assert len(networks)==1 + provision_start=time.perf_counter() + execute(['docker','volume','create',volume],text=True) + execute(['docker','run','-d','--name',target,'--network',networks[0], + '--cpus=1','--memory=512m','--memory-swap=512m','--pids-limit=128', + '--mount','type=volume,source='+volume+',target=/var/lib/postgresql/data', + '--mount','type=bind,source='+str(ROOT/'secrets/db_password')+',target=/run/secrets/db_password,readonly', + '-e','POSTGRES_DB=messenger','-e','POSTGRES_USER=messenger','-e','POSTGRES_PASSWORD_FILE=/run/secrets/db_password',IMAGE],text=True) + deadline=time.monotonic()+30 + while True: + status=subprocess.run(['docker','exec',target,'pg_isready','-h','127.0.0.1','-U','messenger','-d','messenger'],capture_output=True) + if status.returncode==0: + break + if time.monotonic()>deadline: + raise RuntimeError('new restore PostgreSQL did not become ready') + time.sleep(.2) + provisioning=time.perf_counter()-provision_start + started=time.perf_counter() + with dump.open('rb') as input_file: + subprocess.run(['docker','exec','-i',target,'pg_restore','--exit-on-error','--no-owner','--no-privileges','-U','messenger','-d','messenger'],stdin=input_file,stdout=subprocess.PIPE,stderr=subprocess.PIPE,check=True) + restore_seconds=time.perf_counter()-started + verified=action('verify',RESTORE_HOST=target) + expected=report['query_experiment']['before_backup'] + assert verified['counts']==expected['counts'] + assert verified['all_messages_sha256']==expected['all_messages_sha256'] + assert verified['selected_messages']==expected['selected_messages'] + assert verified['orphan_messages']==0 + assert verified['counts']['messages']==100000 + report['backup_restore']={'backup_seconds':round(backup_seconds,4),'dump_bytes':dump.stat().st_size, + 'dump_sha256':hashlib.sha256(dump.read_bytes()).hexdigest(), + 'new_container_and_volume':True,'provision_seconds':round(provisioning,4), + 'restore_seconds':round(restore_seconds,4),'provision_plus_restore_seconds':round(provisioning+restore_seconds,4), + 'verification':verified,'all_counts_and_samples_equal':True,'all_messages_checksum_equal':True, + 'after_backup_canary_intentionally_absent':True} + report['status']='PASS' + destination=ROOT/'evidence/research.json' + destination.write_text(json.dumps(report,ensure_ascii=False,indent=2)+'\n') + print('PASS: restored 100000 messages to a new PostgreSQL container/volume; counts, 7 samples, complete SHA256 and foreign keys verified.',flush=True) + execute([sys.executable,'research/summarize.py'],text=True) + finally: + subprocess.run(['docker','rm','-f',target],capture_output=True) + # This volume was created by this experiment and contains synthetic data only. + subprocess.run(['docker','volume','rm',volume],capture_output=True) + action('cleanup') + + +if __name__=='__main__': + main() diff --git a/research/summarize.py b/research/summarize.py new file mode 100644 index 00000000..0384db47 --- /dev/null +++ b/research/summarize.py @@ -0,0 +1,56 @@ +"""Export raw-backed CSV, plans, a readable comparison and optional figures.""" +import csv +import json +from pathlib import Path +import statistics + +ROOT=Path(__file__).resolve().parents[1] +r=json.loads((ROOT/'evidence/research.json').read_text()) +experiment=r['query_experiment'] +rows=[] +plans=[] +lines=['# План запроса и восстановление','', '| Индекс | Сообщений в чате | Запрос | p50, мс | p95, мс | p99, мс | Sequential QPS |','| --- | ---: | --- | ---: | ---: | ---: | ---: |'] +for variant in experiment['variants']: + for read in variant['reads']: + row={'index':variant['name'],'chat_size':read['chat_size'],'query':read['query'],**read['latency_ms'],'qps':read['observed_sequential_qps']} + rows.append(row) + lines.append('| '+' | '.join(str(row[k]) for k in ('index','chat_size','query','p50','p95','p99','qps'))+' |') + plans.append({'index':variant['name'],'chat_size':read['chat_size'],'query':read['query'],'explain_analyze_buffers':read['explain']}) +lines.extend(['','| Индекс | Индексы messages, MiB | INSERT 1000 + COMMIT p50, мс | p95, мс | Средний WAL на 1000 строк, bytes |','| --- | ---: | ---: | ---: | ---: |']) +for v in experiment['variants']: + lines.append(f"| {v['name']} | {v['indexes_bytes']/1024**2:.3f} | {v['write_commit_ms']['p50']} | {v['write_commit_ms']['p95']} | {statistics.mean(w['wal_bytes'] for w in v['write_runs']):.0f} |") +b=r['backup_restore'] +lines.extend(['',f"Backup: {b['dump_bytes']} bytes, {b['backup_seconds']} с. Новый PostgreSQL/volume: {b['provision_seconds']} с. Restore: {b['restore_seconds']} с. Provision + restore: {b['provision_plus_restore_seconds']} с.",'', + 'Сверены количества всех таблиц, 7 выбранных сообщений, SHA-256 всей истории из 100 000 сообщений, отсутствие нарушенных связей. Запись после снимка ожидаемо отсутствует в восстановленной БД.']) +(ROOT/'evidence/research-summary.md').write_text('\n'.join(lines)+'\n') +(ROOT/'evidence/explain-plans.json').write_text(json.dumps(plans,ensure_ascii=False,indent=2)+'\n') +with (ROOT/'evidence/query-measurements.csv').open('w',newline='') as f: + writer=csv.DictWriter(f,fieldnames=list(rows[0]),lineterminator='\n') + writer.writeheader(); writer.writerows(rows) +try: + import matplotlib + matplotlib.use('Agg') + import matplotlib.pyplot as plt +except ImportError: + print('Tables/plans saved; install research/requirements.txt to build optional figures.') +else: + colors={'chat_only':'#d77a4b','keyset':'#227cba','covering':'#319667'} + fig,axes=plt.subplots(1,3,figsize=(15,5),constrained_layout=True) + for kind,style in [('keyset','-'),('offset','--')]: + for name,color in colors.items(): + selected=sorted((row for row in rows if row['index']==name and row['query']==kind),key=lambda x:x['chat_size']) + axes[0].plot([x['chat_size'] for x in selected],[x['p95'] for x in selected],style,marker='o',color=color,label=name+' / '+kind) + axes[0].set(xscale='log',yscale='log',xlabel='Сообщений в чате',ylabel='p95, мс',title='Чтение поздней страницы') + axes[0].legend(fontsize=7) + names=list(colors) + axes[1].bar(names,[v['write_commit_ms']['p50'] for v in experiment['variants']],color=list(colors.values())) + axes[1].set(ylabel='мс',title='1000 INSERT + COMMIT, p50') + axes[2].bar(names,[v['indexes_bytes']/1024**2 for v in experiment['variants']],color=list(colors.values())) + axes[2].set(ylabel='MiB',title='Размер всех индексов messages') + for ax in axes: + ax.spines[['top','right']].set_visible(False) + ax.grid(axis='y',alpha=.2); ax.set_axisbelow(True) + fig.suptitle('PostgreSQL: 100 000 сообщений, неравномерные чаты, 1 CPU / 512 MiB',weight='bold') + fig.savefig(ROOT/'evidence/research.png',dpi=160) + plt.close(fig) +print('\n'.join(lines)) diff --git a/research/worker.py b/research/worker.py new file mode 100644 index 00000000..00c80185 --- /dev/null +++ b/research/worker.py @@ -0,0 +1,156 @@ +"""Controlled SQL measurements on isolated databases, never the application DB.""" +import datetime as dt +import hashlib +import json +import math +import os +from pathlib import Path +import statistics +import sys +import time +import uuid + +import psycopg +from psycopg import sql +from psycopg.conninfo import make_conninfo + +BASE=Path(os.environ['DATABASE_URL_FILE']).read_text().strip() +SIZES=[80000,15000,4000,1000] +USER=uuid.UUID('11111111-1111-4111-8111-111111111111') +START=dt.datetime(2026,1,1,tzinfo=dt.timezone.utc) + + +def stats(values): + ordered=sorted(values) + def pct(p): + return round(ordered[math.ceil(p*len(ordered))-1],4) + return {'p50':pct(.5),'p95':pct(.95),'p99':pct(.99),'mean':round(statistics.mean(values),4)} + + +def fingerprint(connection): + counts={table:connection.execute(sql.SQL('SELECT count(*) FROM {}').format(sql.Identifier(table))).fetchone()[0] + for table in ('users','chats','chat_members','messages','sessions','schema_migrations','app_settings')} + digest=hashlib.sha256() + with connection.cursor(name='fingerprint') as cursor: + cursor.execute('SELECT id,chat_id,sender_id,text,created_at FROM messages ORDER BY id') + for row in cursor: + digest.update((json.dumps(row,default=str,separators=(',',':'))+'\n').encode()) + samples=[] + for offset in (1,500,80000,80001,95000,99000,100000): + row=connection.execute('SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE id=md5(%s)::uuid',('research-message-'+str(offset),)).fetchone() + samples.append([str(v) if isinstance(v,(uuid.UUID,dt.datetime)) else v for v in row]) + return {'counts':counts,'all_messages_sha256':digest.hexdigest(),'selected_messages':samples} + + +def benchmark(): + prefix=os.environ['RESEARCH_PREFIX'] + report={'postgres_version':None,'dataset_messages':sum(SIZES),'chat_sizes':SIZES,'read_repeats':30,'warmup_queries':3, + 'write_batches':10,'write_batch_size':1000,'limits':{'postgres_cpus':1,'postgres_memory_mib':512,'client_cpus':1,'client_memory_mib':512}, + 'variants':[]} + databases=[] + for variant in ('chat_only','keyset','covering'): + name=prefix+'_'+variant + with psycopg.connect(BASE,autocommit=True) as admin: + admin.execute(sql.SQL('CREATE DATABASE {}').format(sql.Identifier(name))) + databases.append(name) + dsn=make_conninfo(BASE,dbname=name) + with psycopg.connect(dsn,autocommit=True) as c: + report['postgres_version']=c.execute('SHOW server_version').fetchone()[0] + migration=(Path('/app/migrations/001_initial.sql')).read_text() + c.execute('CREATE TABLE schema_migrations(version text PRIMARY KEY,sha256 text NOT NULL,applied_at timestamptz NOT NULL DEFAULT clock_timestamp())') + c.execute(migration) + c.execute('INSERT INTO schema_migrations(version,sha256) VALUES (%s,%s)',('001_initial.sql',hashlib.sha256(migration.encode()).hexdigest())) + c.execute("INSERT INTO app_settings(name,value) VALUES ('cursor_key',%s)",('00'*32,)) + if variant=='chat_only': + c.execute('DROP INDEX messages_chat_created_id_idx') + c.execute('CREATE INDEX messages_chat_only_idx ON messages(chat_id)') + elif variant=='covering': + c.execute('DROP INDEX messages_chat_created_id_idx') + c.execute('CREATE INDEX messages_chat_covering_idx ON messages(chat_id,created_at,id) INCLUDE (sender_id,text)') + c.execute('INSERT INTO users(id,username,display_name,password_hash) VALUES (%s,%s,%s,%s)',(USER,'research_user','Synthetic fixture','research-only-not-a-real-password')) + c.execute("INSERT INTO sessions(token_hash,user_id,expires_at) VALUES (%s,%s,clock_timestamp()+interval '1 hour')",(hashlib.sha256(b'synthetic-not-a-session-token').digest(),USER)) + offset=0 + seeded=time.perf_counter() + for index,size in enumerate(SIZES,1): + cid=uuid.UUID(int=index) + c.execute('INSERT INTO chats(id,title) VALUES (%s,%s)',(cid,'Synthetic chat '+str(index))) + c.execute('INSERT INTO chat_members(chat_id,user_id) VALUES (%s,%s)',(cid,USER)) + c.execute("INSERT INTO messages(id,chat_id,sender_id,text,created_at) SELECT md5('research-message-'||g)::uuid,%s,%s,repeat('x',100)||':'||g,%s::timestamptz+g*interval '1 microsecond' FROM generate_series(%s,%s) g",(cid,USER,START,offset+1,offset+size)) + offset+=size + seed_s=time.perf_counter()-seeded + c.execute('VACUUM (ANALYZE) messages') + writes=[] + for batch in range(10): + started=time.perf_counter() + with c.transaction(): + plan=c.execute("EXPLAIN (ANALYZE, BUFFERS, WAL, FORMAT JSON) INSERT INTO messages(id,chat_id,sender_id,text,created_at) SELECT md5(%s||g)::uuid,%s,%s,'write-'||repeat('x',100)||':'||g,clock_timestamp()+g*interval '1 microsecond' FROM generate_series(1,1000) g",('write-'+str(batch)+'-',uuid.UUID(int=1),USER)).fetchone()[0][0] + writes.append({'commit_wall_ms':round((time.perf_counter()-started)*1000,4),'execution_ms':plan['Execution Time'], + 'wal_bytes':plan['Plan'].get('WAL Bytes',0),'shared_dirtied_blocks':plan['Plan'].get('Shared Dirtied Blocks',0)}) + c.execute("DELETE FROM messages WHERE text LIKE 'write-%'") + c.execute('VACUUM (ANALYZE) messages') + row={'name':variant,'database':name,'seed_seconds':round(seed_s,4), + 'table_bytes':c.execute("SELECT pg_table_size('messages')").fetchone()[0], + 'indexes_bytes':c.execute("SELECT pg_indexes_size('messages')").fetchone()[0], + 'write_runs':writes,'write_commit_ms':stats([w['commit_wall_ms'] for w in writes]),'reads':[]} + offset=0 + for index,size in enumerate(SIZES,1): + cid=uuid.UUID(int=index) + cursor_at=offset+size-100 + cursor_time=START+dt.timedelta(microseconds=cursor_at) + cursor_id=uuid.UUID(hashlib.md5(('research-message-'+str(cursor_at)).encode()).hexdigest()) + for kind in ('keyset','offset'): + if kind=='keyset': + query='SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=%s AND (created_at,id)>(%s,%s) ORDER BY created_at,id LIMIT 50' + parameters=(cid,cursor_time,cursor_id) + else: + query='SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=%s ORDER BY created_at,id OFFSET %s LIMIT 50' + parameters=(cid,size-100) + for _ in range(3): + assert len(c.execute(query,parameters).fetchall())==50 + timings=[] + total_started=time.perf_counter() + for _ in range(30): + started=time.perf_counter() + result=c.execute(query,parameters).fetchall() + assert len(result)==50 + timings.append((time.perf_counter()-started)*1000) + total_elapsed=time.perf_counter()-total_started + plan=c.execute('EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) '+query,parameters).fetchone()[0][0] + row['reads'].append({'chat_size':size,'query':kind,'latency_ms':stats(timings),'raw_latency_ms':[round(v,4) for v in timings], + 'successful_queries':30,'failed_queries':0,'observed_sequential_qps':round(30/total_elapsed,3),'explain':plan}) + offset+=size + report['variants'].append(row) + report['databases']=databases + report['backup_source']=prefix+'_keyset' + # Keep the production-index database immutable until the dump starts. + with psycopg.connect(make_conninfo(BASE,dbname=report['backup_source'])) as connection: + report['before_backup']=fingerprint(connection) + print(json.dumps(report,ensure_ascii=False,default=str)) + + +def verify(): + dsn=make_conninfo(BASE,host=os.environ['RESTORE_HOST'],dbname='messenger') + with psycopg.connect(dsn) as connection: + report=fingerprint(connection) + orphan=connection.execute('SELECT count(*) FROM messages m LEFT JOIN chat_members cm ON (cm.chat_id,cm.user_id)=(m.chat_id,m.sender_id) WHERE cm.user_id IS NULL').fetchone()[0] + report['orphan_messages']=orphan + print(json.dumps(report,ensure_ascii=False)) + + +def canary(): + name=os.environ['RESEARCH_SOURCE'] + with psycopg.connect(make_conninfo(BASE,dbname=name)) as connection: + connection.execute("INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES (%s,%s,%s,'AFTER_BACKUP_CANARY',clock_timestamp())",(uuid.uuid4(),uuid.UUID(int=1),USER)) + count=connection.execute('SELECT count(*) FROM messages').fetchone()[0] + print(json.dumps({'source_messages_after_dump':count,'expected_messages_missing_from_snapshot':1})) + + +def cleanup(): + prefix=os.environ['RESEARCH_PREFIX'] + with psycopg.connect(BASE,autocommit=True) as admin: + for variant in ('chat_only','keyset','covering'): + admin.execute(sql.SQL('DROP DATABASE IF EXISTS {} WITH (FORCE)').format(sql.Identifier(prefix+'_'+variant))) + print(json.dumps({'isolated_databases_removed':True})) + + +{'benchmark':benchmark,'verify':verify,'canary':canary,'cleanup':cleanup}[os.environ['RESEARCH_ACTION']]() diff --git a/scripts/bootstrap.py b/scripts/bootstrap.py new file mode 100644 index 00000000..de06c781 --- /dev/null +++ b/scripts/bootstrap.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +"""Generate local credentials once, without printing or committing their values.""" +import os +from pathlib import Path +import secrets + +ROOT = Path(__file__).resolve().parents[1] +folder = ROOT / 'secrets' +folder.mkdir(mode=0o700, exist_ok=True) + +def write_once(name, value): + path = folder / name + if not path.exists(): + descriptor = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) + with os.fdopen(descriptor,'w') as file: + file.write(value + '\n') + # Parent directory is 0700 on the host; mounted files must also be readable + # by nonroot service UIDs inside their explicitly selected containers. + path.chmod(0o644) + return path.read_text().strip() + +password = write_once('db_password', secrets.token_hex(24)) +cache_password = write_once('cache_password', secrets.token_hex(24)) +write_once('database_url', f'postgresql://messenger:{password}@postgres:5432/messenger') +write_once('cache_url', f'redis://:{cache_password}@valkey:6379/0') +write_once('valkey.conf', f'bind 0.0.0.0\nprotected-mode yes\nport 6379\nrequirepass {cache_password}\nmaxmemory 64mb\nmaxmemory-policy allkeys-lru\nsave ""\nappendonly no\n') +env = ROOT / '.env' +if not env.exists(): + env.write_text((ROOT / '.env.example').read_text()) +print('PASS: local secret files ready; existing credentials preserved; values not printed') diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..57814661 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,6 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +# Actual processes and all stores restart; named volumes are preserved. +docker compose restart --timeout 10 api postgres valkey +docker compose up -d --wait --wait-timeout 45 diff --git a/src/app.py b/src/app.py new file mode 100644 index 00000000..e90bd13a --- /dev/null +++ b/src/app.py @@ -0,0 +1,104 @@ +"""Lab2 entry point, retaining the HTTP transport from the Python lab1.""" +import argparse +import logging +import os +import re +import signal +import threading +from urllib.parse import urlsplit +from urllib.request import urlopen + +from http_transport import APIError, Handler, Server, invalid, page_query + + +class API(Handler): + def authorization(self): + values = self.headers.get_all('Authorization',[]) + return values[0] if len(values) == 1 else None + + def dispatch(self): + parsed = urlsplit(self.path) + path, method, store = parsed.path, self.command, self.server.store + if method == 'GET' and path == '/health/live': + return 200, {'status':'ok'} + if self.server.stopping.is_set(): + raise APIError(503,'unavailable','Сервер завершает работу') + if method == 'GET' and path == '/health/ready': + store.ready() + return 200, {'status':'ok'} + if method == 'POST' and path == '/api/v1/users': + return 201, store.user(self.json_body()) + if method == 'POST' and path == '/api/v1/auth/sessions': + if self.headers.get('Transfer-Encoding') or self.headers.get('Content-Length','0') != '0': + raise invalid('Вход не принимает тело запроса') + return 201, store.login(self.authorization()) + if not path.startswith('/api/v1/'): + raise APIError(404,'not_found','Объект не найден') + actor = store.authenticate(self.authorization()) + if method == 'DELETE' and path == '/api/v1/auth/sessions/current': + store.logout(self.authorization()) + return 204, None + if method == 'GET' and path == '/api/v1/users/me': + return 200, store.profile(actor) + if path == '/api/v1/chats': + if method == 'POST': + return 201, store.chat(actor,self.json_body()) + if method == 'GET': + return 200, store.list_chats(actor,*page_query(parsed.query)) + match = re.fullmatch(r'/api/v1/chats/([^/]+)(/messages)?',path) + if match: + cid, messages = match.groups() + chat = store.accessible(actor,cid) + if method == 'GET' and not messages: + return 200, chat + if method == 'POST' and messages: + return 201, store.message(actor,cid,self.json_body()) + if method == 'GET' and messages: + return 200, store.history(actor,cid,*page_query(parsed.query)) + raise APIError(404,'not_found','Объект не найден') + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--healthcheck',action='store_true') + parser.add_argument('--migrate',action='store_true') + args = parser.parse_args() + host = os.environ.get('APP_HOST','0.0.0.0') + port = int(os.environ.get('APP_PORT','8080')) + if args.healthcheck: + target = '127.0.0.1' if host == '0.0.0.0' else host + with urlopen(f'http://{target}:{port}/health/ready',timeout=3) as response: + if response.status != 200: + raise SystemExit(1) + return + logging.basicConfig(level=logging.INFO,format='%(asctime)s %(levelname)s %(message)s') + from migrate import migrate + migrate() + if args.migrate: + print('PASS: migrations applied') + return + from store import Store + store = Store() + server = Server((host,port),store,API) + deadline = threading.Timer(7.5,server.close_active) + deadline.daemon = True + def stop(signum,_frame): + if not server.stopping.is_set(): + server.stopping.set() + logging.info('shutdown_started signal=%s grace_seconds=8',signum) + deadline.start() + threading.Thread(target=server.shutdown,daemon=True).start() + signal.signal(signal.SIGTERM,stop) + signal.signal(signal.SIGINT,stop) + logging.info('started host=%s port=%s',host,port) + try: + server.serve_forever(poll_interval=.1) + finally: + server.server_close() + deadline.cancel() + store.close() + logging.info('shutdown_complete') + + +if __name__ == '__main__': + main() diff --git a/src/config.py b/src/config.py new file mode 100644 index 00000000..eb7d3548 --- /dev/null +++ b/src/config.py @@ -0,0 +1,14 @@ +"""Only read secret values from files; never print DSNs or credentials.""" +import os +from pathlib import Path + + +def secret(name): + return Path(os.environ[name + '_FILE']).read_text().strip() + + +def integer(name, default, minimum=1, maximum=86400): + value = int(os.environ.get(name, str(default))) + if not minimum <= value <= maximum: + raise ValueError('Invalid configuration: ' + name) + return value diff --git a/src/http_transport.py b/src/http_transport.py new file mode 100644 index 00000000..5db85c41 --- /dev/null +++ b/src/http_transport.py @@ -0,0 +1,198 @@ +"""Reusable strict JSON/HTTP transport carried over from the Python lab1.""" +from __future__ import annotations + +import json +import logging +import re +import socket +import threading +import time +import uuid +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from urllib.parse import parse_qs + +LOG = logging.getLogger('messenger') +MAX_BODY_BYTES = 65536 + + +class APIError(Exception): + def __init__(self, status: int, code: str, message: str, headers=None): + self.status, self.code, self.message = status, code, message + self.headers = headers or {} + + +def invalid(message='Неверный запрос'): + return APIError(400, 'invalid_request', message) + + +def identifier(value): + if not isinstance(value, str) or not re.fullmatch( + r'[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}', value + ): + raise invalid('Ожидался UUID') + return str(uuid.UUID(value)) + + +def text(value, maximum): + if not isinstance(value, str) or not 1 <= len(value) <= maximum or not value.strip(): + raise invalid('Неверная длина или тип строки') + # Reject unpaired JSON surrogate escapes; valid supplementary Unicode is preserved. + try: + value.encode('utf-8') + except UnicodeError: + raise invalid('Неверная строка Unicode') from None + return value + + +def fields(body, required): + if not isinstance(body, dict) or set(body) != set(required): + raise invalid('Неверный набор полей JSON') + + +def page_query(query): + try: + values = parse_qs(query, keep_blank_values=True, strict_parsing=True) + except ValueError: + raise invalid('Неверная строка запроса') from None + if set(values) - {'limit', 'cursor'} or any(len(v) != 1 for v in values.values()): + raise invalid('Неизвестные или повторяющиеся параметры') + raw_limit = values.get('limit', ['50'])[0] + if not re.fullmatch(r'[0-9]+', raw_limit) or len(raw_limit) > 3: + raise invalid('Limit должен быть целым от 1 до 100') + limit = int(raw_limit) + if not 1 <= limit <= 100: + raise invalid('Limit должен быть целым от 1 до 100') + cursor = values.get('cursor', [None])[0] + if cursor == '': + raise invalid('Cursor не может быть пустым') + return limit, cursor + + +class Server(ThreadingHTTPServer): + daemon_threads = False + block_on_close = True + request_queue_size = 128 + allow_reuse_address = True + + def __init__(self, address, store, handler=None, request_logging=True): + self.store = store + self.stopping = threading.Event() + self.request_logging = request_logging + self.connections = set() + self.connections_lock = threading.Lock() + super().__init__(address, handler or Handler) + + def get_request(self): + conn, address = super().get_request() + conn.settimeout(3) + with self.connections_lock: + self.connections.add(conn) + return conn, address + + def shutdown_request(self, request): + with self.connections_lock: + self.connections.discard(request) + super().shutdown_request(request) + + def close_active(self): + """Enforce the overall shutdown deadline even for trickling clients.""" + with self.connections_lock: + for conn in list(self.connections): + try: + conn.shutdown(socket.SHUT_RDWR) + except OSError: + pass + conn.close() + + +class Handler(BaseHTTPRequestHandler): + # Close each connection: no unbounded idle keep-alive worker lifetime. + protocol_version = 'HTTP/1.0' + server_version = 'Messenger/1' + sys_version = '' + + def log_message(self, *_args): + pass + + def send_error(self, code, message=None, explain=None): + # Covers errors generated by the HTTP parser as well as business errors. + self.request_id = getattr(self, 'request_id', str(uuid.uuid4())) + mapped = {400: 'invalid_request', 404: 'not_found', 413: 'payload_too_large', 415: 'unsupported_media_type'} + self.respond(code, {'error': {'code': mapped.get(code, 'invalid_request'), + 'message': 'Неверный HTTP-запрос', 'request_id': self.request_id}}) + + def respond(self, status, body): + payload = b'' if status == 204 else json.dumps(body, ensure_ascii=False, separators=(',', ':')).encode('utf-8') + self.send_response(status) + self.send_header('Content-Type', 'application/json; charset=utf-8') + self.send_header('Content-Length', str(len(payload))) + self.send_header('X-Request-Id', self.request_id) + self.send_header('Connection', 'close') + for key, value in getattr(self, 'response_headers', {}).items(): + self.send_header(key, value) + self.end_headers() + self.wfile.write(payload) + self.close_connection = True + + def json_body(self): + media = self.headers.get_content_type() + if media != 'application/json' or self.headers.get_content_charset('utf-8').lower() != 'utf-8': + raise APIError(415, 'unsupported_media_type', 'Требуется application/json UTF-8') + lengths = self.headers.get_all('Content-Length', []) + if self.headers.get('Transfer-Encoding') or len(lengths) != 1 or not re.fullmatch(r'[0-9]+', lengths[0]): + raise invalid('Требуется один Content-Length; chunked не поддерживается') + if len(lengths[0]) > 8 or int(lengths[0]) > MAX_BODY_BYTES: + raise APIError(413, 'payload_too_large', 'JSON превышает 65536 байт') + length = int(lengths[0]) + raw = self.rfile.read(length) + if len(raw) != length: + raise invalid('Неполное тело JSON') + + def pairs(values): + result = {} + for key, value in values: + if key in result: + raise ValueError('duplicate key') + result[key] = value + return result + + def constant(_value): + raise ValueError('nonstandard number') + + try: + return json.loads(raw.decode('utf-8'), object_pairs_hook=pairs, parse_constant=constant) + except (ValueError, UnicodeError, RecursionError): + raise invalid('Неверный JSON') from None + + def dispatch(self): + raise NotImplementedError + + def handle_api(self): + self.request_id = str(uuid.uuid4()) + self.response_headers = {} + started = time.monotonic() + status = 500 + try: + status, body = self.dispatch() + except APIError as exc: + status = exc.status + self.response_headers = exc.headers + body = {'error': {'code': exc.code, 'message': exc.message, 'request_id': self.request_id}} + except (TimeoutError, ConnectionError): + status = 400 + body = {'error': {'code': 'invalid_request', 'message': 'Передача тела запроса прервана', 'request_id': self.request_id}} + except Exception: + # Never log exception text/locals: they can contain the message body. + LOG.error('internal_error request_id=%s', self.request_id) + body = {'error': {'code': 'internal_error', 'message': 'Внутренняя ошибка', 'request_id': self.request_id}} + try: + self.respond(status, body) + except (BrokenPipeError, ConnectionResetError, TimeoutError): + pass + finally: + if self.server.request_logging: + LOG.info('request request_id=%s method=%s status=%s duration_ms=%.3f', + self.request_id, self.command, status, (time.monotonic() - started) * 1000) + + do_GET = do_POST = do_PUT = do_DELETE = do_PATCH = do_OPTIONS = do_HEAD = handle_api + diff --git a/src/migrate.py b/src/migrate.py new file mode 100644 index 00000000..7553d0bd --- /dev/null +++ b/src/migrate.py @@ -0,0 +1,30 @@ +"""Versioned, transactional migrations; safe under concurrent API startup.""" +import hashlib +from pathlib import Path +import secrets +import psycopg +from config import secret + +ROOT = Path(__file__).resolve().parents[1] + + +def migrate(): + with psycopg.connect(secret('DATABASE_URL'), connect_timeout=2) as connection: + connection.execute('SELECT pg_advisory_xact_lock(20260201)') + connection.execute('CREATE TABLE IF NOT EXISTS schema_migrations (version text PRIMARY KEY, sha256 text NOT NULL, applied_at timestamptz NOT NULL DEFAULT clock_timestamp())') + for file in sorted((ROOT / 'migrations').glob('*.sql')): + sql = file.read_text() + digest = hashlib.sha256(sql.encode()).hexdigest() + prior = connection.execute('SELECT sha256 FROM schema_migrations WHERE version=%s', (file.name,)).fetchone() + if prior: + if prior[0] != digest: + raise RuntimeError('Applied migration checksum changed: ' + file.name) + continue + connection.execute(sql) + connection.execute('INSERT INTO schema_migrations(version,sha256) VALUES (%s,%s)', (file.name,digest)) + connection.execute("INSERT INTO app_settings(name,value) VALUES ('cursor_key',%s) ON CONFLICT DO NOTHING", (secrets.token_hex(32),)) + + +if __name__ == '__main__': + migrate() + print('PASS: migrations applied; no duplicate data') diff --git a/src/store.py b/src/store.py new file mode 100644 index 00000000..d3721dc5 --- /dev/null +++ b/src/store.py @@ -0,0 +1,265 @@ +"""PostgreSQL is authoritative; Valkey caches only immutable public profiles.""" +from contextlib import contextmanager +import base64 +import datetime as dt +import hashlib +import hmac +import json +import logging +import os +import re +import secrets +import uuid + +import psycopg +from psycopg.rows import dict_row +from psycopg_pool import ConnectionPool, PoolTimeout, TooManyRequests +import redis +from redis.backoff import NoBackoff +from redis.retry import Retry + +from config import integer, secret +from http_transport import APIError, fields, identifier, invalid, text + +LOG = logging.getLogger('messenger') +USER_COLUMNS = 'id,username,display_name,created_at' +CHAT_COLUMNS = 'c.id,c.title,c.created_at,ARRAY(SELECT user_id FROM chat_members WHERE chat_id=c.id ORDER BY user_id) AS member_ids' + + +def public(row): + result = {} + for key, value in row.items(): + if isinstance(value, uuid.UUID): + value = str(value) + elif isinstance(value, dt.datetime): + value = value.astimezone(dt.timezone.utc).isoformat(timespec='microseconds').replace('+00:00','Z') + elif isinstance(value, list): + value = [str(item) for item in value] + result[key] = value + return result + + +def password_hash(password, salt=None): + salt = salt if salt is not None else secrets.token_bytes(16) + derived = hashlib.scrypt(password.encode('utf-8'), salt=salt, n=16384, r=8, p=1, dklen=32, maxmem=64*1024*1024) + return 'scrypt$16384$8$1$' + salt.hex() + '$' + derived.hex() + + +def password_valid(password, encoded): + parts = encoded.split('$') + return hmac.compare_digest(password_hash(password, bytes.fromhex(parts[4])), encoded) + + +def unauthorized(basic=False): + challenge = 'Basic realm="messenger", charset="UTF-8"' if basic else 'Bearer realm="messenger"' + return APIError(401, 'unauthorized', 'Неверные или отсутствующие учётные данные', {'WWW-Authenticate': challenge}) + + +class Store: + def __init__(self): + if os.environ.get('SESSION_STORE','postgres') != 'postgres': + raise ValueError('SESSION_STORE must be postgres') + self.ttl = integer('SESSION_TTL_SECONDS', 3600) + self.cache_ttl = integer('PROFILE_CACHE_TTL_SECONDS', 300) + self.pool = ConnectionPool(secret('DATABASE_URL'), min_size=1, + max_size=integer('DB_POOL_MAX', 8, maximum=32), + max_waiting=32, timeout=2, reconnect_timeout=3, + check=ConnectionPool.check_connection, + kwargs={'connect_timeout': 2, 'row_factory': dict_row, + 'options': '-c statement_timeout=2000 -c lock_timeout=1000 -c idle_in_transaction_session_timeout=5000'}, + open=True) + self.cache = redis.Redis.from_url(secret('CACHE_URL'), decode_responses=True, + socket_connect_timeout=.2, socket_timeout=.2, + max_connections=16, retry=Retry(NoBackoff(), 0)) + with self.connection() as connection: + self.cursor_key = bytes.fromhex(connection.execute("SELECT value FROM app_settings WHERE name='cursor_key'").fetchone()['value']) + self.dummy_hash = password_hash('not-an-existing-password') + + @contextmanager + def connection(self): + try: + with self.pool.connection() as connection: + yield connection + except (psycopg.OperationalError, psycopg.InterfaceError, PoolTimeout, TooManyRequests, + psycopg.errors.QueryCanceled, psycopg.errors.LockNotAvailable): + raise APIError(503, 'unavailable', 'Хранилище временно недоступно') from None + + def close(self): + self.pool.close() + self.cache.close() + + def ready(self): + with self.connection() as connection: + connection.execute('SELECT 1') + + def user(self, body): + fields(body, ('username','display_name','password')) + name = body['username'] + if not isinstance(name, str) or not re.fullmatch(r'[a-z0-9_]{3,32}', name): + raise invalid('Неверный username') + display = text(body['display_name'], 100) + password = text(body['password'], 128) + if len(password) < 12: + raise invalid('Пароль должен содержать 12–128 символов') + encoded = password_hash(password) + try: + with self.connection() as connection: + user = connection.execute('INSERT INTO users(id,username,display_name,password_hash) VALUES (%s,%s,%s,%s) RETURNING ' + USER_COLUMNS, + (uuid.uuid4(),name,display,encoded)).fetchone() + return public(user) + except psycopg.errors.UniqueViolation: + raise APIError(409, 'conflict', 'Username занят') from None + + def profile(self, actor): + key = 'profile:v1:' + actor + try: + value = self.cache.get(key) + if value is not None: + result = json.loads(value) + if set(result) == {'id','username','display_name','created_at'} and result['id'] == actor: + return result + except (redis.RedisError, ValueError, TypeError): + LOG.info('profile_cache_fallback') + with self.connection() as connection: + result = public(connection.execute('SELECT ' + USER_COLUMNS + ' FROM users WHERE id=%s', (actor,)).fetchone()) + try: + self.cache.setex(key, self.cache_ttl, json.dumps(result)) + except redis.RedisError: + LOG.info('profile_cache_write_skipped') + return result + + def login(self, authorization): + try: + scheme, value = authorization.split(' ', 1) + if scheme.lower() != 'basic': + raise ValueError + decoded = base64.b64decode(value, validate=True).decode('utf-8') + username, separator, password = decoded.partition(':') + if not separator or not re.fullmatch(r'[a-z0-9_]{3,32}',username) or not 12 <= len(password) <= 128: + raise ValueError + password.encode('utf-8') + except (ValueError, AttributeError, UnicodeError): + raise unauthorized(True) from None + with self.connection() as connection: + user = connection.execute('SELECT id,password_hash FROM users WHERE username=%s', (username,)).fetchone() + valid = password_valid(password, user['password_hash'] if user else self.dummy_hash) + if not valid or user is None: + raise unauthorized(True) + token = secrets.token_urlsafe(32) + with self.connection() as connection: + session = connection.execute("INSERT INTO sessions(token_hash,user_id,expires_at) VALUES (%s,%s,clock_timestamp() + %s * interval '1 second') RETURNING expires_at", + (hashlib.sha256(token.encode()).digest(),user['id'],self.ttl)).fetchone() + return {'token': token, 'token_type': 'Bearer', 'expires_at': public(session)['expires_at'], 'user': self.profile(str(user['id']))} + + @staticmethod + def token_hash(authorization): + try: + scheme, token = authorization.split(' ', 1) + if scheme.lower() != 'bearer' or not re.fullmatch(r'[A-Za-z0-9_-]{43}', token): + raise ValueError + return hashlib.sha256(token.encode()).digest() + except (ValueError, AttributeError): + raise unauthorized() from None + + def authenticate(self, authorization): + digest = self.token_hash(authorization) + with self.connection() as connection: + session = connection.execute('SELECT user_id FROM sessions WHERE token_hash=%s AND expires_at > clock_timestamp()', (digest,)).fetchone() + if session is None: + raise unauthorized() + return str(session['user_id']) + + def logout(self, authorization): + digest = self.token_hash(authorization) + with self.connection() as connection: + revoked = connection.execute('DELETE FROM sessions WHERE token_hash=%s AND expires_at > clock_timestamp() RETURNING user_id', (digest,)).fetchone() + if revoked is None: + raise unauthorized() + + def chat(self, actor, body): + fields(body, ('title','member_ids')) + title = text(body['title'],100) + members = body['member_ids'] + if not isinstance(members,list) or not 1 <= len(members) <= 100: + raise invalid('Ожидался список участников длиной 1–100') + members = [identifier(member) for member in members] + if actor not in members or len(set(members)) != len(members): + raise invalid('Неверный список участников') + cid = uuid.uuid4() + try: + with self.connection() as connection: + # Deliberately rely on FK inside the SAME transaction: any bad + # member rolls back the chat and all earlier memberships. + connection.execute('INSERT INTO chats(id,title) VALUES (%s,%s)', (cid,title)) + with connection.cursor() as cursor: + cursor.executemany('INSERT INTO chat_members(chat_id,user_id) VALUES (%s,%s)', [(cid,member) for member in members]) + return public(connection.execute('SELECT ' + CHAT_COLUMNS + ' FROM chats c WHERE c.id=%s', (cid,)).fetchone()) + except psycopg.errors.ForeignKeyViolation: + raise invalid('Неверный список участников') from None + + def accessible(self, actor, cid, connection=None): + cid = identifier(cid) + if connection is None: + with self.connection() as connection: + return self.accessible(actor,cid,connection) + chat = connection.execute('SELECT ' + CHAT_COLUMNS + ' FROM chats c WHERE c.id=%s AND EXISTS (SELECT 1 FROM chat_members WHERE chat_id=c.id AND user_id=%s)', (cid,actor)).fetchone() + if chat is None: + raise APIError(404, 'not_found', 'Объект не найден') + return public(chat) + + def message(self, actor, cid, body): + fields(body, ('text',)) + value = text(body['text'],4000) + with self.connection() as connection: + self.accessible(actor,cid,connection) + timestamp = connection.execute("UPDATE chats SET last_message_at=greatest(clock_timestamp(),last_message_at + interval '1 microsecond') WHERE id=%s RETURNING last_message_at", (cid,)).fetchone()['last_message_at'] + result = connection.execute('INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES (%s,%s,%s,%s,%s) RETURNING id,chat_id,sender_id,text,created_at', (uuid.uuid4(),cid,actor,value,timestamp)).fetchone() + return public(result) + + def encode_cursor(self, scope, row): + raw = json.dumps([scope,row['created_at'],row['id']], separators=(',',':')).encode() + return base64.urlsafe_b64encode(raw + hmac.digest(self.cursor_key, raw, 'sha256')).decode().rstrip('=') + + def decode_cursor(self, scope, cursor): + try: + if not cursor or len(cursor) > 2048: + raise ValueError + data = base64.b64decode(cursor + '=' * (-len(cursor) % 4), altchars=b'-_',validate=True) + raw, signature = data[:-32], data[-32:] + if not hmac.compare_digest(hmac.digest(self.cursor_key,raw,'sha256'),signature): + raise ValueError + resource, timestamp, uid = json.loads(raw) + if resource != scope or not isinstance(timestamp,str): + raise ValueError + return dt.datetime.fromisoformat(timestamp.replace('Z','+00:00')), identifier(uid) + except (ValueError, TypeError, UnicodeError, APIError): + raise invalid('Неверный cursor для данного ресурса') from None + + def history(self, actor, cid, limit, cursor): + scope = 'messages:' + identifier(cid) + after = self.decode_cursor(scope,cursor) if cursor is not None else None + with self.connection() as connection: + self.accessible(actor,cid,connection) + query = 'SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=%s' + values = [cid] + if after: + query += ' AND (created_at,id) > (%s,%s)' + values.extend(after) + query += ' ORDER BY created_at,id LIMIT %s' + rows = connection.execute(query, (*values,limit+1)).fetchall() + items = [public(row) for row in rows[:limit]] + return {'items':items, 'next_cursor':self.encode_cursor(scope,items[-1]) if len(rows)>limit else None} + + def list_chats(self, actor, limit, cursor): + scope = 'chats:' + actor + after = self.decode_cursor(scope,cursor) if cursor is not None else None + query = 'SELECT ' + CHAT_COLUMNS + ' FROM chats c JOIN chat_members mine ON mine.chat_id=c.id WHERE mine.user_id=%s' + values = [actor] + if after: + query += ' AND (c.created_at,c.id) > (%s,%s)' + values.extend(after) + query += ' ORDER BY c.created_at,c.id LIMIT %s' + with self.connection() as connection: + rows = connection.execute(query, (*values,limit+1)).fetchall() + items = [public(row) for row in rows[:limit]] + return {'items':items, 'next_cursor':self.encode_cursor(scope,items[-1]) if len(rows)>limit else None} diff --git a/student-tests/clean_clone.py b/student-tests/clean_clone.py new file mode 100644 index 00000000..25b7355c --- /dev/null +++ b/student-tests/clean_clone.py @@ -0,0 +1,52 @@ +"""Verify the committed tree using a fresh clone and isolated Compose volumes.""" +import datetime as dt +import json +import os +from pathlib import Path +import socket +import subprocess +import sys +import tempfile +import uuid + +ROOT=Path(__file__).resolve().parents[1] + + +def main(): + project='mtusi-lab2-clean-'+uuid.uuid4().hex[:8] + with socket.socket() as s: + s.bind(('127.0.0.1',0)) + port=s.getsockname()[1] + report={'date_utc':dt.datetime.now(dt.timezone.utc).isoformat(), + 'source_commit':subprocess.check_output(['git','rev-parse','HEAD'],cwd=ROOT,text=True).strip(), + 'fresh_project_and_volumes':True} + with tempfile.TemporaryDirectory(prefix='mtusi-lab2-clean-') as directory: + clone=Path(directory)/'repo' + subprocess.run(['git','clone','--no-local',str(ROOT),str(clone)],check=True) + assert not subprocess.check_output(['git','status','--porcelain'],cwd=clone) + environment={**os.environ,'COMPOSE_PROJECT_NAME':project,'HOST_PORT':str(port),'BASE_URL':'http://127.0.0.1:'+str(port)} + def run(*command): + subprocess.run(command,cwd=clone,env=environment,check=True) + run(sys.executable,'scripts/bootstrap.py') + try: + run('docker','compose','build','--no-cache') + run('docker','compose','up','-d','--wait','--wait-timeout','60') + run('make','check','test','student-test','PYTHON='+sys.executable,'BASE_URL='+environment['BASE_URL']) + run(sys.executable,'student-tests/infrastructure.py') + report['checks']={'build_from_source':True,'public_smoke':True,'own_http_tests':True, + 'infrastructure_and_public_persistence':True} + report['infrastructure']=json.loads((clone/'evidence/infrastructure.json').read_text()) + api_id=subprocess.check_output(['docker','compose','ps','-q','api'],cwd=clone,env=environment,text=True).strip() + user=subprocess.check_output(['docker','exec',api_id,'id','-u'],text=True).strip() + assert user=='10001' + report['checks']['api_uid_10001']=True + report['status']='PASS' + finally: + # Only this fresh disposable test project's volumes are removed. + subprocess.run(['docker','compose','down','--volumes','--remove-orphans'],cwd=clone,env=environment,check=True) + (ROOT/'evidence/clean-clone.json').write_text(json.dumps(report,ensure_ascii=False,indent=2)+'\n') + print(json.dumps(report,ensure_ascii=False,indent=2)) + + +if __name__=='__main__': + main() diff --git a/student-tests/infrastructure.py b/student-tests/infrastructure.py new file mode 100644 index 00000000..7d2df066 --- /dev/null +++ b/student-tests/infrastructure.py @@ -0,0 +1,161 @@ +"""Explicit, recoverable failure tests for THIS Compose project only.""" +import datetime as dt +import hashlib +import json +import os +from pathlib import Path +import subprocess +import sys +import time +import uuid + +ROOT=Path(__file__).resolve().parents[1] +sys.path.insert(0,str(ROOT/'tests')) +from client import Client + + +def compose(*args,input=None): + return subprocess.run(['docker','compose',*args],cwd=ROOT,input=input,text=True,capture_output=True,check=True).stdout.strip() + + +def internal(code): + return json.loads(compose('exec','-T','api','python','-',input=code)) + + +def ready(client): + deadline=time.monotonic()+30 + while True: + try: + client.call('GET','/health/ready',schema='Health') + return + except AssertionError: + if time.monotonic()>deadline: + raise + time.sleep(.2) + + +def main(): + base=os.environ.get('BASE_URL','http://localhost:8082') + client=Client(base,timeout=8) + alice,bob,eve,chat=client.bootstrap() + report={'date_utc':dt.datetime.now(dt.timezone.utc).isoformat(),'checks':{}} + checks=report['checks'] + def state_counts(): + return internal("""import json,os,psycopg +from pathlib import Path +with psycopg.connect(Path(os.environ['DATABASE_URL_FILE']).read_text().strip()) as c: + print(json.dumps({name:c.execute('SELECT count(*) FROM '+name).fetchone()[0] for name in ('chats','chat_members')})) +""") + prior=state_counts() + client.call('POST','/api/v1/chats',400,actor=alice,body={'title':'Rollback probe','member_ids':[alice['id'],bob['id'],str(uuid.uuid4())]}) + assert prior==state_counts() + checks['failed_chat_transaction_leaves_no_chat_or_members']=True + client.call('GET','/api/v1/users/me',actor=alice,schema='User') + cache=internal('''import json,os,redis +from pathlib import Path +r=redis.Redis.from_url(Path(os.environ['CACHE_URL_FILE']).read_text().strip(),decode_responses=True) +keys=r.keys('profile:v1:*') +values=[json.loads(r.get(k)) for k in keys] +assert keys and all(set(v)=={'id','username','display_name','created_at'} for v in values) +print(json.dumps({'cached_profiles':len(keys),'all_profiles_public_fields_only':True,'positive_ttl':all(r.ttl(k)>0 for k in keys)})) +''') + assert cache['positive_ttl'] + checks['valkey_really_caches_profiles']=cache + checks['password_kdf_and_hashed_tokens']=internal('''import json,os,psycopg +from pathlib import Path +with psycopg.connect(Path(os.environ['DATABASE_URL_FILE']).read_text().strip()) as c: + p=c.execute("SELECT bool_and(password_hash LIKE 'scrypt$16384$8$1$%') FROM users").fetchone()[0] + t=c.execute('SELECT bool_and(octet_length(token_hash)=32) FROM sessions').fetchone()[0] + print(json.dumps({'all_passwords_scrypt':p,'all_token_digests_32_bytes':t})) +''') + assert all(checks['password_kdf_and_hashed_tokens'].values()) + try: + compose('stop','valkey') + started=time.monotonic() + me=client.call('GET','/api/v1/users/me',actor=alice,schema='User')['json'] + assert me==alice['user'] + client.call('GET','/api/v1/users/me',401,headers={'X-User-Id':alice['id']}) + message=client.message(alice,chat,'cache outage synthetic')['json'] + page=client.call('GET','/api/v1/chats/'+chat['id']+'/messages',actor=bob,schema='MessagePage')['json'] + assert message in page['items'] + # Revocation remains authoritative while the cache is down. + client.call('DELETE','/api/v1/auth/sessions/current',204,actor=alice) + client.call('GET','/api/v1/users/me',401,actor=alice) + checks['cache_outage']={'fallback_profile_and_history':True,'anonymous_rejected':True,'logout_effective':True,'scenario_seconds':round(time.monotonic()-started,3)} + finally: + compose('up','-d','--wait','valkey') + client.call('GET','/api/v1/users/me',401,actor=alice) + client.call('GET','/api/v1/users/me',actor=bob,schema='User') + checks['cache_restart_no_revoked_session_resurrection']=True + internal('''import json,os,redis +from pathlib import Path +r=redis.Redis.from_url(Path(os.environ['CACHE_URL_FILE']).read_text().strip()) +r.flushdb() +print(json.dumps({'flushed':True})) +''') + assert client.call('GET','/api/v1/users/me',actor=bob,schema='User')['json']==bob['user'] + checks['cache_flush_repopulates_from_postgres']=True + try: + compose('stop','postgres') + started=time.monotonic() + unavailable=client.call('GET','/api/v1/users/me',503,actor=bob) + elapsed=time.monotonic()-started + assert elapsed<7 + assert unavailable['json']['error']['code']=='unavailable' + client.call('GET','/health/live',schema='Health') + client.call('GET','/health/ready',503) + client.call('GET','/api/v1/users/me',401) + checks['postgres_outage']={'authenticated_request_503_seconds':round(elapsed,3),'live_200':True,'ready_503':True,'no_anonymous_trust':True} + finally: + compose('up','-d','--wait','postgres') + ready(client) + assert client.call('GET','/api/v1/users/me',actor=bob,schema='User')['json']==bob['user'] + checks['postgres_recovery_same_session']=True + # A second API container uses the same PostgreSQL and Valkey, short TTL only. + name='mtusi-lab2-short-ttl-'+uuid.uuid4().hex[:8] + try: + compose('run','-d','--no-deps','--name',name,'-p','127.0.0.1::8080','-e','SESSION_TTL_SECONDS=2','api') + mapping=subprocess.check_output(['docker','port',name,'8080/tcp'],text=True).strip() + short=Client('http://127.0.0.1:'+mapping.rsplit(':',1)[1]) + ready(short) + actor=short.user('expiry') + short.call('GET','/api/v1/users/me',actor=actor,schema='User') + time.sleep(2.2) + short.call('GET','/api/v1/users/me',401,actor=actor) + checks['expiry_ttl_2_seconds']=True + finally: + subprocess.run(['docker','rm','-f',name],capture_output=True) + migrations_before=internal('''import json,os,psycopg +from pathlib import Path +with psycopg.connect(Path(os.environ['DATABASE_URL_FILE']).read_text().strip()) as c: + print(json.dumps({'migrations':c.execute('SELECT count(*) FROM schema_migrations').fetchone()[0],'users':c.execute('SELECT count(*) FROM users').fetchone()[0]})) +''') + compose('exec','-T','api','python','src/app.py','--migrate') + migrations_after=internal('''import json,os,psycopg +from pathlib import Path +with psycopg.connect(Path(os.environ['DATABASE_URL_FILE']).read_text().strip()) as c: + print(json.dumps({'migrations':c.execute('SELECT count(*) FROM schema_migrations').fetchone()[0],'users':c.execute('SELECT count(*) FROM users').fetchone()[0]})) +''') + assert migrations_before==migrations_after + checks['repeat_migration_does_not_duplicate_data']=migrations_after + # Actual process start times around the public all-service restart scenario. + def states(): + values={} + for service in ('api','postgres','valkey'): + cid=compose('ps','-q',service) + values[service]=json.loads(subprocess.check_output(['docker','inspect',cid],text=True))[0]['State']['StartedAt'] + return values + before=states() + started=time.monotonic() + completed=subprocess.run(['make','test-persistence','ACTION_SCRIPT=scripts/restart.sh','BASE_URL='+base],cwd=ROOT,text=True,capture_output=True,check=True) + (ROOT/'evidence/persistence.txt').write_text(completed.stdout+completed.stderr) + after=states() + assert all(before[s]!=after[s] for s in before) + checks['full_stack_persistence']={'api_and_all_stores_restarted':True,'before':before,'after':after,'elapsed_seconds':round(time.monotonic()-started,3)} + report['status']='PASS' + (ROOT/'evidence/infrastructure.json').write_text(json.dumps(report,ensure_ascii=False,indent=2)+'\n') + print(json.dumps(report,ensure_ascii=False,indent=2)) + + +if __name__=='__main__': + main() diff --git a/student-tests/test_api.py b/student-tests/test_api.py new file mode 100644 index 00000000..ac350c17 --- /dev/null +++ b/student-tests/test_api.py @@ -0,0 +1,152 @@ +"""Independent HTTP checks against the real PostgreSQL/Valkey Compose stack.""" +from concurrent.futures import ThreadPoolExecutor +import base64 +import json +import os +from pathlib import Path +import sys +import threading +import unittest +from urllib.parse import quote +import uuid + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client + + +def basic(username,password): + return 'Basic ' + base64.b64encode((username + ':' + password).encode()).decode() + + +class APItests(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.base = os.environ.get('BASE_URL','http://localhost:8082') + cls.c = Client(cls.base) + cls.c.call('GET','/health/ready',schema='Health') + + def setUp(self): + self.alice,self.bob,self.eve,self.chat = self.c.bootstrap() + self.path = '/api/v1/chats/' + self.chat['id'] + + def test_malformed_json_and_types(self): + for raw in (b'{',b'null',b'[]',b'{} {}',b'\xff',b'{"username":NaN}',b'{"username":"one","username":"two"}'): + self.c.call('POST','/api/v1/users',400,raw=raw,headers={'Content-Type':'application/json'}) + valid = dict(self.alice['registration']) + for field, value in [('username',True),('username','ab'),('display_name',None),('display_name',123),('password',False),('password','a'*11),('password','a'*129)]: + self.c.call('POST','/api/v1/users',400,body={**valid,field:value}) + self.c.call('POST','/api/v1/users',400,body={**valid,'unexpected':'x'}) + self.c.call('POST','/api/v1/users',415,raw=b'{}',headers={'Content-Type':'text/plain'}) + self.c.call('POST','/api/v1/users',413,raw=b'x'*65537,headers={'Content-Type':'application/json'}) + raw=json.dumps({**valid,'display_name':'\ud800'}).encode() + self.c.call('POST','/api/v1/users',400,raw=raw,headers={'Content-Type':'application/json'}) + + def test_empty_message_unicode_and_sender(self): + for body in ({'text':''},{'text':'\t\n\u2003'},{'text':None},{'text':123},{'text':'🐍'*4001},{'text':'x','sender_id':self.eve['id']}): + self.c.call('POST',self.path+'/messages',400,body=body,actor=self.alice) + message=self.c.message(self.alice,self.chat,'🐍'*4000)['json'] + self.assertEqual(message['sender_id'],self.alice['id']) + self.assertEqual(message['text'],'🐍'*4000) + + def test_authentication_before_access_and_foreign_chat(self): + for suffix in ('','/messages'): + self.c.call('GET',self.path+suffix,404,actor=self.eve) + self.c.call('POST',self.path+'/messages',404,body={'text':'x'},actor=self.eve) + self.c.call('GET','/api/v1/chats/not-a-uuid?limit=0',401) + self.c.call('GET','/api/v1/users/me',401,headers={'X-User-Id':self.alice['id']}) + me=self.c.call('GET','/api/v1/users/me',actor=self.alice,headers={'X-User-Id':self.eve['id']},schema='User')['json'] + self.assertEqual(me,self.alice['user']) + + def test_bad_password_unknown_username_and_basic_only_for_login(self): + known=self.alice['registration'] + for authorization in ('Basic not-base64','Bearer '+self.alice['token'],basic(known['username'],'Wrong-password-123'),basic('unknown_'+uuid.uuid4().hex[:10],'Wrong-password-123')): + response=self.c.call('POST','/api/v1/auth/sessions',401,headers={'Authorization':authorization}) + self.assertEqual(response['headers']['WWW-Authenticate'],'Basic realm="messenger", charset="UTF-8"') + self.assertEqual(response['json']['error']['message'],'Неверные или отсутствующие учётные данные') + self.c.call('GET','/api/v1/users/me',401,headers={'Authorization':basic(known['username'],known['password'])}) + self.c.call('POST','/api/v1/auth/sessions',400,body={},headers={'Authorization':basic(known['username'],known['password'])}) + + def test_unicode_password_128_codepoints_and_colon(self): + username='unicode_'+uuid.uuid4().hex[:12] + password='😀'*126+':я' + user=self.c.call('POST','/api/v1/users',201,body={'username':username,'display_name':'🐍'*100,'password':password},schema='User')['json'] + session=self.c.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':basic(username,password)},schema='Session')['json'] + self.assertEqual(session['user'],user) + self.assertNotIn('password',user) + self.assertNotIn('password_hash',user) + self.assertGreaterEqual(len(base64.urlsafe_b64decode(session['token']+'=')),32) + + def test_logout_revokes_only_current_session(self): + credentials=self.alice['registration'] + session=self.c.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':basic(credentials['username'],credentials['password'])},schema='Session')['json'] + other={'id':self.alice['id'],'token':session['token']} + self.c.call('DELETE','/api/v1/auth/sessions/current',204,actor=self.alice) + self.c.call('GET','/api/v1/users/me',401,actor=self.alice) + self.c.call('DELETE','/api/v1/auth/sessions/current',401,actor=self.alice) + self.c.call('GET','/api/v1/users/me',actor=other,schema='User') + + def test_chat_rollback_and_membership_validation(self): + before=self.c.call('GET','/api/v1/chats',actor=self.alice,schema='ChatPage')['json'] + for members in ([],[self.alice['id'],self.alice['id']],[self.bob['id']],[self.alice['id'],self.bob['id'],str(uuid.uuid4())],None,[self.alice['id'],1]): + self.c.call('POST','/api/v1/chats',400,body={'title':'ROLLBACK','member_ids':members},actor=self.alice) + self.assertEqual(before,self.c.call('GET','/api/v1/chats',actor=self.alice,schema='ChatPage')['json']) + + def test_multiple_pages_bad_limits_and_cross_resource_cursor(self): + expected=[self.c.message(self.alice,self.chat,str(i))['json'] for i in range(13)] + found=[] + cursor=saved=None + while True: + path=self.path+'/messages?limit='+('2' if cursor is None else '3') + if cursor: + path+='&cursor='+quote(cursor,safe='') + page=self.c.call('GET',path,actor=self.bob,schema='MessagePage')['json'] + found.extend(page['items']) + cursor=page['next_cursor'] + saved=saved or cursor + if cursor is None: + break + self.assertEqual(found,expected) + other=self.c.call('POST','/api/v1/chats',201,body={'title':'Other','member_ids':[self.alice['id']]},actor=self.alice,schema='Chat')['json'] + self.c.call('GET','/api/v1/chats/'+other['id']+'/messages?cursor='+quote(saved,safe=''),400,actor=self.alice) + for query in ('limit=0','limit=101','limit=1.5','limit=true','limit=-1','limit=1&limit=2','cursor=','cursor=bad'): + self.c.call('GET',self.path+'/messages?'+query,400,actor=self.alice) + + def test_read_after_write_during_pagination(self): + expected=[self.c.message(self.alice,self.chat,str(i))['json'] for i in range(3)] + first=self.c.call('GET',self.path+'/messages?limit=2',actor=self.alice,schema='MessagePage')['json'] + late=self.c.message(self.bob,self.chat,'late')['json'] + rest=self.c.call('GET',self.path+'/messages?cursor='+quote(first['next_cursor'],safe=''),actor=self.alice,schema='MessagePage')['json'] + self.assertEqual(first['items']+rest['items'],expected+[late]) + + def test_100_messages_10_clients(self): + actors=[self.alice,self.bob]+[self.c.user('writer') for _ in range(8)] + chat=self.c.call('POST','/api/v1/chats',201,body={'title':'Concurrency','member_ids':[a['id'] for a in actors]},actor=self.alice,schema='Chat')['json'] + barrier=threading.Barrier(10) + def worker(index): + local=Client(self.base) + barrier.wait() + return [local.message(actors[index],chat,f'{index}:{i}')['json'] for i in range(10)] + with ThreadPoolExecutor(max_workers=10) as pool: + sent=[m for values in pool.map(worker,range(10)) for m in values] + page=self.c.call('GET','/api/v1/chats/'+chat['id']+'/messages?limit=100',actor=self.alice,schema='MessagePage')['json'] + self.assertEqual(len(page['items']),100) + self.assertEqual(len({m['id'] for m in page['items']}),100) + self.assertEqual({m['id'] for m in sent},{m['id'] for m in page['items']}) + self.assertIsNone(page['next_cursor']) + + def test_racing_registration(self): + username='race_'+uuid.uuid4().hex[:12] + barrier=threading.Barrier(10) + def register(_): + local=Client(self.base) + barrier.wait() + return local.call('POST','/api/v1/users',[201,409],body={'username':username,'display_name':'Concurrent','password':'Long-password-123'},schema='User')['status'] + with ThreadPoolExecutor(max_workers=10) as pool: + statuses=list(pool.map(register,range(10))) + self.assertEqual(statuses.count(201),1) + self.assertEqual(statuses.count(409),9) + + +if __name__ == '__main__': + unittest.main()