|
|
|
|
@@ -9,7 +9,7 @@ set -e
|
|
|
|
|
CONFIG_HOME="/config"
|
|
|
|
|
PGDATA="${PGDATA:-/config/database}"
|
|
|
|
|
PG_VERSION_FILE="$PGDATA/pg_major_version"
|
|
|
|
|
VECTOR_VERSION_FILE="$PGDATA/pgvector_version"
|
|
|
|
|
VCHORD_VERSION_FILE="$PGDATA/vchord_version"
|
|
|
|
|
|
|
|
|
|
# Define current PostgreSQL major version
|
|
|
|
|
PG_MAJOR_VERSION="${PG_MAJOR:-15}"
|
|
|
|
|
@@ -46,11 +46,11 @@ cd /config || true
|
|
|
|
|
bashio::log.info "Starting PostgreSQL..."
|
|
|
|
|
|
|
|
|
|
if [ "$(bashio::info.arch)" = "armv7" ]; then
|
|
|
|
|
bashio::log.warning "ARMv7 detected: Starting without vectors.so"
|
|
|
|
|
bashio::log.warning "ARMv7 detected: Starting without extensions"
|
|
|
|
|
docker-entrypoint.sh postgres & true
|
|
|
|
|
exit 0
|
|
|
|
|
else
|
|
|
|
|
docker-entrypoint.sh postgres -c shared_preload_libraries=vectors.so -c search_path="public, vectors" & true
|
|
|
|
|
docker-entrypoint.sh postgres -c config_file=/etc/postgresql/postgresql.conf & true
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
###############################
|
|
|
|
|
@@ -94,9 +94,9 @@ update_postgres() {
|
|
|
|
|
export BACKUP_DIR="/config/backups"
|
|
|
|
|
export PSQL_VERSION="$PG_MAJOR_VERSION"
|
|
|
|
|
|
|
|
|
|
# Install binaries
|
|
|
|
|
# Install binaries
|
|
|
|
|
apt-get update &>/dev/null
|
|
|
|
|
install -y procps rsync postgresql-$PG_MAJOR_VERSION postgresql-$OLD_PG_VERSION &>/dev/null
|
|
|
|
|
install -y procps rsync postgresql-"$PG_MAJOR_VERSION" postgresql-"$OLD_PG_VERSION" &>/dev/null
|
|
|
|
|
|
|
|
|
|
# Download and run the upgrade script
|
|
|
|
|
TMP_SCRIPT=$(mktemp)
|
|
|
|
|
@@ -110,65 +110,66 @@ update_postgres() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#####################################
|
|
|
|
|
# Enable & Upgrade pgvector.rs #
|
|
|
|
|
# Enable & Upgrade vchord #
|
|
|
|
|
#####################################
|
|
|
|
|
|
|
|
|
|
# Function: Check if 'vectors' extension is enabled
|
|
|
|
|
check_vector_extension() {
|
|
|
|
|
bashio::log.info "Checking if 'vectors' extension is enabled..."
|
|
|
|
|
# Function: Check if 'vchord' extension is enabled
|
|
|
|
|
check_vchord_extension() {
|
|
|
|
|
bashio::log.info "Checking if 'vchord' extension is enabled..."
|
|
|
|
|
local result
|
|
|
|
|
result=$(psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" \
|
|
|
|
|
-tAc "SELECT extname FROM pg_extension WHERE extname = 'vectors';")
|
|
|
|
|
if [[ "$result" == "vectors" ]]; then
|
|
|
|
|
bashio::log.info "'vectors' extension is enabled."
|
|
|
|
|
-tAc "SELECT extname FROM pg_extension WHERE extname = 'vchord';")
|
|
|
|
|
if [[ "$result" == "vchord" ]]; then
|
|
|
|
|
bashio::log.info "'vchord' extension is enabled."
|
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
bashio::log.error "'vectors' extension is NOT enabled."
|
|
|
|
|
bashio::log.error "'vchord' extension is NOT enabled."
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Function: Enable (or re-create) 'vectors' extension
|
|
|
|
|
enable_vector_extension() {
|
|
|
|
|
bashio::log.info "Enabling 'vectors' extension..."
|
|
|
|
|
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" -c "DROP EXTENSION IF EXISTS vectors; CREATE EXTENSION vectors;" >/dev/null 2>&1
|
|
|
|
|
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" -c "ALTER EXTENSION vectors UPDATE; SELECT pgvectors_upgrade();" >/dev/null 2>&1
|
|
|
|
|
# Function: Enable (or re-create) 'vchord' extension
|
|
|
|
|
enable_vchord_extension() {
|
|
|
|
|
bashio::log.info "Enabling 'vchord' extension..."
|
|
|
|
|
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" -c "DROP EXTENSION IF EXISTS vchord;" >/dev/null 2>&1
|
|
|
|
|
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" -c "CREATE EXTENSION vchord;" >/dev/null 2>&1
|
|
|
|
|
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" -c "ALTER EXTENSION vchord UPDATE;" >/dev/null 2>&1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Function: Store the current pgvector.rs version in a file
|
|
|
|
|
store_vector_version() {
|
|
|
|
|
# Function: Store the current vchord version in a file
|
|
|
|
|
store_vchord_version() {
|
|
|
|
|
local version
|
|
|
|
|
version=$(psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" \
|
|
|
|
|
-tAc "SELECT extversion FROM pg_extension WHERE extname = 'vectors';")
|
|
|
|
|
echo "$version" > "$VECTOR_VERSION_FILE"
|
|
|
|
|
-tAc "SELECT extversion FROM pg_extension WHERE extname = 'vchord';")
|
|
|
|
|
echo "$version" > "$VCHORD_VERSION_FILE"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Function: Detect previous and new pgvector.rs versions, and upgrade if needed
|
|
|
|
|
upgrade_vector_extension() {
|
|
|
|
|
# Function: Detect previous and new vchord versions, and upgrade if needed
|
|
|
|
|
upgrade_vchord_extension() {
|
|
|
|
|
local current_version desired_version
|
|
|
|
|
current_version=$(cat "$VECTOR_VERSION_FILE" 2>/dev/null || echo "unknown")
|
|
|
|
|
current_version=$(cat "$VCHORD_VERSION_FILE" 2>/dev/null || echo "unknown")
|
|
|
|
|
desired_version=$(psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" \
|
|
|
|
|
-tAc "SELECT extversion FROM pg_extension WHERE extname = 'vectors';")
|
|
|
|
|
-tAc "SELECT extversion FROM pg_extension WHERE extname = 'vchord';")
|
|
|
|
|
|
|
|
|
|
if [[ "$current_version" != "$desired_version" ]]; then
|
|
|
|
|
bashio::log.warning "Upgrading 'vectors' extension from version $current_version → $desired_version..."
|
|
|
|
|
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" -c "ALTER EXTENSION vectors UPDATE;" >/dev/null 2>&1
|
|
|
|
|
bashio::log.warning "Upgrading 'vchord' extension from version $current_version → $desired_version..."
|
|
|
|
|
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME@$DB_PORT" -c "ALTER EXTENSION vchord UPDATE;" >/dev/null 2>&1
|
|
|
|
|
|
|
|
|
|
# Cleanup outdated indexes
|
|
|
|
|
bashio::log.info "Cleaning up outdated vector indexes..."
|
|
|
|
|
# Cleanup outdated indexes if needed (customize this line as needed for your DB schema)
|
|
|
|
|
bashio::log.info "Cleaning up outdated vector indexes (if any)..."
|
|
|
|
|
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" \
|
|
|
|
|
-c "DROP INDEX IF EXISTS clip_index;" >/dev/null 2>&1
|
|
|
|
|
|
|
|
|
|
# Store new pgvector version
|
|
|
|
|
echo "$desired_version" > "$VECTOR_VERSION_FILE"
|
|
|
|
|
# Store new vchord version
|
|
|
|
|
echo "$desired_version" > "$VCHORD_VERSION_FILE"
|
|
|
|
|
else
|
|
|
|
|
bashio::log.info "'vectors' extension is already at the latest version ($desired_version)."
|
|
|
|
|
bashio::log.info "'vchord' extension is already at the latest version ($desired_version)."
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Function: Troubleshoot vector extension
|
|
|
|
|
troubleshoot_vector_extension() {
|
|
|
|
|
bashio::log.error "Troubleshooting pgvector.rs installation..."
|
|
|
|
|
# Function: Troubleshoot vchord extension
|
|
|
|
|
troubleshoot_vchord_extension() {
|
|
|
|
|
bashio::log.error "Troubleshooting vchord installation..."
|
|
|
|
|
|
|
|
|
|
if ! pg_isready -h "$DB_HOSTNAME" -p "$DB_PORT" -U "$DB_USERNAME" >/dev/null 2>&1; then
|
|
|
|
|
bashio::log.error "PostgreSQL is not running or unreachable."
|
|
|
|
|
@@ -177,9 +178,9 @@ troubleshoot_vector_extension() {
|
|
|
|
|
|
|
|
|
|
local ext_check
|
|
|
|
|
ext_check=$(psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" \
|
|
|
|
|
-tAc "SELECT count(*) FROM pg_available_extensions WHERE name = 'vectors';")
|
|
|
|
|
-tAc "SELECT count(*) FROM pg_available_extensions WHERE name = 'vchord';")
|
|
|
|
|
if [[ "$ext_check" -eq 0 ]]; then
|
|
|
|
|
bashio::log.error "'vectors' extension is missing. Ensure pgvector.rs is installed."
|
|
|
|
|
bashio::log.error "'vchord' extension is missing. Ensure vchord is installed."
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
@@ -188,20 +189,20 @@ troubleshoot_vector_extension() {
|
|
|
|
|
# Main Extension Handling #
|
|
|
|
|
###################################
|
|
|
|
|
|
|
|
|
|
# Store previous vector version
|
|
|
|
|
# Store previous vchord version
|
|
|
|
|
update_postgres
|
|
|
|
|
|
|
|
|
|
if ! check_vector_extension; then
|
|
|
|
|
enable_vector_extension
|
|
|
|
|
if ! check_vchord_extension; then
|
|
|
|
|
enable_vchord_extension
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Store previous vector version
|
|
|
|
|
store_vector_version
|
|
|
|
|
# Store previous vchord version
|
|
|
|
|
store_vchord_version
|
|
|
|
|
|
|
|
|
|
# Upgrade vector extension if needed
|
|
|
|
|
upgrade_vector_extension
|
|
|
|
|
# Upgrade vchord extension if needed
|
|
|
|
|
upgrade_vchord_extension
|
|
|
|
|
|
|
|
|
|
# Final verification
|
|
|
|
|
check_vector_extension || troubleshoot_vector_extension
|
|
|
|
|
check_vchord_extension || troubleshoot_vchord_extension
|
|
|
|
|
|
|
|
|
|
bashio::log.info "All initialization steps completed successfully!" ) & true
|
|
|
|
|
|