Skip to content

Instantly share code, notes, and snippets.

@glittershark
Created August 20, 2013 19:42
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glittershark/6286217 to your computer and use it in GitHub Desktop.
Save glittershark/6286217 to your computer and use it in GitHub Desktop.
Postgresql aggregate function for tsvectors
CREATE OR REPLACE FUNCTION concat_tsvectors(tsv1 tsvector, tsv2 tsvector)
RETURNS tsvector AS $$
BEGIN
RETURN coalesce(tsv1, to_tsvector('default', ''))
|| coalesce(tsv2, to_tsvector('default', ''));
END;
$$ LANGUAGE plpgsql;
CREATE AGGREGATE tsvector_agg (
BASETYPE = tsvector,
SFUNC = concat_tsvectors,
STYPE = tsvector,
INITCOND = ''
);
@Jl14Salvador
Copy link

Jl14Salvador commented Mar 1, 2019

@EvanCarroll Although I see that tsvector_concat is available by using the command \df+ tsvector_concat, I can't find any single point of documentation that shows that this function exists in the Postgres Docs. Regardless, thanks for the info!

@molomby
Copy link

molomby commented Oct 1, 2019

A version using the built in tsvector_concat function as suggested by @EvanCarroll:

create aggregate tsvector_agg (tsvector) (
	STYPE = pg_catalog.tsvector,
	SFUNC = pg_catalog.tsvector_concat,
	INITCOND = ''
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment