Fix incorrect execution of failover process
authorTakuma Hoshiai <hoshiai@sraoss.co.jp>
Tue, 2 Jun 2020 08:08:00 +0000 (17:08 +0900)
committerTakuma Hoshiai <hoshiai@sraoss.co.jp>
Tue, 2 Jun 2020 08:08:00 +0000 (17:08 +0900)
This problem happen by executing pg_terminate_backend command with native replication.
In addition, add regression test for pg_terminate_backend command.

src/protocol/pool_process_query.c
src/test/regression/tests/073.pg_terminate_backend/test.sh [new file with mode: 0755]

index 5916de23c7ec498e8649bc93ef711f8222103eed..66d0e1153139468626d361cd964d130d4c8dfaca 100644 (file)
@@ -4758,8 +4758,17 @@ pool_config->client_idle_limit)));
                                                was_error = 1;
                                                if (!VALID_BACKEND(i))
                                                        break;
-                                               notice_backend_error(i, REQ_DETAIL_SWITCHOVER);
-                                               sleep(5);
+                                               if (CONNECTION(backend, i)->con_info->swallow_termination == 1)
+                                               {
+                                                       ereport(FATAL,
+                                                                       (errmsg("connection to postmaster on DB node %d was lost due to pg_terminate_backend", i),
+                                                                        errdetail("pg_terminate_backend was called on the backend")));
+                                               }
+                                               else
+                                               {
+                                                       notice_backend_error(i, REQ_DETAIL_SWITCHOVER);
+                                                       sleep(5);
+                                               }
                                        }
 
                                        /*
diff --git a/src/test/regression/tests/073.pg_terminate_backend/test.sh b/src/test/regression/tests/073.pg_terminate_backend/test.sh
new file mode 100755 (executable)
index 0000000..c7eafcf
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/env bash
+#-------------------------------------------------------------------
+# test script for pg_terminate_backend command.
+
+WHOAMI=`whoami`
+source $TESTLIBS
+TESTDIR=testdir
+PSQL=$PGBIN/psql
+
+for mode in s r n
+do
+       rm -fr $TESTDIR
+       mkdir $TESTDIR
+       cd $TESTDIR
+
+       # create test environment
+       echo -n "creating test environment..."
+       $PGPOOL_SETUP -m $mode -n 2 || exit 1
+       echo "done."
+
+       source ./bashrc.ports
+
+       export PGPORT=$PGPOOL_PORT
+
+
+       # start pgpool-II
+       ./startall
+
+       sleep 1
+
+       $PSQL test -p $PGPORT -c "SELECT pg_sleep(20);" &
+
+       sleep 2
+       # get process id which query is executed
+       PID=`ps -ef |grep "postgres:" |grep SELECT | awk 'NR==1 {print $2}'`
+
+       $PSQL test -p $PGPORT -c "SELECT pg_terminate_backend($PID)"
+
+       grep "found the pg_terminate_backend request" log/pgpool.log
+       if [ $? -ne 0 ];then
+               echo "pgpool cannot recognize a node which is executing pg_terminate_backend command."
+               ./shutdownall
+               exit 1
+       fi
+
+       sleep 5
+
+       grep "failover" log/pgpool.log
+       if [ $? -eq 0 ];then
+               echo "Failed pg_terminate_backend. failover is executing."
+               ./shutdownall
+               exit 1
+       fi
+
+       ./shutdownall
+
+       cd ..
+done
+
+exit 0