pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。
此模块实现了hstore数据类型,用于在单个 PostgreSQL值中存储一组键/值对。这在多种场景中 都很有用,例如属性很多但很少查看的行,或者半结构化数据。键和值都只是文 本字符串。
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 节。
在输出时,即使严格来说并非必需,键和值也总是带有双引号。
hstore 操作符和函数hstore模块提供的操作符见表 F.8, 函数见表 F.9。
表 F.8. hstore 操作符
| Operator | Description | Example | Result |
|---|---|---|---|
hstore -> text |
get value for key (NULL if not present) |
'a=>x, b=>y'::hstore -> 'a' |
x |
hstore -> text[] |
get values for keys (NULL if not present) |
'a=>x, b=>y, c=>z'::hstore -> ARRAY['c','a'] |
{"z","x"} |
text => text |
make single-pair hstore |
'a' => 'b' |
"a"=>"b" |
hstore || hstore |
concatenate hstores |
'a=>b, c=>d'::hstore || 'c=>x, d=>q'::hstore |
"a"=>"b", "c"=>"x", "d"=>"q" |
hstore ? text |
does hstore contain key? |
'a=>1'::hstore ? 'a' |
t |
hstore ?& text[] |
does hstore contain all specified keys? |
'a=>1,b=>2'::hstore ?& ARRAY['a','b'] |
t |
hstore ?| text[] |
does hstore contain any of the specified keys? |
'a=>1,b=>2'::hstore ?| ARRAY['b','c'] |
t |
hstore @> hstore |
does left operand contain right? | 'a=>b, b=>1, c=>NULL'::hstore @> 'b=>1' |
t |
hstore <@ hstore |
is left operand contained in right? | 'a=>c'::hstore <@ 'a=>b, b=>1, c=>NULL' |
f |
hstore - text |
delete key from left operand | 'a=>1, b=>2, c=>3'::hstore - 'b'::text |
"a"=>"1", "c"=>"3" |
hstore - text[] |
delete keys from left operand | 'a=>1, b=>2, c=>3'::hstore - ARRAY['a','b'] |
"c"=>"3" |
hstore - hstore |
delete matching pairs from left operand | 'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore |
"a"=>"1", "c"=>"3" |
record #= hstore |
replace fields in record with matching values from hstore |
see Examples section | |
%% hstore |
convert hstore to array of alternating keys and values |
%% 'a=>foo, b=>bar'::hstore |
{a,foo,b,bar} |
%# hstore |
convert hstore to two-dimensional key/value array |
%# 'a=>foo, b=>bar'::hstore |
{{a,foo},{b,bar}} |
在 PostgreSQL 8.2 之前,包含操作符 @> 和 <@ 分别称为 @ 和 ~。这些名称仍然可用,但已弃用,最终将被删除。请注意,旧名称与核心几何数据类型以前采用的约定正好相反!
=> 操作符已弃用,未来的发行版中可能会将其删除。 请改用 hstore(text, text) 函数。
表 F.9. hstore Functions
| Function | Return Type | Description | Example | Result |
|---|---|---|---|---|
hstore(record) |
hstore |
construct an hstore from a record or row |
hstore(ROW(1,2)) |
f1=>1,f2=>2 |
hstore(text[]) |
hstore |
construct an hstore from an array, which may be either a key/value array, or a two-dimensional array |
hstore(ARRAY['a','1','b','2']) || hstore(ARRAY[['c','3'],['d','4']]) |
a=>1, b=>2, c=>3, d=>4 |
hstore(text[], text[]) |
hstore |
construct an hstore from separate key and value arrays |
hstore(ARRAY['a','b'], ARRAY['1','2']) |
"a"=>"1","b"=>"2" |
hstore(text, text) |
hstore |
make single-item hstore |
hstore('a', 'b') |
"a"=>"b" |
akeys(hstore) |
text[] |
get hstore's keys as an array |
akeys('a=>1,b=>2') |
{a,b} |
skeys(hstore) |
setof text |
get hstore's keys as a set |
skeys('a=>1,b=>2') |
a b |
avals(hstore) |
text[] |
get hstore's values as an array |
avals('a=>1,b=>2') |
{1,2} |
svals(hstore) |
setof text |
get hstore's values as a set |
svals('a=>1,b=>2') |
1 2 |
hstore_to_array(hstore) |
text[] |
get hstore's keys and values as an array of alternating keys and values |
hstore_to_array('a=>1,b=>2') |
{a,1,b,2} |
hstore_to_matrix(hstore) |
text[] |
get hstore's keys and values as a two-dimensional array |
hstore_to_matrix('a=>1,b=>2') |
{{a,1},{b,2}} |
slice(hstore, text[]) |
hstore |
extract a subset of an hstore |
slice('a=>1,b=>2,c=>3'::hstore, ARRAY['b','c','x']) |
"b"=>"2", "c"=>"3" |
each(hstore) |
setof(key text, value text) |
get hstore's keys and values as a set |
select * from each('a=>1,b=>2') |
key | value -----+------- a | 1 b | 2 |
exist(hstore,text) |
boolean |
does hstore contain key? |
exist('a=>1','a') |
t |
defined(hstore,text) |
boolean |
does hstore contain non-NULL value for key? |
defined('a=>NULL','a') |
f |
delete(hstore,text) |
hstore |
delete pair with matching key | delete('a=>1,b=>2','b') |
"a"=>"1" |
delete(hstore,text[]) |
hstore |
delete pairs with matching keys | delete('a=>1,b=>2,c=>3',ARRAY['a','b']) |
"c"=>"3" |
delete(hstore,hstore) |
hstore |
delete pairs matching those in the second argument | delete('a=>1,b=>2','a=>4,b=>2'::hstore) |
"a"=>"1" |
populate_record(record,hstore) |
record |
replace fields in record with matching values from hstore |
see Examples section |
populate_record函数的第一个参数实际声明为anyelement,而不是record,但它会在运行时出错并拒绝非记录类型。
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值的排序顺序本身并没有特别实用的意义,但这些索引可能适合 用于等值查找。可按如下方式为=比较创建索引:
CREATE INDEX hidx ON testhstore USING BTREE (h); CREATE INDEX hidx ON testhstore USING HASH (h);
添加一个键,或用新值更新现有键:
UPDATE tab SET h = h || ('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::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)
由于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
...................
自 PostgreSQL 9.0 起,hstore使用了与更早版本不同的内部表 示。这不会妨碍转储/恢复升级,因为文本表示(即转储中使用的表示)没有改变。
在进行二进制升级时,通过让新代码识别旧格式数据,维持了向上兼容性。这会在 处理尚未被新代码修改过的数据时带来轻微的性能损失。可以通过执行如下 UPDATE语句,强制升级表列中的所有值:
UPDATE tablename SET hstorecol = hstorecol || '';
另一种方式是:
ALTER TABLE tablename ALTER hstorecol TYPE hstore USING hstorecol || '';
使用ALTER TABLE方法需要对表加 独占锁,但不会因旧行版本而导致表膨胀。
Oleg Bartunov <oleg@sai.msu.su>,俄罗斯莫斯科,莫斯科大学
Teodor Sigaev <teodor@sigaev.ru>,俄罗斯莫斯科,Delta-Soft Ltd.
Andrew Gierth <andrew@tao11.riddles.org.uk>,英国,对本模块作 了额外增强
译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。