Index creation concurrently

From CarlosCaPe/octorato · View interactive version

Index creation concurrently is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Index Creation (CONCURRENTLY)

How to use this skill

Skill file

Index Creation (CONCURRENTLY)

What

CREATE INDEX CONCURRENTLY builds an index without holding an exclusive lock

on the table. Regular CREATE INDEX blocks all writes (INSERT/UPDATE/DELETE)

for the duration of the build. The CONCURRENTLY variant allows writes to

continue while the index is being built.

Why

On production tables with active traffic, a regular index build can cause

downtime. A 100M-row table might take minutes to index, during which all

writes are blocked. CONCURRENTLY eliminates this risk at the cost of a

slightly longer build time.

Lock comparison (PG 16 docs: explicit-locking.html)

| Mode | Lock Acquired | Blocks Writes? | Blocks Reads? |

|------|---------------|----------------|---------------|

| CREATE INDEX | SHARE | Yes (INSERT/UPDATE/DELETE blocked) | No |

| CREATE INDEX CONCURRENTLY | SHARE UPDATE EXCLUSIVE | No | No |

How

Basic concurrent index

CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_appointmentinfo_hospitalid
    ON public."AppointmentInfo" ("HospitalId");

Covering index (INCLUDE)

CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_vwr_animalid_covering
    ON analytics."events" ("entity_id")
    INCLUDE ("AnimalName", "Species", "Breed");

Important: Cannot run inside a transaction

-- This FAILS:
BEGIN;
CREATE INDEX CONCURRENTLY ix_test ON public."MyTable" ("MyCol");
COMMIT;

-- This WORKS (outside any transaction block):
CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_test
    ON public."MyTable" ("MyCol");

Index Types We Used

| Type | Purpose | Example |

|------|---------|---------|

| B-tree (default) | Equality and range queries | WHERE "HospitalId" = 123 |

| Covering (INCLUDE) | Avoid table lookups for common queries | Index-only scans |

| Composite | Multi-column lookups | WHERE "Col1" = x AND "Col2" = y |

When to Use

Where We Used It

References

-- see "Indexing strategy (actual usage)" and Backlog #5 (Advanced indexes)

-- and index decisions

Gotchas

```sql

SELECT indexrelid::regclass, indisvalid

FROM pg_index

WHERE NOT indisvalid;

```


Category: DDL | Origin: ,

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.