postgresql - 为 PL/pgSQL 中实现的函数设置配置参数

标签 postgresql plpgsql

我已经在 PL/pgSQL 中编写了几个函数,我想通过一些配置条目来控制它们的行为,这些条目也可以在运行时更改(每个 session )。是否可以在 postgresql.conf 中定义新的自定义配置条目?如果不是,解决方法是什么?

作为我的搜索结果,我遇到了 part of documentation其中说:

18.16. Customized Options

This feature was designed to allow parameters not normally known to PostgreSQL to be added by add-on modules (such as procedural languages). This allows extension modules to be configured in the standard ways.

如果这篇文章用“否”回答我的问题,我的 PL/pgSQL 函数是否可以被视为一个扩展模块,以便它们可以在配置文件中拥有自己的配置条目?

最佳答案

您可以在 postgresql.conf 中定义您的自定义参数。只需附加一行(例如):

my_param.new_param = 'something'

并重新加载配置(或重启服务器)。

在您的客户端中,您可以使用show 命令访问参数:

SHOW my_param.new_param;

或使用current_setting()函数:

SELECT current_setting('my_param.new_param');

您可以更改当前参数(在本地 session 中):

SET my_param.new_param TO 'new value';

也可以为数据库定义自定义参数:

ALTER DATABASE test SET my_param.new_param TO 'new test value';
-- each new client to the database will see the parameter with new value
-- current setting of the parameter remains unchanged

-- or
SET my_param.new_param TO 'new test value';
ALTER DATABASE test SET my_param.new_param FROM CURRENT;

自定义参数必须包含句点。形式上,前缀应该指示它相关的扩展,但 Postgres 不以任何方式检查它。您可以有许多自定义参数。

关于postgresql - 为 PL/pgSQL 中实现的函数设置配置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34476062/

相关文章:

python - 如何使用 SQLAlchemy ORM 指定 PostgreSQL DateStyle 属性

postgresql - 在 PostgreSQL WHERE 子句中,如何查找 ID 不在数组中的所有行?

postgresql - 如何编写返回文本或整数值的函数?

performance - "LIMIT 1"会提高 plpgsql 中 SELECT INTO 查询的性能吗

mysql - 从不同数据库的两个表中查询数据

mysql - Elasticsearch + 还有什么?

postgresql - 在postgres中获取月份的第一个日期

json - Postgres 函数以 JSON 形式更新后显示新列值

postgresql - 如何在 PostgreSQL 中创建 guid

sql - 分组要求额外的列