PostgreSQL Source Code
git master
Toggle main menu visibility
Main Page
Related Pages
Namespaces
Namespace List
Namespace Members
All
a
c
d
g
h
i
k
l
m
p
r
s
t
Functions
Variables
Data Structures
Data Structures
Data Structure Index
Class Hierarchy
Data Fields
All
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Functions
_
a
c
d
f
h
i
n
o
p
r
s
t
~
Variables
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Enumerations
Files
File List
Globals
All
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Functions
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Variables
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Typedefs
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Enumerations
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
Enumerator
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
z
Macros
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
•
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Pages
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
15
PG_MODULE_MAGIC_EXT
(
16
.
name
=
"lo"
,
17
.version = PG_VERSION
18
);
19
20
21
/*
22
* This is the trigger that protects us from orphaned large objects
23
*/
24
PG_FUNCTION_INFO_V1
(
lo_manage
);
25
26
Datum
27
lo_manage
(
PG_FUNCTION_ARGS
)
28
{
29
TriggerData
*trigdata = (
TriggerData
*) fcinfo->context;
30
int
attnum
;
/* attribute number to monitor */
31
char
**
args
;
/* Args containing attr name */
32
TupleDesc
tupdesc;
/* Tuple Descriptor */
33
HeapTuple
rettuple;
/* Tuple to be returned */
34
bool
isdelete;
/* are we deleting? */
35
HeapTuple
newtuple;
/* The new value for tuple */
36
HeapTuple
trigtuple;
/* The original value of tuple */
37
38
if
(!
CALLED_AS_TRIGGER
(fcinfo))
/* internal error */
39
elog
(
ERROR
,
"lo_manage: not fired by trigger manager"
);
40
41
if
(!
TRIGGER_FIRED_FOR_ROW
(trigdata->
tg_event
))
/* internal error */
42
elog
(
ERROR
,
"%s: must be fired for row"
,
43
trigdata->
tg_trigger
->
tgname
);
44
45
/*
46
* Fetch some values from trigdata
47
*/
48
newtuple = trigdata->
tg_newtuple
;
49
trigtuple = trigdata->
tg_trigtuple
;
50
tupdesc = trigdata->
tg_relation
->
rd_att
;
51
args
= trigdata->
tg_trigger
->
tgargs
;
52
53
if
(
args
== NULL)
/* internal error */
54
elog
(
ERROR
,
"%s: no column name provided in the trigger definition"
,
55
trigdata->
tg_trigger
->
tgname
);
56
57
/* tuple to return to Executor */
58
if
(
TRIGGER_FIRED_BY_UPDATE
(trigdata->
tg_event
))
59
rettuple = newtuple;
60
else
61
rettuple = trigtuple;
62
63
/* Are we deleting the row? */
64
isdelete =
TRIGGER_FIRED_BY_DELETE
(trigdata->
tg_event
);
65
66
/* Get the column we're interested in */
67
attnum
=
SPI_fnumber
(tupdesc,
args
[0]);
68
69
if
(
attnum
<= 0)
70
elog
(
ERROR
,
"%s: column \"%s\" does not exist"
,
71
trigdata->
tg_trigger
->
tgname
,
args
[0]);
72
73
/*
74
* Handle updates
75
*
76
* Here, if the value of the monitored attribute changes, then the large
77
* object associated with the original value is unlinked.
78
*/
79
if
(newtuple != NULL &&
80
bms_is_member
(
attnum
-
FirstLowInvalidHeapAttributeNumber
, trigdata->
tg_updatedcols
))
81
{
82
char
*orig =
SPI_getvalue
(trigtuple, tupdesc,
attnum
);
83
char
*newv =
SPI_getvalue
(newtuple, tupdesc,
attnum
);
84
85
if
(orig != NULL && (newv == NULL || strcmp(orig, newv) != 0))
86
DirectFunctionCall1
(
be_lo_unlink
,
87
ObjectIdGetDatum
(
atooid
(orig)));
88
89
if
(newv)
90
pfree
(newv);
91
if
(orig)
92
pfree
(orig);
93
}
94
95
/*
96
* Handle deleting of rows
97
*
98
* Here, we unlink the large object associated with the managed attribute
99
*/
100
if
(isdelete)
101
{
102
char
*orig =
SPI_getvalue
(trigtuple, tupdesc,
attnum
);
103
104
if
(orig != NULL)
105
{
106
DirectFunctionCall1
(
be_lo_unlink
,
107
ObjectIdGetDatum
(
atooid
(orig)));
108
109
pfree
(orig);
110
}
111
}
112
113
return
PointerGetDatum
(rettuple);
114
}
be_lo_unlink
Datum be_lo_unlink(PG_FUNCTION_ARGS)
Definition:
be-fsstubs.c:314
bms_is_member
bool bms_is_member(int x, const Bitmapset *a)
Definition:
bitmapset.c:510
ERROR
#define ERROR
Definition:
elog.h:39
elog
#define elog(elevel,...)
Definition:
elog.h:225
DirectFunctionCall1
#define DirectFunctionCall1(func, arg1)
Definition:
fmgr.h:682
PG_FUNCTION_ARGS
#define PG_FUNCTION_ARGS
Definition:
fmgr.h:193
if
if(TABLE==NULL||TABLE_index==NULL)
Definition:
isn.c:81
PG_MODULE_MAGIC_EXT
PG_MODULE_MAGIC_EXT(.name="lo",.version=PG_VERSION)
lo_manage
Datum lo_manage(PG_FUNCTION_ARGS)
Definition:
lo.c:27
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(lo_manage)
pfree
void pfree(void *pointer)
Definition:
mcxt.c:2150
generate_unaccent_rules.args
args
Definition:
generate_unaccent_rules.py:288
attnum
int16 attnum
Definition:
pg_attribute.h:74
postgres.h
PointerGetDatum
static Datum PointerGetDatum(const void *X)
Definition:
postgres.h:327
Datum
uintptr_t Datum
Definition:
postgres.h:69
ObjectIdGetDatum
static Datum ObjectIdGetDatum(Oid X)
Definition:
postgres.h:257
atooid
#define atooid(x)
Definition:
postgres_ext.h:41
rel.h
SPI_fnumber
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition:
spi.c:1176
SPI_getvalue
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition:
spi.c:1221
spi.h
HeapTupleData
Definition:
htup.h:63
RelationData::rd_att
TupleDesc rd_att
Definition:
rel.h:112
TriggerData
Definition:
trigger.h:32
TriggerData::tg_relation
Relation tg_relation
Definition:
trigger.h:35
TriggerData::tg_updatedcols
const Bitmapset * tg_updatedcols
Definition:
trigger.h:43
TriggerData::tg_event
TriggerEvent tg_event
Definition:
trigger.h:34
TriggerData::tg_newtuple
HeapTuple tg_newtuple
Definition:
trigger.h:37
TriggerData::tg_trigger
Trigger * tg_trigger
Definition:
trigger.h:38
TriggerData::tg_trigtuple
HeapTuple tg_trigtuple
Definition:
trigger.h:36
Trigger::tgname
char * tgname
Definition:
reltrigger.h:27
Trigger::tgargs
char ** tgargs
Definition:
reltrigger.h:41
TupleDescData
Definition:
tupdesc.h:136
FirstLowInvalidHeapAttributeNumber
#define FirstLowInvalidHeapAttributeNumber
Definition:
sysattr.h:27
trigger.h
TRIGGER_FIRED_BY_DELETE
#define TRIGGER_FIRED_BY_DELETE(event)
Definition:
trigger.h:113
CALLED_AS_TRIGGER
#define CALLED_AS_TRIGGER(fcinfo)
Definition:
trigger.h:26
TRIGGER_FIRED_FOR_ROW
#define TRIGGER_FIRED_FOR_ROW(event)
Definition:
trigger.h:122
TRIGGER_FIRED_BY_UPDATE
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition:
trigger.h:116
name
const char * name
Definition:
wait_event_funcs.c:28
contrib
lo
lo.c
Generated on Sat Apr 26 2025 18:13:14 for PostgreSQL Source Code by
1.9.4