3
.\" Author: The PostgreSQL Global Development Group
4
.\" Generator: DocBook XSL Stylesheets v1.75.1 <http://docbook.sf.net/>
6
.\" Manual: PostgreSQL 9.1beta1 Documentation
7
.\" Source: PostgreSQL 9.1beta1
10
.TH "CREATE RULE" "7" "2011-04-27" "PostgreSQL 9.1beta1" "PostgreSQL 9.1beta1 Documentation"
11
.\" -----------------------------------------------------------------
12
.\" * set default formatting
13
.\" -----------------------------------------------------------------
14
.\" disable hyphenation
16
.\" disable justification (adjust text to left margin only)
18
.\" -----------------------------------------------------------------
19
.\" * MAIN CONTENT STARTS HERE *
20
.\" -----------------------------------------------------------------
22
CREATE_RULE \- define a new rewrite rule
27
CREATE [ OR REPLACE ] RULE \fIname\fR AS ON \fIevent\fR
28
TO \fItable\fR [ WHERE \fIcondition\fR ]
29
DO [ ALSO | INSTEAD ] { NOTHING | \fIcommand\fR | ( \fIcommand\fR ; \fIcommand\fR \&.\&.\&. ) }
34
defines a new rule applying to a specified table or view\&.
35
CREATE OR REPLACE RULE
36
will either create a new rule, or replace an existing rule of the same name for the same table\&.
40
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
42
rule can replace a given command by another, or cause a command not to be executed at all\&. Rules are used to implement table 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 commands 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
43
Chapter 37, The Rule System, in the documentation\&.
47
rules must be unconditional
49
rules and must have actions that consist of a single
53
rule effectively turns the table into a view, whose visible contents are the rows returned by the rule\(aqs
55
command rather than whatever had been stored in the table (if anything)\&. It is considered better style to write a
57
command than to create a real table and define an
61
You can create the illusion of an updatable view by defining
65
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
67
and so on, then be sure to put a suitable
69
clause into each of these rules\&. Alternatively, an updatable view can be implemented using
72
CREATE TRIGGER (\fBCREATE_TRIGGER\fR(7)))\&.
74
There is a catch if you try to use conditional rules for view updates: there
78
rule for each action you wish to allow on the view\&. If the rule is conditional, or is not
79
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
81
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
83
action\&. (This method does not currently work to support
90
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\&.
104
The name (optionally schema\-qualified) of the table or view the rule applies to\&.
111
conditional expression (returning
112
boolean)\&. The condition expression cannot refer to any tables except
115
OLD, and cannot contain aggregate functions\&.
121
indicates that the commands should be executed
123
the original command\&.
129
indicates that the commands should be executed
131
the original command\&.
144
The command or commands that make up the rule action\&. Valid commands are
155
\fIcommand\fR, the special table names
159
can be used to refer to values in the referenced table\&.
165
rules to refer to the new row being inserted or updated\&.
171
rules to refer to the existing row being updated or deleted\&.
174
You must be the owner of a table to create or change rules for it\&.
180
on a view, you can add a
182
clause that emits the view\(aqs columns\&. This clause will be used to compute the outputs if the rule is triggered by an
186
command respectively\&. When the rule is triggered by a command without
187
RETURNING, the rule\(aqs
189
clause will be ignored\&. The current implementation allows only unconditional
192
RETURNING; furthermore there can be at most one
194
clause among all the rules for the same event\&. (This ensures that there is only one candidate
196
clause to be used to compute the results\&.)
198
queries on the view will be rejected if there is no
200
clause in any available rule\&.
202
It is very important to take care to avoid circular rules\&. For example, though each of the following two rule definitions are accepted by
207
to report an error because of recursive expansion of a rule:
213
CREATE RULE "_RETURN" AS
218
CREATE RULE "_RETURN" AS
229
Presently, if a rule action contains a
233
command will be executed unconditionally, that is, the
235
will be issued even if there are not any rows that the rule should apply to\&. For example, in:
241
CREATE RULE notify_me AS ON UPDATE TO mytable DO ALSO NOTIFY mytable;
243
UPDATE mytable SET name = \(aqfoo\(aq WHERE id = 42;
251
event will be sent during the
252
UPDATE, whether or not there are any rows that match the condition
253
id = 42\&. This is an implementation restriction that might be fixed in future releases\&.
259
language extension, as is the entire query rewrite system\&.