-
Notifications
You must be signed in to change notification settings - Fork 79
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, nice! Checks are still failing: https://github.com/r-dbi/RPostgres/actions/runs/11416140787/job/31767025307?pr=472#step:16:263
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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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,"'")) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Closes #376. Introduces a postgres-specific function
postgresImportLargeObject
for importing large objects from client side using libpq'slo_import_with_oid