Commit afd57dbb authored by Jan Wieck's avatar Jan Wieck

Changed manpages for create_rule/drop_rule to the semantics

of the rule system of v6.4 and descendants.

Jan
parent fa0f2416
.\" This is -*-nroff-*- .\" This is -*-nroff-*-
.\" XXX standard disclaimer belongs here.... .\" XXX standard disclaimer belongs here....
.\" $Header: /cvsroot/pgsql/src/man/Attic/create_rule.l,v 1.10 1999/02/02 17:46:17 momjian Exp $ .\" $Header: /cvsroot/pgsql/src/man/Attic/create_rule.l,v 1.11 1999/02/07 22:10:09 wieck Exp $
.TH "CREATE RULE" SQL 11/05/95 PostgreSQL PostgreSQL .TH "CREATE RULE" SQL 11/05/95 PostgreSQL PostgreSQL
.SH NAME .SH NAME
create rule - define a new rule create rule - define a new rule
...@@ -10,7 +10,7 @@ create rule - define a new rule ...@@ -10,7 +10,7 @@ create rule - define a new rule
\fBas\fR \fBon\fR event \fBas\fR \fBon\fR event
\fBto\fR object [\fBwhere\fR clause] \fBto\fR object [\fBwhere\fR clause]
\fBdo\fR [\fBinstead\fR] \fBdo\fR [\fBinstead\fR]
[action | nothing | \fB[\fPactions...\fB]\fP] [\fBnothing\fP | action | \fB(\fPactions...\fB)\fP]
.fi .fi
.SH DESCRIPTION .SH DESCRIPTION
.PP .PP
...@@ -26,73 +26,53 @@ is one of ...@@ -26,73 +26,53 @@ is one of
or or
.IR insert . .IR insert .
.IR Object .IR Object
is either: is a class name.
.nf .PP
a class name
\fIor\fR
class.column
.fi
The The
.BR "from"
clause, the
.BR "where" .BR "where"
clause, and the clause, and the
.IR action .IR action
are respectively normal SQL are respectively normal SQL
.BR "from"
clauses,
.BR "where" .BR "where"
clauses and collections of SQL commands with the following change: clauses and collections of SQL commands with the following change:
.IP .IP
.BR new .BR new
or or
.BR current .BR old
can appear instead of can appear instead of
an instance variable whenever an instance an instance variable whenever an instance
variable is permissible in SQL. variable is permissible in SQL.
.PP .PP
Since v6.4 rules on
.IR select
are restricted to build
.BR views .
.BR "Create view"
should be used instead.
.PP
The semantics of a rule is that at the time an individual instance is The semantics of a rule is that at the time an individual instance is
accessed, updated, inserted or deleted, there is a updated, inserted or deleted, there is an
.BR current .BR old
instance instance
(for retrieves, updates and deletes) and a (for updates and deletes) and a
.BR new .BR new
instance (for updates and appends). If the event specified in the instance (for updates and inserts). If the event specified in the
.BR "on" .BR "on"
clause and the condition specified in the clause and the condition specified in the
.BR "where" .BR "where"
clause are true for the current instance, then the clause are true, then the
.IR action .IR action
part of the rule is executed. First, however, values from fields in part of the rule is executed. First, however, values from fields in
the current instance and/or the new instance are substituted for: the old instance and/or the new instance are substituted for:
.nf .nf
current.attribute-name old.attribute-name
new.attribute-name new.attribute-name
.fi .fi
The The
.IR action .IR action
part of the rule executes with same command and transaction identifier part of the rule executes with same transaction identifier
as the user command that caused activation. before the user command that caused activation.
.PP .PP
A note of caution about SQL rules is in order. If the same class
name or instance variable appears in the event,
.BR where
clause and the
.IR action
parts of a rule, they are all considered different tuple variables.
More accurately,
.BR new
and
.BR current
are the only tuple variables that are shared between these clauses.
For example, the following two rules have the same semantics:
.nf
on update to EMP.salary where EMP.name = "Joe"
do update EMP ( ... ) where ...
on update to EMP-1.salary where EMP-2.name = "Joe"
do update EMP-3 ( ... ) where ...
.fi
Each rule can have the optional tag Each rule can have the optional tag
.BR "instead" . .BR "instead" .
Without this tag Without this tag
...@@ -104,116 +84,92 @@ part will be done instead of the user command. ...@@ -104,116 +84,92 @@ part will be done instead of the user command.
In this later case, the action can be the keyword In this later case, the action can be the keyword
.BR nothing . .BR nothing .
.PP .PP
When choosing between the rewrite and instance rule systems for a
particular rule application, remember that in the rewrite system
.BR current
refers to a relation and some qualifiers whereas in the instance
system it refers to an instance (tuple).
.PP
It is very important to note that the It is very important to note that the
.BR rewrite .BR rewrite
rule system will rule system will
neither detect nor process circular neither detect nor process circular
rules. For example, though each of the following two rule rules. For example, though each of the following two rule
definitions are accepted by Postgres, the definitions are accepted by Postgres, the
.IR retrieve .IR update
command will cause command to one of the classes will cause
Postgres to Postgres to abort the transaction during the attempt to apply rules.
.IR crash :
.nf .nf
-- --
--Example of a circular rewrite rule combination. --Example of a circular rewrite rule combination.
-- --
create rule bad_rule_combination_1 as create rule bad_rule_combination_1 as
on select to EMP on update to EMP
do instead select to TOYEMP do update TOY set ...;
create rule bad_rule_combination_2 as create rule bad_rule_combination_2 as
on select to TOYEMP on update to TOY
do instead select to EMP do update EMP set ...;
--
--This attempt to retrieve from EMP will cause Postgres to crash.
--
select * from EMP
.fi .fi
.PP .PP
You must have You must have
.IR "rule definition" .IR "rule definition"
access to a class in order to define a rule on it. access to a class in order to define a rule on it.
.PP
In contrast to queries run by trigger procedures,
the rule actions are executed under the permissions of the owner
of the
.BR event
class. Thus, if the owner of a class defines a rule that inserts something
into another one (like in the log example below), the user updating the
.BR event
class must not have
.IR insert
permissions for the class specified in the
.BR "rule actions" .
This technique can safely be used to deny users from modifying event logging.
.SH EXAMPLES .SH EXAMPLES
.nf .nf
-- --
--Make Sam get the same salary adjustment as Joe --Make Sam get the same salary adjustment as Joe
-- --
create rule example_1 as create rule example_1 as
on update EMP.salary where current.name = "Joe" on update to EMP where old.name = "Joe"
do update EMP (salary = new.salary) do update EMP set salary = new.salary
where EMP.name = "Sam" where EMP.name = "Sam";
.fi .fi
At the time Joe receives a salary adjustment, the event will become At the time Joe receives a salary adjustment, the event will become
true and Joe's current instance and proposed new instance are available true and Joe's old instance and proposed new instance are available
to the execution routines. Hence, his new salary is substituted into the to the execution routines. Hence, his new salary is substituted into the
.IR action .IR action
part of the rule which is subsequently executed. This propagates part of the rule which is executed. This propagates
Joe's salary on to Sam. Joe's salary on to Sam.
.nf .nf
-- --
--Make Bill get Joe's salary when it is accessed -- Log changes to salary
-- --
create rule example_2 as create rule example_2 as
on select to EMP.salary on insert to EMP
where current.name = "Bill" do insert into EMP_LOG (name, newsal, when)
do instead values (new.name, new.salary, 'now'::text);
select (EMP.salary) from EMP where EMP.name = "Joe"
.fi
.nf
--
--Deny Joe access to the salary of employees in the shoe
--department. (pg_username() returns the name of the current user)
--
create rule example_3 as create rule example_3 as
on select to EMP.salary on update to EMP where old.salary != new.salary
where current.dept = "shoe" do insert into EMP_LOG (name, oldsal, newsal, when)
and pg_username() = "Joe" values (old.name, old.salary, new.salary, 'now'::text);
do instead nothing
.fi
.nf
--
--Create a view of the employees working in the toy department.
--
create TOYEMP(name = name, salary = int4)
create rule example_4 as create rule example_4 as
on select to TOYEMP on delete to EMP
do instead select (EMP.name, EMP.salary) from EMP do insert into EMP_LOG (name, oldsal, when)
where EMP.dept = "toy" values (old.name, old.salary, 'now'::text);
.fi
.nf
--
--All new employees must make 5,000 or less
--
create rule example_5 as
on insert to EMP where new.salary > 5000
do update newset salary = 5000
.fi .fi
.SH "SEE ALSO" .SH "SEE ALSO"
drop_rule(l), drop_rule(l),
create_view(l). create_view(l),
create_trigger(l).
.SH BUGS .SH BUGS
.PP .PP
.BR "instead" The rule system stores the rule definition as query plans into text
rules do not work properly.
.PP
The object in a SQL rule cannot be an array reference and cannot
have parameters.
.PP
Aside from the \*(lqoid\*(rq field, system attributes cannot be
referenced anywhere in a rule. Among other things, this means that
functions of instances (e.g., \*(lqfoo(emp)\*(rq where \*(lqemp\*(rq
is a class) cannot be called anywhere in a rule.
.PP
The rule system store the rule text and query plans as text
attributes. This implies that creation of rules may fail if the attributes. This implies that creation of rules may fail if the
rule plus its various internal representations exceed some value rule in its internal representations exceed some value
that is on the order of one page (8KB). that is on the order of one page (8KB).
.\" This is -*-nroff-*- .\" This is -*-nroff-*-
.\" XXX standard disclaimer belongs here.... .\" XXX standard disclaimer belongs here....
.\" $Header: /cvsroot/pgsql/src/man/Attic/drop_rule.l,v 1.3 1998/06/23 17:52:36 momjian Exp $ .\" $Header: /cvsroot/pgsql/src/man/Attic/drop_rule.l,v 1.4 1999/02/07 22:10:10 wieck Exp $
.TH "DROP RULE" SQL 11/05/95 PostgreSQL PostgreSQL .TH "DROP RULE" SQL 11/05/95 PostgreSQL PostgreSQL
.SH NAME .SH NAME
drop rule \- removes a current rule from Postgres drop rule \- removes a current rule from Postgres
...@@ -21,7 +21,5 @@ drop rule example_1 ...@@ -21,7 +21,5 @@ drop rule example_1
.fi .fi
.SH "SEE ALSO" .SH "SEE ALSO"
create_rule(l), create_rule(l),
drop_view(l). drop_view(l),
.SH BUGS drop_trigger(l).
Once a rule is dropped, access to historical information the rule has
written may disappear.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment