Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and...

42
Oracle to PostgreSQL replication and migration Tomasz Rybak TeraData [email protected] PgConfEU, 2015-10-29 Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 1 / 42

Transcript of Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and...

Page 1: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Oracle to PostgreSQL replication and migration

Tomasz Rybak

[email protected]

PgConfEU, 2015-10-29

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 1 / 42

Page 2: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Outline

1 Our system

2 ORQ

3 Problems and solutionsPerformance

4 Our changes

5 ProcessLesson learned

6 Summary

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 2 / 42

Page 3: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Who am I

ProfessionallyDBA at Teradata in Muenchen

FLOSSDebian Maintainer (with open NM process)PythonGPGPU (PyOpenCL, PyCUDA)

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 3 / 42

Page 4: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Disclaimer

All opinions presented here are my own, and my employer is notresponsible for them.

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 4 / 42

Page 5: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Our system

Big picture

Many databases

PostgreSQLOracle

Databases created over many years

Oracle

First database; initial schemaQuite old (10g), currently unsupported version

Decision

Unification on PostgreSQLUnification of schema

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 5 / 42

Page 6: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Our system

Oracle

Over 700 tables

widest table over 600 columnslargest table 5.5G rowsdatabase size almost 1.5TB

Our first database

Old hardware

Magnetic hard drivesHigh IOLimited disk space

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 6 / 42

Page 7: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Our system

Production

Live systems

Clients’ jobs constantly running

Cannot stop and migrate data offline

Cannot add much load

Quite high IO

We are already using Londiste on PostgreSQL side

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 7 / 42

Page 8: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

ORQ

Solution

ORQ from 2ndQuadrant

Similar to londiste. . . but not identical

Mostly Python

cx Oraclepsycopg2

Quite a bit of PL/SQL

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 8 / 42

Page 9: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

ORQ

Architecture

Code on Oracle

TriggersTable with eventsMetadata tables: subscribers, etc.

PostgreSQL

ExtensionJust information about tables

Daemons

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 9 / 42

Page 10: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

ORQ

Daemons

orqd ticker, allowing for determining when to apply transactionchanges

orqrep replicator; only one (unlike in londiste)

orqrep-admin management tool

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 10 / 42

Page 11: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

ORQ

Oracle

orq user and schema

triggers

configuration tables

ticksqueuesdaemon statustrigger namesand not the list of replicated tables

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 11 / 42

Page 12: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

ORQ

PostgreSQL

user

extension

only one table: list of replicated tables

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 12 / 42

Page 13: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

ORQ

Queues

For storing events

Insert, Update, DeleteAlso adding and removing table

Three (not one) tables for queue

Tables rollingHelps with cleaning old events

Need to prevent replication from rolling tables with long transactions

Need to determine from which queues to read events

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 13 / 42

Page 14: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

ORQ

Multi-table queues

Helps with changes to event tables on live system

More difficult debugging

More complicated event queries

orqd

Determines when to rotate based on ticks seen by orqrepDeletes old ticks

Lack of rotation suggests some stuck transaction

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 14 / 42

Page 15: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Problems and solutions

Encoding

Everything should be in UTF8

Two ways of inserting data on PostgreSQL side

initial copyevent applying

Problems for PostgreSQL: Not always valid UTF8 characters

Difficult debugging

cx Oracle uses Oracle libraries

Force Oracle to give use UTF8

Both as parameter to cx Oracle and as environment variable

Took lot of checking and searching

Still not always perfect

Additional code to check and fix text fields

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 15 / 42

Page 16: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Problems and solutions

Object names

Oracle size limit: 30 characters

Triggers: table name and suffix

Need to manage triggers

Additional table with mapping

table name ⇒ shortened trigger name

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 16 / 42

Page 17: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Problems and solutions

Triggers

Initially one trigger for all operations

Generated per table, using list of columns

For many tables it was bigger than 32k

We had to move to CLOB from varchar

For some of tables (500 columns) generated 560kB of code

Much larger than limit

Documentation: 32kBTests: 90kB

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 17 / 42

Page 18: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Problems and solutions

Triggers - cont.

Complexity matters more than size

We had to split trigger to 3, to decrease number of statements

It allowed for simplifying of trigger code

Static list of columns

Causes problems with schema migration

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 18 / 42

Page 19: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Problems and solutions Performance

Events

Need to group transaction statements

Need to order transactions

ora rowscn

Virtual columnSimilar to txid. . . but not really

Each event had ora rowscn

Order by it

Ticks (from orqd) determine which SCN to use

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 19 / 42

Page 20: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Problems and solutions Performance

System Change Number

ora rowscn is virtual column

No index!Full Table Scan for all events

Problematic when more than few hundred thousands of events

Decrease time of rolling event tables

Did not help

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 20 / 42

Page 21: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Problems and solutions Performance

Index

Need to use index

Cannot on ora rowscn

Create ordinary column and fill in

SCN given at the end of transactionLet’s use dbms fallback.get current change number!

Big mistake: It’s changing during transaction!

Caused problems more than once

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 21 / 42

Page 22: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Problems and solutions Performance

Index - cont.

Keep the column but update it on batch

10g: cannot use ora rowscn in non-selectLoop over transaction

Not fast enough for our needs

Tried to use during tick generation but it didn’t work

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 22 / 42

Page 23: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Problems and solutions Performance

Back to blackboard

SCN issued at the end of transaction

Before that column is empty

Problem

Two problems?

Grouping events into transactionsTransactions ordering

Solution: separate grouping and ordering

Events use local transaction idNot ordered, using implementation detailsFile, slot, transaction inside slot

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 23 / 42

Page 24: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Problems and solutions Performance

Changes

Additional table for transactions

Additional trigger, per statement

Check whether transaction exists, to avoid bloat

One row per transaction — we can use SCN

Problem with joining tables and rotating

Use CTE/WITH

local transaction id exists only after DML

Problem when adding or removing table

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 24 / 42

Page 25: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Problems and solutions Performance

Initial copy

Not very fast: less than 2M rows per minute

Similar speed on sqlplus

Tried different approaches

Finally using Java

Copy done in separate process

Java was fast for narrow tables, but slower for wide ones

JDBC vs. cx OracleDifferent GC

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 25 / 42

Page 26: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Our changes

Schema differences

Oracle uses schema per user

Tables are prefixed when read by different user

Not in PostgreSQL

So we use “different” table names for Oracle and PostgreSQL

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 26 / 42

Page 27: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Our changes

Logging

Error in trigger

Log it, and otherwise ignore

Do not rollback parent transaction!

EXCEPTION WHEN OTHERS THEN . . .

Just like “try: except:” in Python

MERGE (UPSERT) to log number of errors

Ignore errors during logging

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 27 / 42

Page 28: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Our changes

Trigger compilation

By default trigger gets compiled on first usage

Slowing first transactionCatching problems on production

We forced compilation of trigger during creation

Logging any problemsAlso removing triggers and table from replication in such a case

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 28 / 42

Page 29: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Our changes

Performance

Dropping indices before initial copy

Recreating them later

Similarly for FKs, constraints, etc.

Already done in Londiste

Problem with restoring after errors

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 29 / 42

Page 30: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Our changes

Security

Separate user

Although with some special privileges

Separate tablespace

Logging errors

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 30 / 42

Page 31: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Process

Schema change

Cleaning schema on Oracle

Unifying as much as possible with PostgreSQL

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 31 / 42

Page 32: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Process

Live systems

Schema migrations on active databases

Need to be applied on replica

Regeneration of triggers

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 32 / 42

Page 33: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Process

First attempt

Missing tick on day of switch

Removed during table rollCaused by dbms flashback.get current system change

orqrep did not know what to do

I was at DebConf

To many unprocessed events when discovered

Led us to discover difference between PostgreSQL and Oracle

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 33 / 42

Page 34: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Process

Interlude

Solve SCN problem

Many tried solutions

Filled up tablespace

Slow MERGE

Lock contention

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 34 / 42

Page 35: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Process

Second attempt

Intensive work on ORQ

Java for copyUsage of transaction id

Direct connection between Postgres and Oracle

To avoid problems during network changes

Quite fast

Failed

Broken storageLogging (and ignoring) errors save us here

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 35 / 42

Page 36: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Process Lesson learned

Truisms

Documentation helps

When it exists

Google helps

When it points to right answersOracle versionsOracle vs. MySQL

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 36 / 42

Page 37: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Process Lesson learned

Tailored solutions

Existing system

We changed it for our needs

We used quick rolls, to avoid keeping many events

Limited look-back — only one table back

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 37 / 42

Page 38: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Process Lesson learned

Quirks

SELECT AS OF SCN

Interesting way of looking into near past

MERGE

UPSERTDid nor work correctlyMaybe I made some errors

clob vs. varchar

Artificial limitationDifferent code to deal with it

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 38 / 42

Page 39: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Process Lesson learned

Hints

Let the flamewar begin!

Did not work for us

Neither for copyNor for event queries

Parallel copy did not work

All tables of degree 1

Suggestion of index usage ignored

prefetch (array size in cx Oracle) did not have much effect

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 39 / 42

Page 40: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Process Lesson learned

Many attempts

Practice makes perfect

Monitoring of databases

. . . and replication

Transaction size statistics

Many changed only one rowSome few dozen thousand changesOne changed 1.1M of rows

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 40 / 42

Page 41: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Summary

Result

We did it! (On Sunday)

Many thanks:

Teradata team2ndQuadrant team

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 41 / 42

Page 42: Oracle to PostgreSQL replication and migration · PDF fileOracle to PostgreSQL replication and migration Tomasz Rybak TeraData tomasz.rybak@teradata.com PgConfEU, 2015-10-29 Tomasz

Summary

QA

Thank you for your attention

Questions?

Contacts to me

Blog: http://tomaszrybak.wordpress.com/Email:

[email protected]@post.pl

Skype: rybakthomasGTalk: [email protected]

Tomasz Rybak (TeraData) Oracle to PostgreSQL PgConfEU, 2015-10-29 42 / 42