【问题标题】:Create index on composite type in Postgres在 Postgres 中为复合类型创建索引
【发布时间】:2020-06-12 04:46:13
【问题描述】:

我正在运行 Postgres 12 我有一个审计字段复合类型,用于防止我的表太胖。

    create type adt_flds as
(
    created_at timestamp,
    updated_at timestamp,
    created_by text,
    updated_by text,
    created_by_client_addr inet,
    updated_by_client_addr inet,
    created_by_client_cmptr text,
    updated_by_client_cmptr text
);

   create table if not exists excel.equip_type
   (
       equip_type_id serial not null constraint equip_type_pk primary key,
       descrip text,
       adt_flds adt_flds 
   );

--Creates a syntax error
create index equip_type_adt_flds_index
    on excel.equip_type (adt_flds.created_at);

我想要索引 created_at 字段,所以当我在 WHERE 语句中过滤 created_at 时,我的查询可以索引扫描而不是堆扫描。我的问题是我的 create index 语句出现语法错误。我将如何修复我的语法,以便我可以在我的 adt_flds 类型的只有 1 个元素上建立索引?

[42601] 错误:“)”位置或附近的语法错误:85

【问题讨论】:

  • 您的意思是equip_type 而不是equip_usage?你能告诉我们你的语法错误吗?
  • 我确实做到了。谢谢。我更正了问题

标签: postgresql


【解决方案1】:

The PostgreSQL documentation 非常清楚在表达式而非表列上创建的索引的语法:

表达式通常必须用括号括起来,如语法所示。但是,如果表达式是函数调用的形式,括号可以省略。

您需要额外的括号来消除歧义。

试试

CREATE INDEX equip_type_adt_flds_index
   ON excel.equip_type (((adt_flds).created_at));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 2019-04-16
    • 2013-03-29
    • 2013-02-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多