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