Force pg_upgrade's to preserve pg_class.oid, not pg_class.relfilenode.

Toast tables have identical pg_class.oid and pg_class.relfilenode, but
for clarity it is good to preserve the pg_class.oid.

Update comments regarding what is preserved, and do some
variable/function renaming for clarity.
This commit is contained in:
Bruce Momjian 2011-01-07 21:25:34 -05:00
parent 541fc3d4df
commit 2896c87ce4
10 changed files with 112 additions and 106 deletions

View File

@ -49,31 +49,31 @@ install_support_functions(void)
"LANGUAGE C STRICT;"));
PQclear(executeQueryOrDie(conn,
"CREATE OR REPLACE FUNCTION "
" binary_upgrade.set_next_pg_type_array_oid(OID) "
" binary_upgrade.set_next_array_pg_type_oid(OID) "
"RETURNS VOID "
"AS '$libdir/pg_upgrade_support' "
"LANGUAGE C STRICT;"));
PQclear(executeQueryOrDie(conn,
"CREATE OR REPLACE FUNCTION "
" binary_upgrade.set_next_pg_type_toast_oid(OID) "
" binary_upgrade.set_next_toast_pg_type_oid(OID) "
"RETURNS VOID "
"AS '$libdir/pg_upgrade_support' "
"LANGUAGE C STRICT;"));
PQclear(executeQueryOrDie(conn,
"CREATE OR REPLACE FUNCTION "
" binary_upgrade.set_next_heap_relfilenode(OID) "
" binary_upgrade.set_next_heap_pg_class_oid(OID) "
"RETURNS VOID "
"AS '$libdir/pg_upgrade_support' "
"LANGUAGE C STRICT;"));
PQclear(executeQueryOrDie(conn,
"CREATE OR REPLACE FUNCTION "
" binary_upgrade.set_next_toast_relfilenode(OID) "
" binary_upgrade.set_next_index_pg_class_oid(OID) "
"RETURNS VOID "
"AS '$libdir/pg_upgrade_support' "
"LANGUAGE C STRICT;"));
PQclear(executeQueryOrDie(conn,
"CREATE OR REPLACE FUNCTION "
" binary_upgrade.set_next_index_relfilenode(OID) "
" binary_upgrade.set_next_toast_pg_class_oid(OID) "
"RETURNS VOID "
"AS '$libdir/pg_upgrade_support' "
"LANGUAGE C STRICT;"));

View File

@ -266,7 +266,7 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
/*
* pg_largeobject contains user data that does not appear the pg_dumpall
* --schema-only output, so we have to upgrade that system table heap and
* --schema-only output, so we have to copy that system table heap and
* index. Ideally we could just get the relfilenode from template1 but
* pg_largeobject_loid_pn_index's relfilenode can change if the table was
* reindexed so we get the relfilenode for each database and upgrade it as

View File

@ -8,23 +8,30 @@
*/
/*
* To simplify the upgrade process, we force certain system items to be
* consistent between old and new clusters:
* To simplify the upgrade process, we force certain system values to be
* identical between old and new clusters:
*
* We control all assignments of pg_class.relfilenode so we can keep the
* same relfilenodes for old and new files. The only exception is
* pg_largeobject, pg_largeobject_metadata, and its indexes, which can
* change due to a cluster, reindex, or vacuum full. (We don't create
* those so have no control over their oid/relfilenode values.)
* We control all assignments of pg_class.oid (and relfilenode) so toast
* oids are the same between old and new clusters. This is important
* because toast oids are stored as toast pointers in user tables.
*
* While pg_class.oid and pg_class.relfilenode are intially the same, they
* can diverge due to cluster, reindex, or vacuum full. The new cluster
* will again have matching pg_class.relfilenode and pg_class.oid values,
* but based on the new relfilenode value, so the old/new oids might
* differ.
* The only place where old/new relfilenode might not match is
* pg_largeobject, pg_largeobject_metadata, and its indexes,
* which can change their relfilenode values due to a cluster, reindex,
* or vacuum full. (We don't create those so have no control over their
* new relfilenode values.)
*
* We control all assignments of pg_type.oid because these are stored
* in composite types.
* FYI, while pg_class.oid and pg_class.relfilenode are intially the same
* in a cluster, but they can diverge due to cluster, reindex, or vacuum
* full. The new cluster will again have matching pg_class.relfilenode
* and pg_class.oid values, but based on the old relfilenode value, so the
* old/new oids might differ.
*
* We control all assignments of pg_type.oid because these oid are stored
* in user composite type values.
*
* We control all assignments of pg_enum.oid because these oid are stored
* in user tables as enum values.
*/

View File

@ -67,8 +67,8 @@ typedef struct
{
char nspname[NAMEDATALEN]; /* namespace name */
char relname[NAMEDATALEN]; /* relation name */
Oid reloid; /* relation oid */
Oid relfilenode; /* relation relfile node */
Oid reloid; /* relation oid */
Oid relfilenode; /* relation relfile node */
Oid toastrelid; /* oid of the toast relation */
char tablespace[MAXPGPATH]; /* relations tablespace path */
} RelInfo;

View File

@ -21,29 +21,36 @@ PG_MODULE_MAGIC;
#endif
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_array_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_toast_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_heap_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_toast_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_index_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_enum_oid;
Datum set_next_pg_type_oid(PG_FUNCTION_ARGS);
Datum set_next_pg_type_array_oid(PG_FUNCTION_ARGS);
Datum set_next_pg_type_toast_oid(PG_FUNCTION_ARGS);
Datum set_next_heap_relfilenode(PG_FUNCTION_ARGS);
Datum set_next_toast_relfilenode(PG_FUNCTION_ARGS);
Datum set_next_index_relfilenode(PG_FUNCTION_ARGS);
Datum set_next_array_pg_type_oid(PG_FUNCTION_ARGS);
Datum set_next_toast_pg_type_oid(PG_FUNCTION_ARGS);
Datum set_next_heap_pg_class_oid(PG_FUNCTION_ARGS);
Datum set_next_index_pg_class_oid(PG_FUNCTION_ARGS);
Datum set_next_toast_pg_class_oid(PG_FUNCTION_ARGS);
Datum set_next_pg_enum_oid(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(set_next_pg_type_oid);
PG_FUNCTION_INFO_V1(set_next_pg_type_array_oid);
PG_FUNCTION_INFO_V1(set_next_pg_type_toast_oid);
PG_FUNCTION_INFO_V1(set_next_heap_relfilenode);
PG_FUNCTION_INFO_V1(set_next_toast_relfilenode);
PG_FUNCTION_INFO_V1(set_next_index_relfilenode);
PG_FUNCTION_INFO_V1(set_next_array_pg_type_oid);
PG_FUNCTION_INFO_V1(set_next_toast_pg_type_oid);
PG_FUNCTION_INFO_V1(set_next_heap_pg_class_oid);
PG_FUNCTION_INFO_V1(set_next_index_pg_class_oid);
PG_FUNCTION_INFO_V1(set_next_toast_pg_class_oid);
PG_FUNCTION_INFO_V1(set_next_pg_enum_oid);
Datum
set_next_pg_type_oid(PG_FUNCTION_ARGS)
{
@ -55,51 +62,51 @@ set_next_pg_type_oid(PG_FUNCTION_ARGS)
}
Datum
set_next_pg_type_array_oid(PG_FUNCTION_ARGS)
set_next_array_pg_type_oid(PG_FUNCTION_ARGS)
{
Oid typoid = PG_GETARG_OID(0);
binary_upgrade_next_pg_type_array_oid = typoid;
binary_upgrade_next_array_pg_type_oid = typoid;
PG_RETURN_VOID();
}
Datum
set_next_pg_type_toast_oid(PG_FUNCTION_ARGS)
set_next_toast_pg_type_oid(PG_FUNCTION_ARGS)
{
Oid typoid = PG_GETARG_OID(0);
binary_upgrade_next_pg_type_toast_oid = typoid;
binary_upgrade_next_toast_pg_type_oid = typoid;
PG_RETURN_VOID();
}
Datum
set_next_heap_relfilenode(PG_FUNCTION_ARGS)
set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid relfilenode = PG_GETARG_OID(0);
Oid reloid = PG_GETARG_OID(0);
binary_upgrade_next_heap_relfilenode = relfilenode;
binary_upgrade_next_heap_pg_class_oid = reloid;
PG_RETURN_VOID();
}
Datum
set_next_toast_relfilenode(PG_FUNCTION_ARGS)
set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid relfilenode = PG_GETARG_OID(0);
Oid reloid = PG_GETARG_OID(0);
binary_upgrade_next_toast_relfilenode = relfilenode;
binary_upgrade_next_index_pg_class_oid = reloid;
PG_RETURN_VOID();
}
Datum
set_next_index_relfilenode(PG_FUNCTION_ARGS)
set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid relfilenode = PG_GETARG_OID(0);
Oid reloid = PG_GETARG_OID(0);
binary_upgrade_next_index_relfilenode = relfilenode;
binary_upgrade_next_toast_pg_class_oid = reloid;
PG_RETURN_VOID();
}

View File

@ -74,8 +74,8 @@
/* Potentially set by contrib/pg_upgrade_support functions */
Oid binary_upgrade_next_heap_relfilenode = InvalidOid;
Oid binary_upgrade_next_toast_relfilenode = InvalidOid;
Oid binary_upgrade_next_heap_pg_class_oid = InvalidOid;
Oid binary_upgrade_next_toast_pg_class_oid = InvalidOid;
static void AddNewRelationTuple(Relation pg_class_desc,
Relation new_rel_desc,
@ -987,22 +987,22 @@ heap_create_with_catalog(const char *relname,
if (!OidIsValid(relid))
{
/*
* Use binary-upgrade override for pg_class.relfilenode/oid,
* Use binary-upgrade override for pg_class.oid/relfilenode,
* if supplied.
*/
if (OidIsValid(binary_upgrade_next_heap_relfilenode) &&
if (OidIsValid(binary_upgrade_next_heap_pg_class_oid) &&
(relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
relkind == RELKIND_VIEW || relkind == RELKIND_COMPOSITE_TYPE ||
relkind == RELKIND_FOREIGN_TABLE))
{
relid = binary_upgrade_next_heap_relfilenode;
binary_upgrade_next_heap_relfilenode = InvalidOid;
relid = binary_upgrade_next_heap_pg_class_oid;
binary_upgrade_next_heap_pg_class_oid = InvalidOid;
}
else if (OidIsValid(binary_upgrade_next_toast_relfilenode) &&
else if (OidIsValid(binary_upgrade_next_toast_pg_class_oid) &&
relkind == RELKIND_TOASTVALUE)
{
relid = binary_upgrade_next_toast_relfilenode;
binary_upgrade_next_toast_relfilenode = InvalidOid;
relid = binary_upgrade_next_toast_pg_class_oid;
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
}
else
relid = GetNewRelFileNode(reltablespace, pg_class_desc,

View File

@ -69,7 +69,7 @@
/* Potentially set by contrib/pg_upgrade_support functions */
Oid binary_upgrade_next_index_relfilenode = InvalidOid;
Oid binary_upgrade_next_index_pg_class_oid = InvalidOid;
/* state info for validate_index bulkdelete callback */
typedef struct
@ -641,13 +641,13 @@ index_create(Oid heapRelationId,
if (!OidIsValid(indexRelationId))
{
/*
* Use binary-upgrade override for pg_class.relfilenode/oid,
* Use binary-upgrade override for pg_class.oid/relfilenode,
* if supplied.
*/
if (OidIsValid(binary_upgrade_next_index_relfilenode))
if (OidIsValid(binary_upgrade_next_index_pg_class_oid))
{
indexRelationId = binary_upgrade_next_index_relfilenode;
binary_upgrade_next_index_relfilenode = InvalidOid;
indexRelationId = binary_upgrade_next_index_pg_class_oid;
binary_upgrade_next_index_pg_class_oid = InvalidOid;
}
else
{

View File

@ -32,9 +32,9 @@
#include "utils/syscache.h"
/* Potentially set by contrib/pg_upgrade_support functions */
extern Oid binary_upgrade_next_toast_relfilenode;
extern Oid binary_upgrade_next_toast_pg_class_oid;
Oid binary_upgrade_next_pg_type_toast_oid = InvalidOid;
Oid binary_upgrade_next_toast_pg_type_oid = InvalidOid;
static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
Datum reloptions);
@ -156,7 +156,7 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptio
* creation even if it seems not to need one.
*/
if (!needs_toast_table(rel) &&
!OidIsValid(binary_upgrade_next_toast_relfilenode))
!OidIsValid(binary_upgrade_next_toast_pg_class_oid))
return false;
/*
@ -201,10 +201,10 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptio
namespaceid = PG_TOAST_NAMESPACE;
/* Use binary-upgrade override for pg_type.oid, if supplied. */
if (OidIsValid(binary_upgrade_next_pg_type_toast_oid))
if (OidIsValid(binary_upgrade_next_toast_pg_type_oid))
{
toast_typid = binary_upgrade_next_pg_type_toast_oid;
binary_upgrade_next_pg_type_toast_oid = InvalidOid;
toast_typid = binary_upgrade_next_toast_pg_type_oid;
binary_upgrade_next_toast_pg_type_oid = InvalidOid;
}
toast_relid = heap_create_with_catalog(toast_relname,

View File

@ -75,7 +75,7 @@ typedef struct
} RelToCheck;
/* Potentially set by contrib/pg_upgrade_support functions */
Oid binary_upgrade_next_pg_type_array_oid = InvalidOid;
Oid binary_upgrade_next_array_pg_type_oid = InvalidOid;
static Oid findTypeInputFunction(List *procname, Oid typeOid);
static Oid findTypeOutputFunction(List *procname, Oid typeOid);
@ -1519,10 +1519,10 @@ AssignTypeArrayOid(void)
Oid type_array_oid;
/* Use binary-upgrade override for pg_type.typarray, if supplied. */
if (OidIsValid(binary_upgrade_next_pg_type_array_oid))
if (OidIsValid(binary_upgrade_next_array_pg_type_oid))
{
type_array_oid = binary_upgrade_next_pg_type_array_oid;
binary_upgrade_next_pg_type_array_oid = InvalidOid;
type_array_oid = binary_upgrade_next_array_pg_type_oid;
binary_upgrade_next_array_pg_type_oid = InvalidOid;
}
else
{

View File

@ -228,7 +228,7 @@ static void binary_upgrade_set_type_oids_by_type_oid(
PQExpBuffer upgrade_buffer, Oid pg_type_oid);
static bool binary_upgrade_set_type_oids_by_rel_oid(
PQExpBuffer upgrade_buffer, Oid pg_rel_oid);
static void binary_upgrade_set_relfilenodes(PQExpBuffer upgrade_buffer,
static void binary_upgrade_set_pg_class_oids(PQExpBuffer upgrade_buffer,
Oid pg_class_oid, bool is_index);
static const char *getAttrName(int attrnum, TableInfo *tblInfo);
static const char *fmtCopyColumnList(const TableInfo *ti);
@ -2243,7 +2243,7 @@ binary_upgrade_set_type_oids_by_type_oid(PQExpBuffer upgrade_buffer,
appendPQExpBuffer(upgrade_buffer,
"\n-- For binary upgrade, must preserve pg_type array oid\n");
appendPQExpBuffer(upgrade_buffer,
"SELECT binary_upgrade.set_next_pg_type_array_oid('%u'::pg_catalog.oid);\n\n",
"SELECT binary_upgrade.set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
pg_type_array_oid);
}
@ -2296,7 +2296,7 @@ binary_upgrade_set_type_oids_by_rel_oid(PQExpBuffer upgrade_buffer,
appendPQExpBuffer(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type toast oid\n");
appendPQExpBuffer(upgrade_buffer,
"SELECT binary_upgrade.set_next_pg_type_toast_oid('%u'::pg_catalog.oid);\n\n",
"SELECT binary_upgrade.set_next_toast_pg_type_oid('%u'::pg_catalog.oid);\n\n",
pg_type_toast_oid);
toast_set = true;
@ -2309,24 +2309,17 @@ binary_upgrade_set_type_oids_by_rel_oid(PQExpBuffer upgrade_buffer,
}
static void
binary_upgrade_set_relfilenodes(PQExpBuffer upgrade_buffer, Oid pg_class_oid,
binary_upgrade_set_pg_class_oids(PQExpBuffer upgrade_buffer, Oid pg_class_oid,
bool is_index)
{
PQExpBuffer upgrade_query = createPQExpBuffer();
int ntups;
PGresult *upgrade_res;
Oid pg_class_relfilenode;
Oid pg_class_reltoastrelid;
Oid pg_class_reltoastidxid;
/*
* Note: we don't need to use pg_relation_filenode() here because this
* function is not intended to be used against system catalogs. Otherwise
* we'd have to worry about which versions pg_relation_filenode is
* available in.
*/
appendPQExpBuffer(upgrade_query,
"SELECT c.relfilenode, c.reltoastrelid, t.reltoastidxid "
"SELECT c.reltoastrelid, t.reltoastidxid "
"FROM pg_catalog.pg_class c LEFT JOIN "
"pg_catalog.pg_class t ON (c.reltoastrelid = t.oid) "
"WHERE c.oid = '%u'::pg_catalog.oid;",
@ -2346,44 +2339,43 @@ binary_upgrade_set_relfilenodes(PQExpBuffer upgrade_buffer, Oid pg_class_oid,
exit_nicely();
}
pg_class_relfilenode = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "relfilenode")));
pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastrelid")));
pg_class_reltoastidxid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastidxid")));
appendPQExpBuffer(upgrade_buffer,
"\n-- For binary upgrade, must preserve relfilenodes\n");
"\n-- For binary upgrade, must preserve pg_class oids\n");
if (!is_index)
{
appendPQExpBuffer(upgrade_buffer,
"SELECT binary_upgrade.set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
pg_class_relfilenode);
"SELECT binary_upgrade.set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
/* only tables have toast tables, not indexes */
if (OidIsValid(pg_class_reltoastrelid))
{
/*
* One complexity is that the table definition might not require the
* creation of a TOAST table, and the TOAST table might have been
* created long after table creation, when the table was loaded with
* wide data. By setting the TOAST relfilenode we force creation of
* the TOAST heap and TOAST index by the backend so we can cleanly
* migrate the files during binary migration.
* One complexity is that the table definition might not require
* the creation of a TOAST table, and the TOAST table might have
* been created long after table creation, when the table was
* loaded with wide data. By setting the TOAST oid we force
* creation of the TOAST heap and TOAST index by the backend
* so we can cleanly copy the files during binary upgrade.
*/
appendPQExpBuffer(upgrade_buffer,
"SELECT binary_upgrade.set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
"SELECT binary_upgrade.set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_reltoastrelid);
/* every toast table has an index */
appendPQExpBuffer(upgrade_buffer,
"SELECT binary_upgrade.set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
"SELECT binary_upgrade.set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_reltoastidxid);
}
}
else
appendPQExpBuffer(upgrade_buffer,
"SELECT binary_upgrade.set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
pg_class_relfilenode);
"SELECT binary_upgrade.set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
appendPQExpBuffer(upgrade_buffer, "\n");
@ -7364,7 +7356,7 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
Oid typrelid = atooid(PQgetvalue(res, 0, i_typrelid));
binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid);
binary_upgrade_set_relfilenodes(q, typrelid, false);
binary_upgrade_set_pg_class_oids(q, typrelid, false);
}
appendPQExpBuffer(q, "CREATE TYPE %s AS (",
@ -11083,7 +11075,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
fmtId(tbinfo->dobj.name));
if (binary_upgrade)
binary_upgrade_set_relfilenodes(q, tbinfo->dobj.catId.oid, false);
binary_upgrade_set_pg_class_oids(q, tbinfo->dobj.catId.oid, false);
appendPQExpBuffer(q, "CREATE VIEW %s AS\n %s\n",
fmtId(tbinfo->dobj.name), viewdef);
@ -11134,7 +11126,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
fmtId(tbinfo->dobj.name));
if (binary_upgrade)
binary_upgrade_set_relfilenodes(q, tbinfo->dobj.catId.oid, false);
binary_upgrade_set_pg_class_oids(q, tbinfo->dobj.catId.oid, false);
appendPQExpBuffer(q, "CREATE %s%s %s",
tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
@ -11616,7 +11608,7 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
if (indxinfo->indexconstraint == 0)
{
if (binary_upgrade)
binary_upgrade_set_relfilenodes(q, indxinfo->dobj.catId.oid, true);
binary_upgrade_set_pg_class_oids(q, indxinfo->dobj.catId.oid, true);
/* Plain secondary index */
appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef);
@ -11699,7 +11691,7 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
}
if (binary_upgrade && !coninfo->condef)
binary_upgrade_set_relfilenodes(q, indxinfo->dobj.catId.oid, true);
binary_upgrade_set_pg_class_oids(q, indxinfo->dobj.catId.oid, true);
appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
fmtId(tbinfo->dobj.name));
@ -12112,7 +12104,7 @@ dumpSequence(Archive *fout, TableInfo *tbinfo)
if (binary_upgrade)
{
binary_upgrade_set_relfilenodes(query, tbinfo->dobj.catId.oid, false);
binary_upgrade_set_pg_class_oids(query, tbinfo->dobj.catId.oid, false);
binary_upgrade_set_type_oids_by_rel_oid(query, tbinfo->dobj.catId.oid);
}