Commit cb5c2ba2 authored by Tom Lane's avatar Tom Lane

Fix multiple bugs in extension dropping.

When we implemented extensions, we made findDependentObjects() treat
EXTENSION dependency links similarly to INTERNAL links.  However, that
logic contained an implicit assumption that an object could have at most
one INTERNAL dependency, so it did not work correctly for objects having
both INTERNAL and DEPENDENCY links.  This led to failure to drop some
extension member objects when dropping the extension.  Furthermore, we'd
never actually exercised the case of recursing to an internally-referenced
(owning) object from anything other than a NORMAL dependency, and it turns
out that passing the incoming dependency's flags to the owning object is
the Wrong Thing.  This led to sometimes dropping a whole extension silently
when we should have rejected the drop command for lack of CASCADE.

Since we obviously were under-testing extension drop scenarios, add some
regression test cases.  Unfortunately, such test cases require some
extensions (duh), so we can't test for problems in the core regression
tests.  I chose to add them to the earthdistance contrib module, which is
a good test case because it has a dependency on the cube contrib module.

Back-patch to 9.1.  Arguably these are pre-existing bugs in INTERNAL
dependency handling, but since it appears that the cases can never arise
pre-9.1, I'll refrain from back-patching the logic changes further than
that.
parent d4aa4914
-- --
-- Test earth distance functions -- Test earthdistance extension
--
-- In this file we also do some testing of extension create/drop scenarios.
-- That's really exercising the core database's dependency logic, so ideally
-- we'd do it in the core regression tests, but we can't for lack of suitable
-- guaranteed-available extensions. earthdistance is a good test case because
-- it has a dependency on the cube extension.
-- --
CREATE EXTENSION earthdistance; -- fail, must install cube first
CREATE EXTENSION cube; CREATE EXTENSION cube;
CREATE EXTENSION earthdistance; CREATE EXTENSION earthdistance;
...@@ -291,3 +298,68 @@ SELECT is_point(ll_to_earth(-30,-90)); ...@@ -291,3 +298,68 @@ SELECT is_point(ll_to_earth(-30,-90));
SELECT cube_dim(ll_to_earth(-30,-90)) <= 3; SELECT cube_dim(ll_to_earth(-30,-90)) <= 3;
SELECT abs(cube_distance(ll_to_earth(-30,-90), '(0)'::cube) / earth() - 1) < SELECT abs(cube_distance(ll_to_earth(-30,-90), '(0)'::cube) / earth() - 1) <
'10e-12'::float8; '10e-12'::float8;
--
-- Now we are going to test extension create/drop scenarios.
--
-- list what's installed
\dT
\df
\do
drop extension cube; -- fail, earthdistance requires it
drop extension earthdistance;
drop type cube; -- fail, extension cube requires it
-- list what's installed
\dT
\df
\do
create table foo (f1 cube, f2 int);
drop extension cube; -- fail, foo.f1 requires it
drop table foo;
drop extension cube;
-- list what's installed
\dT
\df
\do
create schema c;
create extension cube with schema c;
-- list what's installed
\dT public.*
\df public.*
\do public.*
\dT c.*
\df c.*
\do c.*
create table foo (f1 c.cube, f2 int);
drop extension cube; -- fail, foo.f1 requires it
drop schema c; -- fail, cube requires it
drop extension cube cascade;
\d foo
-- list what's installed
\dT public.*
\df public.*
\do public.*
\dT c.*
\df c.*
\do c.*
drop schema c;
...@@ -98,6 +98,7 @@ typedef struct ...@@ -98,6 +98,7 @@ typedef struct
#define DEPFLAG_AUTO 0x0004 /* reached via auto dependency */ #define DEPFLAG_AUTO 0x0004 /* reached via auto dependency */
#define DEPFLAG_INTERNAL 0x0008 /* reached via internal dependency */ #define DEPFLAG_INTERNAL 0x0008 /* reached via internal dependency */
#define DEPFLAG_EXTENSION 0x0010 /* reached via extension dependency */ #define DEPFLAG_EXTENSION 0x0010 /* reached via extension dependency */
#define DEPFLAG_REVERSE 0x0020 /* reverse internal/extension link */
/* expansible list of ObjectAddresses */ /* expansible list of ObjectAddresses */
...@@ -190,6 +191,9 @@ static void add_exact_object_address_extra(const ObjectAddress *object, ...@@ -190,6 +191,9 @@ static void add_exact_object_address_extra(const ObjectAddress *object,
static bool object_address_present_add_flags(const ObjectAddress *object, static bool object_address_present_add_flags(const ObjectAddress *object,
int flags, int flags,
ObjectAddresses *addrs); ObjectAddresses *addrs);
static bool stack_address_present_add_flags(const ObjectAddress *object,
int flags,
ObjectAddressStack *stack);
static void getRelationDescription(StringInfo buffer, Oid relid); static void getRelationDescription(StringInfo buffer, Oid relid);
static void getOpFamilyDescription(StringInfo buffer, Oid opfid); static void getOpFamilyDescription(StringInfo buffer, Oid opfid);
...@@ -459,7 +463,6 @@ findDependentObjects(const ObjectAddress *object, ...@@ -459,7 +463,6 @@ findDependentObjects(const ObjectAddress *object,
ObjectAddress otherObject; ObjectAddress otherObject;
ObjectAddressStack mystack; ObjectAddressStack mystack;
ObjectAddressExtra extra; ObjectAddressExtra extra;
ObjectAddressStack *stackptr;
/* /*
* If the target object is already being visited in an outer recursion * If the target object is already being visited in an outer recursion
...@@ -477,27 +480,8 @@ findDependentObjects(const ObjectAddress *object, ...@@ -477,27 +480,8 @@ findDependentObjects(const ObjectAddress *object,
* auto dependency, too, if we had to. However there are no known cases * auto dependency, too, if we had to. However there are no known cases
* where that would be necessary. * where that would be necessary.
*/ */
for (stackptr = stack; stackptr; stackptr = stackptr->next) if (stack_address_present_add_flags(object, flags, stack))
{ return;
if (object->classId == stackptr->object->classId &&
object->objectId == stackptr->object->objectId)
{
if (object->objectSubId == stackptr->object->objectSubId)
{
stackptr->flags |= flags;
return;
}
/*
* Could visit column with whole table already on stack; this is
* the same case noted in object_address_present_add_flags().
* (It's not clear this can really happen, but we might as well
* check.)
*/
if (stackptr->object->objectSubId == 0)
return;
}
}
/* /*
* It's also possible that the target object has already been completely * It's also possible that the target object has already been completely
...@@ -513,12 +497,13 @@ findDependentObjects(const ObjectAddress *object, ...@@ -513,12 +497,13 @@ findDependentObjects(const ObjectAddress *object,
/* /*
* The target object might be internally dependent on some other object * The target object might be internally dependent on some other object
* (its "owner"). If so, and if we aren't recursing from the owning * (its "owner"), and/or be a member of an extension (also considered its
* object, we have to transform this deletion request into a deletion * owner). If so, and if we aren't recursing from the owning object, we
* request of the owning object. (We'll eventually recurse back to this * have to transform this deletion request into a deletion request of the
* object, but the owning object has to be visited first so it will be * owning object. (We'll eventually recurse back to this object, but the
* deleted after.) The way to find out about this is to scan the * owning object has to be visited first so it will be deleted after.)
* pg_depend entries that show what this object depends on. * The way to find out about this is to scan the pg_depend entries that
* show what this object depends on.
*/ */
ScanKeyInit(&key[0], ScanKeyInit(&key[0],
Anum_pg_depend_classid, Anum_pg_depend_classid,
...@@ -567,7 +552,7 @@ findDependentObjects(const ObjectAddress *object, ...@@ -567,7 +552,7 @@ findDependentObjects(const ObjectAddress *object,
* 1. At the outermost recursion level, disallow the DROP. (We * 1. At the outermost recursion level, disallow the DROP. (We
* just ereport here, rather than proceeding, since no other * just ereport here, rather than proceeding, since no other
* dependencies are likely to be interesting.) However, if * dependencies are likely to be interesting.) However, if
* the other object is listed in pendingObjects, just release * the owning object is listed in pendingObjects, just release
* the caller's lock and return; we'll eventually complete the * the caller's lock and return; we'll eventually complete the
* DROP when we reach that entry in the pending list. * DROP when we reach that entry in the pending list.
*/ */
...@@ -595,31 +580,31 @@ findDependentObjects(const ObjectAddress *object, ...@@ -595,31 +580,31 @@ findDependentObjects(const ObjectAddress *object,
/* /*
* 2. When recursing from the other end of this dependency, * 2. When recursing from the other end of this dependency,
* it's okay to continue with the deletion. This holds when * it's okay to continue with the deletion. This holds when
* recursing from a whole object that includes the nominal * recursing from a whole object that includes the nominal
* other end as a component, too. * other end as a component, too. Since there can be more
* than one "owning" object, we have to allow matches that
* are more than one level down in the stack.
*/ */
if (stack->object->classId == otherObject.classId && if (stack_address_present_add_flags(&otherObject, 0, stack))
stack->object->objectId == otherObject.objectId &&
(stack->object->objectSubId == otherObject.objectSubId ||
stack->object->objectSubId == 0))
break; break;
/* /*
* 3. When recursing from anyplace else, transform this * 3. Not all the owning objects have been visited, so
* deletion request into a delete of the other object. * transform this deletion request into a delete of this
* owning object.
* *
* First, release caller's lock on this object and get * First, release caller's lock on this object and get
* deletion lock on the other object. (We must release * deletion lock on the owning object. (We must release
* caller's lock to avoid deadlock against a concurrent * caller's lock to avoid deadlock against a concurrent
* deletion of the other object.) * deletion of the owning object.)
*/ */
ReleaseDeletionLock(object); ReleaseDeletionLock(object);
AcquireDeletionLock(&otherObject); AcquireDeletionLock(&otherObject);
/* /*
* The other object might have been deleted while we waited to * The owning object might have been deleted while we waited
* lock it; if so, neither it nor the current object are * to lock it; if so, neither it nor the current object are
* interesting anymore. We test this by checking the * interesting anymore. We test this by checking the
* pg_depend entry (see notes below). * pg_depend entry (see notes below).
*/ */
...@@ -631,13 +616,18 @@ findDependentObjects(const ObjectAddress *object, ...@@ -631,13 +616,18 @@ findDependentObjects(const ObjectAddress *object,
} }
/* /*
* Okay, recurse to the other object instead of proceeding. We * Okay, recurse to the owning object instead of proceeding.
* treat this exactly as if the original reference had linked *
* to that object instead of this one; hence, pass through the * We do not need to stack the current object; we want the
* same flags and stack. * traversal order to be as if the original reference had
* linked to the owning object instead of this one.
*
* The dependency type is a "reverse" dependency: we need to
* delete the owning object if this one is to be deleted, but
* this linkage is never a reason for an automatic deletion.
*/ */
findDependentObjects(&otherObject, findDependentObjects(&otherObject,
flags, DEPFLAG_REVERSE,
stack, stack,
targetObjects, targetObjects,
pendingObjects, pendingObjects,
...@@ -2016,6 +2006,43 @@ object_address_present_add_flags(const ObjectAddress *object, ...@@ -2016,6 +2006,43 @@ object_address_present_add_flags(const ObjectAddress *object,
return false; return false;
} }
/*
* Similar to above, except we search an ObjectAddressStack.
*/
static bool
stack_address_present_add_flags(const ObjectAddress *object,
int flags,
ObjectAddressStack *stack)
{
ObjectAddressStack *stackptr;
for (stackptr = stack; stackptr; stackptr = stackptr->next)
{
const ObjectAddress *thisobj = stackptr->object;
if (object->classId == thisobj->classId &&
object->objectId == thisobj->objectId)
{
if (object->objectSubId == thisobj->objectSubId)
{
stackptr->flags |= flags;
return true;
}
/*
* Could visit column with whole table already on stack; this is
* the same case noted in object_address_present_add_flags(), and
* as in that case, we don't propagate flags for the component to
* the whole object.
*/
if (thisobj->objectSubId == 0)
return true;
}
}
return false;
}
/* /*
* Record multiple dependencies from an ObjectAddresses array, after first * Record multiple dependencies from an ObjectAddresses array, after first
* removing any duplicates. * removing any duplicates.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment