1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-27 00:12:01 +03:00

Repair ALTER EXTENSION ... SET SCHEMA.

It turns out that we broke this in commit e5bc9454e, because
the code was assuming that no dependent types would appear
among the extension's direct dependencies, and now they do.

This isn't terribly hard to fix: just skip dependent types,
expecting that we will recurse to them when we process the parent
object (which should also be among the direct dependencies).
But a little bit of refactoring is needed so that we can avoid
duplicating logic about what is a dependent type.

Although there is some testing of ALTER EXTENSION SET SCHEMA,
it failed to cover interesting cases, so add more tests.

Discussion: https://postgr.es/m/930191.1715205151@sss.pgh.pa.us
This commit is contained in:
Tom Lane
2024-05-09 12:19:43 -04:00
parent d82ab9fc31
commit 9effc4608e
11 changed files with 141 additions and 27 deletions

View File

@@ -598,16 +598,16 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt,
/*
* Change an object's namespace given its classOid and object Oid.
*
* Objects that don't have a namespace should be ignored.
* Objects that don't have a namespace should be ignored, as should
* dependent types such as array types.
*
* This function is currently used only by ALTER EXTENSION SET SCHEMA,
* so it only needs to cover object types that can be members of an
* extension, and it doesn't have to deal with certain special cases
* such as not wanting to process array types --- those should never
* be direct members of an extension anyway.
* so it only needs to cover object kinds that can be members of an
* extension, and it can silently ignore dependent types --- we assume
* those will be moved when their parent object is moved.
*
* Returns the OID of the object's previous namespace, or InvalidOid if
* object doesn't have a schema.
* object doesn't have a schema or was ignored due to being a dependent type.
*/
Oid
AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid,
@@ -631,7 +631,7 @@ AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid,
}
case TypeRelationId:
oldNspOid = AlterTypeNamespace_oid(objid, nspOid, objsMoved);
oldNspOid = AlterTypeNamespace_oid(objid, nspOid, true, objsMoved);
break;
case ProcedureRelationId:

View File

@@ -2940,7 +2940,7 @@ AlterExtensionNamespace(const char *extensionName, const char *newschema, Oid *o
/*
* If not all the objects had the same old namespace (ignoring any
* that are not in namespaces), complain.
* that are not in namespaces or are dependent types), complain.
*/
if (dep_oldNspOid != InvalidOid && dep_oldNspOid != oldNspOid)
ereport(ERROR,

View File

@@ -18017,8 +18017,11 @@ AlterTableNamespaceInternal(Relation rel, Oid oldNspOid, Oid nspOid,
/* Fix the table's row type too, if it has one */
if (OidIsValid(rel->rd_rel->reltype))
AlterTypeNamespaceInternal(rel->rd_rel->reltype,
nspOid, false, false, objsMoved);
AlterTypeNamespaceInternal(rel->rd_rel->reltype, nspOid,
false, /* isImplicitArray */
false, /* ignoreDependent */
false, /* errorOnTableType */
objsMoved);
/* Fix other dependent stuff */
AlterIndexNamespaces(classRel, rel, oldNspOid, nspOid, objsMoved);

View File

@@ -4068,7 +4068,7 @@ AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype,
typename = makeTypeNameFromNameList(names);
typeOid = typenameTypeId(NULL, typename);
/* Don't allow ALTER DOMAIN on a type */
/* Don't allow ALTER DOMAIN on a non-domain type */
if (objecttype == OBJECT_DOMAIN && get_typtype(typeOid) != TYPTYPE_DOMAIN)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
@@ -4079,7 +4079,7 @@ AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype,
nspOid = LookupCreationNamespace(newschema);
objsMoved = new_object_addresses();
oldNspOid = AlterTypeNamespace_oid(typeOid, nspOid, objsMoved);
oldNspOid = AlterTypeNamespace_oid(typeOid, nspOid, false, objsMoved);
free_object_addresses(objsMoved);
if (oldschema)
@@ -4090,8 +4090,21 @@ AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype,
return myself;
}
/*
* ALTER TYPE SET SCHEMA, where the caller has already looked up the OIDs
* of the type and the target schema and checked the schema's privileges.
*
* If ignoreDependent is true, we silently ignore dependent types
* (array types and table rowtypes) rather than raising errors.
*
* This entry point is exported for use by AlterObjectNamespace_oid,
* which doesn't want errors when it passes OIDs of dependent types.
*
* Returns the type's old namespace OID, or InvalidOid if we did nothing.
*/
Oid
AlterTypeNamespace_oid(Oid typeOid, Oid nspOid, ObjectAddresses *objsMoved)
AlterTypeNamespace_oid(Oid typeOid, Oid nspOid, bool ignoreDependent,
ObjectAddresses *objsMoved)
{
Oid elemOid;
@@ -4102,15 +4115,23 @@ AlterTypeNamespace_oid(Oid typeOid, Oid nspOid, ObjectAddresses *objsMoved)
/* don't allow direct alteration of array types */
elemOid = get_element_type(typeOid);
if (OidIsValid(elemOid) && get_array_type(elemOid) == typeOid)
{
if (ignoreDependent)
return InvalidOid;
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("cannot alter array type %s",
format_type_be(typeOid)),
errhint("You can alter type %s, which will alter the array type as well.",
format_type_be(elemOid))));
}
/* and do the work */
return AlterTypeNamespaceInternal(typeOid, nspOid, false, true, objsMoved);
return AlterTypeNamespaceInternal(typeOid, nspOid,
false, /* isImplicitArray */
ignoreDependent, /* ignoreDependent */
true, /* errorOnTableType */
objsMoved);
}
/*
@@ -4122,15 +4143,21 @@ AlterTypeNamespace_oid(Oid typeOid, Oid nspOid, ObjectAddresses *objsMoved)
* if any. isImplicitArray should be true only when doing this internal
* recursion (outside callers must never try to move an array type directly).
*
* If ignoreDependent is true, we silently don't process table types.
*
* If errorOnTableType is true, the function errors out if the type is
* a table type. ALTER TABLE has to be used to move a table to a new
* namespace.
* namespace. (This flag is ignored if ignoreDependent is true.)
*
* Returns the type's old namespace OID.
* We also do nothing if the type is already listed in *objsMoved.
* After a successful move, we add the type to *objsMoved.
*
* Returns the type's old namespace OID, or InvalidOid if we did nothing.
*/
Oid
AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
bool isImplicitArray,
bool ignoreDependent,
bool errorOnTableType,
ObjectAddresses *objsMoved)
{
@@ -4185,15 +4212,21 @@ AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
get_rel_relkind(typform->typrelid) == RELKIND_COMPOSITE_TYPE);
/* Enforce not-table-type if requested */
if (typform->typtype == TYPTYPE_COMPOSITE && !isCompositeType &&
errorOnTableType)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("%s is a table's row type",
format_type_be(typeOid)),
/* translator: %s is an SQL ALTER command */
errhint("Use %s instead.",
"ALTER TABLE")));
if (typform->typtype == TYPTYPE_COMPOSITE && !isCompositeType)
{
if (ignoreDependent)
{
table_close(rel, RowExclusiveLock);
return InvalidOid;
}
if (errorOnTableType)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("%s is a table's row type",
format_type_be(typeOid)),
/* translator: %s is an SQL ALTER command */
errhint("Use %s instead.", "ALTER TABLE")));
}
if (oldNspOid != nspOid)
{
@@ -4260,7 +4293,11 @@ AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
/* Recursively alter the associated array type, if any */
if (OidIsValid(arrayOid))
AlterTypeNamespaceInternal(arrayOid, nspOid, true, true, objsMoved);
AlterTypeNamespaceInternal(arrayOid, nspOid,
true, /* isImplicitArray */
false, /* ignoreDependent */
true, /* errorOnTableType */
objsMoved);
return oldNspOid;
}