~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to doc/src/sgml/ref/create_rule.sgml

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!--
 
2
$PostgreSQL: pgsql/doc/src/sgml/ref/create_rule.sgml,v 1.45 2005-01-04 00:39:53 tgl Exp $
 
3
PostgreSQL documentation
 
4
-->
 
5
 
 
6
<refentry id="SQL-CREATERULE">
 
7
 <refmeta>
 
8
  <refentrytitle id="sql-createrule-title">CREATE RULE</refentrytitle>
 
9
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 
10
 </refmeta>
 
11
 
 
12
 <refnamediv>
 
13
  <refname>CREATE RULE</refname>
 
14
  <refpurpose>define a new rewrite rule</refpurpose>
 
15
 </refnamediv>
 
16
 
 
17
 <indexterm zone="sql-createrule">
 
18
  <primary>CREATE RULE</primary>
 
19
 </indexterm>
 
20
 
 
21
 <refsynopsisdiv>
 
22
<synopsis>
 
23
CREATE [ OR REPLACE ] RULE <replaceable class="parameter">name</replaceable> AS ON <replaceable class="parameter">event</replaceable>
 
24
    TO <replaceable class="parameter">table</replaceable> [ WHERE <replaceable class="parameter">condition</replaceable> ]
 
25
    DO [ ALSO | INSTEAD ] { NOTHING | <replaceable class="parameter">command</replaceable> | ( <replaceable class="parameter">command</replaceable> ; <replaceable class="parameter">command</replaceable> ... ) }
 
26
</synopsis>
 
27
 </refsynopsisdiv>
 
28
 
 
29
 <refsect1>
 
30
  <title>Description</title>
 
31
 
 
32
  <para>
 
33
   <command>CREATE RULE</command> defines a new rule applying to a specified
 
34
   table or view.
 
35
   <command>CREATE OR REPLACE RULE</command> will either create a
 
36
   new rule, or replace an existing rule of the same name for the same
 
37
   table.
 
38
  </para>
 
39
 
 
40
  <para>
 
41
   The <productname>PostgreSQL</productname> rule system allows one to
 
42
   define an alternate action to be performed on insertions, updates,
 
43
   or deletions in database tables.  Roughly speaking, a rule causes
 
44
   additional commands to be executed when a given command on a given
 
45
   table is executed.  Alternatively, an <literal>INSTEAD</literal>
 
46
   rule can replace a given command by another, or cause a command
 
47
   not to be executed at all.  Rules are used to implement table
 
48
   views as well.  It is important to realize that a rule is really
 
49
   a command transformation mechanism, or command macro.  The
 
50
   transformation happens before the execution of the commands starts.
 
51
   If you actually want an operation that fires independently for each
 
52
   physical row, you probably want to use a trigger, not a rule.
 
53
   More information about the rules system is in <xref linkend="rules">.
 
54
  </para>
 
55
  
 
56
  <para>
 
57
   Presently, <literal>ON SELECT</literal> rules must be unconditional
 
58
   <literal>INSTEAD</literal> rules and must have actions that consist
 
59
   of a single <command>SELECT</command> command.  Thus, an
 
60
   <literal>ON SELECT</literal> rule effectively turns the table into
 
61
   a view, whose visible contents are the rows returned by the rule's
 
62
   <command>SELECT</command> command rather than whatever had been
 
63
   stored in the table (if anything).  It is considered better style
 
64
   to write a <command>CREATE VIEW</command> command than to create a
 
65
   real table and define an <literal>ON SELECT</literal> rule for it.
 
66
  </para>
 
67
 
 
68
  <para>
 
69
   You can create the illusion of an updatable view by defining
 
70
   <literal>ON INSERT</literal>, <literal>ON UPDATE</literal>, and
 
71
   <literal>ON DELETE</literal> rules (or any subset of those that's
 
72
   sufficient for your purposes) to replace update actions on the view
 
73
   with appropriate updates on other tables.
 
74
  </para>
 
75
 
 
76
  <para>
 
77
   There is a catch if you try to use conditional rules for view
 
78
   updates: there <emphasis>must</> be an unconditional
 
79
   <literal>INSTEAD</literal> rule for each action you wish to allow
 
80
   on the view.  If the rule is conditional, or is not
 
81
   <literal>INSTEAD</literal>, then the system will still reject
 
82
   attempts to perform the update action, because it thinks it might
 
83
   end up trying to perform the action on the dummy table of the view
 
84
   in some cases.  If you want to handle all the useful cases in
 
85
   conditional rules, add an unconditional <literal>DO
 
86
   INSTEAD NOTHING</literal> rule to ensure that the system
 
87
   understands it will never be called on to update the dummy table.
 
88
   Then make the conditional rules non-<literal>INSTEAD</literal>; in
 
89
   the cases where they are applied, they add to the default
 
90
   <literal>INSTEAD NOTHING</literal> action.
 
91
  </para>
 
92
 </refsect1>
 
93
 
 
94
 <refsect1>
 
95
  <title>Parameters</title>
 
96
 
 
97
  <variablelist>
 
98
   <varlistentry>
 
99
    <term><replaceable class="parameter">name</replaceable></term>
 
100
    <listitem>
 
101
     <para>
 
102
      The name of a rule to create.  This must be distinct from the
 
103
      name of any other rule for the same table.  Multiple rules on
 
104
      the same table and same event type are applied in alphabetical
 
105
      name order.
 
106
     </para>
 
107
    </listitem>
 
108
   </varlistentry>
 
109
 
 
110
   <varlistentry>
 
111
    <term><replaceable class="parameter">event</replaceable></term>
 
112
    <listitem>
 
113
     <para>
 
114
      The event is one of <literal>SELECT</literal>,
 
115
      <literal>INSERT</literal>, <literal>UPDATE</literal>, or
 
116
      <literal>DELETE</literal>.
 
117
     </para>
 
118
    </listitem>
 
119
   </varlistentry>
 
120
 
 
121
   <varlistentry>
 
122
    <term><replaceable class="parameter">table</replaceable></term>
 
123
    <listitem>
 
124
     <para>
 
125
      The name (optionally schema-qualified) of the table or view the
 
126
      rule applies to.
 
127
     </para>
 
128
    </listitem>
 
129
   </varlistentry>
 
130
 
 
131
   <varlistentry>
 
132
    <term><replaceable class="parameter">condition</replaceable></term>
 
133
    <listitem>
 
134
     <para>
 
135
      Any <acronym>SQL</acronym> conditional expression (returning
 
136
      <type>boolean</type>).  The condition expression may not refer
 
137
      to any tables except <literal>NEW</> and <literal>OLD</>, and
 
138
      may not contain aggregate functions.
 
139
     </para>
 
140
    </listitem>
 
141
   </varlistentry>
 
142
 
 
143
   <varlistentry>
 
144
    <term><option>INSTEAD</option></term>
 
145
    <listitem>
 
146
     <para>
 
147
      <literal>INSTEAD</literal> indicates that the commands should be
 
148
      executed <emphasis>instead of</> the original command.
 
149
     </para>
 
150
    </listitem>
 
151
   </varlistentry>
 
152
 
 
153
   <varlistentry>
 
154
    <term><option>ALSO</option></term>
 
155
    <listitem>
 
156
     <para>
 
157
      <literal>ALSO</literal> indicates that the commands should be
 
158
      executed <emphasis>in addition to</emphasis> the original
 
159
      command.
 
160
     </para>
 
161
 
 
162
     <para>
 
163
      If neither <literal>ALSO</literal> nor
 
164
      <literal>INSTEAD</literal> is specified, <literal>ALSO</literal>
 
165
      is the default.
 
166
     </para>
 
167
    </listitem>
 
168
   </varlistentry>
 
169
 
 
170
   <varlistentry>
 
171
    <term><replaceable class="parameter">command</replaceable></term>
 
172
    <listitem>
 
173
     <para>
 
174
      The command or commands that make up the rule action.  Valid
 
175
      commands are <command>SELECT</command>,
 
176
      <command>INSERT</command>, <command>UPDATE</command>,
 
177
      <command>DELETE</command>, or <command>NOTIFY</command>.
 
178
     </para>
 
179
    </listitem>
 
180
   </varlistentry>
 
181
  </variablelist>
 
182
 
 
183
  <para>
 
184
   Within <replaceable class="parameter">condition</replaceable> and
 
185
   <replaceable class="parameter">command</replaceable>, the special
 
186
   table names <literal>NEW</literal> and <literal>OLD</literal> may
 
187
   be used to refer to values in the referenced table.
 
188
   <literal>NEW</literal> is valid in <literal>ON INSERT</literal> and
 
189
   <literal>ON UPDATE</literal> rules to refer to the new row being
 
190
   inserted or updated.  <literal>OLD</literal> is valid in
 
191
   <literal>ON UPDATE</literal> and <literal>ON DELETE</literal> rules
 
192
   to refer to the existing row being updated or deleted.
 
193
  </para>
 
194
 </refsect1>
 
195
 
 
196
 <refsect1>
 
197
  <title>Notes</title>
 
198
 
 
199
  <para>
 
200
   You must have the privilege <literal>RULE</literal> on a table to
 
201
   be allowed to define a rule on it.
 
202
  </para>
 
203
 
 
204
  <para>
 
205
   It is very important to take care to avoid circular rules.  For
 
206
   example, though each of the following two rule definitions are
 
207
   accepted by <productname>PostgreSQL</productname>, the
 
208
   <command>SELECT</command> command would cause
 
209
   <productname>PostgreSQL</productname> to report an error because
 
210
   the query cycled too many times:
 
211
 
 
212
<programlisting>
 
213
CREATE RULE "_RETURN" AS
 
214
    ON SELECT TO t1
 
215
    DO INSTEAD 
 
216
        SELECT * FROM t2;
 
217
 
 
218
CREATE RULE "_RETURN" AS
 
219
    ON SELECT TO t2
 
220
    DO INSTEAD 
 
221
        SELECT * FROM t1;
 
222
 
 
223
SELECT * FROM t1;
 
224
</programlisting>
 
225
  </para>
 
226
 
 
227
  <para>
 
228
   Presently, if a rule action contains a <command>NOTIFY</command>
 
229
   command, the <command>NOTIFY</command> command will be executed
 
230
   unconditionally, that is, the <command>NOTIFY</command> will be
 
231
   issued even if there are not any rows that the rule should apply
 
232
   to.  For example, in
 
233
<programlisting>
 
234
CREATE RULE notify_me AS ON UPDATE TO mytable DO ALSO NOTIFY mytable;
 
235
 
 
236
UPDATE mytable SET name = 'foo' WHERE id = 42;
 
237
</programlisting>
 
238
   one <command>NOTIFY</command> event will be sent during the
 
239
   <command>UPDATE</command>, whether or not there are any rows that
 
240
   match the condition <literal>id = 42</literal>.  This is an
 
241
   implementation restriction that may be fixed in future releases.
 
242
  </para>
 
243
 </refsect1>
 
244
 
 
245
 <refsect1>
 
246
  <title>Compatibility</title>
 
247
 
 
248
  <para>
 
249
   <command>CREATE RULE</command> is a
 
250
   <productname>PostgreSQL</productname> language extension, as is the
 
251
   entire query rewrite system.
 
252
  </para>
 
253
 </refsect1>
 
254
</refentry>
 
255
 
 
256
<!-- Keep this comment at the end of the file
 
257
Local variables:
 
258
mode: sgml
 
259
sgml-omittag:nil
 
260
sgml-shorttag:t
 
261
sgml-minimize-attributes:nil
 
262
sgml-always-quote-attributes:t
 
263
sgml-indent-step:1
 
264
sgml-indent-data:t
 
265
sgml-parent-document:nil
 
266
sgml-default-dtd-file:"../reference.ced"
 
267
sgml-exposed-tags:nil
 
268
sgml-local-catalogs:"/usr/lib/sgml/catalog"
 
269
sgml-local-ecat-files:nil
 
270
End:
 
271
-->