4 * relfilenode functions
6 * Copyright (c) 2010-2012, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/relfilenode.c
12 #include "pg_upgrade.h"
14 #include "catalog/pg_class.h"
15 #include "access/transam.h"
18 static void transfer_single_new_db(pageCnvCtx *pageConverter,
19 FileNameMap *maps, int size);
20 static void transfer_relfile(pageCnvCtx *pageConverter,
21 const char *fromfile, const char *tofile,
22 const char *nspname, const char *relname);
24 /* used by scandir(), must be global */
25 char scandir_file_pattern[MAXPGPATH];
28 * transfer_all_new_dbs()
30 * Responsible for upgrading all database. invokes routines to generate mappings and then
31 * physically link the databases.
34 transfer_all_new_dbs(DbInfoArr *old_db_arr,
35 DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
37 int old_dbnum, new_dbnum;
38 const char *msg = NULL;
40 prep_status("%s user relation files\n",
41 user_opts.transfer_mode == TRANSFER_MODE_LINK ? "Linking" : "Copying");
43 /* Scan the old cluster databases and transfer their files */
44 for (old_dbnum = new_dbnum = 0;
45 old_dbnum < old_db_arr->ndbs;
46 old_dbnum++, new_dbnum++)
48 DbInfo *old_db = &old_db_arr->dbs[old_dbnum], *new_db = NULL;
49 FileNameMap *mappings;
51 pageCnvCtx *pageConverter = NULL;
54 * Advance past any databases that exist in the new cluster
55 * but not in the old, e.g. "postgres". (The user might
56 * have removed the 'postgres' database from the old cluster.)
58 for (; new_dbnum < new_db_arr->ndbs; new_dbnum++)
60 new_db = &new_db_arr->dbs[new_dbnum];
61 if (strcmp(old_db->db_name, new_db->db_name) == 0)
65 if (new_dbnum >= new_db_arr->ndbs)
66 pg_log(PG_FATAL, "old database \"%s\" not found in the new cluster\n",
70 mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
75 print_maps(mappings, n_maps, new_db->db_name);
77 #ifdef PAGE_CONVERSION
78 msg = setupPageConverter(&pageConverter);
80 transfer_single_new_db(pageConverter, mappings, n_maps);
86 prep_status(" "); /* in case nothing printed; pass a space so gcc
87 * doesn't complain about empty format
96 * get_pg_database_relfilenode()
98 * Retrieves the relfilenode for a few system-catalog tables. We need these
99 * relfilenodes later in the upgrade process.
102 get_pg_database_relfilenode(ClusterInfo *cluster)
104 PGconn *conn = connectToServer(cluster, "template1");
108 res = executeQueryOrDie(conn,
109 "SELECT c.relname, c.relfilenode "
110 "FROM pg_catalog.pg_class c, "
111 " pg_catalog.pg_namespace n "
112 "WHERE c.relnamespace = n.oid AND "
113 " n.nspname = 'pg_catalog' AND "
114 " c.relname = 'pg_database' "
115 "ORDER BY c.relname");
117 i_relfile = PQfnumber(res, "relfilenode");
118 cluster->pg_database_oid = atooid(PQgetvalue(res, 0, i_relfile));
126 * transfer_single_new_db()
128 * create links for mappings stored in "maps" array.
131 transfer_single_new_db(pageCnvCtx *pageConverter,
132 FileNameMap *maps, int size)
134 char old_dir[MAXPGPATH];
135 struct dirent **namelist = NULL;
139 bool vm_crashsafe_change = false;
143 /* Do not copy non-crashsafe vm files for binaries that assume crashsafety */
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_change = true;
148 for (mapnum = 0; mapnum < size; mapnum++)
150 char old_file[MAXPGPATH];
151 char new_file[MAXPGPATH];
153 /* Changed tablespaces? Need a new directory scan? */
154 if (strcmp(maps[mapnum].old_dir, old_dir) != 0)
158 for (fileno = 0; fileno < numFiles; fileno++)
159 pg_free(namelist[fileno]);
163 snprintf(old_dir, sizeof(old_dir), "%s", maps[mapnum].old_dir);
164 numFiles = load_directory(old_dir, &namelist);
167 /* Copying files might take some time, so give feedback. */
169 snprintf(old_file, sizeof(old_file), "%s/%u", maps[mapnum].old_dir,
170 maps[mapnum].old_relfilenode);
171 snprintf(new_file, sizeof(new_file), "%s/%u", maps[mapnum].new_dir,
172 maps[mapnum].new_relfilenode);
173 pg_log(PG_REPORT, OVERWRITE_MESSAGE, old_file);
176 * Copy/link the relation file to the new cluster
179 transfer_relfile(pageConverter, old_file, new_file,
180 maps[mapnum].nspname, maps[mapnum].relname);
182 /* fsm/vm files added in PG 8.4 */
183 if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
186 * Copy/link any fsm and vm files, if they exist
188 snprintf(scandir_file_pattern, sizeof(scandir_file_pattern), "%u_",
189 maps[mapnum].old_relfilenode);
191 for (fileno = 0; fileno < numFiles; fileno++)
193 char *vm_offset = strstr(namelist[fileno]->d_name, "_vm");
194 bool is_vm_file = false;
196 /* Is a visibility map file? (name ends with _vm) */
197 if (vm_offset && strlen(vm_offset) == strlen("_vm"))
200 if (strncmp(namelist[fileno]->d_name, scandir_file_pattern,
201 strlen(scandir_file_pattern)) == 0 &&
202 (!is_vm_file || !vm_crashsafe_change))
204 snprintf(old_file, sizeof(old_file), "%s/%s", maps[mapnum].old_dir,
205 namelist[fileno]->d_name);
206 snprintf(new_file, sizeof(new_file), "%s/%u%s", maps[mapnum].new_dir,
207 maps[mapnum].new_relfilenode, strchr(namelist[fileno]->d_name, '_'));
210 transfer_relfile(pageConverter, old_file, new_file,
211 maps[mapnum].nspname, maps[mapnum].relname);
217 * Now copy/link any related segments as well. Remember, PG breaks
218 * large files into 1GB segments, the first segment has no extension,
219 * subsequent segments are named relfilenode.1, relfilenode.2,
220 * relfilenode.3, ... 'fsm' and 'vm' files use underscores so are not
223 snprintf(scandir_file_pattern, sizeof(scandir_file_pattern), "%u.",
224 maps[mapnum].old_relfilenode);
226 for (fileno = 0; fileno < numFiles; fileno++)
228 if (strncmp(namelist[fileno]->d_name, scandir_file_pattern,
229 strlen(scandir_file_pattern)) == 0)
231 snprintf(old_file, sizeof(old_file), "%s/%s", maps[mapnum].old_dir,
232 namelist[fileno]->d_name);
233 snprintf(new_file, sizeof(new_file), "%s/%u%s", maps[mapnum].new_dir,
234 maps[mapnum].new_relfilenode, strchr(namelist[fileno]->d_name, '.'));
237 transfer_relfile(pageConverter, old_file, new_file,
238 maps[mapnum].nspname, maps[mapnum].relname);
246 for (fileno = 0; fileno < numFiles; fileno++)
247 pg_free(namelist[fileno]);
256 * Copy or link file from old cluster to new one.
259 transfer_relfile(pageCnvCtx *pageConverter, const char *old_file,
260 const char *new_file, const char *nspname, const char *relname)
264 if ((user_opts.transfer_mode == TRANSFER_MODE_LINK) && (pageConverter != NULL))
265 pg_log(PG_FATAL, "This upgrade requires page-by-page conversion, "
266 "you must use copy mode instead of link mode.\n");
268 if (user_opts.transfer_mode == TRANSFER_MODE_COPY)
270 pg_log(PG_INFO, "copying \"%s\" to \"%s\"\n", old_file, new_file);
272 if ((msg = copyAndUpdateFile(pageConverter, old_file, new_file, true)) != NULL)
273 pg_log(PG_FATAL, "error while copying relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
274 nspname, relname, old_file, new_file, msg);
278 pg_log(PG_INFO, "linking \"%s\" to \"%s\"\n", old_file, new_file);
280 if ((msg = linkAndUpdateFile(pageConverter, old_file, new_file)) != NULL)
282 "error while creating link for relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
283 nspname, relname, old_file, new_file, msg);