> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/appendixes/contrib/hstore/hstore-ops-funcs.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# F.17.2. hstore 運算子與函式

## F.17.2. `hstore` 運算子與函式 [#](#hstore-ops-funcs) <a href="#hstore-ops-funcs" id="hstore-ops-funcs"></a>

`hstore` 模組提供的運算子列於[表 F.6](#hstore-op-table)，函式列於[表 F.7](#hstore-func-table)。

### 表 F.6. `hstore` 運算子 [#](#hstore-op-table) <a href="#hstore-op-table" id="hstore-op-table"></a>

| <p>運算子</p><p>說明</p><p>範例</p>                                                                                                                                                                                                 |                                                                                                                            |                                                                                                                    |   |                                                                             |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | - | --------------------------------------------------------------------------- |
| <p><code>hstore</code> <code>-></code> <code>text</code> → <code>text</code></p><p>傳回與指定鍵關聯的值；若不存在則傳回 <code>NULL</code>。</p><p><code>'a=>x, b=>y'::hstore -> 'a'</code> → <code>x</code></p>                                 |                                                                                                                            |                                                                                                                    |   |                                                                             |
| <p><code>hstore</code> <code>-></code> <code>text\[]</code> → <code>text\[]</code></p><p>傳回與指定鍵關聯的值；若不存在則傳回 <code>NULL</code>。</p><p><code>'a=>x, b=>y, c=>z'::hstore -> ARRAY\['c','a']</code> → <code>{"z","x"}</code></p> |                                                                                                                            |                                                                                                                    |   |                                                                             |
| <p><code>hstore</code> <code>                                                                                                                                                                                                |                                                                                                                            | </code> <code>hstore</code> → <code>hstore</code></p><p>串接兩個 <code>hstore</code>。</p><p><code>'a=>b, c=>d'::hstore |   | 'c=>x, d=>q'::hstore</code> → <code>"a"=>"b", "c"=>"x", "d"=>"q"</code></p> |
| <p><code>hstore</code> <code>?</code> <code>text</code> → <code>boolean</code></p><p><code>hstore</code> 是否包含該鍵？</p><p><code>'a=>1'::hstore ? 'a'</code> → <code>t</code></p>                                                |                                                                                                                            |                                                                                                                    |   |                                                                             |
| <p><code>hstore</code> <code>?&</code> <code>text\[]</code> → <code>boolean</code></p><p><code>hstore</code> 是否包含所有指定鍵？</p><p><code>'a=>1,b=>2'::hstore ?& ARRAY\['a','b']</code> → <code>t</code></p>                       |                                                                                                                            |                                                                                                                    |   |                                                                             |
| <p><code>hstore</code> <code>?                                                                                                                                                                                               | </code> <code>text\[]</code> → <code>boolean</code></p><p><code>hstore</code> 是否包含任何指定鍵？</p><p><code>'a=>1,b=>2'::hstore ? | ARRAY\['b','c']</code> → <code>t</code></p>                                                                        |   |                                                                             |
| <p><code>hstore</code> <code>@></code> <code>hstore</code> → <code>boolean</code></p><p>左運算元是否包含右運算元？</p><p><code>'a=>b, b=>1, c=>NULL'::hstore @> 'b=>1'</code> → <code>t</code></p>                                        |                                                                                                                            |                                                                                                                    |   |                                                                             |
| <p><code>hstore</code> <code><@</code> <code>hstore</code> → <code>boolean</code></p><p>左運算元是否包含於右運算元？</p><p><code>'a=>c'::hstore <@ 'a=>b, b=>1, c=>NULL'</code> → <code>f</code></p>                                       |                                                                                                                            |                                                                                                                    |   |                                                                             |
| <p><code>hstore</code> <code>-</code> <code>text</code> → <code>hstore</code></p><p>從左運算元刪除鍵。</p><p><code>'a=>1, b=>2, c=>3'::hstore - 'b'::text</code> → <code>"a"=>"1", "c"=>"3"</code></p>                                |                                                                                                                            |                                                                                                                    |   |                                                                             |
| <p><code>hstore</code> <code>-</code> <code>text\[]</code> → <code>hstore</code></p><p>從左運算元刪除鍵。</p><p><code>'a=>1, b=>2, c=>3'::hstore - ARRAY\['a','b']</code> → <code>"c"=>"3"</code></p>                                 |                                                                                                                            |                                                                                                                    |   |                                                                             |
| <p><code>hstore</code> <code>-</code> <code>hstore</code> → <code>hstore</code></p><p>從左運算元刪除與右運算元配對相符的配對。</p><p><code>'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore</code> → <code>"a"=>"1", "c"=>"3"</code></p>        |                                                                                                                            |                                                                                                                    |   |                                                                             |
| <p><code>anyelement</code> <code>#=</code> <code>hstore</code> → <code>anyelement</code></p><p>以 <code>hstore</code> 的相符值取代左運算元（必須是複合型別）的欄位。</p><p><code>ROW(1,3) #= 'f1=>11'::hstore</code> → <code>(11,3)</code></p>       |                                                                                                                            |                                                                                                                    |   |                                                                             |
| <p><code>%%</code> <code>hstore</code> → <code>text\[]</code></p><p>將 <code>hstore</code> 轉換為鍵和值交錯排列的陣列。</p><p><code>%% 'a=>foo, b=>bar'::hstore</code> → <code>{a,foo,b,bar}</code></p>                                     |                                                                                                                            |                                                                                                                    |   |                                                                             |
| <p><code>%#</code> <code>hstore</code> → <code>text\[]</code></p><p>將 <code>hstore</code> 轉換為二維鍵／值陣列。</p><p><code>%# 'a=>foo, b=>bar'::hstore</code> → <code>{{a,foo},{b,bar}}</code></p>                                    |                                                                                                                            |                                                                                                                    |   |                                                                             |

<br>

### 表 F.7. `hstore` 函式 [#](#hstore-func-table) <a href="#hstore-func-table" id="hstore-func-table"></a>

<table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><p>函式</p><p>說明</p><p>範例</p></td></tr><tr><td><p><code>hstore</code> ( <code>record</code> ) → <code>hstore</code></p><p>從記錄或資料列建構 <code>hstore</code>。</p><p><code>hstore(ROW(1,2))</code> → <code>"f1"=>"1", "f2"=>"2"</code></p></td></tr><tr><td><p><code>hstore</code> ( <code>text[]</code> ) → <code>hstore</code></p><p>從陣列建構 <code>hstore</code>；該陣列可以是鍵／值陣列或二維陣列。</p><p><code>hstore(ARRAY['a','1','b','2'])</code> → <code>"a"=>"1", "b"=>"2"</code></p><p><code>hstore(ARRAY[['c','3'],['d','4']])</code> → <code>"c"=>"3", "d"=>"4"</code></p></td></tr><tr><td><p><code>hstore</code> ( <code>text[]</code>, <code>text[]</code> ) → <code>hstore</code></p><p>從分離的鍵和值陣列建構 <code>hstore</code>。</p><p><code>hstore(ARRAY['a','b'], ARRAY['1','2'])</code> → <code>"a"=>"1", "b"=>"2"</code></p></td></tr><tr><td><p><code>hstore</code> ( <code>text</code>, <code>text</code> ) → <code>hstore</code></p><p>建立只含一個項目的 <code>hstore</code>。</p><p><code>hstore('a', 'b')</code> → <code>"a"=>"b"</code></p></td></tr><tr><td><p><code>akeys</code> ( <code>hstore</code> ) → <code>text[]</code></p><p>將 <code>hstore</code> 的鍵擷取為陣列。</p><p><code>akeys('a=>1,b=>2')</code> → <code>{a,b}</code></p></td></tr><tr><td><p><code>skeys</code> ( <code>hstore</code> ) → <code>setof text</code></p><p>將 <code>hstore</code> 的鍵擷取為集合。</p><p><code>skeys('a=>1,b=>2')</code> →</p><pre><code>a
b
</code></pre></td></tr><tr><td><p><code>avals</code> ( <code>hstore</code> ) → <code>text[]</code></p><p>將 <code>hstore</code> 的值擷取為陣列。</p><p><code>avals('a=>1,b=>2')</code> → <code>{1,2}</code></p></td></tr><tr><td><p><code>svals</code> ( <code>hstore</code> ) → <code>setof text</code></p><p>將 <code>hstore</code> 的值擷取為集合。</p><p><code>svals('a=>1,b=>2')</code> →</p><pre><code>1
2
</code></pre></td></tr><tr><td><p><code>hstore_to_array</code> ( <code>hstore</code> ) → <code>text[]</code></p><p>將 <code>hstore</code> 的鍵和值擷取為鍵和值交錯排列的陣列。</p><p><code>hstore_to_array('a=>1,b=>2')</code> → <code>{a,1,b,2}</code></p></td></tr><tr><td><p><code>hstore_to_matrix</code> ( <code>hstore</code> ) → <code>text[]</code></p><p>將 <code>hstore</code> 的鍵和值擷取為二維陣列。</p><p><code>hstore_to_matrix('a=>1,b=>2')</code> → <code>{{a,1},{b,2}}</code></p></td></tr><tr><td><p><code>hstore_to_json</code> ( <code>hstore</code> ) → <code>json</code></p><p>將 <code>hstore</code> 轉換為 <code>json</code> 值，並將所有非 NULL 值轉換為 JSON 字串。</p><p>將 <code>hstore</code> 值轉型為 <code>json</code> 時，會隱含使用此函式。</p><p><code>hstore_to_json('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4')</code> → <code>{"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"}</code></p></td></tr><tr><td><p><code>hstore_to_jsonb</code> ( <code>hstore</code> ) → <code>jsonb</code></p><p>將 <code>hstore</code> 轉換為 <code>jsonb</code> 值，並將所有非 NULL 值轉換為 JSON 字串。</p><p>將 <code>hstore</code> 值轉型為 <code>jsonb</code> 時，會隱含使用此函式。</p><p><code>hstore_to_jsonb('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4')</code> → <code>{"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"}</code></p></td></tr><tr><td><p><code>hstore_to_json_loose</code> ( <code>hstore</code> ) → <code>json</code></p><p>將 <code>hstore</code> 轉換為 <code>json</code> 值，但會嘗試區分數值與布林值，讓它們在 JSON 中不加引號。</p><p><code>hstore_to_json_loose('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4')</code> → <code>{"a key": 1, "b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4}</code></p></td></tr><tr><td><p><code>hstore_to_jsonb_loose</code> ( <code>hstore</code> ) → <code>jsonb</code></p><p>將 <code>hstore</code> 轉換為 <code>jsonb</code> 值，但會嘗試區分數值與布林值，讓它們在 JSON 中不加引號。</p><p><code>hstore_to_jsonb_loose('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4')</code> → <code>{"a key": 1, "b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4}</code></p></td></tr><tr><td><p><code>slice</code> ( <code>hstore</code>, <code>text[]</code> ) → <code>hstore</code></p><p>擷取只含指定鍵的 <code>hstore</code> 子集。</p><p><code>slice('a=>1,b=>2,c=>3'::hstore, ARRAY['b','c','x'])</code> → <code>"b"=>"2", "c"=>"3"</code></p></td></tr><tr><td><p><code>each</code> ( <code>hstore</code> ) → <code>setof record</code> ( <em><code>key</code></em> <code>text</code>, <em><code>value</code></em> <code>text</code> )</p><p>將 <code>hstore</code> 的鍵和值擷取為一組記錄。</p><p><code>select * from each('a=>1,b=>2')</code> →</p><pre><code> key | value
-----+-------
 a   | 1
 b   | 2
</code></pre></td></tr><tr><td><p><code>exist</code> ( <code>hstore</code>, <code>text</code> ) → <code>boolean</code></p><p><code>hstore</code> 是否包含該鍵？</p><p><code>exist('a=>1', 'a')</code> → <code>t</code></p></td></tr><tr><td><p><code>defined</code> ( <code>hstore</code>, <code>text</code> ) → <code>boolean</code></p><p><code>hstore</code> 是否包含該鍵的非 <code>NULL</code> 值？</p><p><code>defined('a=>NULL', 'a')</code> → <code>f</code></p></td></tr><tr><td><p><code>delete</code> ( <code>hstore</code>, <code>text</code> ) → <code>hstore</code></p><p>刪除鍵相符的配對。</p><p><code>delete('a=>1,b=>2', 'b')</code> → <code>"a"=>"1"</code></p></td></tr><tr><td><p><code>delete</code> ( <code>hstore</code>, <code>text[]</code> ) → <code>hstore</code></p><p>刪除鍵相符的配對。</p><p><code>delete('a=>1,b=>2,c=>3', ARRAY['a','b'])</code> → <code>"c"=>"3"</code></p></td></tr><tr><td><p><code>delete</code> ( <code>hstore</code>, <code>hstore</code> ) → <code>hstore</code></p><p>刪除與第二個引數相符的配對。</p><p><code>delete('a=>1,b=>2', 'a=>4,b=>2'::hstore)</code> → <code>"a"=>"1"</code></p></td></tr><tr><td><p><code>populate_record</code> ( <code>anyelement</code>, <code>hstore</code> ) → <code>anyelement</code></p><p>以 <code>hstore</code> 的相符值取代左運算元（必須是複合型別）的欄位。</p><p><code>populate_record(ROW(1,2), 'f1=>42'::hstore)</code> → <code>(42,2)</code></p></td></tr></tbody></table>

<br>

除了這些運算子與函式之外，`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. hstore — hstore 鍵／值資料型別](https://docs.postgresql.tw/19/appendixes/contrib/hstore.md#hstore)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/hstore.html)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/appendixes/contrib/hstore/hstore-ops-funcs.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
