1 /*-------------------------------------------------------------------------
2 *
3 * dependency.c
4 * Routines to support inter-object dependencies.
5 *
6 *
7 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * IDENTIFICATION
11 * src/backend/catalog/dependency.c
12 *
13 *-------------------------------------------------------------------------
14 */
15 #include "postgres.h"
16
17 #include "access/htup_details.h"
18 #include "access/xact.h"
19 #include "catalog/dependency.h"
20 #include "catalog/heap.h"
21 #include "catalog/index.h"
22 #include "catalog/objectaccess.h"
23 #include "catalog/pg_am.h"
24 #include "catalog/pg_amop.h"
25 #include "catalog/pg_amproc.h"
26 #include "catalog/pg_attrdef.h"
27 #include "catalog/pg_authid.h"
28 #include "catalog/pg_cast.h"
29 #include "catalog/pg_collation.h"
30 #include "catalog/pg_collation_fn.h"
31 #include "catalog/pg_constraint.h"
32 #include "catalog/pg_constraint_fn.h"
33 #include "catalog/pg_conversion.h"
34 #include "catalog/pg_conversion_fn.h"
35 #include "catalog/pg_database.h"
36 #include "catalog/pg_default_acl.h"
37 #include "catalog/pg_depend.h"
38 #include "catalog/pg_event_trigger.h"
39 #include "catalog/pg_extension.h"
40 #include "catalog/pg_foreign_data_wrapper.h"
41 #include "catalog/pg_foreign_server.h"
42 #include "catalog/pg_init_privs.h"
43 #include "catalog/pg_language.h"
44 #include "catalog/pg_largeobject.h"
45 #include "catalog/pg_namespace.h"
46 #include "catalog/pg_opclass.h"
47 #include "catalog/pg_operator.h"
48 #include "catalog/pg_opfamily.h"
49 #include "catalog/pg_policy.h"
50 #include "catalog/pg_proc.h"
51 #include "catalog/pg_rewrite.h"
52 #include "catalog/pg_tablespace.h"
53 #include "catalog/pg_transform.h"
54 #include "catalog/pg_trigger.h"
55 #include "catalog/pg_ts_config.h"
56 #include "catalog/pg_ts_dict.h"
57 #include "catalog/pg_ts_parser.h"
58 #include "catalog/pg_ts_template.h"
59 #include "catalog/pg_type.h"
60 #include "catalog/pg_user_mapping.h"
61 #include "commands/comment.h"
62 #include "commands/defrem.h"
63 #include "commands/event_trigger.h"
64 #include "commands/extension.h"
65 #include "commands/policy.h"
66 #include "commands/proclang.h"
67 #include "commands/schemacmds.h"
68 #include "commands/seclabel.h"
69 #include "commands/trigger.h"
70 #include "commands/typecmds.h"
71 #include "nodes/nodeFuncs.h"
72 #include "parser/parsetree.h"
73 #include "rewrite/rewriteRemove.h"
74 #include "storage/lmgr.h"
75 #include "utils/fmgroids.h"
76 #include "utils/guc.h"
77 #include "utils/lsyscache.h"
78 #include "utils/syscache.h"
79 #include "utils/tqual.h"
80
81
82 /*
83 * Deletion processing requires additional state for each ObjectAddress that
84 * it's planning to delete. For simplicity and code-sharing we make the
85 * ObjectAddresses code support arrays with or without this extra state.
86 */
87 typedef struct
88 {
89 int flags; /* bitmask, see bit definitions below */
90 ObjectAddress dependee; /* object whose deletion forced this one */
91 } ObjectAddressExtra;
92
93 /* ObjectAddressExtra flag bits */
94 #define DEPFLAG_ORIGINAL 0x0001 /* an original deletion target */
95 #define DEPFLAG_NORMAL 0x0002 /* reached via normal dependency */
96 #define DEPFLAG_AUTO 0x0004 /* reached via auto dependency */
97 #define DEPFLAG_INTERNAL 0x0008 /* reached via internal dependency */
98 #define DEPFLAG_EXTENSION 0x0010 /* reached via extension dependency */
99 #define DEPFLAG_REVERSE 0x0020 /* reverse internal/extension link */
100
101
102 /* expansible list of ObjectAddresses */
103 struct ObjectAddresses
104 {
105 ObjectAddress *refs; /* => palloc'd array */
106 ObjectAddressExtra *extras; /* => palloc'd array, or NULL if not used */
107 int numrefs; /* current number of references */
108 int maxrefs; /* current size of palloc'd array(s) */
109 };
110
111 /* typedef ObjectAddresses appears in dependency.h */
112
113 /* threaded list of ObjectAddresses, for recursion detection */
114 typedef struct ObjectAddressStack
115 {
116 const ObjectAddress *object; /* object being visited */
117 int flags; /* its current flag bits */
118 struct ObjectAddressStack *next; /* next outer stack level */
119 } ObjectAddressStack;
120
121 /* for find_expr_references_walker */
122 typedef struct
123 {
124 ObjectAddresses *addrs; /* addresses being accumulated */
125 List *rtables; /* list of rangetables to resolve Vars */
126 } find_expr_references_context;
127
128 /*
129 * This constant table maps ObjectClasses to the corresponding catalog OIDs.
130 * See also getObjectClass().
131 */
132 static const Oid object_classes[] = {
133 RelationRelationId, /* OCLASS_CLASS */
134 ProcedureRelationId, /* OCLASS_PROC */
135 TypeRelationId, /* OCLASS_TYPE */
136 CastRelationId, /* OCLASS_CAST */
137 CollationRelationId, /* OCLASS_COLLATION */
138 ConstraintRelationId, /* OCLASS_CONSTRAINT */
139 ConversionRelationId, /* OCLASS_CONVERSION */
140 AttrDefaultRelationId, /* OCLASS_DEFAULT */
141 LanguageRelationId, /* OCLASS_LANGUAGE */
142 LargeObjectRelationId, /* OCLASS_LARGEOBJECT */
143 OperatorRelationId, /* OCLASS_OPERATOR */
144 OperatorClassRelationId, /* OCLASS_OPCLASS */
145 OperatorFamilyRelationId, /* OCLASS_OPFAMILY */
146 AccessMethodRelationId, /* OCLASS_AM */
147 AccessMethodOperatorRelationId, /* OCLASS_AMOP */
148 AccessMethodProcedureRelationId, /* OCLASS_AMPROC */
149 RewriteRelationId, /* OCLASS_REWRITE */
150 TriggerRelationId, /* OCLASS_TRIGGER */
151 NamespaceRelationId, /* OCLASS_SCHEMA */
152 TSParserRelationId, /* OCLASS_TSPARSER */
153 TSDictionaryRelationId, /* OCLASS_TSDICT */
154 TSTemplateRelationId, /* OCLASS_TSTEMPLATE */
155 TSConfigRelationId, /* OCLASS_TSCONFIG */
156 AuthIdRelationId, /* OCLASS_ROLE */
157 DatabaseRelationId, /* OCLASS_DATABASE */
158 TableSpaceRelationId, /* OCLASS_TBLSPACE */
159 ForeignDataWrapperRelationId, /* OCLASS_FDW */
160 ForeignServerRelationId, /* OCLASS_FOREIGN_SERVER */
161 UserMappingRelationId, /* OCLASS_USER_MAPPING */
162 DefaultAclRelationId, /* OCLASS_DEFACL */
163 ExtensionRelationId, /* OCLASS_EXTENSION */
164 EventTriggerRelationId, /* OCLASS_EVENT_TRIGGER */
165 PolicyRelationId, /* OCLASS_POLICY */
166 TransformRelationId /* OCLASS_TRANSFORM */
167 };
168
169
170 static void findDependentObjects(const ObjectAddress *object,
171 int flags,
172 ObjectAddressStack *stack,
173 ObjectAddresses *targetObjects,
174 const ObjectAddresses *pendingObjects,
175 Relation *depRel);
176 static void reportDependentObjects(const ObjectAddresses *targetObjects,
177 DropBehavior behavior,
178 int msglevel,
179 const ObjectAddress *origObject);
180 static void deleteOneObject(const ObjectAddress *object,
181 Relation *depRel, int32 flags);
182 static void doDeletion(const ObjectAddress *object, int flags);
183 static bool find_expr_references_walker(Node *node,
184 find_expr_references_context *context);
185 static void eliminate_duplicate_dependencies(ObjectAddresses *addrs);
186 static int object_address_comparator(const void *a, const void *b);
187 static void add_object_address(ObjectClass oclass, Oid objectId, int32 subId,
188 ObjectAddresses *addrs);
189 static void add_exact_object_address_extra(const ObjectAddress *object,
190 const ObjectAddressExtra *extra,
191 ObjectAddresses *addrs);
192 static bool object_address_present_add_flags(const ObjectAddress *object,
193 int flags,
194 ObjectAddresses *addrs);
195 static bool stack_address_present_add_flags(const ObjectAddress *object,
196 int flags,
197 ObjectAddressStack *stack);
198 static void DeleteInitPrivs(const ObjectAddress *object);
199
200
201 /*
202 * Go through the objects given running the final actions on them, and execute
203 * the actual deletion.
204 */
205 static void
deleteObjectsInList(ObjectAddresses * targetObjects,Relation * depRel,int flags)206 deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel,
207 int flags)
208 {
209 int i;
210
211 /*
212 * Keep track of objects for event triggers, if necessary.
213 */
214 if (trackDroppedObjectsNeeded() && !(flags & PERFORM_DELETION_INTERNAL))
215 {
216 for (i = 0; i < targetObjects->numrefs; i++)
217 {
218 const ObjectAddress *thisobj = &targetObjects->refs[i];
219 const ObjectAddressExtra *extra = &targetObjects->extras[i];
220 bool original = false;
221 bool normal = false;
222
223 if (extra->flags & DEPFLAG_ORIGINAL)
224 original = true;
225 if (extra->flags & DEPFLAG_NORMAL)
226 normal = true;
227 if (extra->flags & DEPFLAG_REVERSE)
228 normal = true;
229
230 if (EventTriggerSupportsObjectClass(getObjectClass(thisobj)))
231 {
232 EventTriggerSQLDropAddObject(thisobj, original, normal);
233 }
234 }
235 }
236
237 /*
238 * Delete all the objects in the proper order.
239 */
240 for (i = 0; i < targetObjects->numrefs; i++)
241 {
242 ObjectAddress *thisobj = targetObjects->refs + i;
243
244 deleteOneObject(thisobj, depRel, flags);
245 }
246 }
247
248 /*
249 * performDeletion: attempt to drop the specified object. If CASCADE
250 * behavior is specified, also drop any dependent objects (recursively).
251 * If RESTRICT behavior is specified, error out if there are any dependent
252 * objects, except for those that should be implicitly dropped anyway
253 * according to the dependency type.
254 *
255 * This is the outer control routine for all forms of DROP that drop objects
256 * that can participate in dependencies. Note that the next two routines
257 * are variants on the same theme; if you change anything here you'll likely
258 * need to fix them too.
259 *
260 * flags should include PERFORM_DELETION_INTERNAL when the drop operation is
261 * not the direct result of a user-initiated action. For example, when a
262 * temporary schema is cleaned out so that a new backend can use it, or when
263 * a column default is dropped as an intermediate step while adding a new one,
264 * that's an internal operation. On the other hand, when we drop something
265 * because the user issued a DROP statement against it, that's not internal.
266 */
267 void
performDeletion(const ObjectAddress * object,DropBehavior behavior,int flags)268 performDeletion(const ObjectAddress *object,
269 DropBehavior behavior, int flags)
270 {
271 Relation depRel;
272 ObjectAddresses *targetObjects;
273
274 /*
275 * We save some cycles by opening pg_depend just once and passing the
276 * Relation pointer down to all the recursive deletion steps.
277 */
278 depRel = heap_open(DependRelationId, RowExclusiveLock);
279
280 /*
281 * Acquire deletion lock on the target object. (Ideally the caller has
282 * done this already, but many places are sloppy about it.)
283 */
284 AcquireDeletionLock(object, 0);
285
286 /*
287 * Construct a list of objects to delete (ie, the given object plus
288 * everything directly or indirectly dependent on it).
289 */
290 targetObjects = new_object_addresses();
291
292 findDependentObjects(object,
293 DEPFLAG_ORIGINAL,
294 NULL, /* empty stack */
295 targetObjects,
296 NULL, /* no pendingObjects */
297 &depRel);
298
299 /*
300 * Check if deletion is allowed, and report about cascaded deletes.
301 */
302 reportDependentObjects(targetObjects,
303 behavior,
304 NOTICE,
305 object);
306
307 /* do the deed */
308 deleteObjectsInList(targetObjects, &depRel, flags);
309
310 /* And clean up */
311 free_object_addresses(targetObjects);
312
313 heap_close(depRel, RowExclusiveLock);
314 }
315
316 /*
317 * performMultipleDeletions: Similar to performDeletion, but act on multiple
318 * objects at once.
319 *
320 * The main difference from issuing multiple performDeletion calls is that the
321 * list of objects that would be implicitly dropped, for each object to be
322 * dropped, is the union of the implicit-object list for all objects. This
323 * makes each check be more relaxed.
324 */
325 void
performMultipleDeletions(const ObjectAddresses * objects,DropBehavior behavior,int flags)326 performMultipleDeletions(const ObjectAddresses *objects,
327 DropBehavior behavior, int flags)
328 {
329 Relation depRel;
330 ObjectAddresses *targetObjects;
331 int i;
332
333 /* No work if no objects... */
334 if (objects->numrefs <= 0)
335 return;
336
337 /*
338 * We save some cycles by opening pg_depend just once and passing the
339 * Relation pointer down to all the recursive deletion steps.
340 */
341 depRel = heap_open(DependRelationId, RowExclusiveLock);
342
343 /*
344 * Construct a list of objects to delete (ie, the given objects plus
345 * everything directly or indirectly dependent on them). Note that
346 * because we pass the whole objects list as pendingObjects context, we
347 * won't get a failure from trying to delete an object that is internally
348 * dependent on another one in the list; we'll just skip that object and
349 * delete it when we reach its owner.
350 */
351 targetObjects = new_object_addresses();
352
353 for (i = 0; i < objects->numrefs; i++)
354 {
355 const ObjectAddress *thisobj = objects->refs + i;
356
357 /*
358 * Acquire deletion lock on each target object. (Ideally the caller
359 * has done this already, but many places are sloppy about it.)
360 */
361 AcquireDeletionLock(thisobj, flags);
362
363 findDependentObjects(thisobj,
364 DEPFLAG_ORIGINAL,
365 NULL, /* empty stack */
366 targetObjects,
367 objects,
368 &depRel);
369 }
370
371 /*
372 * Check if deletion is allowed, and report about cascaded deletes.
373 *
374 * If there's exactly one object being deleted, report it the same way as
375 * in performDeletion(), else we have to be vaguer.
376 */
377 reportDependentObjects(targetObjects,
378 behavior,
379 NOTICE,
380 (objects->numrefs == 1 ? objects->refs : NULL));
381
382 /* do the deed */
383 deleteObjectsInList(targetObjects, &depRel, flags);
384
385 /* And clean up */
386 free_object_addresses(targetObjects);
387
388 heap_close(depRel, RowExclusiveLock);
389 }
390
391 /*
392 * deleteWhatDependsOn: attempt to drop everything that depends on the
393 * specified object, though not the object itself. Behavior is always
394 * CASCADE.
395 *
396 * This is currently used only to clean out the contents of a schema
397 * (namespace): the passed object is a namespace. We normally want this
398 * to be done silently, so there's an option to suppress NOTICE messages.
399 *
400 * Note we don't fire object drop event triggers here; it would be wrong to do
401 * so for the current only use of this function, but if more callers are added
402 * this might need to be reconsidered.
403 */
404 void
deleteWhatDependsOn(const ObjectAddress * object,bool showNotices)405 deleteWhatDependsOn(const ObjectAddress *object,
406 bool showNotices)
407 {
408 Relation depRel;
409 ObjectAddresses *targetObjects;
410 int i;
411
412 /*
413 * We save some cycles by opening pg_depend just once and passing the
414 * Relation pointer down to all the recursive deletion steps.
415 */
416 depRel = heap_open(DependRelationId, RowExclusiveLock);
417
418 /*
419 * Acquire deletion lock on the target object. (Ideally the caller has
420 * done this already, but many places are sloppy about it.)
421 */
422 AcquireDeletionLock(object, 0);
423
424 /*
425 * Construct a list of objects to delete (ie, the given object plus
426 * everything directly or indirectly dependent on it).
427 */
428 targetObjects = new_object_addresses();
429
430 findDependentObjects(object,
431 DEPFLAG_ORIGINAL,
432 NULL, /* empty stack */
433 targetObjects,
434 NULL, /* no pendingObjects */
435 &depRel);
436
437 /*
438 * Check if deletion is allowed, and report about cascaded deletes.
439 */
440 reportDependentObjects(targetObjects,
441 DROP_CASCADE,
442 showNotices ? NOTICE : DEBUG2,
443 object);
444
445 /*
446 * Delete all the objects in the proper order, except we skip the original
447 * object.
448 */
449 for (i = 0; i < targetObjects->numrefs; i++)
450 {
451 ObjectAddress *thisobj = targetObjects->refs + i;
452 ObjectAddressExtra *thisextra = targetObjects->extras + i;
453
454 if (thisextra->flags & DEPFLAG_ORIGINAL)
455 continue;
456
457 /*
458 * Since this function is currently only used to clean out temporary
459 * schemas, we pass PERFORM_DELETION_INTERNAL here, indicating that
460 * the operation is an automatic system operation rather than a user
461 * action. If, in the future, this function is used for other
462 * purposes, we might need to revisit this.
463 */
464 deleteOneObject(thisobj, &depRel, PERFORM_DELETION_INTERNAL);
465 }
466
467 /* And clean up */
468 free_object_addresses(targetObjects);
469
470 heap_close(depRel, RowExclusiveLock);
471 }
472
473 /*
474 * findDependentObjects - find all objects that depend on 'object'
475 *
476 * For every object that depends on the starting object, acquire a deletion
477 * lock on the object, add it to targetObjects (if not already there),
478 * and recursively find objects that depend on it. An object's dependencies
479 * will be placed into targetObjects before the object itself; this means
480 * that the finished list's order represents a safe deletion order.
481 *
482 * The caller must already have a deletion lock on 'object' itself,
483 * but must not have added it to targetObjects. (Note: there are corner
484 * cases where we won't add the object either, and will also release the
485 * caller-taken lock. This is a bit ugly, but the API is set up this way
486 * to allow easy rechecking of an object's liveness after we lock it. See
487 * notes within the function.)
488 *
489 * When dropping a whole object (subId = 0), we find dependencies for
490 * its sub-objects too.
491 *
492 * object: the object to add to targetObjects and find dependencies on
493 * flags: flags to be ORed into the object's targetObjects entry
494 * stack: list of objects being visited in current recursion; topmost item
495 * is the object that we recursed from (NULL for external callers)
496 * targetObjects: list of objects that are scheduled to be deleted
497 * pendingObjects: list of other objects slated for destruction, but
498 * not necessarily in targetObjects yet (can be NULL if none)
499 * *depRel: already opened pg_depend relation
500 */
501 static void
findDependentObjects(const ObjectAddress * object,int flags,ObjectAddressStack * stack,ObjectAddresses * targetObjects,const ObjectAddresses * pendingObjects,Relation * depRel)502 findDependentObjects(const ObjectAddress *object,
503 int flags,
504 ObjectAddressStack *stack,
505 ObjectAddresses *targetObjects,
506 const ObjectAddresses *pendingObjects,
507 Relation *depRel)
508 {
509 ScanKeyData key[3];
510 int nkeys;
511 SysScanDesc scan;
512 HeapTuple tup;
513 ObjectAddress otherObject;
514 ObjectAddressStack mystack;
515 ObjectAddressExtra extra;
516
517 /*
518 * If the target object is already being visited in an outer recursion
519 * level, just report the current flags back to that level and exit. This
520 * is needed to avoid infinite recursion in the face of circular
521 * dependencies.
522 *
523 * The stack check alone would result in dependency loops being broken at
524 * an arbitrary point, ie, the first member object of the loop to be
525 * visited is the last one to be deleted. This is obviously unworkable.
526 * However, the check for internal dependency below guarantees that we
527 * will not break a loop at an internal dependency: if we enter the loop
528 * at an "owned" object we will switch and start at the "owning" object
529 * instead. We could probably hack something up to avoid breaking at an
530 * auto dependency, too, if we had to. However there are no known cases
531 * where that would be necessary.
532 */
533 if (stack_address_present_add_flags(object, flags, stack))
534 return;
535
536 /*
537 * It's also possible that the target object has already been completely
538 * processed and put into targetObjects. If so, again we just add the
539 * specified flags to its entry and return.
540 *
541 * (Note: in these early-exit cases we could release the caller-taken
542 * lock, since the object is presumably now locked multiple times; but it
543 * seems not worth the cycles.)
544 */
545 if (object_address_present_add_flags(object, flags, targetObjects))
546 return;
547
548 /*
549 * The target object might be internally dependent on some other object
550 * (its "owner"), and/or be a member of an extension (also considered its
551 * owner). If so, and if we aren't recursing from the owning object, we
552 * have to transform this deletion request into a deletion request of the
553 * owning object. (We'll eventually recurse back to this object, but the
554 * owning object has to be visited first so it will be deleted after.) The
555 * way to find out about this is to scan the pg_depend entries that show
556 * what this object depends on.
557 */
558 ScanKeyInit(&key[0],
559 Anum_pg_depend_classid,
560 BTEqualStrategyNumber, F_OIDEQ,
561 ObjectIdGetDatum(object->classId));
562 ScanKeyInit(&key[1],
563 Anum_pg_depend_objid,
564 BTEqualStrategyNumber, F_OIDEQ,
565 ObjectIdGetDatum(object->objectId));
566 if (object->objectSubId != 0)
567 {
568 ScanKeyInit(&key[2],
569 Anum_pg_depend_objsubid,
570 BTEqualStrategyNumber, F_INT4EQ,
571 Int32GetDatum(object->objectSubId));
572 nkeys = 3;
573 }
574 else
575 nkeys = 2;
576
577 scan = systable_beginscan(*depRel, DependDependerIndexId, true,
578 NULL, nkeys, key);
579
580 while (HeapTupleIsValid(tup = systable_getnext(scan)))
581 {
582 Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
583
584 otherObject.classId = foundDep->refclassid;
585 otherObject.objectId = foundDep->refobjid;
586 otherObject.objectSubId = foundDep->refobjsubid;
587
588 switch (foundDep->deptype)
589 {
590 case DEPENDENCY_NORMAL:
591 case DEPENDENCY_AUTO:
592 case DEPENDENCY_AUTO_EXTENSION:
593 /* no problem */
594 break;
595
596 case DEPENDENCY_EXTENSION:
597
598 /*
599 * If the other object is the extension currently being
600 * created/altered, ignore this dependency and continue with
601 * the deletion. This allows dropping of an extension's
602 * objects within the extension's scripts, as well as corner
603 * cases such as dropping a transient object created within
604 * such a script.
605 */
606 if (creating_extension &&
607 otherObject.classId == ExtensionRelationId &&
608 otherObject.objectId == CurrentExtensionObject)
609 break;
610
611 /* Otherwise, treat this like an internal dependency */
612 /* FALL THRU */
613
614 case DEPENDENCY_INTERNAL:
615
616 /*
617 * This object is part of the internal implementation of
618 * another object, or is part of the extension that is the
619 * other object. We have three cases:
620 *
621 * 1. At the outermost recursion level, disallow the DROP. (We
622 * just ereport here, rather than proceeding, since no other
623 * dependencies are likely to be interesting.) However, if
624 * the owning object is listed in pendingObjects, just release
625 * the caller's lock and return; we'll eventually complete the
626 * DROP when we reach that entry in the pending list.
627 */
628 if (stack == NULL)
629 {
630 char *otherObjDesc;
631
632 if (pendingObjects &&
633 object_address_present(&otherObject, pendingObjects))
634 {
635 systable_endscan(scan);
636 /* need to release caller's lock; see notes below */
637 ReleaseDeletionLock(object);
638 return;
639 }
640 otherObjDesc = getObjectDescription(&otherObject);
641 ereport(ERROR,
642 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
643 errmsg("cannot drop %s because %s requires it",
644 getObjectDescription(object),
645 otherObjDesc),
646 errhint("You can drop %s instead.",
647 otherObjDesc)));
648 }
649
650 /*
651 * 2. When recursing from the other end of this dependency,
652 * it's okay to continue with the deletion. This holds when
653 * recursing from a whole object that includes the nominal
654 * other end as a component, too. Since there can be more
655 * than one "owning" object, we have to allow matches that are
656 * more than one level down in the stack.
657 */
658 if (stack_address_present_add_flags(&otherObject, 0, stack))
659 break;
660
661 /*
662 * 3. Not all the owning objects have been visited, so
663 * transform this deletion request into a delete of this
664 * owning object.
665 *
666 * First, release caller's lock on this object and get
667 * deletion lock on the owning object. (We must release
668 * caller's lock to avoid deadlock against a concurrent
669 * deletion of the owning object.)
670 */
671 ReleaseDeletionLock(object);
672 AcquireDeletionLock(&otherObject, 0);
673
674 /*
675 * The owning object might have been deleted while we waited
676 * to lock it; if so, neither it nor the current object are
677 * interesting anymore. We test this by checking the
678 * pg_depend entry (see notes below).
679 */
680 if (!systable_recheck_tuple(scan, tup))
681 {
682 systable_endscan(scan);
683 ReleaseDeletionLock(&otherObject);
684 return;
685 }
686
687 /*
688 * Okay, recurse to the owning object instead of proceeding.
689 *
690 * We do not need to stack the current object; we want the
691 * traversal order to be as if the original reference had
692 * linked to the owning object instead of this one.
693 *
694 * The dependency type is a "reverse" dependency: we need to
695 * delete the owning object if this one is to be deleted, but
696 * this linkage is never a reason for an automatic deletion.
697 */
698 findDependentObjects(&otherObject,
699 DEPFLAG_REVERSE,
700 stack,
701 targetObjects,
702 pendingObjects,
703 depRel);
704 /* And we're done here. */
705 systable_endscan(scan);
706 return;
707 case DEPENDENCY_PIN:
708
709 /*
710 * Should not happen; PIN dependencies should have zeroes in
711 * the depender fields...
712 */
713 elog(ERROR, "incorrect use of PIN dependency with %s",
714 getObjectDescription(object));
715 break;
716 default:
717 elog(ERROR, "unrecognized dependency type '%c' for %s",
718 foundDep->deptype, getObjectDescription(object));
719 break;
720 }
721 }
722
723 systable_endscan(scan);
724
725 /*
726 * Now recurse to any dependent objects. We must visit them first since
727 * they have to be deleted before the current object.
728 */
729 mystack.object = object; /* set up a new stack level */
730 mystack.flags = flags;
731 mystack.next = stack;
732
733 ScanKeyInit(&key[0],
734 Anum_pg_depend_refclassid,
735 BTEqualStrategyNumber, F_OIDEQ,
736 ObjectIdGetDatum(object->classId));
737 ScanKeyInit(&key[1],
738 Anum_pg_depend_refobjid,
739 BTEqualStrategyNumber, F_OIDEQ,
740 ObjectIdGetDatum(object->objectId));
741 if (object->objectSubId != 0)
742 {
743 ScanKeyInit(&key[2],
744 Anum_pg_depend_refobjsubid,
745 BTEqualStrategyNumber, F_INT4EQ,
746 Int32GetDatum(object->objectSubId));
747 nkeys = 3;
748 }
749 else
750 nkeys = 2;
751
752 scan = systable_beginscan(*depRel, DependReferenceIndexId, true,
753 NULL, nkeys, key);
754
755 while (HeapTupleIsValid(tup = systable_getnext(scan)))
756 {
757 Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
758 int subflags;
759
760 otherObject.classId = foundDep->classid;
761 otherObject.objectId = foundDep->objid;
762 otherObject.objectSubId = foundDep->objsubid;
763
764 /*
765 * Must lock the dependent object before recursing to it.
766 */
767 AcquireDeletionLock(&otherObject, 0);
768
769 /*
770 * The dependent object might have been deleted while we waited to
771 * lock it; if so, we don't need to do anything more with it. We can
772 * test this cheaply and independently of the object's type by seeing
773 * if the pg_depend tuple we are looking at is still live. (If the
774 * object got deleted, the tuple would have been deleted too.)
775 */
776 if (!systable_recheck_tuple(scan, tup))
777 {
778 /* release the now-useless lock */
779 ReleaseDeletionLock(&otherObject);
780 /* and continue scanning for dependencies */
781 continue;
782 }
783
784 /* Recurse, passing flags indicating the dependency type */
785 switch (foundDep->deptype)
786 {
787 case DEPENDENCY_NORMAL:
788 subflags = DEPFLAG_NORMAL;
789 break;
790 case DEPENDENCY_AUTO:
791 case DEPENDENCY_AUTO_EXTENSION:
792 subflags = DEPFLAG_AUTO;
793 break;
794 case DEPENDENCY_INTERNAL:
795 subflags = DEPFLAG_INTERNAL;
796 break;
797 case DEPENDENCY_EXTENSION:
798 subflags = DEPFLAG_EXTENSION;
799 break;
800 case DEPENDENCY_PIN:
801
802 /*
803 * For a PIN dependency we just ereport immediately; there
804 * won't be any others to report.
805 */
806 ereport(ERROR,
807 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
808 errmsg("cannot drop %s because it is required by the database system",
809 getObjectDescription(object))));
810 subflags = 0; /* keep compiler quiet */
811 break;
812 default:
813 elog(ERROR, "unrecognized dependency type '%c' for %s",
814 foundDep->deptype, getObjectDescription(object));
815 subflags = 0; /* keep compiler quiet */
816 break;
817 }
818
819 findDependentObjects(&otherObject,
820 subflags,
821 &mystack,
822 targetObjects,
823 pendingObjects,
824 depRel);
825 }
826
827 systable_endscan(scan);
828
829 /*
830 * Finally, we can add the target object to targetObjects. Be careful to
831 * include any flags that were passed back down to us from inner recursion
832 * levels.
833 */
834 extra.flags = mystack.flags;
835 if (stack)
836 extra.dependee = *stack->object;
837 else
838 memset(&extra.dependee, 0, sizeof(extra.dependee));
839 add_exact_object_address_extra(object, &extra, targetObjects);
840 }
841
842 /*
843 * reportDependentObjects - report about dependencies, and fail if RESTRICT
844 *
845 * Tell the user about dependent objects that we are going to delete
846 * (or would need to delete, but are prevented by RESTRICT mode);
847 * then error out if there are any and it's not CASCADE mode.
848 *
849 * targetObjects: list of objects that are scheduled to be deleted
850 * behavior: RESTRICT or CASCADE
851 * msglevel: elog level for non-error report messages
852 * origObject: base object of deletion, or NULL if not available
853 * (the latter case occurs in DROP OWNED)
854 */
855 static void
reportDependentObjects(const ObjectAddresses * targetObjects,DropBehavior behavior,int msglevel,const ObjectAddress * origObject)856 reportDependentObjects(const ObjectAddresses *targetObjects,
857 DropBehavior behavior,
858 int msglevel,
859 const ObjectAddress *origObject)
860 {
861 bool ok = true;
862 StringInfoData clientdetail;
863 StringInfoData logdetail;
864 int numReportedClient = 0;
865 int numNotReportedClient = 0;
866 int i;
867
868 /*
869 * If no error is to be thrown, and the msglevel is too low to be shown to
870 * either client or server log, there's no need to do any of the work.
871 *
872 * Note: this code doesn't know all there is to be known about elog
873 * levels, but it works for NOTICE and DEBUG2, which are the only values
874 * msglevel can currently have. We also assume we are running in a normal
875 * operating environment.
876 */
877 if (behavior == DROP_CASCADE &&
878 msglevel < client_min_messages &&
879 (msglevel < log_min_messages || log_min_messages == LOG))
880 return;
881
882 /*
883 * We limit the number of dependencies reported to the client to
884 * MAX_REPORTED_DEPS, since client software may not deal well with
885 * enormous error strings. The server log always gets a full report.
886 */
887 #define MAX_REPORTED_DEPS 100
888
889 initStringInfo(&clientdetail);
890 initStringInfo(&logdetail);
891
892 /*
893 * We process the list back to front (ie, in dependency order not deletion
894 * order), since this makes for a more understandable display.
895 */
896 for (i = targetObjects->numrefs - 1; i >= 0; i--)
897 {
898 const ObjectAddress *obj = &targetObjects->refs[i];
899 const ObjectAddressExtra *extra = &targetObjects->extras[i];
900 char *objDesc;
901
902 /* Ignore the original deletion target(s) */
903 if (extra->flags & DEPFLAG_ORIGINAL)
904 continue;
905
906 objDesc = getObjectDescription(obj);
907
908 /* An object being dropped concurrently doesn't need to be reported */
909 if (objDesc == NULL)
910 continue;
911
912 /*
913 * If, at any stage of the recursive search, we reached the object via
914 * an AUTO, INTERNAL, or EXTENSION dependency, then it's okay to
915 * delete it even in RESTRICT mode.
916 */
917 if (extra->flags & (DEPFLAG_AUTO |
918 DEPFLAG_INTERNAL |
919 DEPFLAG_EXTENSION))
920 {
921 /*
922 * auto-cascades are reported at DEBUG2, not msglevel. We don't
923 * try to combine them with the regular message because the
924 * results are too confusing when client_min_messages and
925 * log_min_messages are different.
926 */
927 ereport(DEBUG2,
928 (errmsg("drop auto-cascades to %s",
929 objDesc)));
930 }
931 else if (behavior == DROP_RESTRICT)
932 {
933 char *otherDesc = getObjectDescription(&extra->dependee);
934
935 if (otherDesc)
936 {
937 if (numReportedClient < MAX_REPORTED_DEPS)
938 {
939 /* separate entries with a newline */
940 if (clientdetail.len != 0)
941 appendStringInfoChar(&clientdetail, '\n');
942 appendStringInfo(&clientdetail, _("%s depends on %s"),
943 objDesc, otherDesc);
944 numReportedClient++;
945 }
946 else
947 numNotReportedClient++;
948 /* separate entries with a newline */
949 if (logdetail.len != 0)
950 appendStringInfoChar(&logdetail, '\n');
951 appendStringInfo(&logdetail, _("%s depends on %s"),
952 objDesc, otherDesc);
953 pfree(otherDesc);
954 }
955 else
956 numNotReportedClient++;
957 ok = false;
958 }
959 else
960 {
961 if (numReportedClient < MAX_REPORTED_DEPS)
962 {
963 /* separate entries with a newline */
964 if (clientdetail.len != 0)
965 appendStringInfoChar(&clientdetail, '\n');
966 appendStringInfo(&clientdetail, _("drop cascades to %s"),
967 objDesc);
968 numReportedClient++;
969 }
970 else
971 numNotReportedClient++;
972 /* separate entries with a newline */
973 if (logdetail.len != 0)
974 appendStringInfoChar(&logdetail, '\n');
975 appendStringInfo(&logdetail, _("drop cascades to %s"),
976 objDesc);
977 }
978
979 pfree(objDesc);
980 }
981
982 if (numNotReportedClient > 0)
983 appendStringInfo(&clientdetail, ngettext("\nand %d other object "
984 "(see server log for list)",
985 "\nand %d other objects "
986 "(see server log for list)",
987 numNotReportedClient),
988 numNotReportedClient);
989
990 if (!ok)
991 {
992 if (origObject)
993 ereport(ERROR,
994 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
995 errmsg("cannot drop %s because other objects depend on it",
996 getObjectDescription(origObject)),
997 errdetail("%s", clientdetail.data),
998 errdetail_log("%s", logdetail.data),
999 errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
1000 else
1001 ereport(ERROR,
1002 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1003 errmsg("cannot drop desired object(s) because other objects depend on them"),
1004 errdetail("%s", clientdetail.data),
1005 errdetail_log("%s", logdetail.data),
1006 errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
1007 }
1008 else if (numReportedClient > 1)
1009 {
1010 ereport(msglevel,
1011 /* translator: %d always has a value larger than 1 */
1012 (errmsg_plural("drop cascades to %d other object",
1013 "drop cascades to %d other objects",
1014 numReportedClient + numNotReportedClient,
1015 numReportedClient + numNotReportedClient),
1016 errdetail("%s", clientdetail.data),
1017 errdetail_log("%s", logdetail.data)));
1018 }
1019 else if (numReportedClient == 1)
1020 {
1021 /* we just use the single item as-is */
1022 ereport(msglevel,
1023 (errmsg_internal("%s", clientdetail.data)));
1024 }
1025
1026 pfree(clientdetail.data);
1027 pfree(logdetail.data);
1028 }
1029
1030 /*
1031 * deleteOneObject: delete a single object for performDeletion.
1032 *
1033 * *depRel is the already-open pg_depend relation.
1034 */
1035 static void
deleteOneObject(const ObjectAddress * object,Relation * depRel,int flags)1036 deleteOneObject(const ObjectAddress *object, Relation *depRel, int flags)
1037 {
1038 ScanKeyData key[3];
1039 int nkeys;
1040 SysScanDesc scan;
1041 HeapTuple tup;
1042
1043 /* DROP hook of the objects being removed */
1044 InvokeObjectDropHookArg(object->classId, object->objectId,
1045 object->objectSubId, flags);
1046
1047 /*
1048 * Close depRel if we are doing a drop concurrently. The object deletion
1049 * subroutine will commit the current transaction, so we can't keep the
1050 * relation open across doDeletion().
1051 */
1052 if (flags & PERFORM_DELETION_CONCURRENTLY)
1053 heap_close(*depRel, RowExclusiveLock);
1054
1055 /*
1056 * Delete the object itself, in an object-type-dependent way.
1057 *
1058 * We used to do this after removing the outgoing dependency links, but it
1059 * seems just as reasonable to do it beforehand. In the concurrent case
1060 * we *must* do it in this order, because we can't make any transactional
1061 * updates before calling doDeletion() --- they'd get committed right
1062 * away, which is not cool if the deletion then fails.
1063 */
1064 doDeletion(object, flags);
1065
1066 /*
1067 * Reopen depRel if we closed it above
1068 */
1069 if (flags & PERFORM_DELETION_CONCURRENTLY)
1070 *depRel = heap_open(DependRelationId, RowExclusiveLock);
1071
1072 /*
1073 * Now remove any pg_depend records that link from this object to others.
1074 * (Any records linking to this object should be gone already.)
1075 *
1076 * When dropping a whole object (subId = 0), remove all pg_depend records
1077 * for its sub-objects too.
1078 */
1079 ScanKeyInit(&key[0],
1080 Anum_pg_depend_classid,
1081 BTEqualStrategyNumber, F_OIDEQ,
1082 ObjectIdGetDatum(object->classId));
1083 ScanKeyInit(&key[1],
1084 Anum_pg_depend_objid,
1085 BTEqualStrategyNumber, F_OIDEQ,
1086 ObjectIdGetDatum(object->objectId));
1087 if (object->objectSubId != 0)
1088 {
1089 ScanKeyInit(&key[2],
1090 Anum_pg_depend_objsubid,
1091 BTEqualStrategyNumber, F_INT4EQ,
1092 Int32GetDatum(object->objectSubId));
1093 nkeys = 3;
1094 }
1095 else
1096 nkeys = 2;
1097
1098 scan = systable_beginscan(*depRel, DependDependerIndexId, true,
1099 NULL, nkeys, key);
1100
1101 while (HeapTupleIsValid(tup = systable_getnext(scan)))
1102 {
1103 simple_heap_delete(*depRel, &tup->t_self);
1104 }
1105
1106 systable_endscan(scan);
1107
1108 /*
1109 * Delete shared dependency references related to this object. Again, if
1110 * subId = 0, remove records for sub-objects too.
1111 */
1112 deleteSharedDependencyRecordsFor(object->classId, object->objectId,
1113 object->objectSubId);
1114
1115
1116 /*
1117 * Delete any comments, security labels, or initial privileges associated
1118 * with this object. (This is a convenient place to do these things,
1119 * rather than having every object type know to do it.)
1120 */
1121 DeleteComments(object->objectId, object->classId, object->objectSubId);
1122 DeleteSecurityLabel(object);
1123 DeleteInitPrivs(object);
1124
1125 /*
1126 * CommandCounterIncrement here to ensure that preceding changes are all
1127 * visible to the next deletion step.
1128 */
1129 CommandCounterIncrement();
1130
1131 /*
1132 * And we're done!
1133 */
1134 }
1135
1136 /*
1137 * doDeletion: actually delete a single object
1138 */
1139 static void
doDeletion(const ObjectAddress * object,int flags)1140 doDeletion(const ObjectAddress *object, int flags)
1141 {
1142 switch (getObjectClass(object))
1143 {
1144 case OCLASS_CLASS:
1145 {
1146 char relKind = get_rel_relkind(object->objectId);
1147
1148 if (relKind == RELKIND_INDEX)
1149 {
1150 bool concurrent = ((flags & PERFORM_DELETION_CONCURRENTLY)
1151 == PERFORM_DELETION_CONCURRENTLY);
1152
1153 Assert(object->objectSubId == 0);
1154 index_drop(object->objectId, concurrent);
1155 }
1156 else
1157 {
1158 if (object->objectSubId != 0)
1159 RemoveAttributeById(object->objectId,
1160 object->objectSubId);
1161 else
1162 heap_drop_with_catalog(object->objectId);
1163 }
1164 break;
1165 }
1166
1167 case OCLASS_PROC:
1168 RemoveFunctionById(object->objectId);
1169 break;
1170
1171 case OCLASS_TYPE:
1172 RemoveTypeById(object->objectId);
1173 break;
1174
1175 case OCLASS_CAST:
1176 DropCastById(object->objectId);
1177 break;
1178
1179 case OCLASS_COLLATION:
1180 RemoveCollationById(object->objectId);
1181 break;
1182
1183 case OCLASS_CONSTRAINT:
1184 RemoveConstraintById(object->objectId);
1185 break;
1186
1187 case OCLASS_CONVERSION:
1188 RemoveConversionById(object->objectId);
1189 break;
1190
1191 case OCLASS_DEFAULT:
1192 RemoveAttrDefaultById(object->objectId);
1193 break;
1194
1195 case OCLASS_LANGUAGE:
1196 DropProceduralLanguageById(object->objectId);
1197 break;
1198
1199 case OCLASS_LARGEOBJECT:
1200 LargeObjectDrop(object->objectId);
1201 break;
1202
1203 case OCLASS_OPERATOR:
1204 RemoveOperatorById(object->objectId);
1205 break;
1206
1207 case OCLASS_OPCLASS:
1208 RemoveOpClassById(object->objectId);
1209 break;
1210
1211 case OCLASS_OPFAMILY:
1212 RemoveOpFamilyById(object->objectId);
1213 break;
1214
1215 case OCLASS_AM:
1216 RemoveAccessMethodById(object->objectId);
1217 break;
1218
1219 case OCLASS_AMOP:
1220 RemoveAmOpEntryById(object->objectId);
1221 break;
1222
1223 case OCLASS_AMPROC:
1224 RemoveAmProcEntryById(object->objectId);
1225 break;
1226
1227 case OCLASS_REWRITE:
1228 RemoveRewriteRuleById(object->objectId);
1229 break;
1230
1231 case OCLASS_TRIGGER:
1232 RemoveTriggerById(object->objectId);
1233 break;
1234
1235 case OCLASS_SCHEMA:
1236 RemoveSchemaById(object->objectId);
1237 break;
1238
1239 case OCLASS_TSPARSER:
1240 RemoveTSParserById(object->objectId);
1241 break;
1242
1243 case OCLASS_TSDICT:
1244 RemoveTSDictionaryById(object->objectId);
1245 break;
1246
1247 case OCLASS_TSTEMPLATE:
1248 RemoveTSTemplateById(object->objectId);
1249 break;
1250
1251 case OCLASS_TSCONFIG:
1252 RemoveTSConfigurationById(object->objectId);
1253 break;
1254
1255 /*
1256 * OCLASS_ROLE, OCLASS_DATABASE, OCLASS_TBLSPACE intentionally not
1257 * handled here
1258 */
1259
1260 case OCLASS_FDW:
1261 RemoveForeignDataWrapperById(object->objectId);
1262 break;
1263
1264 case OCLASS_FOREIGN_SERVER:
1265 RemoveForeignServerById(object->objectId);
1266 break;
1267
1268 case OCLASS_USER_MAPPING:
1269 RemoveUserMappingById(object->objectId);
1270 break;
1271
1272 case OCLASS_DEFACL:
1273 RemoveDefaultACLById(object->objectId);
1274 break;
1275
1276 case OCLASS_EXTENSION:
1277 RemoveExtensionById(object->objectId);
1278 break;
1279
1280 case OCLASS_EVENT_TRIGGER:
1281 RemoveEventTriggerById(object->objectId);
1282 break;
1283
1284 case OCLASS_POLICY:
1285 RemovePolicyById(object->objectId);
1286 break;
1287
1288 case OCLASS_TRANSFORM:
1289 DropTransformById(object->objectId);
1290 break;
1291
1292 default:
1293 elog(ERROR, "unrecognized object class: %u",
1294 object->classId);
1295 }
1296 }
1297
1298 /*
1299 * AcquireDeletionLock - acquire a suitable lock for deleting an object
1300 *
1301 * Accepts the same flags as performDeletion (though currently only
1302 * PERFORM_DELETION_CONCURRENTLY does anything).
1303 *
1304 * We use LockRelation for relations, LockDatabaseObject for everything
1305 * else. Shared-across-databases objects are not currently supported
1306 * because no caller cares, but could be modified to use LockSharedObject.
1307 */
1308 void
AcquireDeletionLock(const ObjectAddress * object,int flags)1309 AcquireDeletionLock(const ObjectAddress *object, int flags)
1310 {
1311 if (object->classId == RelationRelationId)
1312 {
1313 /*
1314 * In DROP INDEX CONCURRENTLY, take only ShareUpdateExclusiveLock on
1315 * the index for the moment. index_drop() will promote the lock once
1316 * it's safe to do so. In all other cases we need full exclusive
1317 * lock.
1318 */
1319 if (flags & PERFORM_DELETION_CONCURRENTLY)
1320 LockRelationOid(object->objectId, ShareUpdateExclusiveLock);
1321 else
1322 LockRelationOid(object->objectId, AccessExclusiveLock);
1323 }
1324 else
1325 {
1326 /* assume we should lock the whole object not a sub-object */
1327 LockDatabaseObject(object->classId, object->objectId, 0,
1328 AccessExclusiveLock);
1329 }
1330 }
1331
1332 /*
1333 * ReleaseDeletionLock - release an object deletion lock
1334 *
1335 * Companion to AcquireDeletionLock.
1336 */
1337 void
ReleaseDeletionLock(const ObjectAddress * object)1338 ReleaseDeletionLock(const ObjectAddress *object)
1339 {
1340 if (object->classId == RelationRelationId)
1341 UnlockRelationOid(object->objectId, AccessExclusiveLock);
1342 else
1343 /* assume we should lock the whole object not a sub-object */
1344 UnlockDatabaseObject(object->classId, object->objectId, 0,
1345 AccessExclusiveLock);
1346 }
1347
1348 /*
1349 * recordDependencyOnExpr - find expression dependencies
1350 *
1351 * This is used to find the dependencies of rules, constraint expressions,
1352 * etc.
1353 *
1354 * Given an expression or query in node-tree form, find all the objects
1355 * it refers to (tables, columns, operators, functions, etc). Record
1356 * a dependency of the specified type from the given depender object
1357 * to each object mentioned in the expression.
1358 *
1359 * rtable is the rangetable to be used to interpret Vars with varlevelsup=0.
1360 * It can be NIL if no such variables are expected.
1361 */
1362 void
recordDependencyOnExpr(const ObjectAddress * depender,Node * expr,List * rtable,DependencyType behavior)1363 recordDependencyOnExpr(const ObjectAddress *depender,
1364 Node *expr, List *rtable,
1365 DependencyType behavior)
1366 {
1367 find_expr_references_context context;
1368
1369 context.addrs = new_object_addresses();
1370
1371 /* Set up interpretation for Vars at varlevelsup = 0 */
1372 context.rtables = list_make1(rtable);
1373
1374 /* Scan the expression tree for referenceable objects */
1375 find_expr_references_walker(expr, &context);
1376
1377 /* Remove any duplicates */
1378 eliminate_duplicate_dependencies(context.addrs);
1379
1380 /* And record 'em */
1381 recordMultipleDependencies(depender,
1382 context.addrs->refs, context.addrs->numrefs,
1383 behavior);
1384
1385 free_object_addresses(context.addrs);
1386 }
1387
1388 /*
1389 * recordDependencyOnSingleRelExpr - find expression dependencies
1390 *
1391 * As above, but only one relation is expected to be referenced (with
1392 * varno = 1 and varlevelsup = 0). Pass the relation OID instead of a
1393 * range table. An additional frammish is that dependencies on that
1394 * relation (or its component columns) will be marked with 'self_behavior',
1395 * whereas 'behavior' is used for everything else.
1396 *
1397 * NOTE: the caller should ensure that a whole-table dependency on the
1398 * specified relation is created separately, if one is needed. In particular,
1399 * a whole-row Var "relation.*" will not cause this routine to emit any
1400 * dependency item. This is appropriate behavior for subexpressions of an
1401 * ordinary query, so other cases need to cope as necessary.
1402 */
1403 void
recordDependencyOnSingleRelExpr(const ObjectAddress * depender,Node * expr,Oid relId,DependencyType behavior,DependencyType self_behavior)1404 recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
1405 Node *expr, Oid relId,
1406 DependencyType behavior,
1407 DependencyType self_behavior)
1408 {
1409 find_expr_references_context context;
1410 RangeTblEntry rte;
1411
1412 context.addrs = new_object_addresses();
1413
1414 /* We gin up a rather bogus rangetable list to handle Vars */
1415 MemSet(&rte, 0, sizeof(rte));
1416 rte.type = T_RangeTblEntry;
1417 rte.rtekind = RTE_RELATION;
1418 rte.relid = relId;
1419 rte.relkind = RELKIND_RELATION; /* no need for exactness here */
1420
1421 context.rtables = list_make1(list_make1(&rte));
1422
1423 /* Scan the expression tree for referenceable objects */
1424 find_expr_references_walker(expr, &context);
1425
1426 /* Remove any duplicates */
1427 eliminate_duplicate_dependencies(context.addrs);
1428
1429 /* Separate self-dependencies if necessary */
1430 if (behavior != self_behavior && context.addrs->numrefs > 0)
1431 {
1432 ObjectAddresses *self_addrs;
1433 ObjectAddress *outobj;
1434 int oldref,
1435 outrefs;
1436
1437 self_addrs = new_object_addresses();
1438
1439 outobj = context.addrs->refs;
1440 outrefs = 0;
1441 for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
1442 {
1443 ObjectAddress *thisobj = context.addrs->refs + oldref;
1444
1445 if (thisobj->classId == RelationRelationId &&
1446 thisobj->objectId == relId)
1447 {
1448 /* Move this ref into self_addrs */
1449 add_exact_object_address(thisobj, self_addrs);
1450 }
1451 else
1452 {
1453 /* Keep it in context.addrs */
1454 *outobj = *thisobj;
1455 outobj++;
1456 outrefs++;
1457 }
1458 }
1459 context.addrs->numrefs = outrefs;
1460
1461 /* Record the self-dependencies */
1462 recordMultipleDependencies(depender,
1463 self_addrs->refs, self_addrs->numrefs,
1464 self_behavior);
1465
1466 free_object_addresses(self_addrs);
1467 }
1468
1469 /* Record the external dependencies */
1470 recordMultipleDependencies(depender,
1471 context.addrs->refs, context.addrs->numrefs,
1472 behavior);
1473
1474 free_object_addresses(context.addrs);
1475 }
1476
1477 /*
1478 * Recursively search an expression tree for object references.
1479 *
1480 * Note: we avoid creating references to columns of tables that participate
1481 * in an SQL JOIN construct, but are not actually used anywhere in the query.
1482 * To do so, we do not scan the joinaliasvars list of a join RTE while
1483 * scanning the query rangetable, but instead scan each individual entry
1484 * of the alias list when we find a reference to it.
1485 *
1486 * Note: in many cases we do not need to create dependencies on the datatypes
1487 * involved in an expression, because we'll have an indirect dependency via
1488 * some other object. For instance Var nodes depend on a column which depends
1489 * on the datatype, and OpExpr nodes depend on the operator which depends on
1490 * the datatype. However we do need a type dependency if there is no such
1491 * indirect dependency, as for example in Const and CoerceToDomain nodes.
1492 *
1493 * Similarly, we don't need to create dependencies on collations except where
1494 * the collation is being freshly introduced to the expression.
1495 */
1496 static bool
find_expr_references_walker(Node * node,find_expr_references_context * context)1497 find_expr_references_walker(Node *node,
1498 find_expr_references_context *context)
1499 {
1500 if (node == NULL)
1501 return false;
1502 if (IsA(node, Var))
1503 {
1504 Var *var = (Var *) node;
1505 List *rtable;
1506 RangeTblEntry *rte;
1507
1508 /* Find matching rtable entry, or complain if not found */
1509 if (var->varlevelsup >= list_length(context->rtables))
1510 elog(ERROR, "invalid varlevelsup %d", var->varlevelsup);
1511 rtable = (List *) list_nth(context->rtables, var->varlevelsup);
1512 if (var->varno <= 0 || var->varno > list_length(rtable))
1513 elog(ERROR, "invalid varno %d", var->varno);
1514 rte = rt_fetch(var->varno, rtable);
1515
1516 /*
1517 * A whole-row Var references no specific columns, so adds no new
1518 * dependency. (We assume that there is a whole-table dependency
1519 * arising from each underlying rangetable entry. While we could
1520 * record such a dependency when finding a whole-row Var that
1521 * references a relation directly, it's quite unclear how to extend
1522 * that to whole-row Vars for JOINs, so it seems better to leave the
1523 * responsibility with the range table. Note that this poses some
1524 * risks for identifying dependencies of stand-alone expressions:
1525 * whole-table references may need to be created separately.)
1526 */
1527 if (var->varattno == InvalidAttrNumber)
1528 return false;
1529 if (rte->rtekind == RTE_RELATION)
1530 {
1531 /* If it's a plain relation, reference this column */
1532 add_object_address(OCLASS_CLASS, rte->relid, var->varattno,
1533 context->addrs);
1534 }
1535 else if (rte->rtekind == RTE_JOIN)
1536 {
1537 /* Scan join output column to add references to join inputs */
1538 List *save_rtables;
1539
1540 /* We must make the context appropriate for join's level */
1541 save_rtables = context->rtables;
1542 context->rtables = list_copy_tail(context->rtables,
1543 var->varlevelsup);
1544 if (var->varattno <= 0 ||
1545 var->varattno > list_length(rte->joinaliasvars))
1546 elog(ERROR, "invalid varattno %d", var->varattno);
1547 find_expr_references_walker((Node *) list_nth(rte->joinaliasvars,
1548 var->varattno - 1),
1549 context);
1550 list_free(context->rtables);
1551 context->rtables = save_rtables;
1552 }
1553 return false;
1554 }
1555 else if (IsA(node, Const))
1556 {
1557 Const *con = (Const *) node;
1558 Oid objoid;
1559
1560 /* A constant must depend on the constant's datatype */
1561 add_object_address(OCLASS_TYPE, con->consttype, 0,
1562 context->addrs);
1563
1564 /*
1565 * We must also depend on the constant's collation: it could be
1566 * different from the datatype's, if a CollateExpr was const-folded to
1567 * a simple constant. However we can save work in the most common
1568 * case where the collation is "default", since we know that's pinned.
1569 */
1570 if (OidIsValid(con->constcollid) &&
1571 con->constcollid != DEFAULT_COLLATION_OID)
1572 add_object_address(OCLASS_COLLATION, con->constcollid, 0,
1573 context->addrs);
1574
1575 /*
1576 * If it's a regclass or similar literal referring to an existing
1577 * object, add a reference to that object. (Currently, only the
1578 * regclass and regconfig cases have any likely use, but we may as
1579 * well handle all the OID-alias datatypes consistently.)
1580 */
1581 if (!con->constisnull)
1582 {
1583 switch (con->consttype)
1584 {
1585 case REGPROCOID:
1586 case REGPROCEDUREOID:
1587 objoid = DatumGetObjectId(con->constvalue);
1588 if (SearchSysCacheExists1(PROCOID,
1589 ObjectIdGetDatum(objoid)))
1590 add_object_address(OCLASS_PROC, objoid, 0,
1591 context->addrs);
1592 break;
1593 case REGOPEROID:
1594 case REGOPERATOROID:
1595 objoid = DatumGetObjectId(con->constvalue);
1596 if (SearchSysCacheExists1(OPEROID,
1597 ObjectIdGetDatum(objoid)))
1598 add_object_address(OCLASS_OPERATOR, objoid, 0,
1599 context->addrs);
1600 break;
1601 case REGCLASSOID:
1602 objoid = DatumGetObjectId(con->constvalue);
1603 if (SearchSysCacheExists1(RELOID,
1604 ObjectIdGetDatum(objoid)))
1605 add_object_address(OCLASS_CLASS, objoid, 0,
1606 context->addrs);
1607 break;
1608 case REGTYPEOID:
1609 objoid = DatumGetObjectId(con->constvalue);
1610 if (SearchSysCacheExists1(TYPEOID,
1611 ObjectIdGetDatum(objoid)))
1612 add_object_address(OCLASS_TYPE, objoid, 0,
1613 context->addrs);
1614 break;
1615 case REGCONFIGOID:
1616 objoid = DatumGetObjectId(con->constvalue);
1617 if (SearchSysCacheExists1(TSCONFIGOID,
1618 ObjectIdGetDatum(objoid)))
1619 add_object_address(OCLASS_TSCONFIG, objoid, 0,
1620 context->addrs);
1621 break;
1622 case REGDICTIONARYOID:
1623 objoid = DatumGetObjectId(con->constvalue);
1624 if (SearchSysCacheExists1(TSDICTOID,
1625 ObjectIdGetDatum(objoid)))
1626 add_object_address(OCLASS_TSDICT, objoid, 0,
1627 context->addrs);
1628 break;
1629
1630 case REGNAMESPACEOID:
1631 objoid = DatumGetObjectId(con->constvalue);
1632 if (SearchSysCacheExists1(NAMESPACEOID,
1633 ObjectIdGetDatum(objoid)))
1634 add_object_address(OCLASS_SCHEMA, objoid, 0,
1635 context->addrs);
1636 break;
1637
1638 /*
1639 * Dependencies for regrole should be shared among all
1640 * databases, so explicitly inhibit to have dependencies.
1641 */
1642 case REGROLEOID:
1643 ereport(ERROR,
1644 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1645 errmsg("constant of the type \"regrole\" cannot be used here")));
1646 break;
1647 }
1648 }
1649 return false;
1650 }
1651 else if (IsA(node, Param))
1652 {
1653 Param *param = (Param *) node;
1654
1655 /* A parameter must depend on the parameter's datatype */
1656 add_object_address(OCLASS_TYPE, param->paramtype, 0,
1657 context->addrs);
1658 /* and its collation, just as for Consts */
1659 if (OidIsValid(param->paramcollid) &&
1660 param->paramcollid != DEFAULT_COLLATION_OID)
1661 add_object_address(OCLASS_COLLATION, param->paramcollid, 0,
1662 context->addrs);
1663 }
1664 else if (IsA(node, FuncExpr))
1665 {
1666 FuncExpr *funcexpr = (FuncExpr *) node;
1667
1668 add_object_address(OCLASS_PROC, funcexpr->funcid, 0,
1669 context->addrs);
1670 /* fall through to examine arguments */
1671 }
1672 else if (IsA(node, OpExpr))
1673 {
1674 OpExpr *opexpr = (OpExpr *) node;
1675
1676 add_object_address(OCLASS_OPERATOR, opexpr->opno, 0,
1677 context->addrs);
1678 /* fall through to examine arguments */
1679 }
1680 else if (IsA(node, DistinctExpr))
1681 {
1682 DistinctExpr *distinctexpr = (DistinctExpr *) node;
1683
1684 add_object_address(OCLASS_OPERATOR, distinctexpr->opno, 0,
1685 context->addrs);
1686 /* fall through to examine arguments */
1687 }
1688 else if (IsA(node, NullIfExpr))
1689 {
1690 NullIfExpr *nullifexpr = (NullIfExpr *) node;
1691
1692 add_object_address(OCLASS_OPERATOR, nullifexpr->opno, 0,
1693 context->addrs);
1694 /* fall through to examine arguments */
1695 }
1696 else if (IsA(node, ScalarArrayOpExpr))
1697 {
1698 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
1699
1700 add_object_address(OCLASS_OPERATOR, opexpr->opno, 0,
1701 context->addrs);
1702 /* fall through to examine arguments */
1703 }
1704 else if (IsA(node, Aggref))
1705 {
1706 Aggref *aggref = (Aggref *) node;
1707
1708 add_object_address(OCLASS_PROC, aggref->aggfnoid, 0,
1709 context->addrs);
1710 /* fall through to examine arguments */
1711 }
1712 else if (IsA(node, WindowFunc))
1713 {
1714 WindowFunc *wfunc = (WindowFunc *) node;
1715
1716 add_object_address(OCLASS_PROC, wfunc->winfnoid, 0,
1717 context->addrs);
1718 /* fall through to examine arguments */
1719 }
1720 else if (IsA(node, SubPlan))
1721 {
1722 /* Extra work needed here if we ever need this case */
1723 elog(ERROR, "already-planned subqueries not supported");
1724 }
1725 else if (IsA(node, FieldSelect))
1726 {
1727 FieldSelect *fselect = (FieldSelect *) node;
1728 Oid argtype = exprType((Node *) fselect->arg);
1729 Oid reltype = get_typ_typrelid(argtype);
1730
1731 /*
1732 * We need a dependency on the specific column named in FieldSelect,
1733 * assuming we can identify the pg_class OID for it. (Probably we
1734 * always can at the moment, but in future it might be possible for
1735 * argtype to be RECORDOID.) If we can make a column dependency then
1736 * we shouldn't need a dependency on the column's type; but if we
1737 * can't, make a dependency on the type, as it might not appear
1738 * anywhere else in the expression.
1739 */
1740 if (OidIsValid(reltype))
1741 add_object_address(OCLASS_CLASS, reltype, fselect->fieldnum,
1742 context->addrs);
1743 else
1744 add_object_address(OCLASS_TYPE, fselect->resulttype, 0,
1745 context->addrs);
1746 /* the collation might not be referenced anywhere else, either */
1747 if (OidIsValid(fselect->resultcollid) &&
1748 fselect->resultcollid != DEFAULT_COLLATION_OID)
1749 add_object_address(OCLASS_COLLATION, fselect->resultcollid, 0,
1750 context->addrs);
1751 }
1752 else if (IsA(node, FieldStore))
1753 {
1754 FieldStore *fstore = (FieldStore *) node;
1755 Oid reltype = get_typ_typrelid(fstore->resulttype);
1756
1757 /* similar considerations to FieldSelect, but multiple column(s) */
1758 if (OidIsValid(reltype))
1759 {
1760 ListCell *l;
1761
1762 foreach(l, fstore->fieldnums)
1763 add_object_address(OCLASS_CLASS, reltype, lfirst_int(l),
1764 context->addrs);
1765 }
1766 else
1767 add_object_address(OCLASS_TYPE, fstore->resulttype, 0,
1768 context->addrs);
1769 }
1770 else if (IsA(node, RelabelType))
1771 {
1772 RelabelType *relab = (RelabelType *) node;
1773
1774 /* since there is no function dependency, need to depend on type */
1775 add_object_address(OCLASS_TYPE, relab->resulttype, 0,
1776 context->addrs);
1777 /* the collation might not be referenced anywhere else, either */
1778 if (OidIsValid(relab->resultcollid) &&
1779 relab->resultcollid != DEFAULT_COLLATION_OID)
1780 add_object_address(OCLASS_COLLATION, relab->resultcollid, 0,
1781 context->addrs);
1782 }
1783 else if (IsA(node, CoerceViaIO))
1784 {
1785 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
1786
1787 /* since there is no exposed function, need to depend on type */
1788 add_object_address(OCLASS_TYPE, iocoerce->resulttype, 0,
1789 context->addrs);
1790 /* the collation might not be referenced anywhere else, either */
1791 if (OidIsValid(iocoerce->resultcollid) &&
1792 iocoerce->resultcollid != DEFAULT_COLLATION_OID)
1793 add_object_address(OCLASS_COLLATION, iocoerce->resultcollid, 0,
1794 context->addrs);
1795 }
1796 else if (IsA(node, ArrayCoerceExpr))
1797 {
1798 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
1799
1800 if (OidIsValid(acoerce->elemfuncid))
1801 add_object_address(OCLASS_PROC, acoerce->elemfuncid, 0,
1802 context->addrs);
1803 add_object_address(OCLASS_TYPE, acoerce->resulttype, 0,
1804 context->addrs);
1805 /* fall through to examine arguments */
1806 }
1807 else if (IsA(node, ConvertRowtypeExpr))
1808 {
1809 ConvertRowtypeExpr *cvt = (ConvertRowtypeExpr *) node;
1810
1811 /* since there is no function dependency, need to depend on type */
1812 add_object_address(OCLASS_TYPE, cvt->resulttype, 0,
1813 context->addrs);
1814 }
1815 else if (IsA(node, CollateExpr))
1816 {
1817 CollateExpr *coll = (CollateExpr *) node;
1818
1819 add_object_address(OCLASS_COLLATION, coll->collOid, 0,
1820 context->addrs);
1821 }
1822 else if (IsA(node, RowExpr))
1823 {
1824 RowExpr *rowexpr = (RowExpr *) node;
1825
1826 add_object_address(OCLASS_TYPE, rowexpr->row_typeid, 0,
1827 context->addrs);
1828 }
1829 else if (IsA(node, RowCompareExpr))
1830 {
1831 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1832 ListCell *l;
1833
1834 foreach(l, rcexpr->opnos)
1835 {
1836 add_object_address(OCLASS_OPERATOR, lfirst_oid(l), 0,
1837 context->addrs);
1838 }
1839 foreach(l, rcexpr->opfamilies)
1840 {
1841 add_object_address(OCLASS_OPFAMILY, lfirst_oid(l), 0,
1842 context->addrs);
1843 }
1844 /* fall through to examine arguments */
1845 }
1846 else if (IsA(node, CoerceToDomain))
1847 {
1848 CoerceToDomain *cd = (CoerceToDomain *) node;
1849
1850 add_object_address(OCLASS_TYPE, cd->resulttype, 0,
1851 context->addrs);
1852 }
1853 else if (IsA(node, OnConflictExpr))
1854 {
1855 OnConflictExpr *onconflict = (OnConflictExpr *) node;
1856
1857 if (OidIsValid(onconflict->constraint))
1858 add_object_address(OCLASS_CONSTRAINT, onconflict->constraint, 0,
1859 context->addrs);
1860 /* fall through to examine arguments */
1861 }
1862 else if (IsA(node, SortGroupClause))
1863 {
1864 SortGroupClause *sgc = (SortGroupClause *) node;
1865
1866 add_object_address(OCLASS_OPERATOR, sgc->eqop, 0,
1867 context->addrs);
1868 if (OidIsValid(sgc->sortop))
1869 add_object_address(OCLASS_OPERATOR, sgc->sortop, 0,
1870 context->addrs);
1871 return false;
1872 }
1873 else if (IsA(node, Query))
1874 {
1875 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1876 Query *query = (Query *) node;
1877 ListCell *lc;
1878 bool result;
1879
1880 /*
1881 * Add whole-relation refs for each plain relation mentioned in the
1882 * subquery's rtable.
1883 *
1884 * Note: query_tree_walker takes care of recursing into RTE_FUNCTION
1885 * RTEs, subqueries, etc, so no need to do that here. But keep it
1886 * from looking at join alias lists.
1887 *
1888 * Note: we don't need to worry about collations mentioned in
1889 * RTE_VALUES or RTE_CTE RTEs, because those must just duplicate
1890 * collations referenced in other parts of the Query. We do have to
1891 * worry about collations mentioned in RTE_FUNCTION, but we take care
1892 * of those when we recurse to the RangeTblFunction node(s).
1893 */
1894 foreach(lc, query->rtable)
1895 {
1896 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
1897
1898 switch (rte->rtekind)
1899 {
1900 case RTE_RELATION:
1901 add_object_address(OCLASS_CLASS, rte->relid, 0,
1902 context->addrs);
1903 break;
1904 default:
1905 break;
1906 }
1907 }
1908
1909 /*
1910 * If the query is an INSERT or UPDATE, we should create a dependency
1911 * on each target column, to prevent the specific target column from
1912 * being dropped. Although we will visit the TargetEntry nodes again
1913 * during query_tree_walker, we won't have enough context to do this
1914 * conveniently, so do it here.
1915 */
1916 if (query->commandType == CMD_INSERT ||
1917 query->commandType == CMD_UPDATE)
1918 {
1919 RangeTblEntry *rte;
1920
1921 if (query->resultRelation <= 0 ||
1922 query->resultRelation > list_length(query->rtable))
1923 elog(ERROR, "invalid resultRelation %d",
1924 query->resultRelation);
1925 rte = rt_fetch(query->resultRelation, query->rtable);
1926 if (rte->rtekind == RTE_RELATION)
1927 {
1928 foreach(lc, query->targetList)
1929 {
1930 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1931
1932 if (tle->resjunk)
1933 continue; /* ignore junk tlist items */
1934 add_object_address(OCLASS_CLASS, rte->relid, tle->resno,
1935 context->addrs);
1936 }
1937 }
1938 }
1939
1940 /*
1941 * Add dependencies on constraints listed in query's constraintDeps
1942 */
1943 foreach(lc, query->constraintDeps)
1944 {
1945 add_object_address(OCLASS_CONSTRAINT, lfirst_oid(lc), 0,
1946 context->addrs);
1947 }
1948
1949 /* Examine substructure of query */
1950 context->rtables = lcons(query->rtable, context->rtables);
1951 result = query_tree_walker(query,
1952 find_expr_references_walker,
1953 (void *) context,
1954 QTW_IGNORE_JOINALIASES |
1955 QTW_EXAMINE_SORTGROUP);
1956 context->rtables = list_delete_first(context->rtables);
1957 return result;
1958 }
1959 else if (IsA(node, SetOperationStmt))
1960 {
1961 SetOperationStmt *setop = (SetOperationStmt *) node;
1962
1963 /* we need to look at the groupClauses for operator references */
1964 find_expr_references_walker((Node *) setop->groupClauses, context);
1965 /* fall through to examine child nodes */
1966 }
1967 else if (IsA(node, RangeTblFunction))
1968 {
1969 RangeTblFunction *rtfunc = (RangeTblFunction *) node;
1970 ListCell *ct;
1971
1972 /*
1973 * Add refs for any datatypes and collations used in a column
1974 * definition list for a RECORD function. (For other cases, it should
1975 * be enough to depend on the function itself.)
1976 */
1977 foreach(ct, rtfunc->funccoltypes)
1978 {
1979 add_object_address(OCLASS_TYPE, lfirst_oid(ct), 0,
1980 context->addrs);
1981 }
1982 foreach(ct, rtfunc->funccolcollations)
1983 {
1984 Oid collid = lfirst_oid(ct);
1985
1986 if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
1987 add_object_address(OCLASS_COLLATION, collid, 0,
1988 context->addrs);
1989 }
1990 }
1991 else if (IsA(node, TableSampleClause))
1992 {
1993 TableSampleClause *tsc = (TableSampleClause *) node;
1994
1995 add_object_address(OCLASS_PROC, tsc->tsmhandler, 0,
1996 context->addrs);
1997 /* fall through to examine arguments */
1998 }
1999
2000 return expression_tree_walker(node, find_expr_references_walker,
2001 (void *) context);
2002 }
2003
2004 /*
2005 * Given an array of dependency references, eliminate any duplicates.
2006 */
2007 static void
eliminate_duplicate_dependencies(ObjectAddresses * addrs)2008 eliminate_duplicate_dependencies(ObjectAddresses *addrs)
2009 {
2010 ObjectAddress *priorobj;
2011 int oldref,
2012 newrefs;
2013
2014 /*
2015 * We can't sort if the array has "extra" data, because there's no way to
2016 * keep it in sync. Fortunately that combination of features is not
2017 * needed.
2018 */
2019 Assert(!addrs->extras);
2020
2021 if (addrs->numrefs <= 1)
2022 return; /* nothing to do */
2023
2024 /* Sort the refs so that duplicates are adjacent */
2025 qsort((void *) addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
2026 object_address_comparator);
2027
2028 /* Remove dups */
2029 priorobj = addrs->refs;
2030 newrefs = 1;
2031 for (oldref = 1; oldref < addrs->numrefs; oldref++)
2032 {
2033 ObjectAddress *thisobj = addrs->refs + oldref;
2034
2035 if (priorobj->classId == thisobj->classId &&
2036 priorobj->objectId == thisobj->objectId)
2037 {
2038 if (priorobj->objectSubId == thisobj->objectSubId)
2039 continue; /* identical, so drop thisobj */
2040
2041 /*
2042 * If we have a whole-object reference and a reference to a part
2043 * of the same object, we don't need the whole-object reference
2044 * (for example, we don't need to reference both table foo and
2045 * column foo.bar). The whole-object reference will always appear
2046 * first in the sorted list.
2047 */
2048 if (priorobj->objectSubId == 0)
2049 {
2050 /* replace whole ref with partial */
2051 priorobj->objectSubId = thisobj->objectSubId;
2052 continue;
2053 }
2054 }
2055 /* Not identical, so add thisobj to output set */
2056 priorobj++;
2057 *priorobj = *thisobj;
2058 newrefs++;
2059 }
2060
2061 addrs->numrefs = newrefs;
2062 }
2063
2064 /*
2065 * qsort comparator for ObjectAddress items
2066 */
2067 static int
object_address_comparator(const void * a,const void * b)2068 object_address_comparator(const void *a, const void *b)
2069 {
2070 const ObjectAddress *obja = (const ObjectAddress *) a;
2071 const ObjectAddress *objb = (const ObjectAddress *) b;
2072
2073 if (obja->classId < objb->classId)
2074 return -1;
2075 if (obja->classId > objb->classId)
2076 return 1;
2077 if (obja->objectId < objb->objectId)
2078 return -1;
2079 if (obja->objectId > objb->objectId)
2080 return 1;
2081
2082 /*
2083 * We sort the subId as an unsigned int so that 0 will come first. See
2084 * logic in eliminate_duplicate_dependencies.
2085 */
2086 if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
2087 return -1;
2088 if ((unsigned int) obja->objectSubId > (unsigned int) objb->objectSubId)
2089 return 1;
2090 return 0;
2091 }
2092
2093 /*
2094 * Routines for handling an expansible array of ObjectAddress items.
2095 *
2096 * new_object_addresses: create a new ObjectAddresses array.
2097 */
2098 ObjectAddresses *
new_object_addresses(void)2099 new_object_addresses(void)
2100 {
2101 ObjectAddresses *addrs;
2102
2103 addrs = palloc(sizeof(ObjectAddresses));
2104
2105 addrs->numrefs = 0;
2106 addrs->maxrefs = 32;
2107 addrs->refs = (ObjectAddress *)
2108 palloc(addrs->maxrefs * sizeof(ObjectAddress));
2109 addrs->extras = NULL; /* until/unless needed */
2110
2111 return addrs;
2112 }
2113
2114 /*
2115 * Add an entry to an ObjectAddresses array.
2116 *
2117 * It is convenient to specify the class by ObjectClass rather than directly
2118 * by catalog OID.
2119 */
2120 static void
add_object_address(ObjectClass oclass,Oid objectId,int32 subId,ObjectAddresses * addrs)2121 add_object_address(ObjectClass oclass, Oid objectId, int32 subId,
2122 ObjectAddresses *addrs)
2123 {
2124 ObjectAddress *item;
2125
2126 /*
2127 * Make sure object_classes is kept up to date with the ObjectClass enum.
2128 */
2129 StaticAssertStmt(lengthof(object_classes) == LAST_OCLASS + 1,
2130 "object_classes[] must cover all ObjectClasses");
2131
2132 /* enlarge array if needed */
2133 if (addrs->numrefs >= addrs->maxrefs)
2134 {
2135 addrs->maxrefs *= 2;
2136 addrs->refs = (ObjectAddress *)
2137 repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2138 Assert(!addrs->extras);
2139 }
2140 /* record this item */
2141 item = addrs->refs + addrs->numrefs;
2142 item->classId = object_classes[oclass];
2143 item->objectId = objectId;
2144 item->objectSubId = subId;
2145 addrs->numrefs++;
2146 }
2147
2148 /*
2149 * Add an entry to an ObjectAddresses array.
2150 *
2151 * As above, but specify entry exactly.
2152 */
2153 void
add_exact_object_address(const ObjectAddress * object,ObjectAddresses * addrs)2154 add_exact_object_address(const ObjectAddress *object,
2155 ObjectAddresses *addrs)
2156 {
2157 ObjectAddress *item;
2158
2159 /* enlarge array if needed */
2160 if (addrs->numrefs >= addrs->maxrefs)
2161 {
2162 addrs->maxrefs *= 2;
2163 addrs->refs = (ObjectAddress *)
2164 repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2165 Assert(!addrs->extras);
2166 }
2167 /* record this item */
2168 item = addrs->refs + addrs->numrefs;
2169 *item = *object;
2170 addrs->numrefs++;
2171 }
2172
2173 /*
2174 * Add an entry to an ObjectAddresses array.
2175 *
2176 * As above, but specify entry exactly and provide some "extra" data too.
2177 */
2178 static void
add_exact_object_address_extra(const ObjectAddress * object,const ObjectAddressExtra * extra,ObjectAddresses * addrs)2179 add_exact_object_address_extra(const ObjectAddress *object,
2180 const ObjectAddressExtra *extra,
2181 ObjectAddresses *addrs)
2182 {
2183 ObjectAddress *item;
2184 ObjectAddressExtra *itemextra;
2185
2186 /* allocate extra space if first time */
2187 if (!addrs->extras)
2188 addrs->extras = (ObjectAddressExtra *)
2189 palloc(addrs->maxrefs * sizeof(ObjectAddressExtra));
2190
2191 /* enlarge array if needed */
2192 if (addrs->numrefs >= addrs->maxrefs)
2193 {
2194 addrs->maxrefs *= 2;
2195 addrs->refs = (ObjectAddress *)
2196 repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2197 addrs->extras = (ObjectAddressExtra *)
2198 repalloc(addrs->extras, addrs->maxrefs * sizeof(ObjectAddressExtra));
2199 }
2200 /* record this item */
2201 item = addrs->refs + addrs->numrefs;
2202 *item = *object;
2203 itemextra = addrs->extras + addrs->numrefs;
2204 *itemextra = *extra;
2205 addrs->numrefs++;
2206 }
2207
2208 /*
2209 * Test whether an object is present in an ObjectAddresses array.
2210 *
2211 * We return "true" if object is a subobject of something in the array, too.
2212 */
2213 bool
object_address_present(const ObjectAddress * object,const ObjectAddresses * addrs)2214 object_address_present(const ObjectAddress *object,
2215 const ObjectAddresses *addrs)
2216 {
2217 int i;
2218
2219 for (i = addrs->numrefs - 1; i >= 0; i--)
2220 {
2221 const ObjectAddress *thisobj = addrs->refs + i;
2222
2223 if (object->classId == thisobj->classId &&
2224 object->objectId == thisobj->objectId)
2225 {
2226 if (object->objectSubId == thisobj->objectSubId ||
2227 thisobj->objectSubId == 0)
2228 return true;
2229 }
2230 }
2231
2232 return false;
2233 }
2234
2235 /*
2236 * As above, except that if the object is present then also OR the given
2237 * flags into its associated extra data (which must exist).
2238 */
2239 static bool
object_address_present_add_flags(const ObjectAddress * object,int flags,ObjectAddresses * addrs)2240 object_address_present_add_flags(const ObjectAddress *object,
2241 int flags,
2242 ObjectAddresses *addrs)
2243 {
2244 bool result = false;
2245 int i;
2246
2247 for (i = addrs->numrefs - 1; i >= 0; i--)
2248 {
2249 ObjectAddress *thisobj = addrs->refs + i;
2250
2251 if (object->classId == thisobj->classId &&
2252 object->objectId == thisobj->objectId)
2253 {
2254 if (object->objectSubId == thisobj->objectSubId)
2255 {
2256 ObjectAddressExtra *thisextra = addrs->extras + i;
2257
2258 thisextra->flags |= flags;
2259 result = true;
2260 }
2261 else if (thisobj->objectSubId == 0)
2262 {
2263 /*
2264 * We get here if we find a need to delete a column after
2265 * having already decided to drop its whole table. Obviously
2266 * we no longer need to drop the subobject, so report that we
2267 * found the subobject in the array. But don't plaster its
2268 * flags on the whole object.
2269 */
2270 result = true;
2271 }
2272 else if (object->objectSubId == 0)
2273 {
2274 /*
2275 * We get here if we find a need to delete a whole table after
2276 * having already decided to drop one of its columns. We
2277 * can't report that the whole object is in the array, but we
2278 * should mark the subobject with the whole object's flags.
2279 *
2280 * It might seem attractive to physically delete the column's
2281 * array entry, or at least mark it as no longer needing
2282 * separate deletion. But that could lead to, e.g., dropping
2283 * the column's datatype before we drop the table, which does
2284 * not seem like a good idea. This is a very rare situation
2285 * in practice, so we just take the hit of doing a separate
2286 * DROP COLUMN action even though we know we're gonna delete
2287 * the table later.
2288 *
2289 * Because there could be other subobjects of this object in
2290 * the array, this case means we always have to loop through
2291 * the whole array; we cannot exit early on a match.
2292 */
2293 ObjectAddressExtra *thisextra = addrs->extras + i;
2294
2295 thisextra->flags |= flags;
2296 }
2297 }
2298 }
2299
2300 return result;
2301 }
2302
2303 /*
2304 * Similar to above, except we search an ObjectAddressStack.
2305 */
2306 static bool
stack_address_present_add_flags(const ObjectAddress * object,int flags,ObjectAddressStack * stack)2307 stack_address_present_add_flags(const ObjectAddress *object,
2308 int flags,
2309 ObjectAddressStack *stack)
2310 {
2311 bool result = false;
2312 ObjectAddressStack *stackptr;
2313
2314 for (stackptr = stack; stackptr; stackptr = stackptr->next)
2315 {
2316 const ObjectAddress *thisobj = stackptr->object;
2317
2318 if (object->classId == thisobj->classId &&
2319 object->objectId == thisobj->objectId)
2320 {
2321 if (object->objectSubId == thisobj->objectSubId)
2322 {
2323 stackptr->flags |= flags;
2324 result = true;
2325 }
2326 else if (thisobj->objectSubId == 0)
2327 {
2328 /*
2329 * We're visiting a column with whole table already on stack.
2330 * As in object_address_present_add_flags(), we can skip
2331 * further processing of the subobject, but we don't want to
2332 * propagate flags for the subobject to the whole object.
2333 */
2334 result = true;
2335 }
2336 else if (object->objectSubId == 0)
2337 {
2338 /*
2339 * We're visiting a table with column already on stack. As in
2340 * object_address_present_add_flags(), we should propagate
2341 * flags for the whole object to each of its subobjects.
2342 */
2343 stackptr->flags |= flags;
2344 }
2345 }
2346 }
2347
2348 return result;
2349 }
2350
2351 /*
2352 * Record multiple dependencies from an ObjectAddresses array, after first
2353 * removing any duplicates.
2354 */
2355 void
record_object_address_dependencies(const ObjectAddress * depender,ObjectAddresses * referenced,DependencyType behavior)2356 record_object_address_dependencies(const ObjectAddress *depender,
2357 ObjectAddresses *referenced,
2358 DependencyType behavior)
2359 {
2360 eliminate_duplicate_dependencies(referenced);
2361 recordMultipleDependencies(depender,
2362 referenced->refs, referenced->numrefs,
2363 behavior);
2364 }
2365
2366 /*
2367 * Clean up when done with an ObjectAddresses array.
2368 */
2369 void
free_object_addresses(ObjectAddresses * addrs)2370 free_object_addresses(ObjectAddresses *addrs)
2371 {
2372 pfree(addrs->refs);
2373 if (addrs->extras)
2374 pfree(addrs->extras);
2375 pfree(addrs);
2376 }
2377
2378 /*
2379 * Determine the class of a given object identified by objectAddress.
2380 *
2381 * This function is essentially the reverse mapping for the object_classes[]
2382 * table. We implement it as a function because the OIDs aren't consecutive.
2383 */
2384 ObjectClass
getObjectClass(const ObjectAddress * object)2385 getObjectClass(const ObjectAddress *object)
2386 {
2387 /* only pg_class entries can have nonzero objectSubId */
2388 if (object->classId != RelationRelationId &&
2389 object->objectSubId != 0)
2390 elog(ERROR, "invalid non-zero objectSubId for object class %u",
2391 object->classId);
2392
2393 switch (object->classId)
2394 {
2395 case RelationRelationId:
2396 /* caller must check objectSubId */
2397 return OCLASS_CLASS;
2398
2399 case ProcedureRelationId:
2400 return OCLASS_PROC;
2401
2402 case TypeRelationId:
2403 return OCLASS_TYPE;
2404
2405 case CastRelationId:
2406 return OCLASS_CAST;
2407
2408 case CollationRelationId:
2409 return OCLASS_COLLATION;
2410
2411 case ConstraintRelationId:
2412 return OCLASS_CONSTRAINT;
2413
2414 case ConversionRelationId:
2415 return OCLASS_CONVERSION;
2416
2417 case AttrDefaultRelationId:
2418 return OCLASS_DEFAULT;
2419
2420 case LanguageRelationId:
2421 return OCLASS_LANGUAGE;
2422
2423 case LargeObjectRelationId:
2424 return OCLASS_LARGEOBJECT;
2425
2426 case OperatorRelationId:
2427 return OCLASS_OPERATOR;
2428
2429 case OperatorClassRelationId:
2430 return OCLASS_OPCLASS;
2431
2432 case OperatorFamilyRelationId:
2433 return OCLASS_OPFAMILY;
2434
2435 case AccessMethodRelationId:
2436 return OCLASS_AM;
2437
2438 case AccessMethodOperatorRelationId:
2439 return OCLASS_AMOP;
2440
2441 case AccessMethodProcedureRelationId:
2442 return OCLASS_AMPROC;
2443
2444 case RewriteRelationId:
2445 return OCLASS_REWRITE;
2446
2447 case TriggerRelationId:
2448 return OCLASS_TRIGGER;
2449
2450 case NamespaceRelationId:
2451 return OCLASS_SCHEMA;
2452
2453 case TSParserRelationId:
2454 return OCLASS_TSPARSER;
2455
2456 case TSDictionaryRelationId:
2457 return OCLASS_TSDICT;
2458
2459 case TSTemplateRelationId:
2460 return OCLASS_TSTEMPLATE;
2461
2462 case TSConfigRelationId:
2463 return OCLASS_TSCONFIG;
2464
2465 case AuthIdRelationId:
2466 return OCLASS_ROLE;
2467
2468 case DatabaseRelationId:
2469 return OCLASS_DATABASE;
2470
2471 case TableSpaceRelationId:
2472 return OCLASS_TBLSPACE;
2473
2474 case ForeignDataWrapperRelationId:
2475 return OCLASS_FDW;
2476
2477 case ForeignServerRelationId:
2478 return OCLASS_FOREIGN_SERVER;
2479
2480 case UserMappingRelationId:
2481 return OCLASS_USER_MAPPING;
2482
2483 case DefaultAclRelationId:
2484 return OCLASS_DEFACL;
2485
2486 case ExtensionRelationId:
2487 return OCLASS_EXTENSION;
2488
2489 case EventTriggerRelationId:
2490 return OCLASS_EVENT_TRIGGER;
2491
2492 case PolicyRelationId:
2493 return OCLASS_POLICY;
2494
2495 case TransformRelationId:
2496 return OCLASS_TRANSFORM;
2497 }
2498
2499 /* shouldn't get here */
2500 elog(ERROR, "unrecognized object class: %u", object->classId);
2501 return OCLASS_CLASS; /* keep compiler quiet */
2502 }
2503
2504 /*
2505 * delete initial ACL for extension objects
2506 */
2507 static void
DeleteInitPrivs(const ObjectAddress * object)2508 DeleteInitPrivs(const ObjectAddress *object)
2509 {
2510 Relation relation;
2511 ScanKeyData key[3];
2512 SysScanDesc scan;
2513 HeapTuple oldtuple;
2514
2515 relation = heap_open(InitPrivsRelationId, RowExclusiveLock);
2516
2517 ScanKeyInit(&key[0],
2518 Anum_pg_init_privs_objoid,
2519 BTEqualStrategyNumber, F_OIDEQ,
2520 ObjectIdGetDatum(object->objectId));
2521 ScanKeyInit(&key[1],
2522 Anum_pg_init_privs_classoid,
2523 BTEqualStrategyNumber, F_OIDEQ,
2524 ObjectIdGetDatum(object->classId));
2525 ScanKeyInit(&key[2],
2526 Anum_pg_init_privs_objsubid,
2527 BTEqualStrategyNumber, F_INT4EQ,
2528 Int32GetDatum(object->objectSubId));
2529
2530 scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
2531 NULL, 3, key);
2532
2533 while (HeapTupleIsValid(oldtuple = systable_getnext(scan)))
2534 simple_heap_delete(relation, &oldtuple->t_self);
2535
2536 systable_endscan(scan);
2537
2538 heap_close(relation, RowExclusiveLock);
2539 }
2540