• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
View triggers pg_east_20110325
 

View triggers pg_east_20110325

on

  • 1,117 views

PostgreSQL 9.1 has triggers on views!

PostgreSQL 9.1 has triggers on views!

Statistics

Views

Total Views
1,117
Views on SlideShare
1,116
Embed Views
1

Actions

Likes
0
Downloads
9
Comments
0

1 Embed 1

http://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \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 View triggers pg_east_20110325 Presentation Transcript

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