PostgreSQL Source Code  git master
relfilenumber.c File Reference
#include "postgres_fe.h"
#include <sys/stat.h>
#include "access/transam.h"
#include "catalog/pg_class_d.h"
#include "pg_upgrade.h"
Include dependency graph for relfilenumber.c:

Go to the source code of this file.

Functions

static void transfer_single_new_db (FileNameMap *maps, int size, char *old_tablespace)
 
static void transfer_relfile (FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit)
 
void transfer_all_new_tablespaces (DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
 
void transfer_all_new_dbs (DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata, char *old_tablespace)
 

Function Documentation

◆ transfer_all_new_dbs()

void transfer_all_new_dbs ( DbInfoArr old_db_arr,
DbInfoArr new_db_arr,
char *  old_pgdata,
char *  new_pgdata,
char *  old_tablespace 
)

Definition at line 89 of file relfilenumber.c.

91 {
92  int old_dbnum,
93  new_dbnum;
94 
95  /* Scan the old cluster databases and transfer their files */
96  for (old_dbnum = new_dbnum = 0;
97  old_dbnum < old_db_arr->ndbs;
98  old_dbnum++, new_dbnum++)
99  {
100  DbInfo *old_db = &old_db_arr->dbs[old_dbnum],
101  *new_db = NULL;
102  FileNameMap *mappings;
103  int n_maps;
104 
105  /*
106  * Advance past any databases that exist in the new cluster but not in
107  * the old, e.g. "postgres". (The user might have removed the
108  * 'postgres' database from the old cluster.)
109  */
110  for (; new_dbnum < new_db_arr->ndbs; new_dbnum++)
111  {
112  new_db = &new_db_arr->dbs[new_dbnum];
113  if (strcmp(old_db->db_name, new_db->db_name) == 0)
114  break;
115  }
116 
117  if (new_dbnum >= new_db_arr->ndbs)
118  pg_fatal("old database \"%s\" not found in the new cluster",
119  old_db->db_name);
120 
121  mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
122  new_pgdata);
123  if (n_maps)
124  {
125  transfer_single_new_db(mappings, n_maps, old_tablespace);
126  }
127  /* We allocate something even for n_maps == 0 */
128  pg_free(mappings);
129  }
130 }
void pg_free(void *ptr)
Definition: fe_memutils.c:105
FileNameMap * gen_db_file_maps(DbInfo *old_db, DbInfo *new_db, int *nmaps, const char *old_pgdata, const char *new_pgdata)
Definition: info.c:43
#define pg_fatal(...)
static void transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace)
DbInfo * dbs
Definition: pg_upgrade.h:217
char * db_name
Definition: pg_upgrade.h:195

References DbInfo::db_name, DbInfoArr::dbs, gen_db_file_maps(), DbInfoArr::ndbs, pg_fatal, pg_free(), and transfer_single_new_db().

Referenced by parallel_transfer_all_new_dbs().

◆ transfer_all_new_tablespaces()

void transfer_all_new_tablespaces ( DbInfoArr old_db_arr,
DbInfoArr new_db_arr,
char *  old_pgdata,
char *  new_pgdata 
)

Definition at line 29 of file relfilenumber.c.

31 {
32  switch (user_opts.transfer_mode)
33  {
35  prep_status_progress("Cloning user relation files");
36  break;
37  case TRANSFER_MODE_COPY:
38  prep_status_progress("Copying user relation files");
39  break;
41  prep_status_progress("Copying user relation files with copy_file_range");
42  break;
43  case TRANSFER_MODE_LINK:
44  prep_status_progress("Linking user relation files");
45  break;
46  }
47 
48  /*
49  * Transferring files by tablespace is tricky because a single database
50  * can use multiple tablespaces. For non-parallel mode, we just pass a
51  * NULL tablespace path, which matches all tablespaces. In parallel mode,
52  * we pass the default tablespace and all user-created tablespaces and let
53  * those operations happen in parallel.
54  */
55  if (user_opts.jobs <= 1)
56  parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
57  new_pgdata, NULL);
58  else
59  {
60  int tblnum;
61 
62  /* transfer default tablespace */
63  parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
64  new_pgdata, old_pgdata);
65 
66  for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
68  new_db_arr,
69  old_pgdata,
70  new_pgdata,
71  os_info.old_tablespaces[tblnum]);
72  /* reap all children */
73  while (reap_child(true) == true)
74  ;
75  }
76 
78  check_ok();
79 }
void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata, char *old_tablespace)
Definition: parallel.c:172
bool reap_child(bool wait_for_child)
Definition: parallel.c:278
static void check_ok(void)
Definition: initdb.c:2036
OSInfo os_info
Definition: pg_upgrade.c:73
@ TRANSFER_MODE_COPY
Definition: pg_upgrade.h:258
@ TRANSFER_MODE_LINK
Definition: pg_upgrade.h:260
@ TRANSFER_MODE_CLONE
Definition: pg_upgrade.h:257
@ TRANSFER_MODE_COPY_FILE_RANGE
Definition: pg_upgrade.h:259
void void prep_status_progress(const char *fmt,...) pg_attribute_printf(1
void end_progress_output(void)
Definition: util.c:43
UserOpts user_opts
Definition: option.c:30
int num_old_tablespaces
Definition: pg_upgrade.h:348
char ** old_tablespaces
Definition: pg_upgrade.h:347
transferMode transfer_mode
Definition: pg_upgrade.h:327
int jobs
Definition: pg_upgrade.h:328

References check_ok(), end_progress_output(), UserOpts::jobs, OSInfo::num_old_tablespaces, OSInfo::old_tablespaces, os_info, parallel_transfer_all_new_dbs(), prep_status_progress(), reap_child(), UserOpts::transfer_mode, TRANSFER_MODE_CLONE, TRANSFER_MODE_COPY, TRANSFER_MODE_COPY_FILE_RANGE, TRANSFER_MODE_LINK, and user_opts.

Referenced by main().

◆ transfer_relfile()

static void transfer_relfile ( FileNameMap map,
const char *  type_suffix,
bool  vm_must_add_frozenbit 
)
static

Definition at line 176 of file relfilenumber.c.

177 {
178  char old_file[MAXPGPATH];
179  char new_file[MAXPGPATH];
180  int segno;
181  char extent_suffix[65];
182  struct stat statbuf;
183 
184  /*
185  * Now copy/link any related segments as well. Remember, PG breaks large
186  * files into 1GB segments, the first segment has no extension, subsequent
187  * segments are named relfilenumber.1, relfilenumber.2, relfilenumber.3.
188  */
189  for (segno = 0;; segno++)
190  {
191  if (segno == 0)
192  extent_suffix[0] = '\0';
193  else
194  snprintf(extent_suffix, sizeof(extent_suffix), ".%d", segno);
195 
196  snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
197  map->old_tablespace,
199  map->db_oid,
200  map->relfilenumber,
201  type_suffix,
202  extent_suffix);
203  snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
204  map->new_tablespace,
206  map->db_oid,
207  map->relfilenumber,
208  type_suffix,
209  extent_suffix);
210 
211  /* Is it an extent, fsm, or vm file? */
212  if (type_suffix[0] != '\0' || segno != 0)
213  {
214  /* Did file open fail? */
215  if (stat(old_file, &statbuf) != 0)
216  {
217  /* File does not exist? That's OK, just return */
218  if (errno == ENOENT)
219  return;
220  else
221  pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %m",
222  map->nspname, map->relname, old_file, new_file);
223  }
224 
225  /* If file is empty, just return */
226  if (statbuf.st_size == 0)
227  return;
228  }
229 
230  unlink(new_file);
231 
232  /* Copying files might take some time, so give feedback. */
233  pg_log(PG_STATUS, "%s", old_file);
234 
235  if (vm_must_add_frozenbit && strcmp(type_suffix, "_vm") == 0)
236  {
237  /* Need to rewrite visibility map format */
238  pg_log(PG_VERBOSE, "rewriting \"%s\" to \"%s\"",
239  old_file, new_file);
240  rewriteVisibilityMap(old_file, new_file, map->nspname, map->relname);
241  }
242  else
243  switch (user_opts.transfer_mode)
244  {
245  case TRANSFER_MODE_CLONE:
246  pg_log(PG_VERBOSE, "cloning \"%s\" to \"%s\"",
247  old_file, new_file);
248  cloneFile(old_file, new_file, map->nspname, map->relname);
249  break;
250  case TRANSFER_MODE_COPY:
251  pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\"",
252  old_file, new_file);
253  copyFile(old_file, new_file, map->nspname, map->relname);
254  break;
256  pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\" with copy_file_range",
257  old_file, new_file);
258  copyFileByRange(old_file, new_file, map->nspname, map->relname);
259  break;
260  case TRANSFER_MODE_LINK:
261  pg_log(PG_VERBOSE, "linking \"%s\" to \"%s\"",
262  old_file, new_file);
263  linkFile(old_file, new_file, map->nspname, map->relname);
264  }
265  }
266 }
void linkFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:190
void rewriteVisibilityMap(const char *fromfile, const char *tofile, const char *schemaName, const char *relName)
Definition: file.c:216
void cloneFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:39
void copyFileByRange(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:151
void copyFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:82
#define MAXPGPATH
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ PG_STATUS
Definition: pg_upgrade.h:269
@ PG_VERBOSE
Definition: pg_upgrade.h:268
#define snprintf
Definition: port.h:238
const char * new_tablespace
Definition: pg_upgrade.h:179
const char * old_tablespace_suffix
Definition: pg_upgrade.h:180
const char * old_tablespace
Definition: pg_upgrade.h:178
RelFileNumber relfilenumber
Definition: pg_upgrade.h:183
char * relname
Definition: pg_upgrade.h:186
char * nspname
Definition: pg_upgrade.h:185
const char * new_tablespace_suffix
Definition: pg_upgrade.h:181
#define stat
Definition: win32_port.h:284

References cloneFile(), copyFile(), copyFileByRange(), FileNameMap::db_oid, linkFile(), MAXPGPATH, FileNameMap::new_tablespace, FileNameMap::new_tablespace_suffix, FileNameMap::nspname, FileNameMap::old_tablespace, FileNameMap::old_tablespace_suffix, pg_fatal, pg_log(), PG_STATUS, PG_VERBOSE, FileNameMap::relfilenumber, FileNameMap::relname, rewriteVisibilityMap(), snprintf, stat::st_size, stat, UserOpts::transfer_mode, TRANSFER_MODE_CLONE, TRANSFER_MODE_COPY, TRANSFER_MODE_COPY_FILE_RANGE, TRANSFER_MODE_LINK, and user_opts.

Referenced by transfer_single_new_db().

◆ transfer_single_new_db()

static void transfer_single_new_db ( FileNameMap maps,
int  size,
char *  old_tablespace 
)
static

Definition at line 138 of file relfilenumber.c.

139 {
140  int mapnum;
141  bool vm_must_add_frozenbit = false;
142 
143  /*
144  * Do we need to rewrite visibilitymap?
145  */
148  vm_must_add_frozenbit = true;
149 
150  for (mapnum = 0; mapnum < size; mapnum++)
151  {
152  if (old_tablespace == NULL ||
153  strcmp(maps[mapnum].old_tablespace, old_tablespace) == 0)
154  {
155  /* transfer primary file */
156  transfer_relfile(&maps[mapnum], "", vm_must_add_frozenbit);
157 
158  /*
159  * Copy/link any fsm and vm files, if they exist
160  */
161  transfer_relfile(&maps[mapnum], "_fsm", vm_must_add_frozenbit);
162  transfer_relfile(&maps[mapnum], "_vm", vm_must_add_frozenbit);
163  }
164  }
165 }
ClusterInfo new_cluster
Definition: pg_upgrade.c:72
ClusterInfo old_cluster
Definition: pg_upgrade.c:71
#define VISIBILITY_MAP_FROZEN_BIT_CAT_VER
Definition: pg_upgrade.h:108
static void transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit)
static pg_noinline void Size size
Definition: slab.c:607
ControlData controldata
Definition: pg_upgrade.h:284
uint32 cat_ver
Definition: pg_upgrade.h:229
static const pg_conv_map maps[]

References ControlData::cat_ver, ClusterInfo::controldata, maps, new_cluster, old_cluster, size, transfer_relfile(), and VISIBILITY_MAP_FROZEN_BIT_CAT_VER.

Referenced by transfer_all_new_dbs().