10 PostgreSQL type extension for managing Large Objects
14 <title>Overview</title>
16 One of the problems with the JDBC driver (and this affects the ODBC driver
17 also), is that the specification assumes that references to BLOBS (Binary
18 Large OBjectS) are stored within a table, and if that entry is changed, the
19 associated BLOB is deleted from the database.
22 As PostgreSQL stands, this doesn't occur. Large objects are treated as
23 objects in their own right; a table entry can reference a large object by
24 OID, but there can be multiple table entries referencing the same large
25 object OID, so the system doesn't delete the large object just because you
26 change or remove one such entry.
29 Now this is fine for new PostgreSQL-specific applications, but existing ones
30 using JDBC or ODBC won't delete the objects, resulting in orphaning - objects
31 that are not referenced by anything, and simply occupy disk space.
36 <title>The Fix</title>
38 I've fixed this by creating a new data type 'lo', some support functions, and
39 a Trigger which handles the orphaning problem. The trigger essentially just
40 does a 'lo_unlink' whenever you delete or modify a value referencing a large
41 object. When you use this trigger, you are assuming that there is only one
42 database reference to any large object that is referenced in a
43 trigger-controlled column!
46 The 'lo' type was created because we needed to differentiate between plain
47 OIDs and Large Objects. Currently the JDBC driver handles this dilemma easily,
48 but (after talking to Byron), the ODBC driver needed a unique type. They had
49 created an 'lo' type, but not the solution to orphaning.
52 You don't actually have to use the 'lo' type to use the trigger, but it may be
53 convenient to use it to keep track of which columns in your database represent
54 large objects that you are managing with the trigger.
59 <title>How to Use</title>
61 The easiest way is by an example:
64 CREATE TABLE image (title TEXT, raster lo);
65 CREATE TRIGGER t_raster BEFORE UPDATE OR DELETE ON image
66 FOR EACH ROW EXECUTE PROCEDURE lo_manage(raster);
69 Create a trigger for each column that contains a lo type, and give the column
70 name as the trigger procedure argument. You can have more than one trigger on
71 a table if you need multiple lo columns in the same table, but don't forget to
72 give a different name to each trigger.
82 Dropping a table will still orphan any objects it contains, as the trigger
86 Avoid this by preceding the 'drop table' with 'delete from {table}'.
89 If you already have, or suspect you have, orphaned large objects, see
90 the contrib/vacuumlo module to help you clean them up. It's a good idea
91 to run contrib/vacuumlo occasionally as a back-stop to the lo_manage
97 Some frontends may create their own tables, and will not create the
98 associated trigger(s). Also, users may not remember (or know) to create
105 As the ODBC driver needs a permanent lo type (& JDBC could be optimised to
106 use it if it's Oid is fixed), and as the above issues can only be fixed by
107 some internal changes, I feel it should become a permanent built-in type.
112 <title>Author</title>
114 Peter Mount <email>peter@retep.org.uk</email> June 13 1998