On Thu, Dec 22, 2011 at 3:19 PM, Robert Haas <[email protected]> wrote:
> On Thu, Dec 22, 2011 at 2:02 PM, Phil Sorber <[email protected]> wrote:
>> On Thu, Dec 22, 2011 at 1:33 PM, Tom Lane <[email protected]> wrote:
>>> Robert Haas <[email protected]> writes:
>>>> I'm wondering if we oughta just return NULL and be done with it.
>>>
>>> +1. There are multiple precedents for that sort of response, which we
>>> introduced exactly so that "SELECT some_function(oid) FROM some_catalog"
>>> wouldn't fail just because one of the rows had gotten deleted by the
>>> time the scan got to it. I don't think it's necessary for the
>>> relation-size functions to be any smarter. Indeed, I'd assumed that's
>>> all that Phil's patch did, since I'd not looked closer till just now.
>>
>> Here it is without the checking for recently dead. If it can't open
>> the relation it simply returns NULL.
>
> I think we probably ought to make pg_database_size() and
> pg_tablespace_size() behave similarly.
>
> --
> Robert Haas
> EnterpriseDB: http://www.enterprisedb.com
> The Enterprise PostgreSQL Company
Changes added.
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
new file mode 100644
index 2ee5966..8c30dc4
*** a/src/backend/utils/adt/dbsize.c
--- b/src/backend/utils/adt/dbsize.c
*************** calculate_database_size(Oid dbOid)
*** 120,131 ****
FreeDir(dirdesc);
- /* Complain if we found no trace of the DB at all */
- if (!totalsize)
- ereport(ERROR,
- (ERRCODE_UNDEFINED_DATABASE,
- errmsg("database with OID %u does not exist", dbOid)));
-
return totalsize;
}
--- 120,125 ----
*************** Datum
*** 133,140 ****
pg_database_size_oid(PG_FUNCTION_ARGS)
{
Oid dbOid = PG_GETARG_OID(0);
! PG_RETURN_INT64(calculate_database_size(dbOid));
}
Datum
--- 127,140 ----
pg_database_size_oid(PG_FUNCTION_ARGS)
{
Oid dbOid = PG_GETARG_OID(0);
+ int64 size;
! size = calculate_database_size(dbOid);
!
! if (!size)
! PG_RETURN_NULL();
!
! PG_RETURN_INT64(size);
}
Datum
*************** pg_database_size_name(PG_FUNCTION_ARGS)
*** 142,149 ****
{
Name dbName = PG_GETARG_NAME(0);
Oid dbOid = get_database_oid(NameStr(*dbName), false);
! PG_RETURN_INT64(calculate_database_size(dbOid));
}
--- 142,155 ----
{
Name dbName = PG_GETARG_NAME(0);
Oid dbOid = get_database_oid(NameStr(*dbName), false);
+ int64 size;
! size = calculate_database_size(dbOid);
!
! if (!size)
! PG_RETURN_NULL();
!
! PG_RETURN_INT64(size);
}
*************** calculate_tablespace_size(Oid tblspcOid)
*** 184,193 ****
dirdesc = AllocateDir(tblspcPath);
if (!dirdesc)
! ereport(ERROR,
! (errcode_for_file_access(),
! errmsg("could not open tablespace directory \"%s\": %m",
! tblspcPath)));
while ((direntry = ReadDir(dirdesc, tblspcPath)) != NULL)
{
--- 190,196 ----
dirdesc = AllocateDir(tblspcPath);
if (!dirdesc)
! return -1;
while ((direntry = ReadDir(dirdesc, tblspcPath)) != NULL)
{
*************** Datum
*** 226,233 ****
pg_tablespace_size_oid(PG_FUNCTION_ARGS)
{
Oid tblspcOid = PG_GETARG_OID(0);
! PG_RETURN_INT64(calculate_tablespace_size(tblspcOid));
}
Datum
--- 229,242 ----
pg_tablespace_size_oid(PG_FUNCTION_ARGS)
{
Oid tblspcOid = PG_GETARG_OID(0);
+ int64 size;
! size = calculate_tablespace_size(tblspcOid);
!
! if (size < 0)
! PG_RETURN_NULL();
!
! PG_RETURN_INT64(size);
}
Datum
*************** pg_tablespace_size_name(PG_FUNCTION_ARGS
*** 235,242 ****
{
Name tblspcName = PG_GETARG_NAME(0);
Oid tblspcOid = get_tablespace_oid(NameStr(*tblspcName), false);
! PG_RETURN_INT64(calculate_tablespace_size(tblspcOid));
}
--- 244,257 ----
{
Name tblspcName = PG_GETARG_NAME(0);
Oid tblspcOid = get_tablespace_oid(NameStr(*tblspcName), false);
+ int64 size;
! size = calculate_tablespace_size(tblspcOid);
!
! if (size < 0)
! PG_RETURN_NULL();
!
! PG_RETURN_INT64(size);
}
*************** pg_relation_size(PG_FUNCTION_ARGS)
*** 289,295 ****
Relation rel;
int64 size;
! rel = relation_open(relOid, AccessShareLock);
size = calculate_relation_size(&(rel->rd_node), rel->rd_backend,
forkname_to_number(text_to_cstring(forkName)));
--- 304,313 ----
Relation rel;
int64 size;
! rel = try_relation_open(relOid, AccessShareLock);
!
! if (rel == NULL)
! PG_RETURN_NULL();
size = calculate_relation_size(&(rel->rd_node), rel->rd_backend,
forkname_to_number(text_to_cstring(forkName)));
*************** calculate_toast_table_size(Oid toastreli
*** 339,352 ****
* those won't have attached toast tables, but they can have multiple forks.
*/
static int64
! calculate_table_size(Oid relOid)
{
int64 size = 0;
- Relation rel;
ForkNumber forkNum;
- rel = relation_open(relOid, AccessShareLock);
-
/*
* heap size, including FSM and VM
*/
--- 357,367 ----
* those won't have attached toast tables, but they can have multiple forks.
*/
static int64
! calculate_table_size(Relation rel)
{
int64 size = 0;
ForkNumber forkNum;
/*
* heap size, including FSM and VM
*/
*************** calculate_table_size(Oid relOid)
*** 360,367 ****
if (OidIsValid(rel->rd_rel->reltoastrelid))
size += calculate_toast_table_size(rel->rd_rel->reltoastrelid);
- relation_close(rel, AccessShareLock);
-
return size;
}
--- 375,380 ----
*************** calculate_table_size(Oid relOid)
*** 371,382 ****
* Can be applied safely to an index, but you'll just get zero.
*/
static int64
! calculate_indexes_size(Oid relOid)
{
int64 size = 0;
- Relation rel;
-
- rel = relation_open(relOid, AccessShareLock);
/*
* Aggregate all indexes on the given relation
--- 384,392 ----
* Can be applied safely to an index, but you'll just get zero.
*/
static int64
! calculate_indexes_size(Relation rel)
{
int64 size = 0;
/*
* Aggregate all indexes on the given relation
*************** calculate_indexes_size(Oid relOid)
*** 405,412 ****
list_free(index_oids);
}
- relation_close(rel, AccessShareLock);
-
return size;
}
--- 415,420 ----
*************** Datum
*** 414,429 ****
pg_table_size(PG_FUNCTION_ARGS)
{
Oid relOid = PG_GETARG_OID(0);
! PG_RETURN_INT64(calculate_table_size(relOid));
}
Datum
pg_indexes_size(PG_FUNCTION_ARGS)
{
Oid relOid = PG_GETARG_OID(0);
! PG_RETURN_INT64(calculate_indexes_size(relOid));
}
/*
--- 422,459 ----
pg_table_size(PG_FUNCTION_ARGS)
{
Oid relOid = PG_GETARG_OID(0);
+ Relation rel;
+ int64 size;
! rel = try_relation_open(relOid, AccessShareLock);
!
! if (rel == NULL)
! PG_RETURN_NULL();
!
! size = calculate_table_size(rel);
!
! relation_close(rel, AccessShareLock);
!
! PG_RETURN_INT64(size);
}
Datum
pg_indexes_size(PG_FUNCTION_ARGS)
{
Oid relOid = PG_GETARG_OID(0);
+ Relation rel;
+ int64 size;
! rel = try_relation_open(relOid, AccessShareLock);
!
! if (rel == NULL)
! PG_RETURN_NULL();
!
! size = calculate_indexes_size(rel);
!
! relation_close(rel, AccessShareLock);
!
! PG_RETURN_INT64(size);
}
/*
*************** pg_indexes_size(PG_FUNCTION_ARGS)
*** 431,437 ****
* including heap data, index data, toast data, FSM, VM.
*/
static int64
! calculate_total_relation_size(Oid Relid)
{
int64 size;
--- 461,467 ----
* including heap data, index data, toast data, FSM, VM.
*/
static int64
! calculate_total_relation_size(Relation Rel)
{
int64 size;
*************** calculate_total_relation_size(Oid Relid)
*** 439,450 ****
* Aggregate the table size, this includes size of the heap, toast and
* toast index with free space and visibility map
*/
! size = calculate_table_size(Relid);
/*
* Add size of all attached indexes as well
*/
! size += calculate_indexes_size(Relid);
return size;
}
--- 469,480 ----
* Aggregate the table size, this includes size of the heap, toast and
* toast index with free space and visibility map
*/
! size = calculate_table_size(Rel);
/*
* Add size of all attached indexes as well
*/
! size += calculate_indexes_size(Rel);
return size;
}
*************** calculate_total_relation_size(Oid Relid)
*** 452,460 ****
Datum
pg_total_relation_size(PG_FUNCTION_ARGS)
{
! Oid relid = PG_GETARG_OID(0);
! PG_RETURN_INT64(calculate_total_relation_size(relid));
}
/*
--- 482,501 ----
Datum
pg_total_relation_size(PG_FUNCTION_ARGS)
{
! Oid relOid = PG_GETARG_OID(0);
! Relation rel;
! int64 size;
! rel = try_relation_open(relOid, AccessShareLock);
!
! if (rel == NULL)
! PG_RETURN_NULL();
!
! size = calculate_total_relation_size(rel);
!
! relation_close(rel, AccessShareLock);
!
! PG_RETURN_INT64(size);
}
/*
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers