(self)
| 4968 | |
| 4969 | @util.memoized_property |
| 4970 | def _index_query(self): |
| 4971 | # NOTE: pg_index is used as from two times to improve performance, |
| 4972 | # since extraing all the index information from `idx_sq` to avoid |
| 4973 | # the second pg_index use leads to a worse performing query in |
| 4974 | # particular when querying for a single table (as of pg 17) |
| 4975 | # NOTE: repeating oids clause improve query performance |
| 4976 | |
| 4977 | # subquery to get the columns |
| 4978 | idx_sq = ( |
| 4979 | select( |
| 4980 | pg_catalog.pg_index.c.indexrelid, |
| 4981 | pg_catalog.pg_index.c.indrelid, |
| 4982 | sql.func.unnest(pg_catalog.pg_index.c.indkey).label("attnum"), |
| 4983 | sql.func.unnest(pg_catalog.pg_index.c.indclass).label( |
| 4984 | "att_opclass" |
| 4985 | ), |
| 4986 | sql.func.generate_subscripts( |
| 4987 | pg_catalog.pg_index.c.indkey, 1 |
| 4988 | ).label("ord"), |
| 4989 | ) |
| 4990 | .where( |
| 4991 | ~pg_catalog.pg_index.c.indisprimary, |
| 4992 | pg_catalog.pg_index.c.indrelid.in_(bindparam("oids")), |
| 4993 | ) |
| 4994 | .subquery("idx") |
| 4995 | ) |
| 4996 | |
| 4997 | attr_sq = ( |
| 4998 | select( |
| 4999 | idx_sq.c.indexrelid, |
| 5000 | idx_sq.c.indrelid, |
| 5001 | idx_sq.c.ord, |
| 5002 | # NOTE: always using pg_get_indexdef is too slow so just |
| 5003 | # invoke when the element is an expression |
| 5004 | sql.case( |
| 5005 | ( |
| 5006 | idx_sq.c.attnum == 0, |
| 5007 | pg_catalog.pg_get_indexdef( |
| 5008 | idx_sq.c.indexrelid, idx_sq.c.ord + 1, True |
| 5009 | ), |
| 5010 | ), |
| 5011 | # NOTE: need to cast this since attname is of type "name" |
| 5012 | # that's limited to 63 bytes, while pg_get_indexdef |
| 5013 | # returns "text" so its output may get cut |
| 5014 | else_=pg_catalog.pg_attribute.c.attname.cast(TEXT), |
| 5015 | ).label("element"), |
| 5016 | (idx_sq.c.attnum == 0).label("is_expr"), |
| 5017 | # since it's converted to array cast it to bigint (oid are |
| 5018 | # "unsigned four-byte integer") to make it easier for |
| 5019 | # dialects to interpret |
| 5020 | idx_sq.c.att_opclass.cast(BIGINT), |
| 5021 | ) |
| 5022 | .select_from(idx_sq) |
| 5023 | .outerjoin( |
| 5024 | # do not remove rows where idx_sq.c.attnum is 0 |
| 5025 | pg_catalog.pg_attribute, |
| 5026 | sql.and_( |
| 5027 | pg_catalog.pg_attribute.c.attnum == idx_sq.c.attnum, |
nothing calls this directly
no test coverage detected