Skip to content

Commit

Permalink
Add support for postgres replication slot (#31)
Browse files Browse the repository at this point in the history
Signed-off-by: souravbiswassanto <[email protected]>
  • Loading branch information
souravbiswassanto authored Mar 15, 2024
1 parent 06ba93a commit 4472844
Show file tree
Hide file tree
Showing 36 changed files with 508 additions and 65 deletions.
9 changes: 7 additions & 2 deletions role_scripts/10/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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

echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf

#echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
Expand Down
10 changes: 8 additions & 2 deletions role_scripts/10/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ touch /tmp/postgresql.conf
echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf
#echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf

echo "archive_mode = always" >>/tmp/postgresql.conf
Expand Down
33 changes: 31 additions & 2 deletions role_scripts/10/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ while true; do
sleep 2
done

if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
while true; do
echo "Create replication slot on primary"
if [[ "${SSL:-0}" == "ON" ]]; then
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)
else
output=$(psql -h "$PRIMARY_HOST" --username=postgres --no-password --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
fi
# check if current pod became leader itself

if [[ $output == *"(1 row)"* || $output == *"already exists"* ]]; then
break
fi

if [[ -e "/run_scripts/tmp/pg-failover-trigger" ]]; then
echo "Postgres promotion trigger_file found. Running primary run script"
/run_scripts/role/run.sh
fi
sleep 2
done
fi


if [[ ! -e "$PGDATA/PG_VERSION" ]]; then
echo "take base basebackup..."
# get basebackup
Expand All @@ -91,14 +115,19 @@ touch /tmp/postgresql.conf
echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
#echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi

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

echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

echo "max_replication_slots = 90" >>/tmp/postgresql.conf
if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
fi
Expand Down
9 changes: 7 additions & 2 deletions role_scripts/10/standby/warm_stanby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ touch /tmp/postgresql.conf
echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf

#echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf

echo "archive_mode = always" >>/tmp/postgresql.conf
Expand Down
9 changes: 7 additions & 2 deletions role_scripts/11/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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

echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf

# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf
echo "archive_mode = always" >>/tmp/postgresql.conf

Expand Down
9 changes: 7 additions & 2 deletions role_scripts/11/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ touch /tmp/postgresql.conf
echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf

# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf

echo "archive_mode = always" >>/tmp/postgresql.conf
Expand Down
32 changes: 30 additions & 2 deletions role_scripts/11/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ while true; do
sleep 2
done

if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
while true; do
echo "Create replication slot on primary"
if [[ "${SSL:-0}" == "ON" ]]; then
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)
else
output=$(psql -h "$PRIMARY_HOST" --username=postgres --no-password --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
fi
# check if current pod became leader itself

if [[ $output == *"(1 row)"* || $output == *"already exists"* ]]; then
break
fi

if [[ -e "/run_scripts/tmp/pg-failover-trigger" ]]; then
echo "Postgres promotion trigger_file found. Running primary run script"
/run_scripts/role/run.sh
fi
sleep 2
done
fi

if [[ ! -e "$PGDATA/PG_VERSION" ]]; then
echo "take base basebackup..."
# get basebackup
Expand All @@ -91,8 +114,13 @@ touch /tmp/postgresql.conf
echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf

# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf

echo "archive_mode = always" >>/tmp/postgresql.conf
Expand Down
9 changes: 7 additions & 2 deletions role_scripts/11/standby/warm_stanby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ touch /tmp/postgresql.conf
echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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
echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf

# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf

echo "archive_mode = always" >>/tmp/postgresql.conf
Expand Down
9 changes: 7 additions & 2 deletions role_scripts/12/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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

echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf

# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf

# 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.
Expand Down
12 changes: 11 additions & 1 deletion role_scripts/12/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,18 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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

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

if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
echo "primary_slot_name = "$CLEAN_HOSTNAME"" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf

# 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.
Expand Down
37 changes: 36 additions & 1 deletion role_scripts/12/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ while true; do
sleep 2
done

if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
while true; do
echo "Create replication slot on primary"
if [[ "${SSL:-0}" == "ON" ]]; then
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)
else
output=$(psql -h "$PRIMARY_HOST" --username=postgres --no-password --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
fi
# check if current pod became leader itself

if [[ $output == *"(1 row)"* || $output == *"already exists"* ]]; then
break
fi

if [[ -e "/run_scripts/tmp/pg-failover-trigger" ]]; then
echo "Postgres promotion trigger_file found. Running primary run script"
/run_scripts/role/run.sh
fi
sleep 2
done
fi


if [[ ! -e "$PGDATA/PG_VERSION" ]]; then
echo "take base basebackup..."
mkdir -p "$PGDATA"
Expand All @@ -86,7 +110,18 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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

echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
echo "primary_slot_name = "$CLEAN_HOSTNAME"" >>/tmp/postgresql.conf
fi


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

Expand Down
12 changes: 11 additions & 1 deletion role_scripts/12/standby/warm_stanby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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

echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
# echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
echo "primary_slot_name = "$CLEAN_HOSTNAME"" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf

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

Expand Down
9 changes: 7 additions & 2 deletions role_scripts/13/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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

echo "wal_keep_size = 1024" >>/tmp/postgresql.conf

# echo "wal_keep_size = 1024" >>/tmp/postgresql.conf
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf

# 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.
Expand Down
12 changes: 11 additions & 1 deletion role_scripts/13/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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

echo "wal_keep_size = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
# echo "wal_keep_size = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_size = 1024" >>/tmp/postgresql.conf
fi
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
echo "primary_slot_name = "$CLEAN_HOSTNAME"" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf

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

Expand Down
37 changes: 35 additions & 2 deletions role_scripts/13/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ while true; do
sleep 2
done

if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
while true; do
echo "Create replication slot on primary"
if [[ "${SSL:-0}" == "ON" ]]; then
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)
else
output=$(psql -h "$PRIMARY_HOST" --username=postgres --no-password --command="SELECT pg_create_physical_replication_slot('${CLEAN_HOSTNAME}', true);" 2>&1 || true)
fi
# check if current pod became leader itself

if [[ $output == *"(1 row)"* || $output == *"already exists"* ]]; then
break
fi

if [[ -e "/run_scripts/tmp/pg-failover-trigger" ]]; then
echo "Postgres promotion trigger_file found. Running primary run script"
/run_scripts/role/run.sh
fi
sleep 2
done
fi


if [[ ! -e "$PGDATA/PG_VERSION" ]]; then
echo "take base basebackup..."
mkdir -p "$PGDATA"
Expand All @@ -86,8 +110,17 @@ echo "wal_level = replica" >>/tmp/postgresql.conf
echo "shared_buffers = $SHARED_BUFFERS" >>/tmp/postgresql.conf
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

echo "wal_keep_size = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13

# echo "wal_keep_size = 1024" >>/tmp/postgresql.conf #it was "wal_keep_segments" in earlier version. changed in version 13
if [ ! -z "${WAL_RETAIN_PARAM:-}" ] && [ ! -z "${WAL_RETAIN_AMOUNT:-}" ]; then
echo "${WAL_RETAIN_PARAM}=${WAL_RETAIN_AMOUNT}" >>/tmp/postgresql.conf
else
echo "wal_keep_size = 1024" >>/tmp/postgresql.conf
fi
if [[ "$WAL_LIMIT_POLICY" == "ReplicationSlot" ]]; then
CLEAN_HOSTNAME="${HOSTNAME//[^[:alnum:]]/}"
echo "primary_slot_name = "$CLEAN_HOSTNAME"" >>/tmp/postgresql.conf
fi
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf

# 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.
Expand Down
Loading

0 comments on commit 4472844

Please sign in to comment.