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

Commit 7fb008c

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 95708e1 commit 7fb008c

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
@@ -7260,40 +7260,58 @@ gincostestimate(PG_FUNCTION_ARGS)
72607260
qinfos = deconstruct_indexquals(path);
72617261

72627262
/*
7263-
* Obtain statistic information from the meta page
7263+
* Obtain statistical information from the meta page, if possible. Else
7264+
* set ginStats to zeroes, and we'll cope below.
72647265
*/
7265-
indexRel = index_open(index->indexoid, AccessShareLock);
7266-
ginGetStats(indexRel, &ginStats);
7267-
index_close(indexRel, AccessShareLock);
7268-
7269-
numEntryPages = ginStats.nEntryPages;
7270-
numDataPages = ginStats.nDataPages;
7271-
numPendingPages = ginStats.nPendingPages;
7272-
numEntries = ginStats.nEntries;
7273-
7274-
/*
7275-
* nPendingPages can be trusted, but the other fields are as of the last
7276-
* VACUUM. Scale them by the ratio numPages / nTotalPages to account for
7277-
* growth since then. If the fields are zero (implying no VACUUM at all,
7278-
* and an index created pre-9.1), assume all pages are entry pages.
7279-
*/
7280-
if (ginStats.nTotalPages == 0 || ginStats.nEntryPages == 0)
7266+
if (!index->hypothetical)
72817267
{
7282-
numEntryPages = numPages;
7283-
numDataPages = 0;
7284-
numEntries = numTuples; /* bogus, but no other info available */
7268+
indexRel = index_open(index->indexoid, AccessShareLock);
7269+
ginGetStats(indexRel, &ginStats);
7270+
index_close(indexRel, AccessShareLock);
72857271
}
72867272
else
72877273
{
7274+
memset(&ginStats, 0, sizeof(ginStats));
7275+
}
7276+
7277+
if (ginStats.nTotalPages > 0 && ginStats.nEntryPages > 0 && numPages > 0)
7278+
{
7279+
/*
7280+
* We got valid stats. nPendingPages can be trusted, but the other
7281+
* fields are data as of the last VACUUM. Scale them by the ratio
7282+
* numPages / nTotalPages to account for growth since then.
7283+
*/
72887284
double scale = numPages / ginStats.nTotalPages;
72897285

7286+
numEntryPages = ginStats.nEntryPages;
7287+
numDataPages = ginStats.nDataPages;
7288+
numPendingPages = ginStats.nPendingPages;
7289+
numEntries = ginStats.nEntries;
7290+
72907291
numEntryPages = ceil(numEntryPages * scale);
72917292
numDataPages = ceil(numDataPages * scale);
72927293
numEntries = ceil(numEntries * scale);
72937294
/* ensure we didn't round up too much */
72947295
numEntryPages = Min(numEntryPages, numPages);
72957296
numDataPages = Min(numDataPages, numPages - numEntryPages);
72967297
}
7298+
else
7299+
{
7300+
/*
7301+
* It's a hypothetical index, or perhaps an index created pre-9.1 and
7302+
* never vacuumed since upgrading. Invent some plausible internal
7303+
* statistics based on the index page count. We estimate that 90% of
7304+
* the index is entry pages, and the rest is data pages. Estimate 100
7305+
* entries per entry page; this is rather bogus since it'll depend on
7306+
* the size of the keys, but it's more robust than trying to predict
7307+
* the number of entries per heap tuple.
7308+
*/
7309+
numPages = Max(numPages, 10);
7310+
numEntryPages = floor(numPages * 0.90);
7311+
numDataPages = numPages - numEntryPages;
7312+
numPendingPages = 0;
7313+
numEntries = floor(numEntryPages * 100);
7314+
}
72977315

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

0 commit comments

Comments
 (0)