gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
7.5KB

  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>38.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 V1.79.1" /><link rel="prev" href="trigger-interface.html" title="38.3. Writing Trigger Functions in C" /><link rel="next" href="event-triggers.html" title="Chapter 39. Event Triggers" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">38.4. A Complete Trigger Example</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="trigger-interface.html" title="38.3. Writing Trigger Functions in C">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="triggers.html" title="Chapter 38. Triggers">Up</a></td><th width="60%" align="center">Chapter 38. Triggers</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="event-triggers.html" title="Chapter 39. Event Triggers">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TRIGGER-EXAMPLE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">38.4. A Complete Trigger Example</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.)
  6. </p><p>
  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.)
  12. </p><p>
  13. First, the table definition:
  14. </p><pre class="programlisting">
  15. CREATE TABLE ttest (
  16. x integer
  17. );
  18. </pre><p>
  19. </p><p>
  20. This is the source code of the trigger function:
  21. </p><pre class="programlisting">
  22. #include "postgres.h"
  23. #include "fmgr.h"
  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 */
  27. PG_MODULE_MAGIC;
  28. PG_FUNCTION_INFO_V1(trigf);
  29. Datum
  30. trigf(PG_FUNCTION_ARGS)
  31. {
  32. TriggerData *trigdata = (TriggerData *) fcinfo-&gt;context;
  33. TupleDesc tupdesc;
  34. HeapTuple rettuple;
  35. char *when;
  36. bool checknull = false;
  37. bool isnull;
  38. int ret, i;
  39. /* make sure it's called as a trigger at all */
  40. if (!CALLED_AS_TRIGGER(fcinfo))
  41. elog(ERROR, "trigf: not called by trigger manager");
  42. /* tuple to return to executor */
  43. if (TRIGGER_FIRED_BY_UPDATE(trigdata-&gt;tg_event))
  44. rettuple = trigdata-&gt;tg_newtuple;
  45. else
  46. rettuple = trigdata-&gt;tg_trigtuple;
  47. /* check for null values */
  48. if (!TRIGGER_FIRED_BY_DELETE(trigdata-&gt;tg_event)
  49. &amp;&amp; TRIGGER_FIRED_BEFORE(trigdata-&gt;tg_event))
  50. checknull = true;
  51. if (TRIGGER_FIRED_BEFORE(trigdata-&gt;tg_event))
  52. when = "before";
  53. else
  54. when = "after ";
  55. tupdesc = trigdata-&gt;tg_relation-&gt;rd_att;
  56. /* connect to SPI manager */
  57. if ((ret = SPI_connect()) &lt; 0)
  58. elog(ERROR, "trigf (fired %s): SPI_connect returned %d", when, ret);
  59. /* get number of rows in table */
  60. ret = SPI_exec("SELECT count(*) FROM ttest", 0);
  61. if (ret &lt; 0)
  62. elog(ERROR, "trigf (fired %s): SPI_exec returned %d", when, ret);
  63. /* count(*) returns int8, so be careful to convert */
  64. i = DatumGetInt64(SPI_getbinval(SPI_tuptable-&gt;vals[0],
  65. SPI_tuptable-&gt;tupdesc,
  66. 1,
  67. &amp;isnull));
  68. elog (INFO, "trigf (fired %s): there are %d rows in ttest", when, i);
  69. SPI_finish();
  70. if (checknull)
  71. {
  72. SPI_getbinval(rettuple, tupdesc, 1, &amp;isnull);
  73. if (isnull)
  74. rettuple = NULL;
  75. }
  76. return PointerGetDatum(rettuple);
  77. }
  78. </pre><p>
  79. </p><p>
  80. After you have compiled the source code (see <a class="xref" href="xfunc-c.html#DFUNC" title="37.10.5. Compiling and Linking Dynamically-Loaded Functions">Section 37.10.5</a>), declare the function and the triggers:
  81. </p><pre class="programlisting">
  82. CREATE FUNCTION trigf() RETURNS trigger
  83. AS '<em class="replaceable"><code>filename</code></em>'
  84. LANGUAGE C;
  85. CREATE TRIGGER tbefore BEFORE INSERT OR UPDATE OR DELETE ON ttest
  86. FOR EACH ROW EXECUTE FUNCTION trigf();
  87. CREATE TRIGGER tafter AFTER INSERT OR UPDATE OR DELETE ON ttest
  88. FOR EACH ROW EXECUTE FUNCTION trigf();
  89. </pre><p>
  90. </p><p>
  91. Now you can test the operation of the trigger:
  92. </p><pre class="screen">
  93. =&gt; INSERT INTO ttest VALUES (NULL);
  94. INFO: trigf (fired before): there are 0 rows in ttest
  95. INSERT 0 0
  96. -- Insertion skipped and AFTER trigger is not fired
  97. =&gt; SELECT * FROM ttest;
  98. x
  99. ---
  100. (0 rows)
  101. =&gt; INSERT INTO ttest VALUES (1);
  102. INFO: trigf (fired before): there are 0 rows in ttest
  103. INFO: trigf (fired after ): there are 1 rows in ttest
  104. ^^^^^^^^
  105. remember what we said about visibility.
  106. INSERT 167793 1
  107. vac=&gt; SELECT * FROM ttest;
  108. x
  109. ---
  110. 1
  111. (1 row)
  112. =&gt; INSERT INTO ttest SELECT x * 2 FROM ttest;
  113. INFO: trigf (fired before): there are 1 rows in ttest
  114. INFO: trigf (fired after ): there are 2 rows in ttest
  115. ^^^^^^
  116. remember what we said about visibility.
  117. INSERT 167794 1
  118. =&gt; SELECT * FROM ttest;
  119. x
  120. ---
  121. 1
  122. 2
  123. (2 rows)
  124. =&gt; UPDATE ttest SET x = NULL WHERE x = 2;
  125. INFO: trigf (fired before): there are 2 rows in ttest
  126. UPDATE 0
  127. =&gt; UPDATE ttest SET x = 4 WHERE x = 2;
  128. INFO: trigf (fired before): there are 2 rows in ttest
  129. INFO: trigf (fired after ): there are 2 rows in ttest
  130. UPDATE 1
  131. vac=&gt; SELECT * FROM ttest;
  132. x
  133. ---
  134. 1
  135. 4
  136. (2 rows)
  137. =&gt; DELETE FROM ttest;
  138. INFO: trigf (fired before): there are 2 rows in ttest
  139. INFO: trigf (fired before): there are 1 rows in ttest
  140. INFO: trigf (fired after ): there are 0 rows in ttest
  141. INFO: trigf (fired after ): there are 0 rows in ttest
  142. ^^^^^^
  143. remember what we said about visibility.
  144. DELETE 2
  145. =&gt; SELECT * FROM ttest;
  146. x
  147. ---
  148. (0 rows)
  149. </pre><p>
  150. </p><p>
  151. There are more complex examples in
  152. <code class="filename">src/test/regress/regress.c</code> and
  153. in <a class="xref" href="contrib-spi.html" title="F.36. spi">spi</a>.
  154. </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="triggers.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="event-triggers.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">38.3. Writing Trigger Functions in C </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 39. Event Triggers</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1