* RelationGetIndexAttrBitmap -- get a bitmap of index attribute numbers * * The result has a bit set for each attribute used anywhere in the index * definitions of all the indexes on this relation. (This includes not only * simple index keys, but attributes used in expressions and partial-index * predicates.) * * Depending on attrKind, a bitmap covering the attnums for all index columns,
| 5370 | * be bms_free'd when not needed anymore. |
| 5371 | */ |
| 5372 | Bitmapset * |
| 5373 | RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind) |
| 5374 | { |
| 5375 | Bitmapset *indexattrs; /* indexed columns */ |
| 5376 | Bitmapset *uindexattrs; /* columns in unique indexes */ |
| 5377 | Bitmapset *pkindexattrs; /* columns in the primary index */ |
| 5378 | Bitmapset *idindexattrs; /* columns in the replica identity */ |
| 5379 | List *indexoidlist; |
| 5380 | List *newindexoidlist; |
| 5381 | Oid relpkindex; |
| 5382 | Oid relreplindex; |
| 5383 | ListCell *l; |
| 5384 | MemoryContext oldcxt; |
| 5385 | |
| 5386 | /* Quick exit if we already computed the result. */ |
| 5387 | if (relation->rd_indexattr != NULL) |
| 5388 | { |
| 5389 | switch (attrKind) |
| 5390 | { |
| 5391 | case INDEX_ATTR_BITMAP_ALL: |
| 5392 | return bms_copy(relation->rd_indexattr); |
| 5393 | case INDEX_ATTR_BITMAP_KEY: |
| 5394 | return bms_copy(relation->rd_keyattr); |
| 5395 | case INDEX_ATTR_BITMAP_PRIMARY_KEY: |
| 5396 | return bms_copy(relation->rd_pkattr); |
| 5397 | case INDEX_ATTR_BITMAP_IDENTITY_KEY: |
| 5398 | return bms_copy(relation->rd_idattr); |
| 5399 | default: |
| 5400 | elog(ERROR, "unknown attrKind %u", attrKind); |
| 5401 | } |
| 5402 | } |
| 5403 | |
| 5404 | /* Fast path if definitely no indexes */ |
| 5405 | if (!RelationGetForm(relation)->relhasindex) |
| 5406 | return NULL; |
| 5407 | |
| 5408 | /* |
| 5409 | * Get cached list of index OIDs. If we have to start over, we do so here. |
| 5410 | */ |
| 5411 | restart: |
| 5412 | indexoidlist = RelationGetIndexList(relation); |
| 5413 | |
| 5414 | /* Fall out if no indexes (but relhasindex was set) */ |
| 5415 | if (indexoidlist == NIL) |
| 5416 | return NULL; |
| 5417 | |
| 5418 | /* |
| 5419 | * Copy the rd_pkindex and rd_replidindex values computed by |
| 5420 | * RelationGetIndexList before proceeding. This is needed because a |
| 5421 | * relcache flush could occur inside index_open below, resetting the |
| 5422 | * fields managed by RelationGetIndexList. We need to do the work with |
| 5423 | * stable values of these fields. |
| 5424 | */ |
| 5425 | relpkindex = relation->rd_pkindex; |
| 5426 | relreplindex = relation->rd_replidindex; |
| 5427 | |
| 5428 | /* |
| 5429 | * For each index, add referenced attributes to indexattrs. |
no test coverage detected