37
PostgreSQL, The Big, The Fast and The Ugly Il piu’ avanzato database open source come big data player Federico Campoli Brandwatch.com 25 Ottobre 2014 Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 1 / 34

PostgreSQL, The Big, The Fast and The Ugly

Embed Size (px)

DESCRIPTION

PostgreSQL negli ultimi anni ha aggiunto funzionalita’ “nosql” ACID compliant e si propone con forza quale attore nell’era di big data. Dopo una rapida introduzione ai dati schemaless HSTORE e JSON verranno illustrate le problematiche correlate usando un caso reale.

Citation preview

Page 1: PostgreSQL, The Big, The Fast and The Ugly

PostgreSQL, The Big, The Fast and The UglyIl piu’ avanzato database open source come big data player

Federico Campoli

Brandwatch.com

25 Ottobre 2014

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 1 / 34

Page 2: PostgreSQL, The Big, The Fast and The Ugly

Sommario

1 PostgreSQL, una storia di eccellenza

2 The Big

3 The Fast

4 The Ugly

5 Conclusioni

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 2 / 34

Page 3: PostgreSQL, The Big, The Fast and The Ugly

Sommario

1 PostgreSQL, una storia di eccellenza

2 The Big

3 The Fast

4 The Ugly

5 Conclusioni

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 3 / 34

Page 4: PostgreSQL, The Big, The Fast and The Ugly

PostgreSQL, una storia di eccellenza

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 4 / 34

Page 5: PostgreSQL, The Big, The Fast and The Ugly

PostgreSQL, una storia di eccellenza

Creato all’universita’ di Berkeley nel 1982 dal Prof. Stonebraker

Nel 1994 Andrew Yu e Jolly Chen aggiungono l’interprete SQL

Nel 1996 diventa un progetto Open Source prendendo il nome di PostgreSQL

Totalmente ACID compliant

Alta scalabilita’ con MVCC

Tablespace

Disponibile per numerosi unix flavours

Dalla versione 8.0 e’ nativo su *cough* MS Windows *cough*

High Availability fornita da hot standby e streaming replication

Federation verso fonti dati eterogenee

Numerosi linguaggi procedurali (pl/pgsql, pl/python, pl/perl...)

Supporto per tipi dato schemaless come HSTORE e JSON

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 5 / 34

Page 6: PostgreSQL, The Big, The Fast and The Ugly

PostgreSQL, una storia di eccellenza

Creato all’universita’ di Berkeley nel 1982 dal Prof. Stonebraker

Nel 1994 Andrew Yu e Jolly Chen aggiungono l’interprete SQL

Nel 1996 diventa un progetto Open Source prendendo il nome di PostgreSQL

Totalmente ACID compliant

Alta scalabilita’ con MVCC

Tablespace

Disponibile per numerosi unix flavours

Dalla versione 8.0 e’ nativo su *cough* MS Windows *cough*

High Availability fornita da hot standby e streaming replication

Federation verso fonti dati eterogenee

Numerosi linguaggi procedurali (pl/pgsql, pl/python, pl/perl...)

Supporto per tipi dato schemaless come HSTORE e JSON

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 5 / 34

Page 7: PostgreSQL, The Big, The Fast and The Ugly

PostgreSQL, una storia di eccellenza

Creato all’universita’ di Berkeley nel 1982 dal Prof. Stonebraker

Nel 1994 Andrew Yu e Jolly Chen aggiungono l’interprete SQL

Nel 1996 diventa un progetto Open Source prendendo il nome di PostgreSQL

Totalmente ACID compliant

Alta scalabilita’ con MVCC

Tablespace

Disponibile per numerosi unix flavours

Dalla versione 8.0 e’ nativo su *cough* MS Windows *cough*

High Availability fornita da hot standby e streaming replication

Federation verso fonti dati eterogenee

Numerosi linguaggi procedurali (pl/pgsql, pl/python, pl/perl...)

Supporto per tipi dato schemaless come HSTORE e JSON

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 5 / 34

Page 8: PostgreSQL, The Big, The Fast and The Ugly

PostgreSQL, una storia di eccellenza

Creato all’universita’ di Berkeley nel 1982 dal Prof. Stonebraker

Nel 1994 Andrew Yu e Jolly Chen aggiungono l’interprete SQL

Nel 1996 diventa un progetto Open Source prendendo il nome di PostgreSQL

Totalmente ACID compliant

Alta scalabilita’ con MVCC

Tablespace

Disponibile per numerosi unix flavours

Dalla versione 8.0 e’ nativo su *cough* MS Windows *cough*

High Availability fornita da hot standby e streaming replication

Federation verso fonti dati eterogenee

Numerosi linguaggi procedurali (pl/pgsql, pl/python, pl/perl...)

Supporto per tipi dato schemaless come HSTORE e JSON

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 5 / 34

Page 9: PostgreSQL, The Big, The Fast and The Ugly

PostgreSQL, una storia di eccellenza

Sviluppo

Programmato in linguaggio C

Numerose versioni supportate

Versioni supportate a lungo termine (almeno 5 anni)

Espandibile con l’uso di librerie esterne

Supporto per le estensioni (dalla versione 9.1)

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 6 / 34

Page 10: PostgreSQL, The Big, The Fast and The Ugly

PostgreSQL, una storia di eccellenza

Limiti

Dimensione massima database. Illimitata.

Dimensione massima singola tabella, 32 TB

Dimensione massima singola riga 1.6 TB

Numero massimo di righe per tabella. Illimitate.

Numero massimo di campi per tabella, 250 1600 a seconda del tipo dati.

Numero massimo tabelle. Illimitato.

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 7 / 34

Page 11: PostgreSQL, The Big, The Fast and The Ugly

PostgreSQL, una storia di eccellenza

Insieme ai tipi dato comuni, PostgreSQL fornisce alcuni tipi decisamente esotici.

Range

Dati geometrici

Indirizzi di rete

XML

JSON

HSTORE (extension)

Gli ultimi due sono oggetto della presentazione in quanto molto popolarinell’universo NOSQL.

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 8 / 34

Page 12: PostgreSQL, The Big, The Fast and The Ugly

Sommario

1 PostgreSQL, una storia di eccellenza

2 The Big

3 The Fast

4 The Ugly

5 Conclusioni

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 9 / 34

Page 13: PostgreSQL, The Big, The Fast and The Ugly

The Big

Image by Caitlin - https://www.flickr.com/people/50194168@N00

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 10 / 34

Page 14: PostgreSQL, The Big, The Fast and The Ugly

The Big

JSON - JavaScript Object Notation

JSON supportato come tipo nativo dalla versione 9.2

La 9.3 introduce numerose funzioni di supporto

Lo storage e’ praticamente testo

La sintassi JSON viene validata al volo

La struttura JSON viene gestita dal database

Dalla versione 9.4 appare JSONB (binario) che migliora notevolmente leperformance

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 11 / 34

Page 15: PostgreSQL, The Big, The Fast and The Ugly

The Big

JSON - EsempiDa record a JSON

postgres=# SELECT row_to_json(ROW(1,’foo’));row_to_json

---------------------{"f1":1,"f2":"foo"}

(1 row)

Espansione di JSON in elementi key to value

postgres=# SELECT * from json_each(’{"a":"foo", "b":"bar"}’);

key | value-----+-------a | "foo"b | "bar"

(2 rows)

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 12 / 34

Page 16: PostgreSQL, The Big, The Fast and The Ugly

The Big

HSTORE e’ un tipo dato per elementi key to value

Supportato come estensione

I dati sono memorizzati come testo

Una libreria condivisa transforma il testo nel tipo HSTORE

Simile a json ma senza elementi annidati

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 13 / 34

Page 17: PostgreSQL, The Big, The Fast and The Ugly

The Big

HSTORE - EsempiDa record a HSTORE

postgres=# SELECT hstore(ROW(1,2));hstore

----------------------"f1"=>"1", "f2"=>"2"

(1 row)

Espansione di HSTORE in elementi key to value

postgres=# SELECT * FROM each(’a=>1,b=>2’);key | value

-----+-------a | 1b | 2

(2 rows)

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 14 / 34

Page 18: PostgreSQL, The Big, The Fast and The Ugly

The Big

C’e’ una differenza sottile tra HSTORE e JSON. Poiche’ HSTORE e’ unestensione la manipolazione del dato avviene grazie ad una libreria esterna.

Questo, combinato con il modo in cui PostgreSQL trasforma il datum quandoviene caricato in memoria, puo avere effetti anche drammatici sulle performance.

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 15 / 34

Page 19: PostgreSQL, The Big, The Fast and The Ugly

Sommario

1 PostgreSQL, una storia di eccellenza

2 The Big

3 The Fast

4 The Ugly

5 Conclusioni

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 16 / 34

Page 20: PostgreSQL, The Big, The Fast and The Ugly

The Fast

Image by Hein Waschefort -http://commons.wikimedia.org/wiki/User:Hein waschefort

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 17 / 34

Page 21: PostgreSQL, The Big, The Fast and The Ugly

The Fast

PostgreSQL memorizza i dati in blocchi di dimensione fissa chiamate pagine. Ognipagina e’ solitamente di 8kb con un header usato per garantire la durabilita’.

Figure : Data page

Dopo l’header c’e’ una lista di puntatori alle tuple fisiche. Ogni tupla e’strutturata come un array di dati il cui tipo e’ inizialmente sconosciuto.

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 18 / 34

Page 22: PostgreSQL, The Big, The Fast and The Ugly

The Fast

Lo stream di dati in tale stato e’ chiamato datum. Una volta caricato in memoriaPostgreSQL risolve il tipo memorizzato usando il catalogo di sistema.

Figure : Tuple structure

Lo header di tupla e’ parte dell’implementazione MVCC.

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 19 / 34

Page 23: PostgreSQL, The Big, The Fast and The Ugly

The Fast

Per ogni tupla caricata in memoria il dato viene trasformato da datum a tiporeale. Le routine di conversione variano a seconda del tipo effettivo.

Un elemento nativo come JSON esegue quindi le routine che sono codificateinternamente al motore di database.

Di contro una trasformazione HSTORE genera un accesso alla libreria esterna perogni tupla esaminata in memoria.

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 20 / 34

Page 24: PostgreSQL, The Big, The Fast and The Ugly

The Fast

Nonostante JSON sia un dato nativo non e’ detto che le sue performance sianomigliori di HSTORE. La validazione e il parsing al volo sono un potenzialebottleneck se la quantita’ di dati e’ importante.

Questo problema viene risolto dal nuovo tipo binario JSONB introdotto conPostgreSQL 9.4. Tutte le routine di validazione e trasformazione avvengonodurante le DML.

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 21 / 34

Page 25: PostgreSQL, The Big, The Fast and The Ugly

Sommario

1 PostgreSQL, una storia di eccellenza

2 The Big

3 The Fast

4 The Ugly

5 Conclusioni

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 22 / 34

Page 26: PostgreSQL, The Big, The Fast and The Ugly

The Ugly

Image by D. Gordon E. Robertson -http://commons.wikimedia.org/wiki/User:Dger

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 23 / 34

Page 27: PostgreSQL, The Big, The Fast and The Ugly

The Ugly

Un caso reale, Brandwatch

Social media analytics

Dati real time e storici

Core database PostgreSQL

Big Data!!!!!

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 24 / 34

Page 28: PostgreSQL, The Big, The Fast and The Ugly

The Ugly

Tanto tempo fa in un datacentre lontano lontano...(Ottobre 2012)

1 database per analytics

Dimensione di “soltanto” 1.2 TB

Struttura denormalizzata

Storage schemaless con HSTORE

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 25 / 34

Page 29: PostgreSQL, The Big, The Fast and The Ugly

The Ugly

Qualche tempo dopo, Giugno 2014...

10 shards per l’analisi dei dati

Dimensione complessiva di 25 TB

HSTORE e denormalizzazione

Sistemi costantemente sovraccarichi

Crescita storage di 300 GB al giorno

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 26 / 34

Page 30: PostgreSQL, The Big, The Fast and The Ugly

The Ugly

Il problema... HSTORE

Sovraccarica la CPU

Storage poco efficiente

Dati a volte incoerenti

Difficile da manutenere

Estremamente pratico

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 27 / 34

Page 31: PostgreSQL, The Big, The Fast and The Ugly

The Ugly

La soluzione... il buon vecchio storage columnar

Riduzione dello spazio del 30%

Carico di CPU ridotto ai minimi termini

Dati consistenti

Crescita ridotta a soli 50 GB al giorno

HSTORE usato solo come input per le procedure

Apertura a successivi miglioramenti

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 28 / 34

Page 32: PostgreSQL, The Big, The Fast and The Ugly

Sommario

1 PostgreSQL, una storia di eccellenza

2 The Big

3 The Fast

4 The Ugly

5 Conclusioni

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 29 / 34

Page 33: PostgreSQL, The Big, The Fast and The Ugly

Conclusioni

I dati schemaless sono molto comodi, permettono grande flessibilita’ seimplementati correttamente.

L’assenza di scalabilita’ orizzontale in PostgreSQL ne limita l’utilizzo se laquantita’ di dati e’ importante.

Un promettente progetto in tal senso e’ PostgreSQL XL -http://www.postgres-xl.org/

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 30 / 34

Page 34: PostgreSQL, The Big, The Fast and The Ugly

Domande

Domande?

Per favore siate semplici, dopo tutto sono soltanto un elettricista...

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 31 / 34

Page 35: PostgreSQL, The Big, The Fast and The Ugly

Contatti

Twitter: 4thdoctor scarf

Blog su PostgreSQL: http://www.pgdba.co.uk

PostgreSQL Book:http://www.slideshare.net/FedericoCampoli/postgresql-dba-01

Lavoro: http://www.brandwatch.com/

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 32 / 34

Page 36: PostgreSQL, The Big, The Fast and The Ugly

Licenza

Licenza CC Attribuzione - Non commerciale - Condividi allo stesso modo 3.0

http://creativecommons.org/licenses/by-nc-sa/3.0/it/deed.it

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 33 / 34

Page 37: PostgreSQL, The Big, The Fast and The Ugly

PostgreSQL, The Big, The Fast and The UglyIl piu’ avanzato database open source come big data player

Federico Campoli

Brandwatch.com

25 Ottobre 2014

Federico Campoli (Brandwatch.com) PostgreSQL, The Big, The Fast and The Ugly 25 Ottobre 2014 34 / 34