How to create an index on a large table in production without blocking and exceeding timeouts in Kubernetes?
sobes.tech AI
Answer from AI
To create an index on a large table in production without locking and exceeding Kubernetes timeouts, you can use the following approaches:
- Creating a CONCURRENTLY index (PostgreSQL) — allows creating an index without locking read and write operations. Example:
CREATE INDEX CONCURRENTLY idx_column ON table(column);
-
Splitting the index creation into stages — if the DBMS does not support creating an index without locks, you can create a new table with the index, then switch traffic.
-
Using online indexing (e.g., in MySQL InnoDB) — the command
ALTER TABLE ... ALGORITHM=INPLACEallows creating an index without a full lock. -
Configuring timeouts and resources in Kubernetes — increase timeouts for migration-related operations to avoid interruptions.
-
Performing the operation during off-peak hours — minimizes load and the likelihood of timeouts.
-
Using migration tools that support online indexing (e.g., pt-online-schema-change for MySQL).
Thus, the key is to leverage DBMS capabilities for online indexing and properly configure the Kubernetes environment to avoid disruptions.