summaryrefslogtreecommitdiff
path: root/src/backend/utils
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2001-06-13 21:44:41 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2001-06-13 21:44:41 +0000
commit1a6bb6d877f60c377635c4e83a1b75ca2f437951 (patch)
tree842ea6e49fcfda307f33a325356a24c27c5cda33 /src/backend/utils
parentf21e3407e6f8b706dc2a490365e13a8e613523de (diff)
downloadpostgresql-1a6bb6d877f60c377635c4e83a1b75ca2f437951.tar.gz
Allow a non-superuser database owner to vacuum all tables in his
database, including system catalogs (but not the shared catalogs, since they don't really belong to his database). This is per recent mailing list discussion. Clean up some other code that also checks for database ownerness by introducing a test function is_dbadmin().
Diffstat (limited to 'src/backend/utils')
-rw-r--r--src/backend/utils/misc/superuser.c56
1 files changed, 44 insertions, 12 deletions
diff --git a/src/backend/utils/misc/superuser.c b/src/backend/utils/misc/superuser.c
index e539d57347..73cfe8cb97 100644
--- a/src/backend/utils/misc/superuser.c
+++ b/src/backend/utils/misc/superuser.c
@@ -1,35 +1,38 @@
/*-------------------------------------------------------------------------
*
* superuser.c
- *
* The superuser() function. Determines if user has superuser privilege.
+ * Also, a function to check for the owner (datdba) of a database.
+ *
*
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/misc/superuser.c,v 1.17 2001/01/24 19:43:16 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/misc/superuser.c,v 1.18 2001/06/13 21:44:41 tgl Exp $
*
- * DESCRIPTION
- * See superuser().
*-------------------------------------------------------------------------
*/
-
#include "postgres.h"
+
+#include "access/heapam.h"
+#include "catalog/catname.h"
+#include "catalog/pg_database.h"
#include "catalog/pg_shadow.h"
#include "utils/syscache.h"
#include "miscadmin.h"
+#include "utils/fmgroids.h"
+
+/*
+ * The Postgres user running this command has Postgres superuser privileges
+ */
bool
superuser(void)
{
-/*--------------------------------------------------------------------------
- The Postgres user running this command has Postgres superuser
- privileges.
---------------------------------------------------------------------------*/
+ bool result = false;
HeapTuple utup;
- bool result;
utup = SearchSysCache(SHADOWSYSID,
ObjectIdGetDatum(GetUserId()),
@@ -38,7 +41,36 @@ superuser(void)
{
result = ((Form_pg_shadow) GETSTRUCT(utup))->usesuper;
ReleaseSysCache(utup);
- return result;
}
- return false;
+ return result;
+}
+
+/*
+ * The Postgres user running this command is the owner of the specified
+ * database.
+ */
+bool
+is_dbadmin(Oid dbid)
+{
+ Relation pg_database;
+ ScanKeyData entry[1];
+ HeapScanDesc scan;
+ HeapTuple dbtuple;
+ int32 dba;
+
+ /* There's no syscache for pg_database, so must look the hard way */
+ pg_database = heap_openr(DatabaseRelationName, AccessShareLock);
+ ScanKeyEntryInitialize(&entry[0], 0x0,
+ ObjectIdAttributeNumber, F_OIDEQ,
+ ObjectIdGetDatum(dbid));
+ scan = heap_beginscan(pg_database, 0, SnapshotNow, 1, entry);
+ dbtuple = heap_getnext(scan, 0);
+ if (!HeapTupleIsValid(dbtuple))
+ elog(ERROR, "database %u does not exist", dbid);
+ dba = ((Form_pg_database) GETSTRUCT(dbtuple))->datdba;
+ heap_endscan(scan);
+ heap_close(pg_database, AccessShareLock);
+
+ /* XXX some confusion about whether userids are OID or int4 ... */
+ return (GetUserId() == (Oid) dba);
}