diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2002-03-26 19:17:02 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2002-03-26 19:17:02 +0000 |
commit | 1dbf8aa7a8159875bb46f6ee6ab0116eee76869b (patch) | |
tree | 9aa08b4247dc230b6d35aa9c34b488b36b716693 /src/backend/commands | |
parent | da631e931f9da4bc5df4bfd39f0c42684adfb8e5 (diff) | |
download | postgresql-1dbf8aa7a8159875bb46f6ee6ab0116eee76869b.tar.gz postgresql-1dbf8aa7a8159875bb46f6ee6ab0116eee76869b.zip |
pg_class has a relnamespace column. You can create and access tables
in schemas other than the system namespace; however, there's no search
path yet, and not all operations work yet on tables outside the system
namespace.
Diffstat (limited to 'src/backend/commands')
-rw-r--r-- | src/backend/commands/cluster.c | 59 | ||||
-rw-r--r-- | src/backend/commands/command.c | 118 | ||||
-rw-r--r-- | src/backend/commands/comment.c | 29 | ||||
-rw-r--r-- | src/backend/commands/creatinh.c | 62 | ||||
-rw-r--r-- | src/backend/commands/indexcmds.c | 74 | ||||
-rw-r--r-- | src/backend/commands/rename.c | 32 | ||||
-rw-r--r-- | src/backend/commands/trigger.c | 18 | ||||
-rw-r--r-- | src/backend/commands/user.c | 8 |
8 files changed, 214 insertions, 186 deletions
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index f3739487426..0bd64232d1a 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -15,7 +15,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.72 2002/02/19 20:11:12 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.73 2002/03/26 19:15:35 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -38,7 +38,8 @@ static Oid copy_heap(Oid OIDOldHeap, char *NewName, bool istemp); -static void copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName); +static void copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName, + bool istemp); static void rebuildheap(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex); /* @@ -54,7 +55,7 @@ static void rebuildheap(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex); * hand, re-creating n indexes may blow out the space. */ void -cluster(char *oldrelname, char *oldindexname) +cluster(RangeVar *oldrelation, char *oldindexname) { Oid OIDOldHeap, OIDOldIndex, @@ -64,34 +65,40 @@ cluster(char *oldrelname, char *oldindexname) bool istemp; char NewHeapName[NAMEDATALEN]; char NewIndexName[NAMEDATALEN]; - char saveoldrelname[NAMEDATALEN]; - char saveoldindexname[NAMEDATALEN]; + RangeVar *saveoldrelation; + RangeVar *saveoldindex; + RangeVar *NewHeap; + RangeVar *NewIndex; /* - * Copy the arguments into local storage, just to be safe. + * 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? */ - StrNCpy(saveoldrelname, oldrelname, NAMEDATALEN); - StrNCpy(saveoldindexname, oldindexname, NAMEDATALEN); + 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_openr(saveoldrelname, AccessExclusiveLock); + OldHeap = heap_openrv(saveoldrelation, AccessExclusiveLock); OIDOldHeap = RelationGetRelid(OldHeap); - OldIndex = index_openr(saveoldindexname); + OldIndex = index_openrv(saveoldindex); LockRelation(OldIndex, AccessExclusiveLock); OIDOldIndex = RelationGetRelid(OldIndex); - istemp = is_temp_rel_name(saveoldrelname); + istemp = is_temp_rel_name(saveoldrelation->relname); /* * 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\"", - saveoldindexname, saveoldrelname); + saveoldindex->relname, saveoldrelation->relname); /* Drop relcache refcnts, but do NOT give up the locks */ heap_close(OldHeap, NoLock); @@ -117,21 +124,26 @@ cluster(char *oldrelname, char *oldindexname) /* Create new index over the tuples of the new heap. */ snprintf(NewIndexName, NAMEDATALEN, "temp_%u", OIDOldIndex); - copy_index(OIDOldIndex, OIDNewHeap, NewIndexName); + copy_index(OIDOldIndex, OIDNewHeap, NewIndexName, istemp); CommandCounterIncrement(); /* Destroy old heap (along with its index) and rename new. */ - heap_drop_with_catalog(saveoldrelname, allowSystemTableMods); + heap_drop_with_catalog(saveoldrelation->relname, allowSystemTableMods); CommandCounterIncrement(); - renamerel(NewHeapName, saveoldrelname); + NewHeap = copyObject(saveoldrelation); + NewHeap->relname = NewHeapName; + NewIndex = copyObject(saveoldindex); + NewIndex->relname = NewIndexName; + + renamerel(NewHeap, saveoldrelation->relname); /* This one might be unnecessary, but let's be safe. */ CommandCounterIncrement(); - renamerel(NewIndexName, saveoldindexname); + renamerel(NewIndex, saveoldindex->relname); } static Oid @@ -151,7 +163,9 @@ copy_heap(Oid OIDOldHeap, char *NewName, bool istemp) */ tupdesc = CreateTupleDescCopyConstr(OldHeapDesc); - OIDNewHeap = heap_create_with_catalog(NewName, tupdesc, + OIDNewHeap = heap_create_with_catalog(NewName, + RelationGetNamespace(OldHeap), + tupdesc, OldHeap->rd_rel->relkind, OldHeap->rd_rel->relhasoids, istemp, @@ -168,7 +182,7 @@ copy_heap(Oid OIDOldHeap, char *NewName, bool istemp) * AlterTableCreateToastTable ends with CommandCounterIncrement(), so * that the TOAST table will be visible for insertion. */ - AlterTableCreateToastTable(NewName, true); + AlterTableCreateToastTable(OIDNewHeap, true); heap_close(OldHeap, NoLock); @@ -176,7 +190,7 @@ copy_heap(Oid OIDOldHeap, char *NewName, bool istemp) } static void -copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName) +copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName, bool istemp) { Relation OldIndex, NewHeap; @@ -189,18 +203,15 @@ copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName) * Create a new index like the old one. To do this I get the info * from pg_index, and add a new index with a temporary name (that will * be changed later). - * - * NOTE: index_create will cause the new index to be a temp relation if - * its parent table is, so we don't need to do anything special for - * the temp-table case here. */ indexInfo = BuildIndexInfo(OldIndex->rd_index); - index_create(RelationGetRelationName(NewHeap), + index_create(OIDNewHeap, NewIndexName, indexInfo, OldIndex->rd_rel->relam, OldIndex->rd_index->indclass, + istemp, OldIndex->rd_index->indisprimary, allowSystemTableMods); diff --git a/src/backend/commands/command.c b/src/backend/commands/command.c index af9e76e2791..bf7980d42c8 100644 --- a/src/backend/commands/command.c +++ b/src/backend/commands/command.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.165 2002/03/22 21:34:44 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.166 2002/03/26 19:15:36 tgl Exp $ * * NOTES * The PerformAddAttribute() code, like most of the relation @@ -26,6 +26,7 @@ #include "catalog/heap.h" #include "catalog/index.h" #include "catalog/indexing.h" +#include "catalog/namespace.h" #include "catalog/pg_attrdef.h" #include "catalog/pg_index.h" #include "catalog/pg_namespace.h" @@ -34,7 +35,6 @@ #include "catalog/pg_type.h" #include "commands/command.h" #include "commands/trigger.h" -#include "commands/defrem.h" #include "executor/execdefs.h" #include "executor/executor.h" #include "miscadmin.h" @@ -415,8 +415,8 @@ AlterTableAddColumn(const char *relationName, rel = heap_openr(RelationRelationName, RowExclusiveLock); - reltup = SearchSysCache(RELNAME, - PointerGetDatum(relationName), + reltup = SearchSysCache(RELOID, + ObjectIdGetDatum(myrelid), 0, 0, 0); if (!HeapTupleIsValid(reltup)) @@ -424,7 +424,7 @@ AlterTableAddColumn(const char *relationName, relationName); if (SearchSysCacheExists(ATTNAME, - ObjectIdGetDatum(reltup->t_data->t_oid), + ObjectIdGetDatum(myrelid), PointerGetDatum(colDef->colname), 0, 0)) elog(ERROR, "ALTER TABLE: column name \"%s\" already exists in table \"%s\"", @@ -463,7 +463,7 @@ AlterTableAddColumn(const char *relationName, attribute = (Form_pg_attribute) GETSTRUCT(attributeTuple); - attribute->attrelid = reltup->t_data->t_oid; + attribute->attrelid = myrelid; namestrcpy(&(attribute->attname), colDef->colname); attribute->atttypid = typeTuple->t_data->t_oid; attribute->attstattarget = DEFAULT_ATTSTATTARGET; @@ -532,7 +532,7 @@ AlterTableAddColumn(const char *relationName, */ if (colDef->constraints != NIL) { - rel = heap_openr(relationName, AccessExclusiveLock); + rel = heap_open(myrelid, AccessExclusiveLock); AddRelationRawConstraints(rel, NIL, colDef->constraints); heap_close(rel, NoLock); } @@ -541,7 +541,7 @@ AlterTableAddColumn(const char *relationName, * Automatically create the secondary relation for TOAST if it * formerly had no such but now has toastable attributes. */ - AlterTableCreateToastTable(relationName, true); + AlterTableCreateToastTable(myrelid, true); } @@ -989,11 +989,7 @@ RemoveColumnReferences(Oid reloid, int attnum, bool checkonly, HeapTuple reltup) } else { - htup = SearchSysCache(RELOID, - ObjectIdGetDatum(index->indexrelid), - 0, 0, 0); - RemoveIndex(NameStr(((Form_pg_class) GETSTRUCT(htup))->relname)); - ReleaseSysCache(htup); + index_drop(index->indexrelid); } break; } @@ -1066,8 +1062,8 @@ AlterTableDropColumn(const char *relationName, * lock the pg_class tuple for update */ rel = heap_openr(RelationRelationName, RowExclusiveLock); - reltup = SearchSysCache(RELNAME, - PointerGetDatum(relationName), + reltup = SearchSysCache(RELOID, + ObjectIdGetDatum(myrelid), 0, 0, 0); if (!HeapTupleIsValid(reltup)) elog(ERROR, "ALTER TABLE: relation \"%s\" not found", @@ -1092,7 +1088,7 @@ AlterTableDropColumn(const char *relationName, * Get the target pg_attribute tuple and make a modifiable copy */ tup = SearchSysCacheCopy(ATTNAME, - ObjectIdGetDatum(reltup->t_data->t_oid), + ObjectIdGetDatum(myrelid), PointerGetDatum(colName), 0, 0); if (!HeapTupleIsValid(tup)) @@ -1370,7 +1366,8 @@ AlterTableAddConstraint(char *relationName, * someone doesn't delete rows out from under us. */ - pkrel = heap_openr(fkconstraint->pktable->relname, AccessExclusiveLock); + pkrel = heap_openrv(fkconstraint->pktable, + AccessExclusiveLock); if (pkrel->rd_rel->relkind != RELKIND_RELATION) elog(ERROR, "referenced table \"%s\" not a relation", fkconstraint->pktable->relname); @@ -1557,43 +1554,48 @@ AlterTableDropConstraint(const char *relationName, * ALTER TABLE OWNER */ void -AlterTableOwner(const char *relationName, const char *newOwnerName) +AlterTableOwner(const RangeVar *tgtrel, const char *newOwnerName) { - Oid relationOid; - Relation relation; - int32 newOwnerSysId; + Relation rel; + Oid myrelid; + int32 newOwnerSysId; /* check that we are the superuser */ if (!superuser()) elog(ERROR, "ALTER TABLE: permission denied"); /* lookup the OID of the target relation */ - relation = RelationNameGetRelation(relationName); - relationOid = relation->rd_id; - RelationClose(relation); + rel = relation_openrv(tgtrel, AccessExclusiveLock); + myrelid = RelationGetRelid(rel); + heap_close(rel, NoLock); /* close rel but keep lock! */ /* lookup the sysid of the new owner */ newOwnerSysId = get_usesysid(newOwnerName); /* do all the actual work */ - AlterTableOwnerId(relationOid, newOwnerSysId); + AlterTableOwnerId(myrelid, newOwnerSysId); } static void AlterTableOwnerId(Oid relationOid, int32 newOwnerSysId) { + Relation target_rel; Relation class_rel; HeapTuple tuple; Relation idescs[Num_pg_class_indices]; Form_pg_class tuple_class; + /* Get exclusive lock till end of transaction on the target table */ + target_rel = heap_open(relationOid, AccessExclusiveLock); + + /* Get its pg_class tuple, too */ + class_rel = heap_openr(RelationRelationName, RowExclusiveLock); + tuple = SearchSysCacheCopy(RELOID, ObjectIdGetDatum(relationOid), 0, 0, 0); if (!HeapTupleIsValid(tuple)) - elog(ERROR, "ALTER TABLE: object ID %hd not found", - relationOid); - + elog(ERROR, "ALTER TABLE: relation %u not found", relationOid); tuple_class = (Form_pg_class) GETSTRUCT(tuple); /* Can we change the ownership of this tuple? */ @@ -1603,7 +1605,6 @@ AlterTableOwnerId(Oid relationOid, int32 newOwnerSysId) * Okay, this is a valid tuple: change its ownership and * write to the heap. */ - class_rel = heap_openr(RelationRelationName, RowExclusiveLock); tuple_class->relowner = newOwnerSysId; simple_heap_update(class_rel, &tuple->t_self, tuple); @@ -1617,25 +1618,25 @@ AlterTableOwnerId(Oid relationOid, int32 newOwnerSysId) * indexes that belong to the table, as well as the table's toast * table (if it has one) */ - if (tuple_class->relkind == RELKIND_RELATION) + if (tuple_class->relkind == RELKIND_RELATION || + tuple_class->relkind == RELKIND_TOASTVALUE) { - /* Search for indexes belonging to this table */ - Relation target_rel; List *index_oid_list, *i; /* Find all the indexes belonging to this relation */ - target_rel = heap_open(relationOid, RowExclusiveLock); index_oid_list = RelationGetIndexList(target_rel); - heap_close(target_rel, RowExclusiveLock); /* For each index, recursively change its ownership */ - foreach (i, index_oid_list) + foreach(i, index_oid_list) { AlterTableOwnerId(lfirsti(i), newOwnerSysId); } freeList(index_oid_list); + } + if (tuple_class->relkind == RELKIND_RELATION) + { /* If it has a toast table, recurse to change its ownership */ if (tuple_class->reltoastrelid != InvalidOid) { @@ -1644,7 +1645,8 @@ AlterTableOwnerId(Oid relationOid, int32 newOwnerSysId) } heap_freetuple(tuple); - heap_close(class_rel, NoLock); + heap_close(class_rel, RowExclusiveLock); + heap_close(target_rel, NoLock); } static void @@ -1669,10 +1671,9 @@ CheckTupleType(Form_pg_class tuple_class) * ALTER TABLE CREATE TOAST TABLE */ void -AlterTableCreateToastTable(const char *relationName, bool silent) +AlterTableCreateToastTable(Oid relOid, bool silent) { Relation rel; - Oid myrelid; HeapTuple reltup; HeapTupleData classtuple; TupleDesc tupdesc; @@ -1690,14 +1691,13 @@ AlterTableCreateToastTable(const char *relationName, bool silent) * Grab an exclusive lock on the target table, which we will NOT * release until end of transaction. */ - rel = heap_openr(relationName, AccessExclusiveLock); - myrelid = RelationGetRelid(rel); + rel = heap_open(relOid, AccessExclusiveLock); if (rel->rd_rel->relkind != RELKIND_RELATION) elog(ERROR, "ALTER TABLE: relation \"%s\" is not a table", - relationName); + RelationGetRelationName(rel)); - if (!pg_class_ownercheck(myrelid, GetUserId())) + if (!pg_class_ownercheck(relOid, GetUserId())) elog(ERROR, "ALTER TABLE: permission denied"); /* @@ -1705,12 +1705,12 @@ AlterTableCreateToastTable(const char *relationName, bool silent) */ class_rel = heap_openr(RelationRelationName, RowExclusiveLock); - reltup = SearchSysCache(RELNAME, - PointerGetDatum(relationName), + reltup = SearchSysCache(RELOID, + ObjectIdGetDatum(relOid), 0, 0, 0); if (!HeapTupleIsValid(reltup)) elog(ERROR, "ALTER TABLE: relation \"%s\" not found", - relationName); + RelationGetRelationName(rel)); classtuple.t_self = reltup->t_self; ReleaseSysCache(reltup); @@ -1739,7 +1739,7 @@ AlterTableCreateToastTable(const char *relationName, bool silent) } elog(ERROR, "ALTER TABLE: relation \"%s\" already has a toast table", - relationName); + RelationGetRelationName(rel)); } /* @@ -1756,14 +1756,14 @@ AlterTableCreateToastTable(const char *relationName, bool silent) } elog(ERROR, "ALTER TABLE: relation \"%s\" does not need a toast table", - relationName); + RelationGetRelationName(rel)); } /* * Create the toast table and its index */ - sprintf(toast_relname, "pg_toast_%u", myrelid); - sprintf(toast_idxname, "pg_toast_%u_idx", myrelid); + sprintf(toast_relname, "pg_toast_%u", relOid); + sprintf(toast_idxname, "pg_toast_%u_idx", relOid); /* this is pretty painful... need a tuple descriptor */ tupdesc = CreateTemplateTupleDesc(3); @@ -1795,7 +1795,9 @@ AlterTableCreateToastTable(const char *relationName, bool silent) * collision, and the toast rel will be destroyed when its master is, * so there's no need to handle the toast rel as temp. */ - toast_relid = heap_create_with_catalog(toast_relname, tupdesc, + toast_relid = heap_create_with_catalog(toast_relname, + RelationGetNamespace(rel), + tupdesc, RELKIND_TOASTVALUE, false, false, true); @@ -1825,9 +1827,9 @@ AlterTableCreateToastTable(const char *relationName, bool silent) classObjectId[0] = OID_BTREE_OPS_OID; classObjectId[1] = INT4_BTREE_OPS_OID; - toast_idxid = index_create(toast_relname, toast_idxname, indexInfo, + toast_idxid = index_create(toast_relid, toast_idxname, indexInfo, BTREE_AM_OID, classObjectId, - true, true); + false, true, true); /* * Update toast rel's pg_class entry to show that it has an index. The @@ -1927,21 +1929,15 @@ LockTableCommand(LockStmt *lockstmt) foreach(p, lockstmt->relations) { RangeVar *relation = lfirst(p); - char *relname = relation->relname; Oid reloid; - int aclresult; + int32 aclresult; Relation rel; /* * We don't want to open the relation until we've checked privilege. * So, manually get the relation OID. */ - reloid = GetSysCacheOid(RELNAME, - PointerGetDatum(relname), - 0, 0, 0); - if (!OidIsValid(reloid)) - elog(ERROR, "LOCK TABLE: relation \"%s\" does not exist", - relname); + reloid = RangeVarGetRelid(relation, false); if (lockstmt->mode == AccessShareLock) aclresult = pg_class_aclcheck(reloid, GetUserId(), @@ -1958,7 +1954,7 @@ LockTableCommand(LockStmt *lockstmt) /* Currently, we only allow plain tables to be locked */ if (rel->rd_rel->relkind != RELKIND_RELATION) elog(ERROR, "LOCK TABLE: %s is not a table", - relname); + relation->relname); relation_close(rel, NoLock); /* close rel, keep lock */ } diff --git a/src/backend/commands/comment.c b/src/backend/commands/comment.c index 4ad49581629..cdc8ce35be3 100644 --- a/src/backend/commands/comment.c +++ b/src/backend/commands/comment.c @@ -7,7 +7,7 @@ * Copyright (c) 1999-2001, PostgreSQL Global Development Group * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/comment.c,v 1.36 2002/03/21 23:27:20 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/comment.c,v 1.37 2002/03/26 19:15:38 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -20,6 +20,7 @@ #include "catalog/indexing.h" #include "catalog/pg_database.h" #include "catalog/pg_description.h" +#include "catalog/pg_namespace.h" #include "catalog/pg_operator.h" #include "catalog/pg_rewrite.h" #include "catalog/pg_trigger.h" @@ -48,7 +49,8 @@ *------------------------------------------------------------------ */ -static void CommentRelation(int objtype, char *relation, char *comment); +static void CommentRelation(int objtype, char * schemaname, char *relation, + char *comment); static void CommentAttribute(char *relation, char *attrib, char *comment); static void CommentDatabase(char *database, char *comment); static void CommentRewrite(char *rule, char *comment); @@ -74,7 +76,7 @@ static void CommentTrigger(char *trigger, char *relation, char *comments); */ void -CommentObject(int objtype, char *objname, char *objproperty, +CommentObject(int objtype, char *schemaname, char *objname, char *objproperty, List *objlist, char *comment) { switch (objtype) @@ -83,7 +85,7 @@ CommentObject(int objtype, char *objname, char *objproperty, case SEQUENCE: case TABLE: case VIEW: - CommentRelation(objtype, objname, comment); + CommentRelation(objtype, schemaname, objname, comment); break; case COLUMN: CommentAttribute(objname, objproperty, comment); @@ -323,9 +325,16 @@ DeleteComments(Oid oid, Oid classoid) */ static void -CommentRelation(int reltype, char *relname, char *comment) +CommentRelation(int reltype, char *schemaname, char *relname, char *comment) { Relation relation; + RangeVar *tgtrel = makeNode(RangeVar); + + + tgtrel->relname = relname; + tgtrel->schemaname = schemaname; + /* FIXME SCHEMA: Can we add comments to temp relations? */ + tgtrel->istemp = false; /* * Open the relation. We do this mainly to acquire a lock that @@ -333,7 +342,7 @@ CommentRelation(int reltype, char *relname, char *comment) * did, they'd fail to remove the entry we are about to make in * pg_description.) */ - relation = relation_openr(relname, AccessShareLock); + relation = relation_openrv(tgtrel, AccessShareLock); /* Check object security */ if (!pg_class_ownercheck(RelationGetRelid(relation), GetUserId())) @@ -504,9 +513,7 @@ CommentRewrite(char *rule, char *comment) /* pg_rewrite doesn't have a hard-coded OID, so must look it up */ - classoid = GetSysCacheOid(RELNAME, - PointerGetDatum(RewriteRelationName), - 0, 0, 0); + classoid = get_relname_relid(RewriteRelationName, PG_CATALOG_NAMESPACE); Assert(OidIsValid(classoid)); /* Call CreateComments() to create/drop the comments */ @@ -604,9 +611,7 @@ CommentAggregate(char *aggregate, List *arguments, char *comment) /* pg_aggregate doesn't have a hard-coded OID, so must look it up */ - classoid = GetSysCacheOid(RELNAME, - PointerGetDatum(AggregateRelationName), - 0, 0, 0); + classoid = get_relname_relid(AggregateRelationName, PG_CATALOG_NAMESPACE); Assert(OidIsValid(classoid)); /* Call CreateComments() to create/drop the comments */ diff --git a/src/backend/commands/creatinh.c b/src/backend/commands/creatinh.c index 0f9bf525ef3..2d68d5d6e7f 100644 --- a/src/backend/commands/creatinh.c +++ b/src/backend/commands/creatinh.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.91 2002/03/22 02:56:31 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.92 2002/03/26 19:15:40 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -18,9 +18,11 @@ #include "access/heapam.h" #include "catalog/catalog.h" #include "catalog/catname.h" -#include "catalog/indexing.h" #include "catalog/heap.h" +#include "catalog/indexing.h" +#include "catalog/namespace.h" #include "catalog/pg_inherits.h" +#include "catalog/pg_namespace.h" #include "catalog/pg_type.h" #include "commands/creatinh.h" #include "miscadmin.h" @@ -54,6 +56,7 @@ Oid DefineRelation(CreateStmt *stmt, char relkind) { char *relname = palloc(NAMEDATALEN); + Oid namespaceId; List *schema = stmt->tableElts; int numberOfAttributes; Oid relationId; @@ -74,6 +77,12 @@ DefineRelation(CreateStmt *stmt, char relkind) StrNCpy(relname, (stmt->relation)->relname, NAMEDATALEN); /* + * Look up the namespace in which we are supposed to create the + * relation. + */ + namespaceId = RangeVarGetCreationNamespace(stmt->relation); + + /* * Merge domain attributes into the known columns before processing table * inheritance. Otherwise we risk adding double constraints to a * domain-type column that's inherited. @@ -147,7 +156,8 @@ DefineRelation(CreateStmt *stmt, char relkind) } } - relationId = heap_create_with_catalog(relname, descriptor, + relationId = heap_create_with_catalog(relname, namespaceId, + descriptor, relkind, stmt->hasoids || parentHasOids, stmt->relation->istemp, @@ -330,7 +340,7 @@ MergeDomainAttributes(List *schema) * Input arguments: * 'schema' is the column/attribute definition for the table. (It's a list * of ColumnDef's.) It is destructively changed. - * 'supers' is a list of names (as Value objects) of parent relations. + * 'supers' is a list of names (as RangeVar nodes) of parent relations. * 'istemp' is TRUE if we are creating a temp relation. * * Output arguments: @@ -418,24 +428,6 @@ MergeAttributes(List *schema, List *supers, bool istemp, } /* - * Reject duplicate names in the list of parents, too. - * - * XXX needs to be smarter about schema-qualified table names. - */ - foreach(entry, supers) - { - List *rest; - - foreach(rest, lnext(entry)) - { - if (strcmp(((RangeVar *) lfirst(entry))->relname, - ((RangeVar *) lfirst(rest))->relname) == 0) - elog(ERROR, "CREATE TABLE: inherited relation \"%s\" duplicated", - ((RangeVar *) lfirst(entry))->relname); - } - } - - /* * Scan the parents left-to-right, and merge their attributes to form * a list of inherited attributes (inhSchema). Also check to see if * we need to inherit an OID column. @@ -443,30 +435,40 @@ MergeAttributes(List *schema, List *supers, bool istemp, child_attno = 0; foreach(entry, supers) { - char *name = ((RangeVar *) lfirst(entry))->relname; + RangeVar *parent = (RangeVar *) lfirst(entry); Relation relation; TupleDesc tupleDesc; TupleConstr *constr; AttrNumber *newattno; AttrNumber parent_attno; - relation = heap_openr(name, AccessShareLock); + relation = heap_openrv(parent, AccessShareLock); if (relation->rd_rel->relkind != RELKIND_RELATION) - elog(ERROR, "CREATE TABLE: inherited relation \"%s\" is not a table", name); + elog(ERROR, "CREATE TABLE: inherited relation \"%s\" is not a table", + parent->relname); /* Permanent rels cannot inherit from temporary ones */ - if (!istemp && is_temp_rel_name(name)) - elog(ERROR, "CREATE TABLE: cannot inherit from temp relation \"%s\"", name); + if (!istemp && is_temp_rel_name(parent->relname)) + elog(ERROR, "CREATE TABLE: cannot inherit from temp relation \"%s\"", + parent->relname); /* * We should have an UNDER permission flag for this, but for now, * demand that creator of a child table own the parent. */ if (!pg_class_ownercheck(RelationGetRelid(relation), GetUserId())) - elog(ERROR, "you do not own table \"%s\"", name); + elog(ERROR, "you do not own table \"%s\"", + parent->relname); + + /* + * Reject duplications in the list of parents. + */ + if (intMember(RelationGetRelid(relation), parentOids)) + elog(ERROR, "CREATE TABLE: inherited relation \"%s\" duplicated", + parent->relname); - parentOids = lappendi(parentOids, relation->rd_id); - setRelhassubclassInRelation(relation->rd_id, true); + parentOids = lappendi(parentOids, RelationGetRelid(relation)); + setRelhassubclassInRelation(RelationGetRelid(relation), true); parentHasOids |= relation->rd_rel->relhasoids; diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 677f1306376..ae3476efb24 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.64 2002/03/20 19:43:47 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.65 2002/03/26 19:15:41 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -18,8 +18,8 @@ #include "access/heapam.h" #include "catalog/catalog.h" #include "catalog/catname.h" -#include "catalog/heap.h" #include "catalog/index.h" +#include "catalog/namespace.h" #include "catalog/pg_opclass.h" #include "commands/defrem.h" #include "miscadmin.h" @@ -33,6 +33,7 @@ #include "utils/fmgroids.h" #include "utils/lsyscache.h" #include "utils/syscache.h" +#include "utils/temprel.h" #define IsFuncIndex(ATTR_LIST) (((IndexElem*)lfirst(ATTR_LIST))->args != NIL) @@ -61,7 +62,7 @@ static Oid GetDefaultOpClass(Oid attrType, Oid accessMethodId); * 'rangetable' is needed to interpret the predicate. */ void -DefineIndex(char *heapRelationName, +DefineIndex(RangeVar *heapRelation, char *indexRelationName, char *accessMethodName, List *attributeList, @@ -73,6 +74,7 @@ DefineIndex(char *heapRelationName, Oid *classObjectId; Oid accessMethodId; Oid relationId; + bool istemp = is_temp_rel_name(heapRelation->relname); Relation rel; HeapTuple tuple; Form_pg_am accessMethodForm; @@ -93,20 +95,20 @@ DefineIndex(char *heapRelationName, /* * Open heap relation, acquire a suitable lock on it, remember its OID */ - rel = heap_openr(heapRelationName, ShareLock); + rel = heap_openrv(heapRelation, ShareLock); /* Note: during bootstrap may see uncataloged relation */ if (rel->rd_rel->relkind != RELKIND_RELATION && rel->rd_rel->relkind != RELKIND_UNCATALOGED) elog(ERROR, "DefineIndex: relation \"%s\" is not a table", - heapRelationName); + heapRelation->relname); relationId = RelationGetRelid(rel); heap_close(rel, NoLock); if (!IsBootstrapProcessingMode() && - IsSystemRelationName(heapRelationName) && + IsSystemRelationName(heapRelation->relname) && !IndexesAreActive(relationId, false)) elog(ERROR, "Existing indexes are inactive. REINDEX first"); @@ -187,9 +189,9 @@ DefineIndex(char *heapRelationName, relationId, accessMethodName, accessMethodId); } - index_create(heapRelationName, indexRelationName, + index_create(relationId, indexRelationName, indexInfo, accessMethodId, classObjectId, - primary, allowSystemTableMods); + istemp, primary, allowSystemTableMods); /* * We update the relation's pg_class tuple even if it already has @@ -500,23 +502,25 @@ GetDefaultOpClass(Oid attrType, Oid accessMethodId) * ... */ void -RemoveIndex(char *name) +RemoveIndex(RangeVar *relation) { + Oid indOid; HeapTuple tuple; - tuple = SearchSysCache(RELNAME, - PointerGetDatum(name), + indOid = RangeVarGetRelid(relation, false); + tuple = SearchSysCache(RELOID, + ObjectIdGetDatum(indOid), 0, 0, 0); if (!HeapTupleIsValid(tuple)) - elog(ERROR, "index \"%s\" does not exist", name); + elog(ERROR, "index \"%s\" does not exist", relation->relname); if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_INDEX) elog(ERROR, "relation \"%s\" is of type \"%c\"", - name, ((Form_pg_class) GETSTRUCT(tuple))->relkind); - - index_drop(tuple->t_data->t_oid); + relation->relname, ((Form_pg_class) GETSTRUCT(tuple))->relkind); ReleaseSysCache(tuple); + + index_drop(indOid); } /* @@ -528,8 +532,9 @@ RemoveIndex(char *name) * ... */ void -ReindexIndex(const char *name, bool force /* currently unused */ ) +ReindexIndex(RangeVar *indexRelation, bool force /* currently unused */ ) { + Oid indOid; HeapTuple tuple; bool overwrite = false; @@ -541,22 +546,24 @@ ReindexIndex(const char *name, bool force /* currently unused */ ) if (IsTransactionBlock()) elog(ERROR, "REINDEX cannot run inside a BEGIN/END block"); - tuple = SearchSysCache(RELNAME, - PointerGetDatum(name), + indOid = RangeVarGetRelid(indexRelation, false); + tuple = SearchSysCache(RELOID, + ObjectIdGetDatum(indOid), 0, 0, 0); if (!HeapTupleIsValid(tuple)) - elog(ERROR, "index \"%s\" does not exist", name); + elog(ERROR, "index \"%s\" does not exist", indexRelation->relname); if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_INDEX) elog(ERROR, "relation \"%s\" is of type \"%c\"", - name, ((Form_pg_class) GETSTRUCT(tuple))->relkind); + indexRelation->relname, + ((Form_pg_class) GETSTRUCT(tuple))->relkind); + + ReleaseSysCache(tuple); if (IsIgnoringSystemIndexes()) overwrite = true; - if (!reindex_index(tuple->t_data->t_oid, force, overwrite)) - elog(WARNING, "index \"%s\" wasn't reindexed", name); - - ReleaseSysCache(tuple); + if (!reindex_index(indOid, force, overwrite)) + elog(WARNING, "index \"%s\" wasn't reindexed", indexRelation->relname); } /* @@ -568,8 +575,9 @@ ReindexIndex(const char *name, bool force /* currently unused */ ) * ... */ void -ReindexTable(const char *name, bool force) +ReindexTable(RangeVar *relation, bool force) { + Oid heapOid; HeapTuple tuple; /* @@ -580,20 +588,22 @@ ReindexTable(const char *name, bool force) if (IsTransactionBlock()) elog(ERROR, "REINDEX cannot run inside a BEGIN/END block"); - tuple = SearchSysCache(RELNAME, - PointerGetDatum(name), + heapOid = RangeVarGetRelid(relation, false); + tuple = SearchSysCache(RELOID, + ObjectIdGetDatum(heapOid), 0, 0, 0); if (!HeapTupleIsValid(tuple)) - elog(ERROR, "table \"%s\" does not exist", name); + elog(ERROR, "table \"%s\" does not exist", relation->relname); if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_RELATION) elog(ERROR, "relation \"%s\" is of type \"%c\"", - name, ((Form_pg_class) GETSTRUCT(tuple))->relkind); - - if (!reindex_relation(tuple->t_data->t_oid, force)) - elog(WARNING, "table \"%s\" wasn't reindexed", name); + relation->relname, + ((Form_pg_class) GETSTRUCT(tuple))->relkind); ReleaseSysCache(tuple); + + if (!reindex_relation(heapOid, force)) + elog(WARNING, "table \"%s\" wasn't reindexed", relation->relname); } /* diff --git a/src/backend/commands/rename.c b/src/backend/commands/rename.c index 0ce475303a7..f050aa9f7fc 100644 --- a/src/backend/commands/rename.c +++ b/src/backend/commands/rename.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.64 2002/03/21 23:27:21 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.65 2002/03/26 19:15:43 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -36,6 +36,7 @@ #include "utils/acl.h" #include "utils/builtins.h" #include "utils/fmgroids.h" +#include "utils/lsyscache.h" #include "utils/relcache.h" #include "utils/syscache.h" #include "utils/temprel.h" @@ -266,19 +267,20 @@ renameatt(char *relname, * renamerel - change the name of a relation */ void -renamerel(const char *oldrelname, const char *newrelname) +renamerel(const RangeVar *relation, const char *newrelname) { Relation targetrelation; Relation relrelation; /* for RELATION relation */ HeapTuple reltup; + Oid namespaceId; Oid reloid; char relkind; bool relhastriggers; Relation irelations[Num_pg_class_indices]; - if (!allowSystemTableMods && IsSystemRelationName(oldrelname)) + if (!allowSystemTableMods && IsSystemRelationName(relation->relname)) elog(ERROR, "renamerel: system relation \"%s\" may not be renamed", - oldrelname); + relation->relname); if (!allowSystemTableMods && IsSystemRelationName(newrelname)) elog(ERROR, "renamerel: Illegal class name: \"%s\" -- pg_ is reserved for system catalogs", @@ -288,15 +290,16 @@ renamerel(const char *oldrelname, const char *newrelname) * Check for renaming a temp table, which only requires altering the * temp-table mapping, not the underlying table. */ - if (rename_temp_relation(oldrelname, newrelname)) + if (rename_temp_relation(relation->relname, newrelname)) return; /* all done... */ /* * Grab an exclusive lock on the target table or index, which we will * NOT release until end of transaction. */ - targetrelation = relation_openr(oldrelname, AccessExclusiveLock); + targetrelation = relation_openrv(relation, AccessExclusiveLock); + namespaceId = RelationGetNamespace(targetrelation); reloid = RelationGetRelid(targetrelation); relkind = targetrelation->rd_rel->relkind; relhastriggers = (targetrelation->rd_rel->reltriggers > 0); @@ -323,13 +326,14 @@ renamerel(const char *oldrelname, const char *newrelname) */ relrelation = heap_openr(RelationRelationName, RowExclusiveLock); - reltup = SearchSysCacheCopy(RELNAME, - PointerGetDatum(oldrelname), + reltup = SearchSysCacheCopy(RELOID, + PointerGetDatum(reloid), 0, 0, 0); if (!HeapTupleIsValid(reltup)) - elog(ERROR, "renamerel: relation \"%s\" does not exist", oldrelname); + elog(ERROR, "renamerel: relation \"%s\" does not exist", + relation->relname); - if (RelnameFindRelid(newrelname) != InvalidOid) + if (get_relname_relid(newrelname, namespaceId) != InvalidOid) elog(ERROR, "renamerel: relation \"%s\" exists", newrelname); /* @@ -352,7 +356,7 @@ renamerel(const char *oldrelname, const char *newrelname) * Also rename the associated type, if any. */ if (relkind != RELKIND_INDEX) - TypeRename(oldrelname, newrelname); + TypeRename(relation->relname, newrelname); /* * If it's a view, must also rename the associated ON SELECT rule. @@ -362,7 +366,7 @@ renamerel(const char *oldrelname, const char *newrelname) char *oldrulename, *newrulename; - oldrulename = MakeRetrieveViewRuleName(oldrelname); + oldrulename = MakeRetrieveViewRuleName(relation->relname); newrulename = MakeRetrieveViewRuleName(newrelname); RenameRewriteRule(oldrulename, newrulename); } @@ -374,11 +378,11 @@ renamerel(const char *oldrelname, const char *newrelname) { /* update tgargs where relname is primary key */ update_ri_trigger_args(reloid, - oldrelname, newrelname, + relation->relname, newrelname, false, true); /* update tgargs where relname is foreign key */ update_ri_trigger_args(reloid, - oldrelname, newrelname, + relation->relname, newrelname, true, true); } } diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index 2fa17612e37..931ddeae7b0 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.107 2002/03/21 23:27:22 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.108 2002/03/26 19:15:45 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -71,7 +71,7 @@ CreateTrigger(CreateTrigStmt *stmt) char *constrname = ""; Oid constrrelid = InvalidOid; - rel = heap_openr(stmt->relation->relname, AccessExclusiveLock); + rel = heap_openrv(stmt->relation, AccessExclusiveLock); if (rel->rd_rel->relkind != RELKIND_RELATION) elog(ERROR, "CreateTrigger: relation \"%s\" is not a table", @@ -106,7 +106,7 @@ CreateTrigger(CreateTrigStmt *stmt) */ Relation conrel; - conrel = heap_openr(stmt->constrrel->relname, NoLock); + conrel = heap_openrv(stmt->constrrel, NoLock); constrrelid = conrel->rd_id; heap_close(conrel, NoLock); } @@ -285,8 +285,8 @@ CreateTrigger(CreateTrigStmt *stmt) * rebuild relcache entries. */ pgrel = heap_openr(RelationRelationName, RowExclusiveLock); - tuple = SearchSysCacheCopy(RELNAME, - PointerGetDatum(stmt->relation->relname), + tuple = SearchSysCacheCopy(RELOID, + ObjectIdGetDatum(RelationGetRelid(rel)), 0, 0, 0); if (!HeapTupleIsValid(tuple)) elog(ERROR, "CreateTrigger: relation %s not found in pg_class", @@ -323,7 +323,7 @@ DropTrigger(DropTrigStmt *stmt) int found = 0; int tgfound = 0; - rel = heap_openr(stmt->relation->relname, AccessExclusiveLock); + rel = heap_openrv(stmt->relation, AccessExclusiveLock); if (rel->rd_rel->relkind != RELKIND_RELATION) elog(ERROR, "DropTrigger: relation \"%s\" is not a table", @@ -380,8 +380,8 @@ DropTrigger(DropTrigStmt *stmt) * rebuild relcache entries. */ pgrel = heap_openr(RelationRelationName, RowExclusiveLock); - tuple = SearchSysCacheCopy(RELNAME, - PointerGetDatum(stmt->relation->relname), + tuple = SearchSysCacheCopy(RELOID, + ObjectIdGetDatum(RelationGetRelid(rel)), 0, 0, 0); if (!HeapTupleIsValid(tuple)) elog(ERROR, "DropTrigger: relation %s not found in pg_class", @@ -452,7 +452,7 @@ RelationRemoveTriggers(Relation rel) pgrel = heap_openr(RelationRelationName, RowExclusiveLock); tup = SearchSysCacheCopy(RELOID, - RelationGetRelid(rel), + ObjectIdGetDatum(RelationGetRelid(rel)), 0, 0, 0); if (!HeapTupleIsValid(tup)) elog(ERROR, "RelationRemoveTriggers: relation %u not found in pg_class", diff --git a/src/backend/commands/user.c b/src/backend/commands/user.c index 3de55ad71c0..98ee4308975 100644 --- a/src/backend/commands/user.c +++ b/src/backend/commands/user.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Header: /cvsroot/pgsql/src/backend/commands/user.c,v 1.93 2002/03/06 06:09:37 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/user.c,v 1.94 2002/03/26 19:15:48 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -919,12 +919,12 @@ DropUser(DropUserStmt *stmt) * check to see if there is an ACL on pg_shadow */ static void -CheckPgUserAclNotNull() +CheckPgUserAclNotNull(void) { HeapTuple htup; - htup = SearchSysCache(RELNAME, - PointerGetDatum(ShadowRelationName), + htup = SearchSysCache(RELOID, + ObjectIdGetDatum(RelOid_pg_shadow), 0, 0, 0); if (!HeapTupleIsValid(htup)) elog(ERROR, "CheckPgUserAclNotNull: \"%s\" not found", |