1 /*
2 * relfilenode.c
3 *
4 * relfilenode functions
5 *
6 * Copyright (c) 2010-2020, 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
transfer_all_new_tablespaces(DbInfoArr * old_db_arr,DbInfoArr * new_db_arr,char * old_pgdata,char * new_pgdata)29 transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
30 char *old_pgdata, char *new_pgdata)
31 {
32 switch (user_opts.transfer_mode)
33 {
34 case TRANSFER_MODE_CLONE:
35 pg_log(PG_REPORT, "Cloning user relation files\n");
36 break;
37 case TRANSFER_MODE_COPY:
38 pg_log(PG_REPORT, "Copying user relation files\n");
39 break;
40 case TRANSFER_MODE_LINK:
41 pg_log(PG_REPORT, "Linking user relation files\n");
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++)
64 parallel_transfer_all_new_dbs(old_db_arr,
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
74 end_progress_output();
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
transfer_all_new_dbs(DbInfoArr * old_db_arr,DbInfoArr * new_db_arr,char * old_pgdata,char * new_pgdata,char * old_tablespace)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 print_maps(mappings, n_maps, new_db->db_name);
123
124 transfer_single_new_db(mappings, n_maps, old_tablespace);
125 }
126 /* We allocate something even for n_maps == 0 */
127 pg_free(mappings);
128 }
129 }
130
131 /*
132 * transfer_single_new_db()
133 *
134 * create links for mappings stored in "maps" array.
135 */
136 static void
transfer_single_new_db(FileNameMap * maps,int size,char * old_tablespace)137 transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace)
138 {
139 int mapnum;
140 bool vm_crashsafe_match = true;
141 bool vm_must_add_frozenbit = false;
142
143 /*
144 * Do the old and new cluster disagree on the crash-safetiness of the vm
145 * files? If so, do not copy them.
146 */
147 if (old_cluster.controldata.cat_ver < VISIBILITY_MAP_CRASHSAFE_CAT_VER &&
148 new_cluster.controldata.cat_ver >= VISIBILITY_MAP_CRASHSAFE_CAT_VER)
149 vm_crashsafe_match = false;
150
151 /*
152 * Do we need to rewrite visibilitymap?
153 */
154 if (old_cluster.controldata.cat_ver < VISIBILITY_MAP_FROZEN_BIT_CAT_VER &&
155 new_cluster.controldata.cat_ver >= VISIBILITY_MAP_FROZEN_BIT_CAT_VER)
156 vm_must_add_frozenbit = true;
157
158 for (mapnum = 0; mapnum < size; mapnum++)
159 {
160 if (old_tablespace == NULL ||
161 strcmp(maps[mapnum].old_tablespace, old_tablespace) == 0)
162 {
163 /* transfer primary file */
164 transfer_relfile(&maps[mapnum], "", vm_must_add_frozenbit);
165
166 /*
167 * Copy/link any fsm and vm files, if they exist
168 */
169 transfer_relfile(&maps[mapnum], "_fsm", vm_must_add_frozenbit);
170 if (vm_crashsafe_match)
171 transfer_relfile(&maps[mapnum], "_vm", vm_must_add_frozenbit);
172 }
173 }
174 }
175
176
177 /*
178 * transfer_relfile()
179 *
180 * Copy or link file from old cluster to new one. If vm_must_add_frozenbit
181 * is true, visibility map forks are converted and rewritten, even in link
182 * mode.
183 */
184 static void
transfer_relfile(FileNameMap * map,const char * type_suffix,bool vm_must_add_frozenbit)185 transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit)
186 {
187 char old_file[MAXPGPATH];
188 char new_file[MAXPGPATH];
189 int segno;
190 char extent_suffix[65];
191 struct stat statbuf;
192
193 /*
194 * Now copy/link any related segments as well. Remember, PG breaks large
195 * files into 1GB segments, the first segment has no extension, subsequent
196 * segments are named relfilenode.1, relfilenode.2, relfilenode.3.
197 */
198 for (segno = 0;; segno++)
199 {
200 if (segno == 0)
201 extent_suffix[0] = '\0';
202 else
203 snprintf(extent_suffix, sizeof(extent_suffix), ".%d", segno);
204
205 snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
206 map->old_tablespace,
207 map->old_tablespace_suffix,
208 map->old_db_oid,
209 map->old_relfilenode,
210 type_suffix,
211 extent_suffix);
212 snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
213 map->new_tablespace,
214 map->new_tablespace_suffix,
215 map->new_db_oid,
216 map->new_relfilenode,
217 type_suffix,
218 extent_suffix);
219
220 /* Is it an extent, fsm, or vm file? */
221 if (type_suffix[0] != '\0' || segno != 0)
222 {
223 /* Did file open fail? */
224 if (stat(old_file, &statbuf) != 0)
225 {
226 /* File does not exist? That's OK, just return */
227 if (errno == ENOENT)
228 return;
229 else
230 pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
231 map->nspname, map->relname, old_file, new_file,
232 strerror(errno));
233 }
234
235 /* If file is empty, just return */
236 if (statbuf.st_size == 0)
237 return;
238 }
239
240 unlink(new_file);
241
242 /* Copying files might take some time, so give feedback. */
243 pg_log(PG_STATUS, "%s", old_file);
244
245 if (vm_must_add_frozenbit && strcmp(type_suffix, "_vm") == 0)
246 {
247 /* Need to rewrite visibility map format */
248 pg_log(PG_VERBOSE, "rewriting \"%s\" to \"%s\"\n",
249 old_file, new_file);
250 rewriteVisibilityMap(old_file, new_file, map->nspname, map->relname);
251 }
252 else
253 switch (user_opts.transfer_mode)
254 {
255 case TRANSFER_MODE_CLONE:
256 pg_log(PG_VERBOSE, "cloning \"%s\" to \"%s\"\n",
257 old_file, new_file);
258 cloneFile(old_file, new_file, map->nspname, map->relname);
259 break;
260 case TRANSFER_MODE_COPY:
261 pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\"\n",
262 old_file, new_file);
263 copyFile(old_file, new_file, map->nspname, map->relname);
264 break;
265 case TRANSFER_MODE_LINK:
266 pg_log(PG_VERBOSE, "linking \"%s\" to \"%s\"\n",
267 old_file, new_file);
268 linkFile(old_file, new_file, map->nspname, map->relname);
269 }
270 }
271 }
272