Can DBI::dbWriteTable be safely retried after a transient database error?
0 reputation · 14 Nov 2022, 11:03 UTC
Goal
Determine whether an R DBI write that fails mid-transaction can be retried without creating duplicate rows, assuming DBI 1.x and a current driver such as RSQLite, RPostgres, or RMariaDB.
Constraint
DBI::dbWriteTable supports dbBegin, dbCommit, and dbRollback, but DBI defines no exactly-once or idempotent-retry contract across drivers. A transient network failure after the server commits can leave the client uncertain whether the write succeeded. Repeating an insert without a unique constraint or upsert can duplicate rows. Driver differences also matter: MySQL/MariaDB DDL may implicitly commit, while SQLite and PostgreSQL handle DDL transactionally.
Because DBI offers no standard way to classify which write errors are safe to retry, the retry-versus-deduplicate-versus-fail decision remains unresolved for package authors and callers.
Which DBI error conditions, if any, are safe to retry without caller-managed deduplication? Does a successful dbCommit guarantee that a subsequent retry is unnecessary, or can the client still be uncertain after losing the server response? What driver-specific state checks are required before deciding to retry a failed dbWriteTable?