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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.