|
27 | 27 | #include "access/xlog.h"
|
28 | 28 | #include "catalog/catalog.h"
|
29 | 29 | #include "catalog/heap.h"
|
| 30 | +#include "catalog/index.h" |
30 | 31 | #include "catalog/pg_am.h"
|
31 | 32 | #include "catalog/pg_proc.h"
|
32 | 33 | #include "catalog/pg_statistic_ext.h"
|
|
46 | 47 | #include "rewrite/rewriteManip.h"
|
47 | 48 | #include "statistics/statistics.h"
|
48 | 49 | #include "storage/bufmgr.h"
|
| 50 | +#include "storage/buf_internals.h" |
| 51 | +#include "storage/lmgr.h" |
49 | 52 | #include "utils/builtins.h"
|
50 | 53 | #include "utils/lsyscache.h"
|
51 | 54 | #include "utils/partcache.h"
|
@@ -80,6 +83,39 @@ static void set_baserel_partition_key_exprs(Relation relation,
|
80 | 83 | static void set_baserel_partition_constraint(Relation relation,
|
81 | 84 | RelOptInfo *rel);
|
82 | 85 |
|
| 86 | +static bool |
| 87 | +is_index_valid(Relation index, LOCKMODE lmode) |
| 88 | +{ |
| 89 | + if (!index->rd_index->indisvalid) |
| 90 | + return false; |
| 91 | + |
| 92 | + if (index->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) |
| 93 | + { |
| 94 | + while (true) |
| 95 | + { |
| 96 | + Buffer metapage = ReadBuffer(index, 0); |
| 97 | + bool isNew = PageIsNew(BufferGetPage(metapage)); |
| 98 | + ReleaseBuffer(metapage); |
| 99 | + if (isNew) |
| 100 | + { |
| 101 | + Relation heap; |
| 102 | + if (lmode != ExclusiveLock) |
| 103 | + { |
| 104 | + UnlockRelation(index, lmode); |
| 105 | + LockRelation(index, ExclusiveLock); |
| 106 | + lmode = ExclusiveLock; |
| 107 | + continue; |
| 108 | + } |
| 109 | + DropRelFileNodesAllBuffers(&index->rd_smgr, 1); |
| 110 | + heap = RelationIdGetRelation(index->rd_index->indrelid); |
| 111 | + index->rd_indam->ambuild(heap, index, BuildIndexInfo(index)); |
| 112 | + RelationClose(heap); |
| 113 | + } |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + return true; |
| 118 | +} |
83 | 119 |
|
84 | 120 | /*
|
85 | 121 | * get_relation_info -
|
@@ -221,7 +257,7 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
|
221 | 257 | * still needs to insert into "invalid" indexes, if they're marked
|
222 | 258 | * indisready.
|
223 | 259 | */
|
224 |
| - if (!index->indisvalid) |
| 260 | + if (!is_index_valid(indexRelation, lmode)) |
225 | 261 | {
|
226 | 262 | index_close(indexRelation, NoLock);
|
227 | 263 | continue;
|
|
0 commit comments