2 39.7. Rules Versus Triggers #
4 Many things that can be done using triggers can also be implemented
5 using the PostgreSQL rule system. One of the things that cannot be
6 implemented by rules are some kinds of constraints, especially foreign
7 keys. It is possible to place a qualified rule that rewrites a command
8 to NOTHING if the value of a column does not appear in another table.
9 But then the data is silently thrown away and that's not a good idea.
10 If checks for valid values are required, and in the case of an invalid
11 value an error message should be generated, it must be done by a
14 In this chapter, we focused on using rules to update views. All of the
15 update rule examples in this chapter can also be implemented using
16 INSTEAD OF triggers on the views. Writing such triggers is often easier
17 than writing rules, particularly if complex logic is required to
20 For the things that can be implemented by both, which is best depends
21 on the usage of the database. A trigger is fired once for each affected
22 row. A rule modifies the query or generates an additional query. So if
23 many rows are affected in one statement, a rule issuing one extra
24 command is likely to be faster than a trigger that is called for every
25 single row and must re-determine what to do many times. However, the
26 trigger approach is conceptually far simpler than the rule approach,
27 and is easier for novices to get right.
29 Here we show an example of how the choice of rules versus triggers
30 plays out in one situation. There are two tables:
31 CREATE TABLE computer (
32 hostname text, -- indexed
33 manufacturer text -- indexed
36 CREATE TABLE software (
37 software text, -- indexed
38 hostname text -- indexed
41 Both tables have many thousands of rows and the indexes on hostname are
42 unique. The rule or trigger should implement a constraint that deletes
43 rows from software that reference a deleted computer. The trigger would
45 DELETE FROM software WHERE hostname = $1;
47 Since the trigger is called for each individual row deleted from
48 computer, it can prepare and save the plan for this command and pass
49 the hostname value in the parameter. The rule would be written as:
50 CREATE RULE computer_del AS ON DELETE TO computer
51 DO DELETE FROM software WHERE hostname = OLD.hostname;
53 Now we look at different types of deletes. In the case of a:
54 DELETE FROM computer WHERE hostname = 'mypc.local.net';
56 the table computer is scanned by index (fast), and the command issued
57 by the trigger would also use an index scan (also fast). The extra
58 command from the rule would be:
59 DELETE FROM software WHERE computer.hostname = 'mypc.local.net'
60 AND software.hostname = computer.hostname;
62 Since there are appropriate indexes set up, the planner will create a
65 -> Index Scan using comp_hostidx on computer
66 -> Index Scan using soft_hostidx on software
68 So there would be not that much difference in speed between the trigger
69 and the rule implementation.
71 With the next delete we want to get rid of all the 2000 computers where
72 the hostname starts with old. There are two possible commands to do
74 DELETE FROM computer WHERE hostname >= 'old'
77 The command added by the rule will be:
78 DELETE FROM software WHERE computer.hostname >= 'old' AND computer.hostname < 'o
80 AND software.hostname = computer.hostname;
84 -> Seq Scan on software
86 -> Index Scan using comp_hostidx on computer
88 The other possible command is:
89 DELETE FROM computer WHERE hostname ~ '^old';
91 which results in the following executing plan for the command added by
94 -> Index Scan using comp_hostidx on computer
95 -> Index Scan using soft_hostidx on software
97 This shows, that the planner does not realize that the qualification
98 for hostname in computer could also be used for an index scan on
99 software when there are multiple qualification expressions combined
100 with AND, which is what it does in the regular-expression version of
101 the command. The trigger will get invoked once for each of the 2000 old
102 computers that have to be deleted, and that will result in one index
103 scan over computer and 2000 index scans over software. The rule
104 implementation will do it with two commands that use indexes. And it
105 depends on the overall size of the table software whether the rule will
106 still be faster in the sequential scan situation. 2000 command
107 executions from the trigger over the SPI manager take some time, even
108 if all the index blocks will soon be in the cache.
110 The last command we look at is:
111 DELETE FROM computer WHERE manufacturer = 'bim';
113 Again this could result in many rows to be deleted from computer. So
114 the trigger will again run many commands through the executor. The
115 command generated by the rule will be:
116 DELETE FROM software WHERE computer.manufacturer = 'bim'
117 AND software.hostname = computer.hostname;
119 The plan for that command will again be the nested loop over two index
120 scans, only using a different index on computer:
122 -> Index Scan using comp_manufidx on computer
123 -> Index Scan using soft_hostidx on software
125 In any of these cases, the extra commands from the rule system will be
126 more or less independent from the number of affected rows in a command.
128 The summary is, rules will only be significantly slower than triggers
129 if their actions result in large and badly qualified joins, a situation
130 where the planner fails.