postgresql數據庫常用命令
--獲取數據庫軟件版本
select version();
--獲取數據庫啟動時間
select pg_postmaster_start_time();
--獲取配置文件最近load時間
select pg_conf_load_time();
--獲取當前數據庫時區
show timezone;
--獲取當前實例中所有數據庫
psql -l
--獲取當前數據庫用戶
select user;
select current_user;
--獲取當前會話用戶
select session_user;
--獲取會話客戶端地址及端口
select inet_client_addr(),inet_client_port();
--獲取當前數據庫服務器地址及端口
--獲取當前會話服務進程
select pg_backend_pid();
--獲取當前參數配置
show shared_buffer;
select current_setting(‘shared_buffers‘);
--修改會話參數配置
set maintenance_work_mem to ‘32m‘;
select set_confit(‘maintenance_work_mem‘,‘32m‘,false);
--獲取當前WAL文件
select pg_xlogfile_name(pg_current_xlog_location());
select pg_xlog_location_diff(pg_current_xlog_insert_location(),pg_current_xlog_location());
--獲取實例中當前進行的備份
select pg_is_in_backup(),pg_backup_start_time();
--獲取當前實例的角色狀態(primary or standby)
select pg_is_in_recovery();
--獲取數據庫大小
select pg_database_size(‘mydb‘),pg_size_pretty(pg_database_size(‘mydb‘));
--表
select pg_size_pretty(pg_relation_size(‘mytab‘));
--表及索引
select pg_size_pretty(pg_total_relation_size(‘mytab‘));
--獲取索引大小
select pg_size_pretty(pg_indexes_size(‘mytab‘));
--獲取表空間大小
select pg_size_pretty(pg_tablespace_size(‘my_tbs‘));
--獲取表中每個數據塊的自由空間
select pg_freespace(‘mytab‘);
--獲取表中每個數據塊中自由空間比例
SELECT (100 * (upper - lower) / pagesize::float8)::integer AS free_pct
FROM page_header(get_raw_page(‘mytab‘,11));
--獲取表對應的數據文件
select pg_relation_filepath(‘mytab‘);
--重新加載配置文件
pg_ctl reload
select pg_reload_conf();
--切換log文件
select pg_rotate_logfile();
--切換xlog文件
select pg_rotate_xlog();
--手工產生檢查點
checkpoint;
--取消正在執行的SQL
select pg_cancel_backend(pid);
--終止後臺服務進程
select pg_terminate_backend(pid);
--獲取正在執行的SQL
select pid,username,query_start,query from pg_stat_activity;
--獲取Combo Command Ids
SELECT t_xmin AS xmin,
t_xmax::text::int8 AS xmax,
t_field3::text::int8 AS cmin_cmax,
(t_infomask::integer & X‘0020‘::integer)::bool AS is_combocid
FROM heap_page_items(get_raw_page(‘mytab‘,0))
ORDER BY 2 DESC, 3;
--創建獲取某數據塊元組原型視圖
CREATE VIEW t1_page0 AS
SELECT ‘(0,‘|| lp || ‘)‘ AS ctid,
CASE lp_flags
WHEN 0 THEN ‘Unused‘
WHEN 1 THEN ‘Normal‘
WHEN 2 THEN ‘Redirect to ‘|| lp_off
WHEN 3 THEN ‘Dead‘
END,
t_xmin::text::int8 AS xmin,
t_xmax::text::int8 AS xmax,
t_ctid
FROM heap_page_items(get_raw_page(‘t1‘,0))
ORDER BY lp;
select * from t1_page0;
postgresql數據庫常用命令