PostgreSQL数据库常用命令

前端之家收集整理的这篇文章主要介绍了PostgreSQL数据库常用命令前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

原文链接:https://www.f2er.com/postgresql/194477.html

猜你在找的Postgre SQL相关文章