3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "CREATE RULE" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CREATE_RULE \- define a new rewrite rule
35 CREATE [ OR REPLACE ] RULE \fIname\fR AS ON \fIevent\fR
36 TO \fItable_name\fR [ WHERE \fIcondition\fR ]
37 DO [ ALSO | INSTEAD ] { NOTHING | \fIcommand\fR | ( \fIcommand\fR ; \fIcommand\fR \&.\&.\&. ) }
39 where \fIevent\fR can be one of:
41 SELECT | INSERT | UPDATE | DELETE
46 defines a new rule applying to a specified table or view\&.
47 \fBCREATE OR REPLACE RULE\fR
48 will either create a new rule, or replace an existing rule of the same name for the same table\&.
52 rule system allows one to define an alternative action to be performed on insertions, updates, or deletions in database tables\&. Roughly speaking, a rule causes additional commands to be executed when a given command on a given table is executed\&. Alternatively, an
54 rule can replace a given command by another, or cause a command not to be executed at all\&. Rules are used to implement SQL views as well\&. It is important to realize that a rule is really a command transformation mechanism, or command macro\&. The transformation happens before the execution of the command starts\&. If you actually want an operation that fires independently for each physical row, you probably want to use a trigger, not a rule\&. More information about the rules system is in
59 rules can only be attached to views\&. Such a rule must be named
60 "_RETURN", must be an unconditional
62 rule, and must have an action that consists of a single
64 command\&. This command defines the visible contents of the view\&. (The view itself is basically a dummy table with no storage\&.) It\*(Aqs best to regard such a rule as an implementation detail\&. While a view can be redefined via
65 CREATE OR REPLACE RULE "_RETURN" AS \&.\&.\&., it\*(Aqs better style to use
66 CREATE OR REPLACE VIEW\&.
68 You can create the illusion of an updatable view by defining
72 rules (or any subset of those that\*(Aqs sufficient for your purposes) to replace update actions on the view with appropriate updates on other tables\&. If you want to support
73 \fBINSERT RETURNING\fR
74 and so on, then be sure to put a suitable
76 clause into each of these rules\&.
78 There is a catch if you try to use conditional rules for complex view updates: there
82 rule for each action you wish to allow on the view\&. If the rule is conditional, or is not
83 INSTEAD, then the system will still reject attempts to perform the update action, because it thinks it might end up trying to perform the action on the dummy table of the view in some cases\&. If you want to handle all the useful cases in conditional rules, add an unconditional
85 rule to ensure that the system understands it will never be called on to update the dummy table\&. Then make the conditional rules non\-INSTEAD; in the cases where they are applied, they add to the default
87 action\&. (This method does not currently work to support
95 .nr an-no-space-flag 1
103 A view that is simple enough to be automatically updatable (see
104 CREATE VIEW (\fBCREATE_VIEW\fR(7))) does not require a user\-created rule in order to be updatable\&. While you can create an explicit rule anyway, the automatic update transformation will generally outperform an explicit rule\&.
106 Another alternative worth considering is to use
109 CREATE TRIGGER (\fBCREATE_TRIGGER\fR(7))) in place of rules\&.
116 The name of a rule to create\&. This must be distinct from the name of any other rule for the same table\&. Multiple rules on the same table and same event type are applied in alphabetical name order\&.
125 DELETE\&. Note that an
129 clause cannot be used on tables that have either
133 rules\&. Consider using an updatable view instead\&.
138 The name (optionally schema\-qualified) of the table or view the rule applies to\&.
145 conditional expression (returning
146 boolean)\&. The condition expression cannot refer to any tables except
149 OLD, and cannot contain aggregate functions\&.
155 indicates that the commands should be executed
157 the original command\&.
163 indicates that the commands should be executed
165 the original command\&.
178 The command or commands that make up the rule action\&. Valid commands are
189 \fIcommand\fR, the special table names
193 can be used to refer to values in the referenced table\&.
199 rules to refer to the new row being inserted or updated\&.
205 rules to refer to the existing row being updated or deleted\&.
208 You must be the owner of a table to create or change rules for it\&.
214 on a view, you can add a
216 clause that emits the view\*(Aqs columns\&. This clause will be used to compute the outputs if the rule is triggered by an
217 \fBINSERT RETURNING\fR,
218 \fBUPDATE RETURNING\fR, or
219 \fBDELETE RETURNING\fR
220 command respectively\&. When the rule is triggered by a command without
221 RETURNING, the rule\*(Aqs
223 clause will be ignored\&. The current implementation allows only unconditional
226 RETURNING; furthermore there can be at most one
228 clause among all the rules for the same event\&. (This ensures that there is only one candidate
230 clause to be used to compute the results\&.)
232 queries on the view will be rejected if there is no
234 clause in any available rule\&.
236 It is very important to take care to avoid circular rules\&. For example, though each of the following two rule definitions are accepted by
241 to report an error because of recursive expansion of a rule:
247 CREATE RULE "_RETURN" AS
252 CREATE RULE "_RETURN" AS
263 Presently, if a rule action contains a
267 command will be executed unconditionally, that is, the
269 will be issued even if there are not any rows that the rule should apply to\&. For example, in:
275 CREATE RULE notify_me AS ON UPDATE TO mytable DO ALSO NOTIFY mytable;
277 UPDATE mytable SET name = \*(Aqfoo\*(Aq WHERE id = 42;
285 event will be sent during the
286 \fBUPDATE\fR, whether or not there are any rows that match the condition
287 id = 42\&. This is an implementation restriction that might be fixed in future releases\&.
293 language extension, as is the entire query rewrite system\&.
295 ALTER RULE (\fBALTER_RULE\fR(7)), DROP RULE (\fBDROP_RULE\fR(7))