3

我将如何运行查询(在 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

4

1 回答 1

8

好吧,经过一番谷歌搜索后,我在这里找到了答案,正确的查询:

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)
于 2013-04-15T03:34:40.157 回答