DataGrip retry strategies: ON CONFLICT upsert vs transaction rollback for idempotent writes
0 reputation · 17 Jan 2024, 15:18 UTC
0 reputation · 17 Jan 2024, 15:18 UTC
Determine the most reliable approach in DataGrip to retry failed write operations without creating duplicate records when the initial attempt partially succeeded or raised a constraint violation.
The solution must work across common databases supported by DataGrip (PostgreSQL, MySQL, SQL Server) and integrate with DataGrip's built-in 'Run' configuration 'Attempts' field for automatic retries.
While both INSERT ... ON CONFLICT and transaction rollback with explicit cleanup can prevent duplicates, it is unclear which pattern DataGrip's retry mechanism handles more safely when a write fails after committing some statements within a multi-statement script.
35275 reputation · 17 Jan 2024, 16:33 UTC
ON CONFLICT outcomes appear as row-count pairs. When a conflict triggers an UPDATE, the Result pane shows Rows updated: N and Rows inserted: 0. A clean insert shows Rows inserted: 1 and Rows updated: 0.The "Attempts" field in a Run Configuration controls whole-script re-execution. If you set Attempts = 3 and the script fails on attempt 1, DataGrip rolls back that transaction, opens a new one, and runs the script again. This means:
BEGIN/COMMIT, DataGrip treats them as one transaction. Adding explicit transaction blocks changes this behavior and may interfere with the retry rollback.| Approach | PostgreSQL | MySQL | SQL Server | DataGrip Visibility |
|---|---|---|---|---|
ON CONFLICT / ON DUPLICATE KEY / MERGE | INSERT ... ON CONFLICT DO UPDATE | INSERT ... ON DUPLICATE KEY UPDATE | MERGE or trigger-based | Result grid shows insert/update counts per attempt |
| Transaction rollback + cleanup | Manual DELETE in catch block | Same | Same | Only final commit visible; intermediate rolls hidden |
Upsert syntax is preferred because:
Attempts = 3.Rows inserted: 1 on attempt 1, then Rows updated: 1 / Rows inserted: 0 on retries.Does your script use explicit BEGIN/COMMIT blocks or rely on DataGrip's implicit transaction handling? Explicit blocks can prevent the automatic rollback between retries and change the idempotency guarantees.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.