Skip to content

fix(database): ported PostgreSQL stores start without planner statistics - #183392

Closed
goutamadwant wants to merge 1 commit into
openclaw:mainfrom
goutamadwant:fix/postgres-port-planner-statistics
Closed

goutamadwant wants to merge 1 commit into
openclaw:mainfrom
goutamadwant:fix/postgres-port-planner-statistics

Conversation

@goutamadwant

Copy link
Copy Markdown
Contributor

Closes #183307

What Problem This Solves

Fixes: PostgreSQL agent stores returned by database port lack planner statistics after bulk materialization and the subsequent memory rebuild.

User Impact

User impact: first queries against a newly ported PostgreSQL agent store can plan from current table statistics instead of PostgreSQL's default estimates.

Why This Change Was Made

The port now runs a namespace-scoped ANALYZE after materialization while port custody is still current. The post-port memory rebuild refreshes the same admitted namespace after closing its writer, and reports a sanitized warning if that secondary refresh fails without misreporting the completed rebuild as failed.

Evidence

Before the fix, the focused PostgreSQL regression observed pg_stat_user_tables.last_analyze IS NULL for a populated session_nodes table when the port returned. With the fix, the same real PostgreSQL path observes last_analyze IS NOT NULL.

Validated on current main:

node scripts/test-database-conformance.mjs --engine postgres --connection "$TEST_POSTGRES_CONNECTION" -- src/infra/database-engine/agent-port.postgres.test.ts src/infra/database-engine/port-command.postgres.test.ts
pnpm test src/commands/doctor-database-port-memory.test.ts src/infra/database-engine/postgres-planner-statistics.test.ts
pnpm tsgo:core
pnpm format:check
node scripts/check-line-cap-ratchet.mjs
node --import ./scripts/tsx.mjs scripts/check-assertion-safety-ratchet.mts
node --import ./scripts/tsx.mjs scripts/check-test-mock-exports.mts
git diff --check

Results: 15 PostgreSQL conformance tests passed, 7 focused unit tests passed, and the typecheck, formatting, ratchets, and diff check passed. The full repository lint still reports pre-existing max-lines violations in unrelated files; the changed-file line-cap ratchet passes.

Disclosure: AI was used to understand the codebase and review the fix.

@clawsweeper

clawsweeper Bot commented Oct 25, 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.

@openclaw-barnacle openclaw-barnacle Bot added commands Command implementations size: M labels Oct 25, 2026
@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 Oct 25, 2026
@clawsweeper

clawsweeper Bot commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

This PR refreshes PostgreSQL planner statistics after database port materialization and the subsequent agent memory rebuild.

Example: Port agent pop with 10,200 memory chunks to PostgreSQL.

  • Before: The rebuilt memory tables can lack statistics until autovacuum runs.
  • After: The command refreshes the admitted namespace after rebuilding memory, or reports a sanitized warning.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The focused implementation has no identified actionable defect, but test-only evidence does not satisfy the external PR's real-behavior proof gate.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Queries immediately after a PostgreSQL port can use default estimates for freshly copied or rebuilt tables.
Reason: A bounded refresh at each bulk-load completion addresses the documented gap without changing steady-state autovacuum policy or adding configuration.

Merge readiness

⛔ Blocked before merge - 1 item remains

Keep open: current main still lacks both statistics refreshes, and this PR includes post-rebuild work absent from the overlapping PR.

Priority: P2
Reviewed head: 0141051718f0c04f7b002ff06fd52a1cce6b4faf

Before merge

  • Add real behavior proof - The supplied evidence reports real PostgreSQL materializer tests and mocked post-rebuild refresh tests, but does not exercise this head through the shipped database port CLI after a populated memory rebuild; provide terminal output or redacted logs with immediate catalog results, excluding credentials and non-public endpoints. The verified diff changes no application schema, persisted format, or stored configuration, so no data migration is required. 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.

Findings

None.

Tests

  • Missing end-to-end proof: Provide a post-fix CLI replay against disposable PostgreSQL showing statistics for copied tables and populated memory tables immediately after command return.
Agent review details

How this fits together

The offline database port copies and verifies stores before publication, then rebuilds agent memory under fresh custody; this change refreshes destination statistics at both completion boundaries.

flowchart TD
  A[Database port CLI] --> B[Offline port owner]
  B --> C[Copy data and rebuild projections]
  C --> D[Analyze destination namespace]
  D --> E[Verify and publish]
  E --> F[Fresh memory owner]
  F --> G[Rebuild memory and close writer]
  G --> H[Refresh statistics and report result]
Loading

Technical review

Best possible solution:

Refresh statistics through the existing bulk-copy and post-rebuild owners, preserving custody, canonical data verification, and nonfatal secondary-refresh reporting.

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

Current-main source confirms both missing refresh boundaries, consistent with the linked report's catalog observations; no target code was executed during this read-only review.

Is this the best way to solve the issue?

The patch addresses both write phases using the existing owners, shares namespace-scoped SQL across the asynchronous and synchronous transports, and preserves the prior publication and failure contracts.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Offline materialization and verification keeps the original intent (cd32ddc: The original port foundations require exclusive custody, unpublished destinations, canonical-byte preservation, and verification before publication.)
  • Post-port memory ownership and result reporting keeps the original intent (215cb73: Memory rebuilding runs after publication under fresh configured custody, and failures must not invalidate an already completed port.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 215cb73 adds src/commands/doctor-database-port-memory.ts:150 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 215cb73bea18; files: src/commands/doctor-database-port-memory.ts)

PR surface

Source +99, Tests +117. Total +216 across 7 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 4 99 0 +99
Tests 3 122 5 +117
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 221 5 +216

Root-cause cluster

Relationship: same_root_cause
Canonical: #183307
Summary: The canonical report covers missing statistics after both materialization and memory rebuilding; the other open PR covers only materialization.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Labels

Label changes:

  • add P2: Missing statistics temporarily degrade planner estimates after an offline port; the report establishes neither data loss nor measured user-visible latency regression.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Label justifications:

  • P2: Missing statistics temporarily degrade planner estimates after an offline port; the report establishes neither data loss nor measured user-visible latency regression.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Attach a shipped CLI replay demonstrating statistics after both materialization and the populated memory rebuild.

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 October 25, 2026, 3:40 AM ET / 07:40 UTC.

steipete added a commit that referenced this pull request Oct 25, 2026
openclaw database port --to postgres and backup restore into PostgreSQL handed back schemas without planner statistics, so first queries planned on default estimates. One shared helper now runs ANALYZE scoped to the destination namespace after the verified commit (and again after the memory rebuild); timeouts or errors are warnings and never undo verified data. Live PostgreSQL proof showed fresh statistics after port.

Fixes #183307. Thanks @etzelm for the report and @lonexreb (#183327) and @goutamadwant (#183392) for the earlier fixes.

Co-authored-by: lonexreb <[email protected]>
Co-authored-by: goutamadwant <[email protected]>
@steipete

Copy link
Copy Markdown
Contributor

Thank you, @goutamadwant! This landed as #183447 (660b252), which runs a namespace-scoped ANALYZE after the verified port commit and again after the memory rebuild, with failures kept as warnings. It also covers backup restore into PostgreSQL through the same helper. You're credited as a co-author on the merge commit. Closing this one as superseded.

@steipete steipete closed this Oct 25, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 26, 2026
…aw#183447)

openclaw database port --to postgres and backup restore into PostgreSQL handed back schemas without planner statistics, so first queries planned on default estimates. One shared helper now runs ANALYZE scoped to the destination namespace after the verified commit (and again after the memory rebuild); timeouts or errors are warnings and never undo verified data. Live PostgreSQL proof showed fresh statistics after port.

Fixes openclaw#183307. Thanks @etzelm for the report and @lonexreb (openclaw#183327) and @goutamadwant (openclaw#183392) for the earlier fixes.

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

Labels

commands Command implementations P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M 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 hands back PostgreSQL schemas with no planner statistics (no ANALYZE), so first queries plan on default estimates

2 participants