1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>37.4. A Complete Trigger Example</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="trigger-interface.html" title="37.3. Writing Trigger Functions in C" /><link rel="next" href="event-triggers.html" title="Chapter 38. Event Triggers" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">37.4. A Complete Trigger Example</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="trigger-interface.html" title="37.3. Writing Trigger Functions in C">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="triggers.html" title="Chapter 37. Triggers">Up</a></td><th width="60%" align="center">Chapter 37. Triggers</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="event-triggers.html" title="Chapter 38. Event Triggers">Next</a></td></tr></table><hr /></div><div class="sect1" id="TRIGGER-EXAMPLE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">37.4. A Complete Trigger Example <a href="#TRIGGER-EXAMPLE" class="id_link">#</a></h2></div></div></div><p>
3 Here is a very simple example of a trigger function written in C.
4 (Examples of triggers written in procedural languages can be found
5 in the documentation of the procedural languages.)
7 The function <code class="function">trigf</code> reports the number of rows in the
8 table <code class="structname">ttest</code> and skips the actual operation if the
9 command attempts to insert a null value into the column
10 <code class="structfield">x</code>. (So the trigger acts as a not-null constraint but
11 doesn't abort the transaction.)
13 First, the table definition:
14 </p><pre class="programlisting">
20 This is the source code of the trigger function:
21 </p><pre class="programlisting">
24 #include "executor/spi.h" /* this is what you need to work with SPI */
25 #include "commands/trigger.h" /* ... triggers ... */
26 #include "utils/rel.h" /* ... and relations */
30 PG_FUNCTION_INFO_V1(trigf);
33 trigf(PG_FUNCTION_ARGS)
35 TriggerData *trigdata = (TriggerData *) fcinfo->context;
39 bool checknull = false;
43 /* make sure it's called as a trigger at all */
44 if (!CALLED_AS_TRIGGER(fcinfo))
45 elog(ERROR, "trigf: not called by trigger manager");
47 /* tuple to return to executor */
48 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
49 rettuple = trigdata->tg_newtuple;
51 rettuple = trigdata->tg_trigtuple;
53 /* check for null values */
54 if (!TRIGGER_FIRED_BY_DELETE(trigdata->tg_event)
55 && TRIGGER_FIRED_BEFORE(trigdata->tg_event))
58 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
63 tupdesc = trigdata->tg_relation->rd_att;
65 /* connect to SPI manager */
68 /* get number of rows in table */
69 ret = SPI_exec("SELECT count(*) FROM ttest", 0);
72 elog(ERROR, "trigf (fired %s): SPI_exec returned %d", when, ret);
74 /* count(*) returns int8, so be careful to convert */
75 i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0],
76 SPI_tuptable->tupdesc,
80 elog (INFO, "trigf (fired %s): there are %d rows in ttest", when, i);
86 SPI_getbinval(rettuple, tupdesc, 1, &isnull);
91 return PointerGetDatum(rettuple);
96 After you have compiled the source code (see <a class="xref" href="xfunc-c.html#DFUNC" title="36.10.5. Compiling and Linking Dynamically-Loaded Functions">Section 36.10.5</a>), declare the function and the triggers:
97 </p><pre class="programlisting">
98 CREATE FUNCTION trigf() RETURNS trigger
99 AS '<em class="replaceable"><code>filename</code></em>'
102 CREATE TRIGGER tbefore BEFORE INSERT OR UPDATE OR DELETE ON ttest
103 FOR EACH ROW EXECUTE FUNCTION trigf();
105 CREATE TRIGGER tafter AFTER INSERT OR UPDATE OR DELETE ON ttest
106 FOR EACH ROW EXECUTE FUNCTION trigf();
109 Now you can test the operation of the trigger:
110 </p><pre class="screen">
111 => INSERT INTO ttest VALUES (NULL);
112 INFO: trigf (fired before): there are 0 rows in ttest
115 -- Insertion skipped and AFTER trigger is not fired
117 => SELECT * FROM ttest;
122 => INSERT INTO ttest VALUES (1);
123 INFO: trigf (fired before): there are 0 rows in ttest
124 INFO: trigf (fired after ): there are 1 rows in ttest
126 remember what we said about visibility.
128 vac=> SELECT * FROM ttest;
134 => INSERT INTO ttest SELECT x * 2 FROM ttest;
135 INFO: trigf (fired before): there are 1 rows in ttest
136 INFO: trigf (fired after ): there are 2 rows in ttest
138 remember what we said about visibility.
140 => SELECT * FROM ttest;
147 => UPDATE ttest SET x = NULL WHERE x = 2;
148 INFO: trigf (fired before): there are 2 rows in ttest
150 => UPDATE ttest SET x = 4 WHERE x = 2;
151 INFO: trigf (fired before): there are 2 rows in ttest
152 INFO: trigf (fired after ): there are 2 rows in ttest
154 vac=> SELECT * FROM ttest;
161 => DELETE FROM ttest;
162 INFO: trigf (fired before): there are 2 rows in ttest
163 INFO: trigf (fired before): there are 1 rows in ttest
164 INFO: trigf (fired after ): there are 0 rows in ttest
165 INFO: trigf (fired after ): there are 0 rows in ttest
167 remember what we said about visibility.
169 => SELECT * FROM ttest;
176 There are more complex examples in
177 <code class="filename">src/test/regress/regress.c</code> and
178 in <a class="xref" href="contrib-spi.html" title="F.41. spi — Server Programming Interface features/examples">spi</a>.
179 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="trigger-interface.html" title="37.3. Writing Trigger Functions in C">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="triggers.html" title="Chapter 37. Triggers">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="event-triggers.html" title="Chapter 38. Event Triggers">Next</a></td></tr><tr><td width="40%" align="left" valign="top">37.3. Writing Trigger Functions in C </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> Chapter 38. Event Triggers</td></tr></table></div></body></html>