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-2022, PostgreSQL Global Development Group
7  * src/bin/pg_upgrade/relfilenode.c
8  */
9 
10 #include "postgres_fe.h"
11 
12 #include <sys/stat.h>
13 
14 #include "access/transam.h"
15 #include "catalog/pg_class_d.h"
16 #include "pg_upgrade.h"
17 
18 static void transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace);
19 static void transfer_relfile(FileNameMap *map, const char *suffix, bool vm_must_add_frozenbit);
20 
21 
22 /*
23  * transfer_all_new_tablespaces()
24  *
25  * Responsible for upgrading all database. invokes routines to generate mappings and then
26  * physically link the databases.
27  */
28 void
30  char *old_pgdata, char *new_pgdata)
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;
40  case TRANSFER_MODE_LINK:
41  prep_status_progress("Linking user relation files");
42  break;
43  }
44 
45  /*
46  * Transferring files by tablespace is tricky because a single database
47  * can use multiple tablespaces. For non-parallel mode, we just pass a
48  * NULL tablespace path, which matches all tablespaces. In parallel mode,
49  * we pass the default tablespace and all user-created tablespaces and let
50  * those operations happen in parallel.
51  */
52  if (user_opts.jobs <= 1)
53  parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
54  new_pgdata, NULL);
55  else
56  {
57  int tblnum;
58 
59  /* transfer default tablespace */
60  parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
61  new_pgdata, old_pgdata);
62 
63  for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
65  new_db_arr,
66  old_pgdata,
67  new_pgdata,
68  os_info.old_tablespaces[tblnum]);
69  /* reap all children */
70  while (reap_child(true) == true)
71  ;
72  }
73 
75  check_ok();
76 }
77 
78 
79 /*
80  * transfer_all_new_dbs()
81  *
82  * Responsible for upgrading all database. invokes routines to generate mappings and then
83  * physically link the databases.
84  */
85 void
86 transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
87  char *old_pgdata, char *new_pgdata, char *old_tablespace)
88 {
89  int old_dbnum,
90  new_dbnum;
91 
92  /* Scan the old cluster databases and transfer their files */
93  for (old_dbnum = new_dbnum = 0;
94  old_dbnum < old_db_arr->ndbs;
95  old_dbnum++, new_dbnum++)
96  {
97  DbInfo *old_db = &old_db_arr->dbs[old_dbnum],
98  *new_db = NULL;
99  FileNameMap *mappings;
100  int n_maps;
101 
102  /*
103  * Advance past any databases that exist in the new cluster but not in
104  * the old, e.g. "postgres". (The user might have removed the
105  * 'postgres' database from the old cluster.)
106  */
107  for (; new_dbnum < new_db_arr->ndbs; new_dbnum++)
108  {
109  new_db = &new_db_arr->dbs[new_dbnum];
110  if (strcmp(old_db->db_name, new_db->db_name) == 0)
111  break;
112  }
113 
114  if (new_dbnum >= new_db_arr->ndbs)
115  pg_fatal("old database \"%s\" not found in the new cluster\n",
116  old_db->db_name);
117 
118  mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
119  new_pgdata);
120  if (n_maps)
121  {
122  transfer_single_new_db(mappings, n_maps, old_tablespace);
123  }
124  /* We allocate something even for n_maps == 0 */
125  pg_free(mappings);
126  }
127 }
128 
129 /*
130  * transfer_single_new_db()
131  *
132  * create links for mappings stored in "maps" array.
133  */
134 static void
135 transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace)
136 {
137  int mapnum;
138  bool vm_must_add_frozenbit = false;
139 
140  /*
141  * Do we need to rewrite visibilitymap?
142  */
145  vm_must_add_frozenbit = true;
146 
147  for (mapnum = 0; mapnum < size; mapnum++)
148  {
149  if (old_tablespace == NULL ||
150  strcmp(maps[mapnum].old_tablespace, old_tablespace) == 0)
151  {
152  /* transfer primary file */
153  transfer_relfile(&maps[mapnum], "", vm_must_add_frozenbit);
154 
155  /*
156  * Copy/link any fsm and vm files, if they exist
157  */
158  transfer_relfile(&maps[mapnum], "_fsm", vm_must_add_frozenbit);
159  transfer_relfile(&maps[mapnum], "_vm", vm_must_add_frozenbit);
160  }
161  }
162 }
163 
164 
165 /*
166  * transfer_relfile()
167  *
168  * Copy or link file from old cluster to new one. If vm_must_add_frozenbit
169  * is true, visibility map forks are converted and rewritten, even in link
170  * mode.
171  */
172 static void
173 transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit)
174 {
175  char old_file[MAXPGPATH];
176  char new_file[MAXPGPATH];
177  int segno;
178  char extent_suffix[65];
179  struct stat statbuf;
180 
181  /*
182  * Now copy/link any related segments as well. Remember, PG breaks large
183  * files into 1GB segments, the first segment has no extension, subsequent
184  * segments are named relfilenode.1, relfilenode.2, relfilenode.3.
185  */
186  for (segno = 0;; segno++)
187  {
188  if (segno == 0)
189  extent_suffix[0] = '\0';
190  else
191  snprintf(extent_suffix, sizeof(extent_suffix), ".%d", segno);
192 
193  snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
194  map->old_tablespace,
196  map->db_oid,
197  map->relfilenode,
198  type_suffix,
199  extent_suffix);
200  snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
201  map->new_tablespace,
203  map->db_oid,
204  map->relfilenode,
205  type_suffix,
206  extent_suffix);
207 
208  /* Is it an extent, fsm, or vm file? */
209  if (type_suffix[0] != '\0' || segno != 0)
210  {
211  /* Did file open fail? */
212  if (stat(old_file, &statbuf) != 0)
213  {
214  /* File does not exist? That's OK, just return */
215  if (errno == ENOENT)
216  return;
217  else
218  pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
219  map->nspname, map->relname, old_file, new_file,
220  strerror(errno));
221  }
222 
223  /* If file is empty, just return */
224  if (statbuf.st_size == 0)
225  return;
226  }
227 
228  unlink(new_file);
229 
230  /* Copying files might take some time, so give feedback. */
231  pg_log(PG_STATUS, "%s", old_file);
232 
233  if (vm_must_add_frozenbit && strcmp(type_suffix, "_vm") == 0)
234  {
235  /* Need to rewrite visibility map format */
236  pg_log(PG_VERBOSE, "rewriting \"%s\" to \"%s\"\n",
237  old_file, new_file);
238  rewriteVisibilityMap(old_file, new_file, map->nspname, map->relname);
239  }
240  else
241  switch (user_opts.transfer_mode)
242  {
243  case TRANSFER_MODE_CLONE:
244  pg_log(PG_VERBOSE, "cloning \"%s\" to \"%s\"\n",
245  old_file, new_file);
246  cloneFile(old_file, new_file, map->nspname, map->relname);
247  break;
248  case TRANSFER_MODE_COPY:
249  pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\"\n",
250  old_file, new_file);
251  copyFile(old_file, new_file, map->nspname, map->relname);
252  break;
253  case TRANSFER_MODE_LINK:
254  pg_log(PG_VERBOSE, "linking \"%s\" to \"%s\"\n",
255  old_file, new_file);
256  linkFile(old_file, new_file, map->nspname, map->relname);
257  }
258  }
259 }
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:175
bool reap_child(bool wait_for_child)
Definition: parallel.c:284
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void linkFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:147
void rewriteVisibilityMap(const char *fromfile, const char *tofile, const char *schemaName, const char *relName)
Definition: file.c:173
void cloneFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:38
void copyFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:78
FileNameMap * gen_db_file_maps(DbInfo *old_db, DbInfo *new_db, int *nmaps, const char *old_pgdata, const char *new_pgdata)
Definition: info.c:39
static void check_ok(void)
Definition: initdb.c:1908
#define pg_fatal(...)
#define MAXPGPATH
OSInfo os_info
Definition: pg_upgrade.c:65
ClusterInfo new_cluster
Definition: pg_upgrade.c:64
ClusterInfo old_cluster
Definition: pg_upgrade.c:63
#define VISIBILITY_MAP_FROZEN_BIT_CAT_VER
Definition: pg_upgrade.h:105
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ TRANSFER_MODE_COPY
Definition: pg_upgrade.h:226
@ TRANSFER_MODE_LINK
Definition: pg_upgrade.h:227
@ TRANSFER_MODE_CLONE
Definition: pg_upgrade.h:225
void void prep_status_progress(const char *fmt,...) pg_attribute_printf(1
@ PG_STATUS
Definition: pg_upgrade.h:236
@ PG_VERBOSE
Definition: pg_upgrade.h:235
void end_progress_output(void)
Definition: util.c:42
#define strerror
Definition: port.h:238
#define snprintf
Definition: port.h:225
void transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
Definition: relfilenode.c:29
static void transfer_relfile(FileNameMap *map, const char *suffix, bool vm_must_add_frozenbit)
Definition: relfilenode.c:173
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:86
static void transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace)
Definition: relfilenode.c:135
UserOpts user_opts
Definition: option.c:29
ControlData controldata
Definition: pg_upgrade.h:253
uint32 cat_ver
Definition: pg_upgrade.h:197
DbInfo * dbs
Definition: pg_upgrade.h:185
char * db_name
Definition: pg_upgrade.h:172
Oid relfilenode
Definition: pg_upgrade.h:160
const char * new_tablespace
Definition: pg_upgrade.h:156
const char * old_tablespace_suffix
Definition: pg_upgrade.h:157
const char * old_tablespace
Definition: pg_upgrade.h:155
char * relname
Definition: pg_upgrade.h:163
char * nspname
Definition: pg_upgrade.h:162
const char * new_tablespace_suffix
Definition: pg_upgrade.h:158
int num_old_tablespaces
Definition: pg_upgrade.h:314
char ** old_tablespaces
Definition: pg_upgrade.h:313
transferMode transfer_mode
Definition: pg_upgrade.h:294
int jobs
Definition: pg_upgrade.h:295
__int64 st_size
Definition: win32_port.h:273
static const pg_conv_map maps[]
#define stat
Definition: win32_port.h:283