Extension Postgres pour stocker maps key-value simples (string→string).
hstore est une extension PostgreSQL ajoutant un type data pour stocker des maps key-value simples (string→string). Précurseur de JSONB depuis Postgres 8.x, encore utilisé pour scenarios spécifiques où simplicité key-value flat suffit.
Activation : `CREATE EXTENSION hstore;`
Format : `'key1=>value1, key2=>value2'::hstore`
Opérateurs : `->` (get value), `?` (key exists), `?&` `?|` (all/any keys), `@>` `<@` (contains), `||` (concat), `-` (delete key), `%%` (array of key/value pairs).
Exemple :
```sql
CREATE TABLE products (
id serial,
name text,
attributes hstore
);
INSERT INTO products (name, attributes) VALUES
('Laptop', 'cpu=>i7, ram=>16GB, ssd=>512GB'),
('Phone', 'os=>Android, ram=>8GB, color=>black');
SELECT * FROM products WHERE attributes->'ram' = '16GB';
SELECT * FROM products WHERE attributes ? 'ssd';
CREATE INDEX idx_attr ON products USING GIN (attributes);
```
Vs JSONB :
- **hstore** : flat key-value only (no nesting, no arrays, no typed values — strings only), simpler.
- **JSONB** : full JSON (nested objects, arrays, numbers, booleans, null), richer.
Quand préférer hstore : (1) **legacy code** still using ; (2) **simple flat key-value** sans besoin nesting/types ; (3) **performance marginal advantages** (simpler type).
Quand préférer JSONB : (1) **any nested structure needed** ; (2) **mixed types** (numbers, booleans) ; (3) **arrays** ; (4) **new development** — JSONB recommended.
Reality 2024+ : JSONB largement dominé hstore en pratique. hstore reste en codebases anciennes. Use case unique restant : ultra-simple flat metadata où JSONB overkill. Compétences DP-300.
200+ certifications, 400 000+ questions, examens blancs chronométrés.
Voir le catalogue →