1
1
.\" This is -*-nroff-*-
2
2
.\" XXX standard disclaimer belongs here....
3
- .\" $Header: /cvsroot/pgsql/src/man/Attic/create_rule.l,v 1.10 1999/02/02 17:46:17 momjian Exp $
3
+ .\" $Header: /cvsroot/pgsql/src/man/Attic/create_rule.l,v 1.11 1999/02/07 22:10:09 wieck Exp $
4
4
.TH "CREATE RULE" SQL 11/05/95 PostgreSQL PostgreSQL
5
5
.SH NAME
6
6
create rule - define a new rule
@@ -10,7 +10,7 @@ create rule - define a new rule
10
10
\fB as \fR \fB on \fR event
11
11
\fB to \fR object [\fB where \fR clause]
12
12
\fB do \fR [\fB instead \fR ]
13
- [action | nothing | \fB [ \fP actions...\fB ] \fP ]
13
+ [\fB nothing \fP | action | \fB ( \fP actions...\fB ) \fP ]
14
14
.fi
15
15
.SH DESCRIPTION
16
16
.PP
@@ -26,73 +26,53 @@ is one of
26
26
or
27
27
.IR insert .
28
28
.IR Object
29
- is either:
30
- .nf
31
- a class name
32
- \fI or \fR
33
- class.column
34
- .fi
29
+ is a class name.
30
+ .PP
35
31
The
36
- .BR " from"
37
- clause, the
38
32
.BR " where"
39
33
clause, and the
40
34
.IR action
41
35
are respectively normal SQL
42
- .BR " from"
43
- clauses,
44
36
.BR " where"
45
37
clauses and collections of SQL commands with the following change:
46
38
.IP
47
39
.BR new
48
40
or
49
- .BR current
41
+ .BR old
50
42
can appear instead of
51
43
an instance variable whenever an instance
52
44
variable is permissible in SQL.
53
45
.PP
46
+ Since v6.4 rules on
47
+ .IR select
48
+ are restricted to build
49
+ .BR views .
50
+ .BR " Create view"
51
+ should be used instead.
52
+ .PP
54
53
The semantics of a rule is that at the time an individual instance is
55
- accessed, updated, inserted or deleted, there is a
56
- .BR current
54
+ updated, inserted or deleted, there is an
55
+ .BR old
57
56
instance
58
- (for retrieves, updates and deletes) and a
57
+ (for updates and deletes) and a
59
58
.BR new
60
- instance (for updates and appends ). If the event specified in the
59
+ instance (for updates and inserts ). If the event specified in the
61
60
.BR " on"
62
61
clause and the condition specified in the
63
62
.BR " where"
64
- clause are true for the current instance , then the
63
+ clause are true, then the
65
64
.IR action
66
65
part of the rule is executed. First, however, values from fields in
67
- the current instance and/or the new instance are substituted for:
66
+ the old instance and/or the new instance are substituted for:
68
67
.nf
69
- current .attribute-name
68
+ old .attribute-name
70
69
new.attribute-name
71
70
.fi
72
71
The
73
72
.IR action
74
- part of the rule executes with same command and transaction identifier
75
- as the user command that caused activation.
73
+ part of the rule executes with same transaction identifier
74
+ before the user command that caused activation.
76
75
.PP
77
- A note of caution about SQL rules is in order. If the same class
78
- name or instance variable appears in the event,
79
- .BR where
80
- clause and the
81
- .IR action
82
- parts of a rule, they are all considered different tuple variables.
83
- More accurately,
84
- .BR new
85
- and
86
- .BR current
87
- are the only tuple variables that are shared between these clauses.
88
- For example, the following two rules have the same semantics:
89
- .nf
90
- on update to EMP.salary where EMP.name = "Joe"
91
- do update EMP ( ... ) where ...
92
-
93
- on update to EMP-1.salary where EMP-2.name = "Joe"
94
- do update EMP-3 ( ... ) where ...
95
- .fi
96
76
Each rule can have the optional tag
97
77
.BR " instead" .
98
78
Without this tag
@@ -104,116 +84,92 @@ part will be done instead of the user command.
104
84
In this later case, the action can be the keyword
105
85
.BR nothing .
106
86
.PP
107
- When choosing between the rewrite and instance rule systems for a
108
- particular rule application, remember that in the rewrite system
109
- .BR current
110
- refers to a relation and some qualifiers whereas in the instance
111
- system it refers to an instance (tuple).
112
- .PP
113
87
It is very important to note that the
114
88
.BR rewrite
115
89
rule system will
116
90
neither detect nor process circular
117
91
rules. For example, though each of the following two rule
118
92
definitions are accepted by Postgres, the
119
- .IR retrieve
120
- command will cause
121
- Postgres to
122
- .IR crash :
93
+ .IR update
94
+ command to one of the classes will cause
95
+ Postgres to abort the transaction during the attempt to apply rules.
123
96
.nf
97
+
124
98
--
125
99
--Example of a circular rewrite rule combination.
126
100
--
127
101
create rule bad_rule_combination_1 as
128
- on select to EMP
129
- do instead select to TOYEMP
102
+ on update to EMP
103
+ do update TOY set ...;
130
104
131
105
create rule bad_rule_combination_2 as
132
- on select to TOYEMP
133
- do instead select to EMP
106
+ on update to TOY
107
+ do update EMP set ...;
134
108
135
- --
136
- --This attempt to retrieve from EMP will cause Postgres to crash.
137
- --
138
- select * from EMP
139
109
.fi
140
110
.PP
141
111
You must have
142
112
.IR " rule definition"
143
113
access to a class in order to define a rule on it.
114
+ .PP
115
+ In contrast to queries run by trigger procedures,
116
+ the rule actions are executed under the permissions of the owner
117
+ of the
118
+ .BR event
119
+ class. Thus, if the owner of a class defines a rule that inserts something
120
+ into another one (like in the log example below), the user updating the
121
+ .BR event
122
+ class must not have
123
+ .IR insert
124
+ permissions for the class specified in the
125
+ .BR " rule actions" .
126
+ This technique can safely be used to deny users from modifying event logging.
144
127
.SH EXAMPLES
145
128
.nf
129
+
146
130
--
147
131
--Make Sam get the same salary adjustment as Joe
148
132
--
149
133
create rule example_1 as
150
- on update EMP.salary where current.name = "Joe"
151
- do update EMP (salary = new.salary)
152
- where EMP.name = "Sam"
134
+ on update to EMP where old.name = "Joe"
135
+ do update EMP set salary = new.salary
136
+ where EMP.name = "Sam";
137
+
153
138
.fi
154
139
At the time Joe receives a salary adjustment, the event will become
155
- true and Joe's current instance and proposed new instance are available
140
+ true and Joe's old instance and proposed new instance are available
156
141
to the execution routines. Hence, his new salary is substituted into the
157
142
.IR action
158
- part of the rule which is subsequently executed. This propagates
143
+ part of the rule which is executed. This propagates
159
144
Joe's salary on to Sam.
160
145
.nf
146
+
161
147
--
162
- --Make Bill get Joe's salary when it is accessed
148
+ -- Log changes to salary
163
149
--
164
150
create rule example_2 as
165
- on select to EMP.salary
166
- where current.name = "Bill"
167
- do instead
168
- select (EMP.salary) from EMP where EMP.name = "Joe"
169
- .fi
170
- .nf
171
- --
172
- --Deny Joe access to the salary of employees in the shoe
173
- --department. (pg_username() returns the name of the current user)
174
- --
151
+ on insert to EMP
152
+ do insert into EMP_LOG (name, newsal, when)
153
+ values (new.name, new.salary, 'now'::text);
154
+
175
155
create rule example_3 as
176
- on select to EMP.salary
177
- where current.dept = "shoe"
178
- and pg_username() = "Joe"
179
- do instead nothing
180
- .fi
181
- .nf
182
- --
183
- --Create a view of the employees working in the toy department.
184
- --
185
- create TOYEMP(name = name, salary = int4)
156
+ on update to EMP where old.salary != new.salary
157
+ do insert into EMP_LOG (name, oldsal, newsal, when)
158
+ values (old.name, old.salary, new.salary, 'now'::text);
186
159
187
160
create rule example_4 as
188
- on select to TOYEMP
189
- do instead select (EMP.name, EMP.salary) from EMP
190
- where EMP.dept = "toy"
191
- .fi
192
- .nf
193
- --
194
- --All new employees must make 5,000 or less
195
- --
196
- create rule example_5 as
197
- on insert to EMP where new.salary > 5000
198
- do update newset salary = 5000
161
+ on delete to EMP
162
+ do insert into EMP_LOG (name, oldsal, when)
163
+ values (old.name, old.salary, 'now'::text);
164
+
199
165
.fi
200
166
.SH "SEE ALSO"
201
167
drop_rule(l),
202
- create_view(l).
168
+ create_view(l),
169
+ create_trigger(l).
203
170
.SH BUGS
204
171
.PP
205
- .BR " instead"
206
- rules do not work properly.
207
- .PP
208
- The object in a SQL rule cannot be an array reference and cannot
209
- have parameters.
210
- .PP
211
- Aside from the \*( lq oid\*( rq field, system attributes cannot be
212
- referenced anywhere in a rule. Among other things, this means that
213
- functions of instances (e.g., \*( lq foo(emp)\*( rq where \*( lq emp\*( rq
214
- is a class) cannot be called anywhere in a rule.
215
- .PP
216
- The rule system store the rule text and query plans as text
172
+ The rule system stores the rule definition as query plans into text
217
173
attributes. This implies that creation of rules may fail if the
218
- rule plus its various internal representations exceed some value
174
+ rule in its internal representations exceed some value
219
175
that is on the order of one page (8KB).
0 commit comments