---
title: "Server Connection Management"
description: "Managing MotherDuck server connections"
---

# Server Connection Management

Managing MotherDuck server connections

## Included pages

- [Monitoring active server connections](https://motherduck.com/docs/sql-reference/motherduck-sql-reference/connection-management/monitor-connections): View active connections and running queries in MotherDuck using md_active_connections.
- [Interrupting active server connections](https://motherduck.com/docs/sql-reference/motherduck-sql-reference/connection-management/interrupt-connections): Cancel running queries and terminate active connections to MotherDuck using SQL commands.
- [Identify client connection and DuckDB ID](https://motherduck.com/docs/sql-reference/motherduck-sql-reference/connection-management/connection-duckdb-id): Retrieve connection identifiers and DuckDB instance IDs for debugging and monitoring.
- [Retrieve the last query ID](https://motherduck.com/docs/sql-reference/motherduck-sql-reference/connection-management/last-query-id): Use MD_LAST_QUERY_ID() to get the UUID of the most recent query sent to MotherDuck.

Source: https://motherduck.com/docs/category/server-connection-management

---

## Monitoring active server connections

Source: https://motherduck.com/docs/sql-reference/motherduck-sql-reference/connection-management/monitor-connections

> View active connections and running queries in MotherDuck using md_active_connections.

:::info
This is a preview feature. Preview features may be operationally incomplete and may offer limited backward compatibility.
:::

# Monitoring active server connections

:::tip
You can also monitor running queries in the MotherDuck UI under **Settings** → **Running Queries**. See [Running Queries](/getting-started/interfaces/motherduck-quick-tour/#running-queries) for details.

For an org-wide view of running and recently completed queries, see the [`RECENT_QUERIES`](/sql-reference/motherduck-sql-reference/md_information_schema/recent_queries/) view (for admins on the Business plan).
:::

The `md_active_server_connections` table function can be used to list all server-side connections that have active transactions.

## Syntax

```sql
FROM md_active_server_connections();
```

This returns a list of active server connections, with the following information:

| **column_name**                     | **column_type** | **description**                                                                  |
|-------------------------------------|-----------------|----------------------------------------------------------------------------------|
| client_duckdb_id                    | UUID            | Unique identifier for the client DuckDB instance that initiated the connection   |
| client_user_agent                   | VARCHAR         | User agent for the client                                                        |
| client_duckdb_version               | USMALLINT[3]    | DuckDB version from the client                                                   |
| client_connection_id                | UUID            | Unique identifier for the client DuckDB connection that initiated the connection |
| client_transaction_id               | UBIGINT         | Identifier for the transaction within the current connection                     |
| server_transaction_stage            | VARCHAR         | Stage the server-side transaction is in                                          |
| server_transaction_elapsed_time     | INTERVAL        | How long the server-side transaction has been in the current stage               |
| client_query_id                     | UBIGINT         | Identifier for the query within the current transaction                          |
| client_query                        | VARCHAR         | Query string (possibly truncated)                                                |
| server_query_elapsed_time           | INTERVAL        | How long the query has been running on the server-side                           |
| server_query_execution_elapsed_time | INTERVAL        | How long the connection has been interrupted                                     |
| server_query_progress               | DOUBLE          | Progress information (value between 0.0 and 1.0)                                 |
| server_interrupt_elapsed_time       | INTERVAL        | How long the connection has been interrupted                                     |
| server_interrupt_reason             | VARCHAR         | Why the connection was interrupted                                               |
| query_total_upload_size             | UBIGINT         | Data uploaded in Bytes                                                           |
| query_total_download_size           | UBIGINT         | Data downloaded in Bytes                                                         |

---

## Interrupting active server connections

Source: https://motherduck.com/docs/sql-reference/motherduck-sql-reference/connection-management/interrupt-connections

> Cancel running queries and terminate active connections to MotherDuck using SQL commands.

:::info
This is a preview feature. Preview features may be operationally incomplete and may offer limited backward compatibility.
:::

# Interrupting active server connections

:::tip
You can also cancel running queries in the MotherDuck UI under **Settings** → **Running Queries**. See [Running Queries](/getting-started/interfaces/motherduck-quick-tour/#running-queries) for details.

To identify long-running queries across your organization, see the [`RECENT_QUERIES`](/sql-reference/motherduck-sql-reference/md_information_schema/recent_queries/) view (for admins on the Business plan).
:::

The `md_interrupt_server_connection` scalar function can be used to interrupt an active transaction on a server-side connection.
This will interrupt and fail / rollback the active transaction (when executing for example a long-running query), but will allow the connection to be used for future transactions and queries.
The function takes as input the `client_connection_id`, i.e. the unique identifier for the client DuckDB connection that initiated the server connection.

## Syntax

```sql
SELECT md_interrupt_server_connection(<client_connection_id>);
```

## Return value

Returns a `BOOLEAN`:

- `true` — the interrupt signal was successfully sent to the connection
- `false` — the interrupt could not be sent, which can happen if: the connection is already closed, the transaction is committing or rolling back, or the server-side connection could not be found

## Example usage

Interrupting a specific connection:

```sql
SELECT md_interrupt_server_connection('2601e799-51b3-47a7-a64f-18688d148887');
```

Using `md_interrupt_server_connection` in conjunction with [`md_active_server_connections`](documentation/sql-reference/motherduck-sql-reference/connection-management/monitor-connections.md) to interrupt a subset or all of the currently active connections:

```sql
-- Interrupt all connections where a `CREATE TABLE` query is running
SELECT md_interrupt_server_connection(client_connection_id)
FROM md_active_server_connections()
WHERE client_query ILIKE 'CREATE TABLE%';
```

The query returns one boolean per matched connection indicating whether each interrupt succeeded. If 0 rows are returned, no active connections matched the filter — the query may have already finished.

---

## Identify client connection and DuckDB ID

Source: https://motherduck.com/docs/sql-reference/motherduck-sql-reference/connection-management/connection-duckdb-id

> Retrieve connection identifiers and DuckDB instance IDs for debugging and monitoring.

:::info
This is a preview feature. Preview features may be operationally incomplete and may offer limited backward compatibility.
:::

# Identify client connection and DuckDB ID

`md_current_client_connection_id` and `md_current_client_duckdb_id` are two scalar functions that can be used to identify the current `client_connection_id` and `client_duckdb_id`.

## Syntax

```sql
SELECT md_current_client_connection_id();
SELECT md_current_client_duckdb_id();
```

## Example usage
To [interrupt](documentation/sql-reference/motherduck-sql-reference/connection-management/interrupt-connections.md) all server-side connections that are initiated by the current client DuckDB instance, we can use:

```sql
SELECT md_interrupt_server_connection(client_connection_id)
FROM md_active_server_connections()
WHERE client_duckdb_id = md_current_client_duckdb_id()
  AND client_connection_id != md_current_client_connection_id();
```

---

## Retrieve the last query ID

Source: https://motherduck.com/docs/sql-reference/motherduck-sql-reference/connection-management/last-query-id

> Use MD_LAST_QUERY_ID() to get the UUID of the most recent query sent to MotherDuck.

# Retrieve the last query ID

`MD_LAST_QUERY_ID()` is a scalar function that returns the UUID of the most recent query sent to the MotherDuck server from the current DuckDB [connection](/key-tasks/authenticating-and-connecting-to-motherduck/connecting-to-motherduck/#create-a-connection). This is the same `QUERY_ID` that appears in the [`QUERY_HISTORY`](../md_information_schema/query_history.md) and [`RECENT_QUERIES`](../md_information_schema/recent_queries.md) views.

The function returns `NULL` if no query has been sent to the server yet on the current connection.

## Syntax

```sql
SELECT MD_LAST_QUERY_ID();
```

## Return type

`UUID`: the unique identifier of the last query, or `NULL` if no server-side query has been run.

## Example usage

### Get the ID of your last query

```sql
SELECT MD_LAST_QUERY_ID();
```

### Look up details for last query from this connection in the query history

```sql
SELECT *
FROM MD_INFORMATION_SCHEMA.QUERY_HISTORY
WHERE QUERY_ID = MD_LAST_QUERY_ID();
```

---
