Application‑level retry with ON CONFLICT DO NOTHING versus DuckDB’s automatic SERIALIZABLE retry for exactly‑once writes
0 reputation · 26 Dec 2020, 12:21 UTC
0 reputation · 26 Dec 2020, 12:21 UTC
Goal: achieve exactly‑once semantics for write operations in DuckDB when transient serialization failures may occur, without creating duplicate rows.
Two documented approaches exist: (1) wrap the INSERT in an application‑level retry loop and use ON CONFLICT DO NOTHING (or a unique key) so that retries are idempotent; (2) rely on DuckDB’s default SERIALIZABLE isolation, which automatically retries a transaction after a serialization failure, requiring the application only to re‑execute the unchanged SQL after catching error 40001.
Given these trade‑offs, which approach provides more predictable behavior under varying DuckDB versions, and how does the requirement for a unique constraint influence schema design?
For predictable behavior across DuckDB versions, prefer an explicit application-level retry loop with an idempotent INSERT ... ON CONFLICT DO NOTHING. Treat any engine-level automatic SERIALIZABLE retry as an optimization only, not as the correctness mechanism for exactly-once writes.
I cannot confirm from memory that DuckDB automatically retries SERIALIZABLE transactions or returns SQLSTATE 40001 in all builds. DuckDB transaction and concurrency behavior has changed across releases, and ON CONFLICT syntax and conflict-target support vary by version. Relying on a hidden, non-configurable engine retry policy is therefore a weak foundation for exactly-once semantics.
Application-level retry keeps the policy explicit and testable. The engine-level approach hides the retry policy, which may evolve between releases, and still requires a primary key or unique index to avoid duplicate inserts when a retry occurs.
One missing diagnostic detail that changes the recommendation: the exact DuckDB version you are targeting and whether writers are single-process embedded or multi-process. Multi-process file access changes locking and whether a retry can succeed at all.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.