42#define SYNC_QUEUE_MAX_LEN (1024)
79 pre_sync_fname(fname,
false);
109 char *old_pgdata,
char *new_pgdata)
139 new_pgdata, NULL, NULL);
146 new_pgdata, old_pgdata, new_pgdata);
173 char *old_pgdata,
char *new_pgdata,
174 char *old_tablespace,
char *new_tablespace)
180 for (old_dbnum = new_dbnum = 0;
181 old_dbnum < old_db_arr->
ndbs;
182 old_dbnum++, new_dbnum++)
184 DbInfo *old_db = &old_db_arr->
dbs[old_dbnum],
194 for (; new_dbnum < new_db_arr->
ndbs; new_dbnum++)
196 new_db = &new_db_arr->
dbs[new_dbnum];
197 if (strcmp(old_db->
db_name, new_db->db_name) == 0)
201 if (new_dbnum >= new_db_arr->
ndbs)
202 pg_fatal(
"old database \"%s\" not found in the new cluster",
240 Oid db_oid,
char *old_catalog_dir,
char *new_db_dir,
243 const char *old_tblspc_suffix;
244 const char *new_tblspc_suffix;
252 old_tblspc_suffix =
"/base";
257 new_tblspc_suffix =
"/base";
262 snprintf(old_tblspc,
sizeof(old_tblspc),
"%s%s", old_tablespace, old_tblspc_suffix);
263 snprintf(new_tblspc,
sizeof(new_tblspc),
"%s%s", new_tablespace, new_tblspc_suffix);
264 snprintf(old_db_dir,
sizeof(old_db_dir),
"%s/%u", old_tblspc, db_oid);
271 snprintf(moved_tblspc,
sizeof(moved_tblspc),
"%s/moved_for_upgrade", old_tblspc);
272 snprintf(old_catalog_dir,
MAXPGPATH,
"%s/%u_old_catalogs", moved_tblspc, db_oid);
276 if (
stat(old_db_dir, &st) != 0)
279 pg_fatal(
"could not stat file \"%s\": %m", old_db_dir);
285 pg_fatal(
"could not create directory \"%s\": %m", moved_tblspc);
289 pg_fatal(
"could not create directory \"%s\": %m", old_catalog_dir);
292 if (rename(new_db_dir, moved_db_dir) != 0)
293 pg_fatal(
"could not rename directory \"%s\" to \"%s\": %m", new_db_dir, moved_db_dir);
296 if (rename(old_db_dir, new_db_dir) != 0)
297 pg_fatal(
"could not rename directory \"%s\" to \"%s\": %m", old_db_dir, new_db_dir);
356 const char *new_db_dir,
const char *moved_db_dir)
367 pg_fatal(
"could not open directory \"%s\": %m", new_db_dir);
368 while (errno = 0, (de =
readdir(dir)) != NULL)
384 if (rename(path,
dest) != 0)
385 pg_fatal(
"could not rename file \"%s\" to \"%s\": %m", path,
dest);
388 pg_fatal(
"could not read directory \"%s\": %m", new_db_dir);
394 pg_fatal(
"could not open directory \"%s\": %m", moved_db_dir);
395 while (errno = 0, (de =
readdir(dir)) != NULL)
411 if (rename(path,
dest) != 0)
412 pg_fatal(
"could not rename file \"%s\" to \"%s\": %m", path,
dest);
426 pg_fatal(
"could not read directory \"%s\": %m", moved_db_dir);
431 pg_fatal(
"could not synchronize directory \"%s\": %m", new_db_dir);
433 pg_fatal(
"could not synchronize parent directory of \"%s\": %m", new_db_dir);
466 old_catalog_dir, new_db_dir, moved_db_dir))
468 old_catalog_dir, new_db_dir, moved_db_dir);
473 old_catalog_dir, new_db_dir, moved_db_dir))
475 old_catalog_dir, new_db_dir, moved_db_dir);
482 old_catalog_dir, new_db_dir, moved_db_dir))
484 old_catalog_dir, new_db_dir, moved_db_dir);
496 char *old_tablespace,
char *new_tablespace)
499 bool vm_must_add_frozenbit =
false;
506 vm_must_add_frozenbit =
true;
516 Assert(!vm_must_add_frozenbit);
518 do_swap(
maps, size, old_tablespace, new_tablespace);
522 for (mapnum = 0; mapnum < size; mapnum++)
524 if (old_tablespace == NULL ||
525 strcmp(
maps[mapnum].old_tablespace, old_tablespace) == 0)
553 char extent_suffix[65];
561 for (segno = 0;; segno++)
564 extent_suffix[0] =
'\0';
566 snprintf(extent_suffix,
sizeof(extent_suffix),
".%d", segno);
568 snprintf(old_file,
sizeof(old_file),
"%s%s/%u/%u%s%s",
575 snprintf(new_file,
sizeof(new_file),
"%s%s/%u/%u%s%s",
584 if (type_suffix[0] !=
'\0' || segno != 0)
587 if (
stat(old_file, &statbuf) != 0)
593 pg_fatal(
"error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %m",
607 if (vm_must_add_frozenbit && strcmp(type_suffix,
"_vm") == 0)
void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata, char *old_tablespace, char *new_tablespace)
bool reap_child(bool wait_for_child)
struct dirent * readdir(DIR *)
DIR * opendir(const char *)
void fsync_fname(const char *fname, bool isdir)
static int fsync_parent_path(const char *fname, int elevel)
void linkFile(const char *src, const char *dst, const char *schemaName, const char *relName)
void rewriteVisibilityMap(const char *fromfile, const char *tofile, const char *schemaName, const char *relName)
void cloneFile(const char *src, const char *dst, const char *schemaName, const char *relName)
void copyFileByRange(const char *src, const char *dst, const char *schemaName, const char *relName)
void copyFile(const char *src, const char *dst, const char *schemaName, const char *relName)
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Assert(PointerIsAligned(start, uint64))
FileNameMap * gen_db_file_maps(DbInfo *old_db, DbInfo *new_db, int *nmaps, const char *old_pgdata, const char *new_pgdata)
static void check_ok(void)
static int pg_cmp_u32(uint32 a, uint32 b)
void pfree(void *pointer)
#define VISIBILITY_MAP_FROZEN_BIT_CAT_VER
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ TRANSFER_MODE_COPY_FILE_RANGE
void void prep_status_progress(const char *fmt,...) pg_attribute_printf(1
void void pg_noreturn void void end_progress_output(void)
int pg_mkdir_p(char *path, int omode)
#define qsort(a, b, c, d)
void transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
static bool prepare_for_swap(const char *old_tablespace, const char *new_tablespace, Oid db_oid, char *old_catalog_dir, char *new_db_dir, char *moved_db_dir)
static void do_swap(FileNameMap *maps, int size, char *old_tablespace, char *new_tablespace)
static char * sync_queue[SYNC_QUEUE_MAX_LEN]
static RelFileNumber parse_relfilenumber(const char *filename)
static void sync_queue_init(void)
static void sync_queue_destroy(void)
static void transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit)
static int sync_queue_len
static bool sync_queue_inited
void transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata, char *old_tablespace, char *new_tablespace)
static void sync_queue_push(const char *fname)
static void swap_catalog_files(FileNameMap *maps, int size, const char *old_catalog_dir, const char *new_db_dir, const char *moved_db_dir)
#define SYNC_QUEUE_MAX_LEN
static int FileNameMapCmp(const void *a, const void *b)
static void transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace, char *new_tablespace)
static void sync_queue_sync_all(void)
#define InvalidRelFileNumber
#define RelFileNumberIsValid(relnumber)
const char * tablespace_suffix
const char * new_tablespace
const char * old_tablespace_suffix
const char * old_tablespace
RelFileNumber relfilenumber
const char * new_tablespace_suffix
transferMode transfer_mode
static const pg_conv_map maps[]