Upload
psoo1978
View
366
Download
1
Embed Size (px)
Citation preview
Views The Rule Rewrite System Further Development Further Development Conclusion
Updatable ViewsWhat we have and what we want
Bernd Helmle
29. Juni 2009
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Agenda
1 Views
2 The PostgreSQL Rule Rewrite System
3 Further Development
4 Conclusions
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Views (1)
“Virtual Relation”
Abstraction Layer, user specific view of the data model
Materialized, not materialized
Independency of the logical data model (to some extent)
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Views (2)
Updatable views defined in SQL92
Simple views, one relation only
No Joins
DISTINCT, GROUP BY, aggregates or UNIONs not allowed
No constant or function expressions in target list
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Views (3)
Updatable Views defined in SQL99
Joins allowed (e.g. one-to-one relationships)
Notion of preserved keys
potentially updatable
updatable
simply updatable
insertable
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Views (4)
CHECK OPTION forces view integrity
Qualification of the view definition becomes view constraint
Violation forces transaction error
CASCADED or LOCAL CHECK OPTION
CREATE VIEW v f o o AS SELECT i d FROM f o oWHERE i d BETWEEN 1 AND 10WITH CHECK OPTION ;
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Rules
Instructs the rewriter to rewrite query trees
More like “macros”
Many tasks are better done with TRIGGERs
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Rules
CREATE OR REPLACE RULE f o o u p d r u l eASON UPDATETO f o oDO ALSOINSERT INTO f o o l o g VALUES(NEW. id ,
l o c a l t i m e s t a m p ) ;
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Rewriting Queries(1)
Views are implemented via rules
CREATE VIEW creates a “dummy relation”
ON SELECT rule with the view definition
Rule has no qualification
Rule is defined as INSTEAD
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Rewriting Queries(2)
CREATE VIEW v f o oASSELECT
∗FROM
f o oWHERE
i d BETWEEN 1 AND 1 0 ;
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Rewriting Queries(3)
RETURN rule for ON SELECT event on relation vfoo
SELECTf o o . i d
FROMf o o ∗NEW∗ , f o o ∗OLD∗ , f o o f o o
WHERE f o o . i d BETWEEN 1 AND 1 0 ;
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Rewriting Queries(4)
1 SELECTing from a view becomes effectively a subquery
2 Rewriter scans Range Table Entries and substitutes RETURNrules
3 Directly folded into the original query tree
SELECT v t e s t . i d FROM (SELECT
f o o . i dFROM
f o o ∗NEW∗ , f o o ∗OLD∗ , f o o f o oWHERE f o o . i d BETWEEN 1 AND 10) AS v t e s t
;
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Updatable Views(1)
Idea: Use rules for update actions, too
Rules for DELETE, UPDATE and INSERT actions
Don’t modify the query tree in place
Need to be INSTEAD
Conditional rules produce more than one query tree
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Updatable Views(2)
Example of a view UPDATE rule.
CREATE OR REPLACE RULE ” UPDATE” ASON UPDATE TO v f o o DO INSTEAD UPDATE ONLY
f o o SET i d = NEW. i dWHERE
CASEWHEN o l d . i d I S NOT NULL THEN o l d .
i d = f o o . i dELSE s e r i a l t e s t . i d I S NULL
END
This just *works* for simple views.
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Updatable Views(3)
CHECK OPTION needs additional thoughts
Obvious solution: Use conditional rules
check function checks update action for integrity
Rule should abort transaction when new tuple violatesCHECK OPTION
Unfortunately, this doesn’t work
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Updatable Views(4)
CREATE OR REPLACE FUNCTIONdummy check opt ion ( p c o n s t r a i n t INBOOLEAN)
RETURNS BOOLEANLANGUAGE p l p g s q lAS$$BEGIN
IF p c o n s t r a i n t I S FALSE THENr a i s e exception ’ Update v i o l a t e s
v iew c o n d i t i o n ’ ;END IF ;
RETURN TRUE;END;$$ ;
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Updatable Views(5)
CREATE OR REPLACE RULE ” INSERT”AS
ON INSERT TO v s e r i a l t e s tWHERE
dummy check opt ion ( new . i d >= 1 AND new .i d <= 10)
DO INSTEADINSERT INTO s e r i a l t e s t ( i d ) VALUES (
new . i d ) ;
Fails due to double evaluation of VOLATILE expressions.
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Further Development(1)
Idea: Create update rules automatically
Need to check query tree if updatable or not after parsing(checkTree())
Transform SELECT -Rule into corresponding update action:1 Range table entries2 Target list3 Join tree4 Command type5 Result relation
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Further Development(2)
Patch to create update rules automatically exists
Implements simple view updates as described in SQL92
CHECK OPTION not completed
Bernd Helmle Updatable Views What we have and what we want
Views The Rule Rewrite System Further Development Further Development Conclusion
Conclusions
Rules works with simple view updates
Much more work needed to support JOINed views etc.
CHECK OPTION needs executor support, e.g. push-downfacility for check option expressions from rewriter to executor
Other databases (e.g. DB2) are using INSTEAD OF triggersfor view updates
Bernd Helmle Updatable Views What we have and what we want