PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
relfilenumber.c File Reference
#include "postgres_fe.h"
#include <sys/stat.h>
#include "pg_upgrade.h"
Include dependency graph for relfilenumber.c:

Go to the source code of this file.

Functions

static void transfer_single_new_db (FileNameMap *maps, int size, char *old_tablespace)
 
static void transfer_relfile (FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit)
 
void transfer_all_new_tablespaces (DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
 
void transfer_all_new_dbs (DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata, char *old_tablespace)
 

Function Documentation

◆ transfer_all_new_dbs()

void transfer_all_new_dbs ( DbInfoArr old_db_arr,
DbInfoArr new_db_arr,
char *  old_pgdata,
char *  new_pgdata,
char *  old_tablespace 
)

Definition at line 87 of file relfilenumber.c.

89{
90 int old_dbnum,
91 new_dbnum;
92
93 /* Scan the old cluster databases and transfer their files */
94 for (old_dbnum = new_dbnum = 0;
95 old_dbnum < old_db_arr->ndbs;
96 old_dbnum++, new_dbnum++)
97 {
98 DbInfo *old_db = &old_db_arr->dbs[old_dbnum],
99 *new_db = NULL;
100 FileNameMap *mappings;
101 int n_maps;
102
103 /*
104 * Advance past any databases that exist in the new cluster but not in
105 * the old, e.g. "postgres". (The user might have removed the
106 * 'postgres' database from the old cluster.)
107 */
108 for (; new_dbnum < new_db_arr->ndbs; new_dbnum++)
109 {
110 new_db = &new_db_arr->dbs[new_dbnum];
111 if (strcmp(old_db->db_name, new_db->db_name) == 0)
112 break;
113 }
114
115 if (new_dbnum >= new_db_arr->ndbs)
116 pg_fatal("old database \"%s\" not found in the new cluster",
117 old_db->db_name);
118
119 mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
120 new_pgdata);
121 if (n_maps)
122 {
123 transfer_single_new_db(mappings, n_maps, old_tablespace);
124 }
125 /* We allocate something even for n_maps == 0 */
126 pg_free(mappings);
127 }
128}
void pg_free(void *ptr)
Definition: fe_memutils.c:105
FileNameMap * gen_db_file_maps(DbInfo *old_db, DbInfo *new_db, int *nmaps, const char *old_pgdata, const char *new_pgdata)
Definition: info.c:45
#define pg_fatal(...)
static void transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace)
DbInfo * dbs
Definition: pg_upgrade.h:215
char * db_name
Definition: pg_upgrade.h:194

References DbInfo::db_name, DbInfoArr::dbs, gen_db_file_maps(), DbInfoArr::ndbs, pg_fatal, pg_free(), and transfer_single_new_db().

Referenced by parallel_transfer_all_new_dbs().

◆ transfer_all_new_tablespaces()

void transfer_all_new_tablespaces ( DbInfoArr old_db_arr,
DbInfoArr new_db_arr,
char *  old_pgdata,
char *  new_pgdata 
)

Definition at line 27 of file relfilenumber.c.

29{
31 {
33 prep_status_progress("Cloning user relation files");
34 break;
36 prep_status_progress("Copying user relation files");
37 break;
39 prep_status_progress("Copying user relation files with copy_file_range");
40 break;
42 prep_status_progress("Linking user relation files");
43 break;
44 }
45
46 /*
47 * Transferring files by tablespace is tricky because a single database
48 * can use multiple tablespaces. For non-parallel mode, we just pass a
49 * NULL tablespace path, which matches all tablespaces. In parallel mode,
50 * we pass the default tablespace and all user-created tablespaces and let
51 * those operations happen in parallel.
52 */
53 if (user_opts.jobs <= 1)
54 parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
55 new_pgdata, NULL);
56 else
57 {
58 int tblnum;
59
60 /* transfer default tablespace */
61 parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
62 new_pgdata, old_pgdata);
63
64 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
66 new_db_arr,
67 old_pgdata,
68 new_pgdata,
69 os_info.old_tablespaces[tblnum]);
70 /* reap all children */
71 while (reap_child(true) == true)
72 ;
73 }
74
76 check_ok();
77}
void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata, char *old_tablespace)
Definition: parallel.c:172
bool reap_child(bool wait_for_child)
Definition: parallel.c:278
static void check_ok(void)
Definition: initdb.c:2119
OSInfo os_info
Definition: pg_upgrade.c:69
@ TRANSFER_MODE_COPY
Definition: pg_upgrade.h:256
@ TRANSFER_MODE_LINK
Definition: pg_upgrade.h:258
@ TRANSFER_MODE_CLONE
Definition: pg_upgrade.h:255
@ TRANSFER_MODE_COPY_FILE_RANGE
Definition: pg_upgrade.h:257
void void prep_status_progress(const char *fmt,...) pg_attribute_printf(1
void end_progress_output(void)
Definition: util.c:43
UserOpts user_opts
Definition: option.c:30
int num_old_tablespaces
Definition: pg_upgrade.h:347
char ** old_tablespaces
Definition: pg_upgrade.h:346
transferMode transfer_mode
Definition: pg_upgrade.h:326
int jobs
Definition: pg_upgrade.h:327

References check_ok(), end_progress_output(), UserOpts::jobs, OSInfo::num_old_tablespaces, OSInfo::old_tablespaces, os_info, parallel_transfer_all_new_dbs(), prep_status_progress(), reap_child(), UserOpts::transfer_mode, TRANSFER_MODE_CLONE, TRANSFER_MODE_COPY, TRANSFER_MODE_COPY_FILE_RANGE, TRANSFER_MODE_LINK, and user_opts.

Referenced by main().

◆ transfer_relfile()

static void transfer_relfile ( FileNameMap map,
const char *  type_suffix,
bool  vm_must_add_frozenbit 
)
static

Definition at line 174 of file relfilenumber.c.

175{
176 char old_file[MAXPGPATH];
177 char new_file[MAXPGPATH];
178 int segno;
179 char extent_suffix[65];
180 struct stat statbuf;
181
182 /*
183 * Now copy/link any related segments as well. Remember, PG breaks large
184 * files into 1GB segments, the first segment has no extension, subsequent
185 * segments are named relfilenumber.1, relfilenumber.2, relfilenumber.3.
186 */
187 for (segno = 0;; segno++)
188 {
189 if (segno == 0)
190 extent_suffix[0] = '\0';
191 else
192 snprintf(extent_suffix, sizeof(extent_suffix), ".%d", segno);
193
194 snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
195 map->old_tablespace,
197 map->db_oid,
198 map->relfilenumber,
199 type_suffix,
200 extent_suffix);
201 snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
202 map->new_tablespace,
204 map->db_oid,
205 map->relfilenumber,
206 type_suffix,
207 extent_suffix);
208
209 /* Is it an extent, fsm, or vm file? */
210 if (type_suffix[0] != '\0' || segno != 0)
211 {
212 /* Did file open fail? */
213 if (stat(old_file, &statbuf) != 0)
214 {
215 /* File does not exist? That's OK, just return */
216 if (errno == ENOENT)
217 return;
218 else
219 pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %m",
220 map->nspname, map->relname, old_file, new_file);
221 }
222
223 /* If file is empty, just return */
224 if (statbuf.st_size == 0)
225 return;
226 }
227
228 unlink(new_file);
229
230 /* Copying files might take some time, so give feedback. */
231 pg_log(PG_STATUS, "%s", old_file);
232
233 if (vm_must_add_frozenbit && strcmp(type_suffix, "_vm") == 0)
234 {
235 /* Need to rewrite visibility map format */
236 pg_log(PG_VERBOSE, "rewriting \"%s\" to \"%s\"",
237 old_file, new_file);
238 rewriteVisibilityMap(old_file, new_file, map->nspname, map->relname);
239 }
240 else
241 switch (user_opts.transfer_mode)
242 {
244 pg_log(PG_VERBOSE, "cloning \"%s\" to \"%s\"",
245 old_file, new_file);
246 cloneFile(old_file, new_file, map->nspname, map->relname);
247 break;
249 pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\"",
250 old_file, new_file);
251 copyFile(old_file, new_file, map->nspname, map->relname);
252 break;
254 pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\" with copy_file_range",
255 old_file, new_file);
256 copyFileByRange(old_file, new_file, map->nspname, map->relname);
257 break;
259 pg_log(PG_VERBOSE, "linking \"%s\" to \"%s\"",
260 old_file, new_file);
261 linkFile(old_file, new_file, map->nspname, map->relname);
262 }
263 }
264}
void linkFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:190
void rewriteVisibilityMap(const char *fromfile, const char *tofile, const char *schemaName, const char *relName)
Definition: file.c:216
void cloneFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:39
void copyFileByRange(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:151
void copyFile(const char *src, const char *dst, const char *schemaName, const char *relName)
Definition: file.c:82
#define MAXPGPATH
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ PG_STATUS
Definition: pg_upgrade.h:267
@ PG_VERBOSE
Definition: pg_upgrade.h:266
#define snprintf
Definition: port.h:238
const char * new_tablespace
Definition: pg_upgrade.h:178
const char * old_tablespace_suffix
Definition: pg_upgrade.h:179
const char * old_tablespace
Definition: pg_upgrade.h:177
RelFileNumber relfilenumber
Definition: pg_upgrade.h:182
char * relname
Definition: pg_upgrade.h:185
char * nspname
Definition: pg_upgrade.h:184
const char * new_tablespace_suffix
Definition: pg_upgrade.h:180
#define stat
Definition: win32_port.h:274

References cloneFile(), copyFile(), copyFileByRange(), FileNameMap::db_oid, linkFile(), MAXPGPATH, FileNameMap::new_tablespace, FileNameMap::new_tablespace_suffix, FileNameMap::nspname, FileNameMap::old_tablespace, FileNameMap::old_tablespace_suffix, pg_fatal, pg_log(), PG_STATUS, PG_VERBOSE, FileNameMap::relfilenumber, FileNameMap::relname, rewriteVisibilityMap(), snprintf, stat::st_size, stat, UserOpts::transfer_mode, TRANSFER_MODE_CLONE, TRANSFER_MODE_COPY, TRANSFER_MODE_COPY_FILE_RANGE, TRANSFER_MODE_LINK, and user_opts.

Referenced by transfer_single_new_db().

◆ transfer_single_new_db()

static void transfer_single_new_db ( FileNameMap maps,
int  size,
char *  old_tablespace 
)
static

Definition at line 136 of file relfilenumber.c.

137{
138 int mapnum;
139 bool vm_must_add_frozenbit = false;
140
141 /*
142 * Do we need to rewrite visibilitymap?
143 */
146 vm_must_add_frozenbit = true;
147
148 for (mapnum = 0; mapnum < size; mapnum++)
149 {
150 if (old_tablespace == NULL ||
151 strcmp(maps[mapnum].old_tablespace, old_tablespace) == 0)
152 {
153 /* transfer primary file */
154 transfer_relfile(&maps[mapnum], "", vm_must_add_frozenbit);
155
156 /*
157 * Copy/link any fsm and vm files, if they exist
158 */
159 transfer_relfile(&maps[mapnum], "_fsm", vm_must_add_frozenbit);
160 transfer_relfile(&maps[mapnum], "_vm", vm_must_add_frozenbit);
161 }
162 }
163}
ClusterInfo new_cluster
Definition: pg_upgrade.c:68
ClusterInfo old_cluster
Definition: pg_upgrade.c:67
#define VISIBILITY_MAP_FROZEN_BIT_CAT_VER
Definition: pg_upgrade.h:107
static void transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit)
static pg_noinline void Size size
Definition: slab.c:607
ControlData controldata
Definition: pg_upgrade.h:282
uint32 cat_ver
Definition: pg_upgrade.h:227
static const pg_conv_map maps[]

References ControlData::cat_ver, ClusterInfo::controldata, maps, new_cluster, old_cluster, size, transfer_relfile(), and VISIBILITY_MAP_FROZEN_BIT_CAT_VER.

Referenced by transfer_all_new_dbs().