1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-21 12:05:57 +03:00
postgres/contrib/pg_freespacemap/pg_freespacemap.c
Tom Lane 4623d71443 Prevent assertion failure in contrib/pg_freespacemap.
Applying pg_freespacemap() to a relation lacking storage (such as a
view) caused an assertion failure, although there was no ill effect
in non-assert builds.  Add an error check for that case.

Bug: #18866
Reported-by: Robins Tharakan <tharakan@gmail.com>
Author: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Euler Taveira <euler@eulerto.com>
Discussion: https://postgr.es/m/18866-d68926d0f1c72d44@postgresql.org
Backpatch-through: 13
2025-03-27 13:20:23 -04:00

54 lines
1.3 KiB
C

/*-------------------------------------------------------------------------
*
* pg_freespacemap.c
* display contents of a free space map
*
* contrib/pg_freespacemap/pg_freespacemap.c
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/relation.h"
#include "fmgr.h"
#include "storage/freespace.h"
#include "utils/rel.h"
PG_MODULE_MAGIC_EXT(
.name = "pg_freespacemap",
.version = PG_VERSION
);
/*
* Returns the amount of free space on a given page, according to the
* free space map.
*/
PG_FUNCTION_INFO_V1(pg_freespace);
Datum
pg_freespace(PG_FUNCTION_ARGS)
{
Oid relid = PG_GETARG_OID(0);
int64 blkno = PG_GETARG_INT64(1);
int16 freespace;
Relation rel;
rel = relation_open(relid, AccessShareLock);
if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("relation \"%s\" does not have storage",
RelationGetRelationName(rel)),
errdetail_relkind_not_supported(rel->rd_rel->relkind)));
if (blkno < 0 || blkno > MaxBlockNumber)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid block number")));
freespace = GetRecordedFreeSpace(rel, blkno);
relation_close(rel, AccessShareLock);
PG_RETURN_INT16(freespace);
}