简体   繁体   English

在大表(600 万条记录)上创建主键,而不锁定 PostgreSQL 中的表

[英]Create a Primary Key on a large table (6Million records) without locking the table in PostgreSQL

I want to create a Primary Key on a table with 6 Million records, but when I execute this:我想在一个有 600 万条记录的表上创建一个主键,但是当我执行这个时:

ALTER TABLE schema_name.table_name ADD CONSTRAINT pkey_name PRIMARY KEY (field_pkey_name);

It's locking my table and alter table does not finish executing...它正在锁定我的表,而 alter table 没有完成执行......

Try to do it in two steps:尝试分两步进行:

CREATE UNIQUE INDEX CONCURRENTLY pkey_name
   ON schema_name.table_name (field_pkey_name);

ALTER TABLE schema_name.table_name
   ADD CONSTRAINT pkey_name PRIMARY KEY USING INDEX pkey_name;

It will still take a long time (even longer), but the table will not be locked.仍然需要很长时间(甚至更长),但表不会被锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM