Repository navigation
Closed
Description
It is really easy to write large files to Postgres using the function postgresImportLargeObject() (thanks to #472).
However, when the file is larger (eg 1GB+) it is hard to export the file again as one would have to use a query (and afaik fields are limited to 1GB), in fact I'm still trying to figure out how to get the file back out...
It would be great to have a postgresExportLargeObject function or similar which can download the file again.
Eg a simple example code is this (assumes a valid con):
file <- "mytestfile.bin"
# generate a file with 800MB filesize using bash
# system(sprintf("dd if=/dev/urandom of=%s bs=1M count=800", file))
prettyunits::pretty_bytes(file.size(file))
#> [1] "838.86 MB"
# 1. Create Table =====
DBI::dbExecute(
con,
"CREATE TABLE IF NOT EXISTS lo_tester (
id SERIAL PRIMARY KEY,
name TEXT,
lo_link OID
)"
)
"lo_tester" %in% DBI::dbListTables(con)
#> [1] TRUE
# 2. Insert Large Object =====
t0 <- Sys.time()
DBI::dbWithTransaction(con, {
oid <- RPostgres::postgresImportLargeObject(con, file)
q <- sprintf(
"INSERT INTO lo_tester (name, lo_link) VALUES ('%s', %d);",
file,
oid
)
DBI::dbExecute(con, q)
})
cat(sprintf("Inserting took %0.2fs\n", difftime(Sys.time(), t0, units = "secs")))
#> Inserting took 144.86s
# inserted as oid
oid
#> [1] 557400
DBI::dbGetQuery(con, "SELECT * FROM lo_tester;")
#> id name lo_link
#> 1 1 mytestfile.bin 557400
# 3. Fetch the data again ====
r <- DBI::dbGetQuery(con, "SELECT lo_get($1) as lo_data", params = list(oid))
#> Error: Failed to fetch row : ERROR: invalid memory alloc request size 1677721603Here postgresExportLargeObject could look like this DBI::postgresExportLargeObject(con, oid, "mytestfile_out.bin").
Is this something that you would consider useful in the package?
Reacted by Copilot
Metadata
Metadata
Assignees
Labels
No labels
Activity
krlmlr commentedon Sep 10, 2025
Thanks. Can you use the
offsetandlengtharguments tolo_get()for chunking the data and reassembling client-side? If you're writing to a file, fetching rowwise withdbFetch(n = 1)will also reduce memory pressure.I'm not sure if
lo_get()exists elsewhere; we could discuss aRPostgres::postgresExportLargeObject()function based on an implementation sketch that works for you.DavZim commentedon Sep 10, 2025
As I am looking at a single object at a time,
n = 1doesnt help too much.For the moment I have a function like this, which allows me to download a LO based on its oid
This works, but I am not sure if this is robust or fast enough for RPostgres or if there are better low-level alternatives to fetch and store the binary data.
krlmlr commentedon Sep 13, 2025
Thanks. This is similar to what I had in mind. (I'm astonished by how difficult it is to get the size of such a large object.)
I thought we could create a query that would return the equivalent of
and then use
dbFetch(n = 1). But I'm not sure if the firstdbFetch()will not proactively fetch all the data, defeating the purpose of the chunking.It really looks like the readily available
lo_export()is doing the right thing: https://github.com/postgres/postgres/blob/f6edf403a99923b98f8b8b3398c7ef32e1ae9a3e/src/interfaces/libpq/fe-lobj.c#L748 . Let me take a closer look.DavZim commentedon Sep 13, 2025
As far as I remember, lo_export writes to file on the server and not on the client, right?
krlmlr commentedon Sep 13, 2025
It's part of libpq, and we're already using
lo_import(). I expect this to be symmetrical.krlmlr commentedon Sep 13, 2025
The PR #520 might be worth a try.
DavZim commentedon Sep 15, 2025
Wow, that was quick and a very interesting (positive) use-case of AI.
The code works for me and does what I would expect it to.
Merge tag 'v1.4.9'
github-actions commentedon Sep 22, 2026
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.