Skip to content

feat: Importing large objects from client side - #472

Merged
krlmlr merged 10 commits into
r-dbi:mainfrom
toppyy:import_large_object
Feb 24, 2025
Merged

krlmlr merged 10 commits into
r-dbi:mainfrom
toppyy:import_large_object

Conversation

@toppyy

@toppyy toppyy commented Oct 19, 2024

Copy link
Copy Markdown
Contributor

Closes #376. Introduces a postgres-specific function postgresImportLargeObject for importing large objects from client side using libpq's lo_import_with_oid

@krlmlr krlmlr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread R/PqConnection.R Outdated

if (is.null(filepath)) stopc("'filepath' cannot be NULL")
if (oid < 0) stopc("'oid' cannot be negative")
if (is.null(oid) | is.na(oid)) stopc("'oid' cannot be NULL/NA")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (is.null(oid) | is.na(oid)) stopc("'oid' cannot be NULL/NA")
if (is.null(oid) || is.na(oid)) stopc("'oid' cannot be NULL/NA")

Or perhaps this could be two checks?

Comment thread R/PqConnection.R Outdated
if (is.null(filepath)) stopc("'filepath' cannot be NULL")
if (oid < 0) stopc("'oid' cannot be negative")
if (is.null(oid) | is.na(oid)) stopc("'oid' cannot be NULL/NA")
if (file.access(filepath,4) == -1) stopc(paste0("Unable to read from filepath '",filepath,"'"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will the error look like if we let libpq do the check?

Please note that it is not a good idea to use this function to test before trying to open a file. On a multi-tasking system, it is possible that the accessibility of a file will change between the time you call file.access() and the time you try to open the file.

@toppyy toppyy Oct 19, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libpq returns an InvalidOid (a zero). Letting libpq do the check has the downside that we cannot inform the user of the specific problem as libpq's lo_import_with_oid() returns a zero if the 1) file does not exists or 2) the assigned oid is already in use.

So currently if the accessibility of the file has changed, an error is thrown with the message Import failed. Maybe you tried to write to an existing oid?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see:

If an error occurs while executing any one of these functions, the function will return an otherwise-impossible value, typically 0 or -1. A message describing the error is stored in the connection object and can be retrieved with PQerrorMessage() .

Would that work well enough?

Also: how do we auto-assign the OID?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I was unaware of PQerrorMessage() - that solves the problem! With the changes in the latest commit, when importing to an existing oid we get:

Error: ERROR: duplicate key value violates unique constraint "pg_largeobject_metadata_oid_index"
DETAIL: Key (oid)=(999) already exists.

(It's a bit clunky as the term "error" is repeated, but informative nonetheless.)

pqlib auto-assigns the oid if the argument 'oid' is zero. From the docs:

If lobjId is InvalidOid (zero) then lo_import_with_oid assigns an unused OID (this is the same behavior as lo_import).

I also changed the return- and argument types to 'Oid' from int as oid's are implemented as unsigned integers.

@krlmlr
krlmlr merged commit cea85e5 into r-dbi:main Feb 24, 2025
@krlmlr

krlmlr commented Feb 24, 2025

Copy link
Copy Markdown
Member

Thanks!

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Feb 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to write R object to Postgres large object

2 participants