Re: [HACKERS] VACUUM and ANALYZE disagreeing on what reltuples means
Tomas Vondra <tomas.vondra@2ndquadrant.com>
From: Tomas Vondra <tomas.vondra@2ndquadrant.com>
To: David Steele <david@pgmasters.net>, Tom Lane <tgl@sss.pgh.pa.us>
Cc: Stephen Frost <sfrost@snowman.net>,
Michael Paquier <michael.paquier@gmail.com>,
Haribabu Kommi <kommi.haribabu@gmail.com>,
"pgsql-hackers@postgresql.org" <pgsql-hackers@postgresql.org>
Date: 2018-03-20T20:14:04Z
Lists: pgsql-hackers
Attachments
- 0001-Compute-index_tuples-correctly-in-bloom-and-spgist.patch.gz (application/gzip) patch 0001
- 0002-Unify-the-definition-of-reltuples-in-VACUUM-ANALYZE-.patch.gz (application/gzip) patch 0002
Hi,
So here is an updated version of the patch/fix, addressing the remaining
issues in v3 posted by Tom in November.
The 0001 part is actually a bugfix in bloom and spgist index AM, which
did something like this:
reltuples = IndexBuildHeapScan(...)
result->heap_tuples = result->index_tuples = reltuples;
That is, these two AMs simply used the number of heap rows for the
index. That does not work for partial indexes, of course, where the
correct index reltuples value is likely much lower.
0001 fixes this by tracking the number of actually indexed rows in the
build states, just like in the other index AMs.
A VACUUM or ANALYZE will fix the estimate, of course, but for tables
that are not changing very much it may take quite a while. So I think
this is something we definitely need to back-patch.
The 0002 part is the main part, unifying the definition of reltuples on
three main places:
a) acquire_sample_rows (ANALYZE)
b) lazy_scan_heap (VACUUM)
c) IndexBuildHeapRangeScan (CREATE INDEX)
As the ANALYZE case seems the most constrained, the other two places
were updated to use the same criteria for which rows to include in the
reltuples estimate:
* HEAPTUPLE_LIVE
* HEAPTUPLE_INSERT_IN_PROGRESS (same transaction)
* HEAPTUPLE_DELETE_IN_PROGRESS (not the same trasaction)
This resolves the issue with oscillating reltuples estimates, produced
by VACUUM and ANALYZE (with many non-removable dead tuples).
I've checked all IndexBuildHeapRangeScan callers, and none of them is
using the reltuples estimate for anything except for passing it to
index_update_stats. Aside from the bug fixed in 0001, of course.
regards
--
Tomas Vondra http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Commits
-
Sync up our various ways of estimating pg_class.reltuples.
- 7c91a0364fcf 11.0 landed
-
Fix tuple counting in SP-GiST index build.
- 649f1792508f 11.0 landed
- eee190da7eeb 9.5.13 landed
- db35bf507ffd 9.6.9 landed
- bf14575c840f 10.4 landed
- 7f6f8ccd976d 9.4.18 landed
- 46f80803a127 9.3.23 landed
-
Fix errors in contrib/bloom index build.
- c35b47286960 11.0 landed
- df90401556ce 9.6.9 landed
- 76e2b5ae4151 10.4 landed
-
When updating reltuples after ANALYZE, just extrapolate from our sample.
- d44ce7b1afdb 9.3.23 landed
- d04900de7d0c 11.0 landed
- c9414e7867f7 9.5.13 landed
- c2c4bc628bbe 9.6.9 landed
- 25a2ba35edbc 9.4.18 landed
- 1bfb5672306d 10.4 landed
-
Avoid holding AutovacuumScheduleLock while rechecking table statistics.
- 5328b6135756 9.3.23 landed
- 95f08d32dec4 9.4.18 landed
- 231329a17564 9.5.13 landed
- 4b0e717053e3 9.6.9 landed
- 4460964aedaa 10.4 landed
- 38f7831d703b 11.0 landed