Skip to content

Commit f409502

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 a45feb1 commit f409502

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/backend/utils/adt/selfuncs.c

Lines changed: 25 additions & 8 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/htup_details.h"
107108
#include "access/sysattr.h"
@@ -7879,11 +7880,31 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
78797880
&spc_seq_page_cost);
78807881

78817882
/*
7882-
* Obtain some data from the index itself.
7883+
* Obtain some data from the index itself, if possible. Otherwise invent
7884+
* some plausible internal statistics based on the relation page count.
78837885
*/
7884-
indexRel = index_open(index->indexoid, AccessShareLock);
7885-
brinGetStats(indexRel, &statsData);
7886-
index_close(indexRel, AccessShareLock);
7886+
if (!index->hypothetical)
7887+
{
7888+
indexRel = index_open(index->indexoid, AccessShareLock);
7889+
brinGetStats(indexRel, &statsData);
7890+
index_close(indexRel, AccessShareLock);
7891+
7892+
/* work out the actual number of ranges in the index */
7893+
indexRanges = Max(ceil((double) baserel->pages /
7894+
statsData.pagesPerRange), 1.0);
7895+
}
7896+
else
7897+
{
7898+
/*
7899+
* Assume default number of pages per range, and estimate the number
7900+
* of ranges based on that.
7901+
*/
7902+
indexRanges = Max(ceil((double) baserel->pages /
7903+
BRIN_DEFAULT_PAGES_PER_RANGE), 1.0);
7904+
7905+
statsData.pagesPerRange = BRIN_DEFAULT_PAGES_PER_RANGE;
7906+
statsData.revmapNumPages = (indexRanges / REVMAP_PAGE_MAXITEMS) + 1;
7907+
}
78877908

78887909
/*
78897910
* Compute index correlation
@@ -7984,10 +8005,6 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
79848005
baserel->relid,
79858006
JOIN_INNER, NULL);
79868007

7987-
/* work out the actual number of ranges in the index */
7988-
indexRanges = Max(ceil((double) baserel->pages / statsData.pagesPerRange),
7989-
1.0);
7990-
79918008
/*
79928009
* Now calculate the minimum possible ranges we could match with if all of
79938010
* the rows were in the perfect order in the table's heap.

0 commit comments

Comments
 (0)