2
* PostgreSQL definitions for managed Large Objects.
10
#include "commands/trigger.h"
11
#include "executor/spi.h"
12
#include "libpq/be-fsstubs.h"
13
#include "utils/rel.h"
18
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
21
/* forward declarations */
22
Datum lo_manage(PG_FUNCTION_ARGS);
26
* This is the trigger that protects us from orphaned large objects
28
PG_FUNCTION_INFO_V1(lo_manage);
31
lo_manage(PG_FUNCTION_ARGS)
33
TriggerData *trigdata = (TriggerData *) fcinfo->context;
34
int attnum; /* attribute number to monitor */
35
char **args; /* Args containing attr name */
36
TupleDesc tupdesc; /* Tuple Descriptor */
37
HeapTuple rettuple; /* Tuple to be returned */
38
bool isdelete; /* are we deleting? */
39
HeapTuple newtuple; /* The new value for tuple */
40
HeapTuple trigtuple; /* The original value of tuple */
42
if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
43
elog(ERROR, "not fired by trigger manager");
46
* Fetch some values from trigdata
48
newtuple = trigdata->tg_newtuple;
49
trigtuple = trigdata->tg_trigtuple;
50
tupdesc = trigdata->tg_relation->rd_att;
51
args = trigdata->tg_trigger->tgargs;
53
/* tuple to return to Executor */
54
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
59
/* Are we deleting the row? */
60
isdelete = TRIGGER_FIRED_BY_DELETE(trigdata->tg_event);
62
/* Get the column we're interested in */
63
attnum = SPI_fnumber(tupdesc, args[0]);
66
elog(ERROR, "column \"%s\" does not exist", args[0]);
71
* Here, if the value of the monitored attribute changes, then the large
72
* object associated with the original value is unlinked.
76
char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
77
char *newv = SPI_getvalue(newtuple, tupdesc, attnum);
79
if (orig != NULL && (newv == NULL || strcmp(orig, newv) != 0))
80
DirectFunctionCall1(lo_unlink,
81
ObjectIdGetDatum(atooid(orig)));
90
* Handle deleting of rows
92
* Here, we unlink the large object associated with the managed attribute
96
char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
100
DirectFunctionCall1(lo_unlink,
101
ObjectIdGetDatum(atooid(orig)));
107
return PointerGetDatum(rettuple);