This psql session snippet should be self-explanatory:
psql (9.1.7)
Type "help" for help.
=> CREATE TABLE languages(language VARCHAR NOT NULL);
CREATE TABLE
=> INSERT INTO languages VALUES ('english'),('french'),('turkish');
INSERT 0 3
=> SELECT language, to_tsvector('english', 'hello world') FROM languages;
language| to_tsvector
---------+---------------------
english | 'hello':1 'world':2
french | 'hello':1 'world':2
turkish | 'hello':1 'world':2
(3 rows)
=> SELECT language, to_tsvector(language, 'hello world') FROM languages;
ERROR: function to_tsvector(character varying, unknown) does not exist
LINE 1: select language, to_tsvector(language, 'hello world')...
^
HINT: No function matches the given name and argument types.
You might need to add explicit type casts.
The problem is that Postgres function to_tsvector doesn’t like varchar field type but this call should be perfectly correct according to the documentation?
Use an explicit type cast:
Or change the column
languages.languageto typeregconfig. See @Swav’s answer.Why?
Postgres allows function overloading. Function signatures are defined by their (optionally schema-qualified) name plus (the list of) input parameter type(s). The 2-parameter form of
to_tsvector()expects typeregconfigas first parameter:If no existing function matches exactly, the rules of Function Type Resolution decide the best match – if any. This is successful for
to_tsvector('english', 'hello world'), with'english'being an untyped string literal. But fails with a parameter typedvarchar, because there is no registered implicit cast fromvarchartoregconfig. The manual:Bold emphasis mine.
The registered casts for
regconfig:Explanation for
castcontext:Read more about the three different types of assignment in the chapter "CREATE CAST".