Skip to content

Commit

Permalink
upgrade: compatibility with multiple pgbouncers
Browse files Browse the repository at this point in the history
  • Loading branch information
vitabaks committed Oct 21, 2023
1 parent 29c7319 commit 6ea2140
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
1 change: 1 addition & 0 deletions molecule/pg_upgrade/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
consul_node_role: server # if dcs_type: "consul"
consul_bootstrap_expect: true # if dcs_type: "consul"
postgresql_version: "14" # redefine the version to install for the upgrade test
pgbouncer_processes: 1 # Test multiple pgbouncer processes (so_reuseport)
cacheable: true

- name: Set variables for custom PostgreSQL data and WAL directory test
Expand Down
4 changes: 2 additions & 2 deletions roles/pgbouncer/templates/pgbouncer.ini.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

[pgbouncer]
logfile = {{ pgbouncer_log_dir }}/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }}.log
pidfile = /run/pgbouncer/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }}.pid
pidfile = /run/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }}/pgbouncer.pid
listen_addr = {{ pgbouncer_listen_addr | default('0.0.0.0') }}
listen_port = {{ pgbouncer_listen_port | default(6432) }}
unix_socket_dir = /run/pgbouncer/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }}
unix_socket_dir = /var/run/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }}
auth_type = {{ pgbouncer_auth_type }}
{% if pgbouncer_auth_user | bool %}
auth_user = {{ pgbouncer_auth_username }}
Expand Down
6 changes: 3 additions & 3 deletions roles/pgbouncer/templates/pgbouncer.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ User=postgres
Group=postgres

PermissionsStartOnly=true
ExecStartPre=-/bin/mkdir -p /run/pgbouncer/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }} {{ pgbouncer_log_dir }}
ExecStartPre=/bin/chown -R postgres:postgres /run/pgbouncer {{ pgbouncer_log_dir }}
ExecStartPre=-/bin/mkdir -p /run/pgbouncer /var/run/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }} {{ pgbouncer_log_dir }}
ExecStartPre=/bin/chown -R postgres:postgres /run/pgbouncer /var/run/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }} {{ pgbouncer_log_dir }}
{% if ansible_os_family == "Debian" %}
ExecStart=/usr/sbin/pgbouncer -d {{ pgbouncer_conf_dir }}/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }}.ini
{% endif %}
{% if ansible_os_family == "RedHat" %}
ExecStart=/usr/bin/pgbouncer -d {{ pgbouncer_conf_dir }}/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }}.ini
{% endif %}
ExecReload=/bin/kill -SIGHUP $MAINPID
PIDFile=/run/pgbouncer/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }}.pid
PIDFile=/run/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }}/pgbouncer.pid
Restart=on-failure

LimitNOFILE=100000
Expand Down
17 changes: 16 additions & 1 deletion roles/upgrade/tasks/pgbouncer_pause.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@
and state <> 'idle'
and query_start < clock_timestamp() - interval '{{ pg_slow_active_query_treshold_to_terminate }} ms'
{{ "and backend_type = 'client backend'" if pg_old_version is version('10', '>=') else '' }}
# Depending on the number of PgBouncer processes, it either uses a local connection or the socket paths for each PgBouncer process.
pgbouncer_pause_command: >-
{% if pgbouncer_processes | default(1) | int == 1 %}
bash -c 'PGPASSWORD={{ patroni_superuser_password }} psql -h localhost -p {{ pgbouncer_listen_port }} -U {{ patroni_superuser_username }} -d pgbouncer -tAXc "PAUSE"'
{% else %}
{% set unix_socket_dir = [] %}
{% for i in range(0, pgbouncer_processes | default(1) | int) %}
{% if i == 0 %}
{{ unix_socket_dir.append('/var/run/pgbouncer') }}
{% else %}
{{ unix_socket_dir.append('/var/run/pgbouncer-' + i | string) }}
{% endif %}
{% endfor %}
echo -e "{{ unix_socket_dir | join('\n') }}" | xargs -I {} -P {{ pgbouncer_processes | default(1) | int }} bash -c 'PGPASSWORD={{ patroni_superuser_password }} psql -h {} -p {{ pgbouncer_listen_port }} -U {{ patroni_superuser_username }} -d pgbouncer -tAXc "PAUSE"'
{% endif %}
ansible.builtin.shell: |
set -o pipefail;
Expand All @@ -46,7 +61,7 @@
# it is assumed that pgbouncer is installed on database servers
pgb_servers="$pg_servers"
pgb_count="$pg_count"
pgb_pause_command="psql -h localhost -p {{ pgbouncer_listen_port }} -U {{ patroni_superuser_username }} -d pgbouncer -tAXc \"PAUSE\""
pgb_pause_command="{{ pgbouncer_pause_command }}"
pgb_resume_command='kill -SIGUSR2 $(pidof pgbouncer)'
start_time=$(date +%s)
Expand Down
30 changes: 28 additions & 2 deletions roles/upgrade/tasks/pre_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,38 @@
# PgBouncer (if 'pgbouncer_pool_pause' is 'true')
# test access via localhost to be able to perform 'PAUSE' command
- name: '[Pre-Check] Test PgBouncer access via localhost'
ansible.builtin.command: >
psql -h localhost -p {{ pgbouncer_listen_port }} -U {{ patroni_superuser_username }} -d pgbouncer -tAXc "SHOW POOLS"
ansible.builtin.command: >-
psql -h localhost
-p {{ pgbouncer_listen_port }}
-U {{ patroni_superuser_username }}
-d pgbouncer
-tAXc "SHOW POOLS"
changed_when: false
environment:
PGPASSWORD: "{{ patroni_superuser_password }}"
when:
- pgbouncer_install | bool
- pgbouncer_pool_pause | bool
- pgbouncer_processes | default(1) | int == 1

- name: '[Pre-Check] Test PgBouncer access via unix socket'
ansible.builtin.command: >-
psql -h /var/run/pgbouncer{{ '-%d' % (idx + 1) if idx > 0 else '' }}
-p {{ pgbouncer_listen_port }}
-U {{ patroni_superuser_username }}
-d pgbouncer
-tAXc "SHOW POOLS"
loop: "{{ range(0, (pgbouncer_processes | default(1) | int)) | list }}"
loop_control:
index_var: idx
label: "{{ 'pgbouncer' if idx == 0 else 'pgbouncer-%d' % (idx + 1) }}"
changed_when: false
environment:
PGPASSWORD: "{{ patroni_superuser_password }}"
when:
- pgbouncer_install | bool
- pgbouncer_pool_pause | bool
- pgbouncer_processes | default(1) | int > 1

# Check the VIP address
- name: Make sure that the cluster ip address (VIP) "{{ cluster_vip }}" is running
Expand Down

0 comments on commit 6ea2140

Please sign in to comment.