Question

Here's my schema

          Column          |            Type            
--------------------------+----------------------------
 id                       | integer                    
 title                    | character varying(255)     
 summary                  | character varying(255)     
 readable_content         | text                       
 created_at               | timestamp without time zone
 updated_at               | timestamp without time zone
 textsearchable_index_col | tsvector                   

Indexes:
    "site_articles_pkey" PRIMARY KEY, btree (id)
    "index_site_articles_on_textsearchable_index_col" gin (textsearchable_index_col)
Triggers:
    site_articles_before_insert_update_row_tr BEFORE INSERT OR UPDATE ON site_articles FOR EACH ROW EXECUTE PROCEDURE site_articles_before_insert_update_row_tr()

and here's the trigger function:

CREATE FUNCTION site_articles_before_insert_update_row_tr() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
    new.tsv := tsvector_update_trigger(textsearchable_index_col, 'pg_catalog.simple', title, summary, readable_content);
    new.tsv := setweight(to_tsvector('pg_catalog.simple', coalesce(new.title,'')), 'A') ||
                setweight(to_tsvector('pg_catalog.simple', coalesce(new.summary,'')), 'B') ||
                setweight(to_tsvector('pg_catalog.simple', coalesce(new.readable_content,'')), 'C');
    RETURN NEW;
END;
$$;

However, when I update a record like this:

UPDATE "site_articles" SET "updated_at" = '2013-12-13 05:43:59.802580' WHERE "site_articles"."id" = 1

I get

ERROR:  column "textsearchable_index_col" does not exist
LINE 1: SELECT tsvector_update_trigger(textsearchable_index_col, 'pg...
                                       ^
QUERY:  SELECT tsvector_update_trigger(textsearchable_index_col, 'pg_catalog.simple', title, summary, readable_content)

I'm pretty sure the column name is correct. Not sure if it matters, I concatenate the rows like this after I added the tsvector column (I'm using Rails migration)

def up
  add_column :site_articles, :textsearchable_index_col, :tsvector

  sql = <<-SQL
    UPDATE site_articles SET textsearchable_index_col =
                   to_tsvector('simple', coalesce("site_articles"."title"::text,'')
                               || ' ' || coalesce("site_articles"."summary"::text, '')
                               || ' ' || coalesce("site_articles"."readable_content"::text, '')
                              );
  SQL
  execute sql

  add_index :site_articles, :textsearchable_index_col, using: 'gin'
end

Did I miss something or each column should have its own tsvector column (not concatenated in one)?

Was it helpful?

Solution

Indeed. It should be new.textsearchable_index_col. The same for subsequent fields.

(Btw, there's little point in assigning tsv twice. Either use your first (corrected) statement, or your second. But not both, since the second overrides the first, and a tsv is expensive to compute.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top