首页 > 数据库技术 > 详细

PostgreSQL主键膨胀的使用CONCURRENTLY维护

时间:2015-08-05 10:47:44      阅读:186      评论:0      收藏:0      [点我收藏+]

1. 维护前

postgres=# \d+ test
                                                        Table
"public.test"
  Column |            Type             |                     Modifiers
| Storage | Stats target | Description
--------+-----------------------------+----------------------------------------------------+---------+--------------+-------------
  id     | integer                     | not null default
nextval(‘test_id_seq‘::regclass) | plain   |              |
  val    | timestamp without time zone | default now()
| plain   |              |
Indexes:
     "test_pkey" PRIMARY KEY, btree (id)

2. 创建新的unique index

postgres=# CREATE UNIQUE INDEX CONCURRENTLY ON test USING btree(id);
CREATE INDEX

3. 替换主键

postgres=# BEGIN;
BEGIN
postgres=# ALTER TABLE test DROP CONSTRAINT test_pkey;
ALTER TABLE
postgres=# ALTER TABLE test ADD CONSTRAINT test_id_idx PRIMARY KEY USING
INDEX test_id_idx;
ALTER TABLE
postgres=# COMMIT;
COMMIT

4. 维护后

postgres=# \d+ test
                                                        Table
"public.test"
  Column |            Type             |                     Modifiers
| Storage | Stats target | Description
--------+-----------------------------+----------------------------------------------------+---------+--------------+-------------
  id     | integer                     | not null default
nextval(‘test_id_seq‘::regclass) | plain   |              |
  val    | timestamp without time zone | default now()
| plain   |              |
Indexes:
     "test_id_idx" PRIMARY KEY, btree (id)

这样的好处是,可以在白天负载低的时候维护主键


PostgreSQL主键膨胀的使用CONCURRENTLY维护

原文:http://my.oschina.net/u/2426299/blog/487913

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!