hstore

这个模块实现了hstore数据类型用来在一个单一UDB-TX值中存储键值对。这在很多情景下都有用,例如带有很多很少被检查的属性的行或者半结构化数据。键和值都是简单的文本字符串。

hstore 外部表示

一个hstore的文本表示用于输入和输出,包括零个或者多个由逗号分隔的*key* => *value*对。一些例子:

k => v
foo => bar, baz => whatever
"1-a" => "anything at all"

键值对的顺序没有意义(并且在输出时也不会重现)。键值对之间或者=>号周围的空白会被忽略。双引号内的键和值可以包括空白、逗号、=>。要在一个键或值中包括一个双引号或一个反斜线,用一个反斜线对它转义。

一个hstore中的每一个键是唯一的。如果你声明了一个有重复键的hstore,只有一个会被存储在hstore中并且无法保证哪一个将被保留:

SELECT 'a=>1,a=>2'::hstore;
 hstore
 ----------
 "a"=>"1"

一个值(但不是一个键)能够是一个 SQL NULL。例如:

key => NULL

NULL关键词是大小写不敏感的。将NULL放在双引号中可以将它当作一个普通的字符串“NULL”。

注意

记住当hstore文本格式当被用于输入时,它应用在任何必须的引用或转义之前。如果你通过一个参数传递一个hstore文字,那么不需要额外的处理。但是如果你将它作为一个引用的文字常数,那么任何单引号字符以及(取决于standard_conforming_strings配置参数的设置)反斜线字符需要被正确地转义。

在输出时,双引号总是围绕着键和值,即使这样做不是绝对必要。

hstore 操作符和函数

hstore模块所提供的操作符显示在表 F.7中,函数在表 F.8中。

表 F.7. hstore 操作符

操作符 描述 例子 结果
hstore -> text 为键得到值(不存在则是NULL 'a=>x, b=>y'::hstore -> 'a' x
hstore -> text[] 为多个键得到值(不存在则是NULL 'a=>x, b=>y, c=>z'::hstore -> ARRAY['c','a'] {"z","x"}
hstore | | hstore 串接hstore
hstore ? text hstore是否包含键? 'a=>1'::hstore ? 'a' t
hstore ?& text[] hstore是否包含所有指定的键? 'a=>1,b=>2'::hstore ?& ARRAY['a','b'] t
hstore ? | text[] hstore是否包含任何指定的键? `’a=>1,b=>2’::hstore ?
hstore @> hstore 左操作数是否包含右操作数? 'a=>b, b=>1, c=>NULL'::hstore @> 'b=>1' t
hstore <@ hstore 左操作数是否被包含在右操作数中? 'a=>c'::hstore <@ 'a=>b, b=>1, c=>NULL' f
hstore - text 从左操作数中删除键 'a=>1, b=>2, c=>3'::hstore - 'b'::text "a"=>"1", "c"=>"3"
hstore - text[] 从左操作数中删除多个键 'a=>1, b=>2, c=>3'::hstore - ARRAY['a','b'] "c"=>"3"
hstore - hstore 从左操作数中删除匹配的对 'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore "a"=>"1", "c"=>"3"
record #= hstore 用来自hstore的匹配值替换record中的域 见示例小节
%% hstore hstore转换成键和值交替出现的数组 %% 'a=>foo, b=>bar'::hstore {a,foo,b,bar}
%# hstore hstore转换成二维的键值数组 %# 'a=>foo, b=>bar'::hstore {{a,foo},{b,bar}}

表 F.8. hstore 函数

函数 返回类型 描述 例子 结果
hstore(record) hstore 从一个记录或行构造一个hstore hstore(ROW(1,2)) f1=>1,f2=>2
hstore(text[]) hstore 从一个数组构造一个hstore,数组可以是一个键值数组或者一个二维数组 `hstore(ARRAY[‘a’,’1’,’b’,’2’])
hstore(text[], text[]) hstore 从独立的键和值数组构建一个hstore hstore(ARRAY['a','b'], ARRAY['1','2']) "a"=>"1","b"=>"2"
hstore(text, text) hstore 构造单一项的hstore hstore('a', 'b') "a"=>"b"
akeys(hstore) text[] 取得hstore的键作为一个数组 akeys('a=>1,b=>2') {a,b}
skeys(hstore) setof text 取得hstore的键作为一个集合 skeys('a=>1,b=>2') a b
avals(hstore) text[] 取得hstore的值作为一个数组 avals('a=>1,b=>2') {1,2}
svals(hstore) setof text 取得hstore的值作为一个集合 svals('a=>1,b=>2') 1 2
hstore_to_array(hstore) text[] 取得hstore的键和值作为一个键和值交替出现的数组 hstore_to_array('a=>1,b=>2') {a,1,b,2}
hstore_to_matrix(hstore) text[] 取得hstore的键和值作为一个二维的数组 hstore_to_matrix('a=>1,b=>2') {{a,1},{b,2}}
hstore_to_json(hstore) json 取得hstore作为一个json值,把所有非空值转换为 JSON 字符串 hstore_to_json('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4') {"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"}
hstore_to_jsonb(hstore) jsonb 取得hstore作为一个jsonb值,把把所有非空值转换为 JSON 字符串 hstore_to_jsonb('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4') {"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"}
hstore_to_json_loose(hstore) json 取得hstore作为一个json值,但是尝试区分数字值和布尔值这样它们在 JSON 中无需引用 hstore_to_json_loose('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4') {"a key": 1, "b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4}
hstore_to_jsonb_loose(hstore) jsonb 取得hstore作为一个jsonb值,但是尝试区分数字值和布尔值这样它们在 JSON 中无需引用 hstore_to_jsonb_loose('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4') {"a key": 1, "b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4}
slice(hstore, text[]) hstore 从一个hstore抽取一个子集 slice('a=>1,b=>2,c=>3'::hstore, ARRAY['b','c','x']) "b"=>"2", "c"=>"3"
each(hstore) setof(key text, value text) 取得hstore的键和值作为一个集合 select * from each('a=>1,b=>2') `key
exist(hstore,text) boolean hstore是否包含键? exist('a=>1','a') t
defined(hstore,text) boolean hstore是否为键包含非NULL值? defined('a=>NULL','a') f
delete(hstore,text) hstore 删除匹配键的对 delete('a=>1,b=>2','b') "a"=>"1"
delete(hstore,text[]) hstore 删除匹配多个键的多个对 delete('a=>1,b=>2,c=>3',ARRAY['a','b']) "c"=>"3"
delete(hstore,hstore) hstore 删除匹配第二个参数的对 delete('a=>1,b=>2','a=>4,b=>2'::hstore) "a"=>"1"
populate_record(record,hstore) record 用来自hstore的匹配值替换record中的域 见示例小节

注意

当一个hstore值被造型成json时,将使用函数hstore_to_json。同样地,当一个hstore值被造型成jsonb时,将使用函数hstore_to_jsonb

索引

hstore有对@>??&?|操作符的 GiST 和 GIN 索引支持。例如:

CREATE INDEX hidx ON testhstore USING GIST (h);
CREATE INDEX hidx ON testhstore USING GIN (h);

hstore也为=操作符支持btreehash索引。这允许hstore列被声明为UNIQUE或者被使用在GROUP BYORDER BYDISTINCT表达式中。hstore值的排序顺序不是特别有用,但是这些索引可能对等值查找有用。为=比较创建以下索引:

CREATE INDEX hidx ON testhstore USING BTREE (h);
CREATE INDEX hidx ON testhstore USING HASH (h);

例子

增加一个键,或者用一个新值更新一个现有的键:

UPDATE tab SET h = h || hstore('c', '3');

删除一个键:

UPDATE tab SET h = delete(h, 'k1');

将一个record转换成一个hstore

CREATE TABLE test (col1 integer, col2 text, col3 text);
INSERT INTO test VALUES (123, 'foo', 'bar');
SELECT hstore(t) FROM test AS t;
                   hstore                    
---------------------------------------------
 "col1"=>"123", "col2"=>"foo", "col3"=>"bar"
(1 row)

将一个hstore转换成一个预定义的record类型:

CREATE TABLE test (col1 integer, col2 text, col3 text);
SELECT * FROM populate_record(null::test1,
                              '"col1"=>"456", "col2"=>"zzz"');
 col1 | col2 | col3 
------+------+------
  456 | zzz  | 
(1 row)

用来自于一个hstore的值修改一个现有的记录:

CREATE TABLE test (col1 integer, col2 text, col3 text);
INSERT INTO test VALUES (123, 'foo', 'bar');
SELECT (r).* FROM (SELECT t #= '"col3"=>"baz"' AS r FROM test t) s;
 col1 | col2 | col3 
------+------+------
  123 | foo  | baz
(1 row)

统计

由于hstore类型本质的宽大性,它能够包含一些不同的键。检查合法键是应用的任务。下列例子验证了用于检查键以及获得统计的一些技术。

简单例子:

SELECT * FROM each('aaa=>bq, b=>NULL, ""=>1');

使用一个表:

SELECT (each(h)).key, (each(h)).value INTO stat FROM testhstore;

在线统计:

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
...................