{"id":477,"date":"2022-08-30T15:02:00","date_gmt":"2022-08-30T15:02:00","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/postgresql-error-function-to_tsvectorcharacter-varying-unknown-does-not-exist-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:02:00","modified_gmt":"2022-08-30T15:02:00","slug":"postgresql-error-function-to_tsvectorcharacter-varying-unknown-does-not-exist-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/postgresql-error-function-to_tsvectorcharacter-varying-unknown-does-not-exist-collection-of-common-programming-errors\/","title":{"rendered":"PostgreSQL ERROR: function to_tsvector(character varying, unknown) does not exist-Collection of common programming errors"},"content":{"rendered":"<p>The function signature cannot be guessed with the explicit type <code>varchar<\/code>. Realize that an <strong>untyped string literal<\/strong> is not the same thing as parameter of type <code>varchar<\/code>.<\/p>\n<p>Use instead:<\/p>\n<pre><code>SELECT language, to_tsvector(language::regconfig, 'hello world') FROM languages;\n<\/code><\/pre>\n<h3>Why?<\/h3>\n<p>The first argument of the 2-argument form is of type <code>regconfig<\/code>, not <code>text<\/code> or <code>varchar<\/code>:<\/p>\n<pre><code>SELECT proname, pg_get_function_arguments(oid)\nFROM   pg_catalog.pg_proc\nWHERE  proname = 'to_tsvector'\n\n   proname   | pg_get_function_arguments\n-------------+---------------------------\n to_tsvector | text\n to_tsvector | regconfig, text\n<\/code><\/pre>\n<p>As long as you don&#8217;t use an explicit type, Postgres will consider all types a string literal can be cast to for a matching function signature.<\/p>\n<p>However, if you use an <em>explicit type<\/em> (like you do in your second example: <code>varchar<\/code>) Postgres cannot find an implicit conversion from <code>varchar<\/code> to <code>regconfig<\/code> and does not consider the function you are looking for. You are telling Postgres: &#8220;Look no further, I want the function with a <code>varchar<\/code> argument (or one that is only an implicit cast away)!&#8221;<\/p>\n<p>I quote from the chapter <strong>Type Conversion &#8211; Functions<\/strong> in the manual:<\/p>\n<blockquote>\n<p>unknown literals are assumed to be convertible to anything for this purpose.<\/p>\n<\/blockquote>\n<p>The registered casts for <code>varchar<\/code>, ordered by proximity:<\/p>\n<pre><code>SELECT castsource::regtype, casttarget::regtype, castcontext\nFROM   pg_catalog.pg_cast\nWHERE  castsource = 'varchar'::regtype\nORDER BY castsource, castcontext = 'i' DESC, castcontext\n\n    castsource     |    casttarget     | castcontext\n-------------------+-------------------+-------------\n character varying | regclass          | i\n character varying | name              | i\n character varying | text              | i\n character varying | character         | i\n character varying | character varying | i\n character varying | \"char\"            | a\n character varying | xml               | e\n<\/code><\/pre>\n<p>Postgres would find a function where the signature can be reached with an <em>implicit cast<\/em>.<br \/>\nExplanation for <code>castcontext<\/code>:<\/p>\n<blockquote>\n<p>castcontext char<br \/>\nIndicates what contexts the cast can be invoked in. e means only as an explicit cast (using CAST or :: syntax). a means implicitly in assignment to a target column, as well as explicitly. i means implicitly in expressions, as well as the other cases.<\/p>\n<\/blockquote>\n<p>Read more about the three different <em>types of assignment<\/em> in the chapter CREATE CAST.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:44:39. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>The function signature cannot be guessed with the explicit type varchar. Realize that an untyped string literal is not the same thing as parameter of type varchar. Use instead: SELECT language, to_tsvector(language::regconfig, &#8216;hello world&#8217;) FROM languages; Why? The first argument of the 2-argument form is of type regconfig, not text or varchar: SELECT proname, pg_get_function_arguments(oid) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=477"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/477\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}