Skip to content

dbAppendTableArrow() and dbWriteTableArrow() are not atomic across batches #650

Not planned

Description

@krlmlr

When a later batch of a multi-batch stream fails, dbAppendTableArrow() keeps the rows of the earlier batches, and dbWriteTableArrow() also leaves the newly created table behind. dbAppendTable() with the same rows in one data frame inserts nothing, because it runs a single COPY.

Location: DBI default methods. dbAppendTableArrow() calls dbAppendTable() once per batch without a transaction, and dbWriteTableArrow() creates the table before appending. Related, for dbWriteTable(): #307.

Reprex against the CRAN release (the output on main at 41cd4be is the same):

Sys.setenv(PGHOST = "localhost", PGPORT = "5432", PGUSER = "runner", PGPASSWORD = "test123", PGDATABASE = "testdb")
library(DBI)
con <- dbConnect(RPostgres::Postgres())
dbExecute(con, "CREATE TABLE t_pk (x int PRIMARY KEY)")
#> [1] 0
s <- nanoarrow::basic_array_stream(list(
  nanoarrow::as_nanoarrow_array(data.frame(x = 1:2)),
  nanoarrow::as_nanoarrow_array(data.frame(x = 2:3))
))
try(dbAppendTableArrow(con, "t_pk", s))
#> Error : COPY returned error : ERROR:  duplicate key value violates unique constraint "t_pk_pkey"
#> DETAIL:  Key (x)=(2) already exists.
#> CONTEXT:  COPY t_pk, line 1
dbGetQuery(con, "SELECT * FROM t_pk")
#>   x
#> 1 1
#> 2 2
# Expected: no rows, as with the data frame path:
dbExecute(con, "TRUNCATE t_pk")
#> [1] 0
try(dbAppendTable(con, "t_pk", data.frame(x = c(1:2, 2:3))))
#> Error : COPY returned error : ERROR:  duplicate key value violates unique constraint "t_pk_pkey"
#> DETAIL:  Key (x)=(2) already exists.
#> CONTEXT:  COPY t_pk, line 3
dbGetQuery(con, "SELECT * FROM t_pk")
#> [1] x
#> <0 rows> (or 0-length row.names)
dbRemoveTable(con, "t_pk")

# dbWriteTableArrow() leaves the new table and the first batch behind when a later batch fails
s <- nanoarrow::basic_array_stream(list(
  nanoarrow::as_nanoarrow_array(arrow::Array$create(data.frame(x = as.difftime(5, units = "secs")))),
  nanoarrow::as_nanoarrow_array(arrow::Array$create(data.frame(x = as.difftime(100000, units = "secs"))))
))
try(dbWriteTableArrow(con, "t_new", s))
#> Error : COPY returned error : ERROR:  date/time field value out of range: "27:46:40"
#> CONTEXT:  COPY t_new, line 1, column x: "27:46:40"
dbExistsTable(con, "t_new")
#> [1] TRUE
dbGetQuery(con, "SELECT x::text FROM t_new")
#>          x
#> 1 00:00:05
# Expected: FALSE, no table
dbRemoveTable(con, "t_new", fail_if_missing = FALSE)

dbGetQuery(con, "SHOW server_version")
#>                          server_version
#> 1 16.15 (Ubuntu 16.15-0ubuntu0.24.04.1)
dbDisconnect(con)
as.data.frame(sessioninfo::package_info(c("RPostgres", "DBI", "nanoarrow", "arrow"), dependencies = FALSE))[c("package", "ondiskversion", "source")]
#>             package ondiskversion         source
#> arrow         arrow      26.0.1.1           RSPM
#> DBI             DBI         1.3.0           RSPM
#> nanoarrow nanoarrow         0.9.0           RSPM
#> RPostgres RPostgres        1.4.10 RSPM (R 4.5.0)

Created on 2026-11-29 with reprex v2.1.1

  • fix append (one COPY or one transaction per call)
  • fix write (create and append in one transaction, which PostgreSQL allows)
  • test
  • report upstream (DBI default methods)

Severity: medium (proposed, Kirill may change).

Native Arrow support is coordinated in #631 and is expected to fix this by construction if the native append streams all batches through one COPY (or runs in one transaction) and the native write wraps create and append in a transaction.

Activity

  1. added
    status: open
    on Nov 29, 2026
  2. krlmlr commented on Nov 29, 2026

    @krlmlr
    MemberAuthor

    Closed to keep the issue tracker for issues reported by others; the work here is still open and tracked with the status: open label.


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugstatus: open

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions