I am trying to remove all large objects from a PostgreSQL database.
We work with large, image datasets. Originally, these were stored as Large Objects in postgres, along with their metadata. I am in the process of converting these databases into just storing file system references to the image in order to better manage the, sometimes conflicting, disk requirements of databases versus image data.
After exporting all of the Large Objects, creating references to them and testing, I am ready to delete the Large Objects.
I have tried to write a function which will allow me to delete all of them, but to no avail. This seemed to work but because not every number in the range corresponds to a large object, it fell over.
CREATE OR REPLACE FUNCTION "DeleteLOs"() RETURNS INTEGER AS
$$
DECLARE
BEGIN
FOR i IN 1620762..1801116 LOOP
SELECT lo_unlink(i);
END LOOP;
RETURN 0;
END;
$$
LANGUAGE plpgsql;
Ideally, I would be able to pair a function like this with a query to ensure I got them all, rather than specifying a range which might not be complete:
SELECT DISTINCT loid FROM pg_largeobject
To delete all existing large objects in your database:
SELECT lo_unlink(l.oid) FROM pg_largeobject_metadata l;
Related answer on dba.SE with more details:
Erwin Brandstetter's answer implemented like this:
SELECT lo_unlink(l.oid) FROM pg_largeobject_metadata l;
vacuum full
cluster
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With