]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/info.c
In pg_upgrade, add various logging improvements:
[postgresql] / contrib / pg_upgrade / info.c
1 /*
2  *      info.c
3  *
4  *      information support functions
5  *
6  *      Copyright (c) 2010-2012, PostgreSQL Global Development Group
7  *      contrib/pg_upgrade/info.c
8  */
9
10 #include "postgres.h"
11
12 #include "pg_upgrade.h"
13
14 #include "access/transam.h"
15
16
17 static void create_rel_filename_map(const char *old_data, const char *new_data,
18                                                 const DbInfo *old_db, const DbInfo *new_db,
19                                                 const RelInfo *old_rel, const RelInfo *new_rel,
20                                                 FileNameMap *map);
21 static void get_db_infos(ClusterInfo *cluster);
22 static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo);
23 static void free_rel_infos(RelInfoArr *rel_arr);
24 static void print_db_infos(DbInfoArr *dbinfo);
25 static void print_rel_infos(RelInfoArr *arr);
26
27
28 /*
29  * gen_db_file_maps()
30  *
31  * generates database mappings for "old_db" and "new_db". Returns a malloc'ed
32  * array of mappings. nmaps is a return parameter which refers to the number
33  * mappings.
34  */
35 FileNameMap *
36 gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
37                                  int *nmaps, const char *old_pgdata, const char *new_pgdata)
38 {
39         FileNameMap *maps;
40         int                     relnum;
41         int                     num_maps = 0;
42
43         if (old_db->rel_arr.nrels != new_db->rel_arr.nrels)
44                 pg_log(PG_FATAL, "old and new databases \"%s\" have a different number of relations\n",
45                            old_db->db_name);
46
47         maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
48                                                                          old_db->rel_arr.nrels);
49
50         for (relnum = 0; relnum < old_db->rel_arr.nrels; relnum++)
51         {
52                 RelInfo    *old_rel = &old_db->rel_arr.rels[relnum];
53                 RelInfo    *new_rel = &new_db->rel_arr.rels[relnum];
54
55                 if (old_rel->reloid != new_rel->reloid)
56                         pg_log(PG_FATAL, "Mismatch of relation OID in database \"%s\": old OID %d, new OID %d\n",
57                                    old_db->db_name, old_rel->reloid, new_rel->reloid);
58
59                 /*
60                  * TOAST table names initially match the heap pg_class oid.
61                  * In pre-8.4, TOAST table names change during CLUSTER; in pre-9.0,
62                  * TOAST table names change during ALTER TABLE ALTER COLUMN SET TYPE.
63                  * In >= 9.0, TOAST relation names always use heap table oids, hence
64                  * we cannot check relation names when upgrading from pre-9.0.
65                  * Clusters upgraded to 9.0 will get matching TOAST names.
66                  */
67                 if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
68                         ((GET_MAJOR_VERSION(old_cluster.major_version) >= 900 ||
69                           strcmp(old_rel->nspname, "pg_toast") != 0) &&
70                          strcmp(old_rel->relname, new_rel->relname) != 0))
71                         pg_log(PG_FATAL, "Mismatch of relation names in database \"%s\": "
72                                    "old name \"%s.%s\", new name \"%s.%s\"\n",
73                                    old_db->db_name, old_rel->nspname, old_rel->relname,
74                                    new_rel->nspname, new_rel->relname);
75
76                 create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
77                                                                 old_rel, new_rel, maps + num_maps);
78                 num_maps++;
79         }
80
81         *nmaps = num_maps;
82         return maps;
83 }
84
85
86 /*
87  * create_rel_filename_map()
88  *
89  * fills a file node map structure and returns it in "map".
90  */
91 static void
92 create_rel_filename_map(const char *old_data, const char *new_data,
93                                                 const DbInfo *old_db, const DbInfo *new_db,
94                                                 const RelInfo *old_rel, const RelInfo *new_rel,
95                                                 FileNameMap *map)
96 {
97         if (strlen(old_rel->tablespace) == 0)
98         {
99                 /*
100                  * relation belongs to the default tablespace, hence relfiles should
101                  * exist in the data directories.
102                  */
103                 snprintf(map->old_dir, sizeof(map->old_dir), "%s/base/%u", old_data,
104                                  old_db->db_oid);
105                 snprintf(map->new_dir, sizeof(map->new_dir), "%s/base/%u", new_data,
106                                  new_db->db_oid);
107         }
108         else
109         {
110                 /* relation belongs to a tablespace, so use the tablespace location */
111                 snprintf(map->old_dir, sizeof(map->old_dir), "%s%s/%u", old_rel->tablespace,
112                                  old_cluster.tablespace_suffix, old_db->db_oid);
113                 snprintf(map->new_dir, sizeof(map->new_dir), "%s%s/%u", new_rel->tablespace,
114                                  new_cluster.tablespace_suffix, new_db->db_oid);
115         }
116
117         /*
118          * old_relfilenode might differ from pg_class.oid (and hence
119          * new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
120          */
121         map->old_relfilenode = old_rel->relfilenode;
122
123         /* new_relfilenode will match old and new pg_class.oid */
124         map->new_relfilenode = new_rel->relfilenode;
125
126         /* used only for logging and error reporing, old/new are identical */
127         snprintf(map->nspname, sizeof(map->nspname), "%s", old_rel->nspname);
128         snprintf(map->relname, sizeof(map->relname), "%s", old_rel->relname);
129 }
130
131
132 void
133 print_maps(FileNameMap *maps, int n_maps, const char *db_name)
134 {
135         if (log_opts.verbose)
136         {
137                 int                     mapnum;
138
139                 pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
140
141                 for (mapnum = 0; mapnum < n_maps; mapnum++)
142                         pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
143                                    maps[mapnum].nspname, maps[mapnum].relname,
144                                    maps[mapnum].old_relfilenode,
145                                    maps[mapnum].new_relfilenode);
146
147                 pg_log(PG_VERBOSE, "\n\n");
148         }
149 }
150
151
152 /*
153  * get_db_and_rel_infos()
154  *
155  * higher level routine to generate dbinfos for the database running
156  * on the given "port". Assumes that server is already running.
157  */
158 void
159 get_db_and_rel_infos(ClusterInfo *cluster)
160 {
161         int                     dbnum;
162
163         if (cluster->dbarr.dbs != NULL)
164                 free_db_and_rel_infos(&cluster->dbarr);
165
166         get_db_infos(cluster);
167
168         for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
169                 get_rel_infos(cluster, &cluster->dbarr.dbs[dbnum]);
170
171         pg_log(PG_VERBOSE, "\n%s databases:\n", CLUSTER_NAME(cluster));
172         if (log_opts.verbose)
173                 print_db_infos(&cluster->dbarr);
174 }
175
176
177 /*
178  * get_db_infos()
179  *
180  * Scans pg_database system catalog and populates all user
181  * databases.
182  */
183 static void
184 get_db_infos(ClusterInfo *cluster)
185 {
186         PGconn     *conn = connectToServer(cluster, "template1");
187         PGresult   *res;
188         int                     ntups;
189         int                     tupnum;
190         DbInfo     *dbinfos;
191         int                     i_datname,
192                                 i_oid,
193                                 i_spclocation;
194         char            query[QUERY_ALLOC];
195
196         snprintf(query, sizeof(query),
197                         "SELECT d.oid, d.datname, %s "
198                         "FROM pg_catalog.pg_database d "
199                         " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
200                         " ON d.dattablespace = t.oid "
201                         "WHERE d.datallowconn = true "
202         /* we don't preserve pg_database.oid so we sort by name */
203                         "ORDER BY 2",
204         /* 9.2 removed the spclocation column */
205                         (GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
206                         "t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation");
207
208         res = executeQueryOrDie(conn, "%s", query);
209
210         i_oid = PQfnumber(res, "oid");
211         i_datname = PQfnumber(res, "datname");
212         i_spclocation = PQfnumber(res, "spclocation");
213
214         ntups = PQntuples(res);
215         dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
216
217         for (tupnum = 0; tupnum < ntups; tupnum++)
218         {
219                 dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
220                 snprintf(dbinfos[tupnum].db_name, sizeof(dbinfos[tupnum].db_name), "%s",
221                                  PQgetvalue(res, tupnum, i_datname));
222                 snprintf(dbinfos[tupnum].db_tblspace, sizeof(dbinfos[tupnum].db_tblspace), "%s",
223                                  PQgetvalue(res, tupnum, i_spclocation));
224         }
225         PQclear(res);
226
227         PQfinish(conn);
228
229         cluster->dbarr.dbs = dbinfos;
230         cluster->dbarr.ndbs = ntups;
231 }
232
233
234 /*
235  * get_rel_infos()
236  *
237  * gets the relinfos for all the user tables of the database refered
238  * by "db".
239  *
240  * NOTE: we assume that relations/entities with oids greater than
241  * FirstNormalObjectId belongs to the user
242  */
243 static void
244 get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
245 {
246         PGconn     *conn = connectToServer(cluster,
247                                                                            dbinfo->db_name);
248         PGresult   *res;
249         RelInfo    *relinfos;
250         int                     ntups;
251         int                     relnum;
252         int                     num_rels = 0;
253         char       *nspname = NULL;
254         char       *relname = NULL;
255         int                     i_spclocation,
256                                 i_nspname,
257                                 i_relname,
258                                 i_oid,
259                                 i_relfilenode;
260         char            query[QUERY_ALLOC];
261
262         /*
263          * pg_largeobject contains user data that does not appear in pg_dumpall
264          * --schema-only output, so we have to copy that system table heap and
265          * index.  We could grab the pg_largeobject oids from template1, but it is
266          * easy to treat it as a normal table. Order by oid so we can join old/new
267          * structures efficiently.
268          */
269
270         snprintf(query, sizeof(query),
271                          "SELECT c.oid, n.nspname, c.relname, "
272                          "      c.relfilenode, %s "
273                          "FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
274                          "         ON c.relnamespace = n.oid "
275                          "  LEFT OUTER JOIN pg_catalog.pg_tablespace t "
276                          "         ON c.reltablespace = t.oid "
277                          "WHERE relkind IN ('r','t', 'i'%s) AND "
278                          /* exclude possible orphaned temp tables */
279                          "  ((n.nspname !~ '^pg_temp_' AND "
280                          "    n.nspname !~ '^pg_toast_temp_' AND "
281                          "    n.nspname NOT IN ('pg_catalog', 'information_schema', 'binary_upgrade') AND "
282                          "        c.oid >= %u) "
283                          "  OR (n.nspname = 'pg_catalog' AND "
284         "    relname IN ('pg_largeobject', 'pg_largeobject_loid_pn_index'%s) )) "
285         /* we preserve pg_class.oid so we sort by it to match old/new */
286                          "ORDER BY 1;",
287         /* 9.2 removed the spclocation column */
288                          (GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
289                          "t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation",
290         /* see the comment at the top of old_8_3_create_sequence_script() */
291                          (GET_MAJOR_VERSION(old_cluster.major_version) <= 803) ?
292                          "" : ", 'S'",
293         /* this oid allows us to skip system toast tables */
294                          FirstNormalObjectId,
295         /* does pg_largeobject_metadata need to be migrated? */
296                          (GET_MAJOR_VERSION(old_cluster.major_version) <= 804) ?
297         "" : ", 'pg_largeobject_metadata', 'pg_largeobject_metadata_oid_index'");
298
299         res = executeQueryOrDie(conn, "%s", query);
300
301         ntups = PQntuples(res);
302
303         relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
304
305         i_oid = PQfnumber(res, "oid");
306         i_nspname = PQfnumber(res, "nspname");
307         i_relname = PQfnumber(res, "relname");
308         i_relfilenode = PQfnumber(res, "relfilenode");
309         i_spclocation = PQfnumber(res, "spclocation");
310
311         for (relnum = 0; relnum < ntups; relnum++)
312         {
313                 RelInfo    *curr = &relinfos[num_rels++];
314                 const char *tblspace;
315
316                 curr->reloid = atooid(PQgetvalue(res, relnum, i_oid));
317
318                 nspname = PQgetvalue(res, relnum, i_nspname);
319                 strlcpy(curr->nspname, nspname, sizeof(curr->nspname));
320
321                 relname = PQgetvalue(res, relnum, i_relname);
322                 strlcpy(curr->relname, relname, sizeof(curr->relname));
323
324                 curr->relfilenode = atooid(PQgetvalue(res, relnum, i_relfilenode));
325
326                 tblspace = PQgetvalue(res, relnum, i_spclocation);
327                 /* if no table tablespace, use the database tablespace */
328                 if (strlen(tblspace) == 0)
329                         tblspace = dbinfo->db_tblspace;
330                 strlcpy(curr->tablespace, tblspace, sizeof(curr->tablespace));
331         }
332         PQclear(res);
333
334         PQfinish(conn);
335
336         dbinfo->rel_arr.rels = relinfos;
337         dbinfo->rel_arr.nrels = num_rels;
338 }
339
340
341 void
342 free_db_and_rel_infos(DbInfoArr *db_arr)
343 {
344         int                     dbnum;
345
346         for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
347                 free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
348         pg_free(db_arr->dbs);
349         db_arr->dbs = NULL;
350         db_arr->ndbs = 0;
351 }
352
353
354 static void
355 free_rel_infos(RelInfoArr *rel_arr)
356 {
357         pg_free(rel_arr->rels);
358         rel_arr->nrels = 0;
359 }
360
361
362 static void
363 print_db_infos(DbInfoArr *db_arr)
364 {
365         int                     dbnum;
366
367         for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
368         {
369                 pg_log(PG_VERBOSE, "Database: %s\n", db_arr->dbs[dbnum].db_name);
370                 print_rel_infos(&db_arr->dbs[dbnum].rel_arr);
371                 pg_log(PG_VERBOSE, "\n\n");
372         }
373 }
374
375
376 static void
377 print_rel_infos(RelInfoArr *arr)
378 {
379         int                     relnum;
380
381         for (relnum = 0; relnum < arr->nrels; relnum++)
382                 pg_log(PG_VERBOSE, "relname: %s.%s: reloid: %u reltblspace: %s\n",
383                            arr->rels[relnum].nspname, arr->rels[relnum].relname,
384                            arr->rels[relnum].reloid, arr->rels[relnum].tablespace);
385 }