PostgreSQL Source Code git master
dependency.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/heap.h"
#include "catalog/index.h"
#include "catalog/namespace.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_am.h"
#include "catalog/pg_amop.h"
#include "catalog/pg_amproc.h"
#include "catalog/pg_attrdef.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_auth_members.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_conversion.h"
#include "catalog/pg_database.h"
#include "catalog/pg_default_acl.h"
#include "catalog/pg_depend.h"
#include "catalog/pg_event_trigger.h"
#include "catalog/pg_extension.h"
#include "catalog/pg_foreign_data_wrapper.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_init_privs.h"
#include "catalog/pg_language.h"
#include "catalog/pg_largeobject.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_opfamily.h"
#include "catalog/pg_parameter_acl.h"
#include "catalog/pg_policy.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_publication.h"
#include "catalog/pg_publication_namespace.h"
#include "catalog/pg_publication_rel.h"
#include "catalog/pg_rewrite.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_subscription.h"
#include "catalog/pg_tablespace.h"
#include "catalog/pg_transform.h"
#include "catalog/pg_trigger.h"
#include "catalog/pg_ts_config.h"
#include "catalog/pg_ts_dict.h"
#include "catalog/pg_ts_parser.h"
#include "catalog/pg_ts_template.h"
#include "catalog/pg_type.h"
#include "catalog/pg_user_mapping.h"
#include "commands/comment.h"
#include "commands/defrem.h"
#include "commands/event_trigger.h"
#include "commands/extension.h"
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/seclabel.h"
#include "commands/sequence.h"
#include "commands/trigger.h"
#include "commands/typecmds.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteRemove.h"
#include "storage/lmgr.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Include dependency graph for dependency.c:

Go to the source code of this file.

Data Structures

struct  ObjectAddressExtra
 
struct  ObjectAddresses
 
struct  ObjectAddressStack
 
struct  ObjectAddressAndFlags
 
struct  find_expr_references_context
 

Macros

#define DEPFLAG_ORIGINAL   0x0001 /* an original deletion target */
 
#define DEPFLAG_NORMAL   0x0002 /* reached via normal dependency */
 
#define DEPFLAG_AUTO   0x0004 /* reached via auto dependency */
 
#define DEPFLAG_INTERNAL   0x0008 /* reached via internal dependency */
 
#define DEPFLAG_PARTITION   0x0010 /* reached via partition dependency */
 
#define DEPFLAG_EXTENSION   0x0020 /* reached via extension dependency */
 
#define DEPFLAG_REVERSE   0x0040 /* reverse internal/extension link */
 
#define DEPFLAG_IS_PART   0x0080 /* has a partition dependency */
 
#define DEPFLAG_SUBOBJECT   0x0100 /* subobject of another deletable object */
 
#define MAX_REPORTED_DEPS   100
 

Typedefs

typedef struct ObjectAddressStack ObjectAddressStack
 

Functions

static void findDependentObjects (const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
 
static void reportDependentObjects (const ObjectAddresses *targetObjects, DropBehavior behavior, int flags, const ObjectAddress *origObject)
 
static void deleteOneObject (const ObjectAddress *object, Relation *depRel, int32 flags)
 
static void doDeletion (const ObjectAddress *object, int flags)
 
static bool find_expr_references_walker (Node *node, find_expr_references_context *context)
 
static void process_function_rte_ref (RangeTblEntry *rte, AttrNumber attnum, find_expr_references_context *context)
 
static void eliminate_duplicate_dependencies (ObjectAddresses *addrs)
 
static int object_address_comparator (const void *a, const void *b)
 
static void add_object_address (Oid classId, Oid objectId, int32 subId, ObjectAddresses *addrs)
 
static void add_exact_object_address_extra (const ObjectAddress *object, const ObjectAddressExtra *extra, ObjectAddresses *addrs)
 
static bool object_address_present_add_flags (const ObjectAddress *object, int flags, ObjectAddresses *addrs)
 
static bool stack_address_present_add_flags (const ObjectAddress *object, int flags, ObjectAddressStack *stack)
 
static void DeleteInitPrivs (const ObjectAddress *object)
 
static void deleteObjectsInList (ObjectAddresses *targetObjects, Relation *depRel, int flags)
 
void performDeletion (const ObjectAddress *object, DropBehavior behavior, int flags)
 
void performDeletionCheck (const ObjectAddress *object, DropBehavior behavior, int flags)
 
void performMultipleDeletions (const ObjectAddresses *objects, DropBehavior behavior, int flags)
 
static void DropObjectById (const ObjectAddress *object)
 
static void deleteOneObject (const ObjectAddress *object, Relation *depRel, int flags)
 
void AcquireDeletionLock (const ObjectAddress *object, int flags)
 
void ReleaseDeletionLock (const ObjectAddress *object)
 
void recordDependencyOnExpr (const ObjectAddress *depender, Node *expr, List *rtable, DependencyType behavior)
 
void collectDependenciesOfExpr (ObjectAddresses *addrs, Node *expr, List *rtable)
 
void recordDependencyOnSingleRelExpr (const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool reverse_self)
 
bool find_temp_object (const ObjectAddresses *addrs, bool local_temp_okay, ObjectAddress *foundobj)
 
bool query_uses_temp_object (Query *query, ObjectAddress *temp_object)
 
ObjectAddressesnew_object_addresses (void)
 
void add_exact_object_address (const ObjectAddress *object, ObjectAddresses *addrs)
 
bool object_address_present (const ObjectAddress *object, const ObjectAddresses *addrs)
 
void record_object_address_dependencies (const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
 
void sort_object_addresses (ObjectAddresses *addrs)
 
void free_object_addresses (ObjectAddresses *addrs)
 

Macro Definition Documentation

◆ DEPFLAG_AUTO

#define DEPFLAG_AUTO   0x0004 /* reached via auto dependency */

Definition at line 104 of file dependency.c.

◆ DEPFLAG_EXTENSION

#define DEPFLAG_EXTENSION   0x0020 /* reached via extension dependency */

Definition at line 107 of file dependency.c.

◆ DEPFLAG_INTERNAL

#define DEPFLAG_INTERNAL   0x0008 /* reached via internal dependency */

Definition at line 105 of file dependency.c.

◆ DEPFLAG_IS_PART

#define DEPFLAG_IS_PART   0x0080 /* has a partition dependency */

Definition at line 109 of file dependency.c.

◆ DEPFLAG_NORMAL

#define DEPFLAG_NORMAL   0x0002 /* reached via normal dependency */

Definition at line 103 of file dependency.c.

◆ DEPFLAG_ORIGINAL

#define DEPFLAG_ORIGINAL   0x0001 /* an original deletion target */

Definition at line 102 of file dependency.c.

◆ DEPFLAG_PARTITION

#define DEPFLAG_PARTITION   0x0010 /* reached via partition dependency */

Definition at line 106 of file dependency.c.

◆ DEPFLAG_REVERSE

#define DEPFLAG_REVERSE   0x0040 /* reverse internal/extension link */

Definition at line 108 of file dependency.c.

◆ DEPFLAG_SUBOBJECT

#define DEPFLAG_SUBOBJECT   0x0100 /* subobject of another deletable object */

Definition at line 110 of file dependency.c.

◆ MAX_REPORTED_DEPS

#define MAX_REPORTED_DEPS   100

Typedef Documentation

◆ ObjectAddressStack

Function Documentation

◆ AcquireDeletionLock()

void AcquireDeletionLock ( const ObjectAddress object,
int  flags 
)

Definition at line 1546 of file dependency.c.

1547{
1548 if (object->classId == RelationRelationId)
1549 {
1550 /*
1551 * In DROP INDEX CONCURRENTLY, take only ShareUpdateExclusiveLock on
1552 * the index for the moment. index_drop() will promote the lock once
1553 * it's safe to do so. In all other cases we need full exclusive
1554 * lock.
1555 */
1558 else
1560 }
1561 else if (object->classId == AuthMemRelationId)
1562 LockSharedObject(object->classId, object->objectId, 0,
1564 else
1565 {
1566 /* assume we should lock the whole object not a sub-object */
1567 LockDatabaseObject(object->classId, object->objectId, 0,
1569 }
1570}
#define PERFORM_DELETION_CONCURRENTLY
Definition: dependency.h:93
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1088
void LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1008
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:107
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39

References AccessExclusiveLock, ObjectAddress::classId, ObjectAddressStack::flags, LockDatabaseObject(), LockRelationOid(), LockSharedObject(), ObjectAddressStack::object, ObjectAddress::objectId, PERFORM_DELETION_CONCURRENTLY, and ShareUpdateExclusiveLock.

Referenced by findDependentObjects(), performDeletion(), performDeletionCheck(), performMultipleDeletions(), and shdepDropOwned().

◆ add_exact_object_address()

void add_exact_object_address ( const ObjectAddress object,
ObjectAddresses addrs 
)

Definition at line 2709 of file dependency.c.

2711{
2712 ObjectAddress *item;
2713
2714 /* enlarge array if needed */
2715 if (addrs->numrefs >= addrs->maxrefs)
2716 {
2717 addrs->maxrefs *= 2;
2718 addrs->refs = (ObjectAddress *)
2719 repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2720 Assert(!addrs->extras);
2721 }
2722 /* record this item */
2723 item = addrs->refs + addrs->numrefs;
2724 *item = *object;
2725 addrs->numrefs++;
2726}
Assert(PointerIsAligned(start, uint64))
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1610
ObjectAddressExtra * extras
Definition: dependency.c:117
ObjectAddress * refs
Definition: dependency.c:116

References Assert(), ObjectAddresses::extras, ObjectAddresses::maxrefs, ObjectAddresses::numrefs, ObjectAddressStack::object, ObjectAddresses::refs, and repalloc().

Referenced by AggregateCreate(), AlterConstraintNamespaces(), AlterIndexNamespaces(), AlterRelationNamespaceInternal(), AlterTypeNamespaceInternal(), ATExecDropColumn(), ATPostAlterTypeCleanup(), CastCreate(), CreateConstraintEntry(), CreateProceduralLanguage(), CreateTransform(), DropClonedTriggersFromPartition(), GenerateTypeDependencies(), heap_create_with_catalog(), index_create(), InsertExtensionTuple(), makeConfigurationDependencies(), makeDictionaryDependencies(), makeOperatorDependencies(), makeParserDependencies(), makeTSTemplateDependencies(), PreCommit_on_commit_actions(), ProcedureCreate(), RangeCreate(), recordDependencyOnSingleRelExpr(), ReindexRelationConcurrently(), RemoveInheritedConstraint(), RemoveObjects(), RemoveRelations(), shdepDropOwned(), and StorePartitionKey().

◆ add_exact_object_address_extra()

static void add_exact_object_address_extra ( const ObjectAddress object,
const ObjectAddressExtra extra,
ObjectAddresses addrs 
)
static

Definition at line 2734 of file dependency.c.

2737{
2738 ObjectAddress *item;
2739 ObjectAddressExtra *itemextra;
2740
2741 /* allocate extra space if first time */
2742 if (!addrs->extras)
2743 addrs->extras = (ObjectAddressExtra *)
2744 palloc(addrs->maxrefs * sizeof(ObjectAddressExtra));
2745
2746 /* enlarge array if needed */
2747 if (addrs->numrefs >= addrs->maxrefs)
2748 {
2749 addrs->maxrefs *= 2;
2750 addrs->refs = (ObjectAddress *)
2751 repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2752 addrs->extras = (ObjectAddressExtra *)
2753 repalloc(addrs->extras, addrs->maxrefs * sizeof(ObjectAddressExtra));
2754 }
2755 /* record this item */
2756 item = addrs->refs + addrs->numrefs;
2757 *item = *object;
2758 itemextra = addrs->extras + addrs->numrefs;
2759 *itemextra = *extra;
2760 addrs->numrefs++;
2761}
void * palloc(Size size)
Definition: mcxt.c:1365

References ObjectAddresses::extras, ObjectAddresses::maxrefs, ObjectAddresses::numrefs, ObjectAddressStack::object, palloc(), ObjectAddresses::refs, and repalloc().

Referenced by findDependentObjects().

◆ add_object_address()

static void add_object_address ( Oid  classId,
Oid  objectId,
int32  subId,
ObjectAddresses addrs 
)
static

Definition at line 2682 of file dependency.c.

2684{
2685 ObjectAddress *item;
2686
2687 /* enlarge array if needed */
2688 if (addrs->numrefs >= addrs->maxrefs)
2689 {
2690 addrs->maxrefs *= 2;
2691 addrs->refs = (ObjectAddress *)
2692 repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2693 Assert(!addrs->extras);
2694 }
2695 /* record this item */
2696 item = addrs->refs + addrs->numrefs;
2697 item->classId = classId;
2698 item->objectId = objectId;
2699 item->objectSubId = subId;
2700 addrs->numrefs++;
2701}

References Assert(), ObjectAddress::classId, ObjectAddresses::extras, ObjectAddresses::maxrefs, ObjectAddresses::numrefs, ObjectAddress::objectId, ObjectAddress::objectSubId, ObjectAddresses::refs, and repalloc().

Referenced by find_expr_references_walker(), and process_function_rte_ref().

◆ collectDependenciesOfExpr()

void collectDependenciesOfExpr ( ObjectAddresses addrs,
Node expr,
List rtable 
)

Definition at line 1646 of file dependency.c.

1648{
1650
1651 context.addrs = addrs;
1652
1653 /* Set up interpretation for Vars at varlevelsup = 0 */
1654 context.rtables = list_make1(rtable);
1655
1656 /* Scan the expression tree for referenceable objects */
1657 find_expr_references_walker(expr, &context);
1658}
static bool find_expr_references_walker(Node *node, find_expr_references_context *context)
Definition: dependency.c:1780
#define list_make1(x1)
Definition: pg_list.h:212
ObjectAddresses * addrs
Definition: dependency.c:142

References find_expr_references_context::addrs, find_expr_references_walker(), list_make1, and find_expr_references_context::rtables.

Referenced by ProcedureCreate(), query_uses_temp_object(), and recordDependencyOnExpr().

◆ DeleteInitPrivs()

static void DeleteInitPrivs ( const ObjectAddress object)
static

Definition at line 2961 of file dependency.c.

2962{
2963 Relation relation;
2964 ScanKeyData key[3];
2965 int nkeys;
2966 SysScanDesc scan;
2967 HeapTuple oldtuple;
2968
2969 relation = table_open(InitPrivsRelationId, RowExclusiveLock);
2970
2971 ScanKeyInit(&key[0],
2972 Anum_pg_init_privs_objoid,
2973 BTEqualStrategyNumber, F_OIDEQ,
2974 ObjectIdGetDatum(object->objectId));
2975 ScanKeyInit(&key[1],
2976 Anum_pg_init_privs_classoid,
2977 BTEqualStrategyNumber, F_OIDEQ,
2978 ObjectIdGetDatum(object->classId));
2979 if (object->objectSubId != 0)
2980 {
2981 ScanKeyInit(&key[2],
2982 Anum_pg_init_privs_objsubid,
2983 BTEqualStrategyNumber, F_INT4EQ,
2984 Int32GetDatum(object->objectSubId));
2985 nkeys = 3;
2986 }
2987 else
2988 nkeys = 2;
2989
2990 scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
2991 NULL, nkeys, key);
2992
2993 while (HeapTupleIsValid(oldtuple = systable_getnext(scan)))
2994 CatalogTupleDelete(relation, &oldtuple->t_self);
2995
2996 systable_endscan(scan);
2997
2998 table_close(relation, RowExclusiveLock);
2999}
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:388
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition: indexing.c:365
#define RowExclusiveLock
Definition: lockdefs.h:38
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:222
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
ItemPointerData t_self
Definition: htup.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References BTEqualStrategyNumber, CatalogTupleDelete(), ObjectAddress::classId, HeapTupleIsValid, Int32GetDatum(), sort-test::key, ObjectAddressStack::object, ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by deleteOneObject().

◆ deleteObjectsInList()

static void deleteObjectsInList ( ObjectAddresses targetObjects,
Relation depRel,
int  flags 
)
static

Definition at line 186 of file dependency.c.

188{
189 int i;
190
191 /*
192 * Keep track of objects for event triggers, if necessary.
193 */
195 {
196 for (i = 0; i < targetObjects->numrefs; i++)
197 {
198 const ObjectAddress *thisobj = &targetObjects->refs[i];
199 const ObjectAddressExtra *extra = &targetObjects->extras[i];
200 bool original = false;
201 bool normal = false;
202
203 if (extra->flags & DEPFLAG_ORIGINAL)
204 original = true;
205 if (extra->flags & DEPFLAG_NORMAL)
206 normal = true;
207 if (extra->flags & DEPFLAG_REVERSE)
208 normal = true;
209
210 if (EventTriggerSupportsObject(thisobj))
211 {
212 EventTriggerSQLDropAddObject(thisobj, original, normal);
213 }
214 }
215 }
216
217 /*
218 * Delete all the objects in the proper order, except that if told to, we
219 * should skip the original object(s).
220 */
221 for (i = 0; i < targetObjects->numrefs; i++)
222 {
223 ObjectAddress *thisobj = targetObjects->refs + i;
224 ObjectAddressExtra *thisextra = targetObjects->extras + i;
225
226 if ((flags & PERFORM_DELETION_SKIP_ORIGINAL) &&
227 (thisextra->flags & DEPFLAG_ORIGINAL))
228 continue;
229
230 deleteOneObject(thisobj, depRel, flags);
231 }
232}
static void deleteOneObject(const ObjectAddress *object, Relation *depRel, int32 flags)
#define DEPFLAG_ORIGINAL
Definition: dependency.c:102
#define DEPFLAG_REVERSE
Definition: dependency.c:108
#define DEPFLAG_NORMAL
Definition: dependency.c:103
#define PERFORM_DELETION_SKIP_ORIGINAL
Definition: dependency.h:95
#define PERFORM_DELETION_INTERNAL
Definition: dependency.h:92
void EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool normal)
bool trackDroppedObjectsNeeded(void)
bool EventTriggerSupportsObject(const ObjectAddress *object)
int i
Definition: isn.c:77

References deleteOneObject(), DEPFLAG_NORMAL, DEPFLAG_ORIGINAL, DEPFLAG_REVERSE, EventTriggerSQLDropAddObject(), EventTriggerSupportsObject(), ObjectAddresses::extras, ObjectAddressExtra::flags, ObjectAddressStack::flags, i, ObjectAddresses::numrefs, PERFORM_DELETION_INTERNAL, PERFORM_DELETION_SKIP_ORIGINAL, ObjectAddresses::refs, and trackDroppedObjectsNeeded().

Referenced by performDeletion(), and performMultipleDeletions().

◆ deleteOneObject() [1/2]

static void deleteOneObject ( const ObjectAddress object,
Relation depRel,
int  flags 
)
static

Definition at line 1296 of file dependency.c.

1297{
1298 ScanKeyData key[3];
1299 int nkeys;
1300 SysScanDesc scan;
1301 HeapTuple tup;
1302
1303 /* DROP hook of the objects being removed */
1304 InvokeObjectDropHookArg(object->classId, object->objectId,
1305 object->objectSubId, flags);
1306
1307 /*
1308 * Close depRel if we are doing a drop concurrently. The object deletion
1309 * subroutine will commit the current transaction, so we can't keep the
1310 * relation open across doDeletion().
1311 */
1313 table_close(*depRel, RowExclusiveLock);
1314
1315 /*
1316 * Delete the object itself, in an object-type-dependent way.
1317 *
1318 * We used to do this after removing the outgoing dependency links, but it
1319 * seems just as reasonable to do it beforehand. In the concurrent case
1320 * we *must* do it in this order, because we can't make any transactional
1321 * updates before calling doDeletion() --- they'd get committed right
1322 * away, which is not cool if the deletion then fails.
1323 */
1324 doDeletion(object, flags);
1325
1326 /*
1327 * Reopen depRel if we closed it above
1328 */
1330 *depRel = table_open(DependRelationId, RowExclusiveLock);
1331
1332 /*
1333 * Now remove any pg_depend records that link from this object to others.
1334 * (Any records linking to this object should be gone already.)
1335 *
1336 * When dropping a whole object (subId = 0), remove all pg_depend records
1337 * for its sub-objects too.
1338 */
1339 ScanKeyInit(&key[0],
1340 Anum_pg_depend_classid,
1341 BTEqualStrategyNumber, F_OIDEQ,
1342 ObjectIdGetDatum(object->classId));
1343 ScanKeyInit(&key[1],
1344 Anum_pg_depend_objid,
1345 BTEqualStrategyNumber, F_OIDEQ,
1346 ObjectIdGetDatum(object->objectId));
1347 if (object->objectSubId != 0)
1348 {
1349 ScanKeyInit(&key[2],
1350 Anum_pg_depend_objsubid,
1351 BTEqualStrategyNumber, F_INT4EQ,
1352 Int32GetDatum(object->objectSubId));
1353 nkeys = 3;
1354 }
1355 else
1356 nkeys = 2;
1357
1358 scan = systable_beginscan(*depRel, DependDependerIndexId, true,
1359 NULL, nkeys, key);
1360
1361 while (HeapTupleIsValid(tup = systable_getnext(scan)))
1362 {
1363 CatalogTupleDelete(*depRel, &tup->t_self);
1364 }
1365
1366 systable_endscan(scan);
1367
1368 /*
1369 * Delete shared dependency references related to this object. Again, if
1370 * subId = 0, remove records for sub-objects too.
1371 */
1373 object->objectSubId);
1374
1375
1376 /*
1377 * Delete any comments, security labels, or initial privileges associated
1378 * with this object. (This is a convenient place to do these things,
1379 * rather than having every object type know to do it.) As above, all
1380 * these functions must remove records for sub-objects too if the subid is
1381 * zero.
1382 */
1383 DeleteComments(object->objectId, object->classId, object->objectSubId);
1384 DeleteSecurityLabel(object);
1385 DeleteInitPrivs(object);
1386
1387 /*
1388 * CommandCounterIncrement here to ensure that preceding changes are all
1389 * visible to the next deletion step.
1390 */
1392
1393 /*
1394 * And we're done!
1395 */
1396}
void DeleteComments(Oid oid, Oid classoid, int32 subid)
Definition: comment.c:326
static void doDeletion(const ObjectAddress *object, int flags)
Definition: dependency.c:1402
static void DeleteInitPrivs(const ObjectAddress *object)
Definition: dependency.c:2961
#define InvokeObjectDropHookArg(classId, objectId, subId, dropflags)
Definition: objectaccess.h:184
void deleteSharedDependencyRecordsFor(Oid classId, Oid objectId, int32 objectSubId)
Definition: pg_shdepend.c:1047
void DeleteSecurityLabel(const ObjectAddress *object)
Definition: seclabel.c:523
void CommandCounterIncrement(void)
Definition: xact.c:1101

References BTEqualStrategyNumber, CatalogTupleDelete(), ObjectAddress::classId, CommandCounterIncrement(), DeleteComments(), DeleteInitPrivs(), DeleteSecurityLabel(), deleteSharedDependencyRecordsFor(), doDeletion(), ObjectAddressStack::flags, HeapTupleIsValid, Int32GetDatum(), InvokeObjectDropHookArg, sort-test::key, ObjectAddressStack::object, ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, PERFORM_DELETION_CONCURRENTLY, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

◆ deleteOneObject() [2/2]

static void deleteOneObject ( const ObjectAddress object,
Relation depRel,
int32  flags 
)
static

Referenced by deleteObjectsInList().

◆ doDeletion()

static void doDeletion ( const ObjectAddress object,
int  flags 
)
static

Definition at line 1402 of file dependency.c.

1403{
1404 switch (object->classId)
1405 {
1406 case RelationRelationId:
1407 {
1408 char relKind = get_rel_relkind(object->objectId);
1409
1410 if (relKind == RELKIND_INDEX ||
1411 relKind == RELKIND_PARTITIONED_INDEX)
1412 {
1413 bool concurrent = ((flags & PERFORM_DELETION_CONCURRENTLY) != 0);
1414 bool concurrent_lock_mode = ((flags & PERFORM_DELETION_CONCURRENT_LOCK) != 0);
1415
1416 Assert(object->objectSubId == 0);
1417 index_drop(object->objectId, concurrent, concurrent_lock_mode);
1418 }
1419 else
1420 {
1421 if (object->objectSubId != 0)
1423 object->objectSubId);
1424 else
1426 }
1427
1428 /*
1429 * for a sequence, in addition to dropping the heap, also
1430 * delete pg_sequence tuple
1431 */
1432 if (relKind == RELKIND_SEQUENCE)
1434 break;
1435 }
1436
1437 case ProcedureRelationId:
1439 break;
1440
1441 case TypeRelationId:
1442 RemoveTypeById(object->objectId);
1443 break;
1444
1445 case ConstraintRelationId:
1447 break;
1448
1449 case AttrDefaultRelationId:
1451 break;
1452
1453 case LargeObjectRelationId:
1454 LargeObjectDrop(object->objectId);
1455 break;
1456
1457 case OperatorRelationId:
1459 break;
1460
1461 case RewriteRelationId:
1463 break;
1464
1465 case TriggerRelationId:
1466 RemoveTriggerById(object->objectId);
1467 break;
1468
1469 case StatisticExtRelationId:
1471 break;
1472
1473 case TSConfigRelationId:
1475 break;
1476
1477 case ExtensionRelationId:
1479 break;
1480
1481 case PolicyRelationId:
1482 RemovePolicyById(object->objectId);
1483 break;
1484
1485 case PublicationNamespaceRelationId:
1487 break;
1488
1489 case PublicationRelRelationId:
1491 break;
1492
1493 case PublicationRelationId:
1495 break;
1496
1497 case CastRelationId:
1498 case CollationRelationId:
1499 case ConversionRelationId:
1500 case LanguageRelationId:
1501 case OperatorClassRelationId:
1502 case OperatorFamilyRelationId:
1503 case AccessMethodRelationId:
1504 case AccessMethodOperatorRelationId:
1505 case AccessMethodProcedureRelationId:
1506 case NamespaceRelationId:
1507 case TSParserRelationId:
1508 case TSDictionaryRelationId:
1509 case TSTemplateRelationId:
1510 case ForeignDataWrapperRelationId:
1511 case ForeignServerRelationId:
1512 case UserMappingRelationId:
1513 case DefaultAclRelationId:
1514 case EventTriggerRelationId:
1515 case TransformRelationId:
1516 case AuthMemRelationId:
1517 DropObjectById(object);
1518 break;
1519
1520 /*
1521 * These global object types are not supported here.
1522 */
1523 case AuthIdRelationId:
1524 case DatabaseRelationId:
1525 case TableSpaceRelationId:
1526 case SubscriptionRelationId:
1527 case ParameterAclRelationId:
1528 elog(ERROR, "global objects cannot be deleted by doDeletion");
1529 break;
1530
1531 default:
1532 elog(ERROR, "unsupported object class: %u", object->classId);
1533 }
1534}
void DeleteSequenceTuple(Oid relid)
Definition: sequence.c:571
static void DropObjectById(const ObjectAddress *object)
Definition: dependency.c:1239
#define PERFORM_DELETION_CONCURRENT_LOCK
Definition: dependency.h:97
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
void RemoveExtensionById(Oid extId)
Definition: extension.c:2151
void RemoveFunctionById(Oid funcOid)
void RemoveAttributeById(Oid relid, AttrNumber attnum)
Definition: heap.c:1683
void heap_drop_with_catalog(Oid relid)
Definition: heap.c:1784
void index_drop(Oid indexId, bool concurrent, bool concurrent_lock_mode)
Definition: index.c:2122
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:2168
void RemoveOperatorById(Oid operOid)
Definition: operatorcmds.c:413
void RemoveAttrDefaultById(Oid attrdefId)
Definition: pg_attrdef.c:208
void RemoveConstraintById(Oid conId)
void LargeObjectDrop(Oid loid)
void RemovePolicyById(Oid policy_id)
Definition: policy.c:332
void RemovePublicationSchemaById(Oid psoid)
void RemovePublicationById(Oid pubid)
void RemovePublicationRelById(Oid proid)
void RemoveRewriteRuleById(Oid ruleOid)
Definition: rewriteRemove.c:33
void RemoveStatisticsById(Oid statsOid)
Definition: statscmds.c:787
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1291
void RemoveTSConfigurationById(Oid cfgId)
Definition: tsearchcmds.c:1108
void RemoveTypeById(Oid typeOid)
Definition: typecmds.c:657

References Assert(), ObjectAddress::classId, DeleteSequenceTuple(), DropObjectById(), elog, ERROR, ObjectAddressStack::flags, get_rel_relkind(), heap_drop_with_catalog(), index_drop(), LargeObjectDrop(), ObjectAddressStack::object, ObjectAddress::objectId, ObjectAddress::objectSubId, PERFORM_DELETION_CONCURRENT_LOCK, PERFORM_DELETION_CONCURRENTLY, RemoveAttrDefaultById(), RemoveAttributeById(), RemoveConstraintById(), RemoveExtensionById(), RemoveFunctionById(), RemoveOperatorById(), RemovePolicyById(), RemovePublicationById(), RemovePublicationRelById(), RemovePublicationSchemaById(), RemoveRewriteRuleById(), RemoveStatisticsById(), RemoveTriggerById(), RemoveTSConfigurationById(), and RemoveTypeById().

Referenced by deleteOneObject().

◆ DropObjectById()

static void DropObjectById ( const ObjectAddress object)
static

Definition at line 1239 of file dependency.c.

1240{
1241 int cacheId;
1242 Relation rel;
1243 HeapTuple tup;
1244
1245 cacheId = get_object_catcache_oid(object->classId);
1246
1247 rel = table_open(object->classId, RowExclusiveLock);
1248
1249 /*
1250 * Use the system cache for the oid column, if one exists.
1251 */
1252 if (cacheId >= 0)
1253 {
1254 tup = SearchSysCache1(cacheId, ObjectIdGetDatum(object->objectId));
1255 if (!HeapTupleIsValid(tup))
1256 elog(ERROR, "cache lookup failed for %s %u",
1257 get_object_class_descr(object->classId), object->objectId);
1258
1259 CatalogTupleDelete(rel, &tup->t_self);
1260
1261 ReleaseSysCache(tup);
1262 }
1263 else
1264 {
1265 ScanKeyData skey[1];
1266 SysScanDesc scan;
1267
1268 ScanKeyInit(&skey[0],
1270 BTEqualStrategyNumber, F_OIDEQ,
1271 ObjectIdGetDatum(object->objectId));
1272
1273 scan = systable_beginscan(rel, get_object_oid_index(object->classId), true,
1274 NULL, 1, skey);
1275
1276 /* we expect exactly one match */
1277 tup = systable_getnext(scan);
1278 if (!HeapTupleIsValid(tup))
1279 elog(ERROR, "could not find tuple for %s %u",
1280 get_object_class_descr(object->classId), object->objectId);
1281
1282 CatalogTupleDelete(rel, &tup->t_self);
1283
1284 systable_endscan(scan);
1285 }
1286
1288}
AttrNumber get_object_attnum_oid(Oid class_id)
const char * get_object_class_descr(Oid class_id)
int get_object_catcache_oid(Oid class_id)
Oid get_object_oid_index(Oid class_id)
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220

References BTEqualStrategyNumber, CatalogTupleDelete(), ObjectAddress::classId, elog, ERROR, get_object_attnum_oid(), get_object_catcache_oid(), get_object_class_descr(), get_object_oid_index(), HeapTupleIsValid, ObjectAddressStack::object, ObjectAddress::objectId, ObjectIdGetDatum(), ReleaseSysCache(), RowExclusiveLock, ScanKeyInit(), SearchSysCache1(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().

◆ eliminate_duplicate_dependencies()

static void eliminate_duplicate_dependencies ( ObjectAddresses addrs)
static

Definition at line 2560 of file dependency.c.

2561{
2562 ObjectAddress *priorobj;
2563 int oldref,
2564 newrefs;
2565
2566 /*
2567 * We can't sort if the array has "extra" data, because there's no way to
2568 * keep it in sync. Fortunately that combination of features is not
2569 * needed.
2570 */
2571 Assert(!addrs->extras);
2572
2573 if (addrs->numrefs <= 1)
2574 return; /* nothing to do */
2575
2576 /* Sort the refs so that duplicates are adjacent */
2577 qsort(addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
2579
2580 /* Remove dups */
2581 priorobj = addrs->refs;
2582 newrefs = 1;
2583 for (oldref = 1; oldref < addrs->numrefs; oldref++)
2584 {
2585 ObjectAddress *thisobj = addrs->refs + oldref;
2586
2587 if (priorobj->classId == thisobj->classId &&
2588 priorobj->objectId == thisobj->objectId)
2589 {
2590 if (priorobj->objectSubId == thisobj->objectSubId)
2591 continue; /* identical, so drop thisobj */
2592
2593 /*
2594 * If we have a whole-object reference and a reference to a part
2595 * of the same object, we don't need the whole-object reference
2596 * (for example, we don't need to reference both table foo and
2597 * column foo.bar). The whole-object reference will always appear
2598 * first in the sorted list.
2599 */
2600 if (priorobj->objectSubId == 0)
2601 {
2602 /* replace whole ref with partial */
2603 priorobj->objectSubId = thisobj->objectSubId;
2604 continue;
2605 }
2606 }
2607 /* Not identical, so add thisobj to output set */
2608 priorobj++;
2609 *priorobj = *thisobj;
2610 newrefs++;
2611 }
2612
2613 addrs->numrefs = newrefs;
2614}
static int object_address_comparator(const void *a, const void *b)
Definition: dependency.c:2620
#define qsort(a, b, c, d)
Definition: port.h:499

References Assert(), ObjectAddress::classId, ObjectAddresses::extras, ObjectAddresses::numrefs, object_address_comparator(), ObjectAddress::objectId, ObjectAddress::objectSubId, qsort, and ObjectAddresses::refs.

Referenced by record_object_address_dependencies(), recordDependencyOnExpr(), and recordDependencyOnSingleRelExpr().

◆ find_expr_references_walker()

static bool find_expr_references_walker ( Node node,
find_expr_references_context context 
)
static

Definition at line 1780 of file dependency.c.

1782{
1783 if (node == NULL)
1784 return false;
1785 if (IsA(node, Var))
1786 {
1787 Var *var = (Var *) node;
1788 List *rtable;
1789 RangeTblEntry *rte;
1790
1791 /* Find matching rtable entry, or complain if not found */
1792 if (var->varlevelsup >= list_length(context->rtables))
1793 elog(ERROR, "invalid varlevelsup %d", var->varlevelsup);
1794 rtable = (List *) list_nth(context->rtables, var->varlevelsup);
1795 if (var->varno <= 0 || var->varno > list_length(rtable))
1796 elog(ERROR, "invalid varno %d", var->varno);
1797 rte = rt_fetch(var->varno, rtable);
1798
1799 /*
1800 * A whole-row Var references no specific columns, so adds no new
1801 * dependency. (We assume that there is a whole-table dependency
1802 * arising from each underlying rangetable entry. While we could
1803 * record such a dependency when finding a whole-row Var that
1804 * references a relation directly, it's quite unclear how to extend
1805 * that to whole-row Vars for JOINs, so it seems better to leave the
1806 * responsibility with the range table. Note that this poses some
1807 * risks for identifying dependencies of stand-alone expressions:
1808 * whole-table references may need to be created separately.)
1809 */
1810 if (var->varattno == InvalidAttrNumber)
1811 return false;
1812 if (rte->rtekind == RTE_RELATION)
1813 {
1814 /* If it's a plain relation, reference this column */
1815 add_object_address(RelationRelationId, rte->relid, var->varattno,
1816 context->addrs);
1817 }
1818 else if (rte->rtekind == RTE_FUNCTION)
1819 {
1820 /* Might need to add a dependency on a composite type's column */
1821 /* (done out of line, because it's a bit bulky) */
1822 process_function_rte_ref(rte, var->varattno, context);
1823 }
1824
1825 /*
1826 * Vars referencing other RTE types require no additional work. In
1827 * particular, a join alias Var can be ignored, because it must
1828 * reference a merged USING column. The relevant join input columns
1829 * will also be referenced in the join qual, and any type coercion
1830 * functions involved in the alias expression will be dealt with when
1831 * we scan the RTE itself.
1832 */
1833 return false;
1834 }
1835 else if (IsA(node, Const))
1836 {
1837 Const *con = (Const *) node;
1838 Oid objoid;
1839
1840 /* A constant must depend on the constant's datatype */
1841 add_object_address(TypeRelationId, con->consttype, 0,
1842 context->addrs);
1843
1844 /*
1845 * We must also depend on the constant's collation: it could be
1846 * different from the datatype's, if a CollateExpr was const-folded to
1847 * a simple constant. However we can save work in the most common
1848 * case where the collation is "default", since we know that's pinned.
1849 */
1850 if (OidIsValid(con->constcollid) &&
1851 con->constcollid != DEFAULT_COLLATION_OID)
1852 add_object_address(CollationRelationId, con->constcollid, 0,
1853 context->addrs);
1854
1855 /*
1856 * If it's a regclass or similar literal referring to an existing
1857 * object, add a reference to that object. (Currently, only the
1858 * regclass and regconfig cases have any likely use, but we may as
1859 * well handle all the OID-alias datatypes consistently.)
1860 */
1861 if (!con->constisnull)
1862 {
1863 switch (con->consttype)
1864 {
1865 case REGPROCOID:
1866 case REGPROCEDUREOID:
1867 objoid = DatumGetObjectId(con->constvalue);
1868 if (SearchSysCacheExists1(PROCOID,
1869 ObjectIdGetDatum(objoid)))
1870 add_object_address(ProcedureRelationId, objoid, 0,
1871 context->addrs);
1872 break;
1873 case REGOPEROID:
1874 case REGOPERATOROID:
1875 objoid = DatumGetObjectId(con->constvalue);
1876 if (SearchSysCacheExists1(OPEROID,
1877 ObjectIdGetDatum(objoid)))
1878 add_object_address(OperatorRelationId, objoid, 0,
1879 context->addrs);
1880 break;
1881 case REGCLASSOID:
1882 objoid = DatumGetObjectId(con->constvalue);
1883 if (SearchSysCacheExists1(RELOID,
1884 ObjectIdGetDatum(objoid)))
1885 add_object_address(RelationRelationId, objoid, 0,
1886 context->addrs);
1887 break;
1888 case REGTYPEOID:
1889 objoid = DatumGetObjectId(con->constvalue);
1890 if (SearchSysCacheExists1(TYPEOID,
1891 ObjectIdGetDatum(objoid)))
1892 add_object_address(TypeRelationId, objoid, 0,
1893 context->addrs);
1894 break;
1895 case REGCOLLATIONOID:
1896 objoid = DatumGetObjectId(con->constvalue);
1897 if (SearchSysCacheExists1(COLLOID,
1898 ObjectIdGetDatum(objoid)))
1899 add_object_address(CollationRelationId, objoid, 0,
1900 context->addrs);
1901 break;
1902 case REGCONFIGOID:
1903 objoid = DatumGetObjectId(con->constvalue);
1904 if (SearchSysCacheExists1(TSCONFIGOID,
1905 ObjectIdGetDatum(objoid)))
1906 add_object_address(TSConfigRelationId, objoid, 0,
1907 context->addrs);
1908 break;
1909 case REGDICTIONARYOID:
1910 objoid = DatumGetObjectId(con->constvalue);
1911 if (SearchSysCacheExists1(TSDICTOID,
1912 ObjectIdGetDatum(objoid)))
1913 add_object_address(TSDictionaryRelationId, objoid, 0,
1914 context->addrs);
1915 break;
1916
1917 case REGNAMESPACEOID:
1918 objoid = DatumGetObjectId(con->constvalue);
1919 if (SearchSysCacheExists1(NAMESPACEOID,
1920 ObjectIdGetDatum(objoid)))
1921 add_object_address(NamespaceRelationId, objoid, 0,
1922 context->addrs);
1923 break;
1924
1925 /*
1926 * Dependencies for regrole should be shared among all
1927 * databases, so explicitly inhibit to have dependencies.
1928 */
1929 case REGROLEOID:
1930 ereport(ERROR,
1931 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1932 errmsg("constant of the type %s cannot be used here",
1933 "regrole")));
1934 break;
1935
1936 /*
1937 * Dependencies for regdatabase should be shared among all
1938 * databases, so explicitly inhibit to have dependencies.
1939 */
1940 case REGDATABASEOID:
1941 ereport(ERROR,
1942 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1943 errmsg("constant of the type %s cannot be used here",
1944 "regdatabase")));
1945 break;
1946 }
1947 }
1948 return false;
1949 }
1950 else if (IsA(node, Param))
1951 {
1952 Param *param = (Param *) node;
1953
1954 /* A parameter must depend on the parameter's datatype */
1955 add_object_address(TypeRelationId, param->paramtype, 0,
1956 context->addrs);
1957 /* and its collation, just as for Consts */
1958 if (OidIsValid(param->paramcollid) &&
1959 param->paramcollid != DEFAULT_COLLATION_OID)
1960 add_object_address(CollationRelationId, param->paramcollid, 0,
1961 context->addrs);
1962 }
1963 else if (IsA(node, FuncExpr))
1964 {
1965 FuncExpr *funcexpr = (FuncExpr *) node;
1966
1967 add_object_address(ProcedureRelationId, funcexpr->funcid, 0,
1968 context->addrs);
1969 /* fall through to examine arguments */
1970 }
1971 else if (IsA(node, OpExpr))
1972 {
1973 OpExpr *opexpr = (OpExpr *) node;
1974
1975 add_object_address(OperatorRelationId, opexpr->opno, 0,
1976 context->addrs);
1977 /* fall through to examine arguments */
1978 }
1979 else if (IsA(node, DistinctExpr))
1980 {
1981 DistinctExpr *distinctexpr = (DistinctExpr *) node;
1982
1983 add_object_address(OperatorRelationId, distinctexpr->opno, 0,
1984 context->addrs);
1985 /* fall through to examine arguments */
1986 }
1987 else if (IsA(node, NullIfExpr))
1988 {
1989 NullIfExpr *nullifexpr = (NullIfExpr *) node;
1990
1991 add_object_address(OperatorRelationId, nullifexpr->opno, 0,
1992 context->addrs);
1993 /* fall through to examine arguments */
1994 }
1995 else if (IsA(node, ScalarArrayOpExpr))
1996 {
1997 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
1998
1999 add_object_address(OperatorRelationId, opexpr->opno, 0,
2000 context->addrs);
2001 /* fall through to examine arguments */
2002 }
2003 else if (IsA(node, Aggref))
2004 {
2005 Aggref *aggref = (Aggref *) node;
2006
2007 add_object_address(ProcedureRelationId, aggref->aggfnoid, 0,
2008 context->addrs);
2009 /* fall through to examine arguments */
2010 }
2011 else if (IsA(node, WindowFunc))
2012 {
2013 WindowFunc *wfunc = (WindowFunc *) node;
2014
2015 add_object_address(ProcedureRelationId, wfunc->winfnoid, 0,
2016 context->addrs);
2017 /* fall through to examine arguments */
2018 }
2019 else if (IsA(node, SubscriptingRef))
2020 {
2021 SubscriptingRef *sbsref = (SubscriptingRef *) node;
2022
2023 /*
2024 * The refexpr should provide adequate dependency on refcontainertype,
2025 * and that type in turn depends on refelemtype. However, a custom
2026 * subscripting handler might set refrestype to something different
2027 * from either of those, in which case we'd better record it.
2028 */
2029 if (sbsref->refrestype != sbsref->refcontainertype &&
2030 sbsref->refrestype != sbsref->refelemtype)
2031 add_object_address(TypeRelationId, sbsref->refrestype, 0,
2032 context->addrs);
2033 /* fall through to examine arguments */
2034 }
2035 else if (IsA(node, SubPlan))
2036 {
2037 /* Extra work needed here if we ever need this case */
2038 elog(ERROR, "already-planned subqueries not supported");
2039 }
2040 else if (IsA(node, FieldSelect))
2041 {
2042 FieldSelect *fselect = (FieldSelect *) node;
2043 Oid argtype = getBaseType(exprType((Node *) fselect->arg));
2044 Oid reltype = get_typ_typrelid(argtype);
2045
2046 /*
2047 * We need a dependency on the specific column named in FieldSelect,
2048 * assuming we can identify the pg_class OID for it. (Probably we
2049 * always can at the moment, but in future it might be possible for
2050 * argtype to be RECORDOID.) If we can make a column dependency then
2051 * we shouldn't need a dependency on the column's type; but if we
2052 * can't, make a dependency on the type, as it might not appear
2053 * anywhere else in the expression.
2054 */
2055 if (OidIsValid(reltype))
2056 add_object_address(RelationRelationId, reltype, fselect->fieldnum,
2057 context->addrs);
2058 else
2059 add_object_address(TypeRelationId, fselect->resulttype, 0,
2060 context->addrs);
2061 /* the collation might not be referenced anywhere else, either */
2062 if (OidIsValid(fselect->resultcollid) &&
2063 fselect->resultcollid != DEFAULT_COLLATION_OID)
2064 add_object_address(CollationRelationId, fselect->resultcollid, 0,
2065 context->addrs);
2066 }
2067 else if (IsA(node, FieldStore))
2068 {
2069 FieldStore *fstore = (FieldStore *) node;
2070 Oid reltype = get_typ_typrelid(fstore->resulttype);
2071
2072 /* similar considerations to FieldSelect, but multiple column(s) */
2073 if (OidIsValid(reltype))
2074 {
2075 ListCell *l;
2076
2077 foreach(l, fstore->fieldnums)
2078 add_object_address(RelationRelationId, reltype, lfirst_int(l),
2079 context->addrs);
2080 }
2081 else
2082 add_object_address(TypeRelationId, fstore->resulttype, 0,
2083 context->addrs);
2084 }
2085 else if (IsA(node, RelabelType))
2086 {
2087 RelabelType *relab = (RelabelType *) node;
2088
2089 /* since there is no function dependency, need to depend on type */
2090 add_object_address(TypeRelationId, relab->resulttype, 0,
2091 context->addrs);
2092 /* the collation might not be referenced anywhere else, either */
2093 if (OidIsValid(relab->resultcollid) &&
2094 relab->resultcollid != DEFAULT_COLLATION_OID)
2095 add_object_address(CollationRelationId, relab->resultcollid, 0,
2096 context->addrs);
2097 }
2098 else if (IsA(node, CoerceViaIO))
2099 {
2100 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
2101
2102 /* since there is no exposed function, need to depend on type */
2103 add_object_address(TypeRelationId, iocoerce->resulttype, 0,
2104 context->addrs);
2105 /* the collation might not be referenced anywhere else, either */
2106 if (OidIsValid(iocoerce->resultcollid) &&
2107 iocoerce->resultcollid != DEFAULT_COLLATION_OID)
2108 add_object_address(CollationRelationId, iocoerce->resultcollid, 0,
2109 context->addrs);
2110 }
2111 else if (IsA(node, ArrayCoerceExpr))
2112 {
2113 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
2114
2115 /* as above, depend on type */
2116 add_object_address(TypeRelationId, acoerce->resulttype, 0,
2117 context->addrs);
2118 /* the collation might not be referenced anywhere else, either */
2119 if (OidIsValid(acoerce->resultcollid) &&
2120 acoerce->resultcollid != DEFAULT_COLLATION_OID)
2121 add_object_address(CollationRelationId, acoerce->resultcollid, 0,
2122 context->addrs);
2123 /* fall through to examine arguments */
2124 }
2125 else if (IsA(node, ConvertRowtypeExpr))
2126 {
2127 ConvertRowtypeExpr *cvt = (ConvertRowtypeExpr *) node;
2128
2129 /* since there is no function dependency, need to depend on type */
2130 add_object_address(TypeRelationId, cvt->resulttype, 0,
2131 context->addrs);
2132 }
2133 else if (IsA(node, CollateExpr))
2134 {
2135 CollateExpr *coll = (CollateExpr *) node;
2136
2137 add_object_address(CollationRelationId, coll->collOid, 0,
2138 context->addrs);
2139 }
2140 else if (IsA(node, RowExpr))
2141 {
2142 RowExpr *rowexpr = (RowExpr *) node;
2143
2144 add_object_address(TypeRelationId, rowexpr->row_typeid, 0,
2145 context->addrs);
2146 }
2147 else if (IsA(node, RowCompareExpr))
2148 {
2149 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2150 ListCell *l;
2151
2152 foreach(l, rcexpr->opnos)
2153 {
2154 add_object_address(OperatorRelationId, lfirst_oid(l), 0,
2155 context->addrs);
2156 }
2157 foreach(l, rcexpr->opfamilies)
2158 {
2159 add_object_address(OperatorFamilyRelationId, lfirst_oid(l), 0,
2160 context->addrs);
2161 }
2162 /* fall through to examine arguments */
2163 }
2164 else if (IsA(node, CoerceToDomain))
2165 {
2166 CoerceToDomain *cd = (CoerceToDomain *) node;
2167
2168 add_object_address(TypeRelationId, cd->resulttype, 0,
2169 context->addrs);
2170 }
2171 else if (IsA(node, NextValueExpr))
2172 {
2173 NextValueExpr *nve = (NextValueExpr *) node;
2174
2175 add_object_address(RelationRelationId, nve->seqid, 0,
2176 context->addrs);
2177 }
2178 else if (IsA(node, OnConflictExpr))
2179 {
2180 OnConflictExpr *onconflict = (OnConflictExpr *) node;
2181
2182 if (OidIsValid(onconflict->constraint))
2183 add_object_address(ConstraintRelationId, onconflict->constraint, 0,
2184 context->addrs);
2185 /* fall through to examine arguments */
2186 }
2187 else if (IsA(node, SortGroupClause))
2188 {
2189 SortGroupClause *sgc = (SortGroupClause *) node;
2190
2191 add_object_address(OperatorRelationId, sgc->eqop, 0,
2192 context->addrs);
2193 if (OidIsValid(sgc->sortop))
2194 add_object_address(OperatorRelationId, sgc->sortop, 0,
2195 context->addrs);
2196 return false;
2197 }
2198 else if (IsA(node, WindowClause))
2199 {
2200 WindowClause *wc = (WindowClause *) node;
2201
2202 if (OidIsValid(wc->startInRangeFunc))
2203 add_object_address(ProcedureRelationId, wc->startInRangeFunc, 0,
2204 context->addrs);
2205 if (OidIsValid(wc->endInRangeFunc))
2206 add_object_address(ProcedureRelationId, wc->endInRangeFunc, 0,
2207 context->addrs);
2208 if (OidIsValid(wc->inRangeColl) &&
2209 wc->inRangeColl != DEFAULT_COLLATION_OID)
2210 add_object_address(CollationRelationId, wc->inRangeColl, 0,
2211 context->addrs);
2212 /* fall through to examine substructure */
2213 }
2214 else if (IsA(node, CTECycleClause))
2215 {
2216 CTECycleClause *cc = (CTECycleClause *) node;
2217
2218 if (OidIsValid(cc->cycle_mark_type))
2219 add_object_address(TypeRelationId, cc->cycle_mark_type, 0,
2220 context->addrs);
2222 add_object_address(CollationRelationId, cc->cycle_mark_collation, 0,
2223 context->addrs);
2224 if (OidIsValid(cc->cycle_mark_neop))
2225 add_object_address(OperatorRelationId, cc->cycle_mark_neop, 0,
2226 context->addrs);
2227 /* fall through to examine substructure */
2228 }
2229 else if (IsA(node, Query))
2230 {
2231 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
2232 Query *query = (Query *) node;
2233 ListCell *lc;
2234 bool result;
2235
2236 /*
2237 * Add whole-relation refs for each plain relation mentioned in the
2238 * subquery's rtable, and ensure we add refs for any type-coercion
2239 * functions used in join alias lists.
2240 *
2241 * Note: query_tree_walker takes care of recursing into RTE_FUNCTION
2242 * RTEs, subqueries, etc, so no need to do that here. But we must
2243 * tell it not to visit join alias lists, or we'll add refs for join
2244 * input columns whether or not they are actually used in our query.
2245 *
2246 * Note: we don't need to worry about collations mentioned in
2247 * RTE_VALUES or RTE_CTE RTEs, because those must just duplicate
2248 * collations referenced in other parts of the Query. We do have to
2249 * worry about collations mentioned in RTE_FUNCTION, but we take care
2250 * of those when we recurse to the RangeTblFunction node(s).
2251 */
2252 foreach(lc, query->rtable)
2253 {
2254 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
2255
2256 switch (rte->rtekind)
2257 {
2258 case RTE_RELATION:
2259 add_object_address(RelationRelationId, rte->relid, 0,
2260 context->addrs);
2261 break;
2262 case RTE_JOIN:
2263
2264 /*
2265 * Examine joinaliasvars entries only for merged JOIN
2266 * USING columns. Only those entries could contain
2267 * type-coercion functions. Also, their join input
2268 * columns must be referenced in the join quals, so this
2269 * won't accidentally add refs to otherwise-unused join
2270 * input columns. (We want to ref the type coercion
2271 * functions even if the merged column isn't explicitly
2272 * used anywhere, to protect possible expansion of the
2273 * join RTE as a whole-row var, and because it seems like
2274 * a bad idea to allow dropping a function that's present
2275 * in our query tree, whether or not it could get called.)
2276 */
2277 context->rtables = lcons(query->rtable, context->rtables);
2278 for (int i = 0; i < rte->joinmergedcols; i++)
2279 {
2280 Node *aliasvar = list_nth(rte->joinaliasvars, i);
2281
2282 if (!IsA(aliasvar, Var))
2283 find_expr_references_walker(aliasvar, context);
2284 }
2285 context->rtables = list_delete_first(context->rtables);
2286 break;
2288
2289 /*
2290 * Cataloged objects cannot depend on tuplestores, because
2291 * those have no cataloged representation. For now we can
2292 * call the tuplestore a "transition table" because that's
2293 * the only kind exposed to SQL, but someday we might have
2294 * to work harder.
2295 */
2296 ereport(ERROR,
2297 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2298 errmsg("transition table \"%s\" cannot be referenced in a persistent object",
2299 rte->eref->aliasname)));
2300 break;
2301 default:
2302 /* Other RTE types can be ignored here */
2303 break;
2304 }
2305 }
2306
2307 /*
2308 * If the query is an INSERT or UPDATE, we should create a dependency
2309 * on each target column, to prevent the specific target column from
2310 * being dropped. Although we will visit the TargetEntry nodes again
2311 * during query_tree_walker, we won't have enough context to do this
2312 * conveniently, so do it here.
2313 */
2314 if (query->commandType == CMD_INSERT ||
2315 query->commandType == CMD_UPDATE)
2316 {
2317 RangeTblEntry *rte;
2318
2319 if (query->resultRelation <= 0 ||
2320 query->resultRelation > list_length(query->rtable))
2321 elog(ERROR, "invalid resultRelation %d",
2322 query->resultRelation);
2323 rte = rt_fetch(query->resultRelation, query->rtable);
2324 if (rte->rtekind == RTE_RELATION)
2325 {
2326 foreach(lc, query->targetList)
2327 {
2328 TargetEntry *tle = (TargetEntry *) lfirst(lc);
2329
2330 if (tle->resjunk)
2331 continue; /* ignore junk tlist items */
2332 add_object_address(RelationRelationId, rte->relid, tle->resno,
2333 context->addrs);
2334 }
2335 }
2336 }
2337
2338 /*
2339 * Add dependencies on constraints listed in query's constraintDeps
2340 */
2341 foreach(lc, query->constraintDeps)
2342 {
2343 add_object_address(ConstraintRelationId, lfirst_oid(lc), 0,
2344 context->addrs);
2345 }
2346
2347 /* Examine substructure of query */
2348 context->rtables = lcons(query->rtable, context->rtables);
2349 result = query_tree_walker(query,
2351 context,
2354 context->rtables = list_delete_first(context->rtables);
2355 return result;
2356 }
2357 else if (IsA(node, SetOperationStmt))
2358 {
2359 SetOperationStmt *setop = (SetOperationStmt *) node;
2360
2361 /* we need to look at the groupClauses for operator references */
2362 find_expr_references_walker((Node *) setop->groupClauses, context);
2363 /* fall through to examine child nodes */
2364 }
2365 else if (IsA(node, RangeTblFunction))
2366 {
2367 RangeTblFunction *rtfunc = (RangeTblFunction *) node;
2368 ListCell *ct;
2369
2370 /*
2371 * Add refs for any datatypes and collations used in a column
2372 * definition list for a RECORD function. (For other cases, it should
2373 * be enough to depend on the function itself.)
2374 */
2375 foreach(ct, rtfunc->funccoltypes)
2376 {
2377 add_object_address(TypeRelationId, lfirst_oid(ct), 0,
2378 context->addrs);
2379 }
2380 foreach(ct, rtfunc->funccolcollations)
2381 {
2382 Oid collid = lfirst_oid(ct);
2383
2384 if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
2385 add_object_address(CollationRelationId, collid, 0,
2386 context->addrs);
2387 }
2388 }
2389 else if (IsA(node, TableFunc))
2390 {
2391 TableFunc *tf = (TableFunc *) node;
2392 ListCell *ct;
2393
2394 /*
2395 * Add refs for the datatypes and collations used in the TableFunc.
2396 */
2397 foreach(ct, tf->coltypes)
2398 {
2399 add_object_address(TypeRelationId, lfirst_oid(ct), 0,
2400 context->addrs);
2401 }
2402 foreach(ct, tf->colcollations)
2403 {
2404 Oid collid = lfirst_oid(ct);
2405
2406 if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
2407 add_object_address(CollationRelationId, collid, 0,
2408 context->addrs);
2409 }
2410 }
2411 else if (IsA(node, TableSampleClause))
2412 {
2413 TableSampleClause *tsc = (TableSampleClause *) node;
2414
2415 add_object_address(ProcedureRelationId, tsc->tsmhandler, 0,
2416 context->addrs);
2417 /* fall through to examine arguments */
2418 }
2419
2421 context);
2422}
#define InvalidAttrNumber
Definition: attnum.h:23
#define OidIsValid(objectId)
Definition: c.h:788
Oid collid
static void add_object_address(Oid classId, Oid objectId, int32 subId, ObjectAddresses *addrs)
Definition: dependency.c:2682
static void process_function_rte_ref(RangeTblEntry *rte, AttrNumber attnum, find_expr_references_context *context)
Definition: dependency.c:2429
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ereport(elevel,...)
Definition: elog.h:150
List * list_delete_first(List *list)
Definition: list.c:943
List * lcons(void *datum, List *list)
Definition: list.c:495
Oid get_typ_typrelid(Oid typid)
Definition: lsyscache.c:2896
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2686
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:158
#define QTW_EXAMINE_SORTGROUP
Definition: nodeFuncs.h:30
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:153
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:25
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
@ CMD_INSERT
Definition: nodes.h:277
@ CMD_UPDATE
Definition: nodes.h:276
@ RTE_JOIN
Definition: parsenodes.h:1071
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1076
@ RTE_FUNCTION
Definition: parsenodes.h:1072
@ RTE_RELATION
Definition: parsenodes.h:1069
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define lfirst_int(lc)
Definition: pg_list.h:173
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define lfirst_oid(lc)
Definition: pg_list.h:174
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:252
unsigned int Oid
Definition: postgres_ext.h:32
Oid aggfnoid
Definition: primnodes.h:463
Oid cycle_mark_collation
Definition: parsenodes.h:1721
Oid resulttype
Definition: primnodes.h:1241
Oid consttype
Definition: primnodes.h:329
AttrNumber fieldnum
Definition: primnodes.h:1162
Expr * arg
Definition: primnodes.h:1161
Oid funcid
Definition: primnodes.h:782
Definition: pg_list.h:54
Definition: nodes.h:135
Oid opno
Definition: primnodes.h:850
Oid paramtype
Definition: primnodes.h:397
Oid paramcollid
Definition: primnodes.h:401
List * rtable
Definition: parsenodes.h:175
CmdType commandType
Definition: parsenodes.h:121
List * targetList
Definition: parsenodes.h:198
RTEKind rtekind
Definition: parsenodes.h:1104
Oid resulttype
Definition: primnodes.h:1218
AttrNumber resno
Definition: primnodes.h:2241
Definition: primnodes.h:262
AttrNumber varattno
Definition: primnodes.h:274
int varno
Definition: primnodes.h:269
Index varlevelsup
Definition: primnodes.h:294
Oid winfnoid
Definition: primnodes.h:597
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:100

References add_object_address(), find_expr_references_context::addrs, Aggref::aggfnoid, FieldSelect::arg, CMD_INSERT, CMD_UPDATE, collid, CollateExpr::collOid, Query::commandType, OnConflictExpr::constraint, Const::consttype, CTECycleClause::cycle_mark_collation, CTECycleClause::cycle_mark_neop, CTECycleClause::cycle_mark_type, DatumGetObjectId(), elog, SortGroupClause::eqop, ereport, errcode(), errmsg(), ERROR, expression_tree_walker, exprType(), FieldSelect::fieldnum, find_expr_references_walker(), FuncExpr::funcid, get_typ_typrelid(), getBaseType(), i, InvalidAttrNumber, IsA, lcons(), lfirst, lfirst_int, lfirst_oid, list_delete_first(), list_length(), list_nth(), ObjectIdGetDatum(), OidIsValid, OpExpr::opno, ScalarArrayOpExpr::opno, Param::paramcollid, Param::paramtype, process_function_rte_ref(), QTW_EXAMINE_SORTGROUP, QTW_IGNORE_JOINALIASES, query_tree_walker, TargetEntry::resno, RelabelType::resulttype, CoerceViaIO::resulttype, ArrayCoerceExpr::resulttype, ConvertRowtypeExpr::resulttype, CoerceToDomain::resulttype, rt_fetch, Query::rtable, find_expr_references_context::rtables, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RangeTblEntry::rtekind, SearchSysCacheExists1, NextValueExpr::seqid, SortGroupClause::sortop, Query::targetList, TableSampleClause::tsmhandler, Var::varattno, Var::varlevelsup, Var::varno, and WindowFunc::winfnoid.

Referenced by collectDependenciesOfExpr(), find_expr_references_walker(), and recordDependencyOnSingleRelExpr().

◆ find_temp_object()

bool find_temp_object ( const ObjectAddresses addrs,
bool  local_temp_okay,
ObjectAddress foundobj 
)

Definition at line 2503 of file dependency.c.

2505{
2506 for (int i = 0; i < addrs->numrefs; i++)
2507 {
2508 const ObjectAddress *thisobj = addrs->refs + i;
2509 Oid objnamespace;
2510
2511 /*
2512 * Use get_object_namespace() to see if this object belongs to a
2513 * schema. If not, we can skip it.
2514 */
2515 objnamespace = get_object_namespace(thisobj);
2516
2517 /*
2518 * If the object is in a temporary namespace, complain, except if
2519 * local_temp_okay and it's our own temp namespace.
2520 */
2521 if (OidIsValid(objnamespace) && isAnyTempNamespace(objnamespace) &&
2522 !(local_temp_okay && isTempNamespace(objnamespace)))
2523 {
2524 *foundobj = *thisobj;
2525 return true;
2526 }
2527 }
2528 return false;
2529}
bool isTempNamespace(Oid namespaceId)
Definition: namespace.c:3719
bool isAnyTempNamespace(Oid namespaceId)
Definition: namespace.c:3757
Oid get_object_namespace(const ObjectAddress *address)

References get_object_namespace(), i, isAnyTempNamespace(), isTempNamespace(), ObjectAddresses::numrefs, OidIsValid, and ObjectAddresses::refs.

Referenced by ProcedureCreate(), and query_uses_temp_object().

◆ findDependentObjects()

static void findDependentObjects ( const ObjectAddress object,
int  objflags,
int  flags,
ObjectAddressStack stack,
ObjectAddresses targetObjects,
const ObjectAddresses pendingObjects,
Relation depRel 
)
static

Definition at line 483 of file dependency.c.

490{
491 ScanKeyData key[3];
492 int nkeys;
493 SysScanDesc scan;
494 HeapTuple tup;
495 ObjectAddress otherObject;
496 ObjectAddress owningObject;
497 ObjectAddress partitionObject;
498 ObjectAddressAndFlags *dependentObjects;
499 int numDependentObjects;
500 int maxDependentObjects;
501 ObjectAddressStack mystack;
502 ObjectAddressExtra extra;
503
504 /*
505 * If the target object is already being visited in an outer recursion
506 * level, just report the current objflags back to that level and exit.
507 * This is needed to avoid infinite recursion in the face of circular
508 * dependencies.
509 *
510 * The stack check alone would result in dependency loops being broken at
511 * an arbitrary point, ie, the first member object of the loop to be
512 * visited is the last one to be deleted. This is obviously unworkable.
513 * However, the check for internal dependency below guarantees that we
514 * will not break a loop at an internal dependency: if we enter the loop
515 * at an "owned" object we will switch and start at the "owning" object
516 * instead. We could probably hack something up to avoid breaking at an
517 * auto dependency, too, if we had to. However there are no known cases
518 * where that would be necessary.
519 */
520 if (stack_address_present_add_flags(object, objflags, stack))
521 return;
522
523 /*
524 * since this function recurses, it could be driven to stack overflow,
525 * because of the deep dependency tree, not only due to dependency loops.
526 */
528
529 /*
530 * It's also possible that the target object has already been completely
531 * processed and put into targetObjects. If so, again we just add the
532 * specified objflags to its entry and return.
533 *
534 * (Note: in these early-exit cases we could release the caller-taken
535 * lock, since the object is presumably now locked multiple times; but it
536 * seems not worth the cycles.)
537 */
538 if (object_address_present_add_flags(object, objflags, targetObjects))
539 return;
540
541 /*
542 * If the target object is pinned, we can just error out immediately; it
543 * won't have any objects recorded as depending on it.
544 */
545 if (IsPinnedObject(object->classId, object->objectId))
547 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
548 errmsg("cannot drop %s because it is required by the database system",
549 getObjectDescription(object, false))));
550
551 /*
552 * The target object might be internally dependent on some other object
553 * (its "owner"), and/or be a member of an extension (also considered its
554 * owner). If so, and if we aren't recursing from the owning object, we
555 * have to transform this deletion request into a deletion request of the
556 * owning object. (We'll eventually recurse back to this object, but the
557 * owning object has to be visited first so it will be deleted after.) The
558 * way to find out about this is to scan the pg_depend entries that show
559 * what this object depends on.
560 */
561 ScanKeyInit(&key[0],
562 Anum_pg_depend_classid,
563 BTEqualStrategyNumber, F_OIDEQ,
564 ObjectIdGetDatum(object->classId));
565 ScanKeyInit(&key[1],
566 Anum_pg_depend_objid,
567 BTEqualStrategyNumber, F_OIDEQ,
568 ObjectIdGetDatum(object->objectId));
569 if (object->objectSubId != 0)
570 {
571 /* Consider only dependencies of this sub-object */
572 ScanKeyInit(&key[2],
573 Anum_pg_depend_objsubid,
574 BTEqualStrategyNumber, F_INT4EQ,
575 Int32GetDatum(object->objectSubId));
576 nkeys = 3;
577 }
578 else
579 {
580 /* Consider dependencies of this object and any sub-objects it has */
581 nkeys = 2;
582 }
583
584 scan = systable_beginscan(*depRel, DependDependerIndexId, true,
585 NULL, nkeys, key);
586
587 /* initialize variables that loop may fill */
588 memset(&owningObject, 0, sizeof(owningObject));
589 memset(&partitionObject, 0, sizeof(partitionObject));
590
591 while (HeapTupleIsValid(tup = systable_getnext(scan)))
592 {
593 Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
594
595 otherObject.classId = foundDep->refclassid;
596 otherObject.objectId = foundDep->refobjid;
597 otherObject.objectSubId = foundDep->refobjsubid;
598
599 /*
600 * When scanning dependencies of a whole object, we may find rows
601 * linking sub-objects of the object to the object itself. (Normally,
602 * such a dependency is implicit, but we must make explicit ones in
603 * some cases involving partitioning.) We must ignore such rows to
604 * avoid infinite recursion.
605 */
606 if (otherObject.classId == object->classId &&
607 otherObject.objectId == object->objectId &&
608 object->objectSubId == 0)
609 continue;
610
611 switch (foundDep->deptype)
612 {
614 case DEPENDENCY_AUTO:
616 /* no problem */
617 break;
618
620
621 /*
622 * If told to, ignore EXTENSION dependencies altogether. This
623 * flag is normally used to prevent dropping extensions during
624 * temporary-object cleanup, even if a temp object was created
625 * during an extension script.
626 */
628 break;
629
630 /*
631 * If the other object is the extension currently being
632 * created/altered, ignore this dependency and continue with
633 * the deletion. This allows dropping of an extension's
634 * objects within the extension's scripts, as well as corner
635 * cases such as dropping a transient object created within
636 * such a script.
637 */
638 if (creating_extension &&
639 otherObject.classId == ExtensionRelationId &&
640 otherObject.objectId == CurrentExtensionObject)
641 break;
642
643 /* Otherwise, treat this like an internal dependency */
644 /* FALL THRU */
645
647
648 /*
649 * This object is part of the internal implementation of
650 * another object, or is part of the extension that is the
651 * other object. We have three cases:
652 *
653 * 1. At the outermost recursion level, we must disallow the
654 * DROP. However, if the owning object is listed in
655 * pendingObjects, just release the caller's lock and return;
656 * we'll eventually complete the DROP when we reach that entry
657 * in the pending list.
658 *
659 * Note: the above statement is true only if this pg_depend
660 * entry still exists by then; in principle, therefore, we
661 * could miss deleting an item the user told us to delete.
662 * However, no inconsistency can result: since we're at outer
663 * level, there is no object depending on this one.
664 */
665 if (stack == NULL)
666 {
667 if (pendingObjects &&
668 object_address_present(&otherObject, pendingObjects))
669 {
670 systable_endscan(scan);
671 /* need to release caller's lock; see notes below */
672 ReleaseDeletionLock(object);
673 return;
674 }
675
676 /*
677 * We postpone actually issuing the error message until
678 * after this loop, so that we can make the behavior
679 * independent of the ordering of pg_depend entries, at
680 * least if there's not more than one INTERNAL and one
681 * EXTENSION dependency. (If there's more, we'll complain
682 * about a random one of them.) Prefer to complain about
683 * EXTENSION, since that's generally a more important
684 * dependency.
685 */
686 if (!OidIsValid(owningObject.classId) ||
687 foundDep->deptype == DEPENDENCY_EXTENSION)
688 owningObject = otherObject;
689 break;
690 }
691
692 /*
693 * 2. When recursing from the other end of this dependency,
694 * it's okay to continue with the deletion. This holds when
695 * recursing from a whole object that includes the nominal
696 * other end as a component, too. Since there can be more
697 * than one "owning" object, we have to allow matches that are
698 * more than one level down in the stack.
699 */
700 if (stack_address_present_add_flags(&otherObject, 0, stack))
701 break;
702
703 /*
704 * 3. Not all the owning objects have been visited, so
705 * transform this deletion request into a delete of this
706 * owning object.
707 *
708 * First, release caller's lock on this object and get
709 * deletion lock on the owning object. (We must release
710 * caller's lock to avoid deadlock against a concurrent
711 * deletion of the owning object.)
712 */
713 ReleaseDeletionLock(object);
714 AcquireDeletionLock(&otherObject, 0);
715
716 /*
717 * The owning object might have been deleted while we waited
718 * to lock it; if so, neither it nor the current object are
719 * interesting anymore. We test this by checking the
720 * pg_depend entry (see notes below).
721 */
722 if (!systable_recheck_tuple(scan, tup))
723 {
724 systable_endscan(scan);
725 ReleaseDeletionLock(&otherObject);
726 return;
727 }
728
729 /*
730 * One way or the other, we're done with the scan; might as
731 * well close it down before recursing, to reduce peak
732 * resource consumption.
733 */
734 systable_endscan(scan);
735
736 /*
737 * Okay, recurse to the owning object instead of proceeding.
738 *
739 * We do not need to stack the current object; we want the
740 * traversal order to be as if the original reference had
741 * linked to the owning object instead of this one.
742 *
743 * The dependency type is a "reverse" dependency: we need to
744 * delete the owning object if this one is to be deleted, but
745 * this linkage is never a reason for an automatic deletion.
746 */
747 findDependentObjects(&otherObject,
749 flags,
750 stack,
751 targetObjects,
752 pendingObjects,
753 depRel);
754
755 /*
756 * The current target object should have been added to
757 * targetObjects while processing the owning object; but it
758 * probably got only the flag bits associated with the
759 * dependency we're looking at. We need to add the objflags
760 * that were passed to this recursion level, too, else we may
761 * get a bogus failure in reportDependentObjects (if, for
762 * example, we were called due to a partition dependency).
763 *
764 * If somehow the current object didn't get scheduled for
765 * deletion, bleat. (That would imply that somebody deleted
766 * this dependency record before the recursion got to it.)
767 * Another idea would be to reacquire lock on the current
768 * object and resume trying to delete it, but it seems not
769 * worth dealing with the race conditions inherent in that.
770 */
771 if (!object_address_present_add_flags(object, objflags,
772 targetObjects))
773 elog(ERROR, "deletion of owning object %s failed to delete %s",
774 getObjectDescription(&otherObject, false),
775 getObjectDescription(object, false));
776
777 /* And we're done here. */
778 return;
779
781
782 /*
783 * Remember that this object has a partition-type dependency.
784 * After the dependency scan, we'll complain if we didn't find
785 * a reason to delete one of its partition dependencies.
786 */
787 objflags |= DEPFLAG_IS_PART;
788
789 /*
790 * Also remember the primary partition owner, for error
791 * messages. If there are multiple primary owners (which
792 * there should not be), we'll report a random one of them.
793 */
794 partitionObject = otherObject;
795 break;
796
798
799 /*
800 * Only use secondary partition owners in error messages if we
801 * find no primary owner (which probably shouldn't happen).
802 */
803 if (!(objflags & DEPFLAG_IS_PART))
804 partitionObject = otherObject;
805
806 /*
807 * Remember that this object has a partition-type dependency.
808 * After the dependency scan, we'll complain if we didn't find
809 * a reason to delete one of its partition dependencies.
810 */
811 objflags |= DEPFLAG_IS_PART;
812 break;
813
814 default:
815 elog(ERROR, "unrecognized dependency type '%c' for %s",
816 foundDep->deptype, getObjectDescription(object, false));
817 break;
818 }
819 }
820
821 systable_endscan(scan);
822
823 /*
824 * If we found an INTERNAL or EXTENSION dependency when we're at outer
825 * level, complain about it now. If we also found a PARTITION dependency,
826 * we prefer to report the PARTITION dependency. This is arbitrary but
827 * seems to be more useful in practice.
828 */
829 if (OidIsValid(owningObject.classId))
830 {
831 char *otherObjDesc;
832
833 if (OidIsValid(partitionObject.classId))
834 otherObjDesc = getObjectDescription(&partitionObject, false);
835 else
836 otherObjDesc = getObjectDescription(&owningObject, false);
837
839 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
840 errmsg("cannot drop %s because %s requires it",
841 getObjectDescription(object, false), otherObjDesc),
842 errhint("You can drop %s instead.", otherObjDesc)));
843 }
844
845 /*
846 * Next, identify all objects that directly depend on the current object.
847 * To ensure predictable deletion order, we collect them up in
848 * dependentObjects and sort the list before actually recursing. (The
849 * deletion order would be valid in any case, but doing this ensures
850 * consistent output from DROP CASCADE commands, which is helpful for
851 * regression testing.)
852 */
853 maxDependentObjects = 128; /* arbitrary initial allocation */
854 dependentObjects = palloc_array(ObjectAddressAndFlags, maxDependentObjects);
855 numDependentObjects = 0;
856
857 ScanKeyInit(&key[0],
858 Anum_pg_depend_refclassid,
859 BTEqualStrategyNumber, F_OIDEQ,
860 ObjectIdGetDatum(object->classId));
861 ScanKeyInit(&key[1],
862 Anum_pg_depend_refobjid,
863 BTEqualStrategyNumber, F_OIDEQ,
864 ObjectIdGetDatum(object->objectId));
865 if (object->objectSubId != 0)
866 {
867 ScanKeyInit(&key[2],
868 Anum_pg_depend_refobjsubid,
869 BTEqualStrategyNumber, F_INT4EQ,
870 Int32GetDatum(object->objectSubId));
871 nkeys = 3;
872 }
873 else
874 nkeys = 2;
875
876 scan = systable_beginscan(*depRel, DependReferenceIndexId, true,
877 NULL, nkeys, key);
878
879 while (HeapTupleIsValid(tup = systable_getnext(scan)))
880 {
881 Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
882 int subflags;
883
884 otherObject.classId = foundDep->classid;
885 otherObject.objectId = foundDep->objid;
886 otherObject.objectSubId = foundDep->objsubid;
887
888 /*
889 * If what we found is a sub-object of the current object, just ignore
890 * it. (Normally, such a dependency is implicit, but we must make
891 * explicit ones in some cases involving partitioning.)
892 */
893 if (otherObject.classId == object->classId &&
894 otherObject.objectId == object->objectId &&
895 object->objectSubId == 0)
896 continue;
897
898 /*
899 * Must lock the dependent object before recursing to it.
900 */
901 AcquireDeletionLock(&otherObject, 0);
902
903 /*
904 * The dependent object might have been deleted while we waited to
905 * lock it; if so, we don't need to do anything more with it. We can
906 * test this cheaply and independently of the object's type by seeing
907 * if the pg_depend tuple we are looking at is still live. (If the
908 * object got deleted, the tuple would have been deleted too.)
909 */
910 if (!systable_recheck_tuple(scan, tup))
911 {
912 /* release the now-useless lock */
913 ReleaseDeletionLock(&otherObject);
914 /* and continue scanning for dependencies */
915 continue;
916 }
917
918 /*
919 * We do need to delete it, so identify objflags to be passed down,
920 * which depend on the dependency type.
921 */
922 switch (foundDep->deptype)
923 {
925 subflags = DEPFLAG_NORMAL;
926 break;
927 case DEPENDENCY_AUTO:
929 subflags = DEPFLAG_AUTO;
930 break;
932 subflags = DEPFLAG_INTERNAL;
933 break;
936 subflags = DEPFLAG_PARTITION;
937 break;
939 subflags = DEPFLAG_EXTENSION;
940 break;
941 default:
942 elog(ERROR, "unrecognized dependency type '%c' for %s",
943 foundDep->deptype, getObjectDescription(object, false));
944 subflags = 0; /* keep compiler quiet */
945 break;
946 }
947
948 /* And add it to the pending-objects list */
949 if (numDependentObjects >= maxDependentObjects)
950 {
951 /* enlarge array if needed */
952 maxDependentObjects *= 2;
953 dependentObjects = (ObjectAddressAndFlags *)
954 repalloc(dependentObjects,
955 maxDependentObjects * sizeof(ObjectAddressAndFlags));
956 }
957
958 dependentObjects[numDependentObjects].obj = otherObject;
959 dependentObjects[numDependentObjects].subflags = subflags;
960 numDependentObjects++;
961 }
962
963 systable_endscan(scan);
964
965 /*
966 * Now we can sort the dependent objects into a stable visitation order.
967 * It's safe to use object_address_comparator here since the obj field is
968 * first within ObjectAddressAndFlags.
969 */
970 if (numDependentObjects > 1)
971 qsort(dependentObjects, numDependentObjects,
972 sizeof(ObjectAddressAndFlags),
974
975 /*
976 * Now recurse to the dependent objects. We must visit them first since
977 * they have to be deleted before the current object.
978 */
979 mystack.object = object; /* set up a new stack level */
980 mystack.flags = objflags;
981 mystack.next = stack;
982
983 for (int i = 0; i < numDependentObjects; i++)
984 {
985 ObjectAddressAndFlags *depObj = dependentObjects + i;
986
987 findDependentObjects(&depObj->obj,
988 depObj->subflags,
989 flags,
990 &mystack,
991 targetObjects,
992 pendingObjects,
993 depRel);
994 }
995
996 pfree(dependentObjects);
997
998 /*
999 * Finally, we can add the target object to targetObjects. Be careful to
1000 * include any flags that were passed back down to us from inner recursion
1001 * levels. Record the "dependee" as being either the most important
1002 * partition owner if there is one, else the object we recursed from, if
1003 * any. (The logic in reportDependentObjects() is such that it can only
1004 * need one of those objects.)
1005 */
1006 extra.flags = mystack.flags;
1007 if (extra.flags & DEPFLAG_IS_PART)
1008 extra.dependee = partitionObject;
1009 else if (stack)
1010 extra.dependee = *stack->object;
1011 else
1012 memset(&extra.dependee, 0, sizeof(extra.dependee));
1013 add_exact_object_address_extra(object, &extra, targetObjects);
1014}
bool IsPinnedObject(Oid classId, Oid objectId)
Definition: catalog.c:370
static bool object_address_present_add_flags(const ObjectAddress *object, int flags, ObjectAddresses *addrs)
Definition: dependency.c:2795
#define DEPFLAG_PARTITION
Definition: dependency.c:106
static void add_exact_object_address_extra(const ObjectAddress *object, const ObjectAddressExtra *extra, ObjectAddresses *addrs)
Definition: dependency.c:2734
static bool stack_address_present_add_flags(const ObjectAddress *object, int flags, ObjectAddressStack *stack)
Definition: dependency.c:2868
#define DEPFLAG_IS_PART
Definition: dependency.c:109
void AcquireDeletionLock(const ObjectAddress *object, int flags)
Definition: dependency.c:1546
void ReleaseDeletionLock(const ObjectAddress *object)
Definition: dependency.c:1578
#define DEPFLAG_AUTO
Definition: dependency.c:104
static void findDependentObjects(const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
Definition: dependency.c:483
bool object_address_present(const ObjectAddress *object, const ObjectAddresses *addrs)
Definition: dependency.c:2769
#define DEPFLAG_EXTENSION
Definition: dependency.c:107
#define DEPFLAG_INTERNAL
Definition: dependency.c:105
#define PERFORM_DELETION_SKIP_EXTENSIONS
Definition: dependency.h:96
@ DEPENDENCY_AUTO
Definition: dependency.h:34
@ DEPENDENCY_AUTO_EXTENSION
Definition: dependency.h:39
@ DEPENDENCY_INTERNAL
Definition: dependency.h:35
@ DEPENDENCY_PARTITION_PRI
Definition: dependency.h:36
@ DEPENDENCY_PARTITION_SEC
Definition: dependency.h:37
@ DEPENDENCY_EXTENSION
Definition: dependency.h:38
@ DEPENDENCY_NORMAL
Definition: dependency.h:33
int errhint(const char *fmt,...)
Definition: elog.c:1330
bool creating_extension
Definition: extension.c:77
Oid CurrentExtensionObject
Definition: extension.c:78
#define palloc_array(type, count)
Definition: fe_memutils.h:76
bool systable_recheck_tuple(SysScanDesc sysscan, HeapTuple tup)
Definition: genam.c:573
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
void pfree(void *pointer)
Definition: mcxt.c:1594
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:72
void check_stack_depth(void)
Definition: stack_depth.c:95
ObjectAddress obj
Definition: dependency.c:135
ObjectAddress dependee
Definition: dependency.c:98
const ObjectAddress * object
Definition: dependency.c:127
struct ObjectAddressStack * next
Definition: dependency.c:129

References AcquireDeletionLock(), add_exact_object_address_extra(), BTEqualStrategyNumber, check_stack_depth(), ObjectAddress::classId, creating_extension, CurrentExtensionObject, ObjectAddressExtra::dependee, DEPENDENCY_AUTO, DEPENDENCY_AUTO_EXTENSION, DEPENDENCY_EXTENSION, DEPENDENCY_INTERNAL, DEPENDENCY_NORMAL, DEPENDENCY_PARTITION_PRI, DEPENDENCY_PARTITION_SEC, DEPFLAG_AUTO, DEPFLAG_EXTENSION, DEPFLAG_INTERNAL, DEPFLAG_IS_PART, DEPFLAG_NORMAL, DEPFLAG_PARTITION, DEPFLAG_REVERSE, elog, ereport, errcode(), errhint(), errmsg(), ERROR, findDependentObjects(), ObjectAddressExtra::flags, ObjectAddressStack::flags, getObjectDescription(), GETSTRUCT(), HeapTupleIsValid, i, Int32GetDatum(), IsPinnedObject(), sort-test::key, ObjectAddressStack::next, ObjectAddressAndFlags::obj, ObjectAddressStack::object, object_address_comparator(), object_address_present(), object_address_present_add_flags(), ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, OidIsValid, palloc_array, PERFORM_DELETION_SKIP_EXTENSIONS, pfree(), qsort, ReleaseDeletionLock(), repalloc(), ScanKeyInit(), stack_address_present_add_flags(), ObjectAddressAndFlags::subflags, systable_beginscan(), systable_endscan(), systable_getnext(), and systable_recheck_tuple().

Referenced by findDependentObjects(), performDeletion(), performDeletionCheck(), and performMultipleDeletions().

◆ free_object_addresses()

◆ new_object_addresses()

ObjectAddresses * new_object_addresses ( void  )

◆ object_address_comparator()

static int object_address_comparator ( const void *  a,
const void *  b 
)
static

Definition at line 2620 of file dependency.c.

2621{
2622 const ObjectAddress *obja = (const ObjectAddress *) a;
2623 const ObjectAddress *objb = (const ObjectAddress *) b;
2624
2625 /*
2626 * Primary sort key is OID descending. Most of the time, this will result
2627 * in putting newer objects before older ones, which is likely to be the
2628 * right order to delete in.
2629 */
2630 if (obja->objectId > objb->objectId)
2631 return -1;
2632 if (obja->objectId < objb->objectId)
2633 return 1;
2634
2635 /*
2636 * Next sort on catalog ID, in case identical OIDs appear in different
2637 * catalogs. Sort direction is pretty arbitrary here.
2638 */
2639 if (obja->classId < objb->classId)
2640 return -1;
2641 if (obja->classId > objb->classId)
2642 return 1;
2643
2644 /*
2645 * Last, sort on object subId.
2646 *
2647 * We sort the subId as an unsigned int so that 0 (the whole object) will
2648 * come first. This is essential for eliminate_duplicate_dependencies,
2649 * and is also the best order for findDependentObjects.
2650 */
2651 if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
2652 return -1;
2653 if ((unsigned int) obja->objectSubId > (unsigned int) objb->objectSubId)
2654 return 1;
2655 return 0;
2656}
int b
Definition: isn.c:74
int a
Definition: isn.c:73

References a, b, ObjectAddress::classId, ObjectAddress::objectId, and ObjectAddress::objectSubId.

Referenced by eliminate_duplicate_dependencies(), findDependentObjects(), and sort_object_addresses().

◆ object_address_present()

bool object_address_present ( const ObjectAddress object,
const ObjectAddresses addrs 
)

Definition at line 2769 of file dependency.c.

2771{
2772 int i;
2773
2774 for (i = addrs->numrefs - 1; i >= 0; i--)
2775 {
2776 const ObjectAddress *thisobj = addrs->refs + i;
2777
2778 if (object->classId == thisobj->classId &&
2779 object->objectId == thisobj->objectId)
2780 {
2781 if (object->objectSubId == thisobj->objectSubId ||
2782 thisobj->objectSubId == 0)
2783 return true;
2784 }
2785 }
2786
2787 return false;
2788}

References ObjectAddress::classId, i, ObjectAddresses::numrefs, ObjectAddressStack::object, ObjectAddress::objectId, ObjectAddress::objectSubId, and ObjectAddresses::refs.

Referenced by AlterConstraintNamespaces(), AlterIndexNamespaces(), AlterRelationNamespaceInternal(), AlterTypeNamespaceInternal(), findDependentObjects(), and PreCommit_on_commit_actions().

◆ object_address_present_add_flags()

static bool object_address_present_add_flags ( const ObjectAddress object,
int  flags,
ObjectAddresses addrs 
)
static

Definition at line 2795 of file dependency.c.

2798{
2799 bool result = false;
2800 int i;
2801
2802 for (i = addrs->numrefs - 1; i >= 0; i--)
2803 {
2804 ObjectAddress *thisobj = addrs->refs + i;
2805
2806 if (object->classId == thisobj->classId &&
2807 object->objectId == thisobj->objectId)
2808 {
2809 if (object->objectSubId == thisobj->objectSubId)
2810 {
2811 ObjectAddressExtra *thisextra = addrs->extras + i;
2812
2813 thisextra->flags |= flags;
2814 result = true;
2815 }
2816 else if (thisobj->objectSubId == 0)
2817 {
2818 /*
2819 * We get here if we find a need to delete a column after
2820 * having already decided to drop its whole table. Obviously
2821 * we no longer need to drop the subobject, so report that we
2822 * found the subobject in the array. But don't plaster its
2823 * flags on the whole object.
2824 */
2825 result = true;
2826 }
2827 else if (object->objectSubId == 0)
2828 {
2829 /*
2830 * We get here if we find a need to delete a whole table after
2831 * having already decided to drop one of its columns. We
2832 * can't report that the whole object is in the array, but we
2833 * should mark the subobject with the whole object's flags.
2834 *
2835 * It might seem attractive to physically delete the column's
2836 * array entry, or at least mark it as no longer needing
2837 * separate deletion. But that could lead to, e.g., dropping
2838 * the column's datatype before we drop the table, which does
2839 * not seem like a good idea. This is a very rare situation
2840 * in practice, so we just take the hit of doing a separate
2841 * DROP COLUMN action even though we know we're gonna delete
2842 * the table later.
2843 *
2844 * What we can do, though, is mark this as a subobject so that
2845 * we don't report it separately, which is confusing because
2846 * it's unpredictable whether it happens or not. But do so
2847 * only if flags != 0 (flags == 0 is a read-only probe).
2848 *
2849 * Because there could be other subobjects of this object in
2850 * the array, this case means we always have to loop through
2851 * the whole array; we cannot exit early on a match.
2852 */
2853 ObjectAddressExtra *thisextra = addrs->extras + i;
2854
2855 if (flags)
2856 thisextra->flags |= (flags | DEPFLAG_SUBOBJECT);
2857 }
2858 }
2859 }
2860
2861 return result;
2862}
#define DEPFLAG_SUBOBJECT
Definition: dependency.c:110

References ObjectAddress::classId, DEPFLAG_SUBOBJECT, ObjectAddresses::extras, ObjectAddressExtra::flags, ObjectAddressStack::flags, i, ObjectAddresses::numrefs, ObjectAddressStack::object, ObjectAddress::objectId, ObjectAddress::objectSubId, and ObjectAddresses::refs.

Referenced by findDependentObjects().

◆ performDeletion()

void performDeletion ( const ObjectAddress object,
DropBehavior  behavior,
int  flags 
)

Definition at line 274 of file dependency.c.

276{
277 Relation depRel;
278 ObjectAddresses *targetObjects;
279
280 /*
281 * We save some cycles by opening pg_depend just once and passing the
282 * Relation pointer down to all the recursive deletion steps.
283 */
284 depRel = table_open(DependRelationId, RowExclusiveLock);
285
286 /*
287 * Acquire deletion lock on the target object. (Ideally the caller has
288 * done this already, but many places are sloppy about it.)
289 */
290 AcquireDeletionLock(object, 0);
291
292 /*
293 * Construct a list of objects to delete (ie, the given object plus
294 * everything directly or indirectly dependent on it).
295 */
296 targetObjects = new_object_addresses();
297
300 flags,
301 NULL, /* empty stack */
302 targetObjects,
303 NULL, /* no pendingObjects */
304 &depRel);
305
306 /*
307 * Check if deletion is allowed, and report about cascaded deletes.
308 */
309 reportDependentObjects(targetObjects,
310 behavior,
311 flags,
312 object);
313
314 /* do the deed */
315 deleteObjectsInList(targetObjects, &depRel, flags);
316
317 /* And clean up */
318 free_object_addresses(targetObjects);
319
321}
static void reportDependentObjects(const ObjectAddresses *targetObjects, DropBehavior behavior, int flags, const ObjectAddress *origObject)
Definition: dependency.c:1030
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2664
static void deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel, int flags)
Definition: dependency.c:186
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2949

References AcquireDeletionLock(), deleteObjectsInList(), DEPFLAG_ORIGINAL, findDependentObjects(), ObjectAddressStack::flags, free_object_addresses(), new_object_addresses(), reportDependentObjects(), RowExclusiveLock, table_close(), and table_open().

Referenced by AlterDomainDropConstraint(), AlterDomainNotNull(), ATExecDropIdentity(), ATExecMergePartitions(), ATExecSplitPartition(), DetachPartitionFinalize(), do_autovacuum(), dropconstraint_internal(), DropForeignKeyConstraintTriggers(), dropOperators(), dropProcedures(), finish_heap_swap(), inv_drop(), PublicationDropSchemas(), PublicationDropTables(), RemoveAttrDefault(), RemoveTempRelations(), RemoveUserMapping(), and SetDefaultACL().

◆ performDeletionCheck()

void performDeletionCheck ( const ObjectAddress object,
DropBehavior  behavior,
int  flags 
)

Definition at line 333 of file dependency.c.

335{
336 Relation depRel;
337 ObjectAddresses *targetObjects;
338
339 Assert(behavior == DROP_RESTRICT);
340
341 depRel = table_open(DependRelationId, RowExclusiveLock);
342
343 AcquireDeletionLock(object, 0);
344
345 /*
346 * Construct a list of objects we want to delete later (ie, the given
347 * object plus everything directly or indirectly dependent on it).
348 */
349 targetObjects = new_object_addresses();
350
353 flags,
354 NULL, /* empty stack */
355 targetObjects,
356 NULL, /* no pendingObjects */
357 &depRel);
358
359 /*
360 * Check if deletion is allowed.
361 */
362 reportDependentObjects(targetObjects,
363 behavior,
364 flags,
365 object);
366
367 /* And clean up */
368 free_object_addresses(targetObjects);
369
371}
@ DROP_RESTRICT
Definition: parsenodes.h:2424

References AcquireDeletionLock(), Assert(), DEPFLAG_ORIGINAL, DROP_RESTRICT, findDependentObjects(), ObjectAddressStack::flags, free_object_addresses(), new_object_addresses(), reportDependentObjects(), RowExclusiveLock, table_close(), and table_open().

Referenced by ATExecMergePartitions(), and ATExecSplitPartition().

◆ performMultipleDeletions()

void performMultipleDeletions ( const ObjectAddresses objects,
DropBehavior  behavior,
int  flags 
)

Definition at line 383 of file dependency.c.

385{
386 Relation depRel;
387 ObjectAddresses *targetObjects;
388 int i;
389
390 /* No work if no objects... */
391 if (objects->numrefs <= 0)
392 return;
393
394 /*
395 * We save some cycles by opening pg_depend just once and passing the
396 * Relation pointer down to all the recursive deletion steps.
397 */
398 depRel = table_open(DependRelationId, RowExclusiveLock);
399
400 /*
401 * Construct a list of objects to delete (ie, the given objects plus
402 * everything directly or indirectly dependent on them). Note that
403 * because we pass the whole objects list as pendingObjects context, we
404 * won't get a failure from trying to delete an object that is internally
405 * dependent on another one in the list; we'll just skip that object and
406 * delete it when we reach its owner.
407 */
408 targetObjects = new_object_addresses();
409
410 for (i = 0; i < objects->numrefs; i++)
411 {
412 const ObjectAddress *thisobj = objects->refs + i;
413
414 /*
415 * Acquire deletion lock on each target object. (Ideally the caller
416 * has done this already, but many places are sloppy about it.)
417 */
418 AcquireDeletionLock(thisobj, flags);
419
420 findDependentObjects(thisobj,
422 flags,
423 NULL, /* empty stack */
424 targetObjects,
425 objects,
426 &depRel);
427 }
428
429 /*
430 * Check if deletion is allowed, and report about cascaded deletes.
431 *
432 * If there's exactly one object being deleted, report it the same way as
433 * in performDeletion(), else we have to be vaguer.
434 */
435 reportDependentObjects(targetObjects,
436 behavior,
437 flags,
438 (objects->numrefs == 1 ? objects->refs : NULL));
439
440 /* do the deed */
441 deleteObjectsInList(targetObjects, &depRel, flags);
442
443 /* And clean up */
444 free_object_addresses(targetObjects);
445
447}

References AcquireDeletionLock(), deleteObjectsInList(), DEPFLAG_ORIGINAL, findDependentObjects(), ObjectAddressStack::flags, free_object_addresses(), i, new_object_addresses(), ObjectAddresses::numrefs, ObjectAddresses::refs, reportDependentObjects(), RowExclusiveLock, table_close(), and table_open().

Referenced by ATExecDropColumn(), DropClonedTriggersFromPartition(), PreCommit_on_commit_actions(), ReindexRelationConcurrently(), RemoveInheritedConstraint(), RemoveObjects(), RemoveRelations(), and shdepDropOwned().

◆ process_function_rte_ref()

static void process_function_rte_ref ( RangeTblEntry rte,
AttrNumber  attnum,
find_expr_references_context context 
)
static

Definition at line 2429 of file dependency.c.

2431{
2432 int atts_done = 0;
2433 ListCell *lc;
2434
2435 /*
2436 * Identify which RangeTblFunction produces this attnum, and see if it
2437 * returns a composite type. If so, we'd better make a dependency on the
2438 * referenced column of the composite type (or actually, of its associated
2439 * relation).
2440 */
2441 foreach(lc, rte->functions)
2442 {
2443 RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2444
2445 if (attnum > atts_done &&
2446 attnum <= atts_done + rtfunc->funccolcount)
2447 {
2448 TupleDesc tupdesc;
2449
2450 /* If it has a coldeflist, it certainly returns RECORD */
2451 if (rtfunc->funccolnames != NIL)
2452 tupdesc = NULL; /* no need to work hard */
2453 else
2454 tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr, true);
2455 if (tupdesc && tupdesc->tdtypeid != RECORDOID)
2456 {
2457 /*
2458 * Named composite type, so individual columns could get
2459 * dropped. Make a dependency on this specific column.
2460 */
2461 Oid reltype = get_typ_typrelid(tupdesc->tdtypeid);
2462
2463 Assert(attnum - atts_done <= tupdesc->natts);
2464 if (OidIsValid(reltype)) /* can this fail? */
2465 add_object_address(RelationRelationId, reltype,
2466 attnum - atts_done,
2467 context->addrs);
2468 return;
2469 }
2470 /* Nothing to do; function's result type is handled elsewhere */
2471 return;
2472 }
2473 atts_done += rtfunc->funccolcount;
2474 }
2475
2476 /* If we get here, must be looking for the ordinality column */
2477 if (rte->funcordinality && attnum == atts_done + 1)
2478 return;
2479
2480 /* this probably can't happen ... */
2481 ereport(ERROR,
2482 (errcode(ERRCODE_UNDEFINED_COLUMN),
2483 errmsg("column %d of relation \"%s\" does not exist",
2484 attnum, rte->eref->aliasname)));
2485}
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:551
int16 attnum
Definition: pg_attribute.h:74
#define NIL
Definition: pg_list.h:68
bool funcordinality
Definition: parsenodes.h:1236
List * functions
Definition: parsenodes.h:1234
Oid tdtypeid
Definition: tupdesc.h:138

References add_object_address(), find_expr_references_context::addrs, Assert(), attnum, ereport, errcode(), errmsg(), ERROR, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_tupdesc(), get_typ_typrelid(), lfirst, NIL, OidIsValid, and TupleDescData::tdtypeid.

Referenced by find_expr_references_walker().

◆ query_uses_temp_object()

bool query_uses_temp_object ( Query query,
ObjectAddress temp_object 
)

Definition at line 2538 of file dependency.c.

2539{
2540 bool result;
2541 ObjectAddresses *addrs;
2542
2543 addrs = new_object_addresses();
2544
2545 /* Collect all dependencies from the Query */
2546 collectDependenciesOfExpr(addrs, (Node *) query, NIL);
2547
2548 /* Look for one that is temp */
2549 result = find_temp_object(addrs, false, temp_object);
2550
2551 free_object_addresses(addrs);
2552
2553 return result;
2554}
void collectDependenciesOfExpr(ObjectAddresses *addrs, Node *expr, List *rtable)
Definition: dependency.c:1646
bool find_temp_object(const ObjectAddresses *addrs, bool local_temp_okay, ObjectAddress *foundobj)
Definition: dependency.c:2503

References collectDependenciesOfExpr(), find_temp_object(), free_object_addresses(), new_object_addresses(), and NIL.

Referenced by DefineView(), and transformCreateTableAsStmt().

◆ record_object_address_dependencies()

void record_object_address_dependencies ( const ObjectAddress depender,
ObjectAddresses referenced,
DependencyType  behavior 
)

◆ recordDependencyOnExpr()

void recordDependencyOnExpr ( const ObjectAddress depender,
Node expr,
List rtable,
DependencyType  behavior 
)

Definition at line 1603 of file dependency.c.

1606{
1607 ObjectAddresses *addrs;
1608
1609 addrs = new_object_addresses();
1610
1611 /* Collect all dependencies from the expression */
1612 collectDependenciesOfExpr(addrs, expr, rtable);
1613
1614 /* Remove duplicates */
1616
1617 /* And record 'em */
1619 addrs->refs, addrs->numrefs,
1620 behavior);
1621
1622 free_object_addresses(addrs);
1623}

References collectDependenciesOfExpr(), eliminate_duplicate_dependencies(), free_object_addresses(), new_object_addresses(), ObjectAddresses::numrefs, recordMultipleDependencies(), and ObjectAddresses::refs.

Referenced by AlterPolicy(), CreatePolicy(), CreateTriggerFiringOn(), GenerateTypeDependencies(), and InsertRule().

◆ recordDependencyOnSingleRelExpr()

void recordDependencyOnSingleRelExpr ( const ObjectAddress depender,
Node expr,
Oid  relId,
DependencyType  behavior,
DependencyType  self_behavior,
bool  reverse_self 
)

Definition at line 1678 of file dependency.c.

1683{
1685 RangeTblEntry rte = {0};
1686
1687 context.addrs = new_object_addresses();
1688
1689 /* We gin up a rather bogus rangetable list to handle Vars */
1690 rte.type = T_RangeTblEntry;
1691 rte.rtekind = RTE_RELATION;
1692 rte.relid = relId;
1693 rte.relkind = RELKIND_RELATION; /* no need for exactness here */
1694 rte.rellockmode = AccessShareLock;
1695
1696 context.rtables = list_make1(list_make1(&rte));
1697
1698 /* Scan the expression tree for referenceable objects */
1699 find_expr_references_walker(expr, &context);
1700
1701 /* Remove any duplicates */
1703
1704 /* Separate self-dependencies if necessary */
1705 if ((behavior != self_behavior || reverse_self) &&
1706 context.addrs->numrefs > 0)
1707 {
1708 ObjectAddresses *self_addrs;
1709 ObjectAddress *outobj;
1710 int oldref,
1711 outrefs;
1712
1713 self_addrs = new_object_addresses();
1714
1715 outobj = context.addrs->refs;
1716 outrefs = 0;
1717 for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
1718 {
1719 ObjectAddress *thisobj = context.addrs->refs + oldref;
1720
1721 if (thisobj->classId == RelationRelationId &&
1722 thisobj->objectId == relId)
1723 {
1724 /* Move this ref into self_addrs */
1725 add_exact_object_address(thisobj, self_addrs);
1726 }
1727 else
1728 {
1729 /* Keep it in context.addrs */
1730 *outobj = *thisobj;
1731 outobj++;
1732 outrefs++;
1733 }
1734 }
1735 context.addrs->numrefs = outrefs;
1736
1737 /* Record the self-dependencies with the appropriate direction */
1738 if (!reverse_self)
1740 self_addrs->refs, self_addrs->numrefs,
1741 self_behavior);
1742 else
1743 {
1744 /* Can't use recordMultipleDependencies, so do it the hard way */
1745 int selfref;
1746
1747 for (selfref = 0; selfref < self_addrs->numrefs; selfref++)
1748 {
1749 ObjectAddress *thisobj = self_addrs->refs + selfref;
1750
1751 recordDependencyOn(thisobj, depender, self_behavior);
1752 }
1753 }
1754
1755 free_object_addresses(self_addrs);
1756 }
1757
1758 /* Record the external dependencies */
1760 context.addrs->refs, context.addrs->numrefs,
1761 behavior);
1762
1764}
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2709
#define AccessShareLock
Definition: lockdefs.h:36
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:45

References AccessShareLock, add_exact_object_address(), find_expr_references_context::addrs, ObjectAddress::classId, eliminate_duplicate_dependencies(), find_expr_references_walker(), free_object_addresses(), list_make1, new_object_addresses(), ObjectAddresses::numrefs, ObjectAddress::objectId, recordDependencyOn(), recordMultipleDependencies(), ObjectAddresses::refs, find_expr_references_context::rtables, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by CreateConstraintEntry(), CreateStatistics(), index_create(), publication_add_relation(), StoreAttrDefault(), and StorePartitionKey().

◆ ReleaseDeletionLock()

void ReleaseDeletionLock ( const ObjectAddress object)

Definition at line 1578 of file dependency.c.

1579{
1580 if (object->classId == RelationRelationId)
1582 else
1583 /* assume we should lock the whole object not a sub-object */
1584 UnlockDatabaseObject(object->classId, object->objectId, 0,
1586}
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:229
void UnlockDatabaseObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1068

References AccessExclusiveLock, ObjectAddress::classId, ObjectAddressStack::object, ObjectAddress::objectId, UnlockDatabaseObject(), and UnlockRelationOid().

Referenced by findDependentObjects(), and shdepDropOwned().

◆ reportDependentObjects()

static void reportDependentObjects ( const ObjectAddresses targetObjects,
DropBehavior  behavior,
int  flags,
const ObjectAddress origObject 
)
static

Definition at line 1030 of file dependency.c.

1034{
1035 int msglevel = (flags & PERFORM_DELETION_QUIETLY) ? DEBUG2 : NOTICE;
1036 bool ok = true;
1037 StringInfoData clientdetail;
1038 StringInfoData logdetail;
1039 int numReportedClient = 0;
1040 int numNotReportedClient = 0;
1041 int i;
1042
1043 /*
1044 * If we need to delete any partition-dependent objects, make sure that
1045 * we're deleting at least one of their partition dependencies, too. That
1046 * can be detected by checking that we reached them by a PARTITION
1047 * dependency at some point.
1048 *
1049 * We just report the first such object, as in most cases the only way to
1050 * trigger this complaint is to explicitly try to delete one partition of
1051 * a partitioned object.
1052 */
1053 for (i = 0; i < targetObjects->numrefs; i++)
1054 {
1055 const ObjectAddressExtra *extra = &targetObjects->extras[i];
1056
1057 if ((extra->flags & DEPFLAG_IS_PART) &&
1058 !(extra->flags & DEPFLAG_PARTITION))
1059 {
1060 const ObjectAddress *object = &targetObjects->refs[i];
1061 char *otherObjDesc = getObjectDescription(&extra->dependee,
1062 false);
1063
1064 ereport(ERROR,
1065 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1066 errmsg("cannot drop %s because %s requires it",
1067 getObjectDescription(object, false), otherObjDesc),
1068 errhint("You can drop %s instead.", otherObjDesc)));
1069 }
1070 }
1071
1072 /*
1073 * If no error is to be thrown, and the msglevel is too low to be shown to
1074 * either client or server log, there's no need to do any of the rest of
1075 * the work.
1076 */
1077 if (behavior == DROP_CASCADE &&
1079 return;
1080
1081 /*
1082 * We limit the number of dependencies reported to the client to
1083 * MAX_REPORTED_DEPS, since client software may not deal well with
1084 * enormous error strings. The server log always gets a full report.
1085 */
1086#define MAX_REPORTED_DEPS 100
1087
1088 initStringInfo(&clientdetail);
1089 initStringInfo(&logdetail);
1090
1091 /*
1092 * We process the list back to front (ie, in dependency order not deletion
1093 * order), since this makes for a more understandable display.
1094 */
1095 for (i = targetObjects->numrefs - 1; i >= 0; i--)
1096 {
1097 const ObjectAddress *obj = &targetObjects->refs[i];
1098 const ObjectAddressExtra *extra = &targetObjects->extras[i];
1099 char *objDesc;
1100
1101 /* Ignore the original deletion target(s) */
1102 if (extra->flags & DEPFLAG_ORIGINAL)
1103 continue;
1104
1105 /* Also ignore sub-objects; we'll report the whole object elsewhere */
1106 if (extra->flags & DEPFLAG_SUBOBJECT)
1107 continue;
1108
1109 objDesc = getObjectDescription(obj, false);
1110
1111 /* An object being dropped concurrently doesn't need to be reported */
1112 if (objDesc == NULL)
1113 continue;
1114
1115 /*
1116 * If, at any stage of the recursive search, we reached the object via
1117 * an AUTO, INTERNAL, PARTITION, or EXTENSION dependency, then it's
1118 * okay to delete it even in RESTRICT mode.
1119 */
1120 if (extra->flags & (DEPFLAG_AUTO |
1124 {
1125 /*
1126 * auto-cascades are reported at DEBUG2, not msglevel. We don't
1127 * try to combine them with the regular message because the
1128 * results are too confusing when client_min_messages and
1129 * log_min_messages are different.
1130 */
1132 (errmsg_internal("drop auto-cascades to %s",
1133 objDesc)));
1134 }
1135 else if (behavior == DROP_RESTRICT)
1136 {
1137 char *otherDesc = getObjectDescription(&extra->dependee,
1138 false);
1139
1140 if (otherDesc)
1141 {
1142 if (numReportedClient < MAX_REPORTED_DEPS)
1143 {
1144 /* separate entries with a newline */
1145 if (clientdetail.len != 0)
1146 appendStringInfoChar(&clientdetail, '\n');
1147 appendStringInfo(&clientdetail, _("%s depends on %s"),
1148 objDesc, otherDesc);
1149 numReportedClient++;
1150 }
1151 else
1152 numNotReportedClient++;
1153 /* separate entries with a newline */
1154 if (logdetail.len != 0)
1155 appendStringInfoChar(&logdetail, '\n');
1156 appendStringInfo(&logdetail, _("%s depends on %s"),
1157 objDesc, otherDesc);
1158 pfree(otherDesc);
1159 }
1160 else
1161 numNotReportedClient++;
1162 ok = false;
1163 }
1164 else
1165 {
1166 if (numReportedClient < MAX_REPORTED_DEPS)
1167 {
1168 /* separate entries with a newline */
1169 if (clientdetail.len != 0)
1170 appendStringInfoChar(&clientdetail, '\n');
1171 appendStringInfo(&clientdetail, _("drop cascades to %s"),
1172 objDesc);
1173 numReportedClient++;
1174 }
1175 else
1176 numNotReportedClient++;
1177 /* separate entries with a newline */
1178 if (logdetail.len != 0)
1179 appendStringInfoChar(&logdetail, '\n');
1180 appendStringInfo(&logdetail, _("drop cascades to %s"),
1181 objDesc);
1182 }
1183
1184 pfree(objDesc);
1185 }
1186
1187 if (numNotReportedClient > 0)
1188 appendStringInfo(&clientdetail, ngettext("\nand %d other object "
1189 "(see server log for list)",
1190 "\nand %d other objects "
1191 "(see server log for list)",
1192 numNotReportedClient),
1193 numNotReportedClient);
1194
1195 if (!ok)
1196 {
1197 if (origObject)
1198 ereport(ERROR,
1199 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1200 errmsg("cannot drop %s because other objects depend on it",
1201 getObjectDescription(origObject, false)),
1202 errdetail_internal("%s", clientdetail.data),
1203 errdetail_log("%s", logdetail.data),
1204 errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
1205 else
1206 ereport(ERROR,
1207 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1208 errmsg("cannot drop desired object(s) because other objects depend on them"),
1209 errdetail_internal("%s", clientdetail.data),
1210 errdetail_log("%s", logdetail.data),
1211 errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
1212 }
1213 else if (numReportedClient > 1)
1214 {
1215 ereport(msglevel,
1216 (errmsg_plural("drop cascades to %d other object",
1217 "drop cascades to %d other objects",
1218 numReportedClient + numNotReportedClient,
1219 numReportedClient + numNotReportedClient),
1220 errdetail_internal("%s", clientdetail.data),
1221 errdetail_log("%s", logdetail.data)));
1222 }
1223 else if (numReportedClient == 1)
1224 {
1225 /* we just use the single item as-is */
1226 ereport(msglevel,
1227 (errmsg_internal("%s", clientdetail.data)));
1228 }
1229
1230 pfree(clientdetail.data);
1231 pfree(logdetail.data);
1232}
#define ngettext(s, p, n)
Definition: c.h:1179
#define MAX_REPORTED_DEPS
#define PERFORM_DELETION_QUIETLY
Definition: dependency.h:94
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1193
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1170
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1243
bool message_level_is_interesting(int elevel)
Definition: elog.c:273
int errdetail_log(const char *fmt,...)
Definition: elog.c:1264
#define _(x)
Definition: elog.c:91
#define DEBUG2
Definition: elog.h:29
#define NOTICE
Definition: elog.h:35
@ DROP_CASCADE
Definition: parsenodes.h:2425
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97

References _, appendStringInfo(), appendStringInfoChar(), StringInfoData::data, DEBUG2, ObjectAddressExtra::dependee, DEPFLAG_AUTO, DEPFLAG_EXTENSION, DEPFLAG_INTERNAL, DEPFLAG_IS_PART, DEPFLAG_ORIGINAL, DEPFLAG_PARTITION, DEPFLAG_SUBOBJECT, DROP_CASCADE, DROP_RESTRICT, ereport, errcode(), errdetail_internal(), errdetail_log(), errhint(), errmsg(), errmsg_internal(), errmsg_plural(), ERROR, ObjectAddresses::extras, ObjectAddressExtra::flags, ObjectAddressStack::flags, getObjectDescription(), i, initStringInfo(), StringInfoData::len, MAX_REPORTED_DEPS, message_level_is_interesting(), ngettext, NOTICE, ObjectAddresses::numrefs, PERFORM_DELETION_QUIETLY, pfree(), and ObjectAddresses::refs.

Referenced by performDeletion(), performDeletionCheck(), and performMultipleDeletions().

◆ sort_object_addresses()

void sort_object_addresses ( ObjectAddresses addrs)

Definition at line 2937 of file dependency.c.

2938{
2939 if (addrs->numrefs > 1)
2940 qsort(addrs->refs, addrs->numrefs,
2941 sizeof(ObjectAddress),
2943}

References ObjectAddresses::numrefs, object_address_comparator(), qsort, and ObjectAddresses::refs.

Referenced by shdepDropOwned().

◆ stack_address_present_add_flags()

static bool stack_address_present_add_flags ( const ObjectAddress object,
int  flags,
ObjectAddressStack stack 
)
static

Definition at line 2868 of file dependency.c.

2871{
2872 bool result = false;
2873 ObjectAddressStack *stackptr;
2874
2875 for (stackptr = stack; stackptr; stackptr = stackptr->next)
2876 {
2877 const ObjectAddress *thisobj = stackptr->object;
2878
2879 if (object->classId == thisobj->classId &&
2880 object->objectId == thisobj->objectId)
2881 {
2882 if (object->objectSubId == thisobj->objectSubId)
2883 {
2884 stackptr->flags |= flags;
2885 result = true;
2886 }
2887 else if (thisobj->objectSubId == 0)
2888 {
2889 /*
2890 * We're visiting a column with whole table already on stack.
2891 * As in object_address_present_add_flags(), we can skip
2892 * further processing of the subobject, but we don't want to
2893 * propagate flags for the subobject to the whole object.
2894 */
2895 result = true;
2896 }
2897 else if (object->objectSubId == 0)
2898 {
2899 /*
2900 * We're visiting a table with column already on stack. As in
2901 * object_address_present_add_flags(), we should propagate
2902 * flags for the whole object to each of its subobjects.
2903 */
2904 if (flags)
2905 stackptr->flags |= (flags | DEPFLAG_SUBOBJECT);
2906 }
2907 }
2908 }
2909
2910 return result;
2911}

References ObjectAddress::classId, DEPFLAG_SUBOBJECT, ObjectAddressStack::flags, ObjectAddressStack::next, ObjectAddressStack::object, ObjectAddress::objectId, and ObjectAddress::objectSubId.

Referenced by findDependentObjects().