2002-07-12 20:43:19 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* dependency.c
|
|
|
|
* Routines to support inter-object dependencies.
|
|
|
|
*
|
|
|
|
*
|
2008-01-01 20:46:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
2002-07-12 20:43:19 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2008-01-01 20:46:01 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/catalog/dependency.c,v 1.69 2008/01/01 19:45:48 momjian Exp $
|
2002-07-12 20:43:19 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/genam.h"
|
|
|
|
#include "access/heapam.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/xact.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "catalog/dependency.h"
|
|
|
|
#include "catalog/heap.h"
|
|
|
|
#include "catalog/index.h"
|
|
|
|
#include "catalog/indexing.h"
|
2002-07-30 00:14:11 +02:00
|
|
|
#include "catalog/namespace.h"
|
2006-12-23 01:43:13 +01:00
|
|
|
#include "catalog/pg_amop.h"
|
|
|
|
#include "catalog/pg_amproc.h"
|
2002-07-15 18:33:32 +02:00
|
|
|
#include "catalog/pg_attrdef.h"
|
2005-07-07 22:40:02 +02:00
|
|
|
#include "catalog/pg_authid.h"
|
2002-07-30 00:14:11 +02:00
|
|
|
#include "catalog/pg_cast.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "catalog/pg_constraint.h"
|
2002-07-25 12:07:13 +02:00
|
|
|
#include "catalog/pg_conversion.h"
|
2005-07-07 22:40:02 +02:00
|
|
|
#include "catalog/pg_database.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "catalog/pg_depend.h"
|
|
|
|
#include "catalog/pg_language.h"
|
2005-04-14 22:03:27 +02:00
|
|
|
#include "catalog/pg_namespace.h"
|
2002-07-30 00:14:11 +02:00
|
|
|
#include "catalog/pg_opclass.h"
|
2005-04-14 22:03:27 +02:00
|
|
|
#include "catalog/pg_operator.h"
|
2006-12-23 01:43:13 +01:00
|
|
|
#include "catalog/pg_opfamily.h"
|
2005-04-14 03:38:22 +02:00
|
|
|
#include "catalog/pg_proc.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "catalog/pg_rewrite.h"
|
2005-07-07 22:40:02 +02:00
|
|
|
#include "catalog/pg_tablespace.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "catalog/pg_trigger.h"
|
2007-08-21 03:11:32 +02:00
|
|
|
#include "catalog/pg_ts_config.h"
|
|
|
|
#include "catalog/pg_ts_dict.h"
|
|
|
|
#include "catalog/pg_ts_parser.h"
|
|
|
|
#include "catalog/pg_ts_template.h"
|
2005-04-14 03:38:22 +02:00
|
|
|
#include "catalog/pg_type.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "commands/comment.h"
|
2005-07-07 22:40:02 +02:00
|
|
|
#include "commands/dbcommands.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "commands/defrem.h"
|
|
|
|
#include "commands/proclang.h"
|
2002-07-18 18:47:26 +02:00
|
|
|
#include "commands/schemacmds.h"
|
2005-07-07 22:40:02 +02:00
|
|
|
#include "commands/tablespace.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "commands/trigger.h"
|
2002-12-06 06:00:34 +01:00
|
|
|
#include "commands/typecmds.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "miscadmin.h"
|
2002-07-16 07:53:34 +02:00
|
|
|
#include "optimizer/clauses.h"
|
|
|
|
#include "parser/parsetree.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "rewrite/rewriteRemove.h"
|
2002-07-30 00:14:11 +02:00
|
|
|
#include "utils/builtins.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "utils/fmgroids.h"
|
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
|
|
|
|
2002-07-16 07:53:34 +02:00
|
|
|
/* expansible list of ObjectAddresses */
|
2006-08-20 23:56:16 +02:00
|
|
|
struct ObjectAddresses
|
2002-07-16 07:53:34 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ObjectAddress *refs; /* => palloc'd array */
|
|
|
|
int numrefs; /* current number of references */
|
|
|
|
int maxrefs; /* current size of palloc'd array */
|
2006-08-20 23:56:16 +02:00
|
|
|
};
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
/* typedef ObjectAddresses appears in dependency.h */
|
2002-07-16 07:53:34 +02:00
|
|
|
|
|
|
|
/* for find_expr_references_walker */
|
|
|
|
typedef struct
|
|
|
|
{
|
2006-08-20 23:56:16 +02:00
|
|
|
ObjectAddresses *addrs; /* addresses being accumulated */
|
2002-09-04 22:31:48 +02:00
|
|
|
List *rtables; /* list of rangetables to resolve Vars */
|
2002-07-16 07:53:34 +02:00
|
|
|
} find_expr_references_context;
|
|
|
|
|
|
|
|
/*
|
2005-04-14 22:03:27 +02:00
|
|
|
* This constant table maps ObjectClasses to the corresponding catalog OIDs.
|
|
|
|
* See also getObjectClass().
|
2002-07-16 07:53:34 +02:00
|
|
|
*/
|
2005-04-14 22:03:27 +02:00
|
|
|
static const Oid object_classes[MAX_OCLASS] = {
|
2007-11-15 22:14:46 +01:00
|
|
|
RelationRelationId, /* OCLASS_CLASS */
|
|
|
|
ProcedureRelationId, /* OCLASS_PROC */
|
|
|
|
TypeRelationId, /* OCLASS_TYPE */
|
|
|
|
CastRelationId, /* OCLASS_CAST */
|
|
|
|
ConstraintRelationId, /* OCLASS_CONSTRAINT */
|
|
|
|
ConversionRelationId, /* OCLASS_CONVERSION */
|
|
|
|
AttrDefaultRelationId, /* OCLASS_DEFAULT */
|
|
|
|
LanguageRelationId, /* OCLASS_LANGUAGE */
|
|
|
|
OperatorRelationId, /* OCLASS_OPERATOR */
|
|
|
|
OperatorClassRelationId, /* OCLASS_OPCLASS */
|
|
|
|
OperatorFamilyRelationId, /* OCLASS_OPFAMILY */
|
2006-12-23 01:43:13 +01:00
|
|
|
AccessMethodOperatorRelationId, /* OCLASS_AMOP */
|
|
|
|
AccessMethodProcedureRelationId, /* OCLASS_AMPROC */
|
2007-11-15 22:14:46 +01:00
|
|
|
RewriteRelationId, /* OCLASS_REWRITE */
|
|
|
|
TriggerRelationId, /* OCLASS_TRIGGER */
|
|
|
|
NamespaceRelationId, /* OCLASS_SCHEMA */
|
|
|
|
TSParserRelationId, /* OCLASS_TSPARSER */
|
|
|
|
TSDictionaryRelationId, /* OCLASS_TSDICT */
|
|
|
|
TSTemplateRelationId, /* OCLASS_TSTEMPLATE */
|
|
|
|
TSConfigRelationId, /* OCLASS_TSCONFIG */
|
|
|
|
AuthIdRelationId, /* OCLASS_ROLE */
|
|
|
|
DatabaseRelationId, /* OCLASS_DATABASE */
|
|
|
|
TableSpaceRelationId /* OCLASS_TBLSPACE */
|
2005-04-14 22:03:27 +02:00
|
|
|
};
|
2002-07-16 07:53:34 +02:00
|
|
|
|
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
static void performDeletionWithList(const ObjectAddress *object,
|
|
|
|
ObjectAddresses *oktodelete,
|
|
|
|
DropBehavior behavior,
|
|
|
|
ObjectAddresses *alreadyDeleted);
|
2002-09-22 02:37:09 +02:00
|
|
|
static void findAutoDeletableObjects(const ObjectAddress *object,
|
2003-08-04 02:43:34 +02:00
|
|
|
ObjectAddresses *oktodelete,
|
2006-08-20 23:56:16 +02:00
|
|
|
Relation depRel, bool addself);
|
2002-07-12 20:43:19 +02:00
|
|
|
static bool recursiveDeletion(const ObjectAddress *object,
|
2002-09-04 22:31:48 +02:00
|
|
|
DropBehavior behavior,
|
2003-03-06 23:54:49 +01:00
|
|
|
int msglevel,
|
2002-09-04 22:31:48 +02:00
|
|
|
const ObjectAddress *callingObject,
|
2002-09-22 02:37:09 +02:00
|
|
|
ObjectAddresses *oktodelete,
|
2006-08-20 23:56:16 +02:00
|
|
|
Relation depRel,
|
|
|
|
ObjectAddresses *alreadyDeleted);
|
2003-02-07 02:33:06 +01:00
|
|
|
static bool deleteDependentObjects(const ObjectAddress *object,
|
2003-08-04 02:43:34 +02:00
|
|
|
const char *objDescription,
|
|
|
|
DropBehavior behavior,
|
|
|
|
int msglevel,
|
|
|
|
ObjectAddresses *oktodelete,
|
|
|
|
Relation depRel);
|
2002-07-12 20:43:19 +02:00
|
|
|
static void doDeletion(const ObjectAddress *object);
|
2002-07-16 07:53:34 +02:00
|
|
|
static bool find_expr_references_walker(Node *node,
|
2002-09-04 22:31:48 +02:00
|
|
|
find_expr_references_context *context);
|
2002-07-16 07:53:34 +02:00
|
|
|
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs);
|
|
|
|
static int object_address_comparator(const void *a, const void *b);
|
2004-05-05 06:48:48 +02:00
|
|
|
static void add_object_address(ObjectClass oclass, Oid objectId, int32 subId,
|
2002-09-04 22:31:48 +02:00
|
|
|
ObjectAddresses *addrs);
|
2002-07-15 18:33:32 +02:00
|
|
|
static void getRelationDescription(StringInfo buffer, Oid relid);
|
2006-12-23 01:43:13 +01:00
|
|
|
static void getOpFamilyDescription(StringInfo buffer, Oid opfid);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* performDeletion: attempt to drop the specified object. If CASCADE
|
|
|
|
* behavior is specified, also drop any dependent objects (recursively).
|
|
|
|
* If RESTRICT behavior is specified, error out if there are any dependent
|
|
|
|
* objects, except for those that should be implicitly dropped anyway
|
|
|
|
* according to the dependency type.
|
|
|
|
*
|
|
|
|
* This is the outer control routine for all forms of DROP that drop objects
|
|
|
|
* that can participate in dependencies.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
performDeletion(const ObjectAddress *object,
|
|
|
|
DropBehavior behavior)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
char *objDescription;
|
|
|
|
Relation depRel;
|
2006-08-20 23:56:16 +02:00
|
|
|
ObjectAddresses *oktodelete;
|
2002-07-12 20:43:19 +02:00
|
|
|
|
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* Get object description for possible use in failure message. Must do
|
|
|
|
* this before deleting it ...
|
2002-07-12 20:43:19 +02:00
|
|
|
*/
|
|
|
|
objDescription = getObjectDescription(object);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We save some cycles by opening pg_depend just once and passing the
|
|
|
|
* Relation pointer down to all the recursive deletion steps.
|
|
|
|
*/
|
2005-04-14 22:03:27 +02:00
|
|
|
depRel = heap_open(DependRelationId, RowExclusiveLock);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-09-22 02:37:09 +02:00
|
|
|
/*
|
|
|
|
* Construct a list of objects that are reachable by AUTO or INTERNAL
|
2005-10-15 04:49:52 +02:00
|
|
|
* dependencies from the target object. These should be deleted silently,
|
|
|
|
* even if the actual deletion pass first reaches one of them via a
|
|
|
|
* non-auto dependency.
|
2002-09-22 02:37:09 +02:00
|
|
|
*/
|
2006-08-20 23:56:16 +02:00
|
|
|
oktodelete = new_object_addresses();
|
2002-09-22 02:37:09 +02:00
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
findAutoDeletableObjects(object, oktodelete, depRel, true);
|
2002-09-22 02:37:09 +02:00
|
|
|
|
2003-03-06 23:54:49 +01:00
|
|
|
if (!recursiveDeletion(object, behavior, NOTICE,
|
2006-08-20 23:56:16 +02:00
|
|
|
NULL, oktodelete, depRel, NULL))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("cannot drop %s because other objects depend on it",
|
|
|
|
objDescription),
|
|
|
|
errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
free_object_addresses(oktodelete);
|
2002-09-22 02:37:09 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
heap_close(depRel, RowExclusiveLock);
|
|
|
|
|
|
|
|
pfree(objDescription);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
/*
|
|
|
|
* performDeletionWithList: As above, but the oktodelete list may have already
|
|
|
|
* filled with some objects. Also, the deleted objects are saved in the
|
|
|
|
* alreadyDeleted list.
|
|
|
|
*
|
2006-12-23 01:43:13 +01:00
|
|
|
* XXX performDeletion could be refactored to be a thin wrapper around this
|
2006-08-20 23:56:16 +02:00
|
|
|
* function.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
performDeletionWithList(const ObjectAddress *object,
|
|
|
|
ObjectAddresses *oktodelete,
|
|
|
|
DropBehavior behavior,
|
|
|
|
ObjectAddresses *alreadyDeleted)
|
|
|
|
{
|
|
|
|
char *objDescription;
|
|
|
|
Relation depRel;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get object description for possible use in failure message. Must do
|
|
|
|
* this before deleting it ...
|
|
|
|
*/
|
|
|
|
objDescription = getObjectDescription(object);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We save some cycles by opening pg_depend just once and passing the
|
|
|
|
* Relation pointer down to all the recursive deletion steps.
|
|
|
|
*/
|
|
|
|
depRel = heap_open(DependRelationId, RowExclusiveLock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a list of objects that are reachable by AUTO or INTERNAL
|
|
|
|
* dependencies from the target object. These should be deleted silently,
|
|
|
|
* even if the actual deletion pass first reaches one of them via a
|
|
|
|
* non-auto dependency.
|
|
|
|
*/
|
|
|
|
findAutoDeletableObjects(object, oktodelete, depRel, true);
|
|
|
|
|
|
|
|
if (!recursiveDeletion(object, behavior, NOTICE,
|
|
|
|
NULL, oktodelete, depRel, alreadyDeleted))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
|
|
|
|
errmsg("cannot drop %s because other objects depend on it",
|
|
|
|
objDescription),
|
|
|
|
errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
|
|
|
|
|
|
|
|
heap_close(depRel, RowExclusiveLock);
|
|
|
|
|
|
|
|
pfree(objDescription);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* performMultipleDeletion: Similar to performDeletion, but act on multiple
|
|
|
|
* objects at once.
|
|
|
|
*
|
|
|
|
* The main difference from issuing multiple performDeletion calls is that the
|
|
|
|
* list of objects that would be implicitly dropped, for each object to be
|
|
|
|
* dropped, is the union of the implicit-object list for all objects. This
|
|
|
|
* makes each check be more relaxed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
performMultipleDeletions(const ObjectAddresses *objects,
|
|
|
|
DropBehavior behavior)
|
|
|
|
{
|
|
|
|
ObjectAddresses *implicit;
|
|
|
|
ObjectAddresses *alreadyDeleted;
|
2006-10-04 02:30:14 +02:00
|
|
|
Relation depRel;
|
|
|
|
int i;
|
2006-08-20 23:56:16 +02:00
|
|
|
|
|
|
|
implicit = new_object_addresses();
|
|
|
|
alreadyDeleted = new_object_addresses();
|
|
|
|
|
|
|
|
depRel = heap_open(DependRelationId, RowExclusiveLock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the list of all objects that would be deleted after deleting the
|
|
|
|
* whole "objects" list. We do this by creating a list of all implicit
|
|
|
|
* (INTERNAL and AUTO) dependencies for each object we collected above.
|
|
|
|
* Note that we must exclude the objects themselves from this list!
|
|
|
|
*/
|
|
|
|
for (i = 0; i < objects->numrefs; i++)
|
|
|
|
{
|
|
|
|
ObjectAddress obj = objects->refs[i];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's in the implicit list, we don't need to delete it explicitly
|
|
|
|
* nor follow the dependencies, because that was already done in a
|
|
|
|
* previous iteration.
|
|
|
|
*/
|
|
|
|
if (object_address_present(&obj, implicit))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Add the objects dependent on this one to the global list of
|
|
|
|
* implicit objects.
|
2006-08-20 23:56:16 +02:00
|
|
|
*/
|
|
|
|
findAutoDeletableObjects(&obj, implicit, depRel, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the deletion. */
|
|
|
|
for (i = 0; i < objects->numrefs; i++)
|
|
|
|
{
|
|
|
|
ObjectAddress obj = objects->refs[i];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip this object if it was already deleted in a previous iteration.
|
|
|
|
*/
|
|
|
|
if (object_address_present(&obj, alreadyDeleted))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip this object if it's also present in the list of implicit
|
|
|
|
* objects --- it will be deleted later.
|
|
|
|
*/
|
|
|
|
if (object_address_present(&obj, implicit))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* delete it */
|
|
|
|
performDeletionWithList(&obj, implicit, behavior, alreadyDeleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_close(depRel, RowExclusiveLock);
|
|
|
|
|
|
|
|
free_object_addresses(implicit);
|
|
|
|
free_object_addresses(alreadyDeleted);
|
|
|
|
}
|
|
|
|
|
2003-02-07 02:33:06 +01:00
|
|
|
/*
|
|
|
|
* deleteWhatDependsOn: attempt to drop everything that depends on the
|
2003-08-04 02:43:34 +02:00
|
|
|
* specified object, though not the object itself. Behavior is always
|
2003-02-07 02:33:06 +01:00
|
|
|
* CASCADE.
|
|
|
|
*
|
|
|
|
* This is currently used only to clean out the contents of a schema
|
2003-03-06 23:54:49 +01:00
|
|
|
* (namespace): the passed object is a namespace. We normally want this
|
|
|
|
* to be done silently, so there's an option to suppress NOTICE messages.
|
2003-02-07 02:33:06 +01:00
|
|
|
*/
|
|
|
|
void
|
2003-03-06 23:54:49 +01:00
|
|
|
deleteWhatDependsOn(const ObjectAddress *object,
|
|
|
|
bool showNotices)
|
2003-02-07 02:33:06 +01:00
|
|
|
{
|
|
|
|
char *objDescription;
|
|
|
|
Relation depRel;
|
2006-08-20 23:56:16 +02:00
|
|
|
ObjectAddresses *oktodelete;
|
2003-02-07 02:33:06 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get object description for possible use in failure messages
|
|
|
|
*/
|
|
|
|
objDescription = getObjectDescription(object);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We save some cycles by opening pg_depend just once and passing the
|
|
|
|
* Relation pointer down to all the recursive deletion steps.
|
|
|
|
*/
|
2005-04-14 22:03:27 +02:00
|
|
|
depRel = heap_open(DependRelationId, RowExclusiveLock);
|
2003-02-07 02:33:06 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a list of objects that are reachable by AUTO or INTERNAL
|
2005-10-15 04:49:52 +02:00
|
|
|
* dependencies from the target object. These should be deleted silently,
|
|
|
|
* even if the actual deletion pass first reaches one of them via a
|
|
|
|
* non-auto dependency.
|
2003-02-07 02:33:06 +01:00
|
|
|
*/
|
2006-08-20 23:56:16 +02:00
|
|
|
oktodelete = new_object_addresses();
|
2003-02-07 02:33:06 +01:00
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
findAutoDeletableObjects(object, oktodelete, depRel, true);
|
2003-02-07 02:33:06 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Now invoke only step 2 of recursiveDeletion: just recurse to the stuff
|
|
|
|
* dependent on the given object.
|
2003-02-07 02:33:06 +01:00
|
|
|
*/
|
|
|
|
if (!deleteDependentObjects(object, objDescription,
|
2003-03-06 23:54:49 +01:00
|
|
|
DROP_CASCADE,
|
2003-05-27 19:49:47 +02:00
|
|
|
showNotices ? NOTICE : DEBUG2,
|
2006-08-20 23:56:16 +02:00
|
|
|
oktodelete, depRel))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
|
|
|
|
errmsg("failed to drop all objects depending on %s",
|
|
|
|
objDescription)));
|
2003-02-07 02:33:06 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We do not need CommandCounterIncrement here, since if step 2 did
|
|
|
|
* anything then each recursive call will have ended with one.
|
|
|
|
*/
|
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
free_object_addresses(oktodelete);
|
2003-02-07 02:33:06 +01:00
|
|
|
|
|
|
|
heap_close(depRel, RowExclusiveLock);
|
|
|
|
|
|
|
|
pfree(objDescription);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
2002-09-22 02:37:09 +02:00
|
|
|
* findAutoDeletableObjects: find all objects that are reachable by AUTO or
|
|
|
|
* INTERNAL dependency paths from the given object. Add them all to the
|
2006-08-20 23:56:16 +02:00
|
|
|
* oktodelete list. If addself is true, the originally given object will also
|
|
|
|
* be added to the list.
|
2002-09-22 02:37:09 +02:00
|
|
|
*
|
|
|
|
* depRel is the already-open pg_depend relation.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
findAutoDeletableObjects(const ObjectAddress *object,
|
|
|
|
ObjectAddresses *oktodelete,
|
2006-08-20 23:56:16 +02:00
|
|
|
Relation depRel, bool addself)
|
2002-09-22 02:37:09 +02:00
|
|
|
{
|
|
|
|
ScanKeyData key[3];
|
|
|
|
int nkeys;
|
|
|
|
SysScanDesc scan;
|
|
|
|
HeapTuple tup;
|
|
|
|
ObjectAddress otherObject;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If this object is already in oktodelete, then we already visited it;
|
|
|
|
* don't do so again (this prevents infinite recursion if there's a loop
|
|
|
|
* in pg_depend). Otherwise, add it.
|
2002-09-22 02:37:09 +02:00
|
|
|
*/
|
|
|
|
if (object_address_present(object, oktodelete))
|
|
|
|
return;
|
2006-08-20 23:56:16 +02:00
|
|
|
if (addself)
|
|
|
|
add_exact_object_address(object, oktodelete);
|
2002-09-22 02:37:09 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan pg_depend records that link to this object, showing the things
|
2005-10-15 04:49:52 +02:00
|
|
|
* that depend on it. For each one that is AUTO or INTERNAL, visit the
|
|
|
|
* referencing object.
|
2002-09-22 02:37:09 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* When dropping a whole object (subId = 0), find pg_depend records for
|
|
|
|
* its sub-objects too.
|
2002-09-22 02:37:09 +02:00
|
|
|
*/
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key[0],
|
|
|
|
Anum_pg_depend_refclassid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->classId));
|
|
|
|
ScanKeyInit(&key[1],
|
|
|
|
Anum_pg_depend_refobjid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->objectId));
|
2002-09-22 02:37:09 +02:00
|
|
|
if (object->objectSubId != 0)
|
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key[2],
|
|
|
|
Anum_pg_depend_refobjsubid,
|
|
|
|
BTEqualStrategyNumber, F_INT4EQ,
|
|
|
|
Int32GetDatum(object->objectSubId));
|
2002-09-22 02:37:09 +02:00
|
|
|
nkeys = 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nkeys = 2;
|
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
scan = systable_beginscan(depRel, DependReferenceIndexId, true,
|
2002-09-22 02:37:09 +02:00
|
|
|
SnapshotNow, nkeys, key);
|
|
|
|
|
|
|
|
while (HeapTupleIsValid(tup = systable_getnext(scan)))
|
|
|
|
{
|
|
|
|
Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
|
|
|
|
|
|
|
|
switch (foundDep->deptype)
|
|
|
|
{
|
|
|
|
case DEPENDENCY_NORMAL:
|
|
|
|
/* ignore */
|
|
|
|
break;
|
|
|
|
case DEPENDENCY_AUTO:
|
|
|
|
case DEPENDENCY_INTERNAL:
|
|
|
|
/* recurse */
|
|
|
|
otherObject.classId = foundDep->classid;
|
|
|
|
otherObject.objectId = foundDep->objid;
|
|
|
|
otherObject.objectSubId = foundDep->objsubid;
|
2006-08-20 23:56:16 +02:00
|
|
|
findAutoDeletableObjects(&otherObject, oktodelete, depRel, true);
|
2002-09-22 02:37:09 +02:00
|
|
|
break;
|
|
|
|
case DEPENDENCY_PIN:
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-09-22 02:37:09 +02:00
|
|
|
/*
|
2003-07-21 03:59:11 +02:00
|
|
|
* For a PIN dependency we just ereport immediately; there
|
2005-10-15 04:49:52 +02:00
|
|
|
* won't be any others to examine, and we aren't ever going to
|
|
|
|
* let the user delete it.
|
2002-09-22 02:37:09 +02:00
|
|
|
*/
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
|
|
|
|
errmsg("cannot drop %s because it is required by the database system",
|
|
|
|
getObjectDescription(object))));
|
2002-09-22 02:37:09 +02:00
|
|
|
break;
|
|
|
|
default:
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "unrecognized dependency type '%c' for %s",
|
2002-09-22 02:37:09 +02:00
|
|
|
foundDep->deptype, getObjectDescription(object));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
systable_endscan(scan);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* recursiveDeletion: delete a single object for performDeletion, plus
|
|
|
|
* (recursively) anything that depends on it.
|
2002-07-12 20:43:19 +02:00
|
|
|
*
|
2002-07-16 07:53:34 +02:00
|
|
|
* Returns TRUE if successful, FALSE if not.
|
|
|
|
*
|
|
|
|
* callingObject is NULL at the outer level, else identifies the object that
|
|
|
|
* we recursed from (the reference object that someone else needs to delete).
|
2002-09-22 02:37:09 +02:00
|
|
|
*
|
|
|
|
* oktodelete is a list of objects verified deletable (ie, reachable by one
|
|
|
|
* or more AUTO or INTERNAL dependencies from the original target).
|
|
|
|
*
|
2002-07-16 07:53:34 +02:00
|
|
|
* depRel is the already-open pg_depend relation.
|
2002-07-12 20:43:19 +02:00
|
|
|
*
|
2002-09-22 02:37:09 +02:00
|
|
|
*
|
2003-07-21 03:59:11 +02:00
|
|
|
* In RESTRICT mode, we perform all the deletions anyway, but ereport a message
|
2002-07-12 20:43:19 +02:00
|
|
|
* and return FALSE if we find a restriction violation. performDeletion
|
|
|
|
* will then abort the transaction to nullify the deletions. We have to
|
|
|
|
* do it this way to (a) report all the direct and indirect dependencies
|
|
|
|
* while (b) not going into infinite recursion if there's a cycle.
|
2002-07-16 07:53:34 +02:00
|
|
|
*
|
|
|
|
* This is even more complex than one could wish, because it is possible for
|
2002-09-22 02:37:09 +02:00
|
|
|
* the same pair of objects to be related by both NORMAL and AUTO/INTERNAL
|
|
|
|
* dependencies. Also, we might have a situation where we've been asked to
|
|
|
|
* delete object A, and objects B and C both have AUTO dependencies on A,
|
|
|
|
* but B also has a NORMAL dependency on C. (Since any of these paths might
|
|
|
|
* be indirect, we can't prevent these scenarios, but must cope instead.)
|
|
|
|
* If we visit C before B then we would mistakenly decide that the B->C link
|
|
|
|
* should prevent the restricted drop from occurring. To handle this, we make
|
|
|
|
* a pre-scan to find all the objects that are auto-deletable from A. If we
|
|
|
|
* visit C first, but B is present in the oktodelete list, then we make no
|
|
|
|
* complaint but recurse to delete B anyway. (Note that in general we must
|
|
|
|
* delete B before deleting C; the drop routine for B may try to access C.)
|
2002-07-16 07:53:34 +02:00
|
|
|
*
|
2002-09-22 02:37:09 +02:00
|
|
|
* Note: in the case where the path to B is traversed first, we will not
|
|
|
|
* see the NORMAL dependency when we reach C, because of the pg_depend
|
|
|
|
* removals done in step 1. The oktodelete list is necessary just
|
|
|
|
* to make the behavior independent of the order in which pg_depend
|
2002-07-16 07:53:34 +02:00
|
|
|
* entries are visited.
|
2002-07-12 20:43:19 +02:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
recursiveDeletion(const ObjectAddress *object,
|
|
|
|
DropBehavior behavior,
|
2003-03-06 23:54:49 +01:00
|
|
|
int msglevel,
|
2002-07-16 07:53:34 +02:00
|
|
|
const ObjectAddress *callingObject,
|
2002-09-22 02:37:09 +02:00
|
|
|
ObjectAddresses *oktodelete,
|
2006-08-20 23:56:16 +02:00
|
|
|
Relation depRel,
|
|
|
|
ObjectAddresses *alreadyDeleted)
|
2002-07-12 20:43:19 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
bool ok = true;
|
|
|
|
char *objDescription;
|
|
|
|
ScanKeyData key[3];
|
|
|
|
int nkeys;
|
|
|
|
SysScanDesc scan;
|
|
|
|
HeapTuple tup;
|
|
|
|
ObjectAddress otherObject;
|
|
|
|
ObjectAddress owningObject;
|
|
|
|
bool amOwned = false;
|
2002-07-12 20:43:19 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get object description for possible use in messages. Must do this
|
|
|
|
* before deleting it ...
|
|
|
|
*/
|
|
|
|
objDescription = getObjectDescription(object);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Step 1: find and remove pg_depend records that link from this object to
|
|
|
|
* others. We have to do this anyway, and doing it first ensures that we
|
|
|
|
* avoid infinite recursion in the case of cycles. Also, some dependency
|
|
|
|
* types require extra processing here.
|
2002-07-12 20:43:19 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* When dropping a whole object (subId = 0), remove all pg_depend records
|
|
|
|
* for its sub-objects too.
|
2002-07-12 20:43:19 +02:00
|
|
|
*/
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key[0],
|
|
|
|
Anum_pg_depend_classid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->classId));
|
|
|
|
ScanKeyInit(&key[1],
|
|
|
|
Anum_pg_depend_objid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->objectId));
|
2002-07-12 20:43:19 +02:00
|
|
|
if (object->objectSubId != 0)
|
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key[2],
|
|
|
|
Anum_pg_depend_objsubid,
|
|
|
|
BTEqualStrategyNumber, F_INT4EQ,
|
|
|
|
Int32GetDatum(object->objectSubId));
|
2002-07-12 20:43:19 +02:00
|
|
|
nkeys = 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nkeys = 2;
|
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
scan = systable_beginscan(depRel, DependDependerIndexId, true,
|
2002-07-12 20:43:19 +02:00
|
|
|
SnapshotNow, nkeys, key);
|
|
|
|
|
|
|
|
while (HeapTupleIsValid(tup = systable_getnext(scan)))
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
|
|
|
otherObject.classId = foundDep->refclassid;
|
|
|
|
otherObject.objectId = foundDep->refobjid;
|
|
|
|
otherObject.objectSubId = foundDep->refobjsubid;
|
|
|
|
|
|
|
|
switch (foundDep->deptype)
|
|
|
|
{
|
|
|
|
case DEPENDENCY_NORMAL:
|
|
|
|
case DEPENDENCY_AUTO:
|
|
|
|
/* no problem */
|
|
|
|
break;
|
|
|
|
case DEPENDENCY_INTERNAL:
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* This object is part of the internal implementation of
|
|
|
|
* another object. We have three cases:
|
2002-07-16 07:53:34 +02:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* 1. At the outermost recursion level, disallow the DROP. (We
|
|
|
|
* just ereport here, rather than proceeding, since no other
|
|
|
|
* dependencies are likely to be interesting.)
|
2002-07-12 20:43:19 +02:00
|
|
|
*/
|
2002-07-16 07:53:34 +02:00
|
|
|
if (callingObject == NULL)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
char *otherObjDesc = getObjectDescription(&otherObject);
|
2002-07-16 07:53:34 +02:00
|
|
|
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
|
|
|
|
errmsg("cannot drop %s because %s requires it",
|
|
|
|
objDescription, otherObjDesc),
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 20:10:30 +01:00
|
|
|
errhint("You can drop %s instead.",
|
2005-10-15 04:49:52 +02:00
|
|
|
otherObjDesc)));
|
2002-07-16 07:53:34 +02:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-07-16 07:53:34 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* 2. When recursing from the other end of this dependency,
|
|
|
|
* it's okay to continue with the deletion. This holds when
|
|
|
|
* recursing from a whole object that includes the nominal
|
|
|
|
* other end as a component, too.
|
2002-07-16 07:53:34 +02:00
|
|
|
*/
|
|
|
|
if (callingObject->classId == otherObject.classId &&
|
|
|
|
callingObject->objectId == otherObject.objectId &&
|
2005-10-15 04:49:52 +02:00
|
|
|
(callingObject->objectSubId == otherObject.objectSubId ||
|
|
|
|
callingObject->objectSubId == 0))
|
2002-07-16 07:53:34 +02:00
|
|
|
break;
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-07-16 07:53:34 +02:00
|
|
|
/*
|
|
|
|
* 3. When recursing from anyplace else, transform this
|
2005-10-15 04:49:52 +02:00
|
|
|
* deletion request into a delete of the other object. (This
|
|
|
|
* will be an error condition iff RESTRICT mode.) In this case
|
|
|
|
* we finish deleting my dependencies except for the INTERNAL
|
|
|
|
* link, which will be needed to cause the owning object to
|
|
|
|
* recurse back to me.
|
2002-07-16 07:53:34 +02:00
|
|
|
*/
|
|
|
|
if (amOwned) /* shouldn't happen */
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "multiple INTERNAL dependencies for %s",
|
2002-07-16 07:53:34 +02:00
|
|
|
objDescription);
|
|
|
|
owningObject = otherObject;
|
|
|
|
amOwned = true;
|
|
|
|
/* "continue" bypasses the simple_heap_delete call below */
|
|
|
|
continue;
|
2002-07-12 20:43:19 +02:00
|
|
|
case DEPENDENCY_PIN:
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Should not happen; PIN dependencies should have zeroes in
|
|
|
|
* the depender fields...
|
2002-07-12 20:43:19 +02:00
|
|
|
*/
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "incorrect use of PIN dependency with %s",
|
2002-07-12 20:43:19 +02:00
|
|
|
objDescription);
|
|
|
|
break;
|
|
|
|
default:
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "unrecognized dependency type '%c' for %s",
|
2002-07-12 20:43:19 +02:00
|
|
|
foundDep->deptype, objDescription);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-07-07 22:40:02 +02:00
|
|
|
/* delete the pg_depend tuple */
|
2002-07-12 20:43:19 +02:00
|
|
|
simple_heap_delete(depRel, &tup->t_self);
|
|
|
|
}
|
|
|
|
|
|
|
|
systable_endscan(scan);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* CommandCounterIncrement here to ensure that preceding changes are all
|
|
|
|
* visible; in particular, that the above deletions of pg_depend entries
|
|
|
|
* are visible. That prevents infinite recursion in case of a dependency
|
|
|
|
* loop (which is perfectly legal).
|
2002-07-12 20:43:19 +02:00
|
|
|
*/
|
|
|
|
CommandCounterIncrement();
|
|
|
|
|
2002-07-16 07:53:34 +02:00
|
|
|
/*
|
|
|
|
* If we found we are owned by another object, ask it to delete itself
|
2002-09-22 02:37:09 +02:00
|
|
|
* instead of proceeding. Complain if RESTRICT mode, unless the other
|
|
|
|
* object is in oktodelete.
|
2002-07-16 07:53:34 +02:00
|
|
|
*/
|
|
|
|
if (amOwned)
|
|
|
|
{
|
2002-09-22 02:37:09 +02:00
|
|
|
if (object_address_present(&owningObject, oktodelete))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(DEBUG2,
|
|
|
|
(errmsg("drop auto-cascades to %s",
|
|
|
|
getObjectDescription(&owningObject))));
|
2002-09-22 02:37:09 +02:00
|
|
|
else if (behavior == DROP_RESTRICT)
|
2002-07-16 07:53:34 +02:00
|
|
|
{
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(msglevel,
|
|
|
|
(errmsg("%s depends on %s",
|
|
|
|
getObjectDescription(&owningObject),
|
|
|
|
objDescription)));
|
2002-07-16 07:53:34 +02:00
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
else
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(msglevel,
|
|
|
|
(errmsg("drop cascades to %s",
|
|
|
|
getObjectDescription(&owningObject))));
|
2002-07-16 07:53:34 +02:00
|
|
|
|
2003-03-06 23:54:49 +01:00
|
|
|
if (!recursiveDeletion(&owningObject, behavior, msglevel,
|
2006-08-20 23:56:16 +02:00
|
|
|
object, oktodelete, depRel, alreadyDeleted))
|
2002-07-16 07:53:34 +02:00
|
|
|
ok = false;
|
|
|
|
|
|
|
|
pfree(objDescription);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Step 2: scan pg_depend records that link to this object, showing the
|
|
|
|
* things that depend on it. Recursively delete those things. Note it's
|
|
|
|
* important to delete the dependent objects before the referenced one,
|
|
|
|
* since the deletion routines might do things like try to update the
|
|
|
|
* pg_class record when deleting a check constraint.
|
2002-07-12 20:43:19 +02:00
|
|
|
*/
|
2003-02-07 02:33:06 +01:00
|
|
|
if (!deleteDependentObjects(object, objDescription,
|
2003-03-06 23:54:49 +01:00
|
|
|
behavior, msglevel,
|
|
|
|
oktodelete, depRel))
|
2003-02-07 02:33:06 +01:00
|
|
|
ok = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We do not need CommandCounterIncrement here, since if step 2 did
|
|
|
|
* anything then each recursive call will have ended with one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Step 3: delete the object itself, and save it to the list of deleted
|
|
|
|
* objects if appropiate.
|
2003-02-07 02:33:06 +01:00
|
|
|
*/
|
|
|
|
doDeletion(object);
|
2006-08-20 23:56:16 +02:00
|
|
|
if (alreadyDeleted != NULL)
|
|
|
|
{
|
|
|
|
if (!object_address_present(object, alreadyDeleted))
|
|
|
|
add_exact_object_address(object, alreadyDeleted);
|
|
|
|
}
|
2003-02-07 02:33:06 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Delete any comments associated with this object. (This is a convenient
|
|
|
|
* place to do it instead of having every object type know to do it.)
|
2003-02-07 02:33:06 +01:00
|
|
|
*/
|
|
|
|
DeleteComments(object->objectId, object->classId, object->objectSubId);
|
|
|
|
|
2005-07-07 22:40:02 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Delete shared dependency references related to this object. Sub-objects
|
|
|
|
* (columns) don't have dependencies on global objects, so skip them.
|
2005-07-07 22:40:02 +02:00
|
|
|
*/
|
|
|
|
if (object->objectSubId == 0)
|
|
|
|
deleteSharedDependencyRecordsFor(object->classId, object->objectId);
|
|
|
|
|
2003-02-07 02:33:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* CommandCounterIncrement here to ensure that preceding changes are all
|
|
|
|
* visible.
|
2003-02-07 02:33:06 +01:00
|
|
|
*/
|
|
|
|
CommandCounterIncrement();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And we're done!
|
|
|
|
*/
|
|
|
|
pfree(objDescription);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* deleteDependentObjects - find and delete objects that depend on 'object'
|
|
|
|
*
|
|
|
|
* Scan pg_depend records that link to the given object, showing
|
|
|
|
* the things that depend on it. Recursively delete those things. (We
|
|
|
|
* don't delete the pg_depend records here, as the recursive call will
|
|
|
|
* do that.) Note it's important to delete the dependent objects
|
|
|
|
* before the referenced one, since the deletion routines might do
|
|
|
|
* things like try to update the pg_class record when deleting a check
|
|
|
|
* constraint.
|
|
|
|
*
|
|
|
|
* When dropping a whole object (subId = 0), find pg_depend records for
|
|
|
|
* its sub-objects too.
|
|
|
|
*
|
|
|
|
* object: the object to find dependencies on
|
|
|
|
* objDescription: description of object (only used for error messages)
|
|
|
|
* behavior: desired drop behavior
|
|
|
|
* oktodelete: stuff that's AUTO-deletable
|
|
|
|
* depRel: already opened pg_depend relation
|
|
|
|
*
|
|
|
|
* Returns TRUE if all is well, false if any problem found.
|
|
|
|
*
|
|
|
|
* NOTE: because we are using SnapshotNow, if a recursive call deletes
|
|
|
|
* any pg_depend tuples that our scan hasn't yet visited, we will not
|
|
|
|
* see them as good when we do visit them. This is essential for
|
|
|
|
* correct behavior if there are multiple dependency paths between two
|
|
|
|
* objects --- else we might try to delete an already-deleted object.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
deleteDependentObjects(const ObjectAddress *object,
|
|
|
|
const char *objDescription,
|
|
|
|
DropBehavior behavior,
|
2003-03-06 23:54:49 +01:00
|
|
|
int msglevel,
|
2003-02-07 02:33:06 +01:00
|
|
|
ObjectAddresses *oktodelete,
|
|
|
|
Relation depRel)
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
ScanKeyData key[3];
|
|
|
|
int nkeys;
|
|
|
|
SysScanDesc scan;
|
|
|
|
HeapTuple tup;
|
|
|
|
ObjectAddress otherObject;
|
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key[0],
|
|
|
|
Anum_pg_depend_refclassid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->classId));
|
|
|
|
ScanKeyInit(&key[1],
|
|
|
|
Anum_pg_depend_refobjid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->objectId));
|
2002-07-12 20:43:19 +02:00
|
|
|
if (object->objectSubId != 0)
|
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key[2],
|
|
|
|
Anum_pg_depend_refobjsubid,
|
|
|
|
BTEqualStrategyNumber, F_INT4EQ,
|
|
|
|
Int32GetDatum(object->objectSubId));
|
2002-07-12 20:43:19 +02:00
|
|
|
nkeys = 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nkeys = 2;
|
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
scan = systable_beginscan(depRel, DependReferenceIndexId, true,
|
2002-07-12 20:43:19 +02:00
|
|
|
SnapshotNow, nkeys, key);
|
|
|
|
|
|
|
|
while (HeapTupleIsValid(tup = systable_getnext(scan)))
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
|
|
|
otherObject.classId = foundDep->classid;
|
|
|
|
otherObject.objectId = foundDep->objid;
|
|
|
|
otherObject.objectSubId = foundDep->objsubid;
|
|
|
|
|
|
|
|
switch (foundDep->deptype)
|
|
|
|
{
|
|
|
|
case DEPENDENCY_NORMAL:
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-09-22 02:37:09 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Perhaps there was another dependency path that would have
|
|
|
|
* allowed silent deletion of the otherObject, had we only
|
|
|
|
* taken that path first. In that case, act like this link is
|
|
|
|
* AUTO, too.
|
2002-09-22 02:37:09 +02:00
|
|
|
*/
|
|
|
|
if (object_address_present(&otherObject, oktodelete))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(DEBUG2,
|
|
|
|
(errmsg("drop auto-cascades to %s",
|
|
|
|
getObjectDescription(&otherObject))));
|
2002-09-22 02:37:09 +02:00
|
|
|
else if (behavior == DROP_RESTRICT)
|
2002-07-12 20:43:19 +02:00
|
|
|
{
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(msglevel,
|
|
|
|
(errmsg("%s depends on %s",
|
|
|
|
getObjectDescription(&otherObject),
|
|
|
|
objDescription)));
|
2002-09-22 02:37:09 +02:00
|
|
|
ok = false;
|
2002-07-12 20:43:19 +02:00
|
|
|
}
|
|
|
|
else
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(msglevel,
|
|
|
|
(errmsg("drop cascades to %s",
|
|
|
|
getObjectDescription(&otherObject))));
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2003-03-06 23:54:49 +01:00
|
|
|
if (!recursiveDeletion(&otherObject, behavior, msglevel,
|
2006-08-20 23:56:16 +02:00
|
|
|
object, oktodelete, depRel, NULL))
|
2002-09-22 02:37:09 +02:00
|
|
|
ok = false;
|
2002-07-12 20:43:19 +02:00
|
|
|
break;
|
|
|
|
case DEPENDENCY_AUTO:
|
|
|
|
case DEPENDENCY_INTERNAL:
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
|
|
|
* We propagate the DROP without complaint even in the
|
|
|
|
* RESTRICT case. (However, normal dependencies on the
|
|
|
|
* component object could still cause failure.)
|
|
|
|
*/
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(DEBUG2,
|
|
|
|
(errmsg("drop auto-cascades to %s",
|
|
|
|
getObjectDescription(&otherObject))));
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2003-03-06 23:54:49 +01:00
|
|
|
if (!recursiveDeletion(&otherObject, behavior, msglevel,
|
2006-08-20 23:56:16 +02:00
|
|
|
object, oktodelete, depRel, NULL))
|
2002-07-12 20:43:19 +02:00
|
|
|
ok = false;
|
|
|
|
break;
|
|
|
|
case DEPENDENCY_PIN:
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
2003-07-21 03:59:11 +02:00
|
|
|
* For a PIN dependency we just ereport immediately; there
|
2002-07-12 20:43:19 +02:00
|
|
|
* won't be any others to report.
|
|
|
|
*/
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
|
|
|
|
errmsg("cannot drop %s because it is required by the database system",
|
|
|
|
objDescription)));
|
2002-07-12 20:43:19 +02:00
|
|
|
break;
|
|
|
|
default:
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "unrecognized dependency type '%c' for %s",
|
2002-07-12 20:43:19 +02:00
|
|
|
foundDep->deptype, objDescription);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
systable_endscan(scan);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* doDeletion: actually delete a single object
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
doDeletion(const ObjectAddress *object)
|
|
|
|
{
|
|
|
|
switch (getObjectClass(object))
|
|
|
|
{
|
|
|
|
case OCLASS_CLASS:
|
|
|
|
{
|
2002-09-20 01:40:56 +02:00
|
|
|
char relKind = get_rel_relkind(object->objectId);
|
2002-09-04 22:31:48 +02:00
|
|
|
|
|
|
|
if (relKind == RELKIND_INDEX)
|
|
|
|
{
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
index_drop(object->objectId);
|
|
|
|
}
|
2002-07-12 20:43:19 +02:00
|
|
|
else
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
if (object->objectSubId != 0)
|
|
|
|
RemoveAttributeById(object->objectId,
|
|
|
|
object->objectSubId);
|
|
|
|
else
|
|
|
|
heap_drop_with_catalog(object->objectId);
|
|
|
|
}
|
|
|
|
break;
|
2002-07-12 20:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case OCLASS_PROC:
|
|
|
|
RemoveFunctionById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_TYPE:
|
|
|
|
RemoveTypeById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
2002-07-30 00:14:11 +02:00
|
|
|
case OCLASS_CAST:
|
|
|
|
DropCastById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
case OCLASS_CONSTRAINT:
|
|
|
|
RemoveConstraintById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
2002-07-25 12:07:13 +02:00
|
|
|
case OCLASS_CONVERSION:
|
|
|
|
RemoveConversionById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
2002-07-15 18:33:32 +02:00
|
|
|
case OCLASS_DEFAULT:
|
|
|
|
RemoveAttrDefaultById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
case OCLASS_LANGUAGE:
|
|
|
|
DropProceduralLanguageById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_OPERATOR:
|
|
|
|
RemoveOperatorById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
2002-07-30 00:14:11 +02:00
|
|
|
case OCLASS_OPCLASS:
|
|
|
|
RemoveOpClassById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
2006-12-23 01:43:13 +01:00
|
|
|
case OCLASS_OPFAMILY:
|
|
|
|
RemoveOpFamilyById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_AMOP:
|
|
|
|
RemoveAmOpEntryById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_AMPROC:
|
|
|
|
RemoveAmProcEntryById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
case OCLASS_REWRITE:
|
|
|
|
RemoveRewriteRuleById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_TRIGGER:
|
|
|
|
RemoveTriggerById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
2002-07-18 18:47:26 +02:00
|
|
|
case OCLASS_SCHEMA:
|
|
|
|
RemoveSchemaById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
case OCLASS_TSPARSER:
|
|
|
|
RemoveTSParserById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_TSDICT:
|
|
|
|
RemoveTSDictionaryById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_TSTEMPLATE:
|
|
|
|
RemoveTSTemplateById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_TSCONFIG:
|
|
|
|
RemoveTSConfigurationById(object->objectId);
|
|
|
|
break;
|
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
/* OCLASS_ROLE, OCLASS_DATABASE, OCLASS_TBLSPACE not handled */
|
2006-12-23 01:43:13 +01:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
default:
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "unrecognized object class: %u",
|
2002-07-12 20:43:19 +02:00
|
|
|
object->classId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-16 07:53:34 +02:00
|
|
|
/*
|
|
|
|
* recordDependencyOnExpr - find expression dependencies
|
|
|
|
*
|
|
|
|
* This is used to find the dependencies of rules, constraint expressions,
|
|
|
|
* etc.
|
|
|
|
*
|
|
|
|
* Given an expression or query in node-tree form, find all the objects
|
|
|
|
* it refers to (tables, columns, operators, functions, etc). Record
|
|
|
|
* a dependency of the specified type from the given depender object
|
|
|
|
* to each object mentioned in the expression.
|
|
|
|
*
|
|
|
|
* rtable is the rangetable to be used to interpret Vars with varlevelsup=0.
|
|
|
|
* It can be NIL if no such variables are expected.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
recordDependencyOnExpr(const ObjectAddress *depender,
|
|
|
|
Node *expr, List *rtable,
|
|
|
|
DependencyType behavior)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
find_expr_references_context context;
|
2002-07-16 07:53:34 +02:00
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
context.addrs = new_object_addresses();
|
2002-07-16 07:53:34 +02:00
|
|
|
|
|
|
|
/* Set up interpretation for Vars at varlevelsup = 0 */
|
2004-05-26 06:41:50 +02:00
|
|
|
context.rtables = list_make1(rtable);
|
2002-07-16 07:53:34 +02:00
|
|
|
|
|
|
|
/* Scan the expression tree for referenceable objects */
|
|
|
|
find_expr_references_walker(expr, &context);
|
|
|
|
|
|
|
|
/* Remove any duplicates */
|
2006-08-20 23:56:16 +02:00
|
|
|
eliminate_duplicate_dependencies(context.addrs);
|
2002-07-16 07:53:34 +02:00
|
|
|
|
|
|
|
/* And record 'em */
|
2003-05-28 18:04:02 +02:00
|
|
|
recordMultipleDependencies(depender,
|
2006-08-20 23:56:16 +02:00
|
|
|
context.addrs->refs, context.addrs->numrefs,
|
2003-05-28 18:04:02 +02:00
|
|
|
behavior);
|
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
free_object_addresses(context.addrs);
|
2003-05-28 18:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* recordDependencyOnSingleRelExpr - find expression dependencies
|
|
|
|
*
|
|
|
|
* As above, but only one relation is expected to be referenced (with
|
2003-08-04 02:43:34 +02:00
|
|
|
* varno = 1 and varlevelsup = 0). Pass the relation OID instead of a
|
2003-05-28 18:04:02 +02:00
|
|
|
* range table. An additional frammish is that dependencies on that
|
|
|
|
* relation (or its component columns) will be marked with 'self_behavior',
|
|
|
|
* whereas 'behavior' is used for everything else.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
|
|
|
|
Node *expr, Oid relId,
|
|
|
|
DependencyType behavior,
|
|
|
|
DependencyType self_behavior)
|
|
|
|
{
|
|
|
|
find_expr_references_context context;
|
|
|
|
RangeTblEntry rte;
|
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
context.addrs = new_object_addresses();
|
2003-05-28 18:04:02 +02:00
|
|
|
|
|
|
|
/* We gin up a rather bogus rangetable list to handle Vars */
|
|
|
|
MemSet(&rte, 0, sizeof(rte));
|
|
|
|
rte.type = T_RangeTblEntry;
|
|
|
|
rte.rtekind = RTE_RELATION;
|
|
|
|
rte.relid = relId;
|
|
|
|
|
2004-05-26 06:41:50 +02:00
|
|
|
context.rtables = list_make1(list_make1(&rte));
|
2003-05-28 18:04:02 +02:00
|
|
|
|
|
|
|
/* Scan the expression tree for referenceable objects */
|
|
|
|
find_expr_references_walker(expr, &context);
|
|
|
|
|
|
|
|
/* Remove any duplicates */
|
2006-08-20 23:56:16 +02:00
|
|
|
eliminate_duplicate_dependencies(context.addrs);
|
2003-05-28 18:04:02 +02:00
|
|
|
|
|
|
|
/* Separate self-dependencies if necessary */
|
2006-08-20 23:56:16 +02:00
|
|
|
if (behavior != self_behavior && context.addrs->numrefs > 0)
|
2003-05-28 18:04:02 +02:00
|
|
|
{
|
2006-08-20 23:56:16 +02:00
|
|
|
ObjectAddresses *self_addrs;
|
2003-05-28 18:04:02 +02:00
|
|
|
ObjectAddress *outobj;
|
|
|
|
int oldref,
|
|
|
|
outrefs;
|
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
self_addrs = new_object_addresses();
|
2003-05-28 18:04:02 +02:00
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
outobj = context.addrs->refs;
|
2003-05-28 18:04:02 +02:00
|
|
|
outrefs = 0;
|
2006-08-20 23:56:16 +02:00
|
|
|
for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
|
2003-05-28 18:04:02 +02:00
|
|
|
{
|
2006-08-20 23:56:16 +02:00
|
|
|
ObjectAddress *thisobj = context.addrs->refs + oldref;
|
2003-05-28 18:04:02 +02:00
|
|
|
|
2005-04-14 03:38:22 +02:00
|
|
|
if (thisobj->classId == RelationRelationId &&
|
2003-05-28 18:04:02 +02:00
|
|
|
thisobj->objectId == relId)
|
|
|
|
{
|
|
|
|
/* Move this ref into self_addrs */
|
|
|
|
add_object_address(OCLASS_CLASS, relId, thisobj->objectSubId,
|
2006-08-20 23:56:16 +02:00
|
|
|
self_addrs);
|
2003-05-28 18:04:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Keep it in context.addrs */
|
|
|
|
outobj->classId = thisobj->classId;
|
|
|
|
outobj->objectId = thisobj->objectId;
|
|
|
|
outobj->objectSubId = thisobj->objectSubId;
|
|
|
|
outobj++;
|
|
|
|
outrefs++;
|
|
|
|
}
|
|
|
|
}
|
2006-08-20 23:56:16 +02:00
|
|
|
context.addrs->numrefs = outrefs;
|
2003-05-28 18:04:02 +02:00
|
|
|
|
|
|
|
/* Record the self-dependencies */
|
|
|
|
recordMultipleDependencies(depender,
|
2006-08-20 23:56:16 +02:00
|
|
|
self_addrs->refs, self_addrs->numrefs,
|
2003-05-28 18:04:02 +02:00
|
|
|
self_behavior);
|
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
free_object_addresses(self_addrs);
|
2003-05-28 18:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Record the external dependencies */
|
2002-07-16 07:53:34 +02:00
|
|
|
recordMultipleDependencies(depender,
|
2006-08-20 23:56:16 +02:00
|
|
|
context.addrs->refs, context.addrs->numrefs,
|
2002-07-16 07:53:34 +02:00
|
|
|
behavior);
|
|
|
|
|
2006-08-20 23:56:16 +02:00
|
|
|
free_object_addresses(context.addrs);
|
2002-07-16 07:53:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recursively search an expression tree for object references.
|
2002-09-11 16:48:55 +02:00
|
|
|
*
|
|
|
|
* Note: we avoid creating references to columns of tables that participate
|
|
|
|
* in an SQL JOIN construct, but are not actually used anywhere in the query.
|
|
|
|
* To do so, we do not scan the joinaliasvars list of a join RTE while
|
|
|
|
* scanning the query rangetable, but instead scan each individual entry
|
|
|
|
* of the alias list when we find a reference to it.
|
2006-03-16 01:31:55 +01:00
|
|
|
*
|
|
|
|
* Note: in many cases we do not need to create dependencies on the datatypes
|
|
|
|
* involved in an expression, because we'll have an indirect dependency via
|
|
|
|
* some other object. For instance Var nodes depend on a column which depends
|
|
|
|
* on the datatype, and OpExpr nodes depend on the operator which depends on
|
|
|
|
* the datatype. However we do need a type dependency if there is no such
|
|
|
|
* indirect dependency, as for example in Const and CoerceToDomain nodes.
|
2002-07-16 07:53:34 +02:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
find_expr_references_walker(Node *node,
|
|
|
|
find_expr_references_context *context)
|
|
|
|
{
|
|
|
|
if (node == NULL)
|
|
|
|
return false;
|
|
|
|
if (IsA(node, Var))
|
|
|
|
{
|
|
|
|
Var *var = (Var *) node;
|
2004-05-26 06:41:50 +02:00
|
|
|
List *rtable;
|
2002-07-16 07:53:34 +02:00
|
|
|
RangeTblEntry *rte;
|
|
|
|
|
|
|
|
/* Find matching rtable entry, or complain if not found */
|
2004-05-26 06:41:50 +02:00
|
|
|
if (var->varlevelsup >= list_length(context->rtables))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "invalid varlevelsup %d", var->varlevelsup);
|
2004-05-26 06:41:50 +02:00
|
|
|
rtable = (List *) list_nth(context->rtables, var->varlevelsup);
|
|
|
|
if (var->varno <= 0 || var->varno > list_length(rtable))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "invalid varno %d", var->varno);
|
2002-07-16 07:53:34 +02:00
|
|
|
rte = rt_fetch(var->varno, rtable);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-19 22:57:41 +02:00
|
|
|
/*
|
|
|
|
* A whole-row Var references no specific columns, so adds no new
|
|
|
|
* dependency.
|
|
|
|
*/
|
|
|
|
if (var->varattno == InvalidAttrNumber)
|
|
|
|
return false;
|
2002-07-16 07:53:34 +02:00
|
|
|
if (rte->rtekind == RTE_RELATION)
|
2002-09-11 16:48:55 +02:00
|
|
|
{
|
|
|
|
/* If it's a plain relation, reference this column */
|
2002-07-16 07:53:34 +02:00
|
|
|
add_object_address(OCLASS_CLASS, rte->relid, var->varattno,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2002-09-11 16:48:55 +02:00
|
|
|
}
|
|
|
|
else if (rte->rtekind == RTE_JOIN)
|
|
|
|
{
|
|
|
|
/* Scan join output column to add references to join inputs */
|
2003-08-04 02:43:34 +02:00
|
|
|
List *save_rtables;
|
2002-12-04 21:00:37 +01:00
|
|
|
|
|
|
|
/* We must make the context appropriate for join's level */
|
|
|
|
save_rtables = context->rtables;
|
2004-05-26 06:41:50 +02:00
|
|
|
context->rtables = list_copy_tail(context->rtables,
|
|
|
|
var->varlevelsup);
|
2002-09-11 16:48:55 +02:00
|
|
|
if (var->varattno <= 0 ||
|
2004-05-26 06:41:50 +02:00
|
|
|
var->varattno > list_length(rte->joinaliasvars))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "invalid varattno %d", var->varattno);
|
2004-05-26 06:41:50 +02:00
|
|
|
find_expr_references_walker((Node *) list_nth(rte->joinaliasvars,
|
2005-10-15 04:49:52 +02:00
|
|
|
var->varattno - 1),
|
2002-09-11 16:48:55 +02:00
|
|
|
context);
|
2004-05-26 06:41:50 +02:00
|
|
|
list_free(context->rtables);
|
2002-12-04 21:00:37 +01:00
|
|
|
context->rtables = save_rtables;
|
2002-09-11 16:48:55 +02:00
|
|
|
}
|
2002-07-16 07:53:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
2005-10-03 01:50:16 +02:00
|
|
|
if (IsA(node, Const))
|
|
|
|
{
|
|
|
|
Const *con = (Const *) node;
|
|
|
|
Oid objoid;
|
|
|
|
|
2006-03-16 01:31:55 +01:00
|
|
|
/* A constant must depend on the constant's datatype */
|
|
|
|
add_object_address(OCLASS_TYPE, con->consttype, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2006-03-16 01:31:55 +01:00
|
|
|
|
2005-10-03 01:50:16 +02:00
|
|
|
/*
|
|
|
|
* If it's a regclass or similar literal referring to an existing
|
2005-10-15 04:49:52 +02:00
|
|
|
* object, add a reference to that object. (Currently, only the
|
2007-08-21 03:11:32 +02:00
|
|
|
* regclass and regconfig cases have any likely use, but we may as
|
|
|
|
* well handle all the OID-alias datatypes consistently.)
|
2005-10-03 01:50:16 +02:00
|
|
|
*/
|
|
|
|
if (!con->constisnull)
|
|
|
|
{
|
|
|
|
switch (con->consttype)
|
|
|
|
{
|
|
|
|
case REGPROCOID:
|
|
|
|
case REGPROCEDUREOID:
|
|
|
|
objoid = DatumGetObjectId(con->constvalue);
|
|
|
|
if (SearchSysCacheExists(PROCOID,
|
|
|
|
ObjectIdGetDatum(objoid),
|
|
|
|
0, 0, 0))
|
|
|
|
add_object_address(OCLASS_PROC, objoid, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2005-10-03 01:50:16 +02:00
|
|
|
break;
|
|
|
|
case REGOPEROID:
|
|
|
|
case REGOPERATOROID:
|
|
|
|
objoid = DatumGetObjectId(con->constvalue);
|
|
|
|
if (SearchSysCacheExists(OPEROID,
|
|
|
|
ObjectIdGetDatum(objoid),
|
|
|
|
0, 0, 0))
|
|
|
|
add_object_address(OCLASS_OPERATOR, objoid, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2005-10-03 01:50:16 +02:00
|
|
|
break;
|
|
|
|
case REGCLASSOID:
|
|
|
|
objoid = DatumGetObjectId(con->constvalue);
|
|
|
|
if (SearchSysCacheExists(RELOID,
|
|
|
|
ObjectIdGetDatum(objoid),
|
|
|
|
0, 0, 0))
|
|
|
|
add_object_address(OCLASS_CLASS, objoid, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2005-10-03 01:50:16 +02:00
|
|
|
break;
|
|
|
|
case REGTYPEOID:
|
|
|
|
objoid = DatumGetObjectId(con->constvalue);
|
|
|
|
if (SearchSysCacheExists(TYPEOID,
|
|
|
|
ObjectIdGetDatum(objoid),
|
|
|
|
0, 0, 0))
|
|
|
|
add_object_address(OCLASS_TYPE, objoid, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2005-10-03 01:50:16 +02:00
|
|
|
break;
|
2007-08-21 03:11:32 +02:00
|
|
|
case REGCONFIGOID:
|
|
|
|
objoid = DatumGetObjectId(con->constvalue);
|
|
|
|
if (SearchSysCacheExists(TSCONFIGOID,
|
|
|
|
ObjectIdGetDatum(objoid),
|
|
|
|
0, 0, 0))
|
|
|
|
add_object_address(OCLASS_TSCONFIG, objoid, 0,
|
|
|
|
context->addrs);
|
|
|
|
break;
|
|
|
|
case REGDICTIONARYOID:
|
|
|
|
objoid = DatumGetObjectId(con->constvalue);
|
|
|
|
if (SearchSysCacheExists(TSDICTOID,
|
|
|
|
ObjectIdGetDatum(objoid),
|
|
|
|
0, 0, 0))
|
|
|
|
add_object_address(OCLASS_TSDICT, objoid, 0,
|
|
|
|
context->addrs);
|
|
|
|
break;
|
2005-10-03 01:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-16 01:31:55 +01:00
|
|
|
if (IsA(node, Param))
|
|
|
|
{
|
|
|
|
Param *param = (Param *) node;
|
|
|
|
|
|
|
|
/* A parameter must depend on the parameter's datatype */
|
|
|
|
add_object_address(OCLASS_TYPE, param->paramtype, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2006-03-16 01:31:55 +01:00
|
|
|
}
|
2002-12-12 16:49:42 +01:00
|
|
|
if (IsA(node, FuncExpr))
|
2002-07-16 07:53:34 +02:00
|
|
|
{
|
2002-12-12 16:49:42 +01:00
|
|
|
FuncExpr *funcexpr = (FuncExpr *) node;
|
2002-07-16 07:53:34 +02:00
|
|
|
|
2002-12-12 16:49:42 +01:00
|
|
|
add_object_address(OCLASS_PROC, funcexpr->funcid, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2002-12-12 16:49:42 +01:00
|
|
|
/* fall through to examine arguments */
|
|
|
|
}
|
|
|
|
if (IsA(node, OpExpr))
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
OpExpr *opexpr = (OpExpr *) node;
|
2002-07-16 07:53:34 +02:00
|
|
|
|
2002-12-12 16:49:42 +01:00
|
|
|
add_object_address(OCLASS_OPERATOR, opexpr->opno, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2002-12-12 16:49:42 +01:00
|
|
|
/* fall through to examine arguments */
|
|
|
|
}
|
|
|
|
if (IsA(node, DistinctExpr))
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
DistinctExpr *distinctexpr = (DistinctExpr *) node;
|
2002-07-16 07:53:34 +02:00
|
|
|
|
2002-12-12 16:49:42 +01:00
|
|
|
add_object_address(OCLASS_OPERATOR, distinctexpr->opno, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2002-07-16 07:53:34 +02:00
|
|
|
/* fall through to examine arguments */
|
|
|
|
}
|
2003-06-29 02:33:44 +02:00
|
|
|
if (IsA(node, ScalarArrayOpExpr))
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
|
2003-06-29 02:33:44 +02:00
|
|
|
|
|
|
|
add_object_address(OCLASS_OPERATOR, opexpr->opno, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2003-06-29 02:33:44 +02:00
|
|
|
/* fall through to examine arguments */
|
|
|
|
}
|
2003-02-16 03:30:39 +01:00
|
|
|
if (IsA(node, NullIfExpr))
|
|
|
|
{
|
|
|
|
NullIfExpr *nullifexpr = (NullIfExpr *) node;
|
|
|
|
|
|
|
|
add_object_address(OCLASS_OPERATOR, nullifexpr->opno, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2003-02-16 03:30:39 +01:00
|
|
|
/* fall through to examine arguments */
|
|
|
|
}
|
2002-07-16 07:53:34 +02:00
|
|
|
if (IsA(node, Aggref))
|
|
|
|
{
|
|
|
|
Aggref *aggref = (Aggref *) node;
|
|
|
|
|
|
|
|
add_object_address(OCLASS_PROC, aggref->aggfnoid, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2002-07-16 07:53:34 +02:00
|
|
|
/* fall through to examine arguments */
|
|
|
|
}
|
2005-12-28 02:30:02 +01:00
|
|
|
if (is_subplan(node))
|
2003-01-10 22:08:15 +01:00
|
|
|
{
|
2005-12-28 02:30:02 +01:00
|
|
|
/* Extra work needed here if we ever need this case */
|
|
|
|
elog(ERROR, "already-planned subqueries not supported");
|
|
|
|
}
|
2006-03-16 01:31:55 +01:00
|
|
|
if (IsA(node, RelabelType))
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
RelabelType *relab = (RelabelType *) node;
|
2006-03-16 01:31:55 +01:00
|
|
|
|
|
|
|
/* since there is no function dependency, need to depend on type */
|
|
|
|
add_object_address(OCLASS_TYPE, relab->resulttype, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2006-03-16 01:31:55 +01:00
|
|
|
}
|
2007-06-05 23:31:09 +02:00
|
|
|
if (IsA(node, CoerceViaIO))
|
|
|
|
{
|
|
|
|
CoerceViaIO *iocoerce = (CoerceViaIO *) node;
|
|
|
|
|
|
|
|
/* since there is no exposed function, need to depend on type */
|
|
|
|
add_object_address(OCLASS_TYPE, iocoerce->resulttype, 0,
|
|
|
|
context->addrs);
|
|
|
|
}
|
2007-03-28 01:21:12 +02:00
|
|
|
if (IsA(node, ArrayCoerceExpr))
|
|
|
|
{
|
|
|
|
ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
|
|
|
|
|
|
|
|
if (OidIsValid(acoerce->elemfuncid))
|
|
|
|
add_object_address(OCLASS_PROC, acoerce->elemfuncid, 0,
|
|
|
|
context->addrs);
|
|
|
|
add_object_address(OCLASS_TYPE, acoerce->resulttype, 0,
|
|
|
|
context->addrs);
|
|
|
|
/* fall through to examine arguments */
|
|
|
|
}
|
2006-03-16 01:31:55 +01:00
|
|
|
if (IsA(node, ConvertRowtypeExpr))
|
|
|
|
{
|
|
|
|
ConvertRowtypeExpr *cvt = (ConvertRowtypeExpr *) node;
|
|
|
|
|
|
|
|
/* since there is no function dependency, need to depend on type */
|
|
|
|
add_object_address(OCLASS_TYPE, cvt->resulttype, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2006-03-16 01:31:55 +01:00
|
|
|
}
|
|
|
|
if (IsA(node, RowExpr))
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
RowExpr *rowexpr = (RowExpr *) node;
|
2006-03-16 01:31:55 +01:00
|
|
|
|
|
|
|
add_object_address(OCLASS_TYPE, rowexpr->row_typeid, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2006-03-16 01:31:55 +01:00
|
|
|
}
|
2005-12-28 02:30:02 +01:00
|
|
|
if (IsA(node, RowCompareExpr))
|
|
|
|
{
|
|
|
|
RowCompareExpr *rcexpr = (RowCompareExpr *) node;
|
|
|
|
ListCell *l;
|
2003-01-10 22:08:15 +01:00
|
|
|
|
2005-12-28 02:30:02 +01:00
|
|
|
foreach(l, rcexpr->opnos)
|
2003-01-10 22:08:15 +01:00
|
|
|
{
|
2005-12-28 02:30:02 +01:00
|
|
|
add_object_address(OCLASS_OPERATOR, lfirst_oid(l), 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2005-12-28 02:30:02 +01:00
|
|
|
}
|
2006-12-23 01:43:13 +01:00
|
|
|
foreach(l, rcexpr->opfamilies)
|
2005-12-28 02:30:02 +01:00
|
|
|
{
|
2006-12-23 01:43:13 +01:00
|
|
|
add_object_address(OCLASS_OPFAMILY, lfirst_oid(l), 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2003-01-10 22:08:15 +01:00
|
|
|
}
|
|
|
|
/* fall through to examine arguments */
|
|
|
|
}
|
2006-03-16 01:31:55 +01:00
|
|
|
if (IsA(node, CoerceToDomain))
|
|
|
|
{
|
|
|
|
CoerceToDomain *cd = (CoerceToDomain *) node;
|
|
|
|
|
|
|
|
add_object_address(OCLASS_TYPE, cd->resulttype, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2006-03-16 01:31:55 +01:00
|
|
|
}
|
2002-07-16 07:53:34 +02:00
|
|
|
if (IsA(node, Query))
|
|
|
|
{
|
|
|
|
/* Recurse into RTE subquery or not-yet-planned sublink subquery */
|
|
|
|
Query *query = (Query *) node;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *rtable;
|
2002-07-16 07:53:34 +02:00
|
|
|
bool result;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Add whole-relation refs for each plain relation mentioned in the
|
2006-03-16 01:31:55 +01:00
|
|
|
* subquery's rtable, as well as datatype refs for any datatypes used
|
|
|
|
* as a RECORD function's output. (Note: query_tree_walker takes care
|
|
|
|
* of recursing into RTE_FUNCTION and RTE_SUBQUERY RTEs, so no need to
|
|
|
|
* do that here. But keep it from looking at join alias lists.)
|
2002-07-16 07:53:34 +02:00
|
|
|
*/
|
|
|
|
foreach(rtable, query->rtable)
|
|
|
|
{
|
|
|
|
RangeTblEntry *rte = (RangeTblEntry *) lfirst(rtable);
|
2006-03-16 01:31:55 +01:00
|
|
|
ListCell *ct;
|
2002-07-16 07:53:34 +02:00
|
|
|
|
2006-03-16 01:31:55 +01:00
|
|
|
switch (rte->rtekind)
|
|
|
|
{
|
|
|
|
case RTE_RELATION:
|
|
|
|
add_object_address(OCLASS_CLASS, rte->relid, 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2006-03-16 01:31:55 +01:00
|
|
|
break;
|
|
|
|
case RTE_FUNCTION:
|
|
|
|
foreach(ct, rte->funccoltypes)
|
|
|
|
{
|
|
|
|
add_object_address(OCLASS_TYPE, lfirst_oid(ct), 0,
|
2006-08-20 23:56:16 +02:00
|
|
|
context->addrs);
|
2006-03-16 01:31:55 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2002-07-16 07:53:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Examine substructure of query */
|
|
|
|
context->rtables = lcons(query->rtable, context->rtables);
|
|
|
|
result = query_tree_walker(query,
|
|
|
|
find_expr_references_walker,
|
2002-09-11 16:48:55 +02:00
|
|
|
(void *) context,
|
|
|
|
QTW_IGNORE_JOINALIASES);
|
2004-05-26 06:41:50 +02:00
|
|
|
context->rtables = list_delete_first(context->rtables);
|
2002-07-16 07:53:34 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return expression_tree_walker(node, find_expr_references_walker,
|
|
|
|
(void *) context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given an array of dependency references, eliminate any duplicates.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
eliminate_duplicate_dependencies(ObjectAddresses *addrs)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ObjectAddress *priorobj;
|
2002-07-16 07:53:34 +02:00
|
|
|
int oldref,
|
|
|
|
newrefs;
|
|
|
|
|
|
|
|
if (addrs->numrefs <= 1)
|
|
|
|
return; /* nothing to do */
|
|
|
|
|
|
|
|
/* Sort the refs so that duplicates are adjacent */
|
|
|
|
qsort((void *) addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
|
|
|
|
object_address_comparator);
|
|
|
|
|
|
|
|
/* Remove dups */
|
|
|
|
priorobj = addrs->refs;
|
|
|
|
newrefs = 1;
|
|
|
|
for (oldref = 1; oldref < addrs->numrefs; oldref++)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ObjectAddress *thisobj = addrs->refs + oldref;
|
2002-07-16 07:53:34 +02:00
|
|
|
|
|
|
|
if (priorobj->classId == thisobj->classId &&
|
|
|
|
priorobj->objectId == thisobj->objectId)
|
|
|
|
{
|
|
|
|
if (priorobj->objectSubId == thisobj->objectSubId)
|
|
|
|
continue; /* identical, so drop thisobj */
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-07-16 07:53:34 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we have a whole-object reference and a reference to a part
|
|
|
|
* of the same object, we don't need the whole-object reference
|
|
|
|
* (for example, we don't need to reference both table foo and
|
|
|
|
* column foo.bar). The whole-object reference will always appear
|
|
|
|
* first in the sorted list.
|
2002-07-16 07:53:34 +02:00
|
|
|
*/
|
|
|
|
if (priorobj->objectSubId == 0)
|
|
|
|
{
|
|
|
|
/* replace whole ref with partial */
|
|
|
|
priorobj->objectSubId = thisobj->objectSubId;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Not identical, so add thisobj to output set */
|
|
|
|
priorobj++;
|
|
|
|
priorobj->classId = thisobj->classId;
|
|
|
|
priorobj->objectId = thisobj->objectId;
|
|
|
|
priorobj->objectSubId = thisobj->objectSubId;
|
|
|
|
newrefs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs->numrefs = newrefs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* qsort comparator for ObjectAddress items
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
object_address_comparator(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const ObjectAddress *obja = (const ObjectAddress *) a;
|
|
|
|
const ObjectAddress *objb = (const ObjectAddress *) b;
|
|
|
|
|
|
|
|
if (obja->classId < objb->classId)
|
|
|
|
return -1;
|
|
|
|
if (obja->classId > objb->classId)
|
|
|
|
return 1;
|
|
|
|
if (obja->objectId < objb->objectId)
|
|
|
|
return -1;
|
|
|
|
if (obja->objectId > objb->objectId)
|
|
|
|
return 1;
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-07-16 07:53:34 +02:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* We sort the subId as an unsigned int so that 0 will come first. See
|
|
|
|
* logic in eliminate_duplicate_dependencies.
|
2002-07-16 07:53:34 +02:00
|
|
|
*/
|
|
|
|
if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
|
|
|
|
return -1;
|
|
|
|
if ((unsigned int) obja->objectSubId > (unsigned int) objb->objectSubId)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines for handling an expansible array of ObjectAddress items.
|
|
|
|
*
|
2006-08-20 23:56:16 +02:00
|
|
|
* new_object_addresses: create a new ObjectAddresses array.
|
2002-07-16 07:53:34 +02:00
|
|
|
*/
|
2006-08-20 23:56:16 +02:00
|
|
|
ObjectAddresses *
|
|
|
|
new_object_addresses(void)
|
2002-07-16 07:53:34 +02:00
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
ObjectAddresses *addrs;
|
2006-08-20 23:56:16 +02:00
|
|
|
|
|
|
|
addrs = palloc(sizeof(ObjectAddresses));
|
|
|
|
|
2002-07-16 07:53:34 +02:00
|
|
|
addrs->numrefs = 0;
|
2006-08-20 23:56:16 +02:00
|
|
|
addrs->maxrefs = 32;
|
2002-07-16 07:53:34 +02:00
|
|
|
addrs->refs = (ObjectAddress *)
|
|
|
|
palloc(addrs->maxrefs * sizeof(ObjectAddress));
|
2006-08-20 23:56:16 +02:00
|
|
|
|
|
|
|
return addrs;
|
2002-07-16 07:53:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an entry to an ObjectAddresses array.
|
|
|
|
*
|
|
|
|
* It is convenient to specify the class by ObjectClass rather than directly
|
|
|
|
* by catalog OID.
|
|
|
|
*/
|
|
|
|
static void
|
2004-05-05 06:48:48 +02:00
|
|
|
add_object_address(ObjectClass oclass, Oid objectId, int32 subId,
|
2002-07-16 07:53:34 +02:00
|
|
|
ObjectAddresses *addrs)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ObjectAddress *item;
|
2002-07-16 07:53:34 +02:00
|
|
|
|
|
|
|
/* enlarge array if needed */
|
|
|
|
if (addrs->numrefs >= addrs->maxrefs)
|
|
|
|
{
|
|
|
|
addrs->maxrefs *= 2;
|
|
|
|
addrs->refs = (ObjectAddress *)
|
|
|
|
repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
|
|
|
|
}
|
|
|
|
/* record this item */
|
|
|
|
item = addrs->refs + addrs->numrefs;
|
|
|
|
item->classId = object_classes[oclass];
|
|
|
|
item->objectId = objectId;
|
|
|
|
item->objectSubId = subId;
|
|
|
|
addrs->numrefs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an entry to an ObjectAddresses array.
|
|
|
|
*
|
|
|
|
* As above, but specify entry exactly.
|
|
|
|
*/
|
2006-08-20 23:56:16 +02:00
|
|
|
void
|
2002-07-16 07:53:34 +02:00
|
|
|
add_exact_object_address(const ObjectAddress *object,
|
|
|
|
ObjectAddresses *addrs)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
ObjectAddress *item;
|
2002-07-16 07:53:34 +02:00
|
|
|
|
|
|
|
/* enlarge array if needed */
|
|
|
|
if (addrs->numrefs >= addrs->maxrefs)
|
|
|
|
{
|
|
|
|
addrs->maxrefs *= 2;
|
|
|
|
addrs->refs = (ObjectAddress *)
|
|
|
|
repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
|
|
|
|
}
|
|
|
|
/* record this item */
|
|
|
|
item = addrs->refs + addrs->numrefs;
|
|
|
|
*item = *object;
|
|
|
|
addrs->numrefs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-22 02:37:09 +02:00
|
|
|
* Test whether an object is present in an ObjectAddresses array.
|
|
|
|
*
|
|
|
|
* We return "true" if object is a subobject of something in the array, too.
|
2002-07-16 07:53:34 +02:00
|
|
|
*/
|
2006-08-20 23:56:16 +02:00
|
|
|
bool
|
2002-09-22 02:37:09 +02:00
|
|
|
object_address_present(const ObjectAddress *object,
|
|
|
|
ObjectAddresses *addrs)
|
2002-07-16 07:53:34 +02:00
|
|
|
{
|
2002-09-22 02:37:09 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = addrs->numrefs - 1; i >= 0; i--)
|
2002-07-16 07:53:34 +02:00
|
|
|
{
|
2002-09-22 02:37:09 +02:00
|
|
|
ObjectAddress *thisobj = addrs->refs + i;
|
2002-07-16 07:53:34 +02:00
|
|
|
|
2002-09-22 02:37:09 +02:00
|
|
|
if (object->classId == thisobj->classId &&
|
|
|
|
object->objectId == thisobj->objectId)
|
2002-07-16 07:53:34 +02:00
|
|
|
{
|
2002-09-22 02:37:09 +02:00
|
|
|
if (object->objectSubId == thisobj->objectSubId ||
|
|
|
|
thisobj->objectSubId == 0)
|
|
|
|
return true;
|
2002-07-16 07:53:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-22 02:37:09 +02:00
|
|
|
return false;
|
2002-07-16 07:53:34 +02:00
|
|
|
}
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
/*
|
|
|
|
* Record multiple dependencies from an ObjectAddresses array, after first
|
|
|
|
* removing any duplicates.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
record_object_address_dependencies(const ObjectAddress *depender,
|
|
|
|
ObjectAddresses *referenced,
|
|
|
|
DependencyType behavior)
|
|
|
|
{
|
|
|
|
eliminate_duplicate_dependencies(referenced);
|
|
|
|
recordMultipleDependencies(depender,
|
|
|
|
referenced->refs, referenced->numrefs,
|
|
|
|
behavior);
|
|
|
|
}
|
|
|
|
|
2002-07-16 07:53:34 +02:00
|
|
|
/*
|
|
|
|
* Clean up when done with an ObjectAddresses array.
|
|
|
|
*/
|
2006-08-20 23:56:16 +02:00
|
|
|
void
|
|
|
|
free_object_addresses(ObjectAddresses *addrs)
|
2002-07-16 07:53:34 +02:00
|
|
|
{
|
|
|
|
pfree(addrs->refs);
|
2006-08-20 23:56:16 +02:00
|
|
|
pfree(addrs);
|
2002-07-16 07:53:34 +02:00
|
|
|
}
|
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
|
|
|
* Determine the class of a given object identified by objectAddress.
|
|
|
|
*
|
2005-04-14 22:03:27 +02:00
|
|
|
* This function is essentially the reverse mapping for the object_classes[]
|
|
|
|
* table. We implement it as a function because the OIDs aren't consecutive.
|
2002-07-12 20:43:19 +02:00
|
|
|
*/
|
2004-05-05 06:48:48 +02:00
|
|
|
ObjectClass
|
2002-07-12 20:43:19 +02:00
|
|
|
getObjectClass(const ObjectAddress *object)
|
|
|
|
{
|
|
|
|
switch (object->classId)
|
|
|
|
{
|
2005-04-14 03:38:22 +02:00
|
|
|
case RelationRelationId:
|
2002-07-12 20:43:19 +02:00
|
|
|
/* caller must check objectSubId */
|
|
|
|
return OCLASS_CLASS;
|
|
|
|
|
2005-04-14 03:38:22 +02:00
|
|
|
case ProcedureRelationId:
|
2002-07-12 20:43:19 +02:00
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_PROC;
|
|
|
|
|
2005-04-14 03:38:22 +02:00
|
|
|
case TypeRelationId:
|
2002-07-12 20:43:19 +02:00
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_TYPE;
|
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
case CastRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_CAST;
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
case ConstraintRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_CONSTRAINT;
|
|
|
|
|
|
|
|
case ConversionRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_CONVERSION;
|
|
|
|
|
|
|
|
case AttrDefaultRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_DEFAULT;
|
|
|
|
|
|
|
|
case LanguageRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_LANGUAGE;
|
|
|
|
|
|
|
|
case OperatorRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_OPERATOR;
|
|
|
|
|
|
|
|
case OperatorClassRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_OPCLASS;
|
|
|
|
|
2006-12-23 01:43:13 +01:00
|
|
|
case OperatorFamilyRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_OPFAMILY;
|
|
|
|
|
|
|
|
case AccessMethodOperatorRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_AMOP;
|
|
|
|
|
|
|
|
case AccessMethodProcedureRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_AMPROC;
|
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
case RewriteRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_REWRITE;
|
|
|
|
|
|
|
|
case TriggerRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_TRIGGER;
|
|
|
|
|
|
|
|
case NamespaceRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_SCHEMA;
|
2005-07-07 22:40:02 +02:00
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
case TSParserRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_TSPARSER;
|
|
|
|
|
|
|
|
case TSDictionaryRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_TSDICT;
|
|
|
|
|
|
|
|
case TSTemplateRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_TSTEMPLATE;
|
|
|
|
|
|
|
|
case TSConfigRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_TSCONFIG;
|
|
|
|
|
2005-07-07 22:40:02 +02:00
|
|
|
case AuthIdRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_ROLE;
|
|
|
|
|
|
|
|
case DatabaseRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_DATABASE;
|
|
|
|
|
|
|
|
case TableSpaceRelationId:
|
|
|
|
Assert(object->objectSubId == 0);
|
|
|
|
return OCLASS_TBLSPACE;
|
2002-07-18 18:47:26 +02:00
|
|
|
}
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
/* shouldn't get here */
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "unrecognized object class: %u", object->classId);
|
2002-07-12 20:43:19 +02:00
|
|
|
return OCLASS_CLASS; /* keep compiler quiet */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getObjectDescription: build an object description for messages
|
|
|
|
*
|
|
|
|
* The result is a palloc'd string.
|
|
|
|
*/
|
2004-05-05 06:48:48 +02:00
|
|
|
char *
|
2002-07-12 20:43:19 +02:00
|
|
|
getObjectDescription(const ObjectAddress *object)
|
|
|
|
{
|
|
|
|
StringInfoData buffer;
|
|
|
|
|
|
|
|
initStringInfo(&buffer);
|
|
|
|
|
|
|
|
switch (getObjectClass(object))
|
|
|
|
{
|
|
|
|
case OCLASS_CLASS:
|
2002-07-15 18:33:32 +02:00
|
|
|
getRelationDescription(&buffer, object->objectId);
|
2002-07-12 20:43:19 +02:00
|
|
|
if (object->objectSubId != 0)
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _(" column %s"),
|
2005-10-15 04:49:52 +02:00
|
|
|
get_relid_attribute_name(object->objectId,
|
|
|
|
object->objectSubId));
|
2002-07-12 20:43:19 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_PROC:
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("function %s"),
|
2002-07-30 00:14:11 +02:00
|
|
|
format_procedure(object->objectId));
|
2002-07-12 20:43:19 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_TYPE:
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("type %s"),
|
2002-07-30 00:14:11 +02:00
|
|
|
format_type_be(object->objectId));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_CAST:
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
Relation castDesc;
|
|
|
|
ScanKeyData skey[1];
|
|
|
|
SysScanDesc rcscan;
|
|
|
|
HeapTuple tup;
|
|
|
|
Form_pg_cast castForm;
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
castDesc = heap_open(CastRelationId, AccessShareLock);
|
2002-07-30 00:14:11 +02:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
ObjectIdAttributeNumber,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->objectId));
|
2002-07-30 00:14:11 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
rcscan = systable_beginscan(castDesc, CastOidIndexId, true,
|
2002-09-04 22:31:48 +02:00
|
|
|
SnapshotNow, 1, skey);
|
2002-07-30 00:14:11 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
tup = systable_getnext(rcscan);
|
2002-07-30 00:14:11 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "could not find tuple for cast %u",
|
2002-09-04 22:31:48 +02:00
|
|
|
object->objectId);
|
2002-07-30 00:14:11 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
castForm = (Form_pg_cast) GETSTRUCT(tup);
|
2002-07-30 00:14:11 +02:00
|
|
|
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("cast from %s to %s"),
|
2002-09-04 22:31:48 +02:00
|
|
|
format_type_be(castForm->castsource),
|
|
|
|
format_type_be(castForm->casttarget));
|
2002-07-30 00:14:11 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
systable_endscan(rcscan);
|
|
|
|
heap_close(castDesc, AccessShareLock);
|
|
|
|
break;
|
|
|
|
}
|
2002-07-12 20:43:19 +02:00
|
|
|
|
|
|
|
case OCLASS_CONSTRAINT:
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
2007-02-14 02:58:58 +01:00
|
|
|
HeapTuple conTup;
|
2002-09-04 22:31:48 +02:00
|
|
|
Form_pg_constraint con;
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2007-02-14 02:58:58 +01:00
|
|
|
conTup = SearchSysCache(CONSTROID,
|
|
|
|
ObjectIdGetDatum(object->objectId),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(conTup))
|
|
|
|
elog(ERROR, "cache lookup failed for constraint %u",
|
2002-09-04 22:31:48 +02:00
|
|
|
object->objectId);
|
2007-02-14 02:58:58 +01:00
|
|
|
con = (Form_pg_constraint) GETSTRUCT(conTup);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (OidIsValid(con->conrelid))
|
|
|
|
{
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("constraint %s on "),
|
2002-09-04 22:31:48 +02:00
|
|
|
NameStr(con->conname));
|
|
|
|
getRelationDescription(&buffer, con->conrelid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("constraint %s"),
|
2002-09-04 22:31:48 +02:00
|
|
|
NameStr(con->conname));
|
|
|
|
}
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2007-02-14 02:58:58 +01:00
|
|
|
ReleaseSysCache(conTup);
|
2002-09-04 22:31:48 +02:00
|
|
|
break;
|
|
|
|
}
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-07-25 12:07:13 +02:00
|
|
|
case OCLASS_CONVERSION:
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
HeapTuple conTup;
|
|
|
|
|
2007-02-14 02:58:58 +01:00
|
|
|
conTup = SearchSysCache(CONVOID,
|
2005-10-15 04:49:52 +02:00
|
|
|
ObjectIdGetDatum(object->objectId),
|
2002-09-04 22:31:48 +02:00
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(conTup))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "cache lookup failed for conversion %u",
|
2002-09-04 22:31:48 +02:00
|
|
|
object->objectId);
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("conversion %s"),
|
2005-10-15 04:49:52 +02:00
|
|
|
NameStr(((Form_pg_conversion) GETSTRUCT(conTup))->conname));
|
2002-09-04 22:31:48 +02:00
|
|
|
ReleaseSysCache(conTup);
|
|
|
|
break;
|
|
|
|
}
|
2002-07-25 12:07:13 +02:00
|
|
|
|
2002-07-15 18:33:32 +02:00
|
|
|
case OCLASS_DEFAULT:
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
Relation attrdefDesc;
|
|
|
|
ScanKeyData skey[1];
|
|
|
|
SysScanDesc adscan;
|
|
|
|
HeapTuple tup;
|
|
|
|
Form_pg_attrdef attrdef;
|
|
|
|
ObjectAddress colobject;
|
2002-07-15 18:33:32 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
attrdefDesc = heap_open(AttrDefaultRelationId, AccessShareLock);
|
2002-07-15 18:33:32 +02:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
ObjectIdAttributeNumber,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->objectId));
|
2002-07-15 18:33:32 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
adscan = systable_beginscan(attrdefDesc, AttrDefaultOidIndexId,
|
2003-11-09 22:30:38 +01:00
|
|
|
true, SnapshotNow, 1, skey);
|
2002-07-15 18:33:32 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
tup = systable_getnext(adscan);
|
2002-07-15 18:33:32 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "could not find tuple for attrdef %u",
|
2002-09-04 22:31:48 +02:00
|
|
|
object->objectId);
|
2002-07-15 18:33:32 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
attrdef = (Form_pg_attrdef) GETSTRUCT(tup);
|
2002-07-15 18:33:32 +02:00
|
|
|
|
2005-04-14 03:38:22 +02:00
|
|
|
colobject.classId = RelationRelationId;
|
2002-09-04 22:31:48 +02:00
|
|
|
colobject.objectId = attrdef->adrelid;
|
|
|
|
colobject.objectSubId = attrdef->adnum;
|
2002-07-15 18:33:32 +02:00
|
|
|
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("default for %s"),
|
2002-09-04 22:31:48 +02:00
|
|
|
getObjectDescription(&colobject));
|
2002-07-15 18:33:32 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
systable_endscan(adscan);
|
|
|
|
heap_close(attrdefDesc, AccessShareLock);
|
|
|
|
break;
|
|
|
|
}
|
2002-07-15 18:33:32 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
case OCLASS_LANGUAGE:
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
HeapTuple langTup;
|
|
|
|
|
|
|
|
langTup = SearchSysCache(LANGOID,
|
2005-10-15 04:49:52 +02:00
|
|
|
ObjectIdGetDatum(object->objectId),
|
2002-09-04 22:31:48 +02:00
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(langTup))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "cache lookup failed for language %u",
|
2002-09-04 22:31:48 +02:00
|
|
|
object->objectId);
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("language %s"),
|
2005-10-15 04:49:52 +02:00
|
|
|
NameStr(((Form_pg_language) GETSTRUCT(langTup))->lanname));
|
2002-09-04 22:31:48 +02:00
|
|
|
ReleaseSysCache(langTup);
|
|
|
|
break;
|
|
|
|
}
|
2002-07-12 20:43:19 +02:00
|
|
|
|
|
|
|
case OCLASS_OPERATOR:
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("operator %s"),
|
2002-07-30 00:14:11 +02:00
|
|
|
format_operator(object->objectId));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_OPCLASS:
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
HeapTuple opcTup;
|
|
|
|
Form_pg_opclass opcForm;
|
|
|
|
HeapTuple amTup;
|
|
|
|
Form_pg_am amForm;
|
|
|
|
char *nspname;
|
|
|
|
|
|
|
|
opcTup = SearchSysCache(CLAOID,
|
2005-10-15 04:49:52 +02:00
|
|
|
ObjectIdGetDatum(object->objectId),
|
2002-09-04 22:31:48 +02:00
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(opcTup))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "cache lookup failed for opclass %u",
|
2002-09-04 22:31:48 +02:00
|
|
|
object->objectId);
|
|
|
|
opcForm = (Form_pg_opclass) GETSTRUCT(opcTup);
|
|
|
|
|
|
|
|
amTup = SearchSysCache(AMOID,
|
2006-12-23 01:43:13 +01:00
|
|
|
ObjectIdGetDatum(opcForm->opcmethod),
|
2002-09-04 22:31:48 +02:00
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(amTup))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "cache lookup failed for access method %u",
|
2006-12-23 01:43:13 +01:00
|
|
|
opcForm->opcmethod);
|
2002-09-04 22:31:48 +02:00
|
|
|
amForm = (Form_pg_am) GETSTRUCT(amTup);
|
|
|
|
|
2004-05-05 06:48:48 +02:00
|
|
|
/* Qualify the name if not visible in search path */
|
|
|
|
if (OpclassIsVisible(object->objectId))
|
|
|
|
nspname = NULL;
|
|
|
|
else
|
|
|
|
nspname = get_namespace_name(opcForm->opcnamespace);
|
|
|
|
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("operator class %s for access method %s"),
|
2004-05-05 06:48:48 +02:00
|
|
|
quote_qualified_identifier(nspname,
|
2005-10-15 04:49:52 +02:00
|
|
|
NameStr(opcForm->opcname)),
|
2002-09-04 22:31:48 +02:00
|
|
|
NameStr(amForm->amname));
|
|
|
|
|
|
|
|
ReleaseSysCache(amTup);
|
|
|
|
ReleaseSysCache(opcTup);
|
|
|
|
break;
|
|
|
|
}
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2006-12-23 01:43:13 +01:00
|
|
|
case OCLASS_OPFAMILY:
|
|
|
|
getOpFamilyDescription(&buffer, object->objectId);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_AMOP:
|
|
|
|
{
|
|
|
|
Relation amopDesc;
|
|
|
|
ScanKeyData skey[1];
|
|
|
|
SysScanDesc amscan;
|
|
|
|
HeapTuple tup;
|
|
|
|
Form_pg_amop amopForm;
|
|
|
|
|
|
|
|
amopDesc = heap_open(AccessMethodOperatorRelationId,
|
|
|
|
AccessShareLock);
|
|
|
|
|
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
ObjectIdAttributeNumber,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->objectId));
|
|
|
|
|
|
|
|
amscan = systable_beginscan(amopDesc, AccessMethodOperatorOidIndexId, true,
|
|
|
|
SnapshotNow, 1, skey);
|
|
|
|
|
|
|
|
tup = systable_getnext(amscan);
|
|
|
|
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
elog(ERROR, "could not find tuple for amop entry %u",
|
|
|
|
object->objectId);
|
|
|
|
|
|
|
|
amopForm = (Form_pg_amop) GETSTRUCT(tup);
|
|
|
|
|
|
|
|
appendStringInfo(&buffer, _("operator %d %s of "),
|
|
|
|
amopForm->amopstrategy,
|
|
|
|
format_operator(amopForm->amopopr));
|
|
|
|
getOpFamilyDescription(&buffer, amopForm->amopfamily);
|
|
|
|
|
|
|
|
systable_endscan(amscan);
|
|
|
|
heap_close(amopDesc, AccessShareLock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OCLASS_AMPROC:
|
|
|
|
{
|
|
|
|
Relation amprocDesc;
|
|
|
|
ScanKeyData skey[1];
|
|
|
|
SysScanDesc amscan;
|
|
|
|
HeapTuple tup;
|
|
|
|
Form_pg_amproc amprocForm;
|
|
|
|
|
|
|
|
amprocDesc = heap_open(AccessMethodProcedureRelationId,
|
|
|
|
AccessShareLock);
|
|
|
|
|
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
ObjectIdAttributeNumber,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->objectId));
|
|
|
|
|
|
|
|
amscan = systable_beginscan(amprocDesc, AccessMethodProcedureOidIndexId, true,
|
|
|
|
SnapshotNow, 1, skey);
|
|
|
|
|
|
|
|
tup = systable_getnext(amscan);
|
|
|
|
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
elog(ERROR, "could not find tuple for amproc entry %u",
|
|
|
|
object->objectId);
|
|
|
|
|
|
|
|
amprocForm = (Form_pg_amproc) GETSTRUCT(tup);
|
|
|
|
|
|
|
|
appendStringInfo(&buffer, _("function %d %s of "),
|
|
|
|
amprocForm->amprocnum,
|
|
|
|
format_procedure(amprocForm->amproc));
|
|
|
|
getOpFamilyDescription(&buffer, amprocForm->amprocfamily);
|
|
|
|
|
|
|
|
systable_endscan(amscan);
|
|
|
|
heap_close(amprocDesc, AccessShareLock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
case OCLASS_REWRITE:
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
Relation ruleDesc;
|
|
|
|
ScanKeyData skey[1];
|
|
|
|
SysScanDesc rcscan;
|
|
|
|
HeapTuple tup;
|
|
|
|
Form_pg_rewrite rule;
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
ruleDesc = heap_open(RewriteRelationId, AccessShareLock);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
ObjectIdAttributeNumber,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->objectId));
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
rcscan = systable_beginscan(ruleDesc, RewriteOidIndexId, true,
|
2002-09-04 22:31:48 +02:00
|
|
|
SnapshotNow, 1, skey);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
tup = systable_getnext(rcscan);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "could not find tuple for rule %u",
|
2002-09-04 22:31:48 +02:00
|
|
|
object->objectId);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
rule = (Form_pg_rewrite) GETSTRUCT(tup);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("rule %s on "),
|
2002-09-04 22:31:48 +02:00
|
|
|
NameStr(rule->rulename));
|
|
|
|
getRelationDescription(&buffer, rule->ev_class);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
systable_endscan(rcscan);
|
|
|
|
heap_close(ruleDesc, AccessShareLock);
|
|
|
|
break;
|
|
|
|
}
|
2002-07-12 20:43:19 +02:00
|
|
|
|
|
|
|
case OCLASS_TRIGGER:
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
Relation trigDesc;
|
|
|
|
ScanKeyData skey[1];
|
|
|
|
SysScanDesc tgscan;
|
|
|
|
HeapTuple tup;
|
|
|
|
Form_pg_trigger trig;
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
trigDesc = heap_open(TriggerRelationId, AccessShareLock);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
ObjectIdAttributeNumber,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(object->objectId));
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
tgscan = systable_beginscan(trigDesc, TriggerOidIndexId, true,
|
2002-09-04 22:31:48 +02:00
|
|
|
SnapshotNow, 1, skey);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
tup = systable_getnext(tgscan);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "could not find tuple for trigger %u",
|
2002-09-04 22:31:48 +02:00
|
|
|
object->objectId);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
trig = (Form_pg_trigger) GETSTRUCT(tup);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("trigger %s on "),
|
2002-09-04 22:31:48 +02:00
|
|
|
NameStr(trig->tgname));
|
|
|
|
getRelationDescription(&buffer, trig->tgrelid);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
systable_endscan(tgscan);
|
|
|
|
heap_close(trigDesc, AccessShareLock);
|
|
|
|
break;
|
|
|
|
}
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2002-07-18 18:47:26 +02:00
|
|
|
case OCLASS_SCHEMA:
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
char *nspname;
|
2002-07-18 18:47:26 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
nspname = get_namespace_name(object->objectId);
|
|
|
|
if (!nspname)
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "cache lookup failed for namespace %u",
|
2002-09-04 22:31:48 +02:00
|
|
|
object->objectId);
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(&buffer, _("schema %s"), nspname);
|
2002-09-04 22:31:48 +02:00
|
|
|
break;
|
|
|
|
}
|
2002-07-18 18:47:26 +02:00
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
case OCLASS_TSPARSER:
|
|
|
|
{
|
|
|
|
HeapTuple tup;
|
|
|
|
|
|
|
|
tup = SearchSysCache(TSPARSEROID,
|
|
|
|
ObjectIdGetDatum(object->objectId),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
elog(ERROR, "cache lookup failed for text search parser %u",
|
|
|
|
object->objectId);
|
|
|
|
appendStringInfo(&buffer, _("text search parser %s"),
|
2007-11-15 22:14:46 +01:00
|
|
|
NameStr(((Form_pg_ts_parser) GETSTRUCT(tup))->prsname));
|
2007-08-21 03:11:32 +02:00
|
|
|
ReleaseSysCache(tup);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OCLASS_TSDICT:
|
|
|
|
{
|
|
|
|
HeapTuple tup;
|
|
|
|
|
|
|
|
tup = SearchSysCache(TSDICTOID,
|
|
|
|
ObjectIdGetDatum(object->objectId),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
elog(ERROR, "cache lookup failed for text search dictionary %u",
|
|
|
|
object->objectId);
|
|
|
|
appendStringInfo(&buffer, _("text search dictionary %s"),
|
2007-11-15 22:14:46 +01:00
|
|
|
NameStr(((Form_pg_ts_dict) GETSTRUCT(tup))->dictname));
|
2007-08-21 03:11:32 +02:00
|
|
|
ReleaseSysCache(tup);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OCLASS_TSTEMPLATE:
|
|
|
|
{
|
|
|
|
HeapTuple tup;
|
|
|
|
|
|
|
|
tup = SearchSysCache(TSTEMPLATEOID,
|
|
|
|
ObjectIdGetDatum(object->objectId),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
elog(ERROR, "cache lookup failed for text search template %u",
|
|
|
|
object->objectId);
|
|
|
|
appendStringInfo(&buffer, _("text search template %s"),
|
2007-11-15 22:14:46 +01:00
|
|
|
NameStr(((Form_pg_ts_template) GETSTRUCT(tup))->tmplname));
|
2007-08-21 03:11:32 +02:00
|
|
|
ReleaseSysCache(tup);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OCLASS_TSCONFIG:
|
|
|
|
{
|
|
|
|
HeapTuple tup;
|
|
|
|
|
|
|
|
tup = SearchSysCache(TSCONFIGOID,
|
|
|
|
ObjectIdGetDatum(object->objectId),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
elog(ERROR, "cache lookup failed for text search configuration %u",
|
|
|
|
object->objectId);
|
|
|
|
appendStringInfo(&buffer, _("text search configuration %s"),
|
2007-11-15 22:14:46 +01:00
|
|
|
NameStr(((Form_pg_ts_config) GETSTRUCT(tup))->cfgname));
|
2007-08-21 03:11:32 +02:00
|
|
|
ReleaseSysCache(tup);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-07-07 22:40:02 +02:00
|
|
|
case OCLASS_ROLE:
|
|
|
|
{
|
|
|
|
appendStringInfo(&buffer, _("role %s"),
|
|
|
|
GetUserNameFromId(object->objectId));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OCLASS_DATABASE:
|
|
|
|
{
|
|
|
|
char *datname;
|
|
|
|
|
|
|
|
datname = get_database_name(object->objectId);
|
|
|
|
if (!datname)
|
|
|
|
elog(ERROR, "cache lookup failed for database %u",
|
|
|
|
object->objectId);
|
|
|
|
appendStringInfo(&buffer, _("database %s"), datname);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OCLASS_TBLSPACE:
|
|
|
|
{
|
|
|
|
char *tblspace;
|
|
|
|
|
|
|
|
tblspace = get_tablespace_name(object->objectId);
|
|
|
|
if (!tblspace)
|
|
|
|
elog(ERROR, "cache lookup failed for tablespace %u",
|
|
|
|
object->objectId);
|
|
|
|
appendStringInfo(&buffer, _("tablespace %s"), tblspace);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
default:
|
2003-07-28 02:09:16 +02:00
|
|
|
appendStringInfo(&buffer, "unrecognized object %u %u %d",
|
2002-07-12 20:43:19 +02:00
|
|
|
object->classId,
|
|
|
|
object->objectId,
|
|
|
|
object->objectSubId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.data;
|
|
|
|
}
|
2002-07-15 18:33:32 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* subroutine for getObjectDescription: describe a relation
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
getRelationDescription(StringInfo buffer, Oid relid)
|
|
|
|
{
|
|
|
|
HeapTuple relTup;
|
2002-09-04 22:31:48 +02:00
|
|
|
Form_pg_class relForm;
|
2002-07-30 00:14:11 +02:00
|
|
|
char *nspname;
|
|
|
|
char *relname;
|
2002-07-15 18:33:32 +02:00
|
|
|
|
|
|
|
relTup = SearchSysCache(RELOID,
|
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(relTup))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "cache lookup failed for relation %u", relid);
|
2002-07-15 18:33:32 +02:00
|
|
|
relForm = (Form_pg_class) GETSTRUCT(relTup);
|
|
|
|
|
2002-07-30 00:14:11 +02:00
|
|
|
/* Qualify the name if not visible in search path */
|
|
|
|
if (RelationIsVisible(relid))
|
|
|
|
nspname = NULL;
|
|
|
|
else
|
|
|
|
nspname = get_namespace_name(relForm->relnamespace);
|
|
|
|
|
|
|
|
relname = quote_qualified_identifier(nspname, NameStr(relForm->relname));
|
|
|
|
|
2002-07-15 18:33:32 +02:00
|
|
|
switch (relForm->relkind)
|
|
|
|
{
|
|
|
|
case RELKIND_RELATION:
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(buffer, _("table %s"),
|
2002-07-30 00:14:11 +02:00
|
|
|
relname);
|
2002-07-15 18:33:32 +02:00
|
|
|
break;
|
|
|
|
case RELKIND_INDEX:
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(buffer, _("index %s"),
|
2002-07-30 00:14:11 +02:00
|
|
|
relname);
|
2002-07-15 18:33:32 +02:00
|
|
|
break;
|
|
|
|
case RELKIND_SEQUENCE:
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(buffer, _("sequence %s"),
|
2002-07-30 00:14:11 +02:00
|
|
|
relname);
|
2002-07-15 18:33:32 +02:00
|
|
|
break;
|
|
|
|
case RELKIND_UNCATALOGED:
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(buffer, _("uncataloged table %s"),
|
2002-07-30 00:14:11 +02:00
|
|
|
relname);
|
2002-07-15 18:33:32 +02:00
|
|
|
break;
|
|
|
|
case RELKIND_TOASTVALUE:
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(buffer, _("toast table %s"),
|
2002-07-30 00:14:11 +02:00
|
|
|
relname);
|
2002-07-15 18:33:32 +02:00
|
|
|
break;
|
|
|
|
case RELKIND_VIEW:
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(buffer, _("view %s"),
|
2002-07-30 00:14:11 +02:00
|
|
|
relname);
|
2002-07-15 18:33:32 +02:00
|
|
|
break;
|
2002-09-20 01:40:56 +02:00
|
|
|
case RELKIND_COMPOSITE_TYPE:
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(buffer, _("composite type %s"),
|
2002-09-20 01:40:56 +02:00
|
|
|
relname);
|
|
|
|
break;
|
2002-07-15 18:33:32 +02:00
|
|
|
default:
|
|
|
|
/* shouldn't get here */
|
2005-02-22 05:43:23 +01:00
|
|
|
appendStringInfo(buffer, _("relation %s"),
|
2002-07-30 00:14:11 +02:00
|
|
|
relname);
|
2002-07-15 18:33:32 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReleaseSysCache(relTup);
|
|
|
|
}
|
2006-12-23 01:43:13 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* subroutine for getObjectDescription: describe an operator family
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
getOpFamilyDescription(StringInfo buffer, Oid opfid)
|
|
|
|
{
|
|
|
|
HeapTuple opfTup;
|
|
|
|
Form_pg_opfamily opfForm;
|
|
|
|
HeapTuple amTup;
|
|
|
|
Form_pg_am amForm;
|
|
|
|
char *nspname;
|
|
|
|
|
|
|
|
opfTup = SearchSysCache(OPFAMILYOID,
|
|
|
|
ObjectIdGetDatum(opfid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(opfTup))
|
|
|
|
elog(ERROR, "cache lookup failed for opfamily %u", opfid);
|
|
|
|
opfForm = (Form_pg_opfamily) GETSTRUCT(opfTup);
|
|
|
|
|
|
|
|
amTup = SearchSysCache(AMOID,
|
|
|
|
ObjectIdGetDatum(opfForm->opfmethod),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(amTup))
|
|
|
|
elog(ERROR, "cache lookup failed for access method %u",
|
|
|
|
opfForm->opfmethod);
|
|
|
|
amForm = (Form_pg_am) GETSTRUCT(amTup);
|
|
|
|
|
|
|
|
/* Qualify the name if not visible in search path */
|
|
|
|
if (OpfamilyIsVisible(opfid))
|
|
|
|
nspname = NULL;
|
|
|
|
else
|
|
|
|
nspname = get_namespace_name(opfForm->opfnamespace);
|
|
|
|
|
|
|
|
appendStringInfo(buffer, _("operator family %s for access method %s"),
|
|
|
|
quote_qualified_identifier(nspname,
|
|
|
|
NameStr(opfForm->opfname)),
|
|
|
|
NameStr(amForm->amname));
|
|
|
|
|
|
|
|
ReleaseSysCache(amTup);
|
|
|
|
ReleaseSysCache(opfTup);
|
|
|
|
}
|