Greenplum各种Tips(不定时更新)
Greenplum接触也有一段时间了,在使用过程中积累了一些命令,在此分享给大家。
1. 查看segment是否有切换(没有记录则没有切换)
SELECT * from gp_segment_configuration where status='d' or mode <> 's';
2. 查看表的分布是否均匀
select gp_segment_id,count(*) from table_name group by 1;
3. 统计各个数据库大小
select datname,pg_size_pretty(pg_database_size(datname)) from pg_database;
4. 显示哪些没有统计信息且可能需要ANALYZE的表
SELECT * from gp_toolkit.gp_stats_missing ;
5. 显示在系统表中被标记为掉线的Segment的信息
SELECT * from gp_toolkit.gp_pgdatabase_invalid;
6. 显示库中表的大小(单位G)
SELECT sotdoid,sotdsize/1024/1024/1024 as sotdsize,sotdtoastsize,sotdadditionalsize,sotdschemaname,sotdtablename from gp_toolkit.gp_size_of_table_disk order by sotdsize desc;
7. 查询一个库中有多少表(如果有分区表不列出子分区)
SELECT relname from pg_class a,pg_namespace b where relname not like '%prt%' and relkind ='r' and a.relnamespace=b.oid and nspname not in ('pg_catalog','information_schema','gp_toolkit') and nspname not like '%pg_temp%';
8. 查询某个用户对某个表有什么权限
select * from INFORMATION_SCHEMA.role_table_grants where grantee='user_name' and table_name='table';
9. 查看分区表的信息
SELECT tablename,partitiontablename,partitiontype,partitionboundary from pg_partitions where tablename='table_name' order by partitionboundary desc limit 3;
10. 字符串类型转换到整型
ALTER TABLE table_name ALTER COLUMN COLUMN_NAME TYPE bigint USING (COLUMN_NAME::bigint);
11. inet类型转换到varchar类型不要子网掩码
ALTER TABLE table_name ALTER COLUMN COLUMN_NAME TYPE varchar(16) USING host(COLUMN_NAME);
12. 导出数据
COPY t1 to '/home/gpadmin/t1.txt' with delimiter '|';
13. 导入数据
copy t1 from '/home/gpadmin/t1.txt' with delimiter '|' LOG ERRORS INTO INSERT_ERRS SEGMENT REJECT LIMIT 100;
14. 远程导入数据
psql -h 1.1.1.1 -U user_name -d db_name -W -c "copy tb1 from stdin with delimiter '|'" < /home/gpadmin/tb1.txt
15. 导数据指定分隔符(和mysql的select into outfile很像)
psql -d db_name -c "select * from tb1" -o tb1.txt -t -A -F $'\t'
16. 生成授权语句
SELECT 'grant select on '||relname||' to user_name;' from pg_class a,pg_namespace b where relname not like '%prt%' and relkind ='r' and has_table_privilege('user_name',a.oid,'select')='f' and a.relnamespace=b.oid and nspname not in ('pg_catalog','information_schema','gp_toolkit') and nspname not like '%pg_temp%';
总结:
无非就是一些SQL命令以及数据字典的使用。熟悉数据字典非常重要。