Skip to content

Fix BulkInsert composite PK handling for partitioned tables#4222

Merged
jeremydmiller merged 1 commit intomasterfrom
fix/4219-bulk-insert-composite-pk
Mar 31, 2026
Merged

Fix BulkInsert composite PK handling for partitioned tables#4222
jeremydmiller merged 1 commit intomasterfrom
fix/4219-bulk-insert-composite-pk

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

Summary

  • Fix BulkInsertAsync with IgnoreDuplicates and OverwriteExisting modes to use the full composite primary key when joining against partitioned tables
  • Previously, BulkLoaderBuilder hardcoded id (+ tenant_id for conjoined) in join expressions, ignoring partition columns that are part of the composite PK
  • Now builds join expressions from table.PrimaryKeyColumns, matching how the generated mt_upsert_* functions already work

Closes #4219

Test plan

  • 2 new regression tests for range-partitioned tables with composite PK (id, date)
  • 32/32 existing bulk insert tests pass (zero regressions)
  • 31/31 partitioning tests pass (zero regressions)

🤖 Generated with Claude Code

BulkLoaderBuilder.CopyNewDocumentsFromTempTable() and
OverwriteDuplicatesFromTempTable() hardcoded 'id' (+ tenant_id) in
their join expressions. For partitioned tables where partition columns
are part of the composite PK (e.g., (id, date)), this caused:
- IgnoreDuplicates: rows with same id but different partition key
  treated as duplicates and skipped
- OverwriteExisting: ambiguous UPDATE matches followed by duplicate
  key violations on the INSERT

Fix: build join expressions from table.PrimaryKeyColumns instead of
hardcoding column names. Also exclude all PK columns from the UPDATE
SET clause (not just 'id').

Closes #4219

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jeremydmiller jeremydmiller merged commit 2c40e54 into master Mar 31, 2026
5 of 6 checks passed
@jeremydmiller jeremydmiller deleted the fix/4219-bulk-insert-composite-pk branch March 31, 2026 21:18
@Gafs63
Copy link
Copy Markdown

Gafs63 commented Apr 1, 2026

Hello,

Thank you so much for the quick fix! We really appreciate your responsiveness.

We're currently on Marten 7.29.0 and upgrading to 8.x would require significant work on our side (dependency updates, testing, etc.).
Would it be possible to backport this fix to a 7.x release?

Thanks again for building and maintaining Marten !

@jeremydmiller
Copy link
Copy Markdown
Member Author

@Gafs63 That's a big ask. Typically we don't do that for old releases outside of JasperFx support customers. I'd take the pull request to 7.* if you want to try to retrofit that yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BulkInsert ignores composite primary key on partitioned tables (only uses id column for ON CONFLICT)

2 participants