* RelationGetIdentityKeyBitmap -- get a bitmap of replica identity attribute * numbers * * A bitmap of index attribute numbers for the configured replica identity * index is returned. * * See also comments of RelationGetIndexAttrBitmap(). * * This is a special purpose function used during logical replication. Here, * unlike RelationGetIndexAttrBitmap(), we don't acquire a lock on the requ
| 5614 | * need to retry here in case of a change in the set of indexes. |
| 5615 | */ |
| 5616 | Bitmapset * |
| 5617 | RelationGetIdentityKeyBitmap(Relation relation) |
| 5618 | { |
| 5619 | Bitmapset *idindexattrs = NULL; /* columns in the replica identity */ |
| 5620 | Relation indexDesc; |
| 5621 | int i; |
| 5622 | Oid replidindex; |
| 5623 | MemoryContext oldcxt; |
| 5624 | |
| 5625 | /* Quick exit if we already computed the result */ |
| 5626 | if (relation->rd_idattr != NULL) |
| 5627 | return bms_copy(relation->rd_idattr); |
| 5628 | |
| 5629 | /* Fast path if definitely no indexes */ |
| 5630 | if (!RelationGetForm(relation)->relhasindex) |
| 5631 | return NULL; |
| 5632 | |
| 5633 | /* Historic snapshot must be set. */ |
| 5634 | Assert(HistoricSnapshotActive()); |
| 5635 | |
| 5636 | replidindex = RelationGetReplicaIndex(relation); |
| 5637 | |
| 5638 | /* Fall out if there is no replica identity index */ |
| 5639 | if (!OidIsValid(replidindex)) |
| 5640 | return NULL; |
| 5641 | |
| 5642 | /* Look up the description for the replica identity index */ |
| 5643 | indexDesc = RelationIdGetRelation(replidindex); |
| 5644 | |
| 5645 | if (!RelationIsValid(indexDesc)) |
| 5646 | elog(ERROR, "could not open relation with OID %u", |
| 5647 | relation->rd_replidindex); |
| 5648 | |
| 5649 | /* Add referenced attributes to idindexattrs */ |
| 5650 | for (i = 0; i < indexDesc->rd_index->indnatts; i++) |
| 5651 | { |
| 5652 | int attrnum = indexDesc->rd_index->indkey.values[i]; |
| 5653 | |
| 5654 | /* |
| 5655 | * We don't include non-key columns into idindexattrs bitmaps. See |
| 5656 | * RelationGetIndexAttrBitmap. |
| 5657 | */ |
| 5658 | if (attrnum != 0) |
| 5659 | { |
| 5660 | if (i < indexDesc->rd_index->indnkeyatts) |
| 5661 | idindexattrs = bms_add_member(idindexattrs, |
| 5662 | attrnum - FirstLowInvalidHeapAttributeNumber); |
| 5663 | } |
| 5664 | } |
| 5665 | |
| 5666 | RelationClose(indexDesc); |
| 5667 | |
| 5668 | /* Don't leak the old values of these bitmaps, if any */ |
| 5669 | bms_free(relation->rd_idattr); |
| 5670 | relation->rd_idattr = NULL; |
| 5671 | |
| 5672 | /* Now save copy of the bitmap in the relcache entry */ |
| 5673 | oldcxt = MemoryContextSwitchTo(CacheMemoryContext); |
no test coverage detected