Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5b0c9d3

Browse files
committed
Cleanup the contrib/lo module: there is no need anymore to implement
a physically separate type. Defining 'lo' as a domain over OID works just fine and is more efficient. Improve documentation and fix up the test script. (Would like to turn test script into a proper regression test, but right now its output is not constant because of numeric OIDs; plus it makes Unix-specific assumptions about files it can import.)
1 parent d20763d commit 5b0c9d3

File tree

6 files changed

+91
-203
lines changed

6 files changed

+91
-203
lines changed

contrib/lo/README.lo

+43-24
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,81 @@ also), is that the specification assumes that references to BLOBS (Binary
88
Large OBjectS) are stored within a table, and if that entry is changed, the
99
associated BLOB is deleted from the database.
1010

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
1619
that are not referenced by anything, and simply occupy disk space.
1720

21+
1822
The Fix
1923

2024
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.
2239

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.
2640

2741
Install
2842

2943
Ok, first build the shared library, and install. Typing 'make install' in the
3044
contrib/lo directory should do it.
3145

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+
3451

3552
How to Use
3653

3754
The easiest way is by an example:
3855

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);
4159

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.
4364

44-
Issues
4565

46-
* dropping a table will still orphan any objects it contains, as the trigger
47-
is not actioned.
66+
Issues
4867

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.
5170

52-
'select lo_unlink({colname}::oid) from {tablename}'
71+
Avoid this by preceding the 'drop table' with 'delete from {table}'.
5372

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.
5577

5678
* Some frontends may create their own tables, and will not create the
5779
associated trigger(s). Also, users may not remember (or know) to create
5880
the triggers.
5981

60-
This can be solved, but would involve changes to the parser.
61-
6282
As the ODBC driver needs a permanent lo type (& JDBC could be optimised to
6383
use it if it's Oid is fixed), and as the above issues can only be fixed by
6484
some internal changes, I feel it should become a permanent built-in type.
6585

6686
I'm releasing this into contrib, just to get it out, and tested.
6787

6888
Peter Mount <peter@retep.org.uk> June 13 1998
69-

contrib/lo/lo.c

+12-116
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* PostgreSQL type definitions for managed LargeObjects.
2+
* PostgreSQL definitions for managed Large Objects.
33
*
4-
* $PostgreSQL: pgsql/contrib/lo/lo.c,v 1.14 2003/11/29 19:51:35 pgsql Exp $
4+
* $PostgreSQL: pgsql/contrib/lo/lo.c,v 1.15 2005/06/23 00:06:37 tgl Exp $
55
*
66
*/
77

@@ -21,117 +21,12 @@
2121
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
2222

2323

24-
/*
25-
* This is the internal storage format for managed large objects
26-
*
27-
*/
28-
29-
typedef Oid Blob;
30-
31-
/*
32-
* Various forward declarations:
33-
*/
34-
35-
Blob *lo_in(char *str); /* Create from String */
36-
char *lo_out(Blob * addr); /* Output oid as String */
37-
Oid lo_oid(Blob * addr); /* Return oid as an oid */
38-
Blob *lo(Oid oid); /* Return Blob based on oid */
39-
Datum lo_manage(PG_FUNCTION_ARGS); /* Trigger handler */
40-
41-
/*
42-
* This creates a large object, and sets its OID to the value in the
43-
* supplied string.
44-
*
45-
* If the string is empty, then a new LargeObject is created, and its oid
46-
* is placed in the resulting lo.
47-
*/
48-
Blob *
49-
lo_in(char *str)
50-
{
51-
Blob *result;
52-
Oid oid;
53-
int count;
54-
55-
if (strlen(str) > 0)
56-
{
57-
count = sscanf(str, "%u", &oid);
58-
59-
if (count < 1)
60-
ereport(ERROR,
61-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
62-
errmsg("error in parsing \"%s\"", str)));
63-
64-
if (oid == InvalidOid)
65-
ereport(ERROR,
66-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
67-
errmsg("illegal oid: \"%s\"", str)));
68-
}
69-
else
70-
{
71-
/*
72-
* There is no Oid passed, so create a new one
73-
*/
74-
oid = DatumGetObjectId(DirectFunctionCall1(lo_creat,
75-
Int32GetDatum(INV_READ | INV_WRITE)));
76-
if (oid == InvalidOid)
77-
/* internal error */
78-
elog(ERROR, "InvalidOid returned from lo_creat");
79-
}
80-
81-
result = (Blob *) palloc(sizeof(Blob));
82-
83-
*result = oid;
84-
85-
return (result);
86-
}
87-
88-
/*
89-
* This simply outputs the Oid of the Blob as a string.
90-
*/
91-
char *
92-
lo_out(Blob * addr)
93-
{
94-
char *result;
95-
96-
if (addr == NULL)
97-
return (NULL);
98-
99-
result = (char *) palloc(32);
100-
snprintf(result, 32, "%u", *addr);
101-
return (result);
102-
}
103-
104-
/*
105-
* This function converts Blob to oid.
106-
*
107-
* eg: select lo_export(raster::oid,'/path/file') from table;
108-
*
109-
*/
110-
Oid
111-
lo_oid(Blob * addr)
112-
{
113-
if (addr == NULL)
114-
return InvalidOid;
115-
return (Oid) (*addr);
116-
}
117-
118-
/*
119-
* This function is used so we can convert oid's to lo's
120-
*
121-
* ie: insert into table values(lo_import('/path/file')::lo);
122-
*
123-
*/
124-
Blob *
125-
lo(Oid oid)
126-
{
127-
Blob *result = (Blob *) palloc(sizeof(Blob));
24+
/* forward declarations */
25+
Datum lo_manage(PG_FUNCTION_ARGS);
12826

129-
*result = oid;
130-
return (result);
131-
}
13227

13328
/*
134-
* This handles the trigger that protects us from orphaned large objects
29+
* This is the trigger that protects us from orphaned large objects
13530
*/
13631
PG_FUNCTION_INFO_V1(lo_manage);
13732

@@ -144,11 +39,10 @@ lo_manage(PG_FUNCTION_ARGS)
14439
TupleDesc tupdesc; /* Tuple Descriptor */
14540
HeapTuple rettuple; /* Tuple to be returned */
14641
bool isdelete; /* are we deleting? */
147-
HeapTuple newtuple = NULL; /* The new value for tuple */
42+
HeapTuple newtuple; /* The new value for tuple */
14843
HeapTuple trigtuple; /* The original value of tuple */
14944

150-
if (!CALLED_AS_TRIGGER(fcinfo))
151-
/* internal error */
45+
if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
15246
elog(ERROR, "not fired by trigger manager");
15347

15448
/*
@@ -168,9 +62,12 @@ lo_manage(PG_FUNCTION_ARGS)
16862
/* Are we deleting the row? */
16963
isdelete = TRIGGER_FIRED_BY_DELETE(trigdata->tg_event);
17064

171-
/* Get the column were interested in */
65+
/* Get the column we're interested in */
17266
attnum = SPI_fnumber(tupdesc, args[0]);
17367

68+
if (attnum <= 0)
69+
elog(ERROR, "column \"%s\" does not exist", args[0]);
70+
17471
/*
17572
* Handle updates
17673
*
@@ -182,7 +79,7 @@ lo_manage(PG_FUNCTION_ARGS)
18279
char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
18380
char *newv = SPI_getvalue(newtuple, tupdesc, attnum);
18481

185-
if (orig != NULL && (newv == NULL || strcmp(orig, newv)))
82+
if (orig != NULL && (newv == NULL || strcmp(orig, newv) != 0))
18683
DirectFunctionCall1(lo_unlink,
18784
ObjectIdGetDatum(atooid(orig)));
18885

@@ -196,7 +93,6 @@ lo_manage(PG_FUNCTION_ARGS)
19693
* Handle deleting of rows
19794
*
19895
* Here, we unlink the large object associated with the managed attribute
199-
*
20096
*/
20197
if (isdelete)
20298
{

contrib/lo/lo.sql.in

+15-48
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,29 @@
11
--
2-
-- PostgreSQL code for LargeObjects
2+
-- PostgreSQL code for managed Large Objects
33
--
4-
-- $PostgreSQL: pgsql/contrib/lo/lo.sql.in,v 1.12 2005/01/29 22:35:02 tgl Exp $
5-
--
6-
--
7-
-- Create the data type
4+
-- $PostgreSQL: pgsql/contrib/lo/lo.sql.in,v 1.13 2005/06/23 00:06:37 tgl Exp $
85
--
96

10-
-- used by the lo type, it takes an oid and returns an lo object
11-
127
-- Adjust this setting to control where the objects get created.
138
SET search_path = public;
149

15-
CREATE FUNCTION lo_in(cstring)
16-
RETURNS lo
17-
AS 'MODULE_PATHNAME'
18-
LANGUAGE C IMMUTABLE STRICT;
19-
20-
-- used by the lo type, it returns the oid of the object
21-
CREATE FUNCTION lo_out(lo)
22-
RETURNS cstring
23-
AS 'MODULE_PATHNAME'
24-
LANGUAGE C IMMUTABLE STRICT;
25-
26-
-- finally the type itself
27-
CREATE TYPE lo (
28-
INTERNALLENGTH = 4,
29-
EXTERNALLENGTH = variable,
30-
INPUT = lo_in,
31-
OUTPUT = lo_out
32-
);
33-
34-
-- this returns the oid associated with a lo object
35-
CREATE FUNCTION lo_oid(lo)
36-
RETURNS oid
37-
AS 'MODULE_PATHNAME'
38-
LANGUAGE C IMMUTABLE STRICT;
10+
--
11+
-- Create the data type ... now just a domain over OID
12+
--
3913

40-
-- same function, named to allow it to be used as a type coercion, eg:
41-
-- CREATE TABLE a (image lo);
42-
-- SELECT image::oid FROM a;
43-
--
44-
CREATE FUNCTION oid(lo)
45-
RETURNS oid
46-
AS 'MODULE_PATHNAME', 'lo_oid'
47-
LANGUAGE C IMMUTABLE STRICT;
48-
CREATE CAST (lo as oid) WITH FUNCTION oid(lo) AS IMPLICIT;
14+
CREATE DOMAIN lo AS pg_catalog.oid;
4915

50-
-- this allows us to convert an oid to a managed lo object
51-
-- ie: insert into test values (lo_import('/fullpath/file')::lo);
52-
CREATE FUNCTION lo(oid)
53-
RETURNS lo
54-
AS 'MODULE_PATHNAME'
55-
LANGUAGE C IMMUTABLE STRICT;
56-
CREATE CAST (oid as lo) WITH FUNCTION lo(oid) AS IMPLICIT;
16+
--
17+
-- For backwards compatibility, define a function named lo_oid.
18+
--
19+
-- The other functions that formerly existed are not needed because
20+
-- the implicit casts between a domain and its underlying type handle them.
21+
--
22+
CREATE FUNCTION lo_oid(lo) RETURNS pg_catalog.oid AS
23+
'SELECT $1::pg_catalog.oid' LANGUAGE SQL STRICT IMMUTABLE;
5724

5825
-- This is used in triggers
5926
CREATE FUNCTION lo_manage()
60-
RETURNS trigger
27+
RETURNS pg_catalog.trigger
6128
AS 'MODULE_PATHNAME'
6229
LANGUAGE C;

contrib/lo/lo_drop.sql

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
--
2-
-- This removes the type (and a test table)
2+
-- This removes the LO type
33
-- It's used just for development
44
--
55

66
-- Adjust this setting to control where the objects get created.
77
SET search_path = public;
88

9-
-- remove our test table
10-
DROP TABLE a;
11-
12-
-- now drop the type and associated C functions
9+
-- drop the type and associated functions
1310
DROP TYPE lo CASCADE;
1411

1512
-- the trigger function has no dependency on the type, so drop separately

0 commit comments

Comments
 (0)