How to avoid lock write data when creating an index in PostgresQL

Source: Internet
Author: User
The problem is caused by the table lock issue of a handsome guy indexing. First, we will introduce Postgresql INDEX creation syntax: Version: 9.1 CREATE [UNIQUE] INDEX [CONCURRENTLY] [name] ONtable [USINGmethod] ({column... "> <LINKhref ="

The problem is caused by the table lock issue of a handsome guy indexing. First, we will introduce the indexing syntax of Postgresql:
Version: 9.1


CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ name ] ON table [ USING method ]    ( { column | ( expression ) } [ COLLATE collation ] [ opclass ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] )    [ WITH ( storage_parameter = value [, ... ] ) ]    [ TABLESPACE tablespace ]    [ WHERE predicate ]

Here, we will not explain the use of many parameters in the syntax (sorting, usage, fill factor, etc.), but will mainly discuss the use cases of concurrently.

Under normal circumstances, when Postgresql creates a common btree index, the DML (insert, update, delete) operation is blocked until the index is complete, and the read operation is not blocked during this period. If there is only one user, this is of course okay, but in the production environment and high concurrency, especially in the case of index creation for large tables, this operation cannot be done. Otherwise, the user will jump up and swear, click a button for a day.

-- Use
Postgresql provides a parameter to avoid table lock due to data writing when indexing online. this parameter is called concurrently. It is easy to use, that is, use create index concurrently instead of create index.

-- Side effects
Of course, using this parameter has a side effect. if you do not use this parameter to create an index, the database only scans the table once. if you use this parameter, the database will scan the table twice, at the same time, wait for all transactions that may read the index to end. as a result, the CPU, I/O, and memory of the system will be slightly affected. Therefore, you can choose one by yourself instead of the default one.

-- Failed
When using the concurrently parameter to create an index, you may encounter failures. for example, if a unique index is created and the data is duplicated, or the user finds that the index is created with an error field, the index creation operation has been canceled. At this time, there will be an index in the table. this is because once the index creation command with this parameter is issued, an index record will be inserted in the system log table first, because the index fails to be created, it will be marked as an INVALID, as shown below:

postgres=# \d t_kenyon       Table "public.t_kenyon" Column |  Type   | Modifiers --------+---------+----------- col    | integer | Indexes:    "idx" btree (col) INVALID

-- Rebuilding
When the above two methods are used to reconstruct an invalid index, one is drop index index_name, and then execute create index concurrently. The reindex index_name command is executed, but the latter does not support the concurrent parameter.

-- Conclusion
It is best to include this parameter when executing the index creation command in production, because it consumes a little more system resources and time in exchange for uninterrupted access updates by users. If the index is rebuilt, you can create an index with the same name on the original base, and then cancel the old index.



Reference: http://www.postgresql.org/docs/9.1/static/sql-createindex.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.