* Generate an IndexStmt node using information from an already existing index * "source_idx". * * heapRel is stored into the IndexStmt's relation field, but we don't use it * otherwise; some callers pass NULL, if they don't need it to be valid. * (The target relation might not exist yet, so we mustn't try to access it.) * * Attribute numbers in expression Vars are adjusted according to attm
| 1693 | * complain if that fails to happen). |
| 1694 | */ |
| 1695 | IndexStmt * |
| 1696 | generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx, |
| 1697 | const AttrMap *attmap, |
| 1698 | Oid *constraintOid) |
| 1699 | { |
| 1700 | Oid source_relid = RelationGetRelid(source_idx); |
| 1701 | HeapTuple ht_idxrel; |
| 1702 | HeapTuple ht_idx; |
| 1703 | HeapTuple ht_am; |
| 1704 | Form_pg_class idxrelrec; |
| 1705 | Form_pg_index idxrec; |
| 1706 | Form_pg_am amrec; |
| 1707 | oidvector *indcollation; |
| 1708 | oidvector *indclass; |
| 1709 | IndexStmt *index; |
| 1710 | List *indexprs; |
| 1711 | ListCell *indexpr_item; |
| 1712 | Oid indrelid; |
| 1713 | Oid constraintId = InvalidOid; |
| 1714 | int keyno; |
| 1715 | Oid keycoltype; |
| 1716 | Datum datum; |
| 1717 | bool isnull; |
| 1718 | |
| 1719 | if (constraintOid) |
| 1720 | *constraintOid = InvalidOid; |
| 1721 | |
| 1722 | /* |
| 1723 | * Fetch pg_class tuple of source index. We can't use the copy in the |
| 1724 | * relcache entry because it doesn't include optional fields. |
| 1725 | */ |
| 1726 | ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(source_relid)); |
| 1727 | if (!HeapTupleIsValid(ht_idxrel)) |
| 1728 | elog(ERROR, "cache lookup failed for relation %u", source_relid); |
| 1729 | idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel); |
| 1730 | |
| 1731 | /* Fetch pg_index tuple for source index from relcache entry */ |
| 1732 | ht_idx = source_idx->rd_indextuple; |
| 1733 | idxrec = (Form_pg_index) GETSTRUCT(ht_idx); |
| 1734 | indrelid = idxrec->indrelid; |
| 1735 | |
| 1736 | /* Fetch the pg_am tuple of the index' access method */ |
| 1737 | ht_am = SearchSysCache1(AMOID, ObjectIdGetDatum(idxrelrec->relam)); |
| 1738 | if (!HeapTupleIsValid(ht_am)) |
| 1739 | elog(ERROR, "cache lookup failed for access method %u", |
| 1740 | idxrelrec->relam); |
| 1741 | amrec = (Form_pg_am) GETSTRUCT(ht_am); |
| 1742 | |
| 1743 | /* Extract indcollation from the pg_index tuple */ |
| 1744 | datum = SysCacheGetAttr(INDEXRELID, ht_idx, |
| 1745 | Anum_pg_index_indcollation, &isnull); |
| 1746 | Assert(!isnull); |
| 1747 | indcollation = (oidvector *) DatumGetPointer(datum); |
| 1748 | |
| 1749 | /* Extract indclass from the pg_index tuple */ |
| 1750 | datum = SysCacheGetAttr(INDEXRELID, ht_idx, |
| 1751 | Anum_pg_index_indclass, &isnull); |
| 1752 | Assert(!isnull); |
no test coverage detected