patternsqlMinor
Replacing a large object in PostgreSQL
Viewed 0 times
postgresqlobjectlargereplacing
Problem
What should one do when one wishes to replace a large object with a new large object. An example would be to update an uploaded file with a new version.
One way would presumably be to create a new large object, update links to the new oid, and then unlink the old object. But is it reasonable to instead replace the existing data, say, by writing over the original (perhaps truncating the contents first)?
FWIW I'd be doing this in JDBC and for PostgreSQL 9.4.
One way would presumably be to create a new large object, update links to the new oid, and then unlink the old object. But is it reasonable to instead replace the existing data, say, by writing over the original (perhaps truncating the contents first)?
FWIW I'd be doing this in JDBC and for PostgreSQL 9.4.
Solution
The idea behind the large object API is to mimic a file-like API. The OID is like the path of the file, and the file descriptor obtained by
JDBC provides
So yes it makes sense to replace LO contents (keeping the same OID) by a truncate followed by
It would work as well to create a new LO with a new OID and then unlink the old one, it's just less elegant and it consumes a new OID for no good reason.
lo_open or lo_creat is the equivalent of the POSIX open() and creat() system calls for files.JDBC provides
LargeObject.truncate() and the both libpq (in C) and the server have built-in lo_truncate() functions.So yes it makes sense to replace LO contents (keeping the same OID) by a truncate followed by
lo_write if libpq, or lowrite if server-side code, or LargeObject.write() if using the dedicated JDBC class, as you could do with a file on a filesystem.It would work as well to create a new LO with a new OID and then unlink the old one, it's just less elegant and it consumes a new OID for no good reason.
Context
StackExchange Database Administrators Q#116103, answer score: 6
Revisions (0)
No revisions yet.