@@ -246,10 +246,17 @@ output_completion_banner(char *analyze_script_file_name,
246246 "by pg_upgrade so, once you start the new server, consider running:\n"
247247 " %s\n\n" , analyze_script_file_name );
248248
249- pg_log (PG_REPORT ,
250- "Running this script will delete the old cluster's data files:\n"
251- " %s\n" ,
252- deletion_script_file_name );
249+
250+ if (deletion_script_file_name )
251+ pg_log (PG_REPORT ,
252+ "Running this script will delete the old cluster's data files:\n"
253+ " %s\n" ,
254+ deletion_script_file_name );
255+ else
256+ pg_log (PG_REPORT ,
257+ "Could not create a script to delete the old cluster's data\n"
258+ "files because user-defined tablespaces exist in the old cluster\n"
259+ "directory. The old cluster's contents must be deleted manually.\n" );
253260}
254261
255262
@@ -584,14 +591,38 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
584591{
585592 FILE * script = NULL ;
586593 int tblnum ;
594+ char old_cluster_pgdata [MAXPGPATH ];
587595
588596 * deletion_script_file_name = pg_malloc (MAXPGPATH );
589597
590- prep_status ("Creating script to delete old cluster" );
591-
592598 snprintf (* deletion_script_file_name , MAXPGPATH , "delete_old_cluster.%s" ,
593599 SCRIPT_EXT );
594600
601+ /*
602+ * Some users (oddly) create tablespaces inside the cluster data
603+ * directory. We can't create a proper old cluster delete script
604+ * in that case.
605+ */
606+ strlcpy (old_cluster_pgdata , old_cluster .pgdata , MAXPGPATH );
607+ canonicalize_path (old_cluster_pgdata );
608+ for (tblnum = 0 ; tblnum < os_info .num_old_tablespaces ; tblnum ++ )
609+ {
610+ char old_tablespace_dir [MAXPGPATH ];
611+
612+ strlcpy (old_tablespace_dir , os_info .old_tablespaces [tblnum ], MAXPGPATH );
613+ canonicalize_path (old_tablespace_dir );
614+ if (path_is_prefix_of_path (old_cluster_pgdata , old_tablespace_dir ))
615+ {
616+ /* Unlink file in case it is left over from a previous run. */
617+ unlink (* deletion_script_file_name );
618+ pg_free (* deletion_script_file_name );
619+ * deletion_script_file_name = NULL ;
620+ return ;
621+ }
622+ }
623+
624+ prep_status ("Creating script to delete old cluster" );
625+
595626 if ((script = fopen_priv (* deletion_script_file_name , "w" )) == NULL )
596627 pg_log (PG_FATAL , "Could not open file \"%s\": %s\n" ,
597628 * deletion_script_file_name , getErrorText (errno ));
0 commit comments