2 * PostgreSQL definitions for managed Large Objects.
4 * $PostgreSQL: pgsql/contrib/lo/lo.c,v 1.15 2005/06/23 00:06:37 tgl Exp $
10 /* Required for largeobjects */
11 #include "libpq/libpq-fs.h"
12 #include "libpq/be-fsstubs.h"
14 /* Required for SPI */
15 #include "executor/spi.h"
17 /* Required for triggers */
18 #include "commands/trigger.h"
21 #define atooid(x) ((Oid) strtoul((x), NULL, 10))
24 /* forward declarations */
25 Datum lo_manage(PG_FUNCTION_ARGS);
29 * This is the trigger that protects us from orphaned large objects
31 PG_FUNCTION_INFO_V1(lo_manage);
34 lo_manage(PG_FUNCTION_ARGS)
36 TriggerData *trigdata = (TriggerData *) fcinfo->context;
37 int attnum; /* attribute number to monitor */
38 char **args; /* Args containing attr name */
39 TupleDesc tupdesc; /* Tuple Descriptor */
40 HeapTuple rettuple; /* Tuple to be returned */
41 bool isdelete; /* are we deleting? */
42 HeapTuple newtuple; /* The new value for tuple */
43 HeapTuple trigtuple; /* The original value of tuple */
45 if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
46 elog(ERROR, "not fired by trigger manager");
49 * Fetch some values from trigdata
51 newtuple = trigdata->tg_newtuple;
52 trigtuple = trigdata->tg_trigtuple;
53 tupdesc = trigdata->tg_relation->rd_att;
54 args = trigdata->tg_trigger->tgargs;
56 /* tuple to return to Executor */
57 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
62 /* Are we deleting the row? */
63 isdelete = TRIGGER_FIRED_BY_DELETE(trigdata->tg_event);
65 /* Get the column we're interested in */
66 attnum = SPI_fnumber(tupdesc, args[0]);
69 elog(ERROR, "column \"%s\" does not exist", args[0]);
74 * Here, if the value of the monitored attribute changes, then the large
75 * object associated with the original value is unlinked.
79 char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
80 char *newv = SPI_getvalue(newtuple, tupdesc, attnum);
82 if (orig != NULL && (newv == NULL || strcmp(orig, newv) != 0))
83 DirectFunctionCall1(lo_unlink,
84 ObjectIdGetDatum(atooid(orig)));
93 * Handle deleting of rows
95 * Here, we unlink the large object associated with the managed attribute
99 char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
103 DirectFunctionCall1(lo_unlink,
104 ObjectIdGetDatum(atooid(orig)));
110 return PointerGetDatum(rettuple);