2006-02-12 11:55:53 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pg_freespacemap.c
|
2008-09-30 19:17:07 +08:00
|
|
|
* display contents of a free space map
|
2006-02-12 11:55:53 +08:00
|
|
|
*
|
2009-06-11 22:49:15 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/pg_freespacemap/pg_freespacemap.c,v 1.14 2009/06/11 14:48:51 momjian Exp $
|
2006-02-12 11:55:53 +08:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2006-05-05 04:39:34 +08:00
|
|
|
|
2008-09-30 19:17:07 +08:00
|
|
|
#include "access/heapam.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "funcapi.h"
|
2008-10-02 20:20:50 +08:00
|
|
|
#include "storage/block.h"
|
2006-02-12 11:55:53 +08:00
|
|
|
#include "storage/freespace.h"
|
2006-05-05 04:39:34 +08:00
|
|
|
|
2006-02-12 11:55:53 +08:00
|
|
|
|
2006-05-31 06:12:16 +08:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2008-09-30 19:17:07 +08:00
|
|
|
Datum pg_freespace(PG_FUNCTION_ARGS);
|
2006-04-27 06:46:09 +08:00
|
|
|
|
|
|
|
/*
|
2008-09-30 19:17:07 +08:00
|
|
|
* Returns the amount of free space on a given page, according to the
|
|
|
|
* free space map.
|
2006-04-27 06:46:09 +08:00
|
|
|
*/
|
2008-09-30 19:17:07 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pg_freespace);
|
2006-05-05 04:39:34 +08:00
|
|
|
|
2006-04-27 06:46:09 +08:00
|
|
|
Datum
|
2008-09-30 19:17:07 +08:00
|
|
|
pg_freespace(PG_FUNCTION_ARGS)
|
2006-04-27 06:46:09 +08:00
|
|
|
{
|
2009-06-11 22:49:15 +08:00
|
|
|
Oid relid = PG_GETARG_OID(0);
|
|
|
|
int64 blkno = PG_GETARG_INT64(1);
|
|
|
|
int16 freespace;
|
|
|
|
Relation rel;
|
2006-05-05 04:39:34 +08:00
|
|
|
|
2008-09-30 19:17:07 +08:00
|
|
|
rel = relation_open(relid, AccessShareLock);
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2008-10-02 20:20:50 +08:00
|
|
|
if (blkno < 0 || blkno > MaxBlockNumber)
|
2008-09-30 19:17:07 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid block number")));
|
2006-04-27 06:46:09 +08:00
|
|
|
|
2008-09-30 19:17:07 +08:00
|
|
|
freespace = GetRecordedFreeSpace(rel, blkno);
|
2006-04-27 06:46:09 +08:00
|
|
|
|
2008-09-30 19:17:07 +08:00
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
PG_RETURN_INT16(freespace);
|
2006-04-27 06:46:09 +08:00
|
|
|
}
|