pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to rebuild index efficiently
5+ messages / 5 participants
[nested] [flat]

* How to rebuild index efficiently
@ 2020-08-03 17:20  Konireddy Rajashekar <rajkonireddy@gmail.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Konireddy Rajashekar @ 2020-08-03 17:20 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi Team,

i have a table of size 2.6TB which is very prone to updates and inserts so
we have tuned autovacuum to run on it very aggressively , so the table
level bloat is fine .
Now we are facing issue with indexes on this table. the total size  of all
indexes  on this table is around 2.4TB.

There is an unique index of 1.2TB size out of which 850 GB is bloat ,
creating another index concurrently is taking lot of time and we cannot
offer down time to reindex this index.

Could you please suggest any ideal approach to tackle this ?

Postgres version:9.6.15

postgres=> \dt+ cust_table
                         List of relations
 Schema |      Name       | Type  | Owner  |  Size   | Description
--------+-----------------+-------+--------+---------+-------------
 public | cust_table | table | raj | 2685 GB |
(1 row)


Index bloat:

database_name | schema_name |          table_name           |
index_name            | bloat_pct | bloat_mb |  index_mb   |  table_mb   |
index_scans
---------------+-------------+-------------------------------+---------------------------------+-----------+----------+-------------+-------------+-------------
 postgres         | public      | cust_table              |
unique_event_type_action_id     |        69 |   884477 | 1285743.648 |
2749094.070 |   342466359




postgres=> \di+ unique_event_type_action_id
                                        List of relations
 Schema |            Name             | Type  | Owner  |      Table      |
 Size   | Description
--------+-----------------------------+-------+--------+-----------------+---------+-------------
 public | unique_event_type_action_id | index | raj |cust_table | 1256 GB |


Regards,
Raj

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to rebuild index efficiently
@ 2020-08-03 17:58  Christophe Pettus <xof@thebuild.com>
  parent: Konireddy Rajashekar <rajkonireddy@gmail.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Christophe Pettus @ 2020-08-03 17:58 UTC (permalink / raw)
  To: Konireddy Rajashekar <rajkonireddy@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>



> On Aug 3, 2020, at 10:20, Konireddy Rajashekar <rajkonireddy@gmail.com> wrote:
> Could you please suggest any ideal approach to tackle this ?

You can do CREATE INDEX CONCURRENTLY to build a new index with the same definition, and when that is complete, drop the old index.  The locking that is required here is modest: CREATE INDEX CONCURRENTLY needs to lock the table briefly at a couple of points in the operation, and dropping the old index requires a brief lock on the table.  It is, however, much less overall lock time than REINDEX would be.

--
-- Christophe Pettus
   xof@thebuild.com






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to rebuild index efficiently
@ 2020-08-03 18:04  Ron <ronljohnsonjr@gmail.com>
  parent: Christophe Pettus <xof@thebuild.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Ron @ 2020-08-03 18:04 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 8/3/20 12:58 PM, Christophe Pettus wrote
>> On Aug 3, 2020, at 10:20, Konireddy Rajashekar <rajkonireddy@gmail.com> wrote:
>> Could you please suggest any ideal approach to tackle this ?
> You can do CREATE INDEX CONCURRENTLY to build a new index with the same definition, and when that is complete, drop the old index.  The locking that is required here is modest: CREATE INDEX CONCURRENTLY needs to lock the table briefly at a couple of points in the operation, and dropping the old index requires a brief lock on the table.  It is, however, much less overall lock time than REINDEX would be.

Of course, you need enough disk space... :)

-- 
Angular momentum makes the world go 'round.





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to rebuild index efficiently
@ 2020-08-03 18:14  Michael Lewis <mlewis@entrata.com>
  parent: Konireddy Rajashekar <rajkonireddy@gmail.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Michael Lewis @ 2020-08-03 18:14 UTC (permalink / raw)
  To: Konireddy Rajashekar <rajkonireddy@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

>
> creating another index concurrently is taking lot of time
>

Could you increase maintenance_work_mem significantly or is that already
quite high?

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to rebuild index efficiently
@ 2020-08-04 06:07  Michael Paquier <michael@paquier.xyz>
  parent: Ron <ronljohnsonjr@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Michael Paquier @ 2020-08-04 06:07 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

On Mon, Aug 03, 2020 at 01:04:45PM -0500, Ron wrote:
> same definition, and when that is complete, drop the old index.  The
> locking that is required here is modest: CREATE INDEX CONCURRENTLY
> needs to lock the table briefly at a couple of points in the
> operation, and dropping the old index requires a brief lock on the
> table.   It is, however, much less overall lock time than REINDEX would be.
> 
> Of course, you need enough disk space... :)

A SHARE UPDATE EXCLUSIVE lock is taken during a CIC, meaning that
writes and reads are allowed on the parent table while the operation
works, but no DDLs are allowed (roughly).  The operation takes a
couple of transactions to complete, and  there are two wait points
after building and validating the new index to make sure that there
are no transactions remaining around that may cause visiblity issues
once the new index is ready to use and becomes valid.  So the
operation is longer, takes more resources, but it has the advantage to
be non-disruptive.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2020-08-04 06:07 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-08-03 17:20 How to rebuild index efficiently Konireddy Rajashekar <rajkonireddy@gmail.com>
2020-08-03 17:58 ` Christophe Pettus <xof@thebuild.com>
2020-08-03 18:04   ` Ron <ronljohnsonjr@gmail.com>
2020-08-04 06:07     ` Michael Paquier <michael@paquier.xyz>
2020-08-03 18:14 ` Michael Lewis <mlewis@entrata.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox