d69873297a
save_file_with_blob_impl did three sequential DB round-trips per upload: resolve_user_id (SELECT folders.user_id), the INSERT, then lookup_folder_path (SELECT folders.path) — the first and third re-reading the same folders row. Replace them with a single statement: a `parent` CTE reads the folder once and the INSERT derives user_id from it and returns the path via the CTE, in one round-trip. An empty CTE (folder vanished between ingest and insert) inserts zero rows and now surfaces as a clean NotFound instead of a generic owner error. Deadlock retry, blob-ref compensation, and the 23505 (duplicate name) mapping are preserved; owner resolution + insert are now atomic (no TOCTOU). What it does and doesn't buy (benchmarked, honest): - Server CPU: UNCHANGED. A server-side 50k loop is identical (13.6s vs 13.6s) — the two extra folder reads are cached point lookups, negligible against the INSERT + per-statement triggers + 13 indexes. - Client-observed latency: 2 fewer client<->DB round-trips per upload. At the measured ~189us/round-trip on localhost that's ~0.38ms/upload; on a networked DB (RTT 0.5-1ms) ~1-2ms/upload. - Connection pool: the metadata phase holds a pooled connection for 1 round-trip instead of 3, freeing it ~3x sooner under upload concurrency. So this is a latency + connection-utilization win (and an atomicity/cleanup), not a server-CPU win. register_file_deferred keeps its own path. https://claude.ai/code/session_01DCszkkU11LYxMEUWr4setK