Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Importing large objects from client side #472

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

toppyy
Copy link

@toppyy toppyy commented Oct 19, 2024

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

Copy link
Member

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

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

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
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?

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
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.

Copy link
Author

@toppyy toppyy Oct 19, 2024

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
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
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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