Skip to content

Commit

Permalink
Move task "Wait for the analyze to complete" to Post-Upgrade tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
vitabaks committed Mar 20, 2024
1 parent faf2535 commit b3de38c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
14 changes: 7 additions & 7 deletions roles/upgrade/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,8 @@ Please see the variable file vars/[upgrade.yml](../../vars/upgrade.yml)

#### 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)
- Note: Uses parallel processes equal to 50% of CPU cores ('`vacuumdb_parallel_jobs`' variable)
- Note: 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.
- **Update extensions in each database**
- Get list of installed PostgreSQL extensions
- Get list of old PostgreSQL extensions
Expand Down Expand Up @@ -320,18 +318,20 @@ Please see the variable file vars/[upgrade.yml](../../vars/upgrade.yml)
- Notes: if 'pg_new_wal_dir' is defined
- **Remove old PostgreSQL packages**
- Notes: if 'pg_old_packages_remove' is 'true'
- **Remove temporary local access rule from pg_hba.conf**
- Notes: if it has been changed
- Update the PostgreSQL configuration
- **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
- **Wait for the analyze to complete.**
- Notes: max wait time: 1 hour ('`vacuumdb_analyze_timeout`' variable)
- **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
58 changes: 41 additions & 17 deletions roles/upgrade/tasks/post_upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,21 @@
- pg_old_packages_remove | bool
- ansible_os_family == "Debian"

# pgbackrest (local)
# Return the pg_hba.conf file to its original state (if it has been changed)
- block:
- name: Remove temporary local access rule from pg_hba.conf
ansible.builtin.blockinfile:
path: "{{ pg_new_confdir }}/pg_hba.conf"
marker: "# {mark} ANSIBLE TEMPORARY pg_upgrade RULE"
state: absent

- name: Update the PostgreSQL configuration
ansible.builtin.command: "{{ pg_new_bindir }}/pg_ctl reload -D {{ pg_new_datadir }}"
when:
- socket_access_result.stderr is defined
- "'no pg_hba.conf entry' in socket_access_result.stderr"

# pgBackRest (local)
- block:
- name: pgbackrest | Check pg-path option
ansible.builtin.command: "grep -c '^pg.*-path=' {{ pgbackrest_conf_file }}"
Expand Down Expand Up @@ -99,7 +113,7 @@
- pgbackrest_install | bool
- pgbackrest_repo_host | length < 1

# pgbackrest (dedicated)
# pgBackRest (dedicated)
- block:
- name: pgbackrest | Check pg-path option
delegate_to: "{{ groups['pgbackrest'][0] }}"
Expand Down Expand Up @@ -151,6 +165,31 @@
ignore_errors: true
when: wal_g_install | bool

# Wait for the analyze to complete
- name: "Collecting statistics in progress. Wait for the analyze to complete."
ansible.builtin.async_status:
jid: "{{ vacuumdb_analyze.ansible_job_id }}"
register: vacuumdb_analyze_job_result
until: vacuumdb_analyze_job_result.finished
retries: "{{ (vacuumdb_analyze_timeout | int) // 10 }}" # max wait time
delay: 10
ignore_errors: true # ignore errors if the task runs for over an vacuumdb_analyze_timeout
when:
- vacuumdb_analyze is defined
- vacuumdb_analyze.ansible_job_id is defined

- name: "Stop pg_terminator script"
ansible.builtin.shell: |
pid=$(cat /tmp/pg_terminator.pid)
ps -p $pid > /dev/null 2>&1 && kill -9 $pid
args:
executable: /bin/bash
ignore_errors: true
when:
- pg_terminator_analyze is defined
- pg_terminator_analyze is changed

# finish (info)
- name: Check the Patroni cluster state
run_once: true
become: true
Expand All @@ -171,21 +210,6 @@
changed_when: false
when: inventory_hostname in groups['primary']

# Return the pg_hba.conf file to its original state (if it has been changed)
- block:
- name: Remove temporary local access rule from pg_hba.conf
ansible.builtin.blockinfile:
path: "{{ pg_new_confdir }}/pg_hba.conf"
marker: "# {mark} ANSIBLE TEMPORARY pg_upgrade RULE"
state: absent

- name: Update the PostgreSQL configuration
ansible.builtin.command: "{{ pg_new_bindir }}/pg_ctl reload -D {{ pg_new_datadir }}"
when:
- socket_access_result.stderr is defined
- "'no pg_hba.conf entry' in socket_access_result.stderr"

# finish (info)
- name: List the Patroni cluster members
run_once: true
ansible.builtin.debug:
Expand Down
18 changes: 0 additions & 18 deletions roles/upgrade/tasks/statistics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,6 @@
poll: 0
register: vacuumdb_analyze
ignore_errors: true # ignore errors if the task runs for over an 'vacuumdb_analyze_timeout'.

- name: "Collecting statistics in progress. Wait for the analyze to complete."
ansible.builtin.async_status:
jid: "{{ vacuumdb_analyze.ansible_job_id }}"
register: vacuumdb_analyze_job_result
until: vacuumdb_analyze_job_result.finished
retries: "{{ (vacuumdb_analyze_timeout | int) // 10 }}" # max wait time
delay: 10
ignore_errors: true # ignore errors if the task runs for over an vacuumdb_analyze_timeout

- name: "Stop pg_terminator script"
ansible.builtin.shell: |
pid=$(cat /tmp/pg_terminator.pid)
ps -p $pid > /dev/null 2>&1 && kill -9 $pid
args:
executable: /bin/bash
ignore_errors: true
when: pg_terminator_analyze is changed
when: inventory_hostname in groups['primary']

...

0 comments on commit b3de38c

Please sign in to comment.