3

I'm trying to add a GIN index that includes a UUID in a Postgres 9.6 database. Technically it is a composite index, with composite GIN support coming from the btree_gin plugin.

I try to create the index with this statement:

CREATE EXTENSION btree_gin;
CREATE INDEX ix_tsv ON text_information USING GIN (client_id, text_search_vector);

but I get this error:

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.

client_id is data type uuid and text_search_vector is a tsvector. I don't think the composite/btree_gin factor is actually relevant, as I get the same error trying to create the index on just client_id alone, but hopefully if there is a solution to this, it is one that will work with a composite index also.

I found PostgreSQL GIN index on array of uuid , which seems to suggest that it should be possible (if an array of UUIDs can be done, then surely an individual UUID can be done). However, the solution there was pretty opaque to me - it's not immediately obvious how to modify this solution to support a single UUID.

I would prefer a solution that doesn't involve casting the UUID to another type in the index or in another column, as I would rather not have to write specialized queries with casts in them (we are using django ORM to generate queries atm).

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Michael Nelson
  • 370
  • 2
  • 11

1 Answers1

5

It is possible for GIN indexes. But not before Postgres 11, where it was added. The release notes:

Allow btree_gin to index bool, bpchar, name and uuid data types (Matheus Oliveira)

So the simple solution is to upgrade to Postgres 11. This should be good news for you:

April 9, 2019: Cloud SQL now supports PostgreSQL version 11.1 Beta

Or, in many cases you can alternatively use a GiST index, for which the same was introduced with Postgres 10, already. The release notes:

Add indexing support to btree_gist for the UUID data type (Paul Jungwirth)

Related:

If neither is an option, you are back to what you wanted to avoid:

casting the uuid to another type in the index

You can create an expression index on a (consistent!) text representation or, theoretically, on two bigint columns derived from the uuid. But the first makes the index considerably bigger and slower and the second creates much more complication ...

The syntax of the cast is simple enough though: uuid::text. In an index expression that requires an extra set of parentheses. With the additional module btree_gin installed:

CREATE INDEX ix_uuid_tsv ON text_information USING GIN ((client_id::uuid), tsv);

Related:

Or you could backport the feature from Postgres 11 - which is not an option with a hosted service like Google Cloud SQL for PostgreSQL as you mentioned in a comment. And I hardly see the use case where one would be skilled enough to implement the backport, but not to upgrade to Postgres 11.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • Unfortunately, we are using a hosted Postgres solution from Google cloud, so upgrading beyond 9.6 is not possible. – Michael Nelson Apr 15 '19 at 15:29
  • Unfortunate, indeed. You are back to where you didn't want to go then ... – Erwin Brandstetter Apr 15 '19 at 16:20
  • 1
    @MichaelNelson at Cloud Next '19 Google announced PostGreSQL 11 is now available as beta: "Cloud SQL for PostgreSQL became one of the fastest-growing databases on GCP after it became generally available last year. We’ve heard you want the latest version, so we are pleased to announce PostgreSQL version 11 support, which includes useful new features like partitioning improvements, stored procedures, and more parallelism." SOURCE: https://cloud.google.com/blog/products/databases/enterprise-databases-managed-for-you – benvdh Apr 15 '19 at 17:26
  • @benvdh: Just found & added the same from the release notes. :) – Erwin Brandstetter Apr 15 '19 at 17:30
  • I had this issue with 9.6, but we only had to upgrade to Postgres 10, not 11 to fix the issue – Simbosan Aug 09 '21 at 04:11