April 10th, 2012 by depesz | Tags: , , , , , , , | 2 comments »
Did it help? If yes - maybe you can help me?

On 6th of April, Simon Riggs committed patch:

Add DROP INDEX CONCURRENTLY [IF EXISTS], uses ShareUpdateExclusiveLock

The log message is pretty laconic, so let's see what it actually means.

Before I can show you new functionality, we need to see and understand what was happening before.

For my tests, I have this table:

$ \d plans
                       Table "public.plans"
    ColumnType           │       Modifiers
───────────────┼──────────────────────────┼────────────────────────
 id            │ text                     │ not null
 plan          │ text                     │ not null
 entered_on    │ timestamp with time zone │ not null default now()
 is_public     │ booleannot null default true
 is_anonymized │ booleannot null default false
 title         │ text                     │
Indexes:
    "plans_pkey" PRIMARY KEY, btree (id)
    "zz" btree (entered_on)

Dropping the index is simple, but let's see what lock it needs:

$ begin;
BEGIN
 
*$ drop index zz;
DROP INDEX
 
*$ select mode, relation::regclass from pg_locks where pid = pg_backend_pid() and locktype = 'relation';
        mode         │            relation
─────────────────────┼─────────────────────────────────
 AccessShareLock     │ pg_locks
 AccessShareLock     │ pg_attribute_relid_attnum_index
 AccessShareLock     │ pg_attribute_relid_attnam_index
 AccessShareLock     │ pg_attribute
 AccessShareLock     │ pg_class_relname_nsp_index
 AccessShareLock     │ pg_class_oid_index
 AccessShareLock     │ pg_namespace_oid_index
 AccessShareLock     │ pg_namespace_nspname_index
 AccessShareLock     │ pg_namespace
 AccessShareLock     │ pg_class
 AccessExclusiveLock │ plans
 AccessExclusiveLock │ 18771
(12 rows)

Important part is the AccessExclusiveLock on plans. It means that whenever you're dropping index, you need to get full exclusive lock on a table. And this can, and will, cause problems in production environments.

Of course – the dropping is usually fast, but getting the lock itself can be long, and this would cause all other accesses to block.

Now, however, we have the “CONCURRENT" drop of indexes. Let's see how it works. Since concurrent drops cannot be in transaction, I will have to somehow “stop it" from finishing to be able to see the locks. That's easy:

psql1 $ begin;
BEGIN
 
psql1 *$ select * from plans limit 1;
  id │ plan │ entered_on │ is_public │ is_anonymized │ title
 ────┼──────┼────────────┼───────────┼───────────────┼───────
 (0 rows)
 
psql2 $ drop index concurrently zz;
(this hangs)
 
psql1 *$ select mode, relation::regclass from pg_locks where "pid of psql2" = pid and locktype = 'relation';
           mode           │ relation
──────────────────────────┼──────────
 ShareUpdateExclusiveLock │ plans
 ShareUpdateExclusiveLock │ zz
(2 rows)
 
psql1 *$ commit;
COMMIT
 
psql2 - finishes DROP INDEX

Just as commit promised – we're seeing new lock level – ShareUpdateExclusiveLock. What exactly it prevents? Documentation says:

Conflicts with the SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, and ACCESS EXCLUSIVE lock modes. This mode protects a table against concurrent schema changes and VACUUM runs.
Acquired by VACUUM (without FULL), ANALYZE, CREATE INDEX CONCURRENTLY, and some forms of ALTER TABLE.

Which means that it conflicts only with:

  • ALTER TABLE (some due to Share Update Exclusive Lock, and some due to Access Exclusive Lock)
  • ANALYZE
  • CLUSTER
  • CREATE INDEX CONCURRENTLY
  • CREATE INDEX without CONCURRENTLY (because of Share Lock)
  • DROP TABLE
  • REINDEX
  • TRUNCATE
  • VACUUM FULL
  • VACUUM (without FULL)

The biggest point is that now it doesn't conflict with normal queries – like UPDATE, DELETE, INSERT or SELECTs. And that's a very good thing.

  1. 2 comments

  2. # Valentine Gogichashvili
    May 11, 2012

    Now it is time actually for somebody to implement REINDEX CONCURRENTLY… as all the missing parts are already there! Cool 🙂

  3. May 29, 2012

    Hi,from the document http://www.postgresql.org/docs/9.2/static/explicit-locking.html

    we can see “SHARE UPDATE EXCLUSIVE” session will not block “ACCESS SHARE”, but in your test it did.
    From your test ,we now “drop index concurrently ” will acquire “SHARE UPDATE EXCLUSIVE”,but “select statement ” will acquire “ACCESS SHARE”

Leave a comment