Setting a configuration parameter for functions implemented in PL/pgSQL

限于喜欢 提交于 2019-12-01 21:14:02

问题


I've written a couple of functions in PL/pgSQL and I would like to control their behavior through some configuration entries, changeable at run time too (per session). Is it possible to define new custom-defined configuration entries in postgresql.conf? If not, what's the work around?

As my search results, I came across the part of documentation which says:

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.

If this passage answers my question with a "No", can my PL/pgSQL functions be considered an extension module so that they can have their own configuration entries in the configuration file?


回答1:


You can define your custom parameters in postgresql.conf. Just append a line (e.g.):

my_param.new_param = 'something'

and reload configuration (or restart server).

In your client you can access the parameter with show command:

SHOW my_param.new_param;

or with current_setting() function:

SELECT current_setting('my_param.new_param');

You can change the current parameter (locally in the session):

SET my_param.new_param TO 'new value';

It is also possible to define custom parameters for a database:

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;

A custom parameter must contain a period. Formally, the prefix should indicate the extension to which it relates, but Postgres does not check it in any way. You can have many custom parameters.



来源:https://stackoverflow.com/questions/34476062/setting-a-configuration-parameter-for-functions-implemented-in-pl-pgsql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!