Fix pg_upgrade to handle extensions.

This follows my proposal of yesterday, namely that we try to recreate the
previous state of the extension exactly, instead of allowing CREATE
EXTENSION to run a SQL script that might create some entirely-incompatible
on-disk state.  In --binary-upgrade mode, pg_dump won't issue CREATE
EXTENSION at all, but instead uses a kluge function provided by
pg_upgrade_support to recreate the pg_extension row (and extension-level
pg_depend entries) without creating any member objects.  The member objects
are then restored in the same way as if they weren't members, in particular
using pg_upgrade's normal hacks to preserve OIDs that need to be preserved.
Then, for each member object, ALTER EXTENSION ADD is issued to recreate the
pg_depend entry that marks it as an extension member.

In passing, fix breakage in pg_upgrade's enum-type support: somebody didn't
fix it when the noise word VALUE got added to ALTER TYPE ADD.  Also,
rationalize parsetree representation of COMMENT ON DOMAIN and fix
get_object_address() to allow OBJECT_DOMAIN.
This commit is contained in:
Tom Lane 2011-02-09 19:17:33 -05:00
parent 2e2d56fea9
commit caddcb8f4b
10 changed files with 544 additions and 183 deletions

View File

@ -36,52 +36,58 @@ install_support_functions_in_new_db(const char *db_name)
PQclear(executeQueryOrDie(conn,
"CREATE OR REPLACE FUNCTION "
" binary_upgrade.set_next_pg_type_oid(OID) "
"binary_upgrade.set_next_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_array_pg_type_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_toast_pg_type_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_pg_class_oid(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_index_pg_class_oid(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_toast_pg_class_oid(OID) "
"binary_upgrade.set_next_toast_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_pg_enum_oid(OID) "
"binary_upgrade.set_next_pg_enum_oid(OID) "
"RETURNS VOID "
"AS '$libdir/pg_upgrade_support' "
"LANGUAGE C STRICT;"));
PQclear(executeQueryOrDie(conn,
"CREATE OR REPLACE FUNCTION "
" binary_upgrade.set_next_pg_authid_oid(OID) "
"binary_upgrade.set_next_pg_authid_oid(OID) "
"RETURNS VOID "
"AS '$libdir/pg_upgrade_support' "
"LANGUAGE C STRICT;"));
PQclear(executeQueryOrDie(conn,
"CREATE OR REPLACE FUNCTION "
"binary_upgrade.create_empty_extension(text, text, bool, text, oid[], text[], text[]) "
"RETURNS VOID "
"AS '$libdir/pg_upgrade_support' "
"LANGUAGE C;"));
PQfinish(conn);
}
@ -139,8 +145,8 @@ get_loadable_libraries(void)
"SELECT DISTINCT probin "
"FROM pg_catalog.pg_proc "
"WHERE prolang = 13 /* C */ AND "
" probin IS NOT NULL AND "
" oid >= %u;",
"probin IS NOT NULL AND "
"oid >= %u;",
FirstNormalObjectId);
totaltups += PQntuples(ress[dbnum]);

View File

@ -1,8 +1,9 @@
/*
* pg_upgrade_sysoids.c
* pg_upgrade_support.c
*
* server-side functions to set backend global variables
* to control oid and relfilenode assignment
* to control oid and relfilenode assignment, and do other special
* hacks needed for pg_upgrade.
*
* Copyright (c) 2010-2011, PostgreSQL Global Development Group
* contrib/pg_upgrade_support/pg_upgrade_support.c
@ -12,7 +13,13 @@
#include "fmgr.h"
#include "catalog/dependency.h"
#include "catalog/namespace.h"
#include "catalog/pg_class.h"
#include "catalog/pg_type.h"
#include "commands/extension.h"
#include "miscadmin.h"
#include "utils/array.h"
#include "utils/builtins.h"
/* THIS IS USED ONLY FOR PG >= 9.0 */
@ -42,6 +49,8 @@ Datum set_next_toast_pg_class_oid(PG_FUNCTION_ARGS);
Datum set_next_pg_enum_oid(PG_FUNCTION_ARGS);
Datum set_next_pg_authid_oid(PG_FUNCTION_ARGS);
Datum create_empty_extension(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(set_next_pg_type_oid);
PG_FUNCTION_INFO_V1(set_next_array_pg_type_oid);
PG_FUNCTION_INFO_V1(set_next_toast_pg_type_oid);
@ -53,6 +62,8 @@ PG_FUNCTION_INFO_V1(set_next_toast_pg_class_oid);
PG_FUNCTION_INFO_V1(set_next_pg_enum_oid);
PG_FUNCTION_INFO_V1(set_next_pg_authid_oid);
PG_FUNCTION_INFO_V1(create_empty_extension);
Datum
set_next_pg_type_oid(PG_FUNCTION_ARGS)
@ -133,3 +144,61 @@ set_next_pg_authid_oid(PG_FUNCTION_ARGS)
PG_RETURN_VOID();
}
Datum
create_empty_extension(PG_FUNCTION_ARGS)
{
text *extName = PG_GETARG_TEXT_PP(0);
text *schemaName = PG_GETARG_TEXT_PP(1);
bool relocatable = PG_GETARG_BOOL(2);
char *extVersion;
Datum extConfig;
Datum extCondition;
List *requiredExtensions;
if (PG_ARGISNULL(3))
extVersion = NULL;
else
extVersion = text_to_cstring(PG_GETARG_TEXT_PP(3));
if (PG_ARGISNULL(4))
extConfig = PointerGetDatum(NULL);
else
extConfig = PG_GETARG_DATUM(4);
if (PG_ARGISNULL(5))
extCondition = PointerGetDatum(NULL);
else
extCondition = PG_GETARG_DATUM(5);
requiredExtensions = NIL;
if (!PG_ARGISNULL(6))
{
ArrayType *textArray = PG_GETARG_ARRAYTYPE_P(6);
Datum *textDatums;
int ndatums;
int i;
deconstruct_array(textArray,
TEXTOID, -1, false, 'i',
&textDatums, NULL, &ndatums);
for (i = 0; i < ndatums; i++)
{
text *txtname = DatumGetTextPP(textDatums[i]);
char *extName = text_to_cstring(txtname);
Oid extOid = get_extension_oid(extName, false);
requiredExtensions = lappend_oid(requiredExtensions, extOid);
}
}
InsertExtensionTuple(text_to_cstring(extName),
GetUserId(),
get_namespace_oid(text_to_cstring(schemaName), false),
relocatable,
extVersion,
extConfig,
extCondition,
requiredExtensions);
PG_RETURN_VOID();
}

View File

@ -139,6 +139,7 @@ get_object_address(ObjectType objtype, List *objname, List *objargs,
address = get_object_address_unqualified(objtype, objname);
break;
case OBJECT_TYPE:
case OBJECT_DOMAIN:
address.classId = TypeRelationId;
address.objectId =
typenameTypeId(NULL, makeTypeNameFromNameList(objname));

View File

@ -105,6 +105,7 @@ CommentObject(CommentStmt *stmt)
strVal(linitial(stmt->objname)));
break;
case OBJECT_TYPE:
case OBJECT_DOMAIN:
if (!pg_type_ownercheck(address.objectId, GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TYPE,
format_type_be(address.objectId));

View File

@ -648,13 +648,7 @@ CreateExtension(CreateExtensionStmt *stmt)
ExtensionControlFile *control;
List *requiredExtensions;
List *requiredSchemas;
Relation rel;
Datum values[Natts_pg_extension];
bool nulls[Natts_pg_extension];
HeapTuple tuple;
Oid extensionOid;
ObjectAddress myself;
ObjectAddress nsp;
ListCell *lc;
/* Must be super user */
@ -801,7 +795,58 @@ CreateExtension(CreateExtensionStmt *stmt)
}
/*
* Insert new tuple into pg_extension.
* Insert new tuple into pg_extension, and create dependency entries.
*/
extensionOid = InsertExtensionTuple(control->name, extowner,
schemaOid, control->relocatable,
control->version,
PointerGetDatum(NULL),
PointerGetDatum(NULL),
requiredExtensions);
/*
* Apply any comment on extension
*/
if (control->comment != NULL)
CreateComments(extensionOid, ExtensionRelationId, 0, control->comment);
/*
* Finally, execute the extension script to create the member objects
*/
execute_extension_script(extensionOid, control, requiredSchemas,
schemaName, schemaOid);
}
/*
* InsertExtensionTuple
*
* Insert the new pg_extension row, and create extension's dependency entries.
* Return the OID assigned to the new row.
*
* This is exported for the benefit of pg_upgrade, which has to create a
* pg_extension entry (and the extension-level dependencies) without
* actually running the extension's script.
*
* extConfig and extCondition should be arrays or PointerGetDatum(NULL).
* We declare them as plain Datum to avoid needing array.h in extension.h.
*/
Oid
InsertExtensionTuple(const char *extName, Oid extOwner,
Oid schemaOid, bool relocatable, const char *extVersion,
Datum extConfig, Datum extCondition,
List *requiredExtensions)
{
Oid extensionOid;
Relation rel;
Datum values[Natts_pg_extension];
bool nulls[Natts_pg_extension];
HeapTuple tuple;
ObjectAddress myself;
ObjectAddress nsp;
ListCell *lc;
/*
* Build and insert the pg_extension tuple
*/
rel = heap_open(ExtensionRelationId, RowExclusiveLock);
@ -809,19 +854,26 @@ CreateExtension(CreateExtensionStmt *stmt)
memset(nulls, 0, sizeof(nulls));
values[Anum_pg_extension_extname - 1] =
DirectFunctionCall1(namein, CStringGetDatum(control->name));
values[Anum_pg_extension_extowner - 1] = ObjectIdGetDatum(extowner);
DirectFunctionCall1(namein, CStringGetDatum(extName));
values[Anum_pg_extension_extowner - 1] = ObjectIdGetDatum(extOwner);
values[Anum_pg_extension_extnamespace - 1] = ObjectIdGetDatum(schemaOid);
values[Anum_pg_extension_extrelocatable - 1] = BoolGetDatum(control->relocatable);
values[Anum_pg_extension_extrelocatable - 1] = BoolGetDatum(relocatable);
if (control->version == NULL)
if (extVersion == NULL)
nulls[Anum_pg_extension_extversion - 1] = true;
else
values[Anum_pg_extension_extversion - 1] =
CStringGetTextDatum(control->version);
CStringGetTextDatum(extVersion);
nulls[Anum_pg_extension_extconfig - 1] = true;
nulls[Anum_pg_extension_extcondition - 1] = true;
if (extConfig == PointerGetDatum(NULL))
nulls[Anum_pg_extension_extconfig - 1] = true;
else
values[Anum_pg_extension_extconfig - 1] = extConfig;
if (extCondition == PointerGetDatum(NULL))
nulls[Anum_pg_extension_extcondition - 1] = true;
else
values[Anum_pg_extension_extcondition - 1] = extCondition;
tuple = heap_form_tuple(rel->rd_att, values, nulls);
@ -831,16 +883,10 @@ CreateExtension(CreateExtensionStmt *stmt)
heap_freetuple(tuple);
heap_close(rel, RowExclusiveLock);
/*
* Apply any comment on extension
*/
if (control->comment != NULL)
CreateComments(extensionOid, ExtensionRelationId, 0, control->comment);
/*
* Record dependencies on owner, schema, and prerequisite extensions
*/
recordDependencyOnOwner(ExtensionRelationId, extensionOid, extowner);
recordDependencyOnOwner(ExtensionRelationId, extensionOid, extOwner);
myself.classId = ExtensionRelationId;
myself.objectId = extensionOid;
@ -864,11 +910,7 @@ CreateExtension(CreateExtensionStmt *stmt)
recordDependencyOn(&myself, &otherext, DEPENDENCY_NORMAL);
}
/*
* Finally, execute the extension script to create the member objects
*/
execute_extension_script(extensionOid, control, requiredSchemas,
schemaName, schemaOid);
return extensionOid;
}

View File

@ -4790,7 +4790,7 @@ comment_type:
| INDEX { $$ = OBJECT_INDEX; }
| SEQUENCE { $$ = OBJECT_SEQUENCE; }
| TABLE { $$ = OBJECT_TABLE; }
| DOMAIN_P { $$ = OBJECT_TYPE; }
| DOMAIN_P { $$ = OBJECT_DOMAIN; }
| TYPE_P { $$ = OBJECT_TYPE; }
| VIEW { $$ = OBJECT_VIEW; }
| CONVERSION_P { $$ = OBJECT_CONVERSION; }

View File

@ -463,6 +463,7 @@ AssignDumpId(DumpableObject *dobj)
dobj->name = NULL; /* must be set later */
dobj->namespace = NULL; /* may be set later */
dobj->dump = true; /* default assumption */
dobj->ext_member = false; /* default assumption */
dobj->dependencies = NULL;
dobj->nDeps = 0;
dobj->allocDeps = 0;

File diff suppressed because it is too large Load Diff

View File

@ -128,6 +128,7 @@ typedef struct _dumpableObject
char *name; /* object name (should never be NULL) */
struct _namespaceInfo *namespace; /* containing namespace, or NULL */
bool dump; /* true if we want to dump this object */
bool ext_member; /* true if object is member of extension */
DumpId *dependencies; /* dumpIds of objects this one depends on */
int nDeps; /* number of valid dependencies */
int allocDeps; /* allocated size of dependencies[] */
@ -144,6 +145,8 @@ typedef struct _extensionInfo
{
DumpableObject dobj;
char *namespace; /* schema containing extension's objects */
bool relocatable;
char *extversion;
char *extconfig; /* info about configuration tables */
char *extcondition;
} ExtensionInfo;

View File

@ -32,6 +32,11 @@ extern void CreateExtension(CreateExtensionStmt *stmt);
extern void RemoveExtensions(DropStmt *stmt);
extern void RemoveExtensionById(Oid extId);
extern Oid InsertExtensionTuple(const char *extName, Oid extOwner,
Oid schemaOid, bool relocatable, const char *extVersion,
Datum extConfig, Datum extCondition,
List *requiredExtensions);
extern void ExecAlterExtensionAddStmt(AlterExtensionAddStmt *stmt);
extern Oid get_extension_oid(const char *extname, bool missing_ok);