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 In the current implementation, neither the key nor the value
19 string can exceed
65535 bytes in length; an error will be thrown if this
20 limit is exceeded. These maximum lengths may change in future releases.
24 <title><type>hstore<
/> External Representation
</title>
27 The text representation of an
<type>hstore<
/> value includes zero
28 or more
<replaceable>key<
/> <literal>=
><
/> <replaceable>value<
/>
29 items, separated by commas. For example:
33 foo =
> bar, baz =
> whatever
34 "1-a" =
> "anything at all"
37 The order of the items is not considered significant (and may not be
38 reproduced on output). Whitespace between items or around the
39 <literal>=
><
/> sign is ignored. Use double quotes if a key or
40 value includes whitespace, comma,
<literal>=<
/> or
<literal>><
/>.
41 To include a double quote or a backslash in a key or value, precede
42 it with another backslash. (Keep in mind that depending on the
43 setting of
<varname>standard_conforming_strings<
/>, you may need to
44 double backslashes in SQL literal strings.)
48 A value (but not a key) can be a SQL NULL. This is represented as
54 The
<literal>NULL<
/> keyword is not case-sensitive. Again, use
55 double quotes if you want the string
<literal>null<
/> to be treated
56 as an ordinary data value.
60 Currently, double quotes are always used to surround key and value
61 strings on output, even when this is not strictly necessary.
67 <title><type>hstore<
/> Operators and Functions
</title>
69 <table id=
"hstore-op-table">
70 <title><type>hstore<
/> Operators
</title>
75 <entry>Operator
</entry>
76 <entry>Description
</entry>
77 <entry>Example
</entry>
84 <entry><type>hstore<
/> <literal>-
><
/> <type>text<
/></entry>
85 <entry>get value for key (null if not present)
</entry>
86 <entry><literal>'a=
>x, b=
>y'::hstore -
> 'a'
</literal></entry>
87 <entry><literal>x
</literal></entry>
91 <entry><type>text<
/> <literal>=
><
/> <type>text<
/></entry>
92 <entry>make single-item
<type>hstore<
/></entry>
93 <entry><literal>'a' =
> 'b'
</literal></entry>
94 <entry><literal>"a"=
>"b"</literal></entry>
98 <entry><type>hstore<
/> <literal>||<
/> <type>hstore<
/></entry>
99 <entry>concatenation
</entry>
100 <entry><literal>'a=
>b, c=
>d'::hstore || 'c=
>x, d=
>q'::hstore
</literal></entry>
101 <entry><literal>"a"=
>"b",
"c"=
>"x",
"d"=
>"q"</literal></entry>
105 <entry><type>hstore<
/> <literal>?<
/> <type>text<
/></entry>
106 <entry>does
<type>hstore<
/> contain key?
</entry>
107 <entry><literal>'a=
>1'::hstore ? 'a'
</literal></entry>
108 <entry><literal>t
</literal></entry>
112 <entry><type>hstore<
/> <literal>@
><
/> <type>hstore<
/></entry>
113 <entry>does left operand contain right?
</entry>
114 <entry><literal>'a=
>b, b=
>1, c=
>NULL'::hstore @
> 'b=
>1'
</literal></entry>
115 <entry><literal>t
</literal></entry>
119 <entry><type>hstore<
/> <literal><@<
/> <type>hstore<
/></entry>
120 <entry>is left operand contained in right?
</entry>
121 <entry><literal>'a=
>c'::hstore
<@ 'a=
>b, b=
>1, c=
>NULL'
</literal></entry>
122 <entry><literal>f
</literal></entry>
130 (Before PostgreSQL
8.2, the containment operators @
> and
<@ were
131 respectively called @ and ~. These names are still available, but are
132 deprecated and will eventually be retired. Notice that the old names
133 are reversed from the convention formerly followed by the core geometric
137 <table id=
"hstore-func-table">
138 <title><type>hstore<
/> Functions
</title>
143 <entry>Function
</entry>
144 <entry>Return Type
</entry>
145 <entry>Description
</entry>
146 <entry>Example
</entry>
147 <entry>Result
</entry>
153 <entry><function>akeys(hstore)
</function></entry>
154 <entry><type>text[]
</type></entry>
155 <entry>get
<type>hstore<
/>'s keys as array
</entry>
156 <entry><literal>akeys('a=
>1,b=
>2')
</literal></entry>
157 <entry><literal>{a,b}
</literal></entry>
161 <entry><function>skeys(hstore)
</function></entry>
162 <entry><type>setof text
</type></entry>
163 <entry>get
<type>hstore<
/>'s keys as set
</entry>
164 <entry><literal>skeys('a=
>1,b=
>2')
</literal></entry>
169 </programlisting></entry>
173 <entry><function>avals(hstore)
</function></entry>
174 <entry><type>text[]
</type></entry>
175 <entry>get
<type>hstore<
/>'s values as array
</entry>
176 <entry><literal>avals('a=
>1,b=
>2')
</literal></entry>
177 <entry><literal>{
1,
2}
</literal></entry>
181 <entry><function>svals(hstore)
</function></entry>
182 <entry><type>setof text
</type></entry>
183 <entry>get
<type>hstore<
/>'s values as set
</entry>
184 <entry><literal>svals('a=
>1,b=
>2')
</literal></entry>
189 </programlisting></entry>
193 <entry><function>each(hstore)
</function></entry>
194 <entry><type>setof (key text, value text)
</type></entry>
195 <entry>get
<type>hstore<
/>'s keys and values as set
</entry>
196 <entry><literal>select * from each('a=
>1,b=
>2')
</literal></entry>
203 </programlisting></entry>
207 <entry><function>exist(hstore,text)
</function></entry>
208 <entry><type>boolean
</type></entry>
209 <entry>does
<type>hstore<
/> contain key?
</entry>
210 <entry><literal>exist('a=
>1','a')
</literal></entry>
211 <entry><literal>t
</literal></entry>
215 <entry><function>defined(hstore,text)
</function></entry>
216 <entry><type>boolean
</type></entry>
217 <entry>does
<type>hstore<
/> contain non-null value for key?
</entry>
218 <entry><literal>defined('a=
>NULL','a')
</literal></entry>
219 <entry><literal>f
</literal></entry>
223 <entry><function>delete(hstore,text)
</function></entry>
224 <entry><type>hstore
</type></entry>
225 <entry>delete any item matching key
</entry>
226 <entry><literal>delete('a=
>1,b=
>2','b')
</literal></entry>
227 <entry><literal>"a"=
>"1"</literal></entry>
236 <title>Indexes
</title>
239 <type>hstore<
/> has index support for
<literal>@
><
/> and
<literal>?<
/>
240 operators. You can use either GiST or GIN index types. For example:
243 CREATE INDEX hidx ON testhstore USING GIST(h);
245 CREATE INDEX hidx ON testhstore USING GIN(h);
250 <title>Examples
</title>
253 Add a key, or update an existing key with a new value:
256 UPDATE tab SET h = h || ('c' =
> '
3');
263 UPDATE tab SET h = delete(h, 'k1');
268 <title>Statistics
</title>
271 The
<type>hstore<
/> type, because of its intrinsic liberality, could
272 contain a lot of different keys. Checking for valid keys is the task of the
273 application. Examples below demonstrate several techniques for checking
274 keys and obtaining statistics.
281 SELECT * FROM each('aaa=
>bq, b=
>NULL,
""=
>1');
288 SELECT (each(h)).key, (each(h)).value INTO stat FROM testhstore;
295 SELECT key, count(*) FROM
296 (SELECT (each(h)).key FROM testhstore) AS stat
298 ORDER BY count DESC, key;
315 <title>Authors
</title>
318 Oleg Bartunov
<email>oleg@sai.msu.su
</email>, Moscow, Moscow University, Russia
322 Teodor Sigaev
<email>teodor@sigaev.ru
</email>, Moscow, Delta-Soft Ltd., Russia