@@ -33,8 +33,6 @@ static RelInfo *relarr_lookup_rel_oid(ClusterInfo *cluster, RelInfoArr *rel_arr,
3333 * generates database mappings for "old_db" and "new_db". Returns a malloc'ed
3434 * array of mappings. nmaps is a return parameter which refers to the number
3535 * mappings.
36- *
37- * NOTE: Its the Caller's responsibility to free the returned array.
3836 */
3937FileNameMap *
4038gen_db_file_maps (DbInfo * old_db ,DbInfo * new_db ,
@@ -45,72 +43,56 @@ gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
4543int num_maps = 0 ;
4644
4745maps = (FileNameMap * )pg_malloc (sizeof (FileNameMap )*
48- new_db -> rel_arr .nrels );
46+ old_db -> rel_arr .nrels );
4947
50- for (relnum = 0 ;relnum < new_db -> rel_arr .nrels ;relnum ++ )
48+ for (relnum = 0 ;relnum < old_db -> rel_arr .nrels ;relnum ++ )
5149{
52- RelInfo * newrel = & new_db -> rel_arr .rels [relnum ];
53- RelInfo * oldrel ;
50+ RelInfo * oldrel = & old_db -> rel_arr .rels [relnum ];
51+ RelInfo * newrel ;
5452
55- /* toast tables are handled by theirparent */
56- if (strcmp (newrel -> nspname ,"pg_toast" )== 0 )
53+ /* toast tables are handled by theirparents */
54+ if (strcmp (oldrel -> nspname ,"pg_toast" )== 0 )
5755continue ;
5856
59- oldrel = relarr_lookup_rel_name (& old_cluster ,& old_db -> rel_arr ,
60- newrel -> nspname ,newrel -> relname );
57+ newrel = relarr_lookup_rel_name (& old_cluster ,& old_db -> rel_arr ,
58+ oldrel -> nspname ,oldrel -> relname );
6159
6260create_rel_filename_map (old_pgdata ,new_pgdata ,old_db ,new_db ,
6361oldrel ,newrel ,maps + num_maps );
6462num_maps ++ ;
6563
6664/*
6765 * So much for mapping this relation; now we need a mapping
68- * for its corresponding toast relation, if any.
66+ * for its corresponding toast relation and toast index , if any.
6967 */
7068if (oldrel -> toastrelid > 0 )
7169{
72- RelInfo * new_toast ;
73- RelInfo * old_toast ;
74- char new_name [MAXPGPATH ];
75- char old_name [MAXPGPATH ];
76-
77- /* construct the new and old relnames for the toast relation */
78- snprintf (old_name ,sizeof (old_name ),"pg_toast_%u" ,oldrel -> reloid );
79- snprintf (new_name ,sizeof (new_name ),"pg_toast_%u" ,newrel -> reloid );
70+ char old_name [MAXPGPATH ],new_name [MAXPGPATH ];
71+ RelInfo * old_toast ,* new_toast ;
8072
81- /* look them up in their respective arrays */
8273old_toast = relarr_lookup_rel_oid (& old_cluster ,& old_db -> rel_arr ,
83- oldrel -> toastrelid );
84- new_toast = relarr_lookup_rel_name (& new_cluster ,& new_db -> rel_arr ,
85- "pg_toast" , new_name );
74+ oldrel -> toastrelid );
75+ new_toast = relarr_lookup_rel_oid (& new_cluster ,& new_db -> rel_arr ,
76+ newrel -> toastrelid );
8677
87- /* finally create a mapping for them */
8878create_rel_filename_map (old_pgdata ,new_pgdata ,old_db ,new_db ,
8979old_toast ,new_toast ,maps + num_maps );
9080num_maps ++ ;
9181
9282/*
93- * also need to provide a mapping for the index of this toast
83+ *We also need to provide a mapping for the index of this toast
9484 * relation. The procedure is similar to what we did above for
9585 * toast relation itself, the only difference being that the
9686 * relnames need to be appended with _index.
9787 */
98-
99- /*
100- * construct the new and old relnames for the toast index
101- * relations
102- */
10388snprintf (old_name ,sizeof (old_name ),"%s_index" ,old_toast -> relname );
104- snprintf (new_name ,sizeof (new_name ),"pg_toast_%u_index" ,
105- newrel -> reloid );
89+ snprintf (new_name ,sizeof (new_name ),"%s_index" ,new_toast -> relname );
10690
107- /* look them up in their respective arrays */
10891old_toast = relarr_lookup_rel_name (& old_cluster ,& old_db -> rel_arr ,
10992"pg_toast" ,old_name );
11093new_toast = relarr_lookup_rel_name (& new_cluster ,& new_db -> rel_arr ,
11194"pg_toast" ,new_name );
11295
113- /* finally create a mapping for them */
11496create_rel_filename_map (old_pgdata ,new_pgdata ,old_db ,
11597new_db ,old_toast ,new_toast ,maps + num_maps );
11698num_maps ++ ;
@@ -133,15 +115,6 @@ create_rel_filename_map(const char *old_data, const char *new_data,
133115const RelInfo * old_rel ,const RelInfo * new_rel ,
134116FileNameMap * map )
135117{
136- map -> old_relfilenode = old_rel -> relfilenode ;
137- map -> new_relfilenode = new_rel -> relfilenode ;
138-
139- snprintf (map -> old_nspname ,sizeof (map -> old_nspname ),"%s" ,old_rel -> nspname );
140- snprintf (map -> new_nspname ,sizeof (map -> new_nspname ),"%s" ,new_rel -> nspname );
141-
142- snprintf (map -> old_relname ,sizeof (map -> old_relname ),"%s" ,old_rel -> relname );
143- snprintf (map -> new_relname ,sizeof (map -> new_relname ),"%s" ,new_rel -> relname );
144-
145118if (strlen (old_rel -> tablespace )== 0 )
146119{
147120/*
@@ -155,14 +128,21 @@ create_rel_filename_map(const char *old_data, const char *new_data,
155128}
156129else
157130{
158- /*
159- * relation belongs to some tablespace, so use the tablespace location
160- */
131+ /* relation belongs to a tablespace, so use the tablespace location */
161132snprintf (map -> old_dir ,sizeof (map -> old_dir ),"%s%s/%u" ,old_rel -> tablespace ,
162133old_cluster .tablespace_suffix ,old_db -> db_oid );
163134snprintf (map -> new_dir ,sizeof (map -> new_dir ),"%s%s/%u" ,new_rel -> tablespace ,
164135new_cluster .tablespace_suffix ,new_db -> db_oid );
165136}
137+
138+ map -> old_relfilenode = old_rel -> relfilenode ;
139+ map -> new_relfilenode = new_rel -> relfilenode ;
140+
141+ /* used only for logging and error reporing */
142+ snprintf (map -> old_nspname ,sizeof (map -> old_nspname ),"%s" ,old_rel -> nspname );
143+ snprintf (map -> new_nspname ,sizeof (map -> new_nspname ),"%s" ,new_rel -> nspname );
144+ snprintf (map -> old_relname ,sizeof (map -> old_relname ),"%s" ,old_rel -> relname );
145+ snprintf (map -> new_relname ,sizeof (map -> new_relname ),"%s" ,new_rel -> relname );
166146}
167147
168148