PostgreSQL常用操作命令

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: 个人常用的PostgreSQL基础命令做一下整理,同时也分享给有需要的小伙伴

需要vacuum的表:

select schemaname||'.'||relname, n_tup_upd, n_tup_del, n_tup_hot_upd, n_live_tup, n_dead_tup, last_vacuum, last_autovacuum
from pg_stat_user_tables
where n_dead_tup > n_live_tup/2;

需要添加index的表:

select schemaname||'.'||relname, pg_size_pretty(pg_table_size(relid)) as table_size, seq_scan, seq_tup_read, idx_scan, idx_tup_fetch
from pg_stat_user_tables
where pg_table_size(relid) > 1024*1024*1024 and seq_scan>10000
order by seq_scan desc;

重复创建的index:

SELECT pg_size_pretty(SUM(pg_relation_size(idx))::BIGINT) AS SIZE,
       (array_agg(idx))[1] AS idx1, (array_agg(idx))[2] AS idx2,
       (array_agg(idx))[3] AS idx3, (array_agg(idx))[4] AS idx4
FROM (
    SELECT indexrelid::regclass AS idx, (indrelid::text ||E'\n'|| indclass::text ||E'\n'|| indkey::text ||E'\n'||COALESCE(indexprs::text,'')||E'\n' || COALESCE(indpred::text,'')) AS KEY
    FROM pg_index) sub
GROUP BY KEY HAVING COUNT(*)>1
ORDER BY SUM(pg_relation_size(idx)) DESC;
 
or
 
select * from(
        select tablespace,schemaname,tablename,indexname,pg_size_pretty(pg_table_size(schemaname||'."'||indexname||'"')) as index_size,indexdef,
               count(1) over( partition by schemaname, tablename, regexp_replace(indexdef, E'(CREATE )(.*)(INDEX )(.+)( ON )(.+)',E'\\1\\3\\5\\6') )
        from pg_indexes) as foo
where count > 1;

表与索引的关系:

SELECT
    t.tablename,
    indexname,
    c.reltuples AS num_rows,
    pg_size_pretty(pg_relation_size(quote_ident(t.tablename)::text)) AS table_size,
    pg_size_pretty(pg_relation_size(quote_ident(indexrelname)::text)) AS index_size,
    CASE WHEN indisunique THEN 'Y' ELSE 'N' END AS UNIQUE,
    idx_scan AS number_of_scans,
    idx_tup_read AS tuples_read,
    idx_tup_fetch AS tuples_fetched
FROM pg_tables t
LEFT OUTER JOIN pg_class c ON t.tablename=c.relname
LEFT OUTER JOIN
    ( SELECT c.relname AS ctablename, ipg.relname AS indexname, x.indnatts AS number_of_columns, idx_scan, idx_tup_read, idx_tup_fetch, indexrelname, indisunique FROM pg_index x
           JOIN pg_class c ON c.oid = x.indrelid
           JOIN pg_class ipg ON ipg.oid = x.indexrelid
           JOIN pg_stat_all_indexes psai ON x.indexrelid = psai.indexrelid )
    AS foo
    ON t.tablename = foo.ctablename
WHERE t.schemaname='public'
ORDER BY 1,2;

无效的index:

select pi.schemaname, pi.relname, pi.indexrelname, pg_size_pretty(pg_table_size(pi.indexrelid))
from pg_indexes pis
join pg_stat_user_indexes pi
on pis.schemaname = pi.schemaname
and pis.tablename = pi.relname
and pis.indexname = pi.indexrelname
left join pg_constraint pco
on pco.conname = pi.indexrelname
and pco.conrelid = pi.relid
where pi.schemaname='public'
        and pco.contype is distinct from  'p' and pco.contype is distinct from  'u'
        and (idx_scan,idx_tup_read,idx_tup_fetch) = (0,0,0)
        and pis.indexdef !~ ' UNIQUE INDEX '
        and pi.relname !~ 'backup$'
 order by pg_table_size(indexrelid)  desc;

查看数据库中所有表的主键:

select table_schema||'.'||relname as tablename,attname as columname
from pg_constraint con,pg_class c,pg_attribute a,information_schema.columns col
where con.conrelid=c.oid
and a.attrelid=c.oid
and contype='p'
and a.attnum=con.conkey[1]
and col.table_name=c.relname
and a.attname=col.column_name;

查看表及索引占用磁盘大小:

select schemaname||'.'||relname,pg_size_pretty(pg_total_relation_size(relid)) as all_size,pg_size_pretty(pg_table_size(relid)) as table_size,pg_size_pretty(pg_indexes_size(relid)) as all_indexes_size from pg_stat_user_tables order by pg_total_relation_size(relid) desc;
 
--增加tablespace
select ut.schemaname||'.'||ut.relname,pg_size_pretty(pg_total_relation_size(relid)) as all_size,pg_size_pretty(pg_table_size(relid)) as table_size,pg_size_pretty(pg_indexes_size(relid)) as all_indexes_size,ts.spcname from pg_class c left join pg_stat_user_tables ut on c.oid=ut.relid left join pg_tablespace ts on c.reltablespace=ts.oid left join pg_namespace ns on c.relnamespace=ns.oid where ns.nspname not in('pg_catalog', 'information_schema', 'pg_toast','pgq','pgq_node','pgq_ext','londiste') and c.relkind='r' order by pg_total_relation_size(relid) desc;

查看索引大小及DDL:

select schemaname||'.'||relname,indexrelname,pg_size_pretty(pg_table_size(indexrelid)),pg_get_indexdef(indexrelid) from pg_stat_user_indexes order by pg_total_relation_size(indexrelid) desc;

重置表或索引的使用率:

select pg_stat_reset_single_table_counters('object_name'::regclass);

同时修改多个实例的hba文件:

ps aux | grep postgres | grep bin | grep data | egrep -v 'grep|pgbouncer'|awk -F 'D' '{print $2"/pg_hba.conf"}'|while read line; do echo $line;echo "host    all      all      127.0.0.1/32    md5" >> $line;pg_ctl -D `echo $line|awk -F 'pg_hba.conf' '{print $1}'` reload;cat $line;done

随机数,时间,字符:

select generate_series(1,10),floor(random()*(25-10)+10),generate_series(now(),now() + '1 day', '1 hours'::interval) as timestamp,random(),random()::numeric(10,2) as numeric,round(random()*100::integer) as integer,lower(chr(int4(random()*26)+65))||chr(int4(random()*26)+65) as char,md5(random()::text) as text;

执行超过3分钟的事物或者sql:

select pid,datname,usename,application_name,client_addr,backend_start,xact_start,query_start,(query_start - now()) as execute_time,state,query from pg_stat_activity where (((xact_start - now()) >= '00:03:00') or ((query_start - now()) >= '00:03:00')) and state != 'idle' order by (query_start - now()) desc;

删除重复数据:

删除以col1,col2为分组的重复数据(逻辑取反方法):
delete from tablename where id not in(select max(id) from tablename group by col1,col2);
 
按col1删除重复数据(适用于数据量较大,取反效果差的表):
select * from tablename where id in(with tmp as (select id,col1 from (select id,col1,count(col1) over(partition by col1) as col1_count from tablename) as foo where col1_count>1)
select id from tmp where id not in(select max(id) from tmp group by msg_id));

重建主键:

先创建唯一索引:
create unique index concurrently on tablename_backup(id);
 
删除原主键约束,在唯一索引上添加主键约束:
begin; alter table tablename_backup drop constraint tablename_backup_pkey1; alter table tablename_backup add PRIMARY KEY USING INDEX tablename_backup_id_idx;commit;

为所有含有主键的表创建序列:

select case when array_length(conkey,1) !=1 then '--'||table_schema||'.'||relname||' 含有复合主键' else 'create sequence '||table_schema||'.'||relname||'_'||attname||'_seq owned by '||table_schema||'.'||relname||'.'||attname||';' end
from pg_constraint con,pg_class c,pg_attribute a,information_schema.columns col
where con.conrelid=c.oid
and a.attrelid=c.oid
and contype='p'
and a.attnum=con.conkey[1]
and col.table_name=c.relname
and a.attname=col.column_name
and col.column_default is null;

为所有表的主键添加对应的序列:

select 'alter table '||table_schema||'.'||relname||' alter column '||attname||' set default nextval('''||table_schema||'.'||relname||'_'||attname||'_seq'');'
from pg_constraint con,pg_class c,pg_attribute a,information_schema.columns col
where con.conrelid=c.oid
and a.attrelid=c.oid
and contype='p'
and a.attnum=con.conkey[1]
and col.table_name=c.relname
and a.attname=col.column_name
and col.column_default is null;

重置所有表的序列:

select case when array_length(conkey,1) !=1 then '--'||table_schema||'.'||relname||' 含有复合主键'
            when table_schema||'.'||relname||'_'||attname||'_seq' in(select s.schemaname||'.'||s.relname from pg_statio_user_sequences s) then 'select setval('''||table_schema||'.'||relname||'_'||attname||'_seq'''||',coalesce(max('||attname||'),0)::bigint + 1) from '||table_schema||'.'||relname||';' else '--'||table_schema||'.'||relname||'_'||attname||'_seq 序列不存在,如想添加序列为主键的默认值,请执行: '||'create sequence '||table_schema||'.'||relname||'_'||attname||'_seq owned by '||table_schema||'.'||relname||'.'||attname||'; alter table '||table_schema||'.'||relname||' alter column '||attname||' set default nextval('''||table_schema||'.'||relname||'_'||attname||'_seq'');' end as col1
from pg_constraint con,pg_class c,pg_attribute a,information_schema.columns col
where con.conrelid=c.oid
and a.attrelid=c.oid
and contype='p'
and a.attnum=con.conkey[1]
and col.table_name=c.relname
and a.attname=col.column_name;

生成按日期的sql:

按天生成:
select 'insert into tablename_backup select * from tablename where create_time>='''||a::timestamptz||'''and create_time<'''||a::timestamptz +'1 day'::interval||''';select pg_sleep(10);' from generate_series('2017-04-01'::date, '2017-06-30'::date, '1 day':: interval) as a ;
 
按月生成:
select 'insert into tablename_backup select * from tablename where create_time>='''||a::date||'''and create_time<'''||a::date + '1 month'::interval||''';select pg_sleep(10);' from generate_series('2017-04-01'::date, '2017-06-30'::date, '1 month':: interval) as a ;

主从同步差异:

物理延时(主库执行):
select application_name,pg_size_pretty(pg_xlog_location_diff(pg_current_xlog_location(), replay_location)) as diff,state from pg_stat_replication;
 
时间延时(从库执行):
SELECT                                
now() AS now,
coalesce(pg_last_xact_replay_timestamp(), now()) replay,
extract(EPOCH FROM (now() - coalesce(pg_last_xact_replay_timestamp(), now()))) AS diff,
(SELECT regexp_replace(a,E'primary_conninfo.+(host.*=.*).*(port=\\d+).+',E'\\1 \\2', 'ig') AS master
FROM regexp_split_to_table(pg_read_file('recovery.conf'), E'\\n') t(a)
WHERE a ~ '^ *primary_conninfo');
 
 
--主从同步是否暂停
select pg_is_xlog_replay_paused();
  
--暂停主从同步
select pg_xlog_replay_pause();
  
--恢复主从同步
select pg_xlog_replay_resume();
 
--强制轮转一个WAL(xlog)文件,这在主从切换时可以用到
select pg_switch_xlog()

关闭当前连接最多的sql:

系统级别,删除所有select
ps auxww | grep 'SELECT' | grep -v grep  | awk '{print $2}' | xargs -I {} kill {}
 
数据库级别,先获取连接数最多的sql
select datname,usename,application_name,client_addr,query, count(query) from pg_stat_activity where state != 'idle' group by datname,usename,application_name,client_addr,query order by count(query) desc limit 5;
 
杀掉连接:
select pg_terminate_backend(pid) from pg_stat_activity where query in(select query from pg_stat_activity where state != 'idle' group by datname,usename,application_name,client_addr,query order by count(query) limit 1 );
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
3月前
|
SQL 关系型数据库 数据库
PostgreSQL常用命令,启动连接,pg_dump导入导出
PostgreSQL常用命令,启动连接,pg_dump导入导出
|
5月前
|
SQL 关系型数据库 PostgreSQL
PostgreSQL和greenplum的copy命令可以添加字段吗?
【6月更文挑战第5天】PostgreSQL和greenplum的copy命令可以添加字段吗?
91 3
|
5月前
|
监控 关系型数据库 数据库
PostgreSQL和greenplum的copy命令如何使用?
【6月更文挑战第5天】PostgreSQL和greenplum的copy命令如何使用?
153 2
|
6月前
|
关系型数据库 数据库 PostgreSQL
postgresql|【基于pg_basebackup命令的归档备份和恢复---热备冷恢复方式】
postgresql|【基于pg_basebackup命令的归档备份和恢复---热备冷恢复方式】
283 0
|
6月前
|
SQL 关系型数据库 数据库
Postgresql基本操作命令
这些是PostgreSQL数据库的一些基本操作命令,用于创建、管理和查询数据库。根据您的需求,可以使用这些命令执行各种数据库操作。
116 4
|
6月前
|
SQL 关系型数据库 MySQL
MySQL【实践 02】MySQL迁移到PostgreSQL数据库的语法调整说明及脚本分享(通过bat命令修改mapper文件内的SQL语法)
MySQL【实践 02】MySQL迁移到PostgreSQL数据库的语法调整说明及脚本分享(通过bat命令修改mapper文件内的SQL语法)
259 0
|
6月前
|
关系型数据库 数据库 数据安全/隐私保护
postgresql |数据库 |postgresql数据库的短命令详细介绍
postgresql |数据库 |postgresql数据库的短命令详细介绍
41 0
|
SQL 安全 前端开发
PostgreSQL 高权限命令执行 (CVE-2019-9193)漏洞复现&实战
PostgreSQL 高权限命令执行 (CVE-2019-9193)漏洞复现&实战
|
SQL 存储 Oracle
【postgreSQL】psql工具特有的快捷命令2
【postgreSQL】psql工具特有的快捷命令2
165 0
|
存储 SQL 关系型数据库
postgresql常见命令及操作
  pgsql已经更新到beta11了,不同版本的服务器启动或相关命令、配置可能会有不同,所以得根据pg版本进行操作。下面记录一些工作中常用到的一些操作,主要包括服务启动、备份/恢复数据、数据目录迁移、常见操作命令 本文环境: postgres : v10.3 os: MAC 虽然已经在kong部署中介绍了postgres的部署,为了行文连贯性,这里再简单记录下pg的启动相关命令。
4251 0