Skip to content

Commit 1c2f9a4

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 19ccaf9 commit 1c2f9a4

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
@@ -212,7 +212,7 @@ static void getOpFamilyDescription(StringInfo buffer, Oid opfid);
212212
* not the direct result of a user-initiated action. For example, when a
213213
* temporary schema is cleaned out so that a new backend can use it, or when
214214
* a column default is dropped as an intermediate step while adding a new one,
215-
* that's an internal operation. On the other hand, when the we drop something
215+
* that's an internal operation. On the other hand, when we drop something
216216
* because the user issued a DROP statement against it, that's not internal.
217217
*/
218218
void
@@ -2083,6 +2083,7 @@ object_address_present_add_flags(const ObjectAddress *object,
20832083
int flags,
20842084
ObjectAddresses *addrs)
20852085
{
2086+
bool result = false;
20862087
int i;
20872088

20882089
for (i = addrs->numrefs - 1; i >= 0; i--)
@@ -2097,22 +2098,48 @@ object_address_present_add_flags(const ObjectAddress *object,
20972098
ObjectAddressExtra *thisextra = addrs->extras + i;
20982099

20992100
thisextra->flags |= flags;
2100-
return true;
2101+
result = true;
21012102
}
2102-
if (thisobj->objectSubId == 0)
2103+
else if (thisobj->objectSubId == 0)
21032104
{
21042105
/*
21052106
* We get here if we find a need to delete a column after
21062107
* having already decided to drop its whole table. Obviously
2107-
* we no longer need to drop the column. But don't plaster
2108-
* its flags on the table.
2108+
* we no longer need to drop the subobject, so report that we
2109+
* found the subobject in the array. But don't plaster its
2110+
* flags on the whole object.
21092111
*/
2110-
return true;
2112+
result = true;
2113+
}
2114+
else if (object->objectSubId == 0)
2115+
{
2116+
/*
2117+
* We get here if we find a need to delete a whole table after
2118+
* having already decided to drop one of its columns. We
2119+
* can't report that the whole object is in the array, but we
2120+
* should mark the subobject with the whole object's flags.
2121+
*
2122+
* It might seem attractive to physically delete the column's
2123+
* array entry, or at least mark it as no longer needing
2124+
* separate deletion. But that could lead to, e.g., dropping
2125+
* the column's datatype before we drop the table, which does
2126+
* not seem like a good idea. This is a very rare situation
2127+
* in practice, so we just take the hit of doing a separate
2128+
* DROP COLUMN action even though we know we're gonna delete
2129+
* the table later.
2130+
*
2131+
* Because there could be other subobjects of this object in
2132+
* the array, this case means we always have to loop through
2133+
* the whole array; we cannot exit early on a match.
2134+
*/
2135+
ObjectAddressExtra *thisextra = addrs->extras + i;
2136+
2137+
thisextra->flags |= flags;
21112138
}
21122139
}
21132140
}
21142141

2115-
return false;
2142+
return result;
21162143
}
21172144

21182145
/*
@@ -2123,6 +2150,7 @@ stack_address_present_add_flags(const ObjectAddress *object,
21232150
int flags,
21242151
ObjectAddressStack *stack)
21252152
{
2153+
bool result = false;
21262154
ObjectAddressStack *stackptr;
21272155

21282156
for (stackptr = stack; stackptr; stackptr = stackptr->next)
@@ -2135,21 +2163,31 @@ stack_address_present_add_flags(const ObjectAddress *object,
21352163
if (object->objectSubId == thisobj->objectSubId)
21362164
{
21372165
stackptr->flags |= flags;
2138-
return true;
2166+
result = true;
2167+
}
2168+
else if (thisobj->objectSubId == 0)
2169+
{
2170+
/*
2171+
* We're visiting a column with whole table already on stack.
2172+
* As in object_address_present_add_flags(), we can skip
2173+
* further processing of the subobject, but we don't want to
2174+
* propagate flags for the subobject to the whole object.
2175+
*/
2176+
result = true;
2177+
}
2178+
else if (object->objectSubId == 0)
2179+
{
2180+
/*
2181+
* We're visiting a table with column already on stack. As in
2182+
* object_address_present_add_flags(), we should propagate
2183+
* flags for the whole object to each of its subobjects.
2184+
*/
2185+
stackptr->flags |= flags;
21392186
}
2140-
2141-
/*
2142-
* Could visit column with whole table already on stack; this is
2143-
* the same case noted in object_address_present_add_flags(), and
2144-
* as in that case, we don't propagate flags for the component to
2145-
* the whole object.
2146-
*/
2147-
if (thisobj->objectSubId == 0)
2148-
return true;
21492187
}
21502188
}
21512189

2152-
return false;
2190+
return result;
21532191
}
21542192

21552193
/*

0 commit comments

Comments
 (0)