On 2020-11-02 16:59, Peter Eisentraut wrote:
I have committed 0003.
For 0001, normal_rand(), I think you should reject negative arguments
with an error.
I have committed a fix for that.
For 0002, I think you should change the block number arguments to int8,
same as other contrib modules do.
Looking further into this, almost all of pageinspect needs to be updated
to handle block numbers larger than INT_MAX correctly. Attached is a
patch for this. It is meant to work like other contrib modules, such as
pg_freespace and pg_visibility. I haven't tested this much yet.
From 30c0e36a1f19b2d09eb81a26949c7793167084e8 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Wed, 25 Nov 2020 17:13:46 +0100
Subject: [PATCH] pageinspect: Change block number arguments to bigint
---
contrib/pageinspect/Makefile | 3 +-
contrib/pageinspect/brinfuncs.c | 2 +-
contrib/pageinspect/btreefuncs.c | 40 ++++++---
contrib/pageinspect/hashfuncs.c | 11 ++-
contrib/pageinspect/pageinspect--1.8--1.9.sql | 81 +++++++++++++++++++
contrib/pageinspect/pageinspect.control | 2 +-
contrib/pageinspect/rawpage.c | 24 ++++--
doc/src/sgml/pageinspect.sgml | 12 +--
8 files changed, 143 insertions(+), 32 deletions(-)
create mode 100644 contrib/pageinspect/pageinspect--1.8--1.9.sql
diff --git a/contrib/pageinspect/Makefile b/contrib/pageinspect/Makefile
index d9d8177116..3fcbfea293 100644
--- a/contrib/pageinspect/Makefile
+++ b/contrib/pageinspect/Makefile
@@ -12,7 +12,8 @@ OBJS = \
rawpage.o
EXTENSION = pageinspect
-DATA = pageinspect--1.7--1.8.sql pageinspect--1.6--1.7.sql \
+DATA = pageinspect--1.8--1.9.sql \
+ pageinspect--1.7--1.8.sql pageinspect--1.6--1.7.sql \
pageinspect--1.5.sql pageinspect--1.5--1.6.sql \
pageinspect--1.4--1.5.sql pageinspect--1.3--1.4.sql \
pageinspect--1.2--1.3.sql pageinspect--1.1--1.2.sql \
diff --git a/contrib/pageinspect/brinfuncs.c b/contrib/pageinspect/brinfuncs.c
index fb32d74a66..6f452c688d 100644
--- a/contrib/pageinspect/brinfuncs.c
+++ b/contrib/pageinspect/brinfuncs.c
@@ -252,7 +252,7 @@ brin_page_items(PG_FUNCTION_ARGS)
int att = attno - 1;
values[0] = UInt16GetDatum(offset);
- values[1] = UInt32GetDatum(dtup->bt_blkno);
+ values[1] = Int64GetDatum((int64) dtup->bt_blkno);
values[2] = UInt16GetDatum(attno);
values[3] =
BoolGetDatum(dtup->bt_columns[att].bv_allnulls);
values[4] =
BoolGetDatum(dtup->bt_columns[att].bv_hasnulls);
diff --git a/contrib/pageinspect/btreefuncs.c b/contrib/pageinspect/btreefuncs.c
index 445605db58..5937de3a1c 100644
--- a/contrib/pageinspect/btreefuncs.c
+++ b/contrib/pageinspect/btreefuncs.c
@@ -164,7 +164,7 @@ Datum
bt_page_stats(PG_FUNCTION_ARGS)
{
text *relname = PG_GETARG_TEXT_PP(0);
- uint32 blkno = PG_GETARG_UINT32(1);
+ int64 blkno = PG_GETARG_INT64(1);
Buffer buffer;
Relation rel;
RangeVar *relrv;
@@ -197,8 +197,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 +226,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),
@@ -409,7 +416,7 @@ Datum
bt_page_items(PG_FUNCTION_ARGS)
{
text *relname = PG_GETARG_TEXT_PP(0);
- uint32 blkno = PG_GETARG_UINT32(1);
+ int64 blkno = PG_GETARG_INT64(1);
Datum result;
FuncCallContext *fctx;
MemoryContext mctx;
@@ -447,8 +454,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);
diff --git a/contrib/pageinspect/hashfuncs.c b/contrib/pageinspect/hashfuncs.c
index 3b2f0339cf..262ed656f6 100644
--- a/contrib/pageinspect/hashfuncs.c
+++ b/contrib/pageinspect/hashfuncs.c
@@ -390,7 +390,7 @@ Datum
hash_bitmap_info(PG_FUNCTION_ARGS)
{
Oid indexRelid = PG_GETARG_OID(0);
- uint64 ovflblkno = PG_GETARG_INT64(1);
+ int64 ovflblkno = PG_GETARG_INT64(1);
HashMetaPage metap;
Buffer metabuf,
mapbuf;
@@ -425,11 +425,16 @@ hash_bitmap_info(PG_FUNCTION_ARGS)
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot access temporary tables of
other sessions")));
+ if (ovflblkno < 0 || ovflblkno > MaxBlockNumber)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("invalid block number")));
+
if (ovflblkno >= RelationGetNumberOfBlocks(indexRel))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("block number " UINT64_FORMAT " is out
of range for relation \"%s\"",
- ovflblkno,
RelationGetRelationName(indexRel))));
+ errmsg("block number %llu is out of range for
relation \"%s\"",
+ (long long unsigned) ovflblkno,
RelationGetRelationName(indexRel))));
/* Read the metapage so we can determine which bitmap page to use */
metabuf = _hash_getbuf(indexRel, HASH_METAPAGE, HASH_READ,
LH_META_PAGE);
diff --git a/contrib/pageinspect/pageinspect--1.8--1.9.sql
b/contrib/pageinspect/pageinspect--1.8--1.9.sql
new file mode 100644
index 0000000000..55f48216dd
--- /dev/null
+++ b/contrib/pageinspect/pageinspect--1.8--1.9.sql
@@ -0,0 +1,81 @@
+/* contrib/pageinspect/pageinspect--1.8--1.9.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION pageinspect UPDATE TO '1.9'" to load this file.
\quit
+
+--
+-- get_raw_page()
+--
+DROP FUNCTION get_raw_page(text, int4);
+CREATE FUNCTION get_raw_page(text, int8)
+RETURNS bytea
+AS 'MODULE_PATHNAME', 'get_raw_page'
+LANGUAGE C STRICT PARALLEL SAFE;
+
+DROP FUNCTION get_raw_page(text, text, int4);
+CREATE FUNCTION get_raw_page(text, text, int8)
+RETURNS bytea
+AS 'MODULE_PATHNAME', 'get_raw_page_fork'
+LANGUAGE C STRICT PARALLEL SAFE;
+
+--
+-- page_checksum()
+--
+DROP FUNCTION page_checksum(IN page bytea, IN blkno int4);
+CREATE FUNCTION page_checksum(IN page bytea, IN blkno int8)
+RETURNS smallint
+AS 'MODULE_PATHNAME', 'page_checksum'
+LANGUAGE C STRICT PARALLEL SAFE;
+
+--
+-- bt_page_stats()
+--
+DROP FUNCTION bt_page_stats(text, int4);
+CREATE FUNCTION bt_page_stats(IN relname text, IN blkno int8,
+ OUT blkno int8,
+ OUT type "char",
+ OUT live_items int4,
+ OUT dead_items int4,
+ OUT avg_item_size int4,
+ OUT page_size int4,
+ OUT free_size int4,
+ OUT btpo_prev int8,
+ OUT btpo_next int8,
+ OUT btpo int4,
+ OUT btpo_flags int4)
+AS 'MODULE_PATHNAME', 'bt_page_stats'
+LANGUAGE C STRICT PARALLEL SAFE;
+
+--
+-- bt_page_items()
+--
+DROP FUNCTION bt_page_items(text, int4);
+CREATE FUNCTION bt_page_items(IN relname text, IN blkno int8,
+ OUT itemoffset smallint,
+ OUT ctid tid,
+ OUT itemlen smallint,
+ OUT nulls bool,
+ OUT vars bool,
+ OUT data text,
+ OUT dead boolean,
+ OUT htid tid,
+ OUT tids tid[])
+RETURNS SETOF record
+AS 'MODULE_PATHNAME', 'bt_page_items'
+LANGUAGE C STRICT PARALLEL SAFE;
+
+--
+-- brin_page_items()
+--
+DROP FUNCTION brin_page_items(IN page bytea, IN index_oid regclass);
+CREATE FUNCTION brin_page_items(IN page bytea, IN index_oid regclass,
+ OUT itemoffset int,
+ OUT blknum int8,
+ OUT attnum int,
+ OUT allnulls bool,
+ OUT hasnulls bool,
+ OUT placeholder bool,
+ OUT value text)
+RETURNS SETOF record
+AS 'MODULE_PATHNAME', 'brin_page_items'
+LANGUAGE C STRICT PARALLEL SAFE;
diff --git a/contrib/pageinspect/pageinspect.control
b/contrib/pageinspect/pageinspect.control
index f8cdf526c6..bd716769a1 100644
--- a/contrib/pageinspect/pageinspect.control
+++ b/contrib/pageinspect/pageinspect.control
@@ -1,5 +1,5 @@
# pageinspect extension
comment = 'inspect the contents of database pages at a low level'
-default_version = '1.8'
+default_version = '1.9'
module_pathname = '$libdir/pageinspect'
relocatable = true
diff --git a/contrib/pageinspect/rawpage.c b/contrib/pageinspect/rawpage.c
index c0181506a5..9ac1510726 100644
--- a/contrib/pageinspect/rawpage.c
+++ b/contrib/pageinspect/rawpage.c
@@ -32,7 +32,7 @@
PG_MODULE_MAGIC;
static bytea *get_raw_page_internal(text *relname, ForkNumber forknum,
-
BlockNumber blkno);
+ int64
blkno);
/*
@@ -46,7 +46,7 @@ Datum
get_raw_page(PG_FUNCTION_ARGS)
{
text *relname = PG_GETARG_TEXT_PP(0);
- uint32 blkno = PG_GETARG_UINT32(1);
+ int64 blkno = PG_GETARG_INT64(1);
bytea *raw_page;
/*
@@ -76,7 +76,7 @@ get_raw_page_fork(PG_FUNCTION_ARGS)
{
text *relname = PG_GETARG_TEXT_PP(0);
text *forkname = PG_GETARG_TEXT_PP(1);
- uint32 blkno = PG_GETARG_UINT32(2);
+ int64 blkno = PG_GETARG_INT64(2);
bytea *raw_page;
ForkNumber forknum;
@@ -91,7 +91,7 @@ get_raw_page_fork(PG_FUNCTION_ARGS)
* workhorse
*/
static bytea *
-get_raw_page_internal(text *relname, ForkNumber forknum, BlockNumber blkno)
+get_raw_page_internal(text *relname, ForkNumber forknum, int64 blkno)
{
bytea *raw_page;
RangeVar *relrv;
@@ -144,11 +144,16 @@ get_raw_page_internal(text *relname, ForkNumber forknum,
BlockNumber blkno)
(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 >= RelationGetNumberOfBlocksInFork(rel, forknum))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("block number %u is out of range for
relation \"%s\"",
- blkno,
RelationGetRelationName(rel))));
+ errmsg("block number %llu is out of range for
relation \"%s\"",
+ (long long unsigned) blkno,
RelationGetRelationName(rel))));
/* Initialize buffer to copy to */
raw_page = (bytea *) palloc(BLCKSZ + VARHDRSZ);
@@ -298,7 +303,7 @@ Datum
page_checksum(PG_FUNCTION_ARGS)
{
bytea *raw_page = PG_GETARG_BYTEA_P(0);
- uint32 blkno = PG_GETARG_INT32(1);
+ int64 blkno = PG_GETARG_INT64(1);
int raw_page_size;
PageHeader page;
@@ -307,6 +312,11 @@ page_checksum(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to use raw page
functions")));
+ if (blkno < 0 || blkno > MaxBlockNumber)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("invalid block number")));
+
raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
/*
diff --git a/doc/src/sgml/pageinspect.sgml b/doc/src/sgml/pageinspect.sgml
index 687c3606ba..233c812345 100644
--- a/doc/src/sgml/pageinspect.sgml
+++ b/doc/src/sgml/pageinspect.sgml
@@ -19,7 +19,7 @@ <title>General Functions</title>
<variablelist>
<varlistentry>
<term>
- <function>get_raw_page(relname text, fork text, blkno int) returns
bytea</function>
+ <function>get_raw_page(relname text, fork text, blkno bigint) returns
bytea</function>
<indexterm>
<primary>get_raw_page</primary>
</indexterm>
@@ -40,7 +40,7 @@ <title>General Functions</title>
<varlistentry>
<term>
- <function>get_raw_page(relname text, blkno int) returns bytea</function>
+ <function>get_raw_page(relname text, blkno bigint) returns
bytea</function>
</term>
<listitem>
@@ -91,7 +91,7 @@ <title>General Functions</title>
<varlistentry>
<term>
- <function>page_checksum(page bytea, blkno int4) returns
smallint</function>
+ <function>page_checksum(page bytea, blkno bigint) returns
smallint</function>
<indexterm>
<primary>page_checksum</primary>
</indexterm>
@@ -315,7 +315,7 @@ <title>B-Tree Functions</title>
<varlistentry>
<term>
- <function>bt_page_stats(relname text, blkno int) returns record</function>
+ <function>bt_page_stats(relname text, blkno bigint) returns
record</function>
<indexterm>
<primary>bt_page_stats</primary>
</indexterm>
@@ -346,7 +346,7 @@ <title>B-Tree Functions</title>
<varlistentry>
<term>
- <function>bt_page_items(relname text, blkno int) returns setof
record</function>
+ <function>bt_page_items(relname text, blkno bigint) returns setof
record</function>
<indexterm>
<primary>bt_page_items</primary>
</indexterm>
@@ -756,7 +756,7 @@ <title>Hash Functions</title>
<varlistentry>
<term>
- <function>hash_bitmap_info(index oid, blkno int) returns record</function>
+ <function>hash_bitmap_info(index oid, blkno bigint) returns
record</function>
<indexterm>
<primary>hash_bitmap_info</primary>
</indexterm>
--
2.29.2