pg_upgrade: remove pre-8.4 code and >= 8.4 check
[pgsql.git] / src / bin / pg_upgrade / relfilenode.c
blobf76ddaaf3a161bcacc6034aa5a366ce8ca74dc68
1 /*
2 * relfilenode.c
4 * relfilenode functions
6 * Copyright (c) 2010-2020, PostgreSQL Global Development Group
7 * src/bin/pg_upgrade/relfilenode.c
8 */
10 #include "postgres_fe.h"
12 #include <sys/stat.h>
14 #include "access/transam.h"
15 #include "catalog/pg_class_d.h"
16 #include "pg_upgrade.h"
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);
23 * transfer_all_new_tablespaces()
25 * Responsible for upgrading all database. invokes routines to generate mappings and then
26 * physically link the databases.
28 void
29 transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
30 char *old_pgdata, char *new_pgdata)
32 switch (user_opts.transfer_mode)
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;
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.
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
57 int tblnum;
59 /* transfer default tablespace */
60 parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
61 new_pgdata, old_pgdata);
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)
74 end_progress_output();
75 check_ok();
80 * transfer_all_new_dbs()
82 * Responsible for upgrading all database. invokes routines to generate mappings and then
83 * physically link the databases.
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)
89 int old_dbnum,
90 new_dbnum;
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++)
97 DbInfo *old_db = &old_db_arr->dbs[old_dbnum],
98 *new_db = NULL;
99 FileNameMap *mappings;
100 int n_maps;
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.)
107 for (; new_dbnum < new_db_arr->ndbs; new_dbnum++)
109 new_db = &new_db_arr->dbs[new_dbnum];
110 if (strcmp(old_db->db_name, new_db->db_name) == 0)
111 break;
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);
118 mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
119 new_pgdata);
120 if (n_maps)
122 print_maps(mappings, n_maps, new_db->db_name);
124 transfer_single_new_db(mappings, n_maps, old_tablespace);
126 /* We allocate something even for n_maps == 0 */
127 pg_free(mappings);
132 * transfer_single_new_db()
134 * create links for mappings stored in "maps" array.
136 static void
137 transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace)
139 int mapnum;
140 bool vm_crashsafe_match = true;
141 bool vm_must_add_frozenbit = false;
144 * Do the old and new cluster disagree on the crash-safetiness of the vm
145 * files? If so, do not copy them.
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;
152 * Do we need to rewrite visibilitymap?
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;
158 for (mapnum = 0; mapnum < size; mapnum++)
160 if (old_tablespace == NULL ||
161 strcmp(maps[mapnum].old_tablespace, old_tablespace) == 0)
163 /* transfer primary file */
164 transfer_relfile(&maps[mapnum], "", vm_must_add_frozenbit);
167 * Copy/link any fsm and vm files, if they exist
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);
178 * transfer_relfile()
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.
184 static void
185 transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit)
187 char old_file[MAXPGPATH];
188 char new_file[MAXPGPATH];
189 int segno;
190 char extent_suffix[65];
191 struct stat statbuf;
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.
198 for (segno = 0;; segno++)
200 if (segno == 0)
201 extent_suffix[0] = '\0';
202 else
203 snprintf(extent_suffix, sizeof(extent_suffix), ".%d", segno);
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);
220 /* Is it an extent, fsm, or vm file? */
221 if (type_suffix[0] != '\0' || segno != 0)
223 /* Did file open fail? */
224 if (stat(old_file, &statbuf) != 0)
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));
235 /* If file is empty, just return */
236 if (statbuf.st_size == 0)
237 return;
240 unlink(new_file);
242 /* Copying files might take some time, so give feedback. */
243 pg_log(PG_STATUS, "%s", old_file);
245 if (vm_must_add_frozenbit && strcmp(type_suffix, "_vm") == 0)
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);
252 else
253 switch (user_opts.transfer_mode)
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);