【问题标题】:Error getting when creating gin index on jsonb column postgresql9.5在 jsonb 列 postgresql 9.5 上创建 gin 索引时出错
【发布时间】:2017-02-19 15:59:56
【问题描述】:

我有product_reviews TABLE 和product_review jsonb 列。

jsonb column : [{"comment": [{"condition": "Good", "Entered": "true"}], "productid": 321}]

CREATE INDEX idx_product_reviews_product_review ON product_reviews  USING gin (CAST (product_review ->> 'productid') AS bigint )   ;

所以当我尝试在这个 jsonb 列上创建 gin 索引时,我得到了错误。

ERROR:  data type bigint has no default operator class for access method "gin"
HINT:  You must specify an operator class for the index or define a default operator class for the data type.

我想将 productid 索引为 bigint 类型值,因此我可以获取 jsonb“comment”值来比较这个 productid。

【问题讨论】:

  • 对于功能索引,你应该使用经典的 btree 索引 - 不要使用 gin
  • 当我用 BTREE 更改 GIN 时,没有给出错误...
  • 但是为什么我不能像我的例子那样使用 GIN?
  • 通常 GIN,GiST 索引用于未定义操作 <> 的类型。定义这些操作后,您可以使用 BTREE 索引。几乎所有类型都支持 GIN、GIST 索引或 BTREE 索引 - 不是两种情况。
  • 数组可以有 GIN 或 GiST 索引。这些索引在结果长度和数组长度方面具有不同的行为。您应该检查极端情况。

标签: postgresql indexing postgresql-9.5


【解决方案1】:

btree_gin https://www.postgresql.org/docs/9.5/btree-gin.html 扩展为许多数据类型提供了 gin 运算符类,包括 bigint。

使用 btree_gin 扩展后创建索引

CREATE EXTENSION btree_gin;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多