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

# F.18. intagg — 整數聚合器與列舉器 \#

### F.18. intagg — 整數聚合器與列舉器 [#](#intagg) <a href="#intagg" id="intagg"></a>

[F.18.1. 函式](#intagg-functions)

[F.18.2. 使用範例](#intagg-samples)

`intagg` 模組提供整數聚合器與列舉器。`intagg` 現已過時，因為內建函式提供了其功能的超集。不過，此模組仍作為內建函式的相容性包裝器提供。

#### F.18.1. 函式 [#](#intagg-functions) <a href="#intagg-functions" id="intagg-functions"></a>

聚合器是聚合函式 `int_array_aggregate(integer)`，會產生恰好包含其輸入整數的整數陣列。它是 `array_agg` 的包裝器；後者可對任何陣列型別執行相同工作。

列舉器是函式 `int_array_enum(integer[])`，會傳回 `setof integer`。它本質上是聚合器的反向操作：指定一個整數陣列，將其展開為一組資料列。它是 `unnest` 的包裝器；後者可對任何陣列型別執行相同工作。

#### F.18.2. 使用範例 [#](#intagg-samples) <a href="#intagg-samples" id="intagg-samples"></a>

許多資料庫系統具有多對多資料表的概念。這類資料表通常位於兩個已建立索引的資料表之間，例如：

```

CREATE TABLE left_table  (id INT PRIMARY KEY, ...);
CREATE TABLE right_table (id INT PRIMARY KEY, ...);
CREATE TABLE many_to_many(id_left  INT REFERENCES left_table,
                          id_right INT REFERENCES right_table);
```

通常會如下使用：

```

SELECT right_table.*
FROM right_table JOIN many_to_many ON (right_table.id = many_to_many.id_right)
WHERE many_to_many.id_left = item;
```

這會傳回左側資料表中某個項目所對應的右側資料表全部項目。這是 SQL 中非常常見的結構。

現在，若 `many_to_many` 資料表的項目數量非常多，此方法可能很繁瑣。這類連接通常會對特定左側項目的資料表中每個右側項目進行索引掃描與擷取。若系統變動非常頻繁，可改善的空間不大。不過，若部分資料相當靜態，可使用聚合器建立摘要資料表。

```

CREATE TABLE summary AS
  SELECT id_left, int_array_aggregate(id_right) AS rights
  FROM many_to_many
  GROUP BY id_left;
```

這會建立一個資料表，每個左側項目各有一個資料列，以及一個右側項目陣列。若沒有使用該陣列的方法，這就沒有太大用處；因此才有陣列列舉器。您可以執行：

```

SELECT id_left, int_array_enum(rights) FROM summary WHERE id_left = item;
```

以上使用 `int_array_enum` 的查詢會產生與下列查詢相同的結果：

```

SELECT id_left, id_right FROM many_to_many WHERE id_left = item;
```

差異在於針對摘要資料表的查詢只需從資料表取得一個資料列，而直接查詢 `many_to_many` 必須為每個項目進行索引掃描並擷取一個資料列。

在某個系統上，`EXPLAIN` 顯示一個成本為 8488 的查詢降至成本 329。原始查詢是涉及 `many_to_many` 資料表的連接，改為下列查詢：

```

SELECT id_right, count(id_right) FROM
  ( SELECT id_left, int_array_enum(rights) AS id_right
    FROM summary
    JOIN (SELECT id FROM left_table
          WHERE id = item) AS lefts
    ON (summary.id_left = lefts.id)
  ) AS list
  GROUP BY id_right
  ORDER BY count DESC;
```

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/intagg.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/intagg.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.
