Skip to content

Commit 7f9f8d9

Browse files
jeff-davishari90
authored andcommitted
CREATE INDEX: don't update table stats if autovacuum=off.
yb conflict resolutions: - src/test/regress/expected/stats_import.out >@@ -12,7 +12,36 @@ CREATE TABLE stats_import.test( <@@ -12,7 +12,35 @@ CREATE TABLE stats_import.test( >+ 'relallvisible', 24::integer, >+ 'relallfrozen', 27::integer); <+ 'relallvisible', 24::integer); >+SELECT relname, relpages, reltuples, relallvisible, relallfrozen <+SELECT relname, relpages, reltuples, relallvisible >+ relname | relpages | reltuples | relallvisible | relallfrozen >+---------+----------+-----------+---------------+-------------- >+ test | 18 | 21 | 24 | 27 <+ relname | relpages | reltuples | relallvisible <+---------+----------+-----------+--------------- <+ test | 18 | 21 | 24] Cause: Missing 99f8f3f: `relallfrozen` was introduced. Resolution: Feature does not exist in pg15 - src/test/regress/sql/stats_import.sql >@@ -15,8 +15,25 @@ CREATE TABLE stats_import.test( <@@ -15,8 +15,24 @@ CREATE TABLE stats_import.test( >+ 'relallvisible', 24::integer, >+ 'relallfrozen', 27::integer); <+ 'relallvisible', 24::integer); >+SELECT relname, relpages, reltuples, relallvisible, relallfrozen <+SELECT relname, relpages, reltuples, relallvisible Cause: Missing 99f8f3f: `relallfrozen` was introduced. Resolution: Feature does not exist in pg15 We previously fixed this for binary upgrade in 71b6617, but a similar problem remained when dumping statistics without data. Fix by not opportunistically updating table stats during CREATE INDEX when autovacuum is disabled. For stats to be stable at all, the server needs to be aware that it should not take every opportunity to update stats. Per discussion, autovacuum=off is a signal that the user expects stats to be stable; though if necessary, we could create a more specific mode in the future. Reported-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> Discussion: https://postgr.es/m/CAExHW5vf9D+8-a5_BEX3y=2y_xY9hiCxV1=C+FnxDvfprWvkng@mail.gmail.com Discussion: https://postgr.es/m/ca81cbf6e6ea2af838df972801ad4da52640a503.camel%40j-davis.com (cherry picked from commit d611f8b)
1 parent 1a9d793 commit 7f9f8d9

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/backend/catalog/index.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include "optimizer/optimizer.h"
6868
#include "parser/parser.h"
6969
#include "pgstat.h"
70+
#include "postmaster/autovacuum.h"
7071
#include "rewrite/rewriteManip.h"
7172
#include "storage/bufmgr.h"
7273
#include "storage/lmgr.h"
@@ -2827,6 +2828,27 @@ index_update_stats(Relation rel,
28272828

28282829
update_stats = reltuples >= 0;
28292830

2831+
/*
2832+
* If autovacuum is off, user may not be expecting table relstats to
2833+
* change. This can be important when restoring a dump that includes
2834+
* statistics, as the table statistics may be restored before the index is
2835+
* created, and we want to preserve the restored table statistics.
2836+
*/
2837+
if (AutoVacuumingActive())
2838+
{
2839+
if (rel->rd_rel->relkind == RELKIND_RELATION ||
2840+
rel->rd_rel->relkind == RELKIND_TOASTVALUE ||
2841+
rel->rd_rel->relkind == RELKIND_MATVIEW)
2842+
{
2843+
StdRdOptions *options = (StdRdOptions *) rel->rd_options;
2844+
2845+
if (options != NULL && !options->autovacuum.enabled)
2846+
update_stats = false;
2847+
}
2848+
}
2849+
else
2850+
update_stats = false;
2851+
28302852
/*
28312853
* Finish I/O and visibility map buffer locks before
28322854
* systable_inplace_update_begin() locks the pg_class buffer. The rd_rel

src/test/regress/expected/stats_import.out

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,35 @@ CREATE TABLE stats_import.test(
1212
arange int4range,
1313
tags text[]
1414
) WITH (autovacuum_enabled = false);
15+
SELECT
16+
pg_catalog.pg_restore_relation_stats(
17+
'relation', 'stats_import.test'::regclass,
18+
'relpages', 18::integer,
19+
'reltuples', 21::real,
20+
'relallvisible', 24::integer);
21+
pg_restore_relation_stats
22+
---------------------------
23+
t
24+
(1 row)
25+
26+
-- CREATE INDEX on a table with autovac disabled should not overwrite
27+
-- stats
1528
CREATE INDEX test_i ON stats_import.test(id);
29+
SELECT relname, relpages, reltuples, relallvisible
30+
FROM pg_class
31+
WHERE oid = 'stats_import.test'::regclass
32+
ORDER BY relname;
33+
relname | relpages | reltuples | relallvisible
34+
---------+----------+-----------+---------------
35+
test | 18 | 21 | 24
36+
(1 row)
37+
38+
SELECT pg_clear_relation_stats('stats_import.test'::regclass);
39+
pg_clear_relation_stats
40+
-------------------------
41+
42+
(1 row)
43+
1644
--
1745
-- relstats tests
1846
--

src/test/regress/sql/stats_import.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,24 @@ CREATE TABLE stats_import.test(
1515
tags text[]
1616
) WITH (autovacuum_enabled = false);
1717

18+
SELECT
19+
pg_catalog.pg_restore_relation_stats(
20+
'relation', 'stats_import.test'::regclass,
21+
'relpages', 18::integer,
22+
'reltuples', 21::real,
23+
'relallvisible', 24::integer);
24+
25+
-- CREATE INDEX on a table with autovac disabled should not overwrite
26+
-- stats
1827
CREATE INDEX test_i ON stats_import.test(id);
1928

29+
SELECT relname, relpages, reltuples, relallvisible
30+
FROM pg_class
31+
WHERE oid = 'stats_import.test'::regclass
32+
ORDER BY relname;
33+
34+
SELECT pg_clear_relation_stats('stats_import.test'::regclass);
35+
2036
--
2137
-- relstats tests
2238
--

0 commit comments

Comments
 (0)