]> granicus.if.org Git - postgresql/blob - src/backend/catalog/dependency.c
40591fd368097099e70a324d656eac859851a985
[postgresql] / src / backend / catalog / dependency.c
1 /*-------------------------------------------------------------------------
2  *
3  * dependency.c
4  *        Routines to support inter-object dependencies.
5  *
6  *
7  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/catalog/dependency.c,v 1.64 2007/02/14 01:58:56 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "access/genam.h"
18 #include "access/heapam.h"
19 #include "access/xact.h"
20 #include "catalog/dependency.h"
21 #include "catalog/heap.h"
22 #include "catalog/index.h"
23 #include "catalog/indexing.h"
24 #include "catalog/namespace.h"
25 #include "catalog/pg_amop.h"
26 #include "catalog/pg_amproc.h"
27 #include "catalog/pg_attrdef.h"
28 #include "catalog/pg_authid.h"
29 #include "catalog/pg_cast.h"
30 #include "catalog/pg_constraint.h"
31 #include "catalog/pg_conversion.h"
32 #include "catalog/pg_database.h"
33 #include "catalog/pg_depend.h"
34 #include "catalog/pg_language.h"
35 #include "catalog/pg_namespace.h"
36 #include "catalog/pg_opclass.h"
37 #include "catalog/pg_operator.h"
38 #include "catalog/pg_opfamily.h"
39 #include "catalog/pg_proc.h"
40 #include "catalog/pg_rewrite.h"
41 #include "catalog/pg_tablespace.h"
42 #include "catalog/pg_trigger.h"
43 #include "catalog/pg_type.h"
44 #include "commands/comment.h"
45 #include "commands/dbcommands.h"
46 #include "commands/defrem.h"
47 #include "commands/proclang.h"
48 #include "commands/schemacmds.h"
49 #include "commands/tablespace.h"
50 #include "commands/trigger.h"
51 #include "commands/typecmds.h"
52 #include "miscadmin.h"
53 #include "optimizer/clauses.h"
54 #include "parser/parsetree.h"
55 #include "rewrite/rewriteRemove.h"
56 #include "utils/builtins.h"
57 #include "utils/fmgroids.h"
58 #include "utils/lsyscache.h"
59 #include "utils/syscache.h"
60
61
62 /* expansible list of ObjectAddresses */
63 struct ObjectAddresses
64 {
65         ObjectAddress *refs;            /* => palloc'd array */
66         int                     numrefs;                /* current number of references */
67         int                     maxrefs;                /* current size of palloc'd array */
68 };
69
70 /* typedef ObjectAddresses appears in dependency.h */
71
72 /* for find_expr_references_walker */
73 typedef struct
74 {
75         ObjectAddresses *addrs;         /* addresses being accumulated */
76         List       *rtables;            /* list of rangetables to resolve Vars */
77 } find_expr_references_context;
78
79 /*
80  * This constant table maps ObjectClasses to the corresponding catalog OIDs.
81  * See also getObjectClass().
82  */
83 static const Oid object_classes[MAX_OCLASS] = {
84         RelationRelationId,                                     /* OCLASS_CLASS */
85         ProcedureRelationId,                            /* OCLASS_PROC */
86         TypeRelationId,                                         /* OCLASS_TYPE */
87         CastRelationId,                                         /* OCLASS_CAST */
88         ConstraintRelationId,                           /* OCLASS_CONSTRAINT */
89         ConversionRelationId,                           /* OCLASS_CONVERSION */
90         AttrDefaultRelationId,                          /* OCLASS_DEFAULT */
91         LanguageRelationId,                                     /* OCLASS_LANGUAGE */
92         OperatorRelationId,                                     /* OCLASS_OPERATOR */
93         OperatorClassRelationId,                        /* OCLASS_OPCLASS */
94         OperatorFamilyRelationId,                       /* OCLASS_OPFAMILY */
95         AccessMethodOperatorRelationId,         /* OCLASS_AMOP */
96         AccessMethodProcedureRelationId,        /* OCLASS_AMPROC */
97         RewriteRelationId,                                      /* OCLASS_REWRITE */
98         TriggerRelationId,                                      /* OCLASS_TRIGGER */
99         NamespaceRelationId,                            /* OCLASS_SCHEMA */
100         AuthIdRelationId,                                       /* OCLASS_ROLE */
101         DatabaseRelationId,                                     /* OCLASS_DATABASE */
102         TableSpaceRelationId                            /* OCLASS_TBLSPACE */
103 };
104
105
106 static void performDeletionWithList(const ObjectAddress *object,
107                                                 ObjectAddresses *oktodelete,
108                                                 DropBehavior behavior,
109                                                 ObjectAddresses *alreadyDeleted);
110 static void findAutoDeletableObjects(const ObjectAddress *object,
111                                                  ObjectAddresses *oktodelete,
112                                                  Relation depRel, bool addself);
113 static bool recursiveDeletion(const ObjectAddress *object,
114                                   DropBehavior behavior,
115                                   int msglevel,
116                                   const ObjectAddress *callingObject,
117                                   ObjectAddresses *oktodelete,
118                                   Relation depRel,
119                                   ObjectAddresses *alreadyDeleted);
120 static bool deleteDependentObjects(const ObjectAddress *object,
121                                            const char *objDescription,
122                                            DropBehavior behavior,
123                                            int msglevel,
124                                            ObjectAddresses *oktodelete,
125                                            Relation depRel);
126 static void doDeletion(const ObjectAddress *object);
127 static bool find_expr_references_walker(Node *node,
128                                                         find_expr_references_context *context);
129 static void eliminate_duplicate_dependencies(ObjectAddresses *addrs);
130 static int      object_address_comparator(const void *a, const void *b);
131 static void add_object_address(ObjectClass oclass, Oid objectId, int32 subId,
132                                    ObjectAddresses *addrs);
133 static void getRelationDescription(StringInfo buffer, Oid relid);
134 static void getOpFamilyDescription(StringInfo buffer, Oid opfid);
135
136
137 /*
138  * performDeletion: attempt to drop the specified object.  If CASCADE
139  * behavior is specified, also drop any dependent objects (recursively).
140  * If RESTRICT behavior is specified, error out if there are any dependent
141  * objects, except for those that should be implicitly dropped anyway
142  * according to the dependency type.
143  *
144  * This is the outer control routine for all forms of DROP that drop objects
145  * that can participate in dependencies.
146  */
147 void
148 performDeletion(const ObjectAddress *object,
149                                 DropBehavior behavior)
150 {
151         char       *objDescription;
152         Relation        depRel;
153         ObjectAddresses *oktodelete;
154
155         /*
156          * Get object description for possible use in failure message. Must do
157          * this before deleting it ...
158          */
159         objDescription = getObjectDescription(object);
160
161         /*
162          * We save some cycles by opening pg_depend just once and passing the
163          * Relation pointer down to all the recursive deletion steps.
164          */
165         depRel = heap_open(DependRelationId, RowExclusiveLock);
166
167         /*
168          * Construct a list of objects that are reachable by AUTO or INTERNAL
169          * dependencies from the target object.  These should be deleted silently,
170          * even if the actual deletion pass first reaches one of them via a
171          * non-auto dependency.
172          */
173         oktodelete = new_object_addresses();
174
175         findAutoDeletableObjects(object, oktodelete, depRel, true);
176
177         if (!recursiveDeletion(object, behavior, NOTICE,
178                                                    NULL, oktodelete, depRel, NULL))
179                 ereport(ERROR,
180                                 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
181                                  errmsg("cannot drop %s because other objects depend on it",
182                                                 objDescription),
183                 errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
184
185         free_object_addresses(oktodelete);
186
187         heap_close(depRel, RowExclusiveLock);
188
189         pfree(objDescription);
190 }
191
192
193 /*
194  * performDeletionWithList: As above, but the oktodelete list may have already
195  * filled with some objects.  Also, the deleted objects are saved in the
196  * alreadyDeleted list.
197  *
198  * XXX performDeletion could be refactored to be a thin wrapper around this
199  * function.
200  */
201 static void
202 performDeletionWithList(const ObjectAddress *object,
203                                                 ObjectAddresses *oktodelete,
204                                                 DropBehavior behavior,
205                                                 ObjectAddresses *alreadyDeleted)
206 {
207         char       *objDescription;
208         Relation        depRel;
209
210         /*
211          * Get object description for possible use in failure message. Must do
212          * this before deleting it ...
213          */
214         objDescription = getObjectDescription(object);
215
216         /*
217          * We save some cycles by opening pg_depend just once and passing the
218          * Relation pointer down to all the recursive deletion steps.
219          */
220         depRel = heap_open(DependRelationId, RowExclusiveLock);
221
222         /*
223          * Construct a list of objects that are reachable by AUTO or INTERNAL
224          * dependencies from the target object.  These should be deleted silently,
225          * even if the actual deletion pass first reaches one of them via a
226          * non-auto dependency.
227          */
228         findAutoDeletableObjects(object, oktodelete, depRel, true);
229
230         if (!recursiveDeletion(object, behavior, NOTICE,
231                                                    NULL, oktodelete, depRel, alreadyDeleted))
232                 ereport(ERROR,
233                                 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
234                                  errmsg("cannot drop %s because other objects depend on it",
235                                                 objDescription),
236                 errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
237
238         heap_close(depRel, RowExclusiveLock);
239
240         pfree(objDescription);
241 }
242
243 /*
244  * performMultipleDeletion: Similar to performDeletion, but act on multiple
245  * objects at once.
246  *
247  * The main difference from issuing multiple performDeletion calls is that the
248  * list of objects that would be implicitly dropped, for each object to be
249  * dropped, is the union of the implicit-object list for all objects.  This
250  * makes each check be more relaxed.
251  */
252 void
253 performMultipleDeletions(const ObjectAddresses *objects,
254                                                  DropBehavior behavior)
255 {
256         ObjectAddresses *implicit;
257         ObjectAddresses *alreadyDeleted;
258         Relation        depRel;
259         int                     i;
260
261         implicit = new_object_addresses();
262         alreadyDeleted = new_object_addresses();
263
264         depRel = heap_open(DependRelationId, RowExclusiveLock);
265
266         /*
267          * Get the list of all objects that would be deleted after deleting the
268          * whole "objects" list.  We do this by creating a list of all implicit
269          * (INTERNAL and AUTO) dependencies for each object we collected above.
270          * Note that we must exclude the objects themselves from this list!
271          */
272         for (i = 0; i < objects->numrefs; i++)
273         {
274                 ObjectAddress obj = objects->refs[i];
275
276                 /*
277                  * If it's in the implicit list, we don't need to delete it explicitly
278                  * nor follow the dependencies, because that was already done in a
279                  * previous iteration.
280                  */
281                 if (object_address_present(&obj, implicit))
282                         continue;
283
284                 /*
285                  * Add the objects dependent on this one to the global list of
286                  * implicit objects.
287                  */
288                 findAutoDeletableObjects(&obj, implicit, depRel, false);
289         }
290
291         /* Do the deletion. */
292         for (i = 0; i < objects->numrefs; i++)
293         {
294                 ObjectAddress obj = objects->refs[i];
295
296                 /*
297                  * Skip this object if it was already deleted in a previous iteration.
298                  */
299                 if (object_address_present(&obj, alreadyDeleted))
300                         continue;
301
302                 /*
303                  * Skip this object if it's also present in the list of implicit
304                  * objects --- it will be deleted later.
305                  */
306                 if (object_address_present(&obj, implicit))
307                         continue;
308
309                 /* delete it */
310                 performDeletionWithList(&obj, implicit, behavior, alreadyDeleted);
311         }
312
313         heap_close(depRel, RowExclusiveLock);
314
315         free_object_addresses(implicit);
316         free_object_addresses(alreadyDeleted);
317 }
318
319 /*
320  * deleteWhatDependsOn: attempt to drop everything that depends on the
321  * specified object, though not the object itself.      Behavior is always
322  * CASCADE.
323  *
324  * This is currently used only to clean out the contents of a schema
325  * (namespace): the passed object is a namespace.  We normally want this
326  * to be done silently, so there's an option to suppress NOTICE messages.
327  */
328 void
329 deleteWhatDependsOn(const ObjectAddress *object,
330                                         bool showNotices)
331 {
332         char       *objDescription;
333         Relation        depRel;
334         ObjectAddresses *oktodelete;
335
336         /*
337          * Get object description for possible use in failure messages
338          */
339         objDescription = getObjectDescription(object);
340
341         /*
342          * We save some cycles by opening pg_depend just once and passing the
343          * Relation pointer down to all the recursive deletion steps.
344          */
345         depRel = heap_open(DependRelationId, RowExclusiveLock);
346
347         /*
348          * Construct a list of objects that are reachable by AUTO or INTERNAL
349          * dependencies from the target object.  These should be deleted silently,
350          * even if the actual deletion pass first reaches one of them via a
351          * non-auto dependency.
352          */
353         oktodelete = new_object_addresses();
354
355         findAutoDeletableObjects(object, oktodelete, depRel, true);
356
357         /*
358          * Now invoke only step 2 of recursiveDeletion: just recurse to the stuff
359          * dependent on the given object.
360          */
361         if (!deleteDependentObjects(object, objDescription,
362                                                                 DROP_CASCADE,
363                                                                 showNotices ? NOTICE : DEBUG2,
364                                                                 oktodelete, depRel))
365                 ereport(ERROR,
366                                 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
367                                  errmsg("failed to drop all objects depending on %s",
368                                                 objDescription)));
369
370         /*
371          * We do not need CommandCounterIncrement here, since if step 2 did
372          * anything then each recursive call will have ended with one.
373          */
374
375         free_object_addresses(oktodelete);
376
377         heap_close(depRel, RowExclusiveLock);
378
379         pfree(objDescription);
380 }
381
382
383 /*
384  * findAutoDeletableObjects: find all objects that are reachable by AUTO or
385  * INTERNAL dependency paths from the given object.  Add them all to the
386  * oktodelete list.  If addself is true, the originally given object will also
387  * be added to the list.
388  *
389  * depRel is the already-open pg_depend relation.
390  */
391 static void
392 findAutoDeletableObjects(const ObjectAddress *object,
393                                                  ObjectAddresses *oktodelete,
394                                                  Relation depRel, bool addself)
395 {
396         ScanKeyData key[3];
397         int                     nkeys;
398         SysScanDesc scan;
399         HeapTuple       tup;
400         ObjectAddress otherObject;
401
402         /*
403          * If this object is already in oktodelete, then we already visited it;
404          * don't do so again (this prevents infinite recursion if there's a loop
405          * in pg_depend).  Otherwise, add it.
406          */
407         if (object_address_present(object, oktodelete))
408                 return;
409         if (addself)
410                 add_exact_object_address(object, oktodelete);
411
412         /*
413          * Scan pg_depend records that link to this object, showing the things
414          * that depend on it.  For each one that is AUTO or INTERNAL, visit the
415          * referencing object.
416          *
417          * When dropping a whole object (subId = 0), find pg_depend records for
418          * its sub-objects too.
419          */
420         ScanKeyInit(&key[0],
421                                 Anum_pg_depend_refclassid,
422                                 BTEqualStrategyNumber, F_OIDEQ,
423                                 ObjectIdGetDatum(object->classId));
424         ScanKeyInit(&key[1],
425                                 Anum_pg_depend_refobjid,
426                                 BTEqualStrategyNumber, F_OIDEQ,
427                                 ObjectIdGetDatum(object->objectId));
428         if (object->objectSubId != 0)
429         {
430                 ScanKeyInit(&key[2],
431                                         Anum_pg_depend_refobjsubid,
432                                         BTEqualStrategyNumber, F_INT4EQ,
433                                         Int32GetDatum(object->objectSubId));
434                 nkeys = 3;
435         }
436         else
437                 nkeys = 2;
438
439         scan = systable_beginscan(depRel, DependReferenceIndexId, true,
440                                                           SnapshotNow, nkeys, key);
441
442         while (HeapTupleIsValid(tup = systable_getnext(scan)))
443         {
444                 Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
445
446                 switch (foundDep->deptype)
447                 {
448                         case DEPENDENCY_NORMAL:
449                                 /* ignore */
450                                 break;
451                         case DEPENDENCY_AUTO:
452                         case DEPENDENCY_INTERNAL:
453                                 /* recurse */
454                                 otherObject.classId = foundDep->classid;
455                                 otherObject.objectId = foundDep->objid;
456                                 otherObject.objectSubId = foundDep->objsubid;
457                                 findAutoDeletableObjects(&otherObject, oktodelete, depRel, true);
458                                 break;
459                         case DEPENDENCY_PIN:
460
461                                 /*
462                                  * For a PIN dependency we just ereport immediately; there
463                                  * won't be any others to examine, and we aren't ever going to
464                                  * let the user delete it.
465                                  */
466                                 ereport(ERROR,
467                                                 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
468                                                  errmsg("cannot drop %s because it is required by the database system",
469                                                                 getObjectDescription(object))));
470                                 break;
471                         default:
472                                 elog(ERROR, "unrecognized dependency type '%c' for %s",
473                                          foundDep->deptype, getObjectDescription(object));
474                                 break;
475                 }
476         }
477
478         systable_endscan(scan);
479 }
480
481
482 /*
483  * recursiveDeletion: delete a single object for performDeletion, plus
484  * (recursively) anything that depends on it.
485  *
486  * Returns TRUE if successful, FALSE if not.
487  *
488  * callingObject is NULL at the outer level, else identifies the object that
489  * we recursed from (the reference object that someone else needs to delete).
490  *
491  * oktodelete is a list of objects verified deletable (ie, reachable by one
492  * or more AUTO or INTERNAL dependencies from the original target).
493  *
494  * depRel is the already-open pg_depend relation.
495  *
496  *
497  * In RESTRICT mode, we perform all the deletions anyway, but ereport a message
498  * and return FALSE if we find a restriction violation.  performDeletion
499  * will then abort the transaction to nullify the deletions.  We have to
500  * do it this way to (a) report all the direct and indirect dependencies
501  * while (b) not going into infinite recursion if there's a cycle.
502  *
503  * This is even more complex than one could wish, because it is possible for
504  * the same pair of objects to be related by both NORMAL and AUTO/INTERNAL
505  * dependencies.  Also, we might have a situation where we've been asked to
506  * delete object A, and objects B and C both have AUTO dependencies on A,
507  * but B also has a NORMAL dependency on C.  (Since any of these paths might
508  * be indirect, we can't prevent these scenarios, but must cope instead.)
509  * If we visit C before B then we would mistakenly decide that the B->C link
510  * should prevent the restricted drop from occurring.  To handle this, we make
511  * a pre-scan to find all the objects that are auto-deletable from A.  If we
512  * visit C first, but B is present in the oktodelete list, then we make no
513  * complaint but recurse to delete B anyway.  (Note that in general we must
514  * delete B before deleting C; the drop routine for B may try to access C.)
515  *
516  * Note: in the case where the path to B is traversed first, we will not
517  * see the NORMAL dependency when we reach C, because of the pg_depend
518  * removals done in step 1.  The oktodelete list is necessary just
519  * to make the behavior independent of the order in which pg_depend
520  * entries are visited.
521  */
522 static bool
523 recursiveDeletion(const ObjectAddress *object,
524                                   DropBehavior behavior,
525                                   int msglevel,
526                                   const ObjectAddress *callingObject,
527                                   ObjectAddresses *oktodelete,
528                                   Relation depRel,
529                                   ObjectAddresses *alreadyDeleted)
530 {
531         bool            ok = true;
532         char       *objDescription;
533         ScanKeyData key[3];
534         int                     nkeys;
535         SysScanDesc scan;
536         HeapTuple       tup;
537         ObjectAddress otherObject;
538         ObjectAddress owningObject;
539         bool            amOwned = false;
540
541         /*
542          * Get object description for possible use in messages.  Must do this
543          * before deleting it ...
544          */
545         objDescription = getObjectDescription(object);
546
547         /*
548          * Step 1: find and remove pg_depend records that link from this object to
549          * others.      We have to do this anyway, and doing it first ensures that we
550          * avoid infinite recursion in the case of cycles. Also, some dependency
551          * types require extra processing here.
552          *
553          * When dropping a whole object (subId = 0), remove all pg_depend records
554          * for its sub-objects too.
555          */
556         ScanKeyInit(&key[0],
557                                 Anum_pg_depend_classid,
558                                 BTEqualStrategyNumber, F_OIDEQ,
559                                 ObjectIdGetDatum(object->classId));
560         ScanKeyInit(&key[1],
561                                 Anum_pg_depend_objid,
562                                 BTEqualStrategyNumber, F_OIDEQ,
563                                 ObjectIdGetDatum(object->objectId));
564         if (object->objectSubId != 0)
565         {
566                 ScanKeyInit(&key[2],
567                                         Anum_pg_depend_objsubid,
568                                         BTEqualStrategyNumber, F_INT4EQ,
569                                         Int32GetDatum(object->objectSubId));
570                 nkeys = 3;
571         }
572         else
573                 nkeys = 2;
574
575         scan = systable_beginscan(depRel, DependDependerIndexId, true,
576                                                           SnapshotNow, nkeys, key);
577
578         while (HeapTupleIsValid(tup = systable_getnext(scan)))
579         {
580                 Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
581
582                 otherObject.classId = foundDep->refclassid;
583                 otherObject.objectId = foundDep->refobjid;
584                 otherObject.objectSubId = foundDep->refobjsubid;
585
586                 switch (foundDep->deptype)
587                 {
588                         case DEPENDENCY_NORMAL:
589                         case DEPENDENCY_AUTO:
590                                 /* no problem */
591                                 break;
592                         case DEPENDENCY_INTERNAL:
593
594                                 /*
595                                  * This object is part of the internal implementation of
596                                  * another object.      We have three cases:
597                                  *
598                                  * 1. At the outermost recursion level, disallow the DROP. (We
599                                  * just ereport here, rather than proceeding, since no other
600                                  * dependencies are likely to be interesting.)
601                                  */
602                                 if (callingObject == NULL)
603                                 {
604                                         char       *otherObjDesc = getObjectDescription(&otherObject);
605
606                                         ereport(ERROR,
607                                                         (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
608                                                          errmsg("cannot drop %s because %s requires it",
609                                                                         objDescription, otherObjDesc),
610                                                          errhint("You can drop %s instead.",
611                                                                          otherObjDesc)));
612                                 }
613
614                                 /*
615                                  * 2. When recursing from the other end of this dependency,
616                                  * it's okay to continue with the deletion. This holds when
617                                  * recursing from a whole object that includes the nominal
618                                  * other end as a component, too.
619                                  */
620                                 if (callingObject->classId == otherObject.classId &&
621                                         callingObject->objectId == otherObject.objectId &&
622                                         (callingObject->objectSubId == otherObject.objectSubId ||
623                                          callingObject->objectSubId == 0))
624                                         break;
625
626                                 /*
627                                  * 3. When recursing from anyplace else, transform this
628                                  * deletion request into a delete of the other object. (This
629                                  * will be an error condition iff RESTRICT mode.) In this case
630                                  * we finish deleting my dependencies except for the INTERNAL
631                                  * link, which will be needed to cause the owning object to
632                                  * recurse back to me.
633                                  */
634                                 if (amOwned)    /* shouldn't happen */
635                                         elog(ERROR, "multiple INTERNAL dependencies for %s",
636                                                  objDescription);
637                                 owningObject = otherObject;
638                                 amOwned = true;
639                                 /* "continue" bypasses the simple_heap_delete call below */
640                                 continue;
641                         case DEPENDENCY_PIN:
642
643                                 /*
644                                  * Should not happen; PIN dependencies should have zeroes in
645                                  * the depender fields...
646                                  */
647                                 elog(ERROR, "incorrect use of PIN dependency with %s",
648                                          objDescription);
649                                 break;
650                         default:
651                                 elog(ERROR, "unrecognized dependency type '%c' for %s",
652                                          foundDep->deptype, objDescription);
653                                 break;
654                 }
655
656                 /* delete the pg_depend tuple */
657                 simple_heap_delete(depRel, &tup->t_self);
658         }
659
660         systable_endscan(scan);
661
662         /*
663          * CommandCounterIncrement here to ensure that preceding changes are all
664          * visible; in particular, that the above deletions of pg_depend entries
665          * are visible.  That prevents infinite recursion in case of a dependency
666          * loop (which is perfectly legal).
667          */
668         CommandCounterIncrement();
669
670         /*
671          * If we found we are owned by another object, ask it to delete itself
672          * instead of proceeding.  Complain if RESTRICT mode, unless the other
673          * object is in oktodelete.
674          */
675         if (amOwned)
676         {
677                 if (object_address_present(&owningObject, oktodelete))
678                         ereport(DEBUG2,
679                                         (errmsg("drop auto-cascades to %s",
680                                                         getObjectDescription(&owningObject))));
681                 else if (behavior == DROP_RESTRICT)
682                 {
683                         ereport(msglevel,
684                                         (errmsg("%s depends on %s",
685                                                         getObjectDescription(&owningObject),
686                                                         objDescription)));
687                         ok = false;
688                 }
689                 else
690                         ereport(msglevel,
691                                         (errmsg("drop cascades to %s",
692                                                         getObjectDescription(&owningObject))));
693
694                 if (!recursiveDeletion(&owningObject, behavior, msglevel,
695                                                            object, oktodelete, depRel, alreadyDeleted))
696                         ok = false;
697
698                 pfree(objDescription);
699
700                 return ok;
701         }
702
703         /*
704          * Step 2: scan pg_depend records that link to this object, showing the
705          * things that depend on it.  Recursively delete those things. Note it's
706          * important to delete the dependent objects before the referenced one,
707          * since the deletion routines might do things like try to update the
708          * pg_class record when deleting a check constraint.
709          */
710         if (!deleteDependentObjects(object, objDescription,
711                                                                 behavior, msglevel,
712                                                                 oktodelete, depRel))
713                 ok = false;
714
715         /*
716          * We do not need CommandCounterIncrement here, since if step 2 did
717          * anything then each recursive call will have ended with one.
718          */
719
720         /*
721          * Step 3: delete the object itself, and save it to the list of deleted
722          * objects if appropiate.
723          */
724         doDeletion(object);
725         if (alreadyDeleted != NULL)
726         {
727                 if (!object_address_present(object, alreadyDeleted))
728                         add_exact_object_address(object, alreadyDeleted);
729         }
730
731         /*
732          * Delete any comments associated with this object.  (This is a convenient
733          * place to do it instead of having every object type know to do it.)
734          */
735         DeleteComments(object->objectId, object->classId, object->objectSubId);
736
737         /*
738          * Delete shared dependency references related to this object. Sub-objects
739          * (columns) don't have dependencies on global objects, so skip them.
740          */
741         if (object->objectSubId == 0)
742                 deleteSharedDependencyRecordsFor(object->classId, object->objectId);
743
744         /*
745          * CommandCounterIncrement here to ensure that preceding changes are all
746          * visible.
747          */
748         CommandCounterIncrement();
749
750         /*
751          * And we're done!
752          */
753         pfree(objDescription);
754
755         return ok;
756 }
757
758
759 /*
760  * deleteDependentObjects - find and delete objects that depend on 'object'
761  *
762  * Scan pg_depend records that link to the given object, showing
763  * the things that depend on it.  Recursively delete those things. (We
764  * don't delete the pg_depend records here, as the recursive call will
765  * do that.)  Note it's important to delete the dependent objects
766  * before the referenced one, since the deletion routines might do
767  * things like try to update the pg_class record when deleting a check
768  * constraint.
769  *
770  * When dropping a whole object (subId = 0), find pg_depend records for
771  * its sub-objects too.
772  *
773  *      object: the object to find dependencies on
774  *      objDescription: description of object (only used for error messages)
775  *      behavior: desired drop behavior
776  *      oktodelete: stuff that's AUTO-deletable
777  *      depRel: already opened pg_depend relation
778  *
779  * Returns TRUE if all is well, false if any problem found.
780  *
781  * NOTE: because we are using SnapshotNow, if a recursive call deletes
782  * any pg_depend tuples that our scan hasn't yet visited, we will not
783  * see them as good when we do visit them.      This is essential for
784  * correct behavior if there are multiple dependency paths between two
785  * objects --- else we might try to delete an already-deleted object.
786  */
787 static bool
788 deleteDependentObjects(const ObjectAddress *object,
789                                            const char *objDescription,
790                                            DropBehavior behavior,
791                                            int msglevel,
792                                            ObjectAddresses *oktodelete,
793                                            Relation depRel)
794 {
795         bool            ok = true;
796         ScanKeyData key[3];
797         int                     nkeys;
798         SysScanDesc scan;
799         HeapTuple       tup;
800         ObjectAddress otherObject;
801
802         ScanKeyInit(&key[0],
803                                 Anum_pg_depend_refclassid,
804                                 BTEqualStrategyNumber, F_OIDEQ,
805                                 ObjectIdGetDatum(object->classId));
806         ScanKeyInit(&key[1],
807                                 Anum_pg_depend_refobjid,
808                                 BTEqualStrategyNumber, F_OIDEQ,
809                                 ObjectIdGetDatum(object->objectId));
810         if (object->objectSubId != 0)
811         {
812                 ScanKeyInit(&key[2],
813                                         Anum_pg_depend_refobjsubid,
814                                         BTEqualStrategyNumber, F_INT4EQ,
815                                         Int32GetDatum(object->objectSubId));
816                 nkeys = 3;
817         }
818         else
819                 nkeys = 2;
820
821         scan = systable_beginscan(depRel, DependReferenceIndexId, true,
822                                                           SnapshotNow, nkeys, key);
823
824         while (HeapTupleIsValid(tup = systable_getnext(scan)))
825         {
826                 Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
827
828                 otherObject.classId = foundDep->classid;
829                 otherObject.objectId = foundDep->objid;
830                 otherObject.objectSubId = foundDep->objsubid;
831
832                 switch (foundDep->deptype)
833                 {
834                         case DEPENDENCY_NORMAL:
835
836                                 /*
837                                  * Perhaps there was another dependency path that would have
838                                  * allowed silent deletion of the otherObject, had we only
839                                  * taken that path first. In that case, act like this link is
840                                  * AUTO, too.
841                                  */
842                                 if (object_address_present(&otherObject, oktodelete))
843                                         ereport(DEBUG2,
844                                                         (errmsg("drop auto-cascades to %s",
845                                                                         getObjectDescription(&otherObject))));
846                                 else if (behavior == DROP_RESTRICT)
847                                 {
848                                         ereport(msglevel,
849                                                         (errmsg("%s depends on %s",
850                                                                         getObjectDescription(&otherObject),
851                                                                         objDescription)));
852                                         ok = false;
853                                 }
854                                 else
855                                         ereport(msglevel,
856                                                         (errmsg("drop cascades to %s",
857                                                                         getObjectDescription(&otherObject))));
858
859                                 if (!recursiveDeletion(&otherObject, behavior, msglevel,
860                                                                            object, oktodelete, depRel, NULL))
861                                         ok = false;
862                                 break;
863                         case DEPENDENCY_AUTO:
864                         case DEPENDENCY_INTERNAL:
865
866                                 /*
867                                  * We propagate the DROP without complaint even in the
868                                  * RESTRICT case.  (However, normal dependencies on the
869                                  * component object could still cause failure.)
870                                  */
871                                 ereport(DEBUG2,
872                                                 (errmsg("drop auto-cascades to %s",
873                                                                 getObjectDescription(&otherObject))));
874
875                                 if (!recursiveDeletion(&otherObject, behavior, msglevel,
876                                                                            object, oktodelete, depRel, NULL))
877                                         ok = false;
878                                 break;
879                         case DEPENDENCY_PIN:
880
881                                 /*
882                                  * For a PIN dependency we just ereport immediately; there
883                                  * won't be any others to report.
884                                  */
885                                 ereport(ERROR,
886                                                 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
887                                                  errmsg("cannot drop %s because it is required by the database system",
888                                                                 objDescription)));
889                                 break;
890                         default:
891                                 elog(ERROR, "unrecognized dependency type '%c' for %s",
892                                          foundDep->deptype, objDescription);
893                                 break;
894                 }
895         }
896
897         systable_endscan(scan);
898
899         return ok;
900 }
901
902
903 /*
904  * doDeletion: actually delete a single object
905  */
906 static void
907 doDeletion(const ObjectAddress *object)
908 {
909         switch (getObjectClass(object))
910         {
911                 case OCLASS_CLASS:
912                         {
913                                 char            relKind = get_rel_relkind(object->objectId);
914
915                                 if (relKind == RELKIND_INDEX)
916                                 {
917                                         Assert(object->objectSubId == 0);
918                                         index_drop(object->objectId);
919                                 }
920                                 else
921                                 {
922                                         if (object->objectSubId != 0)
923                                                 RemoveAttributeById(object->objectId,
924                                                                                         object->objectSubId);
925                                         else
926                                                 heap_drop_with_catalog(object->objectId);
927                                 }
928                                 break;
929                         }
930
931                 case OCLASS_PROC:
932                         RemoveFunctionById(object->objectId);
933                         break;
934
935                 case OCLASS_TYPE:
936                         RemoveTypeById(object->objectId);
937                         break;
938
939                 case OCLASS_CAST:
940                         DropCastById(object->objectId);
941                         break;
942
943                 case OCLASS_CONSTRAINT:
944                         RemoveConstraintById(object->objectId);
945                         break;
946
947                 case OCLASS_CONVERSION:
948                         RemoveConversionById(object->objectId);
949                         break;
950
951                 case OCLASS_DEFAULT:
952                         RemoveAttrDefaultById(object->objectId);
953                         break;
954
955                 case OCLASS_LANGUAGE:
956                         DropProceduralLanguageById(object->objectId);
957                         break;
958
959                 case OCLASS_OPERATOR:
960                         RemoveOperatorById(object->objectId);
961                         break;
962
963                 case OCLASS_OPCLASS:
964                         RemoveOpClassById(object->objectId);
965                         break;
966
967                 case OCLASS_OPFAMILY:
968                         RemoveOpFamilyById(object->objectId);
969                         break;
970
971                 case OCLASS_AMOP:
972                         RemoveAmOpEntryById(object->objectId);
973                         break;
974
975                 case OCLASS_AMPROC:
976                         RemoveAmProcEntryById(object->objectId);
977                         break;
978
979                 case OCLASS_REWRITE:
980                         RemoveRewriteRuleById(object->objectId);
981                         break;
982
983                 case OCLASS_TRIGGER:
984                         RemoveTriggerById(object->objectId);
985                         break;
986
987                 case OCLASS_SCHEMA:
988                         RemoveSchemaById(object->objectId);
989                         break;
990
991                 /* OCLASS_ROLE, OCLASS_DATABASE, OCLASS_TBLSPACE not handled */
992
993                 default:
994                         elog(ERROR, "unrecognized object class: %u",
995                                  object->classId);
996         }
997 }
998
999 /*
1000  * recordDependencyOnExpr - find expression dependencies
1001  *
1002  * This is used to find the dependencies of rules, constraint expressions,
1003  * etc.
1004  *
1005  * Given an expression or query in node-tree form, find all the objects
1006  * it refers to (tables, columns, operators, functions, etc).  Record
1007  * a dependency of the specified type from the given depender object
1008  * to each object mentioned in the expression.
1009  *
1010  * rtable is the rangetable to be used to interpret Vars with varlevelsup=0.
1011  * It can be NIL if no such variables are expected.
1012  */
1013 void
1014 recordDependencyOnExpr(const ObjectAddress *depender,
1015                                            Node *expr, List *rtable,
1016                                            DependencyType behavior)
1017 {
1018         find_expr_references_context context;
1019
1020         context.addrs = new_object_addresses();
1021
1022         /* Set up interpretation for Vars at varlevelsup = 0 */
1023         context.rtables = list_make1(rtable);
1024
1025         /* Scan the expression tree for referenceable objects */
1026         find_expr_references_walker(expr, &context);
1027
1028         /* Remove any duplicates */
1029         eliminate_duplicate_dependencies(context.addrs);
1030
1031         /* And record 'em */
1032         recordMultipleDependencies(depender,
1033                                                            context.addrs->refs, context.addrs->numrefs,
1034                                                            behavior);
1035
1036         free_object_addresses(context.addrs);
1037 }
1038
1039 /*
1040  * recordDependencyOnSingleRelExpr - find expression dependencies
1041  *
1042  * As above, but only one relation is expected to be referenced (with
1043  * varno = 1 and varlevelsup = 0).      Pass the relation OID instead of a
1044  * range table.  An additional frammish is that dependencies on that
1045  * relation (or its component columns) will be marked with 'self_behavior',
1046  * whereas 'behavior' is used for everything else.
1047  */
1048 void
1049 recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
1050                                                                 Node *expr, Oid relId,
1051                                                                 DependencyType behavior,
1052                                                                 DependencyType self_behavior)
1053 {
1054         find_expr_references_context context;
1055         RangeTblEntry rte;
1056
1057         context.addrs = new_object_addresses();
1058
1059         /* We gin up a rather bogus rangetable list to handle Vars */
1060         MemSet(&rte, 0, sizeof(rte));
1061         rte.type = T_RangeTblEntry;
1062         rte.rtekind = RTE_RELATION;
1063         rte.relid = relId;
1064
1065         context.rtables = list_make1(list_make1(&rte));
1066
1067         /* Scan the expression tree for referenceable objects */
1068         find_expr_references_walker(expr, &context);
1069
1070         /* Remove any duplicates */
1071         eliminate_duplicate_dependencies(context.addrs);
1072
1073         /* Separate self-dependencies if necessary */
1074         if (behavior != self_behavior && context.addrs->numrefs > 0)
1075         {
1076                 ObjectAddresses *self_addrs;
1077                 ObjectAddress *outobj;
1078                 int                     oldref,
1079                                         outrefs;
1080
1081                 self_addrs = new_object_addresses();
1082
1083                 outobj = context.addrs->refs;
1084                 outrefs = 0;
1085                 for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
1086                 {
1087                         ObjectAddress *thisobj = context.addrs->refs + oldref;
1088
1089                         if (thisobj->classId == RelationRelationId &&
1090                                 thisobj->objectId == relId)
1091                         {
1092                                 /* Move this ref into self_addrs */
1093                                 add_object_address(OCLASS_CLASS, relId, thisobj->objectSubId,
1094                                                                    self_addrs);
1095                         }
1096                         else
1097                         {
1098                                 /* Keep it in context.addrs */
1099                                 outobj->classId = thisobj->classId;
1100                                 outobj->objectId = thisobj->objectId;
1101                                 outobj->objectSubId = thisobj->objectSubId;
1102                                 outobj++;
1103                                 outrefs++;
1104                         }
1105                 }
1106                 context.addrs->numrefs = outrefs;
1107
1108                 /* Record the self-dependencies */
1109                 recordMultipleDependencies(depender,
1110                                                                    self_addrs->refs, self_addrs->numrefs,
1111                                                                    self_behavior);
1112
1113                 free_object_addresses(self_addrs);
1114         }
1115
1116         /* Record the external dependencies */
1117         recordMultipleDependencies(depender,
1118                                                            context.addrs->refs, context.addrs->numrefs,
1119                                                            behavior);
1120
1121         free_object_addresses(context.addrs);
1122 }
1123
1124 /*
1125  * Recursively search an expression tree for object references.
1126  *
1127  * Note: we avoid creating references to columns of tables that participate
1128  * in an SQL JOIN construct, but are not actually used anywhere in the query.
1129  * To do so, we do not scan the joinaliasvars list of a join RTE while
1130  * scanning the query rangetable, but instead scan each individual entry
1131  * of the alias list when we find a reference to it.
1132  *
1133  * Note: in many cases we do not need to create dependencies on the datatypes
1134  * involved in an expression, because we'll have an indirect dependency via
1135  * some other object.  For instance Var nodes depend on a column which depends
1136  * on the datatype, and OpExpr nodes depend on the operator which depends on
1137  * the datatype.  However we do need a type dependency if there is no such
1138  * indirect dependency, as for example in Const and CoerceToDomain nodes.
1139  */
1140 static bool
1141 find_expr_references_walker(Node *node,
1142                                                         find_expr_references_context *context)
1143 {
1144         if (node == NULL)
1145                 return false;
1146         if (IsA(node, Var))
1147         {
1148                 Var                *var = (Var *) node;
1149                 List       *rtable;
1150                 RangeTblEntry *rte;
1151
1152                 /* Find matching rtable entry, or complain if not found */
1153                 if (var->varlevelsup >= list_length(context->rtables))
1154                         elog(ERROR, "invalid varlevelsup %d", var->varlevelsup);
1155                 rtable = (List *) list_nth(context->rtables, var->varlevelsup);
1156                 if (var->varno <= 0 || var->varno > list_length(rtable))
1157                         elog(ERROR, "invalid varno %d", var->varno);
1158                 rte = rt_fetch(var->varno, rtable);
1159
1160                 /*
1161                  * A whole-row Var references no specific columns, so adds no new
1162                  * dependency.
1163                  */
1164                 if (var->varattno == InvalidAttrNumber)
1165                         return false;
1166                 if (rte->rtekind == RTE_RELATION)
1167                 {
1168                         /* If it's a plain relation, reference this column */
1169                         add_object_address(OCLASS_CLASS, rte->relid, var->varattno,
1170                                                            context->addrs);
1171                 }
1172                 else if (rte->rtekind == RTE_JOIN)
1173                 {
1174                         /* Scan join output column to add references to join inputs */
1175                         List       *save_rtables;
1176
1177                         /* We must make the context appropriate for join's level */
1178                         save_rtables = context->rtables;
1179                         context->rtables = list_copy_tail(context->rtables,
1180                                                                                           var->varlevelsup);
1181                         if (var->varattno <= 0 ||
1182                                 var->varattno > list_length(rte->joinaliasvars))
1183                                 elog(ERROR, "invalid varattno %d", var->varattno);
1184                         find_expr_references_walker((Node *) list_nth(rte->joinaliasvars,
1185                                                                                                                   var->varattno - 1),
1186                                                                                 context);
1187                         list_free(context->rtables);
1188                         context->rtables = save_rtables;
1189                 }
1190                 return false;
1191         }
1192         if (IsA(node, Const))
1193         {
1194                 Const      *con = (Const *) node;
1195                 Oid                     objoid;
1196
1197                 /* A constant must depend on the constant's datatype */
1198                 add_object_address(OCLASS_TYPE, con->consttype, 0,
1199                                                    context->addrs);
1200
1201                 /*
1202                  * If it's a regclass or similar literal referring to an existing
1203                  * object, add a reference to that object.      (Currently, only the
1204                  * regclass case has any likely use, but we may as well handle all the
1205                  * OID-alias datatypes consistently.)
1206                  */
1207                 if (!con->constisnull)
1208                 {
1209                         switch (con->consttype)
1210                         {
1211                                 case REGPROCOID:
1212                                 case REGPROCEDUREOID:
1213                                         objoid = DatumGetObjectId(con->constvalue);
1214                                         if (SearchSysCacheExists(PROCOID,
1215                                                                                          ObjectIdGetDatum(objoid),
1216                                                                                          0, 0, 0))
1217                                                 add_object_address(OCLASS_PROC, objoid, 0,
1218                                                                                    context->addrs);
1219                                         break;
1220                                 case REGOPEROID:
1221                                 case REGOPERATOROID:
1222                                         objoid = DatumGetObjectId(con->constvalue);
1223                                         if (SearchSysCacheExists(OPEROID,
1224                                                                                          ObjectIdGetDatum(objoid),
1225                                                                                          0, 0, 0))
1226                                                 add_object_address(OCLASS_OPERATOR, objoid, 0,
1227                                                                                    context->addrs);
1228                                         break;
1229                                 case REGCLASSOID:
1230                                         objoid = DatumGetObjectId(con->constvalue);
1231                                         if (SearchSysCacheExists(RELOID,
1232                                                                                          ObjectIdGetDatum(objoid),
1233                                                                                          0, 0, 0))
1234                                                 add_object_address(OCLASS_CLASS, objoid, 0,
1235                                                                                    context->addrs);
1236                                         break;
1237                                 case REGTYPEOID:
1238                                         objoid = DatumGetObjectId(con->constvalue);
1239                                         if (SearchSysCacheExists(TYPEOID,
1240                                                                                          ObjectIdGetDatum(objoid),
1241                                                                                          0, 0, 0))
1242                                                 add_object_address(OCLASS_TYPE, objoid, 0,
1243                                                                                    context->addrs);
1244                                         break;
1245                         }
1246                 }
1247                 return false;
1248         }
1249         if (IsA(node, Param))
1250         {
1251                 Param      *param = (Param *) node;
1252
1253                 /* A parameter must depend on the parameter's datatype */
1254                 add_object_address(OCLASS_TYPE, param->paramtype, 0,
1255                                                    context->addrs);
1256         }
1257         if (IsA(node, FuncExpr))
1258         {
1259                 FuncExpr   *funcexpr = (FuncExpr *) node;
1260
1261                 add_object_address(OCLASS_PROC, funcexpr->funcid, 0,
1262                                                    context->addrs);
1263                 /* fall through to examine arguments */
1264         }
1265         if (IsA(node, OpExpr))
1266         {
1267                 OpExpr     *opexpr = (OpExpr *) node;
1268
1269                 add_object_address(OCLASS_OPERATOR, opexpr->opno, 0,
1270                                                    context->addrs);
1271                 /* fall through to examine arguments */
1272         }
1273         if (IsA(node, DistinctExpr))
1274         {
1275                 DistinctExpr *distinctexpr = (DistinctExpr *) node;
1276
1277                 add_object_address(OCLASS_OPERATOR, distinctexpr->opno, 0,
1278                                                    context->addrs);
1279                 /* fall through to examine arguments */
1280         }
1281         if (IsA(node, ScalarArrayOpExpr))
1282         {
1283                 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
1284
1285                 add_object_address(OCLASS_OPERATOR, opexpr->opno, 0,
1286                                                    context->addrs);
1287                 /* fall through to examine arguments */
1288         }
1289         if (IsA(node, NullIfExpr))
1290         {
1291                 NullIfExpr *nullifexpr = (NullIfExpr *) node;
1292
1293                 add_object_address(OCLASS_OPERATOR, nullifexpr->opno, 0,
1294                                                    context->addrs);
1295                 /* fall through to examine arguments */
1296         }
1297         if (IsA(node, Aggref))
1298         {
1299                 Aggref     *aggref = (Aggref *) node;
1300
1301                 add_object_address(OCLASS_PROC, aggref->aggfnoid, 0,
1302                                                    context->addrs);
1303                 /* fall through to examine arguments */
1304         }
1305         if (is_subplan(node))
1306         {
1307                 /* Extra work needed here if we ever need this case */
1308                 elog(ERROR, "already-planned subqueries not supported");
1309         }
1310         if (IsA(node, RelabelType))
1311         {
1312                 RelabelType *relab = (RelabelType *) node;
1313
1314                 /* since there is no function dependency, need to depend on type */
1315                 add_object_address(OCLASS_TYPE, relab->resulttype, 0,
1316                                                    context->addrs);
1317         }
1318         if (IsA(node, ConvertRowtypeExpr))
1319         {
1320                 ConvertRowtypeExpr *cvt = (ConvertRowtypeExpr *) node;
1321
1322                 /* since there is no function dependency, need to depend on type */
1323                 add_object_address(OCLASS_TYPE, cvt->resulttype, 0,
1324                                                    context->addrs);
1325         }
1326         if (IsA(node, RowExpr))
1327         {
1328                 RowExpr    *rowexpr = (RowExpr *) node;
1329
1330                 add_object_address(OCLASS_TYPE, rowexpr->row_typeid, 0,
1331                                                    context->addrs);
1332         }
1333         if (IsA(node, RowCompareExpr))
1334         {
1335                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1336                 ListCell   *l;
1337
1338                 foreach(l, rcexpr->opnos)
1339                 {
1340                         add_object_address(OCLASS_OPERATOR, lfirst_oid(l), 0,
1341                                                            context->addrs);
1342                 }
1343                 foreach(l, rcexpr->opfamilies)
1344                 {
1345                         add_object_address(OCLASS_OPFAMILY, lfirst_oid(l), 0,
1346                                                            context->addrs);
1347                 }
1348                 /* fall through to examine arguments */
1349         }
1350         if (IsA(node, CoerceToDomain))
1351         {
1352                 CoerceToDomain *cd = (CoerceToDomain *) node;
1353
1354                 add_object_address(OCLASS_TYPE, cd->resulttype, 0,
1355                                                    context->addrs);
1356         }
1357         if (IsA(node, Query))
1358         {
1359                 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1360                 Query      *query = (Query *) node;
1361                 ListCell   *rtable;
1362                 bool            result;
1363
1364                 /*
1365                  * Add whole-relation refs for each plain relation mentioned in the
1366                  * subquery's rtable, as well as datatype refs for any datatypes used
1367                  * as a RECORD function's output.  (Note: query_tree_walker takes care
1368                  * of recursing into RTE_FUNCTION and RTE_SUBQUERY RTEs, so no need to
1369                  * do that here.  But keep it from looking at join alias lists.)
1370                  */
1371                 foreach(rtable, query->rtable)
1372                 {
1373                         RangeTblEntry *rte = (RangeTblEntry *) lfirst(rtable);
1374                         ListCell   *ct;
1375
1376                         switch (rte->rtekind)
1377                         {
1378                                 case RTE_RELATION:
1379                                         add_object_address(OCLASS_CLASS, rte->relid, 0,
1380                                                                            context->addrs);
1381                                         break;
1382                                 case RTE_FUNCTION:
1383                                         foreach(ct, rte->funccoltypes)
1384                                         {
1385                                                 add_object_address(OCLASS_TYPE, lfirst_oid(ct), 0,
1386                                                                                    context->addrs);
1387                                         }
1388                                         break;
1389                                 default:
1390                                         break;
1391                         }
1392                 }
1393
1394                 /* Examine substructure of query */
1395                 context->rtables = lcons(query->rtable, context->rtables);
1396                 result = query_tree_walker(query,
1397                                                                    find_expr_references_walker,
1398                                                                    (void *) context,
1399                                                                    QTW_IGNORE_JOINALIASES);
1400                 context->rtables = list_delete_first(context->rtables);
1401                 return result;
1402         }
1403         return expression_tree_walker(node, find_expr_references_walker,
1404                                                                   (void *) context);
1405 }
1406
1407 /*
1408  * Given an array of dependency references, eliminate any duplicates.
1409  */
1410 static void
1411 eliminate_duplicate_dependencies(ObjectAddresses *addrs)
1412 {
1413         ObjectAddress *priorobj;
1414         int                     oldref,
1415                                 newrefs;
1416
1417         if (addrs->numrefs <= 1)
1418                 return;                                 /* nothing to do */
1419
1420         /* Sort the refs so that duplicates are adjacent */
1421         qsort((void *) addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
1422                   object_address_comparator);
1423
1424         /* Remove dups */
1425         priorobj = addrs->refs;
1426         newrefs = 1;
1427         for (oldref = 1; oldref < addrs->numrefs; oldref++)
1428         {
1429                 ObjectAddress *thisobj = addrs->refs + oldref;
1430
1431                 if (priorobj->classId == thisobj->classId &&
1432                         priorobj->objectId == thisobj->objectId)
1433                 {
1434                         if (priorobj->objectSubId == thisobj->objectSubId)
1435                                 continue;               /* identical, so drop thisobj */
1436
1437                         /*
1438                          * If we have a whole-object reference and a reference to a part
1439                          * of the same object, we don't need the whole-object reference
1440                          * (for example, we don't need to reference both table foo and
1441                          * column foo.bar).  The whole-object reference will always appear
1442                          * first in the sorted list.
1443                          */
1444                         if (priorobj->objectSubId == 0)
1445                         {
1446                                 /* replace whole ref with partial */
1447                                 priorobj->objectSubId = thisobj->objectSubId;
1448                                 continue;
1449                         }
1450                 }
1451                 /* Not identical, so add thisobj to output set */
1452                 priorobj++;
1453                 priorobj->classId = thisobj->classId;
1454                 priorobj->objectId = thisobj->objectId;
1455                 priorobj->objectSubId = thisobj->objectSubId;
1456                 newrefs++;
1457         }
1458
1459         addrs->numrefs = newrefs;
1460 }
1461
1462 /*
1463  * qsort comparator for ObjectAddress items
1464  */
1465 static int
1466 object_address_comparator(const void *a, const void *b)
1467 {
1468         const ObjectAddress *obja = (const ObjectAddress *) a;
1469         const ObjectAddress *objb = (const ObjectAddress *) b;
1470
1471         if (obja->classId < objb->classId)
1472                 return -1;
1473         if (obja->classId > objb->classId)
1474                 return 1;
1475         if (obja->objectId < objb->objectId)
1476                 return -1;
1477         if (obja->objectId > objb->objectId)
1478                 return 1;
1479
1480         /*
1481          * We sort the subId as an unsigned int so that 0 will come first. See
1482          * logic in eliminate_duplicate_dependencies.
1483          */
1484         if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
1485                 return -1;
1486         if ((unsigned int) obja->objectSubId > (unsigned int) objb->objectSubId)
1487                 return 1;
1488         return 0;
1489 }
1490
1491 /*
1492  * Routines for handling an expansible array of ObjectAddress items.
1493  *
1494  * new_object_addresses: create a new ObjectAddresses array.
1495  */
1496 ObjectAddresses *
1497 new_object_addresses(void)
1498 {
1499         ObjectAddresses *addrs;
1500
1501         addrs = palloc(sizeof(ObjectAddresses));
1502
1503         addrs->numrefs = 0;
1504         addrs->maxrefs = 32;
1505         addrs->refs = (ObjectAddress *)
1506                 palloc(addrs->maxrefs * sizeof(ObjectAddress));
1507
1508         return addrs;
1509 }
1510
1511 /*
1512  * Add an entry to an ObjectAddresses array.
1513  *
1514  * It is convenient to specify the class by ObjectClass rather than directly
1515  * by catalog OID.
1516  */
1517 static void
1518 add_object_address(ObjectClass oclass, Oid objectId, int32 subId,
1519                                    ObjectAddresses *addrs)
1520 {
1521         ObjectAddress *item;
1522
1523         /* enlarge array if needed */
1524         if (addrs->numrefs >= addrs->maxrefs)
1525         {
1526                 addrs->maxrefs *= 2;
1527                 addrs->refs = (ObjectAddress *)
1528                         repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
1529         }
1530         /* record this item */
1531         item = addrs->refs + addrs->numrefs;
1532         item->classId = object_classes[oclass];
1533         item->objectId = objectId;
1534         item->objectSubId = subId;
1535         addrs->numrefs++;
1536 }
1537
1538 /*
1539  * Add an entry to an ObjectAddresses array.
1540  *
1541  * As above, but specify entry exactly.
1542  */
1543 void
1544 add_exact_object_address(const ObjectAddress *object,
1545                                                  ObjectAddresses *addrs)
1546 {
1547         ObjectAddress *item;
1548
1549         /* enlarge array if needed */
1550         if (addrs->numrefs >= addrs->maxrefs)
1551         {
1552                 addrs->maxrefs *= 2;
1553                 addrs->refs = (ObjectAddress *)
1554                         repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
1555         }
1556         /* record this item */
1557         item = addrs->refs + addrs->numrefs;
1558         *item = *object;
1559         addrs->numrefs++;
1560 }
1561
1562 /*
1563  * Test whether an object is present in an ObjectAddresses array.
1564  *
1565  * We return "true" if object is a subobject of something in the array, too.
1566  */
1567 bool
1568 object_address_present(const ObjectAddress *object,
1569                                            ObjectAddresses *addrs)
1570 {
1571         int                     i;
1572
1573         for (i = addrs->numrefs - 1; i >= 0; i--)
1574         {
1575                 ObjectAddress *thisobj = addrs->refs + i;
1576
1577                 if (object->classId == thisobj->classId &&
1578                         object->objectId == thisobj->objectId)
1579                 {
1580                         if (object->objectSubId == thisobj->objectSubId ||
1581                                 thisobj->objectSubId == 0)
1582                                 return true;
1583                 }
1584         }
1585
1586         return false;
1587 }
1588
1589 /*
1590  * Clean up when done with an ObjectAddresses array.
1591  */
1592 void
1593 free_object_addresses(ObjectAddresses *addrs)
1594 {
1595         pfree(addrs->refs);
1596         pfree(addrs);
1597 }
1598
1599 /*
1600  * Determine the class of a given object identified by objectAddress.
1601  *
1602  * This function is essentially the reverse mapping for the object_classes[]
1603  * table.  We implement it as a function because the OIDs aren't consecutive.
1604  */
1605 ObjectClass
1606 getObjectClass(const ObjectAddress *object)
1607 {
1608         switch (object->classId)
1609         {
1610                 case RelationRelationId:
1611                         /* caller must check objectSubId */
1612                         return OCLASS_CLASS;
1613
1614                 case ProcedureRelationId:
1615                         Assert(object->objectSubId == 0);
1616                         return OCLASS_PROC;
1617
1618                 case TypeRelationId:
1619                         Assert(object->objectSubId == 0);
1620                         return OCLASS_TYPE;
1621
1622                 case CastRelationId:
1623                         Assert(object->objectSubId == 0);
1624                         return OCLASS_CAST;
1625
1626                 case ConstraintRelationId:
1627                         Assert(object->objectSubId == 0);
1628                         return OCLASS_CONSTRAINT;
1629
1630                 case ConversionRelationId:
1631                         Assert(object->objectSubId == 0);
1632                         return OCLASS_CONVERSION;
1633
1634                 case AttrDefaultRelationId:
1635                         Assert(object->objectSubId == 0);
1636                         return OCLASS_DEFAULT;
1637
1638                 case LanguageRelationId:
1639                         Assert(object->objectSubId == 0);
1640                         return OCLASS_LANGUAGE;
1641
1642                 case OperatorRelationId:
1643                         Assert(object->objectSubId == 0);
1644                         return OCLASS_OPERATOR;
1645
1646                 case OperatorClassRelationId:
1647                         Assert(object->objectSubId == 0);
1648                         return OCLASS_OPCLASS;
1649
1650                 case OperatorFamilyRelationId:
1651                         Assert(object->objectSubId == 0);
1652                         return OCLASS_OPFAMILY;
1653
1654                 case AccessMethodOperatorRelationId:
1655                         Assert(object->objectSubId == 0);
1656                         return OCLASS_AMOP;
1657
1658                 case AccessMethodProcedureRelationId:
1659                         Assert(object->objectSubId == 0);
1660                         return OCLASS_AMPROC;
1661
1662                 case RewriteRelationId:
1663                         Assert(object->objectSubId == 0);
1664                         return OCLASS_REWRITE;
1665
1666                 case TriggerRelationId:
1667                         Assert(object->objectSubId == 0);
1668                         return OCLASS_TRIGGER;
1669
1670                 case NamespaceRelationId:
1671                         Assert(object->objectSubId == 0);
1672                         return OCLASS_SCHEMA;
1673
1674                 case AuthIdRelationId:
1675                         Assert(object->objectSubId == 0);
1676                         return OCLASS_ROLE;
1677
1678                 case DatabaseRelationId:
1679                         Assert(object->objectSubId == 0);
1680                         return OCLASS_DATABASE;
1681
1682                 case TableSpaceRelationId:
1683                         Assert(object->objectSubId == 0);
1684                         return OCLASS_TBLSPACE;
1685         }
1686
1687         /* shouldn't get here */
1688         elog(ERROR, "unrecognized object class: %u", object->classId);
1689         return OCLASS_CLASS;            /* keep compiler quiet */
1690 }
1691
1692 /*
1693  * getObjectDescription: build an object description for messages
1694  *
1695  * The result is a palloc'd string.
1696  */
1697 char *
1698 getObjectDescription(const ObjectAddress *object)
1699 {
1700         StringInfoData buffer;
1701
1702         initStringInfo(&buffer);
1703
1704         switch (getObjectClass(object))
1705         {
1706                 case OCLASS_CLASS:
1707                         getRelationDescription(&buffer, object->objectId);
1708                         if (object->objectSubId != 0)
1709                                 appendStringInfo(&buffer, _(" column %s"),
1710                                                                  get_relid_attribute_name(object->objectId,
1711                                                                                                            object->objectSubId));
1712                         break;
1713
1714                 case OCLASS_PROC:
1715                         appendStringInfo(&buffer, _("function %s"),
1716                                                          format_procedure(object->objectId));
1717                         break;
1718
1719                 case OCLASS_TYPE:
1720                         appendStringInfo(&buffer, _("type %s"),
1721                                                          format_type_be(object->objectId));
1722                         break;
1723
1724                 case OCLASS_CAST:
1725                         {
1726                                 Relation        castDesc;
1727                                 ScanKeyData skey[1];
1728                                 SysScanDesc rcscan;
1729                                 HeapTuple       tup;
1730                                 Form_pg_cast castForm;
1731
1732                                 castDesc = heap_open(CastRelationId, AccessShareLock);
1733
1734                                 ScanKeyInit(&skey[0],
1735                                                         ObjectIdAttributeNumber,
1736                                                         BTEqualStrategyNumber, F_OIDEQ,
1737                                                         ObjectIdGetDatum(object->objectId));
1738
1739                                 rcscan = systable_beginscan(castDesc, CastOidIndexId, true,
1740                                                                                         SnapshotNow, 1, skey);
1741
1742                                 tup = systable_getnext(rcscan);
1743
1744                                 if (!HeapTupleIsValid(tup))
1745                                         elog(ERROR, "could not find tuple for cast %u",
1746                                                  object->objectId);
1747
1748                                 castForm = (Form_pg_cast) GETSTRUCT(tup);
1749
1750                                 appendStringInfo(&buffer, _("cast from %s to %s"),
1751                                                                  format_type_be(castForm->castsource),
1752                                                                  format_type_be(castForm->casttarget));
1753
1754                                 systable_endscan(rcscan);
1755                                 heap_close(castDesc, AccessShareLock);
1756                                 break;
1757                         }
1758
1759                 case OCLASS_CONSTRAINT:
1760                         {
1761                                 HeapTuple       conTup;
1762                                 Form_pg_constraint con;
1763
1764                                 conTup = SearchSysCache(CONSTROID,
1765                                                                                 ObjectIdGetDatum(object->objectId),
1766                                                                                 0, 0, 0);
1767                                 if (!HeapTupleIsValid(conTup))
1768                                         elog(ERROR, "cache lookup failed for constraint %u",
1769                                                  object->objectId);
1770                                 con = (Form_pg_constraint) GETSTRUCT(conTup);
1771
1772                                 if (OidIsValid(con->conrelid))
1773                                 {
1774                                         appendStringInfo(&buffer, _("constraint %s on "),
1775                                                                          NameStr(con->conname));
1776                                         getRelationDescription(&buffer, con->conrelid);
1777                                 }
1778                                 else
1779                                 {
1780                                         appendStringInfo(&buffer, _("constraint %s"),
1781                                                                          NameStr(con->conname));
1782                                 }
1783
1784                                 ReleaseSysCache(conTup);
1785                                 break;
1786                         }
1787
1788                 case OCLASS_CONVERSION:
1789                         {
1790                                 HeapTuple       conTup;
1791
1792                                 conTup = SearchSysCache(CONVOID,
1793                                                                                 ObjectIdGetDatum(object->objectId),
1794                                                                                 0, 0, 0);
1795                                 if (!HeapTupleIsValid(conTup))
1796                                         elog(ERROR, "cache lookup failed for conversion %u",
1797                                                  object->objectId);
1798                                 appendStringInfo(&buffer, _("conversion %s"),
1799                                  NameStr(((Form_pg_conversion) GETSTRUCT(conTup))->conname));
1800                                 ReleaseSysCache(conTup);
1801                                 break;
1802                         }
1803
1804                 case OCLASS_DEFAULT:
1805                         {
1806                                 Relation        attrdefDesc;
1807                                 ScanKeyData skey[1];
1808                                 SysScanDesc adscan;
1809                                 HeapTuple       tup;
1810                                 Form_pg_attrdef attrdef;
1811                                 ObjectAddress colobject;
1812
1813                                 attrdefDesc = heap_open(AttrDefaultRelationId, AccessShareLock);
1814
1815                                 ScanKeyInit(&skey[0],
1816                                                         ObjectIdAttributeNumber,
1817                                                         BTEqualStrategyNumber, F_OIDEQ,
1818                                                         ObjectIdGetDatum(object->objectId));
1819
1820                                 adscan = systable_beginscan(attrdefDesc, AttrDefaultOidIndexId,
1821                                                                                         true, SnapshotNow, 1, skey);
1822
1823                                 tup = systable_getnext(adscan);
1824
1825                                 if (!HeapTupleIsValid(tup))
1826                                         elog(ERROR, "could not find tuple for attrdef %u",
1827                                                  object->objectId);
1828
1829                                 attrdef = (Form_pg_attrdef) GETSTRUCT(tup);
1830
1831                                 colobject.classId = RelationRelationId;
1832                                 colobject.objectId = attrdef->adrelid;
1833                                 colobject.objectSubId = attrdef->adnum;
1834
1835                                 appendStringInfo(&buffer, _("default for %s"),
1836                                                                  getObjectDescription(&colobject));
1837
1838                                 systable_endscan(adscan);
1839                                 heap_close(attrdefDesc, AccessShareLock);
1840                                 break;
1841                         }
1842
1843                 case OCLASS_LANGUAGE:
1844                         {
1845                                 HeapTuple       langTup;
1846
1847                                 langTup = SearchSysCache(LANGOID,
1848                                                                                  ObjectIdGetDatum(object->objectId),
1849                                                                                  0, 0, 0);
1850                                 if (!HeapTupleIsValid(langTup))
1851                                         elog(ERROR, "cache lookup failed for language %u",
1852                                                  object->objectId);
1853                                 appendStringInfo(&buffer, _("language %s"),
1854                                   NameStr(((Form_pg_language) GETSTRUCT(langTup))->lanname));
1855                                 ReleaseSysCache(langTup);
1856                                 break;
1857                         }
1858
1859                 case OCLASS_OPERATOR:
1860                         appendStringInfo(&buffer, _("operator %s"),
1861                                                          format_operator(object->objectId));
1862                         break;
1863
1864                 case OCLASS_OPCLASS:
1865                         {
1866                                 HeapTuple       opcTup;
1867                                 Form_pg_opclass opcForm;
1868                                 HeapTuple       amTup;
1869                                 Form_pg_am      amForm;
1870                                 char       *nspname;
1871
1872                                 opcTup = SearchSysCache(CLAOID,
1873                                                                                 ObjectIdGetDatum(object->objectId),
1874                                                                                 0, 0, 0);
1875                                 if (!HeapTupleIsValid(opcTup))
1876                                         elog(ERROR, "cache lookup failed for opclass %u",
1877                                                  object->objectId);
1878                                 opcForm = (Form_pg_opclass) GETSTRUCT(opcTup);
1879
1880                                 amTup = SearchSysCache(AMOID,
1881                                                                            ObjectIdGetDatum(opcForm->opcmethod),
1882                                                                            0, 0, 0);
1883                                 if (!HeapTupleIsValid(amTup))
1884                                         elog(ERROR, "cache lookup failed for access method %u",
1885                                                  opcForm->opcmethod);
1886                                 amForm = (Form_pg_am) GETSTRUCT(amTup);
1887
1888                                 /* Qualify the name if not visible in search path */
1889                                 if (OpclassIsVisible(object->objectId))
1890                                         nspname = NULL;
1891                                 else
1892                                         nspname = get_namespace_name(opcForm->opcnamespace);
1893
1894                                 appendStringInfo(&buffer, _("operator class %s for access method %s"),
1895                                                                  quote_qualified_identifier(nspname,
1896                                                                                                   NameStr(opcForm->opcname)),
1897                                                                  NameStr(amForm->amname));
1898
1899                                 ReleaseSysCache(amTup);
1900                                 ReleaseSysCache(opcTup);
1901                                 break;
1902                         }
1903
1904                 case OCLASS_OPFAMILY:
1905                         getOpFamilyDescription(&buffer, object->objectId);
1906                         break;
1907
1908                 case OCLASS_AMOP:
1909                         {
1910                                 Relation        amopDesc;
1911                                 ScanKeyData skey[1];
1912                                 SysScanDesc amscan;
1913                                 HeapTuple       tup;
1914                                 Form_pg_amop amopForm;
1915
1916                                 amopDesc = heap_open(AccessMethodOperatorRelationId,
1917                                                                          AccessShareLock);
1918
1919                                 ScanKeyInit(&skey[0],
1920                                                         ObjectIdAttributeNumber,
1921                                                         BTEqualStrategyNumber, F_OIDEQ,
1922                                                         ObjectIdGetDatum(object->objectId));
1923
1924                                 amscan = systable_beginscan(amopDesc, AccessMethodOperatorOidIndexId, true,
1925                                                                                         SnapshotNow, 1, skey);
1926
1927                                 tup = systable_getnext(amscan);
1928
1929                                 if (!HeapTupleIsValid(tup))
1930                                         elog(ERROR, "could not find tuple for amop entry %u",
1931                                                  object->objectId);
1932
1933                                 amopForm = (Form_pg_amop) GETSTRUCT(tup);
1934
1935                                 appendStringInfo(&buffer, _("operator %d %s of "),
1936                                                                  amopForm->amopstrategy,
1937                                                                  format_operator(amopForm->amopopr));
1938                                 getOpFamilyDescription(&buffer, amopForm->amopfamily);
1939
1940                                 systable_endscan(amscan);
1941                                 heap_close(amopDesc, AccessShareLock);
1942                                 break;
1943                         }
1944
1945                 case OCLASS_AMPROC:
1946                         {
1947                                 Relation        amprocDesc;
1948                                 ScanKeyData skey[1];
1949                                 SysScanDesc amscan;
1950                                 HeapTuple       tup;
1951                                 Form_pg_amproc amprocForm;
1952
1953                                 amprocDesc = heap_open(AccessMethodProcedureRelationId,
1954                                                                            AccessShareLock);
1955
1956                                 ScanKeyInit(&skey[0],
1957                                                         ObjectIdAttributeNumber,
1958                                                         BTEqualStrategyNumber, F_OIDEQ,
1959                                                         ObjectIdGetDatum(object->objectId));
1960
1961                                 amscan = systable_beginscan(amprocDesc, AccessMethodProcedureOidIndexId, true,
1962                                                                                         SnapshotNow, 1, skey);
1963
1964                                 tup = systable_getnext(amscan);
1965
1966                                 if (!HeapTupleIsValid(tup))
1967                                         elog(ERROR, "could not find tuple for amproc entry %u",
1968                                                  object->objectId);
1969
1970                                 amprocForm = (Form_pg_amproc) GETSTRUCT(tup);
1971
1972                                 appendStringInfo(&buffer, _("function %d %s of "),
1973                                                                  amprocForm->amprocnum,
1974                                                                  format_procedure(amprocForm->amproc));
1975                                 getOpFamilyDescription(&buffer, amprocForm->amprocfamily);
1976
1977                                 systable_endscan(amscan);
1978                                 heap_close(amprocDesc, AccessShareLock);
1979                                 break;
1980                         }
1981
1982                 case OCLASS_REWRITE:
1983                         {
1984                                 Relation        ruleDesc;
1985                                 ScanKeyData skey[1];
1986                                 SysScanDesc rcscan;
1987                                 HeapTuple       tup;
1988                                 Form_pg_rewrite rule;
1989
1990                                 ruleDesc = heap_open(RewriteRelationId, AccessShareLock);
1991
1992                                 ScanKeyInit(&skey[0],
1993                                                         ObjectIdAttributeNumber,
1994                                                         BTEqualStrategyNumber, F_OIDEQ,
1995                                                         ObjectIdGetDatum(object->objectId));
1996
1997                                 rcscan = systable_beginscan(ruleDesc, RewriteOidIndexId, true,
1998                                                                                         SnapshotNow, 1, skey);
1999
2000                                 tup = systable_getnext(rcscan);
2001
2002                                 if (!HeapTupleIsValid(tup))
2003                                         elog(ERROR, "could not find tuple for rule %u",
2004                                                  object->objectId);
2005
2006                                 rule = (Form_pg_rewrite) GETSTRUCT(tup);
2007
2008                                 appendStringInfo(&buffer, _("rule %s on "),
2009                                                                  NameStr(rule->rulename));
2010                                 getRelationDescription(&buffer, rule->ev_class);
2011
2012                                 systable_endscan(rcscan);
2013                                 heap_close(ruleDesc, AccessShareLock);
2014                                 break;
2015                         }
2016
2017                 case OCLASS_TRIGGER:
2018                         {
2019                                 Relation        trigDesc;
2020                                 ScanKeyData skey[1];
2021                                 SysScanDesc tgscan;
2022                                 HeapTuple       tup;
2023                                 Form_pg_trigger trig;
2024
2025                                 trigDesc = heap_open(TriggerRelationId, AccessShareLock);
2026
2027                                 ScanKeyInit(&skey[0],
2028                                                         ObjectIdAttributeNumber,
2029                                                         BTEqualStrategyNumber, F_OIDEQ,
2030                                                         ObjectIdGetDatum(object->objectId));
2031
2032                                 tgscan = systable_beginscan(trigDesc, TriggerOidIndexId, true,
2033                                                                                         SnapshotNow, 1, skey);
2034
2035                                 tup = systable_getnext(tgscan);
2036
2037                                 if (!HeapTupleIsValid(tup))
2038                                         elog(ERROR, "could not find tuple for trigger %u",
2039                                                  object->objectId);
2040
2041                                 trig = (Form_pg_trigger) GETSTRUCT(tup);
2042
2043                                 appendStringInfo(&buffer, _("trigger %s on "),
2044                                                                  NameStr(trig->tgname));
2045                                 getRelationDescription(&buffer, trig->tgrelid);
2046
2047                                 systable_endscan(tgscan);
2048                                 heap_close(trigDesc, AccessShareLock);
2049                                 break;
2050                         }
2051
2052                 case OCLASS_SCHEMA:
2053                         {
2054                                 char       *nspname;
2055
2056                                 nspname = get_namespace_name(object->objectId);
2057                                 if (!nspname)
2058                                         elog(ERROR, "cache lookup failed for namespace %u",
2059                                                  object->objectId);
2060                                 appendStringInfo(&buffer, _("schema %s"), nspname);
2061                                 break;
2062                         }
2063
2064                 case OCLASS_ROLE:
2065                         {
2066                                 appendStringInfo(&buffer, _("role %s"),
2067                                                                  GetUserNameFromId(object->objectId));
2068                                 break;
2069                         }
2070
2071                 case OCLASS_DATABASE:
2072                         {
2073                                 char       *datname;
2074
2075                                 datname = get_database_name(object->objectId);
2076                                 if (!datname)
2077                                         elog(ERROR, "cache lookup failed for database %u",
2078                                                  object->objectId);
2079                                 appendStringInfo(&buffer, _("database %s"), datname);
2080                                 break;
2081                         }
2082
2083                 case OCLASS_TBLSPACE:
2084                         {
2085                                 char       *tblspace;
2086
2087                                 tblspace = get_tablespace_name(object->objectId);
2088                                 if (!tblspace)
2089                                         elog(ERROR, "cache lookup failed for tablespace %u",
2090                                                  object->objectId);
2091                                 appendStringInfo(&buffer, _("tablespace %s"), tblspace);
2092                                 break;
2093                         }
2094
2095                 default:
2096                         appendStringInfo(&buffer, "unrecognized object %u %u %d",
2097                                                          object->classId,
2098                                                          object->objectId,
2099                                                          object->objectSubId);
2100                         break;
2101         }
2102
2103         return buffer.data;
2104 }
2105
2106 /*
2107  * subroutine for getObjectDescription: describe a relation
2108  */
2109 static void
2110 getRelationDescription(StringInfo buffer, Oid relid)
2111 {
2112         HeapTuple       relTup;
2113         Form_pg_class relForm;
2114         char       *nspname;
2115         char       *relname;
2116
2117         relTup = SearchSysCache(RELOID,
2118                                                         ObjectIdGetDatum(relid),
2119                                                         0, 0, 0);
2120         if (!HeapTupleIsValid(relTup))
2121                 elog(ERROR, "cache lookup failed for relation %u", relid);
2122         relForm = (Form_pg_class) GETSTRUCT(relTup);
2123
2124         /* Qualify the name if not visible in search path */
2125         if (RelationIsVisible(relid))
2126                 nspname = NULL;
2127         else
2128                 nspname = get_namespace_name(relForm->relnamespace);
2129
2130         relname = quote_qualified_identifier(nspname, NameStr(relForm->relname));
2131
2132         switch (relForm->relkind)
2133         {
2134                 case RELKIND_RELATION:
2135                         appendStringInfo(buffer, _("table %s"),
2136                                                          relname);
2137                         break;
2138                 case RELKIND_INDEX:
2139                         appendStringInfo(buffer, _("index %s"),
2140                                                          relname);
2141                         break;
2142                 case RELKIND_SEQUENCE:
2143                         appendStringInfo(buffer, _("sequence %s"),
2144                                                          relname);
2145                         break;
2146                 case RELKIND_UNCATALOGED:
2147                         appendStringInfo(buffer, _("uncataloged table %s"),
2148                                                          relname);
2149                         break;
2150                 case RELKIND_TOASTVALUE:
2151                         appendStringInfo(buffer, _("toast table %s"),
2152                                                          relname);
2153                         break;
2154                 case RELKIND_VIEW:
2155                         appendStringInfo(buffer, _("view %s"),
2156                                                          relname);
2157                         break;
2158                 case RELKIND_COMPOSITE_TYPE:
2159                         appendStringInfo(buffer, _("composite type %s"),
2160                                                          relname);
2161                         break;
2162                 default:
2163                         /* shouldn't get here */
2164                         appendStringInfo(buffer, _("relation %s"),
2165                                                          relname);
2166                         break;
2167         }
2168
2169         ReleaseSysCache(relTup);
2170 }
2171
2172 /*
2173  * subroutine for getObjectDescription: describe an operator family
2174  */
2175 static void
2176 getOpFamilyDescription(StringInfo buffer, Oid opfid)
2177 {
2178         HeapTuple       opfTup;
2179         Form_pg_opfamily opfForm;
2180         HeapTuple       amTup;
2181         Form_pg_am      amForm;
2182         char       *nspname;
2183
2184         opfTup = SearchSysCache(OPFAMILYOID,
2185                                                         ObjectIdGetDatum(opfid),
2186                                                         0, 0, 0);
2187         if (!HeapTupleIsValid(opfTup))
2188                 elog(ERROR, "cache lookup failed for opfamily %u", opfid);
2189         opfForm = (Form_pg_opfamily) GETSTRUCT(opfTup);
2190
2191         amTup = SearchSysCache(AMOID,
2192                                                    ObjectIdGetDatum(opfForm->opfmethod),
2193                                                    0, 0, 0);
2194         if (!HeapTupleIsValid(amTup))
2195                 elog(ERROR, "cache lookup failed for access method %u",
2196                          opfForm->opfmethod);
2197         amForm = (Form_pg_am) GETSTRUCT(amTup);
2198
2199         /* Qualify the name if not visible in search path */
2200         if (OpfamilyIsVisible(opfid))
2201                 nspname = NULL;
2202         else
2203                 nspname = get_namespace_name(opfForm->opfnamespace);
2204
2205         appendStringInfo(buffer, _("operator family %s for access method %s"),
2206                                          quote_qualified_identifier(nspname,
2207                                                                                                 NameStr(opfForm->opfname)),
2208                                          NameStr(amForm->amname));
2209
2210         ReleaseSysCache(amTup);
2211         ReleaseSysCache(opfTup);
2212 }