Difference between revisions of "System/PostgreSQL"

From LunaSys
Jump to navigation Jump to search
Line 7: Line 7:
 
From [http://wiki.postgresql.org/wiki/Streaming_Replication]
 
From [http://wiki.postgresql.org/wiki/Streaming_Replication]
  
Install postgres in the primary and standby server as usual. This requires only configure, make and make install.
+
1. Set up connections and authentication so that the standby server can successfully connect to the replication pseudo-database on the primary.
2. Create the initial database cluster in the primary server as usual, using initdb.
 
3. Set up connections and authentication so that the standby server can successfully connect to the replication pseudo-database on the primary.
 
$ $EDITOR postgresql.conf
 
  
listen_addresses = '192.168.0.10'
+
$ $EDITOR postgresql.conf
 +
listen_addresses = '192.168.0.10'
 +
$ $EDITOR pg_hba.conf
 +
# The standby server must have superuser access privileges.
 +
host  replication  postgres  192.168.0.20/22  trust
  
$ $EDITOR pg_hba.conf
+
4. Set up the streaming replication related parameters on the primary server.
  
# The standby server must have superuser access privileges.
+
$ $EDITOR postgresql.conf
host replication postgres 192.168.0.20/22 trust
+
  wal_level = hot_standby
4. Set up the streaming replication related parameters on the primary server.
+
  max_wal_senders = 5
$ $EDITOR postgresql.conf
+
  wal_keep_segments = 32
 +
  archive_mode    = on
 +
archive_command = 'cp %p /path_to/archive/%f'
  
# To enable read-only queries on a standby server, wal_level must be set to
+
5. Start postgres on the primary server.
# "hot_standby". But you can choose "archive" if you never connect to the
 
# server in standby mode.
 
wal_level = hot_standby
 
  
# Set the maximum number of concurrent connections from the standby servers.
+
6. Make a base backup by copying the primary server's data directory to the standby server.
max_wal_senders = 5
 
  
# To prevent the primary server from removing the WAL segments required for
+
$ psql -c "SELECT pg_start_backup('label', true)"
# the standby server before shipping them, set the minimum number of segments
+
$ rsync -a ${PGDATA}/ standby:/srv/pgsql/standby/ --exclude postmaster.pid
# retained in the pg_xlog directory. At least wal_keep_segments should be
+
$ psql -c "SELECT pg_stop_backup()"
# larger than the number of segments generated between the beginning of
 
# online-backup and the startup of streaming replication. If you enable WAL
 
# archiving to an archive directory accessible from the standby, this may
 
# not be necessary.
 
wal_keep_segments = 32
 
  
# Enable WAL archiving on the primary to an archive directory accessible from
 
# the standby. If wal_keep_segments is a high enough number to retain the WAL
 
# segments required for the standby server, this is not necessary.
 
archive_mode    = on
 
archive_command = 'cp %p /path_to/archive/%f'
 
5. Start postgres on the primary server.
 
6. Make a base backup by copying the primary server's data directory to the standby server.
 
$ psql -c "SELECT pg_start_backup('label', true)"
 
$ rsync -a ${PGDATA}/ standby:/srv/pgsql/standby/ --exclude postmaster.pid
 
$ psql -c "SELECT pg_stop_backup()"
 
 
7. Set up replication-related parameters, connections and authentication in the standby server like the primary, so that the standby might work as a primary after failover.
 
7. Set up replication-related parameters, connections and authentication in the standby server like the primary, so that the standby might work as a primary after failover.
 +
 
8. Enable read-only queries on the standby server. But if wal_level is archive on the primary, leave hot_standby unchanged (i.e., off).
 
8. Enable read-only queries on the standby server. But if wal_level is archive on the primary, leave hot_standby unchanged (i.e., off).
$ $EDITOR postgresql.conf
 
  
hot_standby = on
+
$ $EDITOR postgresql.conf
 +
hot_standby = on
 +
 
 
9. Create a recovery command file in the standby server; the following parameters are required for streaming replication.
 
9. Create a recovery command file in the standby server; the following parameters are required for streaming replication.
$ $EDITOR recovery.conf
+
$ $EDITOR recovery.conf
# Note that recovery.conf must be in $PGDATA directory.
+
# Note that recovery.conf must be in $PGDATA directory.
 +
standby_mode          = 'on'
 +
primary_conninfo      = 'host=192.168.0.10 port=5432 user=postgres'
 +
trigger_file = '/path_to/trigger'
 +
restore_command = 'cp /path_to/archive/%f "%p"'
  
# Specifies whether to start the server as a standby. In streaming replication,
+
10. Start postgres in the standby server. It will start streaming replication.
# this parameter must to be set to on.
 
standby_mode          = 'on'
 
  
# Specifies a connection string which is used for the standby server to connect
+
11. You can calculate the replication lag by comparing the current WAL write location on the primary with the last WAL location received/replayed by the standby. They can be retrieved using pg_current_xlog_location on the primary and the pg_last_xlog_receive_location/pg_last_xlog_replay_location on the standby, respectively.
# with the primary.
 
primary_conninfo      = 'host=192.168.0.10 port=5432 user=postgres'
 
 
 
# Specifies a trigger file whose presence should cause streaming replication to
 
# end (i.e., failover).
 
trigger_file = '/path_to/trigger'
 
  
# Specifies a command to load archive segments from the WAL archive. If
+
$ psql -c "SELECT pg_current_xlog_location()" -h192.168.0.10 (primary host)
# wal_keep_segments is a high enough number to retain the WAL segments
 
# required for the standby server, this may not be necessary. But
 
# a large workload can cause segments to be recycled before the standby
 
# is fully synchronized, requiring you to start again from a new base backup.
 
restore_command = 'cp /path_to/archive/%f "%p"'
 
10. Start postgres in the standby server. It will start streaming replication.
 
11. You can calculate the replication lag by comparing the current WAL write location on the primary with the last WAL location received/replayed by the standby. They can be retrieved using pg_current_xlog_location on the primary and the pg_last_xlog_receive_location/pg_last_xlog_replay_location on the standby, respectively.
 
$ psql -c "SELECT pg_current_xlog_location()" -h192.168.0.10 (primary host)
 
 
  pg_current_xlog_location  
 
  pg_current_xlog_location  
--------------------------
+
--------------------------
 
  0/2000000
 
  0/2000000
(1 row)
+
(1 row)
  
$ psql -c "select pg_last_xlog_receive_location()" -h192.168.0.20 (standby host)
+
$ psql -c "select pg_last_xlog_receive_location()" -h192.168.0.20 (standby host)
 
  pg_last_xlog_receive_location  
 
  pg_last_xlog_receive_location  
-------------------------------
+
-------------------------------
 
  0/2000000
 
  0/2000000
(1 row)
+
(1 row)
  
$ psql -c "select pg_last_xlog_replay_location()" -h192.168.0.20 (standby host)
+
$ psql -c "select pg_last_xlog_replay_location()" -h192.168.0.20 (standby host)
 
  pg_last_xlog_replay_location  
 
  pg_last_xlog_replay_location  
------------------------------
+
------------------------------
 
  0/2000000
 
  0/2000000
(1 row)
+
(1 row)
 +
 
 
12. You can also check the progress of streaming replication by using ps command.
 
12. You can also check the progress of streaming replication by using ps command.
# The displayed LSNs indicate the byte position that the standby server has
 
# written up to in the xlogs.
 
[primary] $ ps -ef | grep sender
 
postgres  6879  6831  0 10:31 ?        00:00:00 postgres: wal sender process postgres 127.0.0.1(44663) streaming 0/2000000
 
  
[standby] $ ps -ef | grep receiver
+
The displayed LSNs indicate the byte position that the standby server has written up to in the xlogs.
postgres  6878  6872  1 10:31 ?        00:00:01 postgres: wal receiver process  streaming 0/2000000
+
 
How to do failover
+
[primary] $ ps -ef | grep sender
 +
postgres  6879  6831  0 10:31 ?        00:00:00 postgres: wal sender process postgres 127.0.0.1(44663) streaming 0/2000000
 +
 
 +
[standby] $ ps -ef | grep receiver
 +
postgres  6878  6872  1 10:31 ?        00:00:01 postgres: wal receiver process  streaming 0/2000000
 +
 
 +
 
 +
* How to do failover
 +
 
 
Create the trigger file in the standby after the primary fails.
 
Create the trigger file in the standby after the primary fails.
How to stop the primary or the standby server
+
 
 +
* How to stop the primary or the standby server
 +
 
 
Shut down it as usual (pg_ctl stop).
 
Shut down it as usual (pg_ctl stop).
How to restart streaming replication after failover
+
 
Repeat the operations from 6th; making a fresh backup, some configurations and starting the original primary as the standby. The primary server doesn't need to be stopped during these operations.
+
* How to restart streaming replication after failover
How to restart streaming replication after the standby fails
+
 
 +
Repeat the operations from 6th; making a fresh backup, some configurations and starting the original primary as the  
 +
standby. The primary server doesn't need to be stopped during these operations.
 +
 
 +
* How to restart streaming replication after the standby fails
 +
 
 
Restart postgres in the standby server after eliminating the cause of failure.
 
Restart postgres in the standby server after eliminating the cause of failure.
How to disconnect the standby from the primary
+
 
 +
* How to disconnect the standby from the primary
 +
 
 
Create the trigger file in the standby while the primary is running. Then the standby would be brought up.
 
Create the trigger file in the standby while the primary is running. Then the standby would be brought up.
How to re-synchronize the stand-alone standby after isolation
+
 
 +
* How to re-synchronize the stand-alone standby after isolation
 +
 
 
Shut down the standby as usual. And repeat the operations from 6th.
 
Shut down the standby as usual. And repeat the operations from 6th.
 +
 
If you have more than one slave, promoting one will break the other(s). Update their recovery.conf settings to point to the new master, set recovery_target_timeline to 'latest', scp/rsync the pg_xlog directory, and restart the slave.
 
If you have more than one slave, promoting one will break the other(s). Update their recovery.conf settings to point to the new master, set recovery_target_timeline to 'latest', scp/rsync the pg_xlog directory, and restart the slave.

Revision as of 00:19, 16 December 2012

Admin

VACUUM FULL ANALYZE

Replication

From [1]

1. Set up connections and authentication so that the standby server can successfully connect to the replication pseudo-database on the primary.

$ $EDITOR postgresql.conf
listen_addresses = '192.168.0.10'
$ $EDITOR pg_hba.conf
# The standby server must have superuser access privileges.
host  replication  postgres  192.168.0.20/22  trust

4. Set up the streaming replication related parameters on the primary server.

$ $EDITOR postgresql.conf
wal_level = hot_standby
max_wal_senders = 5
wal_keep_segments = 32
archive_mode    = on
archive_command = 'cp %p /path_to/archive/%f'

5. Start postgres on the primary server.

6. Make a base backup by copying the primary server's data directory to the standby server.

$ psql -c "SELECT pg_start_backup('label', true)"
$ rsync -a ${PGDATA}/ standby:/srv/pgsql/standby/ --exclude postmaster.pid
$ psql -c "SELECT pg_stop_backup()"

7. Set up replication-related parameters, connections and authentication in the standby server like the primary, so that the standby might work as a primary after failover.

8. Enable read-only queries on the standby server. But if wal_level is archive on the primary, leave hot_standby unchanged (i.e., off).

$ $EDITOR postgresql.conf
hot_standby = on

9. Create a recovery command file in the standby server; the following parameters are required for streaming replication.

$ $EDITOR recovery.conf
# Note that recovery.conf must be in $PGDATA directory.
standby_mode          = 'on'
primary_conninfo      = 'host=192.168.0.10 port=5432 user=postgres'
trigger_file = '/path_to/trigger'
restore_command = 'cp /path_to/archive/%f "%p"'

10. Start postgres in the standby server. It will start streaming replication.

11. You can calculate the replication lag by comparing the current WAL write location on the primary with the last WAL location received/replayed by the standby. They can be retrieved using pg_current_xlog_location on the primary and the pg_last_xlog_receive_location/pg_last_xlog_replay_location on the standby, respectively.

$ psql -c "SELECT pg_current_xlog_location()" -h192.168.0.10 (primary host)
pg_current_xlog_location 
--------------------------
0/2000000
(1 row)
$ psql -c "select pg_last_xlog_receive_location()" -h192.168.0.20 (standby host)
pg_last_xlog_receive_location 
-------------------------------
0/2000000
(1 row)
$ psql -c "select pg_last_xlog_replay_location()" -h192.168.0.20 (standby host)
pg_last_xlog_replay_location 
------------------------------
0/2000000
(1 row)

12. You can also check the progress of streaming replication by using ps command.

The displayed LSNs indicate the byte position that the standby server has written up to in the xlogs.

[primary] $ ps -ef | grep sender
postgres  6879  6831  0 10:31 ?        00:00:00 postgres: wal sender process postgres 127.0.0.1(44663) streaming 0/2000000
[standby] $ ps -ef | grep receiver
postgres  6878  6872  1 10:31 ?        00:00:01 postgres: wal receiver process   streaming 0/2000000


* How to do failover

Create the trigger file in the standby after the primary fails.

* How to stop the primary or the standby server

Shut down it as usual (pg_ctl stop).

* How to restart streaming replication after failover

Repeat the operations from 6th; making a fresh backup, some configurations and starting the original primary as the standby. The primary server doesn't need to be stopped during these operations.

* How to restart streaming replication after the standby fails

Restart postgres in the standby server after eliminating the cause of failure.

* How to disconnect the standby from the primary

Create the trigger file in the standby while the primary is running. Then the standby would be brought up.

* How to re-synchronize the stand-alone standby after isolation

Shut down the standby as usual. And repeat the operations from 6th.

If you have more than one slave, promoting one will break the other(s). Update their recovery.conf settings to point to the new master, set recovery_target_timeline to 'latest', scp/rsync the pg_xlog directory, and restart the slave.