Deleting OID type BLOB for PostgreSQL

Specifically deleting a row that contains a Large Object reference does not delete the Large Object. Deleting the Large Object is a separate operation that needs to be performed..

In JDBC this can be done in two steps:

Delete the large object (call PostgreSQL function lo_unlink)

long oid = jdbcTemplate.queryForObject("select image from InventoryItem where id = ?",
			new Object[] { id },
			new RowMapper<Long>() {
			    public Long mapRow(ResultSet rs, int row) throws SQLException {
				return new Long(rs.getLong(1));
jdbcTemplate.execute("select lo_unlink(" + oid + ")");

Delete the row in the referring table:

jdbcTemplate.update("delete from InventoryItem where id = '" + id + "'");

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s