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