# 会话函数

> 查询连接身份、当前数据库、命名锁及语句状态。

---

LLMS 索引： [llms.txt](/zh/llms.txt)

---

查询连接身份、当前数据库、命名锁及语句状态。

## connection_id {#connection_id}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-connection-id-02cf0605" href="#mysql-connection-id-02cf0605">`mysql.connection_id() → bigint`</a> | 返回本连接的 PostgreSQL 后端进程号。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L130-L131) |

## current_user {#current_user}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-current-user-ebe52fe9" href="#mysql-current-user-ebe52fe9">`mysql."current_user"() → text`</a> | 将当前有效 PostgreSQL 角色显示为 role@%。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L120-L123) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mysm/user.c#L22-L38) |

## database {#database}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-database-f727e295" href="#mysql-database-f727e295">`mysql.database() → text`</a> | 返回本会话选中的 MySQL 数据库。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L63-L66) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mys_compat.c#L110-L133) |

## found_rows {#found_rows}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-found-rows-82106f3b" href="#mysql-found-rows-82106f3b">`mysql.found_rows() → bigint`</a> | 读取本会话记录的匹配行数。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L105-L108) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mysm/strfuncs.c#L1355-L1359) |

## get_lock {#get_lock}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-get-lock-pg-catalog-any-pg-catalog-any-af36de9b" href="#mysql-get-lock-pg-catalog-any-pg-catalog-any-af36de9b">`mysql.get_lock("any", "any") → bigint`</a> | 在超时时限内获取指定名称的会话锁。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L30-L33) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mys_lock.c#L495-L519) |

## is_free_lock {#is_free_lock}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-is-free-lock-pg-catalog-any-ab3f9f21" href="#mysql-is-free-lock-pg-catalog-any-ab3f9f21">`mysql.is_free_lock("any") → bigint`</a> | 检查指定名称的锁是否空闲。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L40-L43) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mys_lock.c#L548-L559) |

## is_used_lock {#is_used_lock}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-is-used-lock-pg-catalog-any-4cb80c4b" href="#mysql-is-used-lock-pg-catalog-any-4cb80c4b">`mysql.is_used_lock("any") → bigint`</a> | 查询指定名称的锁由哪个连接持有。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L45-L48) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mys_lock.c#L562-L577) |

## last_insert_id {#last_insert_id}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-last-insert-id-52a135f5" href="#mysql-last-insert-id-52a135f5">`mysql.last_insert_id() → bigint`</a> | 读取本会话最近生成的插入 ID。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L110-L113) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mysm/strfuncs.c#L1362-L1366) |

## release_all_locks {#release_all_locks}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-release-all-locks-2039902e" href="#mysql-release-all-locks-2039902e">`mysql.release_all_locks() → bigint`</a> | 释放本会话持有的所有命名锁。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L50-L53) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mys_lock.c#L607-L611) |

## release_lock {#release_lock}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-release-lock-pg-catalog-any-188c9cdf" href="#mysql-release-lock-pg-catalog-any-188c9cdf">`mysql.release_lock("any") → bigint`</a> | 释放指定名称的会话锁。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L35-L38) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mys_lock.c#L525-L545) |

## row_count {#row_count}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-row-count-d2566d6f" href="#mysql-row-count-d2566d6f">`mysql.row_count() → bigint`</a> | 读取上一条语句影响的行数。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L100-L103) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mysm/strfuncs.c#L1348-L1352) |

## schema {#schema}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-schema-fd365ee7" href="#mysql-schema-fd365ee7">`mysql.schema() → text`</a> | 返回本会话选中的 MySQL 数据库。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L68-L71) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mys_compat.c#L110-L133) |

## session_user {#session_user}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-session-user-59992dbc" href="#mysql-session-user-59992dbc">`mysql."session_user"() → text`</a> | 返回会话角色和客户端主机名。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L125-L128) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mysm/user.c#L41-L59) |

## sleep {#sleep}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-sleep-numeric-88311da1" href="#mysql-sleep-numeric-88311da1">`mysql.sleep(seconds numeric) → character varying`</a> | 等待指定的秒数。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L6-L17) |

## user {#user}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-user-38884724" href="#mysql-user-38884724">`mysql."user"() → text`</a> | 以文本返回 PostgreSQL 的 current_user。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L55-L61) |

## uuid {#uuid}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-uuid-c9833996" href="#mysql-uuid-c9833996">`mysql.uuid() → text`</a> | 生成 UUID。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L19-L27) |

## uuid_short {#uuid_short}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-uuid-short-4c578abe" href="#mysql-uuid-short-4c578abe">`mysql.uuid_short() → text`</a> | 生成 MySQL 短格式标识符。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L115-L118) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mysm/uuid_short.c#L21-L29) |

## version {#version}

| 对象签名 | 用途 | 源码定义 |
|---|---|---|
| <a id="mysql-version-4bb1f911" href="#mysql-version-4bb1f911">`mysql.version() → text`</a> | 返回对 MySQL 客户端通告的服务器版本。 | [SQL](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/sql/baseline/46-func-sess.sql#L73-L76) · [C](https://github.com/pgsty/pg_mysql/blob/0d51adcf5151ca5c98bc61de84f287ec3b4fe4a3/src/mysm/strfuncs.c#L1434-L1440) |

---

反链：

- [函数](/zh/docs/mysql/reference/objects/functions/)
