2006-02-12 03:55:53 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pg_freespacemap.c
|
2008-09-30 11:17:07 +00:00
|
|
|
* display contents of a free space map
|
2006-02-12 03:55:53 +00:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* contrib/pg_freespacemap/pg_freespacemap.c
|
2006-02-12 03:55:53 +00:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2006-05-04 20:39:34 +00:00
|
|
|
|
2019-01-21 10:18:20 -08:00
|
|
|
#include "access/relation.h"
|
2024-10-28 08:02:17 +01:00
|
|
|
#include "fmgr.h"
|
2006-02-12 03:55:53 +00:00
|
|
|
#include "storage/freespace.h"
|
2006-05-04 20:39:34 +00:00
|
|
|
|
2006-05-30 22:12:16 +00:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2006-04-26 22:46:09 +00:00
|
|
|
/*
|
2008-09-30 11:17:07 +00:00
|
|
|
* Returns the amount of free space on a given page, according to the
|
|
|
|
* free space map.
|
2006-04-26 22:46:09 +00:00
|
|
|
*/
|
2008-09-30 11:17:07 +00:00
|
|
|
PG_FUNCTION_INFO_V1(pg_freespace);
|
2006-05-04 20:39:34 +00:00
|
|
|
|
2006-04-26 22:46:09 +00:00
|
|
|
Datum
|
2008-09-30 11:17:07 +00:00
|
|
|
pg_freespace(PG_FUNCTION_ARGS)
|
2006-04-26 22:46:09 +00:00
|
|
|
{
|
2008-09-30 11:17:07 +00:00
|
|
|
Oid relid = PG_GETARG_OID(0);
|
2008-10-02 12:20:50 +00:00
|
|
|
int64 blkno = PG_GETARG_INT64(1);
|
2008-09-30 11:17:07 +00:00
|
|
|
int16 freespace;
|
|
|
|
Relation rel;
|
2006-05-04 20:39:34 +00:00
|
|
|
|
2008-09-30 11:17:07 +00:00
|
|
|
rel = relation_open(relid, AccessShareLock);
|
2006-10-04 00:30:14 +00:00
|
|
|
|
2008-10-02 12:20:50 +00:00
|
|
|
if (blkno < 0 || blkno > MaxBlockNumber)
|
2008-09-30 11:17:07 +00:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid block number")));
|
2006-04-26 22:46:09 +00:00
|
|
|
|
2008-09-30 11:17:07 +00:00
|
|
|
freespace = GetRecordedFreeSpace(rel, blkno);
|
2006-04-26 22:46:09 +00:00
|
|
|
|
2008-09-30 11:17:07 +00:00
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
PG_RETURN_INT16(freespace);
|
2006-04-26 22:46:09 +00:00
|
|
|
}
|