codekingpro/portable-devtools
115k
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.28. 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.27. System Administration Functions" /><link rel="next" href="functions-event-triggers.html" title="9.29. 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.28. Trigger Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-admin.html" title="9.27. 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 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="functions-event-triggers.html" title="9.29. 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.28. 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 trigger5 functions that can be used directly in user-defined triggers. These6 are summarized in <a class="xref" href="functions-trigger.html#BUILTIN-TRIGGERS-TABLE" title="Table 9.103. Built-In Trigger Functions">Table 9.103</a>.7 (Additional built-in trigger functions exist, which implement foreign8 key constraints and deferred index constraints. Those are not documented9 here since users need not use them directly.)10 </p><p>11 For more information about creating triggers, see12 <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.103. 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 Function15 </p>16 <p>17 Description18 </p>19 <p>20 Example Usage21 </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">22 <a id="id-1.5.8.34.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.34.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 associated38 plain-text document column(s). The text search configuration to use39 is specified by name as a trigger argument. See40 <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.34.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 associated51 plain-text document column(s). The text search configuration to use52 is taken from a <code class="type">regconfig</code> column of the table. See53 <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 the61 row from taking place. This overrides the normal behavior which always62 performs a physical row update63 regardless of whether or not the data has changed. (This normal behavior64 makes updates run faster, since no checking is required, and is also65 useful in certain cases.)66 </p><p>67 Ideally, you should avoid running updates that don't actually68 change the data in the record. Redundant updates can cost considerable69 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 not72 always easy, or even possible, and writing expressions to detect73 them can be error-prone. An alternative is to use74 <code class="function">suppress_redundant_updates_trigger</code>, which will skip75 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 be81 added to a table like this:82</p><pre class="programlisting">83CREATE TRIGGER z_min_update84BEFORE UPDATE ON tablename85FOR 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 that88 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 therefore90 choose a trigger name that comes after the name of any other trigger91 you might have on the table. (Hence the <span class="quote">“<span class="quote">z</span>”</span> prefix in the92 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.27. 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.29. Event Trigger Functions">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.27. System Administration Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 9.29. Event Trigger Functions</td></tr></table></div></body></html>