Skip to content

Commit

Permalink
Make index insert compatible with outside callers
Browse files Browse the repository at this point in the history
We split aminsert method to aminsert and aminsertextended.

aminsert is a method for indexes implemented in other extensions, it
accepts ItemPointer tupleid.

aminsertextended is for internal Postgres indexes and Orioledb, it
accepts Datum tupleid. They are not supposed to call aminsert method, so
that it is set NULL for them. We can not rely that extensions are aware
of aminsertextended, so index_insert() calls aminsert if it's not NULL
preferentially.

Signature of index_insert() is reverted so that it could be called by other
extensions. Datum tupleid is confined inside index_insert method.
  • Loading branch information
pashkinelfe committed Oct 18, 2024
1 parent 313aa6c commit 333f7e3
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 27 deletions.
3 changes: 2 additions & 1 deletion contrib/bloom/blutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ blhandler(PG_FUNCTION_ARGS)

amroutine->ambuild = blbuild;
amroutine->ambuildempty = blbuildempty;
amroutine->aminsert = blinsert;
amroutine->aminsert = NULL;
amroutine->aminsertextended = blinsert;
amroutine->aminsertcleanup = NULL;
amroutine->ambulkdelete = blbulkdelete;
amroutine->amvacuumcleanup = blvacuumcleanup;
Expand Down
1 change: 1 addition & 0 deletions doc/src/sgml/indexam.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ typedef struct IndexAmRoutine
ambuild_function ambuild;
ambuildempty_function ambuildempty;
aminsert_function aminsert;
aminsert_extended_function aminsertextended;
aminsertcleanup_function aminsertcleanup;
ambulkdelete_function ambulkdelete;
amvacuumcleanup_function amvacuumcleanup;
Expand Down
3 changes: 2 additions & 1 deletion src/backend/access/brin/brin.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ brinhandler(PG_FUNCTION_ARGS)

amroutine->ambuild = brinbuild;
amroutine->ambuildempty = brinbuildempty;
amroutine->aminsert = brininsert;
amroutine->aminsert = NULL;
amroutine->aminsertextended = brininsert;
amroutine->aminsertcleanup = brininsertcleanup;
amroutine->ambulkdelete = brinbulkdelete;
amroutine->amvacuumcleanup = brinvacuumcleanup;
Expand Down
2 changes: 1 addition & 1 deletion src/backend/access/common/toast_internals.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ toast_save_datum(Relation rel, Datum value,
/* Only index relations marked as ready can be updated */
if (toastidxs[i]->rd_index->indisready)
index_insert(toastidxs[i], t_values, t_isnull,
ItemPointerGetDatum(&(toasttup->t_self)),
&(toasttup->t_self),
toastrel,
toastidxs[i]->rd_index->indisunique ?
UNIQUE_CHECK_YES : UNIQUE_CHECK_NO,
Expand Down
3 changes: 2 additions & 1 deletion src/backend/access/gin/ginutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ ginhandler(PG_FUNCTION_ARGS)

amroutine->ambuild = ginbuild;
amroutine->ambuildempty = ginbuildempty;
amroutine->aminsert = gininsert;
amroutine->aminsert = NULL;
amroutine->aminsertextended = gininsert;
amroutine->aminsertcleanup = NULL;
amroutine->ambulkdelete = ginbulkdelete;
amroutine->amvacuumcleanup = ginvacuumcleanup;
Expand Down
3 changes: 2 additions & 1 deletion src/backend/access/gist/gist.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ gisthandler(PG_FUNCTION_ARGS)

amroutine->ambuild = gistbuild;
amroutine->ambuildempty = gistbuildempty;
amroutine->aminsert = gistinsert;
amroutine->aminsert = NULL;
amroutine->aminsertextended = gistinsert;
amroutine->aminsertcleanup = NULL;
amroutine->ambulkdelete = gistbulkdelete;
amroutine->amvacuumcleanup = gistvacuumcleanup;
Expand Down
3 changes: 2 additions & 1 deletion src/backend/access/hash/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ hashhandler(PG_FUNCTION_ARGS)

amroutine->ambuild = hashbuild;
amroutine->ambuildempty = hashbuildempty;
amroutine->aminsert = hashinsert;
amroutine->aminsert = NULL;
amroutine->aminsertextended = hashinsert;
amroutine->aminsertcleanup = NULL;
amroutine->ambulkdelete = hashbulkdelete;
amroutine->amvacuumcleanup = hashvacuumcleanup;
Expand Down
2 changes: 1 addition & 1 deletion src/backend/access/heap/heapam_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2313,7 +2313,7 @@ heapam_index_validate_scan(Relation heapRelation,
index_insert(indexRelation,
values,
isnull,
ItemPointerGetDatum(&rootTuple),
&rootTuple,
heapRelation,
indexInfo->ii_Unique ?
UNIQUE_CHECK_YES : UNIQUE_CHECK_NO,
Expand Down
21 changes: 18 additions & 3 deletions src/backend/access/index/indexam.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,39 @@ bool
index_insert(Relation indexRelation,
Datum *values,
bool *isnull,
Datum tupleid,
ItemPointer tupleid,
Relation heapRelation,
IndexUniqueCheck checkUnique,
bool indexUnchanged,
IndexInfo *indexInfo)
{
RELATION_CHECKS;
CHECK_REL_PROCEDURE(aminsert);

if (indexRelation->rd_indam->aminsertextended == NULL && indexRelation->rd_indam->aminsert == NULL )
elog(ERROR, "at least one function aminsert or aminsertextended should be defined for index \"%s\"", \
RelationGetRelationName(indexRelation));

if (!(indexRelation->rd_indam->ampredlocks))
CheckForSerializableConflictIn(indexRelation,
(ItemPointer) NULL,
InvalidBlockNumber);

return indexRelation->rd_indam->aminsert(indexRelation, values, isnull,
if (indexRelation->rd_indam->aminsert)
{
/* compatibility method for extension AM's not aware of aminsertextended */
return indexRelation->rd_indam->aminsert(indexRelation, values, isnull,
tupleid, heapRelation,
checkUnique, indexUnchanged,
indexInfo);
}
else
{
/* index insert method for internal AM's and Orioledb that are aware of aminsertextended */
return indexRelation->rd_indam->aminsertextended(indexRelation, values, isnull,
ItemPointerGetDatum(tupleid), heapRelation,
checkUnique, indexUnchanged,
indexInfo);
}
}

/* -------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/backend/access/nbtree/nbtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ bthandler(PG_FUNCTION_ARGS)

amroutine->ambuild = btbuild;
amroutine->ambuildempty = btbuildempty;
amroutine->aminsert = btinsert;
amroutine->aminsert = NULL;
amroutine->aminsertextended = btinsert;
amroutine->aminsertcleanup = NULL;
amroutine->ambulkdelete = btbulkdelete;
amroutine->amvacuumcleanup = btvacuumcleanup;
Expand Down
3 changes: 2 additions & 1 deletion src/backend/access/spgist/spgutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ spghandler(PG_FUNCTION_ARGS)

amroutine->ambuild = spgbuild;
amroutine->ambuildempty = spgbuildempty;
amroutine->aminsert = spginsert;
amroutine->aminsert = NULL;
amroutine->aminsertextended = spginsert;
amroutine->aminsertcleanup = NULL;
amroutine->ambulkdelete = spgbulkdelete;
amroutine->amvacuumcleanup = spgvacuumcleanup;
Expand Down
2 changes: 1 addition & 1 deletion src/backend/catalog/indexing.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple,
index_insert(index, /* index relation */
values, /* array of index Datums */
isnull, /* is-null flags */
ItemPointerGetDatum(&(heapTuple->t_self)), /* tid of heap tuple */
&(heapTuple->t_self), /* tid of heap tuple */
heapRelation,
index->rd_index->indisunique ?
UNIQUE_CHECK_YES : UNIQUE_CHECK_NO,
Expand Down
2 changes: 1 addition & 1 deletion src/backend/commands/constraint.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ unique_key_recheck(PG_FUNCTION_ARGS)
* the row is now dead, because that is the TID the index will know
* about.
*/
index_insert(indexRel, values, isnull, ItemPointerGetDatum(&checktid),
index_insert(indexRel, values, isnull, &checktid,
trigdata->tg_relation, UNIQUE_CHECK_EXISTING,
false, indexInfo);

Expand Down
20 changes: 9 additions & 11 deletions src/backend/executor/execIndexing.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,19 @@ ExecInsertIndexTuples(ResultRelInfo *resultRelInfo,
ExprContext *econtext;
Datum values[INDEX_MAX_KEYS];
bool isnull[INDEX_MAX_KEYS];
Datum tupleid;
ItemPointer tupleid;


if (table_get_row_ref_type(resultRelInfo->ri_RelationDesc) == ROW_REF_ROWID)
{
bool isnull;
tupleid = slot_getsysattr(slot, RowIdAttributeNumber, &isnull);
tupleid = DatumGetItemPointer(slot_getsysattr(slot, RowIdAttributeNumber, &isnull));
Assert(!isnull);
}
else
{
Assert(ItemPointerIsValid(&slot->tts_tid));
tupleid = PointerGetDatum(&slot->tts_tid);
tupleid = &slot->tts_tid;
}

/*
Expand Down Expand Up @@ -473,7 +473,6 @@ ExecInsertIndexTuples(ResultRelInfo *resultRelInfo,
{
bool violationOK;
CEOUC_WAIT_MODE waitMode;
ItemPointer raw_tupleid = DatumGetItemPointer(tupleid);

if (applyNoDupErr)
{
Expand All @@ -494,7 +493,7 @@ ExecInsertIndexTuples(ResultRelInfo *resultRelInfo,
satisfiesConstraint =
check_exclusion_or_unique_constraint(heapRelation,
indexRelation, indexInfo,
raw_tupleid, values, isnull,
tupleid, values, isnull,
estate, false,
waitMode, violationOK, NULL);
}
Expand Down Expand Up @@ -537,18 +536,18 @@ ExecUpdateIndexTuples(ResultRelInfo *resultRelInfo,
ExprContext *econtext;
Datum values[INDEX_MAX_KEYS];
bool isnull[INDEX_MAX_KEYS];
Datum tupleid;
ItemPointer tupleid;

if (table_get_row_ref_type(resultRelInfo->ri_RelationDesc) == ROW_REF_ROWID)
{
bool isnull;
tupleid = slot_getsysattr(slot, RowIdAttributeNumber, &isnull);
tupleid = DatumGetItemPointer(slot_getsysattr(slot, RowIdAttributeNumber, &isnull));
Assert(!isnull);
}
else
{
Assert(ItemPointerIsValid(&slot->tts_tid));
tupleid = PointerGetDatum(&slot->tts_tid);
tupleid = &slot->tts_tid;
}

/*
Expand Down Expand Up @@ -717,7 +716,7 @@ ExecUpdateIndexTuples(ResultRelInfo *resultRelInfo,
old_valid,
values, /* array of index Datums */
isnull, /* null flags */
tupleid, /* tid of heap tuple */
ItemPointerGetDatum(tupleid), /* tid of heap tuple */
valuesOld,
isnullOld,
oldTupleid,
Expand Down Expand Up @@ -768,7 +767,6 @@ ExecUpdateIndexTuples(ResultRelInfo *resultRelInfo,
{
bool violationOK;
CEOUC_WAIT_MODE waitMode;
ItemPointer raw_tupleid = DatumGetItemPointer(tupleid);

if (applyNoDupErr)
{
Expand All @@ -789,7 +787,7 @@ ExecUpdateIndexTuples(ResultRelInfo *resultRelInfo,
satisfiesConstraint =
check_exclusion_or_unique_constraint(heapRelation,
indexRelation, indexInfo,
raw_tupleid, values, isnull,
tupleid, values, isnull,
estate, false,
waitMode, violationOK, NULL);
}
Expand Down
12 changes: 12 additions & 0 deletions src/include/access/amapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,24 @@ typedef void (*ambuildempty_function) (Relation indexRelation);

/* insert this tuple */
typedef bool (*aminsert_function) (Relation indexRelation,
Datum *values,
bool *isnull,
ItemPointer tupleid,
Relation heapRelation,
IndexUniqueCheck checkUnique,
bool indexUnchanged,
struct IndexInfo *indexInfo);

/* extended version of aminsert taking Datum tupleid */
typedef bool (*aminsert_extended_function) (Relation indexRelation,
Datum *values,
bool *isnull,
Datum tupleid,
Relation heapRelation,
IndexUniqueCheck checkUnique,
bool indexUnchanged,
struct IndexInfo *indexInfo);

/* update this tuple */
typedef bool (*amupdate_function) (Relation indexRelation,
bool new_valid,
Expand Down Expand Up @@ -288,6 +299,7 @@ typedef struct IndexAmRoutine
ambuild_function ambuild;
ambuildempty_function ambuildempty;
aminsert_function aminsert;
aminsert_extended_function aminsertextended;
aminsertcleanup_function aminsertcleanup;
amupdate_function amupdate;
amdelete_function amdelete;
Expand Down
2 changes: 1 addition & 1 deletion src/include/access/genam.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ extern void index_close(Relation relation, LOCKMODE lockmode);

extern bool index_insert(Relation indexRelation,
Datum *values, bool *isnull,
Datum tupleid,
ItemPointer tupleid,
Relation heapRelation,
IndexUniqueCheck checkUnique,
bool indexUnchanged,
Expand Down
3 changes: 2 additions & 1 deletion src/test/modules/dummy_index_am/dummy_index_am.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ dihandler(PG_FUNCTION_ARGS)

amroutine->ambuild = dibuild;
amroutine->ambuildempty = dibuildempty;
amroutine->aminsert = diinsert;
amroutine->aminsert = NULL;
amroutine->aminsertextended = diinsert;
amroutine->ambulkdelete = dibulkdelete;
amroutine->amvacuumcleanup = divacuumcleanup;
amroutine->amcanreturn = NULL;
Expand Down
1 change: 1 addition & 0 deletions src/tools/pgindent/typedefs.list
Original file line number Diff line number Diff line change
Expand Up @@ -3266,6 +3266,7 @@ amgetbitmap_function
amgettuple_function
aminitparallelscan_function
aminsert_function
aminsert_extended_function
aminsertcleanup_function
ammarkpos_function
amoptions_function
Expand Down

0 comments on commit 333f7e3

Please sign in to comment.