embedded database logic
play

Embedded Database Logic Lecture # 15 Database Systems Andy Pavlo - PowerPoint PPT Presentation

Embedded Database Logic Lecture # 15 Database Systems Andy Pavlo AP AP Computer Science 15-445/15-645 Carnegie Mellon Univ. Fall 2018 2 ADM IN ISTRIVIA Project #3 is due Monday October 19 th Project #4 is due Monday December 10 th


  1. Embedded Database Logic Lecture # 15 Database Systems Andy Pavlo AP AP Computer Science 15-445/15-645 Carnegie Mellon Univ. Fall 2018

  2. 2 ADM IN ISTRIVIA Project #3 is due Monday October 19 th Project #4 is due Monday December 10 th Homework #4 is due Monday November 12 th CMU 15-445/645 (Fall 2018)

  3. 3 UPCO M IN G DATABASE EVEN TS BlazingDB Tech Talk → Thursday October 25 th @ 12pm → CIC - 4th floor (ISTC Panther Hollow Room) Brytlyt Tech Talk → Thursday November 1 st @ 12pm → CIC - 4th floor (ISTC Panther Hollow Room) CMU 15-445/645 (Fall 2018)

  4. 4 O BSERVATIO N Until now, we have assumed that all of the logic for an application is located in the application itself. The application has a "conversation" with the DBMS to store/retrieve data. → Protocols: JDBC, ODBC CMU 15-445/645 (Fall 2018)

  5. 5 CO N VERSATIO N AL DATABASE API Parser Application Planner Optimizer BEGIN Query Execution SQL Program Logic SQL Program Logic ⋮ COMMIT CMU 15-445/645 (Fall 2018)

  6. 5 CO N VERSATIO N AL DATABASE API Parser Application Planner Optimizer BEGIN Query Execution SQL Program Logic SQL Program Logic ⋮ COMMIT CMU 15-445/645 (Fall 2018)

  7. 5 CO N VERSATIO N AL DATABASE API Parser Application Planner Optimizer BEGIN Query Execution SQL Program Logic SQL Program Logic ⋮ COMMIT CMU 15-445/645 (Fall 2018)

  8. 5 CO N VERSATIO N AL DATABASE API Parser Application Planner Optimizer BEGIN Query Execution SQL Program Logic SQL Program Logic ⋮ COMMIT CMU 15-445/645 (Fall 2018)

  9. 5 CO N VERSATIO N AL DATABASE API Parser Application Planner Optimizer BEGIN Query Execution SQL Program Logic SQL Program Logic ⋮ COMMIT CMU 15-445/645 (Fall 2018)

  10. 6 EM BEDDED DATABASE LO GIC Move application logic into the DBMS to avoid multiple network round-trips. Potential Benefits → Efficiency → Reuse CMU 15-445/645 (Fall 2018)

  11. 7 TO DAY'S AGEN DA User-defined Functions Stored Procedures Triggers Change Notifications User-defined Types Views CMU 15-445/645 (Fall 2018)

  12. 8 USER- DEFIN ED FUN CTIO N S A user-defined function (UDF) is a function written by the application developer that extends the system's functionality beyond its built-in operations. → It takes in input arguments (scalars) → Perform some computation → Return a result (scalars, tables) CMU 15-445/645 (Fall 2018)

  13. 9 UDF DEFIN ITIO N Return Types: → Scalar Functions: Return a single data value → Table Functions: Return a single result table. Computation Definition: → SQL Functions → External Programming Language CMU 15-445/645 (Fall 2018)

  14. 10 UDF SQ L FUN CTIO N S CREATE TABLE foo ( A SQL-based UDF contains a list of SQL id INT PRIMARY KEY , statements that the DBMS executes in val VARCHAR (16) order when the UDF is invoked. ); → The function returns whatever the result is of the last query executed; CREATE FUNCTION get_foo( int ) RETURNS foo AS $$ SELECT * FROM foo WHERE foo.id = $1 ; $$ LANGUAGE SQL; CMU 15-445/645 (Fall 2018)

  15. 10 UDF SQ L FUN CTIO N S CREATE TABLE foo ( A SQL-based UDF contains a list of SQL id INT PRIMARY KEY , statements that the DBMS executes in val VARCHAR (16) order when the UDF is invoked. ); → The function returns whatever the result is of the last query executed; CREATE FUNCTION get_foo( int ) Input Args RETURNS foo AS $$ SELECT * FROM foo WHERE foo.id = $1 ; $$ LANGUAGE SQL; CMU 15-445/645 (Fall 2018)

  16. 10 UDF SQ L FUN CTIO N S CREATE TABLE foo ( A SQL-based UDF contains a list of SQL id INT PRIMARY KEY , statements that the DBMS executes in val VARCHAR (16) order when the UDF is invoked. ); → The function returns whatever the result is of the last query executed; CREATE FUNCTION get_foo( int ) RETURNS foo AS $$ Return Args SELECT * FROM foo WHERE foo.id = $1 ; $$ LANGUAGE SQL; CMU 15-445/645 (Fall 2018)

  17. 10 UDF SQ L FUN CTIO N S CREATE TABLE foo ( A SQL-based UDF contains a list of SQL id INT PRIMARY KEY , statements that the DBMS executes in val VARCHAR (16) order when the UDF is invoked. ); → The function returns whatever the result is of the last query executed; CREATE FUNCTION get_foo( int ) RETURNS foo AS $$ SELECT * FROM foo WHERE foo.id = $1 ; Function Body $$ LANGUAGE SQL; CMU 15-445/645 (Fall 2018)

  18. 11 UDF EXTERN AL PRO GRAM M IN G LAN GUAGE Some DBMSs support writing UDFs in languages other than SQL. → SQL Standard : SQL/PSM → Oracle/DB2 : PL/SQL → Postgres : PL/pgSQL → MSSQL/Sybase : Transact-SQL Other systems support more common programming languages: → Sandbox vs. non-Sandbox CMU 15-445/645 (Fall 2018)

  19. 12 PL/ PGSQ L EXAM PLE CREATE OR REPLACE FUNCTION get_foo( int ) RETURNS SETOF foo AS $$ BEGIN RETURN QUERY SELECT * FROM foo WHERE foo.id = $1 ; END ; $$ LANGUAGE plpgsql; CMU 15-445/645 (Fall 2018)

  20. 13 PL/ PGSQ L EXAM PLE (2) CREATE OR REPLACE FUNCTION sum_foo(i int ) RETURNS int AS $$ DECLARE foo_rec RECORD ; DECLARE out INT ; BEGIN out := 0; FOR foo_rec IN SELECT id FROM foo WHERE id > i LOOP out := out + foo_rec.id; END LOOP ; RETURN out; END ; $$ LANGUAGE plpgsql; CMU 15-445/645 (Fall 2018)

  21. 14 STO RED PRO CEDURES A stored procedure is a self-contained function that performs more complex logic inside of the DBMS. → Can have many input/output parameters. → Can modify the database table/structures. → Not normally used within a SQL query. Some DBMSs distinguish UDFs vs. stored procedures, but not all. CMU 15-445/645 (Fall 2018)

  22. 15 STO RED PRO CEDURES Application BEGIN SQL Program Logic SQL Program Logic ⋮ COMMIT CMU 15-445/645 (Fall 2018)

  23. 15 STO RED PRO CEDURES Application PROC(x) CALL PROC(x=99) CMU 15-445/645 (Fall 2018)

  24. 15 STO RED PRO CEDURES Application PROC(x) CALL PROC(x=99) CMU 15-445/645 (Fall 2018)

  25. 16 STO RED PRO CEDURE VS. UDF A UDF is meant to perform a subset of a read-only computation within a query. A stored procedure is meant to perform a complete computation that is independent of a query. CMU 15-445/645 (Fall 2018)

  26. 17 DATABASE TRIGGERS A trigger instructs the DBMS to invoke a UDF when some event occurs in the database. The developer has to define: → What type of event will cause it to fire. → The scope of the event. → When it fires relative to that event. CMU 15-445/645 (Fall 2018)

  27. 18 TRIGGER EXAM PLE CREATE TABLE foo ( CREATE TABLE foo_audit ( id INT PRIMARY KEY , id SERIAL PRIMARY KEY , val VARCHAR (16) foo_id INT REFERENCES foo (id), ); orig_val VARCHAR , cdate TIMESTAMP ); CMU 15-445/645 (Fall 2018)

  28. 18 TRIGGER EXAM PLE CREATE TABLE foo ( CREATE TABLE foo_audit ( id INT PRIMARY KEY , id SERIAL PRIMARY KEY , val VARCHAR (16) foo_id INT REFERENCES foo (id), CREATE OR REPLACE FUNCTION log_foo_updates() ); orig_val VARCHAR , RETURNS trigger AS $$ cdate TIMESTAMP BEGIN ); IF NEW .val <> OLD .val THEN Tuple Versions INSERT INTO foo_audit (foo_id, orig_val, cdate) VALUES ( OLD .id, OLD .val, NOW ()); END IF ; RETURN NEW ; END ; $$ LANGUAGE plpgsql; CMU 15-445/645 (Fall 2018)

  29. 18 TRIGGER EXAM PLE CREATE TABLE foo ( CREATE TABLE foo_audit ( id INT PRIMARY KEY , id SERIAL PRIMARY KEY , val VARCHAR (16) foo_id INT REFERENCES foo (id), CREATE OR REPLACE FUNCTION log_foo_updates() ); orig_val VARCHAR , RETURNS trigger AS $$ cdate TIMESTAMP BEGIN ); IF NEW .val <> OLD .val THEN INSERT INTO foo_audit (foo_id, orig_val, cdate) VALUES ( OLD .id, OLD .val, NOW ()); END IF ; CREATE TRIGGER foo_updates RETURN NEW ; BEFORE UPDATE ON foo FOR EACH ROW END ; EXECUTE PROCEDURE log_foo_updates(); $$ LANGUAGE plpgsql; CMU 15-445/645 (Fall 2018)

  30. 19 TRIGGER DEFIN ITIO N Event Type: Trigger Timing: Event Scope: → Before the statement executes. → INSERT → TABLE → After the statement executes → UPDATE → DATABASE → Before each row that the statement → DELETE → VIEW affects. → TRUNCATE → SYSTEM → After each row that the statement → CREATE affects. → ALTER → Instead of the statement. → DROP CMU 15-445/645 (Fall 2018)

  31. 20 CH AN GE N OTIFICATIO N S A change notification is like a trigger except that the DBMS sends a message to an external entity that something notable has happened in the database. → Think a "pub/sub" system. → Can be chained with a trigger to pass along whenever a change occurs. SQL standard: LISTEN + NOTIFY CMU 15-445/645 (Fall 2018)

  32. 21 N OTIFICATIO N EXAM PLE CREATE OR REPLACE FUNCTION notify_foo_updates() RETURNS trigger AS $$ DECLARE notification JSON ; BEGIN notification = row_to_json ( NEW ); Notification PERFORM pg_notify ('foo_update', Payload notification::text); RETURN NEW ; END ; $$ LANGUAGE plpgsql; CMU 15-445/645 (Fall 2018)

  33. 21 N OTIFICATIO N EXAM PLE CREATE OR REPLACE FUNCTION notify_foo_updates() RETURNS trigger AS $$ DECLARE notification JSON ; BEGIN notification = row_to_json ( NEW ); Notification PERFORM pg_notify ('foo_update', Payload notification::text); RETURN NEW ; END ; CREATE TRIGGER foo_notify $$ LANGUAGE plpgsql; AFTER INSERT ON foo_audit FOR EACH ROW EXECUTE PROCEDURE notify_foo_updates(); CMU 15-445/645 (Fall 2018)

  34. 22 O BSERVATIO N All DBMSs support the basic primitive types in the SQL standard. They also support basic arithmetic and string manipulation on them. But what if we want to store data that doesn't match any of the built-in types? coordinate (x, y, label) CMU 15-445/645 (Fall 2018)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend