2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* info.c
|
|
|
|
*
|
|
|
|
* information support functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2010-07-04 00:33:15 +08:00
|
|
|
* Copyright (c) 2010, 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
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
#include "access/transam.h"
|
|
|
|
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
static void get_db_infos(ClusterInfo *cluster);
|
|
|
|
static void dbarr_print(ClusterInfo *cluster);
|
2010-10-20 05:38:16 +08:00
|
|
|
static void relarr_print(RelInfoArr *arr);
|
2011-01-02 01:28:48 +08:00
|
|
|
static void get_rel_infos(ClusterInfo *cluster, const int dbnum);
|
2010-05-12 10:19:11 +08:00
|
|
|
static void relarr_free(RelInfoArr *rel_arr);
|
2010-10-20 05:38:16 +08:00
|
|
|
static void map_rel(const RelInfo *oldrel,
|
2010-07-07 03:19:02 +08:00
|
|
|
const RelInfo *newrel, const DbInfo *old_db,
|
|
|
|
const DbInfo *new_db, const char *olddata,
|
|
|
|
const char *newdata, FileNameMap *map);
|
2010-10-20 05:38:16 +08:00
|
|
|
static void map_rel_by_id(Oid oldid, Oid newid,
|
2010-05-12 10:19:11 +08:00
|
|
|
const char *old_nspname, const char *old_relname,
|
|
|
|
const char *new_nspname, const char *new_relname,
|
|
|
|
const char *old_tablespace, const DbInfo *old_db,
|
|
|
|
const DbInfo *new_db, const char *olddata,
|
|
|
|
const char *newdata, FileNameMap *map);
|
2011-01-02 01:06:36 +08:00
|
|
|
static RelInfo *relarr_lookup_reloid(ClusterInfo *cluster, RelInfoArr *rel_arr,
|
|
|
|
Oid oid);
|
|
|
|
static RelInfo *relarr_lookup_rel(ClusterInfo *cluster, RelInfoArr *rel_arr,
|
|
|
|
const char *nspname, const char *relname);
|
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.
|
|
|
|
*
|
|
|
|
* NOTE: Its the Caller's responsibility to free the returned array.
|
|
|
|
*/
|
|
|
|
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) *
|
2010-05-12 10:19:11 +08:00
|
|
|
new_db->rel_arr.nrels);
|
|
|
|
|
|
|
|
for (relnum = 0; relnum < new_db->rel_arr.nrels; relnum++)
|
|
|
|
{
|
|
|
|
RelInfo *newrel = &new_db->rel_arr.rels[relnum];
|
|
|
|
RelInfo *oldrel;
|
|
|
|
|
|
|
|
/* toast tables are handled by their parent */
|
|
|
|
if (strcmp(newrel->nspname, "pg_toast") == 0)
|
|
|
|
continue;
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
oldrel = relarr_lookup_rel(&old_cluster, &old_db->rel_arr,
|
|
|
|
newrel->nspname, newrel->relname);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
map_rel(oldrel, newrel, old_db, new_db, old_pgdata, new_pgdata,
|
2010-05-12 10:19:11 +08:00
|
|
|
maps + num_maps);
|
|
|
|
num_maps++;
|
|
|
|
|
|
|
|
/*
|
2010-10-21 04:02:00 +08:00
|
|
|
* So much for mapping this relation; now we need a mapping
|
|
|
|
* for its corresponding toast relation, if any.
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
if (oldrel->toastrelid > 0)
|
|
|
|
{
|
|
|
|
RelInfo *new_toast;
|
|
|
|
RelInfo *old_toast;
|
|
|
|
char new_name[MAXPGPATH];
|
|
|
|
char old_name[MAXPGPATH];
|
|
|
|
|
|
|
|
/* construct the new and old relnames for the toast relation */
|
|
|
|
snprintf(old_name, sizeof(old_name), "pg_toast_%u",
|
|
|
|
oldrel->reloid);
|
|
|
|
snprintf(new_name, sizeof(new_name), "pg_toast_%u",
|
|
|
|
newrel->reloid);
|
|
|
|
|
|
|
|
/* look them up in their respective arrays */
|
2011-01-02 01:06:36 +08:00
|
|
|
old_toast = relarr_lookup_reloid(&old_cluster, &old_db->rel_arr,
|
|
|
|
oldrel->toastrelid);
|
|
|
|
new_toast = relarr_lookup_rel(&new_cluster, &new_db->rel_arr,
|
|
|
|
"pg_toast", new_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* finally create a mapping for them */
|
2010-10-20 05:38:16 +08:00
|
|
|
map_rel(old_toast, new_toast, old_db, new_db, old_pgdata, new_pgdata,
|
2010-05-12 10:19:11 +08:00
|
|
|
maps + num_maps);
|
|
|
|
num_maps++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* also need to provide a mapping for the index of this toast
|
|
|
|
* relation. The procedure is similar to what we did above for
|
|
|
|
* toast relation itself, the only difference being that the
|
|
|
|
* relnames need to be appended with _index.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* construct the new and old relnames for the toast index
|
|
|
|
* relations
|
|
|
|
*/
|
|
|
|
snprintf(old_name, sizeof(old_name), "%s_index", old_toast->relname);
|
|
|
|
snprintf(new_name, sizeof(new_name), "pg_toast_%u_index",
|
|
|
|
newrel->reloid);
|
|
|
|
|
|
|
|
/* look them up in their respective arrays */
|
2010-10-21 04:02:00 +08:00
|
|
|
/* we lose our cache location here */
|
2011-01-02 01:06:36 +08:00
|
|
|
old_toast = relarr_lookup_rel(&old_cluster, &old_db->rel_arr,
|
|
|
|
"pg_toast", old_name);
|
|
|
|
new_toast = relarr_lookup_rel(&new_cluster, &new_db->rel_arr,
|
|
|
|
"pg_toast", new_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* finally create a mapping for them */
|
2010-10-20 05:38:16 +08:00
|
|
|
map_rel(old_toast, new_toast, old_db, new_db, old_pgdata,
|
2010-05-12 10:19:11 +08:00
|
|
|
new_pgdata, maps + num_maps);
|
|
|
|
num_maps++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*nmaps = num_maps;
|
|
|
|
return maps;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
map_rel(const RelInfo *oldrel, const RelInfo *newrel,
|
2010-05-12 10:19:11 +08:00
|
|
|
const DbInfo *old_db, const DbInfo *new_db, const char *olddata,
|
|
|
|
const char *newdata, FileNameMap *map)
|
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
map_rel_by_id(oldrel->relfilenode, newrel->relfilenode, oldrel->nspname,
|
2010-05-12 10:19:11 +08:00
|
|
|
oldrel->relname, newrel->nspname, newrel->relname, oldrel->tablespace, old_db,
|
|
|
|
new_db, olddata, newdata, map);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* map_rel_by_id()
|
|
|
|
*
|
|
|
|
* fills a file node map structure and returns it in "map".
|
|
|
|
*/
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
map_rel_by_id(Oid oldid, Oid newid,
|
2010-05-12 10:19:11 +08:00
|
|
|
const char *old_nspname, const char *old_relname,
|
|
|
|
const char *new_nspname, const char *new_relname,
|
|
|
|
const char *old_tablespace, const DbInfo *old_db,
|
|
|
|
const DbInfo *new_db, const char *olddata,
|
|
|
|
const char *newdata, FileNameMap *map)
|
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
map->old_relfilenode = oldid;
|
|
|
|
map->new_relfilenode = newid;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
snprintf(map->old_nspname, sizeof(map->old_nspname), "%s", old_nspname);
|
|
|
|
snprintf(map->old_relname, sizeof(map->old_relname), "%s", old_relname);
|
|
|
|
snprintf(map->new_nspname, sizeof(map->new_nspname), "%s", new_nspname);
|
|
|
|
snprintf(map->new_relname, sizeof(map->new_relname), "%s", new_relname);
|
|
|
|
|
|
|
|
if (strlen(old_tablespace) == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* relation belongs to the default tablespace, hence relfiles would
|
|
|
|
* exist in the data directories.
|
|
|
|
*/
|
2010-10-20 08:33:19 +08:00
|
|
|
snprintf(map->old_dir, sizeof(map->old_dir), "%s/base/%u", olddata, old_db->db_oid);
|
|
|
|
snprintf(map->new_dir, sizeof(map->new_dir), "%s/base/%u", newdata, new_db->db_oid);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* relation belongs to some tablespace, hence copy its physical
|
|
|
|
* location
|
|
|
|
*/
|
2010-10-20 08:33:19 +08:00
|
|
|
snprintf(map->old_dir, sizeof(map->old_dir), "%s%s/%u", old_tablespace,
|
2010-10-20 05:38:16 +08:00
|
|
|
old_cluster.tablespace_suffix, old_db->db_oid);
|
2010-10-20 08:33:19 +08:00
|
|
|
snprintf(map->new_dir, sizeof(map->new_dir), "%s%s/%u", old_tablespace,
|
2010-10-20 05:38:16 +08:00
|
|
|
new_cluster.tablespace_suffix, new_db->db_oid);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
print_maps(FileNameMap *maps, int n, const char *dbName)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 10:31:17 +08:00
|
|
|
if (log_opts.debug)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int mapnum;
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_DEBUG, "mappings for db %s:\n", dbName);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
for (mapnum = 0; mapnum < n; mapnum++)
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_DEBUG, "%s.%s:%u ==> %s.%s:%u\n",
|
|
|
|
maps[mapnum].old_nspname, maps[mapnum].old_relname,
|
2010-10-20 06:37:04 +08:00
|
|
|
maps[mapnum].old_relfilenode,
|
2010-10-20 05:38:16 +08:00
|
|
|
maps[mapnum].new_nspname, maps[mapnum].new_relname,
|
2010-10-20 06:37:04 +08:00
|
|
|
maps[mapnum].new_relfilenode);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_DEBUG, "\n\n");
|
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;
|
|
|
|
int i_datname;
|
|
|
|
int i_oid;
|
|
|
|
int i_spclocation;
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
res = executeQueryOrDie(conn,
|
2010-07-07 03:19:02 +08:00
|
|
|
"SELECT d.oid, d.datname, t.spclocation "
|
|
|
|
"FROM pg_catalog.pg_database d "
|
|
|
|
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
|
|
|
|
" ON d.dattablespace = t.oid "
|
|
|
|
"WHERE d.datallowconn = true");
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
i_datname = PQfnumber(res, "datname");
|
|
|
|
i_oid = PQfnumber(res, "oid");
|
|
|
|
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));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
snprintf(dbinfos[tupnum].db_name, sizeof(dbinfos[tupnum].db_name), "%s",
|
|
|
|
PQgetvalue(res, tupnum, i_datname));
|
|
|
|
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_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
|
2011-01-02 01:06:36 +08:00
|
|
|
get_db_and_rel_infos(ClusterInfo *cluster)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int dbnum;
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
get_db_infos(cluster);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
|
2011-01-02 01:28:48 +08:00
|
|
|
get_rel_infos(cluster, dbnum);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 10:31:17 +08:00
|
|
|
if (log_opts.debug)
|
2011-01-02 01:06:36 +08:00
|
|
|
dbarr_print(cluster);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_rel_infos()
|
|
|
|
*
|
|
|
|
* gets the relinfos for all the user tables of the database refered
|
|
|
|
* by "db".
|
|
|
|
*
|
|
|
|
* NOTE: we assume that relations/entities with oids greater than
|
|
|
|
* FirstNormalObjectId belongs to the user
|
|
|
|
*/
|
|
|
|
static void
|
2011-01-02 01:28:48 +08:00
|
|
|
get_rel_infos(ClusterInfo *cluster, const int dbnum)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-02 01:28:48 +08:00
|
|
|
PGconn *conn = connectToServer(cluster,
|
|
|
|
cluster->dbarr.dbs[dbnum].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;
|
|
|
|
int i_spclocation = -1;
|
|
|
|
int i_nspname = -1;
|
|
|
|
int i_relname = -1;
|
|
|
|
int i_oid = -1;
|
|
|
|
int i_relfilenode = -1;
|
|
|
|
int i_reltoastrelid = -1;
|
|
|
|
char query[QUERY_ALLOC];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pg_largeobject contains user data that does not appear the pg_dumpall
|
2010-12-15 20:11:31 +08:00
|
|
|
* --schema-only output, so we have to upgrade that system table heap and
|
2010-05-12 10:19:11 +08:00
|
|
|
* index. Ideally we could just get the relfilenode from template1 but
|
|
|
|
* pg_largeobject_loid_pn_index's relfilenode can change if the table was
|
2010-12-15 20:11:31 +08:00
|
|
|
* reindexed so we get the relfilenode for each database and upgrade it as
|
2010-05-12 10:19:11 +08:00
|
|
|
* a normal user table.
|
2010-10-20 08:33:19 +08:00
|
|
|
* Order by tablespace so we can cache the directory contents efficiently.
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
snprintf(query, sizeof(query),
|
|
|
|
"SELECT DISTINCT c.oid, n.nspname, c.relname, "
|
|
|
|
" c.relfilenode, c.reltoastrelid, t.spclocation "
|
|
|
|
"FROM pg_catalog.pg_class c JOIN "
|
|
|
|
" pg_catalog.pg_namespace n "
|
|
|
|
" ON c.relnamespace = n.oid "
|
|
|
|
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
|
|
|
|
" ON c.reltablespace = t.oid "
|
|
|
|
"WHERE (( n.nspname NOT IN ('pg_catalog', 'information_schema') "
|
|
|
|
" AND c.oid >= %u "
|
|
|
|
" ) OR ( "
|
|
|
|
" n.nspname = 'pg_catalog' "
|
2010-05-20 02:56:47 +08:00
|
|
|
" AND relname IN "
|
|
|
|
" ('pg_largeobject', 'pg_largeobject_loid_pn_index') )) "
|
|
|
|
" AND relkind IN ('r','t', 'i'%s)"
|
2010-05-12 10:19:11 +08:00
|
|
|
"GROUP BY c.oid, n.nspname, c.relname, c.relfilenode,"
|
|
|
|
" c.reltoastrelid, t.spclocation, "
|
|
|
|
" n.nspname "
|
2010-10-20 08:33:19 +08:00
|
|
|
"ORDER BY t.spclocation, n.nspname, c.relname;",
|
2010-05-12 10:19:11 +08:00
|
|
|
FirstNormalObjectId,
|
2010-05-14 08:43:42 +08:00
|
|
|
/* see the comment at the top of old_8_3_create_sequence_script() */
|
2010-10-20 05:38:16 +08:00
|
|
|
(GET_MAJOR_VERSION(old_cluster.major_version) <= 803) ?
|
2010-05-20 02:56:47 +08:00
|
|
|
"" : ", 'S'");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
res = executeQueryOrDie(conn, 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");
|
|
|
|
i_reltoastrelid = PQfnumber(res, "reltoastrelid");
|
|
|
|
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);
|
2010-05-14 08:32:21 +08:00
|
|
|
strlcpy(curr->nspname, nspname, sizeof(curr->nspname));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
relname = PQgetvalue(res, relnum, i_relname);
|
2010-05-14 08:32:21 +08:00
|
|
|
strlcpy(curr->relname, relname, sizeof(curr->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));
|
|
|
|
curr->toastrelid = atooid(PQgetvalue(res, relnum, i_reltoastrelid));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
tblspace = PQgetvalue(res, relnum, i_spclocation);
|
|
|
|
/* if no table tablespace, use the database tablespace */
|
|
|
|
if (strlen(tblspace) == 0)
|
2011-01-02 01:28:48 +08:00
|
|
|
tblspace = cluster->dbarr.dbs[dbnum].db_tblspace;
|
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-02 01:28:48 +08:00
|
|
|
cluster->dbarr.dbs[dbnum].rel_arr.rels = relinfos;
|
|
|
|
cluster->dbarr.dbs[dbnum].rel_arr.nrels = num_rels;
|
|
|
|
cluster->dbarr.dbs[dbnum].rel_arr.last_relname_lookup = 0;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dbarr_lookup_db()
|
|
|
|
*
|
|
|
|
* Returns the pointer to the DbInfo structure
|
|
|
|
*/
|
|
|
|
DbInfo *
|
|
|
|
dbarr_lookup_db(DbInfoArr *db_arr, const char *db_name)
|
|
|
|
{
|
|
|
|
int dbnum;
|
|
|
|
|
|
|
|
for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
|
|
|
|
{
|
|
|
|
if (strcmp(db_arr->dbs[dbnum].db_name, db_name) == 0)
|
|
|
|
return &db_arr->dbs[dbnum];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* relarr_lookup_rel()
|
|
|
|
*
|
|
|
|
* Searches "relname" in rel_arr. Returns the *real* pointer to the
|
|
|
|
* RelInfo structure.
|
|
|
|
*/
|
|
|
|
static RelInfo *
|
2011-01-02 01:06:36 +08:00
|
|
|
relarr_lookup_rel(ClusterInfo *cluster, RelInfoArr *rel_arr,
|
|
|
|
const char *nspname, const char *relname)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 10:05:43 +08:00
|
|
|
int relnum;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 10:05:43 +08:00
|
|
|
/* Test next lookup first, for speed */
|
2010-10-21 04:02:00 +08:00
|
|
|
if (rel_arr->last_relname_lookup + 1 < rel_arr->nrels &&
|
|
|
|
strcmp(rel_arr->rels[rel_arr->last_relname_lookup + 1].nspname, nspname) == 0 &&
|
|
|
|
strcmp(rel_arr->rels[rel_arr->last_relname_lookup + 1].relname, relname) == 0)
|
2010-10-20 10:05:43 +08:00
|
|
|
{
|
2010-10-21 04:02:00 +08:00
|
|
|
rel_arr->last_relname_lookup++;
|
|
|
|
return &rel_arr->rels[rel_arr->last_relname_lookup];
|
2010-10-20 10:05:43 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
for (relnum = 0; relnum < rel_arr->nrels; relnum++)
|
|
|
|
{
|
|
|
|
if (strcmp(rel_arr->rels[relnum].nspname, nspname) == 0 &&
|
|
|
|
strcmp(rel_arr->rels[relnum].relname, relname) == 0)
|
2010-10-20 10:05:43 +08:00
|
|
|
{
|
2010-10-21 04:02:00 +08:00
|
|
|
rel_arr->last_relname_lookup = relnum;
|
2010-05-12 10:19:11 +08:00
|
|
|
return &rel_arr->rels[relnum];
|
2010-10-20 10:05:43 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL, "Could not find %s.%s in %s cluster\n",
|
2011-01-02 01:06:36 +08:00
|
|
|
nspname, relname, CLUSTER_NAME(cluster));
|
2010-05-12 10:19:11 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* relarr_lookup_reloid()
|
|
|
|
*
|
|
|
|
* Returns a pointer to the RelInfo structure for the
|
|
|
|
* given oid or NULL if the desired entry cannot be
|
|
|
|
* found.
|
|
|
|
*/
|
|
|
|
static RelInfo *
|
2011-01-02 01:06:36 +08:00
|
|
|
relarr_lookup_reloid(ClusterInfo *cluster, RelInfoArr *rel_arr, Oid oid)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int relnum;
|
|
|
|
|
|
|
|
for (relnum = 0; relnum < rel_arr->nrels; relnum++)
|
|
|
|
{
|
|
|
|
if (rel_arr->rels[relnum].reloid == oid)
|
|
|
|
return &rel_arr->rels[relnum];
|
|
|
|
}
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL, "Could not find %d in %s cluster\n",
|
2011-01-02 01:06:36 +08:00
|
|
|
oid, CLUSTER_NAME(cluster));
|
2010-05-12 10:19:11 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
relarr_free(RelInfoArr *rel_arr)
|
|
|
|
{
|
|
|
|
pg_free(rel_arr->rels);
|
|
|
|
rel_arr->nrels = 0;
|
2010-10-21 04:02:00 +08:00
|
|
|
rel_arr->last_relname_lookup = 0;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
dbarr_free(DbInfoArr *db_arr)
|
|
|
|
{
|
|
|
|
int dbnum;
|
|
|
|
|
|
|
|
for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
|
|
|
|
relarr_free(&db_arr->dbs[dbnum].rel_arr);
|
|
|
|
db_arr->ndbs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2011-01-02 01:06:36 +08:00
|
|
|
dbarr_print(ClusterInfo *cluster)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int dbnum;
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
pg_log(PG_DEBUG, "%s databases\n", CLUSTER_NAME(cluster));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-02 01:06:36 +08:00
|
|
|
pg_log(PG_DEBUG, "Database: %s\n", cluster->dbarr.dbs[dbnum].db_name);
|
|
|
|
relarr_print(&cluster->dbarr.dbs[dbnum].rel_arr);
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_DEBUG, "\n\n");
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
relarr_print(RelInfoArr *arr)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int relnum;
|
|
|
|
|
|
|
|
for (relnum = 0; relnum < arr->nrels; relnum++)
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_DEBUG, "relname: %s.%s: reloid: %u reltblspace: %s\n",
|
2010-05-12 10:19:11 +08:00
|
|
|
arr->rels[relnum].nspname, arr->rels[relnum].relname,
|
|
|
|
arr->rels[relnum].reloid, arr->rels[relnum].tablespace);
|
|
|
|
}
|