SlideShare a Scribd company logo
One Database To Rule 'em All
PHP Usergroup Berlin
November 2015
Stefanie Janine Stölting
SQL/MED
Defined by ISO/IEC 9075-9:200
Supported by
DB2
MariaDB
With CONNECT storage engine,
implementation differs to the standard
PostgreSQL
Implementation
Foreign Data Wrapper
Read only
Read and write
Installation as extensions
Available FDW
Examples:
Oracle (pgxn.org)
MS SQL Server / Sybase ASE
readonly (pgxn.org)
MongoDB
readonly (pgxn.org)
MariaDB / MySQL (github.com)
Data source
The example data used in the SQL part is
availabale from Chinook Database:
PostgreSQL
MySQL
SQLite
CTE
Common Table Expressions will be used in examples
● Example:
WITH RECURSIVE t(n) AS (
VALUES (1)
UNION ALL
SELECT n+1 FROM t WHERE n < 100
)
SELECT sum(n), min(n), max(n) FROM t;
●
Result:
Live Data examples
Live Data examples
-- Create the foreign data wrapper extension in the current database
CREATE EXTENSION mysql_fdw;
-- Create the mapping to the foreign MariaDB server
CREATE SERVER mariadb_server
FOREIGN DATA WRAPPER mysql_fdw
OPTIONS (host '127.0.0.1', port '3306');
-- Create a user mapping with user and password of the foreign table
-- PostgreSQL gives you options to connect this user with its own users
CREATE USER MAPPING FOR PUBLIC SERVER mariadb_server
OPTIONS (username 'pg_test', password 'secret');
Live Data examples
-- Create the MariaDB foreign table, column definitions have to match
CREATE FOREIGN TABLE mysql_album(
"AlbumId" integer,
"Title" character varying(160),
"ArtistId" integer
)
SERVER mariadb_server
OPTIONS(
dbname 'Chinook',
table_name 'Album'
);
-- Select some data
SELECT * FROM mysql_album;
Live Data examples
-- Create the mapping to the foreign SQLite file
CREATE SERVER sqlite_server
FOREIGN DATA WRAPPER sqlite_fdw
OPTIONS (database '/var/sqlite/Chinook_Sqlite.sqlite')
;
-- Create the SQLite foreign table, column definitions have to match
CREATE FOREIGN TABLE sqlite_artist(
"ArtistId" integer,
"Name" character varying(120)
)
SERVER sqlite_server
OPTIONS(
table 'Artist'
);
Live Data examples
-- Select some data
SELECT * FROM sqlite_artist;
Live Data examples
-- Join PostgreSQL, MariaDB and SQLite tables
SELECT *
FROM sqlite_artist AS artist
INNER JOIN mysql_album AS album
ON artist."ArtistId" = album."ArtistId"
INNER JOIN "Track" AS track
ON album."AlbumId" = track."AlbumId"
;
Live Data examples
-- Joining SQLite and MariaDB tables using PostgreSQL expressions
WITH album AS
(
SELECT "ArtistId"
, array_agg("Title") AS album_titles
FROM mysql_album
GROUP BY "ArtistId"
)
SELECT artist."Name" AS artist
, album.album_titles
FROM sqlite_artist AS artist
INNER JOIN album
ON artist."ArtistId" = album."ArtistId"
;
Live Data examples
-- Creates an materialized view on foreign tables
CREATE MATERIALIZED VIEW mv_album_artist AS
WITH album AS
(
SELECT "ArtistId"
, array_agg("Title") AS album_titles
FROM mysql_album
GROUP BY "ArtistId"
)
SELECT artist."Name" AS artist
, album.album_titles
, SUM(ARRAY_LENGTH(album_titles, 1))
FROM sqlite_artist AS artist
LEFT OUTER JOIN album
ON artist."ArtistId" = album."ArtistId"
GROUP BY artist."Name"
, album.album_titles
;
-- Creates a unique index on a mv
CREATE UNIQUE INDEX mv_album_artist__artist ON mv_album_artist(artist);
Live Data examples
-- Select the mv data
SELECT *
FROM mv_album_artist
WHERE artist = 'AC/DC'
;
Live Data examples
-- SELECT the amount of albums from the MariaDB table from MariaDB, not with a foreign data
wrapper
SELECT count( * ) AS AlbumCount
FROM `Album`
;
Live Data examples
-- Insert data calculated from foreign tables using PostgreSQL features into another foreign table
INSERT INTO mysql_album("AlbumId", "ArtistId", "Title")
WITH album AS
(
-- Generate a new album id
SELECT MAX(album."AlbumId") + 1 AS new_album_id
FROM mysql_album AS album
)
SELECT album.new_album_id
, artist."ArtistId"
, 'Back in Black'
FROM sqlite_artist AS artist, album
WHERE artist."Name" = 'AC/DC'
GROUP BY album.new_album_id
, artist."ArtistId"
;
Live Data examples
-- SELECT the amount of albums from the MariaDB table from MariaDB, not with a foreign data
wrapper
SELECT count( * ) AS AlbumCount
FROM `Album`
;
Live Data examples
-- Select data from the materialized view
SELECT *
FROM mv_album_artist
WHERE artist = 'AC/DC'
ORDER BY artist
;
-- Refresh the mv to see the recently added data
REFRESH MATERIALIZED VIEW mv_album_artist;
-- We can even delete data from foreign tables
DELETE FROM mysql_album
WHERE "Title" = 'Back in Black'
AND "ArtistId" = 1
;
Live Data examples
-- Using PostgreSQL JSON with data from MariaDB and SQLite
-- Step 1: Albums with tracks as JSON
WITH albums AS
(
SELECT a."ArtistId" AS artist_id
, a."Title" AS album_title
, array_agg(t."Name") AS album_tracks
FROM mysql_album AS a
INNER JOIN "Track" AS t
ON a."AlbumId" = t."AlbumId"
GROUP BY a."ArtistId"
, a."Title"
)
SELECT row_to_json(albums) AS album_tracks
FROM albums
;
Live Data examples
-- Step 2 Abums including tracks with aritsts
WITH albums AS
(
SELECT a."ArtistId" AS artist_id
, a."Title" AS album_title
, array_agg(t."Name") AS album_tracks
FROM mysql_album AS a
INNER JOIN "Track" AS t
ON a."AlbumId" = t."AlbumId"
GROUP BY a."ArtistId"
, a."Title"
)
, js_albums AS
(
SELECT row_to_json(albums) AS album_tracks
FROM albums
)
SELECT a."Name" AS artist
, al.album_tracks AS albums_tracks
FROM sqlite_artist AS a
INNER JOIN js_albums AS al
ON a."ArtistId" = CAST(al.album_tracks->>'artist_id' AS INT)
;
Live Data examples
Live Data examples
-- Step 3 Return one row for an artist with all albums
CREATE MATERIALIZED VIEW v_artist_data AS
WITH albums AS
(
SELECT a."ArtistId" AS artist_id
, a."Title" AS album_title
, array_agg(t."Name") AS album_tracks
FROM mysql_album AS a
INNER JOIN "Track" AS t
ON a."AlbumId" = t."AlbumId"
GROUP BY a."ArtistId"
, a."Title"
)
, js_albums AS
(
SELECT row_to_json(albums) AS album_tracks
FROM albums
)
, artist_albums AS
(
SELECT a."Name" AS artist
, array_agg(al.album_tracks) AS albums_tracks
FROM sqlite_artist AS a
INNER JOIN js_albums AS al
ON a."ArtistId" = CAST(al.album_tracks->>'artist_id' AS INT)
GROUP BY a."Name"
)
SELECT CAST(row_to_json(artist_albums) AS JSONB) AS artist_data
FROM artist_albums
;
Live Data examples
CREATE MATERIALIZED VIEW mv_artist_data AS
SELECT *
FROM v_artist_data
;
CREATE INDEX artist_data_gin ON mv_artist_data USING
GIN(artist_data);
Live Data examples
-- SELECT data from that materialized view, that does
querying
-- PostgreSQL, MariaDB, and SQLite tables in one SQL
statement
SELECT jsonb_pretty(artist_data) pretty_artistdata
FROM mv_artist_data
WHERE artist_data->>'artist' IN ('Miles Davis', 'AC/DC')
;
Live Data examples
{
"artist": "Miles Davis",
"albums_tracks":
[
{
"artist_id": 68,
"album_title": "Miles Ahead",
"album_tracks":
[
"I Don't Wanna Be Kissed (By Anyone But You) (Alternate Take)",
"The Meaning Of The Blues/Lament (Alternate Take)",
"Blues For Pablo (Alternate Take)",
"Springsville (Alternate Take)",
...
]
},
{
...
}
]
}
Live Data examples
-- SELECT some data using JSON methods
SELECT jsonb_pretty(artist_data#>'{albums_tracks}') AS all_albums
, jsonb_pretty(artist_data#>'{albums_tracks, 0}') AS tracks_0
, artist_data#>'{albums_tracks, 0, album_title}' AS title
FROM mv_artist_data
WHERE artist_data->'albums_tracks' @> '[{"album_title":"Miles Ahead"}]'
;
User Group / Conferences
PostgreSQL User Group Berlin
German PostgreSQL Conference 2015
Hamburg, November 26-27, 2015
FOSDEM PGDay 2016
Brussels, January 29, 2016
FOSDEM 2016, PostgreSQL devroom
Brussels, January 30-31, 2016
Link List
PGXN Extensions:
- mysql_fdw, MySQL/MariaDB FDW
- sqlite_fdw, SQLite FDW
- jsonbx, PostgreSQL 9.5 JSONB features
Slide and source on Github:
https://github.com/sjstoelting/talks
One Database To Rule 'em All
This document by Stefanie Janine Stölting is covered by the
Creative Commons Attribution 4.0 International

More Related Content

ODP
NoSQL as Not Only SQL
ODP
NoSQL as Not Only SQL (FrOSCon 11)
ODP
JSON By Example
ODP
NoSQL The SQL Way
ODP
The PostgreSQL JSON Feature Tour
ODP
No sql the-sql-way
PDF
Efficient Use of indexes in MySQL
PDF
Efficient Indexes in MySQL
NoSQL as Not Only SQL
NoSQL as Not Only SQL (FrOSCon 11)
JSON By Example
NoSQL The SQL Way
The PostgreSQL JSON Feature Tour
No sql the-sql-way
Efficient Use of indexes in MySQL
Efficient Indexes in MySQL

What's hot (17)

PDF
Rug hogan-10-03-2012
PDF
Data Munging in R - Chicago R User Group
KEY
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
PDF
Python Ireland Nov 2010 Talk: Unit Testing
PDF
Postgre(No)SQL - A JSON journey
PPTX
R- create a table from a list of files.... before webmining
PPT
spug_2008-08
PDF
Переход на Scala: босиком по граблям
PPTX
Looping the Loop with SPL Iterators
KEY
R for Pirates. ESCCONF October 27, 2011
PDF
Collectors in the Wild
PPTX
Golang slidesaudrey
PPTX
Kotlin decoration - February Berlin Kotlin Meetup
PDF
Rob Sullivan at Heroku's Waza 2013: Your Database -- A Story of Indifference
PDF
DHWI Linked Open Data - What I Did
PDF
perl-pocket
PPTX
Merge Multiple CSV in single data frame using R
Rug hogan-10-03-2012
Data Munging in R - Chicago R User Group
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Python Ireland Nov 2010 Talk: Unit Testing
Postgre(No)SQL - A JSON journey
R- create a table from a list of files.... before webmining
spug_2008-08
Переход на Scala: босиком по граблям
Looping the Loop with SPL Iterators
R for Pirates. ESCCONF October 27, 2011
Collectors in the Wild
Golang slidesaudrey
Kotlin decoration - February Berlin Kotlin Meetup
Rob Sullivan at Heroku's Waza 2013: Your Database -- A Story of Indifference
DHWI Linked Open Data - What I Did
perl-pocket
Merge Multiple CSV in single data frame using R
Ad

Viewers also liked (17)

PDF
Cass Morgan Lodge Presentation
PDF
PPTX
My last vacation correction
DOCX
Chứng bệnh “hẹp ống sống” nguy hiểm thế nào
PDF
Effect of grafting cucumber onto some rootstocks for controlling root knot ne...
PDF
Nymbus_650_Presentation-short
PDF
Impact of Different Grafting Methods on Yield and Quality of Watermelon
PPTX
Allied presentation PD Jan 2015
PPTX
The edfu temple
DOCX
Nibras Updated 2 (1) (1)
PDF
Overcoming challenges for start-ups by integrating eastern and western ecosys...
PPTX
SIstem Tata Udara dan Refrigerasi pada Ruang Fitness Kapal Tanker 17500 DWT
PDF
DOC
Prateek chauhan cv senior business analyst_csc
PDF
Production and Evaluation of Highly Yielding Sweet Pepper Hybrids under Gree...
Cass Morgan Lodge Presentation
My last vacation correction
Chứng bệnh “hẹp ống sống” nguy hiểm thế nào
Effect of grafting cucumber onto some rootstocks for controlling root knot ne...
Nymbus_650_Presentation-short
Impact of Different Grafting Methods on Yield and Quality of Watermelon
Allied presentation PD Jan 2015
The edfu temple
Nibras Updated 2 (1) (1)
Overcoming challenges for start-ups by integrating eastern and western ecosys...
SIstem Tata Udara dan Refrigerasi pada Ruang Fitness Kapal Tanker 17500 DWT
Prateek chauhan cv senior business analyst_csc
Production and Evaluation of Highly Yielding Sweet Pepper Hybrids under Gree...
Ad

Similar to One Databyse To Rule 'em All (20)

ODP
One Database To Rule 'em All
ODP
One Database to Rule 'em all (FrOSCon 11)
ODP
One Database To Rule 'em All
PDF
Advanced MariaDB features that developers love.pdf
PDF
Hybrid Databases - PHP UK Conference 22 February 2019
PDF
MariaDB for Developers and Operators (DevOps)
PDF
Moving to hybrid relational/JSON data models
PPTX
The rise of json in rdbms land jab17
PDF
MySQL Document Store - when SQL & NoSQL live together... in peace!
PDF
Big Data Analytics with MariaDB ColumnStore
PPTX
Somewhere between schema and schemaless
PDF
M|18 Somewhere Between Schema and Schemaless
PDF
Wie man Datenbankinfrastrukturen in Containern zum Laufen bringt
PDF
Big Data with MySQL
PPTX
MySQL vs. MonetDB
PDF
Hyrbid Data Models (relational + json)
PDF
Hybrid Data Models - Relational + JSON
PDF
MySQL 5.7 Tutorial Dutch PHP Conference 2015
PDF
MySQL 5.7. Tutorial - Dutch PHP Conference 2015
PDF
Using semi-structured data in modern applications
One Database To Rule 'em All
One Database to Rule 'em all (FrOSCon 11)
One Database To Rule 'em All
Advanced MariaDB features that developers love.pdf
Hybrid Databases - PHP UK Conference 22 February 2019
MariaDB for Developers and Operators (DevOps)
Moving to hybrid relational/JSON data models
The rise of json in rdbms land jab17
MySQL Document Store - when SQL & NoSQL live together... in peace!
Big Data Analytics with MariaDB ColumnStore
Somewhere between schema and schemaless
M|18 Somewhere Between Schema and Schemaless
Wie man Datenbankinfrastrukturen in Containern zum Laufen bringt
Big Data with MySQL
MySQL vs. MonetDB
Hyrbid Data Models (relational + json)
Hybrid Data Models - Relational + JSON
MySQL 5.7 Tutorial Dutch PHP Conference 2015
MySQL 5.7. Tutorial - Dutch PHP Conference 2015
Using semi-structured data in modern applications

Recently uploaded (20)

PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
L1 - Introduction to python Backend.pptx
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Cost to Outsource Software Development in 2025
PDF
Complete Guide to Website Development in Malaysia for SMEs
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
assetexplorer- product-overview - presentation
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Operating system designcfffgfgggggggvggggggggg
Why Generative AI is the Future of Content, Code & Creativity?
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Monitoring Stack: Grafana, Loki & Promtail
Autodesk AutoCAD Crack Free Download 2025
Reimagine Home Health with the Power of Agentic AI​
L1 - Introduction to python Backend.pptx
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Cost to Outsource Software Development in 2025
Complete Guide to Website Development in Malaysia for SMEs
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
Odoo Companies in India – Driving Business Transformation.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
assetexplorer- product-overview - presentation
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Patient Appointment Booking in Odoo with online payment
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free

One Databyse To Rule 'em All

  • 1. One Database To Rule 'em All PHP Usergroup Berlin November 2015 Stefanie Janine Stölting
  • 2. SQL/MED Defined by ISO/IEC 9075-9:200 Supported by DB2 MariaDB With CONNECT storage engine, implementation differs to the standard PostgreSQL
  • 3. Implementation Foreign Data Wrapper Read only Read and write Installation as extensions
  • 4. Available FDW Examples: Oracle (pgxn.org) MS SQL Server / Sybase ASE readonly (pgxn.org) MongoDB readonly (pgxn.org) MariaDB / MySQL (github.com)
  • 5. Data source The example data used in the SQL part is availabale from Chinook Database: PostgreSQL MySQL SQLite
  • 6. CTE Common Table Expressions will be used in examples ● Example: WITH RECURSIVE t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 100 ) SELECT sum(n), min(n), max(n) FROM t; ● Result:
  • 8. Live Data examples -- Create the foreign data wrapper extension in the current database CREATE EXTENSION mysql_fdw; -- Create the mapping to the foreign MariaDB server CREATE SERVER mariadb_server FOREIGN DATA WRAPPER mysql_fdw OPTIONS (host '127.0.0.1', port '3306'); -- Create a user mapping with user and password of the foreign table -- PostgreSQL gives you options to connect this user with its own users CREATE USER MAPPING FOR PUBLIC SERVER mariadb_server OPTIONS (username 'pg_test', password 'secret');
  • 9. Live Data examples -- Create the MariaDB foreign table, column definitions have to match CREATE FOREIGN TABLE mysql_album( "AlbumId" integer, "Title" character varying(160), "ArtistId" integer ) SERVER mariadb_server OPTIONS( dbname 'Chinook', table_name 'Album' ); -- Select some data SELECT * FROM mysql_album;
  • 10. Live Data examples -- Create the mapping to the foreign SQLite file CREATE SERVER sqlite_server FOREIGN DATA WRAPPER sqlite_fdw OPTIONS (database '/var/sqlite/Chinook_Sqlite.sqlite') ; -- Create the SQLite foreign table, column definitions have to match CREATE FOREIGN TABLE sqlite_artist( "ArtistId" integer, "Name" character varying(120) ) SERVER sqlite_server OPTIONS( table 'Artist' );
  • 11. Live Data examples -- Select some data SELECT * FROM sqlite_artist;
  • 12. Live Data examples -- Join PostgreSQL, MariaDB and SQLite tables SELECT * FROM sqlite_artist AS artist INNER JOIN mysql_album AS album ON artist."ArtistId" = album."ArtistId" INNER JOIN "Track" AS track ON album."AlbumId" = track."AlbumId" ;
  • 13. Live Data examples -- Joining SQLite and MariaDB tables using PostgreSQL expressions WITH album AS ( SELECT "ArtistId" , array_agg("Title") AS album_titles FROM mysql_album GROUP BY "ArtistId" ) SELECT artist."Name" AS artist , album.album_titles FROM sqlite_artist AS artist INNER JOIN album ON artist."ArtistId" = album."ArtistId" ;
  • 14. Live Data examples -- Creates an materialized view on foreign tables CREATE MATERIALIZED VIEW mv_album_artist AS WITH album AS ( SELECT "ArtistId" , array_agg("Title") AS album_titles FROM mysql_album GROUP BY "ArtistId" ) SELECT artist."Name" AS artist , album.album_titles , SUM(ARRAY_LENGTH(album_titles, 1)) FROM sqlite_artist AS artist LEFT OUTER JOIN album ON artist."ArtistId" = album."ArtistId" GROUP BY artist."Name" , album.album_titles ; -- Creates a unique index on a mv CREATE UNIQUE INDEX mv_album_artist__artist ON mv_album_artist(artist);
  • 15. Live Data examples -- Select the mv data SELECT * FROM mv_album_artist WHERE artist = 'AC/DC' ;
  • 16. Live Data examples -- SELECT the amount of albums from the MariaDB table from MariaDB, not with a foreign data wrapper SELECT count( * ) AS AlbumCount FROM `Album` ;
  • 17. Live Data examples -- Insert data calculated from foreign tables using PostgreSQL features into another foreign table INSERT INTO mysql_album("AlbumId", "ArtistId", "Title") WITH album AS ( -- Generate a new album id SELECT MAX(album."AlbumId") + 1 AS new_album_id FROM mysql_album AS album ) SELECT album.new_album_id , artist."ArtistId" , 'Back in Black' FROM sqlite_artist AS artist, album WHERE artist."Name" = 'AC/DC' GROUP BY album.new_album_id , artist."ArtistId" ;
  • 18. Live Data examples -- SELECT the amount of albums from the MariaDB table from MariaDB, not with a foreign data wrapper SELECT count( * ) AS AlbumCount FROM `Album` ;
  • 19. Live Data examples -- Select data from the materialized view SELECT * FROM mv_album_artist WHERE artist = 'AC/DC' ORDER BY artist ; -- Refresh the mv to see the recently added data REFRESH MATERIALIZED VIEW mv_album_artist; -- We can even delete data from foreign tables DELETE FROM mysql_album WHERE "Title" = 'Back in Black' AND "ArtistId" = 1 ;
  • 20. Live Data examples -- Using PostgreSQL JSON with data from MariaDB and SQLite -- Step 1: Albums with tracks as JSON WITH albums AS ( SELECT a."ArtistId" AS artist_id , a."Title" AS album_title , array_agg(t."Name") AS album_tracks FROM mysql_album AS a INNER JOIN "Track" AS t ON a."AlbumId" = t."AlbumId" GROUP BY a."ArtistId" , a."Title" ) SELECT row_to_json(albums) AS album_tracks FROM albums ;
  • 21. Live Data examples -- Step 2 Abums including tracks with aritsts WITH albums AS ( SELECT a."ArtistId" AS artist_id , a."Title" AS album_title , array_agg(t."Name") AS album_tracks FROM mysql_album AS a INNER JOIN "Track" AS t ON a."AlbumId" = t."AlbumId" GROUP BY a."ArtistId" , a."Title" ) , js_albums AS ( SELECT row_to_json(albums) AS album_tracks FROM albums ) SELECT a."Name" AS artist , al.album_tracks AS albums_tracks FROM sqlite_artist AS a INNER JOIN js_albums AS al ON a."ArtistId" = CAST(al.album_tracks->>'artist_id' AS INT) ;
  • 23. Live Data examples -- Step 3 Return one row for an artist with all albums CREATE MATERIALIZED VIEW v_artist_data AS WITH albums AS ( SELECT a."ArtistId" AS artist_id , a."Title" AS album_title , array_agg(t."Name") AS album_tracks FROM mysql_album AS a INNER JOIN "Track" AS t ON a."AlbumId" = t."AlbumId" GROUP BY a."ArtistId" , a."Title" ) , js_albums AS ( SELECT row_to_json(albums) AS album_tracks FROM albums ) , artist_albums AS ( SELECT a."Name" AS artist , array_agg(al.album_tracks) AS albums_tracks FROM sqlite_artist AS a INNER JOIN js_albums AS al ON a."ArtistId" = CAST(al.album_tracks->>'artist_id' AS INT) GROUP BY a."Name" ) SELECT CAST(row_to_json(artist_albums) AS JSONB) AS artist_data FROM artist_albums ;
  • 24. Live Data examples CREATE MATERIALIZED VIEW mv_artist_data AS SELECT * FROM v_artist_data ; CREATE INDEX artist_data_gin ON mv_artist_data USING GIN(artist_data);
  • 25. Live Data examples -- SELECT data from that materialized view, that does querying -- PostgreSQL, MariaDB, and SQLite tables in one SQL statement SELECT jsonb_pretty(artist_data) pretty_artistdata FROM mv_artist_data WHERE artist_data->>'artist' IN ('Miles Davis', 'AC/DC') ;
  • 26. Live Data examples { "artist": "Miles Davis", "albums_tracks": [ { "artist_id": 68, "album_title": "Miles Ahead", "album_tracks": [ "I Don't Wanna Be Kissed (By Anyone But You) (Alternate Take)", "The Meaning Of The Blues/Lament (Alternate Take)", "Blues For Pablo (Alternate Take)", "Springsville (Alternate Take)", ... ] }, { ... } ] }
  • 27. Live Data examples -- SELECT some data using JSON methods SELECT jsonb_pretty(artist_data#>'{albums_tracks}') AS all_albums , jsonb_pretty(artist_data#>'{albums_tracks, 0}') AS tracks_0 , artist_data#>'{albums_tracks, 0, album_title}' AS title FROM mv_artist_data WHERE artist_data->'albums_tracks' @> '[{"album_title":"Miles Ahead"}]' ;
  • 28. User Group / Conferences PostgreSQL User Group Berlin German PostgreSQL Conference 2015 Hamburg, November 26-27, 2015 FOSDEM PGDay 2016 Brussels, January 29, 2016 FOSDEM 2016, PostgreSQL devroom Brussels, January 30-31, 2016
  • 29. Link List PGXN Extensions: - mysql_fdw, MySQL/MariaDB FDW - sqlite_fdw, SQLite FDW - jsonbx, PostgreSQL 9.5 JSONB features Slide and source on Github: https://github.com/sjstoelting/talks
  • 30. One Database To Rule 'em All This document by Stefanie Janine Stölting is covered by the Creative Commons Attribution 4.0 International