To implement an online schema change in PostgreSQL 15, tools like pg_repack
or logical replication can be used. Here's a real-life example using pg_repack
:
Scenario: You need to add an index to a large, heavily used table customer_data
.
pg_repack
:
pg_repack
extension on your PostgreSQL server.pg_repack
:
Execute pg_repack
to add the index while allowing normal operations on the table:
pg_repack -d mydatabase --table=customer_data --create-index="CREATE INDEX idx_customer_name ON customer_data(name)"
In this example, pg_repack
reorganizes the customer_data
table to add the idx_customer_name
index, minimizing locks and allowing the table to remain online and accessible during the process.