]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/functions-trigger.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / functions-trigger.html
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>9.29. Trigger Functions</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="functions-admin.html" title="9.28. System Administration Functions" /><link rel="next" href="functions-event-triggers.html" title="9.30. Event Trigger Functions" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">9.29. Trigger Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-admin.html" title="9.28. System Administration Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><th width="60%" align="center">Chapter 9. Functions and Operators</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="functions-event-triggers.html" title="9.30. Event Trigger Functions">Next</a></td></tr></table><hr /></div><div class="sect1" id="FUNCTIONS-TRIGGER"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.29. Trigger Functions <a href="#FUNCTIONS-TRIGGER" class="id_link">#</a></h2></div></div></div><p>
3    While many uses of triggers involve user-written trigger functions,
4    <span class="productname">PostgreSQL</span> provides a few built-in trigger
5    functions that can be used directly in user-defined triggers.  These
6    are summarized in <a class="xref" href="functions-trigger.html#BUILTIN-TRIGGERS-TABLE" title="Table 9.110. Built-In Trigger Functions">Table 9.110</a>.
7    (Additional built-in trigger functions exist, which implement foreign
8    key constraints and deferred index constraints.  Those are not documented
9    here since users need not use them directly.)
10   </p><p>
11    For more information about creating triggers, see
12    <a class="xref" href="sql-createtrigger.html" title="CREATE TRIGGER"><span class="refentrytitle">CREATE TRIGGER</span></a>.
13   </p><div class="table" id="BUILTIN-TRIGGERS-TABLE"><p class="title"><strong>Table 9.110. Built-In Trigger Functions</strong></p><div class="table-contents"><table class="table" summary="Built-In Trigger Functions" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
14         Function
15        </p>
16        <p>
17         Description
18        </p>
19        <p>
20         Example Usage
21        </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
22         <a id="id-1.5.8.35.4.2.2.1.1.1.1" class="indexterm"></a>
23         <code class="function">suppress_redundant_updates_trigger</code> ( )
24         → <code class="returnvalue">trigger</code>
25        </p>
26        <p>
27         Suppresses do-nothing update operations.  See below for details.
28        </p>
29        <p>
30         <code class="literal">CREATE TRIGGER ... suppress_redundant_updates_trigger()</code>
31        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
32         <a id="id-1.5.8.35.4.2.2.2.1.1.1" class="indexterm"></a>
33         <code class="function">tsvector_update_trigger</code> ( )
34         → <code class="returnvalue">trigger</code>
35        </p>
36        <p>
37         Automatically updates a <code class="type">tsvector</code> column from associated
38         plain-text document column(s).  The text search configuration to use
39         is specified by name as a trigger argument.  See
40         <a class="xref" href="textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS" title="12.4.3. Triggers for Automatic Updates">Section 12.4.3</a> for details.
41        </p>
42        <p>
43         <code class="literal">CREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body)</code>
44        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
45         <a id="id-1.5.8.35.4.2.2.3.1.1.1" class="indexterm"></a>
46         <code class="function">tsvector_update_trigger_column</code> ( )
47         → <code class="returnvalue">trigger</code>
48        </p>
49        <p>
50         Automatically updates a <code class="type">tsvector</code> column from associated
51         plain-text document column(s).  The text search configuration to use
52         is taken from a <code class="type">regconfig</code> column of the table.  See
53         <a class="xref" href="textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS" title="12.4.3. Triggers for Automatic Updates">Section 12.4.3</a> for details.
54        </p>
55        <p>
56         <code class="literal">CREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, tsconfigcol, title, body)</code>
57        </p></td></tr></tbody></table></div></div><br class="table-break" /><p>
58       The <code class="function">suppress_redundant_updates_trigger</code> function,
59       when applied as a row-level <code class="literal">BEFORE UPDATE</code> trigger,
60       will prevent any update that does not actually change the data in the
61       row from taking place.  This overrides the normal behavior which always
62       performs a physical row update
63       regardless of whether or not the data has changed. (This normal behavior
64       makes updates run faster, since no checking is required, and is also
65       useful in certain cases.)
66     </p><p>
67       Ideally, you should avoid running updates that don't actually
68       change the data in the record. Redundant updates can cost considerable
69       unnecessary time, especially if there are lots of indexes to alter,
70       and space in dead rows that will eventually have to be vacuumed.
71       However, detecting such situations in client code is not
72       always easy, or even possible, and writing expressions to detect
73       them can be error-prone. An alternative is to use
74       <code class="function">suppress_redundant_updates_trigger</code>, which will skip
75       updates that don't change the data. You should use this with care,
76       however. The trigger takes a small but non-trivial time for each record,
77       so if most of the records affected by updates do actually change,
78       use of this trigger will make updates run slower on average.
79     </p><p>
80       The <code class="function">suppress_redundant_updates_trigger</code> function can be
81       added to a table like this:
82 </p><pre class="programlisting">
83 CREATE TRIGGER z_min_update
84 BEFORE UPDATE ON tablename
85 FOR EACH ROW EXECUTE FUNCTION suppress_redundant_updates_trigger();
86 </pre><p>
87       In most cases, you need to fire this trigger last for each row, so that
88       it does not override other triggers that might wish to alter the row.
89       Bearing in mind that triggers fire in name order, you would therefore
90       choose a trigger name that comes after the name of any other trigger
91       you might have on the table.  (Hence the <span class="quote">“<span class="quote">z</span>”</span> prefix in the
92       example.)
93     </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-admin.html" title="9.28. System Administration Functions">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-event-triggers.html" title="9.30. Event Trigger Functions">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.28. System Administration Functions </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"> 9.30. Event Trigger Functions</td></tr></table></div></body></html>