【问题标题】:Create a postgres function and call it with trigger创建一个 postgres 函数并使用触发器调用它
【发布时间】:2022-01-25 12:49:13
【问题描述】:

我正在处理 ts_vector 更新,当更新或插入行时将调用它。我的工作查询是这样的;

UPDATE backend_article SET search_vector =
                               setweight(to_tsvector(coalesce(lookup.title,'')), 'A') ||
                               setweight(to_tsvector(coalesce(lookup.keywords,'')), 'B') ||
                               setweight(to_tsvector(coalesce(lookup.abstract,'')), 'B') ||
                               setweight(array_to_tsvector(lookup.authors_list), 'B')  ||
                               setweight(array_to_tsvector(lookup.tag_label_list), 'B') ||
                               setweight(array_to_tsvector(lookup.tag_wiki_list), 'B') ||
                               setweight(array_to_tsvector(lookup.tag_description_list), 'B') 
    FROM
    (SELECT backend_article.title as title, backend_article.keywords as keywords, 
    backend_article.abstract as abstract, 
    coalesce(array_agg(distinct backend_author.name) filter (where (backend_author.name is not null) and (length(backend_author.name) > 0)),'{}') as authors_list, 
    coalesce(array_agg(distinct backend_tag.label) filter (where (backend_tag.label is not null) and (length(backend_tag.label) > 0)), '{}') as tag_label_list , 
    coalesce(array_agg(distinct backend_tag.wiki_description) filter (where (backend_tag.wiki_description is not null) and (length(backend_tag.wiki_description) > 0)), '{}') as tag_wiki_list,
    coalesce(array_agg(distinct backend_tag.custom_description) filter (where (backend_tag.custom_description is not null) and (length(backend_tag.custom_description ) > 0)), '{}') as tag_description_list 
    FROM backend_article
    LEFT OUTER JOIN backend_article_authors ON (backend_article.id = backend_article_authors.article_id)  
    LEFT OUTER JOIN backend_author ON (backend_article_authors.author_id = backend_author.id) 
    LEFT OUTER JOIN backend_article_tags ON (backend_article.id = backend_article_tags.article_id) 
    LEFT OUTER JOIN backend_tag ON (backend_article_tags.tag_id = backend_tag.id) 
    GROUP BY backend_article.id
    ORDER BY backend_article.id ASC) as lookup 
    where backend_article.title = lookup.title 

我尝试用这种方式创建一个函数;

CREATE FUNCTION search_vector_update() RETURNS trigger AS $$
begin
    UPDATE backend_article SET search_vector =
                               setweight(to_tsvector(coalesce(lookup.title,'')), 'A') ||
                               setweight(to_tsvector(coalesce(lookup.keywords,'')), 'B') ||
                               setweight(to_tsvector(coalesce(lookup.abstract,'')), 'B') ||
                               setweight(array_to_tsvector(lookup.authors_list), 'B')  ||
                               setweight(array_to_tsvector(lookup.tag_label_list), 'B') ||
                               setweight(array_to_tsvector(lookup.tag_wiki_list), 'B') ||
                               setweight(array_to_tsvector(lookup.tag_description_list), 'B') 
    FROM
    (SELECT backend_article.title as title, backend_article.keywords as keywords, 
    backend_article.abstract as abstract, 
    coalesce(array_agg(distinct backend_author.name) filter (where (backend_author.name is not null) and (length(backend_author.name) > 0)),'{}') as authors_list, 
    coalesce(array_agg(distinct backend_tag.label) filter (where (backend_tag.label is not null) and (length(backend_tag.label) > 0)), '{}') as tag_label_list , 
    coalesce(array_agg(distinct backend_tag.wiki_description) filter (where (backend_tag.wiki_description is not null) and (length(backend_tag.wiki_description) > 0)), '{}') as tag_wiki_list,
    coalesce(array_agg(distinct backend_tag.custom_description) filter (where (backend_tag.custom_description is not null) and (length(backend_tag.custom_description ) > 0)), '{}') as tag_description_list 
    FROM backend_article
    LEFT OUTER JOIN backend_article_authors ON (backend_article.id = backend_article_authors.article_id)  
    LEFT OUTER JOIN backend_author ON (backend_article_authors.author_id = backend_author.id) 
    LEFT OUTER JOIN backend_article_tags ON (backend_article.id = backend_article_tags.article_id) 
    LEFT OUTER JOIN backend_tag ON (backend_article_tags.tag_id = backend_tag.id) 
    GROUP BY backend_article.id
    ORDER BY backend_article.id ASC) as lookup 
    where backend_article.title = lookup.title 
end;
$$ LANGUAGE plpgsql;

我收到错误“错误:输入结束时函数定义意外结束 第 27 行:$$ LANGUAGE plpgsql;"。我是 postgresql 函数和触发器的新手。我愿意接受任何建议和解决方案。

【问题讨论】:

    标签: postgresql triggers plpgsql


    【解决方案1】:

    您在此之后缺少一个分号:

    where backend_article.title = lookup.title
    

    添加它,这应该可以工作。

    【讨论】:

      猜你喜欢
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多