diff --git a/src/postgres/src/backend/catalog/indexing.c b/src/postgres/src/backend/catalog/indexing.c index 481b699a878e7..c3522bab98280 100644 --- a/src/postgres/src/backend/catalog/indexing.c +++ b/src/postgres/src/backend/catalog/indexing.c @@ -107,6 +107,13 @@ CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple) */ for (i = 0; i < numIndexes; i++) { + /* + * No need to update YugaByte primary key which is intrinic part of + * the base table. + */ + if (IsYugaByteEnabled() && relationDescs[i]->rd_index->indisprimary) + continue; + IndexInfo *indexInfo; indexInfo = indexInfoArray[i]; @@ -190,6 +197,13 @@ CatalogIndexDelete(CatalogIndexState indstate, HeapTuple heapTuple) */ for (i = 0; i < numIndexes; i++) { + /* + * No need to update YugaByte primary key which is intrinic part of + * the base table. + */ + if (IsYugaByteEnabled() && relationDescs[i]->rd_index->indisprimary) + continue; + IndexInfo *indexInfo; indexInfo = indexInfoArray[i]; @@ -318,9 +332,10 @@ CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup) if (IsYugaByteEnabled()) { - HeapTuple oldtup = NULL; + HeapTuple oldtup = NULL; + bool has_indices = HasYBSecondaryIndices(heapRel); - if (heapRel->rd_rel->relhasindex) + if (has_indices) { if (tup->t_ybctid) { @@ -336,7 +351,7 @@ CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup) /* Update the local cache automatically */ YBSetSysCacheTuple(heapRel, tup); - if (heapRel->rd_rel->relhasindex) + if (has_indices) CatalogIndexInsert(indstate, tup); } else @@ -363,9 +378,10 @@ CatalogTupleUpdateWithInfo(Relation heapRel, ItemPointer otid, HeapTuple tup, { if (IsYugaByteEnabled()) { - HeapTuple oldtup = NULL; + HeapTuple oldtup = NULL; + bool has_indices = HasYBSecondaryIndices(heapRel); - if (heapRel->rd_rel->relhasindex) + if (has_indices) { if (tup->t_ybctid) { @@ -381,7 +397,7 @@ CatalogTupleUpdateWithInfo(Relation heapRel, ItemPointer otid, HeapTuple tup, /* Update the local cache automatically */ YBSetSysCacheTuple(heapRel, tup); - if (heapRel->rd_rel->relhasindex) + if (has_indices) CatalogIndexInsert(indstate, tup); } else diff --git a/src/postgres/src/backend/executor/execIndexing.c b/src/postgres/src/backend/executor/execIndexing.c index 63ad9201b3640..971fc3ec0bf31 100644 --- a/src/postgres/src/backend/executor/execIndexing.c +++ b/src/postgres/src/backend/executor/execIndexing.c @@ -322,6 +322,13 @@ ExecInsertIndexTuples(TupleTableSlot *slot, indexInfo = indexInfoArray[i]; + /* + * No need to update YugaByte primary key which is intrinic part of + * the base table. + */ + if (IsYugaByteEnabled() && indexRelation->rd_index->indisprimary) + continue; + /* If the index is marked as read-only, ignore it */ if (!indexInfo->ii_ReadyForInserts) continue; @@ -515,6 +522,13 @@ ExecDeleteIndexTuples(Datum ybctid, HeapTuple tuple, EState *estate) if (indexRelation == NULL) continue; + /* + * No need to update YugaByte primary key which is intrinic part of + * the base table. + */ + if (IsYugaByteEnabled() && indexRelation->rd_index->indisprimary) + continue; + indexInfo = indexInfoArray[i]; /* If the index is marked as read-only, ignore it */ diff --git a/src/postgres/src/backend/executor/nodeModifyTable.c b/src/postgres/src/backend/executor/nodeModifyTable.c index a0d9bf5aaa281..823ba7d42ef96 100644 --- a/src/postgres/src/backend/executor/nodeModifyTable.c +++ b/src/postgres/src/backend/executor/nodeModifyTable.c @@ -94,6 +94,17 @@ static void ExecSetupChildParentMapForSubplan(ModifyTableState *mtstate); static TupleConversionMap *tupconv_map_for_subplan(ModifyTableState *node, int whichplan); +/* + * Returns if a table has secondary indices. + */ +static bool +HasSecondaryIndices(ResultRelInfo *resultRelInfo) +{ + return resultRelInfo->ri_NumIndices > 1 || + (resultRelInfo->ri_NumIndices == 1 && + !resultRelInfo->ri_IndexRelationDescs[0]->rd_index->indisprimary); +} + /* * Verify that the tuples to be produced by INSERT or UPDATE match the * target relation's rowtype @@ -430,10 +441,7 @@ ExecInsert(ModifyTableState *mtstate, bool has_triggers = resultRelInfo->ri_TrigDesc && resultRelInfo->ri_TrigDesc->numtriggers > 0; - bool has_indices = resultRelInfo->ri_NumIndices > 1 || - (resultRelInfo->ri_NumIndices == 1 && - !resultRelInfo->ri_IndexRelationDescs[0]->rd_index - ->indisprimary); + bool has_indices = HasSecondaryIndices(resultRelInfo); bool is_single_row_txn = estate->es_yb_is_single_row_modify_txn && !has_indices && @@ -452,7 +460,7 @@ ExecInsert(ModifyTableState *mtstate, tuple); } - if (resultRelInfo->ri_NumIndices > 0) + if (HasSecondaryIndices(resultRelInfo)) { /* insert index entries for tuple */ recheckIndexes = ExecInsertIndexTuples(slot, tuple, @@ -782,7 +790,7 @@ ExecDelete(ModifyTableState *mtstate, { YBCExecuteDelete(resultRelationDesc, planSlot); - if (resultRelInfo->ri_NumIndices > 0) + if (HasSecondaryIndices(resultRelInfo)) { Datum ybctid = YBCGetYBTupleIdFromSlot(planSlot); @@ -1117,7 +1125,7 @@ ExecUpdate(ModifyTableState *mtstate, if (resultRelInfo->ri_projectReturning) slot = ExecFilterJunk(resultRelInfo->ri_junkFilter, planSlot); - if (resultRelInfo->ri_NumIndices > 0) + if (HasSecondaryIndices(resultRelInfo)) { Datum ybctid = YBCGetYBTupleIdFromSlot(planSlot); @@ -2210,9 +2218,8 @@ ExecModifyTable(PlanState *pstate) * wholerow junk attribute with the ybctid for removing old * index entries for UPDATE and DELETE. */ - if (IsYugaByteEnabled() && - IsYBRelation(resultRelInfo->ri_RelationDesc) && - (resultRelInfo->ri_NumIndices > 0)) + if (IsYBRelation(resultRelInfo->ri_RelationDesc) && + HasSecondaryIndices(resultRelInfo)) { resno = ExecFindJunkAttribute(junkfilter, "wholerow"); datum = ExecGetJunkAttribute(slot, resno, &isNull); @@ -2434,12 +2441,14 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) * already, since we share the resultrel state with the original * query. * - * For a YugaByte table, we delete the old index entries during DELETE so - * the indexes need to be opened still. + * For a YugaByte table, we need to update the secondary indices for + * all of the INSERT, UPDATE, and DELETE statements. */ - if (resultRelInfo->ri_RelationDesc->rd_rel->relhasindex && - (operation != CMD_DELETE || IsYugaByteEnabled()) && - resultRelInfo->ri_IndexRelationDescs == NULL) + if ((IsYBRelation(resultRelInfo->ri_RelationDesc) ? + HasYBSecondaryIndices(resultRelInfo->ri_RelationDesc) : + (resultRelInfo->ri_RelationDesc->rd_rel->relhasindex && + operation != CMD_DELETE)) && + resultRelInfo->ri_IndexRelationDescs == NULL) ExecOpenIndices(resultRelInfo, node->onConflictAction != ONCONFLICT_NONE); diff --git a/src/postgres/src/backend/rewrite/rewriteHandler.c b/src/postgres/src/backend/rewrite/rewriteHandler.c index aef1094334395..77806822883b7 100644 --- a/src/postgres/src/backend/rewrite/rewriteHandler.c +++ b/src/postgres/src/backend/rewrite/rewriteHandler.c @@ -1319,9 +1319,9 @@ rewriteTargetListUD(Query *parsetree, RangeTblEntry *target_rte, if (IsYBRelation(target_relation)) { /* - * If there are indexes on the target table, return the whole row also. + * If there are secondary indices on the target table, return the whole row also. */ - if (target_relation->rd_rel->relhasindex) + if (HasYBSecondaryIndices(target_relation)) { var = makeWholeRowVar(target_rte, parsetree->resultRelation, diff --git a/src/postgres/src/backend/utils/misc/pg_yb_utils.c b/src/postgres/src/backend/utils/misc/pg_yb_utils.c index a3909ea28c608..f9ca2bf181209 100644 --- a/src/postgres/src/backend/utils/misc/pg_yb_utils.c +++ b/src/postgres/src/backend/utils/misc/pg_yb_utils.c @@ -104,6 +104,29 @@ IsYBRelationById(Oid relid) return is_supported; } +bool +HasYBSecondaryIndices(Relation relation) +{ + if (!relation->rd_rel->relhasindex) + return false; + + bool has_indices = false; + List *indexlist = RelationGetIndexList(relation); + ListCell *lc; + + foreach(lc, indexlist) + { + if (lfirst_oid(lc) == relation->rd_pkindex) + continue; + has_indices = true; + break; + } + + list_free(indexlist); + + return has_indices; +} + bool YBNeedRetryAfterCacheRefresh(ErrorData *edata) { diff --git a/src/postgres/src/include/pg_yb_utils.h b/src/postgres/src/include/pg_yb_utils.h index b4b9e1c5df4c5..4a82bbd13749d 100644 --- a/src/postgres/src/include/pg_yb_utils.h +++ b/src/postgres/src/include/pg_yb_utils.h @@ -83,6 +83,8 @@ extern bool IsYBRelationById(Oid relid); extern bool IsYBRelation(Relation relation); +extern bool HasYBSecondaryIndices(Relation relation); + extern bool YBNeedRetryAfterCacheRefresh(ErrorData *error); extern void YBReportFeatureUnsupported(const char *err_msg);