加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL数据库常用命令

发布时间:2020-12-13 17:05:20 所属栏目:百科 来源:网络整理
导读:1获取数据库软件版本 select version(); 2获取数据库启动时间 select pg_postmaster_start_time(); 3获取配置文件最近加载时间 select pg_conf_load_time(); 4获取当前数据库区时 show timezone; 5获取当前实例中所有数据库 psql -l 6获取当前数据库用户 sel

1>获取数据库软件版本

select version();

2>获取数据库启动时间

select pg_postmaster_start_time();

3>获取配置文件最近加载时间

select pg_conf_load_time();

4>获取当前数据库区时

show timezone;

5>获取当前实例中所有数据库

psql -l

6>获取当前数据库用户

select user;

select current_user;

7>获取当前会话用户

select session_user;

8>获取会话客户端地址及端口

select inet_client_addr(),inet_server_port();

9>获取当前数据库服务器地址及端口

select inet_server_addr(),inet_server_port();

10>获取当前会话服务进程

select pg_backend_pid();

11>获取当前参数配置

show shared_buffer;

select current_setting('shared_buffers');

12>修改会话参数配置

set maintenance_work_mem to '32m';

select set_confit('maintenance_work_mem','32m',false);

13>获取当前wal文件

select pg_clogfile_name(pg_current_xlog_location());

14>获取没写到磁盘的WAL buffer

selectpg_xlog_location_diff(pg_current_xlog_insert_location(),pg_current_xlog_location());

15>获取实例中当前进行的备份

select pg_is_in_backup(),pg_backup_start_time();
16>获取当前实例的角色状态(primary or standby)
select pg_is_in_recovery();

17>获取数据库大小
selectpg_database_size('mydb'),pg_size_pretty(pg_database_size('mydb'));
18>获取表大小
--表
select pg_size_pretty(pg_relation_size('mytab'));
--表及索引
selectpg_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 ASfree_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 ASis_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_ctid
FROM heap_page_items(get_raw_page('t1',0))
ORDER BY lp;

select * from t1_page0;




转载于http://weibo.com/p/2304183f2b488e0102x9x9

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读