52
/ Denish Patel Sr. Database Architect

Out of the Box Replication in Postgres 9.4(PgConfUS)

Embed Size (px)

Citation preview

Page 1: Out of the Box Replication in Postgres 9.4(PgConfUS)

/

Denish PatelSr. Database Architect

Page 2: Out of the Box Replication in Postgres 9.4(PgConfUS)

} Sr. Database Architect @ Medallia} Previously:◦ DB Engineer@ WithMe◦ Lead Database Architect @ OmniTI

} Expertise in PostgreSQL , Oracle, MySQL, NoSQL} Contact : [email protected] or

[email protected]} Twitter: @DenishPatel} Blog: http://www.pateldenish.com} Postgres Slack Channel (https://postgres-

slack.herokuapp.com/)

1

Page 3: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ What is WAL?§ Postgres Replication History§ How you setup replication now?§ What’s missing ?§ Why replication slots?§ DemoI’m NOT going to discuss …§ SLONY or other 3rd party replication tools§ 3rd party replication management tools

2

Page 4: Out of the Box Replication in Postgres 9.4(PgConfUS)

} Roll-forward recovery aka REDO} flushed to disk to guarantee commit durability} sequential writes} lower cost than flushing page cache} Allows us to do cool things

� Crash recovery� Binary backups� Point-In Time Recovery� Replication

3

Page 5: Out of the Box Replication in Postgres 9.4(PgConfUS)

} Automatically enabled; no action required} Make sure to have enough space on server} Stored under pg_xlog directory} Normally, 16MB in size

ü--with-wal-segsize config option at build} Each segment is divided into pages (8 kB page)

ü--with-wal-blocksize config option at build◦ Segment name starts with..

000000010000000000000000

4

Page 6: Out of the Box Replication in Postgres 9.4(PgConfUS)

select * from pg_settings where name='wal_level';name | wal_levelsetting | hot_standbyunit | category | Write-Ahead Log / Settingsshort_desc | Set the level of information written to the WAL.extra_desc | context | postmastervartype | enumsource | configuration filemin_val | max_val | enumvals | { minimal, archive, hot_standby, logical }boot_val | minimalreset_val | hot_standbysourcefile | /var/lib/pgsql/9.4/data/postgresql.auto.confsourceline | 4

5

Page 7: Out of the Box Replication in Postgres 9.4(PgConfUS)

Almost everything is replicated, but...

§ unlogged tables (As name suggests)§ temporary tables§ hash indexes? (generally don’t use?)

6

Page 8: Out of the Box Replication in Postgres 9.4(PgConfUS)

Postgres 7.0: WALPostgres 8.0: PITR (Point-In-Time-Recovery)Postgres 8.2: pg_standbyPostgres 9.0: Hot_standby, Streaming replicationPostgres 9.1: pg_basebackup, Synchronous

replicationPostgres 9.2: Cascading ReplicationPostgres 9.3: Standby can switch timeline to follow

new masterPostgres 9.4: Replication Slots , Logical decoding

7

Page 9: Out of the Box Replication in Postgres 9.4(PgConfUS)

initdb

8

Page 10: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ max_wal_senders=10§ wal_level=hot_standby§ hot_standby=on (on standby)

9

Page 11: Out of the Box Replication in Postgres 9.4(PgConfUS)

#TYPE DATABASE USER ADDRESS METHOD host replication replication 10.0.0.1/32 md5

10

Page 12: Out of the Box Replication in Postgres 9.4(PgConfUS)

pg_ctl restart

11

Page 13: Out of the Box Replication in Postgres 9.4(PgConfUS)

CREATE ROLE replication WITH LOGIN REPLICATION;\password replication

12

Page 14: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ Take file system level backups§ What tools you are using for backups?

13

Page 15: Out of the Box Replication in Postgres 9.4(PgConfUS)

primary_conninfo = 'host=primaryhost user=replication password=replication'

standby_mode = on

14

Page 16: Out of the Box Replication in Postgres 9.4(PgConfUS)

pg_ctl start

15

Page 17: Out of the Box Replication in Postgres 9.4(PgConfUS)

Have you configured archiving?

16

Page 18: Out of the Box Replication in Postgres 9.4(PgConfUS)

wal_keep_segments

17

Page 19: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ postgresql.conf archive_mode = onarchive_command = 'cp %p /some/where/%f'

18

Page 20: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ recovery.conf restore_command = 'cp /some/where/%f %p'

19

Page 21: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ local or remote copy§ Scp§ Rsync§ NFS§ pg_archivecleanup

20

Page 22: Out of the Box Replication in Postgres 9.4(PgConfUS)

archive_command = 'rsync %p standby1::pg/%f && rsync %p standby2::pg/%f'

archive_command = 'echo standby1 standby2 ...| xargs -d" " -I{} -n1 -P0 -r rsync %p {}::pg/%f'

21

Page 23: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ OmniPITR§ WAL-E§ Repmgr§ Pgbarman§ Skytools§ A lot of Custom scripts

22

Page 24: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ fsync capabilities§ cp: no§ dd: GNU coreutils§ SSH: OpenSSH 6.5 sftp-server (Jan 2014)§ rsync: patch or wrapper§ NFS: Supported

23

Page 25: Out of the Box Replication in Postgres 9.4(PgConfUS)

24

Page 26: Out of the Box Replication in Postgres 9.4(PgConfUS)

max_replication_slots = 8

25

Page 27: Out of the Box Replication in Postgres 9.4(PgConfUS)

SELECT * FROM pg_create_physical_replication_slot('name');

26

Page 28: Out of the Box Replication in Postgres 9.4(PgConfUS)

select * from pg_replication_slots ;slot_name |plugin |slot_type

|datoid|database|active|xmin|catalog_xmin|restart_lsn------------+--------+-----------+--------+----

------+--------+------+--------------+---standby1 | | physical | | | t | |

|0/21000058standby2 | | physical | | | t | |

|0/21000058standby3 | | physical | | | t | |

|0/21000058

27

Page 29: Out of the Box Replication in Postgres 9.4(PgConfUS)

primary_slot_name = ‘name'

28

Page 30: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ Keep necessary WAL files§ Each standby can have different WAL apply

status§ Single access control setup§ fsync on receiving side

29

Page 31: Out of the Box Replication in Postgres 9.4(PgConfUS)

pg_basebackup \-h primaryhost-U replication \-D $PGDATA \-X stream \–P –v -R

30

Page 32: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ Meet pg_receivexlog§ (Available since Postgers 9.1!)

pg_receivexlog \-D archivedir \

--slot archivingslot \-h primaryhost -U replication

§ -- synchronous option in 9.5

31

Page 33: Out of the Box Replication in Postgres 9.4(PgConfUS)

Are you ready to setup replication without any external tools?

1. pg_basebackup2. streaming with Replication Slots3. pg_receivexlog

32

Page 34: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ Google Drive:§ Login: pgtraining/pgcon§ pgtraining user has sudo access§ You can access terminal on the desktop§ Internet should be working within VM§ Copy/paste should work between VM and Host§ Take snapshot along the process so you can rollback

easily

33

Page 35: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ Remove Postgres 8.4 versionsudo yum erase postgresql.*§ Setup yum repo for your desired versionsudo yum install http://yum.postgresql.org/9.4/redhat/rhel-

6-x86_64/pgdg-redhat94-9.4-1.noarch.rpm§ Install PostgreSQL 9.4 & Contrib modulessudo yum install postgresql94-server postgresql94-contrib§ Create postgres cluster & initial automatic startupsudo service postgresql-9.4 initdbsudo chkconfig postgresql-9.4 onsudo service postgresql-9.4 start

34

Page 36: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ Become postgres system usersudo su - postgres§ Log into database using psql (\? to see all available

commands)§ Create a role & database for yourselfCREATE ROLE pgtraining WITH LOGIN SUPERUSER;CREATE DATABASE pgtraining;§ You can login as pgtraining user (psql –U pgtraining –d

pgtraining)§ Create replication role for laterCREATE ROLE replication WITH LOGIN REPLICATION;§ Set password (”replication”)\password replication

35

Page 37: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ http://www.postgresql.org/docs/9.4/static/auth-pg-hba-conf.html

§ Find location of hba_filepostgres=# show hba_file;§ Add following entry for replication user§ Try to avoid trust authentication§ [pgtraining@localhost ~]$ sudo vi

/var/lib/pgsql/9.4/data/pg_hba.conf host replication replication 127.0.0.1/32

md5

36

Page 38: Out of the Box Replication in Postgres 9.4(PgConfUS)

alter system set wal_level = hot_standby;alter system set archive_mode=on;alter system set max_replication_slots=8;alter system set archive_timeout = 60;alter system set max_wal_senders = 8;alter system set wal_keep_segments=100;alter system set logging_collector=on;

37

Page 39: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ Restart databasesudo service postgresql-9.4 restart§ Verify settingspsql=# show max_wal_senders;

38

Page 40: Out of the Box Replication in Postgres 9.4(PgConfUS)

SELECT * FROM pg_create_physical_replication_slot('standby1');

39

Page 41: Out of the Box Replication in Postgres 9.4(PgConfUS)

sudo su - postgres

pg_basebackup -h 127.0.0.1 -U replication -D /var/lib/pgsql/9.4/slave -R -Xs -P -v

40

Page 42: Out of the Box Replication in Postgres 9.4(PgConfUS)

cd /var/lib/pgsql/9.4/slave§ Edit Standby postgresql.conf file port = 5433 hot_standby = on§ Edit Standby recovery.conf filestandby_mode = 'on'primary_conninfo = 'user=replication password=replication

host=127.0.0.1 port=5432'primary_slot_name='standby1'trigger_file = '/var/lib/pgsql/9.4/slave/finish.recovery'recovery_target_timeline='latest'

41

Page 43: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ Copy existing init filesudo cp /etc/init.d/postgresql-9.4 /etc/init.d/postgresql-9.4-

5433§ Edit config file to change (as root):

PGDATA=/var/lib/pgsql/9.4/slavePGLOG=/var/lib/pgsql/9.4/pgstartup-5433.logPGUPLOG=/var/lib/pgsql/$PGMAJORVERSION/pgupgrade-5433.log

§ Register service, start up slavesudo chkconfig postgresql-9.4-5433 onsudo service postgresql-9.4-5433 start

42

Page 44: Out of the Box Replication in Postgres 9.4(PgConfUS)

pgtraining=# select * from pg_stat_replication;-[ RECORD 1 ]----+------------------------------pid | 3260usesysid | 24576usename | replicationapplication_name | walreceiverclient_addr | 127.0.0.1client_hostname | client_port | 53206backend_start | 2015-06-08 14:47:50.057326-04backend_xmin | state | streamingsent_location | 0/240000B8write_location | 0/240000B8flush_location | 0/240000B8replay_location | 0/240000B8sync_priority | 0sync_state | async

43

Page 45: Out of the Box Replication in Postgres 9.4(PgConfUS)

pgtraining=# select * from pg_replication_slots;-[ RECORD 1 ]+-----------slot_name | standby1plugin | slot_type | physicaldatoid | database | active | txmin | catalog_xmin | restart_lsn | 0/270000EC

44

Page 46: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ Create slot for archiving:SELECT * FROM

pg_create_physical_replication_slot('archiver1');§ Create archive directory

mkdir /var/lib/pgsql/9.4/archive§ Start archiving process in background/usr/pgsql-9.4/bin/pg_receivexlog -h 127.0.0.1 -p

5432 -U replication –s 'archiver1' -n -v -D /var/lib/pgsql/9.4/archive

§ Put under init.d for continuous run§ Switch xlog : primary_db_sever# select

pg_switch_xlog();

45

Page 47: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ Monitor Disk space§ Monitor slave lagselect pg_xlog_location_diff(sent_location, write_location) AS

byte_lagfrom pg_stat_replicationwhere application_name='pg_receivexlog';

§ Monitor WAL archive processselect pg_xlog_location_diff(sent_location, write_location) AS

byte_lagfrom pg_stat_replication

where application_name='walreceiver';

46

Page 48: Out of the Box Replication in Postgres 9.4(PgConfUS)

} Number of pg_xlogs on master } Monitor pg_recievexlog process} Make sure archive location has new files} pg_basebackup log files for successful backup…

look for “pg_basebackup: base backup completed”

47

Page 49: Out of the Box Replication in Postgres 9.4(PgConfUS)

§ Conference committee§ Peter Eisentraut§ You!!

48

Page 50: Out of the Box Replication in Postgres 9.4(PgConfUS)

} http://www.medallia.com/careers/} http://www.medallia.com/open-positions/◦ Ops Engineers◦ Automation Engineers◦ Manager Engineering◦ SRE◦ Many more …

50

Page 51: Out of the Box Replication in Postgres 9.4(PgConfUS)

[email protected] or [email protected]

Twitter: DenishPatel

49

Page 52: Out of the Box Replication in Postgres 9.4(PgConfUS)

} Stay in touch with Postgres users around the world

} 300+ members!} Join today : https://postgres-

slack.herokuapp.com/

52