Your SlideShare is downloading. ×
View triggers pg_east_20110325
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

View triggers pg_east_20110325

1,060

Published on

PostgreSQL 9.1 has triggers on views!

PostgreSQL 9.1 has triggers on views!

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,060
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \n
  • First American photo from space (Mercury). Quite a view!\n
  • \n
  • This doesn't make sense!\n
  • \n
  • The query, not the result set, is cached.\n
  • The query, not the result set, is cached.\n
  • \n
  • \n
  • \n
  • Let's go back a bit. What's your least favorite PostgreSQL feature?\n
  • \n
  • \n
  • \n
  • \n
  • This is how we used to do it:\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript

    • 1. View Triggers!JDCon EastMarch 25, 2011Copyright © 2011David Fetter dfetter@vmware.comAll Rights Reserved
    • 2. ...or does it?
    • 3. What is a View?
    • 4. What is a View?• Cached query
    • 5. What is a View?• Cached query• Modified at runtime
    • 6. What is a Trigger?• Executable code• Call and Response• Scope: row or statement
    • 7. What is a View Trigger?• Cached query brings rows• Code executes on each
    • 8. Trigger Contexts SCOPE WHEN EVENT ROW STATEMENT INSERT UPDATE TABLES TABLES/VIEWS BEFORE DELETE TRUNCATE ― TABLES INSERT UPDATE TABLES TABLES/VIEWS AFTER DELETE TRUNCATE ― ― INSERT UPDATE VIEWS ― DELETEINSTEAD OF TRUNCATE ― ―
    • 9. Why?
    • 10. QueryRewriteRULEs :P
    • 11. Actually,
    • 12. QueryRewriteSUthis is a family presentation
    • 13. Like C Pre-ProcessorMacros, only Less Fun
    • 14. No more RULEs! :)
    • 15. View Triggers Before• For each VIEW • Shadow table • 3 RULEs • INSERT RULE • UPDATE RULE • DELETE RULE • Trigger on Shadow Table
    • 16. Shadow TableCREATE TABLE v_t_shadow ( old_column, old_list, old_here, new_column, new_list, new_here,);
    • 17. INSERT RULECREATE RULE t_v_insert ON INSERT TO t_v DO INSTEAD INSERT INTO t_v_shadow ( action, new_column, new_list, new_here) VALUES ( I, NEW.t_v_column, NEW.list, NEW.here );
    • 18. DELETE RULECREATE RULE t_v_delete ON DELETE TO t_v DO INSTEAD INSERT INTO t_v_shadow ( action, column, list, here) VALUES ( D, OLD.t_v_column, OLD.list, OLD.here );
    • 19. UPDATE RULECREATE RULE t_v_update ON UPDATE TO t_v DO INSTEAD INSERT INTO t_v_shadow ( action, column, list, here) VALUES ( U, OLD.t_v_column, OLD.list, OLD.here, NEW.t_v_column, NEW.list, NEW.here );
    • 20. Shadow Table Trigger FunctionCREATE OR REPLACE FUNCTION shadow_table_t_v()RETURNS TRIGGERLANGUAGE plpgsqlAS $$BEGIN IF NEW.action = I THEN ... ELSIF NEW.action = D THEN ... ELSIF NEW.action = U THEN ... END IF; RETURN NULL;END;$$;
    • 21. Shadow TriggerCREATE TRIGGER shadow_table_t_v BEFORE INSERT OR UPDATE OR DELETE ON shadow_t_v FOR EACH ROW EXECUTE PROCEDURE shadow_trigger_t_v();
    • 22. •Performance? • Not So Much™
    • 23. •Concurrency? • Whats that?
    • 24. With Triggers on Views
    • 25. With Triggers on Views• Thats
    • 26. With Triggers on Views• Thats• All
    • 27. With Triggers on Views• Thats• All• Just
    • 28. With Triggers on Views• Thats• All• Just•A
    • 29. With Triggers on Views• Thats• All• Just•A• Bad
    • 30. With Triggers on Views• Thats• All• Just•A• Bad• Memory
    • 31. View Triggers Now• Create trigger function• Create trigger• DONE!
    • 32. TableCREATE TABLE triangle_victims ( first_name TEXT, last_name TEXT NOT NULL, age INTEGER, notes TEXT);
    • 33. ViewCREATE VIEW triangle_victims_vAS SELECT COALESCE( first_name || , ) || last_name AS "full_name", age, notesFROM triangle_victims;
    • 34. Trigger FunctionCREATE OR REPLACE FUNCTION write_triangle_victims_v()RETURNS TRIGGERLANGUAGE plpgsqlAS $$DECLARE name_split TEXT[];BEGIN IF TG_OP = INSERT THEN name_split = string_to_array(NEW.full_name, ); INSERT INTO triangle_victims (first_name, last_name, age, notes) VALUES ( array_to_string( name_split[1:array_upper(name_split,1)-1], ), name_split[array_upper(name_split,1)], NEW.age, NEW.notes ); RETURN NEW; ELSIF TG_OP = DELETE THEN /* Similar code goes here */ ELSIF TG_OP = UPDATE THEN /* And slightly more complicated here */ END IF;END;$$;
    • 35. Triangle Shirtwaist Firehttp://en.wikipedia.org/wiki/Triangle_Shirtwaist_Factory_firehttp://law2.umkc.edu/faculty/projects/ftrials/triangle/trianglevictims2.html
    • 36. • Questions?• Comments?
    • 37. Thank You!JDCon EastMarch 25, 2011Copyright © 2011David Fetter dfetter@vmware.comAll Rights Reserved

    ×