bench: трек 2 — планы запросов, стоимость записи, backup/restore со сверкой

plans.py: 121 120 сообщений (60k/15k/2k/80), 3 индекса x keyset/OFFSET x 3 размера
чата, pgbench в отдельном контейнере; WAL на вставку детерминированно.
backup_restore.py: pg_dump живой БД во время записи через API, восстановление в
новый контейнер и volume, сверка MD5 по чатам, API на восстановленной БД.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alexey Bodanyuk
2026-10-12 20:08:13 +03:00
co-authored by Claude Opus 5.5
parent a8c6af8c7d
commit eb92e8e91e
21 changed files with 1256 additions and 0 deletions
+281
View File
@@ -0,0 +1,281 @@
#!/usr/bin/env python3
"""Track 2, part 2: backup -> restore into NEW storage -> reconciliation, with RPO/RTO.
python bench/track2/backup_restore.py --repeats 3 # stand must be running
1. The live stand database gets (once) >= 100 000 synthetic messages with uneven chat sizes.
2. A writer sends messages through the real API (POST /messages) the whole time.
3. `pg_dump -Fc` of the live database (one consistent MVCC snapshot) to ./backups (git-ignored).
4. Restore into NEW storage: a fresh PostgreSQL container with a NEW volume (`pg_restore -j 2`).
5. Reconciliation source vs restored: row counts, per-chat MD5 of every message
(id, created_at, text), the first/last 50 messages of the biggest chat, schema_migrations.
Writer messages: the restored ones must be an exact PREFIX of the confirmed ones
(consistent snapshot, no torn state); the rest is the data-loss window (RPO).
6. The app itself is started against the restored database and reads the writer's chat
with a fresh login.
Results: evidence/track2/backup.json. No passwords or tokens are written.
"""
from __future__ import annotations
import argparse
import base64
import datetime as dt
import json
import secrets
import subprocess
import sys
import threading
import time
import uuid
from pathlib import Path
import httpx2 as httpx
ROOT = Path(__file__).resolve().parents[2]
IMAGE = "postgres:18.6-bookworm@sha256:afc7e2d441324c0388fa80c3d24f733b4194a4eb7f47dd8ee2b08eb1a24a647c"
APP_IMAGE = "mtusi-messenger:lab2"
RESTORE_PG, RESTORE_API, RESTORE_NET, RESTORE_VOL = "mtusi-restore-pg", "mtusi-restore-api", "mtusi-restore-net", "mtusi-restore-pgdata"
BASE = "http://127.0.0.1:8080"
SEED_USER = "00000000-0000-4000-8000-0000000c0001"
SEED_LIVE = f"""
\\set ON_ERROR_STOP on
BEGIN;
INSERT INTO users (id, username, display_name, password_hash)
VALUES ('{SEED_USER}', 'track2_live', convert_to('Track 2 live', 'UTF8'), 'not-a-login') ON CONFLICT DO NOTHING;
CREATE TEMP TABLE sizes AS
SELECT n, CASE WHEN n = 1 THEN 60000 WHEN n <= 3 THEN 15000 WHEN n <= 11 THEN 2000 ELSE 80 END AS size,
md5('track2-live-chat-' || n)::uuid AS chat_id
FROM generate_series(1::int, 200::int) AS n;
INSERT INTO chats (id, title, created_by, created_at)
SELECT chat_id, convert_to('track2 live ' || n, 'UTF8'), '{SEED_USER}', timestamptz '2026-02-01 00:00:00+00'
FROM sizes ON CONFLICT DO NOTHING;
INSERT INTO chat_members (chat_id, user_id, position, chat_created_at)
SELECT chat_id, '{SEED_USER}', 0, timestamptz '2026-02-01 00:00:00+00' FROM sizes ON CONFLICT DO NOTHING;
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT gen_random_uuid(), s.chat_id, '{SEED_USER}',
convert_to(left(repeat('сообщение ', 40), 5 + (random() * 195)::int), 'UTF8'),
timestamptz '2026-02-01 00:00:00+00' + g * interval '1 second'
FROM sizes s CROSS JOIN LATERAL generate_series(1::int, s.size) AS g
WHERE NOT EXISTS (SELECT 1 FROM messages m WHERE m.chat_id = s.chat_id);
UPDATE chats c SET last_message_at = m.mx FROM (SELECT chat_id, max(created_at) mx FROM messages GROUP BY chat_id) m
WHERE m.chat_id = c.id AND c.created_by = '{SEED_USER}';
COMMIT;
"""
RECONCILE_SQL = """
\\pset footer off
SELECT 'count:' || t || ':' || n FROM (
SELECT 'users' t, count(*) n FROM users UNION ALL SELECT 'chats', count(*) FROM chats
UNION ALL SELECT 'chat_members', count(*) FROM chat_members UNION ALL SELECT 'messages', count(*) FROM messages
UNION ALL SELECT 'schema_migrations', count(*) FROM schema_migrations) x ORDER BY t;
SELECT 'chat:' || chat_id || ':' || count(*) || ':' ||
md5(string_agg(id::text || '|' || created_at::text || '|' || encode(text, 'hex'), ',' ORDER BY created_at, id))
FROM messages WHERE chat_id <> '{writer_chat}' GROUP BY chat_id ORDER BY chat_id;
SELECT 'big:' || md5(string_agg(x, ',')) FROM (
(SELECT id::text || '|' || encode(text, 'hex') AS x FROM messages WHERE chat_id = md5('track2-live-chat-1')::uuid
ORDER BY created_at, id LIMIT 50)
UNION ALL
(SELECT * FROM (SELECT id::text || '|' || encode(text, 'hex') FROM messages WHERE chat_id = md5('track2-live-chat-1')::uuid
ORDER BY created_at DESC, id DESC LIMIT 50) y)) z;
SELECT 'mig:' || string_agg(version || '=' || checksum, ',' ORDER BY version) FROM schema_migrations;
SELECT 'idx:' || string_agg(indexname, ',' ORDER BY indexname) FROM pg_indexes WHERE schemaname = 'public';
SELECT 'writer:' || string_agg(id::text, ',' ORDER BY created_at, id) FROM messages WHERE chat_id = '{writer_chat}';
"""
def sh(args, *, input_bytes: bytes | None = None, check: bool = True, timeout: float = 600) -> subprocess.CompletedProcess:
result = subprocess.run(args, input=input_bytes, capture_output=True, timeout=timeout, cwd=ROOT)
if check and result.returncode != 0:
raise RuntimeError(f"{' '.join(map(str, args[:5]))}: {result.stderr.decode(errors='replace')[-1500:]}")
return result
def live_psql(sql: str) -> str:
return sh(["docker", "compose", "exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger",
"-X", "-q", "-At", "-f", "-"], input_bytes=sql.encode()).stdout.decode().strip()
def restored_psql(sql: str) -> str:
return sh(["docker", "exec", "-i", RESTORE_PG, "psql", "-U", "messenger", "-d", "messenger",
"-X", "-q", "-At", "-f", "-"], input_bytes=sql.encode()).stdout.decode().strip()
def parse(text: str) -> dict:
out: dict = {"count": {}, "chat": {}}
for line in text.splitlines():
kind, _, rest = line.partition(":")
if kind == "count":
name, _, value = rest.partition(":")
out["count"][name] = int(value)
elif kind == "chat":
chat, _, digest = rest.partition(":")
out["chat"][chat] = digest
else:
out[kind] = rest
return out
class Writer(threading.Thread):
"""Real API traffic during the backup: one message every ~10 ms, confirmations recorded."""
def __init__(self):
super().__init__(daemon=True)
self.client = httpx.Client(base_url=BASE, timeout=10)
self.username = "writer_" + uuid.uuid4().hex[:10]
self.password = secrets.token_urlsafe(16)
user = self.client.post("/api/v1/users", json={"username": self.username, "display_name": "Writer",
"password": self.password}).json()
auth = base64.b64encode(f"{self.username}:{self.password}".encode()).decode()
token = self.client.post("/api/v1/auth/sessions", headers={"Authorization": "Basic " + auth}).json()["token"]
self.headers = {"Authorization": "Bearer " + token}
self.chat = self.client.post("/api/v1/chats", headers=self.headers,
json={"title": "writer", "member_ids": [user["id"]]}).json()["id"]
self.confirmed: list[tuple[str, str]] = []
self.errors = 0
self.stop = threading.Event()
def run(self) -> None:
n = 0
while not self.stop.is_set():
r = self.client.post(f"/api/v1/chats/{self.chat}/messages", headers=self.headers, json={"text": f"w{n}"})
if r.status_code == 201:
self.confirmed.append((r.json()["id"], r.json()["created_at"]))
else:
self.errors += 1
n += 1
time.sleep(0.01)
def new_storage() -> str:
for cmd in (["docker", "rm", "-f", "-v", RESTORE_API, RESTORE_PG], ["docker", "volume", "rm", "-f", RESTORE_VOL],
["docker", "network", "rm", RESTORE_NET]):
sh(cmd, check=False)
sh(["docker", "network", "create", RESTORE_NET])
password = secrets.token_urlsafe(18)
sh(["docker", "run", "-d", "--name", RESTORE_PG, "--network", RESTORE_NET, "--cpus", "2", "--memory", "2g",
"-e", f"POSTGRES_PASSWORD={password}", "-e", "POSTGRES_USER=messenger", "-e", "POSTGRES_DB=messenger",
"-v", f"{RESTORE_VOL}:/var/lib/postgresql", IMAGE])
for _ in range(120):
if sh(["docker", "exec", RESTORE_PG, "pg_isready", "-U", "messenger", "-d", "messenger", "-q"], check=False).returncode == 0:
time.sleep(1)
return password
time.sleep(0.5)
raise RuntimeError("restore PostgreSQL did not start")
def app_on_restored(password: str, writer: Writer, expected_ids: list[str]) -> dict:
"""Run our API image against the restored DB (sessions in PostgreSQL) and read the writer chat."""
url = f"postgresql://messenger:{password}@{RESTORE_PG}:5432/messenger"
sh(["docker", "run", "-d", "--name", RESTORE_API, "--network", RESTORE_NET, "-p", "127.0.0.1:18090:8080",
"-e", "SESSION_STORE=postgres", "-e", f"DATABASE_URL={url}", "-e", "DATABASE_URL_FILE=", APP_IMAGE])
client = httpx.Client(base_url="http://127.0.0.1:18090", timeout=10)
for _ in range(60):
try:
if client.get("/health/ready").status_code == 200:
break
except httpx.HTTPError:
pass
time.sleep(0.5)
auth = base64.b64encode(f"{writer.username}:{writer.password}".encode()).decode()
token = client.post("/api/v1/auth/sessions", headers={"Authorization": "Basic " + auth}).json()["token"]
seen, cursor = [], None
while True:
q = "?limit=100" + (f"&cursor={cursor}" if cursor else "")
page = client.get(f"/api/v1/chats/{writer.chat}/messages{q}", headers={"Authorization": "Bearer " + token}).json()
seen += [m["id"] for m in page["items"]]
cursor = page["next_cursor"]
if not cursor:
break
sh(["docker", "rm", "-f", RESTORE_API], check=False)
return {"login_ok": True, "history_via_api_equals_restored": seen == expected_ids, "messages_read": len(seen)}
def one_round(round_no: int, backups: Path) -> dict:
writer = Writer()
writer.start()
time.sleep(2) # writer is in steady state before the backup starts
dump_file = backups / f"messenger-round{round_no}.dump"
started_wall = dt.datetime.now(dt.timezone.utc)
started = time.monotonic()
with dump_file.open("wb") as f:
proc = subprocess.run(["docker", "compose", "exec", "-T", "postgres", "pg_dump", "-U", "messenger", "-d", "messenger",
"-Fc", "-Z", "6"], stdout=f, stderr=subprocess.PIPE, cwd=ROOT, timeout=600)
if proc.returncode != 0:
raise RuntimeError(proc.stderr.decode()[-1000:])
dump_s = time.monotonic() - started
time.sleep(2) # keep writing after the dump: these confirmed messages are not in the backup
writer.stop.set()
writer.join()
source = parse(live_psql(RECONCILE_SQL.format(writer_chat=writer.chat)))
password = new_storage()
started = time.monotonic()
sh(["docker", "exec", "-i", RESTORE_PG, "sh", "-c", "cat > /tmp/backup.dump"], input_bytes=dump_file.read_bytes())
sh(["docker", "exec", RESTORE_PG, "pg_restore", "-U", "messenger", "-d", "messenger", "--no-owner", "-j", "2",
"--exit-on-error", "/tmp/backup.dump"])
restore_s = time.monotonic() - started
started = time.monotonic()
restored_psql("ANALYZE;")
analyze_s = time.monotonic() - started
restored = parse(restored_psql(RECONCILE_SQL.format(writer_chat=writer.chat)))
confirmed_ids = [m[0] for m in writer.confirmed]
restored_writer = restored.get("writer", "").split(",") if restored.get("writer") else []
prefix_ok = restored_writer == confirmed_ids[:len(restored_writer)]
lost = confirmed_ids[len(restored_writer):]
last_in_backup = writer.confirmed[len(restored_writer) - 1][1] if restored_writer else None
first_lost = writer.confirmed[len(restored_writer)][1] if lost else None
other_counts_equal = {k: source["count"][k] == restored["count"][k] for k in source["count"] if k != "messages"}
api_check = app_on_restored(password, writer, restored_writer)
result = {
"round": round_no, "backup_started_utc": started_wall.isoformat(timespec="seconds"),
"dump_seconds": round(dump_s, 2), "dump_bytes": dump_file.stat().st_size,
"restore_seconds": round(restore_s, 2), "analyze_seconds": round(analyze_s, 2),
"rto_seconds_restore_plus_analyze": round(restore_s + analyze_s, 2),
"source_counts_after_writer_stopped": source["count"], "restored_counts": restored["count"],
"messages_in_restored": restored["count"]["messages"],
"per_chat_md5_equal": source["chat"] == restored["chat"], "chats_compared": len(source["chat"]),
"big_chat_first_last_50_equal": source["big"] == restored["big"],
"schema_migrations_equal": source["mig"] == restored["mig"], "indexes_equal": source["idx"] == restored["idx"],
"other_table_counts_equal": other_counts_equal,
"writer": {"confirmed": len(confirmed_ids), "errors": writer.errors, "in_backup": len(restored_writer),
"restored_is_exact_prefix": prefix_ok, "lost_confirmed_after_snapshot": len(lost),
"last_message_in_backup_at": last_in_backup, "first_lost_message_at": first_lost},
"app_on_restored_db": api_check,
}
result["reconciled"] = bool(result["per_chat_md5_equal"] and result["big_chat_first_last_50_equal"]
and result["schema_migrations_equal"] and result["indexes_equal"]
and all(other_counts_equal.values()) and prefix_ok
and api_check["history_via_api_equals_restored"])
return result
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("--repeats", type=int, default=3)
parser.add_argument("--out", default=str(ROOT / "evidence/track2/backup.json"))
args = parser.parse_args()
backups = ROOT / "backups"
backups.mkdir(exist_ok=True)
live_psql(SEED_LIVE)
rounds = []
try:
for i in range(1, args.repeats + 1):
r = one_round(i, backups)
rounds.append(r)
print(json.dumps({k: r[k] for k in ("round", "dump_seconds", "dump_bytes", "restore_seconds", "messages_in_restored",
"reconciled")} | {"writer": r["writer"]}, ensure_ascii=False), flush=True)
finally:
for cmd in (["docker", "rm", "-f", "-v", RESTORE_API, RESTORE_PG], ["docker", "volume", "rm", "-f", RESTORE_VOL],
["docker", "network", "rm", RESTORE_NET]):
sh(cmd, check=False)
Path(args.out).parent.mkdir(parents=True, exist_ok=True)
Path(args.out).write_text(json.dumps({"image": IMAGE, "rounds": rounds}, ensure_ascii=False, indent=2) + "\n",
encoding="utf-8", newline="\n")
print("done:", args.out)
return 0 if all(r["reconciled"] for r in rounds) else 1
if __name__ == "__main__":
sys.exit(main())
+286
View File
@@ -0,0 +1,286 @@
#!/usr/bin/env python3
"""Track 2, part 1: query plans and write cost on >= 100 000 messages with uneven chat sizes.
python bench/track2/plans.py # ~20 min, results in evidence/track2/
An ISOLATED PostgreSQL container (same pinned image as compose.yaml, --cpus 2 --memory 2g,
no published ports) gets the schema from migrations/*.sql and a deterministic synthetic
dataset: 1 chat x 60 000, 2 x 15 000, 8 x 2 000, 189 x 80 messages = 121 120 messages,
texts of 10..400 bytes. The live stand is not touched: indexes are dropped and created
here freely.
For every index variant the script runs pgbench (prepared statements, like the driver):
* reads — one history page (LIMIT 51) after a RANDOM position of a big/medium/small chat,
keyset query (the app's SQL) vs OFFSET; 4 clients;
* writes — the app's message transaction (UPDATE chats ... RETURNING + INSERT); 8 clients;
and records TPS, p50/p95/p99 latency, WAL bytes per insert, index/table sizes, EXPLAIN.
"""
from __future__ import annotations
import argparse
import csv
import json
import math
import platform
import secrets
import statistics
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "src"))
from messenger.repo import HISTORY_AFTER # noqa: E402 (the app's real statement)
IMAGE = "postgres:18.6-bookworm@sha256:afc7e2d441324c0388fa80c3d24f733b4194a4eb7f47dd8ee2b08eb1a24a647c"
NAME = "mtusi-track2-pg"
CLIENT = "mtusi-track2-client" # pgbench runs here: it must not eat the server's CPU quota
PASSWORD = secrets.token_urlsafe(16) # throwaway, never printed
USER_ID = "00000000-0000-4000-8000-0000000b0001"
VARIANTS = {
# name: (DDL of secondary indexes on messages, description)
"keyset_index": (["CREATE INDEX messages_chat_keyset ON messages (chat_id, created_at, id)"],
"(chat_id, created_at, id) — used by the app"),
"chat_id_index": (["CREATE INDEX messages_chat_only ON messages (chat_id)"],
"(chat_id) only"),
"no_index": ([], "primary key only"),
"keyset_plus_sender": (["CREATE INDEX messages_chat_keyset ON messages (chat_id, created_at, id)",
"CREATE INDEX messages_sender_time ON messages (sender_id, created_at)"],
"app index + a second index (sender_id, created_at)"),
}
READ_VARIANTS = ["keyset_index", "chat_id_index", "no_index"]
WRITE_VARIANTS = ["no_index", "keyset_index", "keyset_plus_sender"]
KINDS = {"big": 60_000, "medium": 15_000, "small": 80}
def sh(args: list[str], input_text: str | None = None, check: bool = True) -> str:
result = subprocess.run(args, input=input_text, capture_output=True, text=True, encoding="utf-8")
if check and result.returncode != 0:
raise RuntimeError(f"{' '.join(args[:4])}: {result.stderr.strip()[-1500:]}")
return result.stdout
def psql(sql: str) -> str:
return sh(["docker", "exec", "-i", NAME, "psql", "-U", "postgres", "-d", "bench", "-X", "-q", "-At",
"-v", "ON_ERROR_STOP=1", "-f", "-"], input_text=sql).strip()
def put(path: str, content: str) -> None:
sh(["docker", "exec", "-i", CLIENT, "sh", "-c", f"cat > {path}"], input_text=content)
def start_container(cpus: str, memory: str) -> None:
sh(["docker", "rm", "-f", "-v", CLIENT, NAME], check=False)
sh(["docker", "run", "-d", "--name", NAME, "--cpus", cpus, "--memory", memory,
"-e", f"POSTGRES_PASSWORD={PASSWORD}", "-e", "POSTGRES_DB=bench", IMAGE])
# Load generator: separate container (own CPU quota) sharing the server's network namespace.
sh(["docker", "run", "-d", "--name", CLIENT, "--network", f"container:{NAME}", "--cpus", "2",
"-e", f"PGPASSWORD={PASSWORD}", "--entrypoint", "sleep", IMAGE, "infinity"])
for _ in range(120):
if subprocess.run(["docker", "exec", NAME, "pg_isready", "-U", "postgres", "-d", "bench", "-q"]).returncode == 0:
time.sleep(1)
return
time.sleep(0.5)
raise RuntimeError("experiment PostgreSQL did not start")
SEED = f"""
SELECT setseed(0.42);
INSERT INTO users (id, username, display_name, password_hash)
VALUES ('{USER_ID}', 'track2_bench', convert_to('Track 2', 'UTF8'), 'not-a-login');
CREATE TABLE bench_sizes AS
SELECT n, CASE WHEN n = 1 THEN 60000 WHEN n <= 3 THEN 15000 WHEN n <= 11 THEN 2000 ELSE 80 END AS size,
md5('track2-chat-' || n)::uuid AS chat_id
FROM generate_series(1::int, 200::int) AS n;
INSERT INTO chats (id, title, created_by, created_at)
SELECT chat_id, convert_to('chat ' || n, 'UTF8'), '{USER_ID}', timestamptz '2026-01-01 00:00:00+00' FROM bench_sizes;
INSERT INTO chat_members (chat_id, user_id, position, chat_created_at)
SELECT chat_id, '{USER_ID}', 0, timestamptz '2026-01-01 00:00:00+00' FROM bench_sizes;
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT gen_random_uuid(), s.chat_id, '{USER_ID}',
convert_to(left(repeat('сообщение ', 40), 5 + (random() * 195)::int), 'UTF8'),
timestamptz '2026-01-01 00:00:00+00' + g * interval '1 second'
FROM bench_sizes s CROSS JOIN LATERAL generate_series(1::int, s.size) AS g;
UPDATE chats c SET last_message_at = m.mx FROM (SELECT chat_id, max(created_at) mx FROM messages GROUP BY chat_id) m
WHERE m.chat_id = c.id;
CREATE TABLE bench_chats AS SELECT n, chat_id AS id FROM bench_sizes;
CREATE TABLE bench_cursors AS
SELECT kind, row_number() OVER (PARTITION BY kind ORDER BY rn)::int AS n, created_at AS ts, id AS mid, rn::int AS off
FROM (SELECT CASE s.n WHEN 1 THEN 'big' WHEN 2 THEN 'medium' ELSE 'small' END AS kind, m.created_at, m.id,
row_number() OVER (PARTITION BY m.chat_id ORDER BY m.created_at, m.id) AS rn, s.size
FROM messages m JOIN bench_sizes s ON s.chat_id = m.chat_id WHERE s.n IN (1, 2, 12)) x
WHERE rn % greatest(1, size / 200) = 0 AND rn < size;
CREATE INDEX ON bench_cursors (kind, n);
VACUUM ANALYZE;
"""
def pg_param(sql: str) -> str:
"""psycopg %s placeholders -> pgbench :variables (passed as parameters in prepared mode)."""
names = iter([":chat", ":cts", ":cid", "51"])
out = sql
while "%s" in out:
out = out.replace("%s", next(names), 1)
return out
READ_SCRIPTS = {
"keyset": "\\set k random(1, :ncur)\n"
"SELECT ts AS cts, mid AS cid FROM bench_cursors WHERE kind = :kind AND n = :k \\gset\n"
+ pg_param(HISTORY_AFTER) + ";\n",
"offset": "\\set k random(1, :ncur)\n"
"SELECT off FROM bench_cursors WHERE kind = :kind AND n = :k \\gset\n"
"SELECT id, sender_id, text, created_at FROM messages WHERE chat_id = :chat "
"ORDER BY created_at, id OFFSET :off LIMIT 51;\n",
}
WRITE_SCRIPT = (
"\\set c random(1, 200)\n"
"SELECT id AS chat FROM bench_chats WHERE n = :c \\gset\n"
"BEGIN;\n"
"UPDATE chats SET last_message_at = greatest(clock_timestamp(), last_message_at + interval '1 microsecond') "
"WHERE id = :chat RETURNING last_message_at AS ts \\gset\n"
f"INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES "
f"(gen_random_uuid(), :chat, '{USER_ID}', convert_to(repeat('w', 120), 'UTF8'), :ts);\n"
"END;\n"
)
def pgbench(script_path: str, clients: int, seconds: int, defines: dict[str, str]) -> dict:
"""Run pgbench with per-transaction logging; return TPS and latency percentiles (ms)."""
sh(["docker", "exec", CLIENT, "sh", "-c", "rm -f /tmp/pgblog*"])
args = ["docker", "exec", CLIENT, "sh", "-c",
"cd /tmp && pgbench -h 127.0.0.1 -U postgres -n -M prepared -c {c} -j {c} -T {t} -l --log-prefix=pgblog {d} -f {f} bench".format(
c=clients, t=seconds, f=script_path, d=" ".join(f"-D {k}={v}" for k, v in defines.items()))]
out = sh(args)
tps = float(next(l for l in out.splitlines() if l.startswith("tps = ")).split()[2])
failed = next((l for l in out.splitlines() if "number of failed transactions" in l), "0").split(":")[-1].split()[0]
lat = sorted(int(line.split()[2]) / 1000 for line in sh(["docker", "exec", CLIENT, "sh", "-c", "cat /tmp/pgblog*"]).splitlines())
def pct(p: float) -> float:
return round(lat[max(0, math.ceil(p * len(lat)) - 1)], 3)
return {"tps": round(tps, 1), "transactions": len(lat), "failed": int(failed),
"p50_ms": pct(.50), "p95_ms": pct(.95), "p99_ms": pct(.99), "mean_ms": round(statistics.mean(lat), 3)}
def use_variant(name: str) -> dict:
psql("DROP INDEX IF EXISTS messages_chat_keyset; DROP INDEX IF EXISTS messages_chat_only; "
"DROP INDEX IF EXISTS messages_sender_time;")
started = time.monotonic()
for ddl in VARIANTS[name][0]:
psql(ddl + ";")
build_s = round(time.monotonic() - started, 2)
psql("VACUUM ANALYZE messages;")
sizes = psql("SELECT indexrelname || '=' || pg_relation_size(indexrelid) FROM pg_stat_user_indexes "
"WHERE relname = 'messages' ORDER BY 1;").split()
return {"index_build_s": build_s, "index_bytes": dict(s.split("=") for s in sizes),
"table_bytes": int(psql("SELECT pg_relation_size('messages');"))}
def explain_late(kind: str) -> str:
chat = psql(f"SELECT chat_id FROM bench_sizes WHERE n = {dict(big=1, medium=2, small=12)[kind]};")
return psql(f"""
SET plan_cache_mode = force_generic_plan;
PREPARE p(uuid, timestamptz, uuid, int) AS {HISTORY_AFTER.replace('%s', '$1', 1).replace('%s', '$2', 1).replace('%s', '$3', 1).replace('%s', '$4', 1)};
SELECT ts, mid FROM bench_cursors WHERE kind = '{kind}' ORDER BY n DESC LIMIT 1 \\gset
EXPLAIN (ANALYZE, BUFFERS) EXECUTE p('{chat}', :'ts', :'mid', 51);
""")
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("--seconds", type=int, default=10)
parser.add_argument("--warmup", type=int, default=3)
parser.add_argument("--repeats", type=int, default=3)
parser.add_argument("--out", default=str(ROOT / "evidence/track2"))
parser.add_argument("--writes-only", action="store_true", help="re-measure writes, keep reads.csv")
parser.add_argument("--write-repeats", type=int, default=5)
args = parser.parse_args()
out = Path(args.out)
(out / "explain").mkdir(parents=True, exist_ok=True)
start_container("2", "2g")
schema = "".join(p.read_text(encoding="utf-8") for p in sorted((ROOT / "migrations").glob("*.sql")))
psql(schema)
seed_started = time.monotonic()
psql(SEED)
seed_s = round(time.monotonic() - seed_started, 1)
stats = psql("SELECT count(*), count(DISTINCT chat_id), min(octet_length(text)), max(octet_length(text)) FROM messages;")
sizes = psql("SELECT size || 'x' || count(*) FROM bench_sizes GROUP BY size ORDER BY size DESC;").split()
for name, script in READ_SCRIPTS.items():
put(f"/tmp/read_{name}.sql", script)
put("/tmp/write.sql", WRITE_SCRIPT)
chats = {k: psql(f"SELECT chat_id FROM bench_sizes WHERE n = {dict(big=1, medium=2, small=12)[k]};") for k in KINDS}
ncur = {k: int(psql(f"SELECT count(*) FROM bench_cursors WHERE kind = '{k}';")) for k in KINDS}
meta = {"image": IMAGE, "limits": {"server_cpus": 2, "server_memory": "2g", "pgbench_client_cpus": 2}, "pg_settings": "image defaults",
"dataset": {"messages_chats_minbytes_maxbytes": stats, "chat_sizes": sizes, "seed_seconds": seed_s},
"pgbench": {"mode": "prepared", "seconds": args.seconds, "warmup": args.warmup, "repeats": args.repeats,
"read_clients": 4, "write_clients": 8, "page": "LIMIT 51 after a random position"},
"host": {"platform": platform.platform(), "docker": sh(["docker", "version", "-f", "{{.Server.Version}}"]).strip()},
"variants": {k: v[1] for k, v in VARIANTS.items()}, "sizes": {}}
print("dataset:", stats, sizes, flush=True)
reads, writes = [], []
if args.writes_only:
old = json.loads((out / "plans-meta.json").read_text(encoding="utf-8"))
meta["sizes"] = old.get("sizes", {})
for variant in ([] if args.writes_only else READ_VARIANTS):
meta["sizes"][variant] = use_variant(variant)
for kind in KINDS:
(out / "explain" / f"{variant}-{kind}-late-page.txt").write_text(explain_late(kind), encoding="utf-8", newline="\n")
for rep in range(1, args.repeats + 1):
for query in READ_SCRIPTS:
for kind in KINDS:
defines = {"chat": chats[kind], "kind": kind, "ncur": str(ncur[kind])}
pgbench(f"/tmp/read_{query}.sql", 4, args.warmup, defines) # warm-up, discarded
row = {"variant": variant, "query": query, "chat": kind, "chat_size": KINDS[kind], "repeat": rep,
**pgbench(f"/tmp/read_{query}.sql", 4, args.seconds, defines)}
reads.append(row)
print("read ", row, flush=True)
wal_rows = []
for variant in WRITE_VARIANTS:
meta["sizes"][variant] = use_variant(variant)
# (a) throughput/latency under concurrency: 8 clients, time-based, autovacuum as in production
for rep in range(1, args.write_repeats + 1):
pgbench("/tmp/write.sql", 8, args.warmup, {})
row = {"variant": variant, "repeat": rep, **pgbench("/tmp/write.sql", 8, args.seconds, {})}
writes.append(row)
print("write", row, flush=True)
# (b) WAL volume per insert, deterministic: the same 5 000 app transactions (1 client),
# autovacuum off for the table and a CHECKPOINT right before, so every run starts from
# the same state and only the index set differs.
psql("ALTER TABLE messages SET (autovacuum_enabled = off);")
for rep in range(1, 4):
psql("CHECKPOINT;")
lsn0 = psql("SELECT pg_current_wal_lsn();")
sh(["docker", "exec", CLIENT, "pgbench", "-h", "127.0.0.1", "-U", "postgres", "-n", "-M", "prepared",
"-c", "1", "-t", "5000", "-f", "/tmp/write.sql", "bench"])
wal = int(psql(f"SELECT pg_current_wal_lsn() - '{lsn0}'::pg_lsn;"))
wal_row = {"variant": variant, "repeat": rep, "inserts": 5000, "wal_bytes": wal,
"wal_bytes_per_insert": round(wal / 5000)}
wal_rows.append(wal_row)
print("wal ", wal_row, flush=True)
psql("ALTER TABLE messages RESET (autovacuum_enabled);")
sh(["docker", "rm", "-f", "-v", CLIENT, NAME], check=False)
with (out / "wal.csv").open("w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=list(wal_rows[0]))
writer.writeheader()
writer.writerows(wal_rows)
meta["pgbench"]["write_repeats"] = args.write_repeats
meta["pgbench"]["wal_method"] = "5000 app transactions, 1 client, autovacuum off, CHECKPOINT before; 3 repeats"
for name, rows in (("reads", reads), ("writes", writes)):
if not rows:
continue
with (out / f"{name}.csv").open("w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=list(rows[0]))
writer.writeheader()
writer.writerows(rows)
(out / "plans-meta.json").write_text(json.dumps(meta, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print("done:", out)
return 0
if __name__ == "__main__":
sys.exit(main())
+110
View File
@@ -0,0 +1,110 @@
#!/usr/bin/env python3
"""Aggregate track 2 raw data into tables (summary.md) and charts (plans.png).
python bench/track2/summarize.py # matplotlib from requirements-bench.txt for the chart
"""
from __future__ import annotations
import csv
import json
import statistics
import sys
from collections import defaultdict
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
OUT = ROOT / "evidence/track2"
SIZE_ORDER = {"big": 0, "medium": 1, "small": 2}
def load(name: str) -> list[dict]:
with (OUT / name).open(encoding="utf-8") as f:
return list(csv.DictReader(f))
def agg(rows: list[dict], keys: tuple[str, ...], fields: tuple[str, ...]) -> dict:
groups: dict = defaultdict(list)
for r in rows:
groups[tuple(r[k] for k in keys)].append(r)
out = {}
for k, g in groups.items():
out[k] = {f: statistics.median(float(r[f]) for r in g) for f in fields}
out[k]["tps_min"] = min(float(r["tps"]) for r in g)
out[k]["tps_max"] = max(float(r["tps"]) for r in g)
out[k]["n"] = len(g)
return out
def fmt(x: float) -> str:
return f"{x:,.0f}".replace(",", " ") if x >= 100 else (f"{x:.1f}" if x >= 10 else f"{x:.2f}")
def main() -> int:
sys.stdout.reconfigure(encoding="utf-8")
reads, writes, wal = load("reads.csv"), load("writes.csv"), load("wal.csv")
meta = json.loads((OUT / "plans-meta.json").read_text(encoding="utf-8"))
r = agg(reads, ("variant", "query", "chat"), ("tps", "p50_ms", "p95_ms", "p99_ms"))
w = agg(writes, ("variant",), ("tps", "p50_ms", "p95_ms", "p99_ms"))
wal_by = {v: [int(r["wal_bytes_per_insert"]) for r in wal if r["variant"] == v] for v in {r["variant"] for r in wal}}
for v, values in wal_by.items():
w[(v,)]["wal_bytes_per_insert"] = statistics.median(values)
w[(v,)]["wal_min"], w[(v,)]["wal_max"] = min(values), max(values)
sizes = {"big": 60000, "medium": 15000, "small": 80}
md = ["## Чтение: одна страница (LIMIT 51) со случайной позиции чата", "",
"Медиана 3 повторов по 10 с, 4 клиента pgbench, prepared statements. TPS min–max — разброс повторов.", "",
"| Индекс | Запрос | Чат (сообщений) | TPS (min–max) | p50, мс | p95, мс | p99, мс |",
"| --- | --- | ---: | ---: | ---: | ---: | ---: |"]
for (variant, query, chat), v in sorted(r.items(), key=lambda kv: (kv[0][0] != "keyset_index", kv[0][0], kv[0][1], SIZE_ORDER[kv[0][2]])):
md.append(f"| {variant} | {query} | {chat} ({sizes[chat]:,}) | {fmt(v['tps'])} ({fmt(v['tps_min'])}–{fmt(v['tps_max'])}) "
f"| {fmt(v['p50_ms'])} | {fmt(v['p95_ms'])} | {fmt(v['p99_ms'])} |".replace(",", " ", 1))
md += ["", "## Запись: транзакция сообщения приложения (UPDATE chats … RETURNING + INSERT)", "",
"TPS и задержка — медиана 5 повторов по 10 с, 8 клиентов. WAL — медиана 3 детерминированных прогонов: "
"5 000 транзакций, 1 клиент, autovacuum выключен, CHECKPOINT перед прогоном.", "",
"| Индексы messages | TPS (min–max) | p50, мс | p95, мс | p99, мс | WAL, байт/вставку (min–max) | Размер индексов, МиБ |",
"| --- | ---: | ---: | ---: | ---: | ---: | ---: |"]
for variant in ("no_index", "keyset_index", "keyset_plus_sender"):
v = w[(variant,)]
idx = meta["sizes"][variant]["index_bytes"]
idx_mib = sum(int(b) for b in idx.values()) / 2**20
md.append(f"| {variant} | {fmt(v['tps'])} ({fmt(v['tps_min'])}–{fmt(v['tps_max'])}) | {fmt(v['p50_ms'])} | {fmt(v['p95_ms'])} "
f"| {fmt(v['p99_ms'])} | {v['wal_bytes_per_insert']:.0f} ({v['wal_min']}–{v['wal_max']}) | {idx_mib:.1f} |")
(OUT / "summary.md").write_text("\n".join(md) + "\n", encoding="utf-8", newline="\n")
print("\n".join(md))
try:
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
except ImportError:
print("matplotlib is not installed: skipping plans.png")
return 0
fig, axes = plt.subplots(1, 2, figsize=(15, 5))
chats = ["small", "medium", "big"]
x = [sizes[c] for c in chats]
styles = {("keyset_index", "keyset"): ("#12a150", "-o"), ("keyset_index", "offset"): ("#12a150", "--s"),
("chat_id_index", "keyset"): ("#e07b00", "-o"), ("chat_id_index", "offset"): ("#e07b00", "--s"),
("no_index", "keyset"): ("#b03060", "-o"), ("no_index", "offset"): ("#b03060", "--s")}
for (variant, query), (color, style) in styles.items():
axes[0].plot(x, [r[(variant, query, c)]["p95_ms"] for c in chats], style, color=color, label=f"{variant}, {query}")
axes[0].set_xscale("log")
axes[0].set_yscale("log")
axes[0].set_xlabel("messages in the chat")
axes[0].set_ylabel("p95 latency of one page, ms (log)")
axes[0].set_title("Read: random page of a chat")
axes[0].grid(True, which="both", alpha=0.3)
axes[0].legend(fontsize=8)
names = ["no_index", "keyset_index", "keyset_plus_sender"]
axes[1].bar([n.replace("_", "\n") for n in names], [w[(n,)]["wal_bytes_per_insert"] for n in names], color="#2a6fdb")
for i, n in enumerate(names):
axes[1].text(i, w[(n,)]["wal_bytes_per_insert"], f"{w[(n,)]['tps']:.0f} TPS", ha="center", va="bottom")
axes[1].set_ylabel("WAL bytes per inserted message")
axes[1].set_title("Write cost of indexes (WAL: 5000 inserts; TPS: 8 clients)")
fig.suptitle("Track 2: 121 120 messages, chats of 60 000 / 15 000 / 2 000 / 80; PostgreSQL 18.6, 2 CPU / 2 GiB")
fig.tight_layout()
fig.savefig(OUT / "plans.png", dpi=120)
print("chart:", OUT / "plans.png")
return 0
if __name__ == "__main__":
sys.exit(main())
+83
View File
@@ -0,0 +1,83 @@
# Шпаргалка к защите ЛР 2
Отчёт с доказательствами — [REPORT.md](../REPORT.md), модель данных — [DATA_MODEL.md](DATA_MODEL.md).
## Что изменилось по сравнению с lab1
| Было в lab1 | Стало в lab2 | Где |
| --- | --- | --- |
| словари в памяти (`store.py`) | PostgreSQL 18, данные переживают рестарт | [repo.py](../src/messenger/repo.py), [migrations/0001_init.sql](../migrations/0001_init.sql) |
| `X-User-Id` | вход по `Basic` → opaque `Bearer` с expiry, logout | [authhdr.py](../src/messenger/authhdr.py), [sessions.py](../src/messenger/sessions.py), [app.py](../src/messenger/app.py) |
| нет паролей | Argon2id (m=19 MiB, t=2, p=1) в отдельном потоке | [passwords.py](../src/messenger/passwords.py) |
| `threading.Lock` + свой счётчик времени | блокировка строки чата `UPDATE chats … RETURNING` в транзакции | `add_message` в repo.py |
| `bisect` по списку | индекс `(chat_id, created_at, id)` и row-comparison в `Index Cond` | `HISTORY_AFTER` в repo.py |
| один контейнер | `compose.yaml`: postgres, valkey, migrate, api; secrets-файлы; named volumes | [compose.yaml](../compose.yaml) |
Что осталось из lab1: формат ошибок и `X-Request-Id`, строгая валидация, cursor (теперь с проверкой диапазона времени), JSON-логи, graceful SIGTERM, healthcheck, Swagger на `/docs`.
## Путь запроса `GET /chats/{id}/messages?cursor=…` в lab2
1. `parse_bearer`: в заголовке ровно `Bearer <43 символа>`, иначе 401 с `WWW-Authenticate: Bearer`.
2. `sessions.user_id_for`: в Valkey ключ `session:<sha256(token)>`, проверка `expires_at`. Valkey недоступен — 503, нет ключа — 401.
3. `path_uuid` → 400; `ensure_member` (`SELECT 1 FROM chat_members …`) → 404 для чужого чата.
4. `cursor.decode` → `(время в мкс, id)`, привязка к чату → иначе 400.
5. `HISTORY_AFTER`: `WHERE chat_id=$1 AND (created_at, id) > ($2,$3) ORDER BY … LIMIT 51` — индекс начинает чтение прямо с cursor.
6. Каждая работа с БД — `db.run(...)`: отдельная задача, транзакция, дедлайн 3 с. Отказ или зависание БД → 503 ровно через дедлайн.
## Вопросы из README
**Что подтверждает COMMIT и какие гарантии зависят от настройки durability?**
COMMIT возвращается клиенту, когда запись транзакции в WAL сброшена на диск (`fsync=on`, `synchronous_commit=on`, это значения по умолчанию). Транзакция переживёт падение процесса PostgreSQL или ОС: при старте PostgreSQL проиграет WAL. От настроек зависит:
- `synchronous_commit=off` — ответ приходит раньше сброса, и последние ~0,6 с подтверждённых транзакций могут пропасть (база при этом останется целой);
- `fsync=off` — после сбоя возможна порча данных;
- кэш записи диска, который «врёт» о сбросе, ломает и то, и другое.
COMMIT на одном узле не защищает от потери самого диска или хоста: для этого нужны реплики (`synchronous_standby_names`) или резервные копии с архивом WAL (трек 2).
У Valkey свой аналог — `appendfsync`:
- у нас `always`: fsync до ответа, ничего не теряется;
- `everysec`: можно потерять до 1 с записей;
- `no`: когда сбросить на диск, решает ОС.
**Где хранятся сессии и что будет после flush/restart Redis (Valkey)?**
По умолчанию (`SESSION_STORE=valkey`) сессия существует **только** в Valkey: ключ `session:<sha256>`, значение `{user_id, expires_at}` и абсолютный срок `PXAT`.
- **Restart:** Valkey перечитывает AOF с volume, сессии на месте (тест `make test-persistence`). TTL не продлевается.
- **FLUSHALL** или потеря volume: все сессии исчезают, все пользователи получают 401 и входят заново. Отказ безопасный: доступ не выдаётся никому.
- **Valkey недоступен:** 503 на защищённые запросы и на logout. Анонимный доступ не открывается.
Почему не кэш над PostgreSQL: у одной копии нечему расходиться. У студентов с кэшем отозванный токен «оживал» после восстановления Redis, у нас такой ситуации быть не может (тест `test_logout_during_valkey_outage_is_never_lost`). Альтернатива `SESSION_STORE=postgres` хранит сессии в таблице `sessions`.
**Чем аутентификация отличается от доступа к конкретному чату?**
- **Аутентификация** — кто ты: действительная Bearer-сессия → `user_id`. Нет или недействительна → 401.
- **Авторизация доступа** — можно ли этому пользователю этот чат: есть ли строка в `chat_members`. Нет → 404, а не 403, чтобы не раскрывать существование чужого чата.
Сначала всегда аутентификация, потом доступ. Отправитель сообщения берётся из сессии, а БД сама проверяет `FOREIGN KEY (chat_id, sender_id) → chat_members`.
**Почему hash пароля и быстрый checksum имеют разные задачи?**
- **Checksum или быстрый хеш (SHA-256)** — контроль целостности и быстрый поиск. Он должен быть быстрым.
- **Хеш пароля** — защита низкоэнтропийного секрета на случай утечки базы. Он должен быть медленным, с солью и memory-hard (Argon2id), чтобы перебор словаря стоил дорого и не ускорялся на GPU.
Токен сессии — 256 бит случайности, перебрать его невозможно. Поэтому для токена хватает SHA-256: храним хеш, чтобы утечка базы не дала готовых токенов. Для пароля SHA-256 нельзя: миллиарды попыток в секунду на GPU.
## Частые ловушки (на чём преподаватель ловил других) и наш ответ
| Проверка преподавателя | У нас | Тест |
| --- | --- | --- |
| logout при упавшем Redis → после восстановления токен снова 200 | logout при упавшем Valkey = 503 (ничего не отозвано); после восстановления logout → 401, и после рестарта Valkey и API тоже 401 | `test_logout_during_valkey_outage_is_never_lost` |
| истёкшая сессия живёт в кэше | expiry проверяется при каждом чтении, ключ живёт до абсолютного `PXAT` | `test_short_ttl_expires[valkey/postgres]` |
| `docker pause postgres` → запрос висит 8+ с | клиентский дедлайн в отдельной задаче: 503 через 3,0 с | `test_postgres_failure[pause]` |
| БД упала → 401 вместо 503 | 503 `unavailable`, анонимный — 401 | `test_postgres_failure`, `test_postgres_session_store` |
| cursor в `Filter` при generic plan | два запроса без `IS NULL OR`, cursor в `Index Cond` | `test_explain.py`, `evidence/explain-history.txt` |
| `\u0000` → 500 | текст в `bytea`, NUL сохраняется | `test_unicode_limits_are_code_points_and_nul_is_stored` |
| пароль из пробелов, `Basic é` → 500 | 400 и 401 | `test_bad_password_on_registration_is_400`, `test_malformed_basic_is_401_not_500` |
| гонка регистрации считает любые ошибки за 409 | проверяются коды: ровно `[201] + [409]*7`, в БД 1 строка | `test_registration_race_exactly_one_201_and_seven_409` |
| rollback «до первого INSERT» | чат вставляется первым, FK участника падает после; `n_tup_ins` вырос, строк нет | `test_create_chat_rolls_back_after_the_chat_row_was_inserted` |
| `SESSION_TTL_SECONDS` не доходит до контейнера | compose передаёт его из `.env`, тест читает `printenv` внутри контейнера | `test_short_ttl_expires` |
| нет команды создания secrets | `python scripts/init_secrets.py` (идемпотентно) | раздел «Запуск из чистого clone» |
## Трек 2 — коротко
- **Гипотеза.** С индексом `(chat_id, created_at, id)` и keyset-запросом стоимость страницы не зависит от размера чата и позиции. С индексом только по `chat_id` нужна сортировка всего чата, с OFFSET — чтение всего префикса. Каждый индекс удорожает запись.
- **Методика.** Отдельный PostgreSQL (2 CPU / 2 GiB), 121 120 сообщений, чаты 60 000 / 15 000 / 2 000 / 80. pgbench с prepared statements; генератор нагрузки — в своём контейнере. 3 повтора. Плюс EXPLAIN для поздней страницы.
- **Backup.** `pg_dump -Fc` живой БД во время записи через API → новый контейнер и новый volume → `pg_restore` → сверка MD5 по каждому чату и по первым/последним сообщениям → наше же API на восстановленной БД читает чат после нового входа.
- **RPO и RTO.** `pg_dump` — снимок на момент старта. Всё подтверждённое после старта дампа в копию не попадает; это и есть граница потери данных (на практике — время с последнего дампа). RTO — время `pg_restore` с перестройкой индексов плюс переключение приложения. Чтобы уменьшить RPO до секунд, нужен архив WAL и PITR или реплика.
+155
View File
@@ -0,0 +1,155 @@
{
"image": "postgres:18.6-bookworm@sha256:afc7e2d441324c0388fa80c3d24f733b4194a4eb7f47dd8ee2b08eb1a24a647c",
"rounds": [
{
"round": 1,
"backup_started_utc": "2026-10-12T17:06:57+00:00",
"dump_seconds": 1.47,
"dump_bytes": 5349383,
"restore_seconds": 2.21,
"analyze_seconds": 0.57,
"rto_seconds_restore_plus_analyze": 2.79,
"source_counts_after_writer_stopped": {
"chat_members": 330,
"chats": 290,
"messages": 171546,
"schema_migrations": 1,
"users": 117
},
"restored_counts": {
"chat_members": 330,
"chats": 290,
"messages": 171410,
"schema_migrations": 1,
"users": 117
},
"messages_in_restored": 171410,
"per_chat_md5_equal": true,
"chats_compared": 257,
"big_chat_first_last_50_equal": true,
"schema_migrations_equal": true,
"indexes_equal": true,
"other_table_counts_equal": {
"chat_members": true,
"chats": true,
"schema_migrations": true,
"users": true
},
"writer": {
"confirmed": 237,
"errors": 0,
"in_backup": 101,
"restored_is_exact_prefix": true,
"lost_confirmed_after_snapshot": 136,
"last_message_in_backup_at": "2026-10-12T17:06:57.831240Z",
"first_lost_message_at": "2026-10-12T17:06:57.856678Z"
},
"app_on_restored_db": {
"login_ok": true,
"history_via_api_equals_restored": true,
"messages_read": 101
},
"reconciled": true
},
{
"round": 2,
"backup_started_utc": "2026-10-12T17:07:13+00:00",
"dump_seconds": 1.7,
"dump_bytes": 5362829,
"restore_seconds": 1.9,
"analyze_seconds": 0.49,
"rto_seconds_restore_plus_analyze": 2.39,
"source_counts_after_writer_stopped": {
"chat_members": 331,
"chats": 291,
"messages": 171788,
"schema_migrations": 1,
"users": 118
},
"restored_counts": {
"chat_members": 331,
"chats": 291,
"messages": 171655,
"schema_migrations": 1,
"users": 118
},
"messages_in_restored": 171655,
"per_chat_md5_equal": true,
"chats_compared": 258,
"big_chat_first_last_50_equal": true,
"schema_migrations_equal": true,
"indexes_equal": true,
"other_table_counts_equal": {
"chat_members": true,
"chats": true,
"schema_migrations": true,
"users": true
},
"writer": {
"confirmed": 242,
"errors": 0,
"in_backup": 109,
"restored_is_exact_prefix": true,
"lost_confirmed_after_snapshot": 133,
"last_message_in_backup_at": "2026-10-12T17:07:14.212768Z",
"first_lost_message_at": "2026-10-12T17:07:14.237207Z"
},
"app_on_restored_db": {
"login_ok": true,
"history_via_api_equals_restored": true,
"messages_read": 109
},
"reconciled": true
},
{
"round": 3,
"backup_started_utc": "2026-10-12T17:07:30+00:00",
"dump_seconds": 1.4,
"dump_bytes": 5371057,
"restore_seconds": 1.76,
"analyze_seconds": 0.49,
"rto_seconds_restore_plus_analyze": 2.25,
"source_counts_after_writer_stopped": {
"chat_members": 332,
"chats": 292,
"messages": 172038,
"schema_migrations": 1,
"users": 119
},
"restored_counts": {
"chat_members": 332,
"chats": 292,
"messages": 171898,
"schema_migrations": 1,
"users": 119
},
"messages_in_restored": 171898,
"per_chat_md5_equal": true,
"chats_compared": 259,
"big_chat_first_last_50_equal": true,
"schema_migrations_equal": true,
"indexes_equal": true,
"other_table_counts_equal": {
"chat_members": true,
"chats": true,
"schema_migrations": true,
"users": true
},
"writer": {
"confirmed": 250,
"errors": 0,
"in_backup": 110,
"restored_is_exact_prefix": true,
"lost_confirmed_after_snapshot": 140,
"last_message_in_backup_at": "2026-10-12T17:07:31.075846Z",
"first_lost_message_at": "2026-10-12T17:07:31.095982Z"
},
"app_on_restored_db": {
"login_ok": true,
"history_via_api_equals_restored": true,
"messages_read": 110
},
"reconciled": true
}
]
}
@@ -0,0 +1,20 @@
Limit (cost=1673.53..1673.58 rows=20 width=238) (actual time=14.400..14.407 rows=51.00 loops=1)
Buffers: shared hit=2124 read=53
-> Sort (cost=1673.53..1674.03 rows=202 width=238) (actual time=14.398..14.402 rows=51.00 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 44kB
Buffers: shared hit=2124 read=53
-> Bitmap Heap Scan on messages (cost=8.89..1665.79 rows=202 width=238) (actual time=13.229..14.305 rows=300.00 loops=1)
Recheck Cond: (chat_id = $1)
Filter: (ROW(created_at, id) > ROW($2, $3))
Rows Removed by Filter: 59700
Heap Blocks: exact=2118
Buffers: shared hit=2118 read=53
-> Bitmap Index Scan on messages_chat_only (cost=0.00..8.84 rows=606 width=0) (actual time=1.829..1.829 rows=60000.00 loops=1)
Index Cond: (chat_id = $1)
Index Searches: 1
Buffers: shared read=53
Planning:
Buffers: shared hit=97
Planning Time: 0.456 ms
Execution Time: 14.511 ms
@@ -0,0 +1,20 @@
Limit (cost=1673.53..1673.58 rows=20 width=238) (actual time=4.411..4.418 rows=51.00 loops=1)
Buffers: shared hit=571 read=14
-> Sort (cost=1673.53..1674.03 rows=202 width=238) (actual time=4.410..4.413 rows=51.00 loops=1)
Sort Key: created_at, id
Sort Method: quicksort Memory: 52kB
Buffers: shared hit=571 read=14
-> Bitmap Heap Scan on messages (cost=8.89..1665.79 rows=202 width=238) (actual time=4.306..4.361 rows=75.00 loops=1)
Recheck Cond: (chat_id = $1)
Filter: (ROW(created_at, id) > ROW($2, $3))
Rows Removed by Filter: 14925
Heap Blocks: exact=564
Buffers: shared hit=565 read=14
-> Bitmap Index Scan on messages_chat_only (cost=0.00..8.84 rows=606 width=0) (actual time=0.764..0.765 rows=15000.00 loops=1)
Index Cond: (chat_id = $1)
Index Searches: 1
Buffers: shared hit=1 read=14
Planning:
Buffers: shared hit=97
Planning Time: 0.339 ms
Execution Time: 4.475 ms
@@ -0,0 +1,20 @@
Limit (cost=1673.53..1673.58 rows=20 width=238) (actual time=0.129..0.130 rows=1.00 loops=1)
Buffers: shared hit=11 read=1
-> Sort (cost=1673.53..1674.03 rows=202 width=238) (actual time=0.127..0.128 rows=1.00 loops=1)
Sort Key: created_at, id
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=11 read=1
-> Bitmap Heap Scan on messages (cost=8.89..1665.79 rows=202 width=238) (actual time=0.104..0.104 rows=1.00 loops=1)
Recheck Cond: (chat_id = $1)
Filter: (ROW(created_at, id) > ROW($2, $3))
Rows Removed by Filter: 79
Heap Blocks: exact=4
Buffers: shared hit=5 read=1
-> Bitmap Index Scan on messages_chat_only (cost=0.00..8.84 rows=606 width=0) (actual time=0.064..0.064 rows=80.00 loops=1)
Index Cond: (chat_id = $1)
Index Searches: 1
Buffers: shared hit=1 read=1
Planning:
Buffers: shared hit=97
Planning Time: 0.368 ms
Execution Time: 0.207 ms
@@ -0,0 +1,10 @@
Limit (cost=0.42..77.92 rows=20 width=237) (actual time=0.456..0.514 rows=51.00 loops=1)
Buffers: shared hit=6 read=4
-> Index Scan using messages_chat_keyset on messages (cost=0.42..783.20 rows=202 width=237) (actual time=0.451..0.504 rows=51.00 loops=1)
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
Index Searches: 1
Buffers: shared hit=6 read=4
Planning:
Buffers: shared hit=120
Planning Time: 0.921 ms
Execution Time: 0.636 ms
@@ -0,0 +1,10 @@
Limit (cost=0.42..77.92 rows=20 width=237) (actual time=0.087..0.114 rows=51.00 loops=1)
Buffers: shared hit=5 read=2
-> Index Scan using messages_chat_keyset on messages (cost=0.42..783.20 rows=202 width=237) (actual time=0.084..0.108 rows=51.00 loops=1)
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
Index Searches: 1
Buffers: shared hit=5 read=2
Planning:
Buffers: shared hit=110
Planning Time: 0.551 ms
Execution Time: 0.156 ms
@@ -0,0 +1,10 @@
Limit (cost=0.42..77.92 rows=20 width=237) (actual time=0.071..0.072 rows=1.00 loops=1)
Buffers: shared hit=2 read=2
-> Index Scan using messages_chat_keyset on messages (cost=0.42..783.20 rows=202 width=237) (actual time=0.069..0.070 rows=1.00 loops=1)
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
Index Searches: 1
Buffers: shared hit=2 read=2
Planning:
Buffers: shared hit=110
Planning Time: 0.373 ms
Execution Time: 0.112 ms
@@ -0,0 +1,20 @@
Limit (cost=6162.88..6165.20 rows=20 width=238) (actual time=12.084..15.940 rows=51.00 loops=1)
Buffers: shared hit=4307
-> Gather Merge (cost=6162.88..6186.40 rows=202 width=238) (actual time=12.082..15.934 rows=51.00 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=4307
-> Sort (cost=5162.85..5163.06 rows=84 width=238) (actual time=8.686..8.690 rows=44.33 loops=3)
Sort Key: created_at, id
Sort Method: quicksort Memory: 57kB
Buffers: shared hit=4307
Worker 0: Sort Method: top-N heapsort Memory: 41kB
Worker 1: Sort Method: quicksort Memory: 58kB
-> Parallel Seq Scan on messages (cost=0.00..5160.17 rows=84 width=238) (actual time=1.775..8.612 rows=100.00 loops=3)
Filter: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
Rows Removed by Filter: 40273
Buffers: shared hit=4277
Planning:
Buffers: shared hit=89
Planning Time: 0.335 ms
Execution Time: 16.001 ms
@@ -0,0 +1,20 @@
Limit (cost=6162.88..6165.20 rows=20 width=238) (actual time=19.403..27.573 rows=51.00 loops=1)
Buffers: shared hit=4307
-> Gather Merge (cost=6162.88..6186.40 rows=202 width=238) (actual time=19.400..27.552 rows=51.00 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=4307
-> Sort (cost=5162.85..5163.06 rows=84 width=238) (actual time=12.274..12.277 rows=21.00 loops=3)
Sort Key: created_at, id
Sort Method: quicksort Memory: 44kB
Buffers: shared hit=4307
Worker 0: Sort Method: quicksort Memory: 27kB
Worker 1: Sort Method: quicksort Memory: 30kB
-> Parallel Seq Scan on messages (cost=0.00..5160.17 rows=84 width=238) (actual time=5.204..12.212 rows=25.00 loops=3)
Filter: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
Rows Removed by Filter: 40348
Buffers: shared hit=4277
Planning:
Buffers: shared hit=85
Planning Time: 0.565 ms
Execution Time: 27.653 ms
@@ -0,0 +1,20 @@
Limit (cost=6162.88..6165.20 rows=20 width=238) (actual time=12.515..16.342 rows=1.00 loops=1)
Buffers: shared hit=4307
-> Gather Merge (cost=6162.88..6186.40 rows=202 width=238) (actual time=12.513..16.339 rows=1.00 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=4307
-> Sort (cost=5162.85..5163.06 rows=84 width=238) (actual time=8.455..8.456 rows=0.33 loops=3)
Sort Key: created_at, id
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=4307
Worker 0: Sort Method: quicksort Memory: 25kB
Worker 1: Sort Method: quicksort Memory: 25kB
-> Parallel Seq Scan on messages (cost=0.00..5160.17 rows=84 width=238) (actual time=7.451..8.415 rows=0.33 loops=3)
Filter: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
Rows Removed by Filter: 40373
Buffers: shared hit=4277
Planning:
Buffers: shared hit=85
Planning Time: 0.289 ms
Execution Time: 16.395 ms
+74
View File
@@ -0,0 +1,74 @@
{
"image": "postgres:18.6-bookworm@sha256:afc7e2d441324c0388fa80c3d24f733b4194a4eb7f47dd8ee2b08eb1a24a647c",
"limits": {
"server_cpus": 2,
"server_memory": "2g",
"pgbench_client_cpus": 2
},
"pg_settings": "image defaults",
"dataset": {
"messages_chats_minbytes_maxbytes": "121120|200|10|380",
"chat_sizes": [
"60000x1",
"15000x2",
"2000x8",
"80x189"
],
"seed_seconds": 3.3
},
"pgbench": {
"mode": "prepared",
"seconds": 10,
"warmup": 3,
"repeats": 3,
"read_clients": 4,
"write_clients": 8,
"page": "LIMIT 51 after a random position",
"write_repeats": 5,
"wal_method": "5000 app transactions, 1 client, autovacuum off, CHECKPOINT before; 3 repeats"
},
"host": {
"platform": "Windows-10-10.0.19045-SP0",
"docker": "29.8.0"
},
"variants": {
"keyset_index": "(chat_id, created_at, id) — used by the app",
"chat_id_index": "(chat_id) only",
"no_index": "primary key only",
"keyset_plus_sender": "app index + a second index (sender_id, created_at)"
},
"sizes": {
"keyset_index": {
"index_build_s": 0.64,
"index_bytes": {
"messages_chat_keyset": "15097856",
"messages_pkey": "10051584"
},
"table_bytes": 64290816
},
"chat_id_index": {
"index_build_s": 0.25,
"index_bytes": {
"messages_chat_only": "876544",
"messages_pkey": "4775936"
},
"table_bytes": 35037184
},
"no_index": {
"index_build_s": 0.0,
"index_bytes": {
"messages_pkey": "4849664"
},
"table_bytes": 35037184
},
"keyset_plus_sender": {
"index_build_s": 1.67,
"index_bytes": {
"messages_chat_keyset": "21102592",
"messages_pkey": "15286272",
"messages_sender_time": "12468224"
},
"table_bytes": 86335488
}
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 115 KiB

+55
View File
@@ -0,0 +1,55 @@
variant,query,chat,chat_size,repeat,tps,transactions,failed,p50_ms,p95_ms,p99_ms,mean_ms
keyset_index,keyset,big,60000,1,2401.7,23998,0,0.873,2.88,32.812,1.651
keyset_index,keyset,medium,15000,1,2991.2,29887,0,0.76,1.681,34.535,1.327
keyset_index,keyset,small,80,1,4017.5,40162,0,0.576,1.691,11.886,0.986
keyset_index,offset,big,60000,1,169.5,1696,0,13.118,70.429,81.03,23.566
keyset_index,offset,medium,15000,1,665.7,6672,0,2.909,47.698,53.811,5.995
keyset_index,offset,small,80,1,3402.6,34012,0,0.658,1.68,29.655,1.165
keyset_index,keyset,big,60000,2,2655.3,26611,0,0.815,2.278,33.597,1.495
keyset_index,keyset,medium,15000,2,1887.8,18889,0,0.905,6.01,32.787,2.101
keyset_index,keyset,small,80,2,2439.0,24420,0,0.659,5.636,25.202,1.622
keyset_index,offset,big,60000,2,180.4,1814,0,11.618,69.186,84.59,22.064
keyset_index,offset,medium,15000,2,791.1,7909,0,2.528,43.867,52.614,5.044
keyset_index,offset,small,80,2,4059.2,40598,0,0.604,1.05,9.089,0.977
keyset_index,keyset,big,60000,3,3486.0,34836,0,0.676,1.191,31.965,1.139
keyset_index,keyset,medium,15000,3,3499.1,34950,0,0.675,1.229,31.369,1.134
keyset_index,keyset,small,80,3,5575.1,55692,0,0.489,0.791,1.744,0.711
keyset_index,offset,big,60000,3,234.7,2357,0,9.301,63.742,68.861,17.016
keyset_index,offset,medium,15000,3,852.0,8550,0,2.398,6.87,52.109,4.683
keyset_index,offset,small,80,3,4251.9,42587,0,0.584,0.973,3.529,0.933
chat_id_index,keyset,big,60000,1,89.8,902,0,27.035,80.852,100.066,44.495
chat_id_index,keyset,medium,15000,1,364.0,3653,0,5.595,55.042,58.907,10.973
chat_id_index,keyset,small,80,1,4115.2,41258,0,0.6,1.015,4.808,0.964
chat_id_index,offset,big,60000,1,28.3,287,0,120.756,277.986,388.776,140.219
chat_id_index,offset,medium,15000,1,135.4,1360,0,16.801,71.112,77.884,29.509
chat_id_index,offset,small,80,1,4214.0,42116,0,0.587,0.978,4.666,0.941
chat_id_index,keyset,big,60000,2,89.4,899,0,28.031,80.795,96.807,44.655
chat_id_index,keyset,medium,15000,2,362.5,3623,0,5.642,55.669,59.361,11.022
chat_id_index,keyset,small,80,2,4068.0,40695,0,0.603,1.026,5.892,0.975
chat_id_index,offset,big,60000,2,28.0,283,0,120.306,292.288,396.718,142.499
chat_id_index,offset,medium,15000,2,140.0,1401,0,15.77,71.238,74.833,28.531
chat_id_index,offset,small,80,2,4222.1,42290,0,0.579,1.019,3.828,0.939
chat_id_index,keyset,big,60000,3,89.5,901,0,27.0,81.494,98.989,44.63
chat_id_index,keyset,medium,15000,3,345.7,3473,0,5.885,55.874,59.633,11.54
chat_id_index,keyset,small,80,3,4140.1,41444,0,0.59,1.026,4.533,0.958
chat_id_index,offset,big,60000,3,29.3,296,0,119.134,208.472,381.353,135.64
chat_id_index,offset,medium,15000,3,134.8,1354,0,16.795,71.384,76.114,29.634
chat_id_index,offset,small,80,3,4266.0,42637,0,0.581,0.967,3.792,0.93
no_index,keyset,big,60000,1,29.0,291,0,111.727,197.369,292.929,137.59
no_index,keyset,medium,15000,1,34.3,345,0,104.45,184.828,192.39,116.54
no_index,keyset,small,80,1,36.3,364,0,102.442,182.084,192.872,110.02
no_index,offset,big,60000,1,25.7,259,0,132.386,293.23,376.222,155.228
no_index,offset,medium,15000,1,63.3,635,0,75.84,94.867,109.291,63.124
no_index,offset,small,80,1,105.1,1055,0,19.797,72.802,78.183,38.004
no_index,keyset,big,60000,2,30.7,309,0,110.372,191.978,200.211,130.118
no_index,keyset,medium,15000,2,33.9,340,0,105.192,185.337,190.981,117.813
no_index,keyset,small,80,2,37.4,377,0,100.838,180.405,188.141,106.88
no_index,offset,big,60000,2,26.4,265,0,134.06,222.582,384.567,151.443
no_index,offset,medium,15000,2,49.7,498,0,85.725,118.239,170.86,80.332
no_index,offset,small,80,2,84.1,843,0,30.964,82.362,94.9,47.488
no_index,keyset,big,60000,3,21.6,219,0,192.061,288.029,303.799,184.125
no_index,keyset,medium,15000,3,26.0,263,0,169.475,207.861,283.698,153.341
no_index,keyset,small,80,3,25.1,255,0,178.656,220.368,289.13,158.613
no_index,offset,big,60000,3,18.0,182,0,205.371,381.47,517.852,221.459
no_index,offset,medium,15000,3,44.9,450,0,90.847,150.378,187.415,88.956
no_index,offset,small,80,3,77.6,781,0,68.094,86.015,98.542,51.458
1 variant query chat chat_size repeat tps transactions failed p50_ms p95_ms p99_ms mean_ms
2 keyset_index keyset big 60000 1 2401.7 23998 0 0.873 2.88 32.812 1.651
3 keyset_index keyset medium 15000 1 2991.2 29887 0 0.76 1.681 34.535 1.327
4 keyset_index keyset small 80 1 4017.5 40162 0 0.576 1.691 11.886 0.986
5 keyset_index offset big 60000 1 169.5 1696 0 13.118 70.429 81.03 23.566
6 keyset_index offset medium 15000 1 665.7 6672 0 2.909 47.698 53.811 5.995
7 keyset_index offset small 80 1 3402.6 34012 0 0.658 1.68 29.655 1.165
8 keyset_index keyset big 60000 2 2655.3 26611 0 0.815 2.278 33.597 1.495
9 keyset_index keyset medium 15000 2 1887.8 18889 0 0.905 6.01 32.787 2.101
10 keyset_index keyset small 80 2 2439.0 24420 0 0.659 5.636 25.202 1.622
11 keyset_index offset big 60000 2 180.4 1814 0 11.618 69.186 84.59 22.064
12 keyset_index offset medium 15000 2 791.1 7909 0 2.528 43.867 52.614 5.044
13 keyset_index offset small 80 2 4059.2 40598 0 0.604 1.05 9.089 0.977
14 keyset_index keyset big 60000 3 3486.0 34836 0 0.676 1.191 31.965 1.139
15 keyset_index keyset medium 15000 3 3499.1 34950 0 0.675 1.229 31.369 1.134
16 keyset_index keyset small 80 3 5575.1 55692 0 0.489 0.791 1.744 0.711
17 keyset_index offset big 60000 3 234.7 2357 0 9.301 63.742 68.861 17.016
18 keyset_index offset medium 15000 3 852.0 8550 0 2.398 6.87 52.109 4.683
19 keyset_index offset small 80 3 4251.9 42587 0 0.584 0.973 3.529 0.933
20 chat_id_index keyset big 60000 1 89.8 902 0 27.035 80.852 100.066 44.495
21 chat_id_index keyset medium 15000 1 364.0 3653 0 5.595 55.042 58.907 10.973
22 chat_id_index keyset small 80 1 4115.2 41258 0 0.6 1.015 4.808 0.964
23 chat_id_index offset big 60000 1 28.3 287 0 120.756 277.986 388.776 140.219
24 chat_id_index offset medium 15000 1 135.4 1360 0 16.801 71.112 77.884 29.509
25 chat_id_index offset small 80 1 4214.0 42116 0 0.587 0.978 4.666 0.941
26 chat_id_index keyset big 60000 2 89.4 899 0 28.031 80.795 96.807 44.655
27 chat_id_index keyset medium 15000 2 362.5 3623 0 5.642 55.669 59.361 11.022
28 chat_id_index keyset small 80 2 4068.0 40695 0 0.603 1.026 5.892 0.975
29 chat_id_index offset big 60000 2 28.0 283 0 120.306 292.288 396.718 142.499
30 chat_id_index offset medium 15000 2 140.0 1401 0 15.77 71.238 74.833 28.531
31 chat_id_index offset small 80 2 4222.1 42290 0 0.579 1.019 3.828 0.939
32 chat_id_index keyset big 60000 3 89.5 901 0 27.0 81.494 98.989 44.63
33 chat_id_index keyset medium 15000 3 345.7 3473 0 5.885 55.874 59.633 11.54
34 chat_id_index keyset small 80 3 4140.1 41444 0 0.59 1.026 4.533 0.958
35 chat_id_index offset big 60000 3 29.3 296 0 119.134 208.472 381.353 135.64
36 chat_id_index offset medium 15000 3 134.8 1354 0 16.795 71.384 76.114 29.634
37 chat_id_index offset small 80 3 4266.0 42637 0 0.581 0.967 3.792 0.93
38 no_index keyset big 60000 1 29.0 291 0 111.727 197.369 292.929 137.59
39 no_index keyset medium 15000 1 34.3 345 0 104.45 184.828 192.39 116.54
40 no_index keyset small 80 1 36.3 364 0 102.442 182.084 192.872 110.02
41 no_index offset big 60000 1 25.7 259 0 132.386 293.23 376.222 155.228
42 no_index offset medium 15000 1 63.3 635 0 75.84 94.867 109.291 63.124
43 no_index offset small 80 1 105.1 1055 0 19.797 72.802 78.183 38.004
44 no_index keyset big 60000 2 30.7 309 0 110.372 191.978 200.211 130.118
45 no_index keyset medium 15000 2 33.9 340 0 105.192 185.337 190.981 117.813
46 no_index keyset small 80 2 37.4 377 0 100.838 180.405 188.141 106.88
47 no_index offset big 60000 2 26.4 265 0 134.06 222.582 384.567 151.443
48 no_index offset medium 15000 2 49.7 498 0 85.725 118.239 170.86 80.332
49 no_index offset small 80 2 84.1 843 0 30.964 82.362 94.9 47.488
50 no_index keyset big 60000 3 21.6 219 0 192.061 288.029 303.799 184.125
51 no_index keyset medium 15000 3 26.0 263 0 169.475 207.861 283.698 153.341
52 no_index keyset small 80 3 25.1 255 0 178.656 220.368 289.13 158.613
53 no_index offset big 60000 3 18.0 182 0 205.371 381.47 517.852 221.459
54 no_index offset medium 15000 3 44.9 450 0 90.847 150.378 187.415 88.956
55 no_index offset small 80 3 77.6 781 0 68.094 86.015 98.542 51.458
+34
View File
@@ -0,0 +1,34 @@
## Чтение: одна страница (LIMIT 51) со случайной позиции чата
Медиана 3 повторов по 10 с, 4 клиента pgbench, prepared statements. TPS min–max — разброс повторов.
| Индекс | Запрос | Чат (сообщений) | TPS (min–max) | p50, мс | p95, мс | p99, мс |
| --- | --- | ---: | ---: | ---: | ---: | ---: |
| keyset_index | keyset | big (60 000) | 2 655 (2 402–3 486) | 0.81 | 2.28 | 32.8 |
| keyset_index | keyset | medium (15 000) | 2 991 (1 888–3 499) | 0.76 | 1.68 | 32.8 |
| keyset_index | keyset | small (80) | 4 018 (2 439–5 575) | 0.58 | 1.69 | 11.9 |
| keyset_index | offset | big (60 000) | 180 (170–235) | 11.6 | 69.2 | 81.0 |
| keyset_index | offset | medium (15 000) | 791 (666–852) | 2.53 | 43.9 | 52.6 |
| keyset_index | offset | small (80) | 4 059 (3 403–4 252) | 0.60 | 1.05 | 9.09 |
| chat_id_index | keyset | big (60 000) | 89.5 (89.4–89.8) | 27.0 | 80.9 | 99.0 |
| chat_id_index | keyset | medium (15 000) | 362 (346–364) | 5.64 | 55.7 | 59.4 |
| chat_id_index | keyset | small (80) | 4 115 (4 068–4 140) | 0.60 | 1.03 | 4.81 |
| chat_id_index | offset | big (60 000) | 28.3 (28.0–29.3) | 120 | 278 | 389 |
| chat_id_index | offset | medium (15 000) | 135 (135–140) | 16.8 | 71.2 | 76.1 |
| chat_id_index | offset | small (80) | 4 222 (4 214–4 266) | 0.58 | 0.98 | 3.83 |
| no_index | keyset | big (60 000) | 29.0 (21.6–30.7) | 112 | 197 | 293 |
| no_index | keyset | medium (15 000) | 33.9 (26.0–34.3) | 105 | 185 | 192 |
| no_index | keyset | small (80) | 36.3 (25.1–37.4) | 102 | 182 | 193 |
| no_index | offset | big (60 000) | 25.7 (18.0–26.4) | 134 | 293 | 385 |
| no_index | offset | medium (15 000) | 49.7 (44.9–63.3) | 85.7 | 118 | 171 |
| no_index | offset | small (80) | 84.1 (77.6–105) | 31.0 | 82.4 | 94.9 |
## Запись: транзакция сообщения приложения (UPDATE chats … RETURNING + INSERT)
TPS и задержка — медиана 5 повторов по 10 с, 8 клиентов. WAL — медиана 3 детерминированных прогонов: 5 000 транзакций, 1 клиент, autovacuum выключен, CHECKPOINT перед прогоном.
| Индексы messages | TPS (min–max) | p50, мс | p95, мс | p99, мс | WAL, байт/вставку (min–max) | Размер индексов, МиБ |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| no_index | 1 954 (1 540–2 055) | 3.55 | 7.14 | 12.2 | 1930 (1884–1940) | 4.6 |
| keyset_index | 1 577 (1 002–1 630) | 4.73 | 8.50 | 13.0 | 2910 (2800–2926) | 24.0 |
| keyset_plus_sender | 1 223 (896–1 607) | 6.25 | 9.30 | 13.9 | 3318 (3310–3414) | 46.6 |
+10
View File
@@ -0,0 +1,10 @@
variant,repeat,inserts,wal_bytes,wal_bytes_per_insert
no_index,1,5000,9419936,1884
no_index,2,5000,9700304,1940
no_index,3,5000,9652184,1930
keyset_index,1,5000,14000160,2800
keyset_index,2,5000,14548936,2910
keyset_index,3,5000,14630336,2926
keyset_plus_sender,1,5000,17071344,3414
keyset_plus_sender,2,5000,16548592,3310
keyset_plus_sender,3,5000,16589232,3318
1 variant repeat inserts wal_bytes wal_bytes_per_insert
2 no_index 1 5000 9419936 1884
3 no_index 2 5000 9700304 1940
4 no_index 3 5000 9652184 1930
5 keyset_index 1 5000 14000160 2800
6 keyset_index 2 5000 14548936 2910
7 keyset_index 3 5000 14630336 2926
8 keyset_plus_sender 1 5000 17071344 3414
9 keyset_plus_sender 2 5000 16548592 3310
10 keyset_plus_sender 3 5000 16589232 3318
+16
View File
@@ -0,0 +1,16 @@
variant,repeat,tps,transactions,failed,p50_ms,p95_ms,p99_ms,mean_ms
no_index,1,2055.1,20532,0,3.43,6.677,10.312,3.882
no_index,2,1953.5,19529,0,3.547,7.139,12.24,4.082
no_index,3,2001.8,20012,0,3.487,6.848,10.324,3.983
no_index,4,1572.9,15729,0,4.303,9.726,16.331,5.067
no_index,5,1539.6,15382,0,4.367,9.093,16.95,5.178
keyset_index,1,1002.2,10036,0,5.796,19.448,26.224,7.955
keyset_index,2,1222.9,12222,0,6.34,10.159,14.613,6.529
keyset_index,3,1629.0,16281,0,4.33,8.497,12.283,4.896
keyset_index,4,1629.7,16291,0,4.393,8.13,12.979,4.896
keyset_index,5,1577.4,15763,0,4.732,8.336,11.991,5.058
keyset_plus_sender,1,937.7,9387,0,6.629,17.527,30.246,8.507
keyset_plus_sender,2,895.6,8954,0,6.249,22.727,33.156,8.916
keyset_plus_sender,3,1606.9,16067,0,4.58,8.243,11.774,4.965
keyset_plus_sender,4,1441.4,14415,0,5.61,8.542,11.815,5.535
keyset_plus_sender,5,1223.3,12264,0,6.29,9.296,13.881,6.526
1 variant repeat tps transactions failed p50_ms p95_ms p99_ms mean_ms
2 no_index 1 2055.1 20532 0 3.43 6.677 10.312 3.882
3 no_index 2 1953.5 19529 0 3.547 7.139 12.24 4.082
4 no_index 3 2001.8 20012 0 3.487 6.848 10.324 3.983
5 no_index 4 1572.9 15729 0 4.303 9.726 16.331 5.067
6 no_index 5 1539.6 15382 0 4.367 9.093 16.95 5.178
7 keyset_index 1 1002.2 10036 0 5.796 19.448 26.224 7.955
8 keyset_index 2 1222.9 12222 0 6.34 10.159 14.613 6.529
9 keyset_index 3 1629.0 16281 0 4.33 8.497 12.283 4.896
10 keyset_index 4 1629.7 16291 0 4.393 8.13 12.979 4.896
11 keyset_index 5 1577.4 15763 0 4.732 8.336 11.991 5.058
12 keyset_plus_sender 1 937.7 9387 0 6.629 17.527 30.246 8.507
13 keyset_plus_sender 2 895.6 8954 0 6.249 22.727 33.156 8.916
14 keyset_plus_sender 3 1606.9 16067 0 4.58 8.243 11.774 4.965
15 keyset_plus_sender 4 1441.4 14415 0 5.61 8.542 11.815 5.535
16 keyset_plus_sender 5 1223.3 12264 0 6.29 9.296 13.881 6.526
+2
View File
@@ -0,0 +1,2 @@
# Only for bench/track2/summarize.py charts (optional).
matplotlib==3.11.2