Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October...

45
https://www.2ndQuadrant.com Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Boriss Mejías Consultant - 2ndQuadrant Air Guitar Player Migrating to PostgreSQL

Transcript of Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October...

Page 1: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Boriss MejíasConsultant - 2ndQuadrant

Air Guitar Player

Migrating to PostgreSQL

Page 2: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Why Migrate to PostgreSQL?

Page 3: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

PostgreSQL● Open Source

– Supported– Extendable

● Advanced● Reliable● Standard Compliant

Page 4: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

PostgreSQL● It’s an All-Rounder

– Low Latency– Big Data– High Availability– “Document” Database

● Sometimes better than dedicated solutions– Scale to petabytes (from Elasticsearch)

Page 5: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

PostgreSQL

● Awesome Community

Page 6: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Why Migrate to Open Source?

Page 7: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Why Migrate to Open Source?

Reason #1 is “Cost”

Page 8: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Why Migrate to Open Source?

Reason #1 is “Cost”(or it used to be)

Page 9: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Top Reasons to Stay in Open Source1. Competitive features, innovation2. Freedom from vendor lock-in3. Quality of solutions4. Ability to customize and fix5. Cost

https://www.slideshare.net/blackducksoftware/ 2016-future-of-open-source-survey-results

Page 10: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Migration Timeline● Effort Assessment● Decision (is it worth?)● Preparation● Testing● Migration● Cleanup

Page 11: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Effort Assessment● Schema● Data● Code

– What language? (SQL / Other)– Where? (Client / Server)

● Architecture

Page 12: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Schema● Usually the easiest part

– Available via common tools● Map data types as appropriate

– Look for simplifications● Consider custom datatypes

– Simpler is better than complex– Complex is better than complicated

Zen of Python

Page 13: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Data Type● PostgreSQL has several data types● Classical: text, numbers, boolean, time/date● Modern: Arrays, JSON● User-defined:

– Composite– Enumerative– Your data type in C

Page 14: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Data Type Gotcha● Oracle NUMBER to NUMERIC● MySQL BOOLEAN to BOOLEAN● Oracle NULL

SELECT first_name

|| second_name

|| last_name;

Page 15: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Data Type Gotcha● Oracle NUMBER to NUMERIC● MySQL BOOLEAN to BOOLEAN● Oracle NULL

SELECT first_name

|| COALESCE(second_name, '')

|| last_name;

Page 16: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Architecture Assessment● High Availability● Disaster Recovery● Multi-Master● Selective Replication

Page 17: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Target Architecture

Page 18: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Target Architecture

Page 19: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Solutions Mapping● Rich PostgreSQL ecosystem

– Core– Contrib / Extensions– Third Party (both FLOSS and proprietary)

● Sometimes difficult to find exact match– Might not be needed– You must match the purpose, not the tool

Page 20: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Page 21: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Application Code● Many programming languages and

frameworks have PostgreSQL drivers– Not an issue (usually)

● Real issue: SQL variants with different feature sets:– Emulate missing features– Remove useless emulations

Page 22: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Application Code Gotcha● SELECT 1 FROM DUAL;● Upper case default in Oracle

– CREATE TABLE DUAL ();– DUAL → dual → “DUAL”

● Exceptions in store procedures

Page 23: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Planning the Migration● The Assessment includes (at least) one Plan

– Time– Cost– Contingency / Rollback

Page 24: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Take Advantage of the Application● Some applications support multiple databases● They have done all the major part of the work● Functions, procedures, data types. It all works

already with PostgreSQL● Just need to migrate the data

Page 25: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Don’t Panic

Page 26: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Vanilla Deployment

Page 27: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Bilberry Deployment

Page 28: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Get the Schema Definition

Page 29: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Migrate Data

Page 30: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Migrate Data Downtime→ Downtime

Page 31: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Redirect the Application

Page 32: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Redirect the Application Showtime!→ Downtime

Page 33: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Alternative Strategy: Phasing Out● Use PostgreSQL for new services

– Keep old services as they are● Useful when standard plans are too

complicated / expensive● No need to migrate old data and code

– Easier: “just” plan new system

Page 34: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Phasing Out and Integration● Integrate new PostgreSQL with existing DBs● Preserve continuity of services● Foreign Data Wrappers

– Pluggable adaptors for other systems– SQL/MED standard– Some of them are read/write

Page 35: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Alternative Strategy: Preparation● Modify the existing system before migrating● Make it nearer to PostgreSQL

– Stop using incompatible features– Rewrite/simplify queries

● Enables application compatibility● Makes migration easier / cheaper / faster

Page 36: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Testing● Compatibility● Performance

● The migration process includes writing tests

Page 37: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Performance Testing● Test must include difficult / critical queries● Ensure that newer optimisations don’t cause

regressions on other queries● Use pgbench (custom scripts)● Analyse the current workload● Reproduce it● Properly dimension hardware

Page 38: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Scripted Migration● The migration procedure should be scripted as

much as possible● A script can be:

– Repeated– Versioned– Benchmarked– Tested

Page 39: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Scripted Migration● The migration procedure should be scripted as

much as possible● A script can be:

– Repeated– Versioned– Benchmarked– Tested (in staging environment)

Page 40: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Scripted Migration● The migration procedure should be scripted as

much as possible● A script can be:

– Repeated– Versioned– Benchmarked– Tested (in staging environment, please!)

Page 41: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Thoughts● Focus on the purpose not on emulating● Make a plan● Test, test, test● Learn PostgreSQL

Page 42: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Thoughts● Focus on the purpose not on emulating● Make a plan● Test, test, test, test, test, test, test● Learn PostgreSQL

Page 43: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Thoughts● Focus on the purpose not on emulating● Make a plan● Test, test, test, test, test, test, test● Learn PostgreSQL and get help

Page 44: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Thoughts● Focus on the purpose not on emulating● Make a plan● Test, test, test, test, test, test, test● Learn PostgreSQL and get help● Don’t Panic

Page 45: Migrating to PostgreSQL · 2019-10-24 · Migrating to PostgreSQL / PgConf.EU Milano, 16 October 2019 Take Advantage of the Application Some applications support multiple databases

https://www.2ndQuadrant.com

Migrating to PostgreSQL / PgConf.EUMilano, 16 October 2019

Thanks and RememberBenjamin Zander’s Rule #6

Boriss [email protected]

@tchorix