postgresql日常運維

1.啓停
postgres –D $PGDATA start
pg_ctl -D $PGDATA start

三種關機模式:
pg_ctl –D $PGDATA stop -m smart 
不允許新連接,等待會話結束

pg_ctl –D $PGDATA stop –m fast
不允許新連接,等待子進程退出,終止備份

pg_ctl –D $PGDATA stop –m immediate
立即退出,下次啓動需重放wal日誌進行恢復

2. pg導數
:::::::::::::::::::::::導出
1)整庫備份
pg_dump  -d <dbname> -Fd -j 10 -Z 5 -v -f /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"` &>out.log
導出sql
nohup pg_dump  -d <dbname> -Fc -Z 9  -s -v -f /$backdir/exp.sql  &
2)只備份數據
pg_dump  -d <dbname> -Fd -j 10 -Z 5 -v -a  -f /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"` &>out.log
3)只備份結構
pg_dump  -d <dbname> -Fd -Z 5 -v -s  -f /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"`  &>out.log
4)只備份特定schema
pg_dump  -d <dbname> -n <schemaname>  -Fd -j 10 -Z 5 -v  -f /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"`  &>out.log

5)只備份特定表(單表不支持並行)
pg_dump  -d <dbname> -t <tablename>  -Fd -Z 5 -v  -f /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"`  &>out.log
多個表 -t <tablename1> -t <tablename2> ...

[postgres@主機:<dbname>:port ~]$ pg_dump --help
-F, --format=c|d|t|p         output file format (custom, directory, tar,
                               plain text (default))
-j, --jobs=NUM               use this many parallel jobs to dump
-Z, --compress=0-9           compression level for compressed formats
-v, --verbose                verbose mode
-f, --file=FILENAME          output file or directory name

:::::::::::::::::::::::恢復
1)整庫恢復
nohup psql -d <dbname> < imp.sql &
pg_restore -d <dbname> -j 10 -v /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"`   &>out.log
2)恢復前刪除已存在object
pg_restore -d <dbname> -j 10 -v -c --if-exists /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"`  &>out.log
3)只恢復數據
pg_restore -d <dbname> -j 10 -v -a /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"`   &>out.log
4)只恢復結構
    pg_restore -d <dbname> -v -s /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"`    &>out.log
5)恢復特定schema
    pg_restore -d <dbname> -n <schemaname> -j 10 -v  /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"`  &>out.log
6)恢復特定表(單表不支持並行)
    pg_restore -d <dbname> -t <tablename> -v /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"`   &>out.log
7)恢復特定函數
    pg_restore -d <dbname> -P <functionname> -j 10 -v /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"`   &>out.log
8)不恢復權限
    pg_restore -d <dbname>  -j 10 -v  -x /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"`  &>out.log
9)替換表owner
pg_restore -d <dbname> -t <tablename> -O --role=devroot -v /$backdir/pgdump_$PGNAME_`date +"%Y%m%d%H%M"` &>out.log

:::::::::::::::::::::::導數注意
<1> 查看數據庫大小
select pg_database_size('<dbname>'); 

<2> 索引的刪除與重建
當數據庫較大時,需要刪除索引和約束,恢復完成後再重建
:::::生成刪除索引語句
select 'drop index'|| b.indexname from pg_tables a,pg_indexes b where a.tablename=b.tablename and a.tableowner='<schemaname>';
:::::生成創建索引語句  
select b.indexdef||';' from pg_tables a,pg_indexes b where a.tablename=b.tablename and a.tableowner='<schemaname>';
:::::生成刪除主鍵語句
select 'alter table '||t.tablename||' drop CONSTRAINT '||i.indexname||';' from  pg_indexes i ,pg_tables t 
where i.schemaname=t.schemaname and i.tablename=t.tablename  and i.indexname like '%pk%' and t.tableowner='<schemaname>';
:::::生成添加主鍵語句
select 'alter table ' ||t.tablename||'   add primary key using index '||i.indexname||';' from  pg_indexes i ,pg_tables t 
where i.schemaname=t.schemaname and i.tablename=t.tablename  and i.indexname like '%pk%' and t.tableowner='<schemaname>';
:::::查詢整庫的索引大小並排序
SELECT c.relname,c2.relname, c2.relpages*8/1024 as size_MB, indexdef||';' as index_def
FROM pg_class c, pg_class c2, pg_index i,pg_indexes iv 
WHERE  c.oid = i.indrelid AND c2.oid = i.indexrelid and c2.relname=iv.indexname ORDER BY c2.relpages*8 desc;


<3> 刪除整庫
導入整庫前,需刪除數據庫
DROP DATABASE <dbname>;

3.添加pg_pathman
注意,需要在每個<db>均升級
\l 查詢數據庫
\c <dbname> 進入數據庫
::::添加插件
create extension pg_pathman; 
::::升級插件
alter extension pg_pathman UPDATE to '1.4';
SET pg_pathman.enable = t;
::::刪除插件
drop extension pg_pathman

4.查詢會話
::::正在連接的會話
select * from pg_stat_activity;
select usename,client_addr,waiting,state,pid from pg_stat_activity;
select * from pg_stat_activity where state='active';
::::查看連接數
select count(*) from pg_stat_activity;
:::::生成批量結束會話語句
select 'SELECT pg_terminate_backend('||pid||');'  from pg_stat_activity;
select 'kill ' || pid from pg_stat_activity where query like '%SELECT message.roomid, message.fromjid, message.tojid%';

6.鎖
:::::查詢當前所有鎖與對象的對應關係
select a.locktype,a.pid,a.relation,a.mode,a.granted,b.relname from pg_locks a,pg_class b where a.relation=b.oid;
:::::查詢鎖定query,用戶
select usename,query,client_addr, query_start,pid,client_addr from pg_stat_activity where pid=15010;
select query from pg_stat_activity where pid=15008;
:::::pg的鎖類型
acesss share: 只與access exclusive衝突,select 加鎖,在用戶select時不能做ddl操作
row share: 與exclusive和access exclusive衝突,類似mysql共享意向鎖,select for update/select for share 加鎖
row exclusive: 與share/share row exclusive/exclusive/access exclusive衝突,UPDATE/INSERT/DELETE加這種鎖
share update exclusive:與row share/access exclusive/share/share row exclusive/exclusive,vacuum(不帶full)/anslyze/create index concurr-ently
share: 與row exclusive/share update exclusive/share row exclusive/exclusive/access exclusive ,create index(不帶concurrently選項語句)加鎖
share row exclusive: 
  Conflicts with the ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, and ACCESS EXCLUSIVE lock modes. This mode protects a table against concurrent data changes, and is self-exclusive so that only one session can hold it at a time.
  Acquired by CREATE TRIGGER and many forms of ALTER TABLE (see ALTER TABLE).
exclusive:
  Conflicts with the ROW SHARE, ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, and ACCESS EXCLUSIVE lock modes. This mode allows only concurrent ACCESS SHARE locks, i.e., only reads from the table can proceed in parallel with a transaction holding this lock mode.
  Acquired by REFRESH MATERIALIZED VIEW CONCURRENTLY.
access exclusive: 類似於mysql的意向排他鎖,爲併發執行設計
  Conflicts with locks of all modes (ACCESS SHARE, ROW SHARE, ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, and ACCESS EXCLUSIVE). This mode guarantees that the holder is the only transaction accessing the table in any way.
  Acquired by the DROP TABLE, TRUNCATE, REINDEX, CLUSTER, VACUUM FULL, and REFRESH MATERIALIZED VIEW (without CONCURRENTLY) commands. Many forms of ALTER TABLE also acquire a lock at this level. This is also the default lock mode for LOCK TABLE statements that do not specify a mode explicitly.

詳見:https://www.postgresql.org/docs/9.5/static/explicit-locking.html

7.權限
\du+ 查看用戶權限及角色
pg_stats
select * from pg_tables where tableowner='<schemaname>';

8.database操作
::::重命名
ALTER DATABASE <dbname>  RENAME TO <new_dbname>;
::::建庫
create database <dbname> owner postgres;
::::刪庫
DROP DATABASE <dbname>;

9. pg_hba.conf
postgres的連接配置文件,標明允許什麼用戶連接,及連接權限
# TYPE DATABASE  USER     ADDRESS         METHOD
  local  all       all                    trust
  host   all       all    ip/8             md5
注意,修改過後使用pg_ctl reload重新應用
  
10.xlog
:::清除20天之前的xlog
find $PGDATA/pg_xlog -mtime +20 -name "000000*"
xlog是pg的wal日誌存儲目錄,相當於Oracle歸檔日誌和redo日誌的合體
如果刪除掉正在應用的日誌會導致數據庫不一致,無法start,最好不要做這種操作
find /paic/pg6604/data/pg_xlog -mtime +6 -name "000000*" -exec rm -rf {} \;

11.數據操作
::::添加列
ALTER TABLE <table_name> ADD <col_name> Character varying(2000) not null default 0;
::::改密碼
ALTER USER <user_name> WITH PASSWORD 'xxxx';
:::::執行sql腳本:
\x
\i a.sql
:::::顯示SQL執行時間:
\timing on
:::::顯示所有表名:
\d
:::::查看錶定義:
\d <表名>
:::::只顯示錶:
\dt
::::只顯示索引:
\di
::::只顯示序列:
\ds
::::只顯示視圖:
\dv
::::顯示Schema:
\dn
::::顯示錶空間:
\db
::::列出用戶和角色:
\du或\dg
::::顯示錶權限分配情況:
\dp <表名>
::::關閉自動提交
\set 	AUTOCOMMIT off
::::顯示所有數據庫:
\l
::::進入某一個庫:
\c <數據庫名>

12.查詢佔用空間
::::查詢數據表佔用空間大小
SELECT  
    table_schema || '.' || table_name AS table_full_name,  
    pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) AS size  
FROM information_schema.tables  
ORDER BY  
    pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC  ;
:::::查詢一個索引大小
select pg_size_pretty(pg_relation_size('indexname))
:::::查看一張表及此它上的索引總大小
select pg_size_pretty(pg_total_relation_size('tablename')); 
	
13.執行計劃
explain (analyze on, verbose on, buffers on, timing on, costs on)
<query>

14.查看慢sql
SELECT  query, calls, total_time, (total_time/calls) as average ,rows, 
        100.0 * shared_blks_hit /nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent 
FROM    pg_stat_statements 
ORDER   BY average DESC LIMIT 10;

 

發佈了29 篇原創文章 · 獲贊 10 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章