Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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