Binary upgrade:

Modify pg_dump --binary-upgrade and add backend support routines to
support the preservation of pg_type oids when doing a binary upgrade.
This allows user-defined composite types and arrays to be binary
upgraded.
This commit is contained in:
Bruce Momjian 2009-12-24 22:09:24 +00:00
parent 668e37d138
commit c44327afa4
6 changed files with 226 additions and 27 deletions

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/catalog/heap.c,v 1.361 2009/12/07 05:22:21 tgl Exp $
* $PostgreSQL: pgsql/src/backend/catalog/heap.c,v 1.362 2009/12/24 22:09:23 momjian Exp $
*
*
* INTERFACE ROUTINES
@ -1001,13 +1001,7 @@ heap_create_with_catalog(const char *relname,
if (IsUnderPostmaster && (relkind == RELKIND_RELATION ||
relkind == RELKIND_VIEW ||
relkind == RELKIND_COMPOSITE_TYPE))
{
/* OK, so pre-assign a type OID for the array type */
Relation pg_type = heap_open(TypeRelationId, AccessShareLock);
new_array_oid = GetNewOid(pg_type);
heap_close(pg_type, AccessShareLock);
}
new_array_oid = AssignTypeArrayOid();
/*
* Since defining a relation also defines a complex type, we add a new

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/catalog/pg_type.c,v 1.127 2009/08/16 18:14:34 tgl Exp $
* $PostgreSQL: pgsql/src/backend/catalog/pg_type.c,v 1.128 2009/12/24 22:09:23 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -32,6 +32,7 @@
#include "utils/rel.h"
#include "utils/syscache.h"
Oid binary_upgrade_next_pg_type_oid = InvalidOid;
/* ----------------------------------------------------------------
* TypeShellMake
@ -119,6 +120,12 @@ TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
*/
tup = heap_form_tuple(tupDesc, values, nulls);
if (OidIsValid(binary_upgrade_next_pg_type_oid))
{
HeapTupleSetOid(tup, binary_upgrade_next_pg_type_oid);
binary_upgrade_next_pg_type_oid = InvalidOid;
}
/*
* insert the tuple in the relation and get the tuple's oid.
*/
@ -409,10 +416,16 @@ TypeCreate(Oid newTypeOid,
values,
nulls);
/* Force the OID if requested by caller, else heap_insert does it */
/* Force the OID if requested by caller */
if (OidIsValid(newTypeOid))
HeapTupleSetOid(tup, newTypeOid);
else if (OidIsValid(binary_upgrade_next_pg_type_oid))
{
HeapTupleSetOid(tup, binary_upgrade_next_pg_type_oid);
binary_upgrade_next_pg_type_oid = InvalidOid;
}
/* else allow system to assign oid */
typeObjectId = simple_heap_insert(pg_type_desc, tup);
}

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/catalog/toasting.c,v 1.22 2009/12/23 02:35:18 tgl Exp $
* $PostgreSQL: pgsql/src/backend/catalog/toasting.c,v 1.23 2009/12/24 22:09:23 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -31,6 +31,7 @@
#include "utils/builtins.h"
#include "utils/syscache.h"
Oid binary_upgrade_next_pg_type_toast_oid = InvalidOid;
static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
Datum reloptions, bool force);
@ -121,6 +122,7 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
Relation class_rel;
Oid toast_relid;
Oid toast_idxid;
Oid toast_typid = InvalidOid;
Oid namespaceid;
char toast_relname[NAMEDATALEN];
char toast_idxname[NAMEDATALEN];
@ -199,11 +201,17 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
else
namespaceid = PG_TOAST_NAMESPACE;
if (OidIsValid(binary_upgrade_next_pg_type_toast_oid))
{
toast_typid = binary_upgrade_next_pg_type_toast_oid;
binary_upgrade_next_pg_type_toast_oid = InvalidOid;
}
toast_relid = heap_create_with_catalog(toast_relname,
namespaceid,
rel->rd_rel->reltablespace,
toastOid,
InvalidOid,
toast_typid,
rel->rd_rel->relowner,
tupdesc,
NIL,

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.140 2009/12/19 00:47:57 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.141 2009/12/24 22:09:23 momjian Exp $
*
* DESCRIPTION
* The "DefineFoo" routines take the parse tree and pick out the
@ -74,6 +74,7 @@ typedef struct
/* atts[] is of allocated length RelationGetNumberOfAttributes(rel) */
} RelToCheck;
Oid binary_upgrade_next_pg_type_array_oid = InvalidOid;
static Oid findTypeInputFunction(List *procname, Oid typeOid);
static Oid findTypeOutputFunction(List *procname, Oid typeOid);
@ -143,7 +144,6 @@ DefineType(List *names, List *parameters)
Oid array_oid;
Oid typoid;
Oid resulttype;
Relation pg_type;
ListCell *pl;
/*
@ -522,10 +522,7 @@ DefineType(List *names, List *parameters)
NameListToString(analyzeName));
#endif
/* Preassign array type OID so we can insert it in pg_type.typarray */
pg_type = heap_open(TypeRelationId, AccessShareLock);
array_oid = GetNewOid(pg_type);
heap_close(pg_type, AccessShareLock);
array_oid = AssignTypeArrayOid();
/*
* now have TypeCreate do all the real work.
@ -1101,7 +1098,6 @@ DefineEnum(CreateEnumStmt *stmt)
AclResult aclresult;
Oid old_type_oid;
Oid enumArrayOid;
Relation pg_type;
/* Convert list of names to a name and namespace */
enumNamespace = QualifiedNameGetCreationNamespace(stmt->typeName,
@ -1129,10 +1125,7 @@ DefineEnum(CreateEnumStmt *stmt)
errmsg("type \"%s\" already exists", enumName)));
}
/* Preassign array type OID so we can insert it in pg_type.typarray */
pg_type = heap_open(TypeRelationId, AccessShareLock);
enumArrayOid = GetNewOid(pg_type);
heap_close(pg_type, AccessShareLock);
enumArrayOid = AssignTypeArrayOid();
/* Create the pg_type entry */
enumTypeOid =
@ -1470,6 +1463,33 @@ findTypeAnalyzeFunction(List *procname, Oid typeOid)
return procOid;
}
/*
* AssignTypeArrayOid
*
* Pre-assign the type's array OID for use in pg_type.typarray
*/
Oid
AssignTypeArrayOid(void)
{
Oid type_array_oid;
/* Pre-assign the type's array OID for use in pg_type.typarray */
if (OidIsValid(binary_upgrade_next_pg_type_array_oid))
{
type_array_oid = binary_upgrade_next_pg_type_array_oid;
binary_upgrade_next_pg_type_array_oid = InvalidOid;
}
else
{
Relation pg_type = heap_open(TypeRelationId, AccessShareLock);
type_array_oid = GetNewOid(pg_type);
heap_close(pg_type, AccessShareLock);
}
return type_array_oid;
}
/*-------------------------------------------------------------------
* DefineCompositeType

View File

@ -12,7 +12,7 @@
* by PostgreSQL
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.560 2009/12/23 04:10:50 momjian Exp $
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.561 2009/12/24 22:09:23 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -196,6 +196,11 @@ static int dumpBlobComments(Archive *AH, void *arg);
static void dumpDatabase(Archive *AH);
static void dumpEncoding(Archive *AH);
static void dumpStdStrings(Archive *AH);
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_clear_pg_type_toast_oid(PQExpBuffer upgrade_buffer);
static const char *getAttrName(int attrnum, TableInfo *tblInfo);
static const char *fmtCopyColumnList(const TableInfo *ti);
static void do_sql_command(PGconn *conn, const char *query);
@ -2176,6 +2181,131 @@ dumpBlobComments(Archive *AH, void *arg)
return 1;
}
static void
binary_upgrade_set_type_oids_by_type_oid(PQExpBuffer upgrade_buffer,
Oid pg_type_oid)
{
PQExpBuffer upgrade_query = createPQExpBuffer();
int ntups;
PGresult *upgrade_res;
Oid pg_type_array_oid;
appendPQExpBuffer(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
appendPQExpBuffer(upgrade_buffer,
"SELECT binary_upgrade.set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
pg_type_oid);
/* we only support old >= 8.3 for binary upgrades */
appendPQExpBuffer(upgrade_query,
"SELECT typarray "
"FROM pg_catalog.pg_type "
"WHERE pg_type.oid = '%u'::pg_catalog.oid;",
pg_type_oid);
upgrade_res = PQexec(g_conn, upgrade_query->data);
check_sql_result(upgrade_res, g_conn, upgrade_query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(upgrade_res);
if (ntups != 1)
{
write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
"query returned %d rows instead of one: %s\n",
ntups),
ntups, upgrade_query->data);
exit_nicely();
}
pg_type_array_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "typarray")));
if (OidIsValid(pg_type_array_oid))
{
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",
pg_type_array_oid);
}
PQclear(upgrade_res);
destroyPQExpBuffer(upgrade_query);
}
static bool
binary_upgrade_set_type_oids_by_rel_oid(PQExpBuffer upgrade_buffer,
Oid pg_rel_oid)
{
PQExpBuffer upgrade_query = createPQExpBuffer();
int ntups;
PGresult *upgrade_res;
Oid pg_type_oid;
bool toast_set = false;
/* we only support old >= 8.3 for binary upgrades */
appendPQExpBuffer(upgrade_query,
"SELECT c.reltype AS crel, t.reltype AS trel "
"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;",
pg_rel_oid);
upgrade_res = PQexec(g_conn, upgrade_query->data);
check_sql_result(upgrade_res, g_conn, upgrade_query->data, PGRES_TUPLES_OK);
/* Expecting a single result only */
ntups = PQntuples(upgrade_res);
if (ntups != 1)
{
write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
"query returned %d rows instead of one: %s\n",
ntups),
ntups, upgrade_query->data);
exit_nicely();
}
pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
binary_upgrade_set_type_oids_by_type_oid(upgrade_buffer, pg_type_oid);
if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel")))
{
/* Toast tables do not have pg_type array rows */
Oid pg_type_toast_oid = atooid(PQgetvalue(upgrade_res, 0,
PQfnumber(upgrade_res, "trel")));
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",
pg_type_toast_oid);
toast_set = true;
}
PQclear(upgrade_res);
destroyPQExpBuffer(upgrade_query);
return toast_set;
}
static void
binary_upgrade_clear_pg_type_toast_oid(PQExpBuffer upgrade_buffer)
{
/*
* One complexity is that while the heap might now have a TOAST table,
* the TOAST table might have been created long after creation when
* the table was loaded with wide data. For that reason, we clear
* binary_upgrade_set_next_pg_type_toast_oid so it is not reused
* by a later table. Logically any later creation that needs a TOAST
* table should have its own TOAST pg_type oid, but we are cautious.
*/
appendPQExpBuffer(upgrade_buffer,
"\n-- For binary upgrade, clear toast oid because it might not have been needed\n");
appendPQExpBuffer(upgrade_buffer,
"SELECT binary_upgrade.set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
InvalidOid);
}
/*
* getNamespaces:
* read all namespaces in the system catalogs and return them in the
@ -6428,6 +6558,10 @@ dumpEnumType(Archive *fout, TypeInfo *tyinfo)
fmtId(tyinfo->dobj.namespace->dobj.name));
appendPQExpBuffer(delq, "%s;\n",
fmtId(tyinfo->dobj.name));
if (binary_upgrade)
binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid);
appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (\n",
fmtId(tyinfo->dobj.name));
for (i = 0; i < num; i++)
@ -6723,6 +6857,10 @@ dumpBaseType(Archive *fout, TypeInfo *tyinfo)
appendPQExpBuffer(delq, "%s CASCADE;\n",
fmtId(tyinfo->dobj.name));
/* We might already have a shell type, but setting pg_type_oid is harmless */
if (binary_upgrade)
binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid);
appendPQExpBuffer(q,
"CREATE TYPE %s (\n"
" INTERNALLENGTH = %s",
@ -6892,6 +7030,9 @@ dumpDomain(Archive *fout, TypeInfo *tyinfo)
else
typdefault = NULL;
if (binary_upgrade)
binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid);
appendPQExpBuffer(q,
"CREATE DOMAIN %s AS %s",
fmtId(tyinfo->dobj.name),
@ -7002,6 +7143,9 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
i_attname = PQfnumber(res, "attname");
i_atttypdefn = PQfnumber(res, "atttypdefn");
if (binary_upgrade)
binary_upgrade_set_type_oids_by_type_oid(q, tyinfo->dobj.catId.oid);
appendPQExpBuffer(q, "CREATE TYPE %s AS (",
fmtId(tyinfo->dobj.name));
@ -7191,6 +7335,10 @@ dumpShellType(Archive *fout, ShellTypeInfo *stinfo)
* after it's filled in, otherwise the backend complains.
*/
if (binary_upgrade)
binary_upgrade_set_type_oids_by_type_oid(q,
stinfo->baseType->dobj.catId.oid);
appendPQExpBuffer(q, "CREATE TYPE %s;\n",
fmtId(stinfo->dobj.name));
@ -10226,10 +10374,15 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
char *storage;
int j,
k;
bool toast_set = false;
/* Make sure we are in proper schema */
selectSourceSchema(tbinfo->dobj.namespace->dobj.name);
if (binary_upgrade)
toast_set = binary_upgrade_set_type_oids_by_rel_oid(q,
tbinfo->dobj.catId.oid);
/* Is it a table or a view? */
if (tbinfo->relkind == RELKIND_VIEW)
{
@ -10606,6 +10759,9 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
}
}
if (binary_upgrade && toast_set)
binary_upgrade_clear_pg_type_toast_oid(q);
ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
tbinfo->dobj.name,
tbinfo->dobj.namespace->dobj.name,
@ -10617,6 +10773,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
tbinfo->dobj.dependencies, tbinfo->dobj.nDeps,
NULL, NULL);
/* Dump Table Comments */
dumpTableComment(fout, tbinfo, reltypename);
@ -11235,6 +11392,10 @@ dumpSequence(Archive *fout, TableInfo *tbinfo)
fmtId(tbinfo->dobj.name));
resetPQExpBuffer(query);
if (binary_upgrade)
binary_upgrade_set_type_oids_by_rel_oid(query, tbinfo->dobj.catId.oid);
appendPQExpBuffer(query,
"CREATE SEQUENCE %s\n",
fmtId(tbinfo->dobj.name));
@ -11270,6 +11431,8 @@ dumpSequence(Archive *fout, TableInfo *tbinfo)
appendPQExpBuffer(query, ";\n");
/* binary_upgrade: no need to clear TOAST table oid */
ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
tbinfo->dobj.name,
tbinfo->dobj.namespace->dobj.name,

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/commands/typecmds.h,v 1.25 2009/01/01 17:23:58 momjian Exp $
* $PostgreSQL: pgsql/src/include/commands/typecmds.h,v 1.26 2009/12/24 22:09:24 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -25,6 +25,7 @@ extern void RemoveTypeById(Oid typeOid);
extern void DefineDomain(CreateDomainStmt *stmt);
extern void DefineEnum(CreateEnumStmt *stmt);
extern Oid DefineCompositeType(const RangeVar *typevar, List *coldeflist);
extern Oid AssignTypeArrayOid(void);
extern void AlterDomainDefault(List *names, Node *defaultRaw);
extern void AlterDomainNotNull(List *names, bool notNull);