From a6ba1f36650334d0f3dcc47e94af059783b93a95 Mon Sep 17 00:00:00 2001 From: Varjitt Jeeva Date: Tue, 5 Nov 2024 16:17:49 -0500 Subject: [PATCH] feat: run ANALYZE as part of create-indexes #611 --- docs/quickstart.md | 4 +++- pgbelt/cmd/schema.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/quickstart.md b/docs/quickstart.md index e9e7f73..34e0525 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -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. diff --git a/pgbelt/cmd/schema.py b/pgbelt/cmd/schema.py index 2190a8c..9e05311 100644 --- a/pgbelt/cmd/schema.py +++ b/pgbelt/cmd/schema.py @@ -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 @@ -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 @@ -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,