PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
The hstore
data type in PostgreSQL is an extension that provides support for simple key-value storage. It's somewhat like a simple version of JSON or a Python dictionary, allowing you to store sets of key-value pairs in a single PostgreSQL value.
The keys and values are simply text strings. One of the primary benefits of the hstore
data type is its ability to index the data and make queries on the key-value pairs efficient.
hstore
:Enabling the hstore extension:
Before you can use the hstore
data type, you have to enable the hstore
extension. You can do this with the CREATE EXTENSION
command:
CREATE EXTENSION hstore;
Creating a table with an hstore column:
CREATE TABLE my_table (id serial primary key, attributes hstore);
Inserting data into an hstore column:
INSERT INTO my_table(attributes) VALUES ('key1 => value1, key2 => value2');
Or using the explicit format:
INSERT INTO my_table(attributes) VALUES ('"key1"=>"value1", "key2"=>"value2"');
Querying data from an hstore column:
To get a value associated with a key:
SELECT attributes -> 'key1' AS my_key_value FROM my_table;
To find rows where a specific key-value pair exists:
SELECT * FROM my_table WHERE attributes @> 'key1 => value1';
To find rows where a specific key exists:
SELECT * FROM my_table WHERE attributes ? 'key1';
Updating data in an hstore column:
UPDATE my_table SET attributes = attributes || 'key3 => value3' WHERE id = 1;
Deleting a key from an hstore column:
UPDATE my_table SET attributes = delete(attributes, 'key1') WHERE id = 1;
Efficient indexing: The hstore
data type can be indexed with GIN or GiST indexes, making queries very fast.
Flexibility: The dynamic nature of the hstore
type can be advantageous for schema-less designs or when dealing with dynamic attributes.
No nested structures: Unlike JSON or JSONB, hstore
only supports a flat set of key-value pairs.
Both keys and values are text: There's no type distinction as you'd have with JSON.
Less standard: While hstore
is powerful, JSONB has gained more traction as a flexible key-value store within PostgreSQL due to its compatibility with the JSON standard and ability to store nested structures.
In many cases, developers choose hstore
for simplicity when they just need flat key-value pairs, and JSONB when they require more complex structures or plan to interoperate with systems that use JSON.
How to create and use hstore in PostgreSQL:
CREATE TABLE products ( id SERIAL PRIMARY KEY, properties HSTORE ); INSERT INTO products (properties) VALUES ('color => red, size => large');
Working with key-value pairs in PostgreSQL hstore:
SELECT properties['color'], properties['size'] FROM products WHERE id = 1;
Inserting and updating data with hstore in PostgreSQL:
-- Inserting INSERT INTO products (properties) VALUES ('weight => 2.5'); -- Updating UPDATE products SET properties = properties || 'price => 19.99' WHERE id = 2;
Querying hstore data in PostgreSQL:
SELECT * FROM products WHERE properties @> 'weight => 2.5';
Indexing hstore columns in PostgreSQL:
CREATE INDEX idx_properties ON products USING GIN(properties);
Searching for values in hstore in PostgreSQL:
SELECT * FROM products WHERE properties -> 'color' = 'red';
Aggregating data with hstore in PostgreSQL:
SELECT AVG((properties -> 'price')::NUMERIC) FROM products;
Modifying hstore values in PostgreSQL:
UPDATE products SET properties = properties || 'color => blue' WHERE id = 3;
Converting between hstore and other data types in PostgreSQL:
SELECT properties::JSON FROM products WHERE id = 4;
Managing NULL values in hstore columns in PostgreSQL:
SELECT * FROM products WHERE properties IS NULL;
Combining hstore with other data types in PostgreSQL:
SELECT properties || 'material => leather' FROM products WHERE id = 5;
Using hstore with JSON in PostgreSQL:
SELECT properties || '{"brand": "XYZ"}'::JSON FROM products WHERE id = 6;
Using hstore in WHERE and HAVING clauses in PostgreSQL:
SELECT * FROM products WHERE properties -> 'size' = 'medium' AND properties @> 'price => 30';
Updating and deleting hstore values in PostgreSQL:
-- Updating UPDATE products SET properties = properties - 'size' WHERE id = 7; -- Deleting DELETE FROM products WHERE properties -> 'color' = 'green';
Working with arrays and hstore in PostgreSQL:
SELECT properties || ARRAY['discount' => '10%']::HSTORE FROM products WHERE id = 8;
Managing duplicates in hstore columns in PostgreSQL:
SELECT properties || 'color => yellow' FROM products WHERE id = 9;