HGDB表空间之间移动数据

一、迁移单个表至另一个表空间
highgo=# create tablespace test location '/home/highgo/test';
CREATE TABLESPACE
highgo=# select * from pg_tablespace;
  spcname   | spcowner | spcacl | spcoptions 
------------+----------+--------+------------
 pg_default |       10 |        | 
 pg_global  |       10 |        | 
 test       |       10 |        | 
(3 rows)


highgo=# \d test
        Table "public.test"
 Column |     Type     | Modifiers 
--------+--------------+-----------
 id     | integer      | 
 name   | character(5) | 
Indexes:
    "t_i_2" btree (id)

highgo=# alter table test set tablespace test;
ALTER TABLE
highgo=# \d test
        Table "public.test"
 Column |     Type     | Modifiers 
--------+--------------+-----------
 id     | integer      | 
 name   | character(5) | 
Indexes:
    "t_i_2" btree (id)
Tablespace: "test"


二、迁移整个数据库至另一个表空间
highgo=# select datname,dattablespace from pg_database where datname='benchmarksql';
   datname    | dattablespace 
--------------+---------------
 benchmarksql |          1663
(1 row)


highgo=# select oid,spcname from pg_tablespace where oid=1663;
 oid  |  spcname   
------+------------
 1663 | pg_default
(1 row)

[root@sourcedb PG_9.4_201409291]# pwd
/home/highgo/test/PG_9.4_201409291
[root@sourcedb PG_9.4_201409291]# ll
total 4
drwx------ 2 highgo highgo 4096 Aug 10 16:29 13010
[root@sourcedb PG_9.4_201409291]# 

highgo=# alter database benchmarksql set tablespace test;
ALTER DATABASE
highgo=# 

[root@sourcedb PG_9.4_201409291]# ll
total 8
drwx------ 2 highgo highgo 4096 Aug 10 16:29 13010
drwx------ 2 highgo highgo 4096 Aug 10 16:32 41209

highgo=# select datname,dattablespace from pg_database where datname='benchmarksql';
   datname    | dattablespace 
--------------+---------------
 benchmarksql |         41255
(1 row)

highgo=# select oid,spcname from pg_tablespace where oid=41255;
  oid  | spcname 
-------+---------
 41255 | test
(1 row)

highgo=# select oid,spcname from pg_tablespace where oid=1663;
 oid  |  spcname   
------+------------
 1663 | pg_default
(1 row)


查看表空间对应的物理路径:
highgo=# \db
           List of tablespaces
    Name    | Owner  |     Location      
------------+--------+-------------------
 pg_default | highgo | 
 pg_global  | highgo | 
 test       | highgo | /home/highgo/test
(3 rows)

**********************************************************
将以上对象重新移动到pg_default表空间中:
highgo=# alter database benchmarksql set  tablespace pg_default;
ALTER DATABASE
highgo=# alter table test set tablespace pg_default;
ALTER TABLE
highgo=# \d test
        Table "public.test"
 Column |     Type     | Modifiers 
--------+--------------+-----------
 id     | integer      | 
 name   | character(5) | 
Indexes:
    "t_i_2" btree (id)

highgo=# select datname,dattablespace from pg_database where datname='benchmarksql';
   datname    | dattablespace 
--------------+---------------
 benchmarksql |          1663
(1 row)

highgo=# select oid,spcname from pg_tablespace where oid=1663;
 oid  |  spcname   
------+------------
 1663 | pg_default
(1 row)
*****************************************************************************
三、把整个表空间的数据全部移动到另一个表空间中
目前HGDB及社区版PG中均不支持此用法。

俄罗斯版PostgreSQL9.4中引入了一个新的功能:一次性把一个空间的多个对象迁移到另一个表空间。如果目录执行者是超级用户,那么源表空间所有的对象都会被迁移过去;否则仅会迁移执行者所拥有的对象。
将pg_default默认表空间中的所有对象迁移至secondry表空间,命令如下:
ALTER TABLESPACE name MOVE { ALL | TABLES | INDEXES | MATERIALIZED VIEWS } [ OWNED BY role_name [, ...] ] TO new_tablespace [ NOWAIT ]
alter tablespace pg_default MOVE ALL TO secondry;
注:在迁移过程中所涉及的database和表会被锁定。

参考链接
http://php5.kiev.ua/pgsql/sql-altertablespace.html

ALTER TABLESPACE -- change the definition of a tablespace or affect objects of a tablespace
SYNOPSIS

ALTER TABLESPACE name RENAME TO new_name
ALTER TABLESPACE name OWNER TO new_owner
ALTER TABLESPACE name SET ( tablespace_option = value [, ... ] )
ALTER TABLESPACE name RESET ( tablespace_option [, ... ] )
ALTER TABLESPACE name MOVE { ALL | TABLES | INDEXES | MATERIALIZED VIEWS } [ OWNED BY role_name [, ...] ] TO new_tablespace [ NOWAIT ]
DESCRIPTION
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值