1 <!-- doc/src/sgml/hstore.sgml -->
6 <indexterm zone="hstore">
7 <primary>hstore</primary>
11 This module implements the <type>hstore</> data type for storing sets of
12 key/value pairs within a single <productname>PostgreSQL</> value.
13 This can be useful in various scenarios, such as rows with many attributes
14 that are rarely examined, or semi-structured data. Keys and values are
19 <title><type>hstore</> External Representation</title>
23 The text representation of an <type>hstore</>, used for input and output,
24 includes zero or more <replaceable>key</> <literal>=></>
25 <replaceable>value</> pairs separated by commas. Some examples:
29 foo => bar, baz => whatever
30 "1-a" => "anything at all"
33 The order of the pairs is not significant (and may not be reproduced on
34 output). Whitespace between pairs or around the <literal>=></> sign is
35 ignored. Double-quote keys and values that include whitespace, commas,
36 <literal>=</>s or <literal>></>s. To include a double quote or a
37 backslash in a key or value, escape it with a backslash.
41 Each key in an <type>hstore</> is unique. If you declare an <type>hstore</>
42 with duplicate keys, only one will be stored in the <type>hstore</> and
43 there is no guarantee as to which will be kept:
46 SELECT 'a=>1,a=>2'::hstore;
54 A value (but not a key) can be an SQL <literal>NULL</>. For example:
60 The <literal>NULL</> keyword is case-insensitive. Double-quote the
61 <literal>NULL</> to treat it as the ordinary string <quote>NULL</quote>.
66 Keep in mind that the <type>hstore</> text format, when used for input,
67 applies <emphasis>before</> any required quoting or escaping. If you are
68 passing an <type>hstore</> literal via a parameter, then no additional
69 processing is needed. But if you're passing it as a quoted literal
70 constant, then any single-quote characters and (depending on the setting of
71 the <varname>standard_conforming_strings</> configuration parameter)
72 backslash characters need to be escaped correctly. See
73 <xref linkend="sql-syntax-strings"> for more on the handling of string
79 On output, double quotes always surround keys and values, even when it's
80 not strictly necessary.
86 <title><type>hstore</> Operators and Functions</title>
88 <table id="hstore-op-table">
89 <title><type>hstore</> Operators</title>
94 <entry>Operator</entry>
95 <entry>Description</entry>
96 <entry>Example</entry>
103 <entry><type>hstore</> <literal>-></> <type>text</></entry>
104 <entry>get value for key (<literal>NULL</> if not present)</entry>
105 <entry><literal>'a=>x, b=>y'::hstore -> 'a'</literal></entry>
106 <entry><literal>x</literal></entry>
110 <entry><type>hstore</> <literal>-></> <type>text[]</></entry>
111 <entry>get values for keys (<literal>NULL</> if not present)</entry>
112 <entry><literal>'a=>x, b=>y, c=>z'::hstore -> ARRAY['c','a']</literal></entry>
113 <entry><literal>{"z","x"}</literal></entry>
117 <entry><type>text</> <literal>=></> <type>text</></entry>
118 <entry>make single-pair <type>hstore</></entry>
119 <entry><literal>'a' => 'b'</literal></entry>
120 <entry><literal>"a"=>"b"</literal></entry>
124 <entry><type>hstore</> <literal>||</> <type>hstore</></entry>
125 <entry>concatenate <type>hstore</>s</entry>
126 <entry><literal>'a=>b, c=>d'::hstore || 'c=>x, d=>q'::hstore</literal></entry>
127 <entry><literal>"a"=>"b", "c"=>"x", "d"=>"q"</literal></entry>
131 <entry><type>hstore</> <literal>?</> <type>text</></entry>
132 <entry>does <type>hstore</> contain key?</entry>
133 <entry><literal>'a=>1'::hstore ? 'a'</literal></entry>
134 <entry><literal>t</literal></entry>
138 <entry><type>hstore</> <literal>?&</> <type>text[]</></entry>
139 <entry>does <type>hstore</> contain all specified keys?</entry>
140 <entry><literal>'a=>1,b=>2'::hstore ?& ARRAY['a','b']</literal></entry>
141 <entry><literal>t</literal></entry>
145 <entry><type>hstore</> <literal>?|</> <type>text[]</></entry>
146 <entry>does <type>hstore</> contain any of the specified keys?</entry>
147 <entry><literal>'a=>1,b=>2'::hstore ?| ARRAY['b','c']</literal></entry>
148 <entry><literal>t</literal></entry>
152 <entry><type>hstore</> <literal>@></> <type>hstore</></entry>
153 <entry>does left operand contain right?</entry>
154 <entry><literal>'a=>b, b=>1, c=>NULL'::hstore @> 'b=>1'</literal></entry>
155 <entry><literal>t</literal></entry>
159 <entry><type>hstore</> <literal><@</> <type>hstore</></entry>
160 <entry>is left operand contained in right?</entry>
161 <entry><literal>'a=>c'::hstore <@ 'a=>b, b=>1, c=>NULL'</literal></entry>
162 <entry><literal>f</literal></entry>
166 <entry><type>hstore</> <literal>-</> <type>text</></entry>
167 <entry>delete key from left operand</entry>
168 <entry><literal>'a=>1, b=>2, c=>3'::hstore - 'b'::text</literal></entry>
169 <entry><literal>"a"=>"1", "c"=>"3"</literal></entry>
173 <entry><type>hstore</> <literal>-</> <type>text[]</></entry>
174 <entry>delete keys from left operand</entry>
175 <entry><literal>'a=>1, b=>2, c=>3'::hstore - ARRAY['a','b']</literal></entry>
176 <entry><literal>"c"=>"3"</literal></entry>
180 <entry><type>hstore</> <literal>-</> <type>hstore</></entry>
181 <entry>delete matching pairs from left operand</entry>
182 <entry><literal>'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore</literal></entry>
183 <entry><literal>"a"=>"1", "c"=>"3"</literal></entry>
187 <entry><type>record</> <literal>#=</> <type>hstore</></entry>
188 <entry>replace fields in <type>record</> with matching values from <type>hstore</></entry>
189 <entry>see Examples section</entry>
194 <entry><literal>%%</> <type>hstore</></entry>
195 <entry>convert <type>hstore</> to array of alternating keys and values</entry>
196 <entry><literal>%% 'a=>foo, b=>bar'::hstore</literal></entry>
197 <entry><literal>{a,foo,b,bar}</literal></entry>
201 <entry><literal>%#</> <type>hstore</></entry>
202 <entry>convert <type>hstore</> to two-dimensional key/value array</entry>
203 <entry><literal>%# 'a=>foo, b=>bar'::hstore</literal></entry>
204 <entry><literal>{{a,foo},{b,bar}}</literal></entry>
213 Prior to PostgreSQL 8.2, the containment operators <literal>@></>
214 and <literal><@</> were called <literal>@</> and <literal>~</>,
215 respectively. These names are still available, but are deprecated and will
216 eventually be removed. Notice that the old names are reversed from the
217 convention formerly followed by the core geometric data types!
223 The <literal>=></> operator is deprecated and may be removed in a
224 future release. Use the <literal>hstore(text, text)</literal> function
229 <table id="hstore-func-table">
230 <title><type>hstore</> Functions</title>
235 <entry>Function</entry>
236 <entry>Return Type</entry>
237 <entry>Description</entry>
238 <entry>Example</entry>
239 <entry>Result</entry>
245 <entry><function>hstore(record)</function></entry>
246 <entry><type>hstore</type></entry>
247 <entry>construct an <type>hstore</> from a record or row</entry>
248 <entry><literal>hstore(ROW(1,2))</literal></entry>
249 <entry><literal>f1=>1,f2=>2</literal></entry>
253 <entry><function>hstore(text[])</function></entry>
254 <entry><type>hstore</type></entry>
255 <entry>construct an <type>hstore</> from an array, which may be either
256 a key/value array, or a two-dimensional array</entry>
257 <entry><literal>hstore(ARRAY['a','1','b','2']) || hstore(ARRAY[['c','3'],['d','4']])</literal></entry>
258 <entry><literal>a=>1, b=>2, c=>3, d=>4</literal></entry>
262 <entry><function>hstore(text[], text[])</function></entry>
263 <entry><type>hstore</type></entry>
264 <entry>construct an <type>hstore</> from separate key and value arrays</entry>
265 <entry><literal>hstore(ARRAY['a','b'], ARRAY['1','2'])</literal></entry>
266 <entry><literal>"a"=>"1","b"=>"2"</literal></entry>
270 <entry><function>hstore(text, text)</function></entry>
271 <entry><type>hstore</type></entry>
272 <entry>make single-item <type>hstore</></entry>
273 <entry><literal>hstore('a', 'b')</literal></entry>
274 <entry><literal>"a"=>"b"</literal></entry>
278 <entry><function>akeys(hstore)</function></entry>
279 <entry><type>text[]</type></entry>
280 <entry>get <type>hstore</>'s keys as an array</entry>
281 <entry><literal>akeys('a=>1,b=>2')</literal></entry>
282 <entry><literal>{a,b}</literal></entry>
286 <entry><function>skeys(hstore)</function></entry>
287 <entry><type>setof text</type></entry>
288 <entry>get <type>hstore</>'s keys as a set</entry>
289 <entry><literal>skeys('a=>1,b=>2')</literal></entry>
294 </programlisting></entry>
298 <entry><function>avals(hstore)</function></entry>
299 <entry><type>text[]</type></entry>
300 <entry>get <type>hstore</>'s values as an array</entry>
301 <entry><literal>avals('a=>1,b=>2')</literal></entry>
302 <entry><literal>{1,2}</literal></entry>
306 <entry><function>svals(hstore)</function></entry>
307 <entry><type>setof text</type></entry>
308 <entry>get <type>hstore</>'s values as a set</entry>
309 <entry><literal>svals('a=>1,b=>2')</literal></entry>
314 </programlisting></entry>
318 <entry><function>hstore_to_array(hstore)</function></entry>
319 <entry><type>text[]</type></entry>
320 <entry>get <type>hstore</>'s keys and values as an array of alternating
321 keys and values</entry>
322 <entry><literal>hstore_to_array('a=>1,b=>2')</literal></entry>
323 <entry><literal>{a,1,b,2}</literal></entry>
327 <entry><function>hstore_to_matrix(hstore)</function></entry>
328 <entry><type>text[]</type></entry>
329 <entry>get <type>hstore</>'s keys and values as a two-dimensional array</entry>
330 <entry><literal>hstore_to_matrix('a=>1,b=>2')</literal></entry>
331 <entry><literal>{{a,1},{b,2}}</literal></entry>
335 <entry><function>slice(hstore, text[])</function></entry>
336 <entry><type>hstore</type></entry>
337 <entry>extract a subset of an <type>hstore</></entry>
338 <entry><literal>slice('a=>1,b=>2,c=>3'::hstore, ARRAY['b','c','x'])</literal></entry>
339 <entry><literal>"b"=>"2", "c"=>"3"</literal></entry>
343 <entry><function>each(hstore)</function></entry>
344 <entry><type>setof(key text, value text)</type></entry>
345 <entry>get <type>hstore</>'s keys and values as a set</entry>
346 <entry><literal>select * from each('a=>1,b=>2')</literal></entry>
353 </programlisting></entry>
357 <entry><function>exist(hstore,text)</function></entry>
358 <entry><type>boolean</type></entry>
359 <entry>does <type>hstore</> contain key?</entry>
360 <entry><literal>exist('a=>1','a')</literal></entry>
361 <entry><literal>t</literal></entry>
365 <entry><function>defined(hstore,text)</function></entry>
366 <entry><type>boolean</type></entry>
367 <entry>does <type>hstore</> contain non-<literal>NULL</> value for key?</entry>
368 <entry><literal>defined('a=>NULL','a')</literal></entry>
369 <entry><literal>f</literal></entry>
373 <entry><function>delete(hstore,text)</function></entry>
374 <entry><type>hstore</type></entry>
375 <entry>delete pair with matching key</entry>
376 <entry><literal>delete('a=>1,b=>2','b')</literal></entry>
377 <entry><literal>"a"=>"1"</literal></entry>
381 <entry><function>delete(hstore,text[])</function></entry>
382 <entry><type>hstore</type></entry>
383 <entry>delete pairs with matching keys</entry>
384 <entry><literal>delete('a=>1,b=>2,c=>3',ARRAY['a','b'])</literal></entry>
385 <entry><literal>"c"=>"3"</literal></entry>
389 <entry><function>delete(hstore,hstore)</function></entry>
390 <entry><type>hstore</type></entry>
391 <entry>delete pairs matching those in the second argument</entry>
392 <entry><literal>delete('a=>1,b=>2','a=>4,b=>2'::hstore)</literal></entry>
393 <entry><literal>"a"=>"1"</literal></entry>
397 <entry><function>populate_record(record,hstore)</function></entry>
398 <entry><type>record</type></entry>
399 <entry>replace fields in <type>record</> with matching values from <type>hstore</></entry>
400 <entry>see Examples section</entry>
410 The function <function>populate_record</function> is actually declared
411 with <type>anyelement</>, not <type>record</>, as its first argument,
412 but it will reject non-record types with a run-time error.
418 <title>Indexes</title>
421 <type>hstore</> has GiST and GIN index support for the <literal>@></>,
422 <literal>?</>, <literal>?&</> and <literal>?|</> operators. For example:
425 CREATE INDEX hidx ON testhstore USING GIST (h);
427 CREATE INDEX hidx ON testhstore USING GIN (h);
431 <type>hstore</> also supports <type>btree</> or <type>hash</> indexes for
432 the <literal>=</> operator. This allows <type>hstore</> columns to be
433 declared <literal>UNIQUE</>, or to be used in <literal>GROUP BY</>,
434 <literal>ORDER BY</> or <literal>DISTINCT</> expressions. The sort ordering
435 for <type>hstore</> values is not particularly useful, but these indexes
436 may be useful for equivalence lookups. Create indexes for <literal>=</>
437 comparisons as follows:
440 CREATE INDEX hidx ON testhstore USING BTREE (h);
442 CREATE INDEX hidx ON testhstore USING HASH (h);
447 <title>Examples</title>
450 Add a key, or update an existing key with a new value:
452 UPDATE tab SET h = h || ('c' => '3');
459 UPDATE tab SET h = delete(h, 'k1');
464 Convert a <type>record</> to an <type>hstore</>:
466 CREATE TABLE test (col1 integer, col2 text, col3 text);
467 INSERT INTO test VALUES (123, 'foo', 'bar');
469 SELECT hstore(t) FROM test AS t;
471 ---------------------------------------------
472 "col1"=>"123", "col2"=>"foo", "col3"=>"bar"
478 Convert an <type>hstore</> to a predefined <type>record</> type:
480 CREATE TABLE test (col1 integer, col2 text, col3 text);
482 SELECT * FROM populate_record(null::test,
483 '"col1"=>"456", "col2"=>"zzz"');
492 Modify an existing record using the values from an <type>hstore</>:
494 CREATE TABLE test (col1 integer, col2 text, col3 text);
495 INSERT INTO test VALUES (123, 'foo', 'bar');
497 SELECT (r).* FROM (SELECT t #= '"col3"=>"baz"' AS r FROM test t) s;
507 <title>Statistics</title>
510 The <type>hstore</> type, because of its intrinsic liberality, could
511 contain a lot of different keys. Checking for valid keys is the task of the
512 application. The following examples demonstrate several techniques for
513 checking keys and obtaining statistics.
519 SELECT * FROM each('aaa=>bq, b=>NULL, ""=>1');
526 SELECT (each(h)).key, (each(h)).value INTO stat FROM testhstore;
533 SELECT key, count(*) FROM
534 (SELECT (each(h)).key FROM testhstore) AS stat
536 ORDER BY count DESC, key;
554 <title>Compatibility</title>
557 <emphasis>When upgrading from older versions, always load the new
558 version of this module into the database before restoring a dump.
559 Otherwise, many new features will be unavailable.</emphasis>
563 As of PostgreSQL 9.0, <type>hstore</> uses a different internal
564 representation than previous versions. This presents no obstacle for
565 dump/restore upgrades since the text representation (used in the dump) is
570 In the event of a binary upgrade, upward compatibility is maintained by
571 having the new code recognize old-format data. This will entail a slight
572 performance penalty when processing data that has not yet been modified by
573 the new code. It is possible to force an upgrade of all values in a table
574 column by doing an <literal>UPDATE</> statement as follows:
576 UPDATE tablename SET hstorecol = hstorecol || '';
581 Another way to do it is:
583 ALTER TABLE tablename ALTER hstorecol TYPE hstore USING hstorecol || '';
585 The <command>ALTER TABLE</> method requires an exclusive lock on the table,
586 but does not result in bloating the table with old row versions.
592 <title>Authors</title>
595 Oleg Bartunov <email>oleg@sai.msu.su</email>, Moscow, Moscow University, Russia
599 Teodor Sigaev <email>teodor@sigaev.ru</email>, Moscow, Delta-Soft Ltd., Russia
603 Additional enhancements by Andrew Gierth <email>andrew@tao11.riddles.org.uk</email>,