]> begriffs open source - ai-pg/blob - full-docs/html/sql-createrule.html
Include links to all subsection html pages, with shorter paths too
[ai-pg] / full-docs / html / sql-createrule.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE RULE</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-createrole.html" title="CREATE ROLE" /><link rel="next" href="sql-createschema.html" title="CREATE SCHEMA" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE RULE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createrole.html" title="CREATE ROLE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createschema.html" title="CREATE SCHEMA">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-CREATERULE"><div class="titlepage"></div><a id="id-1.9.3.79.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE RULE</span></h2><p>CREATE RULE — define a new rewrite rule</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 CREATE [ OR REPLACE ] RULE <em class="replaceable"><code>name</code></em> AS ON <em class="replaceable"><code>event</code></em>
4     TO <em class="replaceable"><code>table_name</code></em> [ WHERE <em class="replaceable"><code>condition</code></em> ]
5     DO [ ALSO | INSTEAD ] { NOTHING | <em class="replaceable"><code>command</code></em> | ( <em class="replaceable"><code>command</code></em> ; <em class="replaceable"><code>command</code></em> ... ) }
6
7 <span class="phrase">where <em class="replaceable"><code>event</code></em> can be one of:</span>
8
9     SELECT | INSERT | UPDATE | DELETE
10 </pre></div><div class="refsect1" id="id-1.9.3.79.5"><h2>Description</h2><p>
11    <code class="command">CREATE RULE</code> defines a new rule applying to a specified
12    table or view.
13    <code class="command">CREATE OR REPLACE RULE</code> will either create a
14    new rule, or replace an existing rule of the same name for the same
15    table.
16   </p><p>
17    The <span class="productname">PostgreSQL</span> rule system allows one to
18    define an alternative action to be performed on insertions, updates,
19    or deletions in database tables.  Roughly speaking, a rule causes
20    additional commands to be executed when a given command on a given
21    table is executed.  Alternatively, an <code class="literal">INSTEAD</code>
22    rule can replace a given command by another, or cause a command
23    not to be executed at all.  Rules are used to implement SQL
24    views as well.  It is important to realize that a rule is really
25    a command transformation mechanism, or command macro.  The
26    transformation happens before the execution of the command starts.
27    If you actually want an operation that fires independently for each
28    physical row, you probably want to use a trigger, not a rule.
29    More information about the rules system is in <a class="xref" href="rules.html" title="Chapter 39. The Rule System">Chapter 39</a>.
30   </p><p>
31    Presently, <code class="literal">ON SELECT</code> rules can only be attached
32    to views.  Such a rule must be named <code class="literal">"_RETURN"</code>,
33    must be an unconditional <code class="literal">INSTEAD</code> rule, and must have
34    an action that consists of a single <code class="command">SELECT</code> command.
35    This command defines the visible contents of the view.  (The view
36    itself is basically a dummy table with no storage.)  It's best to
37    regard such a rule as an implementation detail.  While a view can be
38    redefined via <code class="literal">CREATE OR REPLACE RULE "_RETURN" AS
39    ...</code>, it's better style to use <code class="literal">CREATE OR REPLACE
40    VIEW</code>.
41   </p><p>
42    You can create the illusion of an updatable view by defining
43    <code class="literal">ON INSERT</code>, <code class="literal">ON UPDATE</code>, and
44    <code class="literal">ON DELETE</code> rules (or any subset of those that's
45    sufficient for your purposes) to replace update actions on the view
46    with appropriate updates on other tables.  If you want to support
47    <code class="command">INSERT RETURNING</code> and so on, then be sure to put a suitable
48    <code class="literal">RETURNING</code> clause into each of these rules.
49   </p><p>
50    There is a catch if you try to use conditional rules for complex view
51    updates: there <span class="emphasis"><em>must</em></span> be an unconditional
52    <code class="literal">INSTEAD</code> rule for each action you wish to allow
53    on the view.  If the rule is conditional, or is not
54    <code class="literal">INSTEAD</code>, then the system will still reject
55    attempts to perform the update action, because it thinks it might
56    end up trying to perform the action on the dummy table of the view
57    in some cases.  If you want to handle all the useful cases in
58    conditional rules, add an unconditional <code class="literal">DO
59    INSTEAD NOTHING</code> rule to ensure that the system
60    understands it will never be called on to update the dummy table.
61    Then make the conditional rules non-<code class="literal">INSTEAD</code>; in
62    the cases where they are applied, they add to the default
63    <code class="literal">INSTEAD NOTHING</code> action.  (This method does not
64    currently work to support <code class="literal">RETURNING</code> queries, however.)
65   </p><div class="note"><h3 class="title">Note</h3><p>
66     A view that is simple enough to be automatically updatable (see <a class="xref" href="sql-createview.html" title="CREATE VIEW"><span class="refentrytitle">CREATE VIEW</span></a>) does not require a user-created rule in
67     order to be updatable.  While you can create an explicit rule anyway,
68     the automatic update transformation will generally outperform an
69     explicit rule.
70    </p><p>
71     Another alternative worth considering is to use <code class="literal">INSTEAD OF</code>
72     triggers (see <a class="xref" href="sql-createtrigger.html" title="CREATE TRIGGER"><span class="refentrytitle">CREATE TRIGGER</span></a>) in place of rules.
73    </p></div></div><div class="refsect1" id="id-1.9.3.79.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
74       The name of a rule to create.  This must be distinct from the
75       name of any other rule for the same table.  Multiple rules on
76       the same table and same event type are applied in alphabetical
77       name order.
78      </p></dd><dt><span class="term"><em class="replaceable"><code>event</code></em></span></dt><dd><p>
79       The event is one of <code class="literal">SELECT</code>,
80       <code class="literal">INSERT</code>, <code class="literal">UPDATE</code>, or
81       <code class="literal">DELETE</code>.  Note that an
82       <code class="command">INSERT</code> containing an <code class="literal">ON
83       CONFLICT</code> clause cannot be used on tables that have
84       either <code class="literal">INSERT</code> or <code class="literal">UPDATE</code>
85       rules.  Consider using an updatable view instead.
86      </p></dd><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
87       The name (optionally schema-qualified) of the table or view the
88       rule applies to.
89      </p></dd><dt><span class="term"><em class="replaceable"><code>condition</code></em></span></dt><dd><p>
90       Any <acronym class="acronym">SQL</acronym> conditional expression (returning
91       <code class="type">boolean</code>).  The condition expression cannot refer
92       to any tables except <code class="literal">NEW</code> and <code class="literal">OLD</code>, and
93       cannot contain aggregate functions.
94      </p></dd><dt><span class="term"><code class="option">INSTEAD</code></span></dt><dd><p><code class="literal">INSTEAD</code> indicates that the commands should be
95       executed <span class="emphasis"><em>instead of</em></span> the original command.
96      </p></dd><dt><span class="term"><code class="option">ALSO</code></span></dt><dd><p><code class="literal">ALSO</code> indicates that the commands should be
97       executed <span class="emphasis"><em>in addition to</em></span> the original
98       command.
99      </p><p>
100       If neither <code class="literal">ALSO</code> nor
101       <code class="literal">INSTEAD</code> is specified, <code class="literal">ALSO</code>
102       is the default.
103      </p></dd><dt><span class="term"><em class="replaceable"><code>command</code></em></span></dt><dd><p>
104       The command or commands that make up the rule action.  Valid
105       commands are <code class="command">SELECT</code>,
106       <code class="command">INSERT</code>, <code class="command">UPDATE</code>,
107       <code class="command">DELETE</code>, or <code class="command">NOTIFY</code>.
108      </p></dd></dl></div><p>
109    Within <em class="replaceable"><code>condition</code></em> and
110    <em class="replaceable"><code>command</code></em>, the special
111    table names <code class="literal">NEW</code> and <code class="literal">OLD</code> can
112    be used to refer to values in the referenced table.
113    <code class="literal">NEW</code> is valid in <code class="literal">ON INSERT</code> and
114    <code class="literal">ON UPDATE</code> rules to refer to the new row being
115    inserted or updated.  <code class="literal">OLD</code> is valid in
116    <code class="literal">ON UPDATE</code> and <code class="literal">ON DELETE</code> rules
117    to refer to the existing row being updated or deleted.
118   </p></div><div class="refsect1" id="id-1.9.3.79.7"><h2>Notes</h2><p>
119    You must be the owner of a table to create or change rules for it.
120   </p><p>
121    In a rule for <code class="literal">INSERT</code>, <code class="literal">UPDATE</code>, or
122    <code class="literal">DELETE</code> on a view, you can add a <code class="literal">RETURNING</code>
123    clause that emits the view's columns.  This clause will be used to compute
124    the outputs if the rule is triggered by an <code class="command">INSERT RETURNING</code>,
125    <code class="command">UPDATE RETURNING</code>, or <code class="command">DELETE RETURNING</code> command
126    respectively.  When the rule is triggered by a command without
127    <code class="literal">RETURNING</code>, the rule's <code class="literal">RETURNING</code> clause will be
128    ignored.  The current implementation allows only unconditional
129    <code class="literal">INSTEAD</code> rules to contain <code class="literal">RETURNING</code>; furthermore
130    there can be at most one <code class="literal">RETURNING</code> clause among all the rules
131    for the same event.  (This ensures that there is only one candidate
132    <code class="literal">RETURNING</code> clause to be used to compute the results.)
133    <code class="literal">RETURNING</code> queries on the view will be rejected if
134    there is no <code class="literal">RETURNING</code> clause in any available rule.
135   </p><p>
136    It is very important to take care to avoid circular rules.  For
137    example, though each of the following two rule definitions are
138    accepted by <span class="productname">PostgreSQL</span>, the
139    <code class="command">SELECT</code> command would cause
140    <span class="productname">PostgreSQL</span> to report an error because
141    of recursive expansion of a rule:
142
143 </p><pre class="programlisting">
144 CREATE RULE "_RETURN" AS
145     ON SELECT TO t1
146     DO INSTEAD
147         SELECT * FROM t2;
148
149 CREATE RULE "_RETURN" AS
150     ON SELECT TO t2
151     DO INSTEAD
152         SELECT * FROM t1;
153
154 SELECT * FROM t1;
155 </pre><p>
156   </p><p>
157    Presently, if a rule action contains a <code class="command">NOTIFY</code>
158    command, the <code class="command">NOTIFY</code> command will be executed
159    unconditionally, that is, the <code class="command">NOTIFY</code> will be
160    issued even if there are not any rows that the rule should apply
161    to.  For example, in:
162 </p><pre class="programlisting">
163 CREATE RULE notify_me AS ON UPDATE TO mytable DO ALSO NOTIFY mytable;
164
165 UPDATE mytable SET name = 'foo' WHERE id = 42;
166 </pre><p>
167    one <code class="command">NOTIFY</code> event will be sent during the
168    <code class="command">UPDATE</code>, whether or not there are any rows that
169    match the condition <code class="literal">id = 42</code>.  This is an
170    implementation restriction that might be fixed in future releases.
171   </p></div><div class="refsect1" id="id-1.9.3.79.8"><h2>Compatibility</h2><p>
172    <code class="command">CREATE RULE</code> is a
173    <span class="productname">PostgreSQL</span> language extension, as is the
174    entire query rewrite system.
175   </p></div><div class="refsect1" id="id-1.9.3.79.9"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alterrule.html" title="ALTER RULE"><span class="refentrytitle">ALTER RULE</span></a>, <a class="xref" href="sql-droprule.html" title="DROP RULE"><span class="refentrytitle">DROP RULE</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createrole.html" title="CREATE ROLE">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createschema.html" title="CREATE SCHEMA">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE ROLE </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> CREATE SCHEMA</td></tr></table></div></body></html>