![]() | ![]() | ![]() | ![]() |
F.10. hstoreThis module implements a data type hstore for storing sets of (key,value) pairs within a single PostgreSQL data field. This can be useful in various scenarios, such as rows with many attributes that are rarely examined, or semi-structured data. F.10.1. hstore External RepresentationThe text representation of an hstore value includes zero or more key => value items, separated by commas. For example: k => v
foo => bar, baz => whatever
"1-a" => "anything at all"
The order of the items is not considered significant (and may not be reproduced on output). Whitespace between items or around the => sign is ignored. Use double quotes if a key or value includes whitespace, comma, = or >. To include a double quote or a backslash in a key or value, precede it with another backslash. (Keep in mind that depending on the setting of standard_conforming_strings, you may need to double backslashes in SQL literal strings.) A value (but not a key) can be a SQL NULL. This is represented as key => NULL The NULL keyword is not case-sensitive. Again, use double quotes if you want the string null to be treated as an ordinary data value. Currently, double quotes are always used to surround key and value strings on output, even when this is not strictly necessary. F.10.2. hstore Operators and FunctionsTable F-5. hstore Operators
(Before PostgreSQL 8.2, the containment operators @> and <@ 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!) Table F-6. hstore Functions
F.10.3. Indexeshstore has index support for @> and ? operators. You can use either GiST or GIN index types. For example: CREATE INDEX hidx ON testhstore USING GIST(h); CREATE INDEX hidx ON testhstore USING GIN(h); F.10.4. ExamplesAdd a key, or update an existing key with a new value: UPDATE tab SET h = h || ('c' => '3');
Delete a key: UPDATE tab SET h = delete(h, 'k1'); F.10.5. StatisticsThe hstore type, because of its intrinsic liberality, could contain a lot of different keys. Checking for valid keys is the task of the application. Examples below demonstrate several techniques for checking keys and obtaining statistics. Simple example: SELECT * FROM each('aaa=>bq, b=>NULL, ""=>1');
Using a table: SELECT (each(h)).key, (each(h)).value INTO stat FROM testhstore; Online statistics: 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
...................
F.10.6. Authors Oleg Bartunov Teodor Sigaev |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||