create_rule.sgml 9.78 KB
Newer Older
1
<!--
Tatsuo Ishii's avatar
Tatsuo Ishii committed
2
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_rule.sgml,v 1.27 2001/09/14 08:19:55 ishii Exp $
3 4 5
Postgres documentation
-->

6 7
<refentry id="SQL-CREATERULE">
 <refmeta>
8
  <refentrytitle id="sql-createrule-title">CREATE RULE</refentrytitle>
9 10 11 12
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
 <refnamediv>
  <refname>
13
   CREATE RULE
14 15
  </refname>
  <refpurpose>
16
   define a new rewrite rule
17
  </refpurpose>
18
 </refnamediv>
19 20
 <refsynopsisdiv>
  <refsynopsisdivinfo>
21
   <date>2001-01-05</date>
22 23 24
  </refsynopsisdivinfo>
  <synopsis>
CREATE RULE <replaceable class="parameter">name</replaceable> AS ON <replaceable class="parameter">event</replaceable>
25
    TO <replaceable class="parameter">object</replaceable> [ WHERE <replaceable class="parameter">condition</replaceable> ]
26 27 28 29 30 31 32 33 34 35 36
    DO [ INSTEAD ] <replaceable class="parameter">action</replaceable>

where <replaceable class="PARAMETER">action</replaceable> can be:

NOTHING
|
<replaceable class="parameter">query</replaceable>
|
( <replaceable class="parameter">query</replaceable> ; <replaceable class="parameter">query</replaceable> ... )
|
[ <replaceable class="parameter">query</replaceable> ; <replaceable class="parameter">query</replaceable> ... ]
37 38 39 40
  </synopsis>

  <refsect2 id="R2-SQL-CREATERULE-1">
   <refsect2info>
41
    <date>2001-01-05</date>
42 43
   </refsect2info>
   <title>
44
    Inputs
45 46
   </title>
   <para>
47

48 49 50 51 52 53 54 55 56 57 58 59 60
    <variablelist>
     <varlistentry>
      <term><replaceable class="parameter">name</replaceable></term>
      <listitem>
       <para>
	The name of a rule to create.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">event</replaceable></term>
      <listitem>
       <para>
61 62 63
	Event is one of <literal>SELECT</literal>,
	<literal>UPDATE</literal>, <literal>DELETE</literal>
	or <literal>INSERT</literal>.
64 65 66 67 68 69 70 71 72
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">object</replaceable></term>
      <listitem>
       <para>
	Object is either <replaceable class="parameter">table</replaceable>
	or <replaceable class="parameter">table</replaceable>.<replaceable
73 74 75
	 class="parameter">column</replaceable>.  (Currently, only the
	 <replaceable class="parameter">table</replaceable> form is
	 actually implemented.)
76 77 78 79 80 81 82
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="parameter">condition</replaceable></term>
      <listitem>
       <para>
83 84 85
	Any SQL boolean-condition expression.  The condition expression may not
	refer to any tables except <literal>new</literal> and
	<literal>old</literal>.
86 87 88 89
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
90
      <term><replaceable class="parameter">query</replaceable></term>
91 92
      <listitem>
       <para>
93 94 95 96 97
        The query or queries making up the
	<replaceable class="PARAMETER">action</replaceable>
	can be any SQL <literal>SELECT</literal>, <literal>INSERT</literal>,
	<literal>UPDATE</literal>, <literal>DELETE</literal>, or
	<literal>NOTIFY</literal> statement.
98 99 100 101 102
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
103 104 105 106 107 108 109 110 111 112 113 114

   <para>
    Within the <replaceable class="parameter">condition</replaceable>
    and <replaceable class="PARAMETER">action</replaceable>, the special
    table names <literal>new</literal> and <literal>old</literal> may be
    used to refer to values in the referenced table (the
    <replaceable class="parameter">object</replaceable>).
    <literal>new</literal> is valid in ON INSERT and ON UPDATE rules
    to refer to the new row being inserted or updated.
    <literal>old</literal> is valid in ON SELECT, ON UPDATE, and ON DELETE
    rules to refer to the existing row being selected, updated, or deleted.
   </para>
115 116 117 118 119 120 121
  </refsect2>

  <refsect2 id="R2-SQL-CREATERULE-2">
   <refsect2info>
    <date>1998-09-11</date>
   </refsect2info>
   <title>
122
    Outputs
123 124 125 126 127 128 129 130 131 132 133 134
   </title>
   <para>

    <variablelist>
     <varlistentry>
      <term><computeroutput>
CREATE
       </computeroutput></term>
      <listitem>
       <para>
	Message returned if the rule is successfully created.
       </para>
135
      </listitem>
136 137
     </varlistentry>
    </variablelist>
138
   </para>
139 140 141 142 143 144 145 146
  </refsect2>
 </refsynopsisdiv>

 <refsect1 id="R1-SQL-CREATERULE-1">
  <refsect1info>
   <date>1998-09-11</date>
  </refsect1info>
  <title>
147
   Description
148
  </title>
149 150 151 152

  <para>
   The <productname>Postgres</productname> 
   <firstterm>rule system</firstterm> allows one to define an
153
   alternate action to be performed on inserts, updates, or deletions
154 155
   from database tables. Rules are used to
   implement table views as well.
156 157
  </para>

158
  <para>
159 160
   The semantics of a rule is that at the time an individual instance (row)
   is
161
   accessed, inserted, updated, or deleted, there is an old instance (for
162
   selects, updates and deletes) and a new instance (for inserts and
163 164
   updates).  All the rules for the given event type and the given target
   object (table) are examined, in an unspecified order.  If the
165
   <replaceable class="parameter">condition</replaceable> specified in the
166
   WHERE clause (if any) is true, the 
167
   <replaceable class="parameter">action</replaceable> part of the rule is
168 169
   executed.  The <replaceable class="parameter">action</replaceable> is
   done instead of the original query if INSTEAD is specified; otherwise
170 171
   it is done after the original query in the case of ON INSERT, or before
   the original query in the case of ON UPDATE or ON DELETE.
172 173 174
   Within both the <replaceable class="parameter">condition</replaceable>
   and <replaceable class="parameter">action</replaceable>, values from
   fields in the old instance and/or the new instance are substituted for
175
   <literal>old.</literal><replaceable class="parameter">attribute-name</replaceable>
176 177
   and <literal>new.</literal><replaceable class="parameter">attribute-name</replaceable>.
  </para>
178

179 180 181 182 183 184 185 186 187 188 189 190
  <para>
   The <replaceable class="parameter">action</replaceable> part of the rule
   can consist of one or more queries.  To write multiple queries, surround
   them with either parentheses or square brackets.  Such queries will be
   performed in the specified order (whereas there are no guarantees about
   the execution order of multiple rules for an object).  The
   <replaceable class="parameter">action</replaceable> can also be NOTHING
   indicating no action.  Thus, a DO INSTEAD NOTHING rule suppresses the
   original query from executing (when its condition is true); a DO NOTHING
   rule is useless.
  </para>

191 192 193 194 195 196
  <para>
   The <replaceable class="parameter">action</replaceable> part of the rule
   executes with the same command and transaction identifier as the user
   command that caused activation.
  </para>
  
197 198
  <refsect2 id="R2-SQL-CREATERULE-3">
   <refsect2info>
199
    <date>2001-01-05</date>
200 201
   </refsect2info>
   <title>
202
    Notes
203
   </title>
204
   <para>
205 206 207 208 209 210 211 212
    Presently, ON SELECT rules must be unconditional INSTEAD rules and must
    have actions that consist of a single SELECT query.  Thus, an ON SELECT
    rule effectively turns the object table into a view, whose visible
    contents are the rows returned by the rule's SELECT query rather than
    whatever had been stored in the table (if anything).  It is considered
    better style to write a CREATE VIEW command than to create a table and
    define an ON SELECT rule for it.
   </para>
213

214
   <para>
215
    You must have rule definition access to a table in order
216 217
    to define a rule on it. Use <command>GRANT</command>
    and <command>REVOKE</command> to change permissions.
218
   </para>
219

220
   <para>
221
    It is very important to take care to avoid circular rules.
222
    For example, though each
223
    of the following two rule definitions are accepted by
224
    <productname>Postgres</productname>, the
225 226
    select command will cause <productname>Postgres</productname> to 
    report an error because the query cycled too many times:
227

228
    <example>
Bruce Momjian's avatar
Bruce Momjian committed
229
     <title>Example of a circular rewrite rule combination:</title>
230
     <programlisting>
Tatsuo Ishii's avatar
Tatsuo Ishii committed
231
CREATE RULE "_RETemp" AS
232
    ON SELECT TO emp
Bruce Momjian's avatar
Bruce Momjian committed
233
    DO INSTEAD 
234
	SELECT * FROM toyemp;
235
     </programlisting>
236

237
     <programlisting>
Tatsuo Ishii's avatar
Tatsuo Ishii committed
238
CREATE RULE "_RETtoyemp" AS
239
    ON SELECT TO toyemp
Bruce Momjian's avatar
Bruce Momjian committed
240
    DO INSTEAD 
241
	SELECT * FROM emp;
242
     </programlisting>
243
     <para>
244 245
      This attempt to select from EMP will cause
      <productname>Postgres</productname> to issue an error
Bruce Momjian's avatar
Bruce Momjian committed
246
      because the queries cycled too many times:
247
      <programlisting>
248
SELECT * FROM emp;
249 250 251
      </programlisting></para>
    </example>
   </para>
252 253 254 255 256 257 258 259 260 261 262 263 264 265

   <para>
    Presently, if a rule contains a NOTIFY query, the NOTIFY will be executed
    unconditionally --- that is, the NOTIFY will be issued even if there are
    not any rows that the rule should apply to.  For example, in
      <programlisting>
CREATE RULE notify_me AS ON UPDATE TO mytable DO NOTIFY mytable;

UPDATE mytable SET name = 'foo' WHERE id = 42;
      </programlisting>
    one NOTIFY event will be sent during the UPDATE, whether or not there
    are any rows with id = 42.  This is an implementation restriction that
    may be fixed in future releases.
   </para>
266
  </refsect2>
267
 </refsect1>
268 269 270

 <refsect1 id="R1-SQL-CREATERULE-4">
  <title>
271
   Compatibility
272 273 274 275 276 277 278
  </title>

  <refsect2 id="R2-SQL-CREATERULE-4">
   <refsect2info>
    <date>1998-09-11</date>
   </refsect2info>
   <title>
279
    SQL92
280
   </title>
281

282
   <para>
283 284
    <command>CREATE RULE</command> statement is a <productname>Postgres</productname>
    language extension.
285
    There is no <command>CREATE RULE</command> statement in <acronym>SQL92</acronym>.
286 287 288
   </para>
  </refsect2>
 </refsect1>
289
</refentry>
290 291 292 293

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
294
sgml-omittag:nil
295 296 297 298 299 300 301 302 303 304 305
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
306
-->