diff --git a/src/backend/access/index/indexam.c b/src/backend/access/index/indexam.c index fe80b8b0bac..3aa9515f736 100644 --- a/src/backend/access/index/indexam.c +++ b/src/backend/access/index/indexam.c @@ -57,6 +57,7 @@ #include "nodes/makefuncs.h" #include "pgstat.h" #include "storage/bufmgr.h" +#include "storage/buf_internals.h" #include "storage/lmgr.h" #include "storage/predicate.h" #include "utils/ruleutils.h" @@ -114,6 +115,7 @@ static IndexScanDesc index_beginscan_internal(Relation indexRelation, * ---------------------------------------------------------------- */ + /* ---------------- * index_open - open an index relation by relation OID * @@ -142,6 +144,41 @@ index_open(Oid relationId, LOCKMODE lockmode) errmsg("\"%s\" is not an index", RelationGetRelationName(r)))); + /* + * NEON: support unogged indexes. + * Data of unlogged indexes is lost after compute restart so we have to reinitialize them to avoid errors. + */ + if (r->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) + { + LOCKMODE orig_lockmode = lockmode; + while (true) + { + Buffer metapage = ReadBuffer(r, 0); + bool isNew = PageIsNew(BufferGetPage(metapage)); + ReleaseBuffer(metapage); + if (isNew) + { + Relation heap; + if (lockmode != ExclusiveLock) + { + UnlockRelation(r, lockmode); + LockRelation(r, ExclusiveLock); + lockmode = ExclusiveLock; + continue; + } + DropRelFileNodesAllBuffers(&r->rd_smgr, 1); + heap = RelationIdGetRelation(r->rd_index->indrelid); + r->rd_indam->ambuild(heap, r, BuildIndexInfo(r)); + RelationClose(heap); + } + break; + } + if (orig_lockmode != lockmode) + { + UnlockRelation(r, lockmode); + LockRelation(r, orig_lockmode); + } + } return r; } diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c index 7d8e4d54678..792866ee040 100644 --- a/src/backend/optimizer/util/plancat.c +++ b/src/backend/optimizer/util/plancat.c @@ -27,7 +27,6 @@ #include "access/xlog.h" #include "catalog/catalog.h" #include "catalog/heap.h" -#include "catalog/index.h" #include "catalog/pg_am.h" #include "catalog/pg_proc.h" #include "catalog/pg_statistic_ext.h" @@ -48,8 +47,6 @@ #include "rewrite/rewriteManip.h" #include "statistics/statistics.h" #include "storage/bufmgr.h" -#include "storage/buf_internals.h" -#include "storage/lmgr.h" #include "utils/builtins.h" #include "utils/lsyscache.h" #include "utils/partcache.h" @@ -84,40 +81,6 @@ static void set_baserel_partition_key_exprs(Relation relation, static void set_baserel_partition_constraint(Relation relation, RelOptInfo *rel); -static bool -is_index_valid(Relation index, LOCKMODE lmode) -{ - if (!index->rd_index->indisvalid) - return false; - - if (index->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) - { - while (true) - { - Buffer metapage = ReadBuffer(index, 0); - bool isNew = PageIsNew(BufferGetPage(metapage)); - ReleaseBuffer(metapage); - if (isNew) - { - Relation heap; - if (lmode != ExclusiveLock) - { - UnlockRelation(index, lmode); - LockRelation(index, ExclusiveLock); - lmode = ExclusiveLock; - continue; - } - DropRelFileNodesAllBuffers(&index->rd_smgr, 1); - heap = RelationIdGetRelation(index->rd_index->indrelid); - index->rd_indam->ambuild(heap, index, BuildIndexInfo(index)); - RelationClose(heap); - } - break; - } - } - return true; -} - /* * get_relation_info - * Retrieves catalog information for a given relation. @@ -260,7 +223,7 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, * still needs to insert into "invalid" indexes, if they're marked * indisready. */ - if (!is_index_valid(indexRelation, lmode)) + if (!index->indisvalid) { index_close(indexRelation, NoLock); continue;