Skip to content

Commit 3bbd67c

Browse files
committed
Fix insertion in GTT indexes
1 parent f01172b commit 3bbd67c

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

src/backend/executor/nodeModifyTable.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,22 @@ ExecInsert(ModifyTableState *mtstate,
354354
ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
355355
OnConflictAction onconflict = node->onConflictAction;
356356

357+
if (resultRelInfo->ri_NumIndices > 0)
357358
ExecMaterializeSlot(slot);
358359

359360
/*
360361
* get information on the (current) result relation
361362
*/
362363
resultRelInfo = estate->es_result_relation_info;
363364
resultRelationDesc = resultRelInfo->ri_RelationDesc;
364-
365+
if (resultRelationDesc->rd_rel->relpersistence == RELPERSISTENCE_SESSION)
366+
{
367+
int i;
368+
for (i = 0; i < resultRelInfo->ri_NumIndices; i++)
369+
{
370+
InitGTTIndexes(resultRelInfo->ri_IndexRelationDescs[i]);
371+
}
372+
}
365373
/*
366374
* BEFORE ROW INSERT Triggers.
367375
*

src/backend/optimizer/util/plancat.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "catalog/catalog.h"
2929
#include "catalog/dependency.h"
3030
#include "catalog/heap.h"
31-
#include "catalog/index.h"
3231
#include "catalog/pg_am.h"
3332
#include "catalog/pg_proc.h"
3433
#include "catalog/pg_statistic_ext.h"
@@ -47,7 +46,6 @@
4746
#include "rewrite/rewriteManip.h"
4847
#include "statistics/statistics.h"
4948
#include "storage/bufmgr.h"
50-
#include "storage/buf_internals.h"
5149
#include "utils/builtins.h"
5250
#include "utils/lsyscache.h"
5351
#include "utils/partcache.h"
@@ -87,21 +85,8 @@ is_index_valid(Relation index)
8785
{
8886
if (!index->rd_index->indisvalid)
8987
return false;
90-
9188
if (index->rd_rel->relpersistence == RELPERSISTENCE_SESSION)
92-
{
93-
Buffer metapage = ReadBuffer(index, 0);
94-
bool isNew = PageIsNew(BufferGetPage(metapage));
95-
ReleaseBuffer(metapage);
96-
if (isNew)
97-
{
98-
Relation heap;
99-
DropRelFileNodeAllLocalBuffers(index->rd_smgr->smgr_rnode.node);
100-
heap = RelationIdGetRelation(index->rd_index->indrelid);
101-
index->rd_indam->ambuild(heap, index, BuildIndexInfo(index));
102-
RelationClose(heap);
103-
}
104-
}
89+
InitGTTIndexes(index);
10590
return true;
10691
}
10792

src/backend/storage/buffer/bufmgr.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
#include <sys/file.h>
3434
#include <unistd.h>
3535

36+
#include "access/amapi.h"
3637
#include "access/tableam.h"
3738
#include "access/xlog.h"
3839
#include "catalog/catalog.h"
40+
#include "catalog/index.h"
3941
#include "catalog/storage.h"
4042
#include "executor/instrument.h"
4143
#include "lib/binaryheap.h"
@@ -4424,3 +4426,19 @@ TestForOldSnapshot_impl(Snapshot snapshot, Relation relation)
44244426
(errcode(ERRCODE_SNAPSHOT_TOO_OLD),
44254427
errmsg("snapshot too old")));
44264428
}
4429+
4430+
void InitGTTIndexes(Relation index)
4431+
{
4432+
Buffer metapage = ReadBuffer(index, 0);
4433+
bool isNew = PageIsNew(BufferGetPage(metapage));
4434+
Assert(index->rd_rel->relpersistence == RELPERSISTENCE_SESSION);
4435+
ReleaseBuffer(metapage);
4436+
if (isNew)
4437+
{
4438+
Relation heap;
4439+
DropRelFileNodeAllLocalBuffers(index->rd_smgr->smgr_rnode.node);
4440+
heap = RelationIdGetRelation(index->rd_index->indrelid);
4441+
index->rd_indam->ambuild(heap, index, BuildIndexInfo(index));
4442+
RelationClose(heap);
4443+
}
4444+
}

src/include/storage/bufmgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ extern void TestForOldSnapshot_impl(Snapshot snapshot, Relation relation);
231231
extern BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype);
232232
extern void FreeAccessStrategy(BufferAccessStrategy strategy);
233233

234+
extern void InitGTTIndexes(Relation rel);
234235

235236
/* inline functions */
236237

0 commit comments

Comments
 (0)