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.

291 line
21KB

  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.1. Overview of Trigger Behavior</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="triggers.html" title="Chapter 38. Triggers" /><link rel="next" href="trigger-datachanges.html" title="38.2. Visibility of Data Changes" /></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.1. Overview of Trigger Behavior</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="triggers.html" title="Chapter 38. Triggers">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-datachanges.html" title="38.2. Visibility of Data Changes">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TRIGGER-DEFINITION"><div class="titlepage"><div><div><h2 class="title" style="clear: both">38.1. Overview of Trigger Behavior</h2></div></div></div><p>
  3. A trigger is a specification that the database should automatically
  4. execute a particular function whenever a certain type of operation is
  5. performed. Triggers can be attached to tables (partitioned or not),
  6. views, and foreign tables.
  7. </p><p>
  8. On tables and foreign tables, triggers can be defined to execute either
  9. before or after any <code class="command">INSERT</code>, <code class="command">UPDATE</code>,
  10. or <code class="command">DELETE</code> operation, either once per modified row,
  11. or once per <acronym class="acronym">SQL</acronym> statement.
  12. <code class="command">UPDATE</code> triggers can moreover be set to fire only if
  13. certain columns are mentioned in the <code class="literal">SET</code> clause of
  14. the <code class="command">UPDATE</code> statement. Triggers can also fire
  15. for <code class="command">TRUNCATE</code> statements. If a trigger event occurs,
  16. the trigger's function is called at the appropriate time to handle the
  17. event.
  18. </p><p>
  19. On views, triggers can be defined to execute instead of
  20. <code class="command">INSERT</code>, <code class="command">UPDATE</code>, or
  21. <code class="command">DELETE</code> operations.
  22. Such <code class="literal">INSTEAD OF</code> triggers
  23. are fired once for each row that needs to be modified in the view.
  24. It is the responsibility of the
  25. trigger's function to perform the necessary modifications to the view's
  26. underlying base table(s) and, where appropriate, return the modified
  27. row as it will appear in the view. Triggers on views can also be defined
  28. to execute once per <acronym class="acronym">SQL</acronym> statement, before or after
  29. <code class="command">INSERT</code>, <code class="command">UPDATE</code>, or
  30. <code class="command">DELETE</code> operations.
  31. However, such triggers are fired only if there is also
  32. an <code class="literal">INSTEAD OF</code> trigger on the view. Otherwise,
  33. any statement targeting the view must be rewritten into a statement
  34. affecting its underlying base table(s), and then the triggers
  35. that will be fired are the ones attached to the base table(s).
  36. </p><p>
  37. The trigger function must be defined before the trigger itself can be
  38. created. The trigger function must be declared as a
  39. function taking no arguments and returning type <code class="literal">trigger</code>.
  40. (The trigger function receives its input through a specially-passed
  41. <code class="structname">TriggerData</code> structure, not in the form of ordinary function
  42. arguments.)
  43. </p><p>
  44. Once a suitable trigger function has been created, the trigger is
  45. established with
  46. <a class="xref" href="sql-createtrigger.html" title="CREATE TRIGGER"><span class="refentrytitle">CREATE TRIGGER</span></a>.
  47. The same trigger function can be used for multiple triggers.
  48. </p><p>
  49. <span class="productname">PostgreSQL</span> offers both <em class="firstterm">per-row</em>
  50. triggers and <em class="firstterm">per-statement</em> triggers. With a per-row
  51. trigger, the trigger function
  52. is invoked once for each row that is affected by the statement
  53. that fired the trigger. In contrast, a per-statement trigger is
  54. invoked only once when an appropriate statement is executed,
  55. regardless of the number of rows affected by that statement. In
  56. particular, a statement that affects zero rows will still result
  57. in the execution of any applicable per-statement triggers. These
  58. two types of triggers are sometimes called <em class="firstterm">row-level</em>
  59. triggers and <em class="firstterm">statement-level</em> triggers,
  60. respectively. Triggers on <code class="command">TRUNCATE</code> may only be
  61. defined at statement level, not per-row.
  62. </p><p>
  63. Triggers are also classified according to whether they fire
  64. <em class="firstterm">before</em>, <em class="firstterm">after</em>, or
  65. <em class="firstterm">instead of</em> the operation. These are referred to
  66. as <code class="literal">BEFORE</code> triggers, <code class="literal">AFTER</code> triggers, and
  67. <code class="literal">INSTEAD OF</code> triggers respectively.
  68. Statement-level <code class="literal">BEFORE</code> triggers naturally fire before the
  69. statement starts to do anything, while statement-level <code class="literal">AFTER</code>
  70. triggers fire at the very end of the statement. These types of
  71. triggers may be defined on tables, views, or foreign tables. Row-level
  72. <code class="literal">BEFORE</code> triggers fire immediately before a particular row is
  73. operated on, while row-level <code class="literal">AFTER</code> triggers fire at the end of
  74. the statement (but before any statement-level <code class="literal">AFTER</code> triggers).
  75. These types of triggers may only be defined on tables and
  76. foreign tables, not views; <code class="literal">BEFORE</code> row-level triggers may not
  77. be defined on partitioned tables.
  78. <code class="literal">INSTEAD OF</code> triggers may only be
  79. defined on views, and only at row level; they fire immediately as each
  80. row in the view is identified as needing to be operated on.
  81. </p><p>
  82. A statement that targets a parent table in an inheritance or partitioning
  83. hierarchy does not cause the statement-level triggers of affected child
  84. tables to be fired; only the parent table's statement-level triggers are
  85. fired. However, row-level triggers of any affected child tables will be
  86. fired.
  87. </p><p>
  88. If an <code class="command">INSERT</code> contains an <code class="literal">ON CONFLICT
  89. DO UPDATE</code> clause, it is possible that the effects of
  90. row-level <code class="literal">BEFORE</code> <code class="command">INSERT</code> triggers and
  91. row-level <code class="literal">BEFORE</code> <code class="command">UPDATE</code> triggers can
  92. both be applied in a way that is apparent from the final state of
  93. the updated row, if an <code class="varname">EXCLUDED</code> column is referenced.
  94. There need not be an <code class="varname">EXCLUDED</code> column reference for
  95. both sets of row-level <code class="literal">BEFORE</code> triggers to execute,
  96. though. The
  97. possibility of surprising outcomes should be considered when there
  98. are both <code class="literal">BEFORE</code> <code class="command">INSERT</code> and
  99. <code class="literal">BEFORE</code> <code class="command">UPDATE</code> row-level triggers
  100. that change a row being inserted/updated (this can be
  101. problematic even if the modifications are more or less equivalent, if
  102. they're not also idempotent). Note that statement-level
  103. <code class="command">UPDATE</code> triggers are executed when <code class="literal">ON
  104. CONFLICT DO UPDATE</code> is specified, regardless of whether or not
  105. any rows were affected by the <code class="command">UPDATE</code> (and
  106. regardless of whether the alternative <code class="command">UPDATE</code>
  107. path was ever taken). An <code class="command">INSERT</code> with an
  108. <code class="literal">ON CONFLICT DO UPDATE</code> clause will execute
  109. statement-level <code class="literal">BEFORE</code> <code class="command">INSERT</code>
  110. triggers first, then statement-level <code class="literal">BEFORE</code>
  111. <code class="command">UPDATE</code> triggers, followed by statement-level
  112. <code class="literal">AFTER</code> <code class="command">UPDATE</code> triggers and finally
  113. statement-level <code class="literal">AFTER</code> <code class="command">INSERT</code>
  114. triggers.
  115. </p><p>
  116. If an <code class="command">UPDATE</code> on a partitioned table causes a row to move
  117. to another partition, it will be performed as a <code class="command">DELETE</code>
  118. from the original partition followed by an <code class="command">INSERT</code> into
  119. the new partition. In this case, all row-level <code class="literal">BEFORE</code>
  120. <code class="command">UPDATE</code> triggers and all row-level
  121. <code class="literal">BEFORE</code> <code class="command">DELETE</code> triggers are fired on
  122. the original partition. Then all row-level <code class="literal">BEFORE</code>
  123. <code class="command">INSERT</code> triggers are fired on the destination partition.
  124. The possibility of surprising outcomes should be considered when all these
  125. triggers affect the row being moved. As far as <code class="literal">AFTER ROW</code>
  126. triggers are concerned, <code class="literal">AFTER</code> <code class="command">DELETE</code>
  127. and <code class="literal">AFTER</code> <code class="command">INSERT</code> triggers are
  128. applied; but <code class="literal">AFTER</code> <code class="command">UPDATE</code> triggers
  129. are not applied because the <code class="command">UPDATE</code> has been converted to
  130. a <code class="command">DELETE</code> and an <code class="command">INSERT</code>. As far as
  131. statement-level triggers are concerned, none of the
  132. <code class="command">DELETE</code> or <code class="command">INSERT</code> triggers are fired,
  133. even if row movement occurs; only the <code class="command">UPDATE</code> triggers
  134. defined on the target table used in the <code class="command">UPDATE</code> statement
  135. will be fired.
  136. </p><p>
  137. Trigger functions invoked by per-statement triggers should always
  138. return <code class="symbol">NULL</code>. Trigger functions invoked by per-row
  139. triggers can return a table row (a value of
  140. type <code class="structname">HeapTuple</code>) to the calling executor,
  141. if they choose. A row-level trigger fired before an operation has
  142. the following choices:
  143. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  144. It can return <code class="symbol">NULL</code> to skip the operation for the
  145. current row. This instructs the executor to not perform the
  146. row-level operation that invoked the trigger (the insertion,
  147. modification, or deletion of a particular table row).
  148. </p></li><li class="listitem"><p>
  149. For row-level <code class="command">INSERT</code>
  150. and <code class="command">UPDATE</code> triggers only, the returned row
  151. becomes the row that will be inserted or will replace the row
  152. being updated. This allows the trigger function to modify the
  153. row being inserted or updated.
  154. </p></li></ul></div><p>
  155. A row-level <code class="literal">BEFORE</code> trigger that does not intend to cause
  156. either of these behaviors must be careful to return as its result the same
  157. row that was passed in (that is, the <code class="varname">NEW</code> row
  158. for <code class="command">INSERT</code> and <code class="command">UPDATE</code>
  159. triggers, the <code class="varname">OLD</code> row for
  160. <code class="command">DELETE</code> triggers).
  161. </p><p>
  162. A row-level <code class="literal">INSTEAD OF</code> trigger should either return
  163. <code class="symbol">NULL</code> to indicate that it did not modify any data from
  164. the view's underlying base tables, or it should return the view
  165. row that was passed in (the <code class="varname">NEW</code> row
  166. for <code class="command">INSERT</code> and <code class="command">UPDATE</code>
  167. operations, or the <code class="varname">OLD</code> row for
  168. <code class="command">DELETE</code> operations). A nonnull return value is
  169. used to signal that the trigger performed the necessary data
  170. modifications in the view. This will cause the count of the number
  171. of rows affected by the command to be incremented. For
  172. <code class="command">INSERT</code> and <code class="command">UPDATE</code> operations only, the trigger
  173. may modify the <code class="varname">NEW</code> row before returning it. This will
  174. change the data returned by
  175. <code class="command">INSERT RETURNING</code> or <code class="command">UPDATE RETURNING</code>,
  176. and is useful when the view will not show exactly the same data
  177. that was provided.
  178. </p><p>
  179. The return value is ignored for row-level triggers fired after an
  180. operation, and so they can return <code class="symbol">NULL</code>.
  181. </p><p>
  182. Some considerations apply for generated
  183. columns.<a id="id-1.8.4.5.15.1" class="indexterm"></a> Stored generated columns are computed after
  184. <code class="literal">BEFORE</code> triggers and before <code class="literal">AFTER</code>
  185. triggers. Therefore, the generated value can be inspected in
  186. <code class="literal">AFTER</code> triggers. In <code class="literal">BEFORE</code> triggers,
  187. the <code class="literal">OLD</code> row contains the old generated value, as one
  188. would expect, but the <code class="literal">NEW</code> row does not yet contain the
  189. new generated value and should not be accessed. In the C language
  190. interface, the content of the column is undefined at this point; a
  191. higher-level programming language should prevent access to a stored
  192. generated column in the <code class="literal">NEW</code> row in a
  193. <code class="literal">BEFORE</code> trigger. Changes to the value of a generated
  194. column in a <code class="literal">BEFORE</code> trigger are ignored and will be
  195. overwritten.
  196. </p><p>
  197. If more than one trigger is defined for the same event on the same
  198. relation, the triggers will be fired in alphabetical order by
  199. trigger name. In the case of <code class="literal">BEFORE</code> and
  200. <code class="literal">INSTEAD OF</code> triggers, the possibly-modified row returned by
  201. each trigger becomes the input to the next trigger. If any
  202. <code class="literal">BEFORE</code> or <code class="literal">INSTEAD OF</code> trigger returns
  203. <code class="symbol">NULL</code>, the operation is abandoned for that row and subsequent
  204. triggers are not fired (for that row).
  205. </p><p>
  206. A trigger definition can also specify a Boolean <code class="literal">WHEN</code>
  207. condition, which will be tested to see whether the trigger should
  208. be fired. In row-level triggers the <code class="literal">WHEN</code> condition can
  209. examine the old and/or new values of columns of the row. (Statement-level
  210. triggers can also have <code class="literal">WHEN</code> conditions, although the feature
  211. is not so useful for them.) In a <code class="literal">BEFORE</code> trigger, the
  212. <code class="literal">WHEN</code>
  213. condition is evaluated just before the function is or would be executed,
  214. so using <code class="literal">WHEN</code> is not materially different from testing the
  215. same condition at the beginning of the trigger function. However, in
  216. an <code class="literal">AFTER</code> trigger, the <code class="literal">WHEN</code> condition is evaluated
  217. just after the row update occurs, and it determines whether an event is
  218. queued to fire the trigger at the end of statement. So when an
  219. <code class="literal">AFTER</code> trigger's
  220. <code class="literal">WHEN</code> condition does not return true, it is not necessary
  221. to queue an event nor to re-fetch the row at end of statement. This
  222. can result in significant speedups in statements that modify many
  223. rows, if the trigger only needs to be fired for a few of the rows.
  224. <code class="literal">INSTEAD OF</code> triggers do not support
  225. <code class="literal">WHEN</code> conditions.
  226. </p><p>
  227. Typically, row-level <code class="literal">BEFORE</code> triggers are used for checking or
  228. modifying the data that will be inserted or updated. For example,
  229. a <code class="literal">BEFORE</code> trigger might be used to insert the current time into a
  230. <code class="type">timestamp</code> column, or to check that two elements of the row are
  231. consistent. Row-level <code class="literal">AFTER</code> triggers are most sensibly
  232. used to propagate the updates to other tables, or make consistency
  233. checks against other tables. The reason for this division of labor is
  234. that an <code class="literal">AFTER</code> trigger can be certain it is seeing the final
  235. value of the row, while a <code class="literal">BEFORE</code> trigger cannot; there might
  236. be other <code class="literal">BEFORE</code> triggers firing after it. If you have no
  237. specific reason to make a trigger <code class="literal">BEFORE</code> or
  238. <code class="literal">AFTER</code>, the <code class="literal">BEFORE</code> case is more efficient, since
  239. the information about
  240. the operation doesn't have to be saved until end of statement.
  241. </p><p>
  242. If a trigger function executes SQL commands then these
  243. commands might fire triggers again. This is known as cascading
  244. triggers. There is no direct limitation on the number of cascade
  245. levels. It is possible for cascades to cause a recursive invocation
  246. of the same trigger; for example, an <code class="command">INSERT</code>
  247. trigger might execute a command that inserts an additional row
  248. into the same table, causing the <code class="command">INSERT</code> trigger
  249. to be fired again. It is the trigger programmer's responsibility
  250. to avoid infinite recursion in such scenarios.
  251. </p><p>
  252. <a id="id-1.8.4.5.20.1" class="indexterm"></a>
  253. When a trigger is being defined, arguments can be specified for
  254. it. The purpose of including arguments in the
  255. trigger definition is to allow different triggers with similar
  256. requirements to call the same function. As an example, there
  257. could be a generalized trigger function that takes as its
  258. arguments two column names and puts the current user in one and
  259. the current time stamp in the other. Properly written, this
  260. trigger function would be independent of the specific table it is
  261. triggering on. So the same function could be used for
  262. <code class="command">INSERT</code> events on any table with suitable
  263. columns, to automatically track creation of records in a
  264. transaction table for example. It could also be used to track
  265. last-update events if defined as an <code class="command">UPDATE</code>
  266. trigger.
  267. </p><p>
  268. Each programming language that supports triggers has its own method
  269. for making the trigger input data available to the trigger function.
  270. This input data includes the type of trigger event (e.g.,
  271. <code class="command">INSERT</code> or <code class="command">UPDATE</code>) as well as any
  272. arguments that were listed in <code class="command">CREATE TRIGGER</code>.
  273. For a row-level trigger, the input data also includes the
  274. <code class="varname">NEW</code> row for <code class="command">INSERT</code> and
  275. <code class="command">UPDATE</code> triggers, and/or the <code class="varname">OLD</code> row
  276. for <code class="command">UPDATE</code> and <code class="command">DELETE</code> triggers.
  277. </p><p>
  278. By default, statement-level triggers do not have any way to examine the
  279. individual row(s) modified by the statement. But an <code class="literal">AFTER
  280. STATEMENT</code> trigger can request that <em class="firstterm">transition tables</em>
  281. be created to make the sets of affected rows available to the trigger.
  282. <code class="literal">AFTER ROW</code> triggers can also request transition tables, so
  283. that they can see the total changes in the table as well as the change in
  284. the individual row they are currently being fired for. The method for
  285. examining the transition tables again depends on the programming language
  286. that is being used, but the typical approach is to make the transition
  287. tables act like read-only temporary tables that can be accessed by SQL
  288. commands issued within the trigger function.
  289. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="triggers.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-datachanges.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 38. Triggers </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 38.2. Visibility of Data Changes</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1