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

# F.38.2. 函式

## F.38.2. 函式 [#](#postgres-fdw-functions) <a href="#postgres-fdw-functions" id="postgres-fdw-functions"></a>

`postgres_fdw_get_connections( IN check_conn boolean DEFAULT false, OUT server_name text, OUT user_name text, OUT valid boolean, OUT used_in_xact boolean, OUT closed boolean, OUT remote_backend_pid int4) returns setof record`

此函式傳回 postgres\_fdw 從本機工作階段與外部伺服器建立的所有開啟中連線的資訊。若沒有開啟中的連線，則不傳回任何紀錄。

若 `check_conn` 設為 `true`， 此函式會檢查每個連線的狀態，並將結果顯示在 `closed` 欄位中。 此功能目前只在支援 `poll` 系統呼叫之非標準 `POLLRDHUP` 擴充的系統（包括 Linux）上可用。 這對於檢查交易中使用的所有連線是否仍然開啟很有用。若有任何連線已關閉，交易就無法成功提交， 因此最好在偵測到已關閉的連線時就立即回復，而不是繼續執行到最後。若此函式回報有連線的 `used_in_xact` 與 `closed` 都是 `true`，使用者可以立即回復交易。

此函式的使用範例：

```

    postgres=# SELECT * FROM postgres_fdw_get_connections(true);
     server_name | user_name | valid | used_in_xact | closed | remote_backend_pid
    -------------+-----------+-------+--------------+-----------------------------
     loopback1   | postgres  | t     | t            | f      |            1353340
     loopback2   | public    | t     | t            | f      |            1353120
     loopback3   |           | f     | t            | f      |            1353156
```

輸出欄位說明列於 [表 F.28](#postgres-fdw-get-connections-columns)。

### 表 F.28. `postgres_fdw_get_connections` 輸出欄位 [#](#postgres-fdw-get-connections-columns) <a href="#postgres-fdw-get-connections-columns" id="postgres-fdw-get-connections-columns"></a>

| 欄位                   | 型別        | 說明                                                                                          |
| -------------------- | --------- | ------------------------------------------------------------------------------------------- |
| `server_name`        | `text`    | 此連線的外部伺服器名稱。若伺服器已被刪除但連線仍保持開啟（亦即被標記為無效），此值將為 `NULL`。                                         |
| `user_name`          | `text`    | 對應到此連線之外部伺服器的本機使用者名稱；若使用的是 public 對應，則為 `public`。 若使用者對應已被刪除但連線仍保持開啟（亦即被標記為無效），此值將為 `NULL`。 |
| `valid`              | `boolean` | 若此連線無效則為 false；無效表示此連線在目前交易中被使用，但其外部伺服器或使用者對應已被變更或刪除。 無效的連線會在交易結束時關閉。否則傳回 true。             |
| `used_in_xact`       | `boolean` | 若此連線在目前交易中被使用則為 true。                                                                       |
| `closed`             | `boolean` | 若此連線已關閉則為 true，否則為 false。 `NULL` 會在 `check_conn` 設為 `false`，或此平台上無法使用連線狀態檢查時傳回。             |
| `remote_backend_pid` | `int4`    | 外部伺服器上處理此連線之遠端後端的程序 ID。若遠端後端已終止且連線已關閉（`closed` 設為 `true`），此欄位仍會顯示已終止後端的程序 ID。               |

<br>

`postgres_fdw_disconnect(server_name text) returns boolean`

此函式會捨棄 `postgres_fdw` 從本機工作階段與指定名稱之外部伺服器建立的開啟中連線。請注意，使用不同的使用者對應，可能會有多個連到指定伺服器的連線。 若這些連線在目前的本機交易中被使用，則不會中斷它們，並會回報警告訊息。 若此函式中斷了至少一個連線，則傳回 `true`，否則傳回 `false`。 若找不到指定名稱的外部伺服器，則會回報錯誤。 此函式的使用範例：

```

    postgres=# SELECT postgres_fdw_disconnect('loopback1');
     postgres_fdw_disconnect
    -------------------------
     t
```

`postgres_fdw_disconnect_all() returns boolean`

此函式會捨棄 `postgres_fdw` 從本機工作階段與外部伺服器建立的所有開啟中連線。若這些連線在目前的本機交易中被使用，則不會中斷它們，並會回報警告訊息。 若此函式中斷了至少一個連線，則傳回 `true`，否則傳回 `false`。 此函式的使用範例：

```

    postgres=# SELECT postgres_fdw_disconnect_all();
     postgres_fdw_disconnect_all
    -----------------------------
     t
```

[返回 F.38. postgres\_fdw — 存取儲存在外部 PostgreSQL 伺服器中的資料](https://docs.postgresql.tw/19/appendixes/contrib/postgres-fdw.md#postgres-fdw)

***

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