PostgreSQL常用腳本整理


1、序列

以自增serial類型主鍵的序列:

alter sequence s_seq restart with 1;  #重置序列
select currval('tablename_pid_seq');
select nextval('tablename_pid_seq');
select setval('tablename_pid_seq',1);

2、修改表

--刪除約束
alter table tablename alter column col drop not null;

--添加約束
alter table tablename add constraint tablename_col_uq unique(col);

--修改類型/長度
alter table tablename alter column col type character varying(2000);

--添加列
alter table tablename add column col character varying(255);

--刪除列
alter table tablename drop column col; 

3、查看數據使用空間

select pg_size_pretty(pg_database_size('dbname')); 

4、優化與監控

#查看數據庫
select * from pg_stat_database

#查看鏈接情況
select * from pg_stat_activity where datname = 't0pagped'

select count(1) from pg_stat_activity;

#根據IP地址匯總連接數

select client_addr,count(client_addr) as num from pg_stat_activity
where datname = 't0pagped' and application_name = 'PostgreSQL JDBC Driver'
group by client_addr
order by num desc

#最大連接數
show max_connections;

#查看表使用情況
select * from pg_stat_user_tables where relname = 'kg_askbob_entity_ins_capital' order by seq_scan desc

#查看表索引
select * from pg_stat_user_indexes where relname = 'kg_askbob_entity_ins_capital'

 

PostgreSQL統計信息:

https://yq.aliyun.com/articles/697692


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM