replication in postgres - pgrpms.org · •sudo -u postgres psql next, create a new user and role...

30
Replication in Postgres

Upload: others

Post on 11-Aug-2020

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

Replication in Postgres

Page 2: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

What is Replication?

Master Slave

Slave

Slave

Slave

Slave

Slave

Slave

Slave

Slave

Page 4: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

Replication Layers

Application

Database logical

Database physical

Operating System

Hardware

Page 5: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

What is Replication?

High availability

App Server

Client

Master Slave

Page 7: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

What is Replication?

Load Balancing

App Server

Client

Master Slave

%50 %50

Page 8: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

max_wal_senders

•max_wal_senders= specifies the maximum

number of concurrent connections

Master

Slave

Slave

Slave

Page 13: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

max_wal_segments

Page 14: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

max_wal_segments

Master Slave

WAL WAL

WAL sender

WAL receiver

WAL Record

16 MB

pg_xlog

WALArchieve directory

Page 15: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

pg_hba.conf configuration for Master

For authentication:

host replication replica 10.70.82.60/32 md5

Page 18: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

Recovery.conf file on standby

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

Page 21: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

Test Replication

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

Page 22: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

Cascading Postgresql Replication

Page 23: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

Topoloji

App Server

Client

Master Slave

readwrite read

SlaveCascading Replication

Replication

read

Reporting Server

Page 24: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

max_standy_archive_delay for standby

Page 25: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

Terminal

Page 26: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

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 - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED
Page 30: Replication in Postgres - pgrpms.org · •sudo -u postgres psql Next, create a new user and role with the following command: •postgres=#CREATE USER replica REPLICATION LOGIN ENCRYPTED

Thank you

Fırat Güleç