1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>F.17. hstore — hstore key/value datatype</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="fuzzystrmatch.html" title="F.16. fuzzystrmatch — determine string similarities and distance" /><link rel="next" href="intagg.html" title="F.18. intagg — integer aggregator and enumerator" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">F.17. hstore — hstore key/value datatype</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="fuzzystrmatch.html" title="F.16. fuzzystrmatch — determine string similarities and distance">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="contrib.html" title="Appendix F. Additional Supplied Modules and Extensions">Up</a></td><th width="60%" align="center">Appendix F. Additional Supplied Modules and Extensions</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="intagg.html" title="F.18. intagg — integer aggregator and enumerator">Next</a></td></tr></table><hr /></div><div class="sect1" id="HSTORE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">F.17. hstore — hstore key/value datatype <a href="#HSTORE" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="hstore.html#HSTORE-EXTERNAL-REP">F.17.1. <code class="type">hstore</code> External Representation</a></span></dt><dt><span class="sect2"><a href="hstore.html#HSTORE-OPS-FUNCS">F.17.2. <code class="type">hstore</code> Operators and Functions</a></span></dt><dt><span class="sect2"><a href="hstore.html#HSTORE-INDEXES">F.17.3. Indexes</a></span></dt><dt><span class="sect2"><a href="hstore.html#HSTORE-EXAMPLES">F.17.4. Examples</a></span></dt><dt><span class="sect2"><a href="hstore.html#HSTORE-STATISTICS">F.17.5. Statistics</a></span></dt><dt><span class="sect2"><a href="hstore.html#HSTORE-COMPATIBILITY">F.17.6. Compatibility</a></span></dt><dt><span class="sect2"><a href="hstore.html#HSTORE-TRANSFORMS">F.17.7. Transforms</a></span></dt><dt><span class="sect2"><a href="hstore.html#HSTORE-AUTHORS">F.17.8. Authors</a></span></dt></dl></div><a id="id-1.11.7.27.2" class="indexterm"></a><p>
3 This module implements the <code class="type">hstore</code> data type for storing sets of
4 key/value pairs within a single <span class="productname">PostgreSQL</span> value.
5 This can be useful in various scenarios, such as rows with many attributes
6 that are rarely examined, or semi-structured data. Keys and values are
9 This module is considered <span class="quote">“<span class="quote">trusted</span>”</span>, that is, it can be
10 installed by non-superusers who have <code class="literal">CREATE</code> privilege
11 on the current database.
12 </p><div class="sect2" id="HSTORE-EXTERNAL-REP"><div class="titlepage"><div><div><h3 class="title">F.17.1. <code class="type">hstore</code> External Representation <a href="#HSTORE-EXTERNAL-REP" class="id_link">#</a></h3></div></div></div><p>
14 The text representation of an <code class="type">hstore</code>, used for input and output,
15 includes zero or more <em class="replaceable"><code>key</code></em> <code class="literal">=></code>
16 <em class="replaceable"><code>value</code></em> pairs separated by commas. Some examples:
18 </p><pre class="synopsis">
20 foo => bar, baz => whatever
21 "1-a" => "anything at all"
24 The order of the pairs is not significant (and may not be reproduced on
25 output). Whitespace between pairs or around the <code class="literal">=></code> sign is
26 ignored. Double-quote keys and values that include whitespace, commas,
27 <code class="literal">=</code>s or <code class="literal">></code>s. To include a double quote or a
28 backslash in a key or value, escape it with a backslash.
30 Each key in an <code class="type">hstore</code> is unique. If you declare an <code class="type">hstore</code>
31 with duplicate keys, only one will be stored in the <code class="type">hstore</code> and
32 there is no guarantee as to which will be kept:
34 </p><pre class="programlisting">
35 SELECT 'a=>1,a=>2'::hstore;
41 A value (but not a key) can be an SQL <code class="literal">NULL</code>. For example:
43 </p><pre class="programlisting">
47 The <code class="literal">NULL</code> keyword is case-insensitive. Double-quote the
48 <code class="literal">NULL</code> to treat it as the ordinary string <span class="quote">“<span class="quote">NULL</span>”</span>.
49 </p><div class="note"><h3 class="title">Note</h3><p>
50 Keep in mind that the <code class="type">hstore</code> text format, when used for input,
51 applies <span class="emphasis"><em>before</em></span> any required quoting or escaping. If you are
52 passing an <code class="type">hstore</code> literal via a parameter, then no additional
53 processing is needed. But if you're passing it as a quoted literal
54 constant, then any single-quote characters and (depending on the setting of
55 the <code class="varname">standard_conforming_strings</code> configuration parameter)
56 backslash characters need to be escaped correctly. See
57 <a class="xref" href="sql-syntax-lexical.html#SQL-SYNTAX-STRINGS" title="4.1.2.1. String Constants">Section 4.1.2.1</a> for more on the handling of string
60 On output, double quotes always surround keys and values, even when it's
61 not strictly necessary.
62 </p></div><div class="sect2" id="HSTORE-OPS-FUNCS"><div class="titlepage"><div><div><h3 class="title">F.17.2. <code class="type">hstore</code> Operators and Functions <a href="#HSTORE-OPS-FUNCS" class="id_link">#</a></h3></div></div></div><p>
63 The operators provided by the <code class="literal">hstore</code> module are
64 shown in <a class="xref" href="hstore.html#HSTORE-OP-TABLE" title="Table F.6. hstore Operators">Table F.6</a>, the functions
65 in <a class="xref" href="hstore.html#HSTORE-FUNC-TABLE" title="Table F.7. hstore Functions">Table F.7</a>.
66 </p><div class="table" id="HSTORE-OP-TABLE"><p class="title"><strong>Table F.6. <code class="type">hstore</code> Operators</strong></p><div class="table-contents"><table class="table" summary="hstore Operators" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
74 </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
75 <code class="type">hstore</code> <code class="literal">-></code> <code class="type">text</code>
76 → <code class="returnvalue">text</code>
79 Returns value associated with given key, or <code class="literal">NULL</code> if
83 <code class="literal">'a=>x, b=>y'::hstore -> 'a'</code>
84 → <code class="returnvalue">x</code>
85 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
86 <code class="type">hstore</code> <code class="literal">-></code> <code class="type">text[]</code>
87 → <code class="returnvalue">text[]</code>
90 Returns values associated with given keys, or <code class="literal">NULL</code>
94 <code class="literal">'a=>x, b=>y, c=>z'::hstore -> ARRAY['c','a']</code>
95 → <code class="returnvalue">{"z","x"}</code>
96 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
97 <code class="type">hstore</code> <code class="literal">||</code> <code class="type">hstore</code>
98 → <code class="returnvalue">hstore</code>
101 Concatenates two <code class="type">hstore</code>s.
104 <code class="literal">'a=>b, c=>d'::hstore || 'c=>x, d=>q'::hstore</code>
105 → <code class="returnvalue">"a"=>"b", "c"=>"x", "d"=>"q"</code>
106 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
107 <code class="type">hstore</code> <code class="literal">?</code> <code class="type">text</code>
108 → <code class="returnvalue">boolean</code>
111 Does <code class="type">hstore</code> contain key?
114 <code class="literal">'a=>1'::hstore ? 'a'</code>
115 → <code class="returnvalue">t</code>
116 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
117 <code class="type">hstore</code> <code class="literal">?&</code> <code class="type">text[]</code>
118 → <code class="returnvalue">boolean</code>
121 Does <code class="type">hstore</code> contain all the specified keys?
124 <code class="literal">'a=>1,b=>2'::hstore ?& ARRAY['a','b']</code>
125 → <code class="returnvalue">t</code>
126 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
127 <code class="type">hstore</code> <code class="literal">?|</code> <code class="type">text[]</code>
128 → <code class="returnvalue">boolean</code>
131 Does <code class="type">hstore</code> contain any of the specified keys?
134 <code class="literal">'a=>1,b=>2'::hstore ?| ARRAY['b','c']</code>
135 → <code class="returnvalue">t</code>
136 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
137 <code class="type">hstore</code> <code class="literal">@></code> <code class="type">hstore</code>
138 → <code class="returnvalue">boolean</code>
141 Does left operand contain right?
144 <code class="literal">'a=>b, b=>1, c=>NULL'::hstore @> 'b=>1'</code>
145 → <code class="returnvalue">t</code>
146 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
147 <code class="type">hstore</code> <code class="literal"><@</code> <code class="type">hstore</code>
148 → <code class="returnvalue">boolean</code>
151 Is left operand contained in right?
154 <code class="literal">'a=>c'::hstore <@ 'a=>b, b=>1, c=>NULL'</code>
155 → <code class="returnvalue">f</code>
156 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
157 <code class="type">hstore</code> <code class="literal">-</code> <code class="type">text</code>
158 → <code class="returnvalue">hstore</code>
161 Deletes key from left operand.
164 <code class="literal">'a=>1, b=>2, c=>3'::hstore - 'b'::text</code>
165 → <code class="returnvalue">"a"=>"1", "c"=>"3"</code>
166 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
167 <code class="type">hstore</code> <code class="literal">-</code> <code class="type">text[]</code>
168 → <code class="returnvalue">hstore</code>
171 Deletes keys from left operand.
174 <code class="literal">'a=>1, b=>2, c=>3'::hstore - ARRAY['a','b']</code>
175 → <code class="returnvalue">"c"=>"3"</code>
176 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
177 <code class="type">hstore</code> <code class="literal">-</code> <code class="type">hstore</code>
178 → <code class="returnvalue">hstore</code>
181 Deletes pairs from left operand that match pairs in the right operand.
184 <code class="literal">'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore</code>
185 → <code class="returnvalue">"a"=>"1", "c"=>"3"</code>
186 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
187 <code class="type">anyelement</code> <code class="literal">#=</code> <code class="type">hstore</code>
188 → <code class="returnvalue">anyelement</code>
191 Replaces fields in the left operand (which must be a composite type)
192 with matching values from <code class="type">hstore</code>.
195 <code class="literal">ROW(1,3) #= 'f1=>11'::hstore</code>
196 → <code class="returnvalue">(11,3)</code>
197 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
198 <code class="literal">%%</code> <code class="type">hstore</code>
199 → <code class="returnvalue">text[]</code>
202 Converts <code class="type">hstore</code> to an array of alternating keys and
206 <code class="literal">%% 'a=>foo, b=>bar'::hstore</code>
207 → <code class="returnvalue">{a,foo,b,bar}</code>
208 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
209 <code class="literal">%#</code> <code class="type">hstore</code>
210 → <code class="returnvalue">text[]</code>
213 Converts <code class="type">hstore</code> to a two-dimensional key/value array.
216 <code class="literal">%# 'a=>foo, b=>bar'::hstore</code>
217 → <code class="returnvalue">{{a,foo},{b,bar}}</code>
218 </p></td></tr></tbody></table></div></div><br class="table-break" /><div class="table" id="HSTORE-FUNC-TABLE"><p class="title"><strong>Table F.7. <code class="type">hstore</code> Functions</strong></p><div class="table-contents"><table class="table" summary="hstore Functions" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
226 </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
227 <a id="id-1.11.7.27.6.4.2.2.1.1.1.1" class="indexterm"></a>
228 <code class="function">hstore</code> ( <code class="type">record</code> )
229 → <code class="returnvalue">hstore</code>
232 Constructs an <code class="type">hstore</code> from a record or row.
235 <code class="literal">hstore(ROW(1,2))</code>
236 → <code class="returnvalue">"f1"=>"1", "f2"=>"2"</code>
237 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
238 <code class="function">hstore</code> ( <code class="type">text[]</code> )
239 → <code class="returnvalue">hstore</code>
242 Constructs an <code class="type">hstore</code> from an array, which may be either
243 a key/value array, or a two-dimensional array.
246 <code class="literal">hstore(ARRAY['a','1','b','2'])</code>
247 → <code class="returnvalue">"a"=>"1", "b"=>"2"</code>
250 <code class="literal">hstore(ARRAY[['c','3'],['d','4']])</code>
251 → <code class="returnvalue">"c"=>"3", "d"=>"4"</code>
252 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
253 <code class="function">hstore</code> ( <code class="type">text[]</code>, <code class="type">text[]</code> )
254 → <code class="returnvalue">hstore</code>
257 Constructs an <code class="type">hstore</code> from separate key and value arrays.
260 <code class="literal">hstore(ARRAY['a','b'], ARRAY['1','2'])</code>
261 → <code class="returnvalue">"a"=>"1", "b"=>"2"</code>
262 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
263 <code class="function">hstore</code> ( <code class="type">text</code>, <code class="type">text</code> )
264 → <code class="returnvalue">hstore</code>
267 Makes a single-item <code class="type">hstore</code>.
270 <code class="literal">hstore('a', 'b')</code>
271 → <code class="returnvalue">"a"=>"b"</code>
272 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
273 <a id="id-1.11.7.27.6.4.2.2.5.1.1.1" class="indexterm"></a>
274 <code class="function">akeys</code> ( <code class="type">hstore</code> )
275 → <code class="returnvalue">text[]</code>
278 Extracts an <code class="type">hstore</code>'s keys as an array.
281 <code class="literal">akeys('a=>1,b=>2')</code>
282 → <code class="returnvalue">{a,b}</code>
283 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
284 <a id="id-1.11.7.27.6.4.2.2.6.1.1.1" class="indexterm"></a>
285 <code class="function">skeys</code> ( <code class="type">hstore</code> )
286 → <code class="returnvalue">setof text</code>
289 Extracts an <code class="type">hstore</code>'s keys as a set.
292 <code class="literal">skeys('a=>1,b=>2')</code>
293 → <code class="returnvalue"></code>
294 </p><pre class="programlisting">
298 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
299 <a id="id-1.11.7.27.6.4.2.2.7.1.1.1" class="indexterm"></a>
300 <code class="function">avals</code> ( <code class="type">hstore</code> )
301 → <code class="returnvalue">text[]</code>
304 Extracts an <code class="type">hstore</code>'s values as an array.
307 <code class="literal">avals('a=>1,b=>2')</code>
308 → <code class="returnvalue">{1,2}</code>
309 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
310 <a id="id-1.11.7.27.6.4.2.2.8.1.1.1" class="indexterm"></a>
311 <code class="function">svals</code> ( <code class="type">hstore</code> )
312 → <code class="returnvalue">setof text</code>
315 Extracts an <code class="type">hstore</code>'s values as a set.
318 <code class="literal">svals('a=>1,b=>2')</code>
319 → <code class="returnvalue"></code>
320 </p><pre class="programlisting">
324 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
325 <a id="id-1.11.7.27.6.4.2.2.9.1.1.1" class="indexterm"></a>
326 <code class="function">hstore_to_array</code> ( <code class="type">hstore</code> )
327 → <code class="returnvalue">text[]</code>
330 Extracts an <code class="type">hstore</code>'s keys and values as an array of
331 alternating keys and values.
334 <code class="literal">hstore_to_array('a=>1,b=>2')</code>
335 → <code class="returnvalue">{a,1,b,2}</code>
336 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
337 <a id="id-1.11.7.27.6.4.2.2.10.1.1.1" class="indexterm"></a>
338 <code class="function">hstore_to_matrix</code> ( <code class="type">hstore</code> )
339 → <code class="returnvalue">text[]</code>
342 Extracts an <code class="type">hstore</code>'s keys and values as a two-dimensional
346 <code class="literal">hstore_to_matrix('a=>1,b=>2')</code>
347 → <code class="returnvalue">{{a,1},{b,2}}</code>
348 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
349 <a id="id-1.11.7.27.6.4.2.2.11.1.1.1" class="indexterm"></a>
350 <code class="function">hstore_to_json</code> ( <code class="type">hstore</code> )
351 → <code class="returnvalue">json</code>
354 Converts an <code class="type">hstore</code> to a <code class="type">json</code> value,
355 converting all non-null values to JSON strings.
358 This function is used implicitly when an <code class="type">hstore</code> value is
359 cast to <code class="type">json</code>.
362 <code class="literal">hstore_to_json('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4')</code>
363 → <code class="returnvalue">{"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"}</code>
364 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
365 <a id="id-1.11.7.27.6.4.2.2.12.1.1.1" class="indexterm"></a>
366 <code class="function">hstore_to_jsonb</code> ( <code class="type">hstore</code> )
367 → <code class="returnvalue">jsonb</code>
370 Converts an <code class="type">hstore</code> to a <code class="type">jsonb</code> value,
371 converting all non-null values to JSON strings.
374 This function is used implicitly when an <code class="type">hstore</code> value is
375 cast to <code class="type">jsonb</code>.
378 <code class="literal">hstore_to_jsonb('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4')</code>
379 → <code class="returnvalue">{"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"}</code>
380 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
381 <a id="id-1.11.7.27.6.4.2.2.13.1.1.1" class="indexterm"></a>
382 <code class="function">hstore_to_json_loose</code> ( <code class="type">hstore</code> )
383 → <code class="returnvalue">json</code>
386 Converts an <code class="type">hstore</code> to a <code class="type">json</code> value, but
387 attempts to distinguish numerical and Boolean values so they are
388 unquoted in the JSON.
391 <code class="literal">hstore_to_json_loose('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4')</code>
392 → <code class="returnvalue">{"a key": 1, "b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4}</code>
393 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
394 <a id="id-1.11.7.27.6.4.2.2.14.1.1.1" class="indexterm"></a>
395 <code class="function">hstore_to_jsonb_loose</code> ( <code class="type">hstore</code> )
396 → <code class="returnvalue">jsonb</code>
399 Converts an <code class="type">hstore</code> to a <code class="type">jsonb</code> value, but
400 attempts to distinguish numerical and Boolean values so they are
401 unquoted in the JSON.
404 <code class="literal">hstore_to_jsonb_loose('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4')</code>
405 → <code class="returnvalue">{"a key": 1, "b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4}</code>
406 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
407 <a id="id-1.11.7.27.6.4.2.2.15.1.1.1" class="indexterm"></a>
408 <code class="function">slice</code> ( <code class="type">hstore</code>, <code class="type">text[]</code> )
409 → <code class="returnvalue">hstore</code>
412 Extracts a subset of an <code class="type">hstore</code> containing only the
416 <code class="literal">slice('a=>1,b=>2,c=>3'::hstore, ARRAY['b','c','x'])</code>
417 → <code class="returnvalue">"b"=>"2", "c"=>"3"</code>
418 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
419 <a id="id-1.11.7.27.6.4.2.2.16.1.1.1" class="indexterm"></a>
420 <code class="function">each</code> ( <code class="type">hstore</code> )
421 → <code class="returnvalue">setof record</code>
422 ( <em class="parameter"><code>key</code></em> <code class="type">text</code>,
423 <em class="parameter"><code>value</code></em> <code class="type">text</code> )
426 Extracts an <code class="type">hstore</code>'s keys and values as a set of records.
429 <code class="literal">select * from each('a=>1,b=>2')</code>
430 → <code class="returnvalue"></code>
431 </p><pre class="programlisting">
437 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
438 <a id="id-1.11.7.27.6.4.2.2.17.1.1.1" class="indexterm"></a>
439 <code class="function">exist</code> ( <code class="type">hstore</code>, <code class="type">text</code> )
440 → <code class="returnvalue">boolean</code>
443 Does <code class="type">hstore</code> contain key?
446 <code class="literal">exist('a=>1', 'a')</code>
447 → <code class="returnvalue">t</code>
448 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
449 <a id="id-1.11.7.27.6.4.2.2.18.1.1.1" class="indexterm"></a>
450 <code class="function">defined</code> ( <code class="type">hstore</code>, <code class="type">text</code> )
451 → <code class="returnvalue">boolean</code>
454 Does <code class="type">hstore</code> contain a non-<code class="literal">NULL</code> value
458 <code class="literal">defined('a=>NULL', 'a')</code>
459 → <code class="returnvalue">f</code>
460 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
461 <a id="id-1.11.7.27.6.4.2.2.19.1.1.1" class="indexterm"></a>
462 <code class="function">delete</code> ( <code class="type">hstore</code>, <code class="type">text</code> )
463 → <code class="returnvalue">hstore</code>
466 Deletes pair with matching key.
469 <code class="literal">delete('a=>1,b=>2', 'b')</code>
470 → <code class="returnvalue">"a"=>"1"</code>
471 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
472 <code class="function">delete</code> ( <code class="type">hstore</code>, <code class="type">text[]</code> )
473 → <code class="returnvalue">hstore</code>
476 Deletes pairs with matching keys.
479 <code class="literal">delete('a=>1,b=>2,c=>3', ARRAY['a','b'])</code>
480 → <code class="returnvalue">"c"=>"3"</code>
481 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
482 <code class="function">delete</code> ( <code class="type">hstore</code>, <code class="type">hstore</code> )
483 → <code class="returnvalue">hstore</code>
486 Deletes pairs matching those in the second argument.
489 <code class="literal">delete('a=>1,b=>2', 'a=>4,b=>2'::hstore)</code>
490 → <code class="returnvalue">"a"=>"1"</code>
491 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
492 <a id="id-1.11.7.27.6.4.2.2.22.1.1.1" class="indexterm"></a>
493 <code class="function">populate_record</code> ( <code class="type">anyelement</code>, <code class="type">hstore</code> )
494 → <code class="returnvalue">anyelement</code>
497 Replaces fields in the left operand (which must be a composite type)
498 with matching values from <code class="type">hstore</code>.
501 <code class="literal">populate_record(ROW(1,2), 'f1=>42'::hstore)</code>
502 → <code class="returnvalue">(42,2)</code>
503 </p></td></tr></tbody></table></div></div><br class="table-break" /><p>
504 In addition to these operators and functions, values of
505 the <code class="type">hstore</code> type can be subscripted, allowing them to act
506 like associative arrays. Only a single subscript of type <code class="type">text</code>
507 can be specified; it is interpreted as a key and the corresponding
508 value is fetched or stored. For example,
510 </p><pre class="programlisting">
511 CREATE TABLE mytable (h hstore);
512 INSERT INTO mytable VALUES ('a=>b, c=>d');
513 SELECT h['a'] FROM mytable;
519 UPDATE mytable SET h['c'] = 'new';
520 SELECT h FROM mytable;
522 ----------------------
523 "a"=>"b", "c"=>"new"
527 A subscripted fetch returns <code class="literal">NULL</code> if the subscript
528 is <code class="literal">NULL</code> or that key does not exist in
529 the <code class="type">hstore</code>. (Thus, a subscripted fetch is not greatly
530 different from the <code class="literal">-></code> operator.)
531 A subscripted update fails if the subscript is <code class="literal">NULL</code>;
532 otherwise, it replaces the value for that key, adding an entry to
533 the <code class="type">hstore</code> if the key does not already exist.
534 </p></div><div class="sect2" id="HSTORE-INDEXES"><div class="titlepage"><div><div><h3 class="title">F.17.3. Indexes <a href="#HSTORE-INDEXES" class="id_link">#</a></h3></div></div></div><p>
535 <code class="type">hstore</code> has GiST and GIN index support for the <code class="literal">@></code>,
536 <code class="literal">?</code>, <code class="literal">?&</code> and <code class="literal">?|</code> operators. For example:
537 </p><pre class="programlisting">
538 CREATE INDEX hidx ON testhstore USING GIST (h);
540 CREATE INDEX hidx ON testhstore USING GIN (h);
542 <code class="literal">gist_hstore_ops</code> GiST opclass approximates a set of
543 key/value pairs as a bitmap signature. Its optional integer parameter
544 <code class="literal">siglen</code> determines the
545 signature length in bytes. The default length is 16 bytes.
546 Valid values of signature length are between 1 and 2024 bytes. Longer
547 signatures lead to a more precise search (scanning a smaller fraction of the index and
548 fewer heap pages), at the cost of a larger index.
550 Example of creating such an index with a signature length of 32 bytes:
551 </p><pre class="programlisting">
552 CREATE INDEX hidx ON testhstore USING GIST (h gist_hstore_ops(siglen=32));
555 <code class="type">hstore</code> also supports <code class="type">btree</code> or <code class="type">hash</code> indexes for
556 the <code class="literal">=</code> operator. This allows <code class="type">hstore</code> columns to be
557 declared <code class="literal">UNIQUE</code>, or to be used in <code class="literal">GROUP BY</code>,
558 <code class="literal">ORDER BY</code> or <code class="literal">DISTINCT</code> expressions. The sort ordering
559 for <code class="type">hstore</code> values is not particularly useful, but these indexes
560 may be useful for equivalence lookups. Create indexes for <code class="literal">=</code>
561 comparisons as follows:
562 </p><pre class="programlisting">
563 CREATE INDEX hidx ON testhstore USING BTREE (h);
565 CREATE INDEX hidx ON testhstore USING HASH (h);
566 </pre></div><div class="sect2" id="HSTORE-EXAMPLES"><div class="titlepage"><div><div><h3 class="title">F.17.4. Examples <a href="#HSTORE-EXAMPLES" class="id_link">#</a></h3></div></div></div><p>
567 Add a key, or update an existing key with a new value:
568 </p><pre class="programlisting">
569 UPDATE tab SET h['c'] = '3';
571 Another way to do the same thing is:
572 </p><pre class="programlisting">
573 UPDATE tab SET h = h || hstore('c', '3');
575 If multiple keys are to be added or changed in one operation,
576 the concatenation approach is more efficient than subscripting:
577 </p><pre class="programlisting">
578 UPDATE tab SET h = h || hstore(array['q', 'w'], array['11', '12']);
582 </p><pre class="programlisting">
583 UPDATE tab SET h = delete(h, 'k1');
586 Convert a <code class="type">record</code> to an <code class="type">hstore</code>:
587 </p><pre class="programlisting">
588 CREATE TABLE test (col1 integer, col2 text, col3 text);
589 INSERT INTO test VALUES (123, 'foo', 'bar');
591 SELECT hstore(t) FROM test AS t;
593 ---------------------------------------------
594 "col1"=>"123", "col2"=>"foo", "col3"=>"bar"
598 Convert an <code class="type">hstore</code> to a predefined <code class="type">record</code> type:
599 </p><pre class="programlisting">
600 CREATE TABLE test (col1 integer, col2 text, col3 text);
602 SELECT * FROM populate_record(null::test,
603 '"col1"=>"456", "col2"=>"zzz"');
610 Modify an existing record using the values from an <code class="type">hstore</code>:
611 </p><pre class="programlisting">
612 CREATE TABLE test (col1 integer, col2 text, col3 text);
613 INSERT INTO test VALUES (123, 'foo', 'bar');
615 SELECT (r).* FROM (SELECT t #= '"col3"=>"baz"' AS r FROM test t) s;
621 </p></div><div class="sect2" id="HSTORE-STATISTICS"><div class="titlepage"><div><div><h3 class="title">F.17.5. Statistics <a href="#HSTORE-STATISTICS" class="id_link">#</a></h3></div></div></div><p>
622 The <code class="type">hstore</code> type, because of its intrinsic liberality, could
623 contain a lot of different keys. Checking for valid keys is the task of the
624 application. The following examples demonstrate several techniques for
625 checking keys and obtaining statistics.
628 </p><pre class="programlisting">
629 SELECT * FROM each('aaa=>bq, b=>NULL, ""=>1');
633 </p><pre class="programlisting">
634 CREATE TABLE stat AS SELECT (each(h)).key, (each(h)).value FROM testhstore;
638 </p><pre class="programlisting">
639 SELECT key, count(*) FROM
640 (SELECT (each(h)).key FROM testhstore) AS stat
642 ORDER BY count DESC, key;
656 </p></div><div class="sect2" id="HSTORE-COMPATIBILITY"><div class="titlepage"><div><div><h3 class="title">F.17.6. Compatibility <a href="#HSTORE-COMPATIBILITY" class="id_link">#</a></h3></div></div></div><p>
657 As of PostgreSQL 9.0, <code class="type">hstore</code> uses a different internal
658 representation than previous versions. This presents no obstacle for
659 dump/restore upgrades since the text representation (used in the dump) is
662 In the event of a binary upgrade, upward compatibility is maintained by
663 having the new code recognize old-format data. This will entail a slight
664 performance penalty when processing data that has not yet been modified by
665 the new code. It is possible to force an upgrade of all values in a table
666 column by doing an <code class="literal">UPDATE</code> statement as follows:
667 </p><pre class="programlisting">
668 UPDATE tablename SET hstorecol = hstorecol || '';
671 Another way to do it is:
672 </p><pre class="programlisting">
673 ALTER TABLE tablename ALTER hstorecol TYPE hstore USING hstorecol || '';
675 The <code class="command">ALTER TABLE</code> method requires an
676 <code class="literal">ACCESS EXCLUSIVE</code> lock on the table,
677 but does not result in bloating the table with old row versions.
678 </p></div><div class="sect2" id="HSTORE-TRANSFORMS"><div class="titlepage"><div><div><h3 class="title">F.17.7. Transforms <a href="#HSTORE-TRANSFORMS" class="id_link">#</a></h3></div></div></div><p>
679 Additional extensions are available that implement transforms for
680 the <code class="type">hstore</code> type for the languages PL/Perl and PL/Python. The
681 extensions for PL/Perl are called <code class="literal">hstore_plperl</code>
682 and <code class="literal">hstore_plperlu</code>, for trusted and untrusted PL/Perl.
683 If you install these transforms and specify them when creating a
684 function, <code class="type">hstore</code> values are mapped to Perl hashes. The
685 extension for PL/Python is called <code class="literal">hstore_plpython3u</code>.
686 If you use it, <code class="type">hstore</code> values are mapped to Python dictionaries.
687 </p></div><div class="sect2" id="HSTORE-AUTHORS"><div class="titlepage"><div><div><h3 class="title">F.17.8. Authors <a href="#HSTORE-AUTHORS" class="id_link">#</a></h3></div></div></div><p>
688 Oleg Bartunov <code class="email"><<a class="email" href="mailto:oleg@sai.msu.su">oleg@sai.msu.su</a>></code>, Moscow, Moscow University, Russia
690 Teodor Sigaev <code class="email"><<a class="email" href="mailto:teodor@sigaev.ru">teodor@sigaev.ru</a>></code>, Moscow, Delta-Soft Ltd., Russia
692 Additional enhancements by Andrew Gierth <code class="email"><<a class="email" href="mailto:andrew@tao11.riddles.org.uk">andrew@tao11.riddles.org.uk</a>></code>,
694 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="fuzzystrmatch.html" title="F.16. fuzzystrmatch — determine string similarities and distance">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="contrib.html" title="Appendix F. Additional Supplied Modules and Extensions">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="intagg.html" title="F.18. intagg — integer aggregator and enumerator">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F.16. fuzzystrmatch — determine string similarities and distance </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> F.18. intagg — integer aggregator and enumerator</td></tr></table></div></body></html>