Skip to content

Commit 2a6ef34

Browse files
committed
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.
1 parent 641459f commit 2a6ef34

File tree

25 files changed

+259
-437
lines changed

25 files changed

+259
-437
lines changed

src/backend/catalog/aclchk.c

Lines changed: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/aclchk.c,v 1.168 2010/07/06 19:18:55 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/aclchk.c,v 1.169 2010/08/05 14:44:58 rhaas Exp $
1212
*
1313
* NOTES
1414
* See acl.h.
@@ -43,6 +43,8 @@
4343
#include "catalog/pg_ts_config.h"
4444
#include "catalog/pg_ts_dict.h"
4545
#include "commands/dbcommands.h"
46+
#include "commands/proclang.h"
47+
#include "commands/tablespace.h"
4648
#include "foreign/foreign.h"
4749
#include "miscadmin.h"
4850
#include "parser/parse_func.h"
@@ -419,7 +421,7 @@ ExecuteGrantStmt(GrantStmt *stmt)
419421
else
420422
istmt.grantees =
421423
lappend_oid(istmt.grantees,
422-
get_roleid_checked(grantee->rolname));
424+
get_role_oid(grantee->rolname, false));
423425
}
424426

425427
/*
@@ -607,12 +609,7 @@ objectNamesToOids(GrantObjectType objtype, List *objnames)
607609
char *dbname = strVal(lfirst(cell));
608610
Oid dbid;
609611

610-
dbid = get_database_oid(dbname);
611-
if (!OidIsValid(dbid))
612-
ereport(ERROR,
613-
(errcode(ERRCODE_UNDEFINED_DATABASE),
614-
errmsg("database \"%s\" does not exist",
615-
dbname)));
612+
dbid = get_database_oid(dbname, false);
616613
objects = lappend_oid(objects, dbid);
617614
}
618615
break;
@@ -631,18 +628,10 @@ objectNamesToOids(GrantObjectType objtype, List *objnames)
631628
foreach(cell, objnames)
632629
{
633630
char *langname = strVal(lfirst(cell));
634-
HeapTuple tuple;
635-
636-
tuple = SearchSysCache1(LANGNAME, PointerGetDatum(langname));
637-
if (!HeapTupleIsValid(tuple))
638-
ereport(ERROR,
639-
(errcode(ERRCODE_UNDEFINED_OBJECT),
640-
errmsg("language \"%s\" does not exist",
641-
langname)));
631+
Oid oid;
642632

643-
objects = lappend_oid(objects, HeapTupleGetOid(tuple));
644-
645-
ReleaseSysCache(tuple);
633+
oid = get_language_oid(langname, false);
634+
objects = lappend_oid(objects, oid);
646635
}
647636
break;
648637
case ACL_OBJECT_LARGEOBJECT:
@@ -663,49 +652,20 @@ objectNamesToOids(GrantObjectType objtype, List *objnames)
663652
foreach(cell, objnames)
664653
{
665654
char *nspname = strVal(lfirst(cell));
666-
HeapTuple tuple;
667-
668-
tuple = SearchSysCache1(NAMESPACENAME,
669-
CStringGetDatum(nspname));
670-
if (!HeapTupleIsValid(tuple))
671-
ereport(ERROR,
672-
(errcode(ERRCODE_UNDEFINED_SCHEMA),
673-
errmsg("schema \"%s\" does not exist",
674-
nspname)));
675-
676-
objects = lappend_oid(objects, HeapTupleGetOid(tuple));
655+
Oid oid;
677656

678-
ReleaseSysCache(tuple);
657+
oid = get_namespace_oid(nspname, false);
658+
objects = lappend_oid(objects, oid);
679659
}
680660
break;
681661
case ACL_OBJECT_TABLESPACE:
682662
foreach(cell, objnames)
683663
{
684664
char *spcname = strVal(lfirst(cell));
685-
ScanKeyData entry[1];
686-
HeapScanDesc scan;
687-
HeapTuple tuple;
688-
Relation relation;
665+
Oid spcoid;
689666

690-
relation = heap_open(TableSpaceRelationId, AccessShareLock);
691-
692-
ScanKeyInit(&entry[0],
693-
Anum_pg_tablespace_spcname,
694-
BTEqualStrategyNumber, F_NAMEEQ,
695-
CStringGetDatum(spcname));
696-
697-
scan = heap_beginscan(relation, SnapshotNow, 1, entry);
698-
tuple = heap_getnext(scan, ForwardScanDirection);
699-
if (!HeapTupleIsValid(tuple))
700-
ereport(ERROR,
701-
(errcode(ERRCODE_UNDEFINED_OBJECT),
702-
errmsg("tablespace \"%s\" does not exist", spcname)));
703-
704-
objects = lappend_oid(objects, HeapTupleGetOid(tuple));
705-
706-
heap_endscan(scan);
707-
708-
heap_close(relation, AccessShareLock);
667+
spcoid = get_tablespace_oid(spcname, false);
668+
objects = lappend_oid(objects, spcoid);
709669
}
710670
break;
711671
case ACL_OBJECT_FDW:
@@ -913,7 +873,7 @@ ExecAlterDefaultPrivilegesStmt(AlterDefaultPrivilegesStmt *stmt)
913873
else
914874
iacls.grantees =
915875
lappend_oid(iacls.grantees,
916-
get_roleid_checked(grantee->rolname));
876+
get_role_oid(grantee->rolname, false));
917877
}
918878

919879
/*
@@ -996,7 +956,7 @@ ExecAlterDefaultPrivilegesStmt(AlterDefaultPrivilegesStmt *stmt)
996956
{
997957
char *rolename = strVal(lfirst(rolecell));
998958

999-
iacls.roleid = get_roleid_checked(rolename);
959+
iacls.roleid = get_role_oid(rolename, false);
1000960

1001961
/*
1002962
* We insist that calling user be a member of each target role. If
@@ -1037,18 +997,12 @@ SetDefaultACLsInSchemas(InternalDefaultACL *iacls, List *nspnames)
1037997
AclResult aclresult;
1038998

1039999
/*
1040-
* Normally we'd use LookupCreationNamespace here, but it's
1041-
* important to do the permissions check against the target role
1042-
* not the calling user, so write it out in full. We require
1043-
* CREATE privileges, since without CREATE you won't be able to do
1044-
* anything using the default privs anyway.
1000+
* Note that we must do the permissions check against the target
1001+
* role not the calling user. We require CREATE privileges,
1002+
* since without CREATE you won't be able to do anything using the
1003+
* default privs anyway.
10451004
*/
1046-
iacls->nspid = GetSysCacheOid1(NAMESPACENAME,
1047-
CStringGetDatum(nspname));
1048-
if (!OidIsValid(iacls->nspid))
1049-
ereport(ERROR,
1050-
(errcode(ERRCODE_UNDEFINED_SCHEMA),
1051-
errmsg("schema \"%s\" does not exist", nspname)));
1005+
iacls->nspid = get_namespace_oid(nspname, false);
10521006

10531007
aclresult = pg_namespace_aclcheck(iacls->nspid, iacls->roleid,
10541008
ACL_CREATE);

src/backend/catalog/namespace.c

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
1515
* IDENTIFICATION
16-
* $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.125 2010/02/26 02:00:36 momjian Exp $
16+
* $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.126 2010/08/05 14:44:58 rhaas Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -332,13 +332,7 @@ RangeVarGetCreationNamespace(const RangeVar *newRelation)
332332
return myTempNamespace;
333333
}
334334
/* use exact schema given */
335-
namespaceId = GetSysCacheOid1(NAMESPACENAME,
336-
CStringGetDatum(newRelation->schemaname));
337-
if (!OidIsValid(namespaceId))
338-
ereport(ERROR,
339-
(errcode(ERRCODE_UNDEFINED_SCHEMA),
340-
errmsg("schema \"%s\" does not exist",
341-
newRelation->schemaname)));
335+
namespaceId = get_namespace_oid(newRelation->schemaname, false);
342336
/* we do not check for USAGE rights here! */
343337
}
344338
else
@@ -2270,7 +2264,7 @@ LookupNamespaceNoError(const char *nspname)
22702264
return InvalidOid;
22712265
}
22722266

2273-
return GetSysCacheOid1(NAMESPACENAME, CStringGetDatum(nspname));
2267+
return get_namespace_oid(nspname, true);
22742268
}
22752269

22762270
/*
@@ -2300,11 +2294,7 @@ LookupExplicitNamespace(const char *nspname)
23002294
*/
23012295
}
23022296

2303-
namespaceId = GetSysCacheOid1(NAMESPACENAME, CStringGetDatum(nspname));
2304-
if (!OidIsValid(namespaceId))
2305-
ereport(ERROR,
2306-
(errcode(ERRCODE_UNDEFINED_SCHEMA),
2307-
errmsg("schema \"%s\" does not exist", nspname)));
2297+
namespaceId = get_namespace_oid(nspname, false);
23082298

23092299
aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE);
23102300
if (aclresult != ACLCHECK_OK)
@@ -2339,11 +2329,7 @@ LookupCreationNamespace(const char *nspname)
23392329
return myTempNamespace;
23402330
}
23412331

2342-
namespaceId = GetSysCacheOid1(NAMESPACENAME, CStringGetDatum(nspname));
2343-
if (!OidIsValid(namespaceId))
2344-
ereport(ERROR,
2345-
(errcode(ERRCODE_UNDEFINED_SCHEMA),
2346-
errmsg("schema \"%s\" does not exist", nspname)));
2332+
namespaceId = get_namespace_oid(nspname, false);
23472333

23482334
aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
23492335
if (aclresult != ACLCHECK_OK)
@@ -2385,12 +2371,7 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p)
23852371
return myTempNamespace;
23862372
}
23872373
/* use exact schema given */
2388-
namespaceId = GetSysCacheOid1(NAMESPACENAME,
2389-
CStringGetDatum(schemaname));
2390-
if (!OidIsValid(namespaceId))
2391-
ereport(ERROR,
2392-
(errcode(ERRCODE_UNDEFINED_SCHEMA),
2393-
errmsg("schema \"%s\" does not exist", schemaname)));
2374+
namespaceId = get_namespace_oid(schemaname, false);
23942375
/* we do not check for USAGE rights here! */
23952376
}
23962377
else
@@ -2413,6 +2394,26 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p)
24132394
return namespaceId;
24142395
}
24152396

2397+
/*
2398+
* get_namespace_oid - given a namespace name, look up the OID
2399+
*
2400+
* If missing_ok is false, throw an error if namespace name not found. If
2401+
* true, just return InvalidOid.
2402+
*/
2403+
Oid
2404+
get_namespace_oid(const char *nspname, bool missing_ok)
2405+
{
2406+
Oid oid;
2407+
2408+
oid = GetSysCacheOid1(NAMESPACENAME, CStringGetDatum(nspname));
2409+
if (!OidIsValid(oid) && !missing_ok)
2410+
ereport(ERROR,
2411+
(errcode(ERRCODE_UNDEFINED_SCHEMA),
2412+
errmsg("schema \"%s\" does not exist", nspname)));
2413+
2414+
return oid;
2415+
}
2416+
24162417
/*
24172418
* makeRangeVarFromNameList
24182419
* Utility routine to convert a qualified-name list into RangeVar form.
@@ -2897,8 +2898,7 @@ recomputeNamespacePath(void)
28972898
char *rname;
28982899

28992900
rname = NameStr(((Form_pg_authid) GETSTRUCT(tuple))->rolname);
2900-
namespaceId = GetSysCacheOid1(NAMESPACENAME,
2901-
CStringGetDatum(rname));
2901+
namespaceId = get_namespace_oid(rname, true);
29022902
ReleaseSysCache(tuple);
29032903
if (OidIsValid(namespaceId) &&
29042904
!list_member_oid(oidlist, namespaceId) &&
@@ -2925,8 +2925,7 @@ recomputeNamespacePath(void)
29252925
else
29262926
{
29272927
/* normal namespace reference */
2928-
namespaceId = GetSysCacheOid1(NAMESPACENAME,
2929-
CStringGetDatum(curname));
2928+
namespaceId = get_namespace_oid(curname, true);
29302929
if (OidIsValid(namespaceId) &&
29312930
!list_member_oid(oidlist, namespaceId) &&
29322931
pg_namespace_aclcheck(namespaceId, roleid,
@@ -3033,8 +3032,7 @@ InitTempTableNamespace(void)
30333032

30343033
snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d", MyBackendId);
30353034

3036-
namespaceId = GetSysCacheOid1(NAMESPACENAME,
3037-
CStringGetDatum(namespaceName));
3035+
namespaceId = get_namespace_oid(namespaceName, true);
30383036
if (!OidIsValid(namespaceId))
30393037
{
30403038
/*
@@ -3066,8 +3064,7 @@ InitTempTableNamespace(void)
30663064
snprintf(namespaceName, sizeof(namespaceName), "pg_toast_temp_%d",
30673065
MyBackendId);
30683066

3069-
toastspaceId = GetSysCacheOid1(NAMESPACENAME,
3070-
CStringGetDatum(namespaceName));
3067+
toastspaceId = get_namespace_oid(namespaceName, true);
30713068
if (!OidIsValid(toastspaceId))
30723069
{
30733070
toastspaceId = NamespaceCreate(namespaceName, BOOTSTRAP_SUPERUSERID);

src/backend/commands/alter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/alter.c,v 1.37 2010/07/28 05:22:24 sriggs Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/alter.c,v 1.38 2010/08/05 14:44:58 rhaas Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -211,7 +211,7 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
211211
void
212212
ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
213213
{
214-
Oid newowner = get_roleid_checked(stmt->newowner);
214+
Oid newowner = get_role_oid(stmt->newowner, false);
215215

216216
switch (stmt->objectType)
217217
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy