2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* info.c
|
|
|
|
*
|
|
|
|
* information support functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2013-01-02 06:15:01 +08:00
|
|
|
* Copyright (c) 2010-2013, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/info.c
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
|
Create libpgcommon, and move pg_malloc et al to it
libpgcommon is a new static library to allow sharing code among the
various frontend programs and backend; this lets us eliminate duplicate
implementations of common routines. We avoid libpgport, because that's
intended as a place for porting issues; per discussion, it seems better
to keep them separate.
The first use case, and the only implemented by this patch, is pg_malloc
and friends, which many frontend programs were already using.
At the same time, we can use this to provide palloc emulation functions
for the frontend; this way, some palloc-using files in the backend can
also be used by the frontend cleanly. To do this, we change palloc() in
the backend to be a function instead of a macro on top of
MemoryContextAlloc(). This was previously believed to cause loss of
performance, but this implementation has been tweaked by Tom and Andres
so that on modern compilers it provides a slight improvement over the
previous one.
This lets us clean up some places that were already with
localized hacks.
Most of the pg_malloc/palloc changes in this patch were authored by
Andres Freund. Zoltán Böszörményi also independently provided a form of
that. libpgcommon infrastructure was authored by Álvaro.
2013-02-12 21:33:40 +08:00
|
|
|
#include "postgres_fe.h"
|
2011-08-27 09:16:24 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
#include "access/transam.h"
|
|
|
|
|
|
|
|
|
2011-01-05 08:11:00 +08:00
|
|
|
static void create_rel_filename_map(const char *old_data, const char *new_data,
|
2011-04-10 23:42:00 +08:00
|
|
|
const DbInfo *old_db, const DbInfo *new_db,
|
|
|
|
const RelInfo *old_rel, const RelInfo *new_rel,
|
|
|
|
FileNameMap *map);
|
2012-11-14 10:10:40 +08:00
|
|
|
static void free_db_and_rel_infos(DbInfoArr *db_arr);
|
2011-01-11 00:45:22 +08:00
|
|
|
static void get_db_infos(ClusterInfo *cluster);
|
|
|
|
static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo);
|
|
|
|
static void free_rel_infos(RelInfoArr *rel_arr);
|
|
|
|
static void print_db_infos(DbInfoArr *dbinfo);
|
2012-12-21 02:56:24 +08:00
|
|
|
static void print_rel_infos(RelInfoArr *rel_arr);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* gen_db_file_maps()
|
|
|
|
*
|
|
|
|
* generates database mappings for "old_db" and "new_db". Returns a malloc'ed
|
|
|
|
* array of mappings. nmaps is a return parameter which refers to the number
|
|
|
|
* mappings.
|
|
|
|
*/
|
|
|
|
FileNameMap *
|
2010-10-20 05:38:16 +08:00
|
|
|
gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
|
2010-05-12 10:19:11 +08:00
|
|
|
int *nmaps, const char *old_pgdata, const char *new_pgdata)
|
|
|
|
{
|
|
|
|
FileNameMap *maps;
|
|
|
|
int relnum;
|
|
|
|
int num_maps = 0;
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
|
2011-01-06 00:37:08 +08:00
|
|
|
old_db->rel_arr.nrels);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-10-02 23:53:45 +08:00
|
|
|
for (relnum = 0; relnum < Min(old_db->rel_arr.nrels, new_db->rel_arr.nrels);
|
|
|
|
relnum++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-06 02:52:44 +08:00
|
|
|
RelInfo *old_rel = &old_db->rel_arr.rels[relnum];
|
2011-02-16 08:01:33 +08:00
|
|
|
RelInfo *new_rel = &new_db->rel_arr.rels[relnum];
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-09 02:44:44 +08:00
|
|
|
if (old_rel->reloid != new_rel->reloid)
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "Mismatch of relation OID in database \"%s\": old OID %d, new OID %d\n",
|
2011-04-10 23:42:00 +08:00
|
|
|
old_db->db_name, old_rel->reloid, new_rel->reloid);
|
2011-03-06 10:12:21 +08:00
|
|
|
|
2011-03-06 11:09:35 +08:00
|
|
|
/*
|
2012-06-11 03:20:04 +08:00
|
|
|
* TOAST table names initially match the heap pg_class oid. In
|
|
|
|
* pre-8.4, TOAST table names change during CLUSTER; in pre-9.0, TOAST
|
|
|
|
* table names change during ALTER TABLE ALTER COLUMN SET TYPE. In >=
|
|
|
|
* 9.0, TOAST relation names always use heap table oids, hence we
|
|
|
|
* cannot check relation names when upgrading from pre-9.0. Clusters
|
|
|
|
* upgraded to 9.0 will get matching TOAST names.
|
2012-12-20 20:37:27 +08:00
|
|
|
* If index names don't match primary key constraint names, this will
|
|
|
|
* fail because pg_dump dumps constraint names and pg_upgrade checks
|
|
|
|
* index names.
|
2011-03-06 11:09:35 +08:00
|
|
|
*/
|
2011-03-06 19:34:58 +08:00
|
|
|
if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
|
2011-09-29 10:30:44 +08:00
|
|
|
((GET_MAJOR_VERSION(old_cluster.major_version) >= 900 ||
|
2011-03-07 10:57:02 +08:00
|
|
|
strcmp(old_rel->nspname, "pg_toast") != 0) &&
|
|
|
|
strcmp(old_rel->relname, new_rel->relname) != 0))
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "Mismatch of relation names in database \"%s\": "
|
|
|
|
"old name \"%s.%s\", new name \"%s.%s\"\n",
|
2011-04-10 23:42:00 +08:00
|
|
|
old_db->db_name, old_rel->nspname, old_rel->relname,
|
|
|
|
new_rel->nspname, new_rel->relname);
|
2011-03-06 10:12:21 +08:00
|
|
|
|
2011-01-05 08:11:00 +08:00
|
|
|
create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
|
2011-04-10 23:42:00 +08:00
|
|
|
old_rel, new_rel, maps + num_maps);
|
2010-05-12 10:19:11 +08:00
|
|
|
num_maps++;
|
|
|
|
}
|
|
|
|
|
2012-10-02 23:53:45 +08:00
|
|
|
/* Do this check after the loop so hopefully we will produce a clearer error above */
|
|
|
|
if (old_db->rel_arr.nrels != new_db->rel_arr.nrels)
|
|
|
|
pg_log(PG_FATAL, "old and new databases \"%s\" have a different number of relations\n",
|
|
|
|
old_db->db_name);
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
*nmaps = num_maps;
|
|
|
|
return maps;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-01-05 08:11:00 +08:00
|
|
|
* create_rel_filename_map()
|
2010-05-12 10:19:11 +08:00
|
|
|
*
|
|
|
|
* fills a file node map structure and returns it in "map".
|
|
|
|
*/
|
|
|
|
static void
|
2011-01-05 08:11:00 +08:00
|
|
|
create_rel_filename_map(const char *old_data, const char *new_data,
|
2011-04-10 23:42:00 +08:00
|
|
|
const DbInfo *old_db, const DbInfo *new_db,
|
|
|
|
const RelInfo *old_rel, const RelInfo *new_rel,
|
|
|
|
FileNameMap *map)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-05 08:11:00 +08:00
|
|
|
if (strlen(old_rel->tablespace) == 0)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
/*
|
2011-01-05 08:11:00 +08:00
|
|
|
* relation belongs to the default tablespace, hence relfiles should
|
2010-05-12 10:19:11 +08:00
|
|
|
* exist in the data directories.
|
|
|
|
*/
|
2013-01-09 21:57:47 +08:00
|
|
|
strlcpy(map->old_tablespace, old_data, sizeof(map->old_tablespace));
|
|
|
|
strlcpy(map->new_tablespace, new_data, sizeof(map->new_tablespace));
|
|
|
|
strlcpy(map->old_tablespace_suffix, "/base", sizeof(map->old_tablespace_suffix));
|
|
|
|
strlcpy(map->new_tablespace_suffix, "/base", sizeof(map->new_tablespace_suffix));
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-06 00:37:08 +08:00
|
|
|
/* relation belongs to a tablespace, so use the tablespace location */
|
2013-01-09 21:57:47 +08:00
|
|
|
strlcpy(map->old_tablespace, old_rel->tablespace, sizeof(map->old_tablespace));
|
|
|
|
strlcpy(map->new_tablespace, new_rel->tablespace, sizeof(map->new_tablespace));
|
|
|
|
strlcpy(map->old_tablespace_suffix, old_cluster.tablespace_suffix,
|
|
|
|
sizeof(map->old_tablespace_suffix));
|
|
|
|
strlcpy(map->new_tablespace_suffix, new_cluster.tablespace_suffix,
|
|
|
|
sizeof(map->new_tablespace_suffix));
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
2011-01-06 00:37:08 +08:00
|
|
|
|
2013-01-09 21:57:47 +08:00
|
|
|
map->old_db_oid = old_db->db_oid;
|
|
|
|
map->new_db_oid = new_db->db_oid;
|
|
|
|
|
2011-01-08 11:57:30 +08:00
|
|
|
/*
|
2011-04-10 23:42:00 +08:00
|
|
|
* old_relfilenode might differ from pg_class.oid (and hence
|
|
|
|
* new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
|
2011-01-08 11:57:30 +08:00
|
|
|
*/
|
2011-01-06 00:37:08 +08:00
|
|
|
map->old_relfilenode = old_rel->relfilenode;
|
2011-01-08 11:57:30 +08:00
|
|
|
|
|
|
|
/* new_relfilenode will match old and new pg_class.oid */
|
2011-01-06 00:37:08 +08:00
|
|
|
map->new_relfilenode = new_rel->relfilenode;
|
|
|
|
|
2011-01-08 11:36:51 +08:00
|
|
|
/* used only for logging and error reporing, old/new are identical */
|
2012-12-21 02:56:24 +08:00
|
|
|
map->nspname = old_rel->nspname;
|
|
|
|
map->relname = old_rel->relname;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2011-01-11 00:45:22 +08:00
|
|
|
print_maps(FileNameMap *maps, int n_maps, const char *db_name)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-03-13 07:47:54 +08:00
|
|
|
if (log_opts.verbose)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int mapnum;
|
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-11 00:45:22 +08:00
|
|
|
for (mapnum = 0; mapnum < n_maps; mapnum++)
|
2012-03-13 07:47:54 +08:00
|
|
|
pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
|
2011-01-08 11:36:51 +08:00
|
|
|
maps[mapnum].nspname, maps[mapnum].relname,
|
2010-10-20 06:37:04 +08:00
|
|
|
maps[mapnum].old_relfilenode,
|
|
|
|
maps[mapnum].new_relfilenode);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
pg_log(PG_VERBOSE, "\n\n");
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-11 00:45:22 +08:00
|
|
|
/*
|
|
|
|
* get_db_and_rel_infos()
|
|
|
|
*
|
|
|
|
* higher level routine to generate dbinfos for the database running
|
|
|
|
* on the given "port". Assumes that server is already running.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
get_db_and_rel_infos(ClusterInfo *cluster)
|
|
|
|
{
|
|
|
|
int dbnum;
|
|
|
|
|
2011-02-16 08:01:33 +08:00
|
|
|
if (cluster->dbarr.dbs != NULL)
|
|
|
|
free_db_and_rel_infos(&cluster->dbarr);
|
2011-02-16 04:00:07 +08:00
|
|
|
|
2011-01-11 00:45:22 +08:00
|
|
|
get_db_infos(cluster);
|
|
|
|
|
|
|
|
for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
|
|
|
|
get_rel_infos(cluster, &cluster->dbarr.dbs[dbnum]);
|
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
pg_log(PG_VERBOSE, "\n%s databases:\n", CLUSTER_NAME(cluster));
|
|
|
|
if (log_opts.verbose)
|
2011-01-11 00:45:22 +08:00
|
|
|
print_db_infos(&cluster->dbarr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* get_db_infos()
|
|
|
|
*
|
2011-01-02 01:06:36 +08:00
|
|
|
* Scans pg_database system catalog and populates all user
|
2010-05-12 10:19:11 +08:00
|
|
|
* databases.
|
|
|
|
*/
|
|
|
|
static void
|
2011-01-02 01:06:36 +08:00
|
|
|
get_db_infos(ClusterInfo *cluster)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-02 01:06:36 +08:00
|
|
|
PGconn *conn = connectToServer(cluster, "template1");
|
2010-05-12 10:19:11 +08:00
|
|
|
PGresult *res;
|
|
|
|
int ntups;
|
|
|
|
int tupnum;
|
|
|
|
DbInfo *dbinfos;
|
2011-04-10 23:42:00 +08:00
|
|
|
int i_datname,
|
|
|
|
i_oid,
|
|
|
|
i_spclocation;
|
2011-12-07 17:35:00 +08:00
|
|
|
char query[QUERY_ALLOC];
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-12-07 17:35:00 +08:00
|
|
|
snprintf(query, sizeof(query),
|
2012-06-11 03:20:04 +08:00
|
|
|
"SELECT d.oid, d.datname, %s "
|
|
|
|
"FROM pg_catalog.pg_database d "
|
|
|
|
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
|
|
|
|
" ON d.dattablespace = t.oid "
|
|
|
|
"WHERE d.datallowconn = true "
|
2011-01-09 02:44:44 +08:00
|
|
|
/* we don't preserve pg_database.oid so we sort by name */
|
2012-06-11 03:20:04 +08:00
|
|
|
"ORDER BY 2",
|
2011-12-07 17:35:00 +08:00
|
|
|
/* 9.2 removed the spclocation column */
|
2012-06-11 03:20:04 +08:00
|
|
|
(GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
|
|
|
|
"t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation");
|
2011-12-07 17:35:00 +08:00
|
|
|
|
|
|
|
res = executeQueryOrDie(conn, "%s", query);
|
2010-07-07 03:19:02 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
i_oid = PQfnumber(res, "oid");
|
2011-03-06 09:18:31 +08:00
|
|
|
i_datname = PQfnumber(res, "datname");
|
2010-05-12 10:19:11 +08:00
|
|
|
i_spclocation = PQfnumber(res, "spclocation");
|
|
|
|
|
|
|
|
ntups = PQntuples(res);
|
2010-10-20 05:38:16 +08:00
|
|
|
dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
for (tupnum = 0; tupnum < ntups; tupnum++)
|
|
|
|
{
|
2010-09-29 06:11:17 +08:00
|
|
|
dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
|
2012-12-21 02:56:24 +08:00
|
|
|
dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
|
2010-05-12 10:19:11 +08:00
|
|
|
snprintf(dbinfos[tupnum].db_tblspace, sizeof(dbinfos[tupnum].db_tblspace), "%s",
|
|
|
|
PQgetvalue(res, tupnum, i_spclocation));
|
|
|
|
}
|
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
PQfinish(conn);
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
cluster->dbarr.dbs = dbinfos;
|
|
|
|
cluster->dbarr.ndbs = ntups;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_rel_infos()
|
|
|
|
*
|
2012-04-24 10:43:09 +08:00
|
|
|
* gets the relinfos for all the user tables of the database referred
|
2010-05-12 10:19:11 +08:00
|
|
|
* by "db".
|
|
|
|
*
|
|
|
|
* NOTE: we assume that relations/entities with oids greater than
|
|
|
|
* FirstNormalObjectId belongs to the user
|
|
|
|
*/
|
|
|
|
static void
|
2011-01-05 08:11:00 +08:00
|
|
|
get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-02 01:28:48 +08:00
|
|
|
PGconn *conn = connectToServer(cluster,
|
2011-01-05 08:11:00 +08:00
|
|
|
dbinfo->db_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
PGresult *res;
|
|
|
|
RelInfo *relinfos;
|
|
|
|
int ntups;
|
|
|
|
int relnum;
|
|
|
|
int num_rels = 0;
|
|
|
|
char *nspname = NULL;
|
|
|
|
char *relname = NULL;
|
2011-04-10 23:42:00 +08:00
|
|
|
int i_spclocation,
|
|
|
|
i_nspname,
|
|
|
|
i_relname,
|
|
|
|
i_oid,
|
2012-04-11 07:57:14 +08:00
|
|
|
i_relfilenode,
|
|
|
|
i_reltablespace;
|
2010-05-12 10:19:11 +08:00
|
|
|
char query[QUERY_ALLOC];
|
|
|
|
|
|
|
|
/*
|
2011-01-08 11:36:51 +08:00
|
|
|
* pg_largeobject contains user data that does not appear in pg_dumpall
|
2011-01-08 10:25:34 +08:00
|
|
|
* --schema-only output, so we have to copy that system table heap and
|
2011-04-10 23:42:00 +08:00
|
|
|
* index. We could grab the pg_largeobject oids from template1, but it is
|
|
|
|
* easy to treat it as a normal table. Order by oid so we can join old/new
|
|
|
|
* structures efficiently.
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
snprintf(query, sizeof(query),
|
2012-10-02 23:46:08 +08:00
|
|
|
"CREATE TEMPORARY TABLE info_rels (reloid) AS SELECT c.oid "
|
2011-04-07 18:22:40 +08:00
|
|
|
"FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
|
|
|
|
" ON c.relnamespace = n.oid "
|
2013-03-31 10:20:53 +08:00
|
|
|
"LEFT OUTER JOIN pg_catalog.pg_index i "
|
|
|
|
" ON c.oid = i.indexrelid "
|
2013-03-04 08:23:31 +08:00
|
|
|
"WHERE relkind IN ('r', 'm', 'i'%s) AND "
|
2013-03-31 10:20:53 +08:00
|
|
|
/* pg_dump only dumps valid indexes; testing indisready is
|
|
|
|
* necessary in 9.2, and harmless in earlier/later versions. */
|
|
|
|
" i.indisvalid IS DISTINCT FROM false AND "
|
|
|
|
" i.indisready IS DISTINCT FROM false AND "
|
2012-06-11 03:20:04 +08:00
|
|
|
/* exclude possible orphaned temp tables */
|
2011-08-26 12:12:32 +08:00
|
|
|
" ((n.nspname !~ '^pg_temp_' AND "
|
|
|
|
" n.nspname !~ '^pg_toast_temp_' AND "
|
2012-10-02 23:46:08 +08:00
|
|
|
/* skip pg_toast because toast index have relkind == 'i', not 't' */
|
|
|
|
" n.nspname NOT IN ('pg_catalog', 'information_schema', "
|
|
|
|
" 'binary_upgrade', 'pg_toast') AND "
|
2011-04-07 18:22:40 +08:00
|
|
|
" c.oid >= %u) "
|
|
|
|
" OR (n.nspname = 'pg_catalog' AND "
|
2012-10-02 23:46:08 +08:00
|
|
|
" relname IN ('pg_largeobject', 'pg_largeobject_loid_pn_index'%s) ));",
|
2011-04-07 18:22:40 +08:00
|
|
|
/* see the comment at the top of old_8_3_create_sequence_script() */
|
|
|
|
(GET_MAJOR_VERSION(old_cluster.major_version) <= 803) ?
|
|
|
|
"" : ", 'S'",
|
2010-05-12 10:19:11 +08:00
|
|
|
FirstNormalObjectId,
|
2011-01-05 12:35:49 +08:00
|
|
|
/* does pg_largeobject_metadata need to be migrated? */
|
|
|
|
(GET_MAJOR_VERSION(old_cluster.major_version) <= 804) ?
|
2011-04-10 23:42:00 +08:00
|
|
|
"" : ", 'pg_largeobject_metadata', 'pg_largeobject_metadata_oid_index'");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-10-02 23:46:08 +08:00
|
|
|
PQclear(executeQueryOrDie(conn, "%s", query));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get TOAST tables and indexes; we have to gather the TOAST tables in
|
|
|
|
* later steps because we can't schema-qualify TOAST tables.
|
|
|
|
*/
|
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"INSERT INTO info_rels "
|
|
|
|
"SELECT reltoastrelid "
|
|
|
|
"FROM info_rels i JOIN pg_catalog.pg_class c "
|
|
|
|
" ON i.reloid = c.oid"));
|
|
|
|
PQclear(executeQueryOrDie(conn,
|
|
|
|
"INSERT INTO info_rels "
|
|
|
|
"SELECT reltoastidxid "
|
|
|
|
"FROM info_rels i JOIN pg_catalog.pg_class c "
|
|
|
|
" ON i.reloid = c.oid"));
|
|
|
|
|
|
|
|
snprintf(query, sizeof(query),
|
|
|
|
"SELECT c.oid, n.nspname, c.relname, "
|
|
|
|
" c.relfilenode, c.reltablespace, %s "
|
|
|
|
"FROM info_rels i JOIN pg_catalog.pg_class c "
|
|
|
|
" ON i.reloid = c.oid "
|
|
|
|
" JOIN pg_catalog.pg_namespace n "
|
|
|
|
" ON c.relnamespace = n.oid "
|
|
|
|
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
|
|
|
|
" ON c.reltablespace = t.oid "
|
|
|
|
/* we preserve pg_class.oid so we sort by it to match old/new */
|
|
|
|
"ORDER BY 1;",
|
|
|
|
/* 9.2 removed the spclocation column */
|
|
|
|
(GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
|
|
|
|
"t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation");
|
|
|
|
|
2011-09-11 04:12:46 +08:00
|
|
|
res = executeQueryOrDie(conn, "%s", query);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
ntups = PQntuples(res);
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
i_oid = PQfnumber(res, "oid");
|
|
|
|
i_nspname = PQfnumber(res, "nspname");
|
|
|
|
i_relname = PQfnumber(res, "relname");
|
|
|
|
i_relfilenode = PQfnumber(res, "relfilenode");
|
2012-04-11 07:57:14 +08:00
|
|
|
i_reltablespace = PQfnumber(res, "reltablespace");
|
2010-05-12 10:19:11 +08:00
|
|
|
i_spclocation = PQfnumber(res, "spclocation");
|
|
|
|
|
|
|
|
for (relnum = 0; relnum < ntups; relnum++)
|
|
|
|
{
|
|
|
|
RelInfo *curr = &relinfos[num_rels++];
|
2010-07-07 03:19:02 +08:00
|
|
|
const char *tblspace;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-09-29 06:11:17 +08:00
|
|
|
curr->reloid = atooid(PQgetvalue(res, relnum, i_oid));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
nspname = PQgetvalue(res, relnum, i_nspname);
|
2012-12-21 02:56:24 +08:00
|
|
|
curr->nspname = pg_strdup(nspname);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
relname = PQgetvalue(res, relnum, i_relname);
|
2012-12-21 02:56:24 +08:00
|
|
|
curr->relname = pg_strdup(relname);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-09-29 06:11:17 +08:00
|
|
|
curr->relfilenode = atooid(PQgetvalue(res, relnum, i_relfilenode));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-04-11 07:57:14 +08:00
|
|
|
if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
|
|
|
|
/* Might be "", meaning the cluster default location. */
|
|
|
|
tblspace = PQgetvalue(res, relnum, i_spclocation);
|
|
|
|
else
|
|
|
|
/* A zero reltablespace indicates the database tablespace. */
|
2011-01-05 08:11:00 +08:00
|
|
|
tblspace = dbinfo->db_tblspace;
|
2012-04-11 07:57:14 +08:00
|
|
|
|
2010-05-14 08:32:21 +08:00
|
|
|
strlcpy(curr->tablespace, tblspace, sizeof(curr->tablespace));
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
PQfinish(conn);
|
|
|
|
|
2011-01-05 08:11:00 +08:00
|
|
|
dbinfo->rel_arr.rels = relinfos;
|
|
|
|
dbinfo->rel_arr.nrels = num_rels;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-14 10:10:40 +08:00
|
|
|
static void
|
2011-01-11 00:45:22 +08:00
|
|
|
free_db_and_rel_infos(DbInfoArr *db_arr)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int dbnum;
|
|
|
|
|
|
|
|
for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
|
2012-12-21 02:56:24 +08:00
|
|
|
{
|
2011-01-11 00:45:22 +08:00
|
|
|
free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
|
2012-12-21 02:56:24 +08:00
|
|
|
pg_free(db_arr->dbs[dbnum].db_name);
|
|
|
|
}
|
2011-01-11 00:45:22 +08:00
|
|
|
pg_free(db_arr->dbs);
|
2011-02-16 08:01:33 +08:00
|
|
|
db_arr->dbs = NULL;
|
2010-05-12 10:19:11 +08:00
|
|
|
db_arr->ndbs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2011-01-11 00:45:22 +08:00
|
|
|
free_rel_infos(RelInfoArr *rel_arr)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-12-21 02:56:24 +08:00
|
|
|
int relnum;
|
|
|
|
|
|
|
|
for (relnum = 0; relnum < rel_arr->nrels; relnum++)
|
|
|
|
{
|
|
|
|
pg_free(rel_arr->rels[relnum].nspname);
|
|
|
|
pg_free(rel_arr->rels[relnum].relname);
|
|
|
|
}
|
2011-01-11 00:45:22 +08:00
|
|
|
pg_free(rel_arr->rels);
|
|
|
|
rel_arr->nrels = 0;
|
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
2011-01-11 00:45:22 +08:00
|
|
|
static void
|
|
|
|
print_db_infos(DbInfoArr *db_arr)
|
|
|
|
{
|
|
|
|
int dbnum;
|
|
|
|
|
|
|
|
for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-03-13 07:47:54 +08:00
|
|
|
pg_log(PG_VERBOSE, "Database: %s\n", db_arr->dbs[dbnum].db_name);
|
2011-01-11 00:45:22 +08:00
|
|
|
print_rel_infos(&db_arr->dbs[dbnum].rel_arr);
|
2012-03-13 07:47:54 +08:00
|
|
|
pg_log(PG_VERBOSE, "\n\n");
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2012-12-21 02:56:24 +08:00
|
|
|
print_rel_infos(RelInfoArr *rel_arr)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int relnum;
|
|
|
|
|
2012-12-21 02:56:24 +08:00
|
|
|
for (relnum = 0; relnum < rel_arr->nrels; relnum++)
|
2012-03-13 07:47:54 +08:00
|
|
|
pg_log(PG_VERBOSE, "relname: %s.%s: reloid: %u reltblspace: %s\n",
|
2012-12-21 02:56:24 +08:00
|
|
|
rel_arr->rels[relnum].nspname, rel_arr->rels[relnum].relname,
|
|
|
|
rel_arr->rels[relnum].reloid, rel_arr->rels[relnum].tablespace);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|