PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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_propgraph_element.h"
#include "catalog/pg_propgraph_element_label.h"
#include "catalog/pg_propgraph_label.h"
#include "catalog/pg_propgraph_label_property.h"
#include "catalog/pg_propgraph_property.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 109 of file dependency.c.

◆ DEPFLAG_EXTENSION

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

Definition at line 112 of file dependency.c.

◆ DEPFLAG_INTERNAL

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

Definition at line 110 of file dependency.c.

◆ DEPFLAG_IS_PART

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

Definition at line 114 of file dependency.c.

◆ DEPFLAG_NORMAL

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

Definition at line 108 of file dependency.c.

◆ DEPFLAG_ORIGINAL

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

Definition at line 107 of file dependency.c.

◆ DEPFLAG_PARTITION

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

Definition at line 111 of file dependency.c.

◆ DEPFLAG_REVERSE

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

Definition at line 113 of file dependency.c.

◆ DEPFLAG_SUBOBJECT

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

Definition at line 115 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 1567 of file dependency.c.

1568{
1569 if (object->classId == RelationRelationId)
1570 {
1571 /*
1572 * In DROP INDEX CONCURRENTLY, take only ShareUpdateExclusiveLock on
1573 * the index for the moment. index_drop() will promote the lock once
1574 * it's safe to do so. In all other cases we need full exclusive
1575 * lock.
1576 */
1579 else
1581 }
1582 else if (object->classId == AuthMemRelationId)
1583 LockSharedObject(object->classId, object->objectId, 0,
1585 else
1586 {
1587 /* assume we should lock the whole object not a sub-object */
1588 LockDatabaseObject(object->classId, object->objectId, 0,
1590 }
1591}
#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
static int fb(int x)

References AccessExclusiveLock, ObjectAddress::classId, fb(), 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 2731 of file dependency.c.

2733{
2734 ObjectAddress *item;
2735
2736 /* enlarge array if needed */
2737 if (addrs->numrefs >= addrs->maxrefs)
2738 {
2739 addrs->maxrefs *= 2;
2740 addrs->refs = (ObjectAddress *)
2741 repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2742 Assert(!addrs->extras);
2743 }
2744 /* record this item */
2745 item = addrs->refs + addrs->numrefs;
2746 *item = *object;
2747 addrs->numrefs++;
2748}
#define Assert(condition)
Definition c.h:945
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
ObjectAddressExtra * extras
Definition dependency.c:122
ObjectAddress * refs
Definition dependency.c:121

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

Referenced by AggregateCreate(), AlterConstraintNamespaces(), AlterIndexNamespaces(), AlterRelationNamespaceInternal(), AlterTypeNamespaceInternal(), array_of_attnums_to_objectaddrs(), array_of_opers_to_objectaddrs(), ATExecDropColumn(), ATPostAlterTypeCleanup(), CastCreate(), CreateConstraintEntry(), CreateProceduralLanguage(), CreateTransform(), DropClonedTriggersFromPartition(), GenerateTypeDependencies(), heap_create_with_catalog(), index_create(), insert_element_record(), 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 2756 of file dependency.c.

2759{
2760 ObjectAddress *item;
2762
2763 /* allocate extra space if first time */
2764 if (!addrs->extras)
2765 addrs->extras = (ObjectAddressExtra *)
2766 palloc(addrs->maxrefs * sizeof(ObjectAddressExtra));
2767
2768 /* enlarge array if needed */
2769 if (addrs->numrefs >= addrs->maxrefs)
2770 {
2771 addrs->maxrefs *= 2;
2772 addrs->refs = (ObjectAddress *)
2773 repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2774 addrs->extras = (ObjectAddressExtra *)
2775 repalloc(addrs->extras, addrs->maxrefs * sizeof(ObjectAddressExtra));
2776 }
2777 /* record this item */
2778 item = addrs->refs + addrs->numrefs;
2779 *item = *object;
2780 itemextra = addrs->extras + addrs->numrefs;
2781 *itemextra = *extra;
2782 addrs->numrefs++;
2783}
void * palloc(Size size)
Definition mcxt.c:1387

References ObjectAddresses::extras, fb(), 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 2704 of file dependency.c.

2706{
2707 ObjectAddress *item;
2708
2709 /* enlarge array if needed */
2710 if (addrs->numrefs >= addrs->maxrefs)
2711 {
2712 addrs->maxrefs *= 2;
2713 addrs->refs = (ObjectAddress *)
2714 repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2715 Assert(!addrs->extras);
2716 }
2717 /* record this item */
2718 item = addrs->refs + addrs->numrefs;
2719 item->classId = classId;
2720 item->objectId = objectId;
2721 item->objectSubId = subId;
2722 addrs->numrefs++;
2723}

References Assert, ObjectAddress::classId, ObjectAddresses::extras, fb(), 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 1667 of file dependency.c.

1669{
1671
1672 context.addrs = addrs;
1673
1674 /* Set up interpretation for Vars at varlevelsup = 0 */
1675 context.rtables = list_make1(rtable);
1676
1677 /* Scan the expression tree for referenceable objects */
1678 find_expr_references_walker(expr, &context);
1679}
static bool find_expr_references_walker(Node *node, find_expr_references_context *context)
#define list_make1(x1)
Definition pg_list.h:212
ObjectAddresses * addrs
Definition dependency.c:147

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 2983 of file dependency.c.

2984{
2985 Relation relation;
2986 ScanKeyData key[3];
2987 int nkeys;
2988 SysScanDesc scan;
2989 HeapTuple oldtuple;
2990
2992
2993 ScanKeyInit(&key[0],
2996 ObjectIdGetDatum(object->objectId));
2997 ScanKeyInit(&key[1],
3000 ObjectIdGetDatum(object->classId));
3001 if (object->objectSubId != 0)
3002 {
3003 ScanKeyInit(&key[2],
3006 Int32GetDatum(object->objectSubId));
3007 nkeys = 3;
3008 }
3009 else
3010 nkeys = 2;
3011
3012 scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
3013 NULL, nkeys, key);
3014
3015 while (HeapTupleIsValid(oldtuple = systable_getnext(scan)))
3016 CatalogTupleDelete(relation, &oldtuple->t_self);
3017
3018 systable_endscan(scan);
3019
3020 table_close(relation, RowExclusiveLock);
3021}
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:252
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
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, fb(), HeapTupleIsValid, Int32GetDatum(), 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 191 of file dependency.c.

193{
194 int i;
195
196 /*
197 * Keep track of objects for event triggers, if necessary.
198 */
200 {
201 for (i = 0; i < targetObjects->numrefs; i++)
202 {
203 const ObjectAddress *thisobj = &targetObjects->refs[i];
204 const ObjectAddressExtra *extra = &targetObjects->extras[i];
205 bool original = false;
206 bool normal = false;
207
208 if (extra->flags & DEPFLAG_ORIGINAL)
209 original = true;
210 if (extra->flags & DEPFLAG_NORMAL)
211 normal = true;
212 if (extra->flags & DEPFLAG_REVERSE)
213 normal = true;
214
216 {
217 EventTriggerSQLDropAddObject(thisobj, original, normal);
218 }
219 }
220 }
221
222 /*
223 * Delete all the objects in the proper order, except that if told to, we
224 * should skip the original object(s).
225 */
226 for (i = 0; i < targetObjects->numrefs; i++)
227 {
230
231 if ((flags & PERFORM_DELETION_SKIP_ORIGINAL) &&
232 (thisextra->flags & DEPFLAG_ORIGINAL))
233 continue;
234
236 }
237}
static void deleteOneObject(const ObjectAddress *object, Relation *depRel, int32 flags)
#define DEPFLAG_ORIGINAL
Definition dependency.c:107
#define DEPFLAG_REVERSE
Definition dependency.c:113
#define DEPFLAG_NORMAL
Definition dependency.c:108
#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(), fb(), ObjectAddressExtra::flags, ObjectAddressStack::flags, i, PERFORM_DELETION_INTERNAL, PERFORM_DELETION_SKIP_ORIGINAL, and trackDroppedObjectsNeeded().

Referenced by performDeletion(), and performMultipleDeletions().

◆ deleteOneObject() [1/2]

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

Definition at line 1312 of file dependency.c.

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

References BTEqualStrategyNumber, CatalogTupleDelete(), ObjectAddress::classId, CommandCounterIncrement(), DeleteComments(), DeleteInitPrivs(), DeleteSecurityLabel(), deleteSharedDependencyRecordsFor(), doDeletion(), fb(), ObjectAddressStack::flags, HeapTupleIsValid, Int32GetDatum(), InvokeObjectDropHookArg, ObjectAddressStack::object, ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, PERFORM_DELETION_CONCURRENTLY, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), 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 1418 of file dependency.c.

1419{
1420 switch (object->classId)
1421 {
1422 case RelationRelationId:
1423 {
1424 char relKind = get_rel_relkind(object->objectId);
1425
1426 if (relKind == RELKIND_INDEX ||
1428 {
1429 bool concurrent = ((flags & PERFORM_DELETION_CONCURRENTLY) != 0);
1431
1432 Assert(object->objectSubId == 0);
1433 index_drop(object->objectId, concurrent, concurrent_lock_mode);
1434 }
1435 else
1436 {
1437 if (object->objectSubId != 0)
1439 object->objectSubId);
1440 else
1442 }
1443
1444 /*
1445 * for a sequence, in addition to dropping the heap, also
1446 * delete pg_sequence tuple
1447 */
1450 break;
1451 }
1452
1455 break;
1456
1457 case TypeRelationId:
1458 RemoveTypeById(object->objectId);
1459 break;
1460
1463 break;
1464
1467 break;
1468
1470 LargeObjectDrop(object->objectId);
1471 break;
1472
1473 case OperatorRelationId:
1475 break;
1476
1477 case RewriteRelationId:
1479 break;
1480
1481 case TriggerRelationId:
1482 RemoveTriggerById(object->objectId);
1483 break;
1484
1487 break;
1488
1489 case TSConfigRelationId:
1491 break;
1492
1495 break;
1496
1497 case PolicyRelationId:
1498 RemovePolicyById(object->objectId);
1499 break;
1500
1503 break;
1504
1507 break;
1508
1511 break;
1512
1513 case CastRelationId:
1516 case LanguageRelationId:
1528 case TSParserRelationId:
1537 case AuthMemRelationId:
1538 DropObjectById(object);
1539 break;
1540
1541 /*
1542 * These global object types are not supported here.
1543 */
1544 case AuthIdRelationId:
1545 case DatabaseRelationId:
1549 elog(ERROR, "global objects cannot be deleted by doDeletion");
1550 break;
1551
1552 default:
1553 elog(ERROR, "unsupported object class: %u", object->classId);
1554 }
1555}
void DeleteSequenceTuple(Oid relid)
Definition sequence.c:571
static void DropObjectById(const ObjectAddress *object)
#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:2330
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:2123
char get_rel_relkind(Oid relid)
Definition lsyscache.c:2223
void RemoveOperatorById(Oid operOid)
void RemoveAttrDefaultById(Oid attrdefId)
Definition pg_attrdef.c:209
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)
void RemoveStatisticsById(Oid statsOid)
Definition statscmds.c:787
void RemoveTriggerById(Oid trigOid)
Definition trigger.c:1293
void RemoveTSConfigurationById(Oid cfgId)
void RemoveTypeById(Oid typeOid)
Definition typecmds.c:659

References Assert, ObjectAddress::classId, DeleteSequenceTuple(), DropObjectById(), elog, ERROR, fb(), 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 1255 of file dependency.c.

1256{
1257 SysCacheIdentifier cacheId;
1258 Relation rel;
1259 HeapTuple tup;
1260
1261 cacheId = get_object_catcache_oid(object->classId);
1262
1263 rel = table_open(object->classId, RowExclusiveLock);
1264
1265 /*
1266 * Use the system cache for the oid column, if one exists.
1267 */
1268 if (cacheId >= 0)
1269 {
1270 tup = SearchSysCache1(cacheId, ObjectIdGetDatum(object->objectId));
1271 if (!HeapTupleIsValid(tup))
1272 elog(ERROR, "cache lookup failed for %s %u",
1273 get_object_class_descr(object->classId), object->objectId);
1274
1275 CatalogTupleDelete(rel, &tup->t_self);
1276
1278 }
1279 else
1280 {
1281 ScanKeyData skey[1];
1282 SysScanDesc scan;
1283
1284 ScanKeyInit(&skey[0],
1287 ObjectIdGetDatum(object->objectId));
1288
1289 scan = systable_beginscan(rel, get_object_oid_index(object->classId), true,
1290 NULL, 1, skey);
1291
1292 /* we expect exactly one match */
1293 tup = systable_getnext(scan);
1294 if (!HeapTupleIsValid(tup))
1295 elog(ERROR, "could not find tuple for %s %u",
1296 get_object_class_descr(object->classId), object->objectId);
1297
1298 CatalogTupleDelete(rel, &tup->t_self);
1299
1300 systable_endscan(scan);
1301 }
1302
1304}
AttrNumber get_object_attnum_oid(Oid class_id)
const char * get_object_class_descr(Oid class_id)
Oid get_object_oid_index(Oid class_id)
SysCacheIdentifier get_object_catcache_oid(Oid class_id)
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:220

References BTEqualStrategyNumber, CatalogTupleDelete(), ObjectAddress::classId, elog, ERROR, fb(), 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(), table_close(), and table_open().

Referenced by doDeletion().

◆ eliminate_duplicate_dependencies()

static void eliminate_duplicate_dependencies ( ObjectAddresses addrs)
static

Definition at line 2582 of file dependency.c.

2583{
2585 int oldref,
2586 newrefs;
2587
2588 /*
2589 * We can't sort if the array has "extra" data, because there's no way to
2590 * keep it in sync. Fortunately that combination of features is not
2591 * needed.
2592 */
2593 Assert(!addrs->extras);
2594
2595 if (addrs->numrefs <= 1)
2596 return; /* nothing to do */
2597
2598 /* Sort the refs so that duplicates are adjacent */
2599 qsort(addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
2601
2602 /* Remove dups */
2603 priorobj = addrs->refs;
2604 newrefs = 1;
2605 for (oldref = 1; oldref < addrs->numrefs; oldref++)
2606 {
2607 ObjectAddress *thisobj = addrs->refs + oldref;
2608
2609 if (priorobj->classId == thisobj->classId &&
2610 priorobj->objectId == thisobj->objectId)
2611 {
2612 if (priorobj->objectSubId == thisobj->objectSubId)
2613 continue; /* identical, so drop thisobj */
2614
2615 /*
2616 * If we have a whole-object reference and a reference to a part
2617 * of the same object, we don't need the whole-object reference
2618 * (for example, we don't need to reference both table foo and
2619 * column foo.bar). The whole-object reference will always appear
2620 * first in the sorted list.
2621 */
2622 if (priorobj->objectSubId == 0)
2623 {
2624 /* replace whole ref with partial */
2625 priorobj->objectSubId = thisobj->objectSubId;
2626 continue;
2627 }
2628 }
2629 /* Not identical, so add thisobj to output set */
2630 priorobj++;
2631 *priorobj = *thisobj;
2632 newrefs++;
2633 }
2634
2635 addrs->numrefs = newrefs;
2636}
static int object_address_comparator(const void *a, const void *b)
#define qsort(a, b, c, d)
Definition port.h:495

References Assert, ObjectAddresses::extras, fb(), ObjectAddresses::numrefs, object_address_comparator(), 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 1801 of file dependency.c.

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

References add_object_address(), find_expr_references_context::addrs, Aggref::aggfnoid, CMD_INSERT, CMD_UPDATE, collid, Query::commandType, OnConflictExpr::constraint, Const::consttype, CTECycleClause::cycle_mark_collation, CTECycleClause::cycle_mark_neop, CTECycleClause::cycle_mark_type, DatumGetObjectId(), elog, ereport, errcode(), errmsg, ERROR, expression_tree_walker, exprType(), fb(), 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, CoerceViaIO::resulttype, CoerceToDomain::resulttype, rt_fetch, Query::rtable, find_expr_references_context::rtables, RTE_FUNCTION, RTE_GRAPH_TABLE, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, SearchSysCacheExists1, Query::targetList, 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 2525 of file dependency.c.

2527{
2528 for (int i = 0; i < addrs->numrefs; i++)
2529 {
2530 const ObjectAddress *thisobj = addrs->refs + i;
2532
2533 /*
2534 * Use get_object_namespace() to see if this object belongs to a
2535 * schema. If not, we can skip it.
2536 */
2538
2539 /*
2540 * If the object is in a temporary namespace, complain, except if
2541 * local_temp_okay and it's our own temp namespace.
2542 */
2545 {
2546 *foundobj = *thisobj;
2547 return true;
2548 }
2549 }
2550 return false;
2551}
bool isTempNamespace(Oid namespaceId)
Definition namespace.c:3721
bool isAnyTempNamespace(Oid namespaceId)
Definition namespace.c:3759
Oid get_object_namespace(const ObjectAddress *address)

References fb(), 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 488 of file dependency.c.

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

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, fb(), findDependentObjects(), ObjectAddressExtra::flags, ObjectAddressStack::flags, Form_pg_depend, getObjectDescription(), GETSTRUCT(), HeapTupleIsValid, i, Int32GetDatum(), IsPinnedObject(), IsSharedRelation(), 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(), pg_fallthrough, qsort, ReleaseDeletionLock(), repalloc(), ScanKeyInit(), stack_address_present_add_flags(), 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 2642 of file dependency.c.

2643{
2644 const ObjectAddress *obja = (const ObjectAddress *) a;
2645 const ObjectAddress *objb = (const ObjectAddress *) b;
2646
2647 /*
2648 * Primary sort key is OID descending. Most of the time, this will result
2649 * in putting newer objects before older ones, which is likely to be the
2650 * right order to delete in.
2651 */
2652 if (obja->objectId > objb->objectId)
2653 return -1;
2654 if (obja->objectId < objb->objectId)
2655 return 1;
2656
2657 /*
2658 * Next sort on catalog ID, in case identical OIDs appear in different
2659 * catalogs. Sort direction is pretty arbitrary here.
2660 */
2661 if (obja->classId < objb->classId)
2662 return -1;
2663 if (obja->classId > objb->classId)
2664 return 1;
2665
2666 /*
2667 * Last, sort on object subId.
2668 *
2669 * We sort the subId as an unsigned int so that 0 (the whole object) will
2670 * come first. This is essential for eliminate_duplicate_dependencies,
2671 * and is also the best order for findDependentObjects.
2672 */
2673 if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
2674 return -1;
2675 if ((unsigned int) obja->objectSubId > (unsigned int) objb->objectSubId)
2676 return 1;
2677 return 0;
2678}
int b
Definition isn.c:74
int a
Definition isn.c:73

References a, b, and fb().

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 2791 of file dependency.c.

2793{
2794 int i;
2795
2796 for (i = addrs->numrefs - 1; i >= 0; i--)
2797 {
2798 const ObjectAddress *thisobj = addrs->refs + i;
2799
2800 if (object->classId == thisobj->classId &&
2801 object->objectId == thisobj->objectId)
2802 {
2803 if (object->objectSubId == thisobj->objectSubId ||
2804 thisobj->objectSubId == 0)
2805 return true;
2806 }
2807 }
2808
2809 return false;
2810}

References ObjectAddress::classId, fb(), 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 2817 of file dependency.c.

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

References ObjectAddress::classId, DEPFLAG_SUBOBJECT, ObjectAddresses::extras, fb(), 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 279 of file dependency.c.

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

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

Referenced by AlterDomainDropConstraint(), AlterDomainNotNull(), AlterPropGraph(), 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 338 of file dependency.c.

340{
343
344 Assert(behavior == DROP_RESTRICT);
345
347
348 AcquireDeletionLock(object, 0);
349
350 /*
351 * Construct a list of objects we want to delete later (ie, the given
352 * object plus everything directly or indirectly dependent on it).
353 */
355
358 flags,
359 NULL, /* empty stack */
361 NULL, /* no pendingObjects */
362 &depRel);
363
364 /*
365 * Check if deletion is allowed.
366 */
368 behavior,
369 flags,
370 object);
371
372 /* And clean up */
374
376}
@ DROP_RESTRICT

References AcquireDeletionLock(), Assert, DEPFLAG_ORIGINAL, DROP_RESTRICT, fb(), 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 388 of file dependency.c.

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

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

Referenced by ATExecDropColumn(), ATPostAlterTypeCleanup(), 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 2451 of file dependency.c.

2453{
2454 int atts_done = 0;
2455 ListCell *lc;
2456
2457 /*
2458 * Identify which RangeTblFunction produces this attnum, and see if it
2459 * returns a composite type. If so, we'd better make a dependency on the
2460 * referenced column of the composite type (or actually, of its associated
2461 * relation).
2462 */
2463 foreach(lc, rte->functions)
2464 {
2466
2467 if (attnum > atts_done &&
2469 {
2470 TupleDesc tupdesc;
2471
2472 /* If it has a coldeflist, it certainly returns RECORD */
2473 if (rtfunc->funccolnames != NIL)
2474 tupdesc = NULL; /* no need to work hard */
2475 else
2476 tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr, true);
2477 if (tupdesc && tupdesc->tdtypeid != RECORDOID)
2478 {
2479 /*
2480 * Named composite type, so individual columns could get
2481 * dropped. Make a dependency on this specific column.
2482 */
2483 Oid reltype = get_typ_typrelid(tupdesc->tdtypeid);
2484
2486 if (OidIsValid(reltype)) /* can this fail? */
2488 attnum - atts_done,
2489 context->addrs);
2490 return;
2491 }
2492 /* Nothing to do; function's result type is handled elsewhere */
2493 return;
2494 }
2495 atts_done += rtfunc->funccolcount;
2496 }
2497
2498 /* If we get here, must be looking for the ordinality column */
2499 if (rte->funcordinality && attnum == atts_done + 1)
2500 return;
2501
2502 /* this probably can't happen ... */
2503 ereport(ERROR,
2505 errmsg("column %d of relation \"%s\" does not exist",
2506 attnum, rte->eref->aliasname)));
2507}
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition funcapi.c:553
int16 attnum
#define NIL
Definition pg_list.h:68

References add_object_address(), find_expr_references_context::addrs, Assert, attnum, ereport, errcode(), errmsg, ERROR, fb(), RangeTblFunction::funcexpr, 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 2560 of file dependency.c.

2561{
2562 bool result;
2563 ObjectAddresses *addrs;
2564
2565 addrs = new_object_addresses();
2566
2567 /* Collect all dependencies from the Query */
2568 collectDependenciesOfExpr(addrs, (Node *) query, NIL);
2569
2570 /* Look for one that is temp */
2571 result = find_temp_object(addrs, false, temp_object);
2572
2573 free_object_addresses(addrs);
2574
2575 return result;
2576}
void collectDependenciesOfExpr(ObjectAddresses *addrs, Node *expr, List *rtable)
bool find_temp_object(const ObjectAddresses *addrs, bool local_temp_okay, ObjectAddress *foundobj)

References collectDependenciesOfExpr(), fb(), 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 1624 of file dependency.c.

1627{
1628 ObjectAddresses *addrs;
1629
1630 addrs = new_object_addresses();
1631
1632 /* Collect all dependencies from the expression */
1633 collectDependenciesOfExpr(addrs, expr, rtable);
1634
1635 /* Remove duplicates */
1637
1638 /* And record 'em */
1640 addrs->refs, addrs->numrefs,
1641 behavior);
1642
1643 free_object_addresses(addrs);
1644}

References collectDependenciesOfExpr(), eliminate_duplicate_dependencies(), fb(), 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 1699 of file dependency.c.

1704{
1706 RangeTblEntry rte = {0};
1707
1708 context.addrs = new_object_addresses();
1709
1710 /* We gin up a rather bogus rangetable list to handle Vars */
1711 rte.type = T_RangeTblEntry;
1712 rte.rtekind = RTE_RELATION;
1713 rte.relid = relId;
1714 rte.relkind = RELKIND_RELATION; /* no need for exactness here */
1715 rte.rellockmode = AccessShareLock;
1716
1717 context.rtables = list_make1(list_make1(&rte));
1718
1719 /* Scan the expression tree for referenceable objects */
1720 find_expr_references_walker(expr, &context);
1721
1722 /* Remove any duplicates */
1724
1725 /* Separate self-dependencies if necessary */
1726 if ((behavior != self_behavior || reverse_self) &&
1727 context.addrs->numrefs > 0)
1728 {
1731 int oldref,
1732 outrefs;
1733
1735
1736 outobj = context.addrs->refs;
1737 outrefs = 0;
1738 for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
1739 {
1740 ObjectAddress *thisobj = context.addrs->refs + oldref;
1741
1742 if (thisobj->classId == RelationRelationId &&
1743 thisobj->objectId == relId)
1744 {
1745 /* Move this ref into self_addrs */
1747 }
1748 else
1749 {
1750 /* Keep it in context.addrs */
1751 *outobj = *thisobj;
1752 outobj++;
1753 outrefs++;
1754 }
1755 }
1756 context.addrs->numrefs = outrefs;
1757
1758 /* Record the self-dependencies with the appropriate direction */
1759 if (!reverse_self)
1761 self_addrs->refs, self_addrs->numrefs,
1763 else
1764 {
1765 /* Can't use recordMultipleDependencies, so do it the hard way */
1766 int selfref;
1767
1768 for (selfref = 0; selfref < self_addrs->numrefs; selfref++)
1769 {
1771
1773 }
1774 }
1775
1777 }
1778
1779 /* Record the external dependencies */
1781 context.addrs->refs, context.addrs->numrefs,
1782 behavior);
1783
1785}
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
#define AccessShareLock
Definition lockdefs.h:36
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition pg_depend.c:47

References AccessShareLock, add_exact_object_address(), find_expr_references_context::addrs, eliminate_duplicate_dependencies(), fb(), find_expr_references_walker(), free_object_addresses(), list_make1, new_object_addresses(), ObjectAddresses::numrefs, recordDependencyOn(), recordMultipleDependencies(), ObjectAddresses::refs, find_expr_references_context::rtables, and RTE_RELATION.

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

◆ ReleaseDeletionLock()

void ReleaseDeletionLock ( const ObjectAddress object)

Definition at line 1599 of file dependency.c.

1600{
1601 if (object->classId == RelationRelationId)
1603 else
1604 /* assume we should lock the whole object not a sub-object */
1605 UnlockDatabaseObject(object->classId, object->objectId, 0,
1607}
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, fb(), 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 1046 of file dependency.c.

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

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

◆ sort_object_addresses()

void sort_object_addresses ( ObjectAddresses addrs)

Definition at line 2959 of file dependency.c.

2960{
2961 if (addrs->numrefs > 1)
2962 qsort(addrs->refs, addrs->numrefs,
2963 sizeof(ObjectAddress),
2965}

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 2890 of file dependency.c.

2893{
2894 bool result = false;
2896
2897 for (stackptr = stack; stackptr; stackptr = stackptr->next)
2898 {
2899 const ObjectAddress *thisobj = stackptr->object;
2900
2901 if (object->classId == thisobj->classId &&
2902 object->objectId == thisobj->objectId)
2903 {
2904 if (object->objectSubId == thisobj->objectSubId)
2905 {
2906 stackptr->flags |= flags;
2907 result = true;
2908 }
2909 else if (thisobj->objectSubId == 0)
2910 {
2911 /*
2912 * We're visiting a column with whole table already on stack.
2913 * As in object_address_present_add_flags(), we can skip
2914 * further processing of the subobject, but we don't want to
2915 * propagate flags for the subobject to the whole object.
2916 */
2917 result = true;
2918 }
2919 else if (object->objectSubId == 0)
2920 {
2921 /*
2922 * We're visiting a table with column already on stack. As in
2923 * object_address_present_add_flags(), we should propagate
2924 * flags for the whole object to each of its subobjects.
2925 */
2926 if (flags)
2927 stackptr->flags |= (flags | DEPFLAG_SUBOBJECT);
2928 }
2929 }
2930 }
2931
2932 return result;
2933}

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

Referenced by findDependentObjects().