View triggers pg_east_20110325

1,561 views

Published on

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,561
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

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
  • View triggers pg_east_20110325

    1. 1. View Triggers!JDCon EastMarch 25, 2011Copyright © 2011David Fetter dfetter@vmware.comAll Rights Reserved
    2. 2. ...or does it?
    3. 3. What is a View?
    4. 4. What is a View?• Cached query
    5. 5. What is a View?• Cached query• Modified at runtime
    6. 6. What is a Trigger?• Executable code• Call and Response• Scope: row or statement
    7. 7. What is a View Trigger?• Cached query brings rows• Code executes on each
    8. 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. 9. Why?
    10. 10. QueryRewriteRULEs :P
    11. 11. Actually,
    12. 12. QueryRewriteSUthis is a family presentation
    13. 13. Like C Pre-ProcessorMacros, only Less Fun
    14. 14. No more RULEs! :)
    15. 15. View Triggers Before• For each VIEW • Shadow table • 3 RULEs • INSERT RULE • UPDATE RULE • DELETE RULE • Trigger on Shadow Table
    16. 16. Shadow TableCREATE TABLE v_t_shadow ( old_column, old_list, old_here, new_column, new_list, new_here,);
    17. 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. 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. 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. 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. 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. 22. •Performance? • Not So Much™
    23. 23. •Concurrency? • Whats that?
    24. 24. With Triggers on Views
    25. 25. With Triggers on Views• Thats
    26. 26. With Triggers on Views• Thats• All
    27. 27. With Triggers on Views• Thats• All• Just
    28. 28. With Triggers on Views• Thats• All• Just•A
    29. 29. With Triggers on Views• Thats• All• Just•A• Bad
    30. 30. With Triggers on Views• Thats• All• Just•A• Bad• Memory
    31. 31. View Triggers Now• Create trigger function• Create trigger• DONE!
    32. 32. TableCREATE TABLE triangle_victims ( first_name TEXT, last_name TEXT NOT NULL, age INTEGER, notes TEXT);
    33. 33. ViewCREATE VIEW triangle_victims_vAS SELECT COALESCE( first_name || , ) || last_name AS "full_name", age, notesFROM triangle_victims;
    34. 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. 35. Triangle Shirtwaist Firehttp://en.wikipedia.org/wiki/Triangle_Shirtwaist_Factory_firehttp://law2.umkc.edu/faculty/projects/ftrials/triangle/trianglevictims2.html
    36. 36. • Questions?• Comments?
    37. 37. Thank You!JDCon EastMarch 25, 2011Copyright © 2011David Fetter dfetter@vmware.comAll Rights Reserved

    ×