Skip to content

Commit c644407

Browse files
committed
Provide statistics for hypothetical BRIN indexes
Trying to use hypothetical indexes with BRIN currently fails when trying to access a relation that does not exist when looking for the statistics. With the current API, it is not possible to easily pass a value for pages_per_range down to the hypothetical index, so this makes use of the default value of BRIN_DEFAULT_PAGES_PER_RANGE, which should be fine enough in most cases. Being able to refine or enforce the hypothetical costs in more optimistic ways would require more refactoring by filling in the statistics when building IndexOptInfo in plancat.c. This would involve ABI breakages around the costing routines, something not fit for stable branches. This is broken since 7e534ad, so backpatch down to v10. Author: Julien Rouhaud, Heikki Linnakangas Reviewed-by: Álvaro Herrera, Tom Lane, Michael Paquier Discussion: https://postgr.es/m/CAOBaU_ZH0LKEA8VFCocr6Lpte1ab0b6FpvgS0y4way+RPSXfYg@mail.gmail.com Backpatch-through: 10
1 parent 2c9772f commit c644407

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/backend/utils/adt/selfuncs.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
#include <math.h>
103103

104104
#include "access/brin.h"
105+
#include "access/brin_page.h"
105106
#include "access/gin.h"
106107
#include "access/table.h"
107108
#include "access/tableam.h"
@@ -6884,12 +6885,34 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
68846885
&spc_seq_page_cost);
68856886

68866887
/*
6887-
* Obtain some data from the index itself. A lock should have already
6888-
* been obtained on the index in plancat.c.
6888+
* Obtain some data from the index itself, if possible. Otherwise invent
6889+
* some plausible internal statistics based on the relation page count.
68896890
*/
6890-
indexRel = index_open(index->indexoid, NoLock);
6891-
brinGetStats(indexRel, &statsData);
6892-
index_close(indexRel, NoLock);
6891+
if (!index->hypothetical)
6892+
{
6893+
/*
6894+
* A lock should have already been obtained on the index in plancat.c.
6895+
*/
6896+
indexRel = index_open(index->indexoid, NoLock);
6897+
brinGetStats(indexRel, &statsData);
6898+
index_close(indexRel, NoLock);
6899+
6900+
/* work out the actual number of ranges in the index */
6901+
indexRanges = Max(ceil((double) baserel->pages /
6902+
statsData.pagesPerRange), 1.0);
6903+
}
6904+
else
6905+
{
6906+
/*
6907+
* Assume default number of pages per range, and estimate the number
6908+
* of ranges based on that.
6909+
*/
6910+
indexRanges = Max(ceil((double) baserel->pages /
6911+
BRIN_DEFAULT_PAGES_PER_RANGE), 1.0);
6912+
6913+
statsData.pagesPerRange = BRIN_DEFAULT_PAGES_PER_RANGE;
6914+
statsData.revmapNumPages = (indexRanges / REVMAP_PAGE_MAXITEMS) + 1;
6915+
}
68936916

68946917
/*
68956918
* Compute index correlation
@@ -6989,10 +7012,6 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
69897012
baserel->relid,
69907013
JOIN_INNER, NULL);
69917014

6992-
/* work out the actual number of ranges in the index */
6993-
indexRanges = Max(ceil((double) baserel->pages / statsData.pagesPerRange),
6994-
1.0);
6995-
69967015
/*
69977016
* Now calculate the minimum possible ranges we could match with if all of
69987017
* the rows were in the perfect order in the table's heap.

0 commit comments

Comments
 (0)