ct es past_present_future_nycpgday_20130322

27
CTEs Past, Present, and Future PGDay NYC 2013/03/22 Copyright© 2013 David Fetter [email protected]

Upload: david-fetter

Post on 01-Dec-2014

695 views

Category:

Sports


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Ct es past_present_future_nycpgday_20130322

CTEsPast, Present, and FuturePGDay NYC 2013/03/22

Copyright© 2013David [email protected]

Page 2: Ct es past_present_future_nycpgday_20130322
Page 3: Ct es past_present_future_nycpgday_20130322

Past (pre-8.4)

ø

Page 4: Ct es past_present_future_nycpgday_20130322

Past (pre-8.4)

Page 5: Ct es past_present_future_nycpgday_20130322

Past (8.4)

- Add support for WITH clauses (CTEs), including WITH RECURSIVE (Yoshiyuki Asaba, Tatsuo Ishii, Tom)

Page 6: Ct es past_present_future_nycpgday_20130322

Past CTEsWITH [RECURSIVE] t1 [(column type,…)] AS( [SELECT | VALUES][UNION [ALL] [SELECT]),t2 AS…tn AS…SELECT…

Page 7: Ct es past_present_future_nycpgday_20130322

SQL is Turing-Complete

Page 8: Ct es past_present_future_nycpgday_20130322
Page 9: Ct es past_present_future_nycpgday_20130322

Travelling Salesman Problem

Given a number of cities and the costs of travelling from any city to any other city, what is the least-cost round-trip route that visits each city exactly once and then returns to the starting city?

Page 10: Ct es past_present_future_nycpgday_20130322

Present (9.1)

Allow data-modification commands (INSERT/UPDATE/DELETE) in WITH clauses

Page 11: Ct es past_present_future_nycpgday_20130322

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) …

Page 12: Ct es past_present_future_nycpgday_20130322

Future (9.4+)

Let's think this through.

Page 13: Ct es past_present_future_nycpgday_20130322

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) …

Page 14: Ct es past_present_future_nycpgday_20130322

Data Manipulation

WITH [RECURSIVE] t1 [(column type,…)]AS (

DML[UNION [ALL] DML]), ...

DML

Page 15: Ct es past_present_future_nycpgday_20130322

Actually...

Page 16: Ct es past_present_future_nycpgday_20130322

Data Manipulation

WITH [RECURSIVE] t1 [(column type,…)]AS (

DML[(UNION|INTERSECT) [ALL] DML]), ...

DML

Page 17: Ct es past_present_future_nycpgday_20130322

DML That Can't Return Rows...YET!

•COPY

•DO

•EXPLAIN

•SHOW

Page 18: Ct es past_present_future_nycpgday_20130322

Fortunately...

commit 7a3e30e608a25800a1f7fdfaaca4da3f0ac0fb07Author: Tom Lane <[email protected]>Date: Sat Aug 12 02:52:06 2006 +0000 Add INSERT/UPDATE/DELETE RETURNING, with basic docs and regression tests. plpgsql support to come later. Along the way, convert execMain's SELECT INTO support into a DestReceiver, in order to eliminate some ugly special cases. Jonah Harris and 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, 1459 insertions(+), 409 deletions(-)

Page 19: Ct es past_present_future_nycpgday_20130322

Why not go the whole route?

Page 20: Ct es past_present_future_nycpgday_20130322

Data{{Manipula,Defini}tion,C

ontrol}

WITH [RECURSIVE] t1 [(column type,…)]AS (

D(M|C|D)L[(UNION|INTERSECT) [ALL] D(M|C|D)L]), ...

D(M|C|D)L

Page 21: Ct es past_present_future_nycpgday_20130322

OMG!

Page 22: Ct es past_present_future_nycpgday_20130322

What Needs to Happen?

Page 23: Ct es past_present_future_nycpgday_20130322

Powers Needed

1.Return rows (RETURNING)

2.Use rowsets (FROM | USING) (later)

3.Wrap in CTE machinery

Page 24: Ct es past_present_future_nycpgday_20130322

Bikesheds

•Which ones first?

•UPDATE RETURNING (NEW & OLD)

•What should RETURNING be able to return?

•What does FROM/USING even mean?

•et cetæra

Page 25: Ct es past_present_future_nycpgday_20130322

Let's Make This Be!

Page 26: Ct es past_present_future_nycpgday_20130322

Questions?Comments!

Page 27: Ct es past_present_future_nycpgday_20130322

Thanks!

Copyright© 2013David [email protected]