PostgreSQL GIN index on array of uuid

前端 未结 2 736
别那么骄傲
别那么骄傲 2020-12-25 14:37

I would like to use a GIN index on uuid[] (to have efficient membership tests for arrays of uuids). However when I try it PostgreSQL gives me an error:

相关标签:
2条回答
  • 2020-12-25 15:12

    As of PostgreSQL 10 the custom operator class _uuid_ops is no longer necessary as there is now a general built-in opclass array_ops on anyarry (see: https://www.postgresql.org/docs/current/static/gin-builtin-opclasses.html)

    0 讨论(0)
  • 2020-12-25 15:18

    Note: this answer is obsolete as this is now part of a standard PostgreSQL, see tbussmann's other answer (which you should upvote).

    Original answer:

    This can be done using the following operator class:

    CREATE OPERATOR CLASS _uuid_ops DEFAULT 
      FOR TYPE _uuid USING gin AS 
      OPERATOR 1 &&(anyarray, anyarray), 
      OPERATOR 2 @>(anyarray, anyarray), 
      OPERATOR 3 <@(anyarray, anyarray), 
      OPERATOR 4 =(anyarray, anyarray), 
      FUNCTION 1 uuid_cmp(uuid, uuid), 
      FUNCTION 2 ginarrayextract(anyarray, internal, internal), 
      FUNCTION 3 ginqueryarrayextract(anyarray, internal, smallint, internal, internal, internal, internal), 
      FUNCTION 4 ginarrayconsistent(internal, smallint, anyarray, integer, internal, internal, internal, internal), 
      STORAGE uuid;
    

    Credits to this for pointing me in the right direction.

    The relevant documentation is in Interfacing extensions to indexes, in particular the operator strategy and function numbers for GIN are described there.

    0 讨论(0)
提交回复
热议问题