From faf2535f15f3450ef5213b806a1a1c4498017a52 Mon Sep 17 00:00:00 2001 From: Vitaliy Kukharik Date: Wed, 20 Mar 2024 11:50:01 +0000 Subject: [PATCH] Analyze a PostgreSQL database (optimizer statistics) immediately after the upgrade --- pg_upgrade.yml | 42 ++++++--------------- roles/upgrade/README.md | 83 +++++++++++++++++++---------------------- 2 files changed, 51 insertions(+), 74 deletions(-) diff --git a/pg_upgrade.yml b/pg_upgrade.yml index bcd2dc23e..584862c3d 100644 --- a/pg_upgrade.yml +++ b/pg_upgrade.yml @@ -56,7 +56,7 @@ tags: - always -- name: "(1/7) PRE-UPGRADE: Perform Pre-Checks" +- name: "(1/6) PRE-UPGRADE: Perform Pre-Checks" hosts: 'primary:secondary' gather_facts: false become: true @@ -79,7 +79,7 @@ - upgrade - pre-checks -- name: "(2/7) PRE-UPGRADE: Install new PostgreSQL packages" +- name: "(2/6) PRE-UPGRADE: Install new PostgreSQL packages" hosts: 'primary:secondary' gather_facts: false become: true @@ -103,7 +103,7 @@ - upgrade-check - packages -- name: "(3/7) PRE-UPGRADE: Initialize new db, schema compatibility check, and pg_upgrade --check" +- name: "(3/6) PRE-UPGRADE: Initialize new db, schema compatibility check, and pg_upgrade --check" hosts: 'primary:secondary' gather_facts: false become: true @@ -143,7 +143,7 @@ - upgrade-check - schema-compatibility-check -- name: "(4/7) PRE-UPGRADE: Prepare the Patroni configuration" +- name: "(4/6) PRE-UPGRADE: Prepare the Patroni configuration" hosts: 'primary:secondary' gather_facts: false become: true @@ -165,7 +165,7 @@ - upgrade - update-config -- name: "(5/7) UPGRADE: Upgrade PostgreSQL" +- name: "(5/6) UPGRADE: Upgrade PostgreSQL" hosts: 'primary:secondary' gather_facts: false become: true @@ -228,7 +228,7 @@ tags: - upgrade -- name: "(6/7) POST-UPGRADE: Perform Post-Checks and Update extensions" +- name: "(6/6) POST-UPGRADE: Analyze a PostgreSQL database (update optimizer statistics) and Post-Upgrade tasks" hosts: 'primary:secondary' gather_facts: false become: true @@ -242,39 +242,23 @@ - name: Include upgrade variables ansible.builtin.include_vars: "vars/upgrade.yml" tasks: - - name: Running Post-Checks + - name: Analyze database ansible.builtin.include_role: name: upgrade - tasks_from: post_checks + tasks_from: statistics + tags: analyze, statistics - name: Update extensions ansible.builtin.include_role: name: upgrade tasks_from: extensions when: update_extensions | bool - tags: - - upgrade - - post-checks - - update-extensions + tags: update_extensions -- name: "(7/7) POST-UPGRADE: Analyze a PostgreSQL database (update optimizer statistics) and Post-Upgrade tasks" - hosts: 'primary:secondary' - gather_facts: false - become: true - become_user: postgres - any_errors_fatal: true - pre_tasks: - - name: Include main variables - ansible.builtin.include_vars: "vars/main.yml" - - name: Include OS-specific variables - ansible.builtin.include_vars: "vars/{{ ansible_os_family }}.yml" - - name: Include upgrade variables - ansible.builtin.include_vars: "vars/upgrade.yml" - tasks: - - name: Analyze database + - name: Running Post-Checks ansible.builtin.include_role: name: upgrade - tasks_from: statistics + tasks_from: post_checks - name: Running Post-Upgrade tasks ansible.builtin.include_role: @@ -283,7 +267,5 @@ tags: - upgrade - post-upgrade - - analyze - - statistics ... diff --git a/roles/upgrade/README.md b/roles/upgrade/README.md index db70850f0..0fd09f4df 100644 --- a/roles/upgrade/README.md +++ b/roles/upgrade/README.md @@ -285,20 +285,12 @@ Please see the variable file vars/[upgrade.yml](../../vars/upgrade.yml) - Start vip-manager service - Make sure that the cluster ip address (VIP) is running -#### 6. POST-UPGRADE: Perform Post-Checks and Update extensions -- **Make sure that physical replication is active** - - if no active replication connections found, print error message: - - "No active replication connections found. Please check the replication status and PostgreSQL logs." -- **Create a table "test_replication" with 10000 rows on the Primary** -- **Wait until the PostgreSQL replica is synchronized** - - Notes: max wait time: 2 minutes -- **Drop a table "test_replication"** -- **Print the result of checking the number of records** - - if the number of rows match, print info message: - - "The PostgreSQL Replication is OK. The number of records in the 'test_replication' table the same as the Primary." - - if the number of rows does not match, print error message: - - "The number of records in the 'test_replication' table does not match the Primary. Please check the replication status and PostgreSQL logs." -- **Get a list of databases** +#### 6. POST-UPGRADE: Analyze a PostgreSQL database (update optimizer statistics) and Post-Upgrade tasks +- **Run vacuumdb to analyze the PostgreSQL databases** + - Notes: Uses parallel processes equal to 50% of CPU cores ('`vacuumdb_parallel_jobs`' variable) + - Notes: Before collecting statistics, the 'pg_terminator' script is launched to monitor and terminate any 'ANALYZE' blockers. Once statistics collection is complete, the script is stopped. + - Wait for the analyze to complete. + - Notes: max wait time: 1 hour ('`vacuumdb_analyze_timeout`' variable) - **Update extensions in each database** - Get list of installed PostgreSQL extensions - Get list of old PostgreSQL extensions @@ -310,33 +302,36 @@ Please see the variable file vars/[upgrade.yml](../../vars/upgrade.yml) - Notes: if pg_repack is installed - Notes: if there are no old extensions, print message: - "The extension versions are up-to-date for the database. No update is required." - -#### 7. POST-UPGRADE: Analyze a PostgreSQL database (update optimizer statistics) and Post-Upgrade tasks -- **Run vacuumdb to analyze the PostgreSQL databases** - - Notes: Uses parallel processes equal to CPU cores ('`vacuumdb_parallel_jobs`' variable) - - Notes: Before collecting statistics, the 'pg_terminator' script is launched to monitor and terminate any 'ANALYZE' blockers. Once statistics collection is complete, the script is stopped. - - Wait for the analyze to complete. - - Notes: max wait time: 1 hour ('`vacuumdb_analyze_timeout`' variable) -- **Ensure the current data directory is the new data directory** - - Notes: to prevent deletion the old directory if it is used -- **Delete the old PostgreSQL data directory** - - Notes: perform pg_dropcluster for Debian based -- **Delete the old PostgreSQL WAL directory** - - Notes: if 'pg_new_wal_dir' is defined -- **Remove old PostgreSQL packages** - - Notes: if 'pg_old_packages_remove' is 'true' -- **pgBackRest** (if 'pgbackrest_install' is 'true') - - Check pg-path option - - Update pg-path in pgbackrest.conf - - Upgrade stanza -- **WAL-G** (if 'wal_g_install' is 'true') - - Update PostgreSQL data directory path in .walg.json - - Update PostgreSQL data directory path in cron jobs -- **Check the Patroni cluster state** -- **Check the current PostgreSQL version** -- **Remove temporary local access rule from pg_hba.conf** - - Notes: if it has been changed - - Update the PostgreSQL configuration -- **Print info messages** - - List the Patroni cluster members - - Upgrade completed +- **Perform Post-Checks** + - Make sure that physical replication is active + - Note: if no active replication connections found, print error message: "No active replication connections found. Please check the replication status and PostgreSQL logs." + - Create a table "test_replication" with 10000 rows on the Primary + - Wait until the PostgreSQL replica is synchronized (max wait time: 2 minutes) + - Drop a table "test_replication" + - Print the result of checking the number of records + - if the number of rows match, print info message: "The PostgreSQL Replication is OK. The number of records in the 'test_replication' table the same as the Primary." + - if the number of rows does not match, print error message: "The number of records in the 'test_replication' table does not match the Primary. Please check the replication status and PostgreSQL logs." +- **Perform Post-Upgrade tasks** + - **Ensure the current data directory is the new data directory** + - Notes: to prevent deletion the old directory if it is used + - **Delete the old PostgreSQL data directory** + - Notes: perform pg_dropcluster for Debian based + - **Delete the old PostgreSQL WAL directory** + - Notes: if 'pg_new_wal_dir' is defined + - **Remove old PostgreSQL packages** + - Notes: if 'pg_old_packages_remove' is 'true' + - **pgBackRest** (if 'pgbackrest_install' is 'true') + - Check pg-path option + - Update pg-path in pgbackrest.conf + - Upgrade stanza + - **WAL-G** (if 'wal_g_install' is 'true') + - Update PostgreSQL data directory path in .walg.json + - Update PostgreSQL data directory path in cron jobs + - **Check the Patroni cluster state** + - **Check the current PostgreSQL version** + - **Remove temporary local access rule from pg_hba.conf** + - Notes: if it has been changed + - Update the PostgreSQL configuration + - **Print info messages** + - List the Patroni cluster members + - Upgrade completed