Skip to content

Commit 69f7937

Browse files
e-ivkovhomper
authored andcommitted
IndexAM: Add amreuse
1 parent f8103ed commit 69f7937

File tree

11 files changed

+55
-0
lines changed

11 files changed

+55
-0
lines changed

contrib/bloom/blutils.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ blhandler(PG_FUNCTION_ARGS)
129129
amroutine->amkeytype = InvalidOid;
130130

131131
amroutine->ambuild = blbuild;
132+
amroutine->amreuse = NULL;
132133
amroutine->ambuildempty = blbuildempty;
133134
amroutine->aminsert = NULL;
134135
amroutine->aminsertextended = blinsert;

src/backend/access/brin/brin.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ brinhandler(PG_FUNCTION_ARGS)
115115
amroutine->amkeytype = InvalidOid;
116116

117117
amroutine->ambuild = brinbuild;
118+
amroutine->amreuse = NULL;
118119
amroutine->ambuildempty = brinbuildempty;
119120
amroutine->aminsert = NULL;
120121
amroutine->aminsertextended = brininsert;

src/backend/access/gin/ginutil.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ ginhandler(PG_FUNCTION_ARGS)
6262
amroutine->amkeytype = InvalidOid;
6363

6464
amroutine->ambuild = ginbuild;
65+
amroutine->amreuse = NULL;
6566
amroutine->ambuildempty = ginbuildempty;
6667
amroutine->aminsert = NULL;
6768
amroutine->aminsertextended = gininsert;

src/backend/access/gist/gist.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ gisthandler(PG_FUNCTION_ARGS)
8484
amroutine->amkeytype = InvalidOid;
8585

8686
amroutine->ambuild = gistbuild;
87+
amroutine->amreuse = NULL;
8788
amroutine->ambuildempty = gistbuildempty;
8889
amroutine->aminsert = NULL;
8990
amroutine->aminsertextended = gistinsert;

src/backend/access/hash/hash.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ hashhandler(PG_FUNCTION_ARGS)
8181
amroutine->amkeytype = INT4OID;
8282

8383
amroutine->ambuild = hashbuild;
84+
amroutine->amreuse = NULL;
8485
amroutine->ambuildempty = hashbuildempty;
8586
amroutine->aminsert = NULL;
8687
amroutine->aminsertextended = hashinsert;

src/backend/access/nbtree/nbtree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ bthandler(PG_FUNCTION_ARGS)
120120
amroutine->amkeytype = InvalidOid;
121121

122122
amroutine->ambuild = btbuild;
123+
amroutine->amreuse = NULL;
123124
amroutine->ambuildempty = btbuildempty;
124125
amroutine->aminsert = NULL;
125126
amroutine->aminsertextended = btinsert;

src/backend/access/spgist/spgutils.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ spghandler(PG_FUNCTION_ARGS)
6868
amroutine->amkeytype = InvalidOid;
6969

7070
amroutine->ambuild = spgbuild;
71+
amroutine->amreuse = NULL;
7172
amroutine->ambuildempty = spgbuildempty;
7273
amroutine->aminsert = NULL;
7374
amroutine->aminsertextended = spginsert;

src/backend/commands/tablecmds.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#include "tcop/utility.h"
9393
#include "utils/acl.h"
9494
#include "utils/builtins.h"
95+
#include "utils/elog.h"
9596
#include "utils/fmgroids.h"
9697
#include "utils/inval.h"
9798
#include "utils/lsyscache.h"
@@ -13942,9 +13943,50 @@ TryReuseIndex(Oid oldId, IndexStmt *stmt)
1394213943
/* If it's a partitioned index, there is no storage to share. */
1394313944
if (irel->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
1394413945
{
13946+
HeapTuple tuple;
13947+
Form_pg_am accessMethodForm;
13948+
IndexAmRoutine *amRoutine;
13949+
char *accessMethodName;
13950+
Oid heapRelId = IndexGetRelation(oldId, false);
13951+
Relation heapRel = table_open(heapRelId, ShareLock);
13952+
1394513953
stmt->oldNumber = irel->rd_locator.relNumber;
1394613954
stmt->oldCreateSubid = irel->rd_createSubid;
1394713955
stmt->oldFirstRelfilelocatorSubid = irel->rd_firstRelfilelocatorSubid;
13956+
13957+
13958+
/*
13959+
* look up the access method to call amreuse
13960+
*/
13961+
accessMethodName = stmt->accessMethod;
13962+
tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
13963+
if (!HeapTupleIsValid(tuple))
13964+
{
13965+
/*
13966+
* Hack to provide more-or-less-transparent updating of old RTREE
13967+
* indexes to GiST: if RTREE is requested and not found, use GIST.
13968+
*/
13969+
if (strcmp(accessMethodName, "rtree") == 0)
13970+
{
13971+
ereport(NOTICE,
13972+
(errmsg("substituting access method \"gist\" for obsolete method \"rtree\"")));
13973+
accessMethodName = "gist";
13974+
tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
13975+
}
13976+
13977+
if (!HeapTupleIsValid(tuple))
13978+
ereport(ERROR,
13979+
(errcode(ERRCODE_UNDEFINED_OBJECT),
13980+
errmsg("access method \"%s\" does not exist",
13981+
accessMethodName)));
13982+
}
13983+
accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
13984+
amRoutine = GetIndexAmRoutineWithTableAM(heapRel->rd_rel->relam, accessMethodForm->amhandler);
13985+
table_close(heapRel, NoLock);
13986+
13987+
if(amRoutine->amreuse) {
13988+
(*amRoutine->amreuse)(irel);
13989+
}
1394813990
}
1394913991
index_close(irel, NoLock);
1395013992
}

src/include/access/amapi.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ typedef IndexBuildResult *(*ambuild_function) (Relation heapRelation,
104104
Relation indexRelation,
105105
struct IndexInfo *indexInfo);
106106

107+
/* reuse current index - don't drop it */
108+
typedef void (*amreuse_function) (Relation indexRelation);
109+
107110
/* build empty index */
108111
typedef void (*ambuildempty_function) (Relation indexRelation);
109112

@@ -295,6 +298,7 @@ typedef struct IndexAmRoutine
295298

296299
/* interface functions */
297300
ambuild_function ambuild;
301+
amreuse_function amreuse;
298302
ambuildempty_function ambuildempty;
299303
aminsert_function aminsert;
300304
aminsert_extended_function aminsertextended;

src/test/modules/dummy_index_am/dummy_index_am.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ dihandler(PG_FUNCTION_ARGS)
301301
amroutine->amkeytype = InvalidOid;
302302

303303
amroutine->ambuild = dibuild;
304+
amroutine->amreuse = NULL;
304305
amroutine->ambuildempty = dibuildempty;
305306
amroutine->aminsert = NULL;
306307
amroutine->aminsertextended = diinsert;

0 commit comments

Comments
 (0)