|
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/table.h"
|
107 | 108 | #include "access/tableam.h"
|
@@ -6884,12 +6885,34 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
|
6884 | 6885 | &spc_seq_page_cost);
|
6885 | 6886 |
|
6886 | 6887 | /*
|
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. |
6889 | 6890 | */
|
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 | + } |
6893 | 6916 |
|
6894 | 6917 | /*
|
6895 | 6918 | * Compute index correlation
|
@@ -6989,10 +7012,6 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
|
6989 | 7012 | baserel->relid,
|
6990 | 7013 | JOIN_INNER, NULL);
|
6991 | 7014 |
|
6992 |
| - /* work out the actual number of ranges in the index */ |
6993 |
| - indexRanges = Max(ceil((double) baserel->pages / statsData.pagesPerRange), |
6994 |
| - 1.0); |
6995 |
| - |
6996 | 7015 | /*
|
6997 | 7016 | * Now calculate the minimum possible ranges we could match with if all of
|
6998 | 7017 | * the rows were in the perfect order in the table's heap.
|
|
0 commit comments