Add -r option to pgpool_setup to allow use of pg_rewind.
authorTatsuo Ishii <ishii@postgresql.org>
Wed, 9 May 2018 08:39:06 +0000 (17:39 +0900)
committerTatsuo Ishii <ishii@postgresql.org>
Wed, 9 May 2018 08:39:06 +0000 (17:39 +0900)
With this option, pgpool_setup creates basebackup.sh which tries
pg_rewind first.  If it fails, falls back to rsync.

doc.ja/src/sgml/ref/pgpool_setup.sgml
doc/src/sgml/ref/pgpool_setup.sgml
src/test/pgpool_setup

index fe618526602b9616d2abb2811307fe8e0b4ab826..458e19e3f688de701f32813e55701652297bce0d 100644 (file)
@@ -224,6 +224,24 @@ Pgpool-II documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>-r</option></term>
+      <listitem>
+       <para>
+<!--
+        Use <command>pg_rewind</command> command in recovery script
+        (basebackup.sh).  If the command fails, switch to use ordinal
+        rsync command.  In certain cases recovery
+        using <command>pg_rewind</command> is much faster than rsync
+        since it does not copy whole database cluster.
+-->
+リカバリスクリプト(basebackup.sh)中で<command>pg_rewind</command>を使います。
+このコマンドが失敗すると、通常のrsyncを使います。
+<command>pg_rewind</command>は、データベースクラスタのすべてをコピーしないので、rsyncに比べてずっと高速になることがあります。
+       </para>
+      </listitem>
+     </varlistentry>
+
     </variablelist>
    </para>
  </refsect1>
@@ -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"オプションを使ったのと同じ効果をもたらします。
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>USE_PG_REWIND</option></term>
+      <listitem>
+       <para>
+<!--
+        If "true", in streaming replication mode, use <command>pg_rewind</command> in basebackup.sh script.
+        This brings the same effect as "-r" option is specified.
+-->
+ストリーミングレプリケーション環境において、この環境変数が"true"に指定されると、basebackup.sh中で<command>pg_rewind</command>を使います。
+これは、"-r"オプションを使ったのと同じ効果をもたらします。
        </para>
       </listitem>
      </varlistentry>
index 29b26b4565c3d1a2e31840d64de774d9401cd4d4..1c30efa9d95d9871ee079046b1f1346e20c4e1f1 100644 (file)
@@ -154,6 +154,19 @@ Pgpool-II documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>-r</option></term>
+      <listitem>
+       <para>
+        Use <command>pg_rewind</command> command in recovery script
+        (basebackup.sh).  If the command fails, switch to use ordinal
+        rsync command.  In certain cases recovery
+        using <command>pg_rewind</command> is much faster than rsync
+        since it does not copy whole database cluster.
+       </para>
+      </listitem>
+     </varlistentry>
+
     </variablelist>
    </para>
  </refsect1>
@@ -237,6 +250,16 @@ Pgpool-II documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>USE_PG_REWIND</option></term>
+      <listitem>
+       <para>
+        If "true", in streaming replication mode, use <command>pg_rewind</command> in basebackup.sh script.
+        This brings the same effect as "-r" option is specified.
+       </para>
+      </listitem>
+     </varlistentry>
+
     </variablelist>
 
    </para>
index 2dba4c1ce8f714a0b7e1b1def59d8fbc938e705d..687d794d5846cd917bf4d241cb48149cd458acd4 100755 (executable)
@@ -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
 #-------------------------------------------