Built-in Physical and Logical Replication in Postgresql Frat Gle - - - PowerPoint PPT Presentation

built in physical and logical replication in postgresql f
SMART_READER_LITE
LIVE PREVIEW

Built-in Physical and Logical Replication in Postgresql Frat Gle - - - PowerPoint PPT Presentation

Built-in Physical and Logical Replication in Postgresql Frat Gle - Company Hepsiexpress - Now Infrastructure & Database Manager Member of Postgresql Europe Contributing to Open Source Community - Next Talks Austria PGDay 2019,


slide-1
SLIDE 1

Built-in Physical and Logical Replication in Postgresql

slide-2
SLIDE 2

Fırat Güleç

  • Company

Hepsiexpress

  • Now

Infrastructure & Database Manager Member of Postgresql Europe Contributing to Open Source Community

  • Next Talks

Austria PGDay 2019, September 6th, 2019

slide-3
SLIDE 3

Agenda

  • What is Replication?
  • Why do we need Replication?
  • How many replication layers do we have?
  • Understanding milestones of built-in Database Physical Replication.
  • What is the purpose of replication? and How to rescue system in case of failover?
  • What is Streaming Replication and what is its advantages? Async vs Sync, Hot standby etc.
  • How to configurate Master and Standby Servers? And What is the most important parameters?

Example of topoloji.

  • What is Cascading Replication and how to configurate it? Live Demo on Terminal.
  • Quorum Commit for Sync Replication etc.
  • What is Logical Replication coming with PostgreSQL 10? And What is its advantages?
  • What is the purpose of Logical replication?
  • How to set up Logical Replication and What are its benefits?
  • Limitations of Logical Replication
  • Logical Replication vs Physical Replication in detail.
  • 10 Questions quiz and giving some gifts to participants according to their success.
slide-4
SLIDE 4

What is Replication?

Master Slave Slave Slave Slave Slave Slave Slave Slave Slave

slide-5
SLIDE 5

Replication Layers

Application Database logical Database physical Operating System Hardware

SAN NAS DRBD Streaming Replication Logical Replication Slony Londiste Pgpool II Bucardo

slide-6
SLIDE 6

High Availability, Load Balancing, a and Replication Feature Matrix

https://www.postgresql.org/docs/11/different-replication-solutions.html

slide-7
SLIDE 7

Pyhsical Replication

slide-8
SLIDE 8

The purpose of Streaming Replication?

High availability

App Server Client

Master Slave

slide-9
SLIDE 9

The purpose of Streaming Replication?

Load Balancing

App Server Client

Master Slave %50 %50

slide-10
SLIDE 10

Database Physical Replication Milestones

File based from 8.3 Streaming Replication 9.0 Synchronous since 9.1 Quorum commit since 10.0

slide-11
SLIDE 11

Architecture of Streaming Replication

Master Slave WAL

WAL sender WAL receiver

WAL Record 16 MB pg_wal WAL Archieve directory

slide-12
SLIDE 12

Database Physical

File based from 8.3 Streaming Replication 9.0 Synchronous since 9.1 Quorum commit since 10.0

slide-13
SLIDE 13

File based Replication from Postgresql 8.3

Master Slave WAL

WAL sender WAL receiver

WAL Record 16 MB pg_wal WAL Archieve directory WAL 16 MB

slide-14
SLIDE 14

Database Physical

File based from 8.3 Streaming Replication 9.0 Synchronous since 9.1 Quorum commit since 10.0

slide-15
SLIDE 15

Streaming Replication

Master Slave WAL

WAL sender WAL receiver

WAL Record 16 MB pg_wal WAL Archieve directory Record-based log shipping The primary and standby servers so that they are as similar as possible 1- Major PostgreSQL release levels is not possible 2- 32-bit to a 64-bit system will not work. pg_wal

WAL

slide-16
SLIDE 16

Database Physical

File based from 8.3 Streaming Replication 9.0 Synchronous since 9.1 Quorum commit since 10.0

slide-17
SLIDE 17

Streaming Replication

Master Slave WAL

WAL sender WAL receiver

WAL Record 16 MB

1- Async vs Sync 2- Hot Standby or Warm Standby?

read write read pg_wal WAL Archieve directory

slide-18
SLIDE 18

Database Physical

File based from 8.3 Streaming Replication 9.0 Synchronous since 9.1 Quorum commit since 10.0

slide-19
SLIDE 19

Quorum Commit for Sync Replication

  • 10.0: Quorum Commit

Master Slave 2 Slave 3 Slave 1 sync 10.0synchronous_standby_names ANY 2 (s1, s2, s3) sync sync 10.0synchronous_standby_names FIRST 2 (s1, s2, s3)

slide-20
SLIDE 20

6 Steps for Streaming Replication

Master Slave

  • 1. Replica user is created
  • 2. Configuration for Master
  • 3. Authentication
  • 4. Configuration for Slave
  • 5. Synchronization
  • 6. Create Recovery.conf
slide-21
SLIDE 21

Hot Standy - postgresql.conf

  • wal_level  determines how much information is written

wal_level=‘minimal’ wal_level=‘replica’ wal_level=‘logical’

slide-22
SLIDE 22

max_wal_senders

  • max_wal_senders= specifies the maximum

number of concurrent connections

Master Slave Slave Slave

slide-23
SLIDE 23

wal_keep_segments

slide-24
SLIDE 24

wal_keep_segments

Master Slave WAL WAL

WAL sender WAL receiver

WAL Record 16 MB pg_wal WAL Archieve directory

slide-25
SLIDE 25

1-Replication User for Master

  • sudo -u postgres psql

Next, create a new user and role with the following command:

  • postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

PASSWORD ‘*******'; postgres=#\du

  • You should see the following output:
slide-26
SLIDE 26

2-Hot Standby C Configuration for r Master

in postgresql.conf

  • wal_level=replica
  • wal_keep_segment=20
  • max_wal_sender=3
  • archieve_mode=on
  • archive_command = 'test ! -f /var/lib/postgresql/pg_log_archive/%f

&& cp %p /var/lib/postgresql/pg_log_archive/%f'

slide-27
SLIDE 27

3-pg_h _hba.conf configuration for Master

For authentication: host replication replica 10.90.82.61/32 md5

slide-28
SLIDE 28

4-Hot standy configuration for slave

In Postgresql.conf

  • hot standby=on

Below configuration in case of fail over

  • archive_mode = on
  • archive_command = 'test ! -f /var/lib/postgresql/pg_log_archive/%f && cp

%p /var/lib/postgresql/pg_log_archive/%f‘

  • wal_keep_segment=20
  • max_wal_sender=3
slide-29
SLIDE 29

5-Syncronize Data from Master Server to Slave Server

On the slave server, stop the postgresql service:

  • sudo systemctl stop postgresql and move existing data folder.
  • pg_basebackup -h 10.90.82.31 -D /var/lib/postgresql/11/main/ -P -U

replica --wal-method=fetch

Master Slave

Transfering…… Data file directory

/var/lib/postgresql/11/main 10.90.82.31 10.90.82.61

slide-30
SLIDE 30

6-Recovery.conf file on standby

Datafile Directory/var/lib/postgresql/11/main

slide-31
SLIDE 31

Test Replication

Commandpsql -x -c "select * from pg_stat_replication;" Streaming Replication is DONE

slide-32
SLIDE 32

Cascading Postgresql Replication

Master Slave Slave Slave Slave Slave Slave Upstream Slaves Downstream Slaves Replication Cascading Replicaiton hot_standby=on Syncronization 1. 2. 3. async

  • 4. recovery.conf
slide-33
SLIDE 33

max_standby_archive_delay for standby

slide-34
SLIDE 34

Logical Replication

slide-35
SLIDE 35

Lo Logical r replication

Master Slave

St Streamin ing r replic licatio ion

Master Slave DB A DB B DB A DB B DB A DB B DB A Employee

What i t is Lo Logical Replication?

slide-36
SLIDE 36

Master Slave

St Streamin ing r replic licatio ion

Master Slave Recovery Mode Create index Create table Drop table Create function Insert Delete Select Hot Standby Select Warm Standby New Instance Create index Create table Drop table Create function Insert Delete Select

What i t is Lo Logical Replication?

Lo Logical r replication

slide-37
SLIDE 37

Master Slave

St Streamin ing r replic licatio ion

Master Slave PostgreSQL 9.5 PostgreSQL 9.5 PostgreSQL 11 PostgreSQL 12 PostgreSQL 9.6 PostgreSQL 10 PostgreSQL 10 PostgreSQL 11

What i t is Lo Logical Replication?

Lo Logical r replication

slide-38
SLIDE 38

Ex Expected use cases of

  • f Log
  • gical

l replicati tion

St Streamin ing r replic licatio ion

Client

  • 1. High availability
  • 2. Load Balancing

Master Standby

  • 1. Analytical Purpose

Master A Master B DB C

  • 2. Sharing a subnet of database
slide-39
SLIDE 39

Ex Expected use cases of

  • f Log
  • gical

l replicati tion

St Streamin ing r replic licatio ion

Client

  • 1. High availability
  • 2. Load Balancing

Master Standby

Master A Master B

  • 3. Online Upgrade

PostgreSQL 10 PostgreSQL 11 App

Dump Schema Logical Replication

slide-40
SLIDE 40

Logical r replication

Master Slave

St Streamin ing r replic licatio ion

Master Slave

WAL sender WAL receiver

WAL Streaming

WAL sender

Publication Subscription

Apply Worker

WAL WAL

Decoding Decoded message Logical Replication Launcher triggers

Arch chitectu cture

slide-41
SLIDE 41

What i is Publicati tion&Subscription?

St Streamin ing r replic licatio ion

Master Slave Slave Streaming Replication Cascading Replication Slave A Slave B Publication 2 Publication 1 Subscription 2 Master Subscription 1 Pub 3 Slave C Subs 3 Users Name Users Name Surname

Async vs Sync

Logical r replication

Logical Replication Slot 1 Logical Replication Slot 2

slide-42
SLIDE 42

Lo Logical r replicati tion

St Streamin ing r replic licatio ion

Master Slave Slave Streaming Replication Cascading Replication DB A Sub 1 Pub 1 Surname DB B Address Pub 2 Sub 2

slide-43
SLIDE 43

Lo Logical r replicati tion

St Streamin ing r replic licatio ion

Master Slave Slave Streaming Replication Cascading Replication Server A Surname Server B Surname Bi directional replication not allowed

slide-44
SLIDE 44

Logical Replication Limitations in 11.0

  • does not replicate schema and DDL
  • does not replicate sequences
  • does not replicate Large Objects
  • Replication is only possible from base tables to base tables
slide-45
SLIDE 45

Lo Logical r replicati tion with Postg tgreSQL 11 11

Master 10.90.82.30

wal_level = logical 1. 3. WAL 6.

Publication 1

repluser CREATE TABLE user (user_name text PRIMARY KEY, full_name text); CREATE PUBLICATION Publication1 for table user; 4. CREATE ROLE repluser WITH LOGIN PASSWORD 'admin123' REPLICATION ; user GRANT SELECT ON public.delivery TO repluser; GRANT USAGE ON SCHEMA public TO repluser; 5. 7.

Slave 10.90.82.31

Connected to db with postgres \c database1 2.

Pg_hba.conf host all repluser 10.90.82.31/0 md5

Connected to db with postgres \c database2 1. 2. CREATE SUBSCRIPTION Subscribe1 CONNECTION 'host=10.90.82.31 dbname=database1 user=repluser password=admin123' PUBLICATION Publication 1 ;

Subscribe1

CREATE TABLE user (user_name text PRIMARY KEY, full_name text); user 3.

Logical Replication is DONE

slide-46
SLIDE 46

Topology(Logical & Pyhsical Replication)

App Server Client

Master Slave read write read Slave Cascading Replication Streaming Replication read

Reporting Server

Slave Logical Replication Analytical purposes

App Server

read write CPU & Memory

slide-47
SLIDE 47

Monitoring on Terminal

1- pg_stat_replication 2- pg_replication_slots 3- pg_publication_tables 1- pg_subscription 2- pg_stat_subscription 3- pg_subscription_rel Master Slave(Logical) Slave(Physical) 1- pg_stat_wal_receiver

slide-48
SLIDE 48

https://kahoot.it/

slide-49
SLIDE 49

With an innovative service approach, We are shaping delivery and transportation system in Turkey

www.hepsiexpress.com

slide-50
SLIDE 50

Hepsiexpress & Horizon 2020

ISA: Innovative logistics Solutions and data framework for the on- demand economy

We are looking for partners!

2018 Applications Focus Areas for New Projects

  • Logistics Research & Innovation
  • Route Optimisation
  • Natural Language Processing
  • Open Source Softwares
slide-51
SLIDE 51

Thank you Fırat Güleç