Writeable CTEs, A Revolution in SQLPGConMay 20, 2011Copyright © 2011David Fetter dfetter@vmware.comAll Rights Reserved
Current CTEsWITH [RECURSIVE] t1 [(column type,…)] AS(    [SELECT | VALUES][UNION [ALL]    [SELECT]),t2 AS…tn AS…SELECT…
Travelling Salesman ProblemGiven a number of cities and the costs of travellingfrom any city to any other city, what is th...
OBTWWith CTE and Windowing, SQL is Turing Complete.
What Didnt theOld Syntax Do?
WRITE!WITH [RECURSIVE] t1 [(column type,…)] AS(    [SELECT | VALUES |   (INSERT | UPDATE | DELETE) [RETURNING]][UNION [ALL...
For 9.1:Simple Partition Management CREATE TABLE log (     ts TIMESTAMPTZ NOT NULL,     msg TEXT NOT NULL );
For 9.1:Simple Partition Management CREATE TABLE log_201101 () INHERITS(log); ALTER TABLE log_201101 ADD CONSTRAINT right_...
For 9.1:    Simple Partition Managementberliner@pgday_eu_2010:54321# WITH t1 AS (    DELETE FROM ONLY log    WHERE ts >=20...
Query Clustering:             I/O MinimizationCREATE TABLE person (    id SERIAL PRIMARY KEY,    first_name TEXT,    last_...
Query Clustering:         I/O MinimizationCREATE TABLE im (    id SERIAL PRIMARY KEY,    provider TEXT NOT NULL, /* should...
Query Clustering:      I/O MinimizationCREATE TABLE phone (    id SERIAL PRIMARY KEY,    country_code TEXT NOT NULL,    ph...
Query Clustering: I/O MinimizationCREATE TABLE street (    id SERIAL PRIMARY KEY,    street1 TEXT NOT NULL,    street2 TEX...
Query Clustering:   I/O MinimizationCREATE TABLE person_im (    person_id INTEGER NOT NULL REFERENCES person (id),    im_i...
Query Clustering:         I/O MinimizationWITH t_person AS (    INSERT INTO person (first_name, last_name)    VALUES (Davi...
Query Clustering:        I/O Minimizationt_im AS (    INSERT INTO im (provider, handle)    VALUES        (Yahoo!, dfetter)...
Query Clustering:        I/O Minimizationt_phone (phone_id) AS (    INSERT INTO phone (country_code, phone_number)    VALU...
Query Clustering:                     I/O Minimizationt_street AS (    INSERT INTO street (street1, city, state, country, ...
Query Clustering:  I/O MinimizationVALUES(true);
Query Clustering:     Transaction ManagementCREATE TABLE foo (    id SERIAL PRIMARY KEY,    bar_id INTEGER NOT NULL);CREAT...
Query Clustering:    Transaction ManagementWITH t AS(! INSERT INTO foo(id, bar_id)! VALUES(! ! DEFAULT,! ! nextval(pg_get_...
Materialized Views WITH t AS (   DELETE FROM foo   RETURNING a, b ) INSERT INTO mv_foo SELECT a, SUM(b) FROM t GROUP BY a
Just Cool Stuff™WITH t AS (  INSERT INTO foo  VALUES (...),...,(...)  RETURNING a)SELECT bar.a, bar.bFROM bar JOIN t USING...
Head-Scratcher: SNAPSHOTSWITH t AS (DELETE FROM foo)SELECT * FROM foo;(0 rows)?(100123 rows)?
Howd He Do That?!?First try: David digs into the grammar and gets cuta few times.
Howd He Do That?!?First try: Marko reworks the planner. It needs toknow when it creates a ModifyTable node. These usedto h...
Howd He Do That?!?First try: Marko reworks the executor.   It needs newnodes. Mmmm...nodes.
Howd He Do That?!?Marko reworks the executor, Part II:Copy & Paste. Now its getting ugly...
Howd He Do That?!?Jaime Casanova, Tom Lane, and Robert Haas look atthe reworked executor.Doh!
Howd He Do That?!?       FAIL!Way too much code copying from toplevel to the new nodes.
Howd He Do That?!?Planner changes for ModifyTable node (a few)
Howd He Do That?!?Executor changes: ONE new node called ModifyTable
Howd He Do That?!?Marko Tiikkaja restructures the whole code base for theModifyTable node. "The usual stuff," (he said cas...
Howd He Do That?!? WIN!
Next StepsINSERT, UPDATE and DELETE on the top level.Finish the patch   EXPLAIN ANALYZE   SPI   SQL functionsBug fixes, cle...
Future?DCL (GRANT/REVOKE)DDL (CREATE/ALTER/DROP)
Where Is It?!?http://www.postgresql.org/developer/beta
WITH t AS (    VALUES        (Questions),        (Comments),        (Rotten Tomatoes))INSERT INTO davidfetterSELECT * FROM...
Thanks, eh! Copyright © 2011 David Fetter dfetter@vmware.com All Rights Reserved
Writeable ct es_pgcon_may_2011
Upcoming SlideShare
Loading in …5
×

Writeable ct es_pgcon_may_2011

1,603 views

Published on

SQL has Common Table Expressions. With PostgreSQL 9.1, you can use them to write.

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Writeable ct es_pgcon_may_2011

  1. 1. Writeable CTEs, A Revolution in SQLPGConMay 20, 2011Copyright © 2011David Fetter dfetter@vmware.comAll Rights Reserved
  2. 2. Current CTEsWITH [RECURSIVE] t1 [(column type,…)] AS( [SELECT | VALUES][UNION [ALL] [SELECT]),t2 AS…tn AS…SELECT…
  3. 3. Travelling Salesman ProblemGiven a number of cities and the costs of travellingfrom any city to any other city, what is the least-cost round-trip route that visits each city exactlyonce and then returns to the starting city?
  4. 4. OBTWWith CTE and Windowing, SQL is Turing Complete.
  5. 5. What Didnt theOld Syntax Do?
  6. 6. WRITE!WITH [RECURSIVE] t1 [(column type,…)] AS( [SELECT | VALUES | (INSERT | UPDATE | DELETE) [RETURNING]][UNION [ALL] [SELECT | VALUES | (INSERT | UPDATE | DELETE) [RETURNING]])(SELECT | INSERT | UPDATE | DELETE) …
  7. 7. For 9.1:Simple Partition Management CREATE TABLE log ( ts TIMESTAMPTZ NOT NULL, msg TEXT NOT NULL );
  8. 8. For 9.1:Simple Partition Management CREATE TABLE log_201101 () INHERITS(log); ALTER TABLE log_201101 ADD CONSTRAINT right_month CHECK( ts >= 2011-01-01 AND ts < 2011-02-01);
  9. 9. For 9.1: Simple Partition Managementberliner@pgday_eu_2010:54321# WITH t1 AS ( DELETE FROM ONLY log WHERE ts >=2011-01-01 AND ts < 2011-02-01 RETURNING *)INSERT INTO log_201101 SELECT * FROM t1;INSERT 0 45270
  10. 10. Query Clustering: I/O MinimizationCREATE TABLE person ( id SERIAL PRIMARY KEY, first_name TEXT, last_name TEXT, CHECK (CASE WHEN first_name IS NULL THEN 0 ELSE 1 END + CASE WHEN last_name IS NULL THEN 0 ELSE 1 END >= 1), birthdate DATE NOT NULL, gender TEXT);
  11. 11. Query Clustering: I/O MinimizationCREATE TABLE im ( id SERIAL PRIMARY KEY, provider TEXT NOT NULL, /* should be fk */ handle TEXT NOT NULL);
  12. 12. Query Clustering: I/O MinimizationCREATE TABLE phone ( id SERIAL PRIMARY KEY, country_code TEXT NOT NULL, phone_number TEXT NOT NULL, extension TEXT);
  13. 13. Query Clustering: I/O MinimizationCREATE TABLE street ( id SERIAL PRIMARY KEY, street1 TEXT NOT NULL, street2 TEXT, street3 TEXT, city TEXT NOT NULL, state TEXT, country TEXT NOT NULL, post_code TEXT);
  14. 14. Query Clustering: I/O MinimizationCREATE TABLE person_im ( person_id INTEGER NOT NULL REFERENCES person (id), im_id INTEGER NOT NULL REFERENCES im (id), UNIQUE (person_id, im_id));CREATE TABLE person_phone ( person_id INTEGER NOT NULL REFERENCES person (id), phone_id INTEGER NOT NULL REFERENCES phone (id), UNIQUE (person_id, phone_id));CREATE TABLE person_street ( person_id INTEGER NOT NULL REFERENCES person (id), street_id INTEGER NOT NULL REFERENCES street (id), UNIQUE (person_id, street_id));
  15. 15. Query Clustering: I/O MinimizationWITH t_person AS ( INSERT INTO person (first_name, last_name) VALUES (David, Fetter) RETURNING id),
  16. 16. Query Clustering: I/O Minimizationt_im AS ( INSERT INTO im (provider, handle) VALUES (Yahoo!, dfetter), (AIM, dfetter666), (XMPP, davidfetter@postgresql.org) RETURNING id),t_person_im AS ( INSERT INTO person_im SELECT * FROM t_person CROSS JOIN t_im),
  17. 17. Query Clustering: I/O Minimizationt_phone (phone_id) AS ( INSERT INTO phone (country_code, phone_number) VALUES (+1,415 235 3778), (+1,650 427 3751) RETURNING id),t_person_phone AS ( INSERT INTO person_phone SELECT * FROM t_person CROSS JOIN t_phone),
  18. 18. Query Clustering: I/O Minimizationt_street AS ( INSERT INTO street (street1, city, state, country, post_code) VALUES (438 W. Grand Ave., Apartment 727,Oakland,California,USA,94612-2341), (3421 Hillview Avenue, Palo Alto,California,USA,94304)),t_person_street AS ( INSERT INTO person_street SELECT * FROM t_person CROSS JOIN t_street)
  19. 19. Query Clustering: I/O MinimizationVALUES(true);
  20. 20. Query Clustering: Transaction ManagementCREATE TABLE foo ( id SERIAL PRIMARY KEY, bar_id INTEGER NOT NULL);CREATE TABLE bar ( id SERIAL PRIMARY KEY, foo_id INTEGER NOT NULL REFERENCES foo(id) ON DELETE CASCADE INITIALLY DEFERRED);ALTER TABLE foo ADD FOREIGN KEY (bar_id) REFERENCES bar(id) ON DELETE CASCADE INITIALLY DEFERRED;
  21. 21. Query Clustering: Transaction ManagementWITH t AS(! INSERT INTO foo(id, bar_id)! VALUES(! ! DEFAULT,! ! nextval(pg_get_serial_sequence(bar, id)) )! RETURNING id AS foo_id, bar_id)INSERT INTO bar(id,foo_id)SELECT bar_id, foo_id FROM t RETURNING *;
  22. 22. Materialized Views WITH t AS ( DELETE FROM foo RETURNING a, b ) INSERT INTO mv_foo SELECT a, SUM(b) FROM t GROUP BY a
  23. 23. Just Cool Stuff™WITH t AS ( INSERT INTO foo VALUES (...),...,(...) RETURNING a)SELECT bar.a, bar.bFROM bar JOIN t USING(a)
  24. 24. Head-Scratcher: SNAPSHOTSWITH t AS (DELETE FROM foo)SELECT * FROM foo;(0 rows)?(100123 rows)?
  25. 25. Howd He Do That?!?First try: David digs into the grammar and gets cuta few times.
  26. 26. Howd He Do That?!?First try: Marko reworks the planner. It needs toknow when it creates a ModifyTable node. These usedto have another name.
  27. 27. Howd He Do That?!?First try: Marko reworks the executor. It needs newnodes. Mmmm...nodes.
  28. 28. Howd He Do That?!?Marko reworks the executor, Part II:Copy & Paste. Now its getting ugly...
  29. 29. Howd He Do That?!?Jaime Casanova, Tom Lane, and Robert Haas look atthe reworked executor.Doh!
  30. 30. Howd He Do That?!? FAIL!Way too much code copying from toplevel to the new nodes.
  31. 31. Howd He Do That?!?Planner changes for ModifyTable node (a few)
  32. 32. Howd He Do That?!?Executor changes: ONE new node called ModifyTable
  33. 33. Howd He Do That?!?Marko Tiikkaja restructures the whole code base for theModifyTable node. "The usual stuff," (he said casually) for new nodes.
  34. 34. Howd He Do That?!? WIN!
  35. 35. Next StepsINSERT, UPDATE and DELETE on the top level.Finish the patch EXPLAIN ANALYZE SPI SQL functionsBug fixes, cleanup, testingRECURSIVEOptimization
  36. 36. Future?DCL (GRANT/REVOKE)DDL (CREATE/ALTER/DROP)
  37. 37. Where Is It?!?http://www.postgresql.org/developer/beta
  38. 38. WITH t AS ( VALUES (Questions), (Comments), (Rotten Tomatoes))INSERT INTO davidfetterSELECT * FROM tRETURNING...
  39. 39. Thanks, eh! Copyright © 2011 David Fetter dfetter@vmware.com All Rights Reserved

×