SlideShare a Scribd company logo
/
Denish Patel
Sr. Database Architect
} Sr. Database Architect @ Medallia
} Previously:
◦ DB Engineer@ WithMe
◦ Lead Database Architect @ OmniTI
} Expertise in PostgreSQL , Oracle, MySQL, NoSQL
} Contact : denish.j.patel@gmail.com or
dpatel@medallia.com
} Twitter: @DenishPatel
} Blog: http://www.pateldenish.com
} Postgres Slack Channel (https://postgres-
slack.herokuapp.com/)
1
§ What is WAL?
§ Postgres Replication History
§ How you setup replication now?
§ What’s missing ?
§ Why replication slots?
§ Demo
I’m NOT going to discuss …
§ SLONY or other 3rd party replication tools
§ 3rd party replication management tools
2
} 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
} 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
select * from pg_settings where name='wal_level';
name | wal_level
setting | hot_standby
unit |
category | Write-Ahead Log / Settings
short_desc | Set the level of information written to the WAL.
extra_desc |
context | postmaster
vartype | enum
source | configuration file
min_val |
max_val |
enumvals | { minimal, archive, hot_standby, logical }
boot_val | minimal
reset_val | hot_standby
sourcefile | /var/lib/pgsql/9.4/data/postgresql.auto.conf
sourceline | 4
5
Almost everything is replicated, but...
§ unlogged tables (As name suggests)
§ temporary tables
§ hash indexes? (generally don’t use?)
6
Postgres 7.0: WAL
Postgres 8.0: PITR (Point-In-Time-Recovery)
Postgres 8.2: pg_standby
Postgres 9.0: Hot_standby, Streaming replication
Postgres 9.1: pg_basebackup, Synchronous
replication
Postgres 9.2: Cascading Replication
Postgres 9.3: Standby can switch timeline to follow
new master
Postgres 9.4: Replication Slots , Logical decoding
7
initdb
8
§ max_wal_senders=10
§ wal_level=hot_standby
§ hot_standby=on (on standby)
9
#TYPE DATABASE USER ADDRESS METHOD
host replication replication 10.0.0.1/32 md5
10
pg_ctl restart
11
CREATE ROLE replication WITH LOGIN REPLICATION;
password replication
12
§ Take file system level backups
§ What tools you are using for backups?
13
primary_conninfo = 'host=primaryhost
user=replication password=replication'
standby_mode = on
14
pg_ctl start
15
Have you configured archiving?
16
wal_keep_segments
17
§ postgresql.conf
archive_mode = on
archive_command = 'cp %p /some/where/%f'
18
§ recovery.conf
restore_command = 'cp /some/where/%f %p'
19
§ local or remote copy
§ Scp
§ Rsync
§ NFS
§ pg_archivecleanup
20
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
§ OmniPITR
§ WAL-E
§ Repmgr
§ Pgbarman
§ Skytools
§ A lot of Custom scripts
22
§ fsync capabilities
§ cp: no
§ dd: GNU coreutils
§ SSH: OpenSSH 6.5 sftp-server (Jan 2014)
§ rsync: patch or wrapper
§ NFS: Supported
23
24
max_replication_slots = 8
25
SELECT * FROM
pg_create_physical_replication_slot('name');
26
select * from pg_replication_slots ;
slot_name |plugin |slot_type
|datoid|database|active|xmin|catalog_xmin|restart_lsn
------------+--------+-----------+--------+----
------+--------+------+--------------+---
standby1 | | physical | | | t | |
|0/21000058
standby2 | | physical | | | t | |
|0/21000058
standby3 | | physical | | | t | |
|0/21000058
27
primary_slot_name = ‘name'
28
§ Keep necessary WAL files
§ Each standby can have different WAL apply
status
§ Single access control setup
§ fsync on receiving side
29
pg_basebackup 
-h primaryhost
-U replication 
-D $PGDATA 
-X stream 
–P –v -R
30
§ Meet pg_receivexlog
§ (Available since Postgers 9.1!)
pg_receivexlog 
-D archivedir 
--slot archivingslot 
-h primaryhost -U replication
§ -- synchronous option in 9.5
31
Are you ready to setup replication without any
external tools?
1. pg_basebackup
2. streaming with Replication Slots
3. pg_receivexlog
32
§ 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
§ Remove Postgres 8.4 version
sudo yum erase postgresql.*
§ Setup yum repo for your desired version
sudo 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 modules
sudo yum install postgresql94-server postgresql94-contrib
§ Create postgres cluster & initial automatic startup
sudo service postgresql-9.4 initdb
sudo chkconfig postgresql-9.4 on
sudo service postgresql-9.4 start
34
§ Become postgres system user
sudo su - postgres
§ Log into database using psql (? to see all available
commands)
§ Create a role & database for yourself
CREATE ROLE pgtraining WITH LOGIN SUPERUSER;
CREATE DATABASE pgtraining;
§ You can login as pgtraining user (psql –U pgtraining –d
pgtraining)
§ Create replication role for later
CREATE ROLE replication WITH LOGIN REPLICATION;
§ Set password (”replication”)
password replication
35
§ http://www.postgresql.org/docs/9.4/static/auth-
pg-hba-conf.html
§ Find location of hba_file
postgres=# 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
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
§ Restart database
sudo service postgresql-9.4 restart
§ Verify settings
psql=# show max_wal_senders;
38
SELECT * FROM pg_create_physical_replication_slot('standby1');
39
sudo su - postgres
pg_basebackup -h 127.0.0.1 -U replication -D
/var/lib/pgsql/9.4/slave -R -Xs -P -v
40
cd /var/lib/pgsql/9.4/slave
§ Edit Standby postgresql.conf file
port = 5433
hot_standby = on
§ Edit Standby recovery.conf file
standby_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
§ Copy existing init file
sudo 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/slave
PGLOG=/var/lib/pgsql/9.4/pgstartup-5433.log
PGUPLOG=/var/lib/pgsql/$PGMAJORVERSION/pgupgrade-
5433.log
§ Register service, start up slave
sudo chkconfig postgresql-9.4-5433 on
sudo service postgresql-9.4-5433 start
42
pgtraining=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid | 3260
usesysid | 24576
usename | replication
application_name | walreceiver
client_addr | 127.0.0.1
client_hostname |
client_port | 53206
backend_start | 2015-06-08 14:47:50.057326-04
backend_xmin |
state | streaming
sent_location | 0/240000B8
write_location | 0/240000B8
flush_location | 0/240000B8
replay_location | 0/240000B8
sync_priority | 0
sync_state | async
43
pgtraining=# select * from pg_replication_slots;
-[ RECORD 1 ]+-----------
slot_name | standby1
plugin |
slot_type | physical
datoid |
database |
active | t
xmin |
catalog_xmin |
restart_lsn | 0/270000EC
44
§ 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
§ Monitor Disk space
§ Monitor slave lag
select pg_xlog_location_diff(sent_location, write_location) AS
byte_lag
from pg_stat_replication
where application_name='pg_receivexlog';
§ Monitor WAL archive process
select pg_xlog_location_diff(sent_location, write_location) AS
byte_lag
from pg_stat_replication
where application_name='walreceiver';
46
} 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
§ Conference committee
§ Peter Eisentraut
§ You!!
48
} http://www.medallia.com/careers/
} http://www.medallia.com/open-positions/
◦ Ops Engineers
◦ Automation Engineers
◦ Manager Engineering
◦ SRE
◦ Many more …
50
Denish.j.patel@gmail.com or dpatel@medallia.com
Twitter: DenishPatel
49
} Stay in touch with Postgres users around the
world
} 300+ members!
} Join today : https://postgres-
slack.herokuapp.com/
52

More Related Content

What's hot (19)

ODP
Logical replication with pglogical
Umair Shahid
 
PDF
Ilya Kosmodemiansky - An ultimate guide to upgrading your PostgreSQL installa...
PostgreSQL-Consulting
 
PDF
Background Tasks in Node - Evan Tahler, TaskRabbit
Redis Labs
 
PDF
Pgbr 2013 postgres on aws
Emanuel Calvo
 
PDF
Deep dive into PostgreSQL statistics.
Alexey Lesovsky
 
PDF
Как PostgreSQL работает с диском
PostgreSQL-Consulting
 
PDF
Feed Burner Scalability
didip
 
PDF
pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)
Wei Shan Ang
 
PDF
Troubleshooting PostgreSQL Streaming Replication
Alexey Lesovsky
 
PPTX
Amazon RDS for PostgreSQL - PGConf 2016
Grant McAlister
 
PDF
PostgreSQL Write-Ahead Log (Heikki Linnakangas)
Ontico
 
PDF
What is new in PostgreSQL 14?
Mydbops
 
PDF
High Availability PostgreSQL with Zalando Patroni
Zalando Technology
 
ODP
Shootout at the PAAS Corral
PostgreSQL Experts, Inc.
 
PDF
PGConf.ASIA 2019 Bali - Building PostgreSQL as a Service with Kubernetes - Ta...
Equnix Business Solutions
 
PDF
PostgreSQL Extensions: A deeper look
Jignesh Shah
 
PDF
GOTO 2013: Why Zalando trusts in PostgreSQL
Henning Jacobs
 
PDF
collectd & PostgreSQL
Mark Wong
 
PDF
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Redis Labs
 
Logical replication with pglogical
Umair Shahid
 
Ilya Kosmodemiansky - An ultimate guide to upgrading your PostgreSQL installa...
PostgreSQL-Consulting
 
Background Tasks in Node - Evan Tahler, TaskRabbit
Redis Labs
 
Pgbr 2013 postgres on aws
Emanuel Calvo
 
Deep dive into PostgreSQL statistics.
Alexey Lesovsky
 
Как PostgreSQL работает с диском
PostgreSQL-Consulting
 
Feed Burner Scalability
didip
 
pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)
Wei Shan Ang
 
Troubleshooting PostgreSQL Streaming Replication
Alexey Lesovsky
 
Amazon RDS for PostgreSQL - PGConf 2016
Grant McAlister
 
PostgreSQL Write-Ahead Log (Heikki Linnakangas)
Ontico
 
What is new in PostgreSQL 14?
Mydbops
 
High Availability PostgreSQL with Zalando Patroni
Zalando Technology
 
Shootout at the PAAS Corral
PostgreSQL Experts, Inc.
 
PGConf.ASIA 2019 Bali - Building PostgreSQL as a Service with Kubernetes - Ta...
Equnix Business Solutions
 
PostgreSQL Extensions: A deeper look
Jignesh Shah
 
GOTO 2013: Why Zalando trusts in PostgreSQL
Henning Jacobs
 
collectd & PostgreSQL
Mark Wong
 
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Redis Labs
 

Viewers also liked (20)

PDF
Keith Fiske - When PostgreSQL Can't, You Can @ Postgres Open
PostgresOpen
 
PDF
Kevin Kempter - PostgreSQL Backup and Recovery Methods @ Postgres Open
PostgresOpen
 
PPTX
David Keeney - SQL Database Server Requests from the Browser @ Postgres Open
PostgresOpen
 
PDF
Gurjeet Singh - How Postgres is Different From (Better Tha) Your RDBMS @ Post...
PostgresOpen
 
PDF
Ryan Jarvinen Open Shift Talk @ Postgres Open 2013
PostgresOpen
 
PDF
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
PostgresOpen
 
PDF
Keith Paskett - Postgres on ZFS @ Postgres Open
PostgresOpen
 
PDF
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
PostgresOpen
 
PDF
Henrietta Dombrovskaya - A New Approach to Resolve Object-Relational Impedanc...
PostgresOpen
 
PDF
Islamabad PUG - 7th Meetup - performance tuning
Umair Shahid
 
PDF
Islamabad PUG - 7th meetup - performance tuning
Umair Shahid
 
PDF
Robert Haas Query Planning Gone Wrong Presentation @ Postgres Open
PostgresOpen
 
PDF
Steve Singer - Managing PostgreSQL with Puppet @ Postgres Open
PostgresOpen
 
PDF
Michael Bayer Introduction to SQLAlchemy @ Postgres Open
PostgresOpen
 
PDF
PoPostgreSQL Web Projects: From Start to FinishStart To Finish
elliando dias
 
PDF
Koichi Suzuki - Postgres-XC Dynamic Cluster Management @ Postgres Open
PostgresOpen
 
PDF
Gbroccolo pgconfeu2016 pgnfs
Giuseppe Broccolo
 
PDF
PostgreSQL HA
haroonm
 
PDF
Michael Paquier - Taking advantage of custom bgworkers @ Postgres Open
PostgresOpen
 
PDF
Geometria Projetiva
elliando dias
 
Keith Fiske - When PostgreSQL Can't, You Can @ Postgres Open
PostgresOpen
 
Kevin Kempter - PostgreSQL Backup and Recovery Methods @ Postgres Open
PostgresOpen
 
David Keeney - SQL Database Server Requests from the Browser @ Postgres Open
PostgresOpen
 
Gurjeet Singh - How Postgres is Different From (Better Tha) Your RDBMS @ Post...
PostgresOpen
 
Ryan Jarvinen Open Shift Talk @ Postgres Open 2013
PostgresOpen
 
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
PostgresOpen
 
Keith Paskett - Postgres on ZFS @ Postgres Open
PostgresOpen
 
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
PostgresOpen
 
Henrietta Dombrovskaya - A New Approach to Resolve Object-Relational Impedanc...
PostgresOpen
 
Islamabad PUG - 7th Meetup - performance tuning
Umair Shahid
 
Islamabad PUG - 7th meetup - performance tuning
Umair Shahid
 
Robert Haas Query Planning Gone Wrong Presentation @ Postgres Open
PostgresOpen
 
Steve Singer - Managing PostgreSQL with Puppet @ Postgres Open
PostgresOpen
 
Michael Bayer Introduction to SQLAlchemy @ Postgres Open
PostgresOpen
 
PoPostgreSQL Web Projects: From Start to FinishStart To Finish
elliando dias
 
Koichi Suzuki - Postgres-XC Dynamic Cluster Management @ Postgres Open
PostgresOpen
 
Gbroccolo pgconfeu2016 pgnfs
Giuseppe Broccolo
 
PostgreSQL HA
haroonm
 
Michael Paquier - Taking advantage of custom bgworkers @ Postgres Open
PostgresOpen
 
Geometria Projetiva
elliando dias
 
Ad

Similar to Out of the box replication in postgres 9.4(pg confus) (20)

PDF
Out of the Box Replication in Postgres 9.4(PgCon)
Denish Patel
 
PDF
Out of the Box Replication in Postgres 9.4(PgCon)
Denish Patel
 
PDF
Streaming replication in practice
Alexey Lesovsky
 
PDF
PostgreSQL Streaming Replication Cheatsheet
Alexey Lesovsky
 
PPTX
Streaming Replication Made Easy in v9.3
Sameer Kumar
 
PDF
How to Replicate PostgreSQL Database
SangJin Kang
 
PDF
ProstgreSQLFailoverConfiguration
Suyog Shirgaonkar
 
PPTX
Built in physical and logical replication in postgresql-Firat Gulec
FIRAT GULEC
 
PPTX
Built-in-Physical-and-Logical-Replication-in-Postgresql-Firat-Gulec.pptx
nadirpervez2
 
PDF
PGDay.Amsterdam 2018 - Stefan Fercot - Save your data with pgBackRest
PGDay.Amsterdam
 
ODP
PostgreSQL Replication in 10 Minutes - SCALE
PostgreSQL Experts, Inc.
 
PDF
Replication in PostgreSQL tutorial given in Postgres Conference 2019
Abbas Butt
 
PPT
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
Command Prompt., Inc
 
PDF
PostgreSQL Replication Tutorial
Hans-Jürgen Schönig
 
PDF
The Magic of Hot Streaming Replication, Bruce Momjian
Fuenteovejuna
 
PDF
Postgres Vienna DB Meetup 2014
Michael Renner
 
PPTX
PostgreSQL Hangout Replication Features v9.4
Ashnikbiz
 
PDF
Hosted PostgreSQL
Mike Fowler
 
PDF
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
EDB
 
PDF
Postgresql 12 streaming replication hol
Vijay Kumar N
 
Out of the Box Replication in Postgres 9.4(PgCon)
Denish Patel
 
Out of the Box Replication in Postgres 9.4(PgCon)
Denish Patel
 
Streaming replication in practice
Alexey Lesovsky
 
PostgreSQL Streaming Replication Cheatsheet
Alexey Lesovsky
 
Streaming Replication Made Easy in v9.3
Sameer Kumar
 
How to Replicate PostgreSQL Database
SangJin Kang
 
ProstgreSQLFailoverConfiguration
Suyog Shirgaonkar
 
Built in physical and logical replication in postgresql-Firat Gulec
FIRAT GULEC
 
Built-in-Physical-and-Logical-Replication-in-Postgresql-Firat-Gulec.pptx
nadirpervez2
 
PGDay.Amsterdam 2018 - Stefan Fercot - Save your data with pgBackRest
PGDay.Amsterdam
 
PostgreSQL Replication in 10 Minutes - SCALE
PostgreSQL Experts, Inc.
 
Replication in PostgreSQL tutorial given in Postgres Conference 2019
Abbas Butt
 
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
Command Prompt., Inc
 
PostgreSQL Replication Tutorial
Hans-Jürgen Schönig
 
The Magic of Hot Streaming Replication, Bruce Momjian
Fuenteovejuna
 
Postgres Vienna DB Meetup 2014
Michael Renner
 
PostgreSQL Hangout Replication Features v9.4
Ashnikbiz
 
Hosted PostgreSQL
Mike Fowler
 
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
EDB
 
Postgresql 12 streaming replication hol
Vijay Kumar N
 
Ad

More from Denish Patel (12)

PDF
Out of the Box Replication in Postgres 9.4(PgConfUS)
Denish Patel
 
PDF
Postgres in Amazon RDS
Denish Patel
 
PDF
Choosing the "D" , Lightning talk
Denish Patel
 
PDF
Scaling postgres
Denish Patel
 
PDF
Two Elephants Inthe Room
Denish Patel
 
PPTX
Deploying Maximum HA Architecture With PostgreSQL
Denish Patel
 
PPT
Deploying Maximum HA Architecture With PostgreSQL
Denish Patel
 
PDF
P90 X Your Database!!
Denish Patel
 
PDF
Achieving Pci Compliace
Denish Patel
 
PDF
Using SQL Standards? Database SQL comparition
Denish Patel
 
PDF
Oracle10g New Features I
Denish Patel
 
PDF
Yet Another Replication Tool: RubyRep
Denish Patel
 
Out of the Box Replication in Postgres 9.4(PgConfUS)
Denish Patel
 
Postgres in Amazon RDS
Denish Patel
 
Choosing the "D" , Lightning talk
Denish Patel
 
Scaling postgres
Denish Patel
 
Two Elephants Inthe Room
Denish Patel
 
Deploying Maximum HA Architecture With PostgreSQL
Denish Patel
 
Deploying Maximum HA Architecture With PostgreSQL
Denish Patel
 
P90 X Your Database!!
Denish Patel
 
Achieving Pci Compliace
Denish Patel
 
Using SQL Standards? Database SQL comparition
Denish Patel
 
Oracle10g New Features I
Denish Patel
 
Yet Another Replication Tool: RubyRep
Denish Patel
 

Recently uploaded (20)

PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 

Out of the box replication in postgres 9.4(pg confus)

  • 2. } 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
  • 3. § What is WAL? § Postgres Replication History § How you setup replication now? § What’s missing ? § Why replication slots? § Demo I’m NOT going to discuss … § SLONY or other 3rd party replication tools § 3rd party replication management tools 2
  • 4. } 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
  • 5. } 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
  • 6. select * from pg_settings where name='wal_level'; name | wal_level setting | hot_standby unit | category | Write-Ahead Log / Settings short_desc | Set the level of information written to the WAL. extra_desc | context | postmaster vartype | enum source | configuration file min_val | max_val | enumvals | { minimal, archive, hot_standby, logical } boot_val | minimal reset_val | hot_standby sourcefile | /var/lib/pgsql/9.4/data/postgresql.auto.conf sourceline | 4 5
  • 7. Almost everything is replicated, but... § unlogged tables (As name suggests) § temporary tables § hash indexes? (generally don’t use?) 6
  • 8. Postgres 7.0: WAL Postgres 8.0: PITR (Point-In-Time-Recovery) Postgres 8.2: pg_standby Postgres 9.0: Hot_standby, Streaming replication Postgres 9.1: pg_basebackup, Synchronous replication Postgres 9.2: Cascading Replication Postgres 9.3: Standby can switch timeline to follow new master Postgres 9.4: Replication Slots , Logical decoding 7
  • 11. #TYPE DATABASE USER ADDRESS METHOD host replication replication 10.0.0.1/32 md5 10
  • 13. CREATE ROLE replication WITH LOGIN REPLICATION; password replication 12
  • 14. § Take file system level backups § What tools you are using for backups? 13
  • 15. primary_conninfo = 'host=primaryhost user=replication password=replication' standby_mode = on 14
  • 17. Have you configured archiving? 16
  • 19. § postgresql.conf archive_mode = on archive_command = 'cp %p /some/where/%f' 18
  • 20. § recovery.conf restore_command = 'cp /some/where/%f %p' 19
  • 21. § local or remote copy § Scp § Rsync § NFS § pg_archivecleanup 20
  • 22. 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
  • 23. § OmniPITR § WAL-E § Repmgr § Pgbarman § Skytools § A lot of Custom scripts 22
  • 24. § fsync capabilities § cp: no § dd: GNU coreutils § SSH: OpenSSH 6.5 sftp-server (Jan 2014) § rsync: patch or wrapper § NFS: Supported 23
  • 25. 24
  • 28. select * from pg_replication_slots ; slot_name |plugin |slot_type |datoid|database|active|xmin|catalog_xmin|restart_lsn ------------+--------+-----------+--------+---- ------+--------+------+--------------+--- standby1 | | physical | | | t | | |0/21000058 standby2 | | physical | | | t | | |0/21000058 standby3 | | physical | | | t | | |0/21000058 27
  • 30. § Keep necessary WAL files § Each standby can have different WAL apply status § Single access control setup § fsync on receiving side 29
  • 31. pg_basebackup -h primaryhost -U replication -D $PGDATA -X stream –P –v -R 30
  • 32. § Meet pg_receivexlog § (Available since Postgers 9.1!) pg_receivexlog -D archivedir --slot archivingslot -h primaryhost -U replication § -- synchronous option in 9.5 31
  • 33. Are you ready to setup replication without any external tools? 1. pg_basebackup 2. streaming with Replication Slots 3. pg_receivexlog 32
  • 34. § 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
  • 35. § Remove Postgres 8.4 version sudo yum erase postgresql.* § Setup yum repo for your desired version sudo 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 modules sudo yum install postgresql94-server postgresql94-contrib § Create postgres cluster & initial automatic startup sudo service postgresql-9.4 initdb sudo chkconfig postgresql-9.4 on sudo service postgresql-9.4 start 34
  • 36. § Become postgres system user sudo su - postgres § Log into database using psql (? to see all available commands) § Create a role & database for yourself CREATE ROLE pgtraining WITH LOGIN SUPERUSER; CREATE DATABASE pgtraining; § You can login as pgtraining user (psql –U pgtraining –d pgtraining) § Create replication role for later CREATE ROLE replication WITH LOGIN REPLICATION; § Set password (”replication”) password replication 35
  • 37. § http://www.postgresql.org/docs/9.4/static/auth- pg-hba-conf.html § Find location of hba_file postgres=# 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
  • 38. 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
  • 39. § Restart database sudo service postgresql-9.4 restart § Verify settings psql=# show max_wal_senders; 38
  • 40. SELECT * FROM pg_create_physical_replication_slot('standby1'); 39
  • 41. sudo su - postgres pg_basebackup -h 127.0.0.1 -U replication -D /var/lib/pgsql/9.4/slave -R -Xs -P -v 40
  • 42. cd /var/lib/pgsql/9.4/slave § Edit Standby postgresql.conf file port = 5433 hot_standby = on § Edit Standby recovery.conf file standby_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
  • 43. § Copy existing init file sudo 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/slave PGLOG=/var/lib/pgsql/9.4/pgstartup-5433.log PGUPLOG=/var/lib/pgsql/$PGMAJORVERSION/pgupgrade- 5433.log § Register service, start up slave sudo chkconfig postgresql-9.4-5433 on sudo service postgresql-9.4-5433 start 42
  • 44. pgtraining=# select * from pg_stat_replication; -[ RECORD 1 ]----+------------------------------ pid | 3260 usesysid | 24576 usename | replication application_name | walreceiver client_addr | 127.0.0.1 client_hostname | client_port | 53206 backend_start | 2015-06-08 14:47:50.057326-04 backend_xmin | state | streaming sent_location | 0/240000B8 write_location | 0/240000B8 flush_location | 0/240000B8 replay_location | 0/240000B8 sync_priority | 0 sync_state | async 43
  • 45. pgtraining=# select * from pg_replication_slots; -[ RECORD 1 ]+----------- slot_name | standby1 plugin | slot_type | physical datoid | database | active | t xmin | catalog_xmin | restart_lsn | 0/270000EC 44
  • 46. § 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
  • 47. § Monitor Disk space § Monitor slave lag select pg_xlog_location_diff(sent_location, write_location) AS byte_lag from pg_stat_replication where application_name='pg_receivexlog'; § Monitor WAL archive process select pg_xlog_location_diff(sent_location, write_location) AS byte_lag from pg_stat_replication where application_name='walreceiver'; 46
  • 48. } 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
  • 49. § Conference committee § Peter Eisentraut § You!! 48
  • 52. } Stay in touch with Postgres users around the world } 300+ members! } Join today : https://postgres- slack.herokuapp.com/ 52