I am using GIN indexes with the pg_trgm
module for indexing varchar
fields. So, to define an index I have to write something like this:
CREATE INDEX gin_field_idx ON table_name USING gin (field gin_trgm_ops);
If I exclude the operator class (git_trgm_ops
) and write:
CREATE INDEX gin_field_idx ON table_name USING gin (field);
PostgreSQL will raise an error:
ERROR: data type character varying 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 do I define a default operator method from the module?
This does not help. It describes how to define a new operator class from extension operators and functions. But I have to define the default operator class from a module.
Any help will be appreciated :-).
1 Answer 1
you can update the pg_opclass table and set the default.
update pg_opclass set opcdefault = true where opcname='gin_trgm_ops'
pg_trgm doesn't do this by default as you might have/want a different GIN default.
select * from pg_opclass where opcname = 'gin_trgm_ops';
than you can create an index:
create index ON table USING gin (field); -- it will use the new default
-
3Let me add a small advisory: manually changing the system catalog may result in unexpected behaviour. This solution works until there is no conflict on the default operator class: if another extension would have its set to default for type
text
, this change would cause a conflict.András Váczi– András Váczi2016年08月02日 12:37:18 +00:00Commented Aug 2, 2016 at 12:37 -
This works immediately, but is pretty dreadful. You are almost guaranteeing yourself problems when it comes to upgrading your database or merging two databases together into one, or splitting one apart.jjanes– jjanes2016年08月03日 15:10:30 +00:00Commented Aug 3, 2016 at 15:10
-
Postgres doesn't have a preference to what is the default behavior, it's up to the DBA to make sure which opclass is default. As with any upgrade/migration the DBA will need to make sure that this setting persists.cohenjo– cohenjo2016年08月03日 15:28:52 +00:00Commented Aug 3, 2016 at 15:28
-
@cohenjo Sure the DBA can micromanage the database, but isn't that directly at odds with what RoR is intended for?jjanes– jjanes2016年08月04日 04:34:36 +00:00Commented Aug 4, 2016 at 4:34
-
It's a simple update to run with the extension creation, it's a small price to pay :)cohenjo– cohenjo2016年08月04日 06:03:35 +00:00Commented Aug 4, 2016 at 6:03
pg_trgm
in order to save a few characters when creating indexes?pg_trgm
to database of large Ruby on Rails(RoR) project. RoR have two ways of migrating database: 1 - byrake db:migrate
or 2 - by loadingschema.rb
. In both cases it breaks older migrations indexes, those already has GIN index type, because they do not have described class. So i want to add default class for not to break older migrations.gin("somecol", gin_trgm_ops)
. And I have the extension created too.