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