Course Notes On Active-Database Management What You Will Learn in This Chapter
Course Notes On Active-Database Management What You Will Learn in This Chapter
an introduction to active-database technology (i.e., triggers), which is a useful extension to classical DBMS technology the basic idea is intuitively simple and natural the detail of trigger denition gets quickly complicated, especially as triggers are part of the database and must be valid for all application programs the practical usage of the technology is mostly about automating simple repetitive operations that would be very time-consuming to be coded in application programs
Active-Database Technology
Passive DBMS: all actions on data result from explicit invocation in application programs (they only do what application programs tell them to do) Active DBMS: execution of actions can be automatically triggered in response to monitored events, including 3 database updates (upon deletion of the data about a customer) 3 points in time (on January 1, every hour) 3 events external to the database (whenever paper jams in the printer)
Evolution of database technology has been going thru representing and supporting more functionality of database applications within the DBMS, e.g., 3 checks of some types of integrity constraints (produced from a declarative denition located with the database schema) 3 stored procedures: precompiled procedures located within the database, invoked from application and system programs 3 common semantics abstracted from application domains (e.g., for spatial, multimedia, temporal, deductive, active databases) Active-database technology 3 a relatively recent extension of traditional DBMS technology 3 most commercial RDBMSs include some capability for rules or triggers 3 research prototypes provide more comprehensive support for active rules than RDBMSs Application semantics in programs for active DBMSs is expressed in: 3 traditional application programs (as for passive DBMSs) 3 rules (in the database, available to all applications)
Exercise: rephrase the above examples as event-condition-action rules Note that many examples have a more declarative form than ECA rules
The basic ECA model for rules is simple and intuitive, but there are signicant dierences in the ways that they have been realized in practice Triggers were not included in the SQL-92 standard Starburst triggers are presented for their simplicity Oracle triggers are based on an early version ( 1993) of the SQL3 standard DB2 triggers are closer to more recent SQL3 proposals
Starburst
Relational prototype by IBM Almaden Research Center Event-Condition-Action rules in Starburst: 3 event: data-manipulation operations (INSERT, DELETE, UPDATE) in SQL 3 condition: Boolean predicate in SQL on the current state of the database 3 action: SQL statements, rule-manipulation statements, rollback
Relations Emp(Name,Sal,Dno) and Dept(Dno,Mgr) The rule is activated when an employee is created, and when the salary or the department of an employee changes rollback = abort the transaction with the statement that caused the triggering event Remark about the example: 3 The constraint (the salary of employees is not larger than the salary of the manager of their department) is declarative 3 rule Mgrsals does not cover all the scenarios that could lead to violating the constraint 3 another rule is necessary on relation Dept, to check the constraint when a department changes its manager
10
Each rule has a unique name Each rule is associated with a single relation Events can only be database updates A rule can monitor several events on the target relation Events can be monitored by several rules The condition is an SQL predicate (true if the result is nonempty) Rules are ordered for execution priority through an (acyclic) partial order (PRECEDES, FOLLOWS)
11
Starburst: Semantics
Rules are triggered by the execution of operations in statements that are part of transactions Rules are statement-level: they are executed once per statement even for statements that trigger events on several tuples Execution mode is deferred: 3 all rules triggered during transaction execution are placed in a conict set 3 rules are not considered until the end of the transaction (transaction commit) unless an explicit PROCESS RULES is executed in the transaction
12
Transaction = a piece of program, a sequence of statements, that is to be treated as atomic (i.e., as an indivisible all-or-nothing whole) unit of work for some aspect of processing 3 for execution (example: a transfer of funds for a bank is a composite operation that must be executed entirely or not at all) 3 for checking constraint satisfaction (constraints may be temporarily violated while the transaction executes, they must be satised before and after the transaction) 3 for concurrency management (the detail of a transaction should not be visible by and should not be intefered with other transactions) 3 for active-rule execution (here) Statement: a part of a transaction; a transaction is a sequence of several statements; a statement expresses an operation on the database (here, for active rules, operations are modications of database tuples) Event: the occurrence of executing a statement, i.e., a request for executing an operation on the database; the operation is not necessarily executed at the same time that the event occurs With the deferred mode, condition evaluation and action execution are decoupled from rule activation Vocabulary: set-oriented is also used for statement-level
Rule Processing
Algorithm for rule selection and execution While the conict set is not empty (1) Select a rule R in the conict set among those rules at highest priority; take R out of the conict set (2) Evaluate the condition of R (3) If the condition of R is true, then execute the action of R
13
14
15
Transaction: insert {(Rick,150), (John,120)} into Emp Name Stefano Patrick Michael Rick John Sal 90 90 110 150 120
16
Here, the action part of the rule causes an event that activates the rule again, in a cascading mode.
Insertion triggers the rule, condition is true (average = 112) action executed Name Stefano Patrick Michael Rick John Sal 81 81 99 135 108
Updating triggers the rule again, condition is true (average = 101) action executed Name Stefano Patrick Michael Rick John Sal 73 73 89 121 97
The rule is triggered again, the condition is false (average = 91) termination
17
The action of SalaryControl updates all tuples of Emp, but these updates lead to a single consideration of SalaryControl (statement-level semantics) Termination for the example: 3 rule processing terminates because the multiplicative factor for salaries is 1: the action of the rule decreases the average salary and the condition of the rule checks that the average salary is below a given value eventually the average salary will go below the given value 3 if the multiplicative factor of the salaries was 1, then rule application would not terminate In general guaranteeing termination 3 is the responsibility of the programmer 3 is not easy
18
Rules consider the net eect of operations between two database states each tuple appears at most once in each temporary table
19
20
Rule evaluation is governed by the net eect of operations: transition relations contain the net eect of all operations within the transaction that cause transitions between database states 3 multiple operations occurring on the same tuple are composed (e.g., insertion of a tuple followed by deletion of the same tuple has no net eect) each tuple appears at most once in each of the sets INSERTED and DELETED More than one of INSERTED and DELETED can be nonempty for the same rule execution as a rule can monitor several events
21
Rule HighPaid inserts highly paid employees into relation HighPaidEmp Insertion of (Rick,150) and (John,120) triggers both SalaryControl and HighPaid INSERTED(Emp) = {(Rick,121),(John,97)} when the condition of HighPaid is evaluated and its action executed Thus HighPaid is triggered and executed in dierent database states 3 it is triggered at the same time as SalaryControl (at the rst insertion into Emp) 3 it is considered and executed after all executions of SalaryControl Note that, by the application of semantics HighPaid, it is logical that it be executed after the adjustments made by SalaryControl Exercises: 3 add Updated(Sal) to the triggering events of HighPaid 3 discuss the behavior of the rules when FOLLOWS SalaryControl is removed from rule HighPaid
22
Transactions can dynamically activate and deactivate existing rules Rules may be grouped in sets Rule processing can be started from within a transaction thru PROCESS RULE to counter the default deferred mode (whereby rules are executed at the commit time of the transaction) Rule processing can be required to concern a specic rule, a specic set of rules, or all rules
Oracle: Triggers
Respond to modication operations (insert, delete, update) to a relation Granularities for rules 3 tuple-level (or row-level): a rule is triggered once for each tuple concerned by the triggering event 3 statement-level: a rule is triggered only once even if several tuples are involved Immediate execution mode: rules are considered immediately after the event has been requested (Starburst rules are deferred) Rules can be considered and executed before, after, or instead of the operation of the triggering event is executed
23
24
The relation concerned is Emp(Name,Sal,Dno) NondecSal is the trigger name t1 and t2 are like tuple variables in SQL: they serve ro refer, in the condition and action parts, to the old tuple (before the update) and the new tuple (after the update) FOR EACH ROW expresses the requirement that the trigger is executed once for each updated tuple
25
Row-level triggers versus statement-level triggers: the real issue is what is considered an event (execute a statement versus change a database tuple) Row-level triggers are requested with a FOR EACH ROW clause 3 the rule is activated once for each tuple concerned 3 useful if the code in the actions depends on data provided by the triggering statement or on the tuples aected) 3 INSERTING, DELETING, UPDATING may be used in the action to check which triggering event has occurred 3 the old and the new value of an aected tuple can be referred to by variables introduced in a REFERENCING clause built-in variables OLD (referring to a deleted tuple or to a tuple before it was updated) and NEW (referring to a newly inserted or newly updated tuple) in case of insertion, only the new state is dened; in case of deletion, only the old state is dened 3 the condition consists of a simple predicate on the current tuple
Statement-level triggers 3 this is the default (i.e., implied when the phrase FOR EACH ROW is omitted 3 the rule is activated once for each triggering statement even if several tuples are involved (e.g., an SQL statement that updates several tuples) or if no tuple is updated 3 references for old and new tuples are meaningless 3 useful if the code in the actions does not depend on the data provided by the triggering statement nor on the tuples aected (e.g., some security check on user, some audit based on the type of triggering statement) 3 do not have a condition part (it is not clear why Oracle made that decision) 3 do not have the possibility to refer to intermediate relation value thru INSERTED, DELETED, UPDATED as in Starburst (it is not clear why Oracle made that decision) Remember that Starburst has statement-level triggers only
26
A classical warehouse-management problem, with two relations: 3 Inventory(Part,PartOnHand,ReorderPoint,ReorderQty) 3 PendingOrders(Part,Qty,Date) Oracle triggers may execute actions containing arbitrary PL/SQL code (not just SQL as for Starburst); PL/SQL extends SQL by adding the typical constructs of a programming language Constraint: there is at most one order per part in PendingOrders (Part is the key of PendingOrders). The tuple is suppressed from PendingOrders when the corresponding parts are supplied to the warehouse. Active Database Systems- 20
The Reorder rule generates a new order (i.e., inserts a tuple into PendingOrders) whenever the quantity PartOnHand of a given part in Inventory falls below ReorderPoint
Transaction executed on October 10, 2000 UPDATE Inventory SET PartOnHand = PartOnHand - 70 WHERE Part = 1 Result: insertion of (1,100,2000-10-10) into PendingOrders Transaction executed the same day UPDATE Inventory SET PartOnHand = PartOnHand - 60 WHERE Part >= 1 Result: insertion of (3,120,2000-10-10) into PendingOrders
27
Exercises: 3 write this example for Starburst 3 adapt rule Reorder to take into account updates of ReorderPoint and ReorderQty
28
The execution of data-management statements (insert, delete, or update) in SQL is interwoven with the execution of triggers that are activated by them, according to the preceding algorithm Two granularities and two triggering times 4 types of triggers: 3 row-level before-triggers (red before modifying each tuple aected by the triggering statement) 3 statement-level before-triggers (red once before executing the triggering statement) 3 row-level after-triggers (red after modifying each row aected by the triggering statement) 3 statement-level after-triggers (red once after executing the triggering statement) The execution also takes into account the checking of constraints and assertions Priority among triggers of the same type (row/statement + before/after) 3 early versions of Oracle forbade more than one trigger of the same type 3 more recent versions relaxed the restriction, but do not allow to specify priorities among triggers activated by the same event and with the same type: the order is controled by the system
29
Instead-of Triggers
Another mode of specifying triggers, besides before and after triggers The action of the instead-of trigger is carried out in place of the statement that produced the activating event Instead-of triggers are typically used to update views Their power must be controled (allowing do X instead of Y in general would lead to complex eects)
30
31
Relations 3 Emp(empno,empname,deptno) 3 Dept(deptno,deptname,mgrno) View: CREATE VIEW Managers AS SELECT d.deptno, d.deptname, e.empno, e.empname FROM Emp e, Dept d WHERE e.empno = d.mgrno An insert into Managers is interpreted as an update of the mgrno attribute of the corresponding dept tuple
32
Every trigger monitors a single event (= Starburst, Oracle, Chimera) Triggers are activated immediately, BEFORE or AFTER their event, have row- or statementlevel granularity, as in Oracle State-transition values are dened for both row- and statement-level granularities 3 OLD and NEW introduce tuple values (as in Oracle) 3 OLD_TABLE and NEW_TABLE for relations (as in Starburst) insertion is described by NEW_TABLE only, deletion by OLD_TABLE only OLD_TABLE and NEW_TABLE are equivalent to the temporary relations INSERTED and DELETED in Starburst (remember that Starburst rules can monitor multiple events) Triggers cannot execute data denition or transactional commands They can raise errors which in turn can cause statement-level rollbacks
33
34
35
36
A referential-integrity constraint requires Part Suppliers to be also distributors, with HDD as a default Supplier FOREIGN KEY (Supplier) REFERENCES Distributor (Name) ON DELETE SET DEFAULT Trigger forcing a statement-level rollback when updating Supplier to NULL CREATE TRIGGER OneSupplier BEFORE UPDATE OF Supplier ON Part REFERENCING NEW AS N FOR EACH ROW WHEN (N.Supplier is NULL) SIGNAL SQLSTATE 70005 (Cannot change supplier to NULL) 37
Relations Part(PartNum,Supplier,Cost), Distributor(Name,City,State), and Audit(User,CurrDate,UpdTuples): with parts and their suppliers, distributors who are also suppliers, and an audit of updates to the Supplier relation
38
39
40
41
42
43
45
46
47
48
49
50
51
52
Some examples of applications that can benet from active technology and business rules: 3 monitoring access to a building and reacting to abnormal circumstances 3 watching evolution of share values on stock market and triggering trading actions 3 managing inventory to follow stock variations 3 managing a network for energy distribution (see later) 3 airway assignment in air trac control Notication (alerters): frequent case of application-specic rules whose actions signal, e.g., thru messages, certain conditions that occur with or without changing the database 3 application inserts in the database regular readings by temperature sensors; active rules monitor exceptional temperature values and raise alarms
53
Integrity Management
For DBMSs, constraints can be 3 built-in, dened by special DDL constructs (e.g., keys, nonnull attributes, unique attributes, referential integrity in RDBMSs) 3 adhoc, arbitrarily complex domain-dependent constraints Integrity maintenance in practice 3 DBMSs check built-in constraints with automatically generated triggers (e.g., for referential integrity) 3 DBMSs support limited forms of adhoc constraints (e.g., with some version of assertions and CHECK clauses of SQL-92) 3 the remaining constraints are implemented as active rules (or in application programs ...)
54
55
56
57
CREATE RULE DeptEmp2 ON Dept WHEN DELETED, UPDATED(DNo) IF EXISTS (SELECT * FROM Emp WHERE NOT EXISTS (SELECT * FROM Dept WHERE DNo=Emp.DeptNo)) THEN ROLLBACK
58
One rule is necessary for each relation (Emp and Dept) The above rules are inecient: computation of the condition checks the whole database Rules may assume that the constraint is veried in the initial state it suces to compute the condition relative to transition values
59
60
61
62
63
64
65
66
67
Replication
Several copies of the same information, typically in a distributed DB Synchronized copies: very costly, often unnecessary asynchronous techniques to propagate changes Asymmetric replication 3 one primary copy (where changes are performed) and several secondary copies (read only, updated asynchronously) 3 capture module: monitors changes made by applications to primary copy 3 application module: propagates changes to secondary sites Symmetric replication 3 copies accept updates asynchronously 3 each have a capture and an application module 3 simultaneous updates may cause loss of consistency
68
69
70
71
72
73
The network is composed of sites and connections between pairs of sites 3 sites comprise power stations (Distributors where power is generated and fed into the network) intermediate nodes (Nodes) where power is transferred to be redistributed across the network nal Users of electrical power 3 connections are called branches class Branch describes all connections between pairs of sites several Wires are placed along the branches wires are of a given WireType, each type carrying a maximum power branches can be dynamically added or dropped to the network
74
75
76
77
78
79
80
81
82
83
84
85
86
87
89