1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-25 13:17:41 +03:00

Standardize get_whatever_oid functions for object types with

unqualified names.

- Add a missing_ok parameter to get_tablespace_oid.
- Avoid duplicating get_tablespace_od guts in objectNamesToOids.
- Add a missing_ok parameter to get_database_oid.
- Replace get_roleid and get_role_checked with get_role_oid.
- Add get_namespace_oid, get_language_oid, get_am_oid.
- Refactor existing code to use new interfaces.

Thanks to KaiGai Kohei for the review.
This commit is contained in:
Robert Haas
2010-08-05 14:45:09 +00:00
parent 641459f269
commit 2a6ef3445c
25 changed files with 259 additions and 437 deletions

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/alter.c,v 1.37 2010/07/28 05:22:24 sriggs Exp $
* $PostgreSQL: pgsql/src/backend/commands/alter.c,v 1.38 2010/08/05 14:44:58 rhaas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -211,7 +211,7 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
void
ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
{
Oid newowner = get_roleid_checked(stmt->newowner);
Oid newowner = get_role_oid(stmt->newowner, false);
switch (stmt->objectType)
{

View File

@@ -7,7 +7,7 @@
* Copyright (c) 1996-2010, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/comment.c,v 1.115 2010/06/13 17:43:12 rhaas Exp $
* $PostgreSQL: pgsql/src/backend/commands/comment.c,v 1.116 2010/08/05 14:44:58 rhaas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -42,6 +42,8 @@
#include "catalog/pg_type.h"
#include "commands/comment.h"
#include "commands/dbcommands.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
#include "commands/tablespace.h"
#include "libpq/be-fsstubs.h"
#include "miscadmin.h"
@@ -686,11 +688,10 @@ CommentDatabase(List *qualname, char *comment)
* of the database. Erroring out would prevent pg_restore from completing
* (which is really pg_restore's fault, but for now we will work around
* the problem here). Consensus is that the best fix is to treat wrong
* database name as a WARNING not an ERROR.
* database name as a WARNING not an ERROR (thus, we tell get_database_oid
* to ignore the error so that we can handle it differently here).
*/
/* First get the database OID */
oid = get_database_oid(database);
oid = get_database_oid(database, true);
if (!OidIsValid(oid))
{
ereport(WARNING,
@@ -729,14 +730,7 @@ CommentTablespace(List *qualname, char *comment)
errmsg("tablespace name cannot be qualified")));
tablespace = strVal(linitial(qualname));
oid = get_tablespace_oid(tablespace);
if (!OidIsValid(oid))
{
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("tablespace \"%s\" does not exist", tablespace)));
return;
}
oid = get_tablespace_oid(tablespace, false);
/* Check object security */
if (!pg_tablespace_ownercheck(oid, GetUserId()))
@@ -766,7 +760,7 @@ CommentRole(List *qualname, char *comment)
errmsg("role name cannot be qualified")));
role = strVal(linitial(qualname));
oid = get_roleid_checked(role);
oid = get_role_oid(role, false);
/* Check object security */
if (!has_privs_of_role(GetUserId(), oid))
@@ -799,11 +793,7 @@ CommentNamespace(List *qualname, char *comment)
errmsg("schema name cannot be qualified")));
namespace = strVal(linitial(qualname));
oid = GetSysCacheOid1(NAMESPACENAME, CStringGetDatum(namespace));
if (!OidIsValid(oid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_SCHEMA),
errmsg("schema \"%s\" does not exist", namespace)));
oid = get_namespace_oid(namespace, false);
/* Check object security */
if (!pg_namespace_ownercheck(oid, GetUserId()))
@@ -1213,11 +1203,7 @@ CommentLanguage(List *qualname, char *comment)
errmsg("language name cannot be qualified")));
language = strVal(linitial(qualname));
oid = GetSysCacheOid1(LANGNAME, CStringGetDatum(language));
if (!OidIsValid(oid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_SCHEMA),
errmsg("language \"%s\" does not exist", language)));
oid = get_language_oid(language, false);
/* Check object security */
if (!superuser())
@@ -1254,12 +1240,7 @@ CommentOpClass(List *qualname, List *arguments, char *comment)
/*
* Get the access method's OID.
*/
amID = GetSysCacheOid1(AMNAME, CStringGetDatum(amname));
if (!OidIsValid(amID))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("access method \"%s\" does not exist",
amname)));
amID = get_am_oid(amname, false);
/*
* Look up the opclass.
@@ -1335,12 +1316,7 @@ CommentOpFamily(List *qualname, List *arguments, char *comment)
/*
* Get the access method's OID.
*/
amID = GetSysCacheOid1(AMNAME, CStringGetDatum(amname));
if (!OidIsValid(amID))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("access method \"%s\" does not exist",
amname)));
amID = get_am_oid(amname, false);
/*
* Look up the opfamily.

View File

@@ -13,7 +13,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.236 2010/07/20 18:14:16 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.237 2010/08/05 14:45:00 rhaas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -255,7 +255,7 @@ createdb(const CreatedbStmt *stmt)
/* obtain OID of proposed owner */
if (dbowner)
datdba = get_roleid_checked(dbowner);
datdba = get_role_oid(dbowner, false);
else
datdba = GetUserId();
@@ -429,12 +429,7 @@ createdb(const CreatedbStmt *stmt)
AclResult aclresult;
tablespacename = strVal(dtablespacename->arg);
dst_deftablespace = get_tablespace_oid(tablespacename);
if (!OidIsValid(dst_deftablespace))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("tablespace \"%s\" does not exist",
tablespacename)));
dst_deftablespace = get_tablespace_oid(tablespacename, false);
/* check permissions */
aclresult = pg_tablespace_aclcheck(dst_deftablespace, GetUserId(),
ACL_CREATE);
@@ -491,7 +486,7 @@ createdb(const CreatedbStmt *stmt)
* message than "unique index violation". There's a race condition but
* we're willing to accept the less friendly message in that case.
*/
if (OidIsValid(get_database_oid(dbname)))
if (OidIsValid(get_database_oid(dbname, true)))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_DATABASE),
errmsg("database \"%s\" already exists", dbname)));
@@ -919,7 +914,7 @@ RenameDatabase(const char *oldname, const char *newname)
* Make sure the new name doesn't exist. See notes for same error in
* CREATE DATABASE.
*/
if (OidIsValid(get_database_oid(newname)))
if (OidIsValid(get_database_oid(newname, true)))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_DATABASE),
errmsg("database \"%s\" already exists", newname)));
@@ -1030,11 +1025,7 @@ movedb(const char *dbname, const char *tblspcname)
/*
* Get tablespace's oid
*/
dst_tblspcoid = get_tablespace_oid(tblspcname);
if (dst_tblspcoid == InvalidOid)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_DATABASE),
errmsg("tablespace \"%s\" does not exist", tblspcname)));
dst_tblspcoid = get_tablespace_oid(tblspcname, false);
/*
* Permission checks
@@ -1402,12 +1393,7 @@ AlterDatabase(AlterDatabaseStmt *stmt, bool isTopLevel)
void
AlterDatabaseSet(AlterDatabaseSetStmt *stmt)
{
Oid datid = get_database_oid(stmt->dbname);
if (!OidIsValid(datid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_DATABASE),
errmsg("database \"%s\" does not exist", stmt->dbname)));
Oid datid = get_database_oid(stmt->dbname, false);
/*
* Obtain a lock on the database and make sure it didn't go away in the
@@ -1818,10 +1804,11 @@ errdetail_busy_db(int notherbackends, int npreparedxacts)
/*
* get_database_oid - given a database name, look up the OID
*
* Returns InvalidOid if database name not found.
* If missing_ok is false, throw an error if database name not found. If
* true, just return InvalidOid.
*/
Oid
get_database_oid(const char *dbname)
get_database_oid(const char *dbname, bool missing_ok)
{
Relation pg_database;
ScanKeyData entry[1];
@@ -1852,6 +1839,12 @@ get_database_oid(const char *dbname)
systable_endscan(scan);
heap_close(pg_database, AccessShareLock);
if (!OidIsValid(oid) && !missing_ok)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_DATABASE),
errmsg("database \"%s\" does not exist",
dbname)));
return oid;
}

View File

@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/foreigncmds.c,v 1.11 2010/02/14 18:42:14 rhaas Exp $
* $PostgreSQL: pgsql/src/backend/commands/foreigncmds.c,v 1.12 2010/08/05 14:45:00 rhaas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -184,7 +184,7 @@ GetUserOidFromMapping(const char *username, bool missing_ok)
return GetUserId();
/* map to provided user */
return missing_ok ? get_roleid(username) : get_roleid_checked(username);
return get_role_oid(username, missing_ok);
}

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.198 2010/07/06 19:18:56 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.199 2010/08/05 14:45:00 rhaas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -218,12 +218,7 @@ DefineIndex(RangeVar *heapRelation,
*/
if (tableSpaceName)
{
tablespaceId = get_tablespace_oid(tableSpaceName);
if (!OidIsValid(tablespaceId))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("tablespace \"%s\" does not exist",
tableSpaceName)));
tablespaceId = get_tablespace_oid(tableSpaceName, false);
}
else
{

View File

@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/opclasscmds.c,v 1.69 2010/07/16 00:13:23 tgl Exp $
* $PostgreSQL: pgsql/src/backend/commands/opclasscmds.c,v 1.70 2010/08/05 14:45:01 rhaas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -641,7 +641,6 @@ DefineOpFamily(CreateOpFamilyStmt *stmt)
char *opfname; /* name of opfamily we're creating */
Oid amoid, /* our AM's oid */
namespaceoid; /* namespace to create opfamily in */
HeapTuple tup;
AclResult aclresult;
/* Convert list of names to a name and namespace */
@@ -654,20 +653,11 @@ DefineOpFamily(CreateOpFamilyStmt *stmt)
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
get_namespace_name(namespaceoid));
/* Get necessary info about access method */
tup = SearchSysCache1(AMNAME, CStringGetDatum(stmt->amname));
if (!HeapTupleIsValid(tup))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("access method \"%s\" does not exist",
stmt->amname)));
amoid = HeapTupleGetOid(tup);
/* Get access method OID, throwing an error if it doesn't exist. */
amoid = get_am_oid(stmt->amname, false);
/* XXX Should we make any privilege check against the AM? */
ReleaseSysCache(tup);
/*
* Currently, we require superuser privileges to create an opfamily. See
* comments in DefineOpClass.
@@ -1427,12 +1417,7 @@ RemoveOpClass(RemoveOpClassStmt *stmt)
/*
* Get the access method's OID.
*/
amID = GetSysCacheOid1(AMNAME, CStringGetDatum(stmt->amname));
if (!OidIsValid(amID))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("access method \"%s\" does not exist",
stmt->amname)));
amID = get_am_oid(stmt->amname, false);
/*
* Look up the opclass.
@@ -1488,12 +1473,7 @@ RemoveOpFamily(RemoveOpFamilyStmt *stmt)
/*
* Get the access method's OID.
*/
amID = GetSysCacheOid1(AMNAME, CStringGetDatum(stmt->amname));
if (!OidIsValid(amID))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("access method \"%s\" does not exist",
stmt->amname)));
amID = get_am_oid(stmt->amname, false);
/*
* Look up the opfamily.
@@ -1650,12 +1630,7 @@ RenameOpClass(List *name, const char *access_method, const char *newname)
Relation rel;
AclResult aclresult;
amOid = GetSysCacheOid1(AMNAME, CStringGetDatum(access_method));
if (!OidIsValid(amOid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("access method \"%s\" does not exist",
access_method)));
amOid = get_am_oid(access_method, false);
rel = heap_open(OperatorClassRelationId, RowExclusiveLock);
@@ -1744,12 +1719,7 @@ RenameOpFamily(List *name, const char *access_method, const char *newname)
Relation rel;
AclResult aclresult;
amOid = GetSysCacheOid1(AMNAME, CStringGetDatum(access_method));
if (!OidIsValid(amOid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("access method \"%s\" does not exist",
access_method)));
amOid = get_am_oid(access_method, false);
rel = heap_open(OperatorFamilyRelationId, RowExclusiveLock);
@@ -1835,12 +1805,7 @@ AlterOpClassOwner(List *name, const char *access_method, Oid newOwnerId)
char *opcname;
char *schemaname;
amOid = GetSysCacheOid1(AMNAME, CStringGetDatum(access_method));
if (!OidIsValid(amOid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("access method \"%s\" does not exist",
access_method)));
amOid = get_am_oid(access_method, false);
rel = heap_open(OperatorClassRelationId, RowExclusiveLock);
@@ -1978,12 +1943,7 @@ AlterOpFamilyOwner(List *name, const char *access_method, Oid newOwnerId)
char *opfname;
char *schemaname;
amOid = GetSysCacheOid1(AMNAME, CStringGetDatum(access_method));
if (!OidIsValid(amOid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("access method \"%s\" does not exist",
access_method)));
amOid = get_am_oid(access_method, false);
rel = heap_open(OperatorFamilyRelationId, RowExclusiveLock);
@@ -2108,3 +2068,22 @@ AlterOpFamilyOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
newOwnerId);
}
}
/*
* get_am_oid - given an access method name, look up the OID
*
* If missing_ok is false, throw an error if access method not found. If
* true, just return InvalidOid.
*/
Oid
get_am_oid(const char *amname, bool missing_ok)
{
Oid oid;
oid = GetSysCacheOid1(AMNAME, CStringGetDatum(amname));
if (!OidIsValid(oid) && !missing_ok)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("access method \"%s\" does not exist", amname)));
return oid;
}

View File

@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/proclang.c,v 1.91 2010/02/26 02:00:39 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/proclang.c,v 1.92 2010/08/05 14:45:01 rhaas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -514,7 +514,7 @@ void
DropProceduralLanguage(DropPLangStmt *stmt)
{
char *languageName;
HeapTuple langTup;
Oid oid;
ObjectAddress object;
/*
@@ -522,34 +522,26 @@ DropProceduralLanguage(DropPLangStmt *stmt)
*/
languageName = case_translate_language_name(stmt->plname);
langTup = SearchSysCache1(LANGNAME, CStringGetDatum(languageName));
if (!HeapTupleIsValid(langTup))
oid = get_language_oid(languageName, stmt->missing_ok);
if (!OidIsValid(oid))
{
if (!stmt->missing_ok)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("language \"%s\" does not exist", languageName)));
else
ereport(NOTICE,
(errmsg("language \"%s\" does not exist, skipping",
languageName)));
ereport(NOTICE,
(errmsg("language \"%s\" does not exist, skipping",
languageName)));
return;
}
/*
* Check permission
*/
if (!pg_language_ownercheck(HeapTupleGetOid(langTup), GetUserId()))
if (!pg_language_ownercheck(oid, GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_LANGUAGE,
languageName);
object.classId = LanguageRelationId;
object.objectId = HeapTupleGetOid(langTup);
object.objectId = oid;
object.objectSubId = 0;
ReleaseSysCache(langTup);
/*
* Do the deletion
*/
@@ -735,3 +727,22 @@ AlterLanguageOwner_internal(HeapTuple tup, Relation rel, Oid newOwnerId)
newOwnerId);
}
}
/*
* get_language_oid - given a language name, look up the OID
*
* If missing_ok is false, throw an error if language name not found. If
* true, just return InvalidOid.
*/
Oid
get_language_oid(const char *langname, bool missing_ok)
{
Oid oid;
oid = GetSysCacheOid1(LANGNAME, CStringGetDatum(langname));
if (!OidIsValid(oid) && !missing_ok)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("language \"%s\" does not exist", langname)));
return oid;
}

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/schemacmds.c,v 1.57 2010/02/26 02:00:39 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/schemacmds.c,v 1.58 2010/08/05 14:45:01 rhaas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -57,7 +57,7 @@ CreateSchemaCommand(CreateSchemaStmt *stmt, const char *queryString)
* Who is supposed to own the new schema?
*/
if (authId)
owner_uid = get_roleid_checked(authId);
owner_uid = get_role_oid(authId, false);
else
owner_uid = saved_uid;
@@ -178,24 +178,13 @@ RemoveSchemas(DropStmt *drop)
errmsg("schema name cannot be qualified")));
namespaceName = strVal(linitial(names));
namespaceId = GetSysCacheOid1(NAMESPACENAME,
CStringGetDatum(namespaceName));
namespaceId = get_namespace_oid(namespaceName, drop->missing_ok);
if (!OidIsValid(namespaceId))
{
if (!drop->missing_ok)
{
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_SCHEMA),
errmsg("schema \"%s\" does not exist",
namespaceName)));
}
else
{
ereport(NOTICE,
(errmsg("schema \"%s\" does not exist, skipping",
namespaceName)));
}
ereport(NOTICE,
(errmsg("schema \"%s\" does not exist, skipping",
namespaceName)));
continue;
}
@@ -264,9 +253,7 @@ RenameSchema(const char *oldname, const char *newname)
errmsg("schema \"%s\" does not exist", oldname)));
/* make sure the new name doesn't exist */
if (HeapTupleIsValid(
SearchSysCache1(NAMESPACENAME,
CStringGetDatum(newname))))
if (OidIsValid(get_namespace_oid(newname, true)))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_SCHEMA),
errmsg("schema \"%s\" already exists", newname)));

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.338 2010/08/03 15:47:02 rhaas Exp $
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.339 2010/08/05 14:45:01 rhaas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -414,12 +414,7 @@ DefineRelation(CreateStmt *stmt, char relkind)
*/
if (stmt->tablespacename)
{
tablespaceId = get_tablespace_oid(stmt->tablespacename);
if (!OidIsValid(tablespaceId))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("tablespace \"%s\" does not exist",
stmt->tablespacename)));
tablespaceId = get_tablespace_oid(stmt->tablespacename, false);
}
else
{
@@ -2941,7 +2936,7 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
break;
case AT_ChangeOwner: /* ALTER OWNER */
ATExecChangeOwner(RelationGetRelid(rel),
get_roleid_checked(cmd->name),
get_role_oid(cmd->name, false),
false, lockmode);
break;
case AT_ClusterOn: /* CLUSTER ON */
@@ -6945,11 +6940,7 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, char *tablespacename, L
AclResult aclresult;
/* Check that the tablespace exists */
tablespaceId = get_tablespace_oid(tablespacename);
if (!OidIsValid(tablespaceId))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("tablespace \"%s\" does not exist", tablespacename)));
tablespaceId = get_tablespace_oid(tablespacename, false);
/* Check its permissions */
aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(), ACL_CREATE);

View File

@@ -40,7 +40,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.78 2010/07/20 18:14:16 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.79 2010/08/05 14:45:01 rhaas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -246,7 +246,7 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
/* However, the eventual owner of the tablespace need not be */
if (stmt->owner)
ownerId = get_roleid_checked(stmt->owner);
ownerId = get_role_oid(stmt->owner, false);
else
ownerId = GetUserId();
@@ -298,7 +298,7 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
* index would catch this anyway, but might as well give a friendlier
* message.)
*/
if (OidIsValid(get_tablespace_oid(stmt->tablespacename)))
if (OidIsValid(get_tablespace_oid(stmt->tablespacename, true)))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg("tablespace \"%s\" already exists",
@@ -1029,7 +1029,7 @@ assign_default_tablespace(const char *newval, bool doit, GucSource source)
if (IsTransactionState())
{
if (newval[0] != '\0' &&
!OidIsValid(get_tablespace_oid(newval)))
!OidIsValid(get_tablespace_oid(newval, true)))
{
ereport(GUC_complaint_elevel(source),
(errcode(ERRCODE_UNDEFINED_OBJECT),
@@ -1079,7 +1079,7 @@ GetDefaultTablespace(bool forTemp)
* to refer to an existing tablespace; we just silently return InvalidOid,
* causing the new object to be created in the database's tablespace.
*/
result = get_tablespace_oid(default_tablespace);
result = get_tablespace_oid(default_tablespace, true);
/*
* Allow explicit specification of database's default tablespace in
@@ -1146,21 +1146,13 @@ assign_temp_tablespaces(const char *newval, bool doit, GucSource source)
continue;
}
/* Else verify that name is a valid tablespace name */
curoid = get_tablespace_oid(curname);
/*
* In an interactive SET command, we ereport for bad info.
* Otherwise, silently ignore any bad list elements.
*/
curoid = get_tablespace_oid(curname, source < PGC_S_INTERACTIVE);
if (curoid == InvalidOid)
{
/*
* In an interactive SET command, we ereport for bad info.
* Otherwise, silently ignore any bad list elements.
*/
if (source >= PGC_S_INTERACTIVE)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("tablespace \"%s\" does not exist",
curname)));
continue;
}
/*
* Allow explicit specification of database's default tablespace
@@ -1259,10 +1251,10 @@ PrepareTempTablespaces(void)
}
/* Else verify that name is a valid tablespace name */
curoid = get_tablespace_oid(curname);
curoid = get_tablespace_oid(curname, true);
if (curoid == InvalidOid)
{
/* Silently ignore any bad list elements */
/* Skip any bad list elements */
continue;
}
@@ -1295,10 +1287,11 @@ PrepareTempTablespaces(void)
/*
* get_tablespace_oid - given a tablespace name, look up the OID
*
* Returns InvalidOid if tablespace name not found.
* If missing_ok is false, throw an error if tablespace name not found. If
* true, just return InvalidOid.
*/
Oid
get_tablespace_oid(const char *tablespacename)
get_tablespace_oid(const char *tablespacename, bool missing_ok)
{
Oid result;
Relation rel;
@@ -1329,6 +1322,12 @@ get_tablespace_oid(const char *tablespacename)
heap_endscan(scandesc);
heap_close(rel, AccessShareLock);
if (!OidIsValid(result) && !missing_ok)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("tablespace \"%s\" does not exist",
tablespacename)));
return result;
}

View File

@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.193 2010/02/26 02:00:40 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.194 2010/08/05 14:45:01 rhaas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -293,8 +293,7 @@ CreateRole(CreateRoleStmt *stmt)
pg_authid_rel = heap_open(AuthIdRelationId, RowExclusiveLock);
pg_authid_dsc = RelationGetDescr(pg_authid_rel);
tuple = SearchSysCache1(AUTHNAME, PointerGetDatum(stmt->role));
if (HeapTupleIsValid(tuple))
if (OidIsValid(get_role_oid(stmt->role, true)))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg("role \"%s\" already exists",
@@ -384,7 +383,7 @@ CreateRole(CreateRoleStmt *stmt)
foreach(item, addroleto)
{
char *oldrolename = strVal(lfirst(item));
Oid oldroleid = get_roleid_checked(oldrolename);
Oid oldroleid = get_role_oid(oldrolename, false);
AddRoleMems(oldrolename, oldroleid,
list_make1(makeString(stmt->role)),
@@ -795,11 +794,7 @@ AlterRoleSet(AlterRoleSetStmt *stmt)
/* look up and lock the database, if specified */
if (stmt->database != NULL)
{
databaseid = get_database_oid(stmt->database);
if (!OidIsValid(databaseid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("database \"%s\" not found", stmt->database)));
databaseid = get_database_oid(stmt->database, false);
shdepLockAndCheckObject(DatabaseRelationId, databaseid);
}
@@ -1099,7 +1094,7 @@ GrantRole(GrantRoleStmt *stmt)
ListCell *item;
if (stmt->grantor)
grantor = get_roleid_checked(stmt->grantor);
grantor = get_role_oid(stmt->grantor, false);
else
grantor = GetUserId();
@@ -1127,7 +1122,7 @@ GrantRole(GrantRoleStmt *stmt)
(errcode(ERRCODE_INVALID_GRANT_OPERATION),
errmsg("column names cannot be included in GRANT/REVOKE ROLE")));
roleid = get_roleid_checked(rolename);
roleid = get_role_oid(rolename, false);
if (stmt->is_grant)
AddRoleMems(rolename, roleid,
stmt->grantee_roles, grantee_ids,
@@ -1194,7 +1189,7 @@ ReassignOwnedObjects(ReassignOwnedStmt *stmt)
}
/* Must have privileges on the receiving side too */
newrole = get_roleid_checked(stmt->newrole);
newrole = get_role_oid(stmt->newrole, false);
if (!has_privs_of_role(GetUserId(), newrole))
ereport(ERROR,
@@ -1220,7 +1215,7 @@ roleNamesToIds(List *memberNames)
foreach(l, memberNames)
{
char *rolename = strVal(lfirst(l));
Oid roleid = get_roleid_checked(rolename);
Oid roleid = get_role_oid(rolename, false);
result = lappend_oid(result, roleid);
}