Skip to content

Commit

Permalink
feat: run ANALYZE as part of create-indexes #611
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeeva committed Nov 5, 2024
1 parent 267918c commit a6ba1f3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ To ensure the bulk COPY phase of the migration runs faster, indexes are not made
They need to be built and this process should be done before the cutover to not prolong your cutover window. You should run
this command during a period of low traffic.

Note that this command will create all the indexes in the target database, **and will run ANALYZE after** to ensure optimal performance.

$ belt create-indexes testdatacenter1 database1

## Step 3: Run ANALYZE on the target database before your application cutover
## Step 3: (Optional) Run ANALYZE on the target database before your application cutover

This is typically run some time before your application cutover, so the target database performs better with the dataset
once the application cuts over to the target database.
Expand Down
15 changes: 15 additions & 0 deletions pgbelt/cmd/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Awaitable
from asyncpg import create_pool

from pgbelt.cmd.helpers import run_with_configs
from pgbelt.config.models import DbupgradeConfig
Expand All @@ -11,6 +12,7 @@
from pgbelt.util.dump import remove_dst_not_valid_constraints
from pgbelt.util.dump import remove_dst_indexes
from pgbelt.util.logs import get_logger
from pgbelt.util.postgres import run_analyze


@run_with_configs
Expand Down Expand Up @@ -109,11 +111,24 @@ async def create_indexes(config_future: Awaitable[DbupgradeConfig]) -> None:
as the owner user. This must only be done after most data is synchronized
(at minimum after the initializing phase) from the source to the destination
database.
After creating indexes, the destination database should be analyzed to ensure
the query planner has the most up-to-date statistics for the indexes.
"""
conf = await config_future
logger = get_logger(conf.db, conf.dc, "schema.dst")
await create_target_indexes(conf, logger, during_sync=False)

# Run ANALYZE after creating indexes (without statement timeout)
async with create_pool(
conf.dst.root_uri,
min_size=1,
server_settings={
"statement_timeout": "0",
},
) as dst_pool:
await run_analyze(dst_pool, logger)


COMMANDS = [
dump_schema,
Expand Down

0 comments on commit a6ba1f3

Please sign in to comment.