Repository navigation
fix: use DO NOTHING for chunk inserts, add 25P02 recovery - #1259
Conversation
recordChunkBatch used ON CONFLICT(hash) DO UPDATE SET updated_at which fails with a chunks_pkey violation when the PostgreSQL sequence is desynced: the INSERT hits the auto-increment PK conflict before the hash conflict clause can fire. After 5 retries the error propagates and leaves a connection in SQLSTATE 25P02 (in_failed_sql_transaction), causing a subsequent narinfo query to return HTTP 500. Fix 1 — recordChunkBatch: replace the upsert with DO NOTHING + a separate SELECT WHERE hash = ... to retrieve the chunk ID. Chunks are content-addressed immutable blobs; an existing row never needs updating. DO NOTHING skips the PK path entirely when the row pre-exists. Fix 2 — WithTransaction: add IsAbortedTransactionError (25P02) and issue a best-effort ROLLBACK via the raw pool after any transaction failure carrying that state, ensuring the connection is clean before it is returned for reuse. Three regression tests cover: new chunk, pre-existing chunk, and duplicate hash within the same batch.
|
Warning Review limit reached
Your plan includes 5 reviews of capacity. Refill in 6 minutes and 3 seconds. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more review capacity refills, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than trial, open-source, and free plans. In all cases, review capacity refills continuously over time. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
📝 WalkthroughWalkthroughChunk record insertion logic switches from upsert-with-touch to insert-or-ignore-then-query, PostgreSQL aborted-transaction errors are classified and trigger explicit connection cleanup, and chunk batch recording behavior is tested across new, existing, and duplicate-hash scenarios. ChangesCDC Chunking and Transaction Resilience
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@pkg/database/client.go`:
- Around line 150-152: The best-effort ROLLBACK call currently uses
context.Background() and can hang; when handling IsAbortedTransactionError(err)
replace the unbounded ExecContext call with one using a short
context.WithTimeout (e.g., few seconds), call c.sdb.ExecContext with that ctx,
and defer cancel() so the cleanup is bounded; update the branch around
IsAbortedTransactionError to create the timeout context, execute ROLLBACK with
that context, and ignore/handle the returned error as before.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6703dcf2-f3c3-4f4a-b43e-0c351fedc943
📒 Files selected for processing (4)
pkg/cache/cache.gopkg/cache/cache_internal_test.gopkg/database/client.gopkg/database/errors.go
Archive the completed change and sync the two new requirements into the canonical CDC chunking spec: - CDC chunk insert MUST use ON CONFLICT DO NOTHING (not upsert), followed by SELECT to retrieve the chunk ID for the link table. - Transaction failure MUST NOT leave PostgreSQL connections in the aborted state (25P02); an explicit ROLLBACK must be issued before returning the connection to the pool. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
|
This change is part of the following stack: Change managed by git-spice. |
There was a problem hiding this comment.
Code Review
This pull request modifies the chunk recording logic to use an ignore-on-conflict strategy followed by a separate query to retrieve IDs, which prevents primary key sequence desynchronization. It also adds a mechanism to handle aborted PostgreSQL transactions and introduces comprehensive tests for chunk batch recording across SQLite, PostgreSQL, and MySQL. Feedback identifies a performance regression in the chunk recording loop, suggesting that the per-chunk database round-trips be optimized into bulk operations. Additionally, the effectiveness of issuing a manual rollback on the connection pool was questioned, as it may not target the specific failed connection and could be better handled via driver-level settings.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1259 +/- ##
============================
============================
☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
UpsertOne(...).Update(...)withCreate().OnConflictColumns(hash).Ignore().Exec()so that aduplicate-hash conflict leaves the transaction alive instead of
aborting it with SQLSTATE 25P02.
nar_file_chunkslink —DO NOTHINGreturns no rows so anexplicit SELECT is required.
database.IsAbortedTransactionError(SQLSTATE 25P02 detector)and a best-effort bare
ROLLBACKinWithTransactionto recoverconnections that entered the aborted state before this fix was
deployed.
testRecordChunkBatch_*subtests.Why
A duplicate chunk hash caused the DB engine to abort the whole
transaction. Every subsequent statement on that connection failed
with "current transaction is aborted, commands ignored until end of
transaction block", which surfaced as HTTP 500 to Nix clients.
🤖 Generated with Claude Code