创建一个 postgres 函数并使用触发器调用它
Create a postgres function and call it with trigger
我正在处理 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 函数和触发器的新手。我愿意接受任何建议和解决方案。
您在这之后少了一个分号:
where backend_article.title = lookup.title
添加它,这应该可以工作。
我正在处理 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 函数和触发器的新手。我愿意接受任何建议和解决方案。
您在这之后少了一个分号:
where backend_article.title = lookup.title
添加它,这应该可以工作。