PostgreSQL Source Code: contrib/pg_freespacemap/pg_freespacemap.c Source File

PostgreSQL Source Code git master
pg_freespacemap.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pg_freespacemap.c
4 * display contents of a free space map
5 *
6 * contrib/pg_freespacemap/pg_freespacemap.c
7 *-------------------------------------------------------------------------
8 */
9#include "postgres.h"
10
11#include "access/relation.h"
12#include "fmgr.h"
13#include "storage/freespace.h"
14#include "utils/rel.h"
15
16 PG_MODULE_MAGIC_EXT(
17 .name = "pg_freespacemap",
18 .version = PG_VERSION
19);
20
21/*
22 * Returns the amount of free space on a given page, according to the
23 * free space map.
24 */
25 PG_FUNCTION_INFO_V1(pg_freespace);
26
27Datum
28 pg_freespace(PG_FUNCTION_ARGS)
29{
30 Oid relid = PG_GETARG_OID(0);
31 int64 blkno = PG_GETARG_INT64(1);
32 int16 freespace;
33 Relation rel;
34
35 rel = relation_open(relid, AccessShareLock);
36
37 if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
38 ereport(ERROR,
39 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
40 errmsg("relation \"%s\" does not have storage",
41 RelationGetRelationName(rel)),
42 errdetail_relkind_not_supported(rel->rd_rel->relkind)));
43
44 if (blkno < 0 || blkno > MaxBlockNumber)
45 ereport(ERROR,
46 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
47 errmsg("invalid block number")));
48
49 freespace = GetRecordedFreeSpace(rel, blkno);
50
51 relation_close(rel, AccessShareLock);
52 PG_RETURN_INT16(freespace);
53}
#define MaxBlockNumber
Definition: block.h:35
int64_t int64
Definition: c.h:535
int16_t int16
Definition: c.h:533
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:356
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
Size GetRecordedFreeSpace(Relation rel, BlockNumber heapBlk)
Definition: freespace.c:244
#define AccessShareLock
Definition: lockdefs.h:36
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
PG_MODULE_MAGIC_EXT(.name="pg_freespacemap",.version=PG_VERSION)
Datum pg_freespace(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(pg_freespace)
uint64_t Datum
Definition: postgres.h:70
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationGetRelationName(relation)
Definition: rel.h:548
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47
Definition: rel.h:56
Form_pg_class rd_rel
Definition: rel.h:111
const char * name

AltStyle によって変換されたページ (->オリジナル) /