fix(retry-batch): handle nil COPY context in line-number retry path (#1333)#1745
Merged
Conversation
…1333) When retry-batch is processing a batch with a known COPY error line number and a subsequent sub-batch fails with an error that has NO COPY context (e.g. an FK violation following a format error), parse-copy-error-context returns nil. The code then did (+ current-batch-pos nil), signalling a type error that was caught as 'BUG: failed to retry-batch'. The rejected rows were lost because %process-bad-row was never called for them. The root crash (the double-increment in the old O(N) row-by-row loop for FK-only batches) was already fixed in #1424 by replacing that path with bisect-batch. This commit closes the remaining edge case: when the INITIAL error has a line number but a later sub-batch raises a context-free error, fall back to bisect-batch for the remaining rows instead of crashing. Also adds a regression test (test/fk-reject.load) that loads a CSV with an FK-violating row into a small batch, verifying that: - pgloader does not crash - the valid rows are committed to the target table - the invalid row is rejected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1333.
Background
When a batch COPY fails,
retry-batchtakes one of two paths:No COPY context (FK violations, deferred constraints):
parse-copy-error-contextreturnsnil→ previously used an O(N) row-by-row loop, now usesbisect-batch(fixed in Slow insert after foreign key error #1424 / batch retry: use binary search when COPY error has no line number #1734).COPY context with line number (format errors, type errors): uses the line-number-guided retry loop.
The Remaining Bug
In path 2, when a sub-batch retry itself fails with an error that has no COPY context (e.g. an FK violation on a column that also had a format error earlier in the same batch), the code did:
This type error was caught by the outer handler in
copy-rows-in-batch.lispas"BUG: failed to retry-batch: ...". The rejected rows were silently lost —%process-bad-rowwas never called for them.Fix
In
src/pg-copy/copy-retry-batch.lisp, whenparse-copy-error-contextreturnsnilfor the new sub-batch error, fall back tobisect-batchfor the remaining rows and exit the retry loop cleanly:Test Coverage
Added
test/fk-reject.load— a regression test that:(1,1)valid,(2,99)bad FK,(3,2)valid(1,1)and(3,2)end up in the target table (via the--regressexpected-output comparison), confirming that neither a crash nor silent data loss occurredNote: the core crash for pure-FK batches ("Invalid index N for SIMPLE-VECTOR N") was already fixed in #1734 by replacing the O(N) loop with
bisect-batch. This PR closes the remaining mixed-error-type edge case.