Skip to content

Commit d2c587a

Browse files
authored
Increase startup times (#501)
* Increase startup times * fix single DB variable * remove duplicated function
1 parent 210e43d commit d2c587a

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

scripts/env-data.sh

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ fi
457457

458458
IFS=','
459459
read -a dbarr <<< "$POSTGRES_DBNAME"
460-
SINGLE_DB=${dbarr[0]}
461-
echo ${SINGLE_DB} > /tmp/pg_dbname.txt
460+
export SINGLE_DB=${dbarr[0]}
461+
462462

463463

464464
if [ -z "${TIMEZONE}" ]; then
@@ -644,31 +644,55 @@ function extension_install() {
644644
}
645645

646646
function directory_checker() {
647-
DATA_PATH=$1
648-
if [ -d "$DATA_PATH" ];then
647+
local DATA_PATH=$1
648+
if [ -d "$DATA_PATH" ]; then
649+
local DB_USER_PERM
650+
local DB_GRP_PERM
649651
DB_USER_PERM=$(stat -c '%U' "${DATA_PATH}")
650652
DB_GRP_PERM=$(stat -c '%G' "${DATA_PATH}")
651-
if [[ ${DB_USER_PERM} != "${USER}" ]] && [[ ${DB_GRP_PERM} != "${GROUP}" ]];then
652-
chown -R "${USER}":"${GROUP}" "${DATA_PATH}"
653+
654+
if [[ ${DB_USER_PERM} != "${USER}" ]] || [[ ${DB_GRP_PERM} != "${GROUP}" ]]; then
655+
chown -R "${USER}:${GROUP}" "${DATA_PATH}"
653656
fi
657+
else
658+
chown "${USER}:${GROUP}" "${DATA_PATH}"
654659
fi
655-
656660
}
661+
662+
663+
657664
function non_root_permission() {
658665
USER="$1"
659666
GROUP="$2"
660-
path_envs=("${DATADIR}" "${WAL_ARCHIVE}" "${SCRIPTS_LOCKFILE_DIR}" "${CONF_LOCKFILE_DIR}" "${EXTRA_CONF_DIR}" "${SSL_DIR}" "${POSTGRES_INITDB_WALDIR}")
661-
for dir_names in "${path_envs[@]}";do
662-
if [ ! -z "${dir_names}" ];then
663-
directory_checker "${dir_names}"
664-
fi
667+
668+
local START_GLOBAL=$(date +%s%N)
669+
670+
path_envs=(
671+
"${DATADIR}" "${WAL_ARCHIVE}" "${SCRIPTS_LOCKFILE_DIR}"
672+
"${CONF_LOCKFILE_DIR}" "${EXTRA_CONF_DIR}" "${SSL_DIR}" "${POSTGRES_INITDB_WALDIR}"
673+
)
674+
675+
for dir_name in "${path_envs[@]}"; do
676+
[[ -n "$dir_name" ]] && directory_checker "$dir_name"
665677
done
666-
services=("/usr/lib/postgresql/" "/etc/" "/var/log/postgresql" "/var/run/!(secrets)" "/var/lib/" "/usr/bin" "/tmp" "/scripts")
667-
for paths in "${services[@]}"; do
668-
directory_checker "${paths}"
678+
679+
services=(
680+
"/usr/lib/postgresql/${POSTGRES_MAJOR_VERSION}/bin" "/var/log/postgresql"
681+
"/var/run/postgresql" ${DATADIR} "/scripts" "/usr/share/postgresql/${POSTGRES_MAJOR_VERSION}"
682+
"${SSL_DIR}" "${WAL_ARCHIVE}" "${SCRIPTS_LOCKFILE_DIR}" "${CONF_LOCKFILE_DIR}" "${EXTRA_CONF_DIR}" "/etc/ssl"
683+
"/etc/postgresql/${POSTGRES_MAJOR_VERSION}/main" "/tmp/pg_*"
684+
)
685+
686+
for path in "${services[@]}"; do
687+
for expanded in $path; do
688+
directory_checker "$expanded"
689+
done
669690
done
670691
chmod -R 750 "${DATADIR}" ${WAL_ARCHIVE}
671692

693+
local END_GLOBAL=$(date +%s%N)
694+
local TOTAL_ELAPSED=$(( (END_GLOBAL - START_GLOBAL) / 1000000 ))
695+
echo -e "[Entrypoint] Total time spent in non_root_permission, changing ownership of directories: \e[1;31m ${TOTAL_ELAPSED} \e[1;31m ms \033[0m"
672696
}
673697

674698
function role_check() {

scripts/setup-database.sh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#!/usr/bin/env bash
22

33
source /scripts/env-data.sh
4-
SINGLE_DB=$(cat /tmp/pg_dbname.txt)
5-
6-
7-
84

95
INITDB_WALDIR_FLAG=""
106

@@ -75,10 +71,11 @@ EOF
7571
fi
7672
fi;
7773

78-
non_root_permission postgres postgres
74+
#non_root_permission postgres postgres
75+
7976
# Set proper permissions
8077
# needs to be done as root:
81-
chown -R postgres:postgres "${DATADIR}" "${WAL_ARCHIVE}"
78+
chown -R postgres:postgres "${DATADIR}" "${WAL_ARCHIVE}" "${SSL_DIR}"
8279
chmod -R 750 "${DATADIR}" "${WAL_ARCHIVE}"
8380

8481
# test database existing

0 commit comments

Comments
 (0)