|
1 |
| -<!-- $PostgreSQL: pgsql/doc/src/sgml/rules.sgml,v 1.36 2004/11/15 06:32:14 neilc Exp $ --> |
| 1 | +<!-- $PostgreSQL: pgsql/doc/src/sgml/rules.sgml,v 1.37 2004/12/30 03:13:56 tgl Exp $ --> |
2 | 2 |
|
3 | 3 | <Chapter Id="rules">
|
4 | 4 | <Title>The Rule System</Title>
|
|
104 | 104 | <ListItem>
|
105 | 105 | <Para>
|
106 | 106 | The range table is a list of relations that are used in the query.
|
107 |
| -In a <command>SELECT</command> statement these are the relations given after |
108 |
| -the <literal>FROM</literal> key word. |
| 107 | +In a <command>SELECT</command> statement these are the relations given after |
| 108 | +the <literal>FROM</literal> key word. |
109 | 109 | </Para>
|
110 | 110 |
|
111 | 111 | <Para>
|
112 | 112 | Every range table entry identifies a table or view and tells
|
113 |
| -by which name it is called in the other parts of the query. |
114 |
| -In the query tree, the range table entries are referenced by |
115 |
| -number rather than by name, so here it doesn't matter if there |
116 |
| -are duplicate names as it would in an <Acronym>SQL</Acronym> |
117 |
| -statement. This can happen after the range tables of rules |
118 |
| -have been merged in. The examples in this chapter will not have |
119 |
| -this situation. |
| 113 | +by which name it is called in the other parts of the query. |
| 114 | +In the query tree, the range table entries are referenced by |
| 115 | +number rather than by name, so here it doesn't matter if there |
| 116 | +are duplicate names as it would in an <Acronym>SQL</Acronym> |
| 117 | +statement. This can happen after the range tables of rules |
| 118 | +have been merged in. The examples in this chapter will not have |
| 119 | +this situation. |
120 | 120 | </Para>
|
121 | 121 | </ListItem>
|
122 | 122 | </VarListEntry>
|
|
128 | 128 | <ListItem>
|
129 | 129 | <Para>
|
130 | 130 | This is an index into the range table that identifies the
|
131 |
| -relation where the results of the query go. |
| 131 | +relation where the results of the query go. |
132 | 132 | </Para>
|
133 | 133 |
|
134 | 134 | <Para>
|
135 |
| -<command>SELECT</command> queries normally don't have a result |
136 |
| -relation. The special case of a <command>SELECT INTO</command> is |
137 |
| -mostly identical to a <command>CREATE TABLE</command> followed by a |
138 |
| -<literal>INSERT ... SELECT</literal> and is not discussed |
139 |
| -separately here. |
| 135 | +<command>SELECT</command> queries normally don't have a result |
| 136 | +relation. The special case of a <command>SELECT INTO</command> is |
| 137 | +mostly identical to a <command>CREATE TABLE</command> followed by a |
| 138 | +<literal>INSERT ... SELECT</literal> and is not discussed |
| 139 | +separately here. |
140 | 140 | </Para>
|
141 | 141 |
|
142 | 142 | <Para>
|
143 | 143 | For <command>INSERT</command>, <command>UPDATE</command>, and
|
144 |
| -<command>DELETE</command> commands, the result relation is the table |
145 |
| -(or view!) where the changes take effect. |
| 144 | +<command>DELETE</command> commands, the result relation is the table |
| 145 | +(or view!) where the changes are to take effect. |
146 | 146 | </Para>
|
147 | 147 | </ListItem>
|
148 | 148 | </VarListEntry>
|
|
167 | 167 |
|
168 | 168 | <Para>
|
169 | 169 | <command>DELETE</command> commands don't need a target list
|
170 |
| -because they don't produce any result. In fact, the planner will |
171 |
| -add a special <acronym>CTID</> entry to the empty target list, but |
172 |
| -this is after the rule system and will be discussed later; for the |
173 |
| -rule system, the target list is empty. |
| 170 | +because they don't produce any result. In fact, the planner will |
| 171 | +add a special <acronym>CTID</> entry to the empty target list, but |
| 172 | +this is after the rule system and will be discussed later; for the |
| 173 | +rule system, the target list is empty. |
174 | 174 | </Para>
|
175 | 175 |
|
176 | 176 | <Para>
|
177 | 177 | For <command>INSERT</command> commands, the target list describes
|
178 |
| -the new rows that should go into the result relation. It consists of the |
179 |
| -expressions in the <literal>VALUES</> clause or the ones from the |
180 |
| -<command>SELECT</command> clause in <literal>INSERT |
181 |
| -... SELECT</literal>. The first step of the rewrite process adds |
182 |
| -target list entries for any columns that were not assigned to by |
183 |
| -the original command but have defaults. Any remaining columns (with |
184 |
| -neither a given value nor a default) will be filled in by the |
185 |
| -planner with a constant null expression. |
| 178 | +the new rows that should go into the result relation. It consists of the |
| 179 | +expressions in the <literal>VALUES</> clause or the ones from the |
| 180 | +<command>SELECT</command> clause in <literal>INSERT |
| 181 | +... SELECT</literal>. The first step of the rewrite process adds |
| 182 | +target list entries for any columns that were not assigned to by |
| 183 | +the original command but have defaults. Any remaining columns (with |
| 184 | +neither a given value nor a default) will be filled in by the |
| 185 | +planner with a constant null expression. |
186 | 186 | </Para>
|
187 | 187 |
|
188 | 188 | <Para>
|
189 | 189 | For <command>UPDATE</command> commands, the target list
|
190 |
| -describes the new rows that should replace the old ones. In the |
191 |
| -rule system, it contains just the expressions from the <literal>SET |
192 |
| -column = expression</literal> part of the command. The planner will handle |
193 |
| -missing columns by inserting expressions that copy the values from |
194 |
| -the old row into the new one. And it will add the special |
195 |
| -<acronym>CTID</> entry just as for <command>DELETE</command>, too. |
| 190 | +describes the new rows that should replace the old ones. In the |
| 191 | +rule system, it contains just the expressions from the <literal>SET |
| 192 | +column = expression</literal> part of the command. The planner will handle |
| 193 | +missing columns by inserting expressions that copy the values from |
| 194 | +the old row into the new one. And it will add the special |
| 195 | +<acronym>CTID</> entry just as for <command>DELETE</command>, too. |
196 | 196 | </Para>
|
197 | 197 |
|
198 | 198 | <Para>
|
199 | 199 | Every entry in the target list contains an expression that can
|
200 |
| -be a constant value, a variable pointing to a column of one |
201 |
| -of the relations in the range table, a parameter, or an expression |
202 |
| -tree made of function calls, constants, variables, operators, etc. |
| 200 | +be a constant value, a variable pointing to a column of one |
| 201 | +of the relations in the range table, a parameter, or an expression |
| 202 | +tree made of function calls, constants, variables, operators, etc. |
203 | 203 | </Para>
|
204 | 204 | </ListItem>
|
205 | 205 | </VarListEntry>
|
|
211 | 211 | <ListItem>
|
212 | 212 | <Para>
|
213 | 213 | The query's qualification is an expression much like one of
|
214 |
| -those contained in the target list entries. The result value of |
215 |
| -this expression is a Boolean that tells whether the operation |
216 |
| -(<command>INSERT</command>, <command>UPDATE</command>, |
217 |
| -<command>DELETE</command>, or <command>SELECT</command>) for the |
218 |
| -final result row should be executed or not. It corresponds to the <literal>WHERE</> clause |
219 |
| -of an <Acronym>SQL</Acronym> statement. |
| 214 | +those contained in the target list entries. The result value of |
| 215 | +this expression is a Boolean that tells whether the operation |
| 216 | +(<command>INSERT</command>, <command>UPDATE</command>, |
| 217 | +<command>DELETE</command>, or <command>SELECT</command>) for the |
| 218 | +final result row should be executed or not. It corresponds to the <literal>WHERE</> clause |
| 219 | +of an <Acronym>SQL</Acronym> statement. |
220 | 220 | </Para>
|
221 | 221 | </ListItem>
|
222 | 222 | </VarListEntry>
|
|
228 | 228 | <ListItem>
|
229 | 229 | <Para>
|
230 | 230 | The query's join tree shows the structure of the <literal>FROM</> clause.
|
231 |
| -For a simple query like <literal>SELECT ... FROM a, b, c</literal>, the join tree is just |
232 |
| -a list of the <literal>FROM</> items, because we are allowed to join them in |
233 |
| -any order. But when <literal>JOIN</> expressions, particularly outer joins, |
234 |
| -are used, we have to join in the order shown by the joins. |
235 |
| -In that case, the join tree shows the structure of the <literal>JOIN</> expressions. The |
236 |
| -restrictions associated with particular <literal>JOIN</> clauses (from <literal>ON</> or |
237 |
| -<literal>USING</> expressions) are stored as qualification expressions attached |
238 |
| -to those join-tree nodes. It turns out to be convenient to store |
239 |
| -the top-level <literal>WHERE</> expression as a qualification attached to the |
240 |
| -top-level join-tree item, too. So really the join tree represents |
241 |
| -both the <literal>FROM</> and <literal>WHERE</> clauses of a <command>SELECT</command>. |
| 231 | +For a simple query like <literal>SELECT ... FROM a, b, c</literal>, the join tree is just |
| 232 | +a list of the <literal>FROM</> items, because we are allowed to join them in |
| 233 | +any order. But when <literal>JOIN</> expressions, particularly outer joins, |
| 234 | +are used, we have to join in the order shown by the joins. |
| 235 | +In that case, the join tree shows the structure of the <literal>JOIN</> expressions. The |
| 236 | +restrictions associated with particular <literal>JOIN</> clauses (from <literal>ON</> or |
| 237 | +<literal>USING</> expressions) are stored as qualification expressions attached |
| 238 | +to those join-tree nodes. It turns out to be convenient to store |
| 239 | +the top-level <literal>WHERE</> expression as a qualification attached to the |
| 240 | +top-level join-tree item, too. So really the join tree represents |
| 241 | +both the <literal>FROM</> and <literal>WHERE</> clauses of a <command>SELECT</command>. |
242 | 242 | </Para>
|
243 | 243 | </ListItem>
|
244 | 244 | </VarListEntry>
|
|
250 | 250 | <ListItem>
|
251 | 251 | <Para>
|
252 | 252 | The other parts of the query tree like the <literal>ORDER BY</>
|
253 |
| -clause aren't of interest here. The rule system |
254 |
| -substitutes some entries there while applying rules, but that |
255 |
| -doesn't have much to do with the fundamentals of the rule |
256 |
| -system. |
| 253 | +clause aren't of interest here. The rule system |
| 254 | +substitutes some entries there while applying rules, but that |
| 255 | +doesn't have much to do with the fundamentals of the rule |
| 256 | +system. |
257 | 257 | </Para>
|
258 | 258 | </ListItem>
|
259 | 259 | </VarListEntry>
|
@@ -322,7 +322,7 @@ CREATE RULE "_RETURN" AS ON SELECT TO myview DO INSTEAD
|
322 | 322 | Currently, there can be only one action in an <literal>ON SELECT</> rule, and it must
|
323 | 323 | be an unconditional <command>SELECT</> action that is <literal>INSTEAD</>. This restriction was
|
324 | 324 | required to make rules safe enough to open them for ordinary users, and
|
325 |
| - it restricts <literal>ON SELECT</> rules toreal view rules. |
| 325 | + it restricts <literal>ON SELECT</> rules toact like views. |
326 | 326 | </Para>
|
327 | 327 |
|
328 | 328 | <Para>
|
@@ -695,29 +695,29 @@ UPDATE t1 SET b = t2.b WHERE t1.a = t2.a;
|
695 | 695 |
|
696 | 696 | <ItemizedList>
|
697 | 697 | <ListItem>
|
698 |
| -<Para> |
699 |
| - The range tables contain entries for the tables <literal>t1</> and <literal>t2</>. |
700 |
| -</Para> |
| 698 | +<Para> |
| 699 | + The range tables contain entries for the tables <literal>t1</> and <literal>t2</>. |
| 700 | +</Para> |
701 | 701 | </ListItem>
|
702 | 702 |
|
703 | 703 | <ListItem>
|
704 |
| -<Para> |
705 |
| - The target lists contain one variable that points to column |
706 |
| - <literal>b</> of the range table entry for table <literal>t2</>. |
707 |
| -</Para> |
| 704 | +<Para> |
| 705 | + The target lists contain one variable that points to column |
| 706 | + <literal>b</> of the range table entry for table <literal>t2</>. |
| 707 | +</Para> |
708 | 708 | </ListItem>
|
709 | 709 |
|
710 | 710 | <ListItem>
|
711 |
| -<Para> |
712 |
| - The qualification expressions compare the columns <literal>a</> of both |
713 |
| - range-table entries for equality. |
714 |
| -</Para> |
| 711 | +<Para> |
| 712 | + The qualification expressions compare the columns <literal>a</> of both |
| 713 | + range-table entries for equality. |
| 714 | +</Para> |
715 | 715 | </ListItem>
|
716 | 716 |
|
717 | 717 | <ListItem>
|
718 |
| -<Para> |
719 |
| - The join trees show a simple join between <literal>t1</> and <literal>t2</>. |
720 |
| -</Para> |
| 718 | +<Para> |
| 719 | + The join trees show a simple join between <literal>t1</> and <literal>t2</>. |
| 720 | +</Para> |
721 | 721 | </ListItem>
|
722 | 722 | </ItemizedList>
|
723 | 723 | </para>
|
@@ -860,34 +860,34 @@ SELECT t1.a, t2.b, t1.ctid FROM t1, t2 WHERE t1.a = t2.a;
|
860 | 860 |
|
861 | 861 | <ItemizedList>
|
862 | 862 | <ListItem>
|
863 |
| -<Para> |
864 |
| - They are allowed to have no action. |
865 |
| -</Para> |
866 |
| -</ListItem> |
| 863 | +<Para> |
| 864 | + They are allowed to have no action. |
| 865 | +</Para> |
| 866 | +</ListItem> |
867 | 867 |
|
868 | 868 | <ListItem>
|
869 |
| -<Para> |
870 |
| - They can have multiple actions. |
871 |
| -</Para> |
872 |
| -</ListItem> |
| 869 | +<Para> |
| 870 | + They can have multiple actions. |
| 871 | +</Para> |
| 872 | +</ListItem> |
873 | 873 |
|
874 | 874 | <ListItem>
|
875 |
| -<Para> |
876 |
| - They can be <literal>INSTEAD</> or <literal>ALSO</> (default). |
877 |
| -</Para> |
878 |
| -</ListItem> |
| 875 | +<Para> |
| 876 | + They can be <literal>INSTEAD</> or <literal>ALSO</> (default). |
| 877 | +</Para> |
| 878 | +</ListItem> |
879 | 879 |
|
880 | 880 | <ListItem>
|
881 |
| -<Para> |
882 |
| - The pseudorelations <literal>NEW</> and <literal>OLD</> become useful. |
883 |
| -</Para> |
884 |
| -</ListItem> |
| 881 | +<Para> |
| 882 | + The pseudorelations <literal>NEW</> and <literal>OLD</> become useful. |
| 883 | +</Para> |
| 884 | +</ListItem> |
885 | 885 |
|
886 | 886 | <ListItem>
|
887 |
| -<Para> |
888 |
| - They can have rule qualifications. |
889 |
| -</Para> |
890 |
| -</ListItem> |
| 887 | +<Para> |
| 888 | + They can have rule qualifications. |
| 889 | +</Para> |
| 890 | +</ListItem> |
891 | 891 | </ItemizedList>
|
892 | 892 |
|
893 | 893 | Second, they don't modify the query tree in place. Instead they
|
@@ -1875,14 +1875,15 @@ GRANT SELECT ON phone_number TO secretary;
|
1875 | 1875 | </Para>
|
1876 | 1876 |
|
1877 | 1877 | <Para>
|
1878 |
| - For the things that can be implemented by both, |
1879 |
| -itdepends on the usage of the database, which is the best. |
| 1878 | + For the things that can be implemented by both, which is best |
| 1879 | + depends on the usage of the database. |
1880 | 1880 | A trigger is fired for any affected row once. A rule manipulates
|
1881 |
| - the querytreeor generates an additionalone. So if many |
| 1881 | + the query or generates an additionalquery. So if many |
1882 | 1882 | rows are affected in one statement, a rule issuing one extra
|
1883 |
| - commandwould usually do a better job than a trigger that is |
| 1883 | + commandis likely to be faster than a trigger that is |
1884 | 1884 | called for every single row and must execute its operations
|
1885 |
| - many times. |
| 1885 | + many times. However, the trigger approach is conceptually far |
| 1886 | + simpler than the rule approach, and is easier for novices to get right. |
1886 | 1887 | </Para>
|
1887 | 1888 |
|
1888 | 1889 | <Para>
|
|