-- 查询未空闲的进程信息
select * from pg_stat_activity where current_query<>'<IDLE>';
结果可查看数据库名,进程标识符,用户名,查询语句,是否锁表,查询开始时间,客户端地址等信息。
当执行错误或者锁表,可在客户端处理,可先用取消进程,如果不好用可用终止进程。
-- GP查看数据锁
SELECT pid,rolname, rsqname, granted,
current_query, datname
FROM pg_roles, gp_toolkit.gp_resqueue_status, pg_locks, pg_stat_activity
WHERE pg_roles.rolresqueue=pg_locks.objid
AND pg_locks.objid=gp_toolkit.gp_resqueue_status.queueid
AND pg_stat_activity.procpid=pg_locks.pid;
-- 取消进程
select pg_cancel_backend(procpid);
-- 终止进程
select pg_terminate_backend(procpid);
-- 版本信息
SELECT VERSION();
-- 客户端编码
SELECT pg_client_encoding();
--当前数据库IP和端口
SELECT inet_server_addr(),inet_server_port();
--查看数据库大小,以字节和GB格式输出
SELECT pg_database_size('devrpt'),pg_size_pretty(pg_database_size('devrpt'));
-- 查看各模式大小
select pg_size_pretty(cast( sum(pg_relation_size( schemaname || '.' || tablename)) as bigint)), schemaname
from pg_tables t inner join pg_namespace d on t.schemaname=d.nspname group by schemaname ORDER BY 1 DESC;
-- 根据模式查询表大小,数据库空间不足清理表时经常使用!
select schemaname || '.' || tablename, pg_size_pretty(pg_relation_size( schemaname || '.' || tablename))
from pg_tables t inner join pg_namespace d on t.schemaname=d.nspname
where schemaname='anrpt' order by pg_relation_size( schemaname || '.' || tablename) desc;
-- 显示是否自动提交
show AUTOCOMMIT
-- 显示最大连接数
show max_connections
-- 按照用户分组查看连接数
select usename, count(*) from pg_stat_activity group by usename order by count(*) desc;
-- 修改最大连接数
gpconfig -c max_connection -v 500 -m 500
-- 修改用户user1的连接数
ALTER ROLE user1 CONNECTION LIMIT 300
-- 查看函数创建语句
select proname, prosrc from pg_proc where proname = 'function_name';
-- 做表分析
vacuum analyze 表名