On Thu, Dec 22, 2011 at 1:33 PM, Tom Lane <t...@sss.pgh.pa.us> wrote: > Robert Haas <robertmh...@gmail.com> 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. > > regards, tom lane
Here it is without the checking for recently dead. If it can't open the relation it simply returns NULL.
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c new file mode 100644 index 2ee5966..134bc03 *** a/src/backend/utils/adt/dbsize.c --- b/src/backend/utils/adt/dbsize.c *************** 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))); --- 289,298 ---- 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 */ --- 342,352 ---- * 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; } --- 360,365 ---- *************** 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 --- 369,377 ---- * 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; } --- 400,405 ---- *************** 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)); } /* --- 407,444 ---- 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; --- 446,452 ---- * 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; } --- 454,465 ---- * 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)); } /* --- 467,486 ---- 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 (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers