问答

pg数据库常用操作 数据库基本操作

pg数据库的常用操作有以下几种:

一、查询

1)查询进程:

select * from pg_stat_activity;

说明:

datname表示数据库名

procpid表示当前的SQL对应的PID

query_start表示SQL执行开始时间

current_query表示当前执行的SQL语句

waiting表示是否正在执行,t表示正在执行,f表示已经执行完成

client_addr表示客户端IP地址

2)kill进程:

kill有两种方式,

第一种是:

SELECT pg_cancel_backend(PID);

第二种是:

SELECT pg_terminate_backend(PID);

3)查询连接数

SELECT count(*) FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;

二、授权

1)sequence授权,跟table授权有区别

grant usage,select on sequence <sequence_name> to <role>;

三、查询size

1、sql查询

postgres=# select pg_size_pretty(pg_database_size('postgres'));

2 、linux du查询

postgres=# SELECT oid from pg_database where datname='postgres';

oid

-------

21360

查看名称为21360的文件的大小即是数据库postgres的大小;

四、解锁,注意要先连接到表所在的database

\c &dbname

select distinct a.relname,b.pid from pg_class a,pg_locks b where a.oid=b.relation and b.granted=true and a.relname like '%&table_name%';

select pg_terminate_backend(&pid);

五、查询哪些表占用的空间大。

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

where table_name like '%awr%'

ORDER BY

pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC ;

相关新闻

联系我们

联系我们

400-800-4147

在线咨询: 点击这里给我发消息

邮件:admin@yxyszz.cn

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部