*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.74 2002/03/29 19:06:03 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.75 2002/03/29 22:10:33 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "commands/rename.h"
#include "miscadmin.h"
#include "utils/builtins.h"
+#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/temprel.h"
bool istemp;
char NewHeapName[NAMEDATALEN];
char NewIndexName[NAMEDATALEN];
- RangeVar *saveoldrelation;
- RangeVar *saveoldindex;
RangeVar *NewHeap;
RangeVar *NewIndex;
- /*
- * FIXME SCHEMAS: The old code had the comment:
- * "Copy the arguments into local storage, just to be safe."
- * By using copyObject we are not using local storage.
- * Was that really necessary?
- */
- saveoldrelation = copyObject(oldrelation);
- saveoldindex = copyObject(oldrelation);
- saveoldindex->relname = pstrdup(oldindexname);
-
/*
* We grab exclusive access to the target rel and index for the
* duration of the transaction.
*/
- OldHeap = heap_openrv(saveoldrelation, AccessExclusiveLock);
+ OldHeap = heap_openrv(oldrelation, AccessExclusiveLock);
OIDOldHeap = RelationGetRelid(OldHeap);
- OldIndex = index_openrv(saveoldindex);
- LockRelation(OldIndex, AccessExclusiveLock);
- OIDOldIndex = RelationGetRelid(OldIndex);
+ istemp = is_temp_rel_name(oldrelation->relname);
- istemp = is_temp_rel_name(saveoldrelation->relname);
+ /*
+ * The index is expected to be in the same namespace as the relation.
+ */
+ OIDOldIndex = get_relname_relid(oldindexname,
+ RelationGetNamespace(OldHeap));
+ if (!OidIsValid(OIDOldIndex))
+ elog(ERROR, "CLUSTER: cannot find index \"%s\" for table \"%s\"",
+ oldindexname, oldrelation->relname);
+ OldIndex = index_open(OIDOldIndex);
+ LockRelation(OldIndex, AccessExclusiveLock);
/*
* Check that index is in fact an index on the given relation
*/
if (OldIndex->rd_index->indrelid != OIDOldHeap)
elog(ERROR, "CLUSTER: \"%s\" is not an index for table \"%s\"",
- saveoldindex->relname, saveoldrelation->relname);
+ oldindexname, oldrelation->relname);
/* Drop relcache refcnts, but do NOT give up the locks */
heap_close(OldHeap, NoLock);
CommandCounterIncrement();
- NewHeap = copyObject(saveoldrelation);
+ /* XXX ugly, and possibly wrong in the presence of schemas... */
+ /* would be better to pass OIDs to renamerel. */
+ NewHeap = copyObject(oldrelation);
NewHeap->relname = NewHeapName;
- NewIndex = copyObject(saveoldindex);
+ NewIndex = copyObject(oldrelation);
NewIndex->relname = NewIndexName;
- renamerel(NewHeap, saveoldrelation->relname);
+ renamerel(NewHeap, oldrelation->relname);
/* This one might be unnecessary, but let's be safe. */
CommandCounterIncrement();
- renamerel(NewIndex, saveoldindex->relname);
+ renamerel(NewIndex, oldindexname);
}
static Oid
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.108 2002/03/26 19:15:45 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.109 2002/03/29 22:10:33 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "catalog/catalog.h"
#include "catalog/catname.h"
#include "catalog/indexing.h"
+#include "catalog/namespace.h"
#include "catalog/pg_language.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_trigger.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
+#include "utils/lsyscache.h"
#include "utils/syscache.h"
stmt->trigname = constrtrigname;
sprintf(constrtrigname, "RI_ConstraintTrigger_%u", newoid());
- if (stmt->constrrel == NULL)
- constrrelid = InvalidOid;
+ if (stmt->constrrel != NULL)
+ constrrelid = RangeVarGetRelid(stmt->constrrel, false);
else
- {
- /*
- * NoLock is probably sufficient here, since we're only
- * interested in getting the relation's OID...
- */
- Relation conrel;
-
- conrel = heap_openrv(stmt->constrrel, NoLock);
- constrrelid = conrel->rd_id;
- heap_close(conrel, NoLock);
- }
+ constrrelid = InvalidOid;
}
TRIGGER_CLEAR_TYPE(tgtype);
heap_close(rel, NoLock);
}
+/*
+ * DropTrigger - drop an individual trigger by name
+ */
void
-DropTrigger(DropTrigStmt *stmt)
+DropTrigger(Oid relid, const char *trigname)
{
Relation rel;
Relation tgrel;
Relation pgrel;
HeapTuple tuple;
Relation ridescs[Num_pg_class_indices];
+ int remaining = 0;
int found = 0;
- int tgfound = 0;
- rel = heap_openrv(stmt->relation, AccessExclusiveLock);
+ rel = heap_open(relid, AccessExclusiveLock);
if (rel->rd_rel->relkind != RELKIND_RELATION)
elog(ERROR, "DropTrigger: relation \"%s\" is not a table",
- stmt->relation->relname);
+ RelationGetRelationName(rel));
- if (!allowSystemTableMods && IsSystemRelationName(stmt->relation->relname))
+ if (!allowSystemTableMods && IsSystemRelationName(RelationGetRelationName(rel)))
elog(ERROR, "DropTrigger: can't drop trigger for system relation %s",
- stmt->relation->relname);
+ RelationGetRelationName(rel));
- if (!pg_class_ownercheck(RelationGetRelid(rel), GetUserId()))
- elog(ERROR, "%s: %s", stmt->relation->relname,
+ if (!pg_class_ownercheck(relid, GetUserId()))
+ elog(ERROR, "%s: %s", RelationGetRelationName(rel),
aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
/*
tgrel = heap_openr(TriggerRelationName, RowExclusiveLock);
ScanKeyEntryInitialize(&key, 0, Anum_pg_trigger_tgrelid,
F_OIDEQ,
- ObjectIdGetDatum(RelationGetRelid(rel)));
+ ObjectIdGetDatum(relid));
tgscan = systable_beginscan(tgrel, TriggerRelidIndex, true,
SnapshotNow, 1, &key);
while (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
{
Form_pg_trigger pg_trigger = (Form_pg_trigger) GETSTRUCT(tuple);
- if (namestrcmp(&(pg_trigger->tgname), stmt->trigname) == 0)
+ if (namestrcmp(&(pg_trigger->tgname), trigname) == 0)
{
/* Delete any comments associated with this trigger */
DeleteComments(tuple->t_data->t_oid, RelationGetRelid(tgrel));
simple_heap_delete(tgrel, &tuple->t_self);
- tgfound++;
+ found++;
}
else
- found++;
+ remaining++;
}
systable_endscan(tgscan);
heap_close(tgrel, RowExclusiveLock);
- if (tgfound == 0)
+ if (found == 0)
elog(ERROR, "DropTrigger: there is no trigger %s on relation %s",
- stmt->trigname, stmt->relation->relname);
- if (tgfound > 1)
+ trigname, RelationGetRelationName(rel));
+ if (found > 1) /* shouldn't happen */
elog(NOTICE, "DropTrigger: found (and deleted) %d triggers %s on relation %s",
- tgfound, stmt->trigname, stmt->relation->relname);
+ found, trigname, RelationGetRelationName(rel));
/*
* Update relation's pg_class entry. Crucial side-effect: other
*/
pgrel = heap_openr(RelationRelationName, RowExclusiveLock);
tuple = SearchSysCacheCopy(RELOID,
- ObjectIdGetDatum(RelationGetRelid(rel)),
+ ObjectIdGetDatum(relid),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "DropTrigger: relation %s not found in pg_class",
- stmt->relation->relname);
+ RelationGetRelationName(rel));
- ((Form_pg_class) GETSTRUCT(tuple))->reltriggers = found;
+ ((Form_pg_class) GETSTRUCT(tuple))->reltriggers = remaining;
simple_heap_update(pgrel, &tuple->t_self, tuple);
CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices, ridescs);
CatalogIndexInsert(ridescs, Num_pg_class_indices, pgrel, tuple);
heap_freetuple(tuple);
heap_close(pgrel, RowExclusiveLock);
- /*
- * We used to try to update the rel's relcache entry here, but that's
- * fairly pointless since it will happen as a byproduct of the
- * upcoming CommandCounterIncrement...
- */
-
/* Keep lock on target rel until end of xact */
heap_close(rel, NoLock);
}
while (HeapTupleIsValid(tup = systable_getnext(tgscan)))
{
- Form_pg_trigger pg_trigger;
- Relation refrel;
- DropTrigStmt *stmt = makeNode(DropTrigStmt);
-
- pg_trigger = (Form_pg_trigger) GETSTRUCT(tup);
-
- stmt->trigname = pstrdup(NameStr(pg_trigger->tgname));
-
- /* May as well grab AccessExclusiveLock, since DropTrigger will. */
- refrel = heap_open(pg_trigger->tgrelid, AccessExclusiveLock);
- stmt->relation = makeNode(RangeVar);
- /* XXX bogus: what about schema? */
- stmt->relation->relname = pstrdup(RelationGetRelationName(refrel));
- heap_close(refrel, NoLock);
+ Form_pg_trigger pg_trigger = (Form_pg_trigger) GETSTRUCT(tup);
elog(NOTICE, "DROP TABLE implicitly drops referential integrity trigger from table \"%s\"",
- stmt->relation->relname);
+ get_temp_rel_by_physicalname(get_rel_name(pg_trigger->tgrelid)));
- DropTrigger(stmt);
+ DropTrigger(pg_trigger->tgrelid, NameStr(pg_trigger->tgname));
/*
* Need to do a command counter increment here to show up new