PostgreSQL中的hstore模块

之前已经介绍过hstore模块的作用和操作符。这次我们继续了解此模块的其他内容。

hstore函数

这里我们列举一些hstore模块提供的函数。

函数

返回值

描述

例子

hstore(record)

hstore

从一个记录或行中创建hstore



见图1

hstore(text[])

hstore

从数组中创建hstore

hstore(text[], text[])

hstore

从不同的键值数组中创建hstore

hstore(text, text)

hstore

创建单一项目hstore



见图2

akeys(hstore)

text[]

取出hstore的键组成一个数组

skeys(hstore)

setof text

取出hstore的键组成一个集合

avals(hstore)

text[]

取出hstore的值组成一个数组



见图3

svals(hstore)

setof text

取出hstore的值组成一个集合

slice(hstore, text[])

hstore

提取一个hstore的一个子集

each(hstore)

setof(key text, value text)

取出hstore的键和值组成一个集合



见图4

exist(hstore,text)

boolean

hstore是否包含key

defined(hstore,text)

boolean

hstore是否包含key上非空值

delete(hstore,text)

hstore

删除key值匹配的一个键值对



见图5

delete(hstore,text[])

hstore

删除key值匹配的多个键值对

delete(hstore,hstore)

hstore

删除匹配第二个参数的键值对

我们以下图片来展示相关函数的操作结果:

PostgreSQL中的hstore模块

图1

PostgreSQL中的hstore模块

图2

PostgreSQL中的hstore模块

图3

PostgreSQL中的hstore模块

图4

PostgreSQL中的hstore模块

图5

索引支持

hstore@>、?、?&和?|操作符提供GiSTGIN索引。

对于 = 操作符,hstore也支持btree或者哈希索引,这允许hstore列被声明为UNIQUE,或者被用于GROUP BY, ORDER BY,或者DISTINCT表达式中。hstore值的排序顺序不是特别有用,但是这些索引可能对等值查询来说很有用。

hstore类型由于其内在的自由度,可能会包含很多不同的键。而检查有效的键则是应用程序的任务。