Skip to content

dbWriteTable() writes list columns that are not lists of raw as garbage bytea #648

Not planned

Description

@krlmlr

A list column whose elements are not raw vectors (for example integer or character vectors) is written as a bytea column with meaningless content, without an error or warning. Through Arrow the same happens: a list<int32> column arrives as a list_of<integer> in dbAppendTableArrow() and is written the same way.

Location: RPostgres, get_data_type() maps any list to BYTEA, and sql_data_copy() encodes each element with paste0("\\x", paste(format(x), collapse = "")), which only makes sense for raw vectors. Binding a list parameter errors cleanly ("Lists must contain raw vectors or NULL"), so only the COPY path is affected.

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())
df <- data.frame(id = 1:3)
df$x <- list(1:2, NULL, c("a", "b"))
dbWriteTable(con, "t_list", df, overwrite = TRUE)
dbGetQuery(con, "SELECT pg_typeof(x)::text AS type, x::text FROM t_list")
#>    type     x
#> 1 bytea \\x12
#> 2 bytea  <NA>
#> 3 bytea \\xab
# Expected: an error, or an array column with {1,2}, NULL, {a,b}
dbRemoveTable(con, "t_list")

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"), dependencies = FALSE))[c("package", "ondiskversion", "source")]
#>             package ondiskversion         source
#> DBI             DBI         1.3.0           RSPM
#> RPostgres RPostgres        1.4.10 RSPM (R 4.5.0)

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

  • decide: error for lists that are not lists of raw, or map them to PostgreSQL arrays
  • fix
  • test

Severity: high (proposed, Kirill may change). Silent data corruption.

Native Arrow support is coordinated in #631; it is not expected to fix this by construction. The data frame path stays as it is, and the native create has to decide what Arrow list types map to (PostgreSQL arrays, or an error).

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