Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b391d88

Browse files
committed
Allow pg_set_relation_stats() to set relpages to -1.
While the default value for relpages is 0, if a partitioned table with at least one child has been analyzed, then the partititoned table will have a relpages value of -1. Author: Corey Huinker Discussion: https://postgr.es/m/CADkLM=fajh1Lpcyr_XsMmq-9Z=SGk-u+_Zeac7Pt0RAN3uiVCg@mail.gmail.com
1 parent 1bd4bc8 commit b391d88

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

doc/src/sgml/func.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30197,7 +30197,7 @@ DETAIL: Make sure pg_wal_replay_wait() isn't called within a transaction with a
3019730197
</para>
3019830198
<para>
3019930199
The value of <structfield>relpages</structfield> must be greater than
30200-
or equal to <literal>0</literal>,
30200+
or equal to <literal>-1</literal>,
3020130201
<structfield>reltuples</structfield> must be greater than or equal to
3020230202
<literal>-1.0</literal>, and <structfield>relallvisible</structfield>
3020330203
must be greater than or equal to <literal>0</literal>.

src/backend/statistics/relation_stats.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,16 @@ relation_statistics_update(FunctionCallInfo fcinfo, int elevel)
9999
{
100100
int32 relpages = PG_GETARG_INT32(RELPAGES_ARG);
101101

102-
if (relpages < 0)
102+
/*
103+
* Partitioned tables may have relpages=-1. Note: for relations with
104+
* no storage, relpages=-1 is not used consistently, but must be
105+
* supported here.
106+
*/
107+
if (relpages < -1)
103108
{
104109
ereport(elevel,
105110
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
106-
errmsg("relpages cannot be < 0")));
111+
errmsg("relpages cannot be < -1")));
107112
table_close(crel, RowExclusiveLock);
108113
return false;
109114
}

src/test/regress/expected/stats_import.out

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,45 @@ SELECT
135135
'stats_import.testview'::regclass);
136136
ERROR: cannot modify statistics for relation "testview"
137137
DETAIL: This operation is not supported for views.
138+
-- relpages may be -1 for partitioned tables
139+
CREATE TABLE stats_import.part_parent ( i integer ) PARTITION BY RANGE(i);
140+
CREATE TABLE stats_import.part_child_1
141+
PARTITION OF stats_import.part_parent
142+
FOR VALUES FROM (0) TO (10);
143+
ANALYZE stats_import.part_parent;
144+
SELECT relpages
145+
FROM pg_class
146+
WHERE oid = 'stats_import.part_parent'::regclass;
147+
relpages
148+
----------
149+
-1
150+
(1 row)
151+
152+
-- although partitioned tables have no storage, setting relpages to a
153+
-- positive value is still allowed
154+
SELECT
155+
pg_catalog.pg_set_relation_stats(
156+
relation => 'stats_import.part_parent'::regclass,
157+
relpages => 2::integer);
158+
pg_set_relation_stats
159+
-----------------------
160+
t
161+
(1 row)
162+
163+
-- nothing stops us from setting it to -1
164+
SELECT
165+
pg_catalog.pg_set_relation_stats(
166+
relation => 'stats_import.part_parent'::regclass,
167+
relpages => -1::integer);
168+
pg_set_relation_stats
169+
-----------------------
170+
t
171+
(1 row)
172+
138173
DROP SCHEMA stats_import CASCADE;
139-
NOTICE: drop cascades to 4 other objects
174+
NOTICE: drop cascades to 5 other objects
140175
DETAIL: drop cascades to type stats_import.complex_type
141176
drop cascades to table stats_import.test
142177
drop cascades to sequence stats_import.testseq
143178
drop cascades to view stats_import.testview
179+
drop cascades to table stats_import.part_parent

src/test/regress/sql/stats_import.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,29 @@ SELECT
9595
pg_catalog.pg_clear_relation_stats(
9696
'stats_import.testview'::regclass);
9797

98+
-- relpages may be -1 for partitioned tables
99+
CREATE TABLE stats_import.part_parent ( i integer ) PARTITION BY RANGE(i);
100+
CREATE TABLE stats_import.part_child_1
101+
PARTITION OF stats_import.part_parent
102+
FOR VALUES FROM (0) TO (10);
103+
104+
ANALYZE stats_import.part_parent;
105+
106+
SELECT relpages
107+
FROM pg_class
108+
WHERE oid = 'stats_import.part_parent'::regclass;
109+
110+
-- although partitioned tables have no storage, setting relpages to a
111+
-- positive value is still allowed
112+
SELECT
113+
pg_catalog.pg_set_relation_stats(
114+
relation => 'stats_import.part_parent'::regclass,
115+
relpages => 2::integer);
116+
117+
-- nothing stops us from setting it to -1
118+
SELECT
119+
pg_catalog.pg_set_relation_stats(
120+
relation => 'stats_import.part_parent'::regclass,
121+
relpages => -1::integer);
122+
98123
DROP SCHEMA stats_import CASCADE;

0 commit comments

Comments
 (0)