On this page
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 时,在任何必需的引号或转义之前*适用。如果要通过参数传递hstore
Literals,则不需要其他处理。但是,如果将其作为带引号的 Literals 常量传递,则任何单引号字符和(取决于standard_conforming_strings
配置参数的设置)反斜杠字符都必须正确转义。有关字符串常量的更 multiprocessing,请参见Section 4.1.2.1。
在输出中,双引号始终围绕键和值,即使在并非绝对必要时也是如此。
F.17.2. hstore 运算符和函数
hstore
模块提供的运算符显示在Table F.8中,功能显示在Table F.9中。
表 F.8.hstore
个运算符
Operator | Description | Example | Result |
---|---|---|---|
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 ? 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 ?| 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 #= hstore |
将record 中的字段替换为hstore 中的匹配值 |
请参阅示例部分 | |
%% hstore |
将hstore 转换为交替键和值的数组 |
%% 'a=>foo, b=>bar'::hstore |
{a,foo,b,bar} |
%# hstore |
将hstore 转换为二维键/值数组 |
%# 'a=>foo, b=>bar'::hstore |
{{a,foo},{b,bar}} |
Note
在 PostgreSQL 8.2 之前,包含运算符@>
和<@
分别称为@
和~
。这些名称仍然可用,但已过时,最终将被删除。注意,旧名称与以前的约定相反,后跟核心几何数据类型!
表 F.9.hstore
功能
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
还支持=
运算符的btree
或hash
索引。这允许将hstore
列声明为UNIQUE
或在GROUP BY
,ORDER BY
或DISTINCT
表达式中使用。 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_plperl
和hstore_plperlu
,用于受信任和不受信任的 PL/Perl。如果安装这些转换并在创建函数时指定它们,则hstore
值将 Map 到 Perl 哈希。 PL/Python 的 extensions 为hstore_plpythonu
,hstore_plpython2u
和hstore_plpython3u
(有关 PL/Python 的命名约定,请参见Section 45.1)。如果使用它们,则hstore
值将 Map 到 Python 字典。
F.17.8. Authors
Oleg Bartunov <oleg@sai.msu.su>
,莫斯科,俄罗斯莫斯科大学
Teodor Sigaev <teodor@sigaev.ru>
,莫斯科,Delta-Soft Ltd.,俄罗斯
英国 Andrew Gierth <andrew@tao11.riddles.org.uk>
的其他增强功能