feat: implement persistent messenger with PostgreSQL and Redis
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
sashadugina06
2026-10-30 14:02:09 +03:00
parent 65003b6119
commit bd34c1050a
40 changed files with 2879 additions and 24 deletions
+130
View File
@@ -0,0 +1,130 @@
{
"date": "2026-10-30T10:56:54.044284+00:00",
"checks": [
{
"command": "make check",
"exit_code": 0,
"stdout": "python3 scripts/check_contract.py\nPASS: offline skeleton structure, local refs and Python syntax; application NOT tested.\n",
"stderr": ""
},
{
"command": "make validate PYTHON=.venv/bin/python",
"exit_code": 0,
"stdout": ".venv/bin/python scripts/validate_openapi.py\nPASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.\n",
"stderr": ""
},
{
"command": "make test",
"exit_code": 0,
"stdout": "python3 tests/smoke.py --base-url \"http://localhost:8080\" \nPASS: lab2 public API scenario; infrastructure and grading evidence remain separate.\n",
"stderr": ""
},
{
"command": "python3 scripts/check_auth.py",
"exit_code": 0,
"stdout": "1. Registration\nRegistration status: 201\nRegistration headers: {'Content-Length': '144', 'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Date': 'Fri, 30 Oct 2026 10:56:55 GMT', 'Keep-Alive': 'timeout=60', 'Proxy-Connection': 'keep-alive', 'Server': 'uvicorn', 'X-Request-Id': 'e18774ae-f206-4216-8a6c-81648825f093'}\nRegistration body: {'id': 'abd4cc85-fe81-4dae-a33a-4b09fcc993a4', 'username': 'test_10537209d0', 'display_name': 'Test User', 'created_at': '2026-10-30T10:56:55.192161Z'}\nPASS: registration: 201 (expected 201)\nUser ID: abd4cc85-fe81-4dae-a33a-4b09fcc993a4\n\n2. Basic login\nPASS: login: 201 (expected 201)\n\n3. Access with Bearer token\nPASS: authorized request: 200 (expected 200)\n\n4. Access without token\nPASS: missing token: 401 (expected 401)\n\n5. X-User-Id cannot authorize\nPASS: X-User-Id rejected: 401 (expected 401)\n\n6. Logout\nPASS: logout: 204 (expected 204)\n\n7. Reuse revoked token\nPASS: revoked token rejected: 401 (expected 401)\n\nALL AUTH CHECKS PASSED\n",
"stderr": ""
},
{
"command": "python3 scripts/check_advanced.py",
"exit_code": 0,
"stdout": "1. Concurrent registration\nPASS: one registration, nine conflicts\n2. Incorrect password\nPASS: incorrect password rejected\n3. Invalid Bearer token\nPASS: invalid token rejected\nPASS: malformed and non-ASCII Bearer tokens rejected\n4. Unknown chat member\nPASS: unknown member rejected\n\nALL ADVANCED API CHECKS PASSED\n",
"stderr": ""
},
{
"command": "python3 scripts/check_messenger.py",
"exit_code": 0,
"stdout": "\n1. Register two users\nPASS: register alice_b35280a7: 201\nPASS: register bob_b35280a7: 201\n\n2. Login\nPASS: login alice_b35280a7: 201\nPASS: login bob_b35280a7: 201\n\n3. Create chat\nPASS: create chat: 201\nChat ID: 1b010c7a-727b-43cf-965c-4377c6c87f05\n\n4. Check chat visibility\nPASS: get chat: 200\nPASS: get chat: 200\n\n5. Send messages\nPASS: send message: 201\nPASS: send message: 201\nPASS: send message: 201\nPASS: send message: 201\nPASS: send message: 201\n\n6. Read first page\nPASS: first page: 200\n\n7. Read second page\nPASS: second page: 200\n\n8. Check chat list\nPASS: list chats: 200\n\n9. Check PostgreSQL persistence\nCreated chat: 1b010c7a-727b-43cf-965c-4377c6c87f05\nCreated messages: 5\n\nALL MESSENGER CHECKS PASSED\n",
"stderr": ""
},
{
"command": "python3 scripts/check_redis_failure.py",
"exit_code": 0,
"stdout": "PASS: Redis contains session profile with TTL\nPASS: PostgreSQL read/write and logout work without Redis\nPASS: stale cache cannot revive revoked session; Redis recovered\n",
"stderr": " Container mtusi2026lab2-redis-1 Stopping\n Container mtusi2026lab2-redis-1 Stopped\ntime=\"2026-10-30T13:56:57+03:00\" level=warning msg=\"Found orphan containers ([mtusi2026lab2-api-ttl-1]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.\"\n Container mtusi2026lab2-redis-1 Created\n Container mtusi2026lab2-redis-1 Starting\n Container mtusi2026lab2-redis-1 Started\n Container mtusi2026lab2-redis-1 Waiting\n Container mtusi2026lab2-redis-1 Healthy\n"
},
{
"command": "python3 scripts/check_db_failure.py",
"exit_code": 0,
"stdout": "PASS: token works before database failure\nPASS: database failure does not grant access: 503\nALL DATABASE FAILURE CHECKS PASSED; original session recovered\n",
"stderr": " Container mtusi2026lab2-postgres-1 Stopping\n Container mtusi2026lab2-postgres-1 Stopped\n Container mtusi2026lab2-postgres-1 Starting\n Container mtusi2026lab2-postgres-1 Started\n"
},
{
"command": "python3 scripts/check_migrations.py",
"exit_code": 0,
"stdout": "PASS: migration creates five tables from zero and reapplies without errors; rolled back\n",
"stderr": ""
},
{
"command": "docker compose exec -T api python",
"exit_code": 0,
"stdout": "PASS: failure after first member insert rolls back chat and membership\n",
"stderr": "",
"stdin_file": "scripts/check_transactions.py"
},
{
"command": "make test-persistence ACTION_SCRIPT=scripts/restart.sh",
"exit_code": 0,
"stdout": "python3 tests/resilience.py --mode persistence --action-script \"scripts/restart.sh\" --base-url \"http://localhost:8080\" \nExecuting explicit persistence hook; hook output suppressed to avoid leaking secrets.\nPASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.\n",
"stderr": ""
},
{
"command": "docker compose -f compose.yaml -f compose.ttl.yaml up -d --build --wait api-ttl",
"exit_code": 0,
"stdout": "Compose now can delegate build to bake for better performances\nJust set COMPOSE_BAKE=true\n#0 building with \"desktop-linux\" instance using docker driver\n\n#1 [api-ttl internal] load build definition from Dockerfile\n#1 transferring dockerfile: 778B done\n#1 DONE 0.0s\n\n#2 [api-ttl internal] load metadata for docker.io/library/python:3.11.16-slim-bookworm\n#2 DONE 0.0s\n\n#3 [api-ttl internal] load .dockerignore\n#3 transferring context: 196B done\n#3 DONE 0.0s\n\n#4 [api-ttl internal] load build context\n#4 transferring context: 720B done\n#4 DONE 0.0s\n\n#5 [api-ttl 1/6] FROM docker.io/library/python:3.11.16-slim-bookworm@sha256:a36c24f9cbdf4fd0f52d67f0823eeac19c2028c637cecc392d97f980d4fec56b\n#5 resolve docker.io/library/python:3.11.16-slim-bookworm@sha256:a36c24f9cbdf4fd0f52d67f0823eeac19c2028c637cecc392d97f980d4fec56b\n#5 resolve docker.io/library/python:3.11.16-slim-bookworm@sha256:a36c24f9cbdf4fd0f52d67f0823eeac19c2028c637cecc392d97f980d4fec56b 0.7s done\n#5 DONE 0.7s\n\n#6 [api-ttl 3/6] COPY requirements.txt .\n#6 CACHED\n\n#7 [api-ttl 4/6] RUN python -m pip install --no-cache-dir -r requirements.txt\n#7 CACHED\n\n#8 [api-ttl 5/6] RUN useradd --uid 10001 --create-home --shell /usr/sbin/nologin appuser\n#8 CACHED\n\n#9 [api-ttl 2/6] WORKDIR /app\n#9 CACHED\n\n#10 [api-ttl 6/6] COPY --chown=appuser:appuser src/ ./src/\n#10 CACHED\n\n#11 [api-ttl] exporting to image\n#11 exporting layers done\n#11 exporting manifest sha256:a6a0f6f9fc66e2ad26dd5d141b27ab8e4d3a8ee26b43afa21aa58c6e5ef10fcd done\n#11 exporting config sha256:9232a3492de92095ab11f4877bc256cb3227c8ae3cc63430bd9f3664a4d357ce done\n#11 exporting attestation manifest sha256:4673ee5cb2ce9fedb997670011d04711c70452a44bc23d18c8313e17370062e2 done\n#11 exporting manifest list sha256:8f1054addd1e51ae837fa0a23e0e485839f97b15257f8aa53638fd848978c5c5 done\n#11 naming to docker.io/library/mtusi2026lab2-api-ttl:latest done\n#11 unpacking to docker.io/library/mtusi2026lab2-api-ttl:latest done\n#11 DONE 0.1s\n\n#12 [api-ttl] resolving provenance for metadata file\n#12 DONE 0.0s\n",
"stderr": " api-ttl Built\n Container mtusi2026lab2-postgres-1 Running\n Container mtusi2026lab2-api-ttl-1 Recreate\n Container mtusi2026lab2-api-ttl-1 Recreated\n Container mtusi2026lab2-postgres-1 Waiting\n Container mtusi2026lab2-postgres-1 Healthy\n Container mtusi2026lab2-api-ttl-1 Starting\n Container mtusi2026lab2-api-ttl-1 Started\n Container mtusi2026lab2-postgres-1 Waiting\n Container mtusi2026lab2-api-ttl-1 Waiting\n Container mtusi2026lab2-postgres-1 Healthy\n Container mtusi2026lab2-api-ttl-1 Healthy\n"
},
{
"command": "python3 scripts/check_ttl.py",
"exit_code": 0,
"stdout": "PASS: session expires after TTL\n",
"stderr": ""
},
{
"command": "docker compose -f compose.yaml -f compose.ttl.yaml stop api-ttl",
"exit_code": 0,
"stdout": "",
"stderr": " Container mtusi2026lab2-api-ttl-1 Stopping\n Container mtusi2026lab2-api-ttl-1 Stopped\n"
},
{
"command": "docker compose exec -T postgres psql -X -v ON_ERROR_STOP=1 -U messenger -d messenger",
"exit_code": 0,
"stdout": "BEGIN\nSET\nPREPARE\n QUERY PLAN \n-----------------------------------------------------------------------------------------------------------------------------------------------------\n Limit (cost=0.42..582.96 rows=584 width=78) (actual time=0.020..0.032 rows=51 loops=1)\n Buffers: shared hit=2 read=3\n -> Index Scan using idx_messages_chat_created_id on messages (cost=0.42..5820.85 rows=5835 width=78) (actual time=0.019..0.028 rows=51 loops=1)\n Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))\n Buffers: shared hit=2 read=3\n Planning:\n Buffers: shared hit=6\n Planning Time: 0.128 ms\n Execution Time: 0.054 ms\n(9 rows)\n\nROLLBACK\n",
"stderr": "",
"stdin_file": "scripts/explain_pagination.sql"
},
{
"command": "docker compose exec -T postgres psql -X -v ON_ERROR_STOP=1 -U messenger -d messenger",
"exit_code": 0,
"stdout": "BEGIN\nTiming is on.\nANALYZE\nTime: 117.334 ms\n chat_id | messages_count \n--------------------------------------+----------------\n f68eff95-e44f-4c79-997c-49eb725cfb4d | 90005\n b14d0a4f-3046-44b2-871e-4ebfb752c543 | 11613\n 41d9abbc-4d0b-44f7-a1a2-ea3de4faa0c3 | 10942\n 1b7b678e-d262-4d61-8167-7fc8b736f3ff | 10005\n 0d8c7368-f88a-4bdb-972e-e4d7e1cd437a | 5\n(5 rows)\n\nTime: 10.412 ms\nTime: 8.597 ms\nTEST A: existing composite index\n QUERY PLAN \n-------------------------------------------------------------------------------------------------------------------------------------------------------\n Limit (cost=0.42..6.74 rows=50 width=45) (actual time=0.025..0.036 rows=50 loops=1)\n Buffers: shared hit=3 read=2\n -> Index Scan using idx_messages_chat_created_id on messages (cost=0.42..11395.89 rows=90150 width=45) (actual time=0.024..0.033 rows=50 loops=1)\n Index Cond: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)\n Buffers: shared hit=3 read=2\n Planning:\n Buffers: shared hit=10\n Planning Time: 0.069 ms\n Execution Time: 0.044 ms\n(9 rows)\n\nTime: 0.360 ms\nTEST B: additional index\nCREATE INDEX\nTime: 0.285 ms\nANALYZE\nTime: 123.698 ms\n QUERY PLAN \n-------------------------------------------------------------------------------------------------------------------------------------------------------\n Limit (cost=0.42..6.75 rows=50 width=45) (actual time=0.007..0.014 rows=50 loops=1)\n Buffers: shared hit=5\n -> Index Scan using idx_messages_chat_created_id on messages (cost=0.42..11378.89 rows=89905 width=45) (actual time=0.006..0.011 rows=50 loops=1)\n Index Cond: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)\n Buffers: shared hit=5\n Planning:\n Buffers: shared hit=15\n Planning Time: 0.081 ms\n Execution Time: 0.023 ms\n(9 rows)\n\nTime: 0.279 ms\nIndex sizes\n indexrelname | size \n------------------------------+---------\n idx_messages_chat_created_id | 9328 kB\n idx_messages_chat_id_only | 1016 kB\n messages_pkey | 5392 kB\n(3 rows)\n\nTime: 1.366 ms\nROLLBACK\nTime: 0.119 ms\n",
"stderr": "NOTICE: relation \"idx_messages_chat_id_only\" already exists, skipping\n",
"stdin_file": "scripts/benchmark_indexes.sql"
},
{
"command": "docker compose exec -T postgres psql -X -v ON_ERROR_STOP=1 -U messenger -d messenger",
"exit_code": 0,
"stdout": "Timing is on.\nTime: 8.859 ms\nCOMPOSITE INDEX\n QUERY PLAN \n-------------------------------------------------------------------------------------------------------------------------------------------------------\n Limit (cost=0.42..6.06 rows=50 width=46) (actual time=0.028..0.039 rows=50 loops=1)\n Buffers: shared hit=5\n -> Index Scan using idx_messages_chat_created_id on messages (cost=0.42..11162.90 rows=98962 width=46) (actual time=0.027..0.036 rows=50 loops=1)\n Index Cond: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)\n Buffers: shared hit=5\n Planning:\n Buffers: shared hit=14\n Planning Time: 0.097 ms\n Execution Time: 0.048 ms\n(9 rows)\n\nTime: 0.507 ms\nBEGIN\nTime: 0.083 ms\nCREATE INDEX\nTime: 0.205 ms\nDROP INDEX\nTime: 0.680 ms\nSINGLE COLUMN INDEX\n QUERY PLAN \n-----------------------------------------------------------------------------------------------------------------------------------------\n Limit (cost=5448.46..5454.21 rows=50 width=46) (actual time=13.704..14.946 rows=50 loops=1)\n Buffers: shared hit=114 read=1543\n -> Gather Merge (cost=5448.46..12142.95 rows=58213 width=46) (actual time=13.703..14.938 rows=50 loops=1)\n Workers Planned: 1\n Workers Launched: 1\n Buffers: shared hit=114 read=1543\n -> Sort (cost=4448.45..4593.98 rows=58213 width=46) (actual time=12.461..12.463 rows=50 loops=2)\n Sort Key: created_at, id\n Sort Method: top-N heapsort Memory: 28kB\n Buffers: shared hit=114 read=1543\n Worker 0: Sort Method: top-N heapsort Memory: 28kB\n -> Parallel Seq Scan on messages (cost=0.00..2514.65 rows=58213 width=46) (actual time=0.012..8.363 rows=45002 loops=2)\n Filter: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)\n Rows Removed by Filter: 16310\n Buffers: shared hit=70 read=1543\n Planning:\n Buffers: shared hit=4\n Planning Time: 0.038 ms\n Execution Time: 14.977 ms\n(19 rows)\n\nTime: 15.268 ms\nROLLBACK\nTime: 0.138 ms\nORIGINAL INDEX RESTORED\n",
"stderr": "NOTICE: relation \"idx_messages_chat_id_only\" already exists, skipping\n",
"stdin_file": "scripts/compare_indexes.sql"
},
{
"command": "docker compose exec -T postgres psql -X -v ON_ERROR_STOP=1 -U messenger -d messenger",
"exit_code": 0,
"stdout": "Timing is on.\nBEGIN\nTime: 0.114 ms\nDROP INDEX\nTime: 0.510 ms\nDROP INDEX\nTime: 0.255 ms\nWRITE WITHOUT SECONDARY INDEXES\n QUERY PLAN \n----------------------------------------------------------------------------------------------------------------------------------------------\n Insert on messages (cost=0.00..275.41 rows=0 width=0) (actual time=38.420..38.421 rows=0 loops=1)\n Buffers: shared hit=39651 read=676 dirtied=815 written=143\n -> Subquery Scan on \"*SELECT*\" (cost=0.00..275.41 rows=10000 width=88) (actual time=0.387..11.577 rows=10000 loops=1)\n Buffers: shared hit=1\n -> Limit (cost=0.00..150.41 rows=10000 width=80) (actual time=0.386..10.826 rows=10000 loops=1)\n Buffers: shared hit=1\n -> Nested Loop (cost=0.00..42113.50 rows=2800000 width=80) (actual time=0.385..10.225 rows=10000 loops=1)\n Buffers: shared hit=1\n -> Function Scan on generate_series (cost=0.00..100.00 rows=10000 width=0) (actual time=0.358..0.388 rows=345 loops=1)\n -> Materialize (cost=0.00..14.20 rows=280 width=32) (actual time=0.000..0.001 rows=29 loops=345)\n Buffers: shared hit=1\n -> Seq Scan on chats c (cost=0.00..12.80 rows=280 width=32) (actual time=0.009..0.013 rows=29 loops=1)\n Buffers: shared hit=1\n Planning:\n Buffers: shared hit=65\n Planning Time: 0.148 ms\n Trigger for constraint messages_chat_id_fkey: time=26.385 calls=10000\n Trigger for constraint messages_sender_id_fkey: time=45.532 calls=10000\n Execution Time: 111.483 ms\n(19 rows)\n\nTime: 112.022 ms\nROLLBACK\nTime: 0.118 ms\nBEGIN\nTime: 0.125 ms\nCREATE INDEX\nTime: 0.326 ms\nWRITE WITH EXISTING INDEXES\n QUERY PLAN \n----------------------------------------------------------------------------------------------------------------------------------------------\n Insert on messages (cost=0.00..275.41 rows=0 width=0) (actual time=48.708..48.709 rows=0 loops=1)\n Buffers: shared hit=90737 read=23 dirtied=292 written=245\n -> Subquery Scan on \"*SELECT*\" (cost=0.00..275.41 rows=10000 width=88) (actual time=0.533..11.244 rows=10000 loops=1)\n Buffers: shared hit=1\n -> Limit (cost=0.00..150.41 rows=10000 width=80) (actual time=0.532..10.510 rows=10000 loops=1)\n Buffers: shared hit=1\n -> Nested Loop (cost=0.00..42113.50 rows=2800000 width=80) (actual time=0.531..9.910 rows=10000 loops=1)\n Buffers: shared hit=1\n -> Function Scan on generate_series (cost=0.00..100.00 rows=10000 width=0) (actual time=0.497..0.525 rows=345 loops=1)\n -> Materialize (cost=0.00..14.20 rows=280 width=32) (actual time=0.000..0.001 rows=29 loops=345)\n Buffers: shared hit=1\n -> Seq Scan on chats c (cost=0.00..12.80 rows=280 width=32) (actual time=0.008..0.011 rows=29 loops=1)\n Buffers: shared hit=1\n Planning Time: 0.055 ms\n Trigger for constraint messages_chat_id_fkey: time=26.086 calls=10000\n Trigger for constraint messages_sender_id_fkey: time=41.720 calls=10000\n Execution Time: 117.756 ms\n(17 rows)\n\nTime: 118.278 ms\nROLLBACK\nTime: 0.159 ms\n",
"stderr": "NOTICE: relation \"idx_messages_chat_id_only\" already exists, skipping\n",
"stdin_file": "scripts/benchmark_writes.sql"
}
],
"restart_before": {
"api": "2026-10-30T10:55:12.637263918Z",
"postgres": "2026-10-30T10:57:10.385474751Z",
"redis": "2026-10-30T10:56:57.171946925Z"
},
"restart_after": {
"api": "2026-10-30T10:57:19.664978005Z",
"postgres": "2026-10-30T10:57:13.61408121Z",
"redis": "2026-10-30T10:57:13.675466502Z"
},
"all_three_services_restarted": true,
"backup_restore_evidence": "evidence/backup_restore.json"
}
+34
View File
@@ -0,0 +1,34 @@
{
"date_utc": "20261030T105422Z-43b52d",
"container": "lab2-audit-restore-20261030t105422z-43b52d",
"volume": "lab2-audit-restore-20261030t105422z-43b52d-data",
"network": "none",
"backup": "backups/20261030T105422Z-43b52d.dump",
"pg_restore_seconds": 0.437,
"source_counts_and_sample_md5": [
"chat_members|50",
"chats|25",
"messages|122616",
"sessions|84",
"users|87",
"00003673-6e8b-4225-a3a1-e15fd328e504|b1cb97941c6ae16d504db747a8e82214",
"000074f3-3b8e-4165-94f3-1b5e99e16a2e|9f58a18ad0e8d3707ae9ab03cbef0866",
"0000c11d-0b41-4b07-acbf-dae56b612638|6f9ecd02b934fd283b096ebd22f38442",
"000297c9-7c10-42c5-a642-9dc384530091|dc792e0e8909c263f626e4205a5947dd",
"0002df01-338a-4ab0-a7c3-6f895fad06d1|ce5e5167d711d1154349cf5f93def759"
],
"restored_counts_and_sample_md5": [
"chat_members|50",
"chats|25",
"messages|122616",
"sessions|84",
"users|87",
"00003673-6e8b-4225-a3a1-e15fd328e504|b1cb97941c6ae16d504db747a8e82214",
"000074f3-3b8e-4165-94f3-1b5e99e16a2e|9f58a18ad0e8d3707ae9ab03cbef0866",
"0000c11d-0b41-4b07-acbf-dae56b612638|6f9ecd02b934fd283b096ebd22f38442",
"000297c9-7c10-42c5-a642-9dc384530091|dc792e0e8909c263f626e4205a5947dd",
"0002df01-338a-4ab0-a7c3-6f895fad06d1|ce5e5167d711d1154349cf5f93def759"
],
"result": "PASS",
"limitation": "counts for all five tables and five message id/text hashes; not full RTO or full DB checksum"
}
+57
View File
@@ -0,0 +1,57 @@
Timing is on.
ANALYZE
Time: 119.924 ms
chat_id | messages_count
--------------------------------------+----------------
f68eff95-e44f-4c79-997c-49eb725cfb4d | 90005
b14d0a4f-3046-44b2-871e-4ebfb752c543 | 11613
1b7b678e-d262-4d61-8167-7fc8b736f3ff | 10005
baabd1bb-8b74-41dd-9bcf-d801d31c160c | 5
aeed09d0-85e4-41c2-a77b-b2c2dd6a0de3 | 5
(5 rows)
Time: 9.632 ms
Time: 8.499 ms
TEST A: existing composite index
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..5.86 rows=50 width=46) (actual time=0.007..0.014 rows=50 loops=1)
Buffers: shared hit=5
-> Index Scan using idx_messages_chat_created_id on messages (cost=0.42..9781.28 rows=89792 width=46) (actual time=0.006..0.011 rows=50 loops=1)
Index Cond: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)
Buffers: shared hit=5
Planning:
Buffers: shared hit=9
Planning Time: 0.042 ms
Execution Time: 0.021 ms
(9 rows)
Time: 0.314 ms
TEST B: additional index
CREATE INDEX
Time: 40.624 ms
ANALYZE
Time: 116.772 ms
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..5.84 rows=50 width=46) (actual time=0.018..0.026 rows=50 loops=1)
Buffers: shared hit=5
-> Index Scan using idx_messages_chat_created_id on messages (cost=0.42..9765.59 rows=90101 width=46) (actual time=0.017..0.023 rows=50 loops=1)
Index Cond: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)
Buffers: shared hit=5
Planning:
Buffers: shared hit=14 read=1
Planning Time: 0.125 ms
Execution Time: 0.043 ms
(9 rows)
Time: 0.372 ms
Index sizes
indexrelname | size
------------------------------+---------
idx_messages_chat_created_id | 7424 kB
idx_messages_chat_id_only | 784 kB
messages_pkey | 4432 kB
(3 rows)
Time: 1.309 ms
+60
View File
@@ -0,0 +1,60 @@
BEGIN
Timing is on.
ANALYZE
Time: 117.334 ms
chat_id | messages_count
--------------------------------------+----------------
f68eff95-e44f-4c79-997c-49eb725cfb4d | 90005
b14d0a4f-3046-44b2-871e-4ebfb752c543 | 11613
41d9abbc-4d0b-44f7-a1a2-ea3de4faa0c3 | 10942
1b7b678e-d262-4d61-8167-7fc8b736f3ff | 10005
0d8c7368-f88a-4bdb-972e-e4d7e1cd437a | 5
(5 rows)
Time: 10.412 ms
Time: 8.597 ms
TEST A: existing composite index
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..6.74 rows=50 width=45) (actual time=0.025..0.036 rows=50 loops=1)
Buffers: shared hit=3 read=2
-> Index Scan using idx_messages_chat_created_id on messages (cost=0.42..11395.89 rows=90150 width=45) (actual time=0.024..0.033 rows=50 loops=1)
Index Cond: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)
Buffers: shared hit=3 read=2
Planning:
Buffers: shared hit=10
Planning Time: 0.069 ms
Execution Time: 0.044 ms
(9 rows)
Time: 0.360 ms
TEST B: additional index
CREATE INDEX
Time: 0.285 ms
ANALYZE
Time: 123.698 ms
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..6.75 rows=50 width=45) (actual time=0.007..0.014 rows=50 loops=1)
Buffers: shared hit=5
-> Index Scan using idx_messages_chat_created_id on messages (cost=0.42..11378.89 rows=89905 width=45) (actual time=0.006..0.011 rows=50 loops=1)
Index Cond: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)
Buffers: shared hit=5
Planning:
Buffers: shared hit=15
Planning Time: 0.081 ms
Execution Time: 0.023 ms
(9 rows)
Time: 0.279 ms
Index sizes
indexrelname | size
------------------------------+---------
idx_messages_chat_created_id | 9328 kB
idx_messages_chat_id_only | 1016 kB
messages_pkey | 5392 kB
(3 rows)
Time: 1.366 ms
ROLLBACK
Time: 0.119 ms
+49
View File
@@ -0,0 +1,49 @@
Timing is on.
Time: 10.952 ms
COMPOSITE INDEX
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..5.93 rows=50 width=46) (actual time=0.028..0.041 rows=50 loops=1)
Buffers: shared hit=1 read=4
-> Index Scan using idx_messages_chat_created_id on messages (cost=0.42..10839.86 rows=98331 width=46) (actual time=0.027..0.037 rows=50 loops=1)
Index Cond: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)
Buffers: shared hit=1 read=4
Planning:
Buffers: shared hit=14
Planning Time: 0.060 ms
Execution Time: 0.048 ms
(9 rows)
Time: 0.417 ms
BEGIN
Time: 0.106 ms
DROP INDEX
Time: 0.657 ms
SINGLE COLUMN INDEX
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=5430.38..5436.13 rows=50 width=46) (actual time=12.333..13.504 rows=50 loops=1)
Buffers: shared hit=182 read=1475
-> Gather Merge (cost=5430.38..12082.21 rows=57842 width=46) (actual time=12.331..13.500 rows=50 loops=1)
Workers Planned: 1
Workers Launched: 1
Buffers: shared hit=182 read=1475
-> Sort (cost=4430.37..4574.98 rows=57842 width=46) (actual time=11.217..11.219 rows=50 loops=2)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 28kB
Buffers: shared hit=182 read=1475
Worker 0: Sort Method: top-N heapsort Memory: 28kB
-> Parallel Seq Scan on messages (cost=0.00..2508.90 rows=57842 width=46) (actual time=0.009..7.022 rows=45002 loops=2)
Filter: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)
Rows Removed by Filter: 16293
Buffers: shared hit=138 read=1475
Planning:
Buffers: shared hit=4
Planning Time: 0.052 ms
Execution Time: 13.525 ms
(19 rows)
Time: 13.780 ms
ROLLBACK
Time: 0.088 ms
ORIGINAL INDEX RESTORED
+51
View File
@@ -0,0 +1,51 @@
Timing is on.
Time: 8.859 ms
COMPOSITE INDEX
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..6.06 rows=50 width=46) (actual time=0.028..0.039 rows=50 loops=1)
Buffers: shared hit=5
-> Index Scan using idx_messages_chat_created_id on messages (cost=0.42..11162.90 rows=98962 width=46) (actual time=0.027..0.036 rows=50 loops=1)
Index Cond: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)
Buffers: shared hit=5
Planning:
Buffers: shared hit=14
Planning Time: 0.097 ms
Execution Time: 0.048 ms
(9 rows)
Time: 0.507 ms
BEGIN
Time: 0.083 ms
CREATE INDEX
Time: 0.205 ms
DROP INDEX
Time: 0.680 ms
SINGLE COLUMN INDEX
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=5448.46..5454.21 rows=50 width=46) (actual time=13.704..14.946 rows=50 loops=1)
Buffers: shared hit=114 read=1543
-> Gather Merge (cost=5448.46..12142.95 rows=58213 width=46) (actual time=13.703..14.938 rows=50 loops=1)
Workers Planned: 1
Workers Launched: 1
Buffers: shared hit=114 read=1543
-> Sort (cost=4448.45..4593.98 rows=58213 width=46) (actual time=12.461..12.463 rows=50 loops=2)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 28kB
Buffers: shared hit=114 read=1543
Worker 0: Sort Method: top-N heapsort Memory: 28kB
-> Parallel Seq Scan on messages (cost=0.00..2514.65 rows=58213 width=46) (actual time=0.012..8.363 rows=45002 loops=2)
Filter: (chat_id = 'f68eff95-e44f-4c79-997c-49eb725cfb4d'::uuid)
Rows Removed by Filter: 16310
Buffers: shared hit=70 read=1543
Planning:
Buffers: shared hit=4
Planning Time: 0.038 ms
Execution Time: 14.977 ms
(19 rows)
Time: 15.268 ms
ROLLBACK
Time: 0.138 ms
ORIGINAL INDEX RESTORED
+6
View File
@@ -0,0 +1,6 @@
indexrelname | index_size | idx_scan
------------------------------+------------+----------
idx_messages_chat_created_id | 8552 kB | 26
idx_messages_chat_id_only | 912 kB | 2
messages_pkey | 4608 kB | 9
(3 rows)
+18
View File
@@ -0,0 +1,18 @@
python3 tests/load.py --base-url "http://localhost:8080"
{
"lab": 2,
"workload": "closed-loop POST messages; no automatic retries",
"requested_duration_seconds": 30,
"elapsed_seconds": 30.021,
"concurrency": 4,
"successful_requests": 10942,
"failed_requests": 0,
"error_ratio": 0.0,
"successful_rps": 364.483,
"successful_latency_ms": {
"p50": 10.341,
"p95": 16.404,
"p99": 21.772
},
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
}
+17
View File
@@ -0,0 +1,17 @@
BEGIN
SET
PREPARE
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..582.96 rows=584 width=78) (actual time=0.020..0.032 rows=51 loops=1)
Buffers: shared hit=2 read=3
-> Index Scan using idx_messages_chat_created_id on messages (cost=0.42..5820.85 rows=5835 width=78) (actual time=0.019..0.028 rows=51 loops=1)
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
Buffers: shared hit=2 read=3
Planning:
Buffers: shared hit=6
Planning Time: 0.128 ms
Execution Time: 0.054 ms
(9 rows)
ROLLBACK
+61
View File
@@ -0,0 +1,61 @@
Timing is on.
BEGIN
Time: 0.100 ms
DROP INDEX
Time: 0.695 ms
DROP INDEX
Time: 0.189 ms
WRITE WITHOUT SECONDARY INDEXES
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------
Insert on messages (cost=0.00..275.41 rows=0 width=0) (actual time=30.658..30.659 rows=0 loops=1)
Buffers: shared hit=39804 read=579 dirtied=749 written=174
-> Subquery Scan on "*SELECT*" (cost=0.00..275.41 rows=10000 width=88) (actual time=0.339..10.740 rows=10000 loops=1)
Buffers: shared hit=1
-> Limit (cost=0.00..150.41 rows=10000 width=80) (actual time=0.338..9.999 rows=10000 loops=1)
Buffers: shared hit=1
-> Nested Loop (cost=0.00..42113.50 rows=2800000 width=80) (actual time=0.337..9.452 rows=10000 loops=1)
Buffers: shared hit=1
-> Function Scan on generate_series (cost=0.00..100.00 rows=10000 width=0) (actual time=0.322..0.365 rows=667 loops=1)
-> Materialize (cost=0.00..14.20 rows=280 width=32) (actual time=0.000..0.001 rows=15 loops=667)
Buffers: shared hit=1
-> Seq Scan on chats c (cost=0.00..12.80 rows=280 width=32) (actual time=0.004..0.006 rows=15 loops=1)
Buffers: shared hit=1
Planning:
Buffers: shared hit=65
Planning Time: 0.146 ms
Trigger for constraint messages_chat_id_fkey: time=25.322 calls=10000
Trigger for constraint messages_sender_id_fkey: time=32.144 calls=10000
Execution Time: 88.932 ms
(19 rows)
Time: 89.644 ms
ROLLBACK
Time: 0.131 ms
BEGIN
Time: 0.082 ms
WRITE WITH EXISTING INDEXES
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------
Insert on messages (cost=0.00..275.41 rows=0 width=0) (actual time=50.655..50.656 rows=0 loops=1)
Buffers: shared hit=90871 read=26 dirtied=348 written=320
-> Subquery Scan on "*SELECT*" (cost=0.00..275.41 rows=10000 width=88) (actual time=0.446..11.088 rows=10000 loops=1)
Buffers: shared hit=1
-> Limit (cost=0.00..150.41 rows=10000 width=80) (actual time=0.445..10.291 rows=10000 loops=1)
Buffers: shared hit=1
-> Nested Loop (cost=0.00..42113.50 rows=2800000 width=80) (actual time=0.444..9.751 rows=10000 loops=1)
Buffers: shared hit=1
-> Function Scan on generate_series (cost=0.00..100.00 rows=10000 width=0) (actual time=0.410..0.455 rows=667 loops=1)
-> Materialize (cost=0.00..14.20 rows=280 width=32) (actual time=0.000..0.001 rows=15 loops=667)
Buffers: shared hit=1
-> Seq Scan on chats c (cost=0.00..12.80 rows=280 width=32) (actual time=0.005..0.008 rows=15 loops=1)
Buffers: shared hit=1
Planning Time: 0.077 ms
Trigger for constraint messages_chat_id_fkey: time=25.106 calls=10000
Trigger for constraint messages_sender_id_fkey: time=31.329 calls=10000
Execution Time: 107.873 ms
(17 rows)
Time: 108.260 ms
ROLLBACK
Time: 0.109 ms
+63
View File
@@ -0,0 +1,63 @@
Timing is on.
BEGIN
Time: 0.114 ms
DROP INDEX
Time: 0.510 ms
DROP INDEX
Time: 0.255 ms
WRITE WITHOUT SECONDARY INDEXES
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------
Insert on messages (cost=0.00..275.41 rows=0 width=0) (actual time=38.420..38.421 rows=0 loops=1)
Buffers: shared hit=39651 read=676 dirtied=815 written=143
-> Subquery Scan on "*SELECT*" (cost=0.00..275.41 rows=10000 width=88) (actual time=0.387..11.577 rows=10000 loops=1)
Buffers: shared hit=1
-> Limit (cost=0.00..150.41 rows=10000 width=80) (actual time=0.386..10.826 rows=10000 loops=1)
Buffers: shared hit=1
-> Nested Loop (cost=0.00..42113.50 rows=2800000 width=80) (actual time=0.385..10.225 rows=10000 loops=1)
Buffers: shared hit=1
-> Function Scan on generate_series (cost=0.00..100.00 rows=10000 width=0) (actual time=0.358..0.388 rows=345 loops=1)
-> Materialize (cost=0.00..14.20 rows=280 width=32) (actual time=0.000..0.001 rows=29 loops=345)
Buffers: shared hit=1
-> Seq Scan on chats c (cost=0.00..12.80 rows=280 width=32) (actual time=0.009..0.013 rows=29 loops=1)
Buffers: shared hit=1
Planning:
Buffers: shared hit=65
Planning Time: 0.148 ms
Trigger for constraint messages_chat_id_fkey: time=26.385 calls=10000
Trigger for constraint messages_sender_id_fkey: time=45.532 calls=10000
Execution Time: 111.483 ms
(19 rows)
Time: 112.022 ms
ROLLBACK
Time: 0.118 ms
BEGIN
Time: 0.125 ms
CREATE INDEX
Time: 0.326 ms
WRITE WITH EXISTING INDEXES
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------
Insert on messages (cost=0.00..275.41 rows=0 width=0) (actual time=48.708..48.709 rows=0 loops=1)
Buffers: shared hit=90737 read=23 dirtied=292 written=245
-> Subquery Scan on "*SELECT*" (cost=0.00..275.41 rows=10000 width=88) (actual time=0.533..11.244 rows=10000 loops=1)
Buffers: shared hit=1
-> Limit (cost=0.00..150.41 rows=10000 width=80) (actual time=0.532..10.510 rows=10000 loops=1)
Buffers: shared hit=1
-> Nested Loop (cost=0.00..42113.50 rows=2800000 width=80) (actual time=0.531..9.910 rows=10000 loops=1)
Buffers: shared hit=1
-> Function Scan on generate_series (cost=0.00..100.00 rows=10000 width=0) (actual time=0.497..0.525 rows=345 loops=1)
-> Materialize (cost=0.00..14.20 rows=280 width=32) (actual time=0.000..0.001 rows=29 loops=345)
Buffers: shared hit=1
-> Seq Scan on chats c (cost=0.00..12.80 rows=280 width=32) (actual time=0.008..0.011 rows=29 loops=1)
Buffers: shared hit=1
Planning Time: 0.055 ms
Trigger for constraint messages_chat_id_fkey: time=26.086 calls=10000
Trigger for constraint messages_sender_id_fkey: time=41.720 calls=10000
Execution Time: 117.756 ms
(17 rows)
Time: 118.278 ms
ROLLBACK
Time: 0.159 ms