Top Banner
Replication in Postgres
30

Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Oct 17, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Replication in Postgres

Page 2: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Agenda

• Replikasyon nedir? Neden ihtiyaç vardır?

• Log-Shipping nedir?

• High Availability'ye ve Load Balancing'e nasıl etkisi vardır?

• Failover anında bizi nasıl kurtarır?

• Core PostgreSQL Replikasyon nasıl yapılır ve tipleri nelerdir? Örnek topoloji.

• Streaming Replication ve avantajları nelerdir?

• Cascading Replication ve detayları nelerdir

• Kurulumdaki master ve standby'ın konfigurasyonu

• Replikasyon için ayarlanması gereken önemli parametreler hangileridir?

• Postgresql 10 ile gelen Logical Repikasyon ve Quorum Commit

• Kahoot Uygulaması ile yarışma→ https://kahoot.it/ veya Uygulamayı indir!!

Page 3: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

What is Replication?

Master Slave

Slave

Slave

Slave

Slave

Slave

Slave

Slave

Slave

Page 4: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Replication Layers

Application

Database logical

Database physical

Operating System

Hardware

Page 5: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

High Availability, Load Balancing, and Replication Feature Matrix

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

Page 6: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

What is Replication?

High availability

App Server

Client

Master Slave

Page 7: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

What is Replication?

Load Balancing

App Server

Client

Master Slave

%50 %50

Page 8: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Database Physical

WAL based Replication

File based from 8.3

Streaming Replication

9.0

Synchronous since 9.1

Quorum commit

since 10.0

Page 9: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Streaming Replication

Master Slave

WAL WAL

WAL sender

WAL receiver

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.

WAL Record

16 MB

pg_xlog

WALArchieve directory

Page 10: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Streaming Replication

Master Slave

WAL WAL

WAL sender

WAL receiver

WAL Record

16 MB

1- Async vs Sync

2- Hot Standby or not?

readwrite read

pg_xlog

WALArchieve directory

Page 11: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Hot Standy - postgresql.conf

•wal_level → determines how much information is written

wal_level=‘minimal’

wal_level=‘archive’

wal_level=‘hot_standby’

Page 12: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

max_wal_senders

•max_wal_senders= specifies the maximum

number of concurrent connections

Master

Slave

Slave

Slave

Page 13: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

max_wal_segments

Page 14: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

max_wal_segments

Master Slave

WAL WAL

WAL sender

WAL receiver

WAL Record

16 MB

pg_xlog

WALArchieve directory

Page 15: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Replication User

• 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:

Page 16: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Hot Standby Configuration for Master

in postgresql.conf

•wal_level=hot_standby

•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'

Page 17: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

pg_hba.conf configuration for Master

For authentication:

host replication replica 10.70.82.60/32 md5

Page 18: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

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

Page 19: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Syncronize Data from Master Server to Slave ServerOn the slave server, stop the postgresql service:

• sudo systemctl stop postgresql and move existing data folder.

pg_basebackup -h 10.70.82.30 -U replica -D /var/lib/postgresql/9.5/main -P –xlog

Master Slave

Transfering……

Data file directory

/var/lib/postgresql/9.5/main

Page 20: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Recovery.conf file on standby

Datafile Directory→/var/lib/postgresql/9.5/main

Page 21: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Test Replication

Command→psql -x -c "select * from pg_stat_replication;"

Page 22: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Cascading Postgresql Replication

Page 23: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Topoloji

App Server

Client

Master Slave

readwrite read

SlaveCascading Replication

Replication

read

Reporting Server

Page 24: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

max_standy_archive_delay for standby

Page 25: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Terminal

Page 26: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Logical replication with PostgreSQL 10

MasterSlave

2- Logical Replication has cross-version support.

1- Block level replication out, row level replication in.

Slave

Publication 2

Publication 1 Subscription 1

Subscription 23- When compared, Logical Replication has less write amplification than Streaming Replication

4- Logical Replication provides storage flexibility through replicating smaller sets (even partitioned tables)

5- Logical Replication replicates data objects based upon their replication identity (generally aprimary key or unique index)

Page 27: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Limitations in 10.0

•does not replicate schema/DDL

•does not replicate sequences

•does not replicate TRUNCATE

•does not replicate Large Objects

• Tables must have primary key or unique key

Page 28: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

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)

9.6→synchronous_standby_names (s1, s2, s3)

sync

sync

Page 29: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver
Page 30: Replication in Postgres - postgresug.londonpostgresug.london/assets/presentations/firat_gulec_replication.pdf · Streaming Replication Master Slave WAL WAL WAL sender WAL receiver

Thank you

Fırat Güleç