19

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:

mydb=> CREATE TABLE foo (val uuid[]);
CREATE TABLE
mydb=> CREATE INDEX foo_idx ON foo USING GIN(val);
ERROR:  data type uuid[] 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.

How can I add the necessary operator class so that it works?

Note that this is a similar question for the type citext but the provided answer doesn't work.

Community
  • 1
  • 1
Florent Guillaume
  • 8,243
  • 1
  • 24
  • 25

2 Answers2

30

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.

Florent Guillaume
  • 8,243
  • 1
  • 24
  • 25
  • Curious to know: might adding btree_gin have been enough, or was the above required? – Denis de Bernardy Nov 13 '13 at 18:39
  • 1
    No, [btree_gin](http://www.postgresql.org/docs/9.3/static/btree-gin.html) does not include array types. – Florent Guillaume Nov 13 '13 at 22:22
  • 2
    Just a note, you can't use this on Heroku or any other postgres host, as it requires superuser privs to create an operator class! – Michael Baldry Nov 28 '14 at 08:28
  • 1
    I tried using this on PostGres 9.6 but after running it I receive the same error as the OP. I thought maybe the underscore in `FOR TYPE _uuid` was a typo, but when I create `FOR TYPE uuid` I get another error when creating the index `cache lookup failed for type 3177436692` . Any thoughts on what I might be doing wrong? – Michael Nelson Apr 04 '19 at 00:53
  • 1
    What about upgrades? Should we drop this operator class definition if, say, we go from 9.6 to version 10? – Pawel Zieminski Mar 10 '20 at 19:24
13

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)

tbussmann
  • 593
  • 7
  • 11