#!/usr/bin/env python3 """Track 2: isolated 100k dataset, index/write comparison, dump -> NEW DB -> checks. Uses only synthetic data in uniquely named research databases; never changes app tables. """ import datetime as dt import json from pathlib import Path import platform import re import statistics import subprocess import time import uuid ROOT = Path(__file__).resolve().parents[1] COMPOSE = ['docker', 'compose', '--env-file', '.env'] USER = '11111111-1111-4111-8111-111111111111' HOT = '22222222-2222-4222-8222-222222222222' def command(args, data=None): result = subprocess.run(args, cwd=ROOT, input=data, capture_output=True) if result.returncode: raise RuntimeError('Research infrastructure command failed: '+args[0]+' '+args[1]+'; '+result.stderr.decode(errors='replace')[:500]) return result.stdout def sql(database, query, service='postgres'): return command(COMPOSE+['exec','-T',service,'psql','-X','-v','ON_ERROR_STOP=1','-U','messenger','-d',database,'-At','-c',query]).decode().strip() def plan(database, query, rollback=False): query = 'EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) '+query if rollback: query = 'BEGIN; '+query+'; ROLLBACK;' output = sql(database, query) start = output.index('[') value, _ = json.JSONDecoder().raw_decode(output[start:]) return value[0] def fingerprint(database, service='postgres'): query = """SELECT json_build_object( 'users',(SELECT count(*) FROM users), 'chats',(SELECT count(*) FROM chats), 'members',(SELECT count(*) FROM chat_members), 'messages',(SELECT count(*) FROM messages), 'content_digest',(SELECT md5(string_agg(md5(row(id,chat_id,sender_id,text,created_at)::text),'' ORDER BY id)) FROM messages), 'selected',(SELECT json_agg(x) FROM (SELECT * FROM messages ORDER BY id LIMIT 10) x))""" return json.loads(sql(database, query, service)) def main(): suffix = uuid.uuid4().hex[:12] database, restored = 'research_'+suffix, 'research_restore_'+suffix created = [] results = {'generated_at_utc':dt.datetime.now(dt.timezone.utc).isoformat(), 'hypothesis':'Index (chat_id,created_at,id) bounds keyset page work independently of total chat history, at a measurable write/index-space cost.', 'host':platform.platform(),'limits':{'postgres_cpu':1,'postgres_memory_mb':512}, 'messages':100000,'distribution':[80000,15000]+[625]*8,'warmup_reads':2,'measured_repetitions':5,'variants':{}} try: command(COMPOSE+['--profile','research','up','-d','--wait','restore']) for name, service, admin in [(database,'postgres','messenger'),(restored,'restore','restore_validation')]: assert re.fullmatch(r'research_(restore_)?[a-f0-9]{12}',name) sql(admin,'CREATE DATABASE '+name,service) created.append((name, service, admin)) sql(database,(ROOT/'migrations'/'001_initial.sql').read_text(encoding='utf-8')) sql(database,"INSERT INTO users(id,username,display_name,password_hash) VALUES('"+USER+"','research_user','Synthetic researcher','research-fixture-not-a-login')") for chat_index, count in enumerate(results['distribution']): chat_id = HOT if chat_index==0 else str(uuid.uuid5(uuid.NAMESPACE_DNS,'lab2-research-chat-'+str(chat_index))) sql(database,f"INSERT INTO chats(id,title) VALUES('{chat_id}','Research {chat_index}'); INSERT INTO chat_members(chat_id,user_id,position) VALUES('{chat_id}','{USER}',0)") sql(database,f"INSERT INTO messages(id,chat_id,sender_id,text,created_at) SELECT md5('seed-{chat_index}-'||n)::uuid,'{chat_id}','{USER}',repeat('x',{200 if chat_index==0 else 40}),'2026-01-01T00:00:00Z'::timestamptz+n*interval '1 second' FROM generate_series(1,{count}) n") sql(database,'ANALYZE messages') results['postgres_version'] = sql(database,'SELECT version()') variants = {'chat_only':'CREATE INDEX research_chat_only ON messages(chat_id)', 'keyset':'CREATE INDEX messages_history ON messages(chat_id,created_at,id)'} for variant,ddl in variants.items(): sql(database,'DROP INDEX IF EXISTS messages_history; DROP INDEX IF EXISTS research_chat_only; '+ddl+'; ANALYZE messages') index_name = 'research_chat_only' if variant=='chat_only' else 'messages_history' measurements = {'index_bytes':int(sql(database,"SELECT pg_relation_size('"+index_name+"')")), 'pages':{}} for position in [0,40000,79000]: query = f"SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id='{HOT}' AND (created_at,id)>('2026-01-01T00:00:00Z'::timestamptz+{position}*interval '1 second','ffffffff-ffff-ffff-ffff-ffffffffffff'::uuid) ORDER BY created_at,id LIMIT 51" for _ in range(2): plan(database,query) plans = [plan(database,query) for _ in range(5)] measurements['pages'][str(position)] = {'execution_ms':[p['Execution Time'] for p in plans], 'median_ms':statistics.median(p['Execution Time'] for p in plans),'representative_plan':plans[2]} writes = [] for repetition in range(5): # Roll back after EACH measured insertion, so both variants see the same logical dataset. query = f"INSERT INTO messages(id,chat_id,sender_id,text,created_at) SELECT md5('write-{variant}-{repetition}-'||n)::uuid,'{HOT}','{USER}',repeat('w',200),'2026-02-01T00:00:00Z'::timestamptz+n*interval '1 second' FROM generate_series(1,2000) n" writes.append(plan(database,query,rollback=True)) measurements['write_2000'] = {'execution_ms':[p['Execution Time'] for p in writes], 'median_ms':statistics.median(p['Execution Time'] for p in writes),'representative_plan':writes[2]} results['variants'][variant] = measurements # The selected production index remains in the restored schema. source = fingerprint(database) check_count = source['messages']==100000 and source['chats']==10 if not check_count: raise AssertionError('Research dataset count mismatch') folder = ROOT/'backups' folder.mkdir(exist_ok=True) backup = folder/(database+'.dump') start = time.monotonic() dump = command(COMPOSE+['exec','-T','postgres','pg_dump','-U','messenger','-d',database,'-Fc']) backup.write_bytes(dump) backup_seconds = time.monotonic()-start start = time.monotonic() command(COMPOSE+['exec','-T','restore','pg_restore','--exit-on-error','--no-owner','-U','messenger','-d',restored], data=dump) restore_seconds = time.monotonic()-start target = fingerprint(restored,'restore') if source!=target: raise AssertionError('Restored data differs from snapshot') results['backup_restore'] = {'backup_bytes':len(dump),'backup_seconds':round(backup_seconds,3), 'restore_seconds':round(restore_seconds,3),'source':source,'restored':target,'equal':True, 'scope':'empty database on separate PostgreSQL container and separate named volume; all counts, ordered digest of every message, and 10 complete messages match'} results['limitations'] = ['Warm buffer cache, 5 sequential trials, local single-host containers; no claim of max production throughput.', 'Write timing is EXPLAIN ANALYZE of 2000 inserts followed by rollback; includes constraints/triggers and excludes durable COMMIT latency. Aborted inserts can leave dead tuples.', 'Logical backup captures the dump snapshot. Writes committed after that snapshot are not included; no WAL archiving/PITR. Restore time excludes operator detection and provisioning.', 'Research fixtures use SQL bulk insert; HTTP validation/authentication cost is measured separately by the public load tool.'] (ROOT/'evidence'/'research.json').write_text(json.dumps(results,ensure_ascii=False,indent=2)+'\n',encoding='utf-8') print('PASS: 100000 messages, two indexes/read-write measurements, backup restored to new database with matching content') print(json.dumps({'read_middle_ms':{name:value['pages']['40000']['median_ms'] for name,value in results['variants'].items()},'restore_seconds':results['backup_restore']['restore_seconds']},ensure_ascii=False)) finally: for name, service, admin in reversed(created): # Only drop names generated AND created by this invocation, never application DB. assert re.fullmatch(r'research_(restore_)?[a-f0-9]{12}',name) sql(admin,'DROP DATABASE '+name+' WITH (FORCE)', service) command(COMPOSE+['--profile','research','stop','restore']) if __name__=='__main__': main()