Skip to content

Commit 951c2f6

Browse files
committed
Fix dependency searching for case where column is visited before table.
When the recursive search in dependency.c visits a column and then later visits the whole table containing the column, it needs to propagate the drop-context flags for the table to the existing target-object entry for the column. Otherwise we might refuse the DROP (if not CASCADE) on the incorrect grounds that there was no automatic drop pathway to the column. Remarkably, this has not been reported before, though it's possible at least when an extension creates both a datatype and a table using that datatype. Rather than just marking the column as allowed to be dropped, it might seem good to skip the DROP COLUMN step altogether, since the later DROP of the table will surely get the job done. The problem with that is that the datatype would then be dropped before the table (since the whole situation occurred because we visited the datatype, and then recursed to the dependent column, before visiting the table). That seems pretty risky, and the case is rare enough that it doesn't seem worth expending a lot of effort or risk to make the drops happen in a safe order. So we just play dumb and delete the column separately according to the existing drop ordering rules. Per report from Petr Jelinek, though this is different from his proposed patch. Back-patch to 9.1, where extensions were introduced. There's currently no evidence that such cases can arise before 9.1, and in any case we would also need to back-patch cb5c2ba to 9.0 if we wanted to back-patch this.
1 parent f449873 commit 951c2f6

File tree

1 file changed

+56
-18
lines changed

1 file changed

+56
-18
lines changed

src/backend/catalog/dependency.c

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel,
244244
* not the direct result of a user-initiated action. For example, when a
245245
* temporary schema is cleaned out so that a new backend can use it, or when
246246
* a column default is dropped as an intermediate step while adding a new one,
247-
* that's an internal operation. On the other hand, when the we drop something
247+
* that's an internal operation. On the other hand, when we drop something
248248
* because the user issued a DROP statement against it, that's not internal.
249249
*/
250250
void
@@ -2110,6 +2110,7 @@ object_address_present_add_flags(const ObjectAddress *object,
21102110
int flags,
21112111
ObjectAddresses *addrs)
21122112
{
2113+
bool result = false;
21132114
int i;
21142115

21152116
for (i = addrs->numrefs - 1; i >= 0; i--)
@@ -2124,22 +2125,48 @@ object_address_present_add_flags(const ObjectAddress *object,
21242125
ObjectAddressExtra *thisextra = addrs->extras + i;
21252126

21262127
thisextra->flags |= flags;
2127-
return true;
2128+
result = true;
21282129
}
2129-
if (thisobj->objectSubId == 0)
2130+
else if (thisobj->objectSubId == 0)
21302131
{
21312132
/*
21322133
* We get here if we find a need to delete a column after
21332134
* having already decided to drop its whole table. Obviously
2134-
* we no longer need to drop the column. But don't plaster
2135-
* its flags on the table.
2135+
* we no longer need to drop the subobject, so report that we
2136+
* found the subobject in the array. But don't plaster its
2137+
* flags on the whole object.
21362138
*/
2137-
return true;
2139+
result = true;
2140+
}
2141+
else if (object->objectSubId == 0)
2142+
{
2143+
/*
2144+
* We get here if we find a need to delete a whole table after
2145+
* having already decided to drop one of its columns. We
2146+
* can't report that the whole object is in the array, but we
2147+
* should mark the subobject with the whole object's flags.
2148+
*
2149+
* It might seem attractive to physically delete the column's
2150+
* array entry, or at least mark it as no longer needing
2151+
* separate deletion. But that could lead to, e.g., dropping
2152+
* the column's datatype before we drop the table, which does
2153+
* not seem like a good idea. This is a very rare situation
2154+
* in practice, so we just take the hit of doing a separate
2155+
* DROP COLUMN action even though we know we're gonna delete
2156+
* the table later.
2157+
*
2158+
* Because there could be other subobjects of this object in
2159+
* the array, this case means we always have to loop through
2160+
* the whole array; we cannot exit early on a match.
2161+
*/
2162+
ObjectAddressExtra *thisextra = addrs->extras + i;
2163+
2164+
thisextra->flags |= flags;
21382165
}
21392166
}
21402167
}
21412168

2142-
return false;
2169+
return result;
21432170
}
21442171

21452172
/*
@@ -2150,6 +2177,7 @@ stack_address_present_add_flags(const ObjectAddress *object,
21502177
int flags,
21512178
ObjectAddressStack *stack)
21522179
{
2180+
bool result = false;
21532181
ObjectAddressStack *stackptr;
21542182

21552183
for (stackptr = stack; stackptr; stackptr = stackptr->next)
@@ -2162,21 +2190,31 @@ stack_address_present_add_flags(const ObjectAddress *object,
21622190
if (object->objectSubId == thisobj->objectSubId)
21632191
{
21642192
stackptr->flags |= flags;
2165-
return true;
2193+
result = true;
2194+
}
2195+
else if (thisobj->objectSubId == 0)
2196+
{
2197+
/*
2198+
* We're visiting a column with whole table already on stack.
2199+
* As in object_address_present_add_flags(), we can skip
2200+
* further processing of the subobject, but we don't want to
2201+
* propagate flags for the subobject to the whole object.
2202+
*/
2203+
result = true;
2204+
}
2205+
else if (object->objectSubId == 0)
2206+
{
2207+
/*
2208+
* We're visiting a table with column already on stack. As in
2209+
* object_address_present_add_flags(), we should propagate
2210+
* flags for the whole object to each of its subobjects.
2211+
*/
2212+
stackptr->flags |= flags;
21662213
}
2167-
2168-
/*
2169-
* Could visit column with whole table already on stack; this is
2170-
* the same case noted in object_address_present_add_flags(), and
2171-
* as in that case, we don't propagate flags for the component to
2172-
* the whole object.
2173-
*/
2174-
if (thisobj->objectSubId == 0)
2175-
return true;
21762214
}
21772215
}
21782216

2179-
return false;
2217+
return result;
21802218
}
21812219

21822220
/*

0 commit comments

Comments
 (0)