Skip to content

Commit 9de2cc4

Browse files
committed
Fix confusion about data type of pg_class.relpages and relallvisible.
Although they're exposed as int4 in pg_class, relpages and relallvisible are really of type BlockNumber, that is uint32. Correct type puns in relation_statistics_update() and remove inappropriate range-checks. The type puns are only cosmetic issues, but the range checks would cause failures with huge relations. Reported-by: Tom Lane <tgl@sss.pgh.pa.us> Author: Corey Huinker <corey.huinker@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/614341.1740269035@sss.pgh.pa.us
1 parent e889422 commit 9de2cc4

File tree

1 file changed

+13
-40
lines changed

1 file changed

+13
-40
lines changed

src/backend/statistics/relation_stats.c

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
#include "utils/fmgrprotos.h"
2525
#include "utils/syscache.h"
2626

27-
#define DEFAULT_RELPAGES Int32GetDatum(0)
28-
#define DEFAULT_RELTUPLES Float4GetDatum(-1.0)
29-
#define DEFAULT_RELALLVISIBLE Int32GetDatum(0)
3027

3128
/*
3229
* Positional argument numbers, names, and types for
@@ -60,40 +57,25 @@ static bool relation_statistics_update(FunctionCallInfo fcinfo, int elevel,
6057
static bool
6158
relation_statistics_update(FunctionCallInfo fcinfo, int elevel, bool inplace)
6259
{
60+
bool result = true;
6361
Oid reloid;
6462
Relation crel;
65-
int32 relpages = DEFAULT_RELPAGES;
63+
BlockNumber relpages = 0;
6664
bool update_relpages = false;
67-
float reltuples = DEFAULT_RELTUPLES;
65+
float reltuples = 0;
6866
bool update_reltuples = false;
69-
int32 relallvisible = DEFAULT_RELALLVISIBLE;
67+
BlockNumber relallvisible = 0;
7068
bool update_relallvisible = false;
71-
bool result = true;
7269

7370
if (!PG_ARGISNULL(RELPAGES_ARG))
7471
{
75-
relpages = PG_GETARG_INT32(RELPAGES_ARG);
76-
77-
/*
78-
* Partitioned tables may have relpages=-1. Note: for relations with
79-
* no storage, relpages=-1 is not used consistently, but must be
80-
* supported here.
81-
*/
82-
if (relpages < -1)
83-
{
84-
ereport(elevel,
85-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
86-
errmsg("relpages cannot be < -1")));
87-
result = false;
88-
}
89-
else
90-
update_relpages = true;
72+
relpages = PG_GETARG_UINT32(RELPAGES_ARG);
73+
update_relpages = true;
9174
}
9275

9376
if (!PG_ARGISNULL(RELTUPLES_ARG))
9477
{
9578
reltuples = PG_GETARG_FLOAT4(RELTUPLES_ARG);
96-
9779
if (reltuples < -1.0)
9880
{
9981
ereport(elevel,
@@ -107,17 +89,8 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel, bool inplace)
10789

10890
if (!PG_ARGISNULL(RELALLVISIBLE_ARG))
10991
{
110-
relallvisible = PG_GETARG_INT32(RELALLVISIBLE_ARG);
111-
112-
if (relallvisible < 0)
113-
{
114-
ereport(elevel,
115-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
116-
errmsg("relallvisible cannot be < 0")));
117-
result = false;
118-
}
119-
else
120-
update_relallvisible = true;
92+
relallvisible = PG_GETARG_UINT32(RELALLVISIBLE_ARG);
93+
update_relallvisible = true;
12194
}
12295

12396
stats_check_required_arg(fcinfo, relarginfo, RELATION_ARG);
@@ -201,7 +174,7 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel, bool inplace)
201174
if (update_relpages && relpages != pgcform->relpages)
202175
{
203176
replaces[nreplaces] = Anum_pg_class_relpages;
204-
values[nreplaces] = Int32GetDatum(relpages);
177+
values[nreplaces] = UInt32GetDatum(relpages);
205178
nreplaces++;
206179
}
207180

@@ -215,7 +188,7 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel, bool inplace)
215188
if (update_relallvisible && relallvisible != pgcform->relallvisible)
216189
{
217190
replaces[nreplaces] = Anum_pg_class_relallvisible;
218-
values[nreplaces] = Int32GetDatum(relallvisible);
191+
values[nreplaces] = UInt32GetDatum(relallvisible);
219192
nreplaces++;
220193
}
221194

@@ -263,11 +236,11 @@ pg_clear_relation_stats(PG_FUNCTION_ARGS)
263236

264237
newfcinfo->args[0].value = PG_GETARG_OID(0);
265238
newfcinfo->args[0].isnull = PG_ARGISNULL(0);
266-
newfcinfo->args[1].value = DEFAULT_RELPAGES;
239+
newfcinfo->args[1].value = UInt32GetDatum(0);
267240
newfcinfo->args[1].isnull = false;
268-
newfcinfo->args[2].value = DEFAULT_RELTUPLES;
241+
newfcinfo->args[2].value = Float4GetDatum(-1.0);
269242
newfcinfo->args[2].isnull = false;
270-
newfcinfo->args[3].value = DEFAULT_RELALLVISIBLE;
243+
newfcinfo->args[3].value = UInt32GetDatum(0);
271244
newfcinfo->args[3].isnull = false;
272245

273246
relation_statistics_update(newfcinfo, ERROR, false);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy