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