【问题标题】:How do you examine objects from psql?你如何检查来自 psql 的对象?
【发布时间】:2011-08-30 20:37:12
【问题描述】:

我想检查 psql 中的索引(和其他对象)。

我创建了索引:

CREATE INDEX my_index on "my_table" (iso_country_code, type_name, UPPER(name) varchar_pattern_ops);

在 psql 中我使用了 describe 命令:

db=> \d+ my_index
Index "public.my_index"
      Column      |          Type          | Storage  | Description 
------------------+------------------------+----------+-------------
 iso_country_code | character varying(3)   | extended | 
 type_name        | character varying(300) | extended | 
 pg_expression_3  | text                   | extended | 
btree, for table "public.my_table"

索引最后一个字段中的表达式只显示pg_expression_3。为什么表达式没有出现在 psql 输出中?

我可以使用 pg_dump 检索表架构,包括 CREATE INDEX 语句:

pg_dump --table my_table --schema-only my_db

【问题讨论】:

    标签: sql database postgresql database-schema


    【解决方案1】:

    这显然是 9.0 之前的行为,请查看 E.5. Release 9.0

    • \d index_name (Khee Chin) 中显示索引列的定义

      该定义对表达式索引很有用。

    对于以前的版本,您仍然可以手动遍历 system catalogs 并使用 pg_get_indexdef(index_oid, column_no, pretty_bool) 函数获取表达式,例如:

    => SELECT oid FROM pg_catalog.pg_class c WHERE c.relname LIKE 'my_index';
      oid  
    -------
     25240
    (1 row)
    => SELECT attname, attnum FROM pg_attribute a WHERE a.attrelid = '25240';
         attname      | attnum 
    ------------------+--------
     iso_country_code |      1
     type_name        |      2
     pg_expression_3  |      3
    (3 rows)
    => SELECT pg_get_indexdef(25240, 3, true);
     pg_get_indexdef 
    -----------------
     upper(name)
    (1 row)
    

    【讨论】:

    • 使用8.x版本的时候比较麻烦。
    猜你喜欢
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多