Skip to content

Commit 4472844

Browse files
Add support for postgres replication slot (#31)
Signed-off-by: souravbiswassanto <[email protected]>
1 parent 06ba93a commit 4472844

36 files changed

+508
-65
lines changed

role_scripts/10/primary/start.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
2020
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
2121
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
2222

23-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
24-
23+
#echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
24+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
25+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
26+
else
27+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
28+
fi
29+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
2530
echo "wal_log_hints = on" >>/tmp/postgresql.conf
2631
echo "archive_mode = always" >>/tmp/postgresql.conf
2732
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf

role_scripts/10/standby/ha_backup_job.sh

+8-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,15 @@ touch /tmp/postgresql.conf
7777
echo "wal_level = replica" >>/tmp/postgresql.conf
7878
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
7979
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
80-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
81-
echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf
80+
#echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
81+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
82+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
83+
else
84+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
85+
fi
8286

87+
echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf
88+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
8389
echo "wal_log_hints = on" >>/tmp/postgresql.conf
8490

8591
echo "archive_mode = always" >>/tmp/postgresql.conf

role_scripts/10/standby/run.sh

+31-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ while true; do
6666
sleep 2
6767
done
6868

69+
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
70+
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
71+
while true; do
72+
echo "Create replication slot on primary"
73+
if [[ "${SSL:-0}" == "ON" ]]; then
74+
output=$(psql -h "$PRIMARY_HOST" --username=postgres "sslmode=$SSL_MODE sslrootcert=/tls/certs/client/ca.crt sslcert=/tls/certs/client/client.crt sslkey=/tls/certs/client/client.key" --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
75+
else
76+
output=$(psql -h "$PRIMARY_HOST" --username=postgres --no-password --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
77+
fi
78+
# check if current pod became leader itself
79+
80+
if [[ $output == *"(1 row)"* || $output == *"already exists"* ]]; then
81+
break
82+
fi
83+
84+
if [[ -e "/run_scripts/tmp/pg-failover-trigger" ]]; then
85+
echo "Postgres promotion trigger_file found. Running primary run script"
86+
/run_scripts/role/run.sh
87+
fi
88+
sleep 2
89+
done
90+
fi
91+
92+
6993
if [[ ! -e "$PGDATA/PG_VERSION" ]]; then
7094
echo "take base basebackup..."
7195
# get basebackup
@@ -91,14 +115,19 @@ touch /tmp/postgresql.conf
91115
echo "wal_level = replica" >>/tmp/postgresql.conf
92116
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
93117
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
94-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
118+
#echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
119+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
120+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
121+
else
122+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
123+
fi
95124

96125
echo "wal_log_hints = on" >>/tmp/postgresql.conf
97126

98127
echo "archive_mode = always" >>/tmp/postgresql.conf
99128
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
100129
echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf
101-
130+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
102131
if [ "$STANDBY" == "hot" ]; then
103132
echo "hot_standby = on" >>/tmp/postgresql.conf
104133
fi

role_scripts/10/standby/warm_stanby.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ touch /tmp/postgresql.conf
2222
echo "wal_level = replica" >>/tmp/postgresql.conf
2323
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
2424
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
25-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
26-
25+
#echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
26+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
27+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
28+
else
29+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
30+
fi
31+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
2732
echo "wal_log_hints = on" >>/tmp/postgresql.conf
2833

2934
echo "archive_mode = always" >>/tmp/postgresql.conf

role_scripts/11/primary/start.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
2020
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
2121
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
2222

23-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
24-
23+
# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
24+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
25+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
26+
else
27+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
28+
fi
29+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
2530
echo "wal_log_hints = on" >>/tmp/postgresql.conf
2631
echo "archive_mode = always" >>/tmp/postgresql.conf
2732

role_scripts/11/standby/ha_backup_job.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ touch /tmp/postgresql.conf
7777
echo "wal_level = replica" >>/tmp/postgresql.conf
7878
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
7979
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
80-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
81-
80+
# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
81+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
82+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
83+
else
84+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
85+
fi
86+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
8287
echo "wal_log_hints = on" >>/tmp/postgresql.conf
8388

8489
echo "archive_mode = always" >>/tmp/postgresql.conf

role_scripts/11/standby/run.sh

+30-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,29 @@ while true; do
6666
sleep 2
6767
done
6868

69+
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
70+
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
71+
while true; do
72+
echo "Create replication slot on primary"
73+
if [[ "${SSL:-0}" == "ON" ]]; then
74+
output=$(psql -h "$PRIMARY_HOST" --username=postgres "sslmode=$SSL_MODE sslrootcert=/tls/certs/client/ca.crt sslcert=/tls/certs/client/client.crt sslkey=/tls/certs/client/client.key" --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
75+
else
76+
output=$(psql -h "$PRIMARY_HOST" --username=postgres --no-password --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
77+
fi
78+
# check if current pod became leader itself
79+
80+
if [[ $output == *"(1 row)"* || $output == *"already exists"* ]]; then
81+
break
82+
fi
83+
84+
if [[ -e "/run_scripts/tmp/pg-failover-trigger" ]]; then
85+
echo "Postgres promotion trigger_file found. Running primary run script"
86+
/run_scripts/role/run.sh
87+
fi
88+
sleep 2
89+
done
90+
fi
91+
6992
if [[ ! -e "$PGDATA/PG_VERSION" ]]; then
7093
echo "take base basebackup..."
7194
# get basebackup
@@ -91,8 +114,13 @@ touch /tmp/postgresql.conf
91114
echo "wal_level = replica" >>/tmp/postgresql.conf
92115
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
93116
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
94-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
95-
117+
# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
118+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
119+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
120+
else
121+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
122+
fi
123+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
96124
echo "wal_log_hints = on" >>/tmp/postgresql.conf
97125

98126
echo "archive_mode = always" >>/tmp/postgresql.conf

role_scripts/11/standby/warm_stanby.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ touch /tmp/postgresql.conf
2222
echo "wal_level = replica" >>/tmp/postgresql.conf
2323
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
2424
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
25-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
26-
25+
# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
26+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
27+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
28+
else
29+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
30+
fi
31+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
2732
echo "wal_log_hints = on" >>/tmp/postgresql.conf
2833

2934
echo "archive_mode = always" >>/tmp/postgresql.conf

role_scripts/12/primary/start.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
2020
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
2121
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
2222

23-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
24-
23+
# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
24+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
25+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
26+
else
27+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
28+
fi
29+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
2530
echo "wal_log_hints = on" >>/tmp/postgresql.conf
2631

2732
# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.

role_scripts/12/standby/ha_backup_job.sh

+11-1
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,18 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
7272
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
7373
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
7474

75-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
75+
#echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
76+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
77+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
78+
else
79+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
80+
fi
7681

82+
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
83+
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
84+
echo "primary_slot_name = "$CLEAN_HOSTNAME"" >>/tmp/postgresql.conf
85+
fi
86+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
7787
echo "wal_log_hints = on" >>/tmp/postgresql.conf
7888

7989
# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.

role_scripts/12/standby/run.sh

+36-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ while true; do
6666
sleep 2
6767
done
6868

69+
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
70+
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
71+
while true; do
72+
echo "Create replication slot on primary"
73+
if [[ "${SSL:-0}" == "ON" ]]; then
74+
output=$(psql -h "$PRIMARY_HOST" --username=postgres "sslmode=$SSL_MODE sslrootcert=/tls/certs/client/ca.crt sslcert=/tls/certs/client/client.crt sslkey=/tls/certs/client/client.key" --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
75+
else
76+
output=$(psql -h "$PRIMARY_HOST" --username=postgres --no-password --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
77+
fi
78+
# check if current pod became leader itself
79+
80+
if [[ $output == *"(1 row)"* || $output == *"already exists"* ]]; then
81+
break
82+
fi
83+
84+
if [[ -e "/run_scripts/tmp/pg-failover-trigger" ]]; then
85+
echo "Postgres promotion trigger_file found. Running primary run script"
86+
/run_scripts/role/run.sh
87+
fi
88+
sleep 2
89+
done
90+
fi
91+
92+
6993
if [[ ! -e "$PGDATA/PG_VERSION" ]]; then
7094
echo "take base basebackup..."
7195
mkdir -p "$PGDATA"
@@ -86,7 +110,18 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
86110
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
87111
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
88112

89-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
113+
# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
114+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
115+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
116+
else
117+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
118+
fi
119+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
120+
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
121+
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
122+
echo "primary_slot_name = "$CLEAN_HOSTNAME"" >>/tmp/postgresql.conf
123+
fi
124+
90125

91126
echo "wal_log_hints = on" >>/tmp/postgresql.conf
92127

role_scripts/12/standby/warm_stanby.sh

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
2020
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
2121
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
2222

23-
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
23+
# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
24+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
25+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
26+
else
27+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
28+
fi
29+
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
30+
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
31+
echo "primary_slot_name = "$CLEAN_HOSTNAME"" >>/tmp/postgresql.conf
32+
fi
33+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
2434

2535
echo "wal_log_hints = on" >>/tmp/postgresql.conf
2636

role_scripts/13/primary/start.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
2020
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
2121
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
2222

23-
echo "wal_keep_size = 1024" >>/tmp/postgresql.conf
24-
23+
# echo "wal_keep_size = 1024" >>/tmp/postgresql.conf
24+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
25+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
26+
else
27+
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
28+
fi
29+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
2530
echo "wal_log_hints = on" >>/tmp/postgresql.conf
2631

2732
# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.

role_scripts/13/standby/ha_backup_job.sh

+11-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,17 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
7272
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
7373
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
7474

75-
echo "wal_keep_size = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
75+
# echo "wal_keep_size = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
76+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
77+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
78+
else
79+
echo "wal_keep_size = 1024" >>/tmp/postgresql.conf
80+
fi
81+
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
82+
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
83+
echo "primary_slot_name = "$CLEAN_HOSTNAME"" >>/tmp/postgresql.conf
84+
fi
85+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
7686

7787
echo "wal_log_hints = on" >>/tmp/postgresql.conf
7888

role_scripts/13/standby/run.sh

+35-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ while true; do
6666
sleep 2
6767
done
6868

69+
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
70+
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
71+
while true; do
72+
echo "Create replication slot on primary"
73+
if [[ "${SSL:-0}" == "ON" ]]; then
74+
output=$(psql -h "$PRIMARY_HOST" --username=postgres "sslmode=$SSL_MODE sslrootcert=/tls/certs/client/ca.crt sslcert=/tls/certs/client/client.crt sslkey=/tls/certs/client/client.key" --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
75+
else
76+
output=$(psql -h "$PRIMARY_HOST" --username=postgres --no-password --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
77+
fi
78+
# check if current pod became leader itself
79+
80+
if [[ $output == *"(1 row)"* || $output == *"already exists"* ]]; then
81+
break
82+
fi
83+
84+
if [[ -e "/run_scripts/tmp/pg-failover-trigger" ]]; then
85+
echo "Postgres promotion trigger_file found. Running primary run script"
86+
/run_scripts/role/run.sh
87+
fi
88+
sleep 2
89+
done
90+
fi
91+
92+
6993
if [[ ! -e "$PGDATA/PG_VERSION" ]]; then
7094
echo "take base basebackup..."
7195
mkdir -p "$PGDATA"
@@ -86,8 +110,17 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
86110
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
87111
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
88112

89-
echo "wal_keep_size = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
90-
113+
# echo "wal_keep_size = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
114+
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
115+
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
116+
else
117+
echo "wal_keep_size = 1024" >>/tmp/postgresql.conf
118+
fi
119+
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
120+
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
121+
echo "primary_slot_name = "$CLEAN_HOSTNAME"" >>/tmp/postgresql.conf
122+
fi
123+
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
91124
echo "wal_log_hints = on" >>/tmp/postgresql.conf
92125

93126
# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.

0 commit comments

Comments
 (0)