README.hstore 4.0 KB
Newer Older
T
Teodor Sigaev 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
Hstore - contrib module for storing (key,value) pairs

[Online version] (http://www.sai.msu.su/~megera/oddmuse/index.cgi?Hstore)

Motivation

Many attributes rarely searched, semistructural data, lazy DBA

Authors

    * Oleg Bartunov <oleg@sai.msu.su>, Moscow, Moscow University, Russia
    * Teodor Sigaev <teodor@sigaev.ru>, Moscow, Delta-Soft Ltd.,Russia

T
Teodor Sigaev 已提交
14 15
LEGAL NOTICES: This module is released under BSD license (as PostgreSQL
itself)
T
Teodor Sigaev 已提交
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

Operations

    * hstore -> text - get value , perl analogy $h{key} 

select 'a=>q, b=>g'->'a';
  ?
------
  q

    * hstore || hstore - concatenation, perl analogy %a=( %b, %c ); 

regression=# select 'a=>b'::hstore || 'c=>d'::hstore;
      ?column?      
--------------------
 "a"=>"b", "c"=>"d"
(1 row)

but, notice

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

    * text => text - creates hstore type from two text strings 

select 'a'=>'b';
  ?column?
----------
  "a"=>"b"

49
    * hstore @> hstore - contains operation, check if left operand contains right. 
T
Teodor Sigaev 已提交
50

51
regression=# select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>c';
T
Teodor Sigaev 已提交
52 53 54 55 56
 ?column? 
----------
 f
(1 row)

57
regression=# select 'a=>b, b=>1, c=>NULL'::hstore @> 'b=>1';
T
Teodor Sigaev 已提交
58 59 60 61 62
 ?column? 
----------
 t
(1 row)

63
    * hstore <@ hstore - contained operation, check if left operand is contained 
T
Teodor Sigaev 已提交
64
	 in right
T
Teodor Sigaev 已提交
65

66 67 68 69 70 71
(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!)

T
Teodor Sigaev 已提交
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
Functions

    * akeys(hstore) - returns all keys from hstore as array 

regression=# select akeys('a=>1,b=>2');
 akeys 
-------
 {a,b}

    * skeys(hstore) - returns all keys from hstore as strings 

regression=# select skeys('a=>1,b=>2');
 skeys 
-------
 a
 b

    * avals(hstore) - returns all values from hstore as array 

regression=# select avals('a=>1,b=>2');
 avals 
-------
 {1,2}

    * svals(hstore) - returns all values from hstore as strings 

regression=# select svals('a=>1,b=>2');
 svals 
-------
 1
 2

T
Teodor Sigaev 已提交
104 105
    * delete (hstore,text) - delete (key,value) from hstore if key matches 
	  argument. 
T
Teodor Sigaev 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119

regression=# select delete('a=>1,b=>2','b');
  delete  
----------
 "a"=>"1"

    * each(hstore) return (key, value) pairs 

regression=# select * from each('a=>1,b=>2');
 key | value 
-----+-------
 a   | 1
 b   | 2

120 121 122
    * exist (hstore,text) 
	* hstore ? text 
	  - returns 'true if key is exists in hstore and false otherwise. 
T
Teodor Sigaev 已提交
123

124 125 126 127
regression=# select exist('a=>1','a'), 'a=>1' ? 'a';
 exist | ?column? 
-------+----------
 t     | t
T
Teodor Sigaev 已提交
128

129
    * defined (hstore,text) - returns true if key is exists in hstore and 
T
Teodor Sigaev 已提交
130
	  its value is not NULL. 
T
Teodor Sigaev 已提交
131

132 133 134
regression=# select defined('a=>NULL','a');
 defined 
---------
T
Teodor Sigaev 已提交
135 136 137 138
 f

Indices

139
Module provides index support for '@>' and '?' operations.
T
Teodor Sigaev 已提交
140 141

create index hidx on testhstore using gist(h);
142
create index hidx on testhstore using gin(h);
T
Teodor Sigaev 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

Note

Use parenthesis in select below, because priority of 'is' is higher than that of '->'

select id from entrants where (info->'education_period') is not null;

Examples

    * add key 

update tt set h=h||'c=>3';

    * delete key 

update tt set h=delete(h,'k1');

    * Statistics

T
Teodor Sigaev 已提交
162 163 164
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.
T
Teodor Sigaev 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

          o simple example 

select * from each('aaa=>bq, b=>NULL, ""=>1 ');

          o using table 

select (each(h)).key, (each(h)).value into stat from testhstore ;

          o online stat 

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
T
Teodor Sigaev 已提交
188
...................