(self)
| 4783 | |
| 4784 | @util.memoized_property |
| 4785 | def _index_query(self): |
| 4786 | # NOTE: pg_index is used as from two times to improve performance, |
| 4787 | # since extraing all the index information from `idx_sq` to avoid |
| 4788 | # the second pg_index use leads to a worse performing query in |
| 4789 | # particular when querying for a single table (as of pg 17) |
| 4790 | # NOTE: repeating oids clause improve query performance |
| 4791 | |
| 4792 | # subquery to get the columns |
| 4793 | idx_sq = ( |
| 4794 | select( |
| 4795 | pg_catalog.pg_index.c.indexrelid, |
| 4796 | pg_catalog.pg_index.c.indrelid, |
| 4797 | sql.func.unnest(pg_catalog.pg_index.c.indkey).label("attnum"), |
| 4798 | sql.func.unnest(pg_catalog.pg_index.c.indclass).label( |
| 4799 | "att_opclass" |
| 4800 | ), |
| 4801 | sql.func.generate_subscripts( |
| 4802 | pg_catalog.pg_index.c.indkey, 1 |
| 4803 | ).label("ord"), |
| 4804 | ) |
| 4805 | .where( |
| 4806 | ~pg_catalog.pg_index.c.indisprimary, |
| 4807 | pg_catalog.pg_index.c.indrelid.in_(bindparam("oids")), |
| 4808 | ) |
| 4809 | .subquery("idx") |
| 4810 | ) |
| 4811 | |
| 4812 | attr_sq = ( |
| 4813 | select( |
| 4814 | idx_sq.c.indexrelid, |
| 4815 | idx_sq.c.indrelid, |
| 4816 | idx_sq.c.ord, |
| 4817 | # NOTE: always using pg_get_indexdef is too slow so just |
| 4818 | # invoke when the element is an expression |
| 4819 | sql.case( |
| 4820 | ( |
| 4821 | idx_sq.c.attnum == 0, |
| 4822 | pg_catalog.pg_get_indexdef( |
| 4823 | idx_sq.c.indexrelid, idx_sq.c.ord + 1, True |
| 4824 | ), |
| 4825 | ), |
| 4826 | # NOTE: need to cast this since attname is of type "name" |
| 4827 | # that's limited to 63 bytes, while pg_get_indexdef |
| 4828 | # returns "text" so its output may get cut |
| 4829 | else_=pg_catalog.pg_attribute.c.attname.cast(TEXT), |
| 4830 | ).label("element"), |
| 4831 | (idx_sq.c.attnum == 0).label("is_expr"), |
| 4832 | pg_catalog.pg_opclass.c.opcname, |
| 4833 | pg_catalog.pg_opclass.c.opcdefault, |
| 4834 | ) |
| 4835 | .select_from(idx_sq) |
| 4836 | .outerjoin( |
| 4837 | # do not remove rows where idx_sq.c.attnum is 0 |
| 4838 | pg_catalog.pg_attribute, |
| 4839 | sql.and_( |
| 4840 | pg_catalog.pg_attribute.c.attnum == idx_sq.c.attnum, |
| 4841 | pg_catalog.pg_attribute.c.attrelid == idx_sq.c.indrelid, |
| 4842 | ), |
nothing calls this directly
no test coverage detected