Skip to main content
The hstore extension provides a data type for storing sets of key-value pairs within a single PostgreSQL value. This can be particularly useful when dealing with semi-structured data or when you need to store attributes that don’t warrant their own columns. Your Nile database arrives with hstore extension already enabled.

Creating Tables with hstore

Here’s how to create a table that includes an hstore column:

Inserting Data

First, create a tenant:
Then you can insert products for this tenant:

Querying hstore Data

The hstore data type comes with several useful operators and functions:

Updating hstore Values

You can update individual key/value pairs or the entire hstore. We use SET nile.tenant_id to guarantee the operation is performed on the correct tenant:

Combining hstore Values

You can combine multiple hstore values using the concatenation operator (||):

Converting To/From JSON

hstore can be converted to and from JSON:

Best Practices

  1. Use hstore when dealing with dynamic attributes that don’t require strict schema validation.
  2. Consider using JSON/JSONB instead if you need to store nested structures or arrays.
  3. Create indexes on frequently queried keys using GiST or GIN indexes:

Performance Considerations

  • hstore is generally more efficient than JSON for simple key-value pairs
  • GIN indexes can significantly improve query performance on hstore columns
  • The storage size of hstore is typically smaller than equivalent JSON storage

Limitations

  • Keys and values must be text strings
  • No support for nested structures
  • No array support within values
  • Maximum size is limited by the maximum TOAST size in PostgreSQL
For more details, refer to the hstore documentation.