Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit afd57db

Browse files
Jan WieckJan Wieck
Jan Wieck
authored and
Jan Wieck
committed
Changed manpages for create_rule/drop_rule to the semantics
of the rule system of v6.4 and descendants. Jan
1 parent fa0f241 commit afd57db

File tree

2 files changed

+69
-115
lines changed

2 files changed

+69
-115
lines changed

src/man/create_rule.l

Lines changed: 66 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" This is -*-nroff-*-
22
.\" 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 $
44
.TH "CREATE RULE" SQL 11/05/95 PostgreSQL PostgreSQL
55
.SH NAME
66
create rule - define a new rule
@@ -10,7 +10,7 @@ create rule - define a new rule
1010
\fBas\fR \fBon\fR event
1111
\fBto\fR object [\fBwhere\fR clause]
1212
\fBdo\fR [\fBinstead\fR]
13-
[action | nothing | \fB[\fPactions...\fB]\fP]
13+
[\fBnothing\fP | action | \fB(\fPactions...\fB)\fP]
1414
.fi
1515
.SH DESCRIPTION
1616
.PP
@@ -26,73 +26,53 @@ is one of
2626
or
2727
.IR insert .
2828
.IR Object
29-
is either:
30-
.nf
31-
a class name
32-
\fIor\fR
33-
class.column
34-
.fi
29+
is a class name.
30+
.PP
3531
The
36-
.BR "from"
37-
clause, the
3832
.BR "where"
3933
clause, and the
4034
.IR action
4135
are respectively normal SQL
42-
.BR "from"
43-
clauses,
4436
.BR "where"
4537
clauses and collections of SQL commands with the following change:
4638
.IP
4739
.BR new
4840
or
49-
.BR current
41+
.BR old
5042
can appear instead of
5143
an instance variable whenever an instance
5244
variable is permissible in SQL.
5345
.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
5453
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
5756
instance
58-
(for retrieves, updates and deletes) and a
57+
(for updates and deletes) and a
5958
.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
6160
.BR "on"
6261
clause and the condition specified in the
6362
.BR "where"
64-
clause are true for the current instance, then the
63+
clause are true, then the
6564
.IR action
6665
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:
6867
.nf
69-
current.attribute-name
68+
old.attribute-name
7069
new.attribute-name
7170
.fi
7271
The
7372
.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.
7675
.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
9676
Each rule can have the optional tag
9777
.BR "instead" .
9878
Without this tag
@@ -104,116 +84,92 @@ part will be done instead of the user command.
10484
In this later case, the action can be the keyword
10585
.BR nothing .
10686
.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
11387
It is very important to note that the
11488
.BR rewrite
11589
rule system will
11690
neither detect nor process circular
11791
rules. For example, though each of the following two rule
11892
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.
12396
.nf
97+
12498
--
12599
--Example of a circular rewrite rule combination.
126100
--
127101
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 ...;
130104

131105
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 ...;
134108

135-
--
136-
--This attempt to retrieve from EMP will cause Postgres to crash.
137-
--
138-
select * from EMP
139109
.fi
140110
.PP
141111
You must have
142112
.IR "rule definition"
143113
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.
144127
.SH EXAMPLES
145128
.nf
129+
146130
--
147131
--Make Sam get the same salary adjustment as Joe
148132
--
149133
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+
153138
.fi
154139
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
156141
to the execution routines. Hence, his new salary is substituted into the
157142
.IR action
158-
part of the rule which is subsequently executed. This propagates
143+
part of the rule which is executed. This propagates
159144
Joe's salary on to Sam.
160145
.nf
146+
161147
--
162-
--Make Bill get Joe's salary when it is accessed
148+
-- Log changes to salary
163149
--
164150
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+
175155
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);
186159

187160
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+
199165
.fi
200166
.SH "SEE ALSO"
201167
drop_rule(l),
202-
create_view(l).
168+
create_view(l),
169+
create_trigger(l).
203170
.SH BUGS
204171
.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 \*(lqoid\*(rq field, system attributes cannot be
212-
referenced anywhere in a rule. Among other things, this means that
213-
functions of instances (e.g., \*(lqfoo(emp)\*(rq where \*(lqemp\*(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
217173
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
219175
that is on the order of one page (8KB).

src/man/drop_rule.l

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" This is -*-nroff-*-
22
.\" XXX standard disclaimer belongs here....
3-
.\" $Header: /cvsroot/pgsql/src/man/Attic/drop_rule.l,v 1.3 1998/06/23 17:52:36 momjian Exp $
3+
.\" $Header: /cvsroot/pgsql/src/man/Attic/drop_rule.l,v 1.4 1999/02/07 22:10:10 wieck Exp $
44
.TH "DROP RULE" SQL 11/05/95 PostgreSQL PostgreSQL
55
.SH NAME
66
drop rule \- removes a current rule from Postgres
@@ -21,7 +21,5 @@ drop rule example_1
2121
.fi
2222
.SH "SEE ALSO"
2323
create_rule(l),
24-
drop_view(l).
25-
.SH BUGS
26-
Once a rule is dropped, access to historical information the rule has
27-
written may disappear.
24+
drop_view(l),
25+
drop_trigger(l).

0 commit comments

Comments
 (0)