Skip to content

Commit

Permalink
Analyze a PostgreSQL database (optimizer statistics) immediately afte…
Browse files Browse the repository at this point in the history
…r the upgrade
  • Loading branch information
vitabaks committed Mar 20, 2024
1 parent 661d8c5 commit faf2535
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 74 deletions.
42 changes: 12 additions & 30 deletions pg_upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -283,7 +267,5 @@
tags:
- upgrade
- post-upgrade
- analyze
- statistics

...
83 changes: 39 additions & 44 deletions roles/upgrade/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit faf2535

Please sign in to comment.