set_config()

一个用于临时设置配置参数的函数

set_config() 是一个用于临时设置 配置参数的系统函数。

set_config()PostgreSQL 7.3 中被添加。

用法

set_config ( setting_name text, new_value text, is_local boolean ) → text

set_config() 临时将参数 setting_name 设置为 new_value,并返回 new_value。它在功能上等同于 SET 命令。

必须指定参数 is_local,它具有以下作用之一:

  • 如果为 FALSE,则更改的设置将应用于当前数据库会话。
  • 如果为 TRUE,则更改的设置将仅应用于当前事务。

要永久修改 配置参数,请使用 ALTER SYSTEM

变更历史

示例

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"

分类

配置, 系统函数

另请参阅

SET, current_setting(), ALTER SYSTEM

反馈

在此处提交关于“set_config()”的任何评论、建议或更正。 here