8

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

2 Answers 2

12

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:

Sign up to request clarification or add additional context in comments.

Comments

4

Erwin Brandstetter's answer implemented like this:

SELECT lo_unlink(l.oid) FROM pg_largeobject_metadata l;

vacuum full

cluster

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.