Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite9986a8

Browse files
committed
Make gincostestimate() cope with hypothetical GIN indexes.
We tried to fetch statistics data from the index metapage, which does notwork if the index isn't actually present. If the index is hypothetical,instead extrapolate some plausible internal statistics based on the indexpage count provided by the index-advisor plugin.There was already some code in gincostestimate() to invent internal statsin this way, but since it was only meant as a stopgap for pre-9.1 GINindexes 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 pagesas 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 eitherdirection. Instead, let's estimate 100 entries per entry page.Perhaps someday somebody will want the index advisor to be able to providethese numbers more directly, but for the moment this should serve.Problem report and initial patch by Julien Rouhaud; modified by me toinvent less-bogus internal statistics. Back-patch to all supportedbranches, since we've supported index advisors since 9.0.
1 parent181346c commite9986a8

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

‎src/backend/utils/adt/selfuncs.c

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7254,40 +7254,58 @@ gincostestimate(PG_FUNCTION_ARGS)
72547254
qinfos=deconstruct_indexquals(path);
72557255

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

7280+
numEntryPages=ginStats.nEntryPages;
7281+
numDataPages=ginStats.nDataPages;
7282+
numPendingPages=ginStats.nPendingPages;
7283+
numEntries=ginStats.nEntries;
7284+
72847285
numEntryPages=ceil(numEntryPages*scale);
72857286
numDataPages=ceil(numDataPages*scale);
72867287
numEntries=ceil(numEntries*scale);
72877288
/* ensure we didn't round up too much */
72887289
numEntryPages=Min(numEntryPages,numPages);
72897290
numDataPages=Min(numDataPages,numPages-numEntryPages);
72907291
}
7292+
else
7293+
{
7294+
/*
7295+
* It's a hypothetical index, or perhaps an index created pre-9.1 and
7296+
* never vacuumed since upgrading. Invent some plausible internal
7297+
* statistics based on the index page count. We estimate that 90% of
7298+
* the index is entry pages, and the rest is data pages. Estimate 100
7299+
* entries per entry page; this is rather bogus since it'll depend on
7300+
* the size of the keys, but it's more robust than trying to predict
7301+
* the number of entries per heap tuple.
7302+
*/
7303+
numPages=Max(numPages,10);
7304+
numEntryPages=floor(numPages*0.90);
7305+
numDataPages=numPages-numEntryPages;
7306+
numPendingPages=0;
7307+
numEntries=floor(numEntryPages*100);
7308+
}
72917309

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp