Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid having vacuum set reltuples to 0 on non-empty relations in the
presence of page pins, which leads to serious estimation errors in the planner. This particularly affects small heavily-accessed tables, especially where locking (e.g. from FK constraints) forces frequent vacuums for mxid cleanup. Fix by keeping separate track of pages whose live tuples were actually counted vs. pages that were only scanned for freezing purposes. Thus, reltuples can only be set to 0 if all pages of the relation were actually counted. Backpatch to all supported versions. Per bug #14057 from Nicolas Baccelli, analyzed by me. Discussion: https://postgr.es/m/20160331103739.8956.94469@wrigleys.postgresql.org
- Loading branch information
1 parent
41306a5
commit 9b626f6
Showing
4 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
Parsed test spec with 2 sessions | ||
|
||
starting permutation: modify vac stats | ||
step modify: | ||
insert into smalltbl select max(id)+1 from smalltbl; | ||
delete from smalltbl where id in (select min(id) from smalltbl); | ||
|
||
step vac: | ||
vacuum smalltbl; | ||
|
||
step stats: | ||
select relpages, reltuples from pg_class | ||
where oid='smalltbl'::regclass; | ||
|
||
relpages reltuples | ||
|
||
1 20 | ||
|
||
starting permutation: modify open fetch1 vac close stats | ||
step modify: | ||
insert into smalltbl select max(id)+1 from smalltbl; | ||
delete from smalltbl where id in (select min(id) from smalltbl); | ||
|
||
step open: | ||
begin; | ||
declare c1 cursor for select * from smalltbl; | ||
|
||
step fetch1: | ||
fetch next from c1; | ||
|
||
id | ||
|
||
2 | ||
step vac: | ||
vacuum smalltbl; | ||
|
||
step close: | ||
commit; | ||
|
||
step stats: | ||
select relpages, reltuples from pg_class | ||
where oid='smalltbl'::regclass; | ||
|
||
relpages reltuples | ||
|
||
1 20 | ||
|
||
starting permutation: modify vac stats | ||
step modify: | ||
insert into smalltbl select max(id)+1 from smalltbl; | ||
delete from smalltbl where id in (select min(id) from smalltbl); | ||
|
||
step vac: | ||
vacuum smalltbl; | ||
|
||
step stats: | ||
select relpages, reltuples from pg_class | ||
where oid='smalltbl'::regclass; | ||
|
||
relpages reltuples | ||
|
||
1 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Test for vacuum's handling of reltuples when pages are skipped due | ||
# to page pins. We absolutely need to avoid setting reltuples=0 in | ||
# such cases, since that interferes badly with planning. | ||
|
||
setup { | ||
create table smalltbl | ||
as select i as id from generate_series(1,20) i; | ||
alter table smalltbl set (autovacuum_enabled = off); | ||
} | ||
setup { | ||
vacuum analyze smalltbl; | ||
} | ||
|
||
teardown { | ||
drop table smalltbl; | ||
} | ||
|
||
session "worker" | ||
step "open" { | ||
begin; | ||
declare c1 cursor for select * from smalltbl; | ||
} | ||
step "fetch1" { | ||
fetch next from c1; | ||
} | ||
step "close" { | ||
commit; | ||
} | ||
step "stats" { | ||
select relpages, reltuples from pg_class | ||
where oid='smalltbl'::regclass; | ||
} | ||
|
||
session "vacuumer" | ||
step "vac" { | ||
vacuum smalltbl; | ||
} | ||
step "modify" { | ||
insert into smalltbl select max(id)+1 from smalltbl; | ||
delete from smalltbl where id in (select min(id) from smalltbl); | ||
} | ||
|
||
permutation "modify" "vac" "stats" | ||
permutation "modify" "open" "fetch1" "vac" "close" "stats" | ||
permutation "modify" "vac" "stats" |