Skip to content

Commit 8aa9dd7

Browse files
committed
Sort the dependent objects before deletion in DROP OWNED BY.
This finishes a task we left undone in commit f1ad067, by extending the delete-in-descending-OID-order rule to deletions triggered by DROP OWNED BY. We've coped with machine-dependent deletion orders one time too many, and the new issues caused by Peter G's recent nbtree hacking seem like the last straw. Discussion: https://postgr.es/m/[email protected]
1 parent a6da004 commit 8aa9dd7

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/backend/catalog/dependency.c

+17
Original file line numberDiff line numberDiff line change
@@ -2610,6 +2610,23 @@ record_object_address_dependencies(const ObjectAddress *depender,
26102610
behavior);
26112611
}
26122612

2613+
/*
2614+
* Sort the items in an ObjectAddresses array.
2615+
*
2616+
* The major sort key is OID-descending, so that newer objects will be listed
2617+
* first in most cases. This is primarily useful for ensuring stable outputs
2618+
* from regression tests; it's not recommended if the order of the objects is
2619+
* determined by user input, such as the order of targets in a DROP command.
2620+
*/
2621+
void
2622+
sort_object_addresses(ObjectAddresses *addrs)
2623+
{
2624+
if (addrs->numrefs > 1)
2625+
qsort((void *) addrs->refs, addrs->numrefs,
2626+
sizeof(ObjectAddress),
2627+
object_address_comparator);
2628+
}
2629+
26132630
/*
26142631
* Clean up when done with an ObjectAddresses array.
26152632
*/

src/backend/catalog/pg_shdepend.c

+8
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,14 @@ shdepDropOwned(List *roleids, DropBehavior behavior)
12661266
systable_endscan(scan);
12671267
}
12681268

1269+
/*
1270+
* For stability of deletion-report ordering, sort the objects into
1271+
* approximate reverse creation order before deletion. (This might also
1272+
* make the deletion go a bit faster, since there's less chance of having
1273+
* to rearrange the objects due to dependencies.)
1274+
*/
1275+
sort_object_addresses(deleteobjs);
1276+
12691277
/* the dependency mechanism does the actual work */
12701278
performMultipleDeletions(deleteobjs, behavior, 0);
12711279

src/include/catalog/dependency.h

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ extern void record_object_address_dependencies(const ObjectAddress *depender,
170170
ObjectAddresses *referenced,
171171
DependencyType behavior);
172172

173+
extern void sort_object_addresses(ObjectAddresses *addrs);
174+
173175
extern void free_object_addresses(ObjectAddresses *addrs);
174176

175177
/* in pg_depend.c */

0 commit comments

Comments
 (0)