diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/access/brin/brin.c | 75 | ||||
-rw-r--r-- | src/backend/access/gin/ginutil.c | 1 | ||||
-rw-r--r-- | src/backend/access/gist/gist.c | 1 | ||||
-rw-r--r-- | src/backend/access/hash/hash.c | 1 | ||||
-rw-r--r-- | src/backend/access/index/indexam.c | 15 | ||||
-rw-r--r-- | src/backend/access/nbtree/nbtree.c | 1 | ||||
-rw-r--r-- | src/backend/access/spgist/spgutils.c | 1 | ||||
-rw-r--r-- | src/backend/executor/execIndexing.c | 5 | ||||
-rw-r--r-- | src/include/access/amapi.h | 4 | ||||
-rw-r--r-- | src/include/access/brin_internal.h | 1 | ||||
-rw-r--r-- | src/include/access/genam.h | 2 |
11 files changed, 96 insertions, 11 deletions
diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c index f0eac078e0b..a5f68f67b74 100644 --- a/src/backend/access/brin/brin.c +++ b/src/backend/access/brin/brin.c @@ -59,6 +59,17 @@ typedef struct BrinBuildState } BrinBuildState; /* + * We use a BrinInsertState to capture running state spanning multiple + * brininsert invocations, within the same command. + */ +typedef struct BrinInsertState +{ + BrinRevmap *bis_rmAccess; + BrinDesc *bis_desc; + BlockNumber bis_pages_per_range; +} BrinInsertState; + +/* * Struct used as "opaque" during index scans */ typedef struct BrinOpaque @@ -72,6 +83,7 @@ typedef struct BrinOpaque static BrinBuildState *initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap, BlockNumber pagesPerRange); +static BrinInsertState *initialize_brin_insertstate(Relation idxRel, IndexInfo *indexInfo); static void terminate_brin_buildstate(BrinBuildState *state); static void brinsummarize(Relation index, Relation heapRel, BlockNumber pageRange, bool include_partial, double *numSummarized, double *numExisting); @@ -117,6 +129,7 @@ brinhandler(PG_FUNCTION_ARGS) amroutine->ambuild = brinbuild; amroutine->ambuildempty = brinbuildempty; amroutine->aminsert = brininsert; + amroutine->aminsertcleanup = brininsertcleanup; amroutine->ambulkdelete = brinbulkdelete; amroutine->amvacuumcleanup = brinvacuumcleanup; amroutine->amcanreturn = NULL; @@ -141,6 +154,27 @@ brinhandler(PG_FUNCTION_ARGS) } /* + * Initialize a BrinInsertState to maintain state to be used across multiple + * tuple inserts, within the same command. + */ +static BrinInsertState * +initialize_brin_insertstate(Relation idxRel, IndexInfo *indexInfo) +{ + BrinInsertState *bistate; + MemoryContext oldcxt; + + oldcxt = MemoryContextSwitchTo(indexInfo->ii_Context); + bistate = palloc0(sizeof(BrinInsertState)); + bistate->bis_desc = brin_build_desc(idxRel); + bistate->bis_rmAccess = brinRevmapInitialize(idxRel, + &bistate->bis_pages_per_range); + indexInfo->ii_AmCache = bistate; + MemoryContextSwitchTo(oldcxt); + + return bistate; +} + +/* * A tuple in the heap is being inserted. To keep a brin index up to date, * we need to obtain the relevant index tuple and compare its stored values * with those of the new tuple. If the tuple values are not consistent with @@ -162,14 +196,24 @@ brininsert(Relation idxRel, Datum *values, bool *nulls, BlockNumber pagesPerRange; BlockNumber origHeapBlk; BlockNumber heapBlk; - BrinDesc *bdesc = (BrinDesc *) indexInfo->ii_AmCache; + BrinInsertState *bistate = (BrinInsertState *) indexInfo->ii_AmCache; BrinRevmap *revmap; + BrinDesc *bdesc; Buffer buf = InvalidBuffer; MemoryContext tupcxt = NULL; MemoryContext oldcxt = CurrentMemoryContext; bool autosummarize = BrinGetAutoSummarize(idxRel); - revmap = brinRevmapInitialize(idxRel, &pagesPerRange); + /* + * If firt time through in this statement, initialize the insert state + * that we keep for all the inserts in the command. + */ + if (!bistate) + bistate = initialize_brin_insertstate(idxRel, indexInfo); + + revmap = bistate->bis_rmAccess; + bdesc = bistate->bis_desc; + pagesPerRange = bistate->bis_pages_per_range; /* * origHeapBlk is the block number where the insertion occurred. heapBlk @@ -228,14 +272,6 @@ brininsert(Relation idxRel, Datum *values, bool *nulls, if (!brtup) break; - /* First time through in this statement? */ - if (bdesc == NULL) - { - MemoryContextSwitchTo(indexInfo->ii_Context); - bdesc = brin_build_desc(idxRel); - indexInfo->ii_AmCache = (void *) bdesc; - MemoryContextSwitchTo(oldcxt); - } /* First time through in this brininsert call? */ if (tupcxt == NULL) { @@ -306,7 +342,6 @@ brininsert(Relation idxRel, Datum *values, bool *nulls, break; } - brinRevmapTerminate(revmap); if (BufferIsValid(buf)) ReleaseBuffer(buf); MemoryContextSwitchTo(oldcxt); @@ -317,6 +352,24 @@ brininsert(Relation idxRel, Datum *values, bool *nulls, } /* + * Callback to clean up the BrinInsertState once all tuple inserts are done. + */ +void +brininsertcleanup(IndexInfo *indexInfo) +{ + BrinInsertState *bistate = (BrinInsertState *) indexInfo->ii_AmCache; + + Assert(bistate); + /* + * Clean up the revmap. Note that the brinDesc has already been cleaned up + * as part of its own memory context. + */ + brinRevmapTerminate(bistate->bis_rmAccess); + bistate->bis_rmAccess = NULL; + bistate->bis_desc = NULL; +} + +/* * Initialize state for a BRIN index scan. * * We read the metapage here to determine the pages-per-range number that this diff --git a/src/backend/access/gin/ginutil.c b/src/backend/access/gin/ginutil.c index 7a4cd93f301..a875c5d3d7a 100644 --- a/src/backend/access/gin/ginutil.c +++ b/src/backend/access/gin/ginutil.c @@ -64,6 +64,7 @@ ginhandler(PG_FUNCTION_ARGS) amroutine->ambuild = ginbuild; amroutine->ambuildempty = ginbuildempty; amroutine->aminsert = gininsert; + amroutine->aminsertcleanup = NULL; amroutine->ambulkdelete = ginbulkdelete; amroutine->amvacuumcleanup = ginvacuumcleanup; amroutine->amcanreturn = NULL; diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c index 8ef5fa03290..9a1bf8f66cb 100644 --- a/src/backend/access/gist/gist.c +++ b/src/backend/access/gist/gist.c @@ -86,6 +86,7 @@ gisthandler(PG_FUNCTION_ARGS) amroutine->ambuild = gistbuild; amroutine->ambuildempty = gistbuildempty; amroutine->aminsert = gistinsert; + amroutine->aminsertcleanup = NULL; amroutine->ambulkdelete = gistbulkdelete; amroutine->amvacuumcleanup = gistvacuumcleanup; amroutine->amcanreturn = gistcanreturn; diff --git a/src/backend/access/hash/hash.c b/src/backend/access/hash/hash.c index 7a025f33cfe..6443ff21bda 100644 --- a/src/backend/access/hash/hash.c +++ b/src/backend/access/hash/hash.c @@ -83,6 +83,7 @@ hashhandler(PG_FUNCTION_ARGS) amroutine->ambuild = hashbuild; amroutine->ambuildempty = hashbuildempty; amroutine->aminsert = hashinsert; + amroutine->aminsertcleanup = NULL; amroutine->ambulkdelete = hashbulkdelete; amroutine->amvacuumcleanup = hashvacuumcleanup; amroutine->amcanreturn = NULL; diff --git a/src/backend/access/index/indexam.c b/src/backend/access/index/indexam.c index b25b03f7abc..597b763d1f6 100644 --- a/src/backend/access/index/indexam.c +++ b/src/backend/access/index/indexam.c @@ -196,6 +196,21 @@ index_insert(Relation indexRelation, indexInfo); } +/* ------------------------- + * index_insert_cleanup - clean up after all index inserts are done + * ------------------------- + */ +void +index_insert_cleanup(Relation indexRelation, + IndexInfo *indexInfo) +{ + RELATION_CHECKS; + Assert(indexInfo); + + if (indexRelation->rd_indam->aminsertcleanup) + indexRelation->rd_indam->aminsertcleanup(indexInfo); +} + /* * index_beginscan - start a scan of an index with amgettuple * diff --git a/src/backend/access/nbtree/nbtree.c b/src/backend/access/nbtree/nbtree.c index a88b36a589a..0930f9b37e3 100644 --- a/src/backend/access/nbtree/nbtree.c +++ b/src/backend/access/nbtree/nbtree.c @@ -122,6 +122,7 @@ bthandler(PG_FUNCTION_ARGS) amroutine->ambuild = btbuild; amroutine->ambuildempty = btbuildempty; amroutine->aminsert = btinsert; + amroutine->aminsertcleanup = NULL; amroutine->ambulkdelete = btbulkdelete; amroutine->amvacuumcleanup = btvacuumcleanup; amroutine->amcanreturn = btcanreturn; diff --git a/src/backend/access/spgist/spgutils.c b/src/backend/access/spgist/spgutils.c index c112e1e5dd4..30c00876a56 100644 --- a/src/backend/access/spgist/spgutils.c +++ b/src/backend/access/spgist/spgutils.c @@ -70,6 +70,7 @@ spghandler(PG_FUNCTION_ARGS) amroutine->ambuild = spgbuild; amroutine->ambuildempty = spgbuildempty; amroutine->aminsert = spginsert; + amroutine->aminsertcleanup = NULL; amroutine->ambulkdelete = spgbulkdelete; amroutine->amvacuumcleanup = spgvacuumcleanup; amroutine->amcanreturn = spgcanreturn; diff --git a/src/backend/executor/execIndexing.c b/src/backend/executor/execIndexing.c index 384b39839a0..2fa2118f3c2 100644 --- a/src/backend/executor/execIndexing.c +++ b/src/backend/executor/execIndexing.c @@ -233,15 +233,20 @@ ExecCloseIndices(ResultRelInfo *resultRelInfo) int i; int numIndices; RelationPtr indexDescs; + IndexInfo **indexInfos; numIndices = resultRelInfo->ri_NumIndices; indexDescs = resultRelInfo->ri_IndexRelationDescs; + indexInfos = resultRelInfo->ri_IndexRelationInfo; for (i = 0; i < numIndices; i++) { if (indexDescs[i] == NULL) continue; /* shouldn't happen? */ + /* Give the index a chance to do some post-insert cleanup */ + index_insert_cleanup(indexDescs[i], indexInfos[i]); + /* Drop lock acquired by ExecOpenIndices */ index_close(indexDescs[i], RowExclusiveLock); } diff --git a/src/include/access/amapi.h b/src/include/access/amapi.h index 995725502a6..244459587fc 100644 --- a/src/include/access/amapi.h +++ b/src/include/access/amapi.h @@ -113,6 +113,9 @@ typedef bool (*aminsert_function) (Relation indexRelation, bool indexUnchanged, struct IndexInfo *indexInfo); +/* cleanup after insert */ +typedef void (*aminsertcleanup_function) (struct IndexInfo *indexInfo); + /* bulk delete */ typedef IndexBulkDeleteResult *(*ambulkdelete_function) (IndexVacuumInfo *info, IndexBulkDeleteResult *stats, @@ -261,6 +264,7 @@ typedef struct IndexAmRoutine ambuild_function ambuild; ambuildempty_function ambuildempty; aminsert_function aminsert; + aminsertcleanup_function aminsertcleanup; ambulkdelete_function ambulkdelete; amvacuumcleanup_function amvacuumcleanup; amcanreturn_function amcanreturn; /* can be NULL */ diff --git a/src/include/access/brin_internal.h b/src/include/access/brin_internal.h index 97ddc925b27..ed231e208eb 100644 --- a/src/include/access/brin_internal.h +++ b/src/include/access/brin_internal.h @@ -96,6 +96,7 @@ extern bool brininsert(Relation idxRel, Datum *values, bool *nulls, IndexUniqueCheck checkUnique, bool indexUnchanged, struct IndexInfo *indexInfo); +extern void brininsertcleanup(struct IndexInfo *indexInfo); extern IndexScanDesc brinbeginscan(Relation r, int nkeys, int norderbys); extern int64 bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm); extern void brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, diff --git a/src/include/access/genam.h b/src/include/access/genam.h index f31dec6ee0f..80dc8d54066 100644 --- a/src/include/access/genam.h +++ b/src/include/access/genam.h @@ -148,6 +148,8 @@ extern bool index_insert(Relation indexRelation, IndexUniqueCheck checkUnique, bool indexUnchanged, struct IndexInfo *indexInfo); +extern void index_insert_cleanup(Relation indexRelation, + struct IndexInfo *indexInfo); extern IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, |