From: Tatsuo Ishii Date: Wed, 9 May 2018 08:39:06 +0000 (+0900) Subject: Add -r option to pgpool_setup to allow use of pg_rewind. X-Git-Tag: V4_0_0_ALPHA1~85 X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=719fbaa76324b1c11932f598763555c60bb0c527;p=pgpool2.git Add -r option to pgpool_setup to allow use of pg_rewind. With this option, pgpool_setup creates basebackup.sh which tries pg_rewind first. If it fails, falls back to rsync. --- diff --git a/doc.ja/src/sgml/ref/pgpool_setup.sgml b/doc.ja/src/sgml/ref/pgpool_setup.sgml index fe6185266..458e19e3f 100644 --- a/doc.ja/src/sgml/ref/pgpool_setup.sgml +++ b/doc.ja/src/sgml/ref/pgpool_setup.sgml @@ -224,6 +224,24 @@ Pgpool-II documentation + + + + + +リカバリスクリプト(basebackup.sh)中でpg_rewindを使います。 +このコマンドが失敗すると、通常のrsyncを使います。 +pg_rewindは、データベースクラスタのすべてをコピーしないので、rsyncに比べてずっと高速になることがあります。 + + + + @@ -337,8 +355,22 @@ Pgpool-II documentation If "true", in streaming replication mode, use replication slot instead of archive. This brings the same effect as "-s" option is specified. --> -ストリーミングレプリケーション環境に置いて、この環境変数が"true"に指定されると、アーカイブログの代わりにレプリケーションスロットを使います。 -これは、"-s"を使ったのと同じ効果をもたらします。 +ストリーミングレプリケーション環境において、この環境変数が"true"に指定されると、アーカイブログの代わりにレプリケーションスロットを使います。 +これは、"-s"オプションを使ったのと同じ効果をもたらします。 + + + + + + + + + +ストリーミングレプリケーション環境において、この環境変数が"true"に指定されると、basebackup.sh中でpg_rewindを使います。 +これは、"-r"オプションを使ったのと同じ効果をもたらします。 diff --git a/doc/src/sgml/ref/pgpool_setup.sgml b/doc/src/sgml/ref/pgpool_setup.sgml index 29b26b456..1c30efa9d 100644 --- a/doc/src/sgml/ref/pgpool_setup.sgml +++ b/doc/src/sgml/ref/pgpool_setup.sgml @@ -154,6 +154,19 @@ Pgpool-II documentation + + + + + Use pg_rewind command in recovery script + (basebackup.sh). If the command fails, switch to use ordinal + rsync command. In certain cases recovery + using pg_rewind is much faster than rsync + since it does not copy whole database cluster. + + + + @@ -237,6 +250,16 @@ Pgpool-II documentation + + + + + If "true", in streaming replication mode, use pg_rewind in basebackup.sh script. + This brings the same effect as "-r" option is specified. + + + + diff --git a/src/test/pgpool_setup b/src/test/pgpool_setup index 2dba4c1ce..687d794d5 100755 --- a/src/test/pgpool_setup +++ b/src/test/pgpool_setup @@ -17,7 +17,7 @@ # directory for *testing* purpose. # Do not use this tool for production environment! # -# usage: pgpool_setup [-m r|s|n|l][-n num_clusters][-p base_port][-pg pg_base_port][--no-stop][-d][-s] +# usage: pgpool_setup [-m r|s|n|l][-n num_clusters][-p base_port][-pg pg_base_port][--no-stop][-d][-s][-r] # -m s: create an installation as streaming replication mode. # (the default) # -m r: create an installation as native replication mode. @@ -34,7 +34,8 @@ # pg_base_port + 1 and so on. # --no-stop: do not stop pgpool and PostgreSQL after the work # -d: start pgpool with debug mode -# -s use replication slot, rather than wal archive. +# -s: use replication slot, rather than wal archive. +# -r: use pg_rewind for base backup if possible. # # The user run this script will become the PostgreSQL super user as # well. Current directory must be empty. Assume that appropreate @@ -77,6 +78,8 @@ PGSOCKET_DIR=${PGSOCKET_DIR:-"/tmp"} INITDBARG="--no-locale -E UTF_8 --data-checksums" # Use replication slot USE_REPLICATION_SLOT=${USE_REPLICATION_SLOT:-"false"} +# Use pg_rewind +USE_PG_REWIND=${USE_PG_REWIND:-"false"} #------------------------------------------- # End of configuration section @@ -324,6 +327,7 @@ SCRIPT=basebackup.sh cat >> $1/$SCRIPT <<'EOF' #! /bin/sh psql=__PGBIN__/psql +pg_rewind=__PGBIN__/pg_rewind DATADIR_BASE=__DATADIR_BASE__ PGSUPERUSER=__PGSUPERUSER__ @@ -333,11 +337,39 @@ DEST_CLUSTER=$3 PORT=$4 recovery_node=$5 +pg_rewind_failed="true" + log=$DATADIR_BASE/log/recovery.log +echo >> $log +date >> $log +EOF + +pg_rewind_failed="true" + +if [ $USE_PG_REWIND = "true" ];then +cat >> $1/$SCRIPT <<'EOF' + +# First try pg_rewind + +# Make backup copy of postgresql.conf since pg_rewind blindly copies +# $master_db_cluster/postgresql.conf. +cp $DEST_CLUSTER/postgresql.conf /tmp/ +echo "pg_rewind starts" >> $log +$pg_rewind -P -D $DEST_CLUSTER --source-server="port=$PORT user=$PGSUPERUSER dbname=postgres" >> $log 2>&1 +if [ $? != 0 ];then + # pg_rewind failed. Fallback to rsync. + echo "pg_rewind failed. Fall back to rsync" >> $log + pg_rewind_failed="true" +fi +EOF +fi + +cat >> $1/$SCRIPT <<'EOF' +if [ $pg_rewind_failed = "true" ];then $psql -p $PORT -c "SELECT pg_start_backup('Streaming Replication', true)" postgres -echo "source: $master_db_cluster dest: $DEST_CLUSTER" > $log +echo "source: $master_db_cluster dest: $DEST_CLUSTER" >> $log rsync -C -a -c --delete --exclude postgresql.conf --exclude postmaster.pid \ --exclude postmaster.opts --exclude pg_log \ @@ -349,6 +381,7 @@ rm -fr $DEST_CLUSTER/pg_xlog mkdir $DEST_CLUSTER/pg_xlog chmod 700 $DEST_CLUSTER/pg_xlog rm $DEST_CLUSTER/recovery.done +fi EOF if [ $USE_SLOT = "true" ];then @@ -377,7 +410,13 @@ EOF fi cat >> $1/$SCRIPT <<'EOF' +if [ $pg_rewind_failed = "true" ];then $psql -p $PORT -c "SELECT pg_stop_backup()" postgres +fi + +if [ $pg_rewind_failed = "false" ];then + cp /tmp/postgresql.conf $DEST_CLUSTER/ +fi EOF #------------------------------------------- @@ -597,18 +636,17 @@ function wait_for_pgpool_reload { ################################################################################ function usage() { - echo "usage: $0 [-m r|s|n|l|y] [-n num_clusters] [-p base_port] [-pg pg_base_port][--no-stop] [-d] [-s]";exit 1 + echo "usage: $0 [-m r|s|n|l|y] [-n num_clusters] [-p base_port] [-pg pg_base_port][--no-stop] [-d] [-s] [-r]";exit 1 } #------------------------------------------- # Argument check -# usage: $0 [-m r|s|n][-n num_clusters][-p base_port][-pg pg_base_port][--no-stop][-d] +# usage: $0 [-m r|s|n][-n num_clusters][-p base_port][-pg pg_base_port][--no-stop][-d][-s][-r] #------------------------------------------- # # default mode is streaming replication mode MODE="s" NO_STOP="false" -USE_SLOT="false" while [ $# -gt 0 ] do @@ -636,9 +674,10 @@ do NO_STOP="true" elif [ $1 = "-d" ];then PGPOOLDEBUG="true" - shift; elif [ $1 = "-s" ];then - USE_SLOT="true" + USE_SLOT="true" + elif [ $1 = "-r" ];then + USE_PG_REWIND="true" elif [ $1 = "--help" -o $1 = "-o" ];then usage exit @@ -671,6 +710,12 @@ esac if [ a$USE_REPLICATION_SLOT = a"true" ];then USE_SLOT="true" fi + +# If USE_PG_REWIND is provided as an environment variable, turn on -r +if [ a$USE_PG_REWIND = a"true" ];then + USE_PG_REWIND="true" +fi + #------------------------------------------- # Make sure that current directory is empty #-------------------------------------------