Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all Large Objects from PostgreSQL database

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
like image 300
George Avatar asked Oct 15 '15 02:10

George


2 Answers

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:

  • Can I do VACUUM FULL to pg_largeobject table?
like image 149
Erwin Brandstetter Avatar answered Nov 04 '22 11:11

Erwin Brandstetter


Erwin Brandstetter's answer implemented like this:

SELECT lo_unlink(l.oid) FROM pg_largeobject_metadata l;

vacuum full

cluster
like image 20
Skysnake Avatar answered Nov 04 '22 10:11

Skysnake