6 <indexterm zone=
"hstore">
7 <primary>hstore
</primary>
11 This module implements a data type
<type>hstore<
/> for storing sets of
12 (key,value) pairs within a single
<productname>PostgreSQL<
/> data field.
13 This can be useful in various scenarios, such as rows with many attributes
14 that are rarely examined, or semi-structured data.
18 <title><type>hstore<
/> External Representation
</title>
21 The text representation of an
<type>hstore<
/> value includes zero
22 or more
<replaceable>key<
/> <literal>=
><
/> <replaceable>value<
/>
23 items, separated by commas. For example:
27 foo =
> bar, baz =
> whatever
28 "1-a" =
> "anything at all"
31 The order of the items is not considered significant (and may not be
32 reproduced on output). Whitespace between items or around the
33 <literal>=
><
/> sign is ignored. Use double quotes if a key or
34 value includes whitespace, comma,
<literal>=<
/> or
<literal>><
/>.
35 To include a double quote or a backslash in a key or value, precede
36 it with another backslash. (Keep in mind that depending on the
37 setting of
<varname>standard_conforming_strings<
/>, you may need to
38 double backslashes in SQL literal strings.)
42 A value (but not a key) can be a SQL NULL. This is represented as
48 The
<literal>NULL<
/> keyword is not case-sensitive. Again, use
49 double quotes if you want the string
<literal>null<
/> to be treated
50 as an ordinary data value.
54 Currently, double quotes are always used to surround key and value
55 strings on output, even when this is not strictly necessary.
61 <title><type>hstore<
/> Operators and Functions
</title>
63 <table id=
"hstore-op-table">
64 <title><type>hstore<
/> Operators
</title>
69 <entry>Operator
</entry>
70 <entry>Description
</entry>
71 <entry>Example
</entry>
78 <entry><type>hstore<
/> <literal>-
><
/> <type>text<
/></entry>
79 <entry>get value for key (null if not present)
</entry>
80 <entry><literal>'a=
>x, b=
>y'::hstore -
> 'a'
</literal></entry>
81 <entry><literal>x
</literal></entry>
85 <entry><type>text<
/> <literal>=
><
/> <type>text<
/></entry>
86 <entry>make single-item
<type>hstore<
/></entry>
87 <entry><literal>'a' =
> 'b'
</literal></entry>
88 <entry><literal>"a"=
>"b"</literal></entry>
92 <entry><type>hstore<
/> <literal>||<
/> <type>hstore<
/></entry>
93 <entry>concatenation
</entry>
94 <entry><literal>'a=
>b, c=
>d'::hstore || 'c=
>x, d=
>q'::hstore
</literal></entry>
95 <entry><literal>"a"=
>"b",
"c"=
>"x",
"d"=
>"q"</literal></entry>
99 <entry><type>hstore<
/> <literal>?<
/> <type>text<
/></entry>
100 <entry>does
<type>hstore<
/> contain key?
</entry>
101 <entry><literal>'a=
>1'::hstore ? 'a'
</literal></entry>
102 <entry><literal>t
</literal></entry>
106 <entry><type>hstore<
/> <literal>@
><
/> <type>hstore<
/></entry>
107 <entry>does left operand contain right?
</entry>
108 <entry><literal>'a=
>b, b=
>1, c=
>NULL'::hstore @
> 'b=
>1'
</literal></entry>
109 <entry><literal>t
</literal></entry>
113 <entry><type>hstore<
/> <literal><@<
/> <type>hstore<
/></entry>
114 <entry>is left operand contained in right?
</entry>
115 <entry><literal>'a=
>c'::hstore
<@ 'a=
>b, b=
>1, c=
>NULL'
</literal></entry>
116 <entry><literal>f
</literal></entry>
124 (Before PostgreSQL
8.2, the containment operators @
> and
<@ were
125 respectively called @ and ~. These names are still available, but are
126 deprecated and will eventually be retired. Notice that the old names
127 are reversed from the convention formerly followed by the core geometric
131 <table id=
"hstore-func-table">
132 <title><type>hstore<
/> Functions
</title>
137 <entry>Function
</entry>
138 <entry>Return Type
</entry>
139 <entry>Description
</entry>
140 <entry>Example
</entry>
141 <entry>Result
</entry>
147 <entry><function>akeys(hstore)
</function></entry>
148 <entry><type>text[]
</type></entry>
149 <entry>get
<type>hstore<
/>'s keys as array
</entry>
150 <entry><literal>akeys('a=
>1,b=
>2')
</literal></entry>
151 <entry><literal>{a,b}
</literal></entry>
155 <entry><function>skeys(hstore)
</function></entry>
156 <entry><type>setof text
</type></entry>
157 <entry>get
<type>hstore<
/>'s keys as set
</entry>
158 <entry><literal>skeys('a=
>1,b=
>2')
</literal></entry>
163 </programlisting></entry>
167 <entry><function>avals(hstore)
</function></entry>
168 <entry><type>text[]
</type></entry>
169 <entry>get
<type>hstore<
/>'s values as array
</entry>
170 <entry><literal>avals('a=
>1,b=
>2')
</literal></entry>
171 <entry><literal>{
1,
2}
</literal></entry>
175 <entry><function>svals(hstore)
</function></entry>
176 <entry><type>setof text
</type></entry>
177 <entry>get
<type>hstore<
/>'s values as set
</entry>
178 <entry><literal>svals('a=
>1,b=
>2')
</literal></entry>
183 </programlisting></entry>
187 <entry><function>each(hstore)
</function></entry>
188 <entry><type>setof (key text, value text)
</type></entry>
189 <entry>get
<type>hstore<
/>'s keys and values as set
</entry>
190 <entry><literal>select * from each('a=
>1,b=
>2')
</literal></entry>
197 </programlisting></entry>
201 <entry><function>exist(hstore,text)
</function></entry>
202 <entry><type>boolean
</type></entry>
203 <entry>does
<type>hstore<
/> contain key?
</entry>
204 <entry><literal>exist('a=
>1','a')
</literal></entry>
205 <entry><literal>t
</literal></entry>
209 <entry><function>defined(hstore,text)
</function></entry>
210 <entry><type>boolean
</type></entry>
211 <entry>does
<type>hstore<
/> contain non-null value for key?
</entry>
212 <entry><literal>defined('a=
>NULL','a')
</literal></entry>
213 <entry><literal>f
</literal></entry>
217 <entry><function>delete(hstore,text)
</function></entry>
218 <entry><type>hstore
</type></entry>
219 <entry>delete any item matching key
</entry>
220 <entry><literal>delete('a=
>1,b=
>2','b')
</literal></entry>
221 <entry><literal>"a"=
>"1"</literal></entry>
230 <title>Indexes
</title>
233 <type>hstore<
/> has index support for
<literal>@
><
/> and
<literal>?<
/>
234 operators. You can use either GiST or GIN index types. For example:
237 CREATE INDEX hidx ON testhstore USING GIST(h);
239 CREATE INDEX hidx ON testhstore USING GIN(h);
244 <title>Examples
</title>
247 Add a key, or update an existing key with a new value:
250 UPDATE tab SET h = h || ('c' =
> '
3');
257 UPDATE tab SET h = delete(h, 'k1');
262 <title>Statistics
</title>
265 The
<type>hstore<
/> type, because of its intrinsic liberality, could
266 contain a lot of different keys. Checking for valid keys is the task of the
267 application. Examples below demonstrate several techniques for checking
268 keys and obtaining statistics.
275 SELECT * FROM each('aaa=
>bq, b=
>NULL,
""=
>1');
282 SELECT (each(h)).key, (each(h)).value INTO stat FROM testhstore;
289 SELECT key, count(*) FROM
290 (SELECT (each(h)).key FROM testhstore) AS stat
292 ORDER BY count DESC, key;
309 <title>Authors
</title>
312 Oleg Bartunov
<email>oleg@sai.msu.su
</email>, Moscow, Moscow University, Russia
316 Teodor Sigaev
<email>teodor@sigaev.ru
</email>, Moscow, Delta-Soft Ltd., Russia