Redrock Postgres 搜索 英文
版本: 9.3 / 9.4 / 9.5 / 9.6 / 10 / 11 / 12 / 13 / 14 / 15 / 16 / 17

F.17. hstore — hstore 键值数据类型 #

F.17.1. hstore 外部表示
F.17.2. hstore 运算符和函数
F.17.3. 索引
F.17.4. 示例
F.17.5. 统计信息
F.17.6. 兼容性
F.17.7. 转换
F.17.8. 创作者

此模块实现了 hstore 数据类型,用于存储单个 PostgreSQL 值中的一组键/值对。这在各种场景中都十分有用,例如具有大量属性但很少检查的行,或者半结构化数据。键和值只是简单的文本字符串。

这个模块被认为是 受信任的,也就是说,它可以由在当前数据库中有 CREATE 权限的非超级用户安装。

F.17.1. 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 配置参数的设置)和反斜杠字符需要进行正确的转义。有关处理字符串常量的更多信息,请参阅 第 4.1.2.1 节

在输出时,双引号始终包围键和值,即使严格来说没有必要。

F.17.2. hstore 运算符和函数 #

hstore 模块提供的运算符显示在 表 F.6 中,函数在 表 F.7 中。

表 F.6 hstore 运算符

运算符

说明

示例

hstore -> texttext

返回与给定键关联的值,如果没有则返回 NULL

'a=>x, b=>y'::hstore -> 'a'x

hstore -> text[]text[]

返回与给定键关联的值,如果没有则返回 NULL

'a=>x, b=>y, c=>z'::hstore -> ARRAY['c','a']{"z","x"}

hstore || hstorehstore

连接两个 hstore

'a=>b, c=>d'::hstore || 'c=>x, d=>q'::hstore"a"=>"b", "c"=>"x", "d"=>"q"

hstore ? textboolean

是否 hstore 包含键?

'a=>1'::hstore ? 'a't

hstore ?& text[]boolean

是否 hstore 包含所有指定键?

'a=>1,b=>2'::hstore ?& ARRAY['a','b']t

hstore ?| text[]boolean

是否 hstore 包含任何指定键?

'a=>1,b=>2'::hstore ?| ARRAY['b','c']t

hstore @> hstoreboolean

左操作数是否包含右操作数?

'a=>b, b=>1, c=>NULL'::hstore @> 'b=>1't

hstore <@ hstoreboolean

左操作数是否包含在右操作数中?

'a=>c'::hstore <@ 'a=>b, b=>1, c=>NULL'f

hstore - texthstore

从左操作数中删除键。

'a=>1, b=>2, c=>3'::hstore - 'b'::text"a"=>"1", "c"=>"3"

hstore - text[]hstore

从左操作数中删除键。

'a=>1, b=>2, c=>3'::hstore - ARRAY['a','b']"c"=>"3"

hstore - hstorehstore

从左操作数中删除与右操作数中键值对匹配的键值对。

'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore"a"=>"1", "c"=>"3"

anyelement #= hstoreanyelement

使用 hstore 中匹配的值替换左操作数(必须是一个复合类型)中的字段。

ROW(1,3) #= 'f1=>11'::hstore(11,3)

%% hstoretext[]

hstore 转换为一个交替存储键和值的数组。

%% 'a=>foo, b=>bar'::hstore{a,foo,b,bar}

%# hstoretext[]

hstore 转换为一个二维键值数组。

%# 'a=>foo, b=>bar'::hstore{{a,foo},{b,bar}}


表 F.7. hstore 函数

函数

说明

示例

hstore ( record ) → hstore

使用记录或行构造一个 hstore

hstore(ROW(1,2))"f1"=>"1", "f2"=>"2"

hstore ( text[] ) → hstore

使用数组构造一个 hstore,数组可以是键值数组或二维数组。

hstore(ARRAY['a','1','b','2'])"a"=>"1", "b"=>"2"

hstore(ARRAY[['c','3'],['d','4']])"c"=>"3", "d"=>"4"

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 值转为 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 值转为 jsonb 时,隐式地使用了此函数。

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 record ( key text, value text )

hstore 的键和值作为一组记录进行提取。

select * from each('a=>1,b=>2')

 key | value
-----+-------
 a   | 1
 b   | 2

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"

deletehstoretext[])→ hstore

删除具有匹配键值对。

delete('a=>1,b=>2,c=>3', ARRAY['a','b'])"c"=>"3"

deletehstorehstore)→ hstore

删除与第二个参数中相匹配的键值对。

delete('a=>1,b=>2', 'a=>4,b=>2'::hstore)"a"=>"1"

populate_recordanyelementhstore)→ anyelement

使用 hstore 中匹配的值替换左操作数(必须是一个复合类型)中的字段。

populate_record(ROW(1,2), 'f1=>42'::hstore)(42,2)


除了这些操作符和函数外,hstore 类型的变量可以进行下标引用,从而使它们类似于关联数组。只能指定 text 类型的单一引用下标;它被解释为一个键,并获取或存储相应的键值。例如,

CREATE TABLE mytable (h hstore);
INSERT INTO mytable VALUES ('a=>b, c=>d');
SELECT h['a'] FROM mytable;
 h
---
 b
(1 row)

UPDATE mytable SET h['c'] = 'new';
SELECT h FROM mytable;
          h
----------------------
 "a"=>"b", "c"=>"new"
(1 row)

如果引用下标为 NULL 或该键在 hstore 中不存在,则引用下标获取返回 NULL。(因此,引用下标获取与 -> 操作符没有太大不同。)如果引用下标为 NULL,则引用下标更新将失败;否则,它将替换该键的值,如果该键已存在,则会向 hstore 添加一个条目。

F.17.3 索引 #

hstore 具有对 @>??&?| 运算符的 GiST 和 GIN 索引支持。例如,

CREATE INDEX hidx ON testhstore USING GIST (h);

CREATE INDEX hidx ON testhstore USING GIN (h);

gist_hstore_ops GiST 操作类将一组键/值对近似为位图签名。它的可选整数参数 siglen 确定以字节为单位的签名长度。默认长度为 16 个字节。有效的签名长度值介于 1 到 2024 个字节之间。更长的签名会导致更精确的搜索(扫描更少的索引和更少的堆页),但会产生更大的索引。

使用 32 个字节的签名长度创建此类索引的示例

CREATE INDEX hidx ON testhstore USING GIST (h gist_hstore_ops(siglen=32));

hstore 还可以支持 btreehash 索引,用于 = 运算符。这允许将 hstore 列声明为 UNIQUE,或将其用于 GROUP BYORDER BYDISTINCT 表达式。 hstore 值的排序顺序不是特别有用,但这些索引可能会对等值查询有用。按如下方式创建 = 比较的索引

CREATE INDEX hidx ON testhstore USING BTREE (h);

CREATE INDEX hidx ON testhstore USING HASH (h);

F.17.4. 示例 #

添加一个键,或使用新值更新一个现有键

UPDATE tab SET h['c'] = '3';

执行相同操作的另一种方法是

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

如果在一次操作中要添加或更改多个键,则连接方法比下标更有效

UPDATE tab SET h = h || hstore(array['q', 'w'], array['11', '12']);

删除一个键

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::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. 统计 #

由于 hstore 类型的内在宽容性,它可以包含很多不同的键。检查有效键是应用程序的任务。以下示例演示了几种检查键和获取统计信息的技术。

简单示例

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

使用一个表

CREATE TABLE stat AS SELECT (each(h)).key, (each(h)).value 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
...................

F.17.6. 兼容性 #

从 PostgreSQL 9.0 开始,hstore 使用不同于以前版本的内部表示。由于文本表示(用于转储中)保持不变,因此这对转储/恢复升级构不成障碍。

在二进制升级的情况下,新代码识别旧格式数据就可以保持向上兼容性。在处理尚未被新代码修改的数据时,这将导致轻微的性能损失。可以通过执行以下 UPDATE 语句强制对表列中的所有值进行升级

UPDATE tablename SET hstorecol = hstorecol || '';

另一种方法是

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

ALTER TABLE 方法需要对表进行 ACCESS EXCLUSIVE 锁定,但不会导致表中出现旧行版本而导致表膨胀。

F.17.7. 转换 #

还有其他扩展可实现 PL/Perl 和 PL/Python 语言的 hstore 类型的转换。PL/Perl 的扩展名为 hstore_plperlhstore_plperlu(分别用于受信任和不受信任的 PL/Perl)。如果您安装了这些转换并在创建函数时指定它们,hstore 值将映射到 Perl 哈希。PL/Python 的扩展名为 hstore_plpython3u。如果您使用它,hstore 值将映射到 Python 词典。

警告

强烈建议在与 hstore 相同的架构中安装转换扩展。否则,如果转换扩展的架构包含由恶意用户定义的对象,在安装时存在安全隐患。

F.17.8. 作者 #

Oleg Bartunov ,俄罗斯莫斯科州,莫斯科大学

Teodor Sigaev ,俄罗斯莫斯科州,Delta-Soft Ltd.

Andrew Gierth ,英国,提供了其他增强功能