Search code examples
sqlpostgresqlpattern-matchingsearch-path

How to use % operator from the extension pg_trgm?


I have my pg_trgm module installed.

pg_trgm | 1.0     | extensions | text similarity measurement and index ...

The schema set is extensions. To use it I have to run something like this select:

extensions.similarity('hello','hallo');

I'm trying to run a statement using the % operator and got the following message.

mydb=# select * from rssdata where description % 'Brazil';
ERROR:  operator does not exist: character varying % unknown
LINE 1: select * from rssdata where description % 'Brazil';
                                            ^
HINT:  No operator matches the given name and argument type(s).
You might need to add explicit type casts. 

What is necessary to run % or <-> operators?


Solution

  • Most probably the problem is with the search_path setting. Run:

    SHOW search_path;
    

    Is the schema where you installed pg_trgm included? If not, include it.

    Or, if you have the necessary privileges, you can change the schema of an extension with:

    ALTER EXTENSION pg_trgm SET SCHEMA public;  -- or the schema you want
    

    Alternatively, you can schema-qualify functions - and even operators using the OPERATOR() construct:

    SELECT * FROM rssdata WHERE extensions.similarity(description, 'Brazil') > .8;
    SELECT * FROM rssdata WHERE description OPERATOR(extensions.%) 'Brazil';
    

    Removes dependency on the search_path. But it's tedious.