summaryrefslogtreecommitdiff
path: root/doc/src/sgml/hstore.sgml
blob: 147fc7fba606ab0d3ef931f5264b8b5d6ce80960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<sect1 id="hstore">
 <title>hstore</title>
 
 <indexterm zone="hstore">
  <primary>hstore</primary>
 </indexterm>

 <para>
  The <literal>hstore</literal> module is usefull for storing (key,value) pairs.
  This module can be useful in different scenarios: case with many attributes
  rarely searched, semistructural data or a lazy DBA.
 </para>

 <sect2>
  <title>Operations</title>
  <itemizedlist>
   <listitem>
    <para>
      <literal>hstore -> text</literal> - get value , perl analogy $h{key} 
    </para>
    <programlisting>
select 'a=>q, b=>g'->'a';
  ?
------
  q
    </programlisting>
    <para>
     Note the use of parenthesis in the select below, because priority of 'is' is 
     higher than that of '->':
    </para>
    <programlisting>
SELECT id FROM entrants WHERE (info->'education_period') IS NOT NULL;
    </programlisting>
   </listitem>

   <listitem>
    <para>
     <literal>hstore || hstore</literal> - concatenation, perl analogy %a=( %b, %c ); 
    </para>
    <programlisting>
regression=# select 'a=>b'::hstore || 'c=>d'::hstore;
      ?column?      
--------------------
 "a"=>"b", "c"=>"d"
(1 row)
    </programlisting>

    <para>
     but, notice
    </para>

    <programlisting>
regression=# select 'a=>b'::hstore || 'a=>d'::hstore;
 ?column? 
----------
 "a"=>"d"
(1 row)
    </programlisting>
   </listitem>

   <listitem>
    <para>
     <literal>text => text</literal> - creates hstore type from two text strings 
    </para>
    <programlisting>
select 'a'=>'b';
  ?column?
----------
  "a"=>"b"
    </programlisting>
   </listitem>

   <listitem>
    <para>
      <literal>hstore @> hstore</literal> - contains operation, check if left operand contains right. 
    </para>
    <programlisting>
regression=# select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>c';
 ?column? 
----------
 f
(1 row)

regression=# select 'a=>b, b=>1, c=>NULL'::hstore @> 'b=>1';
 ?column? 
----------
 t
(1 row)
    </programlisting>
   </listitem>

   <listitem>
    <para>
     <literal>hstore &lt;@ hstore</literal> - contained operation, check if 
     left operand is contained in right
    </para>
    <para>
     (Before PostgreSQL 8.2, the containment operators @&gt; and &lt;@ were
     respectively called @ and ~.  These names are still available, but are
     deprecated and will eventually be retired.  Notice that the old names
     are reversed from the convention formerly followed by the core geometric
     datatypes!)
    </para>
   </listitem>
  </itemizedlist>
 </sect2>

 <sect2>
  <title>Functions</title>

  <itemizedlist>
   <listitem>
    <para>
     <literal>akeys(hstore)</literal> - returns all keys from hstore as array 
    </para>
    <programlisting>
regression=# select akeys('a=>1,b=>2');
 akeys 
-------
 {a,b}
    </programlisting>
   </listitem>

   <listitem>
    <para>
     <literal>skeys(hstore)</literal> - returns all keys from hstore as strings 
    </para>
    <programlisting>
regression=# select skeys('a=>1,b=>2');
 skeys 
-------
 a
 b
    </programlisting>
   </listitem>

   <listitem>
    <para>
     <literal>avals(hstore)</literal> - returns all values from hstore as array 
    </para>
    <programlisting>
regression=# select avals('a=>1,b=>2');
 avals 
-------
 {1,2}
    </programlisting>
   </listitem>

   <listitem>
    <para>
     <literal>svals(hstore)</literal> - returns all values from hstore as 
     strings 
    </para>
    <programlisting>
regression=# select svals('a=>1,b=>2');
 svals 
-------
 1
 2
    </programlisting>
   </listitem>

   <listitem>
    <para>
     <literal>delete (hstore,text)</literal> - delete (key,value) from hstore if 
     key matches argument. 
    </para>
    <programlisting>
regression=# select delete('a=>1,b=>2','b');
  delete  
----------
 "a"=>"1"
    </programlisting>
   </listitem>

   <listitem>
    <para>
     <literal>each(hstore)</literal> - return (key, value) pairs 
    </para>
    <programlisting>
regression=# select * from each('a=>1,b=>2');
 key | value 
-----+-------
 a   | 1
 b   | 2
    </programlisting>
   </listitem>

   <listitem>
    <para>
     <literal>exist (hstore,text)</literal>
    </para>
    <para>
     <literal>hstore ? text</literal> - returns 'true if key is exists in hstore 
     and false otherwise. 
    </para>
    <programlisting>
regression=# select exist('a=>1','a'), 'a=>1' ? 'a';
 exist | ?column? 
-------+----------
 t     | t
    </programlisting>
   </listitem>

   <listitem>
    <para>
     <literal>defined (hstore,text)</literal> - returns true if key is exists in 
      hstore and its value is not NULL. 
    </para>
    <programlisting>
regression=# select defined('a=>NULL','a');
 defined 
---------
 f
    </programlisting>
   </listitem>
  </itemizedlist>
 </sect2>

 <sect2>
  <title>Indices</title>
  <para>
   Module provides index support for '@>' and '?' operations.
  </para>
  <programlisting>
CREATE INDEX hidx ON testhstore USING GIST(h);
CREATE INDEX hidx ON testhstore USING GIN(h);
  </programlisting>
 </sect2>

 <sect2>
  <title>Examples</title>

  <para>
   Add a key:
  </para>
  <programlisting>
UPDATE tt SET h=h||'c=>3';
  </programlisting>
  <para>
   Delete a key:
  </para>
  <programlisting>
UPDATE tt SET h=delete(h,'k1');
  </programlisting>
 </sect2>

 <sect2>
  <title>Statistics</title>
  <para>
hstore type, because of its intrinsic liberality, could contain a lot of 
different keys. Checking for valid keys is the task of application. 
Examples below demonstrate several techniques how to check keys statistics.
  </para>

  <para>
   Simple example
  </para>
  <programlisting>
SELECT * FROM each('aaa=>bq, b=>NULL, ""=>1 ');
  </programlisting>

  <para>
   Using table
  </para>
  <programlisting>
SELECT (each(h)).key, (each(h)).value INTO stat FROM testhstore ;
  </programlisting>

  <para>Online stat</para>
  <programlisting>
SELECT key, count(*) FROM (SELECT (each(h)).key FROM testhstore) AS stat GROUP BY key ORDER BY count DESC, key;
    key    | count 
-----------+-------
 line      |   883
 query     |   207
 pos       |   203
 node      |   202
 space     |   197
 status    |   195
 public    |   194
 title     |   190
 org       |   189
...................
  </programlisting>
 </sect2>

 <sect2>
  <title>Authors</title>
  <para>
   Oleg Bartunov <email>oleg@sai.msu.su</email>, Moscow, Moscow University, Russia
  </para>
  <para>
   Teodor Sigaev <email>teodor@sigaev.ru</email>, Moscow, Delta-Soft Ltd.,Russia
  </para>
 </sect2>
</sect1>