为了测试pg模拟down机,可使用插件pg_kaboom:GitHub - pgguru/pg_kaboom: Devious SQL-based SQL tools to crash your PostgreSQL server
$ git clone git@github.com:CrunchyData/pg_kaboom.git
$ cd pg_kaboom
$ make PG_CONFIG=path/to/pg_config && make install PG_CONFIG=path/to/pg_config
$ psql -c 'CREATE EXTENSION pg_kaboom' -U pg13 -d postgres
开始模拟:
[pg13@hdp1 ~]$ psql -d postgres
psql (13.3, server 13.2)
Type "help" for help.
postgres=# SET pg_kaboom.disclaimer = 'I can afford to lose this data and server';
SET
postgres=# SET pg_kaboom.execute = on;
SET
postgres=# SELECT pg_kaboom('segfault');
ERROR: for safety, pg_kaboom.disclaimer must be explicitly set to 'I can afford to lose this data and server'
#这里需要再次确认
postgres=# SET pg_kaboom.disclaimer = 'I can afford to lose this data and server';
SET
postgres=# SELECT pg_kaboom('segfault');
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
#目前已经down机
!?>
所以我们可以通过这个工具模拟主库down机。来测试主备切换、生成数据库core文件等工作。
除了上述函数pg_kaboom的参数segfault之外,参数还有以下几个:
break-archive
:: install a brokenarchive_command
and force a restart
fill-log
:: allocate all of the space inside the logs directory
fill-pgdata
:: allocate all of the space inside the $PGDATA directory
fill-pgwal
:: allocate all of the space inside the $PGDATA/pg_wal directory
mem
:: allocate some memory
restart
:: do an immediate restart of the server
rm-pgdata
:: do arm -Rf $PGDATA
segfault
:: cause a segfault in the running backend process
signal
:: send aSIGKILL
to the Postmaster process
xact-wrap
:: force the database to run an xact-wraparound vacuumYou can also use the following "special" weapons:
random
:: choose a random weapon
null
:: don't do anything, just go through the normal flow