Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions deployment/Compose/compose/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ services:
volumes:
# Mounts the container's internal data directory to a named volume
- ${SYS_DATA_DIR}/postgres:/var/lib/postgresql
# Mounts initialization scripts to refresh collations on container restart
- ./setup/init-db:/docker-entrypoint-initdb.d
Comment thread
coderabbitai[bot] marked this conversation as resolved.
# Custom entrypoint wrapper to execute init scripts on restart, not just init
- ./setup/docker-entrypoint.sh:/usr/local/bin/custom-entrypoint.sh
entrypoint: ["/usr/local/bin/custom-entrypoint.sh"]
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
Expand Down
19 changes: 19 additions & 0 deletions deployment/Compose/compose/setup/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -e

# Custom entrypoint wrapper to run collation refresh before standard postgres entrypoint
# This ensures the refresh runs on container restart, not just initial setup

# Run our custom initialization scripts
if [ -f /var/lib/postgresql/data/PG_VERSION ]; then
echo "Running custom initialization tasks..."
for script in /docker-entrypoint-initdb.d/*; do
if [ -f "$script" ] && [ -x "$script" ] && [[ "$script" == *.sh ]]; then
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

⚓ Line 11 blocks the refresh script from ever running in this Compose setup.

Ye guard uses -x, but deployment/Compose/compose/compose.yaml Line 43 bind-mounts host files as-is, and deployment/Compose/compose/setup/init-db/01-refresh-collations.sh is non-executable in the provided context. Result: the collation fix is silently skipped.

Proposed patch
-        if [ -f "$script" ] && [ -x "$script" ] && [[ "$script" == *.sh ]]; then
+        if [ -f "$script" ] && [[ "$script" == *.sh ]]; then
             echo "Running $script"
-            "$script"
+            bash "$script"
         fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if [ -f "$script" ] && [ -x "$script" ] && [[ "$script" == *.sh ]]; then
if [ -f "$script" ] && [[ "$script" == *.sh ]]; then
echo "Running $script"
bash "$script"
fi

echo "Running $script"
"$script"
fi
done
fi
Comment on lines +8 to +16
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🚨 Line 8-16 runs DB maintenance before Postgres is up, so psql-based init scripts can fail hard.

The wrapper executes scripts before Line 19 delegates to the official entrypoint. But deployment/Compose/compose/setup/init-db/01-refresh-collations.sh runs psql; without a running server, that path can fail under set -e and break startup.

Safer direction
-# Run our custom initialization scripts
-if [ -f /var/lib/postgresql/data/PG_VERSION ]; then
-    echo "Running custom initialization tasks..."
-    for script in /docker-entrypoint-initdb.d/*; do
-        if [ -f "$script" ] && [ -x "$script" ] && [[ "$script" == *.sh ]]; then
-            echo "Running $script"
-            "$script"
-        fi
-    done
-fi
-
-# Invoke the original postgres entrypoint
-exec /usr/local/bin/docker-entrypoint.sh "$@"
+# NOTE: DB-dependent scripts must run only after postgres is accepting connections.
+# Consider moving collation refresh to:
+# 1) a post-start one-shot job (after healthcheck), or
+# 2) logic that explicitly starts/stops a temporary server before running scripts.
+exec /usr/local/bin/docker-entrypoint.sh "$@"


# Invoke the original postgres entrypoint
exec /usr/local/bin/docker-entrypoint.sh "$@"
25 changes: 25 additions & 0 deletions deployment/Compose/compose/setup/init-db/01-refresh-collations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
set -e

# Refresh collation versions for all non-template databases
# This fixes collation mismatches when glibc library is updated during PostgreSQL minor version upgrades
# Only run if not a new init (i.e., on container restart/upgrade)
if [ -f /var/lib/postgresql/data/PG_VERSION ]; then
echo "Checking and refreshing collation versions..."

# Get list of databases that need collation refresh
psql -d postgres -t -c "SELECT datname FROM pg_database WHERE datname NOT IN ('template0','template1') AND EXISTS (SELECT 1 FROM pg_collation WHERE collversion IS NOT NULL AND collversion <> pg_collation_actual_version(oid))" > /tmp/needs_refresh.txt 2>&1 || { echo "Warning: Could not check collation versions"; true; }

# Run REFRESH for each DB that needs it
while read -r db; do
# Skip empty lines
if [ -z "$(echo "$db" | xargs)" ]; then
continue
fi
db=$(echo "$db" | xargs) # Trim whitespace
echo "Refreshing collation for database: $db"
psql -d "$db" -c "ALTER DATABASE \"$db\" REFRESH COLLATION VERSION"
done < /tmp/needs_refresh.txt
Comment thread
coderabbitai[bot] marked this conversation as resolved.

rm -f /tmp/needs_refresh.txt
fi