In pg_upgrade, remove functions that did sequential array scans looking

up relations, but rather order old/new relations and use the same array
index value for both.  This should speed up pg_upgrade for databases
with many relations.
This commit is contained in:
Bruce Momjian 2011-01-08 13:44:44 -05:00
parent ebaf648ad2
commit 002c105a07
5 changed files with 37 additions and 109 deletions

View File

@ -13,13 +13,13 @@
/*
* install_support_functions_in_db()
* install_support_functions_in_new_db()
*
* pg_upgrade requires some support functions that enable it to modify
* backend behavior.
*/
void
install_support_functions_in_db(const char *db_name)
install_support_functions_in_new_db(const char *db_name)
{
PGconn *conn = connectToServer(&new_cluster, db_name);
@ -87,7 +87,7 @@ install_support_functions_in_db(const char *db_name)
void
uninstall_support_functions(void)
uninstall_support_functions_from_new_cluster(void)
{
int dbnum;

View File

@ -21,10 +21,6 @@ static void create_rel_filename_map(const char *old_data, const char *new_data,
const DbInfo *old_db, const DbInfo *new_db,
const RelInfo *old_rel, const RelInfo *new_rel,
FileNameMap *map);
static RelInfo *relarr_lookup_rel_name(ClusterInfo *cluster, RelInfoArr *rel_arr,
const char *nspname, const char *relname);
static RelInfo *relarr_lookup_rel_oid(ClusterInfo *cluster, RelInfoArr *rel_arr,
Oid oid);
/*
@ -42,18 +38,22 @@ gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
int relnum;
int num_maps = 0;
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);
maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
old_db->rel_arr.nrels);
for (relnum = 0; relnum < old_db->rel_arr.nrels; relnum++)
{
RelInfo *old_rel = &old_db->rel_arr.rels[relnum];
RelInfo *new_rel;
/* old/new relation names always match */
new_rel = relarr_lookup_rel_name(&new_cluster, &new_db->rel_arr,
old_rel->nspname, old_rel->relname);
RelInfo *new_rel = &old_db->rel_arr.rels[relnum];
if (old_rel->reloid != new_rel->reloid)
pg_log(PG_FATAL, "mismatch of relation id: database \"%s\", old relid %d, new relid %d\n",
old_db->db_name, old_rel->reloid, new_rel->reloid);
create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
old_rel, new_rel, maps + num_maps);
num_maps++;
@ -153,7 +153,9 @@ get_db_infos(ClusterInfo *cluster)
"FROM pg_catalog.pg_database d "
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true");
"WHERE d.datallowconn = true "
/* we don't preserve pg_database.oid so we sort by name */
"ORDER BY 2");
i_datname = PQfnumber(res, "datname");
i_oid = PQfnumber(res, "oid");
@ -258,7 +260,8 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
"GROUP BY c.oid, n.nspname, c.relname, c.relfilenode,"
" c.reltoastrelid, t.spclocation, "
" n.nspname "
"ORDER BY t.spclocation, n.nspname, c.relname;",
/* we preserve pg_class.oid so we sort by it to match old/new */
"ORDER BY 1;",
FirstNormalObjectId,
/* does pg_largeobject_metadata need to be migrated? */
(GET_MAJOR_VERSION(old_cluster.major_version) <= 804) ?
@ -308,86 +311,6 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
dbinfo->rel_arr.rels = relinfos;
dbinfo->rel_arr.nrels = num_rels;
dbinfo->rel_arr.last_relname_lookup = 0;
}
/*
* 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_name()
*
* Searches "relname" in rel_arr. Returns the *real* pointer to the
* RelInfo structure.
*/
static RelInfo *
relarr_lookup_rel_name(ClusterInfo *cluster, RelInfoArr *rel_arr,
const char *nspname, const char *relname)
{
int relnum;
/* Test next lookup first, for speed */
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)
{
rel_arr->last_relname_lookup++;
return &rel_arr->rels[rel_arr->last_relname_lookup];
}
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)
{
rel_arr->last_relname_lookup = relnum;
return &rel_arr->rels[relnum];
}
}
pg_log(PG_FATAL, "Could not find %s.%s in %s cluster\n",
nspname, relname, CLUSTER_NAME(cluster));
return NULL;
}
/*
* relarr_lookup_rel_oid()
*
* Returns a pointer to the RelInfo structure for the
* given oid or NULL if the desired entry cannot be
* found.
*/
static RelInfo *
relarr_lookup_rel_oid(ClusterInfo *cluster, RelInfoArr *rel_arr, Oid oid)
{
int relnum;
for (relnum = 0; relnum < rel_arr->nrels; relnum++)
{
if (rel_arr->rels[relnum].reloid == oid)
return &rel_arr->rels[relnum];
}
pg_log(PG_FATAL, "Could not find %d in %s cluster\n",
oid, CLUSTER_NAME(cluster));
return NULL;
}
@ -396,7 +319,6 @@ free_rel_arr(RelInfoArr *rel_arr)
{
pg_free(rel_arr->rels);
rel_arr->nrels = 0;
rel_arr->last_relname_lookup = 0;
}

View File

@ -229,7 +229,7 @@ prepare_new_databases(void)
* Install support functions in the database accessed by
* GLOBALS_DUMP_FILE because it can preserve pg_authid.oid.
*/
install_support_functions_in_db(os_info.user);
install_support_functions_in_new_db(os_info.user);
/*
* We have to create the databases first so we can install support
@ -244,6 +244,7 @@ prepare_new_databases(void)
GLOBALS_DUMP_FILE, log_opts.filename);
check_ok();
/* we load this to get a current list of databases */
get_db_and_rel_infos(&new_cluster);
stop_postmaster(false, false);
@ -266,7 +267,7 @@ create_new_objects(void)
/* skip db we already installed */
if (strcmp(new_db->db_name, os_info.user) != 0)
install_support_functions_in_db(new_db->db_name);
install_support_functions_in_new_db(new_db->db_name);
}
check_ok();
@ -279,11 +280,11 @@ create_new_objects(void)
DB_DUMP_FILE, log_opts.filename);
check_ok();
/* regenerate now that we have db schemas */
/* regenerate now that we have objects in the databases */
dbarr_free(&new_cluster.dbarr);
get_db_and_rel_infos(&new_cluster);
uninstall_support_functions();
uninstall_support_functions_from_new_cluster();
stop_postmaster(false, false);
}

View File

@ -77,7 +77,6 @@ typedef struct
{
RelInfo *rels;
int nrels;
int last_relname_lookup; /* cache of last lookup location */
} RelInfoArr;
/*
@ -321,8 +320,8 @@ void check_hard_link(void);
/* function.c */
void install_support_functions_in_db(const char *db_name);
void uninstall_support_functions(void);
void install_support_functions_in_new_db(const char *db_name);
void uninstall_support_functions_from_new_cluster(void);
void get_loadable_libraries(void);
void check_loadable_libraries(void);
@ -331,8 +330,7 @@ void check_loadable_libraries(void);
FileNameMap *gen_db_file_maps(DbInfo *old_db,
DbInfo *new_db, int *nmaps, const char *old_pgdata,
const char *new_pgdata);
void get_db_and_rel_infos(ClusterInfo *cluster);
DbInfo *dbarr_lookup_db(DbInfoArr *db_arr, const char *db_name);
void get_db_and_rel_infos(ClusterInfo *cluster);
void dbarr_free(DbInfoArr *db_arr);
void print_maps(FileNameMap *maps, int n,
const char *dbName);

View File

@ -29,22 +29,29 @@ char scandir_file_pattern[MAXPGPATH];
* physically link the databases.
*/
const char *
transfer_all_new_dbs(DbInfoArr *olddb_arr,
DbInfoArr *newdb_arr, char *old_pgdata, char *new_pgdata)
transfer_all_new_dbs(DbInfoArr *old_db_arr,
DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
{
int dbnum;
const char *msg = NULL;
prep_status("Restoring user relation files\n");
for (dbnum = 0; dbnum < newdb_arr->ndbs; dbnum++)
if (old_db_arr->ndbs != new_db_arr->ndbs)
pg_log(PG_FATAL, "old and new clusters have a different number of databases\n");
for (dbnum = 0; dbnum < old_db_arr->ndbs; dbnum++)
{
DbInfo *new_db = &newdb_arr->dbs[dbnum];
DbInfo *old_db = dbarr_lookup_db(olddb_arr, new_db->db_name);
DbInfo *old_db = &old_db_arr->dbs[dbnum];
DbInfo *new_db = &new_db_arr->dbs[dbnum];
FileNameMap *mappings;
int n_maps;
pageCnvCtx *pageConverter = NULL;
if (strcmp(old_db->db_name, new_db->db_name) != 0)
pg_log(PG_FATAL, "old and new databases have a different names: old \"%s\", new \"%s\"\n",
old_db->db_name, new_db->db_name);
n_maps = 0;
mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
new_pgdata);