Skip to content

fix: finish database port --to postgres in minutes instead of hours over a network link - #201374

Merged
VACInc merged 1 commit into
openclaw:mainfrom
deepujain:fix/201249-database-port-to-postgres-transcript
Nov 18, 2026
Merged

VACInc merged 1 commit into
openclaw:mainfrom
deepujain:fix/201249-database-port-to-postgres-transcript

Conversation

@deepujain

Copy link
Copy Markdown
Contributor

Closes #201249

Prior work: #188397 (merged) made the port copy navigation rows exactly instead of rebuilding them row by row. It left the search rows, which this PR batches.

What Problem This Solves

Moving an agent store to PostgreSQL with openclaw database port --to postgres rebuilds the transcript search rows after the bulk copy, and it sent one INSERT per message. When the PostgreSQL server is not on the same network, each INSERT waits a full round trip, so the port time is set by latency, not by data size. The reporter measured about 21,000 search rows at roughly 92 ms per round trip, and the Gateway has to stay offline for the whole port.

User Impact

User impact: operators porting a large agent store to a remote PostgreSQL server wait for a few dozen round trips instead of one per message, so the Gateway comes back much sooner. The ported rows are the same, in the same order.

Why This Change Was Made

The transcript owner already has a batched PostgreSQL write that allocates identities in message order and pairs each identity with its own content. The port was bypassing it with a hand-written per-row statement. It now builds the same statement for a batch of up to 512 rows and runs it over the port's connection, so there is one allocation contract instead of two. To make that possible, the compile-only PostgreSQL Kysely facade in kysely-sync.ts is exported for asynchronous callers that run the compiled query themselves. Rows are collected from the source generator one batch at a time, so memory stays bounded.

This does not change the other cost the issue mentions, reading each session's events back from the target before projecting. That is left for a separate change.

Evidence

Before the change, a 1,100-message session produced 1,100 search-row statements in the new regression test. After it, the same session produces 3 (512, 512 and 76 rows), the message IDs reach the database in transcript order, and each statement stays well under PostgreSQL's 65,535-parameter limit. The test fails on the old code and passes on the new code. For the reporter's 21,000 rows that is about 41 statements instead of 21,000; at 92 ms each that is a few seconds of waiting by arithmetic, not a measurement.

I ran the exact compiled statement against a real PostgreSQL engine (PGlite) with a 1,100-row, three-batch run that included null message IDs, roles and timestamps. Row IDs came out 1 through 1,100 in input order, each row's text, message ID, role and timestamp matched its input, session_transcript_fts_rows agreed with session_transcript_fts, and the identity sequence ended at 1,100.

Commands and what was not run
  • New regression test src/infra/database-engine/agent-port-projections.test.ts, two tests, run with vitest 5.0.5: pass on the change, fail on the old agent-port-projections.ts (1,100 statements instead of 3). Wall time about 1 s including module import; this is not a pnpm test measurement because a full pnpm install was not possible in the environment.
  • PGlite check of the compiled statement, described above (throwaway test, not committed).
  • oxfmt --check on the four changed files, tsc on the changed files with no errors reported in them, and git diff --check pass.
  • Existing session-transcript-projection-rebuild and query-compiler tests pass. Four unrelated kysely-sync tests fail only because tsx is missing from the scratch dependency set.
  • Not run: the PostgreSQL conformance suite (scripts/test-database-conformance.mjs --engine postgres) against a live server, pnpm tsgo, pnpm lint (oxlint did not discover files in this environment), a port over a real WAN link, and CI.

…uery per row

`openclaw database port --to postgres` rebuilt the transcript search rows with
one awaited INSERT per message. Over a link with real latency that is one round
trip per row, so a store with tens of thousands of messages kept the Gateway
offline for hours.

The port now sends up to 512 rows per statement, reusing the transcript owner's
identity allocation so rows keep their message order and identities stay paired
with their content. The compile-only PostgreSQL Kysely facade is exported so the
asynchronous port connection can run the owner's query.

Closes openclaw#201249
Related: openclaw#188397

Signed-off-by: Deepak Jain <[email protected]>
@clawsweeper

clawsweeper Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@deepujain
deepujain marked this pull request as ready for review November 18, 2026 18:31
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 18, 2026
@clawsweeper

clawsweeper Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

Batch transcript search writes during PostgreSQL ports using the transcript owner's existing identity-allocation query.

Example: Port one session containing 1,100 searchable messages to PostgreSQL.

  • Before: The search rebuild awaits 1,100 write statements.
  • After: The search rebuild awaits three write statements containing 512, 512, and 76 rows.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The implementation is focused and source-consistent, but supplied validation does not establish a completed port or preservation of existing-store search projections in session_transcript_fts_rows and session_transcript_fts; latest-stable upgrade-and-port evidence remains contributor work.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🦐 gold shrimp (3/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Operators keep the Gateway offline while remote PostgreSQL ports wait for one network round trip per searchable message.
Reason: The change removes a demonstrated source of unnecessary round trips without adding configuration or changing the intended search result contract.

Merge readiness

⛔ Blocked before merge - 2 items remain

This PR addresses a current PostgreSQL port performance defect with a focused change that preserves the existing identity-allocation contract; no concrete correctness defect was found.

Priority: P2
Reviewed head: b5eabb574d1dfd7d7677a7a5e0982b926b2e9487

Before merge

  • Add real behavior proof - The contributor reports a recording-client regression and direct execution of compiled SQL in PGlite, but supplies no terminal trace, logs, or artifact exercising the changed port owner through the shipped database-port command; redact IPs, API keys, phone numbers, and non-public endpoints from contributed evidence. After adding proof, update the PR body, then request a new review with @clawsweeper re-review. The PR author or someone with repository write access can request it.
  • Add data-model compatibility proof - The review found that existing stored data may not work after upgrade. Show that existing data still loads and works with this change.

Findings

None.

Tests

  • Low-value test src/infra/database-engine/agent-port-projections.test.ts: Remove the SQL-spelling assertions for nextval and order by at lines 67–68; they cannot establish persisted identity/content pairing or row-ID order, whereas the surrounding query-budget regression protects the actual performance defect.
  • Missing end-to-end proof: Provide redacted terminal output or logs of a real multi-batch database port to PostgreSQL, including resulting search content, row-ID order and sequence advancement; the body reports the mock regression failing on base and passing on head.
Agent review details

How this fits together

The offline database-port worker copies canonical agent data, rebuilds derived transcript search rows on the destination connection, verifies the destination, and commits the port.

flowchart TD
  A[Database port CLI] --> B[Port worker transaction]
  B --> C[Copy canonical agent tables]
  C --> D[Prepare transcript projections]
  D --> E[Shared FTS allocation query]
  E --> F[PostgreSQL search tables]
  F --> G[Verify and commit]
Loading

Technical review

Best possible solution:

Retain bounded PostgreSQL batching through the shared transcript allocation owner, with the existing port transaction and verification boundaries.

Do we have a high-confidence way to reproduce the issue?

Current-main source confirms one awaited write per FTS row, matching the official-release report; this read-only review did not execute a port.

Is this the best way to solve the issue?

Reusing the established allocation query is the smallest complete fix for serial FTS writes; eliminating transcript readback is a separate optimization.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 3da0bc802e10.

Provenance checked

  • PostgreSQL agent-port search rebuild keeps the original intent (835dc1e: The original agent-family port rebuilt derived search rows while preserving canonical data, identities, and transaction verification.)
  • Shared PostgreSQL FTS allocation keeps the original intent (fix: batch PostgreSQL transcript search rows with repeated IDs #175431: Allocate distinct identities and pair them by input position so repeated or nullable message IDs cannot corrupt mapping or chronological order.)
  • Reusable projection allocation builder keeps the original intent (03834a7: Share FTS allocation while combining bounded projection writes into fewer PostgreSQL trips.)

Testing

Proof path: unit tests only.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 03834a7 adds src/config/sessions/session-transcript-fts.ts:40 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 03834a76775d; files: src/config/sessions/session-transcript-fts.ts)

PR surface

Source +57, Tests +89. Total +146 across 4 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 3 68 11 +57
Tests 1 89 0 +89
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 157 11 +146

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

  • add P2: Serial network round trips prolong planned downtime during an advanced, operator-initiated database migration.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Label justifications:

  • P2: Serial network round trips prolong planned downtime during an advanced, operator-initiated database migration.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 18, 2026, 1:47 PM ET / 18:47 UTC.

@VACInc VACInc self-assigned this Nov 18, 2026
@VACInc
VACInc merged commit bc4e269 into openclaw:main Nov 18, 2026
152 of 155 checks passed
@VACInc

VACInc commented Nov 18, 2026

Copy link
Copy Markdown
Member

Merged via explicitly authorized admin squash with attributed pre-existing CI failures.

@VACInc

VACInc commented Nov 18, 2026

Copy link
Copy Markdown
Member

Inherited CI failure named for the record (landed at b5eabb574d1dfd7d7677a7a5e0982b926b2e9487):

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 19, 2026
…uery per row (openclaw#201374)

`openclaw database port --to postgres` rebuilt the transcript search rows with
one awaited INSERT per message. Over a link with real latency that is one round
trip per row, so a store with tens of thousands of messages kept the Gateway
offline for hours.

The port now sends up to 512 rows per statement, reusing the transcript owner's
identity allocation so rows keep their message order and identities stay paired
with their content. The compile-only PostgreSQL Kysely facade is exported so the
asynchronous port connection can run the owner's query.

Closes openclaw#201249
Related: openclaw#188397

Signed-off-by: Deepak Jain <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 19, 2026
…uery per row (#201374)

`openclaw database port --to postgres` rebuilt the transcript search rows with
one awaited INSERT per message. Over a link with real latency that is one round
trip per row, so a store with tens of thousands of messages kept the Gateway
offline for hours.

The port now sends up to 512 rows per statement, reusing the transcript owner's
identity allocation so rows keep their message order and identities stay paired
with their content. The compile-only PostgreSQL Kysely facade is exported so the
asynchronous port connection can run the owner's query.

Closes #201249
Related: #188397

Signed-off-by: Deepak Jain <[email protected]>

(cherry picked from commit bc4e269)

Co-authored-by: RomneyDa <[email protected]>
github-actions Bot pushed a commit to clawhaven/openclaw that referenced this pull request Nov 23, 2026
…uery per row (openclaw#201374)

`openclaw database port --to postgres` rebuilt the transcript search rows with
one awaited INSERT per message. Over a link with real latency that is one round
trip per row, so a store with tens of thousands of messages kept the Gateway
offline for hours.

The port now sends up to 512 rows per statement, reusing the transcript owner's
identity allocation so rows keep their message order and identities stay paired
with their content. The compile-only PostgreSQL Kysely facade is exported so the
asynchronous port connection can run the owner's query.

Closes openclaw#201249
Related: openclaw#188397

Signed-off-by: Deepak Jain <[email protected]>

(cherry picked from commit bc4e269)

Release adaptation: Preserve stable activeRows loop and metadata INSERT; replace only PG FTS loop and share allocator. Add retention assertions; runtime proof blocked.

Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: database port --to postgres: transcript projection rebuild sends one INSERT per row, so ports over WAN take hours

2 participants