Skip to content

Commit fdf405c

Browse files
authored
Merge pull request vitabaks#80 from vitabaks/dev
custom WAL location (Issue vitabaks#75)
2 parents 54ed666 + ccd0bc9 commit fdf405c

File tree

6 files changed

+142
-17
lines changed

6 files changed

+142
-17
lines changed
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
# yamllint disable rule:line-length
3+
# yamllint disable rule:comments-indentation
4+
5+
- name: "Make sure {{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }} is symlink" # noqa 204
6+
stat:
7+
path: "{{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}"
8+
register: sym
9+
10+
- block: # synchronize WAL`s if wal dir exist (and is not symlink)
11+
- name: Make sure rsync is installed (for synchronize wal dir)
12+
package:
13+
name:
14+
- rsync
15+
- sshpass
16+
state: present
17+
environment: "{{ proxy_env | default({}) }}"
18+
19+
- name: Stop patroni service (for create symlink)
20+
systemd:
21+
name: patroni
22+
state: stopped
23+
register: patroni_stop_result
24+
25+
- name: Make sure PostgreSQL is stopped
26+
become: true
27+
become_user: postgres
28+
command: "{{ postgresql_bin_dir }}/pg_ctl status -D {{ postgresql_data_dir }}"
29+
register: stop_result
30+
changed_when: false
31+
failed_when: false
32+
until: stop_result.rc == 3
33+
retries: 100
34+
delay: 6
35+
36+
- name: "Synchronize {{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }} to {{ postgresql_wal_dir }}" # noqa 204
37+
become: true
38+
become_user: postgres
39+
synchronize:
40+
src: "{{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}/"
41+
dest: "{{ postgresql_wal_dir }}/"
42+
delegate_to: "{{ inventory_hostname }}"
43+
44+
- name: "Rename {{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }} to {{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}_old" # noqa 204
45+
command: mv {{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }} {{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}_old # noqa 204
46+
register: mv_result
47+
when: sym.stat.exists and not sym.stat.islnk|bool
48+
49+
- name: "Create symlink {{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }} -> {{ postgresql_wal_dir }}" # noqa 204
50+
become: true
51+
become_user: postgres
52+
file:
53+
src: "{{ postgresql_wal_dir }}"
54+
dest: "{{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}"
55+
state: link
56+
when: sym.stat.islnk is not defined or not sym.stat.islnk|bool
57+
58+
- block: # start patroni
59+
- name: Start patroni service
60+
systemd:
61+
name: patroni
62+
state: started
63+
64+
- name: Wait for port 8008 to become open on the host
65+
wait_for:
66+
port: 8008
67+
host: "{{ hostvars[inventory_hostname]['inventory_hostname'] }}"
68+
state: started
69+
timeout: 120
70+
delay: 10
71+
ignore_errors: false
72+
73+
- name: Check that the patroni is healthy
74+
uri:
75+
url: "http://{{ hostvars[inventory_hostname]['inventory_hostname'] }}:8008/health"
76+
status_code: 200
77+
register: replica_result
78+
until: replica_result.status == 200
79+
retries: 120
80+
delay: 10
81+
when: patroni_stop_result is changed
82+
83+
- name: "Remove {{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}_old directory"
84+
file:
85+
path: "{{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}_old"
86+
state: absent
87+
when:
88+
- replica_result.status is defined
89+
- replica_result.status == 200
90+
91+
...

roles/patroni/tasks/main.yml

+43-15
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@
342342
mode: 0700
343343
tags: patroni
344344

345+
- name: Prepare PostgreSQL | make sure the custom WAL directory "{{ postgresql_wal_dir }}" exists
346+
file:
347+
path: "{{ postgresql_wal_dir }}"
348+
owner: postgres
349+
group: postgres
350+
state: directory
351+
mode: 0700
352+
when: postgresql_wal_dir is defined and postgresql_wal_dir | length > 0
353+
tags: patroni, custom_wal_dir
354+
345355
- block: # when postgresql exists (master)
346356
- name: Prepare PostgreSQL | check that data directory "{{ postgresql_data_dir }}" is initialized on Master
347357
stat:
@@ -521,7 +531,7 @@
521531
postgresql_packages|join(" ") is not search("postgrespro")) and
522532
not postgresql_conf_file.stat.exists
523533

524-
- name: Prepare PostgreSQL | make sure the data directory "{{ postgresql_data_dir }}" is empty on Master
534+
- name: Prepare PostgreSQL | make sure the data directory "{{ postgresql_data_dir }}" is empty
525535
file:
526536
path: "{{ postgresql_data_dir }}"
527537
state: "{{ item }}"
@@ -531,7 +541,19 @@
531541
loop:
532542
- absent
533543
- directory
534-
when: is_master == "true" and
544+
when: patroni_cluster_bootstrap_method != "pgbackrest" # --delta restore
545+
546+
- name: Prepare PostgreSQL | make sure the custom WAL directory "{{ postgresql_wal_dir }}" is empty
547+
file:
548+
path: "{{ postgresql_wal_dir }}"
549+
state: "{{ item }}"
550+
owner: postgres
551+
group: postgres
552+
mode: 0700
553+
loop:
554+
- absent
555+
- directory
556+
when: (postgresql_wal_dir is defined and postgresql_wal_dir | length > 0) and
535557
patroni_cluster_bootstrap_method != "pgbackrest" # --delta restore
536558
when: postgresql_exists != "true"
537559
tags: patroni, point_in_time_recovery
@@ -710,6 +732,7 @@
710732
group: postgres
711733
mode: 0640
712734
when: patroni_cluster_bootstrap_method != "initdb" and
735+
postgresql_conf_dir != postgresql_data_dir and
713736
(existing_pgcluster is not defined or not existing_pgcluster|bool)
714737
tags: patroni
715738

@@ -752,13 +775,24 @@
752775
tags: patroni, patroni_start_master, point_in_time_recovery
753776

754777
- block: # pg_hba (using a templates/pg_hba.conf.j2)
755-
- name: Prepare PostgreSQL | generate pg_hba.conf
778+
- name: Prepare PostgreSQL | generate pg_hba.conf on Master
756779
template:
757780
src: templates/pg_hba.conf.j2
758781
dest: "{{ postgresql_conf_dir }}/pg_hba.conf"
759782
owner: postgres
760783
group: postgres
761784
mode: 0640
785+
when: is_master == "true"
786+
787+
- name: Prepare PostgreSQL | generate pg_hba.conf on Replica
788+
template:
789+
src: templates/pg_hba.conf.j2
790+
dest: "{{ postgresql_conf_dir }}/pg_hba.conf"
791+
owner: postgres
792+
group: postgres
793+
mode: 0640
794+
when: is_master != "true" and
795+
postgresql_conf_dir != postgresql_data_dir
762796

763797
- name: Prepare PostgreSQL | reload for apply the pg_hba.conf
764798
become: true
@@ -837,21 +871,10 @@
837871
group: postgres
838872
mode: 0640
839873
when: existing_pgcluster is defined and existing_pgcluster|bool
874+
and postgresql_conf_dir != postgresql_data_dir
840875
tags: patroni, pg_hba, pg_hba_generate
841876

842877
- block: # start patroni on replica
843-
- name: Prepare PostgreSQL | make sure the data directory "{{ postgresql_data_dir }}" is empty on Replica
844-
file:
845-
path: "{{ postgresql_data_dir }}"
846-
state: "{{ item }}"
847-
owner: postgres
848-
group: postgres
849-
mode: 0700
850-
loop:
851-
- absent
852-
- directory
853-
when: not 'pgbackrest' in patroni_create_replica_methods # --delta restore
854-
855878
- name: Start patroni service on Replica servers
856879
systemd:
857880
daemon_reload: true
@@ -879,6 +902,11 @@
879902
when: is_master != "true"
880903
tags: patroni, patroni_start_replica, point_in_time_recovery
881904

905+
# create symlink pg_xlog/pg_wal to custom WAL dir
906+
- import_tasks: custom_wal_dir.yml
907+
when: postgresql_wal_dir is defined and postgresql_wal_dir | length > 0
908+
tags: patroni, custom_wal_dir
909+
882910
# disable postgresql from autostart
883911
- block: # "Debian"
884912
- name: Turning off postgresql autostart from config "start.conf" (will be managed by patroni)

roles/patroni/templates/patroni.yml.j2

+3
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ postgresql:
148148
{% for item in basebackup %}
149149
{{ item.option }}: '{{ item.value }}'
150150
{% endfor %}
151+
{% if postgresql_version is version('10', '>=') and postgresql_wal_dir is defined and postgresql_wal_dir | length > 0 %}
152+
waldir: {{ postgresql_wal_dir }}
153+
{% endif %}
151154
{% endif %}
152155
{% else %}
153156
- basebackup

tags.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- - patroni_start_master
3939
- - patroni_start_replica
4040
- - postgresql_disable
41+
- - custom_wal_dir
4142
- pgbouncer
4243
- - pgbouncer_install
4344
- - pgbouncer_conf

vars/Debian.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
# PostgreSQL variables
55
postgresql_cluster_name: "main"
6-
postgresql_data_dir: "/var/lib/postgresql/{{ postgresql_version }}/{{ postgresql_cluster_name }}"
6+
postgresql_data_dir: "/var/lib/postgresql/{{ postgresql_version }}/{{ postgresql_cluster_name }}" # You can specify custom data dir path
7+
postgresql_wal_dir: "" # custom WAL dir path (symlink will be created) [optional]
78
postgresql_conf_dir: "/etc/postgresql/{{ postgresql_version }}/{{ postgresql_cluster_name }}"
89
postgresql_bin_dir: "/usr/lib/postgresql/{{ postgresql_version }}/bin"
910
postgresql_log_dir: "/var/log/postgresql"

vars/RedHat.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
# yamllint disable rule:line-length
33

44
# PostgreSQL variables
5-
postgresql_data_dir: "/var/lib/pgsql/{{ postgresql_version }}/data"
5+
postgresql_data_dir: "/var/lib/pgsql/{{ postgresql_version }}/data" # You can specify custom data dir path
6+
postgresql_wal_dir: "" # custom WAL dir path (symlink will be created) [optional]
67
postgresql_conf_dir: "{{ postgresql_data_dir }}"
78
postgresql_bin_dir: "/usr/pgsql-{{ postgresql_version }}/bin"
89
postgresql_log_dir: "/var/log/postgresql"

0 commit comments

Comments
 (0)