停止數據庫的命令:
pg_ctl stop -D $PGDATA [-m shutdown-mode]
shutdown-mode有如下幾種模式:
1. smart: 等所有的連接中止后,關閉數據庫。如果客戶端連接不終止, 則無法關閉數據庫。
開啟一個空會話:
[root@localhost ~]# su - postgres [postgres@localhost ~]$ psql psql (9.4.4) Type "help" for help. postgres=#
用smart關閉數據庫:
[postgres@localhost ~]$ pg_ctl stop -D $PGDATA -m smart waiting for server to shut down............................................................... failed pg_ctl: server does not shut down HINT: The "-m fast" option immediately disconnects sessions rather than waiting for session-initiated disconnection
2. fast: 快速關閉數據庫, 斷開客戶端的連接,讓已有的事務回滾,然后正常關閉數據庫。
[postgres@localhost ~]$ pg_ctl stop -D $PGDATA -m fast waiting for server to shut down.... done server stopped
查看關閉日志:
LOG: received fast shutdown request LOG: aborting any active transactions FATAL: terminating connection due to administrator command LOG: shutting down LOG: database system is shut down
會話被強制中斷,然后關閉數據庫。
起一個事務,然后測試關閉:
postgres=# create table t(id int primary key, name varchar(9)); CREATE TABLE postgres=# begin; BEGIN postgres=# insert into t values(1,'a') postgres-# ; INSERT 0 1
不提交, 然后用FAST MODE去關閉數據庫:
[postgres@localhost ~]$ pg_ctl stop -D $PGDATA -m fast waiting for server to shut down.... done server stopped 查看日志: LOG: received fast shutdown request LOG: aborting any active transactions LOG: autovacuum launcher shutting down FATAL: terminating connection due to administrator command LOG: shutting down LOG: database system is shut down
同樣是直接中斷會話, 而不去管事務有沒有提交。
postgres=# select * from t; id | name ----+------ (0 rows)
沒有提交的數據, 在重啟之后並不能查到。
3. immediate: 立即關閉數據庫,立即停止數據庫進程,直接退出,下次啟動時會進行實例恢復。
postgres=# insert into t values(1,'a') ; INSERT 0 1 postgres=# select * from t; id | name ----+------ 1 | a (1 row)
關閉數據庫:
[postgres@localhost ~]$ pg_ctl stop -D $PGDATA -m immediate waiting for server to shut down.... done server stopped 查看日志: LOG: received immediate shutdown request WARNING: terminating connection because of crash of another server process DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory. HINT: In a moment you should be able to reconnect to the database and repeat your command. WARNING: terminating connection because of crash of another server process DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory. HINT: In a moment you should be able to reconnect to the database and repeat your command.
啟動數據庫:
[postgres@localhost ~]$ pg_ctl -D /apps/pgsql/pgdata -l 1.log start server starting 查看日志: LOG: database system was interrupted; last known up at 2017-04-27 18:56:47 PDT LOG: database system was not properly shut down; automatic recovery in progress #提示非正常關機,自動開啟恢復。 LOG: redo starts at 0/181F910 LOG: record with zero length at 0/181FA90 LOG: redo done at 0/181FA60 LOG: last completed transaction was at log time 2017-04-27 18:59:13.727213-07 LOG: MultiXact member wraparound protections are now enabled LOG: autovacuum launcher started LOG: database system is ready to accept connections
查看數據:
[postgres@localhost ~]$ psql psql (9.4.4) Type "help" for help. postgres=# select * from t; id | name ----+------ 1 | a (1 row)
提交的數據已通過實例恢復。
小結:
對比以上三種關庫模式:
smart最為安全,但最慢, 需要將所有連接都斷開后,才會關庫,默認關庫模式。
fast強制中斷會話,而不管有操作有沒有提交,在做系統維護(系統維護時一般應用都正常關閉了,或者不再會有事務操作。)時,需要這種模式來關閉數據庫。
immediate最暴力的方式,不管數據有沒有落盤(POSGRE是遵循WAL機制),就直接關掉, 待啟動時進行實例恢復, 如果在關庫前有大量的事務沒有寫入磁盤, 那這個恢復過程可能會非常的漫長。