5
5
* Copyright (c) 2002-2007, PostgreSQL Global Development Group
6
6
*
7
7
* IDENTIFICATION
8
- * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.13 2007/08/27 01:19:14 tgl Exp $
8
+ * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.14 2007/08/29 17:24:29 tgl Exp $
9
9
*
10
10
*/
11
11
@@ -79,6 +79,13 @@ calculate_database_size(Oid dbOid)
79
79
struct dirent * direntry ;
80
80
char dirpath [MAXPGPATH ];
81
81
char pathname [MAXPGPATH ];
82
+ AclResult aclresult ;
83
+
84
+ /* User must have connect privilege for target database */
85
+ aclresult = pg_database_aclcheck (dbOid , GetUserId (), ACL_CONNECT );
86
+ if (aclresult != ACLCHECK_OK )
87
+ aclcheck_error (aclresult , ACL_KIND_DATABASE ,
88
+ get_database_name (dbOid ));
82
89
83
90
/* Shared storage in pg_global is not counted */
84
91
@@ -122,10 +129,6 @@ pg_database_size_oid(PG_FUNCTION_ARGS)
122
129
{
123
130
Oid dbOid = PG_GETARG_OID (0 );
124
131
125
- if (!pg_database_ownercheck (dbOid , GetUserId ()))
126
- aclcheck_error (ACLCHECK_NOT_OWNER , ACL_KIND_DATABASE ,
127
- get_database_name (dbOid ));
128
-
129
132
PG_RETURN_INT64 (calculate_database_size (dbOid ));
130
133
}
131
134
@@ -141,10 +144,6 @@ pg_database_size_name(PG_FUNCTION_ARGS)
141
144
errmsg ("database \"%s\" does not exist" ,
142
145
NameStr (* dbName ))));
143
146
144
- if (!pg_database_ownercheck (dbOid , GetUserId ()))
145
- aclcheck_error (ACLCHECK_NOT_OWNER , ACL_KIND_DATABASE ,
146
- NameStr (* dbName ));
147
-
148
147
PG_RETURN_INT64 (calculate_database_size (dbOid ));
149
148
}
150
149
@@ -160,6 +159,19 @@ calculate_tablespace_size(Oid tblspcOid)
160
159
int64 totalsize = 0 ;
161
160
DIR * dirdesc ;
162
161
struct dirent * direntry ;
162
+ AclResult aclresult ;
163
+
164
+ /*
165
+ * User must have CREATE privilege for target tablespace, either explicitly
166
+ * granted or implicitly because it is default for current database.
167
+ */
168
+ if (tblspcOid != MyDatabaseTableSpace )
169
+ {
170
+ aclresult = pg_tablespace_aclcheck (tblspcOid , GetUserId (), ACL_CREATE );
171
+ if (aclresult != ACLCHECK_OK )
172
+ aclcheck_error (aclresult , ACL_KIND_TABLESPACE ,
173
+ get_tablespace_name (tblspcOid ));
174
+ }
163
175
164
176
if (tblspcOid == DEFAULTTABLESPACE_OID )
165
177
snprintf (tblspcPath , MAXPGPATH , "base" );
@@ -212,11 +224,6 @@ pg_tablespace_size_oid(PG_FUNCTION_ARGS)
212
224
{
213
225
Oid tblspcOid = PG_GETARG_OID (0 );
214
226
215
- if (!superuser ())
216
- ereport (ERROR ,
217
- (errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
218
- (errmsg ("must be superuser to use pg_tablespace_size" ))));
219
-
220
227
PG_RETURN_INT64 (calculate_tablespace_size (tblspcOid ));
221
228
}
222
229
@@ -226,11 +233,6 @@ pg_tablespace_size_name(PG_FUNCTION_ARGS)
226
233
Name tblspcName = PG_GETARG_NAME (0 );
227
234
Oid tblspcOid = get_tablespace_oid (NameStr (* tblspcName ));
228
235
229
- if (!superuser ())
230
- ereport (ERROR ,
231
- (errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
232
- (errmsg ("must be superuser to use pg_tablespace_size" ))));
233
-
234
236
if (!OidIsValid (tblspcOid ))
235
237
ereport (ERROR ,
236
238
(errcode (ERRCODE_UNDEFINED_OBJECT ),
@@ -289,10 +291,6 @@ pg_relation_size_oid(PG_FUNCTION_ARGS)
289
291
290
292
rel = relation_open (relOid , AccessShareLock );
291
293
292
- if (!pg_class_ownercheck (RelationGetRelid (rel ), GetUserId ()))
293
- aclcheck_error (ACLCHECK_NOT_OWNER , ACL_KIND_CLASS ,
294
- RelationGetRelationName (rel ));
295
-
296
294
size = calculate_relation_size (& (rel -> rd_node ));
297
295
298
296
relation_close (rel , AccessShareLock );
@@ -311,10 +309,6 @@ pg_relation_size_name(PG_FUNCTION_ARGS)
311
309
relrv = makeRangeVarFromNameList (textToQualifiedNameList (relname ));
312
310
rel = relation_openrv (relrv , AccessShareLock );
313
311
314
- if (!pg_class_ownercheck (RelationGetRelid (rel ), GetUserId ()))
315
- aclcheck_error (ACLCHECK_NOT_OWNER , ACL_KIND_CLASS ,
316
- RelationGetRelationName (rel ));
317
-
318
312
size = calculate_relation_size (& (rel -> rd_node ));
319
313
320
314
relation_close (rel , AccessShareLock );
@@ -336,11 +330,6 @@ calculate_total_relation_size(Oid Relid)
336
330
ListCell * cell ;
337
331
338
332
heapRel = relation_open (Relid , AccessShareLock );
339
-
340
- if (!pg_class_ownercheck (RelationGetRelid (heapRel ), GetUserId ()))
341
- aclcheck_error (ACLCHECK_NOT_OWNER , ACL_KIND_CLASS ,
342
- RelationGetRelationName (heapRel ));
343
-
344
333
toastOid = heapRel -> rd_rel -> reltoastrelid ;
345
334
346
335
/* Get the heap size */
@@ -380,8 +369,6 @@ pg_total_relation_size_oid(PG_FUNCTION_ARGS)
380
369
{
381
370
Oid relid = PG_GETARG_OID (0 );
382
371
383
- /* permission check is inside calculate_total_relation_size */
384
-
385
372
PG_RETURN_INT64 (calculate_total_relation_size (relid ));
386
373
}
387
374
@@ -395,8 +382,6 @@ pg_total_relation_size_name(PG_FUNCTION_ARGS)
395
382
relrv = makeRangeVarFromNameList (textToQualifiedNameList (relname ));
396
383
relid = RangeVarGetRelid (relrv , false);
397
384
398
- /* permission check is inside calculate_total_relation_size */
399
-
400
385
PG_RETURN_INT64 (calculate_total_relation_size (relid ));
401
386
}
402
387
0 commit comments