Skip to content

Commit ab14e0e

Browse files
committed
Make gincostestimate() cope with hypothetical GIN indexes.
We tried to fetch statistics data from the index metapage, which does not work if the index isn't actually present. If the index is hypothetical, instead extrapolate some plausible internal statistics based on the index page count provided by the index-advisor plugin. There was already some code in gincostestimate() to invent internal stats in this way, but since it was only meant as a stopgap for pre-9.1 GIN indexes that hadn't been vacuumed since upgrading, it was pretty crude. If we want it to support index advisors, we should try a little harder. A small amount of testing says that it's better to estimate the entry pages as 90% of the index, not 100%. Also, estimating the number of entries (keys) as equal to the heap tuple count could be wildly wrong in either direction. Instead, let's estimate 100 entries per entry page. Perhaps someday somebody will want the index advisor to be able to provide these numbers more directly, but for the moment this should serve. Problem report and initial patch by Julien Rouhaud; modified by me to invent less-bogus internal statistics. Back-patch to all supported branches, since we've supported index advisors since 9.0.
1 parent 346cc2f commit ab14e0e

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

src/backend/utils/adt/selfuncs.c

+38-20
Original file line numberDiff line numberDiff line change
@@ -7095,40 +7095,58 @@ gincostestimate(PG_FUNCTION_ARGS)
70957095
GinStatsData ginStats;
70967096

70977097
/*
7098-
* Obtain statistic information from the meta page
7098+
* Obtain statistical information from the meta page, if possible. Else
7099+
* set ginStats to zeroes, and we'll cope below.
70997100
*/
7100-
indexRel = index_open(index->indexoid, AccessShareLock);
7101-
ginGetStats(indexRel, &ginStats);
7102-
index_close(indexRel, AccessShareLock);
7103-
7104-
numEntryPages = ginStats.nEntryPages;
7105-
numDataPages = ginStats.nDataPages;
7106-
numPendingPages = ginStats.nPendingPages;
7107-
numEntries = ginStats.nEntries;
7108-
7109-
/*
7110-
* nPendingPages can be trusted, but the other fields are as of the last
7111-
* VACUUM. Scale them by the ratio numPages / nTotalPages to account for
7112-
* growth since then. If the fields are zero (implying no VACUUM at all,
7113-
* and an index created pre-9.1), assume all pages are entry pages.
7114-
*/
7115-
if (ginStats.nTotalPages == 0 || ginStats.nEntryPages == 0)
7101+
if (!index->hypothetical)
71167102
{
7117-
numEntryPages = numPages;
7118-
numDataPages = 0;
7119-
numEntries = numTuples; /* bogus, but no other info available */
7103+
indexRel = index_open(index->indexoid, AccessShareLock);
7104+
ginGetStats(indexRel, &ginStats);
7105+
index_close(indexRel, AccessShareLock);
71207106
}
71217107
else
71227108
{
7109+
memset(&ginStats, 0, sizeof(ginStats));
7110+
}
7111+
7112+
if (ginStats.nTotalPages > 0 && ginStats.nEntryPages > 0 && numPages > 0)
7113+
{
7114+
/*
7115+
* We got valid stats. nPendingPages can be trusted, but the other
7116+
* fields are data as of the last VACUUM. Scale them by the ratio
7117+
* numPages / nTotalPages to account for growth since then.
7118+
*/
71237119
double scale = numPages / ginStats.nTotalPages;
71247120

7121+
numEntryPages = ginStats.nEntryPages;
7122+
numDataPages = ginStats.nDataPages;
7123+
numPendingPages = ginStats.nPendingPages;
7124+
numEntries = ginStats.nEntries;
7125+
71257126
numEntryPages = ceil(numEntryPages * scale);
71267127
numDataPages = ceil(numDataPages * scale);
71277128
numEntries = ceil(numEntries * scale);
71287129
/* ensure we didn't round up too much */
71297130
numEntryPages = Min(numEntryPages, numPages);
71307131
numDataPages = Min(numDataPages, numPages - numEntryPages);
71317132
}
7133+
else
7134+
{
7135+
/*
7136+
* It's a hypothetical index, or perhaps an index created pre-9.1 and
7137+
* never vacuumed since upgrading. Invent some plausible internal
7138+
* statistics based on the index page count. We estimate that 90% of
7139+
* the index is entry pages, and the rest is data pages. Estimate 100
7140+
* entries per entry page; this is rather bogus since it'll depend on
7141+
* the size of the keys, but it's more robust than trying to predict
7142+
* the number of entries per heap tuple.
7143+
*/
7144+
numPages = Max(numPages, 10);
7145+
numEntryPages = floor(numPages * 0.90);
7146+
numDataPages = numPages - numEntryPages;
7147+
numPendingPages = 0;
7148+
numEntries = floor(numEntryPages * 100);
7149+
}
71327150

71337151
/* In an empty index, numEntries could be zero. Avoid divide-by-zero */
71347152
if (numEntries < 1)

0 commit comments

Comments
 (0)