PostgreSQL Source Code git master
lo.c
Go to the documentation of this file.
1/*
2 * PostgreSQL definitions for managed Large Objects.
3 *
4 * contrib/lo/lo.c
5 *
6 */
7
8#include "postgres.h"
9
10#include "commands/trigger.h"
11#include "executor/spi.h"
12#include "utils/fmgrprotos.h"
13#include "utils/rel.h"
14
16
17
18/*
19 * This is the trigger that protects us from orphaned large objects
20 */
22
25{
26 TriggerData *trigdata = (TriggerData *) fcinfo->context;
27 int attnum; /* attribute number to monitor */
28 char **args; /* Args containing attr name */
29 TupleDesc tupdesc; /* Tuple Descriptor */
30 HeapTuple rettuple; /* Tuple to be returned */
31 bool isdelete; /* are we deleting? */
32 HeapTuple newtuple; /* The new value for tuple */
33 HeapTuple trigtuple; /* The original value of tuple */
34
35 if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
36 elog(ERROR, "lo_manage: not fired by trigger manager");
37
38 if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event)) /* internal error */
39 elog(ERROR, "%s: must be fired for row",
40 trigdata->tg_trigger->tgname);
41
42 /*
43 * Fetch some values from trigdata
44 */
45 newtuple = trigdata->tg_newtuple;
46 trigtuple = trigdata->tg_trigtuple;
47 tupdesc = trigdata->tg_relation->rd_att;
48 args = trigdata->tg_trigger->tgargs;
49
50 if (args == NULL) /* internal error */
51 elog(ERROR, "%s: no column name provided in the trigger definition",
52 trigdata->tg_trigger->tgname);
53
54 /* tuple to return to Executor */
55 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
56 rettuple = newtuple;
57 else
58 rettuple = trigtuple;
59
60 /* Are we deleting the row? */
61 isdelete = TRIGGER_FIRED_BY_DELETE(trigdata->tg_event);
62
63 /* Get the column we're interested in */
64 attnum = SPI_fnumber(tupdesc, args[0]);
65
66 if (attnum <= 0)
67 elog(ERROR, "%s: column \"%s\" does not exist",
68 trigdata->tg_trigger->tgname, args[0]);
69
70 /*
71 * Handle updates
72 *
73 * Here, if the value of the monitored attribute changes, then the large
74 * object associated with the original value is unlinked.
75 */
76 if (newtuple != NULL &&
78 {
79 char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
80 char *newv = SPI_getvalue(newtuple, tupdesc, attnum);
81
82 if (orig != NULL && (newv == NULL || strcmp(orig, newv) != 0))
85
86 if (newv)
87 pfree(newv);
88 if (orig)
89 pfree(orig);
90 }
91
92 /*
93 * Handle deleting of rows
94 *
95 * Here, we unlink the large object associated with the managed attribute
96 */
97 if (isdelete)
98 {
99 char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
100
101 if (orig != NULL)
102 {
104 ObjectIdGetDatum(atooid(orig)));
105
106 pfree(orig);
107 }
108 }
109
110 return PointerGetDatum(rettuple);
111}
Datum be_lo_unlink(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:314
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
PG_MODULE_MAGIC
Definition: lo.c:15
Datum lo_manage(PG_FUNCTION_ARGS)
Definition: lo.c:24
PG_FUNCTION_INFO_V1(lo_manage)
void pfree(void *pointer)
Definition: mcxt.c:1521
int16 attnum
Definition: pg_attribute.h:74
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
#define atooid(x)
Definition: postgres_ext.h:43
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1175
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:1220
TupleDesc rd_att
Definition: rel.h:112
Relation tg_relation
Definition: trigger.h:35
const Bitmapset * tg_updatedcols
Definition: trigger.h:43
TriggerEvent tg_event
Definition: trigger.h:34
HeapTuple tg_newtuple
Definition: trigger.h:37
Trigger * tg_trigger
Definition: trigger.h:38
HeapTuple tg_trigtuple
Definition: trigger.h:36
char * tgname
Definition: reltrigger.h:27
char ** tgargs
Definition: reltrigger.h:41
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:113
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:122
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:116