Convert the snapshots in batches, not one round trip per row

The backfill issued an UPDATE per action. It runs at container start, before
uvicorn binds the port, against a database at the other end of a network --
944 rows on production, so a thousand sequential round trips standing between
the deploy and its first health check.

One executemany per batch instead. Same rows, same verification, same
transaction; nineteen round trips rather than nine hundred.

Re-verified on Postgres, since executemany binds bytea through a different
psycopg path: 720,864 B of JSON to 204,293 B, every row equal.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Dvvqn9ZDR4ixeFPHNbww7
This commit is contained in:
parththakkar106
2026-08-17 14:48:44 +05:30
co-authored by Claude Opus 5
parent 4f067516de
commit 0eef367f30
+9 -1
View File
@@ -319,6 +319,7 @@ def _backfill_context_snapshot(conn) -> None:
).all()
if not rows:
return
params = []
for row_id, stored in rows:
value = json.loads(stored) if isinstance(stored, str) else stored
if value is None:
@@ -330,9 +331,16 @@ def _backfill_context_snapshot(conn) -> None:
"compress/decompress round trip; refusing to drop the "
"original column"
)
params.append({"z": packed, "id": row_id})
if params:
# One executemany per batch, not one statement per row. This runs
# at container start, before the port opens, against a database on
# the other end of a network: a thousand round trips is the
# difference between a deploy that comes up and a health check that
# times out waiting for it.
conn.execute(
text("UPDATE actions SET context_snapshot_z = :z WHERE id = :id"),
{"z": packed, "id": row_id},
params,
)
last_id = rows[-1][0]