@@ -225,80 +225,55 @@ detect_old_cluster_locale()
225225 old_cluster_lc_ctype=" "
226226 old_cluster_locale=" "
227227
228- debug " detecting locale from old cluster at $old_datadir "
229-
230- # Try to get locale from running database using psql
231- # First check if server is running
232- local pidfile=" $old_datadir /postmaster.pid"
233- local server_was_running=false
234- local temp_server_started=false
235-
236- if [ -f " $pidfile " ]; then
237- # Check if the PID in the file is actually running
238- local pid=$( head -n 1 " $pidfile " 2> /dev/null)
239- if [ -n " $pid " ] && kill -0 " $pid " 2> /dev/null; then
240- server_was_running=true
241- debug " old cluster server is running (PID: $pid ), querying locale via psql"
242- fi
228+ if old_data_in_use; then
229+ return 1
243230 fi
244231
245232 # If server is not running, try to start it temporarily
246- if ! $server_was_running ; then
247- debug " old cluster server is not running, attempting temporary start to query locale"
248-
249- # Create a temporary socket directory
250- local temp_sockdir=$( mktemp -d 2> /dev/null)
251- if [ $? -ne 0 ] || [ -z " $temp_sockdir " ]; then
252- debug " failed to create temporary socket directory"
253- return 1
254- fi
255-
256- local temp_log=" $old_datadir /temp_locale_query.log"
257-
258- # Try to start server with minimal configuration
259- # Use a different port to avoid conflicts
260- local temp_port=$(( old_port + 10000 ))
261-
262- # Start server in background with minimal settings
263- # Use -m fast to speed up startup
264- if " $old_bindir /pg_ctl" -D " $old_datadir " -o " -p $temp_port -c listen_addresses='' -c unix_socket_directories='$temp_sockdir ' -c max_connections=5 -c shared_buffers=128kB" start -w -l " $temp_log " -m fast > /dev/null 2>&1 ; then
265- temp_server_started=true
266- debug " temporarily started old cluster server on port $temp_port "
267- # Give it a moment to be ready
268- sleep 2
269- else
270- debug " failed to start old cluster server temporarily (see $temp_log for details)"
271- rm -rf " $temp_sockdir "
272- rm -f " $temp_log "
273- return 1
274- fi
275- fi
276-
277- # Query locale from template1 database
278- local query=" SELECT datcollate, datctype FROM pg_catalog.pg_database WHERE datname = 'template1'"
279- local result
280- local query_port=" $old_port "
281- if $temp_server_started ; then
282- query_port=" $temp_port "
233+ debug " old cluster server is not running, attempting temporary start to query locale"
234+
235+ # Create a temporary socket directory
236+ local temp_sockdir=$( mktemp -d 2> /dev/null)
237+ if [ $? -ne 0 ] || [ -z " $temp_sockdir " ]; then
238+ debug " failed to create temporary socket directory"
239+ return 1
283240 fi
284-
285- # Use appropriate connection method
286- local psql_exit_code=0
287- if $temp_server_started ; then
288- result=$( " $old_bindir /psql" -t -A -h " $temp_sockdir " -p " $query_port " -d template1 -c " $query " 2> /dev/null)
289- psql_exit_code=$?
241+
242+ local temp_log=" $old_datadir /temp_locale_query.log"
243+
244+ # Try to start server with minimal configuration
245+ # Use a different port to avoid conflicts
246+ local temp_port=$(( old_port + 10000 ))
247+
248+ # Start server in background with minimal settings
249+ # Use -m fast to speed up startup
250+ if " $old_bindir /pg_ctl" -D " $old_datadir " -o " -p $temp_port -c listen_addresses='' -c unix_socket_directories='$temp_sockdir ' -c max_connections=5 -c shared_buffers=128kB" start -w -l " $temp_log " -m fast > /dev/null 2>&1 ; then
251+ debug " temporarily started old cluster server on port $temp_port "
252+ # Give it a moment to be ready
253+ sleep 2
290254 else
291- result=$( " $old_bindir /psql" -t -A -p " $query_port " -d template1 -c " $query " 2> /dev/null)
292- psql_exit_code=$?
293- fi
294-
295- # Stop temporary server if we started it
296- if $temp_server_started ; then
297- " $old_bindir /pg_ctl" -D " $old_datadir " stop > /dev/null 2>&1
255+ debug " failed to start old cluster server temporarily (see $temp_log for details)"
298256 rm -rf " $temp_sockdir "
299257 rm -f " $temp_log "
258+ return 1
300259 fi
260+
301261
262+ # Query locale from template1 database
263+ local query=" SELECT datcollate, datctype FROM pg_catalog.pg_database WHERE datname = 'template1'"
264+ local result
265+ local query_port=" $temp_port "
266+
267+ # Perform query
268+ local psql_exit_code=0
269+ result=$( psql -t -A -h " $temp_sockdir " -p " $query_port " -d template1 -c " $query " 2> /dev/null)
270+ psql_exit_code=$?
271+
272+ # Stop temporary server
273+ " $old_bindir /pg_ctl" -D " $old_datadir " stop > /dev/null 2>&1
274+ rm -rf " $temp_sockdir "
275+ rm -f " $temp_log "
276+
302277 # Parse result
303278 if [ $psql_exit_code -eq 0 ] && [ -n " $result " ]; then
304279 # Parse result: datcollate|datctype
0 commit comments