简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM