F.17. hstore

此模块实现hstore数据类型,以在单个 PostgreSQL 值中存储键/值对的集合。这在各种情况下很有用,例如具有许多很少检查的属性的行或半结构化数据。键和值只是文本字符串。

F.17.1. hstore 外部表示

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

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

对的 Sequences 并不重要(并且可能不会在输出中复制)。线对之间或=>符号周围的空格将被忽略。双引号键和值,包括空格,逗号,= s 或> s。要在键或值中包含双引号或反斜杠,请使用反斜杠对其进行转义。

hstore中的每个键都是唯一的。如果您声明具有重复密钥的hstore,则只会在hstore中存储一个,并且不能保证会保留哪个:

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

值(但不是键)可以是 SQL NULL。例如:

key => NULL

NULL关键字不区分大小写。用双引号将NULL视为普通字符串“ NULL”。

Note

请记住,hstore文本格式在用于 Importing 时,在任何必需的引号或转义之前*适用。如果要通过参数传递hstoreLiterals,则不需要其他处理。但是,如果将其作为带引号的 Literals 常量传递,则任何单引号字符和(取决于standard_conforming_strings配置参数的设置)反斜杠字符都必须正确转义。有关字符串常量的更 multiprocessing,请参见Section 4.1.2.1

在输出中,双引号始终围绕键和值,即使在并非绝对必要时也是如此。

F.17.2. hstore 运算符和函数

hstore模块提供的运算符显示在Table F.8中,功能显示在Table F.9中。

表 F.8.hstore个运算符

OperatorDescriptionExampleResult
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 s'a=>b, c=>d'::hstore || 'c=>x, d=>q'::hstore"a"=>"b", "c"=>"x", "d"=>"q"
hstore ? texthstore是否包含密钥?'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 ?| ARRAY['b','c']t
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 #= hstorerecord中的字段替换为hstore中的匹配值请参阅示例部分
%% hstorehstore转换为交替键和值的数组%% 'a=>foo, b=>bar'::hstore{a,foo,b,bar}
%# hstorehstore转换为二维键/值数组%# 'a=>foo, b=>bar'::hstore{{a,foo},{b,bar}}

Note

在 PostgreSQL 8.2 之前,包含运算符@><@分别称为@~。这些名称仍然可用,但已过时,最终将被删除。注意,旧名称与以前的约定相反,后跟核心几何数据类型!

表 F.9.hstore功能

FunctionReturn TypeDescriptionExampleResult
hstore(record) hstore从记录或行构造一个hstorehstore(ROW(1,2))f1=>1,f2=>2
hstore(text[])hstore从数组构造一个hstore,该数组可以是键/值数组,也可以是二维数组hstore(ARRAY['a','1','b','2']) || hstore(ARRAY[['c','3'],['d','4']])a=>1, b=>2, c=>3, d=>4
hstore(text[], text[])hstore从单独的键和值数组构造一个hstorehstore(ARRAY['a','b'], ARRAY['1','2'])"a"=>"1","b"=>"2"
hstore(text, text)hstore制作单项hstorehstore('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 texthstore的值作为一组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) jsonhstore作为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) jsonbhstore作为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) jsonhstore作为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) jsonbhstore作为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')关键值
-----+-------
一个
1 个
b
2
exist(hstore,text) booleanhstore是否包含密钥?exist('a=>1','a')t
defined(hstore,text) booleanhstore是否包含非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) recordhstore中的匹配值替换record中的字段请参见示例部分

Note

hstore值强制转换为json时,将使用函数hstore_to_json。同样,当将hstore值强制转换为jsonb时使用hstore_to_jsonb

Note

函数populate_record实际上是使用anyelement而不是record作为其第一个参数声明的,但是它将拒绝带有运行时错误的非记录类型。

F.17.3. Indexes

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值的排序 Sequences 不是特别有用,但是这些索引可能对等效查找有用。为=比较创建索引,如下所示:

CREATE INDEX hidx ON testhstore USING BTREE (h);

CREATE INDEX hidx ON testhstore USING HASH (h);

F.17.4. Examples

添加密钥,或使用新值更新现有密钥:

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转换为 sched 义的record类型:

CREATE TABLE test (col1 integer, col2 text, col3 text);

SELECT * FROM populate_record(null::test,
                              '"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)

F.17.5. Statistics

hstore类型由于其固有的自由度,可能包含许多不同的键。检查有效密钥是应用程序的任务。以下示例演示了几种用于检查密钥和获取统计信息的技术。

Simple example:

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

使用表格:

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

从 PostgreSQL 9.0 开始,hstore使用与以前版本不同的内部表示形式。这对转储/还原升级没有任何障碍,因为文本表示形式(在转储中使用)保持不变。

如果进行二进制升级,则通过使新代码识别旧格式的数据来保持向上兼容性。当处理尚未被新代码修改的数据时,这将对性能造成轻微的影响。通过执行UPDATE语句,可以强制升级表列中的所有值,如下所示:

UPDATE tablename SET hstorecol = hstorecol || '';

另一种方法是:

ALTER TABLE tablename ALTER hstorecol TYPE hstore USING hstorecol || '';

ALTER TABLE方法需要在表上具有排他锁,但不会导致旧版本的表膨胀。

F.17.7. Transforms

可以使用其他扩展来实现 PL/Perl 和 PL/Python 语言对hstore类型的转换。 PL/Perl 的 extensions 称为hstore_plperlhstore_plperlu,用于受信任和不受信任的 PL/Perl。如果安装这些转换并在创建函数时指定它们,则hstore值将 Map 到 Perl 哈希。 PL/Python 的 extensions 为hstore_plpythonuhstore_plpython2uhstore_plpython3u(有关 PL/Python 的命名约定,请参见Section 45.1)。如果使用它们,则hstore值将 Map 到 Python 字典。

F.17.8. Authors

Oleg Bartunov <[email protected]>,莫斯科,俄罗斯莫斯科大学

Teodor Sigaev <[email protected]>,莫斯科,Delta-Soft Ltd.,俄罗斯

英国 Andrew Gierth <[email protected]>的其他增强功能