88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/catalog/aclchk.c,v 1.127 2006/04/30 21:15:33 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/catalog/aclchk.c,v 1.128 2006/05/03 22:45:26 tgl Exp $
1212 *
1313 * NOTES
1414 * See acl.h.
3434#include "catalog/pg_proc.h"
3535#include "catalog/pg_tablespace.h"
3636#include "catalog/pg_type.h"
37+ #include "commands/dbcommands.h"
3738#include "miscadmin.h"
3839#include "parser/parse_func.h"
3940#include "utils/acl.h"
@@ -412,8 +413,8 @@ objectNamesToOids(GrantObjectType objtype, List *objnames)
412413case ACL_OBJECT_SEQUENCE :
413414foreach (cell ,objnames )
414415{
415- Oid relOid ;
416416RangeVar * relvar = (RangeVar * )lfirst (cell );
417+ Oid relOid ;
417418
418419relOid = RangeVarGetRelid (relvar , false);
419420objects = lappend_oid (objects ,relOid );
@@ -423,32 +424,15 @@ objectNamesToOids(GrantObjectType objtype, List *objnames)
423424foreach (cell ,objnames )
424425{
425426char * dbname = strVal (lfirst (cell ));
426- ScanKeyData entry [1 ];
427- HeapScanDesc scan ;
428- HeapTuple tuple ;
429- Relation relation ;
430-
431- relation = heap_open (DatabaseRelationId ,AccessShareLock );
427+ Oid dbid ;
432428
433- /*
434- * There's no syscache for pg_database, so we must look the
435- * hard way.
436- */
437- ScanKeyInit (& entry [0 ],
438- Anum_pg_database_datname ,
439- BTEqualStrategyNumber ,F_NAMEEQ ,
440- CStringGetDatum (dbname ));
441- scan = heap_beginscan (relation ,SnapshotNow ,1 ,entry );
442- tuple = heap_getnext (scan ,ForwardScanDirection );
443- if (!HeapTupleIsValid (tuple ))
429+ dbid = get_database_oid (dbname );
430+ if (!OidIsValid (dbid ))
444431ereport (ERROR ,
445432(errcode (ERRCODE_UNDEFINED_DATABASE ),
446- errmsg ("database \"%s\" does not exist" ,dbname )));
447- objects = lappend_oid (objects ,HeapTupleGetOid (tuple ));
448-
449- heap_close (relation ,AccessShareLock );
450-
451- heap_endscan (scan );
433+ errmsg ("database \"%s\" does not exist" ,
434+ dbname )));
435+ objects = lappend_oid (objects ,dbid );
452436}
453437break ;
454438case ACL_OBJECT_FUNCTION :
@@ -474,7 +458,8 @@ objectNamesToOids(GrantObjectType objtype, List *objnames)
474458if (!HeapTupleIsValid (tuple ))
475459ereport (ERROR ,
476460(errcode (ERRCODE_UNDEFINED_OBJECT ),
477- errmsg ("language \"%s\" does not exist" ,langname )));
461+ errmsg ("language \"%s\" does not exist" ,
462+ langname )));
478463
479464objects = lappend_oid (objects ,HeapTupleGetOid (tuple ));
480465
@@ -493,7 +478,8 @@ objectNamesToOids(GrantObjectType objtype, List *objnames)
493478if (!HeapTupleIsValid (tuple ))
494479ereport (ERROR ,
495480(errcode (ERRCODE_UNDEFINED_SCHEMA ),
496- errmsg ("schema \"%s\" does not exist" ,nspname )));
481+ errmsg ("schema \"%s\" does not exist" ,
482+ nspname )));
497483
498484objects = lappend_oid (objects ,HeapTupleGetOid (tuple ));
499485
@@ -764,22 +750,13 @@ ExecGrant_Database(InternalGrant *istmt)
764750int nnewmembers ;
765751Oid * oldmembers ;
766752Oid * newmembers ;
767- ScanKeyData entry [1 ];
768- SysScanDesc scan ;
769753HeapTuple tuple ;
770754
771- /* There's no syscache for pg_database, so must look the hard way */
772- ScanKeyInit (& entry [0 ],
773- ObjectIdAttributeNumber ,
774- BTEqualStrategyNumber ,F_OIDEQ ,
775- ObjectIdGetDatum (datId ));
776- scan = systable_beginscan (relation ,DatabaseOidIndexId , true,
777- SnapshotNow ,1 ,entry );
778-
779- tuple = systable_getnext (scan );
780-
755+ tuple = SearchSysCache (DATABASEOID ,
756+ ObjectIdGetDatum (datId ),
757+ 0 ,0 ,0 );
781758if (!HeapTupleIsValid (tuple ))
782- elog (ERROR ,"could not find tuple for database %u" ,datId );
759+ elog (ERROR ,"cache lookup failed for database %u" ,datId );
783760
784761pg_database_tuple = (Form_pg_database )GETSTRUCT (tuple );
785762
@@ -847,7 +824,7 @@ ExecGrant_Database(InternalGrant *istmt)
847824noldmembers ,oldmembers ,
848825nnewmembers ,newmembers );
849826
850- systable_endscan ( scan );
827+ ReleaseSysCache ( tuple );
851828
852829pfree (new_acl );
853830
@@ -1657,61 +1634,31 @@ pg_database_aclmask(Oid db_oid, Oid roleid,
16571634AclMode mask ,AclMaskHow how )
16581635{
16591636AclMode result ;
1660- Relation pg_database ;
1661- ScanKeyData entry [1 ];
1662- SysScanDesc scan ;
16631637HeapTuple tuple ;
1638+ Datum aclDatum ;
1639+ bool isNull ;
1640+ Acl * acl ;
1641+ Oid ownerId ;
16641642
16651643/* Superusers bypass all permission checking. */
16661644if (superuser_arg (roleid ))
16671645return mask ;
16681646
16691647/*
16701648 * Get the database's ACL from pg_database
1671- *
1672- * There's no syscache for pg_database, so must look the hard way
16731649 */
1674- pg_database = heap_open (DatabaseRelationId ,AccessShareLock );
1675- ScanKeyInit (& entry [0 ],
1676- ObjectIdAttributeNumber ,
1677- BTEqualStrategyNumber ,F_OIDEQ ,
1678- ObjectIdGetDatum (db_oid ));
1679- scan = systable_beginscan (pg_database ,DatabaseOidIndexId , true,
1680- SnapshotNow ,1 ,entry );
1681- tuple = systable_getnext (scan );
1650+ tuple = SearchSysCache (DATABASEOID ,
1651+ ObjectIdGetDatum (db_oid ),
1652+ 0 ,0 ,0 );
16821653if (!HeapTupleIsValid (tuple ))
16831654ereport (ERROR ,
16841655(errcode (ERRCODE_UNDEFINED_DATABASE ),
16851656errmsg ("database with OID %u does not exist" ,db_oid )));
16861657
1687- result = pg_database_tuple_aclmask (tuple ,RelationGetDescr (pg_database ),
1688- roleid ,mask ,how );
1689-
1690- systable_endscan (scan );
1691- heap_close (pg_database ,AccessShareLock );
1692-
1693- return result ;
1694- }
1695-
1696- /*
1697- * This is split out so that ReverifyMyDatabase can perform an ACL check
1698- * without a whole extra search of pg_database
1699- */
1700- AclMode
1701- pg_database_tuple_aclmask (HeapTuple db_tuple ,TupleDesc tupdesc ,
1702- Oid roleid ,AclMode mask ,AclMaskHow how )
1703- {
1704- AclMode result ;
1705- Datum aclDatum ;
1706- bool isNull ;
1707- Acl * acl ;
1708- Oid ownerId ;
1709-
1710- ownerId = ((Form_pg_database )GETSTRUCT (db_tuple ))-> datdba ;
1711-
1712- aclDatum = heap_getattr (db_tuple ,Anum_pg_database_datacl ,
1713- tupdesc ,& isNull );
1658+ ownerId = ((Form_pg_database )GETSTRUCT (tuple ))-> datdba ;
17141659
1660+ aclDatum = SysCacheGetAttr (DATABASEOID ,tuple ,Anum_pg_database_datacl ,
1661+ & isNull );
17151662if (isNull )
17161663{
17171664/* No ACL, so build default ACL */
@@ -1730,6 +1677,8 @@ pg_database_tuple_aclmask(HeapTuple db_tuple, TupleDesc tupdesc,
17301677if (acl && (Pointer )acl != DatumGetPointer (aclDatum ))
17311678pfree (acl );
17321679
1680+ ReleaseSysCache (tuple );
1681+
17331682return result ;
17341683}
17351684
@@ -2298,36 +2247,24 @@ pg_opclass_ownercheck(Oid opc_oid, Oid roleid)
22982247bool
22992248pg_database_ownercheck (Oid db_oid ,Oid roleid )
23002249{
2301- Relation pg_database ;
2302- ScanKeyData entry [1 ];
2303- SysScanDesc scan ;
2304- HeapTuple dbtuple ;
2250+ HeapTuple tuple ;
23052251Oid dba ;
23062252
23072253/* Superusers bypass all permission checking. */
23082254if (superuser_arg (roleid ))
23092255return true;
23102256
2311- /* There's no syscache for pg_database, so must look the hard way */
2312- pg_database = heap_open (DatabaseRelationId ,AccessShareLock );
2313- ScanKeyInit (& entry [0 ],
2314- ObjectIdAttributeNumber ,
2315- BTEqualStrategyNumber ,F_OIDEQ ,
2316- ObjectIdGetDatum (db_oid ));
2317- scan = systable_beginscan (pg_database ,DatabaseOidIndexId , true,
2318- SnapshotNow ,1 ,entry );
2319-
2320- dbtuple = systable_getnext (scan );
2321-
2322- if (!HeapTupleIsValid (dbtuple ))
2257+ tuple = SearchSysCache (DATABASEOID ,
2258+ ObjectIdGetDatum (db_oid ),
2259+ 0 ,0 ,0 );
2260+ if (!HeapTupleIsValid (tuple ))
23232261ereport (ERROR ,
23242262(errcode (ERRCODE_UNDEFINED_DATABASE ),
23252263errmsg ("database with OID %u does not exist" ,db_oid )));
23262264
2327- dba = ((Form_pg_database )GETSTRUCT (dbtuple ))-> datdba ;
2265+ dba = ((Form_pg_database )GETSTRUCT (tuple ))-> datdba ;
23282266
2329- systable_endscan (scan );
2330- heap_close (pg_database ,AccessShareLock );
2267+ ReleaseSysCache (tuple );
23312268
23322269return has_privs_of_role (roleid ,dba );
23332270}