postgresql is yesql!tapoueh.org/images/confs/allyourbaseconf2015.pdf · block range index for big...

52
PostgreSQL is YeSQL! All Your Base Conference 2015 Dimitri Fontaine [email protected] @tapoueh 13 November 2015 Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 1 / 52

Upload: trannhan

Post on 03-Feb-2018

231 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

PostgreSQL is YeSQL!All Your Base Conference 2015

Dimitri Fontaine [email protected]

@tapoueh

13 November 2015

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 1 / 52

Page 2: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Dimitri Fontaine

Principal Consultant at 2ndQuadrant

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 2 / 52

Page 3: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Relational Database Management System

Antikythera mechanism

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 3 / 52

Page 4: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Relational Database Management System

Antikythera mechanism

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 4 / 52

Page 5: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Relational Database Management System

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 5 / 52

Page 6: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

PostgreSQL is YeSQL!

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 6 / 52

Page 7: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

PostgreSQL is YeSQL!

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 7 / 52

Page 8: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Relational Database Management System

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 8 / 52

Page 9: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Relational Database Management System

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 9 / 52

Page 10: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Relational Database Management System

D is for Durability

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 10 / 52

Page 11: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Transactions

A is for Atomic, that is,Transactions

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 11 / 52

Page 12: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Transactions

BEGIN;

DROP TABLE critical_data;

ROLLBACK;

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 12 / 52

Page 13: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Consistent Hot Backups

I is for Isolated, that is,Transactions

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 13 / 52

Page 14: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

You don’t want backups, you want data loss recovery!

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 14 / 52

Page 15: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Consistency

C is for Consistency

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 15 / 52

Page 16: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Scaling: up or out?

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 16 / 52

Page 17: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

High Availability of Services and Data

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 17 / 52

Page 18: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

High Availability: Consistency and Durability

Primary

Archive

Standby 1 Standby 2

archive command

Streaming Replication Streaming Replication

restore command

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 18 / 52

Page 19: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

High Availability

DC1 DC2

SR S1

P1 P2

S2 S3

BDR

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 19 / 52

Page 20: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Consistency

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 20 / 52

Page 21: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Data Types and Constraints

Data integrity First Layer: data type

• Integer

• Arbitrary precision

numbers, UUID

• Floating point

• Character, Text

• Bytea, bitstring

• Date/Time, Time Zones

• Boolean

• Enum, Arrays, Composite

Types, Range Types

• Point, Line Segments,

Boxes

• Paths, Polygons, Circles

• Inet, CIDR, Macaddr

• JSON, XML

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 21 / 52

Page 22: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Advanced data types and constraints

CREATE TABLE reservation

(

room text,

professor text,

during period,

EXCLUDE USING gist

(

room with =,

during with &&

)

);

CREATE TABLE circles (

c circle,

EXCLUDE USING gist

(c WITH &&)

);

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 22 / 52

Page 23: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Structured Query Language

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 23 / 52

Page 24: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Finding the last counter value before reset

Write some SQL here

tick | nb | max

------+----+-----

1 | 0 |

2 | 10 |

3 | 20 |

4 | 30 |

5 | 40 | 40

6 | 0 |

7 | 20 |

8 | 30 |

9 | 60 | 60

(9 rows)

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 24 / 52

Page 25: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Window Functions: lead() over()

select tick,

nb,

lead(nb) over (order by tick)

from measures;

tick | nb | lead

------+----+------

1 | 0 | 10

2 | 10 | 20

3 | 20 | 30

4 | 30 | 40

5 | 40 | 0

6 | 0 | 20

7 | 20 | 30

8 | 30 | 60

9 | 60 |

(9 rows)

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 25 / 52

Page 26: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Window Functions and CASE

select tick, nb,

case when lead(nb) over w < nb

then nb

when lead(nb) over w is null

then nb

else null

end as max

from measures

window w as (order by tick);

tick | nb | max

------+----+-----

1 | 0 |

2 | 10 |

3 | 20 |

4 | 30 |

5 | 40 | 40

6 | 0 |

7 | 20 |

8 | 30 |

9 | 60 | 60

(9 rows)

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 26 / 52

Page 27: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

PostgreSQL Extensibility

PostgreSQL is highly extensible

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 27 / 52

Page 28: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

B-Tree

Balanced Tree, the default index type

• Built for speed

• unique concurrency tricks

• Balanced

• support function: cmp

• operators: <= < = > >=

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 28 / 52

Page 29: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Generalized Index Search Tree

GiST or the Indexing API

• Built for comfort

• Balanced

• API: consistent, same, union

• API: penalty, picksplit

• API: compress, decompress

• operators: @> <@ && @@ = &< &>

<<| ...

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 29 / 52

Page 30: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Generalized Inverted iNdex

Indexing several pointers per value, inversed cardinality

• Built for Text Search and Arrays

• Balanced

• API: compare, consistent

• API: extractValue,extractQuery

• operators: @> <@ && =

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 30 / 52

Page 31: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

GIN indexes main use case

PostgreSQL Full Text Search

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 31 / 52

Page 32: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Spaced Partition GiST

Unbalanced index, used forGIS

!"#$%&'()*+,-.%/#,0,(%12$'#-%%%%%%%%3

45,+67899.%%!

))':'.%;'<%9=678.%7899

!"#$%&''

!!"#$%&'()*+,"()

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 32 / 52

Page 33: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

BRIN

Block Range Index for Big Data

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 33 / 52

Page 34: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Structured Query Language

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 34 / 52

Page 35: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

IP Ranges, ip4r, Geolocation

PostgreSQL allows using SQL and JOINs to match IP4R with geolocation.

select *

from geolite.blocks

join geolite.location

using(locid)

where iprange

>>=

’74.125.195.147’;

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 35 / 52

Page 36: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

IP Ranges, ip4r, Geolocation

PostgreSQL allows using SQL and JOINs to match IP4R with geolocation.

select *

from geolite.blocks

join geolite.location

using(locid)

where iprange

>>=

’74.125.195.147’;

-[ RECORD 1 ]----------------------------

locid | 2703

iprange | 74.125.189.24-74.125.255.255

country | US

region | CA

city | Mountain View

postalcode | 94043

location | (-122.0574,37.4192)

metrocode | 807

areacode | 650

Time: 1.335 ms

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 36 / 52

Page 37: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Geolocation: ip4r meets earthdistance

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 37 / 52

Page 38: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Some pubs nearby... some place...

with geoloc as

(

select location as l

from location

join blocks using(locid)

where iprange

>>=

’212.58.251.195’

)

select name,

pos <@> l miles

from pubnames, geoloc

order by pos <-> l

limit 10;

name | miles

--------------------+-------

Blue Anchor | 0.299

Dukes Head | 0.360

Blue Ball | 0.337

Bell (aka The Rat) | 0.481

on the Green | 0.602

Fox & Hounds | 0.549

Chequers | 0.712

Sportsman | 1.377

Kingswood Arms | 1.205

Tattenham Corner | 2.007

(10 rows)

Time: 3.275 ms

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 38 / 52

Page 39: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

NBA Games Statistics

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 39 / 52

Page 40: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

NBA Games Statistics

An interesting factoid: the team that recorded the fewest defensiverebounds in a win was the 1995-96 Toronto Raptors, who beat theMilwaukee Bucks 93-87 on 12/26/1995 despite recording only 14 defensiverebounds.

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 40 / 52

Page 41: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

PostgreSQL Data Mining

with stats(game, team, drb, min) as (

select ts.game, ts.team, drb, min(drb) over ()

from team_stats ts

join winners w on w.id = ts.game

and w.winner = ts.team

)

select game.date::date,

host.name || ’ -- ’ || host_score as host,

guest.name || ’ -- ’ || guest_score as guest,

stats.drb as winner_drb

from stats

join game on game.id = stats.game

join team host on host.id = game.host

join team guest on guest.id = game.guest

where drb = min;

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 41 / 52

Page 42: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

PostgreSQL Data Mining-[ RECORD 1 ]----------------------------

date | 1995-12-26

host | Toronto Raptors -- 93

guest | Milwaukee Bucks -- 87

winner_drb | 14

-[ RECORD 2 ]----------------------------

date | 1996-02-02

host | Golden State Warriors -- 114

guest | Toronto Raptors -- 111

winner_drb | 14

-[ RECORD 3 ]----------------------------

date | 1998-03-31

host | Vancouver Grizzlies -- 101

guest | Dallas Mavericks -- 104

winner_drb | 14

-[ RECORD 4 ]----------------------------

date | 2009-01-14

host | New York Knicks -- 128

guest | Washington Wizards -- 122

winner_drb | 14

Time: 126.276 msDimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 42 / 52

Page 43: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Pure SQL Histogram drawing

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 43 / 52

Page 44: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Pure SQL Histogram drawing

with drb_stats as (

select min(drb) as min,

max(drb) as max

from team_stats

),

histogram as (

select width_bucket(drb, min, max, 9) as bucket,

int4range(min(drb), max(drb), ’[]’) as range,

count(*) as freq

from team_stats, drb_stats

group by bucket

order by bucket

)

select bucket, range, freq,

repeat(’*’, (freq::float / max(freq) over() * 30)::int) as bar

from histogram;

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 44 / 52

Page 45: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Pure SQL Histogram drawing

bucket | range | freq | bar

--------+---------+-------+--------------------------------

1 | [10,15) | 52 |

2 | [15,20) | 1363 | **

3 | [20,25) | 8832 | *************

4 | [25,30) | 20917 | ******************************

5 | [30,35) | 20681 | ******************************

6 | [35,40) | 9166 | *************

7 | [40,45) | 2093 | ***

8 | [45,50) | 247 |

9 | [50,54) | 20 |

10 | [54,55) | 1 |

(10 rows)

Time: 53.570 ms

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 45 / 52

Page 46: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Writeable CTE

with queue as (

insert into queue (extension)

select id

from extension

where shortname = $1

returning id, extension

)

select q.id, e.id as ext_id,

e.fullname, e.uri, e.description

from queue q

join extension e on q.extension = e.id;

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 46 / 52

Page 47: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

PostgreSQL Joins

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 47 / 52

Page 48: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

PostgreSQL Joins

WITH upd AS (

UPDATE target t

SET counter = t.counter + s.counter,

FROM source s

WHERE t.id = s.id

RETURNING s.id

)

INSERT INTO target(id, counter)

SELECT id, sum(counter)

FROM source s LEFT JOIN upd t USING(id)

WHERE t.id IS NULL

GROUP BY s.id

RETURNING t.id

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 48 / 52

Page 49: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

PostgreSQL Lateral Joins

Fetch the 10 most recent news for each topic (Top-N)

SELECT n.*

FROM subscriptions s

JOIN LATERAL (SELECT *

FROM news n

WHERE n.topic = s.topic

ORDER BY n.created DESC

LIMIT 10

) top_news ON (true)

WHERE s.user_id = ?

ORDER BY n.created DESC

LIMIT 10

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 49 / 52

Page 50: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 50 / 52

Page 51: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

PostgreSQL is YeSQL!

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 51 / 52

Page 52: PostgreSQL is YeSQL!tapoueh.org/images/confs/AllYourBaseConf2015.pdf · Block Range Index for Big Data ... PostgreSQL Data Mining ... Dimitri Fontaine dimitri@2ndQuadrant.fr @tapoueh

Questions?

Now is the time to ask!

Dimitri Fontaine [email protected] @tapoueh PostgreSQL is YeSQL! 13 November 2015 52 / 52