]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/info.c
Use "upgrade" in preference over "migrate" in pg_upgrade messages and
[postgresql] / contrib / pg_upgrade / info.c
1 /*
2  *      info.c
3  *
4  *      information support functions
5  *
6  *      Copyright (c) 2010, PostgreSQL Global Development Group
7  *      contrib/pg_upgrade/info.c
8  */
9
10 #include "pg_upgrade.h"
11
12 #include "access/transam.h"
13
14
15 static void get_db_infos(DbInfoArr *dbinfos,
16                          Cluster whichCluster);
17 static void dbarr_print(DbInfoArr *arr,
18                         Cluster whichCluster);
19 static void relarr_print(RelInfoArr *arr);
20 static void get_rel_infos(const DbInfo *dbinfo,
21                           RelInfoArr *relarr, Cluster whichCluster);
22 static void relarr_free(RelInfoArr *rel_arr);
23 static void map_rel(const RelInfo *oldrel,
24                 const RelInfo *newrel, const DbInfo *old_db,
25                 const DbInfo *new_db, const char *olddata,
26                 const char *newdata, FileNameMap *map);
27 static void map_rel_by_id(Oid oldid, Oid newid,
28                           const char *old_nspname, const char *old_relname,
29                           const char *new_nspname, const char *new_relname,
30                           const char *old_tablespace, const DbInfo *old_db,
31                           const DbInfo *new_db, const char *olddata,
32                           const char *newdata, FileNameMap *map);
33 static RelInfo *relarr_lookup_reloid(RelInfoArr *rel_arr,
34                                 Oid oid, Cluster whichCluster);
35 static RelInfo *relarr_lookup_rel(RelInfoArr *rel_arr,
36                                   const char *nspname, const char *relname,
37                                   Cluster whichCluster);
38
39
40 /*
41  * gen_db_file_maps()
42  *
43  * generates database mappings for "old_db" and "new_db". Returns a malloc'ed
44  * array of mappings. nmaps is a return parameter which refers to the number
45  * mappings.
46  *
47  * NOTE: Its the Caller's responsibility to free the returned array.
48  */
49 FileNameMap *
50 gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
51                                  int *nmaps, const char *old_pgdata, const char *new_pgdata)
52 {
53         FileNameMap *maps;
54         int                     relnum;
55         int                     num_maps = 0;
56
57         maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
58                                                                          new_db->rel_arr.nrels);
59
60         for (relnum = 0; relnum < new_db->rel_arr.nrels; relnum++)
61         {
62                 RelInfo    *newrel = &new_db->rel_arr.rels[relnum];
63                 RelInfo    *oldrel;
64
65                 /* toast tables are handled by their parent */
66                 if (strcmp(newrel->nspname, "pg_toast") == 0)
67                         continue;
68
69                 oldrel = relarr_lookup_rel(&old_db->rel_arr, newrel->nspname,
70                                                                    newrel->relname, CLUSTER_OLD);
71
72                 map_rel(oldrel, newrel, old_db, new_db, old_pgdata, new_pgdata,
73                                 maps + num_maps);
74                 num_maps++;
75
76                 /*
77                  * So much for mapping this relation;  now we need a mapping
78                  * for its corresponding toast relation, if any.
79                  */
80                 if (oldrel->toastrelid > 0)
81                 {
82                         RelInfo    *new_toast;
83                         RelInfo    *old_toast;
84                         char            new_name[MAXPGPATH];
85                         char            old_name[MAXPGPATH];
86
87                         /* construct the new and old relnames for the toast relation */
88                         snprintf(old_name, sizeof(old_name), "pg_toast_%u",
89                                          oldrel->reloid);
90                         snprintf(new_name, sizeof(new_name), "pg_toast_%u",
91                                          newrel->reloid);
92
93                         /* look them up in their respective arrays */
94                         old_toast = relarr_lookup_reloid(&old_db->rel_arr,
95                                                                                          oldrel->toastrelid, CLUSTER_OLD);
96                         new_toast = relarr_lookup_rel(&new_db->rel_arr,
97                                                                                   "pg_toast", new_name, CLUSTER_NEW);
98
99                         /* finally create a mapping for them */
100                         map_rel(old_toast, new_toast, old_db, new_db, old_pgdata, new_pgdata,
101                                         maps + num_maps);
102                         num_maps++;
103
104                         /*
105                          * also need to provide a mapping for the index of this toast
106                          * relation. The procedure is similar to what we did above for
107                          * toast relation itself, the only difference being that the
108                          * relnames need to be appended with _index.
109                          */
110
111                         /*
112                          * construct the new and old relnames for the toast index
113                          * relations
114                          */
115                         snprintf(old_name, sizeof(old_name), "%s_index", old_toast->relname);
116                         snprintf(new_name, sizeof(new_name), "pg_toast_%u_index",
117                                          newrel->reloid);
118
119                         /* look them up in their respective arrays */
120                         /* we lose our cache location here */
121                         old_toast = relarr_lookup_rel(&old_db->rel_arr,
122                                                                                   "pg_toast", old_name, CLUSTER_OLD);
123                         new_toast = relarr_lookup_rel(&new_db->rel_arr,
124                                                                                   "pg_toast", new_name, CLUSTER_NEW);
125
126                         /* finally create a mapping for them */
127                         map_rel(old_toast, new_toast, old_db, new_db, old_pgdata,
128                                         new_pgdata, maps + num_maps);
129                         num_maps++;
130                 }
131         }
132
133         *nmaps = num_maps;
134         return maps;
135 }
136
137
138 static void
139 map_rel(const RelInfo *oldrel, const RelInfo *newrel,
140                 const DbInfo *old_db, const DbInfo *new_db, const char *olddata,
141                 const char *newdata, FileNameMap *map)
142 {
143         map_rel_by_id(oldrel->relfilenode, newrel->relfilenode, oldrel->nspname,
144                                   oldrel->relname, newrel->nspname, newrel->relname, oldrel->tablespace, old_db,
145                                   new_db, olddata, newdata, map);
146 }
147
148
149 /*
150  * map_rel_by_id()
151  *
152  * fills a file node map structure and returns it in "map".
153  */
154 static void
155 map_rel_by_id(Oid oldid, Oid newid,
156                           const char *old_nspname, const char *old_relname,
157                           const char *new_nspname, const char *new_relname,
158                           const char *old_tablespace, const DbInfo *old_db,
159                           const DbInfo *new_db, const char *olddata,
160                           const char *newdata, FileNameMap *map)
161 {
162         map->old_relfilenode = oldid;
163         map->new_relfilenode = newid;
164
165         snprintf(map->old_nspname, sizeof(map->old_nspname), "%s", old_nspname);
166         snprintf(map->old_relname, sizeof(map->old_relname), "%s", old_relname);
167         snprintf(map->new_nspname, sizeof(map->new_nspname), "%s", new_nspname);
168         snprintf(map->new_relname, sizeof(map->new_relname), "%s", new_relname);
169
170         if (strlen(old_tablespace) == 0)
171         {
172                 /*
173                  * relation belongs to the default tablespace, hence relfiles would
174                  * exist in the data directories.
175                  */
176                 snprintf(map->old_dir, sizeof(map->old_dir), "%s/base/%u", olddata, old_db->db_oid);
177                 snprintf(map->new_dir, sizeof(map->new_dir), "%s/base/%u", newdata, new_db->db_oid);
178         }
179         else
180         {
181                 /*
182                  * relation belongs to some tablespace, hence copy its physical
183                  * location
184                  */
185                 snprintf(map->old_dir, sizeof(map->old_dir), "%s%s/%u", old_tablespace,
186                                  old_cluster.tablespace_suffix, old_db->db_oid);
187                 snprintf(map->new_dir, sizeof(map->new_dir), "%s%s/%u", old_tablespace,
188                                  new_cluster.tablespace_suffix, new_db->db_oid);
189         }
190 }
191
192
193 void
194 print_maps(FileNameMap *maps, int n, const char *dbName)
195 {
196         if (log_opts.debug)
197         {
198                 int                     mapnum;
199
200                 pg_log(PG_DEBUG, "mappings for db %s:\n", dbName);
201
202                 for (mapnum = 0; mapnum < n; mapnum++)
203                         pg_log(PG_DEBUG, "%s.%s:%u ==> %s.%s:%u\n",
204                                    maps[mapnum].old_nspname, maps[mapnum].old_relname,
205                                    maps[mapnum].old_relfilenode,
206                                    maps[mapnum].new_nspname, maps[mapnum].new_relname,
207                                    maps[mapnum].new_relfilenode);
208
209                 pg_log(PG_DEBUG, "\n\n");
210         }
211 }
212
213
214 /*
215  * get_db_infos()
216  *
217  * Scans pg_database system catalog and returns (in dbinfs_arr) all user
218  * databases.
219  */
220 static void
221 get_db_infos(DbInfoArr *dbinfs_arr, Cluster whichCluster)
222 {
223         PGconn     *conn = connectToServer("template1", whichCluster);
224         PGresult   *res;
225         int                     ntups;
226         int                     tupnum;
227         DbInfo     *dbinfos;
228         int                     i_datname;
229         int                     i_oid;
230         int                     i_spclocation;
231
232         res = executeQueryOrDie(conn,
233                                                         "SELECT d.oid, d.datname, t.spclocation "
234                                                         "FROM pg_catalog.pg_database d "
235                                                         " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
236                                                         " ON d.dattablespace = t.oid "
237                                                         "WHERE d.datallowconn = true");
238
239         i_datname = PQfnumber(res, "datname");
240         i_oid = PQfnumber(res, "oid");
241         i_spclocation = PQfnumber(res, "spclocation");
242
243         ntups = PQntuples(res);
244         dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
245
246         for (tupnum = 0; tupnum < ntups; tupnum++)
247         {
248                 dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
249
250                 snprintf(dbinfos[tupnum].db_name, sizeof(dbinfos[tupnum].db_name), "%s",
251                                  PQgetvalue(res, tupnum, i_datname));
252                 snprintf(dbinfos[tupnum].db_tblspace, sizeof(dbinfos[tupnum].db_tblspace), "%s",
253                                  PQgetvalue(res, tupnum, i_spclocation));
254         }
255         PQclear(res);
256
257         PQfinish(conn);
258
259         dbinfs_arr->dbs = dbinfos;
260         dbinfs_arr->ndbs = ntups;
261 }
262
263
264 /*
265  * get_db_and_rel_infos()
266  *
267  * higher level routine to generate dbinfos for the database running
268  * on the given "port". Assumes that server is already running.
269  */
270 void
271 get_db_and_rel_infos(DbInfoArr *db_arr, Cluster whichCluster)
272 {
273         int                     dbnum;
274
275         get_db_infos(db_arr, whichCluster);
276
277         for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
278                 get_rel_infos(&db_arr->dbs[dbnum],
279                                           &db_arr->dbs[dbnum].rel_arr, whichCluster);
280
281         if (log_opts.debug)
282                 dbarr_print(db_arr, whichCluster);
283 }
284
285
286 /*
287  * get_rel_infos()
288  *
289  * gets the relinfos for all the user tables of the database refered
290  * by "db".
291  *
292  * NOTE: we assume that relations/entities with oids greater than
293  * FirstNormalObjectId belongs to the user
294  */
295 static void
296 get_rel_infos(const DbInfo *dbinfo, RelInfoArr *relarr, Cluster whichCluster)
297 {
298         PGconn     *conn = connectToServer(dbinfo->db_name, whichCluster);
299         PGresult   *res;
300         RelInfo    *relinfos;
301         int                     ntups;
302         int                     relnum;
303         int                     num_rels = 0;
304         char       *nspname = NULL;
305         char       *relname = NULL;
306         int                     i_spclocation = -1;
307         int                     i_nspname = -1;
308         int                     i_relname = -1;
309         int                     i_oid = -1;
310         int                     i_relfilenode = -1;
311         int                     i_reltoastrelid = -1;
312         char            query[QUERY_ALLOC];
313
314         /*
315          * pg_largeobject contains user data that does not appear the pg_dumpall
316          * --schema-only output, so we have to upgrade that system table heap and
317          * index.  Ideally we could just get the relfilenode from template1 but
318          * pg_largeobject_loid_pn_index's relfilenode can change if the table was
319          * reindexed so we get the relfilenode for each database and upgrade it as
320          * a normal user table.
321          * Order by tablespace so we can cache the directory contents efficiently.
322          */
323
324         snprintf(query, sizeof(query),
325                          "SELECT DISTINCT c.oid, n.nspname, c.relname, "
326                          "      c.relfilenode, c.reltoastrelid, t.spclocation "
327                          "FROM pg_catalog.pg_class c JOIN "
328                          "              pg_catalog.pg_namespace n "
329                          "      ON c.relnamespace = n.oid "
330                          "   LEFT OUTER JOIN pg_catalog.pg_tablespace t "
331                          "      ON c.reltablespace = t.oid "
332                          "WHERE (( n.nspname NOT IN ('pg_catalog', 'information_schema') "
333                          "      AND c.oid >= %u "
334                          "      ) OR ( "
335                          "      n.nspname = 'pg_catalog' "
336                          "      AND relname IN "
337                          "        ('pg_largeobject', 'pg_largeobject_loid_pn_index') )) "
338                          "      AND relkind IN ('r','t', 'i'%s)"
339                          "GROUP BY  c.oid, n.nspname, c.relname, c.relfilenode,"
340                          "                      c.reltoastrelid, t.spclocation, "
341                          "                      n.nspname "
342                          "ORDER BY t.spclocation, n.nspname, c.relname;",
343                          FirstNormalObjectId,
344         /* see the comment at the top of old_8_3_create_sequence_script() */
345                          (GET_MAJOR_VERSION(old_cluster.major_version) <= 803) ?
346                          "" : ", 'S'");
347
348         res = executeQueryOrDie(conn, query);
349
350         ntups = PQntuples(res);
351
352         relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
353
354         i_oid = PQfnumber(res, "oid");
355         i_nspname = PQfnumber(res, "nspname");
356         i_relname = PQfnumber(res, "relname");
357         i_relfilenode = PQfnumber(res, "relfilenode");
358         i_reltoastrelid = PQfnumber(res, "reltoastrelid");
359         i_spclocation = PQfnumber(res, "spclocation");
360
361         for (relnum = 0; relnum < ntups; relnum++)
362         {
363                 RelInfo    *curr = &relinfos[num_rels++];
364                 const char *tblspace;
365
366                 curr->reloid = atooid(PQgetvalue(res, relnum, i_oid));
367
368                 nspname = PQgetvalue(res, relnum, i_nspname);
369                 strlcpy(curr->nspname, nspname, sizeof(curr->nspname));
370
371                 relname = PQgetvalue(res, relnum, i_relname);
372                 strlcpy(curr->relname, relname, sizeof(curr->relname));
373
374                 curr->relfilenode = atooid(PQgetvalue(res, relnum, i_relfilenode));
375                 curr->toastrelid = atooid(PQgetvalue(res, relnum, i_reltoastrelid));
376
377                 tblspace = PQgetvalue(res, relnum, i_spclocation);
378                 /* if no table tablespace, use the database tablespace */
379                 if (strlen(tblspace) == 0)
380                         tblspace = dbinfo->db_tblspace;
381                 strlcpy(curr->tablespace, tblspace, sizeof(curr->tablespace));
382         }
383         PQclear(res);
384
385         PQfinish(conn);
386
387         relarr->rels = relinfos;
388         relarr->nrels = num_rels;
389         relarr->last_relname_lookup = 0;
390 }
391
392
393 /*
394  * dbarr_lookup_db()
395  *
396  * Returns the pointer to the DbInfo structure
397  */
398 DbInfo *
399 dbarr_lookup_db(DbInfoArr *db_arr, const char *db_name)
400 {
401         int                     dbnum;
402
403         for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
404         {
405                 if (strcmp(db_arr->dbs[dbnum].db_name, db_name) == 0)
406                         return &db_arr->dbs[dbnum];
407         }
408
409         return NULL;
410 }
411
412
413 /*
414  * relarr_lookup_rel()
415  *
416  * Searches "relname" in rel_arr. Returns the *real* pointer to the
417  * RelInfo structure.
418  */
419 static RelInfo *
420 relarr_lookup_rel(RelInfoArr *rel_arr, const char *nspname,
421                                         const char *relname, Cluster whichCluster)
422 {
423         int                     relnum;
424
425         /* Test next lookup first, for speed */
426         if (rel_arr->last_relname_lookup + 1 < rel_arr->nrels &&
427                 strcmp(rel_arr->rels[rel_arr->last_relname_lookup + 1].nspname, nspname) == 0 &&
428                 strcmp(rel_arr->rels[rel_arr->last_relname_lookup + 1].relname, relname) == 0)
429         {
430                 rel_arr->last_relname_lookup++;
431                 return &rel_arr->rels[rel_arr->last_relname_lookup];
432         }
433
434         for (relnum = 0; relnum < rel_arr->nrels; relnum++)
435         {
436                 if (strcmp(rel_arr->rels[relnum].nspname, nspname) == 0 &&
437                         strcmp(rel_arr->rels[relnum].relname, relname) == 0)
438                 {
439                         rel_arr->last_relname_lookup = relnum;
440                         return &rel_arr->rels[relnum];
441                 }
442         }
443         pg_log(PG_FATAL, "Could not find %s.%s in %s cluster\n",
444                    nspname, relname, CLUSTERNAME(whichCluster));
445         return NULL;
446 }
447
448
449 /*
450  * relarr_lookup_reloid()
451  *
452  *      Returns a pointer to the RelInfo structure for the
453  *      given oid or NULL if the desired entry cannot be
454  *      found.
455  */
456 static RelInfo *
457 relarr_lookup_reloid(RelInfoArr *rel_arr, Oid oid,
458                                          Cluster whichCluster)
459 {
460         int                     relnum;
461
462         for (relnum = 0; relnum < rel_arr->nrels; relnum++)
463         {
464                 if (rel_arr->rels[relnum].reloid == oid)
465                         return &rel_arr->rels[relnum];
466         }
467         pg_log(PG_FATAL, "Could not find %d in %s cluster\n",
468                    oid, CLUSTERNAME(whichCluster));
469         return NULL;
470 }
471
472
473 static void
474 relarr_free(RelInfoArr *rel_arr)
475 {
476         pg_free(rel_arr->rels);
477         rel_arr->nrels = 0;
478         rel_arr->last_relname_lookup = 0;
479 }
480
481
482 void
483 dbarr_free(DbInfoArr *db_arr)
484 {
485         int                     dbnum;
486
487         for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
488                 relarr_free(&db_arr->dbs[dbnum].rel_arr);
489         db_arr->ndbs = 0;
490 }
491
492
493 static void
494 dbarr_print(DbInfoArr *arr, Cluster whichCluster)
495 {
496         int                     dbnum;
497
498         pg_log(PG_DEBUG, "%s databases\n", CLUSTERNAME(whichCluster));
499
500         for (dbnum = 0; dbnum < arr->ndbs; dbnum++)
501         {
502                 pg_log(PG_DEBUG, "Database: %s\n", arr->dbs[dbnum].db_name);
503                 relarr_print(&arr->dbs[dbnum].rel_arr);
504                 pg_log(PG_DEBUG, "\n\n");
505         }
506 }
507
508
509 static void
510 relarr_print(RelInfoArr *arr)
511 {
512         int                     relnum;
513
514         for (relnum = 0; relnum < arr->nrels; relnum++)
515                 pg_log(PG_DEBUG, "relname: %s.%s: reloid: %u reltblspace: %s\n",
516                            arr->rels[relnum].nspname, arr->rels[relnum].relname,
517                            arr->rels[relnum].reloid, arr->rels[relnum].tablespace);
518 }