5
5
* Copyright (c) 2002-2008, PostgreSQL Global Development Group
6
6
*
7
7
* IDENTIFICATION
8
- * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.20 2008/08/11 11:05:11 heikki Exp $
8
+ * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.21 2008/10/03 07:33:09 heikki Exp $
9
9
*
10
10
*/
11
11
@@ -248,15 +248,14 @@ pg_tablespace_size_name(PG_FUNCTION_ARGS)
248
248
* calculate size of a relation
249
249
*/
250
250
static int64
251
- calculate_relation_size (RelFileNode * rfn )
251
+ calculate_relation_size (RelFileNode * rfn , ForkNumber forknum )
252
252
{
253
253
int64 totalsize = 0 ;
254
254
char * relationpath ;
255
255
char pathname [MAXPGPATH ];
256
256
unsigned int segcount = 0 ;
257
257
258
- /* XXX: This ignores the other forks. */
259
- relationpath = relpath (* rfn , MAIN_FORKNUM );
258
+ relationpath = relpath (* rfn , forknum );
260
259
261
260
for (segcount = 0 ;; segcount ++ )
262
261
{
@@ -284,34 +283,47 @@ calculate_relation_size(RelFileNode *rfn)
284
283
return totalsize ;
285
284
}
286
285
287
- Datum
288
- pg_relation_size_oid (PG_FUNCTION_ARGS )
289
- {
290
- Oid relOid = PG_GETARG_OID (0 );
291
- Relation rel ;
292
- int64 size ;
293
286
294
- rel = relation_open (relOid , AccessShareLock );
287
+ /*
288
+ * XXX: Consider making this global and moving elsewhere. But currently
289
+ * there's no other users for this.
290
+ *
291
+ * Remember to also update the errhint below if you add entries, and the
292
+ * documentation for pg_relation_size().
293
+ */
294
+ static char * forkNames [] = {
295
+ "main" , /* MAIN_FORKNUM */
296
+ "fsm" /* FSM_FORKNUM */
297
+ };
295
298
296
- size = calculate_relation_size (& (rel -> rd_node ));
299
+ static ForkNumber
300
+ forkname_to_number (char * forkName )
301
+ {
302
+ ForkNumber forkNum ;
297
303
298
- relation_close (rel , AccessShareLock );
304
+ for (forkNum = 0 ; forkNum <= MAX_FORKNUM ; forkNum ++ )
305
+ if (strcmp (forkNames [forkNum ], forkName ) == 0 )
306
+ return forkNum ;
299
307
300
- PG_RETURN_INT64 (size );
308
+ ereport (ERROR ,
309
+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
310
+ errmsg ("invalid fork name" ),
311
+ errhint ("Valid fork names are 'main' and 'fsm'" )));
312
+ return InvalidForkNumber ; /* keep compiler quiet */
301
313
}
302
314
303
315
Datum
304
- pg_relation_size_name (PG_FUNCTION_ARGS )
316
+ pg_relation_size (PG_FUNCTION_ARGS )
305
317
{
306
- text * relname = PG_GETARG_TEXT_P (0 );
307
- RangeVar * relrv ;
318
+ Oid relOid = PG_GETARG_OID (0 );
319
+ text * forkName = PG_GETARG_TEXT_P ( 1 ) ;
308
320
Relation rel ;
309
321
int64 size ;
310
322
311
- relrv = makeRangeVarFromNameList (textToQualifiedNameList (relname ));
312
- rel = relation_openrv (relrv , AccessShareLock );
323
+ rel = relation_open (relOid , AccessShareLock );
313
324
314
- size = calculate_relation_size (& (rel -> rd_node ));
325
+ size = calculate_relation_size (& (rel -> rd_node ),
326
+ forkname_to_number (text_to_cstring (forkName )));
315
327
316
328
relation_close (rel , AccessShareLock );
317
329
@@ -330,12 +342,15 @@ calculate_total_relation_size(Oid Relid)
330
342
Oid toastOid ;
331
343
int64 size ;
332
344
ListCell * cell ;
345
+ ForkNumber forkNum ;
333
346
334
347
heapRel = relation_open (Relid , AccessShareLock );
335
348
toastOid = heapRel -> rd_rel -> reltoastrelid ;
336
349
337
350
/* Get the heap size */
338
- size = calculate_relation_size (& (heapRel -> rd_node ));
351
+ size = 0 ;
352
+ for (forkNum = 0 ; forkNum <= MAX_FORKNUM ; forkNum ++ )
353
+ size += calculate_relation_size (& (heapRel -> rd_node ), forkNum );
339
354
340
355
/* Include any dependent indexes */
341
356
if (heapRel -> rd_rel -> relhasindex )
@@ -349,7 +364,8 @@ calculate_total_relation_size(Oid Relid)
349
364
350
365
iRel = relation_open (idxOid , AccessShareLock );
351
366
352
- size += calculate_relation_size (& (iRel -> rd_node ));
367
+ for (forkNum = 0 ; forkNum <= MAX_FORKNUM ; forkNum ++ )
368
+ size += calculate_relation_size (& (iRel -> rd_node ), forkNum );
353
369
354
370
relation_close (iRel , AccessShareLock );
355
371
}
@@ -367,26 +383,13 @@ calculate_total_relation_size(Oid Relid)
367
383
}
368
384
369
385
Datum
370
- pg_total_relation_size_oid (PG_FUNCTION_ARGS )
386
+ pg_total_relation_size (PG_FUNCTION_ARGS )
371
387
{
372
388
Oid relid = PG_GETARG_OID (0 );
373
389
374
390
PG_RETURN_INT64 (calculate_total_relation_size (relid ));
375
391
}
376
392
377
- Datum
378
- pg_total_relation_size_name (PG_FUNCTION_ARGS )
379
- {
380
- text * relname = PG_GETARG_TEXT_P (0 );
381
- RangeVar * relrv ;
382
- Oid relid ;
383
-
384
- relrv = makeRangeVarFromNameList (textToQualifiedNameList (relname ));
385
- relid = RangeVarGetRelid (relrv , false);
386
-
387
- PG_RETURN_INT64 (calculate_total_relation_size (relid ));
388
- }
389
-
390
393
/*
391
394
* formatting with size units
392
395
*/
0 commit comments