* Initialize index-access-method support data for an index relation */
| 1469 | * Initialize index-access-method support data for an index relation |
| 1470 | */ |
| 1471 | void |
| 1472 | RelationInitIndexAccessInfo(Relation relation) |
| 1473 | { |
| 1474 | HeapTuple tuple; |
| 1475 | Form_pg_am aform; |
| 1476 | Datum indcollDatum; |
| 1477 | Datum indclassDatum; |
| 1478 | Datum indoptionDatum; |
| 1479 | bool isnull; |
| 1480 | oidvector *indcoll; |
| 1481 | oidvector *indclass; |
| 1482 | int2vector *indoption; |
| 1483 | MemoryContext indexcxt; |
| 1484 | MemoryContext oldcontext; |
| 1485 | int indnatts; |
| 1486 | int indnkeyatts; |
| 1487 | uint16 amsupport; |
| 1488 | |
| 1489 | /* |
| 1490 | * Make a copy of the pg_index entry for the index. Since pg_index |
| 1491 | * contains variable-length and possibly-null fields, we have to do this |
| 1492 | * honestly rather than just treating it as a Form_pg_index struct. |
| 1493 | */ |
| 1494 | tuple = SearchSysCache1(INDEXRELID, |
| 1495 | ObjectIdGetDatum(RelationGetRelid(relation))); |
| 1496 | if (!HeapTupleIsValid(tuple)) |
| 1497 | elog(ERROR, "cache lookup failed for index %u", |
| 1498 | RelationGetRelid(relation)); |
| 1499 | oldcontext = MemoryContextSwitchTo(CacheMemoryContext); |
| 1500 | relation->rd_indextuple = heap_copytuple(tuple); |
| 1501 | relation->rd_index = (Form_pg_index) GETSTRUCT(relation->rd_indextuple); |
| 1502 | MemoryContextSwitchTo(oldcontext); |
| 1503 | ReleaseSysCache(tuple); |
| 1504 | |
| 1505 | /* |
| 1506 | * Look up the index's access method, save the OID of its handler function |
| 1507 | */ |
| 1508 | tuple = SearchSysCache1(AMOID, ObjectIdGetDatum(relation->rd_rel->relam)); |
| 1509 | if (!HeapTupleIsValid(tuple)) |
| 1510 | elog(ERROR, "cache lookup failed for access method %u", |
| 1511 | relation->rd_rel->relam); |
| 1512 | aform = (Form_pg_am) GETSTRUCT(tuple); |
| 1513 | relation->rd_amhandler = aform->amhandler; |
| 1514 | ReleaseSysCache(tuple); |
| 1515 | |
| 1516 | indnatts = RelationGetNumberOfAttributes(relation); |
| 1517 | if (indnatts != IndexRelationGetNumberOfAttributes(relation)) |
| 1518 | elog(ERROR, "relnatts disagrees with indnatts for index %u", |
| 1519 | RelationGetRelid(relation)); |
| 1520 | indnkeyatts = IndexRelationGetNumberOfKeyAttributes(relation); |
| 1521 | |
| 1522 | /* |
| 1523 | * Make the private context to hold index access info. The reason we need |
| 1524 | * a context, and not just a couple of pallocs, is so that we won't leak |
| 1525 | * any subsidiary info attached to fmgr lookup records. |
| 1526 | */ |
| 1527 | indexcxt = AllocSetContextCreate(CacheMemoryContext, |
| 1528 | "index info", |
no test coverage detected