@@ -8,62 +8,81 @@ also), is that the specification assumes that references to BLOBS (Binary
8
8
Large OBjectS) are stored within a table, and if that entry is changed, the
9
9
associated BLOB is deleted from the database.
10
10
11
- As PostgreSQL stands, this doesn't occur. It allocates an OID for each object,
12
- and it is up to the application to store, and ultimately delete the objects.
13
-
14
- Now this is fine for new postgresql specific applications, but existing ones
15
- using JDBC or ODBC wont delete the objects, arising to orphaning - objects
11
+ As PostgreSQL stands, this doesn't occur. Large objects are treated as
12
+ objects in their own right; a table entry can reference a large object by
13
+ OID, but there can be multiple table entries referencing the same large
14
+ object OID, so the system doesn't delete the large object just because you
15
+ change or remove one such entry.
16
+
17
+ Now this is fine for new PostgreSQL-specific applications, but existing ones
18
+ using JDBC or ODBC won't delete the objects, resulting in orphaning - objects
16
19
that are not referenced by anything, and simply occupy disk space.
17
20
21
+
18
22
The Fix
19
23
20
24
I've fixed this by creating a new data type 'lo', some support functions, and
21
- a Trigger which handles the orphaning problem.
25
+ a Trigger which handles the orphaning problem. The trigger essentially just
26
+ does a 'lo_unlink' whenever you delete or modify a value referencing a large
27
+ object. When you use this trigger, you are assuming that there is only one
28
+ database reference to any large object that is referenced in a
29
+ trigger-controlled column!
30
+
31
+ The 'lo' type was created because we needed to differentiate between plain
32
+ OIDs and Large Objects. Currently the JDBC driver handles this dilemma easily,
33
+ but (after talking to Byron), the ODBC driver needed a unique type. They had
34
+ created an 'lo' type, but not the solution to orphaning.
35
+
36
+ You don't actually have to use the 'lo' type to use the trigger, but it may be
37
+ convenient to use it to keep track of which columns in your database represent
38
+ large objects that you are managing with the trigger.
22
39
23
- The 'lo' type was created because we needed to differenciate between normal
24
- Oid's and Large Objects. Currently the JDBC driver handles this dilema easily,
25
- but (after talking to Byron), the ODBC driver needed a unique type. They had created an 'lo' type, but not the solution to orphaning.
26
40
27
41
Install
28
42
29
43
Ok, first build the shared library, and install. Typing 'make install' in the
30
44
contrib/lo directory should do it.
31
45
32
- Then, as the postgres super user, run the lo.sql script. This will install the
33
- type, and define the support functions.
46
+ Then, as the postgres super user, run the lo.sql script in any database that
47
+ needs the features. This will install the type, and define the support
48
+ functions. You can run the script once in template1, and the objects will be
49
+ inherited by subsequently-created databases.
50
+
34
51
35
52
How to Use
36
53
37
54
The easiest way is by an example:
38
55
39
- > create table image (title text,raster lo);
40
- > create trigger t_image before update or delete on image for each row execute procedure lo_manage(raster);
56
+ > create table image (title text, raster lo);
57
+ > create trigger t_raster before update or delete on image
58
+ > for each row execute procedure lo_manage(raster);
41
59
42
- Here, a trigger is created for each column that contains a lo type.
60
+ Create a trigger for each column that contains a lo type, and give the column
61
+ name as the trigger procedure argument. You can have more than one trigger on
62
+ a table if you need multiple lo columns in the same table, but don't forget to
63
+ give a different name to each trigger.
43
64
44
- Issues
45
65
46
- * dropping a table will still orphan any objects it contains, as the trigger
47
- is not actioned.
66
+ Issues
48
67
49
- For now, precede the 'drop table' with 'delete from {table}'. However, this
50
- could be fixed by having 'drop table' perform an additional
68
+ * Dropping a table will still orphan any objects it contains, as the trigger
69
+ is not executed.
51
70
52
- 'select lo_unlink({colname}::oid) from {tablename}'
71
+ Avoid this by preceding the 'drop table' with 'delete from {table}'.
53
72
54
- for each column, before actually dropping the table.
73
+ If you already have, or suspect you have, orphaned large objects, see
74
+ the contrib/vacuumlo module to help you clean them up. It's a good idea
75
+ to run contrib/vacuumlo occasionally as a back-stop to the lo_manage
76
+ trigger.
55
77
56
78
* Some frontends may create their own tables, and will not create the
57
79
associated trigger(s). Also, users may not remember (or know) to create
58
80
the triggers.
59
81
60
- This can be solved, but would involve changes to the parser.
61
-
62
82
As the ODBC driver needs a permanent lo type (& JDBC could be optimised to
63
83
use it if it's Oid is fixed), and as the above issues can only be fixed by
64
84
some internal changes, I feel it should become a permanent built-in type.
65
85
66
86
I'm releasing this into contrib, just to get it out, and tested.
67
87
68
88
Peter Mount <peter@retep.org.uk> June 13 1998
69
-
0 commit comments