gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

168 行
12KB

  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.3. Writing Trigger Functions in C</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-datachanges.html" title="38.2. Visibility of Data Changes" /><link rel="next" href="trigger-example.html" title="38.4. A Complete Trigger Example" /></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.3. Writing Trigger Functions in C</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="trigger-datachanges.html" title="38.2. Visibility of Data Changes">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="trigger-example.html" title="38.4. A Complete Trigger Example">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TRIGGER-INTERFACE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">38.3. Writing Trigger Functions in C</h2></div></div></div><a id="id-1.8.4.7.2" class="indexterm"></a><a id="id-1.8.4.7.3" class="indexterm"></a><p>
  3. This section describes the low-level details of the interface to a
  4. trigger function. This information is only needed when writing
  5. trigger functions in C. If you are using a higher-level language then
  6. these details are handled for you. In most cases you should consider
  7. using a procedural language before writing your triggers in C. The
  8. documentation of each procedural language explains how to write a
  9. trigger in that language.
  10. </p><p>
  11. Trigger functions must use the <span class="quote">“<span class="quote">version 1</span>”</span> function manager
  12. interface.
  13. </p><p>
  14. When a function is called by the trigger manager, it is not passed
  15. any normal arguments, but it is passed a <span class="quote">“<span class="quote">context</span>”</span>
  16. pointer pointing to a <code class="structname">TriggerData</code> structure. C
  17. functions can check whether they were called from the trigger
  18. manager or not by executing the macro:
  19. </p><pre class="programlisting">
  20. CALLED_AS_TRIGGER(fcinfo)
  21. </pre><p>
  22. which expands to:
  23. </p><pre class="programlisting">
  24. ((fcinfo)-&gt;context != NULL &amp;&amp; IsA((fcinfo)-&gt;context, TriggerData))
  25. </pre><p>
  26. If this returns true, then it is safe to cast
  27. <code class="literal">fcinfo-&gt;context</code> to type <code class="literal">TriggerData
  28. *</code> and make use of the pointed-to
  29. <code class="structname">TriggerData</code> structure. The function must
  30. <span class="emphasis"><em>not</em></span> alter the <code class="structname">TriggerData</code>
  31. structure or any of the data it points to.
  32. </p><p>
  33. <code class="structname">struct TriggerData</code> is defined in
  34. <code class="filename">commands/trigger.h</code>:
  35. </p><pre class="programlisting">
  36. typedef struct TriggerData
  37. {
  38. NodeTag type;
  39. TriggerEvent tg_event;
  40. Relation tg_relation;
  41. HeapTuple tg_trigtuple;
  42. HeapTuple tg_newtuple;
  43. Trigger *tg_trigger;
  44. TupleTableSlot *tg_trigslot;
  45. TupleTableSlot *tg_newslot;
  46. Tuplestorestate *tg_oldtable;
  47. Tuplestorestate *tg_newtable;
  48. } TriggerData;
  49. </pre><p>
  50. where the members are defined as follows:
  51. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="structfield">type</code></span></dt><dd><p>
  52. Always <code class="literal">T_TriggerData</code>.
  53. </p></dd><dt><span class="term"><code class="structfield">tg_event</code></span></dt><dd><p>
  54. Describes the event for which the function is called. You can use the
  55. following macros to examine <code class="literal">tg_event</code>:
  56. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">TRIGGER_FIRED_BEFORE(tg_event)</code></span></dt><dd><p>
  57. Returns true if the trigger fired before the operation.
  58. </p></dd><dt><span class="term"><code class="literal">TRIGGER_FIRED_AFTER(tg_event)</code></span></dt><dd><p>
  59. Returns true if the trigger fired after the operation.
  60. </p></dd><dt><span class="term"><code class="literal">TRIGGER_FIRED_INSTEAD(tg_event)</code></span></dt><dd><p>
  61. Returns true if the trigger fired instead of the operation.
  62. </p></dd><dt><span class="term"><code class="literal">TRIGGER_FIRED_FOR_ROW(tg_event)</code></span></dt><dd><p>
  63. Returns true if the trigger fired for a row-level event.
  64. </p></dd><dt><span class="term"><code class="literal">TRIGGER_FIRED_FOR_STATEMENT(tg_event)</code></span></dt><dd><p>
  65. Returns true if the trigger fired for a statement-level event.
  66. </p></dd><dt><span class="term"><code class="literal">TRIGGER_FIRED_BY_INSERT(tg_event)</code></span></dt><dd><p>
  67. Returns true if the trigger was fired by an <code class="command">INSERT</code> command.
  68. </p></dd><dt><span class="term"><code class="literal">TRIGGER_FIRED_BY_UPDATE(tg_event)</code></span></dt><dd><p>
  69. Returns true if the trigger was fired by an <code class="command">UPDATE</code> command.
  70. </p></dd><dt><span class="term"><code class="literal">TRIGGER_FIRED_BY_DELETE(tg_event)</code></span></dt><dd><p>
  71. Returns true if the trigger was fired by a <code class="command">DELETE</code> command.
  72. </p></dd><dt><span class="term"><code class="literal">TRIGGER_FIRED_BY_TRUNCATE(tg_event)</code></span></dt><dd><p>
  73. Returns true if the trigger was fired by a <code class="command">TRUNCATE</code> command.
  74. </p></dd></dl></div><p>
  75. </p></dd><dt><span class="term"><code class="structfield">tg_relation</code></span></dt><dd><p>
  76. A pointer to a structure describing the relation that the trigger fired for.
  77. Look at <code class="filename">utils/rel.h</code> for details about
  78. this structure. The most interesting things are
  79. <code class="literal">tg_relation-&gt;rd_att</code> (descriptor of the relation
  80. tuples) and <code class="literal">tg_relation-&gt;rd_rel-&gt;relname</code>
  81. (relation name; the type is not <code class="type">char*</code> but
  82. <code class="type">NameData</code>; use
  83. <code class="literal">SPI_getrelname(tg_relation)</code> to get a <code class="type">char*</code> if you
  84. need a copy of the name).
  85. </p></dd><dt><span class="term"><code class="structfield">tg_trigtuple</code></span></dt><dd><p>
  86. A pointer to the row for which the trigger was fired. This is
  87. the row being inserted, updated, or deleted. If this trigger
  88. was fired for an <code class="command">INSERT</code> or
  89. <code class="command">DELETE</code> then this is what you should return
  90. from the function if you don't want to replace the row with
  91. a different one (in the case of <code class="command">INSERT</code>) or
  92. skip the operation. For triggers on foreign tables, values of system
  93. columns herein are unspecified.
  94. </p></dd><dt><span class="term"><code class="structfield">tg_newtuple</code></span></dt><dd><p>
  95. A pointer to the new version of the row, if the trigger was
  96. fired for an <code class="command">UPDATE</code>, and <code class="symbol">NULL</code> if
  97. it is for an <code class="command">INSERT</code> or a
  98. <code class="command">DELETE</code>. This is what you have to return
  99. from the function if the event is an <code class="command">UPDATE</code>
  100. and you don't want to replace this row by a different one or
  101. skip the operation. For triggers on foreign tables, values of system
  102. columns herein are unspecified.
  103. </p></dd><dt><span class="term"><code class="structfield">tg_trigger</code></span></dt><dd><p>
  104. A pointer to a structure of type <code class="structname">Trigger</code>,
  105. defined in <code class="filename">utils/reltrigger.h</code>:
  106. </p><pre class="programlisting">
  107. typedef struct Trigger
  108. {
  109. Oid tgoid;
  110. char *tgname;
  111. Oid tgfoid;
  112. int16 tgtype;
  113. char tgenabled;
  114. bool tgisinternal;
  115. Oid tgconstrrelid;
  116. Oid tgconstrindid;
  117. Oid tgconstraint;
  118. bool tgdeferrable;
  119. bool tginitdeferred;
  120. int16 tgnargs;
  121. int16 tgnattr;
  122. int16 *tgattr;
  123. char **tgargs;
  124. char *tgqual;
  125. char *tgoldtable;
  126. char *tgnewtable;
  127. } Trigger;
  128. </pre><p>
  129. where <code class="structfield">tgname</code> is the trigger's name,
  130. <code class="structfield">tgnargs</code> is the number of arguments in
  131. <code class="structfield">tgargs</code>, and <code class="structfield">tgargs</code> is an array of
  132. pointers to the arguments specified in the <code class="command">CREATE
  133. TRIGGER</code> statement. The other members are for internal use
  134. only.
  135. </p></dd><dt><span class="term"><code class="structfield">tg_trigslot</code></span></dt><dd><p>
  136. The slot containing <code class="structfield">tg_trigtuple</code>,
  137. or a <code class="symbol">NULL</code> pointer if there is no such tuple.
  138. </p></dd><dt><span class="term"><code class="structfield">tg_newslot</code></span></dt><dd><p>
  139. The slot containing <code class="structfield">tg_newtuple</code>,
  140. or a <code class="symbol">NULL</code> pointer if there is no such tuple.
  141. </p></dd><dt><span class="term"><code class="structfield">tg_oldtable</code></span></dt><dd><p>
  142. A pointer to a structure of type <code class="structname">Tuplestorestate</code>
  143. containing zero or more rows in the format specified by
  144. <code class="structfield">tg_relation</code>, or a <code class="symbol">NULL</code> pointer
  145. if there is no <code class="literal">OLD TABLE</code> transition relation.
  146. </p></dd><dt><span class="term"><code class="structfield">tg_newtable</code></span></dt><dd><p>
  147. A pointer to a structure of type <code class="structname">Tuplestorestate</code>
  148. containing zero or more rows in the format specified by
  149. <code class="structfield">tg_relation</code>, or a <code class="symbol">NULL</code> pointer
  150. if there is no <code class="literal">NEW TABLE</code> transition relation.
  151. </p></dd></dl></div><p>
  152. </p><p>
  153. To allow queries issued through SPI to reference transition tables, see
  154. <a class="xref" href="spi-spi-register-trigger-data.html" title="SPI_register_trigger_data"><span class="refentrytitle">SPI_register_trigger_data</span></a>.
  155. </p><p>
  156. A trigger function must return either a
  157. <code class="structname">HeapTuple</code> pointer or a <code class="symbol">NULL</code> pointer
  158. (<span class="emphasis"><em>not</em></span> an SQL null value, that is, do not set <em class="parameter"><code>isNull</code></em> true).
  159. Be careful to return either
  160. <code class="structfield">tg_trigtuple</code> or <code class="structfield">tg_newtuple</code>,
  161. as appropriate, if you don't want to modify the row being operated on.
  162. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="trigger-datachanges.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="trigger-example.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">38.2. Visibility of Data Changes </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 38.4. A Complete Trigger Example</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1