logical replication in postgresql - flossuk 2016

26
© 2ndQuadrant 2016 Logical Replication in PostgreSQL Petr Jelínek PostgreSQL Contributor & Consultant

Upload: petr-jelinek

Post on 15-Jan-2017

372 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Logical Replication in PostgreSQL

Petr JelínekPostgreSQL Contributor & Consultant

Page 2: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

History

Page 3: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Postgres Replication History

• PostgreSQL 7.x (~2000)– Replication should not be part of core Postgres

• PostgreSQL 8.0 (2005)– Point-In-Time Recovery

• PostgreSQL 9.0 (2010)– Streaming Replication (physical)

• PostgreSQL 9.4 (2014)– Logical Decoding (changeset extraction)

Page 4: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Logical Replication History

• Trigger based solutions– Slony (~2004)

– Londiste (~2007)

• Result of the no replication in core philosophy

• Use table(s) as queue– Amplify writes on the upstream

• Written in scripting languages

• But work for the most part

Page 5: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

BDR Project

• Native logical replication

• Multi-master

• Firm design 2011

• Prototype 2012

• Resulted in many patches to PostgreSQL 9.3, 9.4, 9.5 and now 9.6

– For example Logical Decoding

• Not yet fully integrated into PostgreSQL

Page 6: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Current state

• BDR– Modified PostgreSQL 9.4 + Extension

– Multi-master

– Transparent DDL

• pglogical– Extension for 9.4+

– One way replication

– Replacement for trigger-based solutions

– Integrating into PostgreSQL core

Page 7: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Streaming Replication

Page 8: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Physical Streaming Replication

WALSender

DatabaseWAL

WALRecvr Startup

DatabaseWAL

User

Master Standby

Page 9: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Logical Streaming Replication

WALSender

Database

Apply

DatabaseWAL

User

Master Standby

Page 10: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Logical Decoding

Page 11: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Logical Decoding

WALSender

DatabaseWAL

User

Master

Page 12: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Logical Decoding

• Extracts information from Write-Ahead Log into logical changes (INSERT/UPDATE/DELETE)

• Per row and commit ordered

• C API for output plugin

• No DDL

• SQL Interface

• Streaming Interface

Page 13: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Existing output plugins

• https://github.com/confluentinc/bottledwater-pg– Decodes change into AVRO

– Sends data to Kafka

• https://github.com/xstevens/decoderbufs– Decodes to protocol buffers

• pglogical_output– Plugin that pglogical uses

– “Native” protocol

– JSON protocol

Page 14: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

pglogical

Page 15: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

pglogical

WALSender

Database

Apply

DatabaseWAL

User

Master Standby

Page 16: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Logical Replication

• Target node is writeable– Allows temp tables

– Allows different indexes

– Allows different security

– Allows (some) data transformation

• Selective Replication– Can replicate subset of database

• Cross-version

Page 17: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

pglogical

• Replicates transactions in commit order

• Selective Replication

• Online Upgrade

• Data Transport– Data integration

– Streaming changes to analytical database

– Master configuration data management

– …

• Optionally semi-synchronous

Page 18: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Replication Sets

• Replication is defined in terms of groups (sets) of tables, rather than individual tables

– Need to be defined on each upstream node

• Table is not replicated until added to a set

• Tables may be defined in more than one set, but changes for the table will only be sent once to each subscriber

• Predefined sets, “default”, “default_insert_only”, “ddl_sql”

Page 19: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Replication Actions

• By default replication sets replicate all actions– INSERT, UPDATE, DELETE, TRUNCATE

• It's possible to filter actions for given replication set

• Useful for data aggregation, data warehousing etc.

Page 20: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Installation

• Extension– All configuration in database

• Provider + Subscriber– CREATE EXTENSION pglogical;

– create_node(name, dsn)

• Provider– replication_set_add_table(set_name, table_name)

• Subscriber– create_subscription(name, provider_dsn, ...)

Page 21: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Example setup

OLTP1 OLTP2

OLAP

CFG

Page 22: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Performance

1 2 4 8 16 24 320

2000

4000

6000

8000

10000

12000

14000

pglocicalslonylondiste3SR

Page 23: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

DDL Replication

• Initial schema either fully synchronized or not at all

• The DDL commands are not automatically replicated yet

• pglogical.replicate_ddl_command( command [, replication_sets])

Page 24: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Other caveats

• Sequences not replicated yet– Fixed in upcoming 1.1

• Big transactions

• Does not play well with physical replication yet– Failover

– Fixed in PostgreSQL 9.6

Page 25: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Future plans

• Make it available in PostgreSQL out of the box – 9.7?

• Data transformation hooks

• Filtering by expression

• Push replication

• Integrate BDR features– Multi-master

– Transparent DDL replication

Page 26: Logical Replication in PostgreSQL - FLOSSUK 2016

© 2ndQuadrant 2016

Questions?

[email protected]

• http://2ndquadrant.com/pglogical/