|
102 | 102 | #include <math.h>
|
103 | 103 |
|
104 | 104 | #include "access/brin.h"
|
| 105 | +#include "access/brin_page.h" |
105 | 106 | #include "access/gin.h"
|
106 | 107 | #include "access/htup_details.h"
|
107 | 108 | #include "access/sysattr.h"
|
@@ -7879,11 +7880,31 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
|
7879 | 7880 | &spc_seq_page_cost);
|
7880 | 7881 |
|
7881 | 7882 | /*
|
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. |
7883 | 7885 | */
|
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 | + } |
7887 | 7908 |
|
7888 | 7909 | /*
|
7889 | 7910 | * Compute index correlation
|
@@ -7984,10 +8005,6 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
|
7984 | 8005 | baserel->relid,
|
7985 | 8006 | JOIN_INNER, NULL);
|
7986 | 8007 |
|
7987 |
| - /* work out the actual number of ranges in the index */ |
7988 |
| - indexRanges = Max(ceil((double) baserel->pages / statsData.pagesPerRange), |
7989 |
| - 1.0); |
7990 |
| - |
7991 | 8008 | /*
|
7992 | 8009 | * Now calculate the minimum possible ranges we could match with if all of
|
7993 | 8010 | * the rows were in the perfect order in the table's heap.
|
|
0 commit comments