【openGauss/MogDB】一种对数据库的列表型参数进行设置的方式

背景

在openGauss/MogDB数据库中,有些数据库参数是列表形式(比如behavior_compat_options、sql_beta_feature、rewrite_rule、plsql_compile_check_options等),如果想对这个列表增加删除值时,需要先查出当前值,然后修改当前值的字符串,再set上去,操作比较麻烦。
本文通过创建一组自定义函数,来对这样的列表型参数进行更简单的设置

函数定义

create function current_setting_as_table (text) returns table(value text)
as $$
select unnest(string_to_array( current_setting($1) ,','));
$$language sql;

create or replace function set_config_add_value (text, text, bool)  returns setOF TEXT
as $$
declare
begin
if not $2=any(string_to_array( current_setting($1) ,',')) then
set_config($1,current_setting($1)||','||$2,$3);
end if;
return query SELECT unnest(string_to_array( current_setting($1) ,','));
end;
$$language plpgsql;

create or replace function set_config_del_value (text, text, bool) returns setOF TEXT
as $$
declare
begin
if  $2=any(string_to_array( current_setting($1) ,',')) then
set_config($1,array_to_string(array_remove(string_to_array( current_setting($1) ,','),$2),','),$3);
end if;
return query SELECT unnest(string_to_array( current_setting($1) ,','));
end;
$$language plpgsql;

使用示例

1.以表格方式查看指定参数的列表值

MogDB=> select * from current_setting_as_table('behavior_compat_options');
              value
---------------------------------
 convert_string_digit_to_numeric
 char_coerce_compat
 proc_outparam_override
 proc_implicit_for_loop_variable
 plpgsql_dependency
 allow_procedure_compile_check
 plstmt_implicit_savepoint
 compat_cursor
 aformat_null_test
 plsql_security_definer
 skip_insert_gs_source
 truncate_numeric_tail_zero
(12 rows)

MogDB=>

2.在指定参数的列表中新增一个值

MogDB=> select set_config_add_value('behavior_compat_options','end_month_calculate',false);
      set_config_add_value
---------------------------------
 convert_string_digit_to_numeric
 char_coerce_compat
 proc_outparam_override
 proc_implicit_for_loop_variable
 plpgsql_dependency
 allow_procedure_compile_check
 plstmt_implicit_savepoint
 compat_cursor
 aformat_null_test
 plsql_security_definer
 skip_insert_gs_source
 truncate_numeric_tail_zero
 end_month_calculate
(13 rows)

MogDB=>

3.在指定参数的列表中移除一个值

MogDB=> select set_config_del_value('behavior_compat_options','end_month_calculate',false);
      set_config_del_value
---------------------------------
 convert_string_digit_to_numeric
 char_coerce_compat
 proc_outparam_override
 proc_implicit_for_loop_variable
 plpgsql_dependency
 allow_procedure_compile_check
 plstmt_implicit_savepoint
 compat_cursor
 aformat_null_test
 plsql_security_definer
 skip_insert_gs_source
 truncate_numeric_tail_zero
(12 rows)

MogDB=>

用到的一些语法或者内置函数说明

  1. returns table/setof 返回表、集合
  2. language sql/plpgsql 函数语言使用sql还是plpgsql,如果是sql语言就只需要select,如果是plpgsql则需要有begin end;
  3. string_to_array函数,将字符串转换成数组类型,第一个参数为原始字符串,第二个参数为分隔符
  4. unnest函数,将一个数组展开成一个表(多行数据)
  5. current_setting函数,指定参数名称获取参数值,和show的结果一样,但更加适用于在sql/plsql中使用
  6. set_config函数,设置某个参数的值。当第三个参数为true时,仅作用于当前事务;当第三个参数为false时,和set作用一样。也是适用于在sql/plsql中使用
  7. text=any(array) ,当某个值存在于数组中,返回true,否则返回false
  8. return query,当返回setof时,可以使用return next 返回单行,或者使用return query 返回一个查询的结果集(多行)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DarkAthena

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值