1 <!-- $PostgreSQL: pgsql/doc/src/sgml/hstore.sgml,v 1.5 2009/11/30 17:56:09 momjian Exp $ -->
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 "NULL".
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>text[]</> <literal>=></> <type>text[]</></entry>
125 <entry>construct an <type>hstore</> from separate key and value arrays</entry>
126 <entry><literal>ARRAY['a','b'] => ARRAY['1','2']</literal></entry>
127 <entry><literal>"a"=>"1","b"=>"2"</literal></entry>
131 <entry><type>hstore</> <literal>=></> <type>text[]</></entry>
132 <entry>extract a subset of an <type>hstore</></entry>
133 <entry><literal>'a=>1,b=>2,c=>3'::hstore => ARRAY['b','c','x']</literal></entry>
134 <entry><literal>"b"=>"2", "c"=>"3"</literal></entry>
138 <entry><type>hstore</> <literal>||</> <type>hstore</></entry>
139 <entry>concatenate <type>hstore</>s</entry>
140 <entry><literal>'a=>b, c=>d'::hstore || 'c=>x, d=>q'::hstore</literal></entry>
141 <entry><literal>"a"=>"b", "c"=>"x", "d"=>"q"</literal></entry>
145 <entry><type>hstore</> <literal>?</> <type>text</></entry>
146 <entry>does <type>hstore</> contain key?</entry>
147 <entry><literal>'a=>1'::hstore ? 'a'</literal></entry>
148 <entry><literal>t</literal></entry>
152 <entry><type>hstore</> <literal>?&</> <type>text[]</></entry>
153 <entry>does <type>hstore</> contain all specified keys?</entry>
154 <entry><literal>'a=>1,b=>2'::hstore ?& ARRAY['a','b']</literal></entry>
155 <entry><literal>t</literal></entry>
159 <entry><type>hstore</> <literal>?|</> <type>text[]</></entry>
160 <entry>does <type>hstore</> contain any of the specified keys?</entry>
161 <entry><literal>'a=>1,b=>2'::hstore ?| ARRAY['b','c']</literal></entry>
162 <entry><literal>t</literal></entry>
166 <entry><type>hstore</> <literal>@></> <type>hstore</></entry>
167 <entry>does left operand contain right?</entry>
168 <entry><literal>'a=>b, b=>1, c=>NULL'::hstore @> 'b=>1'</literal></entry>
169 <entry><literal>t</literal></entry>
173 <entry><type>hstore</> <literal><@</> <type>hstore</></entry>
174 <entry>is left operand contained in right?</entry>
175 <entry><literal>'a=>c'::hstore <@ 'a=>b, b=>1, c=>NULL'</literal></entry>
176 <entry><literal>f</literal></entry>
180 <entry><type>hstore</> <literal>-</> <type>text</></entry>
181 <entry>delete key from left operand</entry>
182 <entry><literal>'a=>1, b=>2, c=>3'::hstore - 'b'::text</literal></entry>
183 <entry><literal>"a"=>"1", "c"=>"3"</literal></entry>
187 <entry><type>hstore</> <literal>-</> <type>text[]</></entry>
188 <entry>delete keys from left operand</entry>
189 <entry><literal>'a=>1, b=>2, c=>3'::hstore - ARRAY['a','b']</literal></entry>
190 <entry><literal>"c"=>"3"</literal></entry>
194 <entry><type>hstore</> <literal>-</> <type>hstore</></entry>
195 <entry>delete matching pairs from left operand</entry>
196 <entry><literal>'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore</literal></entry>
197 <entry><literal>"a"=>"1", "c"=>"3"</literal></entry>
201 <entry><type>record</> <literal>#=</> <type>hstore</></entry>
202 <entry>replace fields in <type>record</> with matching values from <type>hstore</></entry>
203 <entry>see Examples section</entry>
208 <entry><literal>%%</> <type>hstore</></entry>
209 <entry>convert <type>hstore</> to array of alternating keys and values</entry>
210 <entry><literal>%% 'a=>foo, b=>bar'::hstore</literal></entry>
211 <entry><literal>{a,foo,b,bar}</literal></entry>
215 <entry><literal>%#</> <type>hstore</></entry>
216 <entry>convert <type>hstore</> to two-dimensional key/value array</entry>
217 <entry><literal>%# 'a=>foo, b=>bar'::hstore</literal></entry>
218 <entry><literal>{{a,foo},{b,bar}}</literal></entry>
227 Prior to PostgreSQL 8.2, the containment operators <literal>@></>
228 and <literal><@</> were called <literal>@</> and <literal>~</>,
229 respectively. These names are still available, but are deprecated and will
230 eventually be removed. Notice that the old names are reversed from the
231 convention formerly followed by the core geometric datatypes!
235 <table id="hstore-func-table">
236 <title><type>hstore</> Functions</title>
241 <entry>Function</entry>
242 <entry>Return Type</entry>
243 <entry>Description</entry>
244 <entry>Example</entry>
245 <entry>Result</entry>
251 <entry><function>hstore(record)</function></entry>
252 <entry><type>hstore</type></entry>
253 <entry>construct an <type>hstore</> from a record or row</entry>
254 <entry><literal>hstore(ROW(1,2))</literal></entry>
255 <entry><literal>f1=>1,f2=>2</literal></entry>
259 <entry><function>hstore(text[])</function></entry>
260 <entry><type>hstore</type></entry>
261 <entry>construct an <type>hstore</> from an array, which may be either
262 a key/value array, or a two-dimensional array</entry>
263 <entry><literal>hstore(ARRAY['a','1','b','2']) || hstore(ARRAY[['c','3'],['d','4']])</literal></entry>
264 <entry><literal>a=>1, b=>2, c=>3, d=>4</literal></entry>
268 <entry><function>akeys(hstore)</function></entry>
269 <entry><type>text[]</type></entry>
270 <entry>get <type>hstore</>'s keys as an array</entry>
271 <entry><literal>akeys('a=>1,b=>2')</literal></entry>
272 <entry><literal>{a,b}</literal></entry>
276 <entry><function>skeys(hstore)</function></entry>
277 <entry><type>setof text</type></entry>
278 <entry>get <type>hstore</>'s keys as a set</entry>
279 <entry><literal>skeys('a=>1,b=>2')</literal></entry>
284 </programlisting></entry>
288 <entry><function>avals(hstore)</function></entry>
289 <entry><type>text[]</type></entry>
290 <entry>get <type>hstore</>'s values as an array</entry>
291 <entry><literal>avals('a=>1,b=>2')</literal></entry>
292 <entry><literal>{1,2}</literal></entry>
296 <entry><function>svals(hstore)</function></entry>
297 <entry><type>setof text</type></entry>
298 <entry>get <type>hstore</>'s values as a set</entry>
299 <entry><literal>svals('a=>1,b=>2')</literal></entry>
304 </programlisting></entry>
308 <entry><function>hstore_to_array(hstore)</function></entry>
309 <entry><type>text[]</type></entry>
310 <entry>get <type>hstore</>'s keys and values as an array of alternating
311 keys and values</entry>
312 <entry><literal>hstore_to_array('a=>1,b=>2')</literal></entry>
313 <entry><literal>{a,1,b,2}</literal></entry>
317 <entry><function>hstore_to_matrix(hstore)</function></entry>
318 <entry><type>text[]</type></entry>
319 <entry>get <type>hstore</>'s keys and values as a two-dimensional array</entry>
320 <entry><literal>hstore_to_matrix('a=>1,b=>2')</literal></entry>
321 <entry><literal>{{a,1},{b,2}}</literal></entry>
325 <entry><function>each(hstore)</function></entry>
326 <entry><type>setof(key text, value text)</type></entry>
327 <entry>get <type>hstore</>'s keys and values as a set</entry>
328 <entry><literal>select * from each('a=>1,b=>2')</literal></entry>
335 </programlisting></entry>
339 <entry><function>exist(hstore,text)</function></entry>
340 <entry><type>boolean</type></entry>
341 <entry>does <type>hstore</> contain key?</entry>
342 <entry><literal>exist('a=>1','a')</literal></entry>
343 <entry><literal>t</literal></entry>
347 <entry><function>defined(hstore,text)</function></entry>
348 <entry><type>boolean</type></entry>
349 <entry>does <type>hstore</> contain non-<literal>NULL</> value for key?</entry>
350 <entry><literal>defined('a=>NULL','a')</literal></entry>
351 <entry><literal>f</literal></entry>
355 <entry><function>delete(hstore,text)</function></entry>
356 <entry><type>hstore</type></entry>
357 <entry>delete pair with matching key</entry>
358 <entry><literal>delete('a=>1,b=>2','b')</literal></entry>
359 <entry><literal>"a"=>"1"</literal></entry>
363 <entry><function>delete(hstore,text[])</function></entry>
364 <entry><type>hstore</type></entry>
365 <entry>delete pairs with matching keys</entry>
366 <entry><literal>delete('a=>1,b=>2,c=>3',ARRAY['a','b'])</literal></entry>
367 <entry><literal>"c"=>"3"</literal></entry>
371 <entry><function>delete(hstore,hstore)</function></entry>
372 <entry><type>hstore</type></entry>
373 <entry>delete pairs matching those in the second argument</entry>
374 <entry><literal>delete('a=>1,b=>2','a=>4,b=>2'::hstore)</literal></entry>
375 <entry><literal>"a"=>"1"</literal></entry>
379 <entry><function>populate_record(record,hstore)</function></entry>
380 <entry><type>record</type></entry>
381 <entry>replace fields in <type>record</> with matching values from <type>hstore</></entry>
382 <entry>see Examples section</entry>
392 The function <function>populate_record</function> is actually declared
393 with <type>anyelement</>, not <type>record</>, as its first argument,
394 but it will reject non-record types with a runtime error.
400 <title>Indexes</title>
403 <type>hstore</> has GiST and GIN index support for the <literal>@></>,
404 <literal>?</>, <literal>?&</> and <literal>?|</> operators. For example:
407 CREATE INDEX hidx ON testhstore USING GIST (h);
409 CREATE INDEX hidx ON testhstore USING GIN (h);
413 <type>hstore</> also supports <type>btree</> or <type>hash</> indexes for
414 the <literal>=</> operator. This allows <type>hstore</> columns to be
415 declared <literal>UNIQUE</>, or to be used in <literal>GROUP BY</>,
416 <literal>ORDER BY</> or <literal>DISTINCT</> expressions. The sort ordering
417 for <type>hstore</> values is not particularly useful, but these indexes
418 may be useful for equivalence lookups. Create indexes for <literal>=</>
419 comparisons as follows:
422 CREATE INDEX hidx ON testhstore USING BTREE (h);
424 CREATE INDEX hidx ON testhstore USING HASH (h);
429 <title>Examples</title>
432 Add a key, or update an existing key with a new value:
435 UPDATE tab SET h = h || ('c' => '3');
442 UPDATE tab SET h = delete(h, 'k1');
446 Convert a <type>record</> to an <type>hstore</>:
449 CREATE TABLE test (col1 integer, col2 text, col3 text);
450 INSERT INTO test VALUES (123, 'foo', 'bar');
452 SELECT hstore(t) FROM test AS t;
454 ---------------------------------------------
455 "col1"=>"123", "col2"=>"foo", "col3"=>"bar"
460 Convert an <type>hstore</> to a predefined <type>record</> type:
463 CREATE TABLE test (col1 integer, col2 text, col3 text);
465 SELECT * FROM populate_record(null::test,
466 '"col1"=>"456", "col2"=>"zzz"');
474 Modify an existing record using the values from an <type>hstore</>:
477 CREATE TABLE test (col1 integer, col2 text, col3 text);
478 INSERT INTO test VALUES (123, 'foo', 'bar');
480 SELECT (r).* FROM (SELECT t #= '"col3"=>"baz"' AS r FROM test t) s;
489 <title>Statistics</title>
492 The <type>hstore</> type, because of its intrinsic liberality, could
493 contain a lot of different keys. Checking for valid keys is the task of the
494 application. The following examples demonstrate several techniques for
495 checking keys and obtaining statistics.
502 SELECT * FROM each('aaa=>bq, b=>NULL, ""=>1');
509 SELECT (each(h)).key, (each(h)).value INTO stat FROM testhstore;
516 SELECT key, count(*) FROM
517 (SELECT (each(h)).key FROM testhstore) AS stat
519 ORDER BY count DESC, key;
536 <title>Compatibility</title>
539 <emphasis>When upgrading from older versions, always load the new
540 version of this module into the database before restoring a dump.
541 Otherwise, many new features will be unavailable.</emphasis>
545 As of PostgreSQL 8.5, <type>hstore</> uses a different internal
546 representation than previous versions. This presents no obstacle for
547 dump/restore upgrades since the text representation (used in the dump) is
552 In the event of a binary upgrade, upward compatibility is maintained by
553 having the new code recognize old-format data. This will entail a slight
554 performance penalty when processing data that has not yet been modified by
555 the new code. It is possible to force an upgrade of all values in a table
556 column by doing an <literal>UPDATE</> statement as follows:
559 UPDATE tablename SET hstorecol = hstorecol || '';
563 Another way to do it is:
565 ALTER TABLE tablename ALTER hstorecol TYPE hstore USING hstorecol || '';
567 The <command>ALTER TABLE</> method requires an exclusive lock on the table,
568 but does not result in bloating the table with old row versions.
574 <title>Authors</title>
577 Oleg Bartunov <email>oleg@sai.msu.su</email>, Moscow, Moscow University, Russia
581 Teodor Sigaev <email>teodor@sigaev.ru</email>, Moscow, Delta-Soft Ltd., Russia
585 Additional enhancements by Andrew Gierth <email>andrew@tao11.riddles.org.uk</email>,