Skip to content

Commit 6736da5

Browse files
committed
Make the blkno arguments bigints instead of int4s. A signed int4 is not
large enough for block numbers higher than 2^31. The old pre-FSM-rewrite pg_freespacemap implementation got this right. While we're at it, remove some unnecessary #includes.
1 parent f10a86e commit 6736da5

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

contrib/pg_freespacemap/pg_freespacemap.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
* pg_freespacemap.c
44
* display contents of a free space map
55
*
6-
* $PostgreSQL: pgsql/contrib/pg_freespacemap/pg_freespacemap.c,v 1.11 2008/09/30 11:17:07 heikki Exp $
6+
* $PostgreSQL: pgsql/contrib/pg_freespacemap/pg_freespacemap.c,v 1.12 2008/10/02 12:20:50 heikki Exp $
77
*-------------------------------------------------------------------------
88
*/
99
#include "postgres.h"
1010

1111
#include "access/heapam.h"
12-
#include "access/htup.h"
13-
#include "catalog/pg_type.h"
1412
#include "funcapi.h"
13+
#include "storage/block.h"
1514
#include "storage/freespace.h"
16-
#include "utils/builtins.h"
1715

1816

1917
PG_MODULE_MAGIC;
@@ -31,13 +29,13 @@ Datum
3129
pg_freespace(PG_FUNCTION_ARGS)
3230
{
3331
Oid relid = PG_GETARG_OID(0);
34-
uint32 blkno = PG_GETARG_UINT32(1);
32+
int64 blkno = PG_GETARG_INT64(1);
3533
int16 freespace;
3634
Relation rel;
3735

3836
rel = relation_open(relid, AccessShareLock);
3937

40-
if (!BlockNumberIsValid(blkno))
38+
if (blkno < 0 || blkno > MaxBlockNumber)
4139
ereport(ERROR,
4240
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4341
errmsg("invalid block number")));
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
/* $PostgreSQL: pgsql/contrib/pg_freespacemap/pg_freespacemap.sql.in,v 1.10 2008/09/30 11:17:07 heikki Exp $ */
1+
/* $PostgreSQL: pgsql/contrib/pg_freespacemap/pg_freespacemap.sql.in,v 1.11 2008/10/02 12:20:50 heikki Exp $ */
22

33
-- Adjust this setting to control where the objects get created.
44
SET search_path = public;
55

66

77
-- Register the C function.
8-
CREATE OR REPLACE FUNCTION pg_freespace(regclass, int4)
8+
CREATE OR REPLACE FUNCTION pg_freespace(regclass, bigint)
99
RETURNS int2
1010
AS 'MODULE_PATHNAME', 'pg_freespace'
1111
LANGUAGE C;
1212

1313
-- pg_freespace shows the recorded space avail at each block in a relation
1414
CREATE OR REPLACE FUNCTION
15-
pg_freespace(rel regclass, blkno OUT int4, avail OUT int2)
15+
pg_freespace(rel regclass, blkno OUT bigint, avail OUT int2)
1616
RETURNS SETOF RECORD
1717
AS $$
18-
SELECT blkno::int4, pg_freespace($1, blkno::int4) AS avail
18+
SELECT blkno, pg_freespace($1, blkno) AS avail
1919
FROM generate_series(0, pg_relation_size($1) / current_setting('block_size')::bigint - 1) AS blkno;
2020
$$
2121
LANGUAGE SQL;
2222

2323

2424
-- Don't want these to be available to public.
25-
REVOKE ALL ON FUNCTION pg_freespace(regclass, int4) FROM PUBLIC;
25+
REVOKE ALL ON FUNCTION pg_freespace(regclass, bigint) FROM PUBLIC;
2626
REVOKE ALL ON FUNCTION pg_freespace(regclass) FROM PUBLIC;

doc/src/sgml/pgfreespacemap.sgml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/pgfreespacemap.sgml,v 2.4 2008/10/02 10:26:51 heikki Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/pgfreespacemap.sgml,v 2.5 2008/10/02 12:20:50 heikki Exp $ -->
22

33
<sect1 id="pgfreespacemap">
44
<title>pg_freespacemap</title>
@@ -41,13 +41,13 @@
4141

4242
<varlistentry>
4343
<term>
44-
<function>pg_freespace(rel regclass IN, blkno OUT int4, avail OUT int2)</function>
44+
<function>pg_freespace(rel regclass IN, blkno OUT bigint, avail OUT int2)</function>
4545
</term>
4646

4747
<listitem>
4848
<para>
4949
Displays the the amount of free space on each page of the relation,
50-
according to the FSM. A set of <literal>(blkno int4, avail int2)</>
50+
according to the FSM. A set of <literal>(blkno bigint, avail int2)</>
5151
tuples is returned, one tuple for each page in the relation.
5252
</para>
5353
</listitem>

0 commit comments

Comments
 (0)