I have never played hstore, but I do something like this when I need an EAV column, for example:
create index on product_eav (eav_value) where (eav_type = 'int');
The limitation here is that you need to be explicit in your request in order to use it, i.e. this request would not use the above index:
select product_id from product_eav where eav_name = 'size' and eav_value = :size;
But this one thing:
select product_id from product_eav where eav_name = 'size' and eav_value = :size and type = 'int';
In your example, it more likely looks like:
create index on product ((data->'size')::int) where (data->'size' is not null);
This should be avoided by adding a reference to the index in the absence of input size. Depending on the version of PG you are using, you may need to change it like this:
select product_id from products where data->'size' is not null and data->'size' = :size;
Another big difference between the regular and partial index is that the latter cannot provide a unique constraint on the definition of the table. This will be successful:
create unique index foo_bar_key on foo (bar) where (cond);
Below will not be:
alter table foo add constraint foo_bar_key unique (bar) where (cond);
But it will be:
alter table foo add constraint foo_bar_excl exclude (bar with =) where (cond);
Denis de bernardy
source share