Greenplum使用简明手册

时间:2022-09-19 09:11:57

GP服务启停

su - gpadmin

gpstart #正常启动

gpstop #正常关闭

gpstop -M fast #快速关闭

gpstop –r #重启

gpstop –u #重新加载配置文件

登陆与退出Greenplum

#正常登陆

psql gpdb

psql -d gpdb -h gphostm -p 5432 -U gpadmin

#使用utility方式

PGOPTIONS="-c gp_session_role=utility" psql -h -d dbname hostname -p port

#退出

在psql命令行执行\q

参数查询

psql -c 'SHOW ALL;' -d gpdb

gpconfig --show max_connections

创建数据库 createdb -h localhost -p 5432 dhdw

创建GP文件系统

# 文件系统名

gpfsdw

# 子节点,视segment数创建目录

mkdir -p /gpfsdw/seg1

mkdir -p /gpfsdw/seg2

chown -R gpadmin:gpadmin /gpfsdw

# 主节点

mkdir -p /gpfsdw/master

chown -R gpadmin:gpadmin /gpfsdw

gpfilespace -o gpfilespace_config

gpfilespace -c gpfilespace_config

创建GP表空间

psql gpdb

create tablespace TBS_DW_DATA filespace gpfsdw;

SET default_tablespace = TBS_DW_DATA;

删除GP数据库 gpdeletesystem -d /gpmaster/gpseg-1 -f

查看segment配置 select * from gp_segment_configuration;

文件系统 select * from pg_filespace_entry;

磁盘、数据库空间

SELECT * FROM gp_toolkit.gp_disk_free ORDER BY dfsegment;

SELECT * FROM gp_toolkit.gp_size_of_database ORDER BY sodddatname;

日志

SELECT * FROM gp_toolkit.__gp_log_master_ext;

SELECT * FROM gp_toolkit.__gp_log_segment_ext;

表描述 /d+ <tablename>

表分析 VACUUM ANALYZE tablename;

表数据分布 SELECT gp_segment_id, count(*) FROM <table_name> GROUP BY gp_segment_id;

表占用空间

SELECT relname as name, sotdsize/1024/1024 as size_MB, sotdtoastsize as toast, sotdadditionalsize as other

   FROM gp_toolkit.gp_size_of_table_disk as sotd, pg_class

 WHERE sotd.sotdoid = pg_class.oid ORDER BY relname;

索引占用空间

SELECT soisize/1024/1024 as size_MB, relname as indexname

 FROM pg_class, gp_toolkit.gp_size_of_index

 WHERE pg_class.oid = gp_size_of_index.soioid

  AND pg_class.relkind='i';

OBJECT的操作统计

SELECT schemaname as schema, objname as table, usename as role, actionname as action, subtype as type, statime as time

 FROM pg_stat_operations

 WHERE objname = '<name>';

SELECT locktype, database, c.relname, l.relation, l.transactionid, l.transaction, l.pid, l.mode, l.granted, a.current_query

 FROM pg_locks l, pg_class c, pg_stat_activity a

 WHERE l.relation=c.oid

  AND l.pid=a.procpid

 ORDER BY c.relname;

队列 SELECT * FROM pg_resqueue_status

查看指定数据库 select pg_size_pretty(pg_database_size('<database_name>'));

所有数据库 select datname,pg_size_pretty(pg_database_size(<database_name>)) from pg_database; 

查看数据库表清单(包括临时表) pg_tables
主表与分区表 pg_partitions
字段清单 information_schema.columns
视图 pg_views
字典 schema : information_schema

GP中查询表和视图清单,视图的字段及其类型

查询schema_name.view_name的字段及其类型:
    select a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod) as type,a.attnotnull
    from pg_catalog.pg_attribute a,pg_catalog.pg_class c, pg_catalog.pg_namespace n
    where  a.attrelid=c.oid
    and c.relname='view_name'
    and a.attnum>0 AND NOT a.attisdropped
    and n.oid = c.relnamespace
    and n.nspname='schema_name'
    order by a.attnum
查询指定schema下view的清单:
    select c.relname
    from pg_catalog.pg_class c, pg_catalog.pg_namespace n
    where n.oid = c.relnamespace
    and n.nspname='schema_name'
    and  c.relkind='v'
查询指定schema下所有table的清单:
    select c.relname
    from pg_catalog.pg_class c, pg_catalog.pg_namespace n
    where n.oid = c.relnamespace
    and n.nspname='schema_name'
    and  c.relkind='r'
查询指定schema下所有外部表的清单:
    select c.relname
    from pg_catalog.pg_class c, pg_catalog.pg_namespace n
    where n.oid = c.relnamespace
    and n.nspname='schema_name'
    and  c.relkind='r'  and relstorage='x'
查询指定schema下所有普通表的清单:
    select c.relname
    from pg_catalog.pg_class c, pg_catalog.pg_namespace n
    where n.oid = c.relnamespace
    and n.nspname='schema_name'
    and  c.relkind='r'  and relstorage in ('h','a');
注意,relstorage字段取值“x”表示外部表,“h”表示heap表,“a”表示append-only表,“v”表示virtual表。
查询指定schema_name下table_owner的表:
    select * from
    (
    select c.relname, pg_catalog.pg_get_userbyid(c.relowner) as table_owner
    from pg_catalog.pg_class c, pg_catalog.pg_namespace n
    where n.oid = c.relnamespace
    and n.nspname='schema_name'
    ) t where t.table_owner = 'table_owner';
查询指定指定table_owner的所有普通表:
        select * from
        (
        select n.nspname, c.relname, pg_catalog.pg_get_userbyid(c.relowner) as table_owner
        from pg_catalog.pg_class c, pg_catalog.pg_namespace n
        where n.oid = c.relnamespace and  c.relkind='r' and relstorage in ('h', 'a')
        ) t where t.table_owner = 'table_owner';

 

加载(LOAD)数据到Greenplum数据库

gpfdist外部表

# 启动服务

gpfdist -d /share/txt -p 8081 –l /share/txt/gpfdist.log &

# 创建外部表,分隔符为’/t’

drop EXTERNAL TABLE TD_APP_LOG_BUYER;

CREATE EXTERNAL TABLE TD_APP_LOG_BUYER (

 IP         text,

 ACCESSTIME text,

 REQMETHOD  text,

 URL        text,

 STATUSCODE int,

 REF        text,

 name       text,

 VID        text)

LOCATION ('gpfdist://gphostm:8081/xxx.txt')

FORMAT 'TEXT' (DELIMITER E'/t'

FILL MISSING FIELDS) SEGMENT REJECT LIMIT 1 percent;

# 创建普通表

create table test select * from TD_APP_LOG_BUYER;

# 索引

# CREATE INDEX idx_test ON test USING bitmap (ip);

# 查询数据

select ip , count(*) from test group by ip order by count(*);

gpload

# 创建控制文件

# 加载数据

gpload -f my_load.yml

copy

COPY country FROM '/data/gpdb/country_data'

 WITH DELIMITER '|' LOG ERRORS INTO err_country

 SEGMENT REJECT LIMIT 10 ROWS;

从Greenplum数据库卸载(UNLOAD)数据

gpfdist外部表

# 创建可写外部表

CREATE WRITABLE EXTERNAL TABLE unload_expenses

( LIKE expenses )

LOCATION ('gpfdist://etlhost-1:8081/expenses1.out',

'gpfdist://etlhost-2:8081/expenses2.out')

FORMAT 'TEXT' (DELIMITER ',')

DISTRIBUTED BY (exp_id);

# 写权限

GRANT INSERT ON writable_ext_table TO <name>;

# 写数据

INSERT INTO writable_ext_table SELECT * FROM regular_table;

copy

COPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/home/gpadmin/a_list_countries.out';

执行sql文件

psql gpdbname –f yoursqlfile.sql

或者psql登陆后执行

\i yoursqlfile.sql

如何杀掉greenplum、postgresql的会话进程

在linux命令行执行kill -s SIGTERM <procid>杀掉该会话的进程
用函数杀sql
对于查询SQL
select pg_cancel_backend(procpid);
其他SQL
select pg_terminate_backend(procpid);

调整Greenplum的SQL查询计划

在oracle中使用hint可以调整SQL的执行计划,在postgresql可以使用如下的方法进行调整

使用set<option> to off/on;调整查询计划,参数如下:
enable_seqscan 是否走全表扫描
enable_hashjoin 是否允许走
hash 连接
enable_nestloop 是否允许走
nestloo 连接
enable_mergejoin 是否允许走合并连接
enable_tidscan 是否允许走tid 扫描(类似oracle中的按rowid访问)
enable_bitmapscan 是否允许走
bitmap 扫描
enable_hashagg 是否允许走
hash 聚集(也就是做group by时)
enable_indexscan 是否允许走索引
enable_sort 是否允许走排序
constraint_exclusion 是否允许走分区