2 9.29. Trigger Functions #
4 While many uses of triggers involve user-written trigger functions,
5 PostgreSQL provides a few built-in trigger functions that can be used
6 directly in user-defined triggers. These are summarized in Table 9.110.
7 (Additional built-in trigger functions exist, which implement foreign
8 key constraints and deferred index constraints. Those are not
9 documented here since users need not use them directly.)
11 For more information about creating triggers, see CREATE TRIGGER.
13 Table 9.110. Built-In Trigger Functions
21 suppress_redundant_updates_trigger ( ) → trigger
23 Suppresses do-nothing update operations. See below for details.
25 CREATE TRIGGER ... suppress_redundant_updates_trigger()
27 tsvector_update_trigger ( ) → trigger
29 Automatically updates a tsvector column from associated plain-text
30 document column(s). The text search configuration to use is specified
31 by name as a trigger argument. See Section 12.4.3 for details.
33 CREATE TRIGGER ... tsvector_update_trigger(tsvcol,
34 'pg_catalog.swedish', title, body)
36 tsvector_update_trigger_column ( ) → trigger
38 Automatically updates a tsvector column from associated plain-text
39 document column(s). The text search configuration to use is taken from
40 a regconfig column of the table. See Section 12.4.3 for details.
42 CREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, tsconfigcol,
45 The suppress_redundant_updates_trigger function, when applied as a
46 row-level BEFORE UPDATE trigger, will prevent any update that does not
47 actually change the data in the row from taking place. This overrides
48 the normal behavior which always performs a physical row update
49 regardless of whether or not the data has changed. (This normal
50 behavior makes updates run faster, since no checking is required, and
51 is also useful in certain cases.)
53 Ideally, you should avoid running updates that don't actually change
54 the data in the record. Redundant updates can cost considerable
55 unnecessary time, especially if there are lots of indexes to alter, and
56 space in dead rows that will eventually have to be vacuumed. However,
57 detecting such situations in client code is not always easy, or even
58 possible, and writing expressions to detect them can be error-prone. An
59 alternative is to use suppress_redundant_updates_trigger, which will
60 skip updates that don't change the data. You should use this with care,
61 however. The trigger takes a small but non-trivial time for each
62 record, so if most of the records affected by updates do actually
63 change, use of this trigger will make updates run slower on average.
65 The suppress_redundant_updates_trigger function can be added to a table
67 CREATE TRIGGER z_min_update
68 BEFORE UPDATE ON tablename
69 FOR EACH ROW EXECUTE FUNCTION suppress_redundant_updates_trigger();
71 In most cases, you need to fire this trigger last for each row, so that
72 it does not override other triggers that might wish to alter the row.
73 Bearing in mind that triggers fire in name order, you would therefore
74 choose a trigger name that comes after the name of any other trigger
75 you might have on the table. (Hence the “z” prefix in the example.)