2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* info.c
|
|
|
|
*
|
|
|
|
* information support functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2014-01-08 05:05:30 +08:00
|
|
|
* Copyright (c) 2010-2014, 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;
|
2014-07-08 01:24:08 +08:00
|
|
|
int old_relnum, new_relnum;
|
2010-05-12 10:19:11 +08:00
|
|
|
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
|
|
|
|
2014-07-08 01:24:08 +08:00
|
|
|
/*
|
|
|
|
* The old database shouldn't have more relations than the new one.
|
|
|
|
* We force the new cluster to have a TOAST table if the old table
|
|
|
|
* had one.
|
|
|
|
*/
|
|
|
|
if (old_db->rel_arr.nrels > new_db->rel_arr.nrels)
|
|
|
|
pg_fatal("old and new databases \"%s\" have a mismatched number of relations\n",
|
|
|
|
old_db->db_name);
|
|
|
|
|
|
|
|
/* Drive the loop using new_relnum, which might be higher. */
|
|
|
|
for (old_relnum = new_relnum = 0; new_relnum < new_db->rel_arr.nrels;
|
|
|
|
new_relnum++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2014-07-08 01:24:08 +08:00
|
|
|
RelInfo *old_rel;
|
|
|
|
RelInfo *new_rel = &new_db->rel_arr.rels[new_relnum];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is possible that the new cluster has a TOAST table for a table
|
|
|
|
* that didn't need one in the old cluster, e.g. 9.0 to 9.1 changed the
|
|
|
|
* NUMERIC length computation. Therefore, if we have a TOAST table
|
|
|
|
* in the new cluster that doesn't match, skip over it and continue
|
|
|
|
* processing. It is possible this TOAST table used an OID that was
|
|
|
|
* reserved in the old cluster, but we have no way of testing that,
|
|
|
|
* and we would have already gotten an error at the new cluster schema
|
|
|
|
* creation stage. Fortunately, since we only restore the OID counter
|
|
|
|
* after schema restore, and restore in OID order via pg_dump, a
|
|
|
|
* conflict would only happen if the new TOAST table had a very low
|
|
|
|
* OID. However, TOAST tables created long after initial table
|
|
|
|
* creation can have any OID, particularly after OID wraparound.
|
|
|
|
*/
|
|
|
|
if (old_relnum == old_db->rel_arr.nrels)
|
|
|
|
{
|
|
|
|
if (strcmp(new_rel->nspname, "pg_toast") == 0)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
pg_fatal("Extra non-TOAST relation found in database \"%s\": new OID %d\n",
|
|
|
|
old_db->db_name, new_rel->reloid);
|
|
|
|
}
|
|
|
|
|
|
|
|
old_rel = &old_db->rel_arr.rels[old_relnum];
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-09 02:44:44 +08:00
|
|
|
if (old_rel->reloid != new_rel->reloid)
|
2014-07-08 01:24:08 +08:00
|
|
|
{
|
|
|
|
if (strcmp(new_rel->nspname, "pg_toast") == 0)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
pg_fatal("Mismatch of relation OID in database \"%s\": old OID %d, new OID %d\n",
|
|
|
|
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
|
2013-05-30 04:58:43 +08:00
|
|
|
* upgraded to 9.0 will get matching TOAST names. 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))
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("Mismatch of relation names in database \"%s\": "
|
2014-05-07 00:12:18 +08:00
|
|
|
"old name \"%s.%s\", new name \"%s.%s\"\n",
|
|
|
|
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++;
|
2014-07-08 01:24:08 +08:00
|
|
|
old_relnum++;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 01:24:08 +08:00
|
|
|
/* Did we fail to exhaust the old array? */
|
|
|
|
if (old_relnum != old_db->rel_arr.nrels)
|
|
|
|
pg_fatal("old and new databases \"%s\" have a mismatched number of relations\n",
|
2014-05-07 00:12:18 +08:00
|
|
|
old_db->db_name);
|
2012-10-02 23:53:45 +08:00
|
|
|
|
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.
|
|
|
|
*/
|
2014-02-13 05:35:24 +08:00
|
|
|
map->old_tablespace = old_data;
|
|
|
|
map->new_tablespace = new_data;
|
|
|
|
map->old_tablespace_suffix = "/base";
|
|
|
|
map->new_tablespace_suffix = "/base";
|
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 */
|
2014-02-13 05:35:24 +08:00
|
|
|
map->old_tablespace = old_rel->tablespace;
|
|
|
|
map->new_tablespace = new_rel->tablespace;
|
|
|
|
map->old_tablespace_suffix = old_cluster.tablespace_suffix;
|
|
|
|
map->new_tablespace_suffix = new_cluster.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,
|
Change the way encoding and locale checks are done in pg_upgrade.
Lc_collate and lc_ctype have been per-database settings since server version
8.4, but pg_upgrade was still treating them as cluster-wide options. It
fetched the values for the template0 databases in old and new cluster, and
compared them. That's backwards; the encoding and locale of the template0
database doesn't matter, as template0 is guaranteed to contain only ASCII
characters. But if there are any other databases that exist on both clusters
(in particular template1 and postgres databases), their encodings and
locales must be compatible.
Also, make the locale comparison more lenient. If the locale names are not
equal, try to canonicalize both of them by passing them to setlocale(). We
used to do that only when upgrading from 9.1 or below, but it seems like a
good idea even with newer versions. If we change the canonical form of a
locale, this allows pg_upgrade to still work. I'm about to do just that to
fix bug #11431, by mapping a locale name that contains non-ASCII characters
to a pure-ASCII alias of the same locale.
No backpatching, because earlier versions of pg_upgrade still support
upgrading from 8.3 servers. That would be more complicated, so it doesn't
seem worth it, given that we haven't received any complaints about this
from users.
2014-10-10 14:59:44 +08:00
|
|
|
i_encoding,
|
|
|
|
i_datcollate,
|
|
|
|
i_datctype,
|
2011-04-10 23:42:00 +08:00
|
|
|
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),
|
Change the way encoding and locale checks are done in pg_upgrade.
Lc_collate and lc_ctype have been per-database settings since server version
8.4, but pg_upgrade was still treating them as cluster-wide options. It
fetched the values for the template0 databases in old and new cluster, and
compared them. That's backwards; the encoding and locale of the template0
database doesn't matter, as template0 is guaranteed to contain only ASCII
characters. But if there are any other databases that exist on both clusters
(in particular template1 and postgres databases), their encodings and
locales must be compatible.
Also, make the locale comparison more lenient. If the locale names are not
equal, try to canonicalize both of them by passing them to setlocale(). We
used to do that only when upgrading from 9.1 or below, but it seems like a
good idea even with newer versions. If we change the canonical form of a
locale, this allows pg_upgrade to still work. I'm about to do just that to
fix bug #11431, by mapping a locale name that contains non-ASCII characters
to a pure-ASCII alias of the same locale.
No backpatching, because earlier versions of pg_upgrade still support
upgrading from 8.3 servers. That would be more complicated, so it doesn't
seem worth it, given that we haven't received any complaints about this
from users.
2014-10-10 14:59:44 +08:00
|
|
|
"SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, "
|
|
|
|
"%s AS spclocation "
|
2012-06-11 03:20:04 +08:00
|
|
|
"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) ?
|
Change the way encoding and locale checks are done in pg_upgrade.
Lc_collate and lc_ctype have been per-database settings since server version
8.4, but pg_upgrade was still treating them as cluster-wide options. It
fetched the values for the template0 databases in old and new cluster, and
compared them. That's backwards; the encoding and locale of the template0
database doesn't matter, as template0 is guaranteed to contain only ASCII
characters. But if there are any other databases that exist on both clusters
(in particular template1 and postgres databases), their encodings and
locales must be compatible.
Also, make the locale comparison more lenient. If the locale names are not
equal, try to canonicalize both of them by passing them to setlocale(). We
used to do that only when upgrading from 9.1 or below, but it seems like a
good idea even with newer versions. If we change the canonical form of a
locale, this allows pg_upgrade to still work. I'm about to do just that to
fix bug #11431, by mapping a locale name that contains non-ASCII characters
to a pure-ASCII alias of the same locale.
No backpatching, because earlier versions of pg_upgrade still support
upgrading from 8.3 servers. That would be more complicated, so it doesn't
seem worth it, given that we haven't received any complaints about this
from users.
2014-10-10 14:59:44 +08:00
|
|
|
"t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid)");
|
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");
|
Change the way encoding and locale checks are done in pg_upgrade.
Lc_collate and lc_ctype have been per-database settings since server version
8.4, but pg_upgrade was still treating them as cluster-wide options. It
fetched the values for the template0 databases in old and new cluster, and
compared them. That's backwards; the encoding and locale of the template0
database doesn't matter, as template0 is guaranteed to contain only ASCII
characters. But if there are any other databases that exist on both clusters
(in particular template1 and postgres databases), their encodings and
locales must be compatible.
Also, make the locale comparison more lenient. If the locale names are not
equal, try to canonicalize both of them by passing them to setlocale(). We
used to do that only when upgrading from 9.1 or below, but it seems like a
good idea even with newer versions. If we change the canonical form of a
locale, this allows pg_upgrade to still work. I'm about to do just that to
fix bug #11431, by mapping a locale name that contains non-ASCII characters
to a pure-ASCII alias of the same locale.
No backpatching, because earlier versions of pg_upgrade still support
upgrading from 8.3 servers. That would be more complicated, so it doesn't
seem worth it, given that we haven't received any complaints about this
from users.
2014-10-10 14:59:44 +08:00
|
|
|
i_encoding = PQfnumber(res, "encoding");
|
|
|
|
i_datcollate = PQfnumber(res, "datcollate");
|
|
|
|
i_datctype = PQfnumber(res, "datctype");
|
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));
|
Change the way encoding and locale checks are done in pg_upgrade.
Lc_collate and lc_ctype have been per-database settings since server version
8.4, but pg_upgrade was still treating them as cluster-wide options. It
fetched the values for the template0 databases in old and new cluster, and
compared them. That's backwards; the encoding and locale of the template0
database doesn't matter, as template0 is guaranteed to contain only ASCII
characters. But if there are any other databases that exist on both clusters
(in particular template1 and postgres databases), their encodings and
locales must be compatible.
Also, make the locale comparison more lenient. If the locale names are not
equal, try to canonicalize both of them by passing them to setlocale(). We
used to do that only when upgrading from 9.1 or below, but it seems like a
good idea even with newer versions. If we change the canonical form of a
locale, this allows pg_upgrade to still work. I'm about to do just that to
fix bug #11431, by mapping a locale name that contains non-ASCII characters
to a pure-ASCII alias of the same locale.
No backpatching, because earlier versions of pg_upgrade still support
upgrading from 8.3 servers. That would be more complicated, so it doesn't
seem worth it, given that we haven't received any complaints about this
from users.
2014-10-10 14:59:44 +08:00
|
|
|
dbinfos[tupnum].db_encoding = atoi(PQgetvalue(res, tupnum, i_encoding));
|
|
|
|
dbinfos[tupnum].db_collate = pg_strdup(PQgetvalue(res, tupnum, i_datcollate));
|
|
|
|
dbinfos[tupnum].db_ctype = pg_strdup(PQgetvalue(res, tupnum, i_datctype));
|
2014-02-13 05:35:24 +08:00
|
|
|
snprintf(dbinfos[tupnum].db_tablespace, sizeof(dbinfos[tupnum].db_tablespace), "%s",
|
2010-05-12 10:19:11 +08:00
|
|
|
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;
|
2014-02-13 05:35:24 +08:00
|
|
|
char *tablespace = 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];
|
2014-05-07 00:12:18 +08:00
|
|
|
char *last_namespace = NULL,
|
|
|
|
*last_tablespace = NULL;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/*
|
2014-07-01 07:55:55 +08:00
|
|
|
* pg_largeobject contains user data that does not appear in pg_dump
|
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),
|
2014-08-26 09:22:08 +08:00
|
|
|
/* get regular heap */
|
|
|
|
"WITH regular_heap (reloid) AS ( "
|
|
|
|
" SELECT c.oid "
|
|
|
|
" FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
|
|
|
|
" ON c.relnamespace = n.oid "
|
|
|
|
" LEFT OUTER JOIN pg_catalog.pg_index i "
|
|
|
|
" ON c.oid = i.indexrelid "
|
|
|
|
" WHERE relkind IN ('r', 'm', 'i', 'S') AND "
|
|
|
|
/*
|
|
|
|
* pg_dump only dumps valid indexes; testing indisready is necessary in
|
|
|
|
* 9.2, and harmless in earlier/later versions.
|
|
|
|
*/
|
2014-08-27 05:26:45 +08:00
|
|
|
" i.indisvalid IS DISTINCT FROM false AND "
|
|
|
|
" i.indisready IS DISTINCT FROM false AND "
|
2014-08-26 09:22:08 +08:00
|
|
|
/* exclude possible orphaned temp tables */
|
|
|
|
" ((n.nspname !~ '^pg_temp_' AND "
|
|
|
|
" n.nspname !~ '^pg_toast_temp_' AND "
|
|
|
|
/* skip pg_toast because toast index have relkind == 'i', not 't' */
|
|
|
|
" n.nspname NOT IN ('pg_catalog', 'information_schema', "
|
|
|
|
" 'binary_upgrade', 'pg_toast') AND "
|
|
|
|
" c.oid >= %u) OR "
|
|
|
|
" (n.nspname = 'pg_catalog' AND "
|
|
|
|
" relname IN ('pg_largeobject', 'pg_largeobject_loid_pn_index'%s) ))), "
|
|
|
|
/*
|
|
|
|
* We have to gather the TOAST tables in later steps because we
|
|
|
|
* can't schema-qualify TOAST tables.
|
|
|
|
*/
|
2014-08-27 05:26:45 +08:00
|
|
|
/* get TOAST heap */
|
2014-08-26 09:22:08 +08:00
|
|
|
" toast_heap (reloid) AS ( "
|
|
|
|
" SELECT reltoastrelid "
|
|
|
|
" FROM regular_heap JOIN pg_catalog.pg_class c "
|
|
|
|
" ON regular_heap.reloid = c.oid "
|
|
|
|
" AND c.reltoastrelid != %u), "
|
|
|
|
/* get indexes on regular and TOAST heap */
|
|
|
|
" all_index (reloid) AS ( "
|
|
|
|
" SELECT indexrelid "
|
|
|
|
" FROM pg_index "
|
|
|
|
" WHERE indisvalid "
|
|
|
|
" AND indrelid IN (SELECT reltoastrelid "
|
|
|
|
" FROM (SELECT reloid FROM regular_heap "
|
|
|
|
" UNION ALL "
|
|
|
|
" SELECT reloid FROM toast_heap) all_heap "
|
|
|
|
" JOIN pg_catalog.pg_class c "
|
|
|
|
" ON all_heap.reloid = c.oid "
|
|
|
|
" AND c.reltoastrelid != %u)) "
|
|
|
|
/* get all rels */
|
|
|
|
"SELECT c.oid, n.nspname, c.relname, "
|
|
|
|
" c.relfilenode, c.reltablespace, %s "
|
|
|
|
"FROM (SELECT reloid FROM regular_heap "
|
|
|
|
" UNION ALL "
|
|
|
|
" SELECT reloid FROM toast_heap "
|
|
|
|
" UNION ALL "
|
|
|
|
" SELECT reloid FROM all_index) all_rels "
|
|
|
|
" JOIN pg_catalog.pg_class c "
|
|
|
|
" ON all_rels.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 "
|
2012-10-02 23:46:08 +08:00
|
|
|
/* we preserve pg_class.oid so we sort by it to match old/new */
|
2014-08-26 09:22:08 +08:00
|
|
|
"ORDER BY 1;",
|
|
|
|
FirstNormalObjectId,
|
|
|
|
/* does pg_largeobject_metadata need to be migrated? */
|
|
|
|
(GET_MAJOR_VERSION(old_cluster.major_version) <= 804) ?
|
|
|
|
"" : ", 'pg_largeobject_metadata', 'pg_largeobject_metadata_oid_index'",
|
|
|
|
InvalidOid, InvalidOid,
|
2012-10-02 23:46:08 +08:00
|
|
|
/* 9.2 removed the spclocation column */
|
2014-08-26 09:22:08 +08:00
|
|
|
(GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
|
|
|
|
"t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation");
|
2012-10-02 23:46:08 +08:00
|
|
|
|
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-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);
|
2014-02-13 05:35:24 +08:00
|
|
|
curr->nsp_alloc = false;
|
|
|
|
|
|
|
|
/*
|
2014-05-07 00:12:18 +08:00
|
|
|
* Many of the namespace and tablespace strings are identical, so we
|
|
|
|
* try to reuse the allocated string pointers where possible to reduce
|
|
|
|
* memory consumption.
|
2014-02-13 05:35:24 +08:00
|
|
|
*/
|
|
|
|
/* Can we reuse the previous string allocation? */
|
|
|
|
if (last_namespace && strcmp(nspname, last_namespace) == 0)
|
|
|
|
curr->nspname = last_namespace;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
last_namespace = curr->nspname = pg_strdup(nspname);
|
|
|
|
curr->nsp_alloc = true;
|
|
|
|
}
|
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));
|
2014-02-13 05:35:24 +08:00
|
|
|
curr->tblsp_alloc = false;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2014-02-13 05:35:24 +08:00
|
|
|
/* Is the tablespace oid non-zero? */
|
2012-04-11 07:57:14 +08:00
|
|
|
if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
|
2014-02-13 05:35:24 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The tablespace location might be "", meaning the cluster
|
|
|
|
* default location, i.e. pg_default or pg_global.
|
|
|
|
*/
|
|
|
|
tablespace = PQgetvalue(res, relnum, i_spclocation);
|
|
|
|
|
|
|
|
/* Can we reuse the previous string allocation? */
|
|
|
|
if (last_tablespace && strcmp(tablespace, last_tablespace) == 0)
|
|
|
|
curr->tablespace = last_tablespace;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
last_tablespace = curr->tablespace = pg_strdup(tablespace);
|
|
|
|
curr->tblsp_alloc = true;
|
|
|
|
}
|
|
|
|
}
|
2012-04-11 07:57:14 +08:00
|
|
|
else
|
2014-02-13 05:35:24 +08:00
|
|
|
/* A zero reltablespace oid indicates the database tablespace. */
|
|
|
|
curr->tablespace = dbinfo->db_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++)
|
|
|
|
{
|
2014-02-13 05:35:24 +08:00
|
|
|
if (rel_arr->rels[relnum].nsp_alloc)
|
|
|
|
pg_free(rel_arr->rels[relnum].nspname);
|
2012-12-21 02:56:24 +08:00
|
|
|
pg_free(rel_arr->rels[relnum].relname);
|
2014-02-13 05:35:24 +08:00
|
|
|
if (rel_arr->rels[relnum].tblsp_alloc)
|
|
|
|
pg_free(rel_arr->rels[relnum].tablespace);
|
2012-12-21 02:56:24 +08:00
|
|
|
}
|
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",
|
2013-06-01 21:38:15 +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
|
|
|
}
|