00001 /*------------------------------------------------------------------------- 00002 * 00003 * pg_freespacemap.c 00004 * display contents of a free space map 00005 * 00006 * contrib/pg_freespacemap/pg_freespacemap.c 00007 *------------------------------------------------------------------------- 00008 */ 00009 #include "postgres.h" 00010 00011 #include "funcapi.h" 00012 #include "storage/freespace.h" 00013 00014 00015 PG_MODULE_MAGIC; 00016 00017 Datum pg_freespace(PG_FUNCTION_ARGS); 00018 00019 /* 00020 * Returns the amount of free space on a given page, according to the 00021 * free space map. 00022 */ 00023 PG_FUNCTION_INFO_V1(pg_freespace); 00024 00025 Datum 00026 pg_freespace(PG_FUNCTION_ARGS) 00027 { 00028 Oid relid = PG_GETARG_OID(0); 00029 int64 blkno = PG_GETARG_INT64(1); 00030 int16 freespace; 00031 Relation rel; 00032 00033 rel = relation_open(relid, AccessShareLock); 00034 00035 if (blkno < 0 || blkno > MaxBlockNumber) 00036 ereport(ERROR, 00037 (errcode(ERRCODE_INVALID_PARAMETER_VALUE), 00038 errmsg("invalid block number"))); 00039 00040 freespace = GetRecordedFreeSpace(rel, blkno); 00041 00042 relation_close(rel, AccessShareLock); 00043 PG_RETURN_INT16(freespace); 00044 }