1 /*
2 * relfilenode.c
3 *
4 * relfilenode functions
5 *
6 * Copyright (c) 2010-2016, 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.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
transfer_all_new_tablespaces(DbInfoArr * old_db_arr,DbInfoArr * new_db_arr,char * old_pgdata,char * new_pgdata)30 transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
31 char *old_pgdata, char *new_pgdata)
32 {
33 pg_log(PG_REPORT, "%s user relation files\n",
34 user_opts.transfer_mode == TRANSFER_MODE_LINK ? "Linking" : "Copying");
35
36 /*
37 * Transferring files by tablespace is tricky because a single database
38 * can use multiple tablespaces. For non-parallel mode, we just pass a
39 * NULL tablespace path, which matches all tablespaces. In parallel mode,
40 * we pass the default tablespace and all user-created tablespaces and let
41 * those operations happen in parallel.
42 */
43 if (user_opts.jobs <= 1)
44 parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
45 new_pgdata, NULL);
46 else
47 {
48 int tblnum;
49
50 /* transfer default tablespace */
51 parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
52 new_pgdata, old_pgdata);
53
54 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
55 parallel_transfer_all_new_dbs(old_db_arr,
56 new_db_arr,
57 old_pgdata,
58 new_pgdata,
59 os_info.old_tablespaces[tblnum]);
60 /* reap all children */
61 while (reap_child(true) == true)
62 ;
63 }
64
65 end_progress_output();
66 check_ok();
67
68 return;
69 }
70
71
72 /*
73 * transfer_all_new_dbs()
74 *
75 * Responsible for upgrading all database. invokes routines to generate mappings and then
76 * physically link the databases.
77 */
78 void
transfer_all_new_dbs(DbInfoArr * old_db_arr,DbInfoArr * new_db_arr,char * old_pgdata,char * new_pgdata,char * old_tablespace)79 transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
80 char *old_pgdata, char *new_pgdata, char *old_tablespace)
81 {
82 int old_dbnum,
83 new_dbnum;
84
85 /* Scan the old cluster databases and transfer their files */
86 for (old_dbnum = new_dbnum = 0;
87 old_dbnum < old_db_arr->ndbs;
88 old_dbnum++, new_dbnum++)
89 {
90 DbInfo *old_db = &old_db_arr->dbs[old_dbnum],
91 *new_db = NULL;
92 FileNameMap *mappings;
93 int n_maps;
94
95 /*
96 * Advance past any databases that exist in the new cluster but not in
97 * the old, e.g. "postgres". (The user might have removed the
98 * 'postgres' database from the old cluster.)
99 */
100 for (; new_dbnum < new_db_arr->ndbs; new_dbnum++)
101 {
102 new_db = &new_db_arr->dbs[new_dbnum];
103 if (strcmp(old_db->db_name, new_db->db_name) == 0)
104 break;
105 }
106
107 if (new_dbnum >= new_db_arr->ndbs)
108 pg_fatal("old database \"%s\" not found in the new cluster\n",
109 old_db->db_name);
110
111 mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
112 new_pgdata);
113 if (n_maps)
114 {
115 print_maps(mappings, n_maps, new_db->db_name);
116
117 transfer_single_new_db(mappings, n_maps, old_tablespace);
118 }
119 /* We allocate something even for n_maps == 0 */
120 pg_free(mappings);
121 }
122
123 return;
124 }
125
126 /*
127 * transfer_single_new_db()
128 *
129 * create links for mappings stored in "maps" array.
130 */
131 static void
transfer_single_new_db(FileNameMap * maps,int size,char * old_tablespace)132 transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace)
133 {
134 int mapnum;
135 bool vm_crashsafe_match = true;
136 bool vm_must_add_frozenbit = false;
137
138 /*
139 * Do the old and new cluster disagree on the crash-safetiness of the vm
140 * files? If so, do not copy them.
141 */
142 if (old_cluster.controldata.cat_ver < VISIBILITY_MAP_CRASHSAFE_CAT_VER &&
143 new_cluster.controldata.cat_ver >= VISIBILITY_MAP_CRASHSAFE_CAT_VER)
144 vm_crashsafe_match = false;
145
146 /*
147 * Do we need to rewrite visibilitymap?
148 */
149 if (old_cluster.controldata.cat_ver < VISIBILITY_MAP_FROZEN_BIT_CAT_VER &&
150 new_cluster.controldata.cat_ver >= VISIBILITY_MAP_FROZEN_BIT_CAT_VER)
151 vm_must_add_frozenbit = true;
152
153 for (mapnum = 0; mapnum < size; mapnum++)
154 {
155 if (old_tablespace == NULL ||
156 strcmp(maps[mapnum].old_tablespace, old_tablespace) == 0)
157 {
158 /* transfer primary file */
159 transfer_relfile(&maps[mapnum], "", vm_must_add_frozenbit);
160
161 /*
162 * Copy/link any fsm and vm files, if they exist
163 */
164 transfer_relfile(&maps[mapnum], "_fsm", vm_must_add_frozenbit);
165 if (vm_crashsafe_match)
166 transfer_relfile(&maps[mapnum], "_vm", vm_must_add_frozenbit);
167 }
168 }
169 }
170
171
172 /*
173 * transfer_relfile()
174 *
175 * Copy or link file from old cluster to new one. If vm_must_add_frozenbit
176 * is true, visibility map forks are converted and rewritten, even in link
177 * mode.
178 */
179 static void
transfer_relfile(FileNameMap * map,const char * type_suffix,bool vm_must_add_frozenbit)180 transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit)
181 {
182 char old_file[MAXPGPATH];
183 char new_file[MAXPGPATH];
184 int segno;
185 char extent_suffix[65];
186 struct stat statbuf;
187
188 /*
189 * Now copy/link any related segments as well. Remember, PG breaks large
190 * files into 1GB segments, the first segment has no extension, subsequent
191 * segments are named relfilenode.1, relfilenode.2, relfilenode.3. copied.
192 */
193 for (segno = 0;; segno++)
194 {
195 if (segno == 0)
196 extent_suffix[0] = '\0';
197 else
198 snprintf(extent_suffix, sizeof(extent_suffix), ".%d", segno);
199
200 snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
201 map->old_tablespace,
202 map->old_tablespace_suffix,
203 map->old_db_oid,
204 map->old_relfilenode,
205 type_suffix,
206 extent_suffix);
207 snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
208 map->new_tablespace,
209 map->new_tablespace_suffix,
210 map->new_db_oid,
211 map->new_relfilenode,
212 type_suffix,
213 extent_suffix);
214
215 /* Is it an extent, fsm, or vm file? */
216 if (type_suffix[0] != '\0' || segno != 0)
217 {
218 /* Did file open fail? */
219 if (stat(old_file, &statbuf) != 0)
220 {
221 /* File does not exist? That's OK, just return */
222 if (errno == ENOENT)
223 return;
224 else
225 pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
226 map->nspname, map->relname, old_file, new_file,
227 strerror(errno));
228 }
229
230 /* If file is empty, just return */
231 if (statbuf.st_size == 0)
232 return;
233 }
234
235 unlink(new_file);
236
237 /* Copying files might take some time, so give feedback. */
238 pg_log(PG_STATUS, "%s", old_file);
239
240 if (vm_must_add_frozenbit && strcmp(type_suffix, "_vm") == 0)
241 {
242 /* Need to rewrite visibility map format */
243 pg_log(PG_VERBOSE, "rewriting \"%s\" to \"%s\"\n",
244 old_file, new_file);
245 rewriteVisibilityMap(old_file, new_file, map->nspname, map->relname);
246 }
247 else if (user_opts.transfer_mode == TRANSFER_MODE_COPY)
248 {
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 }
253 else
254 {
255 pg_log(PG_VERBOSE, "linking \"%s\" to \"%s\"\n",
256 old_file, new_file);
257 linkFile(old_file, new_file, map->nspname, map->relname);
258 }
259 }
260 }
261