Depending on the specifics of your use case, it may be worth knowing that tsquery has syntax for querying for word prefixes. Combine this with the tsvector indexed field, and you can very quickly find the word prefixes.
Create your “huge” table:
CREATE TABLE huge_table ( field text, field_tsv tsvector );
Add an index:
CREATE INDEX field_tsv_idx ON huge_table USING gin(field_tsv);
Add a trigger to update the indexed column:
CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE ON huge_table FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger(field_tsv, 'pg_catalog.english', field);
Add some mock data
INSERT INTO huge_table (field) VALUES ('something nice'); INSERT INTO huge_table (field) VALUES ('another thing');
Then the request for prefixes with some kind of limit:
SELECT field FROM huge_table WHERE field_tsv @@ to_tsquery('anot:*') LIMIT 20; field
More on docs , especially in index types , as your index can get quite large.
source share