1212#include "access/transam.h"
1313
1414
15- static void get_db_infos (ClusterInfo * cluster );
16- static void print_db_arr (ClusterInfo * cluster );
17- static void print_rel_arr (RelInfoArr * arr );
18- static void get_rel_infos (ClusterInfo * cluster ,DbInfo * dbinfo );
19- static void free_rel_arr (RelInfoArr * rel_arr );
2015static void create_rel_filename_map (const char * old_data ,const char * new_data ,
2116const DbInfo * old_db ,const DbInfo * new_db ,
2217const RelInfo * old_rel ,const RelInfo * new_rel ,
2318FileNameMap * map );
19+ static void get_db_infos (ClusterInfo * cluster );
20+ static void get_rel_infos (ClusterInfo * cluster ,DbInfo * dbinfo );
21+ static void free_rel_infos (RelInfoArr * rel_arr );
22+ static void print_db_infos (DbInfoArr * dbinfo );
23+ static void print_rel_infos (RelInfoArr * arr );
2424
2525
2626/*
@@ -111,15 +111,15 @@ create_rel_filename_map(const char *old_data, const char *new_data,
111111
112112
113113void
114- print_maps (FileNameMap * maps ,int n ,const char * dbName )
114+ print_maps (FileNameMap * maps ,int n_maps ,const char * db_name )
115115{
116116if (log_opts .debug )
117117{
118118int mapnum ;
119119
120- pg_log (PG_DEBUG ,"mappings for db %s:\n" ,dbName );
120+ pg_log (PG_DEBUG ,"mappings for db %s:\n" ,db_name );
121121
122- for (mapnum = 0 ;mapnum < n ;mapnum ++ )
122+ for (mapnum = 0 ;mapnum < n_maps ;mapnum ++ )
123123pg_log (PG_DEBUG ,"%s.%s: %u to %u\n" ,
124124maps [mapnum ].nspname ,maps [mapnum ].relname ,
125125maps [mapnum ].old_relfilenode ,
@@ -130,6 +130,30 @@ print_maps(FileNameMap *maps, int n, const char *dbName)
130130}
131131
132132
133+ /*
134+ * get_db_and_rel_infos()
135+ *
136+ * higher level routine to generate dbinfos for the database running
137+ * on the given "port". Assumes that server is already running.
138+ */
139+ void
140+ get_db_and_rel_infos (ClusterInfo * cluster )
141+ {
142+ int dbnum ;
143+
144+ get_db_infos (cluster );
145+
146+ for (dbnum = 0 ;dbnum < cluster -> dbarr .ndbs ;dbnum ++ )
147+ get_rel_infos (cluster ,& cluster -> dbarr .dbs [dbnum ]);
148+
149+ if (log_opts .debug )
150+ {
151+ pg_log (PG_DEBUG ,"%s databases\n" ,CLUSTER_NAME (cluster ));
152+ print_db_infos (& cluster -> dbarr );
153+ }
154+ }
155+
156+
133157/*
134158 * get_db_infos()
135159 *
@@ -144,9 +168,7 @@ get_db_infos(ClusterInfo *cluster)
144168int ntups ;
145169int tupnum ;
146170DbInfo * dbinfos ;
147- int i_datname ;
148- int i_oid ;
149- int i_spclocation ;
171+ int i_datname ,i_oid ,i_spclocation ;
150172
151173res = executeQueryOrDie (conn ,
152174"SELECT d.oid, d.datname, t.spclocation "
@@ -182,27 +204,6 @@ get_db_infos(ClusterInfo *cluster)
182204}
183205
184206
185- /*
186- * get_db_and_rel_infos()
187- *
188- * higher level routine to generate dbinfos for the database running
189- * on the given "port". Assumes that server is already running.
190- */
191- void
192- get_db_and_rel_infos (ClusterInfo * cluster )
193- {
194- int dbnum ;
195-
196- get_db_infos (cluster );
197-
198- for (dbnum = 0 ;dbnum < cluster -> dbarr .ndbs ;dbnum ++ )
199- get_rel_infos (cluster ,& cluster -> dbarr .dbs [dbnum ]);
200-
201- if (log_opts .debug )
202- print_db_arr (cluster );
203- }
204-
205-
206207/*
207208 * get_rel_infos()
208209 *
@@ -224,27 +225,20 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
224225int num_rels = 0 ;
225226char * nspname = NULL ;
226227char * relname = NULL ;
227- int i_spclocation = -1 ;
228- int i_nspname = -1 ;
229- int i_relname = -1 ;
230- int i_oid = -1 ;
231- int i_relfilenode = -1 ;
232- int i_reltoastrelid = -1 ;
228+ int i_spclocation ,i_nspname ,i_relname ,i_oid ,i_relfilenode ;
233229char query [QUERY_ALLOC ];
234230
235231/*
236232 * pg_largeobject contains user data that does not appear in pg_dumpall
237233 * --schema-only output, so we have to copy that system table heap and
238- * index. Ideally we could just get the relfilenode from template1 but
239- * pg_largeobject_loid_pn_index's relfilenode can change if the table was
240- * reindexed so we get the relfilenode for each database and upgrade it as
241- * a normal user table.
242- * Order by tablespace so we can cache the directory contents efficiently.
234+ * index. We could grab the pg_largeobject oids from template1, but
235+ * it is easy to treat it as a normal table.
236+ * Order by oid so we can join old/new structures efficiently.
243237 */
244238
245239snprintf (query ,sizeof (query ),
246- "SELECTDISTINCT c.oid, n.nspname, c.relname, "
247- "c.relfilenode,c.reltoastrelid, t.spclocation "
240+ "SELECT c.oid, n.nspname, c.relname, "
241+ "c.relfilenode, t.spclocation "
248242"FROM pg_catalog.pg_class c JOIN "
249243"pg_catalog.pg_namespace n "
250244"ON c.relnamespace = n.oid "
@@ -256,10 +250,7 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
256250"n.nspname = 'pg_catalog' "
257251"AND relname IN "
258252" ('pg_largeobject', 'pg_largeobject_loid_pn_index'%s) )) "
259- "AND relkind IN ('r','t', 'i'%s)"
260- "GROUP BY c.oid, n.nspname, c.relname, c.relfilenode,"
261- "c.reltoastrelid, t.spclocation, "
262- "n.nspname "
253+ "AND relkind IN ('r','t', 'i'%s) "
263254/* we preserve pg_class.oid so we sort by it to match old/new */
264255"ORDER BY 1;" ,
265256FirstNormalObjectId ,
@@ -280,7 +271,6 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
280271i_nspname = PQfnumber (res ,"nspname" );
281272i_relname = PQfnumber (res ,"relname" );
282273i_relfilenode = PQfnumber (res ,"relfilenode" );
283- i_reltoastrelid = PQfnumber (res ,"reltoastrelid" );
284274i_spclocation = PQfnumber (res ,"spclocation" );
285275
286276for (relnum = 0 ;relnum < ntups ;relnum ++ )
@@ -297,7 +287,6 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
297287strlcpy (curr -> relname ,relname ,sizeof (curr -> relname ));
298288
299289curr -> relfilenode = atooid (PQgetvalue (res ,relnum ,i_relfilenode ));
300- curr -> toastrelid = atooid (PQgetvalue (res ,relnum ,i_reltoastrelid ));
301290
302291tblspace = PQgetvalue (res ,relnum ,i_spclocation );
303292/* if no table tablespace, use the database tablespace */
@@ -314,43 +303,42 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
314303}
315304
316305
317- static void
318- free_rel_arr (RelInfoArr * rel_arr )
319- {
320- pg_free (rel_arr -> rels );
321- rel_arr -> nrels = 0 ;
322- }
323-
324-
325306void
326- dbarr_free (DbInfoArr * db_arr )
307+ free_db_and_rel_infos (DbInfoArr * db_arr )
327308{
328309int dbnum ;
329310
330311for (dbnum = 0 ;dbnum < db_arr -> ndbs ;dbnum ++ )
331- free_rel_arr (& db_arr -> dbs [dbnum ].rel_arr );
312+ free_rel_infos (& db_arr -> dbs [dbnum ].rel_arr );
313+ pg_free (db_arr -> dbs );
332314db_arr -> ndbs = 0 ;
333315}
334316
335317
336318static void
337- print_db_arr ( ClusterInfo * cluster )
319+ free_rel_infos ( RelInfoArr * rel_arr )
338320{
339- int dbnum ;
321+ pg_free (rel_arr -> rels );
322+ rel_arr -> nrels = 0 ;
323+ }
340324
341- pg_log (PG_DEBUG ,"%s databases\n" ,CLUSTER_NAME (cluster ));
342325
343- for (dbnum = 0 ;dbnum < cluster -> dbarr .ndbs ;dbnum ++ )
326+ static void
327+ print_db_infos (DbInfoArr * db_arr )
328+ {
329+ int dbnum ;
330+
331+ for (dbnum = 0 ;dbnum < db_arr -> ndbs ;dbnum ++ )
344332{
345- pg_log (PG_DEBUG ,"Database: %s\n" ,cluster -> dbarr . dbs [dbnum ].db_name );
346- print_rel_arr ( & cluster -> dbarr . dbs [dbnum ].rel_arr );
333+ pg_log (PG_DEBUG ,"Database: %s\n" ,db_arr -> dbs [dbnum ].db_name );
334+ print_rel_infos ( & db_arr -> dbs [dbnum ].rel_arr );
347335pg_log (PG_DEBUG ,"\n\n" );
348336}
349337}
350338
351339
352340static void
353- print_rel_arr (RelInfoArr * arr )
341+ print_rel_infos (RelInfoArr * arr )
354342{
355343int relnum ;
356344