如何运行查询(在PostgreSQL内部表上)以检索部分索引的条件表达式(在本例中为roles_user_group_role_idx
)。
# \d roles
Table "public.roles"
Column | Type | Modifiers
---------------+-----------------------------+---------------------------------------
id | character varying(15) | not null default next_id('r'::bpchar)
user_id | character varying(15) | not null
group_id | character varying(15) | not null
role | character varying(255) | not null
language_code | character varying(2) |
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
Indexes:
"roles_pkey" PRIMARY KEY, btree (id)
"roles_user_group_role_idx" UNIQUE, btree (user_id, group_id, role) WHERE language_code IS NULL
根据PostgreSQL pg_index doc,pg_index.indpred似乎是我需要研究的领域。运行此查询:
SELECT
C.oid,
I.indpred
FROM pg_catalog.pg_class C,
pg_catalog.pg_namespace N,
pg_catalog.pg_index I
WHERE C.relname = 'roles_user_group_role_idx'
AND C.relnamespace = N.oid
AND I.indexrelid = C.oid
AND N.nspname = 'public';
给了我这个,但并不存在。
oid | indpred
---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1235504 | {NULLTEST :arg {VAR :varno 1 :varattno 5 :vartype 1043 :vartypmod 6 :varcollid 100 :varlevelsup 0 :varnoold 1 :varoattno 5 :location 95} :nulltesttype 0 :argisrow false}
我正朝着正确的方向前进吗?如何获取部分索引的WHERE子句?我正在使用PG 9.2
答案 0 :(得分:7)
在谷歌搜索后,我找到了答案here,正确的查询:
SELECT
C.oid,
pg_get_expr(I.indpred, I.indrelid)
FROM pg_catalog.pg_class C,
pg_catalog.pg_namespace N,
pg_catalog.pg_index I
WHERE C.relname = 'roles_user_group_role_idx'
AND C.relnamespace = N.oid
AND I.indexrelid = C.oid
AND N.nspname = 'public';
结果:
oid | pg_get_expr
---------+-------------------------
1235504 | (language_code IS NULL)