Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ct es past_present_future_nycpgday_20130322

  • Login to see the comments

  • Be the first to like this

Ct es past_present_future_nycpgday_20130322

  1. 1. CTEs Past, Present, and Future PGDay NYC 2013/03/22Copyright© 2013David Fetterdavid@fetter.org
  2. 2. Past (pre-8.4) ø
  3. 3. ☹Past (pre-8.4)
  4. 4. Past (8.4)- Add support for WITH clauses (CTEs),including WITH RECURSIVE(Yoshiyuki Asaba, Tatsuo Ishii, Tom)
  5. 5. Past CTEsWITH [RECURSIVE] t1 [(column type,…)] AS( [SELECT | VALUES][UNION [ALL] [SELECT]),t2 AS…tn AS…SELECT…
  6. 6. SQL is Turing-Complete
  7. 7. 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?
  8. 8. Present (9.1)Allow data-modification commands(INSERT/UPDATE/DELETE) inWITH clauses
  9. 9. YAY!WITH [RECURSIVE] t1 [(column type,…)] AS( [SELECT | VALUES | (INSERT | UPDATE | DELETE) [RETURNING]][UNION [ALL] [SELECT | VALUES | (INSERT | UPDATE | DELETE) [RETURNING]]), ...(SELECT | INSERT | UPDATE | DELETE) …
  10. 10. Future (9.4+)Lets think this through.
  11. 11. NOW!WITH [RECURSIVE] t1 [(column type,…)] AS( [SELECT | VALUES | (INSERT | UPDATE | DELETE) [RETURNING]][UNION [ALL] [SELECT | VALUES | (INSERT | UPDATE | DELETE) [RETURNING]]), ...(SELECT | INSERT | UPDATE | DELETE) …
  12. 12. Data ManipulationWITH [RECURSIVE] t1 [(column type,…)]AS ( DML[UNION [ALL] DML]), ...DML
  13. 13. Actually...
  14. 14. Data ManipulationWITH [RECURSIVE] t1 [(column type,…)]AS ( DML[(UNION|INTERSECT) [ALL] DML]), ...DML
  15. 15. DML That Cant Return Rows...YET! Return Rows...YET!• COPY• DO• EXPLAIN• SHOW
  16. 16. Fortunately...commit 7a3e30e608a25800a1f7fdfaaca4da3f0ac0fb07Author: Tom Lane <tgl@sss.pgh.pa.us>Date:Sat Aug 12 02:52:06 2006 +0000 Add INSERT/UPDATE/DELETE RETURNING, with basic docsand regression tests. plpgsql support to come later. Along the way, convert execMains SELECTINTO support into a DestReceiver, in order to eliminate some ugly special cases. Jonah Harrisand Tom Lane doc/src/sgml/ref/delete.sgml | 56 +++++++++- doc/src/sgml/ref/insert.sgml | 65 ++++++++++-- doc/src/sgml/ref/update.sgml | 57 +++++++++- src/backend/access/common/printtup.c | 8 +- src/backend/commands/prepare.c | 9 +- src/backend/executor/execMain.c | 655 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------- src/backend/executor/spi.c | 9 +- src/backend/nodes/copyfuncs.c | 9 +- src/backend/nodes/equalfuncs.c | 9 +- src/backend/nodes/outfuncs.c | 4 +- src/backend/nodes/readfuncs.c | 4 +- src/backend/optimizer/plan/planagg.c | 3+- src/backend/optimizer/plan/planner.c | 39 ++++++- src/backend/optimizer/plan/setrefs.c | 137 ++++++++++++++++++------ src/backend/optimizer/prep/prepjointree.c | 6 +-src/backend/optimizer/prep/preptlist.c | 39 ++++++- src/backend/optimizer/util/clauses.c | 5 +- src/backend/parser/analyze.c | 89 ++++++++++++++--src/backend/parser/gram.y | 20 +++- src/backend/parser/keywords.c | 3 +- src/backend/tcop/dest.c | 9 +- src/backend/tcop/pquery.c | 152 +++++++++++++++----------- src/backend/tcop/utility.c | 4 +- src/backend/utils/adt/ruleutils.c | 123 +++++++++++++++------- src/include/catalog/catversion.h | 4 +-src/include/executor/executor.h | 3 +- src/include/nodes/execnodes.h | 4 +- src/include/nodes/parsenodes.h | 24 ++++- src/include/optimizer/planmain.h | 5 +-src/include/tcop/dest.h | 9 +- src/include/utils/portal.h | 16 ++- src/test/regress/expected/returning.out | 195 ++++++++++++++++++++++++++++++++++src/test/regress/parallel_schedule | 4 +- src/test/regress/serial_schedule | 3 +- src/test/regress/sql/returning.sql | 87 +++++++++++++++ 35 files changed, 1459insertions(+), 409 deletions(-) doc/src/sgml/ref/delete.sgml | 56 +++++++++- doc/src/sgml/ref/insert.sgml | 65 ++++++++++-- doc/src/sgml/ref/update.sgml | 57 +++++++++-src/backend/access/common/printtup.c | 8 +- src/backend/commands/prepare.c | 9 +- src/backend/executor/execMain.c | 655 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------- src/backend/executor/spi.c | 9 +- src/backend/nodes/copyfuncs.c | 9 +-src/backend/nodes/equalfuncs.c | 9 +- src/backend/nodes/outfuncs.c | 4 +- src/backend/nodes/readfuncs.c | 4 +- src/backend/optimizer/plan/planagg.c | 3 +-src/backend/optimizer/plan/planner.c | 39 ++++++- src/backend/optimizer/plan/setrefs.c | 137 ++++++++++++++++++------ src/backend/optimizer/prep/prepjointree.c | 6 +-src/backend/optimizer/prep/preptlist.c | 39 ++++++- src/backend/optimizer/util/clauses.c | 5 +- src/backend/parser/analyze.c | 89 ++++++++++++++--src/backend/parser/gram.y | 20 +++- src/backend/parser/keywords.c | 3 +- src/backend/tcop/dest.c | 9 +- src/backend/tcop/pquery.c | 152 +++++++++++++++----------- src/backend/tcop/utility.c | 4 +- src/backend/utils/adt/ruleutils.c | 123 +++++++++++++++------- src/include/catalog/catversion.h | 4 +-src/include/executor/executor.h | 3 +- src/include/nodes/execnodes.h | 4 +- src/include/nodes/parsenodes.h | 24 ++++- src/include/optimizer/planmain.h | 5 +-src/include/tcop/dest.h | 9 +- src/include/utils/portal.h | 16 ++- src/test/regress/expected/returning.out | 195 ++++++++++++++++++++++++++++++++++src/test/regress/parallel_schedule | 4 +- src/test/regress/serial_schedule | 3 +- src/test/regress/sql/returning.sql | 87 +++++++++++++++ 35 files changed, 1459insertions(+), 409 deletions(-)
  17. 17. Why not go the whole route?
  18. 18. Data {{Manipula,Defini}tion,Control} {{Manipula,Defini}tion,Control}WITH [RECURSIVE] t1 [(column type,…)]AS ( D(M|C|D)L[(UNION|INTERSECT) [ALL] D(M|C|D)L]), ...D(M|C|D)L
  19. 19. OMG!
  20. 20. What Needs to Happen?
  21. 21. Powers Needed1.Return rows (RETURNING)2.Use rowsets (FROM | USING) (later)3.Wrap in CTE machinery
  22. 22. Bikesheds• Which ones first?• UPDATE RETURNING (NEW & OLD)• What should RETURNING be able to return?• What does FROM/USING even mean?• et cetæra
  23. 23. Lets Make This Be!
  24. 24. Questions?Comments!Comments!
  25. 25. Thanks!Copyright© 2013David Fetterdavid@fetter.org

×