> 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/seg.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# F.39. seg — 線段或浮點數區間的資料型別 \#

### F.39. seg — 線段或浮點數區間的資料型別 [#](#seg) <a href="#seg" id="seg"></a>

[F.39.1. 設計理由](#seg-rationale)

[F.39.2. 語法](https://docs.postgresql.tw/19/appendixes/contrib/seg/seg-syntax.md#seg-syntax)

[F.39.3. 精確度](#seg-precision)

[F.39.4. 用法](https://docs.postgresql.tw/19/appendixes/contrib/seg/seg-usage.md#seg-usage)

[F.39.5. 注意事項](#seg-notes)

[F.39.6. 致謝](#seg-credits)

此模組實作資料型別 `seg`，用來表示線段或浮點數區間。 `seg` 可以表示區間端點的不確定性，因此特別適合用來表示實驗室量測值。

此模組被視為「受信任」，也就是說，具有目前資料庫 `CREATE` 權限的非超級使用者也可以安裝它。

#### F.39.1. 設計理由 [#](#seg-rationale) <a href="#seg-rationale" id="seg-rationale"></a>

量測值的幾何性質通常比數值連續體中的一個點更複雜。量測值通常是該連續體中邊界有些模糊的一段。量測結果之所以呈現為區間，一方面是因為不確定性與隨機性，另一方面也因為被量測的值本身可能就是一個表示某種條件的區間，例如某種蛋白質維持穩定的溫度範圍。

單憑常識就可以看出，將這類資料儲存為區間，似乎比儲存為一對數字更方便。實際上，在大多數應用中這樣做甚至更有效率。

沿著常識再往下想，邊界的模糊性意味著使用傳統數值資料型別會造成某種程度的資訊流失。想想看：你的儀器讀數為 6.50，你將這個讀數輸入資料庫。取出時會得到什麼？請看：

```

test=> select 6.50 :: float8 as "pH";
 pH
---
6.5
(1 row)
```

在量測的世界裡，6.50 與 6.5 並不相同，兩者的差異有時可能至關重要。實驗者通常會記下（並發表）他們信任的位數。6.50 實際上是一個模糊區間，包含在另一個更大、更模糊的區間 6.5 之內，兩者的中心點（大概）是它們唯一的共同特徵。我們絕對不希望這樣不同的資料項目看起來一模一樣。

結論是什麼？若有一種特殊的資料型別，能以可任意變動的精確度記錄區間的邊界，會很方便。所謂可變動，是指每個資料元素各自記錄自己的精確度。

請看：

```

test=> select '6.25 .. 6.50'::seg as "pH";
          pH
------------
6.25 .. 6.50
(1 row)
```

#### F.39.2. 語法 [#](#seg-syntax) <a href="#seg-syntax" id="seg-syntax"></a>

完整內容請見 [F.39.2. 語法](https://docs.postgresql.tw/19/appendixes/contrib/seg/seg-syntax.md#seg-syntax)。

#### F.39.3. 精確度 [#](#seg-precision) <a href="#seg-precision" id="seg-precision"></a>

`seg` 值在內部儲存為一對 32 位元浮點數。這表示有效位數超過 7 位的數字會被截斷。

有效位數在 7 位以下的數字會保留原本的精確度。也就是說，若你的查詢傳回 0.00，你可以確定結尾的零並不是格式化造成的假象：它們反映的是原始資料的精確度。前導零的個數不影響精確度：值 0.0067 被視為只有 2 位有效數字。

#### F.39.4. 用法 [#](#seg-usage) <a href="#seg-usage" id="seg-usage"></a>

完整內容請見 [F.39.4. 用法](https://docs.postgresql.tw/19/appendixes/contrib/seg/seg-usage.md#seg-usage)。

#### F.39.5. 注意事項 [#](#seg-notes) <a href="#seg-notes" id="seg-notes"></a>

使用範例請參閱迴歸測試 `sql/seg.sql`。

將 `(+-)` 轉換為一般範圍的機制，在判定邊界的有效位數時並不完全準確。例如，若產生的區間包含 10 的乘冪，它會在下界多加一位數字：

```

postgres=> select '10(+-)1'::seg as seg;
      seg
---------
9.0 .. 11             -- should be: 9 .. 11
```

R-tree 索引的效能在很大程度上取決於輸入值最初的順序。依 `seg` 欄位對輸入資料表排序可能非常有幫助；範例請參閱指令稿 `sort-segments.pl`。

#### F.39.6. 致謝 [#](#seg-credits) <a href="#seg-credits" id="seg-credits"></a>

原作者：Gene Selkov, Jr. `<selkovjr@mcs.anl.gov>`， Argonne National Laboratory 數學與電腦科學部門（Mathematics and Computer Science Division）。

我首先要感謝 Joe Hellerstein 教授 （<https://dsf.berkeley.edu/jmh/>）闡明了 GiST 的精髓（<http://gist.cs.berkeley.edu/>）。我也感謝所有過去與現在的 Postgres 開發者，讓我能夠建立自己的世界，並在其中不受打擾地生活。此外，我要向 Argonne 實驗室與美國能源部致上謝意，感謝他們多年來對我資料庫研究的忠實支持。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/seg.html)（原文版本：18.6；核對日期：2026-10-22）


---

# 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/seg.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.
