set_config() 是一个用于临时设置 配置参数的系统函数。
set_config() 在 PostgreSQL 7.3 中被添加。
用法
set_config (setting_nametext,new_valuetext,is_localboolean ) → text
set_config() 临时将参数 setting_name 设置为 new_value,并返回 new_value。它在功能上等同于 SET 命令。
必须指定参数 is_local,它具有以下作用之一:
- 如果为
FALSE,则更改的设置将应用于当前数据库会话。 - 如果为
TRUE,则更改的设置将仅应用于当前事务。
要永久修改 配置参数,请使用 ALTER SYSTEM。
变更历史
- PostgreSQL 7.3
- 添加于 (commit 1ce03603)
示例
将 client_min_messages 设置为 log 以应用于当前会话。
postgres=# SELECT set_config('client_min_messages', 'log', FALSE);
set_config
------------
log
(1 row)
postgres=# SHOW client_min_messages;
LOG: statement: SHOW client_min_messages;
client_min_messages
---------------------
log
(1 row)
开始一个新的会话将重置该设置。
postgres=# \c - postgres You are now connected to database "postgres" as user "postgres". postgres=# SHOW client_min_messages; client_min_messages --------------------- notice (1 row)
将 client_min_messages 设置为 log 以应用于当前事务。
postgres=# BEGIN;
BEGIN
postgres=*# SELECT set_config('client_min_messages', 'log', TRUE);
set_config
------------
log
(1 row)
postgres=*# SHOW client_min_messages;
LOG: statement: SHOW client_min_messages;
client_min_messages
---------------------
log
(1 row)
事务完成后,将恢复原始值。
postgres=*# ROLLBACK ; LOG: statement: ROLLBACK ; ROLLBACK postgres=# SHOW client_min_messages; client_min_messages --------------------- notice (1 row)
尝试为配置参数设置无效值。
postgres=# SELECT set_config('client_min_messages', 'foo', TRUE);
ERROR: invalid value for parameter "client_min_messages": "foo"
HINT: Available values: debug5, debug4, debug3, debug2, debug1, log, notice, warning, error.
尝试设置一个无效的配置参数。
postgres=# SELECT set_config('foo', 'bar', TRUE);
ERROR: unrecognized configuration parameter "foo"
参考资料
- PostgreSQL 文档: 配置设置函数
