1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

pageinspect: Change block number arguments to bigint

Block numbers are 32-bit unsigned integers.  Therefore, the smallest
SQL integer type that they can fit in is bigint.  However, in the
pageinspect module, most input and output parameters dealing with
block numbers were declared as int.  The behavior with block numbers
larger than a signed 32-bit integer was therefore dubious.  Change
these arguments to type bigint and add some more explicit error
checking on the block range.

(Other contrib modules appear to do this correctly already.)

Since we are changing argument types of existing functions, in order
to not misbehave if the binary is updated before the extension is
updated, we need to create new C symbols for the entry points, similar
to how it's done in other extensions as well.

Reported-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://www.postgresql.org/message-id/flat/d8f6bdd536df403b9b33816e9f7e0b9d@G08CNEXMBPEKD05.g08.fujitsu.local
This commit is contained in:
Peter Eisentraut
2021-01-19 10:28:05 +01:00
parent ee79a548e7
commit f18aa1b203
18 changed files with 328 additions and 31 deletions

View File

@ -41,8 +41,10 @@
#include "utils/varlena.h"
PG_FUNCTION_INFO_V1(bt_metap);
PG_FUNCTION_INFO_V1(bt_page_items_1_9);
PG_FUNCTION_INFO_V1(bt_page_items);
PG_FUNCTION_INFO_V1(bt_page_items_bytea);
PG_FUNCTION_INFO_V1(bt_page_stats_1_9);
PG_FUNCTION_INFO_V1(bt_page_stats);
#define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
@ -160,11 +162,11 @@ GetBTPageStatistics(BlockNumber blkno, Buffer buffer, BTPageStat *stat)
* Usage: SELECT * FROM bt_page_stats('t1_pkey', 1);
* -----------------------------------------------
*/
Datum
bt_page_stats(PG_FUNCTION_ARGS)
static Datum
bt_page_stats_internal(PG_FUNCTION_ARGS, enum pageinspect_version ext_version)
{
text *relname = PG_GETARG_TEXT_PP(0);
uint32 blkno = PG_GETARG_UINT32(1);
int64 blkno = (ext_version == PAGEINSPECT_V1_8 ? PG_GETARG_UINT32(1) : PG_GETARG_INT64(1));
Buffer buffer;
Relation rel;
RangeVar *relrv;
@ -197,8 +199,15 @@ bt_page_stats(PG_FUNCTION_ARGS)
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot access temporary tables of other sessions")));
if (blkno < 0 || blkno > MaxBlockNumber)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid block number")));
if (blkno == 0)
elog(ERROR, "block 0 is a meta page");
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("block 0 is a meta page")));
CHECK_RELATION_BLOCK_RANGE(rel, blkno);
@ -219,16 +228,16 @@ bt_page_stats(PG_FUNCTION_ARGS)
elog(ERROR, "return type must be a row type");
j = 0;
values[j++] = psprintf("%d", stat.blkno);
values[j++] = psprintf("%u", stat.blkno);
values[j++] = psprintf("%c", stat.type);
values[j++] = psprintf("%d", stat.live_items);
values[j++] = psprintf("%d", stat.dead_items);
values[j++] = psprintf("%d", stat.avg_item_size);
values[j++] = psprintf("%d", stat.page_size);
values[j++] = psprintf("%d", stat.free_size);
values[j++] = psprintf("%d", stat.btpo_prev);
values[j++] = psprintf("%d", stat.btpo_next);
values[j++] = psprintf("%d", (stat.type == 'd') ? stat.btpo.xact : stat.btpo.level);
values[j++] = psprintf("%u", stat.live_items);
values[j++] = psprintf("%u", stat.dead_items);
values[j++] = psprintf("%u", stat.avg_item_size);
values[j++] = psprintf("%u", stat.page_size);
values[j++] = psprintf("%u", stat.free_size);
values[j++] = psprintf("%u", stat.btpo_prev);
values[j++] = psprintf("%u", stat.btpo_next);
values[j++] = psprintf("%u", (stat.type == 'd') ? stat.btpo.xact : stat.btpo.level);
values[j++] = psprintf("%d", stat.btpo_flags);
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
@ -239,6 +248,19 @@ bt_page_stats(PG_FUNCTION_ARGS)
PG_RETURN_DATUM(result);
}
Datum
bt_page_stats_1_9(PG_FUNCTION_ARGS)
{
return bt_page_stats_internal(fcinfo, PAGEINSPECT_V1_9);
}
/* entry point for old extension version */
Datum
bt_page_stats(PG_FUNCTION_ARGS)
{
return bt_page_stats_internal(fcinfo, PAGEINSPECT_V1_8);
}
/*
* cross-call data structure for SRF
@ -405,11 +427,11 @@ bt_page_print_tuples(struct user_args *uargs)
* Usage: SELECT * FROM bt_page_items('t1_pkey', 1);
*-------------------------------------------------------
*/
Datum
bt_page_items(PG_FUNCTION_ARGS)
static Datum
bt_page_items_internal(PG_FUNCTION_ARGS, enum pageinspect_version ext_version)
{
text *relname = PG_GETARG_TEXT_PP(0);
uint32 blkno = PG_GETARG_UINT32(1);
int64 blkno = (ext_version == PAGEINSPECT_V1_8 ? PG_GETARG_UINT32(1) : PG_GETARG_INT64(1));
Datum result;
FuncCallContext *fctx;
MemoryContext mctx;
@ -447,8 +469,15 @@ bt_page_items(PG_FUNCTION_ARGS)
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot access temporary tables of other sessions")));
if (blkno < 0 || blkno > MaxBlockNumber)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid block number")));
if (blkno == 0)
elog(ERROR, "block 0 is a meta page");
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("block 0 is a meta page")));
CHECK_RELATION_BLOCK_RANGE(rel, blkno);
@ -506,6 +535,19 @@ bt_page_items(PG_FUNCTION_ARGS)
SRF_RETURN_DONE(fctx);
}
Datum
bt_page_items_1_9(PG_FUNCTION_ARGS)
{
return bt_page_items_internal(fcinfo, PAGEINSPECT_V1_9);
}
/* entry point for old extension version */
Datum
bt_page_items(PG_FUNCTION_ARGS)
{
return bt_page_items_internal(fcinfo, PAGEINSPECT_V1_8);
}
/*-------------------------------------------------------
* bt_page_items_bytea()
*