I have a need to store data using the HSTORE type and index by key.
CREATE INDEX ix_product_size ON product(((data->'Size')::INT))
CREATE INDEX ix_product_color ON product(((data->'Color')))
etc.
What are the practical limitations of using expression indexes? In my case, there could be several hundred different types of data, hence several hundred expression indexes. Every insert, update, and select query will have to process against these indexes in order to pick the correct one.
I’ve never played with hstore, but I do something similar when I need an EAV column, e.g.:
The limitation in doing so is that you need to be explicit in your query to make use of it, i.e. this query would not make use of the above index:
But this one would:
In your example it should likely be more like:
This should avoid adding a reference to the index when there is no size entry. Depending on the PG version you’re using the query may need to be modified like so:
Another big difference between regular and partial index is that the latter cannot enforce a unique constraint in a table definition. This will succeed:
The following won’t:
But this will: