PostgreSQL Quick Tip: Indexing Large Columns
When working with large columns, you can run into some indexing issues. For instance, there’s a limit on how many characters a btree index entry can use, which poses problems for indexing text fields, since inserts and updates will fail if they use more characters than the index can use. Also, you can run into performance and disk consumption issues with large columns. The solution for this is to index columns using the hashtext() function. so, ie:
CREATE INDEX table1_column1_hash ON table1 hashtext(column1);
Then in your queries, do:
SELECT * FROM table1 WHERE hashtext(column1) = hashtext('foo');
There’s some issues with this solution, mainly being that you can only have exact matches for the column, so you can’t do range scans or LIKE queries, however, if you need searching features you should probably be using full text searching, which is available in PostgreSQL 8.2+.
