0

I have a table

create table device_entity
(
 id uuid not null primary key,
 name varchar(255),
 metadata hstore
);

This table has entries with 'country' = 'uk' in the metadata field.

This query returns all entries where the key 'country' is present in the metadata field.

SELECT * from device_entity where metadata ? 'country'

How can I select all entries where the key 'country' = 'uk'?

asked Sep 17, 2022 at 9:20
1
  • Use the -> operator - but nowadays I would prefer a jsonb column over hstore Commented Sep 17, 2022 at 9:39

1 Answer 1

0
SELECT * from device_entity where (metadata -> 'country') = 'uk';

This query will probably have quite slow performance which can probably be improved significantly by deploying the right inverted index.

answered Sep 17, 2022 at 10:31
3
  • No need for an "inverted index". A B-Tree index on the expression should work just fine Commented Sep 17, 2022 at 11:19
  • Of course, a dedicated column "country" in the table would be best for ease of use, index, and performance. Commented Sep 17, 2022 at 11:45
  • b-tree would work if this is the only interesting key Commented Sep 17, 2022 at 11:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.