PostgreSQL Source Code  git master
relfilenode.c
Go to the documentation of this file.
1 /*
2  * relfilenode.c
3  *
4  * relfilenode functions
5  *
6  * Copyright (c) 2010-2019, PostgreSQL Global Development Group
7  * src/bin/pg_upgrade/relfilenode.c
8  */
9 
10 #include "postgres_fe.h"
11 
12 #include "pg_upgrade.h"
13 
14 #include <sys/stat.h>
15 #include "catalog/pg_class_d.h"
16 #include "access/transam.h"
17 
18 
19 static void transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace);
20 static void transfer_relfile(FileNameMap *map, const char *suffix, bool vm_must_add_frozenbit);
21 
22 
23 /*
24  * transfer_all_new_tablespaces()
25  *
26  * Responsible for upgrading all database. invokes routines to generate mappings and then
27  * physically link the databases.
28  */
29 void
31  char *old_pgdata, char *new_pgdata)
32 {
33  switch (user_opts.transfer_mode)
34  {
36  pg_log(PG_REPORT, "Cloning user relation files\n");
37  break;
38  case TRANSFER_MODE_COPY:
39  pg_log(PG_REPORT, "Copying user relation files\n");
40  break;
41  case TRANSFER_MODE_LINK:
42  pg_log(PG_REPORT, "Linking user relation files\n");
43  break;
44  }
45 
46  /*
47  * Transferring files by tablespace is tricky because a single database
48  * can use multiple tablespaces. For non-parallel mode, we just pass a
49  * NULL tablespace path, which matches all tablespaces. In parallel mode,
50  * we pass the default tablespace and all user-created tablespaces and let
51  * those operations happen in parallel.
52  */
53  if (user_opts.jobs <= 1)
54  parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
55  new_pgdata, NULL);
56  else
57  {
58  int tblnum;
59 
60  /* transfer default tablespace */
61  parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
62  new_pgdata, old_pgdata);
63 
64  for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
66  new_db_arr,
67  old_pgdata,
68  new_pgdata,
69  os_info.old_tablespaces[tblnum]);
70  /* reap all children */
71  while (reap_child(true) == true)
72  ;
73  }
74 
76  check_ok();
77 
78  return;
79 }
80 
81 
82 /*
83  * transfer_all_new_dbs()
84  *
85  * Responsible for upgrading all database. invokes routines to generate mappings and then
86  * physically link the databases.
87  */
88 void
89 transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
90  char *old_pgdata, char *new_pgdata, char *old_tablespace)
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\n",
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  print_maps(mappings, n_maps, new_db->db_name);
126 
127  transfer_single_new_db(mappings, n_maps, old_tablespace);
128  }
129  /* We allocate something even for n_maps == 0 */
130  pg_free(mappings);
131  }
132 
133  return;
134 }
135 
136 /*
137  * transfer_single_new_db()
138  *
139  * create links for mappings stored in "maps" array.
140  */
141 static void
142 transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace)
143 {
144  int mapnum;
145  bool vm_crashsafe_match = true;
146  bool vm_must_add_frozenbit = false;
147 
148  /*
149  * Do the old and new cluster disagree on the crash-safetiness of the vm
150  * files? If so, do not copy them.
151  */
154  vm_crashsafe_match = false;
155 
156  /*
157  * Do we need to rewrite visibilitymap?
158  */
161  vm_must_add_frozenbit = true;
162 
163  for (mapnum = 0; mapnum < size; mapnum++)
164  {
165  if (old_tablespace == NULL ||
166  strcmp(maps[mapnum].old_tablespace, old_tablespace) == 0)
167  {
168  /* transfer primary file */
169  transfer_relfile(&maps[mapnum], "", vm_must_add_frozenbit);
170 
171  /* fsm/vm files added in PG 8.4 */
173  {
174  /*
175  * Copy/link any fsm and vm files, if they exist
176  */
177  transfer_relfile(&maps[mapnum], "_fsm", vm_must_add_frozenbit);
178  if (vm_crashsafe_match)
179  transfer_relfile(&maps[mapnum], "_vm", vm_must_add_frozenbit);
180  }
181  }
182  }
183 }
184 
185 
186 /*
187  * transfer_relfile()
188  *
189  * Copy or link file from old cluster to new one. If vm_must_add_frozenbit
190  * is true, visibility map forks are converted and rewritten, even in link
191  * mode.
192  */
193 static void
194 transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit)
195 {
196  char old_file[MAXPGPATH];
197  char new_file[MAXPGPATH];
198  int segno;
199  char extent_suffix[65];
200  struct stat statbuf;
201 
202  /*
203  * Now copy/link any related segments as well. Remember, PG breaks large
204  * files into 1GB segments, the first segment has no extension, subsequent
205  * segments are named relfilenode.1, relfilenode.2, relfilenode.3.
206  */
207  for (segno = 0;; segno++)
208  {
209  if (segno == 0)
210  extent_suffix[0] = '\0';
211  else
212  snprintf(extent_suffix, sizeof(extent_suffix), ".%d", segno);
213 
214  snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
215  map->old_tablespace,
217  map->old_db_oid,
218  map->old_relfilenode,
219  type_suffix,
220  extent_suffix);
221  snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
222  map->new_tablespace,
224  map->new_db_oid,
225  map->new_relfilenode,
226  type_suffix,
227  extent_suffix);
228 
229  /* Is it an extent, fsm, or vm file? */
230  if (type_suffix[0] != '\0' || segno != 0)
231  {
232  /* Did file open fail? */
233  if (stat(old_file, &statbuf) != 0)
234  {
235  /* File does not exist? That's OK, just return */
236  if (errno == ENOENT)
237  return;
238  else
239  pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
240  map->nspname, map->relname, old_file, new_file,
241  strerror(errno));
242  }
243 
244  /* If file is empty, just return */
245  if (statbuf.st_size == 0)
246  return;
247  }
248 
249  unlink(new_file);
250 
251  /* Copying files might take some time, so give feedback. */
252  pg_log(PG_STATUS, "%s", old_file);
253 
254  if (vm_must_add_frozenbit && strcmp(type_suffix, "_vm") == 0)
255  {
256  /* Need to rewrite visibility map format */
257  pg_log(PG_VERBOSE, "rewriting \"%s\" to \"%s\"\n",
258  old_file, new_file);
259  rewriteVisibilityMap(old_file, new_file, map->nspname, map->relname);
260  }
261  else
262  switch (user_opts.transfer_mode)
263  {
264  case TRANSFER_MODE_CLONE:
265  pg_log(PG_VERBOSE, "cloning \"%s\" to \"%s\"\n",
266  old_file, new_file);
267  cloneFile(old_file, new_file, map->nspname, map->relname);
268  break;
269  case TRANSFER_MODE_COPY:
270  pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\"\n",
271  old_file, new_file);
272  copyFile(old_file, new_file, map->nspname, map->relname);
273  break;
274  case TRANSFER_MODE_LINK:
275  pg_log(PG_VERBOSE, "linking \"%s\" to \"%s\"\n",
276  old_file, new_file);
277  linkFile(old_file, new_file, map->nspname, map->relname);
278  }
279  }
280 }
void cloneFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:43
Oid old_relfilenode
Definition: pg_upgrade.h:168
uint32 major_version
Definition: pg_upgrade.h:269
#define VISIBILITY_MAP_CRASHSAFE_CAT_VER
Definition: pg_upgrade.h:102
ControlData controldata
Definition: pg_upgrade.h:259
void rewriteVisibilityMap(const char *fromfile, const char *tofile, const char *schemaName, const char *relName)
Definition: file.c:178
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:23
void end_progress_output(void)
Definition: util.c:44
FileNameMap * gen_db_file_maps(DbInfo *old_db, DbInfo *new_db, int *nmaps, const char *old_pgdata, const char *new_pgdata)
Definition: info.c:41
#define pg_fatal(...)
Definition: pg_rewind.h:43
Oid new_db_oid
Definition: pg_upgrade.h:162
int jobs
Definition: pg_upgrade.h:295
void copyFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:83
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
void transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata, char *old_tablespace)
Definition: relfilenode.c:89
void print_maps(FileNameMap *maps, int n_maps, const char *db_name)
Definition: info.c:285
const char * old_tablespace
Definition: pg_upgrade.h:157
Oid new_relfilenode
Definition: pg_upgrade.h:169
#define MAXPGPATH
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:178
void linkFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:152
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
static void check_ok(void)
Definition: initdb.c:2079
const char * old_tablespace_suffix
Definition: pg_upgrade.h:159
transferMode transfer_mode
Definition: pg_upgrade.h:294
UserOpts user_opts
Definition: option.c:32
static void transfer_relfile(FileNameMap *map, const char *suffix, bool vm_must_add_frozenbit)
Definition: relfilenode.c:194
#define stat(a, b)
Definition: win32_port.h:255
#define VISIBILITY_MAP_FROZEN_BIT_CAT_VER
Definition: pg_upgrade.h:107
uint32 cat_ver
Definition: pg_upgrade.h:204
char ** old_tablespaces
Definition: pg_upgrade.h:313
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
Oid old_db_oid
Definition: pg_upgrade.h:161
static const pg_conv_map maps[]
#define strerror
Definition: port.h:205
void pg_free(void *ptr)
Definition: fe_memutils.c:105
bool reap_child(bool wait_for_child)
Definition: parallel.c:289
char * nspname
Definition: pg_upgrade.h:171
void transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
Definition: relfilenode.c:30
int num_old_tablespaces
Definition: pg_upgrade.h:314
OSInfo os_info
Definition: pg_upgrade.c:61
char * db_name
Definition: pg_upgrade.h:181
DbInfo * dbs
Definition: pg_upgrade.h:192
#define snprintf
Definition: port.h:192
const char * new_tablespace
Definition: pg_upgrade.h:158
const char * new_tablespace_suffix
Definition: pg_upgrade.h:160
char * relname
Definition: pg_upgrade.h:172
static void transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace)
Definition: relfilenode.c:142