OSDN Git Service

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