I have the following constructor (as a test):
CREATE TABLE product (id BIGSERIAL PRIMARY KEY, ext hstore);
CREATE INDEX ix_product_ext ON product USING GIN(ext);
INSERT
INTO product (id, ext)
SELECT id, ('size=>' || CEILING(10 + RANDOM() * 90) || ',mass=>' || CEILING(10 + RANDOM() * 90))::hstore
FROM generate_series(1, 100000) id;
I have the following query, which works ok:
SELECT COUNT(id)
FROM (
SELECT id
FROM product
WHERE (ext->'size')::INT >= 41
AND (ext->'mass')::INT <= 20
) T
But I believe the correct way to do this is using the @> operator. I have the following, but it gives a syntax error:
SELECT COUNT(id)
FROM (
SELECT id
FROM product
WHERE ext @> 'size>=41,mass<=20'
) T
How should I write this?
Reading hstore documentation your (last query) size>=41
does not mean "when size is greater or equal than 41":
text => text make single-pair hstore
Following that you can't write mass<=20
, because there is no such operation. Using @>
operator:
hstore @> hstore does left operand contain right?
you can write:
SELECT count(id)
FROM product
WHERE ext @> 'size=>41,mass=>20';
However it takes only these products where size is equal to 41 and mass is equal to 20.