1CREATE RULE(7) PostgreSQL 9.2.24 Documentation CREATE RULE(7)
2
3
4
6 CREATE_RULE - define a new rewrite rule
7
9 CREATE [ OR REPLACE ] RULE name AS ON event
10 TO table_name [ WHERE condition ]
11 DO [ ALSO | INSTEAD ] { NOTHING | command | ( command ; command ... ) }
12
14 CREATE RULE defines a new rule applying to a specified table or view.
15 CREATE OR REPLACE RULE will either create a new rule, or replace an
16 existing rule of the same name for the same table.
17
18 The PostgreSQL rule system allows one to define an alternative action
19 to be performed on insertions, updates, or deletions in database
20 tables. Roughly speaking, a rule causes additional commands to be
21 executed when a given command on a given table is executed.
22 Alternatively, an INSTEAD rule can replace a given command by another,
23 or cause a command not to be executed at all. Rules are used to
24 implement table views as well. It is important to realize that a rule
25 is really a command transformation mechanism, or command macro. The
26 transformation happens before the execution of the commands starts. If
27 you actually want an operation that fires independently for each
28 physical row, you probably want to use a trigger, not a rule. More
29 information about the rules system is in Chapter 37, The Rule System,
30 in the documentation.
31
32 Presently, ON SELECT rules must be unconditional INSTEAD rules and must
33 have actions that consist of a single SELECT command. Thus, an ON
34 SELECT rule effectively turns the table into a view, whose visible
35 contents are the rows returned by the rule's SELECT command rather than
36 whatever had been stored in the table (if anything). It is considered
37 better style to write a CREATE VIEW command than to create a real table
38 and define an ON SELECT rule for it.
39
40 You can create the illusion of an updatable view by defining ON INSERT,
41 ON UPDATE, and ON DELETE rules (or any subset of those that's
42 sufficient for your purposes) to replace update actions on the view
43 with appropriate updates on other tables. If you want to support INSERT
44 RETURNING and so on, then be sure to put a suitable RETURNING clause
45 into each of these rules. Alternatively, an updatable view can be
46 implemented using INSTEAD OF triggers (see CREATE TRIGGER
47 (CREATE_TRIGGER(7))).
48
49 There is a catch if you try to use conditional rules for view updates:
50 there must be an unconditional INSTEAD rule for each action you wish to
51 allow on the view. If the rule is conditional, or is not INSTEAD, then
52 the system will still reject attempts to perform the update action,
53 because it thinks it might end up trying to perform the action on the
54 dummy table of the view in some cases. If you want to handle all the
55 useful cases in conditional rules, add an unconditional DO INSTEAD
56 NOTHING rule to ensure that the system understands it will never be
57 called on to update the dummy table. Then make the conditional rules
58 non-INSTEAD; in the cases where they are applied, they add to the
59 default INSTEAD NOTHING action. (This method does not currently work to
60 support RETURNING queries, however.)
61
63 name
64 The name of a rule to create. This must be distinct from the name
65 of any other rule for the same table. Multiple rules on the same
66 table and same event type are applied in alphabetical name order.
67
68 event
69 The event is one of SELECT, INSERT, UPDATE, or DELETE.
70
71 table_name
72 The name (optionally schema-qualified) of the table or view the
73 rule applies to.
74
75 condition
76 Any SQL conditional expression (returning boolean). The condition
77 expression cannot refer to any tables except NEW and OLD, and
78 cannot contain aggregate functions.
79
80 INSTEAD
81 INSTEAD indicates that the commands should be executed instead of
82 the original command.
83
84 ALSO
85 ALSO indicates that the commands should be executed in addition to
86 the original command.
87
88 If neither ALSO nor INSTEAD is specified, ALSO is the default.
89
90 command
91 The command or commands that make up the rule action. Valid
92 commands are SELECT, INSERT, UPDATE, DELETE, or NOTIFY.
93
94 Within condition and command, the special table names NEW and OLD can
95 be used to refer to values in the referenced table. NEW is valid in ON
96 INSERT and ON UPDATE rules to refer to the new row being inserted or
97 updated. OLD is valid in ON UPDATE and ON DELETE rules to refer to the
98 existing row being updated or deleted.
99
101 You must be the owner of a table to create or change rules for it.
102
103 In a rule for INSERT, UPDATE, or DELETE on a view, you can add a
104 RETURNING clause that emits the view's columns. This clause will be
105 used to compute the outputs if the rule is triggered by an INSERT
106 RETURNING, UPDATE RETURNING, or DELETE RETURNING command respectively.
107 When the rule is triggered by a command without RETURNING, the rule's
108 RETURNING clause will be ignored. The current implementation allows
109 only unconditional INSTEAD rules to contain RETURNING; furthermore
110 there can be at most one RETURNING clause among all the rules for the
111 same event. (This ensures that there is only one candidate RETURNING
112 clause to be used to compute the results.) RETURNING queries on the
113 view will be rejected if there is no RETURNING clause in any available
114 rule.
115
116 It is very important to take care to avoid circular rules. For example,
117 though each of the following two rule definitions are accepted by
118 PostgreSQL, the SELECT command would cause PostgreSQL to report an
119 error because of recursive expansion of a rule:
120
121 CREATE RULE "_RETURN" AS
122 ON SELECT TO t1
123 DO INSTEAD
124 SELECT * FROM t2;
125
126 CREATE RULE "_RETURN" AS
127 ON SELECT TO t2
128 DO INSTEAD
129 SELECT * FROM t1;
130
131 SELECT * FROM t1;
132
133 Presently, if a rule action contains a NOTIFY command, the NOTIFY
134 command will be executed unconditionally, that is, the NOTIFY will be
135 issued even if there are not any rows that the rule should apply to.
136 For example, in:
137
138 CREATE RULE notify_me AS ON UPDATE TO mytable DO ALSO NOTIFY mytable;
139
140 UPDATE mytable SET name = 'foo' WHERE id = 42;
141
142 one NOTIFY event will be sent during the UPDATE, whether or not there
143 are any rows that match the condition id = 42. This is an
144 implementation restriction that might be fixed in future releases.
145
147 CREATE RULE is a PostgreSQL language extension, as is the entire query
148 rewrite system.
149
150
151
152PostgreSQL 9.2.24 2017-11-06 CREATE RULE(7)