1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5
>Writing Trigger Functions in C</TITLE
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
12
TITLE="PostgreSQL 9.3beta1 Documentation"
13
HREF="index.html"><LINK
16
HREF="triggers.html"><LINK
18
TITLE="Visibility of Data Changes"
19
HREF="trigger-datachanges.html"><LINK
21
TITLE="A Complete Trigger Example"
22
HREF="trigger-example.html"><LINK
25
HREF="stylesheet.css"><META
26
HTTP-EQUIV="Content-Type"
27
CONTENT="text/html; charset=ISO-8859-1"><META
29
CONTENT="2013-05-06T21:00:50"></HEAD
35
SUMMARY="Header navigation table"
47
>PostgreSQL 9.3beta1 Documentation</A
56
TITLE="Visibility of Data Changes"
57
HREF="trigger-datachanges.html"
74
>Chapter 36. Triggers</TD
80
TITLE="A Complete Trigger Example"
81
HREF="trigger-example.html"
95
NAME="TRIGGER-INTERFACE"
96
>36.3. Writing Trigger Functions in C</A
99
> This section describes the low-level details of the interface to a
100
trigger function. This information is only needed when writing
101
trigger functions in C. If you are using a higher-level language then
102
these details are handled for you. In most cases you should consider
103
using a procedural language before writing your triggers in C. The
104
documentation of each procedural language explains how to write a
105
trigger in that language.
108
> Trigger functions must use the <SPAN
115
> When a function is called by the trigger manager, it is not passed
116
any normal arguments, but it is passed a <SPAN
120
pointer pointing to a <TT
124
functions can check whether they were called from the trigger
125
manager or not by executing the macro:
127
CLASS="PROGRAMLISTING"
128
>CALLED_AS_TRIGGER(fcinfo)</PRE
132
CLASS="PROGRAMLISTING"
133
>((fcinfo)->context != NULL && IsA((fcinfo)->context, TriggerData))</PRE
135
If this returns true, then it is safe to cast
138
>fcinfo->context</TT
143
> and make use of the pointed-to
147
> structure. The function must
158
structure or any of the data it points to.
163
>struct TriggerData</TT
167
>commands/trigger.h</TT
171
CLASS="PROGRAMLISTING"
172
>typedef struct TriggerData
175
TriggerEvent tg_event;
176
Relation tg_relation;
177
HeapTuple tg_trigtuple;
178
HeapTuple tg_newtuple;
180
Buffer tg_trigtuplebuf;
181
Buffer tg_newtuplebuf;
185
where the members are defined as follows:
212
> Describes the event for which the function is called. You can use the
213
following macros to examine <TT
226
>TRIGGER_FIRED_BEFORE(tg_event)</TT
230
> Returns true if the trigger fired before the operation.
236
>TRIGGER_FIRED_AFTER(tg_event)</TT
240
> Returns true if the trigger fired after the operation.
246
>TRIGGER_FIRED_INSTEAD(tg_event)</TT
250
> Returns true if the trigger fired instead of the operation.
256
>TRIGGER_FIRED_FOR_ROW(tg_event)</TT
260
> Returns true if the trigger fired for a row-level event.
266
>TRIGGER_FIRED_FOR_STATEMENT(tg_event)</TT
270
> Returns true if the trigger fired for a statement-level event.
276
>TRIGGER_FIRED_BY_INSERT(tg_event)</TT
280
> Returns true if the trigger was fired by an <TT
289
>TRIGGER_FIRED_BY_UPDATE(tg_event)</TT
293
> Returns true if the trigger was fired by an <TT
302
>TRIGGER_FIRED_BY_DELETE(tg_event)</TT
306
> Returns true if the trigger was fired by a <TT
315
>TRIGGER_FIRED_BY_TRUNCATE(tg_event)</TT
319
> Returns true if the trigger was fired by a <TT
337
> A pointer to a structure describing the relation that the trigger fired for.
342
this structure. The most interesting things are
345
>tg_relation->rd_att</TT
346
> (descriptor of the relation
349
>tg_relation->rd_rel->relname</TT
351
(relation name; the type is not <TT
361
>SPI_getrelname(tg_relation)</TT
366
need a copy of the name).
376
> A pointer to the row for which the trigger was fired. This is
377
the row being inserted, updated, or deleted. If this trigger
385
> then this is what you should return
386
from the function if you don't want to replace the row with
387
a different one (in the case of <TT
401
> A pointer to the new version of the row, if the trigger was
416
>. This is what you have to return
417
from the function if the event is an <TT
421
and you don't want to replace this row by a different one or
432
> A pointer to a structure of type <TT
438
>utils/reltrigger.h</TT
442
CLASS="PROGRAMLISTING"
443
>typedef struct Trigger
467
> is the trigger's name,
471
> is the number of arguments in
479
pointers to the arguments specified in the <TT
483
> statement. The other members are for internal use
494
> The buffer containing <TT
501
is no such tuple or it is not stored in a disk buffer.
511
> The buffer containing <TT
518
is no such tuple or it is not stored in a disk buffer.
526
> A trigger function must return either a
540
> an SQL null value, that is, do not set <TT
544
Be careful to return either
552
as appropriate, if you don't want to modify the row being operated on.
560
SUMMARY="Footer navigation table"
571
HREF="trigger-datachanges.html"
589
HREF="trigger-example.html"
599
>Visibility of Data Changes</TD
613
>A Complete Trigger Example</TD
b'\\ No newline at end of file'